org-attach.el 17 KB

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