Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

PATCH: replace-string enhancement



This allows the replace-string widget to offer you the previous strings
used for editing.  This is off by default.  There's a style to turn
it on, plus you can use numeric arguments.  (I changed my mind about the
default at the last minute.)

I had a brief go at trying to maintain a separate history for the
strings, but it seems to be rather tricky since replace-string works by
manipulating the current history line.  Hence the usual assumptions
about adding new lines are invalid.  So I haven't changed it.

Index: Functions/Zle/replace-string
===================================================================
RCS file: /cvsroot/zsh/zsh/Functions/Zle/replace-string,v
retrieving revision 1.1
diff -u -r1.1 replace-string
--- Functions/Zle/replace-string	3 Feb 2003 11:06:03 -0000	1.1
+++ Functions/Zle/replace-string	13 Jan 2005 15:10:43 -0000
@@ -4,14 +4,21 @@
 autoload read-from-minibuffer
 
 local p1="Replace: " p2="   with: "
-local src rep REPLY MATCH MBEGIN MEND curwidget=$WIDGET
+local REPLY MATCH MBEGIN MEND curwidget=$WIDGET previous
 local -a match mbegin mend
 
-read-from-minibuffer $p1 || return 1
-src=$REPLY
+if (( ${+NUMERIC} )); then
+  (( $NUMERIC > 0 )) && previous=1
+else
+  zstyle -t ":zle:$WIDGET" edit-previous && previous=1
+fi
+
+read-from-minibuffer $p1 ${previous:+$_replace_string_src} || return 1
+_replace_string_src=$REPLY
 
-read-from-minibuffer "$p1$src$p2" || return 1
-rep=$REPLY
+read-from-minibuffer "$p1$_replace_string_src$p2" \
+  ${previous:+$_replace_string_rep} || return 1
+_replace_string_rep=$REPLY
 
 if [[ $curwidget = *pattern* ]]; then
     local rep2
@@ -20,6 +27,7 @@
     # while preceded by an odd number of backslashes is inactive,
     # with one backslash being stripped.  A similar logic applies
     # to \digit.
+    local rep=$_replace_string_rep
     while [[ $rep = (#b)([^\\]#)(\\\\)#(\\|)(\&|\\<->|\\\{<->\})(*) ]]; do
 	if [[ -n $match[3] ]]; then
 	    # Expression is quoted, strip quotes
@@ -37,9 +45,9 @@
 	rep=${match[5]}
     done
     rep2+=$rep
-    LBUFFER=${LBUFFER//(#bm)$~src/${(e)rep2}}
-    RBUFFER=${RBUFFER//(#bm)$~src/${(e)rep2}}
+    LBUFFER=${LBUFFER//(#bm)$~_replace_string_src/${(e)rep2}}
+    RBUFFER=${RBUFFER//(#bm)$~_replace_string_src/${(e)rep2}}
 else
-    LBUFFER=${LBUFFER//$src/$rep}
-    RBUFFER=${RBUFFER//$src/$rep}
+    LBUFFER=${LBUFFER//$_replace_string_src/$_replace_string_rep}
+    RBUFFER=${RBUFFER//$_replace_string_src/$_replace_string_rep}
 fi
Index: Doc/Zsh/contrib.yo
===================================================================
RCS file: /cvsroot/zsh/zsh/Doc/Zsh/contrib.yo,v
retrieving revision 1.38
diff -u -r1.38 contrib.yo
--- Doc/Zsh/contrib.yo	9 Dec 2004 14:44:46 -0000	1.38
+++ Doc/Zsh/contrib.yo	13 Jan 2005 15:10:44 -0000
@@ -814,6 +814,13 @@
 `tt(\{)var(N)tt(})' may be used to protect the digit from following
 digits.
 
+By default the previous source or replacement string will not be offered
+for editing.  However, this feature can be activated by setting the style
+tt(edit-previous) in the context tt(:zle:)var(widget) (for example,
+tt(:zle:replace-string)) to tt(true).  In addition, a positive
+numeric argument forces the previous values to be offered, a negative or
+zero argument forces them not to be.
+
 For example, starting from the line:
 
 example(print This line contains fan and fond)

-- 
Peter Stephenson <pws@xxxxxxx>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070


**********************************************************************
This email and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom they
are addressed. If you have received this email in error please notify
the system manager.

This footnote also confirms that this email message has been swept by
MIMEsweeper for the presence of computer viruses.

www.mimesweeper.com
**********************************************************************



Messages sorted by: Reverse Date, Date, Thread, Author