Skip to content

Commit 762f28f

Browse files
committed
fix: Avoid commands in PS1 causing issues.
With the fix of issue #79, PS1 is displayed after the bash completion mark, but later on ignored. This doesn't work if PS1 executes commands, as the command execution will trigger the TRAP. This change evaluates PS1 beforehand, so it won't confuse the trap. With this approach, an outdated PS1 may be displayed after a Bash syntax error, but this is temporary, as executing any command fixes it. issue #80
1 parent d32863d commit 762f28f

File tree

2 files changed

+66
-12
lines changed

2 files changed

+66
-12
lines changed

bash-completion.el

Lines changed: 11 additions & 12 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\" \"${__ebcpc:-$PROMPT_COMMAND}\" $__ebcor);"
1539+
" __ebcp=(\"$PS1\" \"$PROMPT_COMMAND\" $__ebcor);"
15401540
" unset PS1 PROMPT_COMMAND __ebcor;"
15411541
"fi;\n"))
15421542
;; single process, define __ebcpre
@@ -1549,16 +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-
" local r=$1 e=${__ebcp[2]};"
1553-
" PS1=\"${__ebcp[0]}\";"
1554-
" __ebcpc=\"${__ebcp[1]}\";"
1555-
" unset __ebcp;"
1556-
" echo \"==emacs==ret=$r==.\";"
1557-
" return $e;"
1552+
" PS1=\"$(eval \"echo \\\"${__ebcp[0]}\\\"\")\";"
1553+
" echo >>/tmp/debug \">>$PS1<<\";"
1554+
" __ebcret=$1;"
1555+
" echo \"==emacs==ret=${__ebcret}==.\";"
1556+
" return ${__ebcp[2]};"
15581557
"} ; function __ebctrap {"
1559-
" if [[ ${#__ebcpc} -gt 0 ]]; then"
1560-
" PROMPT_COMMAND=\"${__ebcpc}\";"
1561-
" unset __ebcpc;"
1558+
" if [[ -n \"$__ebcret\" && ${#__ebcp[@]} -gt 0 ]]; then"
1559+
" PS1=\"${__ebcp[0]}\";"
1560+
" PROMPT_COMMAND=\"${__ebcp[1]}\";"
1561+
" unset __ebcp __ebcret;"
15621562
" fi;"
15631563
"} ; "
15641564
"if [[ \"$(trap -p DEBUG)\" =~ trap\\ --\\ \\'(.*)\\'\\ DEBUG ]]; then "
@@ -1571,9 +1571,8 @@ Return the status code of the command, as a number."
15711571
" set +x; set +o emacs; set +o vi;"
15721572
" echo \"==emacs==bash=${BASH_VERSION}==.\";"
15731573
" if [[ ${#__ebcp[@]} = 0 ]]; then "
1574-
" __ebcp=(\"$PS1\" \"${__ebcpc:-$PROMPT_COMMAND}\" $__ebcor);"
1574+
" __ebcp=(\"$PS1\" \"${PROMPT_COMMAND}\" $__ebcor);"
15751575
" fi;"
1576-
;;" PS1='==emacs==prompt=1==.';"
15771576
" unset PS1 PROMPT_COMMAND __ebcor;"
15781577
" __ebcnohistory 1;"
15791578
"} ; { __ebcpre; %s; __ebcret $?; }\n")))

test/bash-completion-integration-test.el

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -960,4 +960,59 @@ $ ")))))
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+
989+
(ert-deftest bash-completion-prompt-after-syntax-error-issue-79 ()
990+
(bash-completion_test-with-shell-harness
991+
(concat ; .bashrc
992+
"PS1='\$ '")
993+
nil
994+
(goto-char (point-max))
995+
(let ((start (point)))
996+
(insert "function foobar")
997+
(completion-at-point)
998+
(insert " {}")
999+
(comint-send-input)
1000+
(bash-completion_test-wait-for-prompt start))))
1001+
1002+
(ert-deftest bash-completion-command-in-prompt-issue-80 ()
1003+
(bash-completion_test-with-shell-harness
1004+
(concat ; .bashrc
1005+
"PS1='`echo boo`$ '")
1006+
nil
1007+
(goto-char (point-max))
1008+
(let ((start (point)))
1009+
(should
1010+
(equal
1011+
"function "
1012+
(bash-completion_test-complete "functi")))
1013+
(bash-completion_test-wait-for-prompt start))
1014+
(should (equal (bash-completion_test-buffer-string)
1015+
(concat
1016+
"boo$ function")))))
1017+
9631018
;;; bash-completion-integration-test.el ends here

0 commit comments

Comments
 (0)