Subject: bug#57334: 28.1; Fix wdired with (dired '(dir f1 f2 ...))

From: Thierry Volpiatto <[email-masked]>
Date: Mon, 22 Aug 2022 15:45:03 +0000

Lars Ingebrigtsen <[email-masked]> writes:

> Thierry Volpiatto <[email-masked]> writes:
>
>>>> +                 (setcdr dired-directory
>
> [...]
>
>> Of course it have to be changed, it has been modified by wdired at this point,
>> so if you want to redisplay a dired buffer reflecting your changes you
>> have to modify it no ?
>
> I don't understand what you mean.

Well, you dired buffer is not the same before and after editing and
saved.

>>> which we shouldn't do.
>>
>> You already do it when DIRNAME is a string isn't it?
>
> Strings can't be modified.

You are speaking of destructive operations, ok, I am just saying you
just modify the string non destructively yes, but you modify it.

>>> (It may even be a constant.)
>>
>> Can you elaborate?
>
> If the list is in purespace, for instance, it can't be modified.

Yes but I hardly see how it would be the case for this.

> In case there's any misunderstanding here, I'm talking about the
> destructive alteration of the list pointed to by dired-directory by that
> `setcdr' -- not the altering of the dired-directory variable.  So the
> safe change here would be something like
>
>   (setq dired-directory (cons (car dired-directory) (mapcar ...)))

Ok, so here a new patch which works same as the previous:

diff --git a/lisp/wdired.el b/lisp/wdired.el
index 106d57174d5..74d05a093b4 100644
--- a/lisp/wdired.el
+++ b/lisp/wdired.el
@@ -537,15 +537,27 @@ non-nil means return old filename."
     (wdired-change-to-dired-mode)
     (if changes
 	(progn
-	  ;; If we are displaying a single file (rather than the
-	  ;; contents of a directory), change dired-directory if that
-	  ;; file was renamed.  (This ought to be generalized to
-	  ;; handle the multiple files case, but that's less trivial).
-	  (when (and (stringp dired-directory)
-		     (not (file-directory-p dired-directory))
-		     (null some-file-names-unchanged)
-		     (= (length files-renamed) 1))
-	    (setq dired-directory (cdr (car files-renamed))))
+          (setq dired-directory
+	        (cond (;; If we are displaying a single file (rather than the
+	               ;; contents of a directory), change dired-directory if that
+	               ;; file was renamed.
+                       (and (stringp dired-directory)
+                            (not (file-directory-p dired-directory))
+                            (null some-file-names-unchanged)
+                            (= (length files-renamed) 1))
+                       (cdr (car files-renamed)))
+                      ;; Fix dired buffers created with
+                      ;; (dired '(foo f1 f2 f3)).
+                      ((and (consp dired-directory)
+                            (cdr dired-directory)
+                            files-renamed)
+                       (cons (car dired-directory)
+                             ;; Replace in `dired-directory' files that have
+                             ;; been modified with their new name keeping
+                             ;; the ones that are unmodified at the same place.
+                             (cl-loop for f in (cdr dired-directory)
+                                      collect (or (assoc-default f files-renamed)
+                                                  f))))))
 	  ;; Re-sort the buffer.
 	  (revert-buffer)
 	  (let ((inhibit-read-only t))

-- 
Thierry