org-attach.el 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  1. ;;; org-attach.el --- Manage file attachments to Org outlines -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2008-2019 Free Software Foundation, Inc.
  3. ;; Author: John Wiegley <johnw@newartisans.com>
  4. ;; Keywords: org data attachment
  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 either by using a custom property DIR or by
  21. ;; using property ID from org-id. When DIR is defined, a location in
  22. ;; the filesystem is directly attached to the outline node. When
  23. ;; org-id is used, attachments are stored in a folder named after the
  24. ;; ID, in a location defined by `org-attach-id-dir'. DIR has
  25. ;; precedence over ID when both parameters are defined for the current
  26. ;; outline node (also when inherited parameters are taken into
  27. ;; account).
  28. ;;; Code:
  29. (require 'cl-lib)
  30. (require 'org)
  31. (require 'ol)
  32. (require 'org-id)
  33. (declare-function dired-dwim-target-directory "dired-aux")
  34. (defgroup org-attach nil
  35. "Options concerning attachments in Org mode."
  36. :tag "Org Attach"
  37. :group 'org)
  38. (defcustom org-attach-id-dir "data/"
  39. "The directory where attachments are stored.
  40. If this is a relative path, it will be interpreted relative to the directory
  41. where the Org file lives."
  42. :group 'org-attach
  43. :type 'directory
  44. :safe #'stringp)
  45. (defcustom org-attach-dir-relative nil
  46. "Non-nil means directories in DIR property are added as relative links.
  47. Defaults to absolute location."
  48. :group 'org-attach
  49. :type 'boolean
  50. :package-version '(Org . "9.3")
  51. :safe #'booleanp)
  52. (defcustom org-attach-auto-tag "ATTACH"
  53. "Tag that will be triggered automatically when an entry has an attachment."
  54. :group 'org-attach
  55. :type '(choice
  56. (const :tag "None" nil)
  57. (string :tag "Tag")))
  58. (defcustom org-attach-preferred-new-method 'id
  59. "Preferred way to attach to nodes without existing ID and DIR property.
  60. This choice is used when adding attachments to nodes without ID
  61. and DIR properties.
  62. Allowed values are:
  63. id Create and use an ID parameter
  64. dir Create and use a DIR parameter
  65. ask Ask the user for input of which method to choose
  66. nil Prefer to not create a new parameter
  67. nil means that ID or DIR has to be created explicitly
  68. before attaching files."
  69. :group 'org-attach
  70. :package-version '(org . "9.3")
  71. :type '(choice
  72. (const :tag "ID parameter" id)
  73. (const :tag "DIR parameter" dir)
  74. (const :tag "Ask user" ask)
  75. (const :tag "Don't create" nil)))
  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-use-inheritance 'selective
  96. "Attachment inheritance for the outline.
  97. Enabling inheritance for org-attach implies two things. First,
  98. that attachment links will look through all parent headings until
  99. it finds the linked attachment. Second, that running org-attach
  100. inside a node without attachments will make org-attach operate on
  101. the first parent heading it finds with an attachment.
  102. Selective means to respect the inheritance setting in
  103. `org-use-property-inheritance'."
  104. :group 'org-attach
  105. :type '(choice
  106. (const :tag "Don't use inheritance" nil)
  107. (const :tag "Inherit parent node attachments" t)
  108. (const :tag "Respect org-use-property-inheritance" selective))
  109. :type 'boolean)
  110. (defcustom org-attach-store-link-p nil
  111. "Non-nil means store a link to a file when attaching it."
  112. :group 'org-attach
  113. :version "24.1"
  114. :type '(choice
  115. (const :tag "Don't store link" nil)
  116. (const :tag "Link to origin location" t)
  117. (const :tag "Link to the attach-dir location" attached)))
  118. (defcustom org-attach-archive-delete nil
  119. "Non-nil means attachments are deleted upon archiving a subtree.
  120. When set to `query', ask the user instead."
  121. :group 'org-attach
  122. :version "26.1"
  123. :package-version '(Org . "8.3")
  124. :type '(choice
  125. (const :tag "Never delete attachments" nil)
  126. (const :tag "Always delete attachments" t)
  127. (const :tag "Query the user" query)))
  128. (defun org-attach-id-folder-format (id)
  129. "Translate an ID into a folder-path.
  130. Default format for how Org translates ID properties to a path for
  131. attachments."
  132. (format "%s/%s"
  133. (substring id 0 2)
  134. (substring id 2)))
  135. (defcustom org-attach-id-to-path-function #'org-attach-id-folder-format
  136. "Function parsing the ID parameter into a folder-path."
  137. :group 'org-attach
  138. :package-version '(Org . "9.3")
  139. :type 'function)
  140. (defvar org-attach-after-change-hook nil
  141. "Hook to be called when files have been added or removed to the attachment folder.")
  142. (defvar org-attach-open-hook nil
  143. "Hook that is invoked by `org-attach-open'.
  144. Created mostly to be compatible with org-attach-git after removing
  145. git-funtionality from this file.")
  146. (defcustom org-attach-commands
  147. '(((?a ?\C-a) org-attach-attach
  148. "Select a file and attach it to the task, using `org-attach-method'.")
  149. ((?c ?\C-c) org-attach-attach-cp
  150. "Attach a file using copy method.")
  151. ((?m ?\C-m) org-attach-attach-mv
  152. "Attach a file using move method.")
  153. ((?l ?\C-l) org-attach-attach-ln
  154. "Attach a file using link method.")
  155. ((?y ?\C-y) org-attach-attach-lns
  156. "Attach a file using symbolic-link method.")
  157. ((?u ?\C-u) org-attach-url
  158. "Attach a file from URL (downloading it).")
  159. ((?b) org-attach-buffer
  160. "Select a buffer and attach its contents to the task.")
  161. ((?n ?\C-n) org-attach-new
  162. "Create a new attachment, as an Emacs buffer.")
  163. ((?z ?\C-z) org-attach-sync
  164. "Synchronize the current task with its attachment\n directory, in case \
  165. you added attachments yourself.\n")
  166. ((?o ?\C-o) org-attach-open
  167. "Open current task's attachments.")
  168. ((?O) org-attach-open-in-emacs
  169. "Like \"o\", but force opening in Emacs.")
  170. ((?f ?\C-f) org-attach-reveal
  171. "Open current task's attachment directory.")
  172. ((?F) org-attach-reveal-in-emacs
  173. "Like \"f\", but force using Dired in Emacs.\n")
  174. ((?d ?\C-d) org-attach-delete-one
  175. "Delete one attachment, you will be prompted for a file name.")
  176. ((?D) org-attach-delete-all
  177. "Delete all of a task's attachments. A safer way is\n to open the \
  178. directory in dired and delete from there.\n")
  179. ((?s ?\C-s) org-attach-set-directory
  180. "Set a specific attachment directory for this entry. Sets DIR property.")
  181. ((?S ?\C-S) org-attach-unset-directory
  182. "Unset the attachment directory for this entry. Removes DIR property.")
  183. ((?q) (lambda () (interactive) (message "Abort")) "Abort."))
  184. "The list of commands for the attachment dispatcher.
  185. Each entry in this list is a list of three elements:
  186. - A list of keys (characters) to select the command (the fist
  187. character in the list is shown in the attachment dispatcher's
  188. splash buffer and minubuffer prompt).
  189. - A command that is called interactively when one of these keys
  190. is pressed.
  191. - A docstring for this command in the attachment dispatcher's
  192. splash buffer."
  193. :group 'org-attach
  194. :package-version '(Org . "9.3")
  195. :type '(repeat (list (repeat :tag "Keys" character)
  196. (function :tag "Command")
  197. (string :tag "Docstring"))))
  198. ;;;###autoload
  199. (defun org-attach ()
  200. "The dispatcher for attachment commands.
  201. Shows a list of commands and prompts for another key to execute a command."
  202. (interactive)
  203. (let (c marker)
  204. (when (eq major-mode 'org-agenda-mode)
  205. (setq marker (or (get-text-property (point) 'org-hd-marker)
  206. (get-text-property (point) 'org-marker)))
  207. (unless marker
  208. (error "No item in current line")))
  209. (save-excursion
  210. (when marker
  211. (set-buffer (marker-buffer marker))
  212. (goto-char marker))
  213. (org-back-to-heading t)
  214. (save-excursion
  215. (save-window-excursion
  216. (unless org-attach-expert
  217. (with-output-to-temp-buffer "*Org Attach*"
  218. (princ
  219. (concat "Attachment folder:\n"
  220. (or (org-attach-dir)
  221. "Can't find an existing attachment-folder")
  222. "\n\n"
  223. (format "Select an Attachment Command:\n\n%s"
  224. (mapconcat
  225. (lambda (entry)
  226. (pcase entry
  227. (`((,key . ,_) ,_ ,docstring)
  228. (format "%c %s"
  229. key
  230. (replace-regexp-in-string "\n\\([\t ]*\\)"
  231. " "
  232. docstring
  233. nil nil 1)))
  234. (_
  235. (user-error
  236. "Invalid `org-attach-commands' item: %S"
  237. entry))))
  238. org-attach-commands
  239. "\n"))))))
  240. (org-fit-window-to-buffer (get-buffer-window "*Org Attach*"))
  241. (message "Select command: [%s]"
  242. (concat (mapcar #'caar org-attach-commands)))
  243. (setq c (read-char-exclusive))
  244. (and (get-buffer "*Org Attach*") (kill-buffer "*Org Attach*"))))
  245. (let ((command (cl-some (lambda (entry)
  246. (and (memq c (nth 0 entry)) (nth 1 entry)))
  247. org-attach-commands)))
  248. (if (commandp command t)
  249. (call-interactively command)
  250. (error "No such attachment command: %c" c))))))
  251. (defun org-attach-dir (&optional create-if-not-exists-p)
  252. "Return the directory associated with the current outline node.
  253. First check for DIR property, then ID property.
  254. `org-attach-use-inheritance' determines whether inherited
  255. properties also will be considered.
  256. If an ID property is found the default mechanism using that ID
  257. will be invoked to access the directory for the current entry.
  258. If CREATE-IF-NOT-EXIST-P is non-nil, `org-attach-dir-get-create'
  259. is run."
  260. (let (attach-dir id)
  261. (cond
  262. (create-if-not-exists-p
  263. (setq attach-dir (org-attach-dir-get-create)))
  264. ((setq attach-dir (org-entry-get nil "DIR" org-attach-use-inheritance))
  265. (org-attach-check-absolute-path attach-dir))
  266. ;; Deprecated and removed from documentation, but still
  267. ;; works. FIXME: Remove after major nr change.
  268. ((setq attach-dir (org-entry-get nil "ATTACH_DIR" org-attach-use-inheritance))
  269. (org-attach-check-absolute-path attach-dir))
  270. ((setq id (org-entry-get nil "ID" org-attach-use-inheritance))
  271. (org-attach-check-absolute-path nil)
  272. (setq attach-dir (org-attach-dir-from-id id))))
  273. attach-dir))
  274. (defun org-attach-dir-get-create ()
  275. "Return existing or new directory associated with the current outline node.
  276. `org-attach-preferred-new-method' decides how to attach
  277. new directory."
  278. (interactive)
  279. (let ((attach-dir (org-attach-dir)))
  280. (unless attach-dir
  281. (let (answer)
  282. (when (eq org-attach-preferred-new-method 'ask)
  283. (message "Create new ID [1] property or DIR [2] property for attachments?")
  284. (setq answer (read-char-exclusive)))
  285. (cond
  286. ((or (eq org-attach-preferred-new-method 'id) (eq answer ?1))
  287. (setq attach-dir (org-attach-dir-from-id (org-id-get nil t))))
  288. ((or (eq org-attach-preferred-new-method 'dir) (eq answer ?2))
  289. (setq attach-dir (org-attach-set-directory)))
  290. ((eq org-attach-preferred-new-method 'nil)
  291. (error "No existing directory. DIR or ID property has to be explicitly created")))))
  292. (unless attach-dir
  293. (error "No attachment directory is associated with the current node"))
  294. (unless (file-directory-p attach-dir)
  295. (make-directory attach-dir t))
  296. attach-dir))
  297. (defun org-attach-dir-from-id (id)
  298. "Returns a file name based on `org-attach-id-dir' and ID."
  299. (expand-file-name
  300. (funcall org-attach-id-to-path-function id)
  301. (expand-file-name org-attach-id-dir)))
  302. (defun org-attach-check-absolute-path (dir)
  303. "Check if we have enough information to root the attachment directory.
  304. When DIR is given, check also if it is already absolute. Otherwise,
  305. assume that it will be relative, and check if `org-attach-id-dir' is
  306. absolute, or if at least the current buffer has a file name.
  307. Throw an error if we cannot root the directory."
  308. (or (and dir (file-name-absolute-p dir))
  309. (file-name-absolute-p org-attach-id-dir)
  310. (buffer-file-name (buffer-base-buffer))
  311. (error "Need absolute `org-attach-id-dir' to attach in buffers without filename")))
  312. (defun org-attach-set-directory ()
  313. "Set the DIR node property and ask to move files there.
  314. The property defines the directory that is used for attachments
  315. of the entry. Creates relative links if `org-attach-dir-relative'
  316. is non-nil.
  317. Return the directory."
  318. (interactive)
  319. (let ((old (org-attach-dir))
  320. (new
  321. (let* ((attach-dir (read-directory-name
  322. "Attachment directory: "
  323. (org-entry-get nil "DIR")))
  324. (current-dir (file-name-directory (or default-directory
  325. buffer-file-name)))
  326. (attach-dir-relative (file-relative-name attach-dir current-dir)))
  327. (org-entry-put nil "DIR" (if org-attach-dir-relative
  328. attach-dir-relative
  329. attach-dir))
  330. attach-dir)))
  331. (unless (or (string= old new)
  332. (not old))
  333. (when (yes-or-no-p "Copy over attachments from old directory? ")
  334. (copy-directory old new t t t))
  335. (when (yes-or-no-p (concat "Delete " old))
  336. (delete-directory old t)))
  337. new))
  338. (defun org-attach-unset-directory ()
  339. "Removes DIR node property.
  340. If attachment folder is changed due to removal of DIR-property
  341. ask to move attachments to new location and ask to delete old
  342. attachment-folder.
  343. Change of attachment-folder due to unset might be if an ID
  344. property is set on the node, or if a separate inherited
  345. DIR-property exists (that is different than the unset one)."
  346. (interactive)
  347. (let ((old (org-attach-dir))
  348. (new
  349. (progn
  350. (org-entry-delete nil "DIR")
  351. ;; ATTACH-DIR is deprecated and removed from documentation,
  352. ;; but still works. Remove code for it after major nr change.
  353. (org-entry-delete nil "ATTACH_DIR")
  354. (org-attach-dir))))
  355. (unless (or (string= old new)
  356. (not old))
  357. (when (and new (yes-or-no-p "Copy over attachments from old directory? "))
  358. (copy-directory old new t nil t))
  359. (when (yes-or-no-p (concat "Delete " old))
  360. (delete-directory old t)))))
  361. (defun org-attach-tag (&optional off)
  362. "Turn the autotag on or (if OFF is set) off."
  363. (when org-attach-auto-tag
  364. (save-excursion
  365. (org-back-to-heading t)
  366. (org-toggle-tag org-attach-auto-tag (if off 'off 'on)))))
  367. (defun org-attach-untag ()
  368. "Turn the autotag off."
  369. (org-attach-tag 'off))
  370. (defun org-attach-store-link (file)
  371. "Add a link to `org-stored-link' when attaching a file.
  372. Only do this when `org-attach-store-link-p' is non-nil."
  373. (setq org-stored-links
  374. (cons (list (org-attach-expand-link file)
  375. (file-name-nondirectory file))
  376. org-stored-links)))
  377. (defun org-attach-url (url)
  378. (interactive "MURL of the file to attach: \n")
  379. (let ((org-attach-method 'url))
  380. (org-attach-attach url)))
  381. (defun org-attach-buffer (buffer-name)
  382. "Attach BUFFER-NAME's contents to current outline node.
  383. BUFFER-NAME is a string. Signals a `file-already-exists' error
  384. if it would overwrite an existing filename."
  385. (interactive "bBuffer whose contents should be attached: ")
  386. (let* ((attach-dir (org-attach-dir 'get-create))
  387. (output (expand-file-name buffer-name attach-dir)))
  388. (when (file-exists-p output)
  389. (signal 'file-already-exists (list "File exists" output)))
  390. (run-hook-with-args 'org-attach-after-change-hook attach-dir)
  391. (org-attach-tag)
  392. (with-temp-file output
  393. (insert-buffer-substring buffer-name))))
  394. (defun org-attach-attach (file &optional visit-dir method)
  395. "Move/copy/link FILE into the attachment directory of the current outline node.
  396. If VISIT-DIR is non-nil, visit the directory with dired.
  397. METHOD may be `cp', `mv', `ln', `lns' or `url' default taken from
  398. `org-attach-method'."
  399. (interactive
  400. (list
  401. (read-file-name "File to keep as an attachment:"
  402. (or (progn
  403. (require 'dired-aux)
  404. (dired-dwim-target-directory))
  405. default-directory))
  406. current-prefix-arg
  407. nil))
  408. (setq method (or method org-attach-method))
  409. (let ((basename (file-name-nondirectory file)))
  410. (let* ((attach-dir (org-attach-dir 'get-create))
  411. (fname (expand-file-name basename attach-dir)))
  412. (cond
  413. ((eq method 'mv) (rename-file file fname))
  414. ((eq method 'cp) (copy-file file fname))
  415. ((eq method 'ln) (add-name-to-file file fname))
  416. ((eq method 'lns) (make-symbolic-link file fname))
  417. ((eq method 'url) (url-copy-file file fname)))
  418. (run-hook-with-args 'org-attach-after-change-hook attach-dir)
  419. (org-attach-tag)
  420. (cond ((eq org-attach-store-link-p 'attached)
  421. (org-attach-store-link fname))
  422. ((eq org-attach-store-link-p t)
  423. (org-attach-store-link file)))
  424. (if visit-dir
  425. (dired attach-dir)
  426. (message "File %S is now an attachment." basename)))))
  427. (defun org-attach-attach-cp ()
  428. "Attach a file by copying it."
  429. (interactive)
  430. (let ((org-attach-method 'cp)) (call-interactively 'org-attach-attach)))
  431. (defun org-attach-attach-mv ()
  432. "Attach a file by moving (renaming) it."
  433. (interactive)
  434. (let ((org-attach-method 'mv)) (call-interactively 'org-attach-attach)))
  435. (defun org-attach-attach-ln ()
  436. "Attach a file by creating a hard link to it.
  437. Beware that this does not work on systems that do not support hard links.
  438. On some systems, this apparently does copy the file instead."
  439. (interactive)
  440. (let ((org-attach-method 'ln)) (call-interactively 'org-attach-attach)))
  441. (defun org-attach-attach-lns ()
  442. "Attach a file by creating a symbolic link to it.
  443. Beware that this does not work on systems that do not support symbolic links.
  444. On some systems, this apparently does copy the file instead."
  445. (interactive)
  446. (let ((org-attach-method 'lns)) (call-interactively 'org-attach-attach)))
  447. (defun org-attach-new (file)
  448. "Create a new attachment FILE for the current outline node.
  449. The attachment is created as an Emacs buffer."
  450. (interactive "sCreate attachment named: ")
  451. (let ((attach-dir (org-attach-dir 'get-create)))
  452. (org-attach-tag)
  453. (find-file (expand-file-name file attach-dir))
  454. (message "New attachment %s" file)))
  455. (defun org-attach-delete-one (&optional file)
  456. "Delete a single attachment."
  457. (interactive)
  458. (let* ((attach-dir (org-attach-dir))
  459. (files (org-attach-file-list attach-dir))
  460. (file (or file
  461. (completing-read
  462. "Delete attachment: "
  463. (mapcar (lambda (f)
  464. (list (file-name-nondirectory f)))
  465. files)))))
  466. (setq file (expand-file-name file attach-dir))
  467. (unless (file-exists-p file)
  468. (error "No such attachment: %s" file))
  469. (delete-file file)
  470. (run-hook-with-args 'org-attach-after-change-hook attach-dir)))
  471. (defun org-attach-delete-all (&optional force)
  472. "Delete all attachments from the current outline node.
  473. This actually deletes the entire attachment directory.
  474. A safer way is to open the directory in dired and delete from there."
  475. (interactive "P")
  476. (let ((attach-dir (org-attach-dir)))
  477. (when (and attach-dir
  478. (or force
  479. (yes-or-no-p "Really remove all attachments of this entry? ")))
  480. (delete-directory attach-dir (yes-or-no-p "Recursive?") t)
  481. (message "Attachment directory removed")
  482. (run-hook-with-args 'org-attach-after-change-hook attach-dir)
  483. (org-attach-untag))))
  484. (defun org-attach-sync ()
  485. "Synchronize the current outline node with its attachments.
  486. This can be used after files have been added externally."
  487. (interactive)
  488. (let ((attach-dir (org-attach-dir)))
  489. (when attach-dir
  490. (run-hook-with-args 'org-attach-after-change-hook attach-dir)
  491. (let ((files (org-attach-file-list attach-dir)))
  492. (org-attach-tag (not files))))
  493. (unless attach-dir (org-attach-tag t))))
  494. (defun org-attach-file-list (dir)
  495. "Return a list of files in the attachment directory.
  496. This ignores files ending in \"~\"."
  497. (delq nil
  498. (mapcar (lambda (x) (if (string-match "^\\.\\.?\\'" x) nil x))
  499. (directory-files dir nil "[^~]\\'"))))
  500. (defun org-attach-reveal ()
  501. "Show the attachment directory of the current outline node.
  502. This will attempt to use an external program to show the directory."
  503. (interactive)
  504. (let ((attach-dir (org-attach-dir)))
  505. (if attach-dir
  506. (org-open-file attach-dir)
  507. (error "No attachment directory exist"))))
  508. (defun org-attach-reveal-in-emacs ()
  509. "Show the attachment directory of the current outline node in dired."
  510. (interactive)
  511. (let ((attach-dir (org-attach-dir)))
  512. (if attach-dir
  513. (dired attach-dir)
  514. (error "No attachment directory exist"))))
  515. (defun org-attach-open (&optional in-emacs)
  516. "Open an attachment of the current outline node.
  517. If there are more than one attachment, you will be prompted for the file name.
  518. This command will open the file using the settings in `org-file-apps'
  519. and in the system-specific variants of this variable.
  520. If IN-EMACS is non-nil, force opening in Emacs."
  521. (interactive "P")
  522. (let ((attach-dir (org-attach-dir)))
  523. (if attach-dir
  524. (let* ((file (pcase (org-attach-file-list attach-dir)
  525. (`(,file) file)
  526. (files (completing-read "Open attachment: "
  527. (mapcar #'list files) nil t))))
  528. (path (expand-file-name file attach-dir)))
  529. (run-hook-with-args 'org-attach-open-hook path)
  530. (org-open-file path in-emacs))
  531. (error "No attachment directory exist"))))
  532. (defun org-attach-open-in-emacs ()
  533. "Open attachment, force opening in Emacs.
  534. See `org-attach-open'."
  535. (interactive)
  536. (org-attach-open 'in-emacs))
  537. (defun org-attach-expand (file)
  538. "Return the full path to the current entry's attachment file FILE.
  539. Basically, this adds the path to the attachment directory."
  540. (expand-file-name file (org-attach-dir)))
  541. (defun org-attach-expand-link (file)
  542. "Return a file link pointing to the current entry's attachment file FILE.
  543. Basically, this adds the path to the attachment directory, and a \"file:\"
  544. prefix."
  545. (concat "file:" (org-attach-expand file)))
  546. (org-link-set-parameters "attachment"
  547. :follow #'org-attach-open-link
  548. :export #'org-attach-export-link
  549. :complete #'org-attach-complete-link)
  550. (defun org-attach-open-link (link &optional in-emacs)
  551. "Attachment link type LINK is expanded with the attached directory and opened.
  552. With optional prefix argument IN-EMACS, Emacs will visit the file.
  553. With a double \\[universal-argument] \\[universal-argument] \
  554. prefix arg, Org tries to avoid opening in Emacs
  555. and to use an external application to visit the file."
  556. (interactive "P")
  557. (let (line search)
  558. (cond
  559. ((string-match "::\\([0-9]+\\)\\'" link)
  560. (setq line (string-to-number (match-string 1 link))
  561. link (substring link 0 (match-beginning 0))))
  562. ((string-match "::\\(.+\\)\\'" link)
  563. (setq search (match-string 1 link)
  564. link (substring link 0 (match-beginning 0)))))
  565. (if (string-match "[*?{]" (file-name-nondirectory link))
  566. (dired (org-attach-expand link))
  567. (org-open-file (org-attach-expand link) in-emacs line search))))
  568. (defun org-attach-complete-link ()
  569. "Advise the user with the available files in the attachment directory."
  570. (let ((attach-dir (org-attach-dir)))
  571. (if attach-dir
  572. (let* ((attached-dir (expand-file-name attach-dir))
  573. (file (read-file-name "File: " attached-dir))
  574. (pwd (file-name-as-directory attached-dir))
  575. (pwd-relative (file-name-as-directory
  576. (abbreviate-file-name attached-dir))))
  577. (cond
  578. ((string-match (concat "^" (regexp-quote pwd-relative) "\\(.+\\)") file)
  579. (concat "attachment:" (match-string 1 file)))
  580. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  581. (expand-file-name file))
  582. (concat "attachment:" (match-string 1 (expand-file-name file))))
  583. (t (concat "attachment:" file))))
  584. (error "No attachment directory exist"))))
  585. (defun org-attach-export-link (link description format)
  586. "Translate attachment LINK from Org mode format to exported FORMAT.
  587. Also includes the DESCRIPTION of the link in the export."
  588. (save-excursion
  589. (let (path desc)
  590. (cond
  591. ((string-match "::\\([0-9]+\\)\\'" link)
  592. (setq link (substring link 0 (match-beginning 0))))
  593. ((string-match "::\\(.+\\)\\'" link)
  594. (setq link (substring link 0 (match-beginning 0)))))
  595. (setq path (file-relative-name (org-attach-expand link))
  596. desc (or description link))
  597. (pcase format
  598. (`html (format "<a target=\"_blank\" href=\"%s\">%s</a>" path desc))
  599. (`latex (format "\\href{%s}{%s}" path desc))
  600. (`texinfo (format "@uref{%s,%s}" path desc))
  601. (`ascii (format "%s (%s)" desc path))
  602. (`md (format "[%s](%s)" desc path))
  603. (_ path)))))
  604. (defun org-attach-archive-delete-maybe ()
  605. "Maybe delete subtree attachments when archiving.
  606. This function is called by `org-archive-hook'. The option
  607. `org-attach-archive-delete' controls its behavior."
  608. (when org-attach-archive-delete
  609. (org-attach-delete-all (not (eq org-attach-archive-delete 'query)))))
  610. ;; Attach from dired.
  611. ;; Add the following lines to the config file to get a binding for
  612. ;; dired-mode.
  613. ;; (add-hook
  614. ;; 'dired-mode-hook
  615. ;; (lambda ()
  616. ;; (define-key dired-mode-map (kbd "C-c C-x a") #'org-attach-dired-to-subtree))))
  617. ;;;###autoload
  618. (defun org-attach-dired-to-subtree (files)
  619. "Attach FILES marked or current file in dired to subtree in other window.
  620. Takes the method given in `org-attach-method' for the attach action.
  621. Precondition: Point must be in a dired buffer.
  622. Idea taken from `gnus-dired-attach'."
  623. (interactive
  624. (list (dired-get-marked-files)))
  625. (unless (eq major-mode 'dired-mode)
  626. (user-error "This command must be triggered in a dired buffer"))
  627. (let ((start-win (selected-window))
  628. (other-win
  629. (get-window-with-predicate
  630. (lambda (window)
  631. (with-current-buffer (window-buffer window)
  632. (eq major-mode 'org-mode))))))
  633. (unless other-win
  634. (user-error
  635. "Can't attach to subtree. No window displaying an Org buffer"))
  636. (select-window other-win)
  637. (dolist (file files)
  638. (org-attach-attach file))
  639. (select-window start-win)
  640. (when (eq 'mv org-attach-method)
  641. (revert-buffer))))
  642. (add-hook 'org-archive-hook 'org-attach-archive-delete-maybe)
  643. (provide 'org-attach)
  644. ;; Local variables:
  645. ;; generated-autoload-file: "org-loaddefs.el"
  646. ;; End:
  647. ;;; org-attach.el ends here