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