Correct defcustom version information
[org-mode/org-tableheadings.git] / lisp / org-attach.el
blob1dccd1e00885237a9f44d09b5c73b5637a4dc1d0
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.
9 ;;
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/>.
23 ;;; Commentary:
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
36 ;; `org-id-method'.
38 ;;; Code:
40 (require 'cl-lib)
41 (require 'org)
42 (require 'org-id)
43 (require 'vc-git)
45 (declare-function dired-dwim-target-directory "dired-aux")
47 (defgroup org-attach nil
48 "Options concerning entry attachments in Org mode."
49 :tag "Org Attach"
50 :group 'org)
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."
56 :group 'org-attach
57 :type 'directory)
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."
62 :group 'org-attach
63 :type 'boolean
64 :version "26.1"
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."
69 :group 'org-attach
70 :version "24.4"
71 :package-version '(Org . "8.0")
72 :type '(choice
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."
78 :group 'org-attach
79 :type '(choice
80 (const :tag "None" nil)
81 (string :tag "Tag")))
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."
88 :group 'org-attach
89 :type '(choice
90 (const :tag "None" nil)
91 (string :tag "Tag")))
93 (defcustom org-attach-method 'cp
94 "The preferred method to attach a file.
95 Allowed values are:
97 mv rename the file to move it into the attachment directory
98 cp copy the file
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."
103 :group 'org-attach
104 :type '(choice
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."
112 :group 'org-attach
113 :type 'boolean)
115 (defcustom org-attach-allow-inheritance t
116 "Non-nil means allow attachment directories be inherited."
117 :group 'org-attach
118 :type 'boolean)
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."
125 :group 'org-attach
126 :version "24.1"
127 :type '(choice
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."
135 :group 'org-attach
136 :version "26.1"
137 :package-version '(Org . "8.3")
138 :type '(choice
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."
146 :group 'org-attach
147 :package-version '(Org . "9.0")
148 :version "26.1"
149 :type '(choice
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)))
154 ;;;###autoload
155 (defun org-attach ()
156 "The dispatcher for attachment commands.
157 Shows a list of commands and prompts for another key to execute a command."
158 (interactive)
159 (let (c marker)
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)))
163 (unless marker
164 (error "No task in current line")))
165 (save-excursion
166 (when marker
167 (set-buffer (marker-buffer marker))
168 (goto-char marker))
169 (org-back-to-heading t)
170 (save-excursion
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*"))))
198 (cond
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
231 current entry.
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"))
236 (cond
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))
241 (setq attach-dir
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))
250 (t ; use the ID
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
256 (format "%s/%s"
257 (substring uuid 0 2)
258 (substring uuid 2))
259 (expand-file-name org-attach-directory))))))
260 (when attach-dir
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)
265 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 \
282 the directory to
283 the default ID based one."
284 (interactive "P")
285 (let ((old (org-attach-dir))
286 (new
287 (progn
288 (if arg (org-entry-delete nil "ATTACH_DIR")
289 (let ((dir (read-directory-name
290 "Attachment directory: "
291 (org-entry-get nil
292 "ATTACH_DIR"
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)
297 (not old))
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."
308 (interactive)
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)
324 (not
325 (string-equal
326 "found"
327 (shell-command-to-string
328 (format "git annex find --format=found --in=here %s"
329 (shell-quote-argument path-relative))))))
330 (let ((should-get
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)))
334 (if should-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"
338 path))))))
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))
346 (changes 0))
347 (when (and git-dir (executable-find "git"))
348 (with-temp-buffer
349 (cd dir)
350 (dolist (new-or-modified
351 (split-string
352 (shell-command-to-string
353 "git ls-files -zmo --exclude-standard") "\0" t))
354 (if (and use-annex
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))
359 (cl-incf changes))
360 (dolist (deleted
361 (split-string
362 (shell-command-to-string "git ls-files -z --deleted") "\0" t))
363 (call-process "git" nil nil nil "rm" deleted)
364 (cl-incf changes))
365 (when (> changes 0)
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
371 (save-excursion
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))
385 org-stored-links)))
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'."
396 (interactive
397 (list
398 (read-file-name "File to keep as an attachment:"
399 (or (progn
400 (require 'dired-aux)
401 (dired-dwim-target-directory))
402 default-directory))
403 current-prefix-arg
404 nil))
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)))
412 (cond
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
419 (org-attach-commit))
420 (org-attach-tag)
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)))
425 (if visit-dir
426 (dired attach-dir)
427 (message "File %S is now a task attachment." basename)))))
429 (defun org-attach-attach-cp ()
430 "Attach a file by copying it."
431 (interactive)
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."
435 (interactive)
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."
441 (interactive)
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."
448 (interactive)
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)))
459 (org-attach-tag)
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."
465 (interactive)
466 (let* ((attach-dir (org-attach-dir t))
467 (files (org-attach-file-list attach-dir))
468 (file (or file
469 (completing-read
470 "Delete attachment: "
471 (mapcar (lambda (f)
472 (list (file-name-nondirectory f)))
473 files)))))
474 (setq file (expand-file-name file attach-dir))
475 (unless (file-exists-p file)
476 (error "No such attachment: %s" file))
477 (delete-file 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."
485 (interactive "P")
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)))
489 (when
490 (and attach-dir
491 (or force
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
496 (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."
502 (interactive)
503 (when org-attach-commit
504 (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)))
508 (when attach-dir
509 (let ((files (org-attach-file-list attach-dir)))
510 (and files (org-attach-tag))
511 (when org-attach-file-list-property
512 (dolist (file files)
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 \"~\"."
520 (delq nil
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."
527 (interactive "P")
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."
533 (interactive)
534 (let ((attach-dir (org-attach-dir t)))
535 (dired attach-dir)))
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."
543 (interactive "P")
544 (let* ((attach-dir (org-attach-dir t))
545 (files (org-attach-file-list attach-dir))
546 (file (if (= (length files) 1)
547 (car files)
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'."
557 (interactive)
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:\"
568 prefix."
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)
584 ;; Local variables:
585 ;; generated-autoload-file: "org-loaddefs.el"
586 ;; End:
588 ;;; org-attach.el ends here