Subject: bug#39608: defun occur does not use a coherent value for default-directory (fix included)

From: Stéphane Boucher <[email-masked]>
Date: Fri, 14 Feb 2020 19:26:52 +0000

When occur creates an *Occur* buffer, the default-directory variable is implicitly set to the default-directory of the buffer the occurences came from.

When occur REUSES an existing *Occur* buffer, it does not update the default-directory variable.

Following is a patch for emacs 26.3:

--- replace.el.orig            2020-02-14 14:13:34.201656900 -0500
+++ replace.el  2020-02-14 14:17:35.330856900 -0500
@@ -1532,7 +1532,8 @@ (defun occur-1 (regexp nlines bufs &opti
   (let (occur-buf
               (active-bufs (delq nil (mapcar #'(lambda (buf)
                                                                                (when (buffer-live-p buf) buf))
-                                                                    bufs))))
+                                                                   bufs)))
+             (source-buffer-default-directory default-directory))
     ;; Handle the case where one of the buffers we're searching is the
     ;; output buffer.  Just rename it.
     (when (member buf-name (mapcar 'buffer-name active-bufs))
@@ -1544,6 +1545,8 @@ (defun occur-1 (regexp nlines bufs &opti
     (setq occur-buf (get-buffer-create buf-name))
     (with-current-buffer occur-buf
+      ;; make the default-directory of the *Occur* buffer match that of the buffer where the occurences come from
+      (setq default-directory source-buffer-default-directory)
       (if (stringp nlines)
                 (fundamental-mode) ;; This is for collect operation.
               (occur-mode))