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