]> git.eshelyaron.com Git - emacs.git/commitdiff
Tweak how dired-copy-filename-as-kill handles file names with spaces
authorLars Ingebrigtsen <larsi@gnus.org>
Fri, 15 Jul 2022 10:36:33 +0000 (12:36 +0200)
committerLars Ingebrigtsen <larsi@gnus.org>
Fri, 15 Jul 2022 10:36:33 +0000 (12:36 +0200)
* lisp/dired.el (dired-copy-filename-as-kill): Quote files
containing spaces (bug#48657).

etc/NEWS
lisp/dired.el

index 57845df9792e9e642990f24dad6aed593f56af79..ba6692aace0defb18dae90d068471d10c0544027 100644 (file)
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -166,6 +166,11 @@ of 'user-emacs-directory'.
 \f
 * Incompatible changes in Emacs 29.1
 
+---
+** 'w' ('dired-copy-filename-as-kill') has changed behaviour.
+If there are several files marked, file names containing space and
+quote characters will be quoted "like this".
+
 +++
 ** Warning about "eager macro-expansion failure" is changed into an error.
 
index 43563d969f15a2c249456b7a3e0e5e2cddef992a..59346a190146fd4df186e14a33f99d79f2f3f168 100644 (file)
@@ -3072,7 +3072,11 @@ If EOL, it should be an position to use instead of
 
 (defun dired-copy-filename-as-kill (&optional arg)
   "Copy names of marked (or next ARG) files into the kill ring.
-The names are separated by a space.
+If there are several names, they will be separated by a space,
+and file names that have spaces or quote characters in them will
+be quoted (with double quotes).  (When there's a single file, no
+quoting is done.)
+
 With a zero prefix arg, use the absolute file name of each marked file.
 With \\[universal-argument], use the file name relative to the Dired buffer's
 `default-directory'.  (This still may contain slashes if in a subdirectory.)
@@ -3082,19 +3086,26 @@ prefix arg and marked files are ignored in this case.
 
 You can then feed the file name(s) to other commands with \\[yank]."
   (interactive "P")
-  (let ((string
-         (or (dired-get-subdir)
-             (mapconcat #'identity
-                        (if arg
-                            (cond ((zerop (prefix-numeric-value arg))
-                                   (dired-get-marked-files))
-                                  ((consp arg)
-                                   (dired-get-marked-files t))
-                                  (t
-                                   (dired-get-marked-files
-                                   'no-dir (prefix-numeric-value arg))))
-                          (dired-get-marked-files 'no-dir))
-                        " "))))
+  (let* ((files
+          (or (ensure-list (dired-get-subdir))
+              (if arg
+                  (cond ((zerop (prefix-numeric-value arg))
+                         (dired-get-marked-files))
+                        ((consp arg)
+                         (dired-get-marked-files t))
+                        (t
+                         (dired-get-marked-files
+                         'no-dir (prefix-numeric-value arg))))
+                (dired-get-marked-files 'no-dir))))
+         (string
+          (if (length= files 1)
+              (car files)
+            (mapconcat (lambda (file)
+                         (if (string-match-p "[ \"']" file)
+                             (format "%S" file)
+                           file))
+                       files
+                       " "))))
     (unless (string= string "")
       (if (eq last-command 'kill-region)
           (kill-append string nil)