emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: "Juan Manuel Macías" <maciaschain@posteo.net>
To: John Kitchin <jkitchin@andrew.cmu.edu>
Cc: orgmode <emacs-orgmode@gnu.org>
Subject: [patch] Re: org-attach a directory?
Date: Wed, 09 Jun 2021 01:38:01 +0000	[thread overview]
Message-ID: <874ke7x1fq.fsf_-_@posteo.net> (raw)
In-Reply-To: <CAJ51ETqhdT5wzOaMmvCPLf5pWa6fUi_VHQPHX88iF8oFppiUVw@mail.gmail.com> (John Kitchin's message of "Tue, 8 Jun 2021 16:41:17 -0400")

[-- Attachment #1: Type: text/plain, Size: 786 bytes --]

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

(Little tested).

Best regards,

Juan Manuel

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Org-attach.el-Add-two-new-methods-for-cp-or-mv-directories.patch --]
[-- Type: text/x-patch, Size: 4462 bytes --]

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

* lisp/org-attach.el (org-attach-commands): add two new attachments
(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
@@ -504,15 +508,20 @@ METHOD may be `cp', `mv', `ln', `lns' or `url' default taken from
   (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)))
-       ((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)
       (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))
             ((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))
 	    ((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."
   (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.

  reply	other threads:[~2021-06-09  1:38 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-06-08 15:49 org-attach a directory? John Kitchin
2021-06-08 16:21 ` Henrik Frisk
2021-06-08 20:41   ` John Kitchin
2021-06-09  1:38     ` Juan Manuel Macías [this message]
2021-06-09  1:38 ` Ihor Radchenko
2021-06-11  1:43 ` stardiviner
2021-06-11 16:35   ` John Kitchin
2021-06-11 17:10     ` Juan Manuel Macías
2021-06-15 12:45     ` stardiviner
2021-06-15 20:22     ` Henrik Frisk

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=874ke7x1fq.fsf_-_@posteo.net \
    --to=maciaschain@posteo.net \
    --cc=emacs-orgmode@gnu.org \
    --cc=jkitchin@andrew.cmu.edu \


* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox


This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).