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

Re: A ZLE widget for calculator



sure thing, much cleaner and simpler. Thanks!

if (( ! ${+aliases[=]} )); 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_hist="${BUFFER}"
         # add quotes
         BUFFER="= ${(q-)${${BUFFER#=}# }}"
      fi
   }

   # as per Bart Schaefer's comment
   add-zle-hook-widget line-finish quote_expression

   ...
}

Next thing: force my luck, and make it also do stuff like
printf “the answer is %s\n” $(= 4 * 3)

Kind regards, erik.
Erik Wybouw



On 28 Mar 2026, at 00:49, Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx> wrote:

On Fri, Mar 27, 2026 at 2:05 PM Erik Wybouw <erik.wybouw@xxxxxxxxx> wrote:

Thank you for the inspiration!

I just augmented it with a trick I use to chain accept-line handlers (my own, and zsh_autosuggest)

You might consider doing this with
 add-zle-hook-widget line-finish quote_expression
instead.  That's actually designed for chaining a series of actions.
It does execute after accept-line, but before handing the buffer over
for execution, so you can do most things you would otherwise do in an
accept-line override.  The one thing it can't do is skip the
accept-line entirely.



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