Subject: bug#64311: [PATCH] Fix shell-dirtrack-mode showing up as enabled in unrelated buffers

From: Vladimir Sedach <[email-masked]>
Date: Thu, 06 Jul 2023 14:30:16 -0600

Attached is a patch that keeps defvaralias, and does not obsolete
anything.

--
Vladimir Sedach

>From 9edd92e6e2763f5e2d7bf6dc5009ce857f3d36ae Mon Sep 17 00:00:00 2001
From: Vladimir Sedach <[email-masked]>
Date: Mon, 26 Jun 2023 22:32:07 -0600
Subject: [PATCH 2/2] Fix shell-dirtrack-mode showing up as enabled in
 unrelated buffers
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

; shell-dirtrack-mode always shows up in unrelated buffers.

; Steps to reproduce:
; 1. emacs -q
; 2. M-x shell
; 3. M-: shell-dirtrack-mode in *scratch* or *GNU Emacs*

; This is because shell-dirtrack-mode is aliased to shell-dirtrackp,
; which has default value t.

; Changes in this patch:
; remove redundant declaration of shell-dirtrackp
; replace existing occurrences of shell-dirtrackp with shell-dirtrack-mode
; add :interactive (shell-mode) suggestion to shell-dirtrack-mode

; shell-dirtrack-mode is still turned on by default. There is an
; explicit call to (shell-dirtrack-mode 1) in the definition of
; shell-mode.

; Definition of shell-dirtrack-mode is moved to quiet warning about
; reference to free variable ‘shell-dirtrack-mode’.
---
 lisp/shell.el | 38 +++++++++++++++++++-------------------
 1 file changed, 19 insertions(+), 19 deletions(-)

diff --git a/lisp/shell.el b/lisp/shell.el
index b74442f1961..47e7fa4b535 100644
--- a/lisp/shell.el
+++ b/lisp/shell.el
@@ -346,10 +346,9 @@ shell-dirstack
   "List of directories saved by pushd in this buffer's shell.
 Thus, this does not include the shell's current directory.")
 
-(defvaralias 'shell-dirtrack-mode 'shell-dirtrackp)
-
-(defvar shell-dirtrackp t
-  "Non-nil in a shell buffer means directory tracking is enabled.")
+(defvaralias 'shell-dirtrackp 'shell-dirtrack-mode
+  "Non-nil in a shell buffer means directory tracking is enabled.
+Superseded by the minor mode variable `shell-dirtrack-mode'.")
 
 (defvar shell-last-dir nil
   "Keep track of last directory for ksh `cd -' command.")
@@ -997,6 +996,20 @@ shell
 ;; replace it with a process filter that watches for and strips out
 ;; these messages.
 
+(define-minor-mode shell-dirtrack-mode
+  "Toggle directory tracking in this shell buffer (Shell Dirtrack mode).
+
+The `dirtrack' package provides an alternative implementation of
+this feature; see the function `dirtrack-mode'.  Also see
+`comint-osc-directory-tracker' for an escape-sequence based
+solution."
+  :lighter nil
+  :interactive (shell-mode)
+  (setq list-buffers-directory (if shell-dirtrack-mode default-directory))
+  (if shell-dirtrack-mode
+      (add-hook 'comint-input-filter-functions #'shell-directory-tracker nil t)
+    (remove-hook 'comint-input-filter-functions #'shell-directory-tracker t)))
+
 (defun shell-directory-tracker (str)
   "Tracks cd, pushd and popd commands issued to the shell.
 This function is called on each input passed to the shell.
@@ -1013,7 +1026,7 @@ shell-directory-tracker
 and `shell-pushd-dunique' control the behavior of the relevant command.
 
 Environment variables are expanded, see function `substitute-in-file-name'."
-  (if shell-dirtrackp
+  (if shell-dirtrack-mode
       ;; We fail gracefully if we think the command will fail in the shell.
 ;;;      (with-demoted-errors "Directory tracker failure: %s"
       ;; This fails so often that it seems better to just ignore errors (?).
@@ -1167,23 +1180,10 @@ shell-extract-num
   (and (string-match "^\\+[1-9][0-9]*$" str)
        (string-to-number str)))
 
-(define-minor-mode shell-dirtrack-mode
-  "Toggle directory tracking in this shell buffer (Shell Dirtrack mode).
-
-The `dirtrack' package provides an alternative implementation of
-this feature; see the function `dirtrack-mode'.  Also see
-`comint-osc-directory-tracker' for an escape-sequence based
-solution."
-  :lighter nil
-  (setq list-buffers-directory (if shell-dirtrack-mode default-directory))
-  (if shell-dirtrack-mode
-      (add-hook 'comint-input-filter-functions #'shell-directory-tracker nil t)
-    (remove-hook 'comint-input-filter-functions #'shell-directory-tracker t)))
-
 (defun shell-cd (dir)
   "Do normal `cd' to DIR, and set `list-buffers-directory'."
   (cd dir)
-  (if shell-dirtrackp
+  (if shell-dirtrack-mode
       (setq list-buffers-directory default-directory)))
 
 (defun shell-resync-dirs ()
-- 
2.20.1