emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Rick Frankel <rick@rickster.com>
To: Nicolas Richard <theonewiththeevillook@yahoo.fr>
Cc: org mode <emacs-orgmode@gnu.org>, Oleh <ohwoeowho@gmail.com>
Subject: Re: Drag images from Firefox to org-mode
Date: Fri, 18 Oct 2013 14:24:45 -0400	[thread overview]
Message-ID: <857b1a922e24dda4404df4d19fabd7e2@mail.rickster.com> (raw)
In-Reply-To: <87y55qpp44.fsf@yahoo.fr>

[-- Attachment #1: Type: text/plain, Size: 840 bytes --]

On 2013-10-18 11:36, Nicolas Richard wrote:
> Note that using
> (let ((coding-system-for-write 'no-conversion))
> (write-region nil nil filename nil nil nil 'confirm))
> instead of
> (write-file ...)
> seemed to fix the problem for Oleh.

Attached is a patch against Nico's url-retrieve-alt which cleans-up
some issues w/ the defcustoms and make the interactive version of
`org-download-image' prompt for a url instead of attempting to use the
clipboard (which kept throwing errors for me). At least in my 
environment
(Chrome -> cygwin emacs on Windows NT),DND doesn't work on windows.

It also uses url-parse to get the image file name, as images with
cache-busters were failing to download (e.g.
http://example.com/image.jpg?20120101).

Again, sorry I can't do a branch and pull request, don't have push
access to github from here.

rick

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-org-download.el.patch --]
[-- Type: text/x-lisp; name=0001-org-download.el.patch, Size: 4670 bytes --]

From 818b6a661087bfabb7363e06249bb925740f9616 Mon Sep 17 00:00:00 2001
From: Rick Frankel <rick@rickster.com>
Date: Fri, 18 Oct 2013 14:03:03 -0400
Subject: [PATCH] * org-download.el ():   - org-download-image-dir: Fix
 mismatch error (string set to nil.)   -
 org-download--backend-cmd: Removed.   -
 org-download-backend: Use a choice instead of a string
 reprensenting   the tag. (org-download--fullname): Use
 url-parse to remove query params urls. Use  
 `file-name-nondirectory' instead of splitting on slashes.
 (org-download--image): Use `org-download-backend' directly
 and generate better error messages for `url-retrieve'
 failures. (org-download-image): Prompt for image url
 instead of attempting to use the clipboard directly. Fix
 docstring to better match implementation.

---
 org-download.el | 49 ++++++++++++++++++++++++-------------------------
 1 file changed, 24 insertions(+), 25 deletions(-)

diff --git a/org-download.el b/org-download.el
index e7ae1a1..1d9ca40 100644
--- a/org-download.el
+++ b/org-download.el
@@ -33,6 +33,7 @@
 
 (eval-when-compile
   (require 'cl))
+(require 'url-parse)
 
 (defgroup org-download nil
   "Image drag-and-drop for org-mode."
@@ -40,24 +41,18 @@
   :prefix "org-download-")
 
 (defcustom org-download-image-dir nil
-  "If not nil, `org-download-image' will store images here."
-  :type 'string
+  "If set, images will be stored in this directory
+instead of the default (see `org-download-image'.)"
+  :type '(choice (const :tag "Default" nil)
+                 (string :tag "Directory"))
   :group 'org-download)
 
-(defvar org-download--backend-cmd nil
-  "Backend command for downloading.
-
-Do not set this directly.  Customize `org-download-backend' instead.")
-
-(defcustom org-download-backend 'wget
-  "Set this to `wget' or `curl' or `url-retrieve'"
-  :set (lambda (symbol value)
-         (case value
-           (wget (setq org-download--backend-cmd "wget \"%s\" -O \"%s\""))
-           (curl (setq org-download--backend-cmd "curl \"%s\" -o \"%s\""))
-           (url-retrieve t)
-           (t (error "Unsupported key: %s" value)))
-         (set-default symbol value))
+(defcustom org-download-backend t
+  "Method to use for downloading"
+  :type '(choice
+          (const :tag "wget" "wget \"%s\" -O \"%s\"")
+          (const :tag "curl" "curl \"%s\" -o \"%s\"")
+          (const :tag "url-retrieve" t))
   :group 'org-download)
 
 (defcustom org-download-timestamp "_%Y-%m-%d_%H:%M:%S"
@@ -91,7 +86,10 @@ Set this to \"\" if you don't want time stamps."
 
 It's affected by `org-download-timestamp' and `org-download-image-dir'
 custom variables."
-  (let ((filename (car (last (split-string link "/"))))
+  (let ((filename
+         (file-name-nondirectory
+          (car (url-path-and-query
+                (url-generic-parse-url link)))))
         (dir (org-download--dir)))
     (format "%s/%s%s.%s"
             dir
@@ -101,14 +99,16 @@ custom variables."
 
 (defun org-download--image (link filename)
   "Save LINK to FILENAME asynchronously and show inline images in current buffer."
-  (if (eq org-download-backend 'url-retrieve)
+  (if (eq org-download-backend t)
       (url-retrieve
        link
        (lambda (status filename buffer)
-         "Write current buffer to FILENAME and update inline images in BUFFER"
+         ;; Write current buffer to FILENAME 
+         ;; and update inline images in BUFFER
          (let ((err (plist-get status :error)))
-           (if err
-               (signal :error (cdr err))))
+           (if err (error
+                    "\"%s\" %s." url
+                    (downcase (nth 2 (assq (nth 2 err) url-http-codes))))))
          (delete-region
           (point-min)
           (progn
@@ -125,16 +125,15 @@ custom variables."
     (require 'async)
     (async-start
      `(lambda() (shell-command
-                 ,(format org-download--backend-cmd link filename)))
+                 ,(format org-download--backend link filename)))
      (lexical-let ((cur-buf (current-buffer)))
        (lambda(x)
          (with-current-buffer cur-buf
            (org-display-inline-images)))))))
 
 (defun org-download-image (link)
-  "Save image at address LINK to current directory's sub-directory DIR.
-DIR is the name of the current level 0 heading."
-  (interactive (list (current-kill 0)))
+  "Save image at address LINK to a either the directory specified by `org-download-image-directory', or a subdirectory named after the current level 0 heading."
+  (interactive "sUrl: ")
   (let ((filename (org-download--fullname link)))
     (if (null (image-type-from-file-name filename))
         (message "not an image URL")
-- 
1.8.0


  parent reply	other threads:[~2013-10-18 18:24 UTC|newest]

Thread overview: 40+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-10-16 10:04 Drag images from Firefox to org-mode Oleh
2013-10-16 13:48 ` Torsten Wagner
2013-10-16 14:22   ` Xebar Saram
2013-10-16 15:52     ` Oleh
2013-10-16 17:28       ` Marcin Borkowski
2013-10-17 11:49         ` Samuel Loury
2013-10-17  3:42       ` Eric Abrahamsen
2013-10-17 12:04 ` Carsten Dominik
2013-10-17 12:43   ` Nicolas Richard
2013-10-17 14:04     ` Rick Frankel
2013-10-17 14:48       ` Nicolas Richard
2013-10-17 16:51         ` Rick Frankel
2013-10-18  5:29           ` Nicolas Richard
2013-10-18 14:44             ` Rick Frankel
2013-10-18 15:39               ` Nicolas Richard
2013-10-18  9:24   ` Oleh
2013-10-18  9:28     ` Carsten Dominik
2013-10-18  9:33       ` Oleh
2013-10-18  9:47     ` Nicolas Richard
2013-10-18 10:04       ` Oleh
2013-10-18 10:23         ` Nicolas Richard
     [not found]           ` <CAA01p3obTUXshcS-m=iB2KOADAx9eNpj8PK_F5O1x5dLtCobcA@mail.gmail.com>
2013-10-18 11:00             ` Nicolas Richard
2013-10-18 11:06               ` Oleh
2013-10-18 15:03               ` Rick Frankel
2013-10-18 15:36                 ` Nicolas Richard
2013-10-18 15:44                   ` Rick Frankel
2013-10-18 18:24                   ` Rick Frankel [this message]
2013-10-18 19:22                     ` Oleh
2013-10-18 20:45                       ` Rick Frankel
2013-10-19 10:34                         ` Oleh
2013-10-20 20:42                           ` Nicolas Richard
2013-10-21  7:24                             ` Oleh
2013-10-23 17:13                             ` Rick Frankel
2013-10-23 18:26                               ` Oleh
2013-10-23 18:59                                 ` Torsten Wagner
2013-10-23 20:01                                   ` Torsten Wagner
2013-10-24  6:47                                     ` Christian Moe
2014-01-04 15:01                                 ` Oleh
2014-01-04 15:17                                   ` Bastien
2013-10-18 10:44         ` Nicolas Richard

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=857b1a922e24dda4404df4d19fabd7e2@mail.rickster.com \
    --to=rick@rickster.com \
    --cc=emacs-orgmode@gnu.org \
    --cc=ohwoeowho@gmail.com \
    --cc=theonewiththeevillook@yahoo.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).