Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: A ZLE widget for calculator
- X-seq: zsh-users 30435
- From: Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx>
- To: zsh-users@xxxxxxx
- Subject: Re: A ZLE widget for calculator
- Date: Fri, 6 Feb 2026 18:51:27 -0800
- Arc-authentication-results: i=1; mx.google.com; arc=none
- Arc-message-signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=9wigwC+D99qTrJaIzmRlXHZNGaq7kiy0+Yg6u9G+uzc=; fh=yk3c4scJWo86Za4IR1HBNZw2a+GkUkfVQ7Fs3jnanfg=; b=d1EjkFk6aaW/rqBFolZ0WH5ZhOJmO4ZJ8mc0uq+4ZlY0toPY1SPGBdIgWtMfpZuYj2 wagU02WFqSsScx1PDGVuDx2M1fcf6C/qLVSXaUO2egjMSc9TfnpBcNgaQmSXBWXBwrqr 8jf0uRFa7/mI44uE6a32m6imFVyQejH6egWwr7hxByY61zmuM2kSkzpEC+EjuGqnZvkZ eX//1Uso1fQKtZCcw5Bhdj/2NfpFszVw9ovML5fMm0gUA2L/nOiakmU+DHrsZZkw4Fmp 9g7AlJX0t6PzNE5kVjKPO9BBgUJKdIsl2vptm5sPcFpN9UNarZPNl+2+eRI4gMOq2rFx P2Fg==; darn=zsh.org
- Arc-seal: i=1; a=rsa-sha256; t=1770432701; cv=none; d=google.com; s=arc-20240605; b=lSOPT3H9lnaJpXbPyDwkngibMSaUfgt/RCKHSECAg18T8rur1pgXhRwhwqyHohutYC Zw9xhxp7USutwfMFkrBj5wleyOygemezOlqygME6b2+mNGjopeCbf2/qm5lFadRQ/Q/b 5vEsqcglw0c2PB6VzTuNxdUhFv6J6kQAtSRVxh1a8wrRGhEuvlvd0pagpsaa/C6RG+si BZ+BFHq4qIHF0l6poKqm5jVN5mnzA6FoQKD5XFZ9sr+KqQYmm1bpd/I1y3hSEfx+AnWY uHiRRleULDZDrxEvFskKncvJRT9O5ao002Pmmb9aIpJz3zta4zXmdm2duWHL2u+1N1i4 Svmw==
- Archived-at: <https://zsh.org/users/30435>
- In-reply-to: <1992876564.527215.1770199206830@mail.virginmedia.com>
- List-id: <zsh-users.zsh.org>
- References: <a1738285-db4e-4658-8148-ea694387e6a8@luffy.cx> <867657643.489413.1770121301447@mail.virginmedia.com> <d606df42-d003-42c0-b921-729575fec8c5@luffy.cx> <1992876564.527215.1770199206830@mail.virginmedia.com>
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