org-attach.el 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. ;;; org-attach.el --- Manage file attachments to org-mode tasks
  2. ;; Copyright (C) 2008 Free Software Foundation, Inc.
  3. ;; Author: John Wiegley <johnw@newartisans.com>
  4. ;; Keywords: org data task
  5. ;; Version: 6.09pre01
  6. ;; This file is part of GNU Emacs.
  7. ;;
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; See the Org-mode manual for information on how to use it.
  20. ;;
  21. ;; Attachments are managed in a special directory called "data", which
  22. ;; lives in the directory given by `org-directory'. If this data
  23. ;; directory is initialized as a Git repository, then org-attach will
  24. ;; automatically commit changes when it sees them.
  25. ;;
  26. ;; Attachment directories are identified using a UUID generated for the
  27. ;; task which has the attachments. These are added as property to the
  28. ;; task when necessary, and should not be deleted or changed by the
  29. ;; user, ever. UUIDs are generated by a mechanism defined in the variable
  30. ;; `org-id-method'.
  31. ;; Ideas: Store region or kill as an attachment.
  32. ;; Support drag-and-drop
  33. (eval-when-compile
  34. (require 'cl))
  35. (require 'org-id)
  36. (require 'org)
  37. (defgroup org-attach nil
  38. "Options concerning entry attachments in Org-mode."
  39. :tag "Org Remember"
  40. :group 'org)
  41. (defcustom org-attach-directory "data/"
  42. "The directory where attachments are stored.
  43. If this is a relative path, it will be interpreted relative to the directory
  44. where the Org file lives."
  45. :group 'org-attach
  46. :type 'direcory)
  47. (defcustom org-attach-auto-tag "ATTACH"
  48. "Tag that will be triggered automatically when an entry has an attachment."
  49. :group 'org-attach
  50. :type '(choice
  51. (const :tag "None" nil)
  52. (string :tag "Tag")))
  53. (defcustom org-attach-file-list-property "Attachments"
  54. "The property used to keep a list of attachment belonging to this entry.
  55. This is not really needed, so you may set this to nil if you don't want it."
  56. :group 'org-attach
  57. :type '(choice
  58. (const :tag "None" nil)
  59. (string :tag "Tag")))
  60. (defcustom org-attach-method 'cp
  61. "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. mv create a hard link when possible. If not, fall back to copy."
  66. :group 'org-attach
  67. :type '(choice
  68. (const :tag "Copy" cp)
  69. (const :tag "Move/Rename" mv)
  70. (const :tag "Link" ln)))
  71. (defcustom org-attach-expert nil
  72. "Non-nil means do not show the splash buffer with the attach dispatcher."
  73. :group 'org-attach
  74. :type 'boolean)
  75. ;;;###autoload
  76. (defun org-attach ()
  77. "The dispatcher for attachment commands.
  78. Shows a list of commands and prompts for another key to execute a command."
  79. (interactive)
  80. (let (c marker)
  81. (when (eq major-mode 'org-agenda-mode)
  82. (setq marker (or (get-text-property (point) 'org-hd-marker)
  83. (get-text-property (point) 'org-marker)))
  84. (unless marker
  85. (error "No task in current line")))
  86. (save-excursion
  87. (when marker
  88. (set-buffer (marker-buffer marker))
  89. (goto-char marker))
  90. (org-back-to-heading t)
  91. (save-excursion
  92. (save-window-excursion
  93. (unless org-attach-expert
  94. (with-output-to-temp-buffer "*Org Attach*"
  95. (princ "Select an Attachment Command:
  96. a Select a file and attach it to the task, using `org-attach-method'.
  97. c/m/l Attach a file using copy/move/link method.
  98. n Create a new attachment, as an Emacs buffer.
  99. z Synchronize the current task with its attachment
  100. directory, in case you added attachments yourself.
  101. o Open current task's attachments.
  102. O Like \"o\", but force opening in Emacs.
  103. f Open current task's attachment directory.
  104. F Like \"f\", but force using dired in Emacs.
  105. D Delete all of a task's attachments. A safer way is
  106. to open the directory in dired and delete from there.")))
  107. (shrink-window-if-larger-than-buffer (get-buffer-window "*Org Attach*"))
  108. (message "Select command: [azoOfFD^a]")
  109. (setq c (read-char-exclusive))
  110. (and (get-buffer "*Org Attach*") (kill-buffer "*Org Attach*"))))
  111. (cond
  112. ((memq c '(?a ?\C-a)) (call-interactively 'org-attach-attach))
  113. ((memq c '(?c ?\C-c))
  114. (let ((org-attach-method 'cp)) (call-interactively 'org-attach-attach)))
  115. ((memq c '(?m ?\C-m))
  116. (let ((org-attach-method 'mv)) (call-interactively 'org-attach-attach)))
  117. ((memq c '(?l ?\C-l))
  118. (let ((org-attach-method 'ln)) (call-interactively 'org-attach-attach)))
  119. ((memq c '(?n ?\C-n)) (call-interactively 'org-attach-new))
  120. ((memq c '(?z ?\C-z)) (call-interactively 'org-attach-sync))
  121. ((memq c '(?o ?\C-o)) (call-interactively 'org-attach-open))
  122. ((eq c ?O) (call-interactively 'org-attach-open-in-emacs))
  123. ((memq c '(?f ?\C-f)) (call-interactively 'org-attach-reveal))
  124. ((memq c '(?F)) (call-interactively 'org-attach-reveal-in-emacs))
  125. ((eq c ?D) (call-interactively 'org-attach-delete))
  126. ((eq c ?q) ((message "Abort")))
  127. (t (error "No such attachment command %c" c))))))
  128. (defun org-attach-dir (&optional create-if-not-exists-p)
  129. "Return the directory associated with the current entry.
  130. If the directory does not exist and CREATE-IF-NOT-EXISTS-P is non-nil,
  131. the directory and the corresponding ID will be created."
  132. (let ((uuid (org-id-get (point) create-if-not-exists-p)))
  133. (when (or uuid create-if-not-exists-p)
  134. (unless uuid
  135. (let ((uuid-string (shell-command-to-string "uuidgen")))
  136. (setf uuid-string
  137. (substring uuid-string 0 (1- (length uuid-string))))
  138. (org-entry-put (point) "ID" uuid-string)
  139. (setf uuid uuid-string)))
  140. (let ((attach-dir (expand-file-name
  141. (format "%s/%s"
  142. (substring uuid 0 2)
  143. (substring uuid 2))
  144. (expand-file-name org-attach-directory))))
  145. (if (and create-if-not-exists-p
  146. (not (file-directory-p attach-dir)))
  147. (make-directory attach-dir t))
  148. (and (file-exists-p attach-dir)
  149. attach-dir)))))
  150. (defun org-attach-commit ()
  151. "Commit changes to git if available."
  152. (let ((dir (expand-file-name org-attach-directory)))
  153. (if (file-exists-p (expand-file-name ".git" dir))
  154. (shell-command
  155. (concat "(cd " dir "; "
  156. " git add .; "
  157. " git ls-files --deleted -z | xargs -0 git rm; "
  158. " git commit -m 'Synchronized attachments')")))))
  159. (defun org-attach-tag (&optional off)
  160. "Turn the autotag on."
  161. (when org-attach-auto-tag
  162. (save-excursion
  163. (org-back-to-heading t)
  164. (org-toggle-tag org-attach-auto-tag (if off 'off 'on)))))
  165. (defun org-attach-untag ()
  166. "Turn the autotag off."
  167. (org-attach-tag 'off))
  168. (defun org-attach-attach (file &optional visit-dir method)
  169. "Move FILE into the attachment directory of the current task.
  170. If VISIT-DIR is non-nil, visit the direcory with dired."
  171. (interactive "fFile to keep as an attachment: \nP")
  172. (setq method (or method org-attach-method))
  173. (let ((basename (file-name-nondirectory file)))
  174. (when org-attach-file-list-property
  175. (org-entry-add-to-multivalued-property
  176. (point) org-attach-file-list-property basename))
  177. (let* ((attach-dir (org-attach-dir t))
  178. (fname (expand-file-name basename attach-dir)))
  179. (cond
  180. ((eq method 'mv) (rename-file file fname))
  181. ((eq method 'cp) (copy-file file fname))
  182. ((eq method 'ln)
  183. (require 'eshell)
  184. (require 'esh-opt)
  185. (require 'em-unix)
  186. (eshell/ln file fname)))
  187. (org-attach-commit)
  188. (org-attach-tag)
  189. (if visit-dir
  190. (dired attach-dir)
  191. (message "File \"%s\" is now a task attachment." basename)))))
  192. (defun org-attach-attach-cp ()
  193. (interactive)
  194. (let ((org-attach-method 'cp)) (call-interactively 'org-attach-attach)))
  195. (defun org-attach-attach-mv ()
  196. (interactive)
  197. (let ((org-attach-method 'mv)) (call-interactively 'org-attach-attach)))
  198. (defun org-attach-attach-ln ()
  199. (let ((org-attach-method 'ln)) (call-interactively 'org-attach-attach)))
  200. (defun org-attach-new (file)
  201. "Create a new attachment FILE for the current task.
  202. The attachment is created as an Emacs buffer."
  203. (interactive "sCreate attachment named: ")
  204. (when org-attach-file-list-property
  205. (org-entry-add-to-multivalued-property
  206. (point) org-attach-file-list-property file))
  207. (let ((attach-dir (org-attach-dir t)))
  208. (org-attach-tag)
  209. (find-file (expand-file-name file attach-dir))
  210. (message "New attachment %s" file)))
  211. (defun org-attach-delete ()
  212. "Delete all attachments from the current task.
  213. A safer way is to open the directory in dired and delete from there."
  214. (interactive)
  215. (when org-attach-file-list-property
  216. (org-entry-delete (point) org-attach-file-list-property))
  217. (let ((attach-dir (org-attach-dir)))
  218. (if attach-dir
  219. (shell-command (format "rm -fr %s" attach-dir))))
  220. (org-attach-commit)
  221. (org-attach-untag))
  222. (defun org-attach-sync ()
  223. "Synchonize the current tasks with its attachments.
  224. This can be used after files have been added externally."
  225. (interactive)
  226. (org-attach-commit)
  227. (when org-attach-file-list-property
  228. (org-entry-delete (point) org-attach-file-list-property))
  229. (let ((attach-dir (org-attach-dir)))
  230. (when attach-dir
  231. (let ((files (org-attach-file-list attach-dir)))
  232. (and files (org-attach-tag))
  233. (when org-attach-file-list-property
  234. (dolist (file files)
  235. (unless (string-match "^\\." file)
  236. (org-entry-add-to-multivalued-property
  237. (point) org-attach-file-list-property file))))))))
  238. (defun org-attach-file-list (dir)
  239. "Return a list of files in the attachment directory.
  240. This ignores files starting with a \".\", and files ending in \"~\"."
  241. (delq nil
  242. (mapcar (lambda (x) (if (string-match "^\\." x) nil x))
  243. (directory-files "." nil "[^~]\\'"))))
  244. (defun org-attach-reveal ()
  245. "Show the attachment directory of the current task in dired."
  246. (interactive)
  247. (let ((attach-dir (org-attach-dir t)))
  248. (org-open-file attach-dir)))
  249. (defun org-attach-reveal-in-emacs ()
  250. "Show the attachment directory of the current task.
  251. This will attempt to use an external program to show the directory."
  252. (interactive)
  253. (let ((attach-dir (org-attach-dir t)))
  254. (dired attach-dir)))
  255. (defun org-attach-open (&optional in-emacs)
  256. "Open an attachment of the current task.
  257. If there are more than one attachment, you will be prompted for the file name.
  258. This command will open the file using the settings in `org-file-apps'
  259. and in the system-specific variants of this variable.
  260. If IN-EMACS is non-nil, force opening in Emacs."
  261. (interactive "P")
  262. (let* ((attach-dir (org-attach-dir t))
  263. (files (org-attach-file-list attach-dir))
  264. (file (if (= (length files) 1)
  265. (car files)
  266. (completing-read "Open attachment: "
  267. (mapcar 'list files) nil t))))
  268. (org-open-file (expand-file-name file attach-dir) in-emacs)))
  269. (defun org-attach-open-in-emacs ()
  270. "Open attachment, force opening in Emacs.
  271. See `org-attach-open'."
  272. (interactive)
  273. (org-attach-open 'in-emacs))
  274. (defun org-attach-open-single-attachment (&optional in-emacs)
  275. (interactive)
  276. (let* ((attach-dir (org-attach-dir t))
  277. (file (read-file-name "Open attachment: " attach-dir nil t)))
  278. (org-open-file file in-emacs)))
  279. (provide 'org-attach)
  280. ;;; org-attach.el ends here