Release 6.21a
[org-mode/org-tableheadings.git] / lisp / org-attach.el
blob0b841d22c65e7d41abfff69f607d65e47936d544
1 ;;; org-attach.el --- Manage file attachments to org-mode tasks
3 ;; Copyright (C) 2008, 2009 Free Software Foundation, Inc.
5 ;; Author: John Wiegley <johnw@newartisans.com>
6 ;; Keywords: org data task
7 ;; Version: 6.21a
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; See the Org-mode manual for information on how to use it.
28 ;; Attachments are managed in a special directory called "data", which
29 ;; lives in the directory given by `org-directory'. If this data
30 ;; directory is initialized as a Git repository, then org-attach will
31 ;; automatically commit changes when it sees them.
33 ;; Attachment directories are identified using a UUID generated for the
34 ;; task which has the attachments. These are added as property to the
35 ;; task when necessary, and should not be deleted or changed by the
36 ;; user, ever. UUIDs are generated by a mechanism defined in the variable
37 ;; `org-id-method'.
39 ;;; Code:
41 (eval-when-compile
42 (require 'cl))
43 (require 'org-id)
44 (require 'org)
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-auto-tag "ATTACH"
59 "Tag that will be triggered automatically when an entry has an attachment."
60 :group 'org-attach
61 :type '(choice
62 (const :tag "None" nil)
63 (string :tag "Tag")))
65 (defcustom org-attach-file-list-property "Attachments"
66 "The property used to keep a list of attachment belonging to this entry.
67 This is not really needed, so you may set this to nil if you don't want it.
68 Also, for entries where children inherit the directory, the list of
69 attachments is not kept in this property."
70 :group 'org-attach
71 :type '(choice
72 (const :tag "None" nil)
73 (string :tag "Tag")))
75 (defcustom org-attach-method 'cp
76 "The preferred method to attach a file.
77 Allowed values are:
79 mv rename the file to move it into the attachment directory
80 cp copy the file
81 ln create a hard link. Note that this is not supported
82 on all systems, and then the result is not defined."
83 :group 'org-attach
84 :type '(choice
85 (const :tag "Copy" cp)
86 (const :tag "Move/Rename" mv)
87 (const :tag "Link" ln)))
89 (defcustom org-attach-expert nil
90 "Non-nil means do not show the splash buffer with the attach dispatcher."
91 :group 'org-attach
92 :type 'boolean)
94 (defcustom org-attach-allow-inheritance t
95 "Non-nil means, allow attachment directories be inherited."
96 :group 'org-attach
97 :type 'boolean)
100 (defvar org-attach-inherited nil
101 "Indicates if the last access to the attachment directory was inherited.")
103 ;;;###autoload
104 (defun org-attach ()
105 "The dispatcher for attachment commands.
106 Shows a list of commands and prompts for another key to execute a command."
107 (interactive)
108 (let (c marker)
109 (when (eq major-mode 'org-agenda-mode)
110 (setq marker (or (get-text-property (point) 'org-hd-marker)
111 (get-text-property (point) 'org-marker)))
112 (unless marker
113 (error "No task in current line")))
114 (save-excursion
115 (when marker
116 (set-buffer (marker-buffer marker))
117 (goto-char marker))
118 (org-back-to-heading t)
119 (save-excursion
120 (save-window-excursion
121 (unless org-attach-expert
122 (with-output-to-temp-buffer "*Org Attach*"
123 (princ "Select an Attachment Command:
125 a Select a file and attach it to the task, using `org-attach-method'.
126 c/m/l Attach a file using copy/move/link method.
127 n Create a new attachment, as an Emacs buffer.
128 z Synchronize the current task with its attachment
129 directory, in case you added attachments yourself.
131 o Open current task's attachments.
132 O Like \"o\", but force opening in Emacs.
133 f Open current task's attachment directory.
134 F Like \"f\", but force using dired in Emacs.
136 d Delete one attachment, you will be prompted for a file name.
137 D Delete all of a task's attachments. A safer way is
138 to open the directory in dired and delete from there.
140 s Set a specific attachment directory for this entry.
141 i Make children of the current entry inherit its attachment directory.")))
142 (org-fit-window-to-buffer (get-buffer-window "*Org Attach*"))
143 (message "Select command: [acmlzoOfFdD]")
144 (setq c (read-char-exclusive))
145 (and (get-buffer "*Org Attach*") (kill-buffer "*Org Attach*"))))
146 (cond
147 ((memq c '(?a ?\C-a)) (call-interactively 'org-attach-attach))
148 ((memq c '(?c ?\C-c))
149 (let ((org-attach-method 'cp)) (call-interactively 'org-attach-attach)))
150 ((memq c '(?m ?\C-m))
151 (let ((org-attach-method 'mv)) (call-interactively 'org-attach-attach)))
152 ((memq c '(?l ?\C-l))
153 (let ((org-attach-method 'ln)) (call-interactively 'org-attach-attach)))
154 ((memq c '(?n ?\C-n)) (call-interactively 'org-attach-new))
155 ((memq c '(?z ?\C-z)) (call-interactively 'org-attach-sync))
156 ((memq c '(?o ?\C-o)) (call-interactively 'org-attach-open))
157 ((eq c ?O) (call-interactively 'org-attach-open-in-emacs))
158 ((memq c '(?f ?\C-f)) (call-interactively 'org-attach-reveal))
159 ((memq c '(?F)) (call-interactively 'org-attach-reveal-in-emacs))
160 ((memq c '(?d ?\C-d)) (call-interactively
161 'org-attach-delete-one))
162 ((eq c ?D) (call-interactively 'org-attach-delete-all))
163 ((eq c ?q) (message "Abort"))
164 ((memq c '(?s ?\C-s)) (call-interactively
165 'org-attach-set-directory))
166 ((memq c '(?i ?\C-i)) (call-interactively
167 'org-attach-set-inherit))
168 (t (error "No such attachment command %c" c))))))
170 (defun org-attach-dir (&optional create-if-not-exists-p)
171 "Return the directory associated with the current entry.
172 This first checks for a local property ATTACH_DIR, and then for an inherited
173 property ATTACH_DIR_INHERIT. If neither exists, the default mechanism
174 using the entry ID will be invoked to access the unique directory for the
175 current entry.
176 If the directory does not exist and CREATE-IF-NOT-EXISTS-P is non-nil,
177 the directory and (if necessary) the corresponding ID will be created."
178 (let (attach-dir uuid inherit)
179 (setq org-attach-inherited (org-entry-get nil "ATTACH_DIR_INHERIT"))
180 (cond
181 ((setq attach-dir (org-entry-get nil "ATTACH_DIR"))
182 (org-attach-check-absolute-path attach-dir))
183 ((and org-attach-allow-inheritance
184 (setq inherit (org-entry-get nil "ATTACH_DIR_INHERIT" t)))
185 (setq attach-dir
186 (save-excursion
187 (save-restriction
188 (widen)
189 (goto-char org-entry-property-inherited-from)
190 (let (org-attach-allow-inheritance)
191 (org-attach-dir create-if-not-exists-p)))))
192 (org-attach-check-absolute-path attach-dir)
193 (setq org-attach-inherited t))
194 (t ; use the ID
195 (org-attach-check-absolute-path nil)
196 (setq uuid (org-id-get (point) create-if-not-exists-p))
197 (when (or uuid create-if-not-exists-p)
198 (unless uuid (error "ID retrieval/creation failed"))
199 (setq attach-dir (expand-file-name
200 (format "%s/%s"
201 (substring uuid 0 2)
202 (substring uuid 2))
203 (expand-file-name org-attach-directory))))))
204 (when attach-dir
205 (if (and create-if-not-exists-p
206 (not (file-directory-p attach-dir)))
207 (make-directory attach-dir t))
208 (and (file-exists-p attach-dir)
209 attach-dir))))
211 (defun org-attach-check-absolute-path (dir)
212 "Check if we have enough information to root the atachment directory.
213 When DIR is given, check also if it is already absolute. Otherwise,
214 assume that it will be relative, and check if `org-attach-directory' is
215 absolute, or if at least the current buffer has a file name.
216 Throw an error if we cannot root the directory."
217 (or (and dir (file-name-absolute-p dir))
218 (file-name-absolute-p org-attach-directory)
219 (buffer-file-name (buffer-base-buffer))
220 (error "Need absolute `org-attach-directory' to attach in buffers without filename.")))
222 (defun org-attach-set-directory ()
223 "Set the ATTACH_DIR property of the current entry.
224 The property defines the directory that is used for attachments
225 of the entry."
226 (interactive)
227 (let ((dir (org-entry-get nil "ATTACH_DIR")))
228 (setq dir (read-directory-name "Attachment directory: " dir))
229 (org-entry-put nil "ATTACH_DIR" dir)))
231 (defun org-attach-set-inherit ()
232 "Set the ATTACH_DIR_INHERIT property of the current entry.
233 The property defines the directory that is used for attachments
234 of the entry and any children that do not explicitly define (by setting
235 the ATTACH_DIR property) their own attachment directory."
236 (interactive)
237 (org-entry-put nil "ATTACH_DIR_INHERIT" "t")
238 (message "Children will inherit attachment directory"))
240 (defun org-attach-commit ()
241 "Commit changes to git if `org-attach-directory' is properly initialized.
242 This checks for the existence of a \".git\" directory in that directory."
243 (let ((dir (expand-file-name org-attach-directory)))
244 (if (file-exists-p (expand-file-name ".git" dir))
245 (shell-command
246 (concat "(cd " dir "; "
247 " git add .; "
248 " git ls-files --deleted -z | xargs -0 git rm; "
249 " git commit -m 'Synchronized attachments')")))))
251 (defun org-attach-tag (&optional off)
252 "Turn the autotag on or (if OFF is set) off."
253 (when org-attach-auto-tag
254 (save-excursion
255 (org-back-to-heading t)
256 (org-toggle-tag org-attach-auto-tag (if off 'off 'on)))))
258 (defun org-attach-untag ()
259 "Turn the autotag off."
260 (org-attach-tag 'off))
262 (defun org-attach-attach (file &optional visit-dir method)
263 "Move/copy/link FILE into the attachment directory of the current task.
264 If VISIT-DIR is non-nil, visit the directory with dired.
265 METHOD may be `cp', `mv', or `ln', default taken from `org-attach-method'."
266 (interactive "fFile to keep as an attachment: \nP")
267 (setq method (or method org-attach-method))
268 (let ((basename (file-name-nondirectory file)))
269 (when (and org-attach-file-list-property (not org-attach-inherited))
270 (org-entry-add-to-multivalued-property
271 (point) org-attach-file-list-property basename))
272 (let* ((attach-dir (org-attach-dir t))
273 (fname (expand-file-name basename attach-dir)))
274 (cond
275 ((eq method 'mv) (rename-file file fname))
276 ((eq method 'cp) (copy-file file fname))
277 ((eq method 'ln) (add-name-to-file file fname)))
278 (org-attach-commit)
279 (org-attach-tag)
280 (if visit-dir
281 (dired attach-dir)
282 (message "File \"%s\" is now a task attachment." basename)))))
284 (defun org-attach-attach-cp ()
285 "Attach a file by copying it."
286 (interactive)
287 (let ((org-attach-method 'cp)) (call-interactively 'org-attach-attach)))
288 (defun org-attach-attach-mv ()
289 "Attach a file by moving (renaming) it."
290 (interactive)
291 (let ((org-attach-method 'mv)) (call-interactively 'org-attach-attach)))
292 (defun org-attach-attach-ln ()
293 "Attach a file by creating a hard link to it.
294 Beware that this does not work on systems that do not support hard links.
295 On some systems, this apparently does copy the file instead."
296 (interactive)
297 (let ((org-attach-method 'ln)) (call-interactively 'org-attach-attach)))
299 (defun org-attach-new (file)
300 "Create a new attachment FILE for the current task.
301 The attachment is created as an Emacs buffer."
302 (interactive "sCreate attachment named: ")
303 (when (and org-attach-file-list-property (not org-attach-inherited))
304 (org-entry-add-to-multivalued-property
305 (point) org-attach-file-list-property file))
306 (let ((attach-dir (org-attach-dir t)))
307 (org-attach-tag)
308 (find-file (expand-file-name file attach-dir))
309 (message "New attachment %s" file)))
311 (defun org-attach-delete-one (&optional file)
312 "Delete a single attachment."
313 (interactive)
314 (let* ((attach-dir (org-attach-dir t))
315 (files (org-attach-file-list attach-dir))
316 (file (or file
317 (org-ido-completing-read
318 "Delete attachment: "
319 (mapcar (lambda (f)
320 (list (file-name-nondirectory f)))
321 files)))))
322 (setq file (expand-file-name file attach-dir))
323 (unless (file-exists-p file)
324 (error "No such attachment: %s" file))
325 (delete-file file)))
327 (defun org-attach-delete-all (&optional force)
328 "Delete all attachments from the current task.
329 This actually deletes the entire attachment directory.
330 A safer way is to open the directory in dired and delete from there."
331 (interactive "P")
332 (when (and org-attach-file-list-property (not org-attach-inherited))
333 (org-entry-delete (point) org-attach-file-list-property))
334 (let ((attach-dir (org-attach-dir)))
335 (when
336 (and attach-dir
337 (or force
338 (y-or-n-p "Are you sure you want to remove all attachments of this entry? ")))
339 (shell-command (format "rm -fr %s" attach-dir))
340 (message "Attachment directory removed")
341 (org-attach-commit)
342 (org-attach-untag))))
344 (defun org-attach-sync ()
345 "Synchronize the current tasks with its attachments.
346 This can be used after files have been added externally."
347 (interactive)
348 (org-attach-commit)
349 (when (and org-attach-file-list-property (not org-attach-inherited))
350 (org-entry-delete (point) org-attach-file-list-property))
351 (let ((attach-dir (org-attach-dir)))
352 (when attach-dir
353 (let ((files (org-attach-file-list attach-dir)))
354 (and files (org-attach-tag))
355 (when org-attach-file-list-property
356 (dolist (file files)
357 (unless (string-match "^\\." file)
358 (org-entry-add-to-multivalued-property
359 (point) org-attach-file-list-property file))))))))
361 (defun org-attach-file-list (dir)
362 "Return a list of files in the attachment directory.
363 This ignores files starting with a \".\", and files ending in \"~\"."
364 (delq nil
365 (mapcar (lambda (x) (if (string-match "^\\." x) nil x))
366 (directory-files dir nil "[^~]\\'"))))
368 (defun org-attach-reveal ()
369 "Show the attachment directory of the current task in dired."
370 (interactive)
371 (let ((attach-dir (org-attach-dir t)))
372 (org-open-file attach-dir)))
374 (defun org-attach-reveal-in-emacs ()
375 "Show the attachment directory of the current task.
376 This will attempt to use an external program to show the directory."
377 (interactive)
378 (let ((attach-dir (org-attach-dir t)))
379 (dired attach-dir)))
381 (defun org-attach-open (&optional in-emacs)
382 "Open an attachment of the current task.
383 If there are more than one attachment, you will be prompted for the file name.
384 This command will open the file using the settings in `org-file-apps'
385 and in the system-specific variants of this variable.
386 If IN-EMACS is non-nil, force opening in Emacs."
387 (interactive "P")
388 (let* ((attach-dir (org-attach-dir t))
389 (files (org-attach-file-list attach-dir))
390 (file (if (= (length files) 1)
391 (car files)
392 (org-ido-completing-read "Open attachment: "
393 (mapcar 'list files) nil t))))
394 (org-open-file (expand-file-name file attach-dir) in-emacs)))
396 (defun org-attach-open-in-emacs ()
397 "Open attachment, force opening in Emacs.
398 See `org-attach-open'."
399 (interactive)
400 (org-attach-open 'in-emacs))
402 (defun org-attach-expand (file)
403 "Return the full path to the current entry's attachment file FILE.
404 Basically, this adds the path to the attachment directory."
405 (expand-file-name file (org-attach-dir)))
407 (defun org-attach-expand-link (file)
408 "Return a file link pointing to the current entry's attachment file FILE.
409 Basically, this adds the path to the attachment directory, and a \"file:\"
410 prefix."
411 (concat "file:" (org-attach-expand file)))
413 (provide 'org-attach)
415 ;; arch-tag: fce93c2e-fe07-4fa3-a905-e10dcc7a6248
416 ;;; org-attach.el ends here