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
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/>.
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
46 (defgroup org-attach nil
47 "Options concerning entry attachments in Org-mode."
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."
58 (defcustom org-attach-auto-tag
"ATTACH"
59 "Tag that will be triggered automatically when an entry has an attachment."
62 (const :tag
"None" nil
)
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."
72 (const :tag
"None" nil
)
75 (defcustom org-attach-method
'cp
76 "The preferred method to attach a file.
79 mv rename the file to move it into the attachment directory
81 ln create a hard link. Note that this is not supported
82 on all systems, and then the result is not defined."
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."
94 (defcustom org-attach-allow-inheritance t
95 "Non-nil means, allow attachment directories be inherited."
100 (defvar org-attach-inherited nil
101 "Indicates if the last access to the attachment directory was inherited.")
105 "The dispatcher for attachment commands.
106 Shows a list of commands and prompts for another key to execute a command."
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
)))
113 (error "No task in current line")))
116 (set-buffer (marker-buffer marker
))
118 (org-back-to-heading t
)
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*"))))
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
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"))
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
)))
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
))
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
203 (expand-file-name org-attach-directory
))))))
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
)
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
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."
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
))
246 (concat "(cd " dir
"; "
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
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
)))
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
)))
282 (message "File \"%s\" is now a task attachment." basename
)))))
284 (defun org-attach-attach-cp ()
285 "Attach a file by copying it."
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."
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."
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
)))
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."
314 (let* ((attach-dir (org-attach-dir t
))
315 (files (org-attach-file-list attach-dir
))
317 (org-icompleting-read
318 "Delete attachment: "
320 (list (file-name-nondirectory f
)))
322 (setq file
(expand-file-name file attach-dir
))
323 (unless (file-exists-p file
)
324 (error "No such attachment: %s" 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."
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)))
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")
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."
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)))
353 (let ((files (org-attach-file-list attach-dir
)))
354 (and files
(org-attach-tag))
355 (when org-attach-file-list-property
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 \"~\"."
365 (mapcar (lambda (x) (if (string-match "^\\." x
) nil x
))
366 (directory-files dir nil
"[^~]\\'"))))
368 (defun org-attach-reveal (&optional if-exists
)
369 "Show the attachment directory of the current task in dired."
371 (let ((attach-dir (org-attach-dir (not if-exists
))))
372 (and attach-dir
(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."
378 (let ((attach-dir (org-attach-dir t
)))
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."
388 (let* ((attach-dir (org-attach-dir t
))
389 (files (org-attach-file-list attach-dir
))
390 (file (if (= (length files
) 1)
392 (org-icompleting-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'."
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:\"
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