1 ;;; org-attach.el --- Manage file attachments to org-mode tasks
3 ;; Copyright (C) 2008-2012 Free Software Foundation, Inc.
5 ;; Author: John Wiegley <johnw@newartisans.com>
6 ;; Keywords: org data task
8 ;; This file is part of GNU Emacs.
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/>.
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
45 (defgroup org-attach nil
46 "Options concerning entry attachments in Org-mode."
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."
57 (defcustom org-attach-git-annex-cutoff
(* 32 1024)
58 "If non-nil, files larger than this will be annexed instead of stored."
61 (const :tag
"None" nil
)
62 (integer :tag
"Bytes")))
64 (defcustom org-attach-auto-tag
"ATTACH"
65 "Tag that will be triggered automatically when an entry has an attachment."
68 (const :tag
"None" nil
)
71 (defcustom org-attach-file-list-property
"Attachments"
72 "The property used to keep a list of attachment belonging to this entry.
73 This is not really needed, so you may set this to nil if you don't want it.
74 Also, for entries where children inherit the directory, the list of
75 attachments is not kept in this property."
78 (const :tag
"None" nil
)
81 (defcustom org-attach-method
'cp
82 "The preferred method to attach a file.
85 mv rename the file to move it into the attachment directory
87 ln create a hard link. Note that this is not supported
88 on all systems, and then the result is not defined.
89 lns create a symbol link. Note that this is not supported
90 on all systems, and then the result is not defined."
93 (const :tag
"Copy" cp
)
94 (const :tag
"Move/Rename" mv
)
95 (const :tag
"Hard Link" ln
)
96 (const :tag
"Symbol Link" lns
)))
98 (defcustom org-attach-expert nil
99 "Non-nil means do not show the splash buffer with the attach dispatcher."
103 (defcustom org-attach-allow-inheritance t
104 "Non-nil means allow attachment directories be inherited."
108 (defvar org-attach-inherited nil
109 "Indicates if the last access to the attachment directory was inherited.")
111 (defcustom org-attach-store-link-p nil
112 "Non-nil means store a link to a file when attaching it."
116 (const :tag
"Don't store link" nil
)
117 (const :tag
"Link to origin location" t
)
118 (const :tag
"Link to the attach-dir location" attached
)))
122 "The dispatcher for attachment commands.
123 Shows a list of commands and prompts for another key to execute a command."
126 (when (eq major-mode
'org-agenda-mode
)
127 (setq marker
(or (get-text-property (point) 'org-hd-marker
)
128 (get-text-property (point) 'org-marker
)))
130 (error "No task in current line")))
133 (set-buffer (marker-buffer marker
))
135 (org-back-to-heading t
)
137 (save-window-excursion
138 (unless org-attach-expert
139 (with-output-to-temp-buffer "*Org Attach*"
140 (princ "Select an Attachment Command:
142 a Select a file and attach it to the task, using `org-attach-method'.
143 c/m/l/y Attach a file using copy/move/link/symbolic-link method.
144 n Create a new attachment, as an Emacs buffer.
145 z Synchronize the current task with its attachment
146 directory, in case you added attachments yourself.
148 o Open current task's attachments.
149 O Like \"o\", but force opening in Emacs.
150 f Open current task's attachment directory.
151 F Like \"f\", but force using dired in Emacs.
153 d Delete one attachment, you will be prompted for a file name.
154 D Delete all of a task's attachments. A safer way is
155 to open the directory in dired and delete from there.
157 s Set a specific attachment directory for this entry.
158 i Make children of the current entry inherit its attachment directory.")))
159 (org-fit-window-to-buffer (get-buffer-window "*Org Attach*"))
160 (message "Select command: [acmlzoOfFdD]")
161 (setq c
(read-char-exclusive))
162 (and (get-buffer "*Org Attach*") (kill-buffer "*Org Attach*"))))
164 ((memq c
'(?a ?\C-a
)) (call-interactively 'org-attach-attach
))
165 ((memq c
'(?c ?\C-c
))
166 (let ((org-attach-method 'cp
)) (call-interactively 'org-attach-attach
)))
167 ((memq c
'(?m ?\C-m
))
168 (let ((org-attach-method 'mv
)) (call-interactively 'org-attach-attach
)))
169 ((memq c
'(?l ?\C-l
))
170 (let ((org-attach-method 'ln
)) (call-interactively 'org-attach-attach
)))
171 ((memq c
'(?y ?\C-y
))
172 (let ((org-attach-method 'lns
)) (call-interactively 'org-attach-attach
)))
173 ((memq c
'(?n ?\C-n
)) (call-interactively 'org-attach-new
))
174 ((memq c
'(?z ?\C-z
)) (call-interactively 'org-attach-sync
))
175 ((memq c
'(?o ?\C-o
)) (call-interactively 'org-attach-open
))
176 ((eq c ?O
) (call-interactively 'org-attach-open-in-emacs
))
177 ((memq c
'(?f ?\C-f
)) (call-interactively 'org-attach-reveal
))
178 ((memq c
'(?F
)) (call-interactively 'org-attach-reveal-in-emacs
))
179 ((memq c
'(?d ?\C-d
)) (call-interactively
180 'org-attach-delete-one
))
181 ((eq c ?D
) (call-interactively 'org-attach-delete-all
))
182 ((eq c ?q
) (message "Abort"))
183 ((memq c
'(?s ?\C-s
)) (call-interactively
184 'org-attach-set-directory
))
185 ((memq c
'(?i ?\C-i
)) (call-interactively
186 'org-attach-set-inherit
))
187 (t (error "No such attachment command %c" c
))))))
189 (defun org-attach-dir (&optional create-if-not-exists-p
)
190 "Return the directory associated with the current entry.
191 This first checks for a local property ATTACH_DIR, and then for an inherited
192 property ATTACH_DIR_INHERIT. If neither exists, the default mechanism
193 using the entry ID will be invoked to access the unique directory for the
195 If the directory does not exist and CREATE-IF-NOT-EXISTS-P is non-nil,
196 the directory and (if necessary) the corresponding ID will be created."
197 (let (attach-dir uuid inherit
)
198 (setq org-attach-inherited
(org-entry-get nil
"ATTACH_DIR_INHERIT"))
200 ((setq attach-dir
(org-entry-get nil
"ATTACH_DIR"))
201 (org-attach-check-absolute-path attach-dir
))
202 ((and org-attach-allow-inheritance
203 (setq inherit
(org-entry-get nil
"ATTACH_DIR_INHERIT" t
)))
208 (goto-char org-entry-property-inherited-from
)
209 (let (org-attach-allow-inheritance)
210 (org-attach-dir create-if-not-exists-p
)))))
211 (org-attach-check-absolute-path attach-dir
)
212 (setq org-attach-inherited t
))
214 (org-attach-check-absolute-path nil
)
215 (setq uuid
(org-id-get (point) create-if-not-exists-p
))
216 (when (or uuid create-if-not-exists-p
)
217 (unless uuid
(error "ID retrieval/creation failed"))
218 (setq attach-dir
(expand-file-name
222 (expand-file-name org-attach-directory
))))))
224 (if (and create-if-not-exists-p
225 (not (file-directory-p attach-dir
)))
226 (make-directory attach-dir t
))
227 (and (file-exists-p attach-dir
)
230 (defun org-attach-check-absolute-path (dir)
231 "Check if we have enough information to root the attachment directory.
232 When DIR is given, check also if it is already absolute. Otherwise,
233 assume that it will be relative, and check if `org-attach-directory' is
234 absolute, or if at least the current buffer has a file name.
235 Throw an error if we cannot root the directory."
236 (or (and dir
(file-name-absolute-p dir
))
237 (file-name-absolute-p org-attach-directory
)
238 (buffer-file-name (buffer-base-buffer))
239 (error "Need absolute `org-attach-directory' to attach in buffers without filename")))
241 (defun org-attach-set-directory ()
242 "Set the ATTACH_DIR property of the current entry.
243 The property defines the directory that is used for attachments
246 (let ((dir (org-entry-get nil
"ATTACH_DIR")))
247 (setq dir
(read-directory-name "Attachment directory: " dir
))
248 (org-entry-put nil
"ATTACH_DIR" dir
)))
250 (defun org-attach-set-inherit ()
251 "Set the ATTACH_DIR_INHERIT property of the current entry.
252 The property defines the directory that is used for attachments
253 of the entry and any children that do not explicitly define (by setting
254 the ATTACH_DIR property) their own attachment directory."
256 (org-entry-put nil
"ATTACH_DIR_INHERIT" "t")
257 (message "Children will inherit attachment directory"))
259 (defun org-attach-commit ()
260 "Commit changes to git if `org-attach-directory' is properly initialized.
261 This checks for the existence of a \".git\" directory in that directory."
262 (let ((dir (expand-file-name org-attach-directory
))
264 (when (file-exists-p (expand-file-name ".git" dir
))
268 (and org-attach-git-annex-cutoff
269 (file-exists-p (expand-file-name ".git/annex" dir
)))))
270 (dolist (new-or-modified
272 (shell-command-to-string
273 "git ls-files -zmo --exclude-standard") "\0" t
))
275 (>= (nth 7 (file-attributes new-or-modified
))
276 org-attach-git-annex-cutoff
))
277 (call-process "git" nil nil nil
"annex" "add" new-or-modified
)
278 (call-process "git" nil nil nil
"add" new-or-modified
))
282 (shell-command-to-string "git ls-files -z --deleted") "\0" t
))
283 (call-process "git" nil nil nil
"rm" deleted
)
286 (shell-command "git commit -m 'Synchronized attachments'"))))))
288 (defun org-attach-tag (&optional off
)
289 "Turn the autotag on or (if OFF is set) off."
290 (when org-attach-auto-tag
292 (org-back-to-heading t
)
293 (org-toggle-tag org-attach-auto-tag
(if off
'off
'on
)))))
295 (defun org-attach-untag ()
296 "Turn the autotag off."
297 (org-attach-tag 'off
))
299 (defun org-attach-store-link (file)
300 "Add a link to `org-stored-link' when attaching a file.
301 Only do this when `org-attach-store-link-p' is non-nil."
302 (setq org-stored-links
303 (cons (list (org-attach-expand-link file
)
304 (file-name-nondirectory file
))
307 (defun org-attach-attach (file &optional visit-dir method
)
308 "Move/copy/link FILE into the attachment directory of the current task.
309 If VISIT-DIR is non-nil, visit the directory with dired.
310 METHOD may be `cp', `mv', `ln', or `lns' default taken from
311 `org-attach-method'."
312 (interactive "fFile to keep as an attachment: \nP")
313 (setq method
(or method org-attach-method
))
314 (let ((basename (file-name-nondirectory file
)))
315 (when (and org-attach-file-list-property
(not org-attach-inherited
))
316 (org-entry-add-to-multivalued-property
317 (point) org-attach-file-list-property basename
))
318 (let* ((attach-dir (org-attach-dir t
))
319 (fname (expand-file-name basename attach-dir
)))
321 ((eq method
'mv
) (rename-file file fname
))
322 ((eq method
'cp
) (copy-file file fname
))
323 ((eq method
'ln
) (add-name-to-file file fname
))
324 ((eq method
'lns
) (make-symbolic-link file fname
)))
327 (cond ((eq org-attach-store-link-p
'attached
)
328 (org-attach-store-link fname
))
329 ((eq org-attach-store-link-p t
)
330 (org-attach-store-link file
)))
333 (message "File \"%s\" is now a task attachment." basename
)))))
335 (defun org-attach-attach-cp ()
336 "Attach a file by copying it."
338 (let ((org-attach-method 'cp
)) (call-interactively 'org-attach-attach
)))
339 (defun org-attach-attach-mv ()
340 "Attach a file by moving (renaming) it."
342 (let ((org-attach-method 'mv
)) (call-interactively 'org-attach-attach
)))
343 (defun org-attach-attach-ln ()
344 "Attach a file by creating a hard link to it.
345 Beware that this does not work on systems that do not support hard links.
346 On some systems, this apparently does copy the file instead."
348 (let ((org-attach-method 'ln
)) (call-interactively 'org-attach-attach
)))
349 (defun org-attach-attach-lns ()
350 "Attach a file by creating a symbolic link to it.
352 Beware that this does not work on systems that do not support symbolic links.
353 On some systems, this apparently does copy the file instead."
355 (let ((org-attach-method 'lns
)) (call-interactively 'org-attach-attach
)))
357 (defun org-attach-new (file)
358 "Create a new attachment FILE for the current task.
359 The attachment is created as an Emacs buffer."
360 (interactive "sCreate attachment named: ")
361 (when (and org-attach-file-list-property
(not org-attach-inherited
))
362 (org-entry-add-to-multivalued-property
363 (point) org-attach-file-list-property file
))
364 (let ((attach-dir (org-attach-dir t
)))
366 (find-file (expand-file-name file attach-dir
))
367 (message "New attachment %s" file
)))
369 (defun org-attach-delete-one (&optional file
)
370 "Delete a single attachment."
372 (let* ((attach-dir (org-attach-dir t
))
373 (files (org-attach-file-list attach-dir
))
375 (org-icompleting-read
376 "Delete attachment: "
378 (list (file-name-nondirectory f
)))
380 (setq file
(expand-file-name file attach-dir
))
381 (unless (file-exists-p file
)
382 (error "No such attachment: %s" file
))
384 (org-attach-commit)))
386 (defun org-attach-delete-all (&optional force
)
387 "Delete all attachments from the current task.
388 This actually deletes the entire attachment directory.
389 A safer way is to open the directory in dired and delete from there."
391 (when (and org-attach-file-list-property
(not org-attach-inherited
))
392 (org-entry-delete (point) org-attach-file-list-property
))
393 (let ((attach-dir (org-attach-dir)))
397 (y-or-n-p "Are you sure you want to remove all attachments of this entry? ")))
398 (shell-command (format "rm -fr %s" attach-dir
))
399 (message "Attachment directory removed")
401 (org-attach-untag))))
403 (defun org-attach-sync ()
404 "Synchronize the current tasks with its attachments.
405 This can be used after files have been added externally."
408 (when (and org-attach-file-list-property
(not org-attach-inherited
))
409 (org-entry-delete (point) org-attach-file-list-property
))
410 (let ((attach-dir (org-attach-dir)))
412 (let ((files (org-attach-file-list attach-dir
)))
413 (and files
(org-attach-tag))
414 (when org-attach-file-list-property
416 (unless (string-match "^\\." file
)
417 (org-entry-add-to-multivalued-property
418 (point) org-attach-file-list-property file
))))))))
420 (defun org-attach-file-list (dir)
421 "Return a list of files in the attachment directory.
422 This ignores files starting with a \".\", and files ending in \"~\"."
424 (mapcar (lambda (x) (if (string-match "^\\." x
) nil x
))
425 (directory-files dir nil
"[^~]\\'"))))
427 (defun org-attach-reveal (&optional if-exists
)
428 "Show the attachment directory of the current task in dired."
430 (let ((attach-dir (org-attach-dir (not if-exists
))))
431 (and attach-dir
(org-open-file attach-dir
))))
433 (defun org-attach-reveal-in-emacs ()
434 "Show the attachment directory of the current task.
435 This will attempt to use an external program to show the directory."
437 (let ((attach-dir (org-attach-dir t
)))
440 (defun org-attach-open (&optional in-emacs
)
441 "Open an attachment of the current task.
442 If there are more than one attachment, you will be prompted for the file name.
443 This command will open the file using the settings in `org-file-apps'
444 and in the system-specific variants of this variable.
445 If IN-EMACS is non-nil, force opening in Emacs."
447 (let* ((attach-dir (org-attach-dir t
))
448 (files (org-attach-file-list attach-dir
))
449 (file (if (= (length files
) 1)
451 (org-icompleting-read "Open attachment: "
452 (mapcar 'list files
) nil t
))))
453 (org-open-file (expand-file-name file attach-dir
) in-emacs
)))
455 (defun org-attach-open-in-emacs ()
456 "Open attachment, force opening in Emacs.
457 See `org-attach-open'."
459 (org-attach-open 'in-emacs
))
461 (defun org-attach-expand (file)
462 "Return the full path to the current entry's attachment file FILE.
463 Basically, this adds the path to the attachment directory."
464 (expand-file-name file
(org-attach-dir)))
466 (defun org-attach-expand-link (file)
467 "Return a file link pointing to the current entry's attachment file FILE.
468 Basically, this adds the path to the attachment directory, and a \"file:\"
470 (concat "file:" (org-attach-expand file
)))
472 (provide 'org-attach
)
475 ;; generated-autoload-file: "org-loaddefs.el"
478 ;;; org-attach.el ends here