From 9250d0c78fbc1c23469c125279604bbb8c965626 Mon Sep 17 00:00:00 2001 From: Federico Tedin Date: Mon, 8 Jul 2024 14:47:33 +0200 Subject: [PATCH] Fix tab expanding not working in shell-mode (bug#70198) In shell-mode, fix tab expanding when environment variables are present before the command. * lisp/shell.el (shell-command-completion): Fix indentation and call use `shell--skip-environment-variables'. (shell--skip-environment-variables): New function. * test/lisp/shell-tests.el (shell-skip-environment-variables): Test new function. --- lisp/shell.el | 22 +++++++++++++++++----- test/lisp/shell-tests.el | 17 +++++++++++++++++ 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/lisp/shell.el b/lisp/shell.el index e1936ff1119..fff7bdd4d71 100644 --- a/lisp/shell.el +++ b/lisp/shell.el @@ -1385,12 +1385,24 @@ shell-dynamic-complete-command (defun shell-command-completion () "Return the completion data for the command at point, if any." - (let ((filename (comint-match-partial-filename))) + (let ((filename (comint-match-partial-filename)) + (pt (point))) (if (and filename - (save-match-data (not (string-match "[~/]" filename))) - (eq (match-beginning 0) - (save-excursion (shell-backward-command 1) (point)))) - (shell--command-completion-data)))) + (save-match-data (not (string-match "[~/]" filename))) + (eq (match-beginning 0) + (save-excursion + ;; Go back to beginning of command + (shell-backward-command 1) + ;; Skip any potential environment variables + (shell--skip-environment-variables pt) + (point)))) + (shell--command-completion-data)))) + +(defun shell--skip-environment-variables (pt) + "Move forward up to PT through any present environment variables." + (while (re-search-forward "=" pt t) + (skip-syntax-forward "^ " pt) + (skip-syntax-forward " " pt))) (defun shell--command-completion-data () "Return the completion data for the command at point." diff --git a/test/lisp/shell-tests.el b/test/lisp/shell-tests.el index 9bdf6b1c0eb..f07166d5995 100644 --- a/test/lisp/shell-tests.el +++ b/test/lisp/shell-tests.el @@ -95,4 +95,21 @@ shell-directory-tracker-cd (should (not (equal start-dir list-buffers-directory))) (should (string-prefix-p list-buffers-directory start-dir))))) +(ert-deftest shell-skip-environment-variables () + (with-temp-buffer + (shell-mode) + (insert "FOO=BAR BAZ= QUUX=abc=def whoami") + (let ((pt (point))) + (shell-backward-command 1) + (shell--skip-environment-variables pt)) + (should (looking-at-p "whoami")) + + (shell-backward-command 1) + (delete-line) + (insert "echo") + (let ((pt (point))) + (shell-backward-command 1) + (shell--skip-environment-variables pt)) + (should (looking-at-p "echo")))) + ;;; shell-tests.el ends here -- 2.43.0