Subject: bug#70198: M-x shell: deal with environment variables present when tab expanding

From: Federico Tedin <[email-masked]>
Date: Wed, 10 Jul 2024 18:22:35 +0200

Dan Jacobson <[email-masked]> writes:

> In M-x shell
> $ dat<TAB>
> expands to date.
> Alas, unlike bash readline,
> $ LC_ALL=C dat<TAB>
> doesn't yet.
>
> emacs-version "29.3"

I took a crack at fixing this, I'm attaching a patch.

It's been some time since my last contribution, but I've kept the
copyright assignment updated (should be under [email-masked]).
There's a chance the formatting for the patch may be a bit off too but I
tried to re-read the guide at CONTRIBUTE.

I've also found something interesting with the
`shell-dynamic-complete-command' function. I do not see it being called,
referred to, or assigned to a key anywhere in the Emacs code, but the
manual mentions it as if it being were actively used:

> Some implementation details of the shell command completion may also be found
> in the lisp documentation of the @code{shell-dynamic-complete-command}
> function.

Maybe the manual is outdated?

- Fede

From 9250d0c78fbc1c23469c125279604bbb8c965626 Mon Sep 17 00:00:00 2001
From: Federico Tedin <[email-masked]>
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