Skip to content

Commit

Permalink
Let most widgets remove history line suffix
Browse files Browse the repository at this point in the history
Resolves #370.
  • Loading branch information
marlonrichert committed Nov 20, 2021
1 parent bfa331e commit 41bed13
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
19 changes: 14 additions & 5 deletions functions/completion/_autocomplete.history_lines
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,21 @@ _autocomplete.history_lines() {
# functions -T _autocomplete.history_lines

_autocomplete.history_lines.suffix() {
case $KEYS in
( [$ZLE_SPACE_SUFFIX_CHARS$ZLE_REMOVE_SUFFIX_CHARS] )
case $WIDGET in
( (|.|*-)(hist(|ory)|search|delete|kill)(|-*) )
;;
( (|.)(self-insert(|-*)|magic-space) )
case $KEYS[-1] in

This comment has been minimized.

Copy link
@amomchilov

amomchilov Jul 8, 2022

Hey @marlonrichert, could you help me understand how this matches for left/right arrow keys?

I would have expected something like \eOC and \eOD to be in $ZLE_SPACE_SUFFIX_CHARS or $ZLE_REMOVE_SUFFIX_CHARS, but that's not the case. (well that wouldn't quite work because these strings chars' are each a member of the character class here in [ ], whereas \eOC is more than 1 character, but you get my point)

How does this work?

This comment has been minimized.

Copy link
@marlonrichert

marlonrichert Jul 8, 2022

Author Owner

It’s handled on lines 138-139, that is, if none of the conditions above are matched, then the suffix is removed by default. No explicit check for arrow keys is made.

However, before that, in the next file on lines 21-24, we define that left and right arrow will always exit the menu, by placing a . in front of their names in the menuselect keymap.

This comment has been minimized.

This comment has been minimized.

Copy link
@amomchilov

amomchilov Jul 9, 2022

Once again, thank you for all this context, I'm learning a lot!

any other zle function not listed leaves menu selection and executes that function. It is possible to make widgets in the above list do the same by using the form of the widget with a ‘.’ in front. For example, the widget ‘.accept-line’ has the effect of leaving menu selection and accepting the entire command line.

Wow that is incredibly freakin' subtle.

On an unrelated note: what's it like getting down and dirty in the depths of ZSH like this? You clearly have a knack for it, but I'm finding myself perpetually perplexed by the intricacies of magical single-character flags all over the place.

The "parameter expansion" (why is it even called that, it seems to operate on "variables" not "parameters", shouldn't it be called "variable expansion"?) is the worst offender I've seen yet. Reading them is completely unintellgible without the decoder ring you get in the cracker jack box (the docs :') ). Is this something you just get used to with time?

This comment has been minimized.

Copy link
@marlonrichert

marlonrichert Jul 10, 2022

Author Owner

“Parameter” just happens to be Zsh’s name for what many other languages call “variables” or “constants”. I think it comes from the practice in Zsh to prefer using functions instead of scripts.

I often use completion to look up the meaning of each flag. For more detailed descriptions, see this chapter of the manual. And yes, you get used to it over time. 🙂 The ones that are used often, you’ll learn to recognize quite soon.

( [$ZLE_SPACE_SUFFIX_CHARS$ZLE_REMOVE_SUFFIX_CHARS] )
LBUFFER=$LBUFFER[1,-1-$1]
;|
( [$ZLE_SPACE_SUFFIX_CHARS] )
LBUFFER+=' '
;;
esac
;;
( * )
LBUFFER=$LBUFFER[1,-1-$1]
;|
( [$ZLE_SPACE_SUFFIX_CHARS] )
LBUFFER+=' '
;;
esac
}
Expand Down
2 changes: 0 additions & 2 deletions functions/widget/.autocomplete.history-search.zle-widget
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ ${0}.context() {
${0}() {
local -P lbuffer="$LBUFFER" rbuffer="$RBUFFER"
local -P keymap_menuselect="$( builtin bindkey -M menuselect -L )"
typeset -g ZLE_REMOVE_SUFFIX_CHARS=${ZLE_REMOVE_SUFFIX_CHARS/ }
{
builtin bindkey -M menuselect -s \
'^R' '^_^_^R' \
Expand All @@ -36,7 +35,6 @@ ${0}() {
builtin zle _history_search

} always {
ZLE_REMOVE_SUFFIX_CHARS+=' '
builtin bindkey -M menuselect -r '^R' '^S' '\e'{\[,O}{D,C}
eval "$keymap_menuselect"
if [[ $BUFFER == $lcontext$rcontext ]] then
Expand Down

0 comments on commit 41bed13

Please sign in to comment.