org-footnote: declare function for the compiler
[org-mode/org-tableheadings.git] / lisp / org-attach.el
blob1ff95be4701fad2c33d8162c2afabdd1796b690e
1 ;;; org-attach.el --- Manage file attachments to org-mode tasks
3 ;; Copyright (C) 2008, 2009, 2010 Free Software Foundation, Inc.
5 ;; Author: John Wiegley <johnw@newartisans.com>
6 ;; Keywords: org data task
7 ;; Version: 7.5
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 same directory as the org file itself. 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)
99 (defvar org-attach-inherited nil
100 "Indicates if the last access to the attachment directory was inherited.")
102 (defcustom org-attach-store-link-p nil
103 "Non-nil means store a link to a file when attaching it."
104 :group 'org-attach
105 :type '(choice
106 (const :tag "Don't store link" nil)
107 (const :tag "Link to origin location" t)
108 (const :tag "Link to the attach-dir location" 'attached)))
110 ;;;###autoload
111 (defun org-attach ()
112 "The dispatcher for attachment commands.
113 Shows a list of commands and prompts for another key to execute a command."
114 (interactive)
115 (let (c marker)
116 (when (eq major-mode 'org-agenda-mode)
117 (setq marker (or (get-text-property (point) 'org-hd-marker)
118 (get-text-property (point) 'org-marker)))
119 (unless marker
120 (error "No task in current line")))
121 (save-excursion
122 (when marker
123 (set-buffer (marker-buffer marker))
124 (goto-char marker))
125 (org-back-to-heading t)
126 (save-excursion
127 (save-window-excursion
128 (unless org-attach-expert
129 (with-output-to-temp-buffer "*Org Attach*"
130 (princ "Select an Attachment Command:
132 a Select a file and attach it to the task, using `org-attach-method'.
133 c/m/l Attach a file using copy/move/link method.
134 n Create a new attachment, as an Emacs buffer.
135 z Synchronize the current task with its attachment
136 directory, in case you added attachments yourself.
138 o Open current task's attachments.
139 O Like \"o\", but force opening in Emacs.
140 f Open current task's attachment directory.
141 F Like \"f\", but force using dired in Emacs.
143 d Delete one attachment, you will be prompted for a file name.
144 D Delete all of a task's attachments. A safer way is
145 to open the directory in dired and delete from there.
147 s Set a specific attachment directory for this entry.
148 i Make children of the current entry inherit its attachment directory.")))
149 (org-fit-window-to-buffer (get-buffer-window "*Org Attach*"))
150 (message "Select command: [acmlzoOfFdD]")
151 (setq c (read-char-exclusive))
152 (and (get-buffer "*Org Attach*") (kill-buffer "*Org Attach*"))))
153 (cond
154 ((memq c '(?a ?\C-a)) (call-interactively 'org-attach-attach))
155 ((memq c '(?c ?\C-c))
156 (let ((org-attach-method 'cp)) (call-interactively 'org-attach-attach)))
157 ((memq c '(?m ?\C-m))
158 (let ((org-attach-method 'mv)) (call-interactively 'org-attach-attach)))
159 ((memq c '(?l ?\C-l))
160 (let ((org-attach-method 'ln)) (call-interactively 'org-attach-attach)))
161 ((memq c '(?n ?\C-n)) (call-interactively 'org-attach-new))
162 ((memq c '(?z ?\C-z)) (call-interactively 'org-attach-sync))
163 ((memq c '(?o ?\C-o)) (call-interactively 'org-attach-open))
164 ((eq c ?O) (call-interactively 'org-attach-open-in-emacs))
165 ((memq c '(?f ?\C-f)) (call-interactively 'org-attach-reveal))
166 ((memq c '(?F)) (call-interactively 'org-attach-reveal-in-emacs))
167 ((memq c '(?d ?\C-d)) (call-interactively
168 'org-attach-delete-one))
169 ((eq c ?D) (call-interactively 'org-attach-delete-all))
170 ((eq c ?q) (message "Abort"))
171 ((memq c '(?s ?\C-s)) (call-interactively
172 'org-attach-set-directory))
173 ((memq c '(?i ?\C-i)) (call-interactively
174 'org-attach-set-inherit))
175 (t (error "No such attachment command %c" c))))))
177 (defun org-attach-dir (&optional create-if-not-exists-p)
178 "Return the directory associated with the current entry.
179 This first checks for a local property ATTACH_DIR, and then for an inherited
180 property ATTACH_DIR_INHERIT. If neither exists, the default mechanism
181 using the entry ID will be invoked to access the unique directory for the
182 current entry.
183 If the directory does not exist and CREATE-IF-NOT-EXISTS-P is non-nil,
184 the directory and (if necessary) the corresponding ID will be created."
185 (let (attach-dir uuid inherit)
186 (setq org-attach-inherited (org-entry-get nil "ATTACH_DIR_INHERIT"))
187 (cond
188 ((setq attach-dir (org-entry-get nil "ATTACH_DIR"))
189 (org-attach-check-absolute-path attach-dir))
190 ((and org-attach-allow-inheritance
191 (setq inherit (org-entry-get nil "ATTACH_DIR_INHERIT" t)))
192 (setq attach-dir
193 (save-excursion
194 (save-restriction
195 (widen)
196 (goto-char org-entry-property-inherited-from)
197 (let (org-attach-allow-inheritance)
198 (org-attach-dir create-if-not-exists-p)))))
199 (org-attach-check-absolute-path attach-dir)
200 (setq org-attach-inherited t))
201 (t ; use the ID
202 (org-attach-check-absolute-path nil)
203 (setq uuid (org-id-get (point) create-if-not-exists-p))
204 (when (or uuid create-if-not-exists-p)
205 (unless uuid (error "ID retrieval/creation failed"))
206 (setq attach-dir (expand-file-name
207 (format "%s/%s"
208 (substring uuid 0 2)
209 (substring uuid 2))
210 (expand-file-name org-attach-directory))))))
211 (when attach-dir
212 (if (and create-if-not-exists-p
213 (not (file-directory-p attach-dir)))
214 (make-directory attach-dir t))
215 (and (file-exists-p attach-dir)
216 attach-dir))))
218 (defun org-attach-check-absolute-path (dir)
219 "Check if we have enough information to root the attachment directory.
220 When DIR is given, check also if it is already absolute. Otherwise,
221 assume that it will be relative, and check if `org-attach-directory' is
222 absolute, or if at least the current buffer has a file name.
223 Throw an error if we cannot root the directory."
224 (or (and dir (file-name-absolute-p dir))
225 (file-name-absolute-p org-attach-directory)
226 (buffer-file-name (buffer-base-buffer))
227 (error "Need absolute `org-attach-directory' to attach in buffers without filename")))
229 (defun org-attach-set-directory ()
230 "Set the ATTACH_DIR property of the current entry.
231 The property defines the directory that is used for attachments
232 of the entry."
233 (interactive)
234 (let ((dir (org-entry-get nil "ATTACH_DIR")))
235 (setq dir (read-directory-name "Attachment directory: " dir))
236 (org-entry-put nil "ATTACH_DIR" dir)))
238 (defun org-attach-set-inherit ()
239 "Set the ATTACH_DIR_INHERIT property of the current entry.
240 The property defines the directory that is used for attachments
241 of the entry and any children that do not explicitly define (by setting
242 the ATTACH_DIR property) their own attachment directory."
243 (interactive)
244 (org-entry-put nil "ATTACH_DIR_INHERIT" "t")
245 (message "Children will inherit attachment directory"))
247 (defun org-attach-commit ()
248 "Commit changes to git if `org-attach-directory' is properly initialized.
249 This checks for the existence of a \".git\" directory in that directory."
250 (let ((dir (expand-file-name org-attach-directory)))
251 (when (file-exists-p (expand-file-name ".git" dir))
252 (with-temp-buffer
253 (cd dir)
254 (shell-command "git add .")
255 (shell-command "git ls-files --deleted" t)
256 (mapc #'(lambda (file)
257 (unless (string= file "")
258 (shell-command
259 (concat "git rm \"" file "\""))))
260 (split-string (buffer-string) "\n"))
261 (shell-command "git commit -m 'Synchronized attachments'")))))
263 (defun org-attach-tag (&optional off)
264 "Turn the autotag on or (if OFF is set) off."
265 (when org-attach-auto-tag
266 (save-excursion
267 (org-back-to-heading t)
268 (org-toggle-tag org-attach-auto-tag (if off 'off 'on)))))
270 (defun org-attach-untag ()
271 "Turn the autotag off."
272 (org-attach-tag 'off))
274 (defun org-attach-store-link (file)
275 "Add a link to `org-stored-link' when attaching a file.
276 Only do this when `org-attach-store-link-p' is non-nil."
277 (setq org-stored-links
278 (cons (list (org-attach-expand-link file)
279 (file-name-nondirectory file))
280 org-stored-links)))
282 (defun org-attach-attach (file &optional visit-dir method)
283 "Move/copy/link FILE into the attachment directory of the current task.
284 If VISIT-DIR is non-nil, visit the directory with dired.
285 METHOD may be `cp', `mv', or `ln', default taken from `org-attach-method'."
286 (interactive "fFile to keep as an attachment: \nP")
287 (setq method (or method org-attach-method))
288 (let ((basename (file-name-nondirectory file)))
289 (when (and org-attach-file-list-property (not org-attach-inherited))
290 (org-entry-add-to-multivalued-property
291 (point) org-attach-file-list-property basename))
292 (let* ((attach-dir (org-attach-dir t))
293 (fname (expand-file-name basename attach-dir)))
294 (cond
295 ((eq method 'mv) (rename-file file fname))
296 ((eq method 'cp) (copy-file file fname))
297 ((eq method 'ln) (add-name-to-file file fname)))
298 (org-attach-commit)
299 (org-attach-tag)
300 (cond ((eq org-attach-store-link-p 'attached)
301 (org-attach-store-link fname))
302 ((eq org-attach-store-link-p t)
303 (org-attach-store-link file)))
304 (if visit-dir
305 (dired attach-dir)
306 (message "File \"%s\" is now a task attachment." basename)))))
308 (defun org-attach-attach-cp ()
309 "Attach a file by copying it."
310 (interactive)
311 (let ((org-attach-method 'cp)) (call-interactively 'org-attach-attach)))
312 (defun org-attach-attach-mv ()
313 "Attach a file by moving (renaming) it."
314 (interactive)
315 (let ((org-attach-method 'mv)) (call-interactively 'org-attach-attach)))
316 (defun org-attach-attach-ln ()
317 "Attach a file by creating a hard link to it.
318 Beware that this does not work on systems that do not support hard links.
319 On some systems, this apparently does copy the file instead."
320 (interactive)
321 (let ((org-attach-method 'ln)) (call-interactively 'org-attach-attach)))
323 (defun org-attach-new (file)
324 "Create a new attachment FILE for the current task.
325 The attachment is created as an Emacs buffer."
326 (interactive "sCreate attachment named: ")
327 (when (and org-attach-file-list-property (not org-attach-inherited))
328 (org-entry-add-to-multivalued-property
329 (point) org-attach-file-list-property file))
330 (let ((attach-dir (org-attach-dir t)))
331 (org-attach-tag)
332 (find-file (expand-file-name file attach-dir))
333 (message "New attachment %s" file)))
335 (defun org-attach-delete-one (&optional file)
336 "Delete a single attachment."
337 (interactive)
338 (let* ((attach-dir (org-attach-dir t))
339 (files (org-attach-file-list attach-dir))
340 (file (or file
341 (org-icompleting-read
342 "Delete attachment: "
343 (mapcar (lambda (f)
344 (list (file-name-nondirectory f)))
345 files)))))
346 (setq file (expand-file-name file attach-dir))
347 (unless (file-exists-p file)
348 (error "No such attachment: %s" file))
349 (delete-file file)
350 (org-attach-commit)))
352 (defun org-attach-delete-all (&optional force)
353 "Delete all attachments from the current task.
354 This actually deletes the entire attachment directory.
355 A safer way is to open the directory in dired and delete from there."
356 (interactive "P")
357 (when (and org-attach-file-list-property (not org-attach-inherited))
358 (org-entry-delete (point) org-attach-file-list-property))
359 (let ((attach-dir (org-attach-dir)))
360 (when
361 (and attach-dir
362 (or force
363 (y-or-n-p "Are you sure you want to remove all attachments of this entry? ")))
364 (shell-command (format "rm -fr %s" attach-dir))
365 (message "Attachment directory removed")
366 (org-attach-commit)
367 (org-attach-untag))))
369 (defun org-attach-sync ()
370 "Synchronize the current tasks with its attachments.
371 This can be used after files have been added externally."
372 (interactive)
373 (org-attach-commit)
374 (when (and org-attach-file-list-property (not org-attach-inherited))
375 (org-entry-delete (point) org-attach-file-list-property))
376 (let ((attach-dir (org-attach-dir)))
377 (when attach-dir
378 (let ((files (org-attach-file-list attach-dir)))
379 (and files (org-attach-tag))
380 (when org-attach-file-list-property
381 (dolist (file files)
382 (unless (string-match "^\\." file)
383 (org-entry-add-to-multivalued-property
384 (point) org-attach-file-list-property file))))))))
386 (defun org-attach-file-list (dir)
387 "Return a list of files in the attachment directory.
388 This ignores files starting with a \".\", and files ending in \"~\"."
389 (delq nil
390 (mapcar (lambda (x) (if (string-match "^\\." x) nil x))
391 (directory-files dir nil "[^~]\\'"))))
393 (defun org-attach-reveal (&optional if-exists)
394 "Show the attachment directory of the current task in dired."
395 (interactive "P")
396 (let ((attach-dir (org-attach-dir (not if-exists))))
397 (and attach-dir (org-open-file attach-dir))))
399 (defun org-attach-reveal-in-emacs ()
400 "Show the attachment directory of the current task.
401 This will attempt to use an external program to show the directory."
402 (interactive)
403 (let ((attach-dir (org-attach-dir t)))
404 (dired attach-dir)))
406 (defun org-attach-open (&optional in-emacs)
407 "Open an attachment of the current task.
408 If there are more than one attachment, you will be prompted for the file name.
409 This command will open the file using the settings in `org-file-apps'
410 and in the system-specific variants of this variable.
411 If IN-EMACS is non-nil, force opening in Emacs."
412 (interactive "P")
413 (let* ((attach-dir (org-attach-dir t))
414 (files (org-attach-file-list attach-dir))
415 (file (if (= (length files) 1)
416 (car files)
417 (org-icompleting-read "Open attachment: "
418 (mapcar 'list files) nil t))))
419 (org-open-file (expand-file-name file attach-dir) in-emacs)))
421 (defun org-attach-open-in-emacs ()
422 "Open attachment, force opening in Emacs.
423 See `org-attach-open'."
424 (interactive)
425 (org-attach-open 'in-emacs))
427 (defun org-attach-expand (file)
428 "Return the full path to the current entry's attachment file FILE.
429 Basically, this adds the path to the attachment directory."
430 (expand-file-name file (org-attach-dir)))
432 (defun org-attach-expand-link (file)
433 "Return a file link pointing to the current entry's attachment file FILE.
434 Basically, this adds the path to the attachment directory, and a \"file:\"
435 prefix."
436 (concat "file:" (org-attach-expand file)))
438 (provide 'org-attach)
440 ;; arch-tag: fce93c2e-fe07-4fa3-a905-e10dcc7a6248
441 ;;; org-attach.el ends here