-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix a bug when the buffer ends in a backslash
- Loading branch information
Showing
1 changed file
with
33 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |