org-download

a fork of abo-abo's org-download
git clone https://git.trogloxene.org/org-download.git
Log | Files | Refs | README

commit 1a9d786a48164895ea4ab69357e51a4dab4d7310
parent 157e982c3b74dca9aeff2e50a0cb2db74282ea7b
Author: Oleh Krehel <ohwoeowho@gmail.com>
Date:   Thu, 31 Oct 2013 09:31:20 +0100

org-download.el: can now save local images.

* org-download.el (org-download--image): now dispatches to local file
  copy method or one of the three remote copy methods.
  (org-download--image/command): new function.
  (org-download--image/url-retrieve): new function.

Diffstat:
Morg-download.el | 79+++++++++++++++++++++++++++++++++++++++++++++++--------------------------------
1 file changed, 47 insertions(+), 32 deletions(-)

diff --git a/org-download.el b/org-download.el @@ -138,38 +138,53 @@ It's affected by `org-download-timestamp' and `org-download--dir'." (defun org-download--image (link filename) "Save LINK to FILENAME asynchronously and show inline images in current buffer." - (if (eq org-download-backend t) - (url-retrieve - link - (lambda (status filename buffer) - ;; Write current buffer to FILENAME - ;; and update inline images in BUFFER - (let ((err (plist-get status :error))) - (if err (error - "\"%s\" %s" link - (downcase (nth 2 (assq (nth 2 err) url-http-codes)))))) - (delete-region - (point-min) - (progn - (re-search-forward "\n\n" nil 'move) - (point))) - (let ((coding-system-for-write 'no-conversion)) - (write-region nil nil filename nil nil nil 'confirm)) - (with-current-buffer buffer - (org-display-inline-images))) - (list - (expand-file-name filename) - (current-buffer)) - nil t) - (require 'async) - (async-start - `(lambda() (shell-command - ,(format org-download-backend link - (expand-file-name filename)))) - (lexical-let ((cur-buf (current-buffer))) - (lambda(x) - (with-current-buffer cur-buf - (org-display-inline-images))))))) + (cond ((string-match "^file://\\(.*\\)" link) + (org-download--image/command + "cp \"%s\" \"%s\"" + (url-unhex-string + (match-string 1 link)) + filename)) + ((eq org-download-backend t) + (org-download--image/url-retrieve link filename)) + (t (org-download--image/command org-download-backend link filename)))) + +(defun org-download--image/command (command link filename) + "Using COMMAND, save LINK to FILENAME. +COMMAND is a format-style string with two slots for LINK and FILENAME." + (require 'async) + (async-start + `(lambda() (shell-command + ,(format command link + (expand-file-name filename)))) + (lexical-let ((cur-buf (current-buffer))) + (lambda(x) + (with-current-buffer cur-buf + (org-display-inline-images)))))) + +(defun org-download--image/url-retrieve (link filename) + "Save LINK to FILENAME using `url-retrieve'." + (url-retrieve + link + (lambda (status filename buffer) + ;; Write current buffer to FILENAME + ;; and update inline images in BUFFER + (let ((err (plist-get status :error))) + (if err (error + "\"%s\" %s" link + (downcase (nth 2 (assq (nth 2 err) url-http-codes)))))) + (delete-region + (point-min) + (progn + (re-search-forward "\n\n" nil 'move) + (point))) + (let ((coding-system-for-write 'no-conversion)) + (write-region nil nil filename nil nil nil 'confirm)) + (with-current-buffer buffer + (org-display-inline-images))) + (list + (expand-file-name filename) + (current-buffer)) + nil t)) (defun org-download-yank () "Call `org-download-image' with current kill."