org-attach.el 21 KB

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