From mboxrd@z Thu Jan 1 00:00:00 1970 From: Rick Frankel Subject: Re: Drag images from Firefox to org-mode Date: Fri, 18 Oct 2013 10:44:37 -0400 Message-ID: <9265ea6f8a8b76ddf5d77de837d057a8@mail.rickster.com> References: <871u3krrt3.fsf@yahoo.fr> <87r4bkq7g7.fsf@yahoo.fr> <8bb344a96ebde88ceca14f1b523bd5a8@mail.rickster.com> <87k3hbqh88.fsf@yahoo.fr> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:54382) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VXBIE-0001Ii-4A for emacs-orgmode@gnu.org; Fri, 18 Oct 2013 10:44:51 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1VXBI9-0001rT-3H for emacs-orgmode@gnu.org; Fri, 18 Oct 2013 10:44:46 -0400 Received: from [204.62.15.78] (port=34443 helo=mail.rickster.com) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VXBI8-0001p3-Vk for emacs-orgmode@gnu.org; Fri, 18 Oct 2013 10:44:41 -0400 In-Reply-To: <87k3hbqh88.fsf@yahoo.fr> List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: Nicolas Richard Cc: emacs-orgmode@gnu.org On 2013-10-18 01:29, Nicolas Richard wrote: > Rick Frankel 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.)