org-attach.el 17 KB

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