org-attach.el 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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.13pre02
  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. ;;; Code:
  32. (eval-when-compile
  33. (require 'cl))
  34. (require 'org-id)
  35. (require 'org)
  36. (defgroup org-attach nil
  37. "Options concerning entry attachments in Org-mode."
  38. :tag "Org Attach"
  39. :group 'org)
  40. (defcustom org-attach-directory "data/"
  41. "The directory where attachments are stored.
  42. If this is a relative path, it will be interpreted relative to the directory
  43. where the Org file lives."
  44. :group 'org-attach
  45. :type 'direcory)
  46. (defcustom org-attach-auto-tag "ATTACH"
  47. "Tag that will be triggered automatically when an entry has an attachment."
  48. :group 'org-attach
  49. :type '(choice
  50. (const :tag "None" nil)
  51. (string :tag "Tag")))
  52. (defcustom org-attach-file-list-property "Attachments"
  53. "The property used to keep a list of attachment belonging to this entry.
  54. This is not really needed, so you may set this to nil if you don't want it."
  55. :group 'org-attach
  56. :type '(choice
  57. (const :tag "None" nil)
  58. (string :tag "Tag")))
  59. (defcustom org-attach-method 'cp
  60. "The preferred method to attach a file.
  61. Allowed values are:
  62. mv rename the file to move it into the attachment directory
  63. cp copy the file
  64. ln create a hard link. Note that this is not supported
  65. on all systems, and then the result is not defined."
  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 one attachment, you will be prompted for a file name.
  106. D Delete all of a task's attachments. A safer way is
  107. to open the directory in dired and delete from there.")))
  108. (org-fit-window-to-buffer (get-buffer-window "*Org Attach*"))
  109. (message "Select command: [acmlzoOfFdD]")
  110. (setq c (read-char-exclusive))
  111. (and (get-buffer "*Org Attach*") (kill-buffer "*Org Attach*"))))
  112. (cond
  113. ((memq c '(?a ?\C-a)) (call-interactively 'org-attach-attach))
  114. ((memq c '(?c ?\C-c))
  115. (let ((org-attach-method 'cp)) (call-interactively 'org-attach-attach)))
  116. ((memq c '(?m ?\C-m))
  117. (let ((org-attach-method 'mv)) (call-interactively 'org-attach-attach)))
  118. ((memq c '(?l ?\C-l))
  119. (let ((org-attach-method 'ln)) (call-interactively 'org-attach-attach)))
  120. ((memq c '(?n ?\C-n)) (call-interactively 'org-attach-new))
  121. ((memq c '(?z ?\C-z)) (call-interactively 'org-attach-sync))
  122. ((memq c '(?o ?\C-o)) (call-interactively 'org-attach-open))
  123. ((eq c ?O) (call-interactively 'org-attach-open-in-emacs))
  124. ((memq c '(?f ?\C-f)) (call-interactively 'org-attach-reveal))
  125. ((memq c '(?F)) (call-interactively 'org-attach-reveal-in-emacs))
  126. ((memq c '(?d ?\C-d)) (call-interactively
  127. 'org-attach-delete-one))
  128. ((eq c ?D) (call-interactively 'org-attach-delete-all))
  129. ((eq c ?q) (message "Abort"))
  130. (t (error "No such attachment command %c" c))))))
  131. (defun org-attach-dir (&optional create-if-not-exists-p)
  132. "Return the directory associated with the current entry.
  133. If the directory does not exist and CREATE-IF-NOT-EXISTS-P is non-nil,
  134. the directory and the corresponding ID will be created."
  135. (when (and (not (buffer-file-name (buffer-base-buffer)))
  136. (not (file-name-absolute-p org-attach-directory)))
  137. (error "Need absolute `org-attach-directory' to attach in buffers without filename."))
  138. (let ((uuid (org-id-get (point) create-if-not-exists-p)))
  139. (when (or uuid create-if-not-exists-p)
  140. (unless uuid
  141. (error "ID retrieval/creation failed"))
  142. (let ((attach-dir (expand-file-name
  143. (format "%s/%s"
  144. (substring uuid 0 2)
  145. (substring uuid 2))
  146. (expand-file-name org-attach-directory))))
  147. (if (and create-if-not-exists-p
  148. (not (file-directory-p attach-dir)))
  149. (make-directory attach-dir t))
  150. (and (file-exists-p attach-dir)
  151. attach-dir)))))
  152. (defun org-attach-commit ()
  153. "Commit changes to git if `org-attach-directory' is properly initialized.
  154. This checks for the existence of a \".git\" directory in that directory."
  155. (let ((dir (expand-file-name org-attach-directory)))
  156. (if (file-exists-p (expand-file-name ".git" dir))
  157. (shell-command
  158. (concat "(cd " dir "; "
  159. " git add .; "
  160. " git ls-files --deleted -z | xargs -0 git rm; "
  161. " git commit -m 'Synchronized attachments')")))))
  162. (defun org-attach-tag (&optional off)
  163. "Turn the autotag on or (if OFF is set) off."
  164. (when org-attach-auto-tag
  165. (save-excursion
  166. (org-back-to-heading t)
  167. (org-toggle-tag org-attach-auto-tag (if off 'off 'on)))))
  168. (defun org-attach-untag ()
  169. "Turn the autotag off."
  170. (org-attach-tag 'off))
  171. (defun org-attach-attach (file &optional visit-dir method)
  172. "Move/copy/link FILE into the attachment directory of the current task.
  173. If VISIT-DIR is non-nil, visit the directory with dired.
  174. METHOD may be `cp', `mv', or `ln', default taken from `org-attach-method'."
  175. (interactive "fFile to keep as an attachment: \nP")
  176. (setq method (or method org-attach-method))
  177. (let ((basename (file-name-nondirectory file)))
  178. (when org-attach-file-list-property
  179. (org-entry-add-to-multivalued-property
  180. (point) org-attach-file-list-property basename))
  181. (let* ((attach-dir (org-attach-dir t))
  182. (fname (expand-file-name basename attach-dir)))
  183. (cond
  184. ((eq method 'mv) (rename-file file fname))
  185. ((eq method 'cp) (copy-file file fname))
  186. ((eq method 'ln) (add-name-to-file 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. "Attach a file by copying it."
  194. (interactive)
  195. (let ((org-attach-method 'cp)) (call-interactively 'org-attach-attach)))
  196. (defun org-attach-attach-mv ()
  197. "Attach a file by moving (renaming) it."
  198. (interactive)
  199. (let ((org-attach-method 'mv)) (call-interactively 'org-attach-attach)))
  200. (defun org-attach-attach-ln ()
  201. "Attach a file by creating a hard link to it.
  202. Beware that this does not work on systems that do not support hard links.
  203. On some systems, this apparently does copy the file instead."
  204. (interactive)
  205. (let ((org-attach-method 'ln)) (call-interactively 'org-attach-attach)))
  206. (defun org-attach-new (file)
  207. "Create a new attachment FILE for the current task.
  208. The attachment is created as an Emacs buffer."
  209. (interactive "sCreate attachment named: ")
  210. (when org-attach-file-list-property
  211. (org-entry-add-to-multivalued-property
  212. (point) org-attach-file-list-property file))
  213. (let ((attach-dir (org-attach-dir t)))
  214. (org-attach-tag)
  215. (find-file (expand-file-name file attach-dir))
  216. (message "New attachment %s" file)))
  217. (defun org-attach-delete-one (&optional file)
  218. "Delete a single attachment."
  219. (interactive)
  220. (let* ((attach-dir (org-attach-dir t))
  221. (files (org-attach-file-list attach-dir))
  222. (file (or file
  223. (org-ido-completing-read
  224. "Delete attachment: "
  225. (mapcar (lambda (f)
  226. (list (file-name-nondirectory f)))
  227. files)))))
  228. (setq file (expand-file-name file attach-dir))
  229. (unless (file-exists-p file)
  230. (error "No such attachment: %s" file))
  231. (delete-file file)))
  232. (defun org-attach-delete-all (&optional force)
  233. "Delete all attachments from the current task.
  234. This actually deletes the entire attachment directory.
  235. A safer way is to open the directory in dired and delete from there."
  236. (interactive "P")
  237. (when org-attach-file-list-property
  238. (org-entry-delete (point) org-attach-file-list-property))
  239. (let ((attach-dir (org-attach-dir)))
  240. (when
  241. (and attach-dir
  242. (or force
  243. (y-or-n-p "Are you sure you want to remove all attachments of this entry? ")))
  244. (shell-command (format "rm -fr %s" attach-dir))
  245. (message "Attachment directory removed")
  246. (org-attach-commit)
  247. (org-attach-untag))))
  248. (defun org-attach-sync ()
  249. "Synchronize the current tasks with its attachments.
  250. This can be used after files have been added externally."
  251. (interactive)
  252. (org-attach-commit)
  253. (when org-attach-file-list-property
  254. (org-entry-delete (point) org-attach-file-list-property))
  255. (let ((attach-dir (org-attach-dir)))
  256. (when attach-dir
  257. (let ((files (org-attach-file-list attach-dir)))
  258. (and files (org-attach-tag))
  259. (when org-attach-file-list-property
  260. (dolist (file files)
  261. (unless (string-match "^\\." file)
  262. (org-entry-add-to-multivalued-property
  263. (point) org-attach-file-list-property file))))))))
  264. (defun org-attach-file-list (dir)
  265. "Return a list of files in the attachment directory.
  266. This ignores files starting with a \".\", and files ending in \"~\"."
  267. (delq nil
  268. (mapcar (lambda (x) (if (string-match "^\\." x) nil x))
  269. (directory-files dir nil "[^~]\\'"))))
  270. (defun org-attach-reveal ()
  271. "Show the attachment directory of the current task in dired."
  272. (interactive)
  273. (let ((attach-dir (org-attach-dir t)))
  274. (org-open-file attach-dir)))
  275. (defun org-attach-reveal-in-emacs ()
  276. "Show the attachment directory of the current task.
  277. This will attempt to use an external program to show the directory."
  278. (interactive)
  279. (let ((attach-dir (org-attach-dir t)))
  280. (dired attach-dir)))
  281. (defun org-attach-open (&optional in-emacs)
  282. "Open an attachment of the current task.
  283. If there are more than one attachment, you will be prompted for the file name.
  284. This command will open the file using the settings in `org-file-apps'
  285. and in the system-specific variants of this variable.
  286. If IN-EMACS is non-nil, force opening in Emacs."
  287. (interactive "P")
  288. (let* ((attach-dir (org-attach-dir t))
  289. (files (org-attach-file-list attach-dir))
  290. (file (if (= (length files) 1)
  291. (car files)
  292. (org-ido-completing-read "Open attachment: "
  293. (mapcar 'list files) nil t))))
  294. (org-open-file (expand-file-name file attach-dir) in-emacs)))
  295. (defun org-attach-open-in-emacs ()
  296. "Open attachment, force opening in Emacs.
  297. See `org-attach-open'."
  298. (interactive)
  299. (org-attach-open 'in-emacs))
  300. (defun org-attach-expand (file)
  301. "Return the full path to the current entry's attachment file FILE.
  302. Basically, this adds the path to the attachment directory."
  303. (expand-file-name file (org-attach-dir)))
  304. (defun org-attach-expand-link (file)
  305. "Return a file link pointing to the current entry's attachment file FILE.
  306. Basically, this adds the path to the attachment directory, and a \"file:\"
  307. prefix."
  308. (concat "file:" (org-attach-expand file)))
  309. (provide 'org-attach)
  310. ;; arch-tag: fce93c2e-fe07-4fa3-a905-e10dcc7a6248
  311. ;;; org-attach.el ends here