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