Re: A ZLE widget for calculator
Erik Wybouw <[email protected]> Fri, 27 Mar 2026 22:04:56 +0100
| Newsgroups | gmane.comp.shells.zsh.user |
|---|---|
| Message-ID | <CABPiPahLaS8Xn9n6ZjJviX6vAgbv1MThHWwsavB97kS=EJS4AA@mail.gmail.com> |
Thank you for the inspiration!
I just augmented it with a trick I use to chain accept-line handlers (my
own, and zsh_autosuggest):
If you read the fine print, you will see that .accept-line is the
rock-bottom handler, not just anything that was bound before.
if [[ -z ${CALCPREVACCEPTLINE} ]]; then
# only do this once, even if the file is sourced multiple times
function quote_expression() {
if [[ ${BUFFER} =~ ^=.* ]]; then
# used below in history hack
typeset -g calc_expr="${BUFFER}"
# add quotes
BUFFER="= ${(q-)${${BUFFER#=}# }}"
fi
if [[ -z ${CALCPREVACCEPTLINE} ]]; then
# bail out
zle .accept-line
else
# up the chain
"${CALCPREVACCEPTLINE}" "${@}"
fi
}
# save the original accept-line widget, so we can call it from our
wrapper
CALCPREVACCEPTLINE=${widgets[accept-line]#*:}
zle -N accept-line quote_expression
fi
Kind regards, erik.
On Tue, Mar 24, 2026 at 10:53 PM Vincent Bernat <[email protected]> wrote:
> On 2026-02-08 16:06, Vincent Bernat wrote:
>
> >> 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.
> >
> > Thanks, it works perfectly!
>
> FI, I have written a short blog post about this:
> https://vincent.bernat.ch/en/blog/2026-zsh-calculator
>
> Thanks again!
>
>