org-attach.el 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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.08
  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-expert nil
  48. "Non-nil means do not show the splash buffer with the attach dispatcher."
  49. :group 'org-attach
  50. :type 'boolean)
  51. ;;;###autoload
  52. (defun org-attach ()
  53. "The dispatcher for attachment commands.
  54. Shows a list of commands and prompts for another key to execute a command."
  55. (interactive)
  56. (let (c marker)
  57. (when (eq major-mode 'org-agenda-mode)
  58. (setq marker (or (get-text-property (point) 'org-hd-marker)
  59. (get-text-property (point) 'org-marker)))
  60. (unless marker
  61. (error "No task in current line")))
  62. (save-excursion
  63. (when marker
  64. (set-buffer (marker-buffer marker))
  65. (goto-char marker))
  66. (org-back-to-heading t)
  67. (save-excursion
  68. (save-window-excursion
  69. (unless org-attach-expert
  70. (with-output-to-temp-buffer "*Org Attach*"
  71. (princ "Select an Attachment Command:
  72. a Select a file and move it into the task's attachment directory.
  73. c Create a new attachment, as an Emacs buffer.
  74. z Synchronize the current task with its attachment
  75. directory, in case you added attachments yourself.
  76. o Open current task's attachments.
  77. O Like \"o\", but force opening in Emacs.
  78. f Open current task's attachment directory.
  79. F Like \"f\", but force using dired in Emacs.
  80. D Delete all of a task's attachments. A safer way is
  81. to open the directory in dired and delete from there.")))
  82. (shrink-window-if-larger-than-buffer (get-buffer-window "*Org Attach*"))
  83. (message "Select command: [azoOfFD^a]")
  84. (setq c (read-char-exclusive))
  85. (and (get-buffer "*Org Attach*") (kill-buffer "*Org Attach*"))))
  86. (cond
  87. ((memq c '(?a ?\C-a)) (call-interactively 'org-attach-attach))
  88. ((memq c '(?c ?\C-c)) (call-interactively 'org-attach-new))
  89. ((memq c '(?z ?\C-z)) (call-interactively 'org-attach-sync))
  90. ((memq c '(?o ?\C-o)) (call-interactively 'org-attach-open))
  91. ((eq c ?O) (call-interactively 'org-attach-open-in-emacs))
  92. ((memq c '(?f ?\C-f)) (call-interactively 'org-attach-reveal))
  93. ((memq c '(?F)) (call-interactively 'org-attach-reveal-in-emacs))
  94. ((eq c ?D) (call-interactively 'org-attach-delete))
  95. (t (error "No such attachment command %c" c))))))
  96. (defun org-attach-dir (&optional create-if-not-exists-p)
  97. "Return the directory associated with the current entry.
  98. If the directory does not exist and CREATE-IF-NOT-EXISTS-P is non-nil,
  99. the directory and the corresponding ID will be created."
  100. (let ((uuid (org-id-get (point) create-if-not-exists-p)))
  101. (when (or uuid create-if-not-exists-p)
  102. (unless uuid
  103. (let ((uuid-string (shell-command-to-string "uuidgen")))
  104. (setf uuid-string
  105. (substring uuid-string 0 (1- (length uuid-string))))
  106. (org-entry-put (point) "ID" uuid-string)
  107. (setf uuid uuid-string)))
  108. (let ((attach-dir (expand-file-name
  109. (format "%s/%s"
  110. (substring uuid 0 2)
  111. (substring uuid 2))
  112. (expand-file-name org-attach-directory))))
  113. (if (and create-if-not-exists-p
  114. (not (file-directory-p attach-dir)))
  115. (make-directory attach-dir t))
  116. (and (file-exists-p attach-dir)
  117. attach-dir)))))
  118. (defun org-attach-commit ()
  119. "Commit changes to git if available."
  120. (let ((dir (expand-file-name org-attach-directory)))
  121. (if (file-exists-p (expand-file-name ".git" dir))
  122. (shell-command
  123. (concat "(cd " dir "; "
  124. " git add .; "
  125. " git ls-files --deleted -z | xargs -0 git rm; "
  126. " git commit -m 'Synchronized attachments')")))))
  127. (defun org-attach-attach (file &optional visit-dir)
  128. "Move FILE into the attachment directory of the current task.
  129. If VISIT-DIR is non-nil, visit the direcory with dired."
  130. (interactive "fFile to keep as an attachment: \nP")
  131. (let ((basename (file-name-nondirectory file)))
  132. (org-entry-add-to-multivalued-property (point) "Attachments"
  133. basename)
  134. (let ((attach-dir (org-attach-dir t)))
  135. (rename-file file (expand-file-name basename attach-dir))
  136. (org-attach-commit)
  137. (if visit-dir
  138. (dired attach-dir)
  139. (message "File \"%s\" is now a task attachment." basename)))))
  140. (defun org-attach-new (file)
  141. "Create a new attachment FILE for the current task.
  142. The attachment is created as an Emacs buffer."
  143. (interactive "sCreate attachment named: ")
  144. (org-entry-add-to-multivalued-property (point) "Attachments"
  145. file)
  146. (let ((attach-dir (org-attach-dir t)))
  147. (find-file (expand-file-name file attach-dir))
  148. (message "New attachment %s" file)))
  149. (defun org-attach-delete ()
  150. "Delete all attachments from the current task.
  151. A safer way is to open the directory in dired and delete from there."
  152. (interactive)
  153. (org-entry-delete (point) "Attachments")
  154. (let ((attach-dir (org-attach-dir)))
  155. (if attach-dir
  156. (shell-command (format "rm -fr %s" attach-dir))))
  157. (org-attach-commit))
  158. (defun org-attach-sync ()
  159. "Synchonize the current tasks with its attachments.
  160. This can be used after files have been added externally."
  161. (interactive)
  162. (org-attach-commit)
  163. (org-entry-delete (point) "Attachments")
  164. (let ((attach-dir (org-attach-dir)))
  165. (when attach-dir
  166. (let ((files (directory-files attach-dir)))
  167. (dolist (file files)
  168. (unless (string-match "^\\." file)
  169. (org-entry-add-to-multivalued-property
  170. (point) "Attachments" file)))))))
  171. (defun org-attach-reveal ()
  172. "Show the attachment directory of the current task in dired."
  173. (interactive)
  174. (let ((attach-dir (org-attach-dir t)))
  175. (org-open-file attach-dir)))
  176. (defun org-attach-reveal-in-emacs ()
  177. "Show the attachment directory of the current task.
  178. This will attempt to use an external program to show the directory."
  179. (interactive)
  180. (let ((attach-dir (org-attach-dir t)))
  181. (dired attach-dir)))
  182. (defun org-attach-open (&optional in-emacs)
  183. "Open an attachment of the current task.
  184. If there are more than one attachment, you will be prompted for the file name.
  185. This command will open the file using the settings in `org-file-apps'
  186. and in the system-specific variants of this variable.
  187. If IN-EMACS is non-nil, force opening in Emacs."
  188. (interactive "P")
  189. (let* ((attach-dir (org-attach-dir t))
  190. (files (org-entry-get-multivalued-property (point) "Attachments"))
  191. (file (if (= (length files) 1)
  192. (car files)
  193. (completing-read "Attachment: " (mapcar 'list files) nil t))))
  194. (org-open-file (expand-file-name file attach-dir) in-emacs)))
  195. (defun org-attach-open-in-emacs ()
  196. "Open attachment, force opening in Emacs.
  197. See `org-attach-open'."
  198. (org-attach-open 'in-emacs))
  199. (defun org-attach-open-single-attachment (&optional in-emacs)
  200. (interactive)
  201. (let* ((attach-dir (org-attach-dir t))
  202. (file (read-file-name "Attachment: " attach-dir nil t)))
  203. (org-open-file file in-emacs)))
  204. (provide 'org-attach)
  205. ;;; org-attach.el ends here