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

Re: A ZLE widget for calculator



On Wed, Feb 4, 2026 at 2:05 AM Peter Stephenson
<p.w.stephenson@xxxxxxxxxxxx> wrote:
>
> Changing the reorded history compared with what's actually executed
> seems a reasonable thing to have, given all the other things you can do
> with history.

I'm a little confused by this one:

On Mon, Feb 2, 2026 at 11:22 PM Vincent Bernat <bernat@xxxxxxxx> wrote:
>
> _vbe_calc_accept_line() {
>      if [[ $BUFFER =~ "= *" ]]; then
>          local expr=${BUFFER#= }
>          zle -I
>          command numbat -e "$expr"
>          print
>          print -s $BUFFER
>          BUFFER=""
>      else
>          zle .accept-line
>      fi
> }

The original problem statement as I understand it, is: When the buffer
starts with "= ", form a single quoted word to pass as an argument to
"numbat -e", but preserve the original buffer in the shell history.

The first implementation, which modifies $BUFFER and then calls
.accept-line, un-quotes and re-quotes the tail of the buffer.  You
said:
> a bit clunky to detect when something was quoted and undo it
The implementation above never handles the un-quote part.  If that was
required in the first implementation, why is that no longer necessary
there?  Won't unwanted extra quotes that appear in the original
$BUFFER be passed through to numbat via "$expr"?

I think the attached does what you want.  It's based on the
modify-$BUFFER + .accept-line variation because among other things
that has the advantage of working at the PS2 prompt.  It
unconditionally removes and re-adds quotes.  The original $BUFFER is
saved in a global variable which is then added to the history by the
preexec hook, after suppressing the automatic history with
zshaddhistory hook.  This could probably use a function instead of an
alias for "=", but I didn't change that.
#autoload

autoload add-zsh-hook

_vbe_calc_history() {
	return ${+_vbe_calc_active}
}
_vbe_calc_preexec() {
	if ((${+_vbe_calc_active}))
	then print -S "$_vbe_calc_active"
	fi
	unset _vbe_calc_active
	return 0
}
_vbe_calc_accept() {
	local expr
    case $BUFFER in
        "= "*)
        	typeset -g _vbe_calc_active="$BUFFER"
        	expr=${(Q)${BUFFER#= }}
        	BUFFER="= ${(q-)expr}" ;;
   	esac
    zle .accept-line
}
add-zsh-hook preexec _vbe_calc_preexec
add-zsh-hook zshaddhistory _vbe_calc_history
zle -N accept-line _vbe_calc_accept
aliases[=]='numbat -e'


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