From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 16010 invoked from network); 10 Mar 2007 23:06:37 -0000 X-Spam-Checker-Version: SpamAssassin 3.1.8 (2007-02-13) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-2.5 required=5.0 tests=BAYES_00,FORGED_RCVD_HELO autolearn=ham version=3.1.8 Received: from news.dotsrc.org (HELO a.mx.sunsite.dk) (130.225.247.88) by ns1.primenet.com.au with SMTP; 10 Mar 2007 23:06:37 -0000 Received-SPF: none (ns1.primenet.com.au: domain at sunsite.dk does not designate permitted sender hosts) Received: (qmail 9978 invoked from network); 10 Mar 2007 23:06:31 -0000 Received: from sunsite.dk (130.225.247.90) by a.mx.sunsite.dk with SMTP; 10 Mar 2007 23:06:31 -0000 Received: (qmail 25898 invoked by alias); 10 Mar 2007 23:06:28 -0000 Mailing-List: contact zsh-workers-help@sunsite.dk; run by ezmlm Precedence: bulk X-No-Archive: yes X-Seq: 23211 Received: (qmail 25889 invoked from network); 10 Mar 2007 23:06:26 -0000 Received: from news.dotsrc.org (HELO a.mx.sunsite.dk) (130.225.247.88) by sunsite.dk with SMTP; 10 Mar 2007 23:06:26 -0000 Received: (qmail 9657 invoked from network); 10 Mar 2007 23:06:26 -0000 Received: from smtp2-g19.free.fr (212.27.42.28) by a.mx.sunsite.dk with SMTP; 10 Mar 2007 23:06:20 -0000 Received: from localhost (evr91-1-82-227-12-160.fbx.proxad.net [82.227.12.160]) by smtp2-g19.free.fr (Postfix) with ESMTP id C8F487D11 for ; Sun, 11 Mar 2007 00:06:18 +0100 (CET) Date: Sun, 11 Mar 2007 00:06:18 +0100 From: "arno." To: zsh-workers@sunsite.dk Subject: PATCH: vi-add-numeric Message-ID: <20070310230618.GA7124@localhost.localdomain> Mail-Followup-To: zsh-workers@sunsite.dk MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="yrj/dFKFPuw6o+aM" Content-Disposition: inline User-Agent: Mutt/1.5.13 (2006-08-11) --yrj/dFKFPuw6o+aM Content-Type: text/plain; charset=iso-8859-15 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Hi, here are two zle widgets They implement Ctrl-A and Ctrl-X (add and substract of vim): add (or=20 substract) one (or more if an argument is given) to the current or next=20 number on the command line. It is quite similar to widget "incarg", but when I discovered that=20 widget, I had nearly finished programming my function. I still borrowed=20 =66rom it the incarg parameter, and a few phrases in comments. But those=20 functions don't behave exactly the same, so maybe vi-add-numeric can be=20 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 incr= ement +# 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))] =3D [0-9] ]]; then + + # cursor is on a number; search its beginning to the left + pos=3D$((${#LBUFFER%%[0-9]##} + 1)) + +else + + # cursor is not on a number; search a number to the right + pos=3D$(($CURSOR + ${#RBUFFER%%[0-9]*} + 1)) + +fi + +# no number found in BUFFER +(($pos <=3D ${#BUFFER})) || return + + +# ok, get the number now +num=3D${${BUFFER[$pos,-1]}%%[^0-9]*} + +# checks if number is negative +if ((pos > 0)) && [ $BUFFER[$((pos - 1))] =3D '-' ]; then + sign=3D"-" +else + sign=3D"+" +fi + +# computes result +newnum=3D$((num $sign ${NUMERIC:-${incarg:-1}})) + + +# replaces old number by result +if ((pos > 1)); then + buf=3D${BUFFER[0,$((pos - 1))]}${BUFFER[$pos,-1]/$num/$newnum} +else + buf=3D${BUFFER/$num/$newnum} +fi + + +# going from negative to zero or positive +if [ $sign =3D '-' ] && ((newnum <=3D 0)); then + if ((newnum =3D=3D 0)); then + + # removes obsolete minus sign + buf[$((pos - 1))]=3D"" + ((pos-=3D1)) # adjust pos for cursor position computation + + elif ((newnum < 0)); then + + # removes obsolete minus sign + buf[$((pos - 1))]=3D"" + # minus sign computed in newnum + buf[$((pos - 1))]=3D"" + ((pos-=3D2)) # adjust pos for cursor position computation + + fi +fi + +# assign computed string to buffer +BUFFER=3D$buf + +# places cursor on last digit of number +CURSOR=3D$((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=3D$((0 - ${NUMERIC:-${incarg:-1}})) +vi-add-numeric --yrj/dFKFPuw6o+aM Content-Type: application/pgp-signature; name="signature.asc" Content-Description: Digital signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.6 (GNU/Linux) iD8DBQFF8znqiH9aBScBsrMRAvGDAJ9mjprqrqgtlQtVt1Eh1h1/M8zFkgCdFBr7 SrYG+J+xNXu8+KU1vi//SkU= =APbU -----END PGP SIGNATURE----- --yrj/dFKFPuw6o+aM--