zsh-users
 help / color / mirror / code / Atom feed
* the function to show a digit argument while it is being typed
@ 2009-11-11  0:00 Dmitry Bolshakov
  2009-11-11  2:22 ` Mikael Magnusson
                   ` (2 more replies)
  0 siblings, 3 replies; 17+ messages in thread
From: Dmitry Bolshakov @ 2009-11-11  0:00 UTC (permalink / raw)
  To: zsh-users

hi

in bash a digit argument is showed when it is being typed

i have writed small function which do the same

#####
function digit-argument-show-while-entering {
	arg="${KEYS[2]}"
	if [[ $arg == - ]]
	then
		zle .neg-argument
	else
		zle .digit-argument
	fi
	while true
	do
		PREDISPLAY="(arg: $arg) "
		zle -R

		read -k k1
		if [[ $k1 == $'\C-g' ]]
		then
			NUMERIC=1
			break
		elif [[ $k1 != $'\e' ]]
		then
			zle -U $k1
			break
		fi

		read -k k2
		if [[ $k2 == <0-9> ]]
		then
			arg=$arg$k2
			NUMERIC=$arg
		else
			zle -U "$k1$k2"
			break
		fi
	done
	PREDISPLAY=""
}
zle -N digit-argument digit-argument-show-while-entering
zle -N neg-argument digit-argument-show-while-entering
#####

it is not well tested but seems to be working

-- 
with best regards
Dmitry Bolshakov


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

* Re: the function to show a digit argument while it is being typed
  2009-11-11  0:00 the function to show a digit argument while it is being typed Dmitry Bolshakov
@ 2009-11-11  2:22 ` Mikael Magnusson
  2009-11-11  4:47   ` Bart Schaefer
  2009-11-11  3:56 ` Bart Schaefer
  2009-11-11  6:28 ` Mikael Magnusson
  2 siblings, 1 reply; 17+ messages in thread
From: Mikael Magnusson @ 2009-11-11  2:22 UTC (permalink / raw)
  To: Dmitry Bolshakov; +Cc: zsh-users

2009/11/11 Dmitry Bolshakov <bdimych@narod.ru>:
> hi
>
> in bash a digit argument is showed when it is being typed
>
> i have writed small function which do the same
>
> #####
> function digit-argument-show-while-entering {
>        arg="${KEYS[2]}"
>        if [[ $arg == - ]]
>        then
>                zle .neg-argument
>        else
>                zle .digit-argument
>        fi
>        while true
>        do
>                PREDISPLAY="(arg: $arg) "
>                zle -R
>
>                read -k k1
>                if [[ $k1 == $'\C-g' ]]
>                then
>                        NUMERIC=1
>                        break
>                elif [[ $k1 != $'\e' ]]
>                then
>                        zle -U $k1
>                        break
>                fi
>
>                read -k k2
>                if [[ $k2 == <0-9> ]]
>                then
>                        arg=$arg$k2
>                        NUMERIC=$arg
>                else
>                        zle -U "$k1$k2"
>                        break
>                fi
>        done
>        PREDISPLAY=""
> }
> zle -N digit-argument digit-argument-show-while-entering
> zle -N neg-argument digit-argument-show-while-entering
> #####
>
> it is not well tested but seems to be working

You probably want to declare local variables so they don't overwrite
things in the main shell, ie "local arg k1 k2" in the beginning.

I have a similar function, it doesn't work well for negative arguments though:

function _digit_argument () {
  zle -M "$NUMERIC$KEYS[-1]"
  zle .digit-argument
}

The problem is $NUMERIC is -1 after pressing just -, so pressing -1
shows -11. After the first digit it shows things correctly though.
(I've tried the obvious thing of swapping the order of the lines and
showing just $NUMERIC, but it doesn't seem to be updated at that
point).

You can do zle .$WIDGET instead of your first if condition.

-- 
Mikael Magnusson


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

* Re: the function to show a digit argument while it is being typed
  2009-11-11  0:00 the function to show a digit argument while it is being typed Dmitry Bolshakov
  2009-11-11  2:22 ` Mikael Magnusson
@ 2009-11-11  3:56 ` Bart Schaefer
  2009-11-11  6:28 ` Mikael Magnusson
  2 siblings, 0 replies; 17+ messages in thread
From: Bart Schaefer @ 2009-11-11  3:56 UTC (permalink / raw)
  To: zsh-users

On Nov 11,  3:00am, Dmitry Bolshakov wrote:
} Subject: the function to show a digit argument while it is being typed
}
} in bash a digit argument is showed when it is being typed
} 
} i have writed small function which do the same

That's very nice, but it shouldn't need to be that complex.  ZLE will
make sure that your function is never called except when $KEYS[2] is a
digit, so you don't need to do your own loop to read more keys.

    function digit-argument-show {
      typeset -g __digit_arg
      if [[ $LASTWIDGET != *-argument ]]
      then
        __digit_arg=""
        PREDISPLAY=""
      fi
      __digit_arg+=$KEYS[2]
      PREDISPLAY="(arg: $__digit_arg) "
      zle -R
      zle .digit-argument
    }

    function neg-argument-show {
      typeset -g __digit_arg=-
      zle .neg-argument
    }

Aha, I see the problem with that ... you need to erase PREDISPLAY after
the last digit-argument has happened.  There's still a simpler way than
writing your own loop, namely by using zle read-command.

    function digit-argument-show {
      typeset -g __digit_arg
      if [[ $LASTWIDGET != (digit|neg)-argument ]]
      then
        __digit_arg=""
        PREDISPLAY=""
      fi
      __digit_arg+=$KEYS[2]
      PREDISPLAY="(arg: $__digit_arg) "
      zle -R
      zle .digit-argument
      local REPLY        # Not strictly necessary
      zle read-command   # Sets $REPLY and $KEYS
      if [[ $REPLY != digit-argument ]]
      then
        __digit_arg=""
        PREDISPLAY=""
      fi
      zle -U $KEYS
    }


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

* Re: the function to show a digit argument while it is being typed
  2009-11-11  2:22 ` Mikael Magnusson
@ 2009-11-11  4:47   ` Bart Schaefer
  2009-11-11  6:17     ` Mikael Magnusson
  0 siblings, 1 reply; 17+ messages in thread
From: Bart Schaefer @ 2009-11-11  4:47 UTC (permalink / raw)
  To: zsh-users

On Nov 11,  3:22am, Mikael Magnusson wrote:
}
} I have a similar function, it doesn't work well for negative arguments
} though:
}
} function _digit_argument () {
}   zle -M "$NUMERIC$KEYS[-1]"
}   zle .digit-argument
} }

Hmm, that suggests yet another possible improvement to my function.  I
assumed $NUMERIC was updated too late to use it in PREDISPLAY, but it
is updated in time to be used in place of my __digit_arg global.

    function digit-argument-show {
      if [[ $LASTWIDGET == neg-argument ]]
      then PREDISPLAY="(arg: $((NUMERIC*$KEYS[-1]))) "
      else PREDISPLAY="(arg: $NUMERIC$KEYS[-1]) "
      fi
      zle -R
      zle .digit-argument
      zle read-command
      [[ $REPLY != digit-argument ]] && PREDISPLAY=""
      zle -U $KEYS
    }

With this, you don't need the neg-argument-show function at all, and
assignments to PREDISPLAY can be replaced with zle -M if you like.


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

* Re: the function to show a digit argument while it is being typed
  2009-11-11  4:47   ` Bart Schaefer
@ 2009-11-11  6:17     ` Mikael Magnusson
  2009-11-11  8:14       ` Bart Schaefer
  0 siblings, 1 reply; 17+ messages in thread
From: Mikael Magnusson @ 2009-11-11  6:17 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: zsh-users

2009/11/11 Bart Schaefer <schaefer@brasslantern.com>:
> On Nov 11,  3:22am, Mikael Magnusson wrote:
> }
> } I have a similar function, it doesn't work well for negative arguments
> } though:
> }
> } function _digit_argument () {
> }   zle -M "$NUMERIC$KEYS[-1]"
> }   zle .digit-argument
> } }
>
> Hmm, that suggests yet another possible improvement to my function.  I
> assumed $NUMERIC was updated too late to use it in PREDISPLAY, but it
> is updated in time to be used in place of my __digit_arg global.
>
>    function digit-argument-show {
>      if [[ $LASTWIDGET == neg-argument ]]
>      then PREDISPLAY="(arg: $((NUMERIC*$KEYS[-1]))) "
>      else PREDISPLAY="(arg: $NUMERIC$KEYS[-1]) "
>      fi
>      zle -R
>      zle .digit-argument
>      zle read-command
>      [[ $REPLY != digit-argument ]] && PREDISPLAY=""
>      zle -U $KEYS
>    }
>
> With this, you don't need the neg-argument-show function at all, and
> assignments to PREDISPLAY can be replaced with zle -M if you like.

With $LASTWIDGET, I can make my function work pretty well too. It only
messes up when you do a neg-argument when there are already some
digits in numeric. Apparently that only clears numeric, rather than
starting over with a new -1. Pressing alt-5 alt-- alt-5 shows arg: 0,
but pressing a key at this point inserts 5 characters. Pressing alt-5
again correctly shows arg: 55. I tried to work around it by running
zle .neg-argument twice, but it has no apparent effect. Aha, this
works:
  elif [[ $LASTWIDGET = neg-argument ]]; then
    zle -M - "$((NUMERIC * $KEYS[-1] ? NUMERIC * $KEYS[-1] : $KEYS[-1]))"


I also noticed ctrl-c gives this message for your function:
_digit_argument:zle:9: not enough arguments for -U
and doesn't reset numeric.

I also noticed zle -M -5 complains about 5 not being an option, zle -M
- -5 does work but I pretty much had to guess that.

Here's my whole working (as far as I can tell) function, bound both to
digit-argument and neg-argument:

function _digit_argument () {
  if [[ $WIDGET = neg-argument ]]; then
    if [[ -n $NUMERIC ]]; then
      zle -M - ""
    else
      zle -M - -
    fi
  elif [[ $LASTWIDGET = neg-argument ]]; then
    zle -M - "$((NUMERIC * $KEYS[-1] ? NUMERIC * $KEYS[-1] : $KEYS[-1]))"
  else
    zle -M - "$NUMERIC$KEYS[-1]"
  fi
  zle .$WIDGET
}

(It does not clear the display when something else is pressed).

-- 
Mikael Magnusson


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

* Re: the function to show a digit argument while it is being typed
  2009-11-11  0:00 the function to show a digit argument while it is being typed Dmitry Bolshakov
  2009-11-11  2:22 ` Mikael Magnusson
  2009-11-11  3:56 ` Bart Schaefer
@ 2009-11-11  6:28 ` Mikael Magnusson
  2 siblings, 0 replies; 17+ messages in thread
From: Mikael Magnusson @ 2009-11-11  6:28 UTC (permalink / raw)
  To: Dmitry Bolshakov; +Cc: zsh-users

2009/11/11 Dmitry Bolshakov <bdimych@narod.ru>:
> hi
>
> in bash a digit argument is showed when it is being typed

btw, I just tried this in bash and it does not work well. If you do
alt--, it prints (arg: -1), but pressing a key at this point inserts
nothing. Pressing alt-- alt-5 displays (arg: -15), with the same
result when pressing a key. Pressing alt-5 alt-- inserts 5 dashes.
Tried in both bash 3 and bash 4.

-- 
Mikael Magnusson


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

* Re: the function to show a digit argument while it is being typed
  2009-11-11  6:17     ` Mikael Magnusson
@ 2009-11-11  8:14       ` Bart Schaefer
  2009-11-11 16:07         ` Greg Klanderman
  0 siblings, 1 reply; 17+ messages in thread
From: Bart Schaefer @ 2009-11-11  8:14 UTC (permalink / raw)
  To: zsh-users

On Nov 11,  7:17am, Mikael Magnusson wrote:
} Subject: Re: the function to show a digit argument while it is being typed
}
} 2009/11/11 Bart Schaefer <schaefer@brasslantern.com>:
} >
} >    function digit-argument-show {
} >      if [[ $LASTWIDGET == neg-argument ]]
} >      then PREDISPLAY="(arg: $((NUMERIC*$KEYS[-1]))) "
} >      else PREDISPLAY="(arg: $NUMERIC$KEYS[-1]) "
} >      fi
} >      zle -R
} >      zle .digit-argument
} >      zle read-command
} >      [[ $REPLY != digit-argument ]] && PREDISPLAY=""
} >      zle -U $KEYS
} >    }
} 
} With $LASTWIDGET, I can make my function work pretty well too. It only
} messes up when you do a neg-argument when there are already some
} digits in numeric. Apparently that only clears numeric, rather than
} starting over with a new -1.

I have no idea whether that should be considered a bug.  One possible
interpretation is that ESC 5 ESC - should run neg-argument five times,
which would result in -1, but in practice neg-argument after either
digit-argument or neg-argument simply erases NUMERIC.  At the very
least there's a documentation omission.

}   elif [[ $LASTWIDGET = neg-argument ]]; then
}     zle -M - "$((NUMERIC * $KEYS[-1] ? NUMERIC * $KEYS[-1] : $KEYS[-1]))"

I think [[ $LASTWIDGET = neg-argument && -n $NUMERIC ]] also works,
and may in fact be better ... then you don't need the ternary.

} I also noticed ctrl-c gives this message for your function:
} _digit_argument:zle:9: not enough arguments for -U
} and doesn't reset numeric.

That's quite interesting; it means a tty interrupt stops read-command
but doesn't set $KEYS.  In fact after some quick testing the doc for
read-command is wildly inaccurate:

    ... If the key sequence is not bound, status 1 is
    returned; typically, however, REPLY is set to undefined-key to
    indicate a useless key sequence.

In fact for an unbound sequence status 0 is returned and REPLY is set
to undefined-key.  The only way I can get a 1 for the status is with
the tty interrupt character, and then REPLY is set to self-insert
(which makes no sense at all).  That also means zle read-command is a
way to trap interrupts, which may be yet another bug.

   function digit-argument-show {
     if [[ $LASTWIDGET == neg-argument && -n $NUMERIC ]]
     then PREDISPLAY="(arg: $((NUMERIC*$KEYS[-1]))) "
     else PREDISPLAY="(arg: $NUMERIC$KEYS[-1]) "
     fi
     zle -R
     zle .digit-argument
     zle read-command
     [[ $REPLY != digit-argument ]] && PREDISPLAY=""
     [[ -n $KEYS ]] && zle -U $KEYS
   }

-- 


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

* Re: the function to show a digit argument while it is being typed
  2009-11-11  8:14       ` Bart Schaefer
@ 2009-11-11 16:07         ` Greg Klanderman
  2009-11-11 17:27           ` Mikael Magnusson
  2009-11-11 17:45           ` Bart Schaefer
  0 siblings, 2 replies; 17+ messages in thread
From: Greg Klanderman @ 2009-11-11 16:07 UTC (permalink / raw)
  To: zsh-users


This is pretty nice.. is there any way to have the argument appear on
the following line instead?  I had to add 'setopt localoptions unset'
since I normally have 'setopt nounset'.  For some reason meta-<digit>
is not working for me at all (even without digit-argument-show) even
though describe-key-briefly does tell me it's bound to
digit-argument.. will have to look into that.  Also would be nice if
this worked with universal-argument.

greg


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

* Re: the function to show a digit argument while it is being typed
  2009-11-11 16:07         ` Greg Klanderman
@ 2009-11-11 17:27           ` Mikael Magnusson
  2009-11-11 17:45           ` Bart Schaefer
  1 sibling, 0 replies; 17+ messages in thread
From: Mikael Magnusson @ 2009-11-11 17:27 UTC (permalink / raw)
  To: gak; +Cc: zsh-users

2009/11/11 Greg Klanderman <gak@klanderman.net>:
>
> This is pretty nice.. is there any way to have the argument appear on
> the following line instead?

My function does that, ie replace the PREDISPLAY="foo" thing with zle
-M - "foo".

-- 
Mikael Magnusson


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

* Re: the function to show a digit argument while it is being typed
  2009-11-11 16:07         ` Greg Klanderman
  2009-11-11 17:27           ` Mikael Magnusson
@ 2009-11-11 17:45           ` Bart Schaefer
  2009-11-11 18:44             ` Greg Klanderman
                               ` (2 more replies)
  1 sibling, 3 replies; 17+ messages in thread
From: Bart Schaefer @ 2009-11-11 17:45 UTC (permalink / raw)
  To: zsh-users

On Nov 11, 11:07am, Greg Klanderman wrote:
} 
} This is pretty nice.. is there any way to have the argument appear on
} the following line instead?

Here's a summarized version based on everything that's gone by so far.

   function digit-argument-show {
     emulate -R zsh
     if [[ $LASTWIDGET == neg-argument && -n $NUMERIC ]]
     then zle -M "arg: $((NUMERIC*$KEYS[-1]))"
     else zle -M "arg: $NUMERIC$KEYS[-1]"
     fi
     zle -R
     zle .digit-argument
     if zle read-command
     then [[ $REPLY != digit-argument ]] && zle -M ""
     else zle send-break
     fi
     [[ -n $KEYS ]] && zle -U $KEYS
   }

I'm not sure why the "zle -R" is needed; odd things happen [*] if it's
left out, probably because .digit-argument does not normally change
BUFFER or CURSOR and therefore doesn't cause a full refresh.

[*] cursor moves to status line but message is not printed, then all the
messages spew out when something does change BUFFER.

} I had to add 'setopt localoptions unset'

The "emulate" should take care of that -- things like shwordsplit also
needed to be handled.

} Also would be nice if this worked with universal-argument.

Hmm, that one is a lot trickier; universal-argument effectively is a
trap on the next self-insert, such that if a digit is typed it behaves
like digit-argument (and also discards any "pending" neg-argument,
which may be yet another bug).  It handles that next keypress in a
way that makes it impossible to intercept in a wrapper widget, which
requires that the new widget not call .universal-argument.

For example, .universal-argument followed by the tty interrupt character
does not accomplish a send-break, which is another in the long line of
bugs that this exercise is uncovering.

Things get even hairier when argument-base is thrown in.


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

* Re: the function to show a digit argument while it is being typed
  2009-11-11 17:45           ` Bart Schaefer
@ 2009-11-11 18:44             ` Greg Klanderman
  2009-11-11 19:46               ` Bart Schaefer
  2009-11-12 18:38             ` Greg Klanderman
  2009-11-12 23:06             ` Greg Klanderman
  2 siblings, 1 reply; 17+ messages in thread
From: Greg Klanderman @ 2009-11-11 18:44 UTC (permalink / raw)
  To: zsh-users

>>>>> On November 11, 2009 Bart Schaefer <schaefer@brasslantern.com> wrote:

> Here's a summarized version based on everything that's gone by so far.

That's not working right with negative arguments for me - an initial
neg-argument is ignored both in what is displayed, and in the effect
of the eventual argument.

Also, if you use neg-argument after using digit-argument, then the
display gets all screwed up because the prompt incorrectly moves up a
line.

> The "emulate" should take care of that -- things like shwordsplit also
> needed to be handled.

Of course that's a better fix.

> } Also would be nice if this worked with universal-argument.

> Hmm, that one is a lot trickier;

That's too bad, as it's what I usually use.  Wonder how hard it'd be
to add this functionality directly to the C code implementing that..

> Things get even hairier when argument-base is thrown in.

Ha ha, I didn't even know about that!

Greg


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

* Re: the function to show a digit argument while it is being typed
  2009-11-11 18:44             ` Greg Klanderman
@ 2009-11-11 19:46               ` Bart Schaefer
  2009-11-11 21:44                 ` Greg Klanderman
  0 siblings, 1 reply; 17+ messages in thread
From: Bart Schaefer @ 2009-11-11 19:46 UTC (permalink / raw)
  To: zsh-users

On Nov 11,  1:44pm, Greg Klanderman wrote:
} Subject: Re: the function to show a digit argument while it is being typed
}
} >>>>> On November 11, 2009 Bart Schaefer <schaefer@brasslantern.com> wrote:
} 
} > Here's a summarized version based on everything that's gone by so far.
} 
} That's not working right with negative arguments for me - an initial
} neg-argument is ignored both in what is displayed, and in the effect
} of the eventual argument.

Hmm, it works for me starting from zsh -f.  Note that you're not
supposed to replace the builtin neg-argument with anything, only
replace digit-argument with digit-argument-show.  Which means that
neg-argument doesn't put anything in the status line until you give
the first digit, then that should show up as a negative number.  I
should have made that clear.

If you want neg-argument to display the leading "-" you'll need to
use another wrapper.


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

* Re: the function to show a digit argument while it is being typed
  2009-11-11 19:46               ` Bart Schaefer
@ 2009-11-11 21:44                 ` Greg Klanderman
  2009-11-11 22:14                   ` Greg Klanderman
  0 siblings, 1 reply; 17+ messages in thread
From: Greg Klanderman @ 2009-11-11 21:44 UTC (permalink / raw)
  To: zsh-users

>>>>> On November 11, 2009 Bart Schaefer <schaefer@brasslantern.com> wrote:

> Note that you're not supposed to replace the builtin neg-argument
> with anything, only replace digit-argument with digit-argument-show.

Ahh, OK then.  Since the original post was replacing both with the
same wrapper, I assumed that was still the case.  Will re-test..

greg


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

* Re: the function to show a digit argument while it is being typed
  2009-11-11 21:44                 ` Greg Klanderman
@ 2009-11-11 22:14                   ` Greg Klanderman
  2009-11-12  4:03                     ` Bart Schaefer
  0 siblings, 1 reply; 17+ messages in thread
From: Greg Klanderman @ 2009-11-11 22:14 UTC (permalink / raw)
  To: zsh-users


This seems to work:

function neg-argument-show () {
  emulate -R zsh
  if [[ $LASTWIDGET == neg-argument || $LASTWIDGET == digit-argument ]] ; then
    zle beep
    return
  fi
  zle -M "arg: -"
  zle -R
  zle .neg-argument
  if zle read-command ; then
    [[ $REPLY != digit-argument ]] && zle -M ""
  else
    zle send-break
  fi
  [[ -n $KEYS ]] && zle -U $KEYS
}

Any suggestions for improvements?

Greg


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

* Re: the function to show a digit argument while it is being typed
  2009-11-11 22:14                   ` Greg Klanderman
@ 2009-11-12  4:03                     ` Bart Schaefer
  0 siblings, 0 replies; 17+ messages in thread
From: Bart Schaefer @ 2009-11-12  4:03 UTC (permalink / raw)
  To: zsh-users

On Nov 11,  5:14pm, Greg Klanderman wrote:
}
} Any suggestions for improvements?

Both my digit-argument-show and your neg-argument-show would benefit
from adding a seemingly-redundant "zle -R" at the very end.  There's
still some odd refresh behavior that can occur if the key sequence
pushed by "zle -U" does not call a widget that updates the display.

Also I'm casting about for a workaround for "if zle read-command",
given that the failure status from read-command depends on a bug
that may get fixed eventually.  No specific suggestions yet.


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

* Re: the function to show a digit argument while it is being typed
  2009-11-11 17:45           ` Bart Schaefer
  2009-11-11 18:44             ` Greg Klanderman
@ 2009-11-12 18:38             ` Greg Klanderman
  2009-11-12 23:06             ` Greg Klanderman
  2 siblings, 0 replies; 17+ messages in thread
From: Greg Klanderman @ 2009-11-12 18:38 UTC (permalink / raw)
  To: zsh-users

>>>>> On November 11, 2009 Bart Schaefer <schaefer@brasslantern.com> wrote:

> Things get even hairier when argument-base is thrown in.

So I decided to play with that a bit, and in my experience, use of
argument-base to set the base only persists for one subsequent use of
digit-argument or universal-argument.  Is that expected?  It's what I
see even with 'zsh -f'.  The documentation is not explicit on this
point, but my assumption is that the setting would persist until the
next change, and I do not see anywhere in the code where zmod.base is
modified other than in argumentbase(), though there are two struct
assignments to it, however they look to be doing save/restore types of
stuff, but I'm not sure what bin_zle_call() is wrapped around..

Anyway, is this how it's supposed to work?

Also, in universalargument(), is it intentional that if an argument is
explicitly passed it is interpreted in base 10?

thanks,
Greg


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

* Re: the function to show a digit argument while it is being typed
  2009-11-11 17:45           ` Bart Schaefer
  2009-11-11 18:44             ` Greg Klanderman
  2009-11-12 18:38             ` Greg Klanderman
@ 2009-11-12 23:06             ` Greg Klanderman
  2 siblings, 0 replies; 17+ messages in thread
From: Greg Klanderman @ 2009-11-12 23:06 UTC (permalink / raw)
  To: zsh-users

>>>>> On November 11, 2009 Bart Schaefer <schaefer@brasslantern.com> wrote:

> } Also would be nice if this worked with universal-argument.

> Hmm, that one is a lot trickier; universal-argument effectively is a
> trap on the next self-insert, such that if a digit is typed it behaves
> like digit-argument (and also discards any "pending" neg-argument,
> which may be yet another bug).  It handles that next keypress in a
> way that makes it impossible to intercept in a wrapper widget, which
> requires that the new widget not call .universal-argument.

So I hacked up a proof of concept in C for showing the argument in the
universal-argument code.. let me know what you think.  It's pretty
light on error checking at this point, mainly I'd like to know if it
prints the right thing, clears when appropriate, and if some cleaned
up version of this might be acceptable.

thanks,
Greg


Index: Src/Zle/zle_misc.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/Zle/zle_misc.c,v
retrieving revision 1.58
diff -u -r1.58 zle_misc.c
--- Src/Zle/zle_misc.c	24 Apr 2009 09:00:38 -0000	1.58
+++ Src/Zle/zle_misc.c	12 Nov 2009 23:02:11 -0000
@@ -710,15 +710,63 @@
 }
 
 /**/
+char *
+int2string(char *buf, int i, int base)
+{
+    char *b = buf;
+    int p, v;
+
+    if (i < 0) {
+        *b++ = '-';
+        i = -1;
+    }
+
+    for (p = 1; p <= i; p *= base)
+        ;
+
+    while (p > 1) {
+        p /= base;
+        v = i / p;
+        i -= v * p;
+        *b++ = (v < 10) ? v+'0' : v-10+'a';
+    }
+
+    *b = (char)0;
+    return buf;
+}
+
+/**/
+void
+showarg(int digcnt, int pref, int minus)
+{
+    char msg[100], buf[100];
+
+    if (minus < 0 && digcnt <= 1)
+        strcpy(msg, "arg: -");
+    else if (digcnt)
+        sprintf(msg, "arg: %s", int2string(buf, minus * (pref ? pref : 1), zmod.base));
+    else
+        sprintf(msg, "arg: %sx", int2string(buf, 4*zmod.tmult, zmod.base));
+
+    if (zmod.base != 10)
+        sprintf(msg+strlen(msg), " [base %d]", zmod.base);
+
+    showmsg(msg);
+    zrefresh();
+}
+
+/**/
 int
 universalargument(char **args)
 {
     int digcnt = 0, pref = 0, minus = 1, gotk;
+
     if (*args) {
 	zmod.mult = atoi(*args);
 	zmod.flags |= MOD_MULT;
 	return 0;
     }
+
     /*
      * TODO: this is quite tricky to do when trying to maintain
      * compatibility between the old input system and Unicode.
@@ -734,16 +782,19 @@
      *
      * Hence for now this remains byte-by-byte.
      */
+    showarg(digcnt, pref, minus);
     while ((gotk = getbyte(0L, NULL)) != EOF) {
 	if (gotk == '-' && !digcnt) {
 	    minus = -1;
 	    digcnt++;
+            showarg(digcnt, pref, minus);
 	} else {
 	    int newdigit = parsedigit(gotk);
 
 	    if (newdigit >= 0) {
 		pref = pref * zmod.base + newdigit;
 		digcnt++;
+                showarg(digcnt, pref, minus);
 	    } else {
 		ungetbyte(gotk);
 		break;
@@ -756,6 +807,8 @@
 	zmod.tmult *= 4;
     zmod.flags |= MOD_TMULT;
     prefixflag = 1;
+    showmsg("");
+    zrefresh();
     return 0;
 }
 


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

end of thread, other threads:[~2009-11-12 23:15 UTC | newest]

Thread overview: 17+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-11-11  0:00 the function to show a digit argument while it is being typed Dmitry Bolshakov
2009-11-11  2:22 ` Mikael Magnusson
2009-11-11  4:47   ` Bart Schaefer
2009-11-11  6:17     ` Mikael Magnusson
2009-11-11  8:14       ` Bart Schaefer
2009-11-11 16:07         ` Greg Klanderman
2009-11-11 17:27           ` Mikael Magnusson
2009-11-11 17:45           ` Bart Schaefer
2009-11-11 18:44             ` Greg Klanderman
2009-11-11 19:46               ` Bart Schaefer
2009-11-11 21:44                 ` Greg Klanderman
2009-11-11 22:14                   ` Greg Klanderman
2009-11-12  4:03                     ` Bart Schaefer
2009-11-12 18:38             ` Greg Klanderman
2009-11-12 23:06             ` Greg Klanderman
2009-11-11  3:56 ` Bart Schaefer
2009-11-11  6:28 ` Mikael Magnusson

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).