Skip to content

Commit 5205034

Browse files
committed
fix: Keep existing trap when installing trap __ebctrap DEBUG
Before this change, any DEBUG trap installed before bash completion was overwritten. This change adds to the existing trap, so if the trap is: trap -- '__somefunc' DEBUG the complete trap ends up being: trap -- '__somefunc; __ebctrap' DEBUG fixes #78
1 parent d063742 commit 5205034

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

bash-completion.el

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1557,7 +1557,13 @@ Return the status code of the command, as a number."
15571557
" PROMPT_COMMAND=\"${__ebcp[1]}\";"
15581558
" unset __ebcp __ebcret;"
15591559
" fi;"
1560-
"} ; trap __ebctrap DEBUG ; function __ebcpre {"
1560+
"} ; "
1561+
"if [[ \"$(trap -p DEBUG)\" =~ trap\\ --\\ \\'(.*)\\'\\ DEBUG ]]; then "
1562+
" trap \"${BASH_REMATCH[1]}; __ebctrap\" DEBUG; "
1563+
"else "
1564+
" trap __ebctrap DEBUG; "
1565+
"fi; "
1566+
"function __ebcpre {"
15611567
" __ebcor=${__ebcor:-$?}; "
15621568
" set +x; set +o emacs; set +o vi;"
15631569
" echo \"==emacs==bash=${BASH_VERSION}==.\";"

test/bash-completion-integration-test.el

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -960,4 +960,30 @@ $ ")))))
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+
963989
;;; bash-completion-integration-test.el ends here

0 commit comments

Comments
 (0)