emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[nongnu] elpa/bash-completion ebaf38ac39 2/6: Remove the need for a spec


From: ELPA Syncer
Subject: [nongnu] elpa/bash-completion ebaf38ac39 2/6: Remove the need for a special setup in single-process mode.
Date: Thu, 26 Jan 2023 15:58:45 -0500 (EST)

branch: elpa/bash-completion
commit ebaf38ac39e08a4fc584a573a3573a3752d4253b
Author: Stephane Zermatten <szermatt@gmx.net>
Commit: Stephane Zermatten <stephane@fuzzy.zia>

    Remove the need for a special setup in single-process mode.
    
    Before this change bash-completion-send would rely on custom functions,
    defined during setup, when bash-completion-use-separate-processes is
    nil. This meant that if, for some reason setup hadn't run, communication
    with the Bash process would just not be possible and the completion
    would freeze.
    
    This typically happened when a bash subprocess is created, as Emacs
    would think that setup was run when it wasn't. With this change,
    completion still doesn't work when called from bash subprocessed, but it
    at least doesn't freeze.
    
    This change required changing the prompt (PS1) used when communicating
    with bash to only include printable characters, to avoid issues when -o
    vi or -o emacs is set in the configuration.
---
 bash-completion.el           | 88 ++++++++++++++++----------------------------
 test/bash-completion-test.el |  6 +--
 2 files changed, 34 insertions(+), 60 deletions(-)

diff --git a/bash-completion.el b/bash-completion.el
index 6418723395..b5915f4f3c 100644
--- a/bash-completion.el
+++ b/bash-completion.el
@@ -270,7 +270,7 @@ Bash processes.")
 (defconst bash-completion-special-chars "[ -$&-*,:-<>?[-^`{-}]"
   "Regexp of characters that must be escaped or quoted.")
 
-(defconst bash-completion--ps1 "'\t$?\v'"
+(defconst bash-completion--ps1 "'==emacs==ret=$?==.'"
   "Value for the special PS1 prompt set for completions, quoted.")
 
 (eval-when-compile
@@ -1195,57 +1195,6 @@ completion in these cases."
            (shell (if process (bash-completion--current-shell))))
       (when (and shell (bash-completion-starts-with shell "bash"))
         (unless (process-get process 'setup-done)
-          ;; The following disables the emacs and vi options. This
-          ;; cannot be done by bash-completion-send as these options
-          ;; interfere with bash-completion-send detecting the end
-          ;; of a command. It disables prompt to avoid interference
-          ;; from commands run by prompts.
-          (let* ((history-unclutter-cmd
-                  (concat
-                   "if [[ ${BASH_VERSINFO[0]} -eq 5 && ${BASH_VERSINFO[1]} -ge 
1 || ${BASH_VERSINFO[0]} -gt 5 ]]; then"
-                   "  history -d $HISTCMD &>/dev/null || true;"
-                   "else"
-                   "  history -d $((HISTCMD - 1)) &>/dev/null || true;"
-                   "fi")))
-            (comint-send-string
-             process
-             (concat
-              "set +o emacs;"
-              "set +o vi;"
-              "if [[ -z \"$__emacs_complete_ps1\" ]]; then"
-              "  __emacs_complete_ps1=\"$PS1\";"
-              "  __emacs_complete_pc=\"$PROMPT_COMMAND\";"
-              "fi;"
-              "PS1='' PROMPT_COMMAND='';"
-              history-unclutter-cmd "\n"))
-
-            ;; The following is a bootstrap command for
-            ;; bash-completion-send itself.
-            (bash-completion-send
-             (concat
-              "function __emacs_complete_pre_command {"
-              "  if [[ -z \"$__emacs_complete_ps1\" ]]; then"
-              "    __emacs_complete_ps1=\"$PS1\";"
-              "    __emacs_complete_pc=\"$PROMPT_COMMAND\";"
-              "  fi;"
-              "  PROMPT_COMMAND=__emacs_complete_prompt;"
-              "  " history-unclutter-cmd ";"
-              "} &&"
-              "function __emacs_complete_prompt {"
-              "  PS1=" bash-completion--ps1 ";"
-              "  PROMPT_COMMAND=__emacs_complete_recover_prompt;"
-              "} &&"
-              "function __emacs_complete_recover_prompt {"
-              "  local r=$?;"
-              "  PS1=\"${__emacs_complete_ps1}\";"
-              "  PROMPT_COMMAND=\"${__emacs_complete_pc}\";"
-              "  unset __emacs_complete_ps1 __emacs_complete_pc;"
-              "  if [[ -n \"$PROMPT_COMMAND\" ]]; then"
-              "    (exit $r); eval \"$PROMPT_COMMAND\";"
-              "  fi;"
-              "} &&"
-              "__emacs_complete_pre_command")
-             process))
           (bash-completion--setup-bash-common process))
         process))))
 
@@ -1496,8 +1445,33 @@ Return the status code of the command, as a number."
          (send-string (if bash-completion-use-separate-processes
                           #'process-send-string
                         #'comint-send-string))
-         (pre-command (unless bash-completion-use-separate-processes
-                        "__emacs_complete_pre_command; "))
+         (pre-command
+          (unless bash-completion-use-separate-processes
+            (concat
+             "set +o emacs; set +o vi;"
+             "if [[ -z \"${__emacs_complete_ps1}\" ]]; then "
+             " __emacs_complete_ps1=\"$PS1\";"
+             " __emacs_complete_pc=\"$PROMPT_COMMAND\";"
+             "fi;"
+             "PROMPT_COMMAND=" ;; set a temporary prompt
+             (bash-completion-quote
+              (concat "PS1=" bash-completion--ps1 ";"
+                      "PROMPT_COMMAND=" ;; recover prompt
+                      (bash-completion-quote
+                       (concat
+                        "__emacs_complete_r=$?;"
+                        "PS1=\"${__emacs_complete_ps1}\";"
+                        "PROMPT_COMMAND=\"${__emacs_complete_pc}\";"
+                        "unset __emacs_complete_ps1 __emacs_complete_pc;"
+                        "if [[ -n \"$PROMPT_COMMAND\" ]]; then"
+                        "  (exit $__emacs_complete_r); eval 
\"$PROMPT_COMMAND\";"
+                        "fi;"))))
+             ;; remove this command from history
+             ";if [[ ${BASH_VERSINFO[0]} -eq 5 && ${BASH_VERSINFO[1]} -ge 1 || 
${BASH_VERSINFO[0]} -gt 5 ]]; then"
+             "  history -d $HISTCMD &>/dev/null || true;"
+             "else"
+             "  history -d $((HISTCMD - 1)) &>/dev/null || true;"
+             "fi;")))
          (complete-command (concat pre-command commandline "\n")))
     (setq bash-completion--debug-info
           (list (cons 'commandline complete-command)
@@ -1507,7 +1481,7 @@ Return the status code of the command, as a number."
     (with-current-buffer (bash-completion--get-buffer process)
       (erase-buffer)
       (funcall send-string process complete-command)
-      (unless (bash-completion--wait-for-regexp process "\t-?[[:digit:]]+\v" 
timeout)
+      (unless (bash-completion--wait-for-regexp process 
"==emacs==ret=-?[[:digit:]]+==." timeout)
         (push (cons 'error "timeout") bash-completion--debug-info)
         (push (cons 'buffer-string (buffer-substring-no-properties (point-min) 
(point-max)))
               bash-completion--debug-info)
@@ -1520,8 +1494,8 @@ Return the status code of the command, as a number."
             (delete-region (match-beginning 0) (line-beginning-position 2)))))
       (let ((status (string-to-number
                      (buffer-substring-no-properties
-                      (1+ (point))
-                      (1- (line-end-position)))))
+                      (+ (point) 13)
+                      (- (line-end-position) 1))))
             (wrapped-status (bash-completion--parse-side-channel-data 
"wrapped-status")))
         (push (cons 'status status) bash-completion--debug-info)
         (push (cons 'wrapped-status wrapped-status) 
bash-completion--debug-info)
diff --git a/test/bash-completion-test.el b/test/bash-completion-test.el
index ad792b4893..2d6a7f34f3 100644
--- a/test/bash-completion-test.el
+++ b/test/bash-completion-test.el
@@ -508,18 +508,18 @@ Return (const return-value new-buffer-content)"
 (ert-deftest bash-completion-send-test ()
   (should (equal
           (cons 0 "line1\nline2\n")
-          (bash-completion-test-send "line1\nline2\n\t0\v")))
+          (bash-completion-test-send "line1\nline2\n==emacs==ret=0==.")))
 
   ;; command failed"
   (should (equal
           (cons 1 "line1\nline2\n")
-          (bash-completion-test-send "line1\nline2\n\t1\v")))
+          (bash-completion-test-send "line1\nline2\n==emacs==ret=1==.")))
 
   ;; wrapped function returned 124"
   (should (equal
           (cons 124 "line1\nline2\n")
           (bash-completion-test-send
-           (concat "line1\nli\e\ewrapped-status=124\e\ene2\n\t0\v")))))
+           (concat 
"line1\nli\e\ewrapped-status=124\e\ene2\n==emacs==ret=0==.")))))
 
 (ert-deftest bash-completion-cd-command-prefix-test ()
   ;; no current dir



reply via email to

[Prev in Thread] Current Thread [Next in Thread]