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

Re: PATCH: command line highlighting



Bart Schaefer wrote:
> On Apr 2,  8:39pm, Peter Stephenson wrote:
> }
> } Now 4.3.6 is out of the way, here's something I've been working on.  It
> } highlights the normal command line (not a completion list)
> 
> Now all we need to make all the would-be highlighters happy is
> highlighting in PREDISPLAY and POSTDISPLAY, I think.

This should work already.  A "P" in the region_highlight indicates the
index includes PRESDISPLAY, and counting is supposed to continue over
POSTDISPLAY.  See, for example, the highlighting added to
read-from-minibuffer.  I should make it more explicit that POSTDISPLAY
is included, however.

Completely separate matter that's just occurred to me... actually,I
think I've inadvertently made read-from-minibuffer behave slightly
differently.  The documentation says it "will work correctly as a widget
in its own right".  However, I've rewritten so that saving and restoring
of special parameters is done by making them local.  This doesn't
actually work when it's called directly as a widget: the parameters are
already local at the level in question and won't be restored when they
go out of scope, because of the way special zle widgets work.  It would
probably be safer to put it back.  It's actually not very useful as a
widget in its own right so it's hardly worth worrying about, but I might
as well get out of the corner I've documented myself into.

pws

Index: Doc/Zsh/zle.yo
===================================================================
RCS file: /cvsroot/zsh/zsh/Doc/Zsh/zle.yo,v
retrieving revision 1.60
diff -u -r1.60 zle.yo
--- Doc/Zsh/zle.yo	4 Apr 2008 09:29:32 -0000	1.60
+++ Doc/Zsh/zle.yo	7 Apr 2008 09:34:53 -0000
@@ -778,8 +778,12 @@
 item(tt(region_highlight) (array))(
 Each element of this array may be set to a string that describes
 highlighting for an arbitrary region of the command line that will
-take effect the next time the command line is redisplayed.  Each
-string consists of the following parts:
+take effect the next time the command line is redisplayed.  Highlighting
+of the non-editable parts of the command line in tt(PREDISPLAY)
+and tt(POSTDISPLAY) are possible, but note that the tt(P) flag
+is needed for character indexing to include tt(PREDISPLAY).
+
+Each string consists of the following parts:
 
 startlist()
 list(Optionally, a `tt(P)' to signify that the start and end offset that
Index: Functions/Zle/read-from-minibuffer
===================================================================
RCS file: /cvsroot/zsh/zsh/Functions/Zle/read-from-minibuffer,v
retrieving revision 1.6
diff -u -r1.6 read-from-minibuffer
--- Functions/Zle/read-from-minibuffer	3 Apr 2008 11:39:11 -0000	1.6
+++ Functions/Zle/read-from-minibuffer	7 Apr 2008 09:34:54 -0000
@@ -21,21 +21,41 @@
 
   local pretext="$PREDISPLAY$LBUFFER$RBUFFER$POSTDISPLAY
 "
-local LBUFFER="$2"
-local RBUFFER="$3"
-local PREDISPLAY="$pretext${1:-? }"
-local POSTDISPLAY=
-local -a region_highlight
-region_highlight=("P${#pretext} ${#PREDISPLAY} bold")
+# 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[@]}")
 
-if [[ -n $keys ]]; then
-  zle -R
-  read -k $keys
-  stat=$?
-else
-  zle recursive-edit -K main
-  stat=$?
-  (( stat )) || REPLY=$BUFFER
-fi
+{
+  LBUFFER="$2"
+  RBUFFER="$3"
+  PREDISPLAY="$pretext${1:-? }"
+  POSTDISPLAY=
+  region_highlight=("P${#pretext} ${#PREDISPLAY} bold")
+
+  if [[ -n $keys ]]; then
+    zle -R
+    read -k $keys
+    stat=$?
+  else
+    zle recursive-edit -K main
+    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

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



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