Subject: bug#74246: [PATCH] Reuse display windows in image-dired

From: martin rudalics <[email-masked]>
Date: Mon, 2 Dec 2024 12:22:05 +0100

> Please explain why 'display-buffer-last-window' wouldn't help
 > for 'image-dired'?  IIUC, 'image-dired' uses one source buffer
 > that could use the buffer-local variable to remember the last
 > window it used to display an image buffer.

Hmm...  Currently 'image-dired-display-image' does

   (let ((buf (get-buffer image-dired-display-image-buffer))
         (cur-win (selected-window)))
     (when buf
       (kill-buffer buf))
     (when-let ((buf (find-file-noselect file nil t)))
       (pop-to-buffer buf)
       (rename-buffer image-dired-display-image-buffer)

so it kills that buffer and its local variables are gone.  Hence we have
another motivation to use 'find-file-noselect-1' directly (or maybe
something like

(defun find-file-noselect-in-buffer (filename buffer &optional nowarn rawfile truename)
   "Visit file FILENAME in live buffer BUFFER.
Replace the contents of BUFFER with the contents of file FILENAME and
make BUFFER visiting file FILENAME.

The file FILENAME must not be visited by another buffer.  BUFFER must
not have been be modified.  Optional arguments NOWARN and RAWFILE are as
for `find-file-noselect'."
   (setq filename
	(abbreviate-file-name
	 (expand-file-name filename)))
   (let* ((truename (abbreviate-file-name (file-truename filename)))
	 (attributes (file-attributes truename))
	 (number (file-attribute-file-identifier attributes)))
     (cond
      ((find-buffer-visiting filename)
       (error "A buffer is already visting %s" filename))
      ((not (buffer-live-p buffer))
       (error "%s is not a live buffer" buffer))
      ((buffer-modified-p buffer)
       (error "Buffer %s has been modified" buffer)))

     (find-file-noselect-1 buffer filename nowarn rawfile truename number)))

to avoid that people overwrite a modified buffer).

Otherwise, you're right.  The question is now whether

- 'display-buffer-use-some-window' should use the buffer-local value of
   'display-buffer-last-window' autonomously, or

- get it via a (some-window . display-buffer-last-window) alist entry.

And obviously whether 'display-buffer' should set the value of
'display-buffer-last-window' itself or leave that to the caller.

Maybe something like (some-window . t) could be used to incite
'display-buffer-use-some-window' to go for the buffer-local value of
that variable and 'window--display-buffer' to set it.

martin