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

Function to increment number on the command line



May you don't need this.  It increments a number on the command line under
or just before the cursor.  You can set $incarg or use the numeric argument
to have an increment other than 1.

It should work with an existing 3.1.x installation, which is why I'm
sending it to zsh-users, but if you're not patching you should send it through
  sed -n 's/^\+\([^+]\)/\1/p'
to extract it.

--- Functions/Zle/incarg.old	Mon Sep 13 16:27:24 1999
+++ Functions/Zle/incarg	Mon Sep 13 16:27:29 1999
@@ -0,0 +1,43 @@
+# Shell function to increment an integer either under the cursor or just
+# to the left of it.  Use
+#   autoload -U incarg
+#   zle -N incarg
+#   bindkey "..." incarg
+# to define it.  For example,
+#   echo 41
+#        ^^^ cursor anywhere here
+# with incarg gives
+#   echo 42
+# with the cursor in the same place.
+#
+# A numeric argument gives a number other than 1 to add (may be negative).
+# If you're going to do it a lot with one particular number, you can set
+# the parameter incarg to that number (a numeric argument still takes
+# precedence).
+
+emulate -L zsh
+setopt extendedglob
+
+local rrest lrest num
+
+rrest=${RBUFFER##[0-9]#}
+if [[ $RBUFFER = [0-9]* ]]; then
+  if [[ -z $rrest ]]; then
+    num=$RBUFFER
+  else
+    num=${RBUFFER[1,-$#rrest-1]}
+  fi
+fi
+
+lrest=${LBUFFER%%[0-9]#}
+if [[ $LBUFFER = *[0-9] ]]; then
+  if [[ -z $lrest ]]; then
+    num="$LBUFFER$num"
+  else
+    num="${LBUFFER[$#lrest+1,-1]}$num"
+  fi
+fi
+
+[[ -n $num ]] && (( num += ${NUMERIC:-${incarg:-1}} ))
+
+BUFFER="$lrest$num$rrest"

-- 
Peter Stephenson <pws@xxxxxx>       Tel: +39 050 844536
WWW:  http://www.ifh.de/~pws/
Dipartimento di Fisica, Via Buonarroti 2, 56127 Pisa, Italy



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