Hi John,

John Kitchin writes:

> I want to attach all the files in a directory on my desktop to the
> attachment directory, something that is more like
> org-attach-attach-mv-directory  (that is not an existing command, but
> what I was thinking of doing). Did I misunderstand what
> org-attach-set-directory does?

As far as I know, that option is not possible. And it's something that
I've always missed in org-attach. I've written this possible patch, with
two new attach methods/commands to copy or move a directory ("C" and
"M"). I guess it would be better to use `read-file-name' and
`read-directory-name' according to each case, but I can't think of how
to solve that. Anyway, `read-file-name' may also apply here for
directories.

(Little tested).

Best regards,

Juan Manuel

>From 5a2f59a74c9b3f8ff1cf25777067780400f9043f Mon Sep 17 00:00:00 2001
From: Juan Manuel Macias <maciasch...@posteo.net>
Date: Wed, 9 Jun 2021 03:03:50 +0200
Subject: [PATCH] Org-attach.el: Add two new methods for copying or moving
 directories

* lisp/org-attach.el (org-attach-commands): add two new attachments
commands
(org-attach-attach): add two new attachment methods, argument `file'
is ranamed as `target'
(org-attach-attach-cp-dir): copy a directory
(org-attach-attach-mv-dir): move (rename) a directory
---
 lisp/org-attach.el | 37 +++++++++++++++++++++++++++----------
 1 file changed, 27 insertions(+), 10 deletions(-)

diff --git a/lisp/org-attach.el b/lisp/org-attach.el
index 715fe3e93..50b1dca3b 100644
--- a/lisp/org-attach.el
+++ b/lisp/org-attach.el
@@ -199,6 +199,10 @@ git-functionality from this file.")
      "Attach a file using copy method.")
     ((?m ?\C-m) org-attach-attach-mv
      "Attach a file using move method.")
+    ((?C ?\C-C) org-attach-attach-cp-dir
+     "Attach a directory using copy dir method.")
+    ((?M ?\C-M) org-attach-attach-mv-dir
+     "Attach a directory using move dir method.")
     ((?l ?\C-l) org-attach-attach-ln
      "Attach a file using link method.")
     ((?y ?\C-y) org-attach-attach-lns
@@ -489,8 +493,8 @@ if it would overwrite an existing filename."
     (with-temp-file output
       (insert-buffer-substring buffer-name))))
 
-(defun org-attach-attach (file &optional visit-dir method)
-  "Move/copy/link FILE into the attachment directory of the current outline node.
+(defun org-attach-attach (target &optional visit-dir method)
+  "Move/copy/link TARGET into the attachment directory of the current outline node.
 If VISIT-DIR is non-nil, visit the directory with dired.
 METHOD may be `cp', `mv', `ln', `lns' or `url' default taken from
 `org-attach-method'."
@@ -504,15 +508,20 @@ METHOD may be `cp', `mv', `ln', `lns' or `url' default taken from
     current-prefix-arg
     nil))
   (setq method (or method org-attach-method))
-  (let ((basename (file-name-nondirectory file)))
+  (let ((basename (if (or (eq method 'mv-dir)
+			  (eq method 'cp-dir))
+		      target
+		    (file-name-nondirectory target))))
     (let* ((attach-dir (org-attach-dir 'get-create))
            (attach-file (expand-file-name basename attach-dir)))
       (cond
-       ((eq method 'mv) (rename-file file attach-file))
-       ((eq method 'cp) (copy-file file attach-file))
-       ((eq method 'ln) (add-name-to-file file attach-file))
-       ((eq method 'lns) (make-symbolic-link file attach-file))
-       ((eq method 'url) (url-copy-file file attach-file)))
+       ((eq method 'mv) (rename-file target attach-file))
+       ((eq method 'cp) (copy-file target attach-file))
+       ((eq method 'mv-dir) (rename-file target (concat attach-dir "/")))
+       ((eq method 'cp-dir) (copy-directory target (concat attach-dir "/")))
+       ((eq method 'ln) (add-name-to-file target attach-file))
+       ((eq method 'lns) (make-symbolic-link target attach-file))
+       ((eq method 'url) (url-copy-file target attach-file)))
       (run-hook-with-args 'org-attach-after-change-hook attach-dir)
       (org-attach-tag)
       (cond ((eq org-attach-store-link-p 'attached)
@@ -520,8 +529,8 @@ METHOD may be `cp', `mv', `ln', `lns' or `url' default taken from
 			 (file-name-nondirectory attach-file))
 		   org-stored-links))
             ((eq org-attach-store-link-p t)
-             (push (list (concat "file:" file)
-			 (file-name-nondirectory file))
+             (push (list (concat "file:" target)
+			 (file-name-nondirectory target))
 		   org-stored-links))
 	    ((eq org-attach-store-link-p 'file)
 	     (push (list (concat "file:" attach-file)
@@ -539,6 +548,14 @@ METHOD may be `cp', `mv', `ln', `lns' or `url' default taken from
   "Attach a file by moving (renaming) it."
   (interactive)
   (let ((org-attach-method 'mv)) (call-interactively 'org-attach-attach)))
+(defun org-attach-attach-cp-dir ()
+  "Attach a directory by copying it."
+  (interactive)
+  (let ((org-attach-method 'cp-dir)) (call-interactively 'org-attach-attach)))
+(defun org-attach-attach-mv-dir ()
+  "Attach a directory by moving (renaming) it."
+  (interactive)
+  (let ((org-attach-method 'mv-dir)) (call-interactively 'org-attach-attach)))
 (defun org-attach-attach-ln ()
   "Attach a file by creating a hard link to it.
 Beware that this does not work on systems that do not support hard links.
-- 
2.31.1

Reply via email to