org-attach.el 17 KB

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