org-attach.el 18 KB

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