-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Python3 fixes. Add zsh completion. Refer to successor: replacer
- Loading branch information
Showing
3 changed files
with
66 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,15 @@ | ||
#!/usr/bin/env python | ||
# (C) Martin V\"ath <[email protected]> | ||
|
||
# There is a successor project written in modern python style: | ||
# https://github.com/vaeth/replacer | ||
# Use that instead since it is better in almost every aspect: | ||
# | ||
# When I wrote this code many years ago, python has just been released. | ||
# There was no python style recommendation out there (or I was not aware of). | ||
# From current viewpoint the code reads rather ugly and hardly pythonic. | ||
# However, I left it unchanged except for some python2/3 compatibility quirks. | ||
|
||
import sys,getopt,os.path | ||
import re,string | ||
from glob import fnmatch | ||
|
@@ -330,7 +340,7 @@ class ReplacePattern: | |
pos=newpos | ||
append(Mgroup(group)) | ||
append(String[pos:]) | ||
return string.join(RString,'') | ||
return ''.join(RString) | ||
|
||
if not GrepOnly: | ||
if PlainReplace: | ||
|
@@ -409,12 +419,22 @@ def ListOfMatches(Firstmatch,SearchPattern,String): | |
MatchList.append(Match) | ||
return MatchList | ||
|
||
def Input(String): | ||
'''raw_input or input''' | ||
try: | ||
try: | ||
return raw_input(String) | ||
except NameError: | ||
return input(String) | ||
except KeyboardInterrupt: | ||
exit(130) | ||
|
||
def DivideString(String,ListOfMatches): | ||
'''Separates the String at the begin/end of the Matches. | ||
The matches must be nonoverlapping and sorted from left to right. | ||
The return value is a list of string which gives the original string after | ||
string.join(returnvalue,''). More precisely, the list ist | ||
''.join(returnvalue). More precisely, the list ist | ||
[Content until Begin Match1, Content of Match1, \\ | ||
Content of End Match1 until Begin Match2, Content of Match2, ...]. | ||
It may happen that some of the above strings are empty.''' | ||
|
@@ -440,7 +460,7 @@ _BackSpacePattern=re.compile("[\\b]|(\33\\[3\\~)") | |
def ReplaceAsk(): | ||
global AlwaysReplace,ReplaceRest,AlwaysWrite,AskForReplace | ||
while 1: | ||
answer=raw_input(_ReplaceAskString) | ||
answer=Input(_ReplaceAskString) | ||
if _BackSpacePattern.search(answer): continue | ||
elif _ReplaceStopPattern.match(answer): sys.exit(0) | ||
elif _ReplaceYesPattern.match(answer): return 1 | ||
|
@@ -601,7 +621,7 @@ def ChangeString(Lines,LineNumber,ListOfMatches): | |
append(String[pos:start]) | ||
pos=end=match.end() | ||
if ShowChanges: | ||
PrintLine([string.join(NewString,''),String[start:end],String[end:]], \ | ||
PrintLine([''.join(NewString),String[start:end],String[end:]], \ | ||
LineNumber,1) | ||
HavePrintedAfter=None | ||
if After>0: | ||
|
@@ -620,7 +640,7 @@ def ChangeString(Lines,LineNumber,ListOfMatches): | |
subs=ReplaceObject.ReplaceString(match) | ||
if ShowChanges: | ||
if HavePrintedAfter: print("---") | ||
PrintLine([string.join(NewString,''),subs,String[end:]],LineNumber,-1) | ||
PrintLine([''.join(NewString),subs,String[end:]],LineNumber,-1) | ||
if AskForReplace: | ||
DoReplace=ReplaceAsk() | ||
if DoReplace: | ||
|
@@ -631,7 +651,7 @@ def ChangeString(Lines,LineNumber,ListOfMatches): | |
if AskForReplace<0: break | ||
append(String[pos:]) | ||
if Changed: | ||
Lines[LineNumber]=string.join(NewString,'') | ||
Lines[LineNumber]=''.join(NewString) | ||
return Changed | ||
|
||
def ProcessNormal(File): | ||
|
@@ -693,7 +713,7 @@ _WriteStopPattern=re.compile("s$",re.IGNORECASE) | |
def WriteAsk(Filename): | ||
global AlwaysWrite | ||
while 1: | ||
answer=raw_input( \ | ||
answer=Input( \ | ||
"Write into File %s? y(es)/n(o)/w(rite always)/s(top all) " % Filename) | ||
if _BackSpacePattern.search(answer): continue | ||
elif _WriteYesPattern.match(answer): return 1 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#compdef pyrep | ||
_arguments -s -S : \ | ||
'(1 2 * -)-h[print help]' \ | ||
'(1 2 * -)-e+[print help on specified topic]:help topic:((s1\:"help on search expression, part 1" s2\:"help on search expression, part 2" s\:"help on search expression" r\:"help on replace expression"))' \ | ||
'(-y)-Y[assume yes and be quiet]' \ | ||
'-y[assume yes except on filechanges]' \ | ||
'-v[verbose (after -Y)]' \ | ||
'-i[ignore case]' \ | ||
'-b[open in binary mode]' \ | ||
'-p[use plain string for search]' \ | ||
'-P[use plain string for replace]' \ | ||
'-H[do not highlight matches]' \ | ||
'-r+[recurse from DIR]:recurse-dir:_files -/' \ | ||
'-f+[read strings from file]:from-file:_files' \ | ||
'-C+[print specified number of lines around match]:number of lines around match:(2)' \ | ||
'-B+[print specified number of lines before match]:number of lines before match:(2)' \ | ||
'-A+[print specified number of lines after match]:number of lines after match:(2)' \ | ||
'(2)-g[grep mode]' \ | ||
'(2)-c[grep mode, count only]' \ | ||
'(2)-l[grep mode, list only]' \ | ||
'-t+[truncate to specified number of symbols]:truncate-number:(+78)' \ | ||
'-T+[show at least specified number of symbols from s/e]:number of symbols to show from s/e:(s40 e40)' \ | ||
'1:search:()' \ | ||
'2:replace:()' \ | ||
'*:files:_files' |