Update copyright years.
[org-mode.git] / lisp / org-attach.el
blob07307e68f6fda2cfb1a9401f12ca19c94c768c6f
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 ;;;###autoload
124 (defun org-attach ()
125 "The dispatcher for attachment commands.
126 Shows a list of commands and prompts for another key to execute a command."
127 (interactive)
128 (let (c marker)
129 (when (eq major-mode 'org-agenda-mode)
130 (setq marker (or (get-text-property (point) 'org-hd-marker)
131 (get-text-property (point) 'org-marker)))
132 (unless marker
133 (error "No task in current line")))
134 (save-excursion
135 (when marker
136 (set-buffer (marker-buffer marker))
137 (goto-char marker))
138 (org-back-to-heading t)
139 (save-excursion
140 (save-window-excursion
141 (unless org-attach-expert
142 (with-output-to-temp-buffer "*Org Attach*"
143 (princ "Select an Attachment Command:
145 a Select a file and attach it to the task, using `org-attach-method'.
146 c/m/l/y Attach a file using copy/move/link/symbolic-link method.
147 n Create a new attachment, as an Emacs buffer.
148 z Synchronize the current task with its attachment
149 directory, in case you added attachments yourself.
151 o Open current task's attachments.
152 O Like \"o\", but force opening in Emacs.
153 f Open current task's attachment directory.
154 F Like \"f\", but force using dired in Emacs.
156 d Delete one attachment, you will be prompted for a file name.
157 D Delete all of a task's attachments. A safer way is
158 to open the directory in dired and delete from there.
160 s Set a specific attachment directory for this entry.
161 i Make children of the current entry inherit its attachment directory.")))
162 (org-fit-window-to-buffer (get-buffer-window "*Org Attach*"))
163 (message "Select command: [acmlzoOfFdD]")
164 (setq c (read-char-exclusive))
165 (and (get-buffer "*Org Attach*") (kill-buffer "*Org Attach*"))))
166 (cond
167 ((memq c '(?a ?\C-a)) (call-interactively 'org-attach-attach))
168 ((memq c '(?c ?\C-c))
169 (let ((org-attach-method 'cp)) (call-interactively 'org-attach-attach)))
170 ((memq c '(?m ?\C-m))
171 (let ((org-attach-method 'mv)) (call-interactively 'org-attach-attach)))
172 ((memq c '(?l ?\C-l))
173 (let ((org-attach-method 'ln)) (call-interactively 'org-attach-attach)))
174 ((memq c '(?y ?\C-y))
175 (let ((org-attach-method 'lns)) (call-interactively 'org-attach-attach)))
176 ((memq c '(?n ?\C-n)) (call-interactively 'org-attach-new))
177 ((memq c '(?z ?\C-z)) (call-interactively 'org-attach-sync))
178 ((memq c '(?o ?\C-o)) (call-interactively 'org-attach-open))
179 ((eq c ?O) (call-interactively 'org-attach-open-in-emacs))
180 ((memq c '(?f ?\C-f)) (call-interactively 'org-attach-reveal))
181 ((memq c '(?F)) (call-interactively 'org-attach-reveal-in-emacs))
182 ((memq c '(?d ?\C-d)) (call-interactively
183 'org-attach-delete-one))
184 ((eq c ?D) (call-interactively 'org-attach-delete-all))
185 ((eq c ?q) (message "Abort"))
186 ((memq c '(?s ?\C-s)) (call-interactively
187 'org-attach-set-directory))
188 ((memq c '(?i ?\C-i)) (call-interactively
189 'org-attach-set-inherit))
190 (t (error "No such attachment command %c" c))))))
192 (defun org-attach-dir (&optional create-if-not-exists-p)
193 "Return the directory associated with the current entry.
194 This first checks for a local property ATTACH_DIR, and then for an inherited
195 property ATTACH_DIR_INHERIT. If neither exists, the default mechanism
196 using the entry ID will be invoked to access the unique directory for the
197 current entry.
198 If the directory does not exist and CREATE-IF-NOT-EXISTS-P is non-nil,
199 the directory and (if necessary) the corresponding ID will be created."
200 (let (attach-dir uuid inherit)
201 (setq org-attach-inherited (org-entry-get nil "ATTACH_DIR_INHERIT"))
202 (cond
203 ((setq attach-dir (org-entry-get nil "ATTACH_DIR"))
204 (org-attach-check-absolute-path attach-dir))
205 ((and org-attach-allow-inheritance
206 (setq inherit (org-entry-get nil "ATTACH_DIR_INHERIT" t)))
207 (setq attach-dir
208 (save-excursion
209 (save-restriction
210 (widen)
211 (goto-char org-entry-property-inherited-from)
212 (let (org-attach-allow-inheritance)
213 (org-attach-dir create-if-not-exists-p)))))
214 (org-attach-check-absolute-path attach-dir)
215 (setq org-attach-inherited t))
216 (t ; use the ID
217 (org-attach-check-absolute-path nil)
218 (setq uuid (org-id-get (point) create-if-not-exists-p))
219 (when (or uuid create-if-not-exists-p)
220 (unless uuid (error "ID retrieval/creation failed"))
221 (setq attach-dir (expand-file-name
222 (format "%s/%s"
223 (substring uuid 0 2)
224 (substring uuid 2))
225 (expand-file-name org-attach-directory))))))
226 (when attach-dir
227 (if (and create-if-not-exists-p
228 (not (file-directory-p attach-dir)))
229 (make-directory attach-dir t))
230 (and (file-exists-p attach-dir)
231 attach-dir))))
233 (defun org-attach-check-absolute-path (dir)
234 "Check if we have enough information to root the attachment directory.
235 When DIR is given, check also if it is already absolute. Otherwise,
236 assume that it will be relative, and check if `org-attach-directory' is
237 absolute, or if at least the current buffer has a file name.
238 Throw an error if we cannot root the directory."
239 (or (and dir (file-name-absolute-p dir))
240 (file-name-absolute-p org-attach-directory)
241 (buffer-file-name (buffer-base-buffer))
242 (error "Need absolute `org-attach-directory' to attach in buffers without filename")))
244 (defun org-attach-set-directory ()
245 "Set the ATTACH_DIR property of the current entry.
246 The property defines the directory that is used for attachments
247 of the entry."
248 (interactive)
249 (let ((dir (org-entry-get nil "ATTACH_DIR")))
250 (setq dir (read-directory-name "Attachment directory: " dir))
251 (org-entry-put nil "ATTACH_DIR" dir)))
253 (defun org-attach-set-inherit ()
254 "Set the ATTACH_DIR_INHERIT property of the current entry.
255 The property defines the directory that is used for attachments
256 of the entry and any children that do not explicitly define (by setting
257 the ATTACH_DIR property) their own attachment directory."
258 (interactive)
259 (org-entry-put nil "ATTACH_DIR_INHERIT" "t")
260 (message "Children will inherit attachment directory"))
262 (defun org-attach-commit ()
263 "Commit changes to git if `org-attach-directory' is properly initialized.
264 This checks for the existence of a \".git\" directory in that directory."
265 (let* ((dir (expand-file-name org-attach-directory))
266 (git-dir (vc-git-root dir))
267 (changes 0))
268 (when (and git-dir (executable-find "git"))
269 (with-temp-buffer
270 (cd dir)
271 (let ((have-annex
272 (and org-attach-git-annex-cutoff
273 (file-exists-p (expand-file-name "annex" git-dir)))))
274 (dolist (new-or-modified
275 (split-string
276 (shell-command-to-string
277 "git ls-files -zmo --exclude-standard") "\0" t))
278 (if (and have-annex
279 (>= (nth 7 (file-attributes new-or-modified))
280 org-attach-git-annex-cutoff))
281 (call-process "git" nil nil nil "annex" "add" new-or-modified)
282 (call-process "git" nil nil nil "add" new-or-modified))
283 (incf changes)))
284 (dolist (deleted
285 (split-string
286 (shell-command-to-string "git ls-files -z --deleted") "\0" t))
287 (call-process "git" nil nil nil "rm" deleted)
288 (incf changes))
289 (when (> changes 0)
290 (shell-command "git commit -m 'Synchronized attachments'"))))))
292 (defun org-attach-tag (&optional off)
293 "Turn the autotag on or (if OFF is set) off."
294 (when org-attach-auto-tag
295 (save-excursion
296 (org-back-to-heading t)
297 (org-toggle-tag org-attach-auto-tag (if off 'off 'on)))))
299 (defun org-attach-untag ()
300 "Turn the autotag off."
301 (org-attach-tag 'off))
303 (defun org-attach-store-link (file)
304 "Add a link to `org-stored-link' when attaching a file.
305 Only do this when `org-attach-store-link-p' is non-nil."
306 (setq org-stored-links
307 (cons (list (org-attach-expand-link file)
308 (file-name-nondirectory file))
309 org-stored-links)))
311 (defun org-attach-attach (file &optional visit-dir method)
312 "Move/copy/link FILE into the attachment directory of the current task.
313 If VISIT-DIR is non-nil, visit the directory with dired.
314 METHOD may be `cp', `mv', `ln', or `lns' default taken from
315 `org-attach-method'."
316 (interactive "fFile to keep as an attachment: \nP")
317 (setq method (or method org-attach-method))
318 (let ((basename (file-name-nondirectory file)))
319 (when (and org-attach-file-list-property (not org-attach-inherited))
320 (org-entry-add-to-multivalued-property
321 (point) org-attach-file-list-property basename))
322 (let* ((attach-dir (org-attach-dir t))
323 (fname (expand-file-name basename attach-dir)))
324 (cond
325 ((eq method 'mv) (rename-file file fname))
326 ((eq method 'cp) (copy-file file fname))
327 ((eq method 'ln) (add-name-to-file file fname))
328 ((eq method 'lns) (make-symbolic-link file fname)))
329 (org-attach-commit)
330 (org-attach-tag)
331 (cond ((eq org-attach-store-link-p 'attached)
332 (org-attach-store-link fname))
333 ((eq org-attach-store-link-p t)
334 (org-attach-store-link file)))
335 (if visit-dir
336 (dired attach-dir)
337 (message "File \"%s\" is now a task attachment." basename)))))
339 (defun org-attach-attach-cp ()
340 "Attach a file by copying it."
341 (interactive)
342 (let ((org-attach-method 'cp)) (call-interactively 'org-attach-attach)))
343 (defun org-attach-attach-mv ()
344 "Attach a file by moving (renaming) it."
345 (interactive)
346 (let ((org-attach-method 'mv)) (call-interactively 'org-attach-attach)))
347 (defun org-attach-attach-ln ()
348 "Attach a file by creating a hard link to it.
349 Beware that this does not work on systems that do not support hard links.
350 On some systems, this apparently does copy the file instead."
351 (interactive)
352 (let ((org-attach-method 'ln)) (call-interactively 'org-attach-attach)))
353 (defun org-attach-attach-lns ()
354 "Attach a file by creating a symbolic link to it.
356 Beware that this does not work on systems that do not support symbolic links.
357 On some systems, this apparently does copy the file instead."
358 (interactive)
359 (let ((org-attach-method 'lns)) (call-interactively 'org-attach-attach)))
361 (defun org-attach-new (file)
362 "Create a new attachment FILE for the current task.
363 The attachment is created as an Emacs buffer."
364 (interactive "sCreate attachment named: ")
365 (when (and org-attach-file-list-property (not org-attach-inherited))
366 (org-entry-add-to-multivalued-property
367 (point) org-attach-file-list-property file))
368 (let ((attach-dir (org-attach-dir t)))
369 (org-attach-tag)
370 (find-file (expand-file-name file attach-dir))
371 (message "New attachment %s" file)))
373 (defun org-attach-delete-one (&optional file)
374 "Delete a single attachment."
375 (interactive)
376 (let* ((attach-dir (org-attach-dir t))
377 (files (org-attach-file-list attach-dir))
378 (file (or file
379 (org-icompleting-read
380 "Delete attachment: "
381 (mapcar (lambda (f)
382 (list (file-name-nondirectory f)))
383 files)))))
384 (setq file (expand-file-name file attach-dir))
385 (unless (file-exists-p file)
386 (error "No such attachment: %s" file))
387 (delete-file file)
388 (org-attach-commit)))
390 (defun org-attach-delete-all (&optional force)
391 "Delete all attachments from the current task.
392 This actually deletes the entire attachment directory.
393 A safer way is to open the directory in dired and delete from there."
394 (interactive "P")
395 (when (and org-attach-file-list-property (not org-attach-inherited))
396 (org-entry-delete (point) org-attach-file-list-property))
397 (let ((attach-dir (org-attach-dir)))
398 (when
399 (and attach-dir
400 (or force
401 (y-or-n-p "Are you sure you want to remove all attachments of this entry? ")))
402 (shell-command (format "rm -fr %s" attach-dir))
403 (message "Attachment directory removed")
404 (org-attach-commit)
405 (org-attach-untag))))
407 (defun org-attach-sync ()
408 "Synchronize the current tasks with its attachments.
409 This can be used after files have been added externally."
410 (interactive)
411 (org-attach-commit)
412 (when (and org-attach-file-list-property (not org-attach-inherited))
413 (org-entry-delete (point) org-attach-file-list-property))
414 (let ((attach-dir (org-attach-dir)))
415 (when attach-dir
416 (let ((files (org-attach-file-list attach-dir)))
417 (and files (org-attach-tag))
418 (when org-attach-file-list-property
419 (dolist (file files)
420 (unless (string-match "^\\." file)
421 (org-entry-add-to-multivalued-property
422 (point) org-attach-file-list-property file))))))))
424 (defun org-attach-file-list (dir)
425 "Return a list of files in the attachment directory.
426 This ignores files starting with a \".\", and files ending in \"~\"."
427 (delq nil
428 (mapcar (lambda (x) (if (string-match "^\\." x) nil x))
429 (directory-files dir nil "[^~]\\'"))))
431 (defun org-attach-reveal (&optional if-exists)
432 "Show the attachment directory of the current task.
433 This will attempt to use an external program to show the directory."
434 (interactive "P")
435 (let ((attach-dir (org-attach-dir (not if-exists))))
436 (and attach-dir (org-open-file attach-dir))))
438 (defun org-attach-reveal-in-emacs ()
439 "Show the attachment directory of the current task in dired."
440 (interactive)
441 (let ((attach-dir (org-attach-dir t)))
442 (dired attach-dir)))
444 (defun org-attach-open (&optional in-emacs)
445 "Open an attachment of the current task.
446 If there are more than one attachment, you will be prompted for the file name.
447 This command will open the file using the settings in `org-file-apps'
448 and in the system-specific variants of this variable.
449 If IN-EMACS is non-nil, force opening in Emacs."
450 (interactive "P")
451 (let* ((attach-dir (org-attach-dir t))
452 (files (org-attach-file-list attach-dir))
453 (file (if (= (length files) 1)
454 (car files)
455 (org-icompleting-read "Open attachment: "
456 (mapcar 'list files) nil t))))
457 (org-open-file (expand-file-name file attach-dir) in-emacs)))
459 (defun org-attach-open-in-emacs ()
460 "Open attachment, force opening in Emacs.
461 See `org-attach-open'."
462 (interactive)
463 (org-attach-open 'in-emacs))
465 (defun org-attach-expand (file)
466 "Return the full path to the current entry's attachment file FILE.
467 Basically, this adds the path to the attachment directory."
468 (expand-file-name file (org-attach-dir)))
470 (defun org-attach-expand-link (file)
471 "Return a file link pointing to the current entry's attachment file FILE.
472 Basically, this adds the path to the attachment directory, and a \"file:\"
473 prefix."
474 (concat "file:" (org-attach-expand file)))
476 (provide 'org-attach)
478 ;; Local variables:
479 ;; generated-autoload-file: "org-loaddefs.el"
480 ;; End:
482 ;;; org-attach.el ends here