Skip to content

Commit d32863d

Browse files
committed
fix: Update prompt after a syntax error.
Before this change, the prompt may be left to the special bash prompt after a syntax error, because Bash won't execute the DEBUG trap at all in such a scenario. At least recent ones won't; that might depend on the exact version of Bash. This change switches to a new scheme that recovers PS1 after bash has executed commands without relying on a trap. This means that PS1 may be printed, but after the special bash-completion marker, so the string will be skipped. PROMPT_COMMAND is still recovered as it was before this change, using a DEBUG trap, to avoid slow prompt command making completion unusable. The drawback of this approach is that that PS1 may not updated after a bash syntax error as it would normally. issue #79
1 parent 5205034 commit d32863d

File tree

2 files changed

+13
-36
lines changed

2 files changed

+13
-36
lines changed

bash-completion.el

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1536,7 +1536,7 @@ Return the status code of the command, as a number."
15361536
" __ebcpre; %s; __ebcret $?; "
15371537
"else "
15381538
" echo ==emacs==nopre=${BASH_VERSION}==.; "
1539-
" __ebcp=(\"$PS1\" \"$PROMPT_COMMAND\" $__ebcor);"
1539+
" __ebcp=(\"$PS1\" \"${__ebcpc:-$PROMPT_COMMAND}\" $__ebcor);"
15401540
" unset PS1 PROMPT_COMMAND __ebcor;"
15411541
"fi;\n"))
15421542
;; single process, define __ebcpre
@@ -1549,13 +1549,16 @@ Return the status code of the command, as a number."
15491549
" fi;"
15501550
" history -d $c &>/dev/null || true;"
15511551
"} ; function __ebcret {"
1552-
" __ebcret=$1;"
1553-
" return ${__ebcp[2]};"
1554-
"} ; function __ebctrap {"
1555-
" if [[ -n \"$__ebcret\" && ${#__ebcp[@]} -gt 0 ]]; then"
1552+
" local r=$1 e=${__ebcp[2]};"
15561553
" PS1=\"${__ebcp[0]}\";"
1557-
" PROMPT_COMMAND=\"${__ebcp[1]}\";"
1558-
" unset __ebcp __ebcret;"
1554+
" __ebcpc=\"${__ebcp[1]}\";"
1555+
" unset __ebcp;"
1556+
" echo \"==emacs==ret=$r==.\";"
1557+
" return $e;"
1558+
"} ; function __ebctrap {"
1559+
" if [[ ${#__ebcpc} -gt 0 ]]; then"
1560+
" PROMPT_COMMAND=\"${__ebcpc}\";"
1561+
" unset __ebcpc;"
15591562
" fi;"
15601563
"} ; "
15611564
"if [[ \"$(trap -p DEBUG)\" =~ trap\\ --\\ \\'(.*)\\'\\ DEBUG ]]; then "
@@ -1568,10 +1571,10 @@ Return the status code of the command, as a number."
15681571
" set +x; set +o emacs; set +o vi;"
15691572
" echo \"==emacs==bash=${BASH_VERSION}==.\";"
15701573
" if [[ ${#__ebcp[@]} = 0 ]]; then "
1571-
" __ebcp=(\"$PS1\" \"$PROMPT_COMMAND\" $__ebcor);"
1574+
" __ebcp=(\"$PS1\" \"${__ebcpc:-$PROMPT_COMMAND}\" $__ebcor);"
15721575
" fi;"
1573-
" PS1=" bash-completion--ps1 ";"
1574-
" unset PROMPT_COMMAND __ebcor;"
1576+
;;" PS1='==emacs==prompt=1==.';"
1577+
" unset PS1 PROMPT_COMMAND __ebcor;"
15751578
" __ebcnohistory 1;"
15761579
"} ; { __ebcpre; %s; __ebcret $?; }\n")))
15771580
commandline)))

test/bash-completion-integration-test.el

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -960,30 +960,4 @@ $ ")))))
960960
"101\n"
961961
"$ ")))))
962962

963-
(ert-deftest bash-completion-keep-existing-trap ()
964-
(bash-completion_test-with-shell-harness
965-
(concat ; .bashrc
966-
"calls=0\n"
967-
"function _calltrap {\n"
968-
" calls=$((calls+1))\n"
969-
"}\n"
970-
"trap _calltrap DEBUG\n"
971-
"PS1='\$ '")
972-
nil
973-
(bash-completion_test-send "n=$calls")
974-
(bash-completion_test-send "tru" 'complete)
975-
(bash-completion_test-send "fals" 'complete)
976-
(bash-completion_test-send "[[ $calls -gt $n ]] && echo ok")
977-
(bash-completion_test-send "trap -p DEBUG")
978-
(should (equal (bash-completion_test-buffer-string)
979-
(concat
980-
"$ n=$calls\n"
981-
"$ true\n"
982-
"$ false\n"
983-
"$ [[ $calls -gt $n ]] && echo ok\n"
984-
"ok\n"
985-
"$ trap -p DEBUG\n"
986-
"trap -- '_calltrap; __ebctrap' DEBUG\n"
987-
"$ ")))))
988-
989963
;;; bash-completion-integration-test.el ends here

0 commit comments

Comments
 (0)