zsh-users
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: Budi <budikusasi@gmail.com>
Cc: Zsh Users <zsh-users@zsh.org>
Subject: Re: widget function must have zle codes
Date: Thu, 17 Aug 2023 21:42:08 -0700	[thread overview]
Message-ID: <CAH+w=7a7XyVCwGHUJJyEQbB6OXCHwy--43dK905XuE9nSxCTNQ@mail.gmail.com> (raw)
In-Reply-To: <CAH0GyZC-1jSx1xs4ZDjH4VLo3PEzUmtqTQF4uOnNc0nrTf=z+Q@mail.gmail.com>

On Thu, Aug 17, 2023 at 6:27 PM Budi <budikusasi@gmail.com> wrote:
>
> bindkey -s "^m" "\e[H   2>/dev/null [[ $- =~ x ]]&&{ set +x;echo Trace
> OFF ;} ||{ set -x;echo Trace ON;};print -s '\e[F'\r\e[A"

To the best of my ability to interpret that ... literally:

binding-for-ctrl-m () {
  zle .beginning-of-line
  zle -I
  {
  if [[ -o xtrace ]]; then
    { set +x; echo Trace OFF }
  else
    { set -x; echo Trace ON }
  fi
  zle .end-of-line
  zle .accept-and-hold
  } 2>/dev/null
}
zle -N binding-for-ctrl-m
bindkey ^m binding-for-ctrl-m

This "works" but there's a problem not of your making:  ZLE widgets
are not allowed to change the global setting of XTRACE -- it is always
saved on entry to ZLE and restored again on exit -- so the set -x or
+x in the widget is useless.

Before I fix that, let me point out that the beginning/end-of-line
motions are not needed, I included them just to translate as closely
as possible.  Even with bind -x in bash the \e[H is not needed; I'm
not sure about the \e[F.  The "zle -I" is what tells zle that some
other output is coming, so it should get out of the way and then
repaint when it gets control again.  bind -x does that too, at least
in some simple experiments.

To avoid the XTRACE problem, if I'm following correctly what you
expect the \e[F\r\e[A part to accomplish, the shortest solution is
probably:

binding-for-ctrl-m() {
  zle -I
  print Trace ON
  eval "() { setopt localoptions xtrace; $BUFFER }"
  print Trace OFF
}

If you actually require the set -x to take global effect but be
invisible to the history etc., it gets a lot more complicated, so I
won't go there yet.

As a last note, "print -s" writes to the history list, not to the
editor, so you would never use that for editor control keystrokes.  If
you really need to send the key binding for an action instead of
calling it with "zle widget-name", that's what "zle -U" is for, but
anything sent that way is not looked at until the current widget
returns.


      parent reply	other threads:[~2023-08-18  4:43 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-18  0:11 Budi
2023-08-18  1:07 ` Bart Schaefer
2023-08-18  1:27   ` Budi
2023-08-18  1:28     ` Budi
2023-08-18  4:42     ` Bart Schaefer [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAH+w=7a7XyVCwGHUJJyEQbB6OXCHwy--43dK905XuE9nSxCTNQ@mail.gmail.com' \
    --to=schaefer@brasslantern.com \
    --cc=budikusasi@gmail.com \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).