Subject: bug#64531: [PATCH v2] Add new command vc-dir-copy-filename-as-kill

From: Ivan Sokolov <[email-masked]>
Date: Tue, 08 Aug 2023 16:44:56 +0300

Eli Zaretskii <[email-masked]> writes:

> However, new commands should be called out in NEWS, and commands that
> are important enough should also be mentioned in the user manual.

Added entry in NEWS, but not in the user manual, there is no such for
vc-dir afaik.

>> +(defun vc-dir-copy-filename-as-kill (&optional absolutep)
>> +  "Copy names of marked files (or file under cursor) into the kill ring.
>
> This sentence is too general: it doesn't mention VC Dir, although
> AFAIU it is specific to vc-dir.

Fixed.

>From 88cb2e3407adf0245bc15a88896e1cd611d559c8 Mon Sep 17 00:00:00 2001
From: Ivan Sokolov <[email-masked]>
Date: Tue, 11 Jul 2023 18:19:57 +0300
Subject: [PATCH v2] Add new command vc-dir-copy-filename-as-kill

* lisp/vc/vc-dir.el (vc-dir-copy-filename-as-kill): New command.

(vc-dir-mode-map): Bind vc-dir-copy-filename-as-kill to `w'.
---
 etc/NEWS          |  3 +++
 lisp/vc/vc-dir.el | 18 ++++++++++++++++++
 2 files changed, 21 insertions(+)

diff --git a/etc/NEWS b/etc/NEWS
index 1a86c9e55e2..da3672e068b 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -154,6 +154,9 @@ This is a string or a list of strings that specifies the Git log
 switches for shortlogs, such as the one produced by 'C-x v L'.
 'vc-git-log-switches' is no longer used for shortlogs.
 
+*** New command 'vc-dir-copy-filename-as-kill'.
+This works like 'dired-copy-filename-as-kill' but in vc-dir buffers.
+
 ** Diff Mode
 
 +++
diff --git a/lisp/vc/vc-dir.el b/lisp/vc/vc-dir.el
index 53d58870b32..dc25467a3e9 100644
--- a/lisp/vc/vc-dir.el
+++ b/lisp/vc/vc-dir.el
@@ -353,6 +353,7 @@ See `run-hooks'."
     (define-key map (kbd "M-s a C-s")   #'vc-dir-isearch)
     (define-key map (kbd "M-s a M-C-s") #'vc-dir-isearch-regexp)
     (define-key map "G" #'vc-dir-ignore)
+    (define-key map "w" #'vc-dir-copy-filename-as-kill)
 
     (let ((branch-map (make-sparse-keymap)))
       (define-key map "b" branch-map)
@@ -930,6 +931,23 @@ system."
   (interactive)
   (view-file (vc-dir-current-file)))
 
+(defun vc-dir-copy-filename-as-kill (&optional absolutep)
+  "Copy filenames from the `vc-dir' buffer into the kill ring.
+If there are marked files, copy their names, otherwise copy the
+file at point.  Names will be separated by a space, each will be
+quoted using `shell-quote-argument'.  If ABSOLUTEP use absolute
+names, otherwise names are relative to the `default-directory'."
+  (interactive "P")
+  (let ((files (or (vc-dir-marked-files)
+                   (list (vc-dir-current-file)))))
+    (unless absolutep
+      (setq files (mapcar #'file-relative-name files)))
+    (let ((string (mapconcat #'shell-quote-argument files " ")))
+      (if (eq last-command 'kill-region)
+          (kill-append string nil)
+        (kill-new string))
+      (message "%s" string))))
+
 (defun vc-dir-isearch ()
   "Search for a string through all marked buffers using Isearch."
   (interactive)
-- 
2.41.0