|
From: | Rick Frankel |
Subject: | Re: [O] Drag images from Firefox to org-mode |
Date: | Fri, 18 Oct 2013 10:44:37 -0400 |
User-agent: | Roundcube Webmail/0.9.0 |
On 2013-10-18 01:29, Nicolas Richard wrote:
Rick Frankel <address@hidden> writes: One small problem, should be =(1+ (point))=, as the above leaves a blank newline at the head of the jpg, making it invalid. Oops, yes [Initially I had (search-forward "\n\n"), which worked fine,... then changed my mind and didn't test. Silly me.] Your code downloaded two images easily, but (signal :error (cdr err)))) signals a weird error for me (something like: "error in process filter: if: peculiar error: http, 404"). I suggest: (error "Error fetching URL %s: %s" url (cdr err))
That seems fine. I was just following the suggestion in the doc string for `url-retrieve'. BTW, did you know that org already has a function which works perfectly for this purpose (well, it's synchronous, but otherwise...) `org-feed-get-feed'? It already has support for url-retrieve, curl and wget. Here's an implementation using it (which does not handle errors): #+BEGIN_SRC emacs-lisp (require 'org-feed) (defun fetch-image (url &optional destdir) (with-current-buffer (org-feed-get-feed url) (write-file (expand-file-name (file-name-nondirectory url) destdir)) (when (display-graphic-p) (pop-to-buffer (current-buffer))))) #+END_SRC and the current implementation with a nicer error message: #+BEGIN_SRC emacs-lisp (defun fetch-image (url &optional destdir) (url-retrieve url (lambda (status url destdir) (let ((err (plist-get status :error))) (if err (error "\"%s\" %s." url (downcase (nth 2 (assq (nth 2 err) url-http-codes)))))) (delete-region (point-min) (progn (re-search-forward "^$" nil 'move) (1+ (point)))) (write-file (expand-file-name (file-name-nondirectory url) destdir)) (when (display-graphic-p) (pop-to-buffer (current-buffer)))) `(,url ,destdir) nil t)) #+END_SRC both are fixed to only display the buffer if running under a window system (which can grok images.)
[Prev in Thread] | Current Thread | [Next in Thread] |