org-attach.el 25 KB

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