org-download.el 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. ;;; org-download.el --- Image drag-and-drop for Emacs org-mode
  2. ;; Copyright (C) 2014 Free Software Foundation, Inc.
  3. ;; Author: Oleh Krehel
  4. ;; Keywords: images, screenshots, download
  5. ;; Homepage: http://orgmode.org
  6. ;; This file is part of GNU Emacs.
  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. ;;
  19. ;; This extension facilitates moving images from point A to point B.
  20. ;;
  21. ;; Point A (the source) can be:
  22. ;; 1. An image inside your browser that you can drag to Emacs.
  23. ;; 2. An image on your file system that you can drag to Emacs.
  24. ;; 3. A local or remote image address in kill-ring.
  25. ;; Use the `org-download-yank' command for this.
  26. ;; Remember that you can use "0 w" in `dired' to get an address.
  27. ;; 4. An screenshot taken using `gnome-screenshot' or `scrot' or `gm'.
  28. ;; Use the `org-download-screenshot' command for this.
  29. ;; Customize the backend with `org-download-screenshot-method'.
  30. ;;
  31. ;; Point B (the target) is an Emacs `org-mode' buffer where the inline
  32. ;; link will be inserted. Several customization options will determine
  33. ;; where exactly on the file system the file will be stored.
  34. ;;
  35. ;; They are:
  36. ;; `org-download-method':
  37. ;; a. 'attach => use `org-mode' attachment machinery
  38. ;; b. 'directory => construct the directory in two stages:
  39. ;; 1. first part of the folder name is:
  40. ;; * either "." (current folder)
  41. ;; * or `org-download-image-dir' (if it's not nil).
  42. ;; `org-download-image-dir' becomes buffer-local when set,
  43. ;; so each file can customize this value, e.g with:
  44. ;; # -*- mode: Org; org-download-image-dir: ~/Pictures/foo; -*-
  45. ;; 2. second part is:
  46. ;; * `org-download-heading-lvl' is nil => ""
  47. ;; * `org-download-heading-lvl' is n => the name of current
  48. ;; heading with level n. Level count starts with 0,
  49. ;; i.e. * is 0, ** is 1, *** is 2 etc.
  50. ;; `org-download-heading-lvl' becomes buffer-local when set,
  51. ;; so each file can customize this value, e.g with:
  52. ;; # -*- mode: Org; org-download-heading-lvl: nil; -*-
  53. ;;
  54. ;; `org-download-timestamp':
  55. ;; optionally add a timestamp to the file name.
  56. ;;
  57. ;; Customize `org-download-backend' to choose between `url-retrieve'
  58. ;; (the default) or `wget' or `curl'.
  59. ;;
  60. ;;; Code:
  61. (eval-when-compile
  62. (require 'cl))
  63. (require 'url-parse)
  64. (require 'url-http)
  65. (defgroup org-download nil
  66. "Image drag-and-drop for org-mode."
  67. :group 'org
  68. :prefix "org-download-")
  69. (defcustom org-download-method 'directory
  70. "The way images should be stored."
  71. :type '(choice
  72. (const :tag "Directory" directory)
  73. (const :tag "Attachment" attach))
  74. :group 'org-download)
  75. (defcustom org-download-image-dir nil
  76. "If set, images will be stored in this directory instead of \".\".
  77. See `org-download--dir-1' for more info."
  78. :type '(choice
  79. (const :tag "Default" nil)
  80. (string :tag "Directory"))
  81. :group 'org-download)
  82. (make-variable-buffer-local 'org-download-image-dir)
  83. (defcustom org-download-heading-lvl 0
  84. "Heading level to be used in `org-download--dir-2'."
  85. :group 'org-download)
  86. (make-variable-buffer-local 'org-download-heading-lvl)
  87. (defcustom org-download-backend t
  88. "Method to use for downloading."
  89. :type '(choice
  90. (const :tag "wget" "wget \"%s\" -O \"%s\"")
  91. (const :tag "curl" "curl \"%s\" -o \"%s\"")
  92. (const :tag "url-retrieve" t))
  93. :group 'org-download)
  94. (defcustom org-download-timestamp "_%Y-%m-%d_%H:%M:%S"
  95. "This `format-time-string'-style string will be appended to the file name.
  96. Set this to \"\" if you don't want time stamps."
  97. :type 'string
  98. :group 'org-download)
  99. (defcustom org-download-screenshot-method "gnome-screenshot -a -f %s"
  100. "The tool to capture screenshots."
  101. :type '(choice
  102. (const :tag "gnome-screenshot" "gnome-screenshot -a -f %s")
  103. (const :tag "scrot" "scrot -s %s")
  104. (const :tag "gm" "gm import %s"))
  105. :group 'org-download)
  106. (defcustom org-download-image-width 0
  107. "When non-zero add #+attr_html: :width tag to the image."
  108. :type 'integer
  109. :group 'org-download)
  110. (defun org-download-get-heading (lvl)
  111. "Return the heading of the current entry's LVL level parent."
  112. (save-excursion
  113. (let ((cur-lvl (org-current-level)))
  114. (unless (= cur-lvl 1)
  115. (org-up-heading-all (- (1- (org-current-level)) lvl)))
  116. (substring-no-properties
  117. (org-get-heading)))))
  118. (defun org-download--dir-1 ()
  119. "Return the first part of the directory path for `org-download--dir'.
  120. It's `org-download-image-dir', unless it's nil. Then it's \".\"."
  121. (or org-download-image-dir "."))
  122. (defun org-download--dir-2 ()
  123. "Return the second part of the directory path for `org-download--dir'.
  124. Unless `org-download-heading-lvl' is nil, it's the name of the current
  125. `org-download-heading-lvl'-leveled heading. Otherwise it's \"\"."
  126. (and org-download-heading-lvl
  127. (org-download-get-heading
  128. org-download-heading-lvl)))
  129. (defun org-download--dir ()
  130. "Return the directory path for image storage.
  131. The path is composed from `org-download--dir-1' and `org-download--dir-2'.
  132. The directory is created if it didn't exist before."
  133. (let* ((part1 (org-download--dir-1))
  134. (part2 (org-download--dir-2))
  135. (dir (if part2
  136. (format "%s/%s" part1 part2)
  137. part1)))
  138. (unless (file-exists-p dir)
  139. (make-directory dir t))
  140. dir))
  141. (defun org-download--fullname (link)
  142. "Return the file name where LINK will be saved to.
  143. It's affected by `org-download-timestamp' and `org-download--dir'."
  144. (let ((filename
  145. (file-name-nondirectory
  146. (car (url-path-and-query
  147. (url-generic-parse-url link)))))
  148. (dir (org-download--dir)))
  149. (format "%s/%s%s.%s"
  150. dir
  151. (file-name-sans-extension filename)
  152. (format-time-string org-download-timestamp)
  153. (file-name-extension filename))))
  154. (defun org-download--image (link filename)
  155. "Save LINK to FILENAME asynchronously and show inline images in current buffer."
  156. (when (string-match "^file://\\(.*\\)" link)
  157. (setq link (url-unhex-string (match-string 1 link))))
  158. (cond ((file-exists-p link)
  159. (org-download--image/command "cp \"%s\" \"%s\"" link filename))
  160. ((eq org-download-backend t)
  161. (org-download--image/url-retrieve link filename))
  162. (t
  163. (org-download--image/command org-download-backend link filename))))
  164. (defun org-download--image/command (command link filename)
  165. "Using COMMAND, save LINK to FILENAME.
  166. COMMAND is a format-style string with two slots for LINK and FILENAME."
  167. (require 'async)
  168. (async-start
  169. `(lambda() (shell-command
  170. ,(format command link
  171. (expand-file-name filename))))
  172. (lexical-let ((cur-buf (current-buffer)))
  173. (lambda(x)
  174. (with-current-buffer cur-buf
  175. (org-display-inline-images))))))
  176. (defun org-download--image/url-retrieve (link filename)
  177. "Save LINK to FILENAME using `url-retrieve'."
  178. (url-retrieve
  179. link
  180. (lambda (status filename buffer)
  181. ;; Write current buffer to FILENAME
  182. ;; and update inline images in BUFFER
  183. (let ((err (plist-get status :error)))
  184. (if err (error
  185. "\"%s\" %s" link
  186. (downcase (nth 2 (assq (nth 2 err) url-http-codes))))))
  187. (delete-region
  188. (point-min)
  189. (progn
  190. (re-search-forward "\n\n" nil 'move)
  191. (point)))
  192. (let ((coding-system-for-write 'no-conversion))
  193. (write-region nil nil filename nil nil nil 'confirm))
  194. (with-current-buffer buffer
  195. (org-display-inline-images)))
  196. (list
  197. (expand-file-name filename)
  198. (current-buffer))
  199. nil t))
  200. (defun org-download-yank ()
  201. "Call `org-download-image' with current kill."
  202. (interactive)
  203. (org-download-image (current-kill 0)))
  204. (defun org-download-screenshot ()
  205. "Capture screenshot and insert the resulting file.
  206. The screenshot tool is determined by `org-download-screenshot-method'."
  207. (interactive)
  208. (let ((link "/tmp/screenshot.png"))
  209. (shell-command (format org-download-screenshot-method link))
  210. (org-download-image link)))
  211. (defun org-download-image (link)
  212. "Save image at address LINK to `org-download--dir'."
  213. (interactive "sUrl: ")
  214. (let ((filename
  215. (if (eq org-download-method 'attach)
  216. (let ((org-download-image-dir (progn (require 'org-attach)
  217. (org-attach-dir t)))
  218. org-download-heading-lvl)
  219. (org-download--fullname link))
  220. (org-download--fullname link))))
  221. (when (image-type-from-file-name filename)
  222. (org-download--image link filename)
  223. (when (eq org-download-method 'attach)
  224. (org-attach-attach filename nil 'none))
  225. (if (looking-back "^[ \t]+")
  226. (delete-region (match-beginning 0) (match-end 0))
  227. (newline))
  228. (insert (format "#+DOWNLOADED: %s @ %s\n%s [[%s]]"
  229. link
  230. (format-time-string "%Y-%m-%d %H:%M:%S")
  231. (if (= org-download-image-width 0)
  232. ""
  233. (format "#+attr_html: :width %dpx\n" org-download-image-width))
  234. filename))
  235. (org-display-inline-images))))
  236. (defun org-download--at-comment-p ()
  237. "Check if current line begins with #+DOWLOADED:."
  238. (save-excursion
  239. (move-beginning-of-line nil)
  240. (looking-at "#\\+DOWNLOADED:")))
  241. (defun org-download-delete ()
  242. "Delete inline image link on current line, and the file that it points to."
  243. (interactive)
  244. (cond ((org-download--at-comment-p)
  245. (delete-region (line-beginning-position)
  246. (line-end-position))
  247. (org-download--delete (line-beginning-position)
  248. nil
  249. 1))
  250. ((region-active-p)
  251. (org-download--delete (region-beginning)
  252. (region-end))
  253. (delete-region (region-beginning)
  254. (region-end)))
  255. (t (org-download--delete (line-beginning-position)
  256. (line-end-position)))))
  257. (defun org-download--delete (beg end &optional times)
  258. "Delete inline image links and the files they point to between BEG and END.
  259. When TIMES isn't nil, delete only TIMES links."
  260. (unless times
  261. (setq times most-positive-fixnum))
  262. (save-excursion
  263. (goto-char beg)
  264. (while (and (>= (decf times) 0)
  265. (re-search-forward "\\[\\[\\([^]]*\\)\\]\\]" end t))
  266. (let ((str (match-string-no-properties 1)))
  267. (delete-region (match-beginning 0)
  268. (match-end 0))
  269. (when (file-exists-p str)
  270. (delete-file str))))))
  271. (defun org-download-dnd (uri action)
  272. "When in `org-mode' and URI points to image, download it.
  273. Otherwise, pass URI and ACTION back to dnd dispatch."
  274. (if (eq major-mode 'org-mode)
  275. ;; probably shouldn't redirect
  276. (unless (org-download-image uri)
  277. (message "not an image URL"))
  278. ;; redirect to someone else
  279. (let ((dnd-protocol-alist
  280. (rassq-delete-all
  281. 'org-download-dnd
  282. (copy-alist dnd-protocol-alist))))
  283. (dnd-handle-one-url nil action uri))))
  284. (defun org-download-enable ()
  285. "Enable org-download."
  286. (unless (eq (cdr (assoc "^\\(https?\\|ftp\\|file\\|nfs\\)://" dnd-protocol-alist))
  287. 'org-download-dnd)
  288. (setq dnd-protocol-alist
  289. `(("^\\(https?\\|ftp\\|file\\|nfs\\)://" . org-download-dnd) ,@dnd-protocol-alist))))
  290. (defun org-download-disable ()
  291. "Disable org-download."
  292. (rassq-delete-all 'org-download-dnd dnd-protocol-alist))
  293. (org-download-enable)
  294. (provide 'org-download)
  295. ;;; org-download.el ends here