Florian Lindner <mailingli...@xgm.de> writes: > Am 01.06.2017 um 06:39 schrieb Eric Abrahamsen: >> Florian Lindner <mailingli...@xgm.de> writes: >> >>> Hello, >>> >>> two questions about moving attachments to org files: >>> >>> C-c C-a a attaches a file and stores it under ./data/ID/... >>> >>> Using C-c C-a s I can set another directory a attachment directory. >>> Can I make org-mode move the content of the previous >>> directory to the new directory? >>> >>> Can I "reset" the attachment directory, i.e. like C-c C-a s but >>> :ATTACH_DIR: is deleted and the contents of the previous >>> directory are moved to ./data/ID?
> I hacked together some lines of lisp that should achieve that. It's my first > non-trivial (from my point of trivialness) > piece of code. I'm open for any suggestions: > > (defun flo/org-attach-move () > (interactive) > (when (org-attach-dir) > (let ((target (read-string "Move attachments to: ")) ; > read-directory-name here? > (attach-dir (org-attach-dir))) > > (if (string= target "") > (progn > (org-entry-delete nil "ATTACH_DIR") > (setq target (org-attach-dir t))) > (progn > (org-entry-put nil "ATTACH_DIR" target) > (make-directory target t)) > ) > > (unless (string= target attach-dir) > (copy-directory attach-dir target t nil t) > (message "Deleting %s" attach-dir) > ;; (shell-command "rm -rf %s" attach-dir) > ) > ) > ) > ) Looks like a good start! My first comment is, this should definitely be written as a patch to `org-attach-set-directory'. It's useful functionality, and fits well into the whole system -- so long as you give users a chance to say no, I don't see why it shouldn't be part of the library. Various comments: 1. Use the prefix arg to differentiate between setting and unsetting a directory. Ie, no prefix arg means set (and an empty string for directory name aborts), prefix arg means unset. The `org-attach' dispatch mechanism will pass the prefix arg through to this function. 2. Definitely use `read-directory-name'! 3. This is a good use of `copy-directory' with the COPY-CONTENTS flag, but I'd still recommend using `directory-files' and then looping over all the files with a `map-y-or-n-p'. That will give users a chance to selectively choose files to move. This is a matter of taste. If you stick with `copy-directory', at least ask the user first. 4. I think you're right not to delete the directory afterwards. Best not to assume too much. 5. The "else" branch of an `if' statement has an implicit `progn', you don't need to add it. 6. Convention is to not put closing parentheses on their own line. Just pile them up at the end of the last form. 7. Personally I'd rework things so you only call `org-attach-dir' once. How to handle this depends a bit on when when-let was introduced into Emacs, and whether Org is okay to support it. Probably safest to use when-let*. so: (when-let* ((attach-dir (org-attach-dir)) (target (read-directory-name "Move attachments to: "))) That way everything will bail if there's no attach dir. HTH, Eric