lisp/org-table.el: fix table alignment
[org-mode/org-tableheadings.git] / lisp / org-attach.el
blobde8b2fc9634c1e61dc4c8b48aade89d8cdfa37a2
1 ;;; org-attach.el --- Manage file attachments to Org tasks -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2008-2019 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 <https://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;; See the Org 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 (require 'cl-lib)
41 (require 'org)
42 (require 'org-id)
43 (require 'vc-git)
45 (declare-function dired-dwim-target-directory "dired-aux")
47 (defgroup org-attach nil
48 "Options concerning entry attachments in Org mode."
49 :tag "Org Attach"
50 :group 'org)
52 (defcustom org-attach-directory "data/"
53 "The directory where attachments are stored.
54 If this is a relative path, it will be interpreted relative to the directory
55 where the Org file lives."
56 :group 'org-attach
57 :type 'directory
58 :safe #'stringp)
60 (defcustom org-attach-commit t
61 "If non-nil commit attachments with git.
62 This is only done if the Org file is in a git repository."
63 :group 'org-attach
64 :type 'boolean
65 :version "26.1"
66 :package-version '(Org . "9.0"))
68 (defcustom org-attach-git-annex-cutoff (* 32 1024)
69 "If non-nil, files larger than this will be annexed instead of stored."
70 :group 'org-attach
71 :version "24.4"
72 :package-version '(Org . "8.0")
73 :type '(choice
74 (const :tag "None" nil)
75 (integer :tag "Bytes")))
77 (defcustom org-attach-auto-tag "ATTACH"
78 "Tag that will be triggered automatically when an entry has an attachment."
79 :group 'org-attach
80 :type '(choice
81 (const :tag "None" nil)
82 (string :tag "Tag")))
84 (defcustom org-attach-file-list-property "Attachments"
85 "The property used to keep a list of attachment belonging to this entry.
86 This is not really needed, so you may set this to nil if you don't want it.
87 Also, for entries where children inherit the directory, the list of
88 attachments is not kept in this property."
89 :group 'org-attach
90 :type '(choice
91 (const :tag "None" nil)
92 (string :tag "Tag")))
94 (defcustom org-attach-method 'cp
95 "The preferred method to attach a file.
96 Allowed values are:
98 mv rename the file to move it into the attachment directory
99 cp copy the file
100 ln create a hard link. Note that this is not supported
101 on all systems, and then the result is not defined.
102 lns create a symbol link. Note that this is not supported
103 on all systems, and then the result is not defined."
104 :group 'org-attach
105 :type '(choice
106 (const :tag "Copy" cp)
107 (const :tag "Move/Rename" mv)
108 (const :tag "Hard Link" ln)
109 (const :tag "Symbol Link" lns)))
111 (defcustom org-attach-expert nil
112 "Non-nil means do not show the splash buffer with the attach dispatcher."
113 :group 'org-attach
114 :type 'boolean)
116 (defcustom org-attach-allow-inheritance t
117 "Non-nil means allow attachment directories be inherited."
118 :group 'org-attach
119 :type 'boolean)
121 (defvar org-attach-inherited nil
122 "Indicates if the last access to the attachment directory was inherited.")
124 (defcustom org-attach-store-link-p nil
125 "Non-nil means store a link to a file when attaching it."
126 :group 'org-attach
127 :version "24.1"
128 :type '(choice
129 (const :tag "Don't store link" nil)
130 (const :tag "Link to origin location" t)
131 (const :tag "Link to the attach-dir location" attached)))
133 (defcustom org-attach-archive-delete nil
134 "Non-nil means attachments are deleted upon archiving a subtree.
135 When set to `query', ask the user instead."
136 :group 'org-attach
137 :version "26.1"
138 :package-version '(Org . "8.3")
139 :type '(choice
140 (const :tag "Never delete attachments" nil)
141 (const :tag "Always delete attachments" t)
142 (const :tag "Query the user" query)))
144 (defcustom org-attach-annex-auto-get 'ask
145 "Confirmation preference for automatically getting annex files.
146 If \\='ask, prompt using `y-or-n-p'. If t, always get. If nil, never get."
147 :group 'org-attach
148 :package-version '(Org . "9.0")
149 :version "26.1"
150 :type '(choice
151 (const :tag "confirm with `y-or-n-p'" ask)
152 (const :tag "always get from annex if necessary" t)
153 (const :tag "never get from annex" nil)))
155 (defcustom org-attach-commands
156 '(((?a ?\C-a) org-attach-attach
157 "Select a file and attach it to the task, using `org-attach-method'.")
158 ((?c ?\C-c) org-attach-attach-cp
159 "Attach a file using copy method.")
160 ((?m ?\C-m) org-attach-attach-mv
161 "Attach a file using move method.")
162 ((?l ?\C-l) org-attach-attach-ln
163 "Attach a file using link method.")
164 ((?y ?\C-y) org-attach-attach-lns
165 "Attach a file using symbolic-link method.")
166 ((?u ?\C-u) org-attach-url
167 "Attach a file from URL (downloading it).")
168 ((?b) org-attach-buffer
169 "Select a buffer and attach its contents to the task.")
170 ((?n ?\C-n) org-attach-new
171 "Create a new attachment, as an Emacs buffer.")
172 ((?z ?\C-z) org-attach-sync
173 "Synchronize the current task with its attachment\n directory, in case \
174 you added attachments yourself.\n")
175 ((?o ?\C-o) org-attach-open
176 "Open current task's attachments.")
177 ((?O) org-attach-open-in-emacs
178 "Like \"o\", but force opening in Emacs.")
179 ((?f ?\C-f) org-attach-reveal
180 "Open current task's attachment directory.")
181 ((?F) org-attach-reveal-in-emacs
182 "Like \"f\", but force using Dired in Emacs.\n")
183 ((?d ?\C-d) org-attach-delete-one
184 "Delete one attachment, you will be prompted for a file name.")
185 ((?D) org-attach-delete-all
186 "Delete all of a task's attachments. A safer way is\n to open the \
187 directory in dired and delete from there.\n")
188 ((?s ?\C-s) org-attach-set-directory
189 "Set a specific attachment directory for this entry or reset to default.")
190 ((?i ?\C-i) org-attach-set-inherit
191 "Make children of the current entry inherit its attachment directory.\n")
192 ((?q) (lambda () (interactive) (message "Abort")) "Abort."))
193 "The list of commands for the attachment dispatcher.
194 Each entry in this list is a list of three elements:
195 - A list of keys (characters) to select the command (the fist
196 character in the list is shown in the attachment dispatcher's
197 splash buffer and minubuffer prompt).
198 - A command that is called interactively when one of these keys
199 is pressed.
200 - A docstring for this command in the attachment dispatcher's
201 splash buffer."
202 :group 'org-attach
203 :package-version '(Org . "9.3")
204 :type '(repeat (list (repeat :tag "Keys" character)
205 (function :tag "Command")
206 (string :tag "Docstring"))))
208 ;;;###autoload
209 (defun org-attach ()
210 "The dispatcher for attachment commands.
211 Shows a list of commands and prompts for another key to execute a command."
212 (interactive)
213 (let (c marker)
214 (when (eq major-mode 'org-agenda-mode)
215 (setq marker (or (get-text-property (point) 'org-hd-marker)
216 (get-text-property (point) 'org-marker)))
217 (unless marker
218 (error "No task in current line")))
219 (save-excursion
220 (when marker
221 (set-buffer (marker-buffer marker))
222 (goto-char marker))
223 (org-back-to-heading t)
224 (save-excursion
225 (save-window-excursion
226 (unless org-attach-expert
227 (with-output-to-temp-buffer "*Org Attach*"
228 (princ
229 (format "Select an Attachment Command:\n\n%s"
230 (mapconcat
231 (lambda (entry)
232 (pcase entry
233 (`((,key . ,_) ,_ ,docstring)
234 (format "%c %s"
236 (replace-regexp-in-string "\n\\([\t ]*\\)"
238 docstring
239 nil nil 1)))
241 (user-error
242 "Invalid `org-attach-commands' item: %S"
243 entry))))
244 org-attach-commands
245 "\n")))))
246 (org-fit-window-to-buffer (get-buffer-window "*Org Attach*"))
247 (message "Select command: [%s]"
248 (concat (mapcar #'caar org-attach-commands)))
249 (setq c (read-char-exclusive))
250 (and (get-buffer "*Org Attach*") (kill-buffer "*Org Attach*"))))
251 (let ((command (cl-some (lambda (entry)
252 (and (memq c (nth 0 entry)) (nth 1 entry)))
253 org-attach-commands)))
254 (if (commandp command t)
255 (call-interactively command)
256 (error "No such attachment command: %c" c))))))
258 (defun org-attach-dir (&optional create-if-not-exists-p)
259 "Return the directory associated with the current entry.
260 This first checks for a local property ATTACH_DIR, and then for an inherited
261 property ATTACH_DIR_INHERIT. If neither exists, the default mechanism
262 using the entry ID will be invoked to access the unique directory for the
263 current entry.
264 If the directory does not exist and CREATE-IF-NOT-EXISTS-P is non-nil,
265 the directory and (if necessary) the corresponding ID will be created."
266 (let (attach-dir uuid)
267 (setq org-attach-inherited (org-entry-get nil "ATTACH_DIR_INHERIT"))
268 (cond
269 ((setq attach-dir (org-entry-get nil "ATTACH_DIR"))
270 (org-attach-check-absolute-path attach-dir))
271 ((and org-attach-allow-inheritance
272 (org-entry-get nil "ATTACH_DIR_INHERIT" t))
273 (setq attach-dir
274 (org-with-wide-buffer
275 (if (marker-position org-entry-property-inherited-from)
276 (goto-char org-entry-property-inherited-from)
277 (org-back-to-heading t))
278 (let (org-attach-allow-inheritance)
279 (org-attach-dir create-if-not-exists-p))))
280 (org-attach-check-absolute-path attach-dir)
281 (setq org-attach-inherited t))
282 (t ; use the ID
283 (org-attach-check-absolute-path nil)
284 (setq uuid (org-id-get (point) create-if-not-exists-p))
285 (when (or uuid create-if-not-exists-p)
286 (unless uuid (error "ID retrieval/creation failed"))
287 (setq attach-dir (expand-file-name
288 (format "%s/%s"
289 (substring uuid 0 2)
290 (substring uuid 2))
291 (expand-file-name org-attach-directory))))))
292 (when attach-dir
293 (if (and create-if-not-exists-p
294 (not (file-directory-p attach-dir)))
295 (make-directory attach-dir t))
296 (and (file-exists-p attach-dir)
297 attach-dir))))
299 (defun org-attach-check-absolute-path (dir)
300 "Check if we have enough information to root the attachment directory.
301 When DIR is given, check also if it is already absolute. Otherwise,
302 assume that it will be relative, and check if `org-attach-directory' is
303 absolute, or if at least the current buffer has a file name.
304 Throw an error if we cannot root the directory."
305 (or (and dir (file-name-absolute-p dir))
306 (file-name-absolute-p org-attach-directory)
307 (buffer-file-name (buffer-base-buffer))
308 (error "Need absolute `org-attach-directory' to attach in buffers without filename")))
310 (defun org-attach-set-directory (&optional arg)
311 "Set the ATTACH_DIR node property and ask to move files there.
312 The property defines the directory that is used for attachments
313 of the entry. When called with `\\[universal-argument]', reset \
314 the directory to
315 the default ID based one."
316 (interactive "P")
317 (let ((old (org-attach-dir))
318 (new
319 (progn
320 (if arg (org-entry-delete nil "ATTACH_DIR")
321 (let ((dir (read-directory-name
322 "Attachment directory: "
323 (org-entry-get nil
324 "ATTACH_DIR"
325 (and org-attach-allow-inheritance t)))))
326 (org-entry-put nil "ATTACH_DIR" dir)))
327 (org-attach-dir t))))
328 (unless (or (string= old new)
329 (not old))
330 (when (yes-or-no-p "Copy over attachments from old directory? ")
331 (copy-directory old new t nil t))
332 (when (yes-or-no-p (concat "Delete " old))
333 (delete-directory old t)))))
335 (defun org-attach-set-inherit ()
336 "Set the ATTACH_DIR_INHERIT property of the current entry.
337 The property defines the directory that is used for attachments
338 of the entry and any children that do not explicitly define (by setting
339 the ATTACH_DIR property) their own attachment directory."
340 (interactive)
341 (org-entry-put nil "ATTACH_DIR_INHERIT" "t")
342 (message "Children will inherit attachment directory"))
344 (defun org-attach-use-annex ()
345 "Return non-nil if git annex can be used."
346 (let ((git-dir (vc-git-root (expand-file-name org-attach-directory))))
347 (and org-attach-git-annex-cutoff
348 (or (file-exists-p (expand-file-name "annex" git-dir))
349 (file-exists-p (expand-file-name ".git/annex" git-dir))))))
351 (defun org-attach-annex-get-maybe (path)
352 "Call git annex get PATH (via shell) if using git annex.
353 Signals an error if the file content is not available and it was not retrieved."
354 (let* ((default-directory (expand-file-name org-attach-directory))
355 (path-relative (file-relative-name path)))
356 (when (and (org-attach-use-annex)
357 (not
358 (string-equal
359 "found"
360 (shell-command-to-string
361 (format "git annex find --format=found --in=here %s"
362 (shell-quote-argument path-relative))))))
363 (let ((should-get
364 (if (eq org-attach-annex-auto-get 'ask)
365 (y-or-n-p (format "Run git annex get %s? " path-relative))
366 org-attach-annex-auto-get)))
367 (if should-get
368 (progn (message "Running git annex get \"%s\"." path-relative)
369 (call-process "git" nil nil nil "annex" "get" path-relative))
370 (error "File %s stored in git annex but it is not available, and was not retrieved"
371 path))))))
373 (defun org-attach-commit ()
374 "Commit changes to git if `org-attach-directory' is properly initialized.
375 This checks for the existence of a \".git\" directory in that directory."
376 (let* ((dir (expand-file-name org-attach-directory))
377 (git-dir (vc-git-root dir))
378 (use-annex (org-attach-use-annex))
379 (changes 0))
380 (when (and git-dir (executable-find "git"))
381 (with-temp-buffer
382 (cd dir)
383 (dolist (new-or-modified
384 (split-string
385 (shell-command-to-string
386 "git ls-files -zmo --exclude-standard") "\0" t))
387 (if (and use-annex
388 (>= (file-attribute-size (file-attributes new-or-modified))
389 org-attach-git-annex-cutoff))
390 (call-process "git" nil nil nil "annex" "add" new-or-modified)
391 (call-process "git" nil nil nil "add" new-or-modified))
392 (cl-incf changes))
393 (dolist (deleted
394 (split-string
395 (shell-command-to-string "git ls-files -z --deleted") "\0" t))
396 (call-process "git" nil nil nil "rm" deleted)
397 (cl-incf changes))
398 (when (> changes 0)
399 (shell-command "git commit -m 'Synchronized attachments'"))))))
401 (defun org-attach-tag (&optional off)
402 "Turn the autotag on or (if OFF is set) off."
403 (when org-attach-auto-tag
404 (save-excursion
405 (org-back-to-heading t)
406 (org-toggle-tag org-attach-auto-tag (if off 'off 'on)))))
408 (defun org-attach-untag ()
409 "Turn the autotag off."
410 (org-attach-tag 'off))
412 (defun org-attach-store-link (file)
413 "Add a link to `org-stored-link' when attaching a file.
414 Only do this when `org-attach-store-link-p' is non-nil."
415 (setq org-stored-links
416 (cons (list (org-attach-expand-link file)
417 (file-name-nondirectory file))
418 org-stored-links)))
420 (defun org-attach-url (url)
421 (interactive "MURL of the file to attach: \n")
422 (org-attach-attach url))
424 (defun org-attach-buffer (buffer-name)
425 "Attach BUFFER-NAME's contents to current task.
426 BUFFER-NAME is a string. Signals a `file-already-exists' error
427 if it would overwrite an existing filename."
428 (interactive "bBuffer whose contents should be attached: ")
429 (let ((output (expand-file-name buffer-name (org-attach-dir t))))
430 (when (file-exists-p output)
431 (signal 'file-already-exists (list "File exists" output)))
432 (when (and org-attach-file-list-property (not org-attach-inherited))
433 (org-entry-add-to-multivalued-property
434 (point) org-attach-file-list-property buffer-name))
435 (org-attach-tag)
436 (with-temp-file output
437 (insert-buffer-substring buffer-name))))
439 (defun org-attach-attach (file &optional visit-dir method)
440 "Move/copy/link FILE into the attachment directory of the current task.
441 If VISIT-DIR is non-nil, visit the directory with dired.
442 METHOD may be `cp', `mv', `ln', `lns' or `url' default taken from
443 `org-attach-method'."
444 (interactive
445 (list
446 (read-file-name "File to keep as an attachment:"
447 (or (progn
448 (require 'dired-aux)
449 (dired-dwim-target-directory))
450 default-directory))
451 current-prefix-arg
452 nil))
453 (setq method (or method org-attach-method))
454 (let ((basename (file-name-nondirectory file)))
455 (when (and org-attach-file-list-property (not org-attach-inherited))
456 (org-entry-add-to-multivalued-property
457 (point) org-attach-file-list-property basename))
458 (let* ((attach-dir (org-attach-dir t))
459 (fname (expand-file-name basename attach-dir)))
460 (cond
461 ((eq method 'mv) (rename-file file fname))
462 ((eq method 'cp) (copy-file file fname))
463 ((eq method 'ln) (add-name-to-file file fname))
464 ((eq method 'lns) (make-symbolic-link file fname))
465 ((eq method 'url) (url-copy-file file fname)))
466 (when org-attach-commit
467 (org-attach-commit))
468 (org-attach-tag)
469 (cond ((eq org-attach-store-link-p 'attached)
470 (org-attach-store-link fname))
471 ((eq org-attach-store-link-p t)
472 (org-attach-store-link file)))
473 (if visit-dir
474 (dired attach-dir)
475 (message "File %S is now a task attachment." basename)))))
477 (defun org-attach-attach-cp ()
478 "Attach a file by copying it."
479 (interactive)
480 (let ((org-attach-method 'cp)) (call-interactively 'org-attach-attach)))
481 (defun org-attach-attach-mv ()
482 "Attach a file by moving (renaming) it."
483 (interactive)
484 (let ((org-attach-method 'mv)) (call-interactively 'org-attach-attach)))
485 (defun org-attach-attach-ln ()
486 "Attach a file by creating a hard link to it.
487 Beware that this does not work on systems that do not support hard links.
488 On some systems, this apparently does copy the file instead."
489 (interactive)
490 (let ((org-attach-method 'ln)) (call-interactively 'org-attach-attach)))
491 (defun org-attach-attach-lns ()
492 "Attach a file by creating a symbolic link to it.
494 Beware that this does not work on systems that do not support symbolic links.
495 On some systems, this apparently does copy the file instead."
496 (interactive)
497 (let ((org-attach-method 'lns)) (call-interactively 'org-attach-attach)))
499 (defun org-attach-new (file)
500 "Create a new attachment FILE for the current task.
501 The attachment is created as an Emacs buffer."
502 (interactive "sCreate attachment named: ")
503 (when (and org-attach-file-list-property (not org-attach-inherited))
504 (org-entry-add-to-multivalued-property
505 (point) org-attach-file-list-property file))
506 (let ((attach-dir (org-attach-dir t)))
507 (org-attach-tag)
508 (find-file (expand-file-name file attach-dir))
509 (message "New attachment %s" file)))
511 (defun org-attach-delete-one (&optional file)
512 "Delete a single attachment."
513 (interactive)
514 (let* ((attach-dir (org-attach-dir t))
515 (files (org-attach-file-list attach-dir))
516 (file (or file
517 (completing-read
518 "Delete attachment: "
519 (mapcar (lambda (f)
520 (list (file-name-nondirectory f)))
521 files)))))
522 (setq file (expand-file-name file attach-dir))
523 (unless (file-exists-p file)
524 (error "No such attachment: %s" file))
525 (delete-file file)
526 (when org-attach-commit
527 (org-attach-commit))))
529 (defun org-attach-delete-all (&optional force)
530 "Delete all attachments from the current task.
531 This actually deletes the entire attachment directory.
532 A safer way is to open the directory in dired and delete from there."
533 (interactive "P")
534 (when (and org-attach-file-list-property (not org-attach-inherited))
535 (org-entry-delete (point) org-attach-file-list-property))
536 (let ((attach-dir (org-attach-dir)))
537 (when
538 (and attach-dir
539 (or force
540 (y-or-n-p "Are you sure you want to remove all attachments of this entry? ")))
541 (shell-command (format "rm -fr %s" attach-dir))
542 (message "Attachment directory removed")
543 (when org-attach-commit
544 (org-attach-commit))
545 (org-attach-untag))))
547 (defun org-attach-sync ()
548 "Synchronize the current tasks with its attachments.
549 This can be used after files have been added externally."
550 (interactive)
551 (when org-attach-commit
552 (org-attach-commit))
553 (when (and org-attach-file-list-property (not org-attach-inherited))
554 (org-entry-delete (point) org-attach-file-list-property))
555 (let ((attach-dir (org-attach-dir)))
556 (when attach-dir
557 (let ((files (org-attach-file-list attach-dir)))
558 (org-attach-tag (not files))
559 (when org-attach-file-list-property
560 (dolist (file files)
561 (unless (string-match "^\\.\\.?\\'" file)
562 (org-entry-add-to-multivalued-property
563 (point) org-attach-file-list-property file))))))))
565 (defun org-attach-file-list (dir)
566 "Return a list of files in the attachment directory.
567 This ignores files ending in \"~\"."
568 (delq nil
569 (mapcar (lambda (x) (if (string-match "^\\.\\.?\\'" x) nil x))
570 (directory-files dir nil "[^~]\\'"))))
572 (defun org-attach-reveal (&optional if-exists)
573 "Show the attachment directory of the current task.
574 This will attempt to use an external program to show the directory."
575 (interactive "P")
576 (let ((attach-dir (org-attach-dir (not if-exists))))
577 (and attach-dir (org-open-file attach-dir))))
579 (defun org-attach-reveal-in-emacs ()
580 "Show the attachment directory of the current task in dired."
581 (interactive)
582 (let ((attach-dir (org-attach-dir t)))
583 (dired attach-dir)))
585 (defun org-attach-open (&optional in-emacs)
586 "Open an attachment of the current task.
587 If there are more than one attachment, you will be prompted for the file name.
588 This command will open the file using the settings in `org-file-apps'
589 and in the system-specific variants of this variable.
590 If IN-EMACS is non-nil, force opening in Emacs."
591 (interactive "P")
592 (let* ((attach-dir (org-attach-dir t))
593 (files (org-attach-file-list attach-dir))
594 (file (if (= (length files) 1)
595 (car files)
596 (completing-read "Open attachment: "
597 (mapcar #'list files) nil t)))
598 (path (expand-file-name file attach-dir)))
599 (org-attach-annex-get-maybe path)
600 (org-open-file path in-emacs)))
602 (defun org-attach-open-in-emacs ()
603 "Open attachment, force opening in Emacs.
604 See `org-attach-open'."
605 (interactive)
606 (org-attach-open 'in-emacs))
608 (defun org-attach-expand (file)
609 "Return the full path to the current entry's attachment file FILE.
610 Basically, this adds the path to the attachment directory."
611 (expand-file-name file (org-attach-dir)))
613 (defun org-attach-expand-link (file)
614 "Return a file link pointing to the current entry's attachment file FILE.
615 Basically, this adds the path to the attachment directory, and a \"file:\"
616 prefix."
617 (concat "file:" (org-attach-expand file)))
619 (defun org-attach-archive-delete-maybe ()
620 "Maybe delete subtree attachments when archiving.
621 This function is called by `org-archive-hook'. The option
622 `org-attach-archive-delete' controls its behavior."
623 (when org-attach-archive-delete
624 (org-attach-delete-all (not (eq org-attach-archive-delete 'query)))))
627 ;; Attach from dired.
629 ;; Add the following lines to the config file to get a binding for
630 ;; dired-mode.
632 ;; (add-hook
633 ;; 'dired-mode-hook
634 ;; (lambda ()
635 ;; (define-key dired-mode-map (kbd "C-c C-x a") #'org-attach-dired-to-subtree))))
637 ;;;###autoload
638 (defun org-attach-dired-to-subtree (files)
639 "Attach FILES marked or current file in dired to subtree in other window.
640 Takes the method given in `org-attach-method' for the attach action.
641 Precondition: Point must be in a dired buffer.
642 Idea taken from `gnus-dired-attach'."
643 (interactive
644 (list (dired-get-marked-files)))
645 (unless (eq major-mode 'dired-mode)
646 (user-error "This command must be triggered in a dired buffer."))
647 (let ((start-win (selected-window))
648 (other-win
649 (get-window-with-predicate
650 (lambda (window)
651 (with-current-buffer (window-buffer window)
652 (eq major-mode 'org-mode))))))
653 (unless other-win
654 (user-error
655 "Can't attach to subtree. No window displaying an Org buffer"))
656 (select-window other-win)
657 (dolist (file files)
658 (org-attach-attach file))
659 (select-window start-win)
660 (when (eq 'mv org-attach-method)
661 (revert-buffer))))
665 (add-hook 'org-archive-hook 'org-attach-archive-delete-maybe)
667 (provide 'org-attach)
669 ;; Local variables:
670 ;; generated-autoload-file: "org-loaddefs.el"
671 ;; End:
673 ;;; org-attach.el ends here