org-attach.el 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  1. ;;; org-attach.el --- Manage file attachments to Org outlines -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2008-2020 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 "Attachment link to the attach-dir location" attached)
  118. (const :tag "File link to the attach-dir location" file)))
  119. (defcustom org-attach-archive-delete nil
  120. "Non-nil means attachments are deleted upon archiving a subtree.
  121. When set to `query', ask the user instead."
  122. :group 'org-attach
  123. :version "26.1"
  124. :package-version '(Org . "8.3")
  125. :type '(choice
  126. (const :tag "Never delete attachments" nil)
  127. (const :tag "Always delete attachments" t)
  128. (const :tag "Query the user" query)))
  129. (defun org-attach-id-uuid-folder-format (id)
  130. "Translate an UUID ID into a folder-path.
  131. Default format for how Org translates ID properties to a path for
  132. attachments. Useful if ID is generated with UUID."
  133. (format "%s/%s"
  134. (substring id 0 2)
  135. (substring id 2)))
  136. (defun org-attach-id-ts-folder-format (id)
  137. "Translate an ID based on a timestamp to a folder-path.
  138. Useful way of translation if ID is generated based on ISO8601
  139. timestamp. Splits the attachment folder hierarchy into
  140. year-month, the rest."
  141. (format "%s/%s"
  142. (substring id 0 6)
  143. (substring id 6)))
  144. (defcustom org-attach-id-to-path-function-list '(org-attach-id-uuid-folder-format
  145. org-attach-id-ts-folder-format)
  146. "List of functions parsing an ID string into a folder-path.
  147. The first function in this list defines the preferred function
  148. which will be used when creating new attachment folders. All
  149. functions of this list will be tried when looking for existing
  150. attachment folders based on ID."
  151. :group 'org-attach
  152. :package-version '(Org . "9.3")
  153. :type '(repeat (function :tag "Function with ID as input")))
  154. (defvar org-attach-after-change-hook nil
  155. "Hook to be called when files have been added or removed to the attachment folder.")
  156. (defvar org-attach-open-hook nil
  157. "Hook that is invoked by `org-attach-open'.
  158. Created mostly to be compatible with org-attach-git after removing
  159. git-functionality from this file.")
  160. (defcustom org-attach-commands
  161. '(((?a ?\C-a) org-attach-attach
  162. "Select a file and attach it to the task, using `org-attach-method'.")
  163. ((?c ?\C-c) org-attach-attach-cp
  164. "Attach a file using copy method.")
  165. ((?m ?\C-m) org-attach-attach-mv
  166. "Attach a file using move method.")
  167. ((?l ?\C-l) org-attach-attach-ln
  168. "Attach a file using link method.")
  169. ((?y ?\C-y) org-attach-attach-lns
  170. "Attach a file using symbolic-link method.")
  171. ((?u ?\C-u) org-attach-url
  172. "Attach a file from URL (downloading it).")
  173. ((?b) org-attach-buffer
  174. "Select a buffer and attach its contents to the task.")
  175. ((?n ?\C-n) org-attach-new
  176. "Create a new attachment, as an Emacs buffer.")
  177. ((?z ?\C-z) org-attach-sync
  178. "Synchronize the current node with its attachment\n directory, in case \
  179. you added attachments yourself.\n")
  180. ((?o ?\C-o) org-attach-open
  181. "Open current node's attachments.")
  182. ((?O) org-attach-open-in-emacs
  183. "Like \"o\", but force opening in Emacs.")
  184. ((?f ?\C-f) org-attach-reveal
  185. "Open current node's attachment directory. Create if missing.")
  186. ((?F) org-attach-reveal-in-emacs
  187. "Like \"f\", but force using Dired in Emacs.\n")
  188. ((?d ?\C-d) org-attach-delete-one
  189. "Delete one attachment, you will be prompted for a file name.")
  190. ((?D) org-attach-delete-all
  191. "Delete all of a node's attachments. A safer way is\n to open the \
  192. directory in dired and delete from there.\n")
  193. ((?s ?\C-s) org-attach-set-directory
  194. "Set a specific attachment directory for this entry. Sets DIR property.")
  195. ((?S ?\C-S) org-attach-unset-directory
  196. "Unset the attachment directory for this entry. Removes DIR property.")
  197. ((?q) (lambda () (interactive) (message "Abort")) "Abort."))
  198. "The list of commands for the attachment dispatcher.
  199. Each entry in this list is a list of three elements:
  200. - A list of keys (characters) to select the command (the fist
  201. character in the list is shown in the attachment dispatcher's
  202. splash buffer and minibuffer prompt).
  203. - A command that is called interactively when one of these keys
  204. is pressed.
  205. - A docstring for this command in the attachment dispatcher's
  206. splash buffer."
  207. :group 'org-attach
  208. :package-version '(Org . "9.3")
  209. :type '(repeat (list (repeat :tag "Keys" character)
  210. (function :tag "Command")
  211. (string :tag "Docstring"))))
  212. ;;;###autoload
  213. (defun org-attach ()
  214. "The dispatcher for attachment commands.
  215. Shows a list of commands and prompts for another key to execute a command."
  216. (interactive)
  217. (let ((dir (org-attach-dir nil 'no-fs-check))
  218. c marker)
  219. (when (eq major-mode 'org-agenda-mode)
  220. (setq marker (or (get-text-property (point) 'org-hd-marker)
  221. (get-text-property (point) 'org-marker)))
  222. (unless marker
  223. (error "No item in current line")))
  224. (org-with-point-at marker
  225. (org-back-to-heading-or-point-min t)
  226. (save-excursion
  227. (save-window-excursion
  228. (unless org-attach-expert
  229. (switch-to-buffer-other-window (get-buffer-create "*Org Attach*"))
  230. (erase-buffer)
  231. (setq cursor-type nil
  232. header-line-format "Use SPC, DEL, C-n or C-p to navigate.")
  233. (insert
  234. (concat "Attachment folder:\n"
  235. (or dir
  236. "Can't find an existing attachment-folder")
  237. (unless (and dir (file-directory-p dir))
  238. "\n(Not yet created)")
  239. "\n\n"
  240. (format "Select an Attachment Command:\n\n%s"
  241. (mapconcat
  242. (lambda (entry)
  243. (pcase entry
  244. (`((,key . ,_) ,_ ,docstring)
  245. (format "%c %s"
  246. key
  247. (replace-regexp-in-string "\n\\([\t ]*\\)"
  248. " "
  249. docstring
  250. nil nil 1)))
  251. (_
  252. (user-error
  253. "Invalid `org-attach-commands' item: %S"
  254. entry))))
  255. org-attach-commands
  256. "\n")))))
  257. (org-fit-window-to-buffer (get-buffer-window "*Org Attach*"))
  258. (let ((msg (format "Select command: [%s]"
  259. (concat (mapcar #'caar org-attach-commands)))))
  260. (message msg)
  261. (setq c (read-char-exclusive))
  262. (while (memq c '(14 16 32 127))
  263. (cond ((= c 14) (ignore-errors (call-interactively 'scroll-up-line)))
  264. ((= c 16) (ignore-errors (call-interactively 'scroll-down-line)))
  265. ((= c 32) (ignore-errors (call-interactively 'scroll-up)))
  266. ((= c 127) (ignore-errors (call-interactively 'scroll-down))))
  267. (message msg)
  268. (setq c (read-char-exclusive))))
  269. (and (get-buffer "*Org Attach*") (kill-buffer "*Org Attach*"))))
  270. (let ((command (cl-some (lambda (entry)
  271. (and (memq c (nth 0 entry)) (nth 1 entry)))
  272. org-attach-commands)))
  273. (if (commandp command t)
  274. (call-interactively command)
  275. (error "No such attachment command: %c" c))))))
  276. (defun org-attach-dir (&optional create-if-not-exists-p no-fs-check)
  277. "Return the directory associated with the current outline node.
  278. First check for DIR property, then ID property.
  279. `org-attach-use-inheritance' determines whether inherited
  280. properties also will be considered.
  281. If an ID property is found the default mechanism using that ID
  282. will be invoked to access the directory for the current entry.
  283. Note that this method returns the directory as declared by ID or
  284. DIR even if the directory doesn't exist in the filesystem.
  285. If CREATE-IF-NOT-EXIST-P is non-nil, `org-attach-dir-get-create'
  286. is run. If NO-FS-CHECK is non-nil, the function returns the path
  287. to the attachment even if it has not yet been initialized in the
  288. filesystem.
  289. If no attachment directory can be derived, return nil."
  290. (let (attach-dir id)
  291. (cond
  292. (create-if-not-exists-p
  293. (setq attach-dir (org-attach-dir-get-create)))
  294. ((setq attach-dir (org-entry-get nil "DIR" org-attach-use-inheritance))
  295. (org-attach-check-absolute-path attach-dir))
  296. ;; Deprecated and removed from documentation, but still
  297. ;; works. FIXME: Remove after major nr change.
  298. ((setq attach-dir (org-entry-get nil "ATTACH_DIR" org-attach-use-inheritance))
  299. (org-attach-check-absolute-path attach-dir))
  300. ((setq id (org-entry-get nil "ID" org-attach-use-inheritance))
  301. (org-attach-check-absolute-path nil)
  302. (setq attach-dir (org-attach-dir-from-id id 'try-all))))
  303. (if no-fs-check
  304. attach-dir
  305. (when (and attach-dir (file-directory-p attach-dir))
  306. attach-dir))))
  307. (defun org-attach-dir-get-create ()
  308. "Return existing or new directory associated with the current outline node.
  309. `org-attach-preferred-new-method' decides how to attach new
  310. directory if neither ID nor DIR property exist.
  311. If the attachment by some reason cannot be created an error will be raised."
  312. (interactive)
  313. (let ((attach-dir (org-attach-dir nil 'no-fs-check)))
  314. (unless attach-dir
  315. (let (answer)
  316. (when (eq org-attach-preferred-new-method 'ask)
  317. (message "Create new ID [1] property or DIR [2] property for attachments?")
  318. (setq answer (read-char-exclusive)))
  319. (cond
  320. ((or (eq org-attach-preferred-new-method 'id) (eq answer ?1))
  321. (setq attach-dir (org-attach-dir-from-id (org-id-get nil t))))
  322. ((or (eq org-attach-preferred-new-method 'dir) (eq answer ?2))
  323. (setq attach-dir (org-attach-set-directory)))
  324. ((eq org-attach-preferred-new-method 'nil)
  325. (error "No existing directory. DIR or ID property has to be explicitly created")))))
  326. (unless attach-dir
  327. (error "No attachment directory is associated with the current node"))
  328. (unless (file-directory-p attach-dir)
  329. (make-directory attach-dir t))
  330. attach-dir))
  331. (defun org-attach-dir-from-id (id &optional try-all)
  332. "Returns a folder path based on `org-attach-id-dir' and ID.
  333. If TRY-ALL is non-nil, try all id-to-path functions in
  334. `org-attach-id-to-path-function-list' and return the first path
  335. that exist in the filesystem, or the first one if none exist.
  336. Otherwise only use the first function in that list."
  337. (let ((attach-dir-preferred (expand-file-name
  338. (funcall (car org-attach-id-to-path-function-list) id)
  339. (expand-file-name org-attach-id-dir))))
  340. (if try-all
  341. (let ((attach-dir attach-dir-preferred)
  342. (fun-list (cdr org-attach-id-to-path-function-list)))
  343. (while (and fun-list (not (file-directory-p attach-dir)))
  344. (setq attach-dir (expand-file-name
  345. (funcall (car fun-list) id)
  346. (expand-file-name org-attach-id-dir)))
  347. (setq fun-list (cdr fun-list)))
  348. (if (file-directory-p attach-dir)
  349. attach-dir
  350. attach-dir-preferred))
  351. attach-dir-preferred)))
  352. (defun org-attach-check-absolute-path (dir)
  353. "Check if we have enough information to root the attachment directory.
  354. When DIR is given, check also if it is already absolute. Otherwise,
  355. assume that it will be relative, and check if `org-attach-id-dir' is
  356. absolute, or if at least the current buffer has a file name.
  357. Throw an error if we cannot root the directory."
  358. (or (and dir (file-name-absolute-p dir))
  359. (file-name-absolute-p org-attach-id-dir)
  360. (buffer-file-name (buffer-base-buffer))
  361. (error "Need absolute `org-attach-id-dir' to attach in buffers without filename")))
  362. (defun org-attach-set-directory ()
  363. "Set the DIR node property and ask to move files there.
  364. The property defines the directory that is used for attachments
  365. of the entry. Creates relative links if `org-attach-dir-relative'
  366. is non-nil.
  367. Return the directory."
  368. (interactive)
  369. (let ((old (org-attach-dir))
  370. (new
  371. (let* ((attach-dir (read-directory-name
  372. "Attachment directory: "
  373. (org-entry-get nil "DIR")))
  374. (current-dir (file-name-directory (or default-directory
  375. buffer-file-name)))
  376. (attach-dir-relative (file-relative-name attach-dir current-dir)))
  377. (org-entry-put nil "DIR" (if org-attach-dir-relative
  378. attach-dir-relative
  379. attach-dir))
  380. attach-dir)))
  381. (unless (or (string= old new)
  382. (not old))
  383. (when (yes-or-no-p "Copy over attachments from old directory? ")
  384. (copy-directory old new t t t))
  385. (when (yes-or-no-p (concat "Delete " old))
  386. (delete-directory old t)))
  387. new))
  388. (defun org-attach-unset-directory ()
  389. "Removes DIR node property.
  390. If attachment folder is changed due to removal of DIR-property
  391. ask to move attachments to new location and ask to delete old
  392. attachment-folder.
  393. Change of attachment-folder due to unset might be if an ID
  394. property is set on the node, or if a separate inherited
  395. DIR-property exists (that is different than the unset one)."
  396. (interactive)
  397. (let ((old (org-attach-dir))
  398. (new
  399. (progn
  400. (org-entry-delete nil "DIR")
  401. ;; ATTACH-DIR is deprecated and removed from documentation,
  402. ;; but still works. Remove code for it after major nr change.
  403. (org-entry-delete nil "ATTACH_DIR")
  404. (org-attach-dir))))
  405. (unless (or (string= old new)
  406. (not old))
  407. (when (and new (yes-or-no-p "Copy over attachments from old directory? "))
  408. (copy-directory old new t nil t))
  409. (when (yes-or-no-p (concat "Delete " old))
  410. (delete-directory old t)))))
  411. (defun org-attach-tag (&optional off)
  412. "Turn the autotag on or (if OFF is set) off."
  413. (when org-attach-auto-tag
  414. (save-excursion
  415. (org-back-to-heading t)
  416. (org-toggle-tag org-attach-auto-tag (if off 'off 'on)))))
  417. (defun org-attach-untag ()
  418. "Turn the autotag off."
  419. (org-attach-tag 'off))
  420. (defun org-attach-url (url)
  421. (interactive "MURL of the file to attach: \n")
  422. (let ((org-attach-method 'url))
  423. (org-attach-attach url)))
  424. (defun org-attach-buffer (buffer-name)
  425. "Attach BUFFER-NAME's contents to current outline node.
  426. BUFFER-NAME is a string. Signals a `file-already-exists' error
  427. if it would overwrite an existing filename."
  428. (interactive "bBuffer whose contents should be attached: ")
  429. (let* ((attach-dir (org-attach-dir 'get-create))
  430. (output (expand-file-name buffer-name attach-dir)))
  431. (when (file-exists-p output)
  432. (signal 'file-already-exists (list "File exists" output)))
  433. (run-hook-with-args 'org-attach-after-change-hook attach-dir)
  434. (org-attach-tag)
  435. (with-temp-file output
  436. (insert-buffer-substring buffer-name))))
  437. (defun org-attach-attach (file &optional visit-dir method)
  438. "Move/copy/link FILE into the attachment directory of the current outline node.
  439. If VISIT-DIR is non-nil, visit the directory with dired.
  440. METHOD may be `cp', `mv', `ln', `lns' or `url' default taken from
  441. `org-attach-method'."
  442. (interactive
  443. (list
  444. (read-file-name "File to keep as an attachment: "
  445. (or (progn
  446. (require 'dired-aux)
  447. (dired-dwim-target-directory))
  448. default-directory))
  449. current-prefix-arg
  450. nil))
  451. (setq method (or method org-attach-method))
  452. (let ((basename (file-name-nondirectory file)))
  453. (let* ((attach-dir (org-attach-dir 'get-create))
  454. (attach-file (expand-file-name basename attach-dir)))
  455. (cond
  456. ((eq method 'mv) (rename-file file attach-file))
  457. ((eq method 'cp) (copy-file file attach-file))
  458. ((eq method 'ln) (add-name-to-file file attach-file))
  459. ((eq method 'lns) (make-symbolic-link file attach-file))
  460. ((eq method 'url) (url-copy-file file attach-file)))
  461. (run-hook-with-args 'org-attach-after-change-hook attach-dir)
  462. (org-attach-tag)
  463. (cond ((eq org-attach-store-link-p 'attached)
  464. (push (list (concat "attachment:" (file-name-nondirectory attach-file))
  465. (file-name-nondirectory attach-file))
  466. org-stored-links))
  467. ((eq org-attach-store-link-p t)
  468. (push (list (concat "file:" file)
  469. (file-name-nondirectory file))
  470. org-stored-links))
  471. ((eq org-attach-store-link-p 'file)
  472. (push (list (concat "file:" attach-file)
  473. (file-name-nondirectory attach-file))
  474. org-stored-links)))
  475. (if visit-dir
  476. (dired attach-dir)
  477. (message "File %S is now an attachment" basename)))))
  478. (defun org-attach-attach-cp ()
  479. "Attach a file by copying it."
  480. (interactive)
  481. (let ((org-attach-method 'cp)) (call-interactively 'org-attach-attach)))
  482. (defun org-attach-attach-mv ()
  483. "Attach a file by moving (renaming) it."
  484. (interactive)
  485. (let ((org-attach-method 'mv)) (call-interactively 'org-attach-attach)))
  486. (defun org-attach-attach-ln ()
  487. "Attach a file by creating a hard link to it.
  488. Beware that this does not work on systems that do not support hard links.
  489. On some systems, this apparently does copy the file instead."
  490. (interactive)
  491. (let ((org-attach-method 'ln)) (call-interactively 'org-attach-attach)))
  492. (defun org-attach-attach-lns ()
  493. "Attach a file by creating a symbolic link to it.
  494. Beware that this does not work on systems that do not support symbolic links.
  495. On some systems, this apparently does copy the file instead."
  496. (interactive)
  497. (let ((org-attach-method 'lns)) (call-interactively 'org-attach-attach)))
  498. (defun org-attach-new (file)
  499. "Create a new attachment FILE for the current outline node.
  500. The attachment is created as an Emacs buffer."
  501. (interactive "sCreate attachment named: ")
  502. (let ((attach-dir (org-attach-dir 'get-create)))
  503. (org-attach-tag)
  504. (find-file (expand-file-name file attach-dir))
  505. (message "New attachment %s" file)))
  506. (defun org-attach-delete-one (&optional file)
  507. "Delete a single attachment."
  508. (interactive)
  509. (let* ((attach-dir (org-attach-dir))
  510. (files (org-attach-file-list attach-dir))
  511. (file (or file
  512. (completing-read
  513. "Delete attachment: "
  514. (mapcar (lambda (f)
  515. (list (file-name-nondirectory f)))
  516. files)))))
  517. (setq file (expand-file-name file attach-dir))
  518. (unless (file-exists-p file)
  519. (error "No such attachment: %s" file))
  520. (delete-file file)
  521. (run-hook-with-args 'org-attach-after-change-hook attach-dir)))
  522. (defun org-attach-delete-all (&optional force)
  523. "Delete all attachments from the current outline node.
  524. This actually deletes the entire attachment directory.
  525. A safer way is to open the directory in dired and delete from there."
  526. (interactive "P")
  527. (let ((attach-dir (org-attach-dir)))
  528. (when (and attach-dir
  529. (or force
  530. (yes-or-no-p "Really remove all attachments of this entry? ")))
  531. (delete-directory attach-dir (yes-or-no-p "Recursive?") t)
  532. (message "Attachment directory removed")
  533. (run-hook-with-args 'org-attach-after-change-hook attach-dir)
  534. (org-attach-untag))))
  535. (defun org-attach-sync ()
  536. "Synchronize the current outline node with its attachments.
  537. This can be used after files have been added externally."
  538. (interactive)
  539. (let ((attach-dir (org-attach-dir)))
  540. (when attach-dir
  541. (run-hook-with-args 'org-attach-after-change-hook attach-dir)
  542. (let ((files (org-attach-file-list attach-dir)))
  543. (org-attach-tag (not files))))
  544. (unless attach-dir (org-attach-tag t))))
  545. (defun org-attach-file-list (dir)
  546. "Return a list of files in the attachment directory.
  547. This ignores files ending in \"~\"."
  548. (delq nil
  549. (mapcar (lambda (x) (if (string-match "^\\.\\.?\\'" x) nil x))
  550. (directory-files dir nil "[^~]\\'"))))
  551. (defun org-attach-reveal ()
  552. "Show the attachment directory of the current outline node.
  553. This will attempt to use an external program to show the
  554. directory. Will create an attachment and folder if it doesn't
  555. exist yet. Respects `org-attach-preferred-new-method'."
  556. (interactive)
  557. (org-open-file (org-attach-dir-get-create)))
  558. (defun org-attach-reveal-in-emacs ()
  559. "Show the attachment directory of the current outline node in dired.
  560. Will create an attachment and folder if it doesn't exist yet.
  561. Respects `org-attach-preferred-new-method'."
  562. (interactive)
  563. (dired (org-attach-dir-get-create)))
  564. (defun org-attach-open (&optional in-emacs)
  565. "Open an attachment of the current outline node.
  566. If there are more than one attachment, you will be prompted for the file name.
  567. This command will open the file using the settings in `org-file-apps'
  568. and in the system-specific variants of this variable.
  569. If IN-EMACS is non-nil, force opening in Emacs."
  570. (interactive "P")
  571. (let ((attach-dir (org-attach-dir)))
  572. (if attach-dir
  573. (let* ((file (pcase (org-attach-file-list attach-dir)
  574. (`(,file) file)
  575. (files (completing-read "Open attachment: "
  576. (mapcar #'list files) nil t))))
  577. (path (expand-file-name file attach-dir)))
  578. (run-hook-with-args 'org-attach-open-hook path)
  579. (org-open-file path in-emacs))
  580. (error "No attachment directory exist"))))
  581. (defun org-attach-open-in-emacs ()
  582. "Open attachment, force opening in Emacs.
  583. See `org-attach-open'."
  584. (interactive)
  585. (org-attach-open 'in-emacs))
  586. (defun org-attach-expand (file)
  587. "Return the full path to the current entry's attachment file FILE.
  588. Basically, this adds the path to the attachment directory."
  589. (expand-file-name file (org-attach-dir)))
  590. (org-link-set-parameters "attachment"
  591. :complete #'org-attach-complete-link)
  592. (defun org-attach-complete-link ()
  593. "Advise the user with the available files in the attachment directory."
  594. (let ((attach-dir (org-attach-dir)))
  595. (if attach-dir
  596. (let* ((attached-dir (expand-file-name attach-dir))
  597. (file (read-file-name "File: " attached-dir))
  598. (pwd (file-name-as-directory attached-dir))
  599. (pwd-relative (file-name-as-directory
  600. (abbreviate-file-name attached-dir))))
  601. (cond
  602. ((string-match (concat "^" (regexp-quote pwd-relative) "\\(.+\\)") file)
  603. (concat "attachment:" (match-string 1 file)))
  604. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  605. (expand-file-name file))
  606. (concat "attachment:" (match-string 1 (expand-file-name file))))
  607. (t (concat "attachment:" file))))
  608. (error "No attachment directory exist"))))
  609. (defun org-attach-archive-delete-maybe ()
  610. "Maybe delete subtree attachments when archiving.
  611. This function is called by `org-archive-hook'. The option
  612. `org-attach-archive-delete' controls its behavior."
  613. (when org-attach-archive-delete
  614. (org-attach-delete-all (not (eq org-attach-archive-delete 'query)))))
  615. ;; Attach from dired.
  616. ;; Add the following lines to the config file to get a binding for
  617. ;; dired-mode.
  618. ;; (add-hook
  619. ;; 'dired-mode-hook
  620. ;; (lambda ()
  621. ;; (define-key dired-mode-map (kbd "C-c C-x a") #'org-attach-dired-to-subtree))))
  622. ;;;###autoload
  623. (defun org-attach-dired-to-subtree (files)
  624. "Attach FILES marked or current file in dired to subtree in other window.
  625. Takes the method given in `org-attach-method' for the attach action.
  626. Precondition: Point must be in a dired buffer.
  627. Idea taken from `gnus-dired-attach'."
  628. (interactive
  629. (list (dired-get-marked-files)))
  630. (unless (eq major-mode 'dired-mode)
  631. (user-error "This command must be triggered in a dired buffer"))
  632. (let ((start-win (selected-window))
  633. (other-win
  634. (get-window-with-predicate
  635. (lambda (window)
  636. (with-current-buffer (window-buffer window)
  637. (eq major-mode 'org-mode))))))
  638. (unless other-win
  639. (user-error
  640. "Can't attach to subtree. No window displaying an Org buffer"))
  641. (select-window other-win)
  642. (dolist (file files)
  643. (org-attach-attach file))
  644. (select-window start-win)
  645. (when (eq 'mv org-attach-method)
  646. (revert-buffer))))
  647. (add-hook 'org-archive-hook 'org-attach-archive-delete-maybe)
  648. (provide 'org-attach)
  649. ;; Local variables:
  650. ;; generated-autoload-file: "org-loaddefs.el"
  651. ;; End:
  652. ;;; org-attach.el ends here