org-attach.el 24 KB

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