zsh-workers
 help / color / mirror / code / Atom feed
* PATCH: replace string etc.
@ 2011-11-04 14:19 Peter Stephenson
  2011-11-07 10:15 ` Peter Stephenson
  0 siblings, 1 reply; 2+ messages in thread
From: Peter Stephenson @ 2011-11-04 14:19 UTC (permalink / raw)
  To: Zsh hackers list

This fixes a couple of minor things.

replace-string when doing regular expression matches didn't replace
anything right of the cursor if it didn't find anything left of the
cursor.

It still doesn't handle cases straddling the cursor.

There's a rather grotesque save and restore in read-from-minibuffer
that could be better done using local variables in an anonymous function
now we have them.

I had a go at trying to make replace-string use a local history,
but it was very hairy; changing the history in the middle of zle has
interesting effects (not fatal ones, hence probably entirely logical if
thought through).  Until some brainwave occurs I've given up.

Index: Functions/Zle/read-from-minibuffer
===================================================================
RCS file: /cvsroot/zsh/zsh/Functions/Zle/read-from-minibuffer,v
retrieving revision 1.7
diff -p -u -r1.7 read-from-minibuffer
--- Functions/Zle/read-from-minibuffer	7 Apr 2008 09:44:34 -0000	1.7
+++ Functions/Zle/read-from-minibuffer	4 Nov 2011 14:14:31 -0000
@@ -19,26 +19,19 @@ while getopts "k:" opt; do
 done
 (( OPTIND > 1 )) && shift $(( OPTIND - 1 ))
 
+local readprompt="$1" lbuf_init="$2" rbuf_init="$3"
+
+# Use anonymous function to make sure special values get restored,
+# even if this function is called as a widget.
+# local +h ensures special parameters stay special.
+() {
   local pretext="$PREDISPLAY$LBUFFER$RBUFFER$POSTDISPLAY
 "
-# We could use the local variables mechanism to save these
-# values, but if read-from-minibuffer is called as a widget
-# (which isn't actually all that useful) the values won't be
-# restored because the variables are already local at the current
-# level and don't get restored when they go out of scope.
-# We could do it with an additional function level.
-  local save_lbuffer=$LBUFFER
-  local save_rbuffer=$RBUFFER
-  local save_predisplay=$PREDISPLAY
-  local save_postdisplay=$POSTDISPLAY
-  local -a save_region_highlight
-  save_region_highlight=("${region_highlight[@]}")
-
-{
-  LBUFFER="$2"
-  RBUFFER="$3"
-  PREDISPLAY="$pretext${1:-? }"
-  POSTDISPLAY=
+  local +h LBUFFER="$lbuf_init"
+  local +h RBUFFER="$rbuf_init"
+  local +h PREDISPLAY="$pretext${readprompt:-? }"
+  local +h POSTDISPLAY=
+  local +h -a region_highlight
   region_highlight=("P${#pretext} ${#PREDISPLAY} bold")
 
   if [[ -n $keys ]]; then
@@ -50,12 +43,6 @@ done
     stat=$?
     (( stat )) || REPLY=$BUFFER
   fi
-} always {
-  LBUFFER=$save_lbuffer
-  RBUFFER=$save_rbuffer
-  PREDISPLAY=$save_predisplay
-  POSTDISPLAY=$save_postdisplay
-  region_highlight=("${save_region_highlight[@]}")
 }
 
 return $stat
Index: Functions/Zle/replace-string-again
===================================================================
RCS file: /cvsroot/zsh/zsh/Functions/Zle/replace-string-again,v
retrieving revision 1.5
diff -p -u -r1.5 replace-string-again
--- Functions/Zle/replace-string-again	8 Sep 2010 16:32:45 -0000	1.5
+++ Functions/Zle/replace-string-again	4 Nov 2011 14:14:31 -0000
@@ -40,8 +40,10 @@ if [[ $curwidget = *(pattern|regex)* ]];
     rep2+=$rep
     if [[ $curwidget = *regex* ]]; then
       autoload -Uz regexp-replace
-      regexp-replace LBUFFER $_replace_string_src $rep2 || return 1
-      regexp-replace RBUFFER $_replace_string_src $rep2 || return 1
+      integer ret=1
+      regexp-replace LBUFFER $_replace_string_src $rep2 && ret=0
+      regexp-replace RBUFFER $_replace_string_src $rep2 && ret=0
+      return ret
     else
       LBUFFER=${LBUFFER//(#bm)$~_replace_string_src/${(e)rep2}}
       RBUFFER=${RBUFFER//(#bm)$~_replace_string_src/${(e)rep2}}


-- 
Peter Stephenson <pws@csr.com>            Software Engineer
Tel: +44 (0)1223 692070                   Cambridge Silicon Radio Limited
Churchill House, Cambridge Business Park, Cowley Road, Cambridge, CB4 0WZ, UK


Member of the CSR plc group of companies. CSR plc registered in England and Wales, registered number 4187346, registered office Churchill House, Cambridge Business Park, Cowley Road, Cambridge, CB4 0WZ, United Kingdom
More information can be found at www.csr.com. Follow CSR on Twitter at http://twitter.com/CSR_PLC and read our blog at www.csr.com/blog


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: PATCH: replace string etc.
  2011-11-04 14:19 PATCH: replace string etc Peter Stephenson
@ 2011-11-07 10:15 ` Peter Stephenson
  0 siblings, 0 replies; 2+ messages in thread
From: Peter Stephenson @ 2011-11-07 10:15 UTC (permalink / raw)
  To: Zsh hackers list

On Fri, 4 Nov 2011 14:19:48 +0000
Peter Stephenson <pws@csr.com> wrote:
> I had a go at trying to make replace-string use a local history,
> but ... I've given up.

However, here's a simple addition that offers the previous replacement
as a default.

Index: Doc/Zsh/contrib.yo
===================================================================
RCS file: /cvsroot/zsh/zsh/Doc/Zsh/contrib.yo,v
retrieving revision 1.137
diff -p -u -r1.137 contrib.yo
--- Doc/Zsh/contrib.yo	3 Jul 2011 11:54:12 -0000	1.137
+++ Doc/Zsh/contrib.yo	7 Nov 2011 10:13:49 -0000
@@ -2335,6 +2335,11 @@ regular expression matching is performed
 replacement.  Note that the previous source and replacement text are the
 same whether pattern, regular expression or string matching is used.
 
+In addition, tt(replace-string) shows the previous replacement above
+the prompt, so long as there was one during the current session; if the
+source string is empty, that replacement will be repeated without
+the widget prompting for a replacement string.
+
 For example, starting from the line:
 
 example(print This line contains fan and fond)
Index: Functions/Zle/replace-string
===================================================================
RCS file: /cvsroot/zsh/zsh/Functions/Zle/replace-string,v
retrieving revision 1.5
diff -p -u -r1.5 replace-string
--- Functions/Zle/replace-string	28 Jul 2010 14:01:12 -0000	1.5
+++ Functions/Zle/replace-string	7 Nov 2011 10:13:49 -0000
@@ -3,7 +3,15 @@ setopt extendedglob
 
 autoload -Uz read-from-minibuffer replace-string-again
 
-local p1="Replace: " p2="   with: "
+local p1  p2
+
+if [[ -n $_replace_string_src ]]; then
+  p1="[$_replace_string_src -> $_replace_string_rep]"$'\n'
+fi
+
+p1+="Replace: "
+p2="   with: "
+
 # Saving curwidget is necessary to avoid the widget name being overwritten.
 local REPLY previous curwidget=$WIDGET
 
@@ -14,10 +22,12 @@ else
 fi
 
 read-from-minibuffer $p1 ${previous:+$_replace_string_src} || return 1
-typeset -g _replace_string_src=$REPLY
+if [[ -n $REPLY ]]; then
+  typeset -g _replace_string_src=$REPLY
 
-read-from-minibuffer "$p1$_replace_string_src$p2" \
-  ${previous:+$_replace_string_rep} || return 1
-typeset -g _replace_string_rep=$REPLY
+  read-from-minibuffer "$p1$_replace_string_src$p2" \
+    ${previous:+$_replace_string_rep} || return 1
+  typeset -g _replace_string_rep=$REPLY
+fi
 
 replace-string-again $curwidget

-- 
Peter Stephenson <pws@csr.com>            Software Engineer
Tel: +44 (0)1223 692070                   Cambridge Silicon Radio Limited
Churchill House, Cambridge Business Park, Cowley Road, Cambridge, CB4 0WZ, UK


Member of the CSR plc group of companies. CSR plc registered in England and Wales, registered number 4187346, registered office Churchill House, Cambridge Business Park, Cowley Road, Cambridge, CB4 0WZ, United Kingdom
More information can be found at www.csr.com. Follow CSR on Twitter at http://twitter.com/CSR_PLC and read our blog at www.csr.com/blog


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2011-11-07 11:11 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-11-04 14:19 PATCH: replace string etc Peter Stephenson
2011-11-07 10:15 ` Peter Stephenson

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).