1 ;;; org-attach.el --- Manage file attachments to Org tasks -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2008-2017 Free Software Foundation, Inc.
5 ;; Author: John Wiegley <johnw@newartisans.com>
6 ;; Keywords: org data task
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
25 ;; See the Org manual for information on how to use it.
27 ;; Attachments are managed in a special directory called "data", which
28 ;; lives in the same directory as the org file itself. If this data
29 ;; directory is initialized as a Git repository, then org-attach will
30 ;; automatically commit changes when it sees them.
32 ;; Attachment directories are identified using a UUID generated for the
33 ;; task which has the attachments. These are added as property to the
34 ;; task when necessary, and should not be deleted or changed by the
35 ;; user, ever. UUIDs are generated by a mechanism defined in the variable
45 (declare-function dired-dwim-target-directory
"dired-aux")
47 (defgroup org-attach nil
48 "Options concerning entry attachments in Org mode."
52 (defcustom org-attach-directory
"data/"
53 "The directory where attachments are stored.
54 If this is a relative path, it will be interpreted relative to the directory
55 where the Org file lives."
60 (defcustom org-attach-commit t
61 "If non-nil commit attachments with git.
62 This is only done if the Org file is in a git repository."
66 :package-version
'(Org .
"9.0"))
68 (defcustom org-attach-git-annex-cutoff
(* 32 1024)
69 "If non-nil, files larger than this will be annexed instead of stored."
72 :package-version
'(Org .
"8.0")
74 (const :tag
"None" nil
)
75 (integer :tag
"Bytes")))
77 (defcustom org-attach-auto-tag
"ATTACH"
78 "Tag that will be triggered automatically when an entry has an attachment."
81 (const :tag
"None" nil
)
84 (defcustom org-attach-file-list-property
"Attachments"
85 "The property used to keep a list of attachment belonging to this entry.
86 This is not really needed, so you may set this to nil if you don't want it.
87 Also, for entries where children inherit the directory, the list of
88 attachments is not kept in this property."
91 (const :tag
"None" nil
)
94 (defcustom org-attach-method
'cp
95 "The preferred method to attach a file.
98 mv rename the file to move it into the attachment directory
100 ln create a hard link. Note that this is not supported
101 on all systems, and then the result is not defined.
102 lns create a symbol link. Note that this is not supported
103 on all systems, and then the result is not defined."
106 (const :tag
"Copy" cp
)
107 (const :tag
"Move/Rename" mv
)
108 (const :tag
"Hard Link" ln
)
109 (const :tag
"Symbol Link" lns
)))
111 (defcustom org-attach-expert nil
112 "Non-nil means do not show the splash buffer with the attach dispatcher."
116 (defcustom org-attach-allow-inheritance t
117 "Non-nil means allow attachment directories be inherited."
121 (defvar org-attach-inherited nil
122 "Indicates if the last access to the attachment directory was inherited.")
124 (defcustom org-attach-store-link-p nil
125 "Non-nil means store a link to a file when attaching it."
129 (const :tag
"Don't store link" nil
)
130 (const :tag
"Link to origin location" t
)
131 (const :tag
"Link to the attach-dir location" attached
)))
133 (defcustom org-attach-archive-delete nil
134 "Non-nil means attachments are deleted upon archiving a subtree.
135 When set to `query', ask the user instead."
138 :package-version
'(Org .
"8.3")
140 (const :tag
"Never delete attachments" nil
)
141 (const :tag
"Always delete attachments" t
)
142 (const :tag
"Query the user" query
)))
144 (defcustom org-attach-annex-auto-get
'ask
145 "Confirmation preference for automatically getting annex files.
146 If \\='ask, prompt using `y-or-n-p'. If t, always get. If nil, never get."
148 :package-version
'(Org .
"9.0")
151 (const :tag
"confirm with `y-or-n-p'" ask
)
152 (const :tag
"always get from annex if necessary" t
)
153 (const :tag
"never get from annex" nil
)))
157 "The dispatcher for attachment commands.
158 Shows a list of commands and prompts for another key to execute a command."
161 (when (eq major-mode
'org-agenda-mode
)
162 (setq marker
(or (get-text-property (point) 'org-hd-marker
)
163 (get-text-property (point) 'org-marker
)))
165 (error "No task in current line")))
168 (set-buffer (marker-buffer marker
))
170 (org-back-to-heading t
)
172 (save-window-excursion
173 (unless org-attach-expert
174 (with-output-to-temp-buffer "*Org Attach*"
175 (princ "Select an Attachment Command:
177 a Select a file and attach it to the task, using `org-attach-method'.
178 c/m/l/y Attach a file using copy/move/link/symbolic-link method.
179 u Attach a file from URL (downloading it).
180 n Create a new attachment, as an Emacs buffer.
181 z Synchronize the current task with its attachment
182 directory, in case you added attachments yourself.
184 o Open current task's attachments.
185 O Like \"o\", but force opening in Emacs.
186 f Open current task's attachment directory.
187 F Like \"f\", but force using dired in Emacs.
189 d Delete one attachment, you will be prompted for a file name.
190 D Delete all of a task's attachments. A safer way is
191 to open the directory in dired and delete from there.
193 s Set a specific attachment directory for this entry or reset to default.
194 i Make children of the current entry inherit its attachment directory.")))
195 (org-fit-window-to-buffer (get-buffer-window "*Org Attach*"))
196 (message "Select command: [acmlzoOfFdD]")
197 (setq c
(read-char-exclusive))
198 (and (get-buffer "*Org Attach*") (kill-buffer "*Org Attach*"))))
200 ((memq c
'(?a ?\C-a
)) (call-interactively 'org-attach-attach
))
201 ((memq c
'(?c ?\C-c
))
202 (let ((org-attach-method 'cp
)) (call-interactively 'org-attach-attach
)))
203 ((memq c
'(?m ?\C-m
))
204 (let ((org-attach-method 'mv
)) (call-interactively 'org-attach-attach
)))
205 ((memq c
'(?l ?\C-l
))
206 (let ((org-attach-method 'ln
)) (call-interactively 'org-attach-attach
)))
207 ((memq c
'(?y ?\C-y
))
208 (let ((org-attach-method 'lns
)) (call-interactively 'org-attach-attach
)))
209 ((memq c
'(?u ?\C-u
))
210 (let ((org-attach-method 'url
)) (call-interactively 'org-attach-url
)))
211 ((memq c
'(?n ?\C-n
)) (call-interactively 'org-attach-new
))
212 ((memq c
'(?z ?\C-z
)) (call-interactively 'org-attach-sync
))
213 ((memq c
'(?o ?\C-o
)) (call-interactively 'org-attach-open
))
214 ((eq c ?O
) (call-interactively 'org-attach-open-in-emacs
))
215 ((memq c
'(?f ?\C-f
)) (call-interactively 'org-attach-reveal
))
216 ((memq c
'(?F
)) (call-interactively 'org-attach-reveal-in-emacs
))
217 ((memq c
'(?d ?\C-d
)) (call-interactively
218 'org-attach-delete-one
))
219 ((eq c ?D
) (call-interactively 'org-attach-delete-all
))
220 ((eq c ?q
) (message "Abort"))
221 ((memq c
'(?s ?\C-s
)) (call-interactively
222 'org-attach-set-directory
))
223 ((memq c
'(?i ?\C-i
)) (call-interactively
224 'org-attach-set-inherit
))
225 (t (error "No such attachment command %c" c
))))))
227 (defun org-attach-dir (&optional create-if-not-exists-p
)
228 "Return the directory associated with the current entry.
229 This first checks for a local property ATTACH_DIR, and then for an inherited
230 property ATTACH_DIR_INHERIT. If neither exists, the default mechanism
231 using the entry ID will be invoked to access the unique directory for the
233 If the directory does not exist and CREATE-IF-NOT-EXISTS-P is non-nil,
234 the directory and (if necessary) the corresponding ID will be created."
235 (let (attach-dir uuid
)
236 (setq org-attach-inherited
(org-entry-get nil
"ATTACH_DIR_INHERIT"))
238 ((setq attach-dir
(org-entry-get nil
"ATTACH_DIR"))
239 (org-attach-check-absolute-path attach-dir
))
240 ((and org-attach-allow-inheritance
241 (org-entry-get nil
"ATTACH_DIR_INHERIT" t
))
243 (org-with-wide-buffer
244 (if (marker-position org-entry-property-inherited-from
)
245 (goto-char org-entry-property-inherited-from
)
246 (org-back-to-heading t
))
247 (let (org-attach-allow-inheritance)
248 (org-attach-dir create-if-not-exists-p
))))
249 (org-attach-check-absolute-path attach-dir
)
250 (setq org-attach-inherited t
))
252 (org-attach-check-absolute-path nil
)
253 (setq uuid
(org-id-get (point) create-if-not-exists-p
))
254 (when (or uuid create-if-not-exists-p
)
255 (unless uuid
(error "ID retrieval/creation failed"))
256 (setq attach-dir
(expand-file-name
260 (expand-file-name org-attach-directory
))))))
262 (if (and create-if-not-exists-p
263 (not (file-directory-p attach-dir
)))
264 (make-directory attach-dir t
))
265 (and (file-exists-p attach-dir
)
268 (defun org-attach-check-absolute-path (dir)
269 "Check if we have enough information to root the attachment directory.
270 When DIR is given, check also if it is already absolute. Otherwise,
271 assume that it will be relative, and check if `org-attach-directory' is
272 absolute, or if at least the current buffer has a file name.
273 Throw an error if we cannot root the directory."
274 (or (and dir
(file-name-absolute-p dir
))
275 (file-name-absolute-p org-attach-directory
)
276 (buffer-file-name (buffer-base-buffer))
277 (error "Need absolute `org-attach-directory' to attach in buffers without filename")))
279 (defun org-attach-set-directory (&optional arg
)
280 "Set the ATTACH_DIR node property and ask to move files there.
281 The property defines the directory that is used for attachments
282 of the entry. When called with `\\[universal-argument]', reset \
284 the default ID based one."
286 (let ((old (org-attach-dir))
289 (if arg
(org-entry-delete nil
"ATTACH_DIR")
290 (let ((dir (read-directory-name
291 "Attachment directory: "
294 (and org-attach-allow-inheritance t
)))))
295 (org-entry-put nil
"ATTACH_DIR" dir
)))
296 (org-attach-dir t
))))
297 (unless (or (string= old new
)
299 (when (yes-or-no-p "Copy over attachments from old directory? ")
300 (copy-directory old new t nil t
))
301 (when (yes-or-no-p (concat "Delete " old
))
302 (delete-directory old t
)))))
304 (defun org-attach-set-inherit ()
305 "Set the ATTACH_DIR_INHERIT property of the current entry.
306 The property defines the directory that is used for attachments
307 of the entry and any children that do not explicitly define (by setting
308 the ATTACH_DIR property) their own attachment directory."
310 (org-entry-put nil
"ATTACH_DIR_INHERIT" "t")
311 (message "Children will inherit attachment directory"))
313 (defun org-attach-use-annex ()
314 "Return non-nil if git annex can be used."
315 (let ((git-dir (vc-git-root (expand-file-name org-attach-directory
))))
316 (and org-attach-git-annex-cutoff
317 (or (file-exists-p (expand-file-name "annex" git-dir
))
318 (file-exists-p (expand-file-name ".git/annex" git-dir
))))))
320 (defun org-attach-annex-get-maybe (path)
321 "Call git annex get PATH (via shell) if using git annex.
322 Signals an error if the file content is not available and it was not retrieved."
323 (let ((path-relative (file-relative-name path
)))
324 (when (and (org-attach-use-annex)
328 (shell-command-to-string
329 (format "git annex find --format=found --in=here %s"
330 (shell-quote-argument path-relative
))))))
332 (if (eq org-attach-annex-auto-get
'ask
)
333 (y-or-n-p (format "Run git annex get %s? " path-relative
))
334 org-attach-annex-auto-get
)))
336 (progn (message "Running git annex get \"%s\"." path-relative
)
337 (call-process "git" nil nil nil
"annex" "get" path-relative
))
338 (error "File %s stored in git annex but it is not available, and was not retrieved"
341 (defun org-attach-commit ()
342 "Commit changes to git if `org-attach-directory' is properly initialized.
343 This checks for the existence of a \".git\" directory in that directory."
344 (let* ((dir (expand-file-name org-attach-directory
))
345 (git-dir (vc-git-root dir
))
346 (use-annex (org-attach-use-annex))
348 (when (and git-dir
(executable-find "git"))
351 (dolist (new-or-modified
353 (shell-command-to-string
354 "git ls-files -zmo --exclude-standard") "\0" t
))
356 (>= (nth 7 (file-attributes new-or-modified
))
357 org-attach-git-annex-cutoff
))
358 (call-process "git" nil nil nil
"annex" "add" new-or-modified
)
359 (call-process "git" nil nil nil
"add" new-or-modified
))
363 (shell-command-to-string "git ls-files -z --deleted") "\0" t
))
364 (call-process "git" nil nil nil
"rm" deleted
)
367 (shell-command "git commit -m 'Synchronized attachments'"))))))
369 (defun org-attach-tag (&optional off
)
370 "Turn the autotag on or (if OFF is set) off."
371 (when org-attach-auto-tag
373 (org-back-to-heading t
)
374 (org-toggle-tag org-attach-auto-tag
(if off
'off
'on
)))))
376 (defun org-attach-untag ()
377 "Turn the autotag off."
378 (org-attach-tag 'off
))
380 (defun org-attach-store-link (file)
381 "Add a link to `org-stored-link' when attaching a file.
382 Only do this when `org-attach-store-link-p' is non-nil."
383 (setq org-stored-links
384 (cons (list (org-attach-expand-link file
)
385 (file-name-nondirectory file
))
388 (defun org-attach-url (url)
389 (interactive "MURL of the file to attach: \n")
390 (org-attach-attach url
))
392 (defun org-attach-attach (file &optional visit-dir method
)
393 "Move/copy/link FILE into the attachment directory of the current task.
394 If VISIT-DIR is non-nil, visit the directory with dired.
395 METHOD may be `cp', `mv', `ln', `lns' or `url' default taken from
396 `org-attach-method'."
399 (read-file-name "File to keep as an attachment:"
402 (dired-dwim-target-directory))
406 (setq method
(or method org-attach-method
))
407 (let ((basename (file-name-nondirectory file
)))
408 (when (and org-attach-file-list-property
(not org-attach-inherited
))
409 (org-entry-add-to-multivalued-property
410 (point) org-attach-file-list-property basename
))
411 (let* ((attach-dir (org-attach-dir t
))
412 (fname (expand-file-name basename attach-dir
)))
414 ((eq method
'mv
) (rename-file file fname
))
415 ((eq method
'cp
) (copy-file file fname
))
416 ((eq method
'ln
) (add-name-to-file file fname
))
417 ((eq method
'lns
) (make-symbolic-link file fname
))
418 ((eq method
'url
) (url-copy-file file fname
)))
419 (when org-attach-commit
422 (cond ((eq org-attach-store-link-p
'attached
)
423 (org-attach-store-link fname
))
424 ((eq org-attach-store-link-p t
)
425 (org-attach-store-link file
)))
428 (message "File %S is now a task attachment." basename
)))))
430 (defun org-attach-attach-cp ()
431 "Attach a file by copying it."
433 (let ((org-attach-method 'cp
)) (call-interactively 'org-attach-attach
)))
434 (defun org-attach-attach-mv ()
435 "Attach a file by moving (renaming) it."
437 (let ((org-attach-method 'mv
)) (call-interactively 'org-attach-attach
)))
438 (defun org-attach-attach-ln ()
439 "Attach a file by creating a hard link to it.
440 Beware that this does not work on systems that do not support hard links.
441 On some systems, this apparently does copy the file instead."
443 (let ((org-attach-method 'ln
)) (call-interactively 'org-attach-attach
)))
444 (defun org-attach-attach-lns ()
445 "Attach a file by creating a symbolic link to it.
447 Beware that this does not work on systems that do not support symbolic links.
448 On some systems, this apparently does copy the file instead."
450 (let ((org-attach-method 'lns
)) (call-interactively 'org-attach-attach
)))
452 (defun org-attach-new (file)
453 "Create a new attachment FILE for the current task.
454 The attachment is created as an Emacs buffer."
455 (interactive "sCreate attachment named: ")
456 (when (and org-attach-file-list-property
(not org-attach-inherited
))
457 (org-entry-add-to-multivalued-property
458 (point) org-attach-file-list-property file
))
459 (let ((attach-dir (org-attach-dir t
)))
461 (find-file (expand-file-name file attach-dir
))
462 (message "New attachment %s" file
)))
464 (defun org-attach-delete-one (&optional file
)
465 "Delete a single attachment."
467 (let* ((attach-dir (org-attach-dir t
))
468 (files (org-attach-file-list attach-dir
))
471 "Delete attachment: "
473 (list (file-name-nondirectory f
)))
475 (setq file
(expand-file-name file attach-dir
))
476 (unless (file-exists-p file
)
477 (error "No such attachment: %s" file
))
479 (when org-attach-commit
480 (org-attach-commit))))
482 (defun org-attach-delete-all (&optional force
)
483 "Delete all attachments from the current task.
484 This actually deletes the entire attachment directory.
485 A safer way is to open the directory in dired and delete from there."
487 (when (and org-attach-file-list-property
(not org-attach-inherited
))
488 (org-entry-delete (point) org-attach-file-list-property
))
489 (let ((attach-dir (org-attach-dir)))
493 (y-or-n-p "Are you sure you want to remove all attachments of this entry? ")))
494 (shell-command (format "rm -fr %s" attach-dir
))
495 (message "Attachment directory removed")
496 (when org-attach-commit
498 (org-attach-untag))))
500 (defun org-attach-sync ()
501 "Synchronize the current tasks with its attachments.
502 This can be used after files have been added externally."
504 (when org-attach-commit
506 (when (and org-attach-file-list-property
(not org-attach-inherited
))
507 (org-entry-delete (point) org-attach-file-list-property
))
508 (let ((attach-dir (org-attach-dir)))
510 (let ((files (org-attach-file-list attach-dir
)))
511 (org-attach-tag (not files
))
512 (when org-attach-file-list-property
514 (unless (string-match "^\\.\\.?\\'" file
)
515 (org-entry-add-to-multivalued-property
516 (point) org-attach-file-list-property file
))))))))
518 (defun org-attach-file-list (dir)
519 "Return a list of files in the attachment directory.
520 This ignores files ending in \"~\"."
522 (mapcar (lambda (x) (if (string-match "^\\.\\.?\\'" x
) nil x
))
523 (directory-files dir nil
"[^~]\\'"))))
525 (defun org-attach-reveal (&optional if-exists
)
526 "Show the attachment directory of the current task.
527 This will attempt to use an external program to show the directory."
529 (let ((attach-dir (org-attach-dir (not if-exists
))))
530 (and attach-dir
(org-open-file attach-dir
))))
532 (defun org-attach-reveal-in-emacs ()
533 "Show the attachment directory of the current task in dired."
535 (let ((attach-dir (org-attach-dir t
)))
538 (defun org-attach-open (&optional in-emacs
)
539 "Open an attachment of the current task.
540 If there are more than one attachment, you will be prompted for the file name.
541 This command will open the file using the settings in `org-file-apps'
542 and in the system-specific variants of this variable.
543 If IN-EMACS is non-nil, force opening in Emacs."
545 (let* ((attach-dir (org-attach-dir t
))
546 (files (org-attach-file-list attach-dir
))
547 (file (if (= (length files
) 1)
549 (completing-read "Open attachment: "
550 (mapcar #'list files
) nil t
)))
551 (path (expand-file-name file attach-dir
)))
552 (org-attach-annex-get-maybe path
)
553 (org-open-file path in-emacs
)))
555 (defun org-attach-open-in-emacs ()
556 "Open attachment, force opening in Emacs.
557 See `org-attach-open'."
559 (org-attach-open 'in-emacs
))
561 (defun org-attach-expand (file)
562 "Return the full path to the current entry's attachment file FILE.
563 Basically, this adds the path to the attachment directory."
564 (expand-file-name file
(org-attach-dir)))
566 (defun org-attach-expand-link (file)
567 "Return a file link pointing to the current entry's attachment file FILE.
568 Basically, this adds the path to the attachment directory, and a \"file:\"
570 (concat "file:" (org-attach-expand file
)))
572 (defun org-attach-archive-delete-maybe ()
573 "Maybe delete subtree attachments when archiving.
574 This function is called by `org-archive-hook'. The option
575 `org-attach-archive-delete' controls its behavior."
576 (when (if (eq org-attach-archive-delete
'query
)
577 (yes-or-no-p "Delete all attachments? ")
578 org-attach-archive-delete
)
579 (org-attach-delete-all t
)))
582 ;; Attach from dired.
584 ;; Add the following lines to the config file to get a binding for
590 ;; (define-key dired-mode-map (kbd "C-c C-x a") #'org-attach-dired-to-subtree))))
592 (defun org-attach-dired-to-subtree (files)
593 "Attach FILES marked or current file in dired to subtree in other window.
594 Takes the method given in `org-attach-method' for the attach action.
595 Precondition: Point must be in a dired buffer.
596 Idea taken from `gnus-dired-attach'."
598 (list (dired-get-marked-files)))
599 (unless (eq major-mode
'dired-mode
)
600 (user-error "This command must be triggered in a dired buffer."))
601 (let ((start-win (selected-window))
603 (get-window-with-predicate
605 (with-current-buffer (window-buffer window
)
606 (eq major-mode
'org-mode
))))))
609 "Can't attach to subtree. No window displaying an Org buffer"))
610 (select-window other-win
)
612 (org-attach-attach file
))
613 (select-window start-win
)))
617 (add-hook 'org-archive-hook
'org-attach-archive-delete-maybe
)
619 (provide 'org-attach
)
622 ;; generated-autoload-file: "org-loaddefs.el"
625 ;;; org-attach.el ends here