zsh-users
 help / color / mirror / code / Atom feed
From: Daniel Shahaf <d.s@daniel.shahaf.name>
To: zsh-users@zsh.org
Subject: Re: prevent some lines directly coming from the history from being executed
Date: Thu, 2 Jun 2022 10:19:05 +0000	[thread overview]
Message-ID: <20220602101905.GE28173@tarpaulin.shahaf.local2> (raw)
In-Reply-To: <1388910908.482590.1653901645100@mail2.virginmedia.com>

v3.  This should incorporate all feedback up to this point.

Cheers,

Daniel

diff --git a/Doc/Zsh/zle.yo b/Doc/Zsh/zle.yo
index 2d033a0a1..7266aa80a 100644
--- a/Doc/Zsh/zle.yo
+++ b/Doc/Zsh/zle.yo
@@ -746,16 +746,67 @@ ifnzman(noderef(Standard Widgets)).
 Other built-in widgets can be defined by other modules (see
 ifzman(zmanref(zshmodules))\
 ifnzman(noderef(Zsh Modules))\
-).  Each built-in widget has two names: its normal canonical name, and the
-same name preceded by a `tt(.)'.  The `tt(.)' name is special: it can't be
-rebound to a different widget.  This makes the widget available even when
-its usual name has been redefined.
+).
 
 User-defined widgets are defined using `tt(zle -N)', and implemented
 as shell functions.  When the widget is executed, the corresponding
 shell function is executed, and can perform editing (or other) actions.
-It is recommended that user-defined widgets should not have names
-starting with `tt(.)'.
+
+cindex(widgets, shadowing standard)
+cindex(widgets, overriding standard)
+cindex(shadowing standard widgets)
+cindex(overriding standard widgets)
+User-defined widgets may shadow (override) standard widgets: for instance,
+after `tt(zle -N self-insert) var(myfunc)', any invocation of the standard
+tt(self-insert) widget (including every keypress that appends an alphanumeric
+or space character to the command line) would invoke the user-defined
+function var(myfunc) rather than the standard implementation of that widget.
+
+In order for the standard implementations to be available to be called by
+user-defined widgets or bound to keypresses even when shadowed, 
+each standard widget `var(foo)' is also available under the
+name `tt(.)var(foo)'.
+These names are reserved and cannot be created as user-defined widgets,
+so for compatibility with possible future revisions of the shell,
+it is recommended that users avoid naming widgets with a leading `tt(.)'.
+
+Continuing the example, var(myfunc) would typically invoke the built-in widget
+it is replacing using the dot-prefix syntax.  For instance:
+
+em(Example #1)
+
+example(self-insert-vowels-twice+LPAR()+RPAR() {
+  [[ $KEYS == [aeiou] ]] && zle .self-insert -- "$@"
+  zle .self-insert -- "$@" 
+}
+zle -N self-insert self-insert-vowels-twice)
+
+This example causes vowels to be inserted twice.  For instance, typing
+`tt(hello)' would insert `tt(heelloo)'.
+
+Note the use of the dot-prefix syntax.  If the tt(self-insert) widget had been
+invoked without the dot DASH()- that is, as `tt(zle self-insert -- "$@")' DASH()-
+then tt(self-insert-vowels-twice) would have been called again, effecting
+a bottomless recursion.
+
+em(Example #2)
+
+In a similar way, to perform a final action on all lines returned from Zle,
+the tt(accept-line) widget can be rebound to a function that performs an action
+on the current line, then calls the tt(.accept-line) widget to return the line
+to the shell.  For example, to change the default behaviour that pressing
+tt(<Enter>) on an empty line redraws the prompt (and runs tt(precmd) hooks,
+if any are configured), one may override accept-line as follows:
+
+example(accept-non-empty-lines+LPAR()+RPAR() {
+    [[ -z $PREBUFFER && -z $BUFFER ]] || zle .accept-line -- "$@"
+}
+zle -N accept-line accept-non-empty-lines)
+
+The special parameters used by these examples DASH()- tt($KEYS),
+tt($PREBUFFER), and tt($BUFFER) DASH()- are documented in the next
+section.
+
 
 texinode(User-Defined Widgets)(Standard Widgets)(Zle Widgets)(Zsh Line Editor)
 sect(User-Defined Widgets)


  parent reply	other threads:[~2022-06-02 10:43 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-24 15:47 Vincent Lefevre
2022-05-24 18:58 ` Bart Schaefer
2022-05-25  2:54   ` Vincent Lefevre
2022-05-25  3:59     ` Bart Schaefer
2022-05-25  8:49       ` Vincent Lefevre
2022-05-26  1:25         ` Bart Schaefer
2022-05-26 14:36           ` Vincent Lefevre
2022-05-26 15:53             ` Daniel Shahaf
2022-05-26 16:13               ` Peter Stephenson
2022-05-27 12:40                 ` Daniel Shahaf
2022-05-28  0:07                   ` Vincent Lefevre
2022-05-28 10:06                     ` Daniel Shahaf
2022-05-28 18:43                       ` Bart Schaefer
2022-05-29 22:55                         ` Daniel Shahaf
2022-05-30  4:04                           ` Bart Schaefer
2022-05-30  9:07                             ` Peter Stephenson
2022-06-02  9:59                               ` Daniel Shahaf
2022-06-02 10:19                               ` Daniel Shahaf [this message]
2022-06-02  9:59                             ` Daniel Shahaf
2022-05-30  9:02                       ` Vincent Lefevre
2022-06-02 10:17                         ` Daniel Shahaf
2022-06-02 13:54                           ` Vincent Lefevre
2022-05-26 16:37             ` Bart Schaefer

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=20220602101905.GE28173@tarpaulin.shahaf.local2 \
    --to=d.s@daniel.shahaf.name \
    --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).