Skip to content

Commit

Permalink
fix a bug when the buffer ends in a backslash
Browse files Browse the repository at this point in the history
  • Loading branch information
romkatv committed Nov 13, 2023
1 parent 1756a7f commit 4b23567
Showing 1 changed file with 33 additions and 25 deletions.
58 changes: 33 additions & 25 deletions zsh-no-ps2
Original file line number Diff line number Diff line change
@@ -1,35 +1,43 @@
#!/usr/bin/env zsh

builtin emulate -L zsh
setopt local_options no_err_return no_err_exit

builtin local -r func=zsh-no-ps2-test

if (( ${+functions[$func]} )); then
builtin unfunction -- $func
fi
# Succeeds if $1 is a well-formed command.
() {
# If it ends with an odd number of backslashes, it's malformed.
() {
builtin emulate -L zsh -o extended_glob
[[ $1 == (|*[^\\])(\\\\)#\\ ]]
} "$1" && builtin return 1

if functions[$func]=$PREBUFFER$BUFFER 2>/dev/null &&
(( ${+functions[$func]} )); then
builtin unfunction -- $func
if [[ -v functions[-zsh-no-ps2-test] ]]; then
builtin unfunction -- -zsh-no-ps2-test
fi
functions[-zsh-no-ps2-test]="$1" 2>/dev/null || builtin return 1
[[ -v functions[-zsh-no-ps2-test] ]] || builtin return 1
builtin unfunction -- -zsh-no-ps2-test
# This suffix allows us to detect two tricky cases: "for x" and "<<END".
# Each of these on their own is a valid function body but neither can
# be executed as a command in interactive shell.
if functions[$func]=$PREBUFFER$BUFFER$'\ndo\ndone' 2>/dev/null &&
(( ${+functions[$func]} )) ; then
builtin unfunction -- $func
else
builtin local w
builtin zstyle -s :zsh-no-ps2: accept-line w || w=accept-line
if [[ -n $w ]]; then
builtin zle -- $w
fi
builtin return
fi
fi
functions[-zsh-no-ps2-test]="$1"$'\ndo\ndone' 2>/dev/null || builtin return 0
[[ -v functions[-zsh-no-ps2-test] ]] || builtin return 0
builtin unfunction -- -zsh-no-ps2-test
builtin return 1
} "$PREBUFFER$BUFFER"

() {
if (( $? )); then
# We do it this way instead of the plain LBUFFER+=$'\n' in order
# to make this plugin work with zsh-autosuggestions.
local -h KEYS=$'\n'
# to make this plugin work with zsh-autosuggestions. Unfortunately, this
# means that binding zsh-no-ps2 to anything other than Enter won't work.
# This can be fixed: we just need to define our own widget that does
# LBUFFER+=$'\n' and invoke it here.
#
# TODO: do what the above says.
builtin zle self-insert-unmeta
}
else
builtin local w
builtin zstyle -s :zsh-no-ps2: accept-line w || w=accept-line
if [[ -n $w ]]; then
builtin zle -- "$w"
fi
fi

0 comments on commit 4b23567

Please sign in to comment.