org-attach.el 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. ;;; org-attach.el --- Manage file attachments to Org tasks -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2008-2019 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. b Select a buffer and attach its contents to the task.
  155. n Create a new attachment, as an Emacs buffer.
  156. z Synchronize the current task with its attachment
  157. directory, in case you added attachments yourself.
  158. o Open current task's attachments.
  159. O Like \"o\", but force opening in Emacs.
  160. f Open current task's attachment directory.
  161. F Like \"f\", but force using dired in Emacs.
  162. d Delete one attachment, you will be prompted for a file name.
  163. D Delete all of a task's attachments. A safer way is
  164. to open the directory in dired and delete from there.
  165. s Set a specific attachment directory for this entry or reset to default.
  166. i Make children of the current entry inherit its attachment directory.")))
  167. (org-fit-window-to-buffer (get-buffer-window "*Org Attach*"))
  168. (message "Select command: [acmlyubnzoOfFdD]")
  169. (setq c (read-char-exclusive))
  170. (and (get-buffer "*Org Attach*") (kill-buffer "*Org Attach*"))))
  171. (cond
  172. ((memq c '(?a ?\C-a)) (call-interactively 'org-attach-attach))
  173. ((memq c '(?c ?\C-c))
  174. (let ((org-attach-method 'cp)) (call-interactively 'org-attach-attach)))
  175. ((memq c '(?m ?\C-m))
  176. (let ((org-attach-method 'mv)) (call-interactively 'org-attach-attach)))
  177. ((memq c '(?l ?\C-l))
  178. (let ((org-attach-method 'ln)) (call-interactively 'org-attach-attach)))
  179. ((memq c '(?y ?\C-y))
  180. (let ((org-attach-method 'lns)) (call-interactively 'org-attach-attach)))
  181. ((memq c '(?u ?\C-u))
  182. (let ((org-attach-method 'url)) (call-interactively 'org-attach-url)))
  183. ((eq c ?b) (call-interactively 'org-attach-buffer))
  184. ((memq c '(?n ?\C-n)) (call-interactively 'org-attach-new))
  185. ((memq c '(?z ?\C-z)) (call-interactively 'org-attach-sync))
  186. ((memq c '(?o ?\C-o)) (call-interactively 'org-attach-open))
  187. ((eq c ?O) (call-interactively 'org-attach-open-in-emacs))
  188. ((memq c '(?f ?\C-f)) (call-interactively 'org-attach-reveal))
  189. ((memq c '(?F)) (call-interactively 'org-attach-reveal-in-emacs))
  190. ((memq c '(?d ?\C-d)) (call-interactively
  191. 'org-attach-delete-one))
  192. ((eq c ?D) (call-interactively 'org-attach-delete-all))
  193. ((eq c ?q) (message "Abort"))
  194. ((memq c '(?s ?\C-s)) (call-interactively
  195. 'org-attach-set-directory))
  196. ((memq c '(?i ?\C-i)) (call-interactively
  197. 'org-attach-set-inherit))
  198. (t (error "No such attachment command %c" c))))))
  199. (defun org-attach-dir (&optional create-if-not-exists-p)
  200. "Return the directory associated with the current entry.
  201. This first checks for a local property ATTACH_DIR, and then for an inherited
  202. property ATTACH_DIR_INHERIT. If neither exists, the default mechanism
  203. using the entry ID will be invoked to access the unique directory for the
  204. current entry.
  205. If the directory does not exist and CREATE-IF-NOT-EXISTS-P is non-nil,
  206. the directory and (if necessary) the corresponding ID will be created."
  207. (let (attach-dir uuid)
  208. (setq org-attach-inherited (org-entry-get nil "ATTACH_DIR_INHERIT"))
  209. (cond
  210. ((setq attach-dir (org-entry-get nil "ATTACH_DIR"))
  211. (org-attach-check-absolute-path attach-dir))
  212. ((and org-attach-allow-inheritance
  213. (org-entry-get nil "ATTACH_DIR_INHERIT" t))
  214. (setq attach-dir
  215. (org-with-wide-buffer
  216. (if (marker-position org-entry-property-inherited-from)
  217. (goto-char org-entry-property-inherited-from)
  218. (org-back-to-heading t))
  219. (let (org-attach-allow-inheritance)
  220. (org-attach-dir create-if-not-exists-p))))
  221. (org-attach-check-absolute-path attach-dir)
  222. (setq org-attach-inherited t))
  223. (t ; use the ID
  224. (org-attach-check-absolute-path nil)
  225. (setq uuid (org-id-get (point) create-if-not-exists-p))
  226. (when (or uuid create-if-not-exists-p)
  227. (unless uuid (error "ID retrieval/creation failed"))
  228. (setq attach-dir (expand-file-name
  229. (format "%s/%s"
  230. (substring uuid 0 2)
  231. (substring uuid 2))
  232. (expand-file-name org-attach-directory))))))
  233. (when attach-dir
  234. (if (and create-if-not-exists-p
  235. (not (file-directory-p attach-dir)))
  236. (make-directory attach-dir t))
  237. (and (file-exists-p attach-dir)
  238. attach-dir))))
  239. (defun org-attach-check-absolute-path (dir)
  240. "Check if we have enough information to root the attachment directory.
  241. When DIR is given, check also if it is already absolute. Otherwise,
  242. assume that it will be relative, and check if `org-attach-directory' is
  243. absolute, or if at least the current buffer has a file name.
  244. Throw an error if we cannot root the directory."
  245. (or (and dir (file-name-absolute-p dir))
  246. (file-name-absolute-p org-attach-directory)
  247. (buffer-file-name (buffer-base-buffer))
  248. (error "Need absolute `org-attach-directory' to attach in buffers without filename")))
  249. (defun org-attach-set-directory (&optional arg)
  250. "Set the ATTACH_DIR node property and ask to move files there.
  251. The property defines the directory that is used for attachments
  252. of the entry. When called with `\\[universal-argument]', reset \
  253. the directory to
  254. the default ID based one."
  255. (interactive "P")
  256. (let ((old (org-attach-dir))
  257. (new
  258. (progn
  259. (if arg (org-entry-delete nil "ATTACH_DIR")
  260. (let ((dir (read-directory-name
  261. "Attachment directory: "
  262. (org-entry-get nil
  263. "ATTACH_DIR"
  264. (and org-attach-allow-inheritance t)))))
  265. (org-entry-put nil "ATTACH_DIR" dir)))
  266. (org-attach-dir t))))
  267. (unless (or (string= old new)
  268. (not old))
  269. (when (yes-or-no-p "Copy over attachments from old directory? ")
  270. (copy-directory old new t nil t))
  271. (when (yes-or-no-p (concat "Delete " old))
  272. (delete-directory old t)))))
  273. (defun org-attach-set-inherit ()
  274. "Set the ATTACH_DIR_INHERIT property of the current entry.
  275. The property defines the directory that is used for attachments
  276. of the entry and any children that do not explicitly define (by setting
  277. the ATTACH_DIR property) their own attachment directory."
  278. (interactive)
  279. (org-entry-put nil "ATTACH_DIR_INHERIT" "t")
  280. (message "Children will inherit attachment directory"))
  281. (defun org-attach-use-annex ()
  282. "Return non-nil if git annex can be used."
  283. (let ((git-dir (vc-git-root (expand-file-name org-attach-directory))))
  284. (and org-attach-git-annex-cutoff
  285. (or (file-exists-p (expand-file-name "annex" git-dir))
  286. (file-exists-p (expand-file-name ".git/annex" git-dir))))))
  287. (defun org-attach-annex-get-maybe (path)
  288. "Call git annex get PATH (via shell) if using git annex.
  289. Signals an error if the file content is not available and it was not retrieved."
  290. (let* ((default-directory (expand-file-name org-attach-directory))
  291. (path-relative (file-relative-name path)))
  292. (when (and (org-attach-use-annex)
  293. (not
  294. (string-equal
  295. "found"
  296. (shell-command-to-string
  297. (format "git annex find --format=found --in=here %s"
  298. (shell-quote-argument path-relative))))))
  299. (let ((should-get
  300. (if (eq org-attach-annex-auto-get 'ask)
  301. (y-or-n-p (format "Run git annex get %s? " path-relative))
  302. org-attach-annex-auto-get)))
  303. (if should-get
  304. (progn (message "Running git annex get \"%s\"." path-relative)
  305. (call-process "git" nil nil nil "annex" "get" path-relative))
  306. (error "File %s stored in git annex but it is not available, and was not retrieved"
  307. path))))))
  308. (defun org-attach-commit ()
  309. "Commit changes to git if `org-attach-directory' is properly initialized.
  310. This checks for the existence of a \".git\" directory in that directory."
  311. (let* ((dir (expand-file-name org-attach-directory))
  312. (git-dir (vc-git-root dir))
  313. (use-annex (org-attach-use-annex))
  314. (changes 0))
  315. (when (and git-dir (executable-find "git"))
  316. (with-temp-buffer
  317. (cd dir)
  318. (dolist (new-or-modified
  319. (split-string
  320. (shell-command-to-string
  321. "git ls-files -zmo --exclude-standard") "\0" t))
  322. (if (and use-annex
  323. (>= (file-attribute-size (file-attributes new-or-modified))
  324. org-attach-git-annex-cutoff))
  325. (call-process "git" nil nil nil "annex" "add" new-or-modified)
  326. (call-process "git" nil nil nil "add" new-or-modified))
  327. (cl-incf changes))
  328. (dolist (deleted
  329. (split-string
  330. (shell-command-to-string "git ls-files -z --deleted") "\0" t))
  331. (call-process "git" nil nil nil "rm" deleted)
  332. (cl-incf changes))
  333. (when (> changes 0)
  334. (shell-command "git commit -m 'Synchronized attachments'"))))))
  335. (defun org-attach-tag (&optional off)
  336. "Turn the autotag on or (if OFF is set) off."
  337. (when org-attach-auto-tag
  338. (save-excursion
  339. (org-back-to-heading t)
  340. (org-toggle-tag org-attach-auto-tag (if off 'off 'on)))))
  341. (defun org-attach-untag ()
  342. "Turn the autotag off."
  343. (org-attach-tag 'off))
  344. (defun org-attach-store-link (file)
  345. "Add a link to `org-stored-link' when attaching a file.
  346. Only do this when `org-attach-store-link-p' is non-nil."
  347. (setq org-stored-links
  348. (cons (list (org-attach-expand-link file)
  349. (file-name-nondirectory file))
  350. org-stored-links)))
  351. (defun org-attach-url (url)
  352. (interactive "MURL of the file to attach: \n")
  353. (org-attach-attach url))
  354. (defun org-attach-buffer (buffer-name)
  355. "Attach BUFFER-NAME's contents to current task.
  356. BUFFER-NAME is a string. Signals a `file-already-exists' error
  357. if it would overwrite an existing filename."
  358. (interactive "bBuffer whose contents should be attached: ")
  359. (let ((output (expand-file-name buffer-name (org-attach-dir t))))
  360. (when (file-exists-p output)
  361. (signal 'file-already-exists (list "File exists" output)))
  362. (when (and org-attach-file-list-property (not org-attach-inherited))
  363. (org-entry-add-to-multivalued-property
  364. (point) org-attach-file-list-property buffer-name))
  365. (org-attach-tag)
  366. (with-temp-file output
  367. (insert-buffer-substring buffer-name))))
  368. (defun org-attach-attach (file &optional visit-dir method)
  369. "Move/copy/link FILE into the attachment directory of the current task.
  370. If VISIT-DIR is non-nil, visit the directory with dired.
  371. METHOD may be `cp', `mv', `ln', `lns' or `url' default taken from
  372. `org-attach-method'."
  373. (interactive
  374. (list
  375. (read-file-name "File to keep as an attachment:"
  376. (or (progn
  377. (require 'dired-aux)
  378. (dired-dwim-target-directory))
  379. default-directory))
  380. current-prefix-arg
  381. nil))
  382. (setq method (or method org-attach-method))
  383. (let ((basename (file-name-nondirectory file)))
  384. (when (and org-attach-file-list-property (not org-attach-inherited))
  385. (org-entry-add-to-multivalued-property
  386. (point) org-attach-file-list-property basename))
  387. (let* ((attach-dir (org-attach-dir t))
  388. (fname (expand-file-name basename attach-dir)))
  389. (cond
  390. ((eq method 'mv) (rename-file file fname))
  391. ((eq method 'cp) (copy-file file fname))
  392. ((eq method 'ln) (add-name-to-file file fname))
  393. ((eq method 'lns) (make-symbolic-link file fname))
  394. ((eq method 'url) (url-copy-file file fname)))
  395. (when org-attach-commit
  396. (org-attach-commit))
  397. (org-attach-tag)
  398. (cond ((eq org-attach-store-link-p 'attached)
  399. (org-attach-store-link fname))
  400. ((eq org-attach-store-link-p t)
  401. (org-attach-store-link file)))
  402. (if visit-dir
  403. (dired attach-dir)
  404. (message "File %S is now a task attachment." basename)))))
  405. (defun org-attach-attach-cp ()
  406. "Attach a file by copying it."
  407. (interactive)
  408. (let ((org-attach-method 'cp)) (call-interactively 'org-attach-attach)))
  409. (defun org-attach-attach-mv ()
  410. "Attach a file by moving (renaming) it."
  411. (interactive)
  412. (let ((org-attach-method 'mv)) (call-interactively 'org-attach-attach)))
  413. (defun org-attach-attach-ln ()
  414. "Attach a file by creating a hard link to it.
  415. Beware that this does not work on systems that do not support hard links.
  416. On some systems, this apparently does copy the file instead."
  417. (interactive)
  418. (let ((org-attach-method 'ln)) (call-interactively 'org-attach-attach)))
  419. (defun org-attach-attach-lns ()
  420. "Attach a file by creating a symbolic link to it.
  421. Beware that this does not work on systems that do not support symbolic links.
  422. On some systems, this apparently does copy the file instead."
  423. (interactive)
  424. (let ((org-attach-method 'lns)) (call-interactively 'org-attach-attach)))
  425. (defun org-attach-new (file)
  426. "Create a new attachment FILE for the current task.
  427. The attachment is created as an Emacs buffer."
  428. (interactive "sCreate attachment named: ")
  429. (when (and org-attach-file-list-property (not org-attach-inherited))
  430. (org-entry-add-to-multivalued-property
  431. (point) org-attach-file-list-property file))
  432. (let ((attach-dir (org-attach-dir t)))
  433. (org-attach-tag)
  434. (find-file (expand-file-name file attach-dir))
  435. (message "New attachment %s" file)))
  436. (defun org-attach-delete-one (&optional file)
  437. "Delete a single attachment."
  438. (interactive)
  439. (let* ((attach-dir (org-attach-dir t))
  440. (files (org-attach-file-list attach-dir))
  441. (file (or file
  442. (completing-read
  443. "Delete attachment: "
  444. (mapcar (lambda (f)
  445. (list (file-name-nondirectory f)))
  446. files)))))
  447. (setq file (expand-file-name file attach-dir))
  448. (unless (file-exists-p file)
  449. (error "No such attachment: %s" file))
  450. (delete-file file)
  451. (when org-attach-commit
  452. (org-attach-commit))))
  453. (defun org-attach-delete-all (&optional force)
  454. "Delete all attachments from the current task.
  455. This actually deletes the entire attachment directory.
  456. A safer way is to open the directory in dired and delete from there."
  457. (interactive "P")
  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
  462. (and attach-dir
  463. (or force
  464. (y-or-n-p "Are you sure you want to remove all attachments of this entry? ")))
  465. (shell-command (format "rm -fr %s" attach-dir))
  466. (message "Attachment directory removed")
  467. (when org-attach-commit
  468. (org-attach-commit))
  469. (org-attach-untag))))
  470. (defun org-attach-sync ()
  471. "Synchronize the current tasks with its attachments.
  472. This can be used after files have been added externally."
  473. (interactive)
  474. (when org-attach-commit
  475. (org-attach-commit))
  476. (when (and org-attach-file-list-property (not org-attach-inherited))
  477. (org-entry-delete (point) org-attach-file-list-property))
  478. (let ((attach-dir (org-attach-dir)))
  479. (when attach-dir
  480. (let ((files (org-attach-file-list attach-dir)))
  481. (org-attach-tag (not files))
  482. (when org-attach-file-list-property
  483. (dolist (file files)
  484. (unless (string-match "^\\.\\.?\\'" file)
  485. (org-entry-add-to-multivalued-property
  486. (point) org-attach-file-list-property file))))))))
  487. (defun org-attach-file-list (dir)
  488. "Return a list of files in the attachment directory.
  489. This ignores files ending in \"~\"."
  490. (delq nil
  491. (mapcar (lambda (x) (if (string-match "^\\.\\.?\\'" x) nil x))
  492. (directory-files dir nil "[^~]\\'"))))
  493. (defun org-attach-reveal (&optional if-exists)
  494. "Show the attachment directory of the current task.
  495. This will attempt to use an external program to show the directory."
  496. (interactive "P")
  497. (let ((attach-dir (org-attach-dir (not if-exists))))
  498. (and attach-dir (org-open-file attach-dir))))
  499. (defun org-attach-reveal-in-emacs ()
  500. "Show the attachment directory of the current task in dired."
  501. (interactive)
  502. (let ((attach-dir (org-attach-dir t)))
  503. (dired attach-dir)))
  504. (defun org-attach-open (&optional in-emacs)
  505. "Open an attachment of the current task.
  506. If there are more than one attachment, you will be prompted for the file name.
  507. This command will open the file using the settings in `org-file-apps'
  508. and in the system-specific variants of this variable.
  509. If IN-EMACS is non-nil, force opening in Emacs."
  510. (interactive "P")
  511. (let* ((attach-dir (org-attach-dir t))
  512. (files (org-attach-file-list attach-dir))
  513. (file (if (= (length files) 1)
  514. (car files)
  515. (completing-read "Open attachment: "
  516. (mapcar #'list files) nil t)))
  517. (path (expand-file-name file attach-dir)))
  518. (org-attach-annex-get-maybe path)
  519. (org-open-file path in-emacs)))
  520. (defun org-attach-open-in-emacs ()
  521. "Open attachment, force opening in Emacs.
  522. See `org-attach-open'."
  523. (interactive)
  524. (org-attach-open 'in-emacs))
  525. (defun org-attach-expand (file)
  526. "Return the full path to the current entry's attachment file FILE.
  527. Basically, this adds the path to the attachment directory."
  528. (expand-file-name file (org-attach-dir)))
  529. (defun org-attach-expand-link (file)
  530. "Return a file link pointing to the current entry's attachment file FILE.
  531. Basically, this adds the path to the attachment directory, and a \"file:\"
  532. prefix."
  533. (concat "file:" (org-attach-expand file)))
  534. (defun org-attach-archive-delete-maybe ()
  535. "Maybe delete subtree attachments when archiving.
  536. This function is called by `org-archive-hook'. The option
  537. `org-attach-archive-delete' controls its behavior."
  538. (when org-attach-archive-delete
  539. (org-attach-delete-all (not (eq org-attach-archive-delete 'query)))))
  540. ;; Attach from dired.
  541. ;; Add the following lines to the config file to get a binding for
  542. ;; dired-mode.
  543. ;; (add-hook
  544. ;; 'dired-mode-hook
  545. ;; (lambda ()
  546. ;; (define-key dired-mode-map (kbd "C-c C-x a") #'org-attach-dired-to-subtree))))
  547. ;;;###autoload
  548. (defun org-attach-dired-to-subtree (files)
  549. "Attach FILES marked or current file in dired to subtree in other window.
  550. Takes the method given in `org-attach-method' for the attach action.
  551. Precondition: Point must be in a dired buffer.
  552. Idea taken from `gnus-dired-attach'."
  553. (interactive
  554. (list (dired-get-marked-files)))
  555. (unless (eq major-mode 'dired-mode)
  556. (user-error "This command must be triggered in a dired buffer."))
  557. (let ((start-win (selected-window))
  558. (other-win
  559. (get-window-with-predicate
  560. (lambda (window)
  561. (with-current-buffer (window-buffer window)
  562. (eq major-mode 'org-mode))))))
  563. (unless other-win
  564. (user-error
  565. "Can't attach to subtree. No window displaying an Org buffer"))
  566. (select-window other-win)
  567. (dolist (file files)
  568. (org-attach-attach file))
  569. (select-window start-win)
  570. (when (eq 'mv org-attach-method)
  571. (revert-buffer))))
  572. (add-hook 'org-archive-hook 'org-attach-archive-delete-maybe)
  573. (provide 'org-attach)
  574. ;; Local variables:
  575. ;; generated-autoload-file: "org-loaddefs.el"
  576. ;; End:
  577. ;;; org-attach.el ends here