org-attach.el 18 KB

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