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 <http://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."
59 (defcustom org-attach-commit t
60 "If non-nil commit attachments with git.
61 This is only done if the Org file is in a git repository."
65 :package-version
'(Org .
"9.0"))
67 (defcustom org-attach-git-annex-cutoff
(* 32 1024)
68 "If non-nil, files larger than this will be annexed instead of stored."
71 :package-version
'(Org .
"8.0")
73 (const :tag
"None" nil
)
74 (integer :tag
"Bytes")))
76 (defcustom org-attach-auto-tag
"ATTACH"
77 "Tag that will be triggered automatically when an entry has an attachment."
80 (const :tag
"None" nil
)
83 (defcustom org-attach-file-list-property
"Attachments"
84 "The property used to keep a list of attachment belonging to this entry.
85 This is not really needed, so you may set this to nil if you don't want it.
86 Also, for entries where children inherit the directory, the list of
87 attachments is not kept in this property."
90 (const :tag
"None" nil
)
93 (defcustom org-attach-method
'cp
94 "The preferred method to attach a file.
97 mv rename the file to move it into the attachment directory
99 ln create a hard link. Note that this is not supported
100 on all systems, and then the result is not defined.
101 lns create a symbol link. Note that this is not supported
102 on all systems, and then the result is not defined."
105 (const :tag
"Copy" cp
)
106 (const :tag
"Move/Rename" mv
)
107 (const :tag
"Hard Link" ln
)
108 (const :tag
"Symbol Link" lns
)))
110 (defcustom org-attach-expert nil
111 "Non-nil means do not show the splash buffer with the attach dispatcher."
115 (defcustom org-attach-allow-inheritance t
116 "Non-nil means allow attachment directories be inherited."
120 (defvar org-attach-inherited nil
121 "Indicates if the last access to the attachment directory was inherited.")
123 (defcustom org-attach-store-link-p nil
124 "Non-nil means store a link to a file when attaching it."
128 (const :tag
"Don't store link" nil
)
129 (const :tag
"Link to origin location" t
)
130 (const :tag
"Link to the attach-dir location" attached
)))
132 (defcustom org-attach-archive-delete nil
133 "Non-nil means attachments are deleted upon archiving a subtree.
134 When set to `query', ask the user instead."
137 :package-version
'(Org .
"8.3")
139 (const :tag
"Never delete attachments" nil
)
140 (const :tag
"Always delete attachments" t
)
141 (const :tag
"Query the user" query
)))
143 (defcustom org-attach-annex-auto-get
'ask
144 "Confirmation preference for automatically getting annex files.
145 If \\='ask, prompt using `y-or-n-p'. If t, always get. If nil, never get."
147 :package-version
'(Org .
"9.0")
150 (const :tag
"confirm with `y-or-n-p'" ask
)
151 (const :tag
"always get from annex if necessary" t
)
152 (const :tag
"never get from annex" nil
)))
156 "The dispatcher for attachment commands.
157 Shows a list of commands and prompts for another key to execute a command."
160 (when (eq major-mode
'org-agenda-mode
)
161 (setq marker
(or (get-text-property (point) 'org-hd-marker
)
162 (get-text-property (point) 'org-marker
)))
164 (error "No task in current line")))
167 (set-buffer (marker-buffer marker
))
169 (org-back-to-heading t
)
171 (save-window-excursion
172 (unless org-attach-expert
173 (with-output-to-temp-buffer "*Org Attach*"
174 (princ "Select an Attachment Command:
176 a Select a file and attach it to the task, using `org-attach-method'.
177 c/m/l/y Attach a file using copy/move/link/symbolic-link method.
178 u Attach a file from URL (downloading it).
179 n Create a new attachment, as an Emacs buffer.
180 z Synchronize the current task with its attachment
181 directory, in case you added attachments yourself.
183 o Open current task's attachments.
184 O Like \"o\", but force opening in Emacs.
185 f Open current task's attachment directory.
186 F Like \"f\", but force using dired in Emacs.
188 d Delete one attachment, you will be prompted for a file name.
189 D Delete all of a task's attachments. A safer way is
190 to open the directory in dired and delete from there.
192 s Set a specific attachment directory for this entry or reset to default.
193 i Make children of the current entry inherit its attachment directory.")))
194 (org-fit-window-to-buffer (get-buffer-window "*Org Attach*"))
195 (message "Select command: [acmlzoOfFdD]")
196 (setq c
(read-char-exclusive))
197 (and (get-buffer "*Org Attach*") (kill-buffer "*Org Attach*"))))
199 ((memq c
'(?a ?\C-a
)) (call-interactively 'org-attach-attach
))
200 ((memq c
'(?c ?\C-c
))
201 (let ((org-attach-method 'cp
)) (call-interactively 'org-attach-attach
)))
202 ((memq c
'(?m ?\C-m
))
203 (let ((org-attach-method 'mv
)) (call-interactively 'org-attach-attach
)))
204 ((memq c
'(?l ?\C-l
))
205 (let ((org-attach-method 'ln
)) (call-interactively 'org-attach-attach
)))
206 ((memq c
'(?y ?\C-y
))
207 (let ((org-attach-method 'lns
)) (call-interactively 'org-attach-attach
)))
208 ((memq c
'(?u ?\C-u
))
209 (let ((org-attach-method 'url
)) (call-interactively 'org-attach-url
)))
210 ((memq c
'(?n ?\C-n
)) (call-interactively 'org-attach-new
))
211 ((memq c
'(?z ?\C-z
)) (call-interactively 'org-attach-sync
))
212 ((memq c
'(?o ?\C-o
)) (call-interactively 'org-attach-open
))
213 ((eq c ?O
) (call-interactively 'org-attach-open-in-emacs
))
214 ((memq c
'(?f ?\C-f
)) (call-interactively 'org-attach-reveal
))
215 ((memq c
'(?F
)) (call-interactively 'org-attach-reveal-in-emacs
))
216 ((memq c
'(?d ?\C-d
)) (call-interactively
217 'org-attach-delete-one
))
218 ((eq c ?D
) (call-interactively 'org-attach-delete-all
))
219 ((eq c ?q
) (message "Abort"))
220 ((memq c
'(?s ?\C-s
)) (call-interactively
221 'org-attach-set-directory
))
222 ((memq c
'(?i ?\C-i
)) (call-interactively
223 'org-attach-set-inherit
))
224 (t (error "No such attachment command %c" c
))))))
226 (defun org-attach-dir (&optional create-if-not-exists-p
)
227 "Return the directory associated with the current entry.
228 This first checks for a local property ATTACH_DIR, and then for an inherited
229 property ATTACH_DIR_INHERIT. If neither exists, the default mechanism
230 using the entry ID will be invoked to access the unique directory for the
232 If the directory does not exist and CREATE-IF-NOT-EXISTS-P is non-nil,
233 the directory and (if necessary) the corresponding ID will be created."
234 (let (attach-dir uuid
)
235 (setq org-attach-inherited
(org-entry-get nil
"ATTACH_DIR_INHERIT"))
237 ((setq attach-dir
(org-entry-get nil
"ATTACH_DIR"))
238 (org-attach-check-absolute-path attach-dir
))
239 ((and org-attach-allow-inheritance
240 (org-entry-get nil
"ATTACH_DIR_INHERIT" t
))
242 (org-with-wide-buffer
243 (if (marker-position org-entry-property-inherited-from
)
244 (goto-char org-entry-property-inherited-from
)
245 (org-back-to-heading t
))
246 (let (org-attach-allow-inheritance)
247 (org-attach-dir create-if-not-exists-p
))))
248 (org-attach-check-absolute-path attach-dir
)
249 (setq org-attach-inherited t
))
251 (org-attach-check-absolute-path nil
)
252 (setq uuid
(org-id-get (point) create-if-not-exists-p
))
253 (when (or uuid create-if-not-exists-p
)
254 (unless uuid
(error "ID retrieval/creation failed"))
255 (setq attach-dir
(expand-file-name
259 (expand-file-name org-attach-directory
))))))
261 (if (and create-if-not-exists-p
262 (not (file-directory-p attach-dir
)))
263 (make-directory attach-dir t
))
264 (and (file-exists-p attach-dir
)
267 (defun org-attach-check-absolute-path (dir)
268 "Check if we have enough information to root the attachment directory.
269 When DIR is given, check also if it is already absolute. Otherwise,
270 assume that it will be relative, and check if `org-attach-directory' is
271 absolute, or if at least the current buffer has a file name.
272 Throw an error if we cannot root the directory."
273 (or (and dir
(file-name-absolute-p dir
))
274 (file-name-absolute-p org-attach-directory
)
275 (buffer-file-name (buffer-base-buffer))
276 (error "Need absolute `org-attach-directory' to attach in buffers without filename")))
278 (defun org-attach-set-directory (&optional arg
)
279 "Set the ATTACH_DIR node property and ask to move files there.
280 The property defines the directory that is used for attachments
281 of the entry. When called with `\\[universal-argument]', reset \
283 the default ID based one."
285 (let ((old (org-attach-dir))
288 (if arg
(org-entry-delete nil
"ATTACH_DIR")
289 (let ((dir (read-directory-name
290 "Attachment directory: "
293 (and org-attach-allow-inheritance t
)))))
294 (org-entry-put nil
"ATTACH_DIR" dir
)))
295 (org-attach-dir t
))))
296 (unless (or (string= old new
)
298 (when (yes-or-no-p "Copy over attachments from old directory? ")
299 (copy-directory old new t nil t
))
300 (when (yes-or-no-p (concat "Delete " old
))
301 (delete-directory old t
)))))
303 (defun org-attach-set-inherit ()
304 "Set the ATTACH_DIR_INHERIT property of the current entry.
305 The property defines the directory that is used for attachments
306 of the entry and any children that do not explicitly define (by setting
307 the ATTACH_DIR property) their own attachment directory."
309 (org-entry-put nil
"ATTACH_DIR_INHERIT" "t")
310 (message "Children will inherit attachment directory"))
312 (defun org-attach-use-annex ()
313 "Return non-nil if git annex can be used."
314 (let ((git-dir (vc-git-root (expand-file-name org-attach-directory
))))
315 (and org-attach-git-annex-cutoff
316 (or (file-exists-p (expand-file-name "annex" git-dir
))
317 (file-exists-p (expand-file-name ".git/annex" git-dir
))))))
319 (defun org-attach-annex-get-maybe (path)
320 "Call git annex get PATH (via shell) if using git annex.
321 Signals an error if the file content is not available and it was not retrieved."
322 (let ((path-relative (file-relative-name path
)))
323 (when (and (org-attach-use-annex)
327 (shell-command-to-string
328 (format "git annex find --format=found --in=here %s"
329 (shell-quote-argument path-relative
))))))
331 (if (eq org-attach-annex-auto-get
'ask
)
332 (y-or-n-p (format "Run git annex get %s? " path-relative
))
333 org-attach-annex-auto-get
)))
335 (progn (message "Running git annex get \"%s\"." path-relative
)
336 (call-process "git" nil nil nil
"annex" "get" path-relative
))
337 (error "File %s stored in git annex but it is not available, and was not retrieved"
340 (defun org-attach-commit ()
341 "Commit changes to git if `org-attach-directory' is properly initialized.
342 This checks for the existence of a \".git\" directory in that directory."
343 (let* ((dir (expand-file-name org-attach-directory
))
344 (git-dir (vc-git-root dir
))
345 (use-annex (org-attach-use-annex))
347 (when (and git-dir
(executable-find "git"))
350 (dolist (new-or-modified
352 (shell-command-to-string
353 "git ls-files -zmo --exclude-standard") "\0" t
))
355 (>= (nth 7 (file-attributes new-or-modified
))
356 org-attach-git-annex-cutoff
))
357 (call-process "git" nil nil nil
"annex" "add" new-or-modified
)
358 (call-process "git" nil nil nil
"add" new-or-modified
))
362 (shell-command-to-string "git ls-files -z --deleted") "\0" t
))
363 (call-process "git" nil nil nil
"rm" deleted
)
366 (shell-command "git commit -m 'Synchronized attachments'"))))))
368 (defun org-attach-tag (&optional off
)
369 "Turn the autotag on or (if OFF is set) off."
370 (when org-attach-auto-tag
372 (org-back-to-heading t
)
373 (org-toggle-tag org-attach-auto-tag
(if off
'off
'on
)))))
375 (defun org-attach-untag ()
376 "Turn the autotag off."
377 (org-attach-tag 'off
))
379 (defun org-attach-store-link (file)
380 "Add a link to `org-stored-link' when attaching a file.
381 Only do this when `org-attach-store-link-p' is non-nil."
382 (setq org-stored-links
383 (cons (list (org-attach-expand-link file
)
384 (file-name-nondirectory file
))
387 (defun org-attach-url (url)
388 (interactive "MURL of the file to attach: \n")
389 (org-attach-attach url
))
391 (defun org-attach-attach (file &optional visit-dir method
)
392 "Move/copy/link FILE into the attachment directory of the current task.
393 If VISIT-DIR is non-nil, visit the directory with dired.
394 METHOD may be `cp', `mv', `ln', `lns' or `url' default taken from
395 `org-attach-method'."
398 (read-file-name "File to keep as an attachment:"
401 (dired-dwim-target-directory))
405 (setq method
(or method org-attach-method
))
406 (let ((basename (file-name-nondirectory file
)))
407 (when (and org-attach-file-list-property
(not org-attach-inherited
))
408 (org-entry-add-to-multivalued-property
409 (point) org-attach-file-list-property basename
))
410 (let* ((attach-dir (org-attach-dir t
))
411 (fname (expand-file-name basename attach-dir
)))
413 ((eq method
'mv
) (rename-file file fname
))
414 ((eq method
'cp
) (copy-file file fname
))
415 ((eq method
'ln
) (add-name-to-file file fname
))
416 ((eq method
'lns
) (make-symbolic-link file fname
))
417 ((eq method
'url
) (url-copy-file file fname
)))
418 (when org-attach-commit
421 (cond ((eq org-attach-store-link-p
'attached
)
422 (org-attach-store-link fname
))
423 ((eq org-attach-store-link-p t
)
424 (org-attach-store-link file
)))
427 (message "File %S is now a task attachment." basename
)))))
429 (defun org-attach-attach-cp ()
430 "Attach a file by copying it."
432 (let ((org-attach-method 'cp
)) (call-interactively 'org-attach-attach
)))
433 (defun org-attach-attach-mv ()
434 "Attach a file by moving (renaming) it."
436 (let ((org-attach-method 'mv
)) (call-interactively 'org-attach-attach
)))
437 (defun org-attach-attach-ln ()
438 "Attach a file by creating a hard link to it.
439 Beware that this does not work on systems that do not support hard links.
440 On some systems, this apparently does copy the file instead."
442 (let ((org-attach-method 'ln
)) (call-interactively 'org-attach-attach
)))
443 (defun org-attach-attach-lns ()
444 "Attach a file by creating a symbolic link to it.
446 Beware that this does not work on systems that do not support symbolic links.
447 On some systems, this apparently does copy the file instead."
449 (let ((org-attach-method 'lns
)) (call-interactively 'org-attach-attach
)))
451 (defun org-attach-new (file)
452 "Create a new attachment FILE for the current task.
453 The attachment is created as an Emacs buffer."
454 (interactive "sCreate attachment named: ")
455 (when (and org-attach-file-list-property
(not org-attach-inherited
))
456 (org-entry-add-to-multivalued-property
457 (point) org-attach-file-list-property file
))
458 (let ((attach-dir (org-attach-dir t
)))
460 (find-file (expand-file-name file attach-dir
))
461 (message "New attachment %s" file
)))
463 (defun org-attach-delete-one (&optional file
)
464 "Delete a single attachment."
466 (let* ((attach-dir (org-attach-dir t
))
467 (files (org-attach-file-list attach-dir
))
470 "Delete attachment: "
472 (list (file-name-nondirectory f
)))
474 (setq file
(expand-file-name file attach-dir
))
475 (unless (file-exists-p file
)
476 (error "No such attachment: %s" file
))
478 (when org-attach-commit
479 (org-attach-commit))))
481 (defun org-attach-delete-all (&optional force
)
482 "Delete all attachments from the current task.
483 This actually deletes the entire attachment directory.
484 A safer way is to open the directory in dired and delete from there."
486 (when (and org-attach-file-list-property
(not org-attach-inherited
))
487 (org-entry-delete (point) org-attach-file-list-property
))
488 (let ((attach-dir (org-attach-dir)))
492 (y-or-n-p "Are you sure you want to remove all attachments of this entry? ")))
493 (shell-command (format "rm -fr %s" attach-dir
))
494 (message "Attachment directory removed")
495 (when org-attach-commit
497 (org-attach-untag))))
499 (defun org-attach-sync ()
500 "Synchronize the current tasks with its attachments.
501 This can be used after files have been added externally."
503 (when org-attach-commit
505 (when (and org-attach-file-list-property
(not org-attach-inherited
))
506 (org-entry-delete (point) org-attach-file-list-property
))
507 (let ((attach-dir (org-attach-dir)))
509 (let ((files (org-attach-file-list attach-dir
)))
510 (and files
(org-attach-tag))
511 (when org-attach-file-list-property
513 (unless (string-match "^\\.\\.?\\'" file
)
514 (org-entry-add-to-multivalued-property
515 (point) org-attach-file-list-property file
))))))))
517 (defun org-attach-file-list (dir)
518 "Return a list of files in the attachment directory.
519 This ignores files ending in \"~\"."
521 (mapcar (lambda (x) (if (string-match "^\\.\\.?\\'" x
) nil x
))
522 (directory-files dir nil
"[^~]\\'"))))
524 (defun org-attach-reveal (&optional if-exists
)
525 "Show the attachment directory of the current task.
526 This will attempt to use an external program to show the directory."
528 (let ((attach-dir (org-attach-dir (not if-exists
))))
529 (and attach-dir
(org-open-file attach-dir
))))
531 (defun org-attach-reveal-in-emacs ()
532 "Show the attachment directory of the current task in dired."
534 (let ((attach-dir (org-attach-dir t
)))
537 (defun org-attach-open (&optional in-emacs
)
538 "Open an attachment of the current task.
539 If there are more than one attachment, you will be prompted for the file name.
540 This command will open the file using the settings in `org-file-apps'
541 and in the system-specific variants of this variable.
542 If IN-EMACS is non-nil, force opening in Emacs."
544 (let* ((attach-dir (org-attach-dir t
))
545 (files (org-attach-file-list attach-dir
))
546 (file (if (= (length files
) 1)
548 (completing-read "Open attachment: "
549 (mapcar #'list files
) nil t
)))
550 (path (expand-file-name file attach-dir
)))
551 (org-attach-annex-get-maybe path
)
552 (org-open-file path in-emacs
)))
554 (defun org-attach-open-in-emacs ()
555 "Open attachment, force opening in Emacs.
556 See `org-attach-open'."
558 (org-attach-open 'in-emacs
))
560 (defun org-attach-expand (file)
561 "Return the full path to the current entry's attachment file FILE.
562 Basically, this adds the path to the attachment directory."
563 (expand-file-name file
(org-attach-dir)))
565 (defun org-attach-expand-link (file)
566 "Return a file link pointing to the current entry's attachment file FILE.
567 Basically, this adds the path to the attachment directory, and a \"file:\"
569 (concat "file:" (org-attach-expand file
)))
571 (defun org-attach-archive-delete-maybe ()
572 "Maybe delete subtree attachments when archiving.
573 This function is called by `org-archive-hook'. The option
574 `org-attach-archive-delete' controls its behavior."
575 (when (if (eq org-attach-archive-delete
'query
)
576 (yes-or-no-p "Delete all attachments? ")
577 org-attach-archive-delete
)
578 (org-attach-delete-all t
)))
580 (add-hook 'org-archive-hook
'org-attach-archive-delete-maybe
)
582 (provide 'org-attach
)
585 ;; generated-autoload-file: "org-loaddefs.el"
588 ;;; org-attach.el ends here