diff --git a/zsh-no-ps2 b/zsh-no-ps2 index cb9a39f..216c2e5 100644 --- a/zsh-no-ps2 +++ b/zsh-no-ps2 @@ -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 "</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