unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Federico Tedin via "Bug reports for GNU Emacs, the Swiss army knife of text editors" <bug-gnu-emacs@gnu.org>
To: Dan Jacobson <jidanni@jidanni.org>
Cc: 70198@debbugs.gnu.org
Subject: bug#70198: M-x shell: deal with environment variables present when tab expanding
Date: Wed, 10 Jul 2024 18:22:35 +0200	[thread overview]
Message-ID: <87plrlz06s.fsf@gmx.de> (raw)
In-Reply-To: <Zg6wRQ1kVfgwALNU@jidanni.org> (Dan Jacobson's message of "Thu, 4 Apr 2024 21:51:01 +0800")

[-- Attachment #1: Type: text/plain, Size: 943 bytes --]

Dan Jacobson <jidanni@jidanni.org> 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 federicotedin@gmail.com).
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


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: patch --]
[-- Type: text/x-diff, Size: 3067 bytes --]

From 9250d0c78fbc1c23469c125279604bbb8c965626 Mon Sep 17 00:00:00 2001
From: Federico Tedin <federicotedin@gmail.com>
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


  reply	other threads:[~2024-07-10 16:22 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-04 13:51 bug#70198: M-x shell: deal with environment variables present when tab expanding Dan Jacobson
2024-07-10 16:22 ` Federico Tedin via Bug reports for GNU Emacs, the Swiss army knife of text editors [this message]
2024-07-10 17:45   ` Eli Zaretskii
2024-07-10 18:11     ` Federico Tedin via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-07-10 18:34       ` Eli Zaretskii
2024-07-10 20:17         ` Federico Tedin via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-07-11  4:36           ` Eli Zaretskii

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87plrlz06s.fsf@gmx.de \
    --to=bug-gnu-emacs@gnu.org \
    --cc=70198@debbugs.gnu.org \
    --cc=federicotedin@gmx.de \
    --cc=jidanni@jidanni.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).