org-attach.el 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. ;;; org-attach.el --- Manage file attachments to Org tasks -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2008-2018 Free Software Foundation, Inc.
  3. ;; Author: John Wiegley <johnw@newartisans.com>
  4. ;; Keywords: org data task
  5. ;; This file is part of GNU Emacs.
  6. ;;
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; See the Org manual for information on how to use it.
  19. ;;
  20. ;; Attachments are managed in a special directory called "data", which
  21. ;; lives in the same directory as the org file itself. If this data
  22. ;; directory is initialized as a Git repository, then org-attach will
  23. ;; automatically commit changes when it sees them.
  24. ;;
  25. ;; Attachment directories are identified using a UUID generated for the
  26. ;; task which has the attachments. These are added as property to the
  27. ;; task when necessary, and should not be deleted or changed by the
  28. ;; user, ever. UUIDs are generated by a mechanism defined in the variable
  29. ;; `org-id-method'.
  30. ;;; Code:
  31. (require 'cl-lib)
  32. (require 'org)
  33. (require 'org-id)
  34. (require 'vc-git)
  35. (declare-function dired-dwim-target-directory "dired-aux")
  36. (defgroup org-attach nil
  37. "Options concerning entry attachments in Org mode."
  38. :tag "Org Attach"
  39. :group 'org)
  40. (defcustom org-attach-directory "data/"
  41. "The directory where attachments are stored.
  42. If this is a relative path, it will be interpreted relative to the directory
  43. where the Org file lives."
  44. :group 'org-attach
  45. :type 'directory)
  46. (defcustom org-attach-commit t
  47. "If non-nil commit attachments with git.
  48. This is only done if the Org file is in a git repository."
  49. :group 'org-attach
  50. :type 'boolean
  51. :version "26.1"
  52. :package-version '(Org . "9.0"))
  53. (defcustom org-attach-git-annex-cutoff (* 32 1024)
  54. "If non-nil, files larger than this will be annexed instead of stored."
  55. :group 'org-attach
  56. :version "24.4"
  57. :package-version '(Org . "8.0")
  58. :type '(choice
  59. (const :tag "None" nil)
  60. (integer :tag "Bytes")))
  61. (defcustom org-attach-auto-tag "ATTACH"
  62. "Tag that will be triggered automatically when an entry has an attachment."
  63. :group 'org-attach
  64. :type '(choice
  65. (const :tag "None" nil)
  66. (string :tag "Tag")))
  67. (defcustom org-attach-file-list-property "Attachments"
  68. "The property used to keep a list of attachment belonging to this entry.
  69. This is not really needed, so you may set this to nil if you don't want it.
  70. Also, for entries where children inherit the directory, the list of
  71. attachments is not kept in this property."
  72. :group 'org-attach
  73. :type '(choice
  74. (const :tag "None" nil)
  75. (string :tag "Tag")))
  76. (defcustom org-attach-method 'cp
  77. "The preferred method to attach a file.
  78. Allowed values are:
  79. mv rename the file to move it into the attachment directory
  80. cp copy the file
  81. ln create a hard link. Note that this is not supported
  82. on all systems, and then the result is not defined.
  83. lns create a symbol link. Note that this is not supported
  84. on all systems, and then the result is not defined."
  85. :group 'org-attach
  86. :type '(choice
  87. (const :tag "Copy" cp)
  88. (const :tag "Move/Rename" mv)
  89. (const :tag "Hard Link" ln)
  90. (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)
  95. (defcustom org-attach-allow-inheritance t
  96. "Non-nil means allow attachment directories be inherited."
  97. :group 'org-attach
  98. :type 'boolean)
  99. (defvar org-attach-inherited nil
  100. "Indicates if the last access to the attachment directory was inherited.")
  101. (defcustom org-attach-store-link-p nil
  102. "Non-nil means store a link to a file when attaching it."
  103. :group 'org-attach
  104. :version "24.1"
  105. :type '(choice
  106. (const :tag "Don't store link" nil)
  107. (const :tag "Link to origin location" t)
  108. (const :tag "Link to the attach-dir location" attached)))
  109. (defcustom org-attach-archive-delete nil
  110. "Non-nil means attachments are deleted upon archiving a subtree.
  111. When set to `query', ask the user instead."
  112. :group 'org-attach
  113. :version "26.1"
  114. :package-version '(Org . "8.3")
  115. :type '(choice
  116. (const :tag "Never delete attachments" nil)
  117. (const :tag "Always delete attachments" t)
  118. (const :tag "Query the user" query)))
  119. (defcustom org-attach-annex-auto-get 'ask
  120. "Confirmation preference for automatically getting annex files.
  121. If \\='ask, prompt using `y-or-n-p'. If t, always get. If nil, never get."
  122. :group 'org-attach
  123. :package-version '(Org . "9.0")
  124. :version "26.1"
  125. :type '(choice
  126. (const :tag "confirm with `y-or-n-p'" ask)
  127. (const :tag "always get from annex if necessary" t)
  128. (const :tag "never get from annex" nil)))
  129. ;;;###autoload
  130. (defun org-attach ()
  131. "The dispatcher for attachment commands.
  132. Shows a list of commands and prompts for another key to execute a command."
  133. (interactive)
  134. (let (c marker)
  135. (when (eq major-mode 'org-agenda-mode)
  136. (setq marker (or (get-text-property (point) 'org-hd-marker)
  137. (get-text-property (point) 'org-marker)))
  138. (unless marker
  139. (error "No task in current line")))
  140. (save-excursion
  141. (when marker
  142. (set-buffer (marker-buffer marker))
  143. (goto-char marker))
  144. (org-back-to-heading t)
  145. (save-excursion
  146. (save-window-excursion
  147. (unless org-attach-expert
  148. (with-output-to-temp-buffer "*Org Attach*"
  149. (princ "Select an Attachment Command:
  150. a Select a file and attach it to the task, using `org-attach-method'.
  151. c/m/l/y Attach a file using copy/move/link/symbolic-link method.
  152. u Attach a file from URL (downloading it).
  153. n Create a new attachment, as an Emacs buffer.
  154. z Synchronize the current task with its attachment
  155. directory, in case you added attachments yourself.
  156. o Open current task's attachments.
  157. O Like \"o\", but force opening in Emacs.
  158. f Open current task's attachment directory.
  159. F Like \"f\", but force using dired in Emacs.
  160. d Delete one attachment, you will be prompted for a file name.
  161. D Delete all of a task's attachments. A safer way is
  162. to open the directory in dired and delete from there.
  163. s Set a specific attachment directory for this entry or reset to default.
  164. i Make children of the current entry inherit its attachment directory.")))
  165. (org-fit-window-to-buffer (get-buffer-window "*Org Attach*"))
  166. (message "Select command: [acmlzoOfFdD]")
  167. (setq c (read-char-exclusive))
  168. (and (get-buffer "*Org Attach*") (kill-buffer "*Org Attach*"))))
  169. (cond
  170. ((memq c '(?a ?\C-a)) (call-interactively 'org-attach-attach))
  171. ((memq c '(?c ?\C-c))
  172. (let ((org-attach-method 'cp)) (call-interactively 'org-attach-attach)))
  173. ((memq c '(?m ?\C-m))
  174. (let ((org-attach-method 'mv)) (call-interactively 'org-attach-attach)))
  175. ((memq c '(?l ?\C-l))
  176. (let ((org-attach-method 'ln)) (call-interactively 'org-attach-attach)))
  177. ((memq c '(?y ?\C-y))
  178. (let ((org-attach-method 'lns)) (call-interactively 'org-attach-attach)))
  179. ((memq c '(?u ?\C-u))
  180. (let ((org-attach-method 'url)) (call-interactively 'org-attach-url)))
  181. ((memq c '(?n ?\C-n)) (call-interactively 'org-attach-new))
  182. ((memq c '(?z ?\C-z)) (call-interactively 'org-attach-sync))
  183. ((memq c '(?o ?\C-o)) (call-interactively 'org-attach-open))
  184. ((eq c ?O) (call-interactively 'org-attach-open-in-emacs))
  185. ((memq c '(?f ?\C-f)) (call-interactively 'org-attach-reveal))
  186. ((memq c '(?F)) (call-interactively 'org-attach-reveal-in-emacs))
  187. ((memq c '(?d ?\C-d)) (call-interactively
  188. 'org-attach-delete-one))
  189. ((eq c ?D) (call-interactively 'org-attach-delete-all))
  190. ((eq c ?q) (message "Abort"))
  191. ((memq c '(?s ?\C-s)) (call-interactively
  192. 'org-attach-set-directory))
  193. ((memq c '(?i ?\C-i)) (call-interactively
  194. 'org-attach-set-inherit))
  195. (t (error "No such attachment command %c" c))))))
  196. (defun org-attach-dir (&optional create-if-not-exists-p)
  197. "Return the directory associated with the current entry.
  198. This first checks for a local property ATTACH_DIR, and then for an inherited
  199. property ATTACH_DIR_INHERIT. If neither exists, the default mechanism
  200. using the entry ID will be invoked to access the unique directory for the
  201. current entry.
  202. If the directory does not exist and CREATE-IF-NOT-EXISTS-P is non-nil,
  203. the directory and (if necessary) the corresponding ID will be created."
  204. (let (attach-dir uuid)
  205. (setq org-attach-inherited (org-entry-get nil "ATTACH_DIR_INHERIT"))
  206. (cond
  207. ((setq attach-dir (org-entry-get nil "ATTACH_DIR"))
  208. (org-attach-check-absolute-path attach-dir))
  209. ((and org-attach-allow-inheritance
  210. (org-entry-get nil "ATTACH_DIR_INHERIT" t))
  211. (setq attach-dir
  212. (org-with-wide-buffer
  213. (if (marker-position org-entry-property-inherited-from)
  214. (goto-char org-entry-property-inherited-from)
  215. (org-back-to-heading t))
  216. (let (org-attach-allow-inheritance)
  217. (org-attach-dir create-if-not-exists-p))))
  218. (org-attach-check-absolute-path attach-dir)
  219. (setq org-attach-inherited t))
  220. (t ; use the ID
  221. (org-attach-check-absolute-path nil)
  222. (setq uuid (org-id-get (point) create-if-not-exists-p))
  223. (when (or uuid create-if-not-exists-p)
  224. (unless uuid (error "ID retrieval/creation failed"))
  225. (setq attach-dir (expand-file-name
  226. (format "%s/%s"
  227. (substring uuid 0 2)
  228. (substring uuid 2))
  229. (expand-file-name org-attach-directory))))))
  230. (when attach-dir
  231. (if (and create-if-not-exists-p
  232. (not (file-directory-p attach-dir)))
  233. (make-directory attach-dir t))
  234. (and (file-exists-p attach-dir)
  235. attach-dir))))
  236. (defun org-attach-check-absolute-path (dir)
  237. "Check if we have enough information to root the attachment directory.
  238. When DIR is given, check also if it is already absolute. Otherwise,
  239. assume that it will be relative, and check if `org-attach-directory' is
  240. absolute, or if at least the current buffer has a file name.
  241. Throw an error if we cannot root the directory."
  242. (or (and dir (file-name-absolute-p dir))
  243. (file-name-absolute-p org-attach-directory)
  244. (buffer-file-name (buffer-base-buffer))
  245. (error "Need absolute `org-attach-directory' to attach in buffers without filename")))
  246. (defun org-attach-set-directory (&optional arg)
  247. "Set the ATTACH_DIR node property and ask to move files there.
  248. The property defines the directory that is used for attachments
  249. of the entry. When called with `\\[universal-argument]', reset \
  250. the directory to
  251. the default ID based one."
  252. (interactive "P")
  253. (let ((old (org-attach-dir))
  254. (new
  255. (progn
  256. (if arg (org-entry-delete nil "ATTACH_DIR")
  257. (let ((dir (read-directory-name
  258. "Attachment directory: "
  259. (org-entry-get nil
  260. "ATTACH_DIR"
  261. (and org-attach-allow-inheritance t)))))
  262. (org-entry-put nil "ATTACH_DIR" dir)))
  263. (org-attach-dir t))))
  264. (unless (or (string= old new)
  265. (not old))
  266. (when (yes-or-no-p "Copy over attachments from old directory? ")
  267. (copy-directory old new t nil t))
  268. (when (yes-or-no-p (concat "Delete " old))
  269. (delete-directory old t)))))
  270. (defun org-attach-set-inherit ()
  271. "Set the ATTACH_DIR_INHERIT property of the current entry.
  272. The property defines the directory that is used for attachments
  273. of the entry and any children that do not explicitly define (by setting
  274. the ATTACH_DIR property) their own attachment directory."
  275. (interactive)
  276. (org-entry-put nil "ATTACH_DIR_INHERIT" "t")
  277. (message "Children will inherit attachment directory"))
  278. (defun org-attach-use-annex ()
  279. "Return non-nil if git annex can be used."
  280. (let ((git-dir (vc-git-root (expand-file-name org-attach-directory))))
  281. (and org-attach-git-annex-cutoff
  282. (or (file-exists-p (expand-file-name "annex" git-dir))
  283. (file-exists-p (expand-file-name ".git/annex" git-dir))))))
  284. (defun org-attach-annex-get-maybe (path)
  285. "Call git annex get PATH (via shell) if using git annex.
  286. Signals an error if the file content is not available and it was not retrieved."
  287. (let ((path-relative (file-relative-name path)))
  288. (when (and (org-attach-use-annex)
  289. (not
  290. (string-equal
  291. "found"
  292. (shell-command-to-string
  293. (format "git annex find --format=found --in=here %s"
  294. (shell-quote-argument path-relative))))))
  295. (let ((should-get
  296. (if (eq org-attach-annex-auto-get 'ask)
  297. (y-or-n-p (format "Run git annex get %s? " path-relative))
  298. org-attach-annex-auto-get)))
  299. (if should-get
  300. (progn (message "Running git annex get \"%s\"." path-relative)
  301. (call-process "git" nil nil nil "annex" "get" path-relative))
  302. (error "File %s stored in git annex but it is not available, and was not retrieved"
  303. path))))))
  304. (defun org-attach-commit ()
  305. "Commit changes to git if `org-attach-directory' is properly initialized.
  306. This checks for the existence of a \".git\" directory in that directory."
  307. (let* ((dir (expand-file-name org-attach-directory))
  308. (git-dir (vc-git-root dir))
  309. (use-annex (org-attach-use-annex))
  310. (changes 0))
  311. (when (and git-dir (executable-find "git"))
  312. (with-temp-buffer
  313. (cd dir)
  314. (dolist (new-or-modified
  315. (split-string
  316. (shell-command-to-string
  317. "git ls-files -zmo --exclude-standard") "\0" t))
  318. (if (and use-annex
  319. (>= (nth 7 (file-attributes new-or-modified))
  320. org-attach-git-annex-cutoff))
  321. (call-process "git" nil nil nil "annex" "add" new-or-modified)
  322. (call-process "git" nil nil nil "add" new-or-modified))
  323. (cl-incf changes))
  324. (dolist (deleted
  325. (split-string
  326. (shell-command-to-string "git ls-files -z --deleted") "\0" t))
  327. (call-process "git" nil nil nil "rm" deleted)
  328. (cl-incf changes))
  329. (when (> changes 0)
  330. (shell-command "git commit -m 'Synchronized attachments'"))))))
  331. (defun org-attach-tag (&optional off)
  332. "Turn the autotag on or (if OFF is set) off."
  333. (when org-attach-auto-tag
  334. (save-excursion
  335. (org-back-to-heading t)
  336. (org-toggle-tag org-attach-auto-tag (if off 'off 'on)))))
  337. (defun org-attach-untag ()
  338. "Turn the autotag off."
  339. (org-attach-tag 'off))
  340. (defun org-attach-store-link (file)
  341. "Add a link to `org-stored-link' when attaching a file.
  342. Only do this when `org-attach-store-link-p' is non-nil."
  343. (setq org-stored-links
  344. (cons (list (org-attach-expand-link file)
  345. (file-name-nondirectory file))
  346. org-stored-links)))
  347. (defun org-attach-url (url)
  348. (interactive "MURL of the file to attach: \n")
  349. (org-attach-attach url))
  350. (defun org-attach-attach (file &optional visit-dir method)
  351. "Move/copy/link FILE into the attachment directory of the current task.
  352. If VISIT-DIR is non-nil, visit the directory with dired.
  353. METHOD may be `cp', `mv', `ln', `lns' or `url' default taken from
  354. `org-attach-method'."
  355. (interactive
  356. (list
  357. (read-file-name "File to keep as an attachment:"
  358. (or (progn
  359. (require 'dired-aux)
  360. (dired-dwim-target-directory))
  361. default-directory))
  362. current-prefix-arg
  363. nil))
  364. (setq method (or method org-attach-method))
  365. (let ((basename (file-name-nondirectory file)))
  366. (when (and org-attach-file-list-property (not org-attach-inherited))
  367. (org-entry-add-to-multivalued-property
  368. (point) org-attach-file-list-property basename))
  369. (let* ((attach-dir (org-attach-dir t))
  370. (fname (expand-file-name basename attach-dir)))
  371. (cond
  372. ((eq method 'mv) (rename-file file fname))
  373. ((eq method 'cp) (copy-file file fname))
  374. ((eq method 'ln) (add-name-to-file file fname))
  375. ((eq method 'lns) (make-symbolic-link file fname))
  376. ((eq method 'url) (url-copy-file file fname)))
  377. (when org-attach-commit
  378. (org-attach-commit))
  379. (org-attach-tag)
  380. (cond ((eq org-attach-store-link-p 'attached)
  381. (org-attach-store-link fname))
  382. ((eq org-attach-store-link-p t)
  383. (org-attach-store-link file)))
  384. (if visit-dir
  385. (dired attach-dir)
  386. (message "File %S is now a task attachment." basename)))))
  387. (defun org-attach-attach-cp ()
  388. "Attach a file by copying it."
  389. (interactive)
  390. (let ((org-attach-method 'cp)) (call-interactively 'org-attach-attach)))
  391. (defun org-attach-attach-mv ()
  392. "Attach a file by moving (renaming) it."
  393. (interactive)
  394. (let ((org-attach-method 'mv)) (call-interactively 'org-attach-attach)))
  395. (defun org-attach-attach-ln ()
  396. "Attach a file by creating a hard link to it.
  397. Beware that this does not work on systems that do not support hard links.
  398. On some systems, this apparently does copy the file instead."
  399. (interactive)
  400. (let ((org-attach-method 'ln)) (call-interactively 'org-attach-attach)))
  401. (defun org-attach-attach-lns ()
  402. "Attach a file by creating a symbolic link to it.
  403. Beware that this does not work on systems that do not support symbolic links.
  404. On some systems, this apparently does copy the file instead."
  405. (interactive)
  406. (let ((org-attach-method 'lns)) (call-interactively 'org-attach-attach)))
  407. (defun org-attach-new (file)
  408. "Create a new attachment FILE for the current task.
  409. The attachment is created as an Emacs buffer."
  410. (interactive "sCreate attachment named: ")
  411. (when (and org-attach-file-list-property (not org-attach-inherited))
  412. (org-entry-add-to-multivalued-property
  413. (point) org-attach-file-list-property file))
  414. (let ((attach-dir (org-attach-dir t)))
  415. (org-attach-tag)
  416. (find-file (expand-file-name file attach-dir))
  417. (message "New attachment %s" file)))
  418. (defun org-attach-delete-one (&optional file)
  419. "Delete a single attachment."
  420. (interactive)
  421. (let* ((attach-dir (org-attach-dir t))
  422. (files (org-attach-file-list attach-dir))
  423. (file (or file
  424. (completing-read
  425. "Delete attachment: "
  426. (mapcar (lambda (f)
  427. (list (file-name-nondirectory f)))
  428. files)))))
  429. (setq file (expand-file-name file attach-dir))
  430. (unless (file-exists-p file)
  431. (error "No such attachment: %s" file))
  432. (delete-file file)
  433. (when org-attach-commit
  434. (org-attach-commit))))
  435. (defun org-attach-delete-all (&optional force)
  436. "Delete all attachments from the current task.
  437. This actually deletes the entire attachment directory.
  438. A safer way is to open the directory in dired and delete from there."
  439. (interactive "P")
  440. (when (and org-attach-file-list-property (not org-attach-inherited))
  441. (org-entry-delete (point) org-attach-file-list-property))
  442. (let ((attach-dir (org-attach-dir)))
  443. (when
  444. (and attach-dir
  445. (or force
  446. (y-or-n-p "Are you sure you want to remove all attachments of this entry? ")))
  447. (shell-command (format "rm -fr %s" attach-dir))
  448. (message "Attachment directory removed")
  449. (when org-attach-commit
  450. (org-attach-commit))
  451. (org-attach-untag))))
  452. (defun org-attach-sync ()
  453. "Synchronize the current tasks with its attachments.
  454. This can be used after files have been added externally."
  455. (interactive)
  456. (when org-attach-commit
  457. (org-attach-commit))
  458. (when (and org-attach-file-list-property (not org-attach-inherited))
  459. (org-entry-delete (point) org-attach-file-list-property))
  460. (let ((attach-dir (org-attach-dir)))
  461. (when attach-dir
  462. (let ((files (org-attach-file-list attach-dir)))
  463. (org-attach-tag (not files))
  464. (when org-attach-file-list-property
  465. (dolist (file files)
  466. (unless (string-match "^\\.\\.?\\'" file)
  467. (org-entry-add-to-multivalued-property
  468. (point) org-attach-file-list-property file))))))))
  469. (defun org-attach-file-list (dir)
  470. "Return a list of files in the attachment directory.
  471. This ignores files ending in \"~\"."
  472. (delq nil
  473. (mapcar (lambda (x) (if (string-match "^\\.\\.?\\'" x) nil x))
  474. (directory-files dir nil "[^~]\\'"))))
  475. (defun org-attach-reveal (&optional if-exists)
  476. "Show the attachment directory of the current task.
  477. This will attempt to use an external program to show the directory."
  478. (interactive "P")
  479. (let ((attach-dir (org-attach-dir (not if-exists))))
  480. (and attach-dir (org-open-file attach-dir))))
  481. (defun org-attach-reveal-in-emacs ()
  482. "Show the attachment directory of the current task in dired."
  483. (interactive)
  484. (let ((attach-dir (org-attach-dir t)))
  485. (dired attach-dir)))
  486. (defun org-attach-open (&optional in-emacs)
  487. "Open an attachment of the current task.
  488. If there are more than one attachment, you will be prompted for the file name.
  489. This command will open the file using the settings in `org-file-apps'
  490. and in the system-specific variants of this variable.
  491. If IN-EMACS is non-nil, force opening in Emacs."
  492. (interactive "P")
  493. (let* ((attach-dir (org-attach-dir t))
  494. (files (org-attach-file-list attach-dir))
  495. (file (if (= (length files) 1)
  496. (car files)
  497. (completing-read "Open attachment: "
  498. (mapcar #'list files) nil t)))
  499. (path (expand-file-name file attach-dir)))
  500. (org-attach-annex-get-maybe path)
  501. (org-open-file path in-emacs)))
  502. (defun org-attach-open-in-emacs ()
  503. "Open attachment, force opening in Emacs.
  504. See `org-attach-open'."
  505. (interactive)
  506. (org-attach-open 'in-emacs))
  507. (defun org-attach-expand (file)
  508. "Return the full path to the current entry's attachment file FILE.
  509. Basically, this adds the path to the attachment directory."
  510. (expand-file-name file (org-attach-dir)))
  511. (defun org-attach-expand-link (file)
  512. "Return a file link pointing to the current entry's attachment file FILE.
  513. Basically, this adds the path to the attachment directory, and a \"file:\"
  514. prefix."
  515. (concat "file:" (org-attach-expand file)))
  516. (defun org-attach-archive-delete-maybe ()
  517. "Maybe delete subtree attachments when archiving.
  518. This function is called by `org-archive-hook'. The option
  519. `org-attach-archive-delete' controls its behavior."
  520. (when (if (eq org-attach-archive-delete 'query)
  521. (yes-or-no-p "Delete all attachments? ")
  522. org-attach-archive-delete)
  523. (org-attach-delete-all t)))
  524. (add-hook 'org-archive-hook 'org-attach-archive-delete-maybe)
  525. (provide 'org-attach)
  526. ;; Local variables:
  527. ;; generated-autoload-file: "org-loaddefs.el"
  528. ;; End:
  529. ;;; org-attach.el ends here