Fix `org-set-visibility-according-to-property'
[org-mode/org-kjn.git] / lisp / org-attach.el
blob1737ec1742d14c460173e73f0b13cc5e436bb288
1 ;;; org-attach.el --- Manage file attachments to org-mode tasks
3 ;; Copyright (C) 2008-2014 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-mode 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 (eval-when-compile
41 (require 'cl))
42 (require 'org-id)
43 (require 'org)
44 (require 'vc-git)
46 (defgroup org-attach nil
47 "Options concerning entry attachments in Org-mode."
48 :tag "Org Attach"
49 :group 'org)
51 (defcustom org-attach-directory "data/"
52 "The directory where attachments are stored.
53 If this is a relative path, it will be interpreted relative to the directory
54 where the Org file lives."
55 :group 'org-attach
56 :type 'directory)
58 (defcustom org-attach-git-annex-cutoff (* 32 1024)
59 "If non-nil, files larger than this will be annexed instead of stored."
60 :group 'org-attach
61 :version "24.4"
62 :package-version '(Org . "8.0")
63 :type '(choice
64 (const :tag "None" nil)
65 (integer :tag "Bytes")))
67 (defcustom org-attach-auto-tag "ATTACH"
68 "Tag that will be triggered automatically when an entry has an attachment."
69 :group 'org-attach
70 :type '(choice
71 (const :tag "None" nil)
72 (string :tag "Tag")))
74 (defcustom org-attach-file-list-property "Attachments"
75 "The property used to keep a list of attachment belonging to this entry.
76 This is not really needed, so you may set this to nil if you don't want it.
77 Also, for entries where children inherit the directory, the list of
78 attachments is not kept in this property."
79 :group 'org-attach
80 :type '(choice
81 (const :tag "None" nil)
82 (string :tag "Tag")))
84 (defcustom org-attach-method 'cp
85 "The preferred method to attach a file.
86 Allowed values are:
88 mv rename the file to move it into the attachment directory
89 cp copy the file
90 ln create a hard link. Note that this is not supported
91 on all systems, and then the result is not defined.
92 lns create a symbol link. Note that this is not supported
93 on all systems, and then the result is not defined."
94 :group 'org-attach
95 :type '(choice
96 (const :tag "Copy" cp)
97 (const :tag "Move/Rename" mv)
98 (const :tag "Hard Link" ln)
99 (const :tag "Symbol Link" lns)))
101 (defcustom org-attach-expert nil
102 "Non-nil means do not show the splash buffer with the attach dispatcher."
103 :group 'org-attach
104 :type 'boolean)
106 (defcustom org-attach-allow-inheritance t
107 "Non-nil means allow attachment directories be inherited."
108 :group 'org-attach
109 :type 'boolean)
111 (defvar org-attach-inherited nil
112 "Indicates if the last access to the attachment directory was inherited.")
114 (defcustom org-attach-store-link-p nil
115 "Non-nil means store a link to a file when attaching it."
116 :group 'org-attach
117 :version "24.1"
118 :type '(choice
119 (const :tag "Don't store link" nil)
120 (const :tag "Link to origin location" t)
121 (const :tag "Link to the attach-dir location" attached)))
123 (defcustom org-attach-archive-delete nil
124 "Non-nil means attachments are deleted upon archiving a subtree.
125 When set to `query', ask the user instead."
126 :group 'org-attach
127 :version "25.1"
128 :package-version '(Org . "8.3")
129 :type '(choice
130 (const :tag "Never delete attachments" nil)
131 (const :tag "Always delete attachments" t)
132 (const :tag "Query the user" query)))
134 ;;;###autoload
135 (defun org-attach ()
136 "The dispatcher for attachment commands.
137 Shows a list of commands and prompts for another key to execute a command."
138 (interactive)
139 (let (c marker)
140 (when (eq major-mode 'org-agenda-mode)
141 (setq marker (or (get-text-property (point) 'org-hd-marker)
142 (get-text-property (point) 'org-marker)))
143 (unless marker
144 (error "No task in current line")))
145 (save-excursion
146 (when marker
147 (set-buffer (marker-buffer marker))
148 (goto-char marker))
149 (org-back-to-heading t)
150 (save-excursion
151 (save-window-excursion
152 (unless org-attach-expert
153 (with-output-to-temp-buffer "*Org Attach*"
154 (princ "Select an Attachment Command:
156 a Select a file and attach it to the task, using `org-attach-method'.
157 c/m/l/y Attach a file using copy/move/link/symbolic-link method.
158 n Create a new attachment, as an Emacs buffer.
159 z Synchronize the current task with its attachment
160 directory, in case you added attachments yourself.
162 o Open current task's attachments.
163 O Like \"o\", but force opening in Emacs.
164 f Open current task's attachment directory.
165 F Like \"f\", but force using dired in Emacs.
167 d Delete one attachment, you will be prompted for a file name.
168 D Delete all of a task's attachments. A safer way is
169 to open the directory in dired and delete from there.
171 s Set a specific attachment directory for this entry.
172 i Make children of the current entry inherit its attachment directory.")))
173 (org-fit-window-to-buffer (get-buffer-window "*Org Attach*"))
174 (message "Select command: [acmlzoOfFdD]")
175 (setq c (read-char-exclusive))
176 (and (get-buffer "*Org Attach*") (kill-buffer "*Org Attach*"))))
177 (cond
178 ((memq c '(?a ?\C-a)) (call-interactively 'org-attach-attach))
179 ((memq c '(?c ?\C-c))
180 (let ((org-attach-method 'cp)) (call-interactively 'org-attach-attach)))
181 ((memq c '(?m ?\C-m))
182 (let ((org-attach-method 'mv)) (call-interactively 'org-attach-attach)))
183 ((memq c '(?l ?\C-l))
184 (let ((org-attach-method 'ln)) (call-interactively 'org-attach-attach)))
185 ((memq c '(?y ?\C-y))
186 (let ((org-attach-method 'lns)) (call-interactively 'org-attach-attach)))
187 ((memq c '(?n ?\C-n)) (call-interactively 'org-attach-new))
188 ((memq c '(?z ?\C-z)) (call-interactively 'org-attach-sync))
189 ((memq c '(?o ?\C-o)) (call-interactively 'org-attach-open))
190 ((eq c ?O) (call-interactively 'org-attach-open-in-emacs))
191 ((memq c '(?f ?\C-f)) (call-interactively 'org-attach-reveal))
192 ((memq c '(?F)) (call-interactively 'org-attach-reveal-in-emacs))
193 ((memq c '(?d ?\C-d)) (call-interactively
194 'org-attach-delete-one))
195 ((eq c ?D) (call-interactively 'org-attach-delete-all))
196 ((eq c ?q) (message "Abort"))
197 ((memq c '(?s ?\C-s)) (call-interactively
198 'org-attach-set-directory))
199 ((memq c '(?i ?\C-i)) (call-interactively
200 'org-attach-set-inherit))
201 (t (error "No such attachment command %c" c))))))
203 (defun org-attach-dir (&optional create-if-not-exists-p)
204 "Return the directory associated with the current entry.
205 This first checks for a local property ATTACH_DIR, and then for an inherited
206 property ATTACH_DIR_INHERIT. If neither exists, the default mechanism
207 using the entry ID will be invoked to access the unique directory for the
208 current entry.
209 If the directory does not exist and CREATE-IF-NOT-EXISTS-P is non-nil,
210 the directory and (if necessary) the corresponding ID will be created."
211 (let (attach-dir uuid inherit)
212 (setq org-attach-inherited (org-entry-get nil "ATTACH_DIR_INHERIT"))
213 (cond
214 ((setq attach-dir (org-entry-get nil "ATTACH_DIR"))
215 (org-attach-check-absolute-path attach-dir))
216 ((and org-attach-allow-inheritance
217 (setq inherit (org-entry-get nil "ATTACH_DIR_INHERIT" t)))
218 (setq attach-dir
219 (save-excursion
220 (save-restriction
221 (widen)
222 (if (marker-position org-entry-property-inherited-from)
223 (goto-char org-entry-property-inherited-from)
224 (org-back-to-heading t))
225 (let (org-attach-allow-inheritance)
226 (org-attach-dir create-if-not-exists-p)))))
227 (org-attach-check-absolute-path attach-dir)
228 (setq org-attach-inherited t))
229 (t ; use the ID
230 (org-attach-check-absolute-path nil)
231 (setq uuid (org-id-get (point) create-if-not-exists-p))
232 (when (or uuid create-if-not-exists-p)
233 (unless uuid (error "ID retrieval/creation failed"))
234 (setq attach-dir (expand-file-name
235 (format "%s/%s"
236 (substring uuid 0 2)
237 (substring uuid 2))
238 (expand-file-name org-attach-directory))))))
239 (when attach-dir
240 (if (and create-if-not-exists-p
241 (not (file-directory-p attach-dir)))
242 (make-directory attach-dir t))
243 (and (file-exists-p attach-dir)
244 attach-dir))))
246 (defun org-attach-check-absolute-path (dir)
247 "Check if we have enough information to root the attachment directory.
248 When DIR is given, check also if it is already absolute. Otherwise,
249 assume that it will be relative, and check if `org-attach-directory' is
250 absolute, or if at least the current buffer has a file name.
251 Throw an error if we cannot root the directory."
252 (or (and dir (file-name-absolute-p dir))
253 (file-name-absolute-p org-attach-directory)
254 (buffer-file-name (buffer-base-buffer))
255 (error "Need absolute `org-attach-directory' to attach in buffers without filename")))
257 (defun org-attach-set-directory ()
258 "Set the ATTACH_DIR property of the current entry.
259 The property defines the directory that is used for attachments
260 of the entry."
261 (interactive)
262 (let ((dir (org-entry-get nil "ATTACH_DIR")))
263 (setq dir (read-directory-name "Attachment directory: " dir))
264 (org-entry-put nil "ATTACH_DIR" dir)))
266 (defun org-attach-set-inherit ()
267 "Set the ATTACH_DIR_INHERIT property of the current entry.
268 The property defines the directory that is used for attachments
269 of the entry and any children that do not explicitly define (by setting
270 the ATTACH_DIR property) their own attachment directory."
271 (interactive)
272 (org-entry-put nil "ATTACH_DIR_INHERIT" "t")
273 (message "Children will inherit attachment directory"))
275 (defun org-attach-commit ()
276 "Commit changes to git if `org-attach-directory' is properly initialized.
277 This checks for the existence of a \".git\" directory in that directory."
278 (let* ((dir (expand-file-name org-attach-directory))
279 (git-dir (vc-git-root dir))
280 (changes 0))
281 (when (and git-dir (executable-find "git"))
282 (with-temp-buffer
283 (cd dir)
284 (let ((have-annex
285 (and org-attach-git-annex-cutoff
286 (file-exists-p (expand-file-name "annex" git-dir)))))
287 (dolist (new-or-modified
288 (split-string
289 (shell-command-to-string
290 "git ls-files -zmo --exclude-standard") "\0" t))
291 (if (and have-annex
292 (>= (nth 7 (file-attributes new-or-modified))
293 org-attach-git-annex-cutoff))
294 (call-process "git" nil nil nil "annex" "add" new-or-modified)
295 (call-process "git" nil nil nil "add" new-or-modified))
296 (incf changes)))
297 (dolist (deleted
298 (split-string
299 (shell-command-to-string "git ls-files -z --deleted") "\0" t))
300 (call-process "git" nil nil nil "rm" deleted)
301 (incf changes))
302 (when (> changes 0)
303 (shell-command "git commit -m 'Synchronized attachments'"))))))
305 (defun org-attach-tag (&optional off)
306 "Turn the autotag on or (if OFF is set) off."
307 (when org-attach-auto-tag
308 (save-excursion
309 (org-back-to-heading t)
310 (org-toggle-tag org-attach-auto-tag (if off 'off 'on)))))
312 (defun org-attach-untag ()
313 "Turn the autotag off."
314 (org-attach-tag 'off))
316 (defun org-attach-store-link (file)
317 "Add a link to `org-stored-link' when attaching a file.
318 Only do this when `org-attach-store-link-p' is non-nil."
319 (setq org-stored-links
320 (cons (list (org-attach-expand-link file)
321 (file-name-nondirectory file))
322 org-stored-links)))
324 (defun org-attach-attach (file &optional visit-dir method)
325 "Move/copy/link FILE into the attachment directory of the current task.
326 If VISIT-DIR is non-nil, visit the directory with dired.
327 METHOD may be `cp', `mv', `ln', or `lns' default taken from
328 `org-attach-method'."
329 (interactive "fFile to keep as an attachment: \nP")
330 (setq method (or method org-attach-method))
331 (let ((basename (file-name-nondirectory file)))
332 (when (and org-attach-file-list-property (not org-attach-inherited))
333 (org-entry-add-to-multivalued-property
334 (point) org-attach-file-list-property basename))
335 (let* ((attach-dir (org-attach-dir t))
336 (fname (expand-file-name basename attach-dir)))
337 (cond
338 ((eq method 'mv) (rename-file file fname))
339 ((eq method 'cp) (copy-file file fname))
340 ((eq method 'ln) (add-name-to-file file fname))
341 ((eq method 'lns) (make-symbolic-link file fname)))
342 (org-attach-commit)
343 (org-attach-tag)
344 (cond ((eq org-attach-store-link-p 'attached)
345 (org-attach-store-link fname))
346 ((eq org-attach-store-link-p t)
347 (org-attach-store-link file)))
348 (if visit-dir
349 (dired attach-dir)
350 (message "File \"%s\" is now a task attachment." basename)))))
352 (defun org-attach-attach-cp ()
353 "Attach a file by copying it."
354 (interactive)
355 (let ((org-attach-method 'cp)) (call-interactively 'org-attach-attach)))
356 (defun org-attach-attach-mv ()
357 "Attach a file by moving (renaming) it."
358 (interactive)
359 (let ((org-attach-method 'mv)) (call-interactively 'org-attach-attach)))
360 (defun org-attach-attach-ln ()
361 "Attach a file by creating a hard link to it.
362 Beware that this does not work on systems that do not support hard links.
363 On some systems, this apparently does copy the file instead."
364 (interactive)
365 (let ((org-attach-method 'ln)) (call-interactively 'org-attach-attach)))
366 (defun org-attach-attach-lns ()
367 "Attach a file by creating a symbolic link to it.
369 Beware that this does not work on systems that do not support symbolic links.
370 On some systems, this apparently does copy the file instead."
371 (interactive)
372 (let ((org-attach-method 'lns)) (call-interactively 'org-attach-attach)))
374 (defun org-attach-new (file)
375 "Create a new attachment FILE for the current task.
376 The attachment is created as an Emacs buffer."
377 (interactive "sCreate attachment named: ")
378 (when (and org-attach-file-list-property (not org-attach-inherited))
379 (org-entry-add-to-multivalued-property
380 (point) org-attach-file-list-property file))
381 (let ((attach-dir (org-attach-dir t)))
382 (org-attach-tag)
383 (find-file (expand-file-name file attach-dir))
384 (message "New attachment %s" file)))
386 (defun org-attach-delete-one (&optional file)
387 "Delete a single attachment."
388 (interactive)
389 (let* ((attach-dir (org-attach-dir t))
390 (files (org-attach-file-list attach-dir))
391 (file (or file
392 (org-icompleting-read
393 "Delete attachment: "
394 (mapcar (lambda (f)
395 (list (file-name-nondirectory f)))
396 files)))))
397 (setq file (expand-file-name file attach-dir))
398 (unless (file-exists-p file)
399 (error "No such attachment: %s" file))
400 (delete-file file)
401 (org-attach-commit)))
403 (defun org-attach-delete-all (&optional force)
404 "Delete all attachments from the current task.
405 This actually deletes the entire attachment directory.
406 A safer way is to open the directory in dired and delete from there."
407 (interactive "P")
408 (when (and org-attach-file-list-property (not org-attach-inherited))
409 (org-entry-delete (point) org-attach-file-list-property))
410 (let ((attach-dir (org-attach-dir)))
411 (when
412 (and attach-dir
413 (or force
414 (y-or-n-p "Are you sure you want to remove all attachments of this entry? ")))
415 (shell-command (format "rm -fr %s" attach-dir))
416 (message "Attachment directory removed")
417 (org-attach-commit)
418 (org-attach-untag))))
420 (defun org-attach-sync ()
421 "Synchronize the current tasks with its attachments.
422 This can be used after files have been added externally."
423 (interactive)
424 (org-attach-commit)
425 (when (and org-attach-file-list-property (not org-attach-inherited))
426 (org-entry-delete (point) org-attach-file-list-property))
427 (let ((attach-dir (org-attach-dir)))
428 (when attach-dir
429 (let ((files (org-attach-file-list attach-dir)))
430 (and files (org-attach-tag))
431 (when org-attach-file-list-property
432 (dolist (file files)
433 (unless (string-match "^\\.\\.?\\'" file)
434 (org-entry-add-to-multivalued-property
435 (point) org-attach-file-list-property file))))))))
437 (defun org-attach-file-list (dir)
438 "Return a list of files in the attachment directory.
439 This ignores files ending in \"~\"."
440 (delq nil
441 (mapcar (lambda (x) (if (string-match "^\\.\\.?\\'" x) nil x))
442 (directory-files dir nil "[^~]\\'"))))
444 (defun org-attach-reveal (&optional if-exists)
445 "Show the attachment directory of the current task.
446 This will attempt to use an external program to show the directory."
447 (interactive "P")
448 (let ((attach-dir (org-attach-dir (not if-exists))))
449 (and attach-dir (org-open-file attach-dir))))
451 (defun org-attach-reveal-in-emacs ()
452 "Show the attachment directory of the current task in dired."
453 (interactive)
454 (let ((attach-dir (org-attach-dir t)))
455 (dired attach-dir)))
457 (defun org-attach-open (&optional in-emacs)
458 "Open an attachment of the current task.
459 If there are more than one attachment, you will be prompted for the file name.
460 This command will open the file using the settings in `org-file-apps'
461 and in the system-specific variants of this variable.
462 If IN-EMACS is non-nil, force opening in Emacs."
463 (interactive "P")
464 (let* ((attach-dir (org-attach-dir t))
465 (files (org-attach-file-list attach-dir))
466 (file (if (= (length files) 1)
467 (car files)
468 (org-icompleting-read "Open attachment: "
469 (mapcar 'list files) nil t))))
470 (org-open-file (expand-file-name file attach-dir) in-emacs)))
472 (defun org-attach-open-in-emacs ()
473 "Open attachment, force opening in Emacs.
474 See `org-attach-open'."
475 (interactive)
476 (org-attach-open 'in-emacs))
478 (defun org-attach-expand (file)
479 "Return the full path to the current entry's attachment file FILE.
480 Basically, this adds the path to the attachment directory."
481 (expand-file-name file (org-attach-dir)))
483 (defun org-attach-expand-link (file)
484 "Return a file link pointing to the current entry's attachment file FILE.
485 Basically, this adds the path to the attachment directory, and a \"file:\"
486 prefix."
487 (concat "file:" (org-attach-expand file)))
489 (defun org-attach-archive-delete-maybe ()
490 "Maybe delete subtree attachments when archiving.
491 This function is called by `org-archive-hook'. The option
492 `org-attach-archive-delete' controls its behavior."
493 (when (if (eq org-attach-archive-delete 'query)
494 (yes-or-no-p "Delete all attachments? ")
495 org-attach-archive-delete)
496 (org-attach-delete-all t)))
498 (add-hook 'org-archive-hook 'org-attach-archive-delete-maybe)
500 (provide 'org-attach)
502 ;; Local variables:
503 ;; generated-autoload-file: "org-loaddefs.el"
504 ;; End:
506 ;;; org-attach.el ends here