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