Skip to content

Commit

Permalink
Python3 fixes. Add zsh completion. Refer to successor: replacer
Browse files Browse the repository at this point in the history
  • Loading branch information
vaeth committed Apr 7, 2018
1 parent 7f5e47b commit fb6d19b
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 9 deletions.
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,23 @@ Search and/or replace regular expressions within many files interactively.
(C) Martin Väth (martin at mvath.de).
This project is under the BSD license.

There is a successor project
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.

`pyrep` (*Py*thon g*rep*) is a python script with which you can search
and also replace regular expressions in a collection of files.
Its functionality is similar to the perl script `plrep` from
https://github.com/vaeth/mv_perl/

To install this script, just copy it into your `$PATH`.
To install this script, just copy the content of bin/ into your `$PATH`.
To get zsh completion, copy the content of zsh/ to your zsh's `$fpath`,
perhaps `/usr/share/zsh/site-functions/`.

For Gentoo, there is an ebuild in the mv overlay (available over layman).
For Gentoo, there is an ebuild in the `mv` repository
(available by `app-select/eselect-repository` or `app-portage/layman`).
34 changes: 27 additions & 7 deletions pyrep → bin/pyrep
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
#!/usr/bin/env python
# (C) Martin V\"ath <martin@mvath.de>

# 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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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.'''
Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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):
Expand Down Expand Up @@ -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
Expand Down
25 changes: 25 additions & 0 deletions zsh/_pyrep
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'

0 comments on commit fb6d19b

Please sign in to comment.