org.el (org-time-stamp): C-u C-u C-c . inserts a timestamp with no prompt.
[org-mode.git] / lisp / org-attach.el
blobcc6b593742cd7d3968af64e529ece053355cd67e
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.
9 ;;
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/>.
23 ;;; Commentary:
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
36 ;; `org-id-method'.
38 ;;; Code:
40 (eval-when-compile
41 (require 'cl))
42 (require 'org-id)
43 (require 'org)
45 (defgroup org-attach nil
46 "Options concerning entry attachments in Org-mode."
47 :tag "Org Attach"
48 :group 'org)
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."
54 :group 'org-attach
55 :type 'directory)
57 (defcustom org-attach-auto-tag "ATTACH"
58 "Tag that will be triggered automatically when an entry has an attachment."
59 :group 'org-attach
60 :type '(choice
61 (const :tag "None" nil)
62 (string :tag "Tag")))
64 (defcustom org-attach-file-list-property "Attachments"
65 "The property used to keep a list of attachment belonging to this entry.
66 This is not really needed, so you may set this to nil if you don't want it.
67 Also, for entries where children inherit the directory, the list of
68 attachments is not kept in this property."
69 :group 'org-attach
70 :type '(choice
71 (const :tag "None" nil)
72 (string :tag "Tag")))
74 (defcustom org-attach-method 'cp
75 "The preferred method to attach a file.
76 Allowed values are:
78 mv rename the file to move it into the attachment directory
79 cp copy the file
80 ln create a hard link. Note that this is not supported
81 on all systems, and then the result is not defined.
82 lns create a symbol link. Note that this is not supported
83 on all systems, and then the result is not defined."
84 :group 'org-attach
85 :type '(choice
86 (const :tag "Copy" cp)
87 (const :tag "Move/Rename" mv)
88 (const :tag "Hard Link" ln)
89 (const :tag "Symbol Link" lns)))
91 (defcustom org-attach-expert nil
92 "Non-nil means do not show the splash buffer with the attach dispatcher."
93 :group 'org-attach
94 :type 'boolean)
96 (defcustom org-attach-allow-inheritance t
97 "Non-nil means allow attachment directories be inherited."
98 :group 'org-attach
99 :type 'boolean)
101 (defvar org-attach-inherited nil
102 "Indicates if the last access to the attachment directory was inherited.")
104 (defcustom org-attach-store-link-p nil
105 "Non-nil means store a link to a file when attaching it."
106 :group 'org-attach
107 :version "24.1"
108 :type '(choice
109 (const :tag "Don't store link" nil)
110 (const :tag "Link to origin location" t)
111 (const :tag "Link to the attach-dir location" attached)))
113 ;;;###autoload
114 (defun org-attach ()
115 "The dispatcher for attachment commands.
116 Shows a list of commands and prompts for another key to execute a command."
117 (interactive)
118 (let (c marker)
119 (when (eq major-mode 'org-agenda-mode)
120 (setq marker (or (get-text-property (point) 'org-hd-marker)
121 (get-text-property (point) 'org-marker)))
122 (unless marker
123 (error "No task in current line")))
124 (save-excursion
125 (when marker
126 (set-buffer (marker-buffer marker))
127 (goto-char marker))
128 (org-back-to-heading t)
129 (save-excursion
130 (save-window-excursion
131 (unless org-attach-expert
132 (with-output-to-temp-buffer "*Org Attach*"
133 (princ "Select an Attachment Command:
135 a Select a file and attach it to the task, using `org-attach-method'.
136 c/m/l/y Attach a file using copy/move/link/symbolic-link method.
137 n Create a new attachment, as an Emacs buffer.
138 z Synchronize the current task with its attachment
139 directory, in case you added attachments yourself.
141 o Open current task's attachments.
142 O Like \"o\", but force opening in Emacs.
143 f Open current task's attachment directory.
144 F Like \"f\", but force using dired in Emacs.
146 d Delete one attachment, you will be prompted for a file name.
147 D Delete all of a task's attachments. A safer way is
148 to open the directory in dired and delete from there.
150 s Set a specific attachment directory for this entry.
151 i Make children of the current entry inherit its attachment directory.")))
152 (org-fit-window-to-buffer (get-buffer-window "*Org Attach*"))
153 (message "Select command: [acmlzoOfFdD]")
154 (setq c (read-char-exclusive))
155 (and (get-buffer "*Org Attach*") (kill-buffer "*Org Attach*"))))
156 (cond
157 ((memq c '(?a ?\C-a)) (call-interactively 'org-attach-attach))
158 ((memq c '(?c ?\C-c))
159 (let ((org-attach-method 'cp)) (call-interactively 'org-attach-attach)))
160 ((memq c '(?m ?\C-m))
161 (let ((org-attach-method 'mv)) (call-interactively 'org-attach-attach)))
162 ((memq c '(?l ?\C-l))
163 (let ((org-attach-method 'ln)) (call-interactively 'org-attach-attach)))
164 ((memq c '(?y ?\C-y))
165 (let ((org-attach-method 'lns)) (call-interactively 'org-attach-attach)))
166 ((memq c '(?n ?\C-n)) (call-interactively 'org-attach-new))
167 ((memq c '(?z ?\C-z)) (call-interactively 'org-attach-sync))
168 ((memq c '(?o ?\C-o)) (call-interactively 'org-attach-open))
169 ((eq c ?O) (call-interactively 'org-attach-open-in-emacs))
170 ((memq c '(?f ?\C-f)) (call-interactively 'org-attach-reveal))
171 ((memq c '(?F)) (call-interactively 'org-attach-reveal-in-emacs))
172 ((memq c '(?d ?\C-d)) (call-interactively
173 'org-attach-delete-one))
174 ((eq c ?D) (call-interactively 'org-attach-delete-all))
175 ((eq c ?q) (message "Abort"))
176 ((memq c '(?s ?\C-s)) (call-interactively
177 'org-attach-set-directory))
178 ((memq c '(?i ?\C-i)) (call-interactively
179 'org-attach-set-inherit))
180 (t (error "No such attachment command %c" c))))))
182 (defun org-attach-dir (&optional create-if-not-exists-p)
183 "Return the directory associated with the current entry.
184 This first checks for a local property ATTACH_DIR, and then for an inherited
185 property ATTACH_DIR_INHERIT. If neither exists, the default mechanism
186 using the entry ID will be invoked to access the unique directory for the
187 current entry.
188 If the directory does not exist and CREATE-IF-NOT-EXISTS-P is non-nil,
189 the directory and (if necessary) the corresponding ID will be created."
190 (let (attach-dir uuid inherit)
191 (setq org-attach-inherited (org-entry-get nil "ATTACH_DIR_INHERIT"))
192 (cond
193 ((setq attach-dir (org-entry-get nil "ATTACH_DIR"))
194 (org-attach-check-absolute-path attach-dir))
195 ((and org-attach-allow-inheritance
196 (setq inherit (org-entry-get nil "ATTACH_DIR_INHERIT" t)))
197 (setq attach-dir
198 (save-excursion
199 (save-restriction
200 (widen)
201 (goto-char org-entry-property-inherited-from)
202 (let (org-attach-allow-inheritance)
203 (org-attach-dir create-if-not-exists-p)))))
204 (org-attach-check-absolute-path attach-dir)
205 (setq org-attach-inherited t))
206 (t ; use the ID
207 (org-attach-check-absolute-path nil)
208 (setq uuid (org-id-get (point) create-if-not-exists-p))
209 (when (or uuid create-if-not-exists-p)
210 (unless uuid (error "ID retrieval/creation failed"))
211 (setq attach-dir (expand-file-name
212 (format "%s/%s"
213 (substring uuid 0 2)
214 (substring uuid 2))
215 (expand-file-name org-attach-directory))))))
216 (when attach-dir
217 (if (and create-if-not-exists-p
218 (not (file-directory-p attach-dir)))
219 (make-directory attach-dir t))
220 (and (file-exists-p attach-dir)
221 attach-dir))))
223 (defun org-attach-check-absolute-path (dir)
224 "Check if we have enough information to root the attachment directory.
225 When DIR is given, check also if it is already absolute. Otherwise,
226 assume that it will be relative, and check if `org-attach-directory' is
227 absolute, or if at least the current buffer has a file name.
228 Throw an error if we cannot root the directory."
229 (or (and dir (file-name-absolute-p dir))
230 (file-name-absolute-p org-attach-directory)
231 (buffer-file-name (buffer-base-buffer))
232 (error "Need absolute `org-attach-directory' to attach in buffers without filename")))
234 (defun org-attach-set-directory ()
235 "Set the ATTACH_DIR property of the current entry.
236 The property defines the directory that is used for attachments
237 of the entry."
238 (interactive)
239 (let ((dir (org-entry-get nil "ATTACH_DIR")))
240 (setq dir (read-directory-name "Attachment directory: " dir))
241 (org-entry-put nil "ATTACH_DIR" dir)))
243 (defun org-attach-set-inherit ()
244 "Set the ATTACH_DIR_INHERIT property of the current entry.
245 The property defines the directory that is used for attachments
246 of the entry and any children that do not explicitly define (by setting
247 the ATTACH_DIR property) their own attachment directory."
248 (interactive)
249 (org-entry-put nil "ATTACH_DIR_INHERIT" "t")
250 (message "Children will inherit attachment directory"))
252 (defun org-attach-commit ()
253 "Commit changes to git if `org-attach-directory' is properly initialized.
254 This checks for the existence of a \".git\" directory in that directory."
255 (let ((dir (expand-file-name org-attach-directory)))
256 (when (file-exists-p (expand-file-name ".git" dir))
257 (with-temp-buffer
258 (cd dir)
259 (shell-command "git add .")
260 (shell-command "git ls-files --deleted" t)
261 (mapc #'(lambda (file)
262 (unless (string= file "")
263 (shell-command
264 (concat "git rm \"" file "\""))))
265 (split-string (buffer-string) "\n"))
266 (shell-command "git commit -m 'Synchronized attachments'")))))
268 (defun org-attach-tag (&optional off)
269 "Turn the autotag on or (if OFF is set) off."
270 (when org-attach-auto-tag
271 (save-excursion
272 (org-back-to-heading t)
273 (org-toggle-tag org-attach-auto-tag (if off 'off 'on)))))
275 (defun org-attach-untag ()
276 "Turn the autotag off."
277 (org-attach-tag 'off))
279 (defun org-attach-store-link (file)
280 "Add a link to `org-stored-link' when attaching a file.
281 Only do this when `org-attach-store-link-p' is non-nil."
282 (setq org-stored-links
283 (cons (list (org-attach-expand-link file)
284 (file-name-nondirectory file))
285 org-stored-links)))
287 (defun org-attach-attach (file &optional visit-dir method)
288 "Move/copy/link FILE into the attachment directory of the current task.
289 If VISIT-DIR is non-nil, visit the directory with dired.
290 METHOD may be `cp', `mv', `ln', or `lns' default taken from
291 `org-attach-method'."
292 (interactive "fFile to keep as an attachment: \nP")
293 (setq method (or method org-attach-method))
294 (let ((basename (file-name-nondirectory file)))
295 (when (and org-attach-file-list-property (not org-attach-inherited))
296 (org-entry-add-to-multivalued-property
297 (point) org-attach-file-list-property basename))
298 (let* ((attach-dir (org-attach-dir t))
299 (fname (expand-file-name basename attach-dir)))
300 (cond
301 ((eq method 'mv) (rename-file file fname))
302 ((eq method 'cp) (copy-file file fname))
303 ((eq method 'ln) (add-name-to-file file fname))
304 ((eq method 'lns) (make-symbolic-link file fname)))
305 (org-attach-commit)
306 (org-attach-tag)
307 (cond ((eq org-attach-store-link-p 'attached)
308 (org-attach-store-link fname))
309 ((eq org-attach-store-link-p t)
310 (org-attach-store-link file)))
311 (if visit-dir
312 (dired attach-dir)
313 (message "File \"%s\" is now a task attachment." basename)))))
315 (defun org-attach-attach-cp ()
316 "Attach a file by copying it."
317 (interactive)
318 (let ((org-attach-method 'cp)) (call-interactively 'org-attach-attach)))
319 (defun org-attach-attach-mv ()
320 "Attach a file by moving (renaming) it."
321 (interactive)
322 (let ((org-attach-method 'mv)) (call-interactively 'org-attach-attach)))
323 (defun org-attach-attach-ln ()
324 "Attach a file by creating a hard link to it.
325 Beware that this does not work on systems that do not support hard links.
326 On some systems, this apparently does copy the file instead."
327 (interactive)
328 (let ((org-attach-method 'ln)) (call-interactively 'org-attach-attach)))
329 (defun org-attach-attach-lns ()
330 "Attach a file by creating a symbolic link to it.
332 Beware that this does not work on systems that do not support symbolic links.
333 On some systems, this apparently does copy the file instead."
334 (interactive)
335 (let ((org-attach-method 'lns)) (call-interactively 'org-attach-attach)))
337 (defun org-attach-new (file)
338 "Create a new attachment FILE for the current task.
339 The attachment is created as an Emacs buffer."
340 (interactive "sCreate attachment named: ")
341 (when (and org-attach-file-list-property (not org-attach-inherited))
342 (org-entry-add-to-multivalued-property
343 (point) org-attach-file-list-property file))
344 (let ((attach-dir (org-attach-dir t)))
345 (org-attach-tag)
346 (find-file (expand-file-name file attach-dir))
347 (message "New attachment %s" file)))
349 (defun org-attach-delete-one (&optional file)
350 "Delete a single attachment."
351 (interactive)
352 (let* ((attach-dir (org-attach-dir t))
353 (files (org-attach-file-list attach-dir))
354 (file (or file
355 (org-icompleting-read
356 "Delete attachment: "
357 (mapcar (lambda (f)
358 (list (file-name-nondirectory f)))
359 files)))))
360 (setq file (expand-file-name file attach-dir))
361 (unless (file-exists-p file)
362 (error "No such attachment: %s" file))
363 (delete-file file)
364 (org-attach-commit)))
366 (defun org-attach-delete-all (&optional force)
367 "Delete all attachments from the current task.
368 This actually deletes the entire attachment directory.
369 A safer way is to open the directory in dired and delete from there."
370 (interactive "P")
371 (when (and org-attach-file-list-property (not org-attach-inherited))
372 (org-entry-delete (point) org-attach-file-list-property))
373 (let ((attach-dir (org-attach-dir)))
374 (when
375 (and attach-dir
376 (or force
377 (y-or-n-p "Are you sure you want to remove all attachments of this entry? ")))
378 (shell-command (format "rm -fr %s" attach-dir))
379 (message "Attachment directory removed")
380 (org-attach-commit)
381 (org-attach-untag))))
383 (defun org-attach-sync ()
384 "Synchronize the current tasks with its attachments.
385 This can be used after files have been added externally."
386 (interactive)
387 (org-attach-commit)
388 (when (and org-attach-file-list-property (not org-attach-inherited))
389 (org-entry-delete (point) org-attach-file-list-property))
390 (let ((attach-dir (org-attach-dir)))
391 (when attach-dir
392 (let ((files (org-attach-file-list attach-dir)))
393 (and files (org-attach-tag))
394 (when org-attach-file-list-property
395 (dolist (file files)
396 (unless (string-match "^\\." file)
397 (org-entry-add-to-multivalued-property
398 (point) org-attach-file-list-property file))))))))
400 (defun org-attach-file-list (dir)
401 "Return a list of files in the attachment directory.
402 This ignores files starting with a \".\", and files ending in \"~\"."
403 (delq nil
404 (mapcar (lambda (x) (if (string-match "^\\." x) nil x))
405 (directory-files dir nil "[^~]\\'"))))
407 (defun org-attach-reveal (&optional if-exists)
408 "Show the attachment directory of the current task in dired."
409 (interactive "P")
410 (let ((attach-dir (org-attach-dir (not if-exists))))
411 (and attach-dir (org-open-file attach-dir))))
413 (defun org-attach-reveal-in-emacs ()
414 "Show the attachment directory of the current task.
415 This will attempt to use an external program to show the directory."
416 (interactive)
417 (let ((attach-dir (org-attach-dir t)))
418 (dired attach-dir)))
420 (defun org-attach-open (&optional in-emacs)
421 "Open an attachment of the current task.
422 If there are more than one attachment, you will be prompted for the file name.
423 This command will open the file using the settings in `org-file-apps'
424 and in the system-specific variants of this variable.
425 If IN-EMACS is non-nil, force opening in Emacs."
426 (interactive "P")
427 (let* ((attach-dir (org-attach-dir t))
428 (files (org-attach-file-list attach-dir))
429 (file (if (= (length files) 1)
430 (car files)
431 (org-icompleting-read "Open attachment: "
432 (mapcar 'list files) nil t))))
433 (org-open-file (expand-file-name file attach-dir) in-emacs)))
435 (defun org-attach-open-in-emacs ()
436 "Open attachment, force opening in Emacs.
437 See `org-attach-open'."
438 (interactive)
439 (org-attach-open 'in-emacs))
441 (defun org-attach-expand (file)
442 "Return the full path to the current entry's attachment file FILE.
443 Basically, this adds the path to the attachment directory."
444 (expand-file-name file (org-attach-dir)))
446 (defun org-attach-expand-link (file)
447 "Return a file link pointing to the current entry's attachment file FILE.
448 Basically, this adds the path to the attachment directory, and a \"file:\"
449 prefix."
450 (concat "file:" (org-attach-expand file)))
452 (provide 'org-attach)
454 ;;; org-attach.el ends here