1 ;;; org-attach.el --- Manage file attachments to org-mode tasks
3 ;; Copyright (C) 2008 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 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
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."
70 (const :tag
"None" nil
)
73 (defcustom org-attach-method
'cp
74 "The preferred method to attach a file.
77 mv rename the file to move it into the attachment directory
79 ln create a hard link. Note that this is not supported
80 on all systems, and then the result is not defined."
83 (const :tag
"Copy" cp
)
84 (const :tag
"Move/Rename" mv
)
85 (const :tag
"Link" ln
)))
87 (defcustom org-attach-expert nil
88 "Non-nil means do not show the splash buffer with the attach dispatcher."
94 "The dispatcher for attachment commands.
95 Shows a list of commands and prompts for another key to execute a command."
98 (when (eq major-mode
'org-agenda-mode
)
99 (setq marker
(or (get-text-property (point) 'org-hd-marker
)
100 (get-text-property (point) 'org-marker
)))
102 (error "No task in current line")))
105 (set-buffer (marker-buffer marker
))
107 (org-back-to-heading t
)
109 (save-window-excursion
110 (unless org-attach-expert
111 (with-output-to-temp-buffer "*Org Attach*"
112 (princ "Select an Attachment Command:
114 a Select a file and attach it to the task, using `org-attach-method'.
115 c/m/l Attach a file using copy/move/link method.
116 n Create a new attachment, as an Emacs buffer.
117 z Synchronize the current task with its attachment
118 directory, in case you added attachments yourself.
120 o Open current task's attachments.
121 O Like \"o\", but force opening in Emacs.
122 f Open current task's attachment directory.
123 F Like \"f\", but force using dired in Emacs.
125 d Delete one attachment, you will be prompted for a file name.
126 D Delete all of a task's attachments. A safer way is
127 to open the directory in dired and delete from there.")))
128 (org-fit-window-to-buffer (get-buffer-window "*Org Attach*"))
129 (message "Select command: [acmlzoOfFdD]")
130 (setq c
(read-char-exclusive))
131 (and (get-buffer "*Org Attach*") (kill-buffer "*Org Attach*"))))
133 ((memq c
'(?a ?\C-a
)) (call-interactively 'org-attach-attach
))
134 ((memq c
'(?c ?\C-c
))
135 (let ((org-attach-method 'cp
)) (call-interactively 'org-attach-attach
)))
136 ((memq c
'(?m ?\C-m
))
137 (let ((org-attach-method 'mv
)) (call-interactively 'org-attach-attach
)))
138 ((memq c
'(?l ?\C-l
))
139 (let ((org-attach-method 'ln
)) (call-interactively 'org-attach-attach
)))
140 ((memq c
'(?n ?\C-n
)) (call-interactively 'org-attach-new
))
141 ((memq c
'(?z ?\C-z
)) (call-interactively 'org-attach-sync
))
142 ((memq c
'(?o ?\C-o
)) (call-interactively 'org-attach-open
))
143 ((eq c ?O
) (call-interactively 'org-attach-open-in-emacs
))
144 ((memq c
'(?f ?\C-f
)) (call-interactively 'org-attach-reveal
))
145 ((memq c
'(?F
)) (call-interactively 'org-attach-reveal-in-emacs
))
146 ((memq c
'(?d ?\C-d
)) (call-interactively
147 'org-attach-delete-one
))
148 ((eq c ?D
) (call-interactively 'org-attach-delete-all
))
149 ((eq c ?q
) (message "Abort"))
150 (t (error "No such attachment command %c" c
))))))
152 (defun org-attach-dir (&optional create-if-not-exists-p
)
153 "Return the directory associated with the current entry.
154 If the directory does not exist and CREATE-IF-NOT-EXISTS-P is non-nil,
155 the directory and the corresponding ID will be created."
156 (when (and (not (buffer-file-name (buffer-base-buffer)))
157 (not (file-name-absolute-p org-attach-directory
)))
158 (error "Need absolute `org-attach-directory' to attach in buffers without filename."))
159 (let ((uuid (org-id-get (point) create-if-not-exists-p
)))
160 (when (or uuid create-if-not-exists-p
)
162 (error "ID retrieval/creation failed"))
163 (let ((attach-dir (expand-file-name
167 (expand-file-name org-attach-directory
))))
168 (if (and create-if-not-exists-p
169 (not (file-directory-p attach-dir
)))
170 (make-directory attach-dir t
))
171 (and (file-exists-p attach-dir
)
174 (defun org-attach-commit ()
175 "Commit changes to git if `org-attach-directory' is properly initialized.
176 This checks for the existence of a \".git\" directory in that directory."
177 (let ((dir (expand-file-name org-attach-directory
)))
178 (if (file-exists-p (expand-file-name ".git" dir
))
180 (concat "(cd " dir
"; "
182 " git ls-files --deleted -z | xargs -0 git rm; "
183 " git commit -m 'Synchronized attachments')")))))
185 (defun org-attach-tag (&optional off
)
186 "Turn the autotag on or (if OFF is set) off."
187 (when org-attach-auto-tag
189 (org-back-to-heading t
)
190 (org-toggle-tag org-attach-auto-tag
(if off
'off
'on
)))))
192 (defun org-attach-untag ()
193 "Turn the autotag off."
194 (org-attach-tag 'off
))
196 (defun org-attach-attach (file &optional visit-dir method
)
197 "Move/copy/link FILE into the attachment directory of the current task.
198 If VISIT-DIR is non-nil, visit the directory with dired.
199 METHOD may be `cp', `mv', or `ln', default taken from `org-attach-method'."
200 (interactive "fFile to keep as an attachment: \nP")
201 (setq method
(or method org-attach-method
))
202 (let ((basename (file-name-nondirectory file
)))
203 (when org-attach-file-list-property
204 (org-entry-add-to-multivalued-property
205 (point) org-attach-file-list-property basename
))
206 (let* ((attach-dir (org-attach-dir t
))
207 (fname (expand-file-name basename attach-dir
)))
209 ((eq method
'mv
) (rename-file file fname
))
210 ((eq method
'cp
) (copy-file file fname
))
211 ((eq method
'ln
) (add-name-to-file file fname
)))
216 (message "File \"%s\" is now a task attachment." basename
)))))
218 (defun org-attach-attach-cp ()
219 "Attach a file by copying it."
221 (let ((org-attach-method 'cp
)) (call-interactively 'org-attach-attach
)))
222 (defun org-attach-attach-mv ()
223 "Attach a file by moving (renaming) it."
225 (let ((org-attach-method 'mv
)) (call-interactively 'org-attach-attach
)))
226 (defun org-attach-attach-ln ()
227 "Attach a file by creating a hard link to it.
228 Beware that this does not work on systems that do not support hard links.
229 On some systems, this apparently does copy the file instead."
231 (let ((org-attach-method 'ln
)) (call-interactively 'org-attach-attach
)))
233 (defun org-attach-new (file)
234 "Create a new attachment FILE for the current task.
235 The attachment is created as an Emacs buffer."
236 (interactive "sCreate attachment named: ")
237 (when org-attach-file-list-property
238 (org-entry-add-to-multivalued-property
239 (point) org-attach-file-list-property file
))
240 (let ((attach-dir (org-attach-dir t
)))
242 (find-file (expand-file-name file attach-dir
))
243 (message "New attachment %s" file
)))
245 (defun org-attach-delete-one (&optional file
)
246 "Delete a single attachment."
248 (let* ((attach-dir (org-attach-dir t
))
249 (files (org-attach-file-list attach-dir
))
251 (org-ido-completing-read
252 "Delete attachment: "
254 (list (file-name-nondirectory f
)))
256 (setq file
(expand-file-name file attach-dir
))
257 (unless (file-exists-p file
)
258 (error "No such attachment: %s" file
))
261 (defun org-attach-delete-all (&optional force
)
262 "Delete all attachments from the current task.
263 This actually deletes the entire attachment directory.
264 A safer way is to open the directory in dired and delete from there."
266 (when org-attach-file-list-property
267 (org-entry-delete (point) org-attach-file-list-property
))
268 (let ((attach-dir (org-attach-dir)))
272 (y-or-n-p "Are you sure you want to remove all attachments of this entry? ")))
273 (shell-command (format "rm -fr %s" attach-dir
))
274 (message "Attachment directory removed")
276 (org-attach-untag))))
278 (defun org-attach-sync ()
279 "Synchronize the current tasks with its attachments.
280 This can be used after files have been added externally."
283 (when org-attach-file-list-property
284 (org-entry-delete (point) org-attach-file-list-property
))
285 (let ((attach-dir (org-attach-dir)))
287 (let ((files (org-attach-file-list attach-dir
)))
288 (and files
(org-attach-tag))
289 (when org-attach-file-list-property
291 (unless (string-match "^\\." file
)
292 (org-entry-add-to-multivalued-property
293 (point) org-attach-file-list-property file
))))))))
295 (defun org-attach-file-list (dir)
296 "Return a list of files in the attachment directory.
297 This ignores files starting with a \".\", and files ending in \"~\"."
299 (mapcar (lambda (x) (if (string-match "^\\." x
) nil x
))
300 (directory-files dir nil
"[^~]\\'"))))
302 (defun org-attach-reveal ()
303 "Show the attachment directory of the current task in dired."
305 (let ((attach-dir (org-attach-dir t
)))
306 (org-open-file attach-dir
)))
308 (defun org-attach-reveal-in-emacs ()
309 "Show the attachment directory of the current task.
310 This will attempt to use an external program to show the directory."
312 (let ((attach-dir (org-attach-dir t
)))
315 (defun org-attach-open (&optional in-emacs
)
316 "Open an attachment of the current task.
317 If there are more than one attachment, you will be prompted for the file name.
318 This command will open the file using the settings in `org-file-apps'
319 and in the system-specific variants of this variable.
320 If IN-EMACS is non-nil, force opening in Emacs."
322 (let* ((attach-dir (org-attach-dir t
))
323 (files (org-attach-file-list attach-dir
))
324 (file (if (= (length files
) 1)
326 (org-ido-completing-read "Open attachment: "
327 (mapcar 'list files
) nil t
))))
328 (org-open-file (expand-file-name file attach-dir
) in-emacs
)))
330 (defun org-attach-open-in-emacs ()
331 "Open attachment, force opening in Emacs.
332 See `org-attach-open'."
334 (org-attach-open 'in-emacs
))
336 (defun org-attach-expand (file)
337 "Return the full path to the current entry's attachment file FILE.
338 Basically, this adds the path to the attachment directory."
339 (expand-file-name file
(org-attach-dir)))
341 (defun org-attach-expand-link (file)
342 "Return a file link pointing to the current entry's attachment file FILE.
343 Basically, this adds the path to the attachment directory, and a \"file:\"
345 (concat "file:" (org-attach-expand file
)))
347 (provide 'org-attach
)
349 ;; arch-tag: fce93c2e-fe07-4fa3-a905-e10dcc7a6248
350 ;;; org-attach.el ends here