zsh-workers
 help / color / mirror / code / Atom feed
From: "arno." <arno.@no-log.org>
To: zsh-workers@sunsite.dk
Subject: PATCH: vi-add-numeric
Date: Sun, 11 Mar 2007 00:06:18 +0100	[thread overview]
Message-ID: <20070310230618.GA7124@localhost.localdomain> (raw)

[-- Attachment #1: Type: text/plain, Size: 3222 bytes --]

Hi, here are two zle widgets

They implement Ctrl-A and Ctrl-X (add and substract of vim): add (or 
substract) one (or more if an argument is given) to the current or next 
number on the command line.

It is quite similar to widget "incarg", but when I discovered that 
widget, I had nearly finished programming my function. I still borrowed 
from it the incarg parameter, and a few phrases in comments. But those 
functions don't behave exactly the same, so maybe vi-add-numeric can be 
useful to some.

arno

Index: Functions/Zle/vi-add-numeric
--- /dev/null
+++ Functions/Zle/vi-add-numeric
@@ -0,0 +1,95 @@
+# This function acts like add command in vim (^A in command mode)
+# Only works now for decimal numbers
+#
+# If cursor is on any digit of a number, increment that number.
+# If cursor is not on a number, find the next number on the right and increment
+# it.
+# Cursor is then placed on the last digit of that number.
+#
+# If a numeric argument is given, increment the number by that argument.
+# Otherwise, increment by shell parameter incarg (it can be useful to set that
+# parameter if you plan to do a lot with a particular number). Otherwise,
+# increment by 1.
+#
+# examples:
+# echo 41 43
+# ^^^^^^^ cursor anywhere here
+# ->
+# echo 42 43
+#       ^ cursor here now
+#
+#
+# echo 41 43
+#        ^^^ cursor anywhere here
+# ->
+# echo 42 43
+#          ^ cursor here now
+#
+
+emulate -L zsh
+setopt extendedglob
+
+local pos num newnu sign buf
+
+if [[ $BUFFER[$((CURSOR + 1))] = [0-9] ]]; then
+
+    # cursor is on a number; search its beginning to the left
+    pos=$((${#LBUFFER%%[0-9]##} + 1))
+
+else
+
+    # cursor is not on a number; search a number to the right
+    pos=$(($CURSOR + ${#RBUFFER%%[0-9]*} + 1))
+
+fi
+
+# no number found in BUFFER
+(($pos <= ${#BUFFER}))  || return
+
+
+# ok, get the number now
+num=${${BUFFER[$pos,-1]}%%[^0-9]*}
+
+# checks if number is negative
+if ((pos > 0)) && [ $BUFFER[$((pos - 1))] = '-' ]; then
+    sign="-"
+else
+    sign="+"
+fi
+
+# computes result
+newnum=$((num $sign ${NUMERIC:-${incarg:-1}}))
+
+
+# replaces old number by result
+if ((pos > 1)); then
+    buf=${BUFFER[0,$((pos - 1))]}${BUFFER[$pos,-1]/$num/$newnum}
+else
+    buf=${BUFFER/$num/$newnum}
+fi
+
+
+# going from negative to zero or positive
+if [ $sign = '-' ] && ((newnum <= 0)); then
+    if ((newnum == 0)); then
+
+        # removes obsolete minus sign
+        buf[$((pos - 1))]=""
+        ((pos-=1)) # adjust pos for cursor position computation
+
+    elif ((newnum < 0)); then
+
+        # removes obsolete minus sign
+        buf[$((pos - 1))]=""
+        # minus sign computed in newnum
+        buf[$((pos - 1))]=""
+        ((pos-=2)) # adjust pos for cursor position computation
+
+    fi
+fi
+
+# assign computed string to buffer
+BUFFER=$buf
+
+# places cursor on last digit of number
+CURSOR=$((pos + $#newnum - 2))
Index: Functions/Zle/vi-substract-numeric
--- /dev/null
+++ Functions/Zle/vi-substract-numeric
@@ -0,0 +1,4 @@
+emulate -L zsh
+autoload -U vi-add-numeric
+NUMERIC=$((0 - ${NUMERIC:-${incarg:-1}}))
+vi-add-numeric

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 189 bytes --]

             reply	other threads:[~2007-03-10 23:06 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-03-10 23:06 arno. [this message]
2007-03-11 10:04 ` arno.

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20070310230618.GA7124@localhost.localdomain \
    --to="arno."@no-log.org \
    --cc=zsh-workers@sunsite.dk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).