emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Drag images from Firefox to org-mode
@ 2013-10-16 10:04 Oleh
  2013-10-16 13:48 ` Torsten Wagner
  2013-10-17 12:04 ` Carsten Dominik
  0 siblings, 2 replies; 40+ messages in thread
From: Oleh @ 2013-10-16 10:04 UTC (permalink / raw)
  To: org mode

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

Hi all,

Here's a little hack that I use to make my life easier:

(require 'async)
(eval-when-compile
  (require 'cl))
(defun org-store-image (link basedir)
  (async-start
   `(lambda() (shell-command
          ,(format "wget \"%s\" -P \"%s\""
                   link
                   (expand-file-name basedir))))
   (lexical-let ((cur-buf (current-buffer)))
     (lambda(x)
       (with-current-buffer cur-buf
         (org-display-inline-images))))))

(defun org-store-image-clipboard (link)
  "Save image at address LINK to current directory's subdirectory DIR.
DIR is the name of the current level 0 heading."
  (interactive (list (current-kill 0)))
  (let ((filename (car (last (split-string link "/"))))
        (dir (save-excursion
               (org-up-heading-all (1- (org-current-level)))
               (substring-no-properties
                (org-get-heading)))))
    (if (null (image-type-from-file-name filename))
        (message "not an image URL")
      (unless (file-exists-p (expand-file-name filename dir))
        (org-store-image link dir))
      (insert (format "[[./%s/%s]]" dir filename))
      (org-display-inline-images))))

(setcdr (assoc "^\\(https?\\|ftp\\|file\\|nfs\\)://" dnd-protocol-alist)
'dnd-org-insert)

(defun dnd-org-insert (uri action)
  (org-store-image-clipboard uri))

When it's a plain image, I can just drag it from the browser to org-mode.
It will be downloaded in async to a subdirectory of the current directory
and the link will be inserted
at point.
For stubborn images that are links I can right click to copy image location
and call
`org-store-image-clipboard' interactively.

I hope it's useful to someone and that I'm not re-implementing standard
functionality.

regards,
Oleh

regards,
Oleh

[-- Attachment #2: Type: text/html, Size: 2471 bytes --]

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: Drag images from Firefox to org-mode
  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-17 12:04 ` Carsten Dominik
  1 sibling, 1 reply; 40+ messages in thread
From: Torsten Wagner @ 2013-10-16 13:48 UTC (permalink / raw)
  To: Oleh; +Cc: org mode

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

This looks like a nice feature!
Maybe you could add to save the address and date the picture was taken,
thus, one could easily refer to the image origin. That is sometimes of
importance.

Thanks for sharing

Torsten



On 16 October 2013 12:04, Oleh <ohwoeowho@gmail.com> wrote:

> Hi all,
>
> Here's a little hack that I use to make my life easier:
>
> (require 'async)
> (eval-when-compile
>   (require 'cl))
> (defun org-store-image (link basedir)
>   (async-start
>    `(lambda() (shell-command
>           ,(format "wget \"%s\" -P \"%s\""
>                    link
>                    (expand-file-name basedir))))
>    (lexical-let ((cur-buf (current-buffer)))
>      (lambda(x)
>        (with-current-buffer cur-buf
>          (org-display-inline-images))))))
>
> (defun org-store-image-clipboard (link)
>   "Save image at address LINK to current directory's subdirectory DIR.
> DIR is the name of the current level 0 heading."
>   (interactive (list (current-kill 0)))
>   (let ((filename (car (last (split-string link "/"))))
>         (dir (save-excursion
>                (org-up-heading-all (1- (org-current-level)))
>                (substring-no-properties
>                 (org-get-heading)))))
>     (if (null (image-type-from-file-name filename))
>         (message "not an image URL")
>       (unless (file-exists-p (expand-file-name filename dir))
>         (org-store-image link dir))
>       (insert (format "[[./%s/%s]]" dir filename))
>       (org-display-inline-images))))
>
> (setcdr (assoc "^\\(https?\\|ftp\\|file\\|nfs\\)://" dnd-protocol-alist)
> 'dnd-org-insert)
>
> (defun dnd-org-insert (uri action)
>   (org-store-image-clipboard uri))
>
> When it's a plain image, I can just drag it from the browser to org-mode.
> It will be downloaded in async to a subdirectory of the current directory
> and the link will be inserted
> at point.
> For stubborn images that are links I can right click to copy image
> location and call
> `org-store-image-clipboard' interactively.
>
> I hope it's useful to someone and that I'm not re-implementing standard
> functionality.
>
> regards,
> Oleh
>
> regards,
> Oleh
>

[-- Attachment #2: Type: text/html, Size: 3128 bytes --]

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: Drag images from Firefox to org-mode
  2013-10-16 13:48 ` Torsten Wagner
@ 2013-10-16 14:22   ` Xebar Saram
  2013-10-16 15:52     ` Oleh
  0 siblings, 1 reply; 40+ messages in thread
From: Xebar Saram @ 2013-10-16 14:22 UTC (permalink / raw)
  To: Torsten Wagner; +Cc: org mode, Oleh

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

Thx Oleh ,This is really amazing!

Ive been waiting for ages for something like this. a few questions
(well ok"requests" :)..) if you
dont mind. please bare in mind i have zero coding skills (or understanding
so my questions may be stupid :))

1.i use alot of images and :in alot of headings and sub headings and the
current method creates tens of folders. can you make an option to store all
images in a central location, IE ~/org/images? also Torstens suggestion of
adding a date to the file name makes alot of sense :)

2.can you add an option to add a local file (IE drag a jpg/png from a
folder) or even better yet build into the script a screenshot ability
function (using scrot etc) like in the following script:

http://lists.gnu.org/archive/html/emacs-orgmode/2013-05/msg00873.html


3.maybe perhaps a text scraper that would do the same for formatted text,
so one would mark a section in the browser, and drag it into org (and the
script would automagicly would get rid of the html yuckyness << notice my
highly technical coding terms and knowledge)

best and thanks alot!

Z

On Wed, Oct 16, 2013 at 4:48 PM, Torsten Wagner <torsten.wagner@gmail.com>wrote:

> This looks like a nice feature!
> Maybe you could add to save the address and date the picture was taken,
> thus, one could easily refer to the image origin. That is sometimes of
> importance.
>
> Thanks for sharing
>
> Torsten
>
>
>
> On 16 October 2013 12:04, Oleh <ohwoeowho@gmail.com> wrote:
>
>> Hi all,
>>
>> Here's a little hack that I use to make my life easier:
>>
>> (require 'async)
>> (eval-when-compile
>>   (require 'cl))
>> (defun org-store-image (link basedir)
>>   (async-start
>>    `(lambda() (shell-command
>>           ,(format "wget \"%s\" -P \"%s\""
>>                    link
>>                    (expand-file-name basedir))))
>>    (lexical-let ((cur-buf (current-buffer)))
>>      (lambda(x)
>>        (with-current-buffer cur-buf
>>          (org-display-inline-images))))))
>>
>> (defun org-store-image-clipboard (link)
>>   "Save image at address LINK to current directory's subdirectory DIR.
>> DIR is the name of the current level 0 heading."
>>   (interactive (list (current-kill 0)))
>>   (let ((filename (car (last (split-string link "/"))))
>>         (dir (save-excursion
>>                (org-up-heading-all (1- (org-current-level)))
>>                (substring-no-properties
>>                 (org-get-heading)))))
>>     (if (null (image-type-from-file-name filename))
>>         (message "not an image URL")
>>       (unless (file-exists-p (expand-file-name filename dir))
>>         (org-store-image link dir))
>>       (insert (format "[[./%s/%s]]" dir filename))
>>       (org-display-inline-images))))
>>
>> (setcdr (assoc "^\\(https?\\|ftp\\|file\\|nfs\\)://" dnd-protocol-alist)
>> 'dnd-org-insert)
>>
>> (defun dnd-org-insert (uri action)
>>   (org-store-image-clipboard uri))
>>
>> When it's a plain image, I can just drag it from the browser to org-mode.
>> It will be downloaded in async to a subdirectory of the current directory
>> and the link will be inserted
>> at point.
>> For stubborn images that are links I can right click to copy image
>> location and call
>> `org-store-image-clipboard' interactively.
>>
>> I hope it's useful to someone and that I'm not re-implementing standard
>> functionality.
>>
>> regards,
>> Oleh
>>
>> regards,
>> Oleh
>>
>
>

[-- Attachment #2: Type: text/html, Size: 5780 bytes --]

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: Drag images from Firefox to org-mode
  2013-10-16 14:22   ` Xebar Saram
@ 2013-10-16 15:52     ` Oleh
  2013-10-16 17:28       ` Marcin Borkowski
  2013-10-17  3:42       ` Eric Abrahamsen
  0 siblings, 2 replies; 40+ messages in thread
From: Oleh @ 2013-10-16 15:52 UTC (permalink / raw)
  To: Xebar Saram; +Cc: org mode

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

The address and date are auto-added as a comment now.
The updated source can be found at https://github.com/abo-abo/org-download

Images can be stored in a custom dir with:

(setq org-download-image-dir "~/Pictures/org")

I still want to add some rescaling option via ImageMagick and
an option for local files.
Other suggestions can be raised as issues on github, so it's
easier to keep track of them.

regards,
Oleh



On Wed, Oct 16, 2013 at 4:22 PM, Xebar Saram <zeltakc@gmail.com> wrote:

> Thx Oleh ,This is really amazing!
>
> Ive been waiting for ages for something like this. a few questions (well
> ok "requests" :)..) if you dont mind. please bare in mind i have zero
> coding skills (or understanding so my questions may be stupid :))
>
> 1.i use alot of images and :in alot of headings and sub headings and the
> current method creates tens of folders. can you make an option to store all
> images in a central location, IE ~/org/images? also Torstens suggestion
> of adding a date to the file name makes alot of sense :)
>
> 2.can you add an option to add a local file (IE drag a jpg/png from a
> folder) or even better yet build into the script a screenshot ability
> function (using scrot etc) like in the following script:
>
> http://lists.gnu.org/archive/html/emacs-orgmode/2013-05/msg00873.html
>
>
> 3.maybe perhaps a text scraper that would do the same for formatted text,
> so one would mark a section in the browser, and drag it into org (and the
> script would automagicly would get rid of the html yuckyness << notice my
> highly technical coding terms and knowledge)
>
> best and thanks alot!
>
> Z
>
>
> On Wed, Oct 16, 2013 at 4:48 PM, Torsten Wagner <torsten.wagner@gmail.com>wrote:
>
>> This looks like a nice feature!
>> Maybe you could add to save the address and date the picture was taken,
>> thus, one could easily refer to the image origin. That is sometimes of
>> importance.
>>
>> Thanks for sharing
>>
>> Torsten
>>
>>
>>
>> On 16 October 2013 12:04, Oleh <ohwoeowho@gmail.com> wrote:
>>
>>> Hi all,
>>>
>>> Here's a little hack that I use to make my life easier:
>>>
>>> (require 'async)
>>> (eval-when-compile
>>>   (require 'cl))
>>> (defun org-store-image (link basedir)
>>>   (async-start
>>>    `(lambda() (shell-command
>>>           ,(format "wget \"%s\" -P \"%s\""
>>>                    link
>>>                    (expand-file-name basedir))))
>>>    (lexical-let ((cur-buf (current-buffer)))
>>>      (lambda(x)
>>>        (with-current-buffer cur-buf
>>>          (org-display-inline-images))))))
>>>
>>> (defun org-store-image-clipboard (link)
>>>   "Save image at address LINK to current directory's subdirectory DIR.
>>> DIR is the name of the current level 0 heading."
>>>   (interactive (list (current-kill 0)))
>>>   (let ((filename (car (last (split-string link "/"))))
>>>         (dir (save-excursion
>>>                (org-up-heading-all (1- (org-current-level)))
>>>                (substring-no-properties
>>>                 (org-get-heading)))))
>>>     (if (null (image-type-from-file-name filename))
>>>         (message "not an image URL")
>>>       (unless (file-exists-p (expand-file-name filename dir))
>>>         (org-store-image link dir))
>>>       (insert (format "[[./%s/%s]]" dir filename))
>>>       (org-display-inline-images))))
>>>
>>> (setcdr (assoc "^\\(https?\\|ftp\\|file\\|nfs\\)://" dnd-protocol-alist)
>>> 'dnd-org-insert)
>>>
>>> (defun dnd-org-insert (uri action)
>>>   (org-store-image-clipboard uri))
>>>
>>> When it's a plain image, I can just drag it from the browser to
>>> org-mode.
>>> It will be downloaded in async to a subdirectory of the current
>>> directory and the link will be inserted
>>> at point.
>>> For stubborn images that are links I can right click to copy image
>>> location and call
>>> `org-store-image-clipboard' interactively.
>>>
>>> I hope it's useful to someone and that I'm not re-implementing standard
>>> functionality.
>>>
>>> regards,
>>> Oleh
>>>
>>> regards,
>>> Oleh
>>>
>>
>>
>

[-- Attachment #2: Type: text/html, Size: 6581 bytes --]

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: Drag images from Firefox to org-mode
  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
  1 sibling, 1 reply; 40+ messages in thread
From: Marcin Borkowski @ 2013-10-16 17:28 UTC (permalink / raw)
  To: emacs-orgmode

Dnia 2013-10-16, o godz. 17:52:51
Oleh <ohwoeowho@gmail.com> napisał(a):

> The address and date are auto-added as a comment now.
> The updated source can be found at
> https://github.com/abo-abo/org-download
> 
> Images can be stored in a custom dir with:
> 
> (setq org-download-image-dir "~/Pictures/org")
> 
> I still want to add some rescaling option via ImageMagick and
> an option for local files.
> Other suggestions can be raised as issues on github, so it's
> easier to keep track of them.

It's not something I need (at least not often), but I wanted to say
that this is really, really cool.  Thank you, sir.

> regards,
> Oleh

Best,

-- 
Marcin Borkowski
http://octd.wmi.amu.edu.pl/en/Marcin_Borkowski
Adam Mickiewicz University

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: Drag images from Firefox to org-mode
  2013-10-16 15:52     ` Oleh
  2013-10-16 17:28       ` Marcin Borkowski
@ 2013-10-17  3:42       ` Eric Abrahamsen
  1 sibling, 0 replies; 40+ messages in thread
From: Eric Abrahamsen @ 2013-10-17  3:42 UTC (permalink / raw)
  To: emacs-orgmode

Oleh <ohwoeowho@gmail.com> writes:

> The address and date are auto-added as a comment now.
> The updated source can be found at https://github.com/abo-abo/org-download
>
> Images can be stored in a custom dir with:
>
> (setq org-download-image-dir "~/Pictures/org")
>
> I still want to add some rescaling option via ImageMagick and 
> an option for local files.
> Other suggestions can be raised as issues on github, so it's 
> easier to keep track of them.

Very cool, thanks!

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: Drag images from Firefox to org-mode
  2013-10-16 17:28       ` Marcin Borkowski
@ 2013-10-17 11:49         ` Samuel Loury
  0 siblings, 0 replies; 40+ messages in thread
From: Samuel Loury @ 2013-10-17 11:49 UTC (permalink / raw)
  To: Marcin Borkowski, emacs-orgmode

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


Marcin Borkowski <mbork@wmi.amu.edu.pl> writes:

> Dnia 2013-10-16, o godz. 17:52:51
> Oleh <ohwoeowho@gmail.com> napisał(a):
>
>> The address and date are auto-added as a comment now.
>> The updated source can be found at
>> https://github.com/abo-abo/org-download
>> 
>> Images can be stored in a custom dir with:
>> 
>> (setq org-download-image-dir "~/Pictures/org")
>> 
>> I still want to add some rescaling option via ImageMagick and
>> an option for local files.
>> Other suggestions can be raised as issues on github, so it's
>> easier to keep track of them.
>
> It's not something I need (at least not often), but I wanted to say
> that this is really, really cool.  Thank you, sir.
Same here.

-- 
Konubinix
GPG Key    : 7439106A
Fingerprint: 5993 BE7A DA65 E2D9 06CE  5C36 75D2 3CED 7439 106A

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 489 bytes --]

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: Drag images from Firefox to org-mode
  2013-10-16 10:04 Drag images from Firefox to org-mode Oleh
  2013-10-16 13:48 ` Torsten Wagner
@ 2013-10-17 12:04 ` Carsten Dominik
  2013-10-17 12:43   ` Nicolas Richard
  2013-10-18  9:24   ` Oleh
  1 sibling, 2 replies; 40+ messages in thread
From: Carsten Dominik @ 2013-10-17 12:04 UTC (permalink / raw)
  To: Oleh; +Cc: org mode

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

Hi Oleh and others,

does anyone know how general this code is?  Does it works on different operating systems?
We might want to include this into the Org core.

- Carsten

On Oct 16, 2013, at 12:04 PM, Oleh <ohwoeowho@gmail.com> wrote:

> Hi all,
> 
> Here's a little hack that I use to make my life easier:
> 
> (require 'async)
> (eval-when-compile
>   (require 'cl))
> (defun org-store-image (link basedir)
>   (async-start
>    `(lambda() (shell-command
>           ,(format "wget \"%s\" -P \"%s\""
>                    link
>                    (expand-file-name basedir))))
>    (lexical-let ((cur-buf (current-buffer)))
>      (lambda(x)
>        (with-current-buffer cur-buf
>          (org-display-inline-images))))))
> 
> (defun org-store-image-clipboard (link)
>   "Save image at address LINK to current directory's subdirectory DIR.
> DIR is the name of the current level 0 heading."
>   (interactive (list (current-kill 0)))
>   (let ((filename (car (last (split-string link "/"))))
>         (dir (save-excursion
>                (org-up-heading-all (1- (org-current-level)))
>                (substring-no-properties
>                 (org-get-heading)))))
>     (if (null (image-type-from-file-name filename))
>         (message "not an image URL")
>       (unless (file-exists-p (expand-file-name filename dir))
>         (org-store-image link dir))
>       (insert (format "[[./%s/%s]]" dir filename))
>       (org-display-inline-images))))
> 
> (setcdr (assoc "^\\(https?\\|ftp\\|file\\|nfs\\)://" dnd-protocol-alist) 'dnd-org-insert)
> 
> (defun dnd-org-insert (uri action)
>   (org-store-image-clipboard uri))
> 
> When it's a plain image, I can just drag it from the browser to org-mode. 
> It will be downloaded in async to a subdirectory of the current directory and the link will be inserted 
> at point. 
> For stubborn images that are links I can right click to copy image location and call
> `org-store-image-clipboard' interactively.
> 
> I hope it's useful to someone and that I'm not re-implementing standard functionality.
> 
> regards,
> Oleh
> 
> regards,
> Oleh


[-- Attachment #2: Message signed with OpenPGP using GPGMail --]
[-- Type: application/pgp-signature, Size: 455 bytes --]

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: Drag images from Firefox to org-mode
  2013-10-17 12:04 ` Carsten Dominik
@ 2013-10-17 12:43   ` Nicolas Richard
  2013-10-17 14:04     ` Rick Frankel
  2013-10-18  9:24   ` Oleh
  1 sibling, 1 reply; 40+ messages in thread
From: Nicolas Richard @ 2013-10-17 12:43 UTC (permalink / raw)
  To: Carsten Dominik; +Cc: org mode, Oleh

Carsten Dominik <drostekirsten@gmail.com> writes:
> does anyone know how general this code is?  Does it works on different operating systems?
> We might want to include this into the Org core.

Since it is based on dnd, and since the documentation of dnd reads:
;; This file provides the generic handling of the drop part only.
;; Different DND backends (X11, W32, etc.) that handle the platform
;; specific DND parts call the functions here to do final delivery of
;; a drop.
I'd highly suspect that the code is portable.

This is my naive opinion, though

HTH,

-- 
Nico.

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: Drag images from Firefox to org-mode
  2013-10-17 12:43   ` Nicolas Richard
@ 2013-10-17 14:04     ` Rick Frankel
  2013-10-17 14:48       ` Nicolas Richard
  0 siblings, 1 reply; 40+ messages in thread
From: Rick Frankel @ 2013-10-17 14:04 UTC (permalink / raw)
  To: Nicolas Richard; +Cc: org mode, Carsten Dominik, Oleh

On 2013-10-17 08:43, Nicolas Richard wrote:
> Carsten Dominik <drostekirsten@gmail.com> writes:
> does anyone know how general this code is?  Does it works on different 
> operating systems?
> We might want to include this into the Org core.
> 
> Since it is based on dnd, and since the documentation of dnd reads:
> ;; This file provides the generic handling of the drop part only.
> ;; Different DND backends (X11, W32, etc.) that handle the platform
> ;; specific DND parts call the functions here to do final delivery of
> ;; a drop.
> I'd highly suspect that the code is portable.

It does make use of `wget', which may not be avalalable on all systems
(e.g., i believe os x only includes `curl' by default.) So it would
need to allow configuration of the image fetch command.

rick

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: Drag images from Firefox to org-mode
  2013-10-17 14:04     ` Rick Frankel
@ 2013-10-17 14:48       ` Nicolas Richard
  2013-10-17 16:51         ` Rick Frankel
  0 siblings, 1 reply; 40+ messages in thread
From: Nicolas Richard @ 2013-10-17 14:48 UTC (permalink / raw)
  To: Rick Frankel; +Cc: org mode, Carsten Dominik, Oleh

Rick Frankel <rick@rickster.com> writes:
> On 2013-10-17 08:43, Nicolas Richard wrote:
>> Carsten Dominik <drostekirsten@gmail.com> writes:
>> does anyone know how general this code is?  Does it works on
>> different operating systems?
>> We might want to include this into the Org core.
>>
>> Since it is based on dnd, and since the documentation of dnd reads:
>> ;; This file provides the generic handling of the drop part only.
>> ;; Different DND backends (X11, W32, etc.) that handle the platform
>> ;; specific DND parts call the functions here to do final delivery of
>> ;; a drop.
>> I'd highly suspect that the code is portable.
>
> It does make use of `wget', which may not be avalalable on all systems
> (e.g., i believe os x only includes `curl' by default.) So it would
> need to allow configuration of the image fetch command.


I'm sorry I missed that. Indeed, on the one "OS X" I ever tried to run
wget on, it didn't have wget.

Perhaps url-retrieve can be used instead. I have no time right now to
code it right, but here's a (synchronous) example with url + filetype +
filename all hardcoded :

(with-current-buffer
    (url-retrieve-synchronously
     "http://www.cnrtl.fr/images/css/bandeau.jpg")
  (delete-region
   (point-min)
   (progn
     (re-search-forward "^$" nil 'move)
     (point)))
  (write-file
   (expand-file-name "~/tmp/foobar.jpg"))
  (pop-to-buffer
   (current-buffer)))

using url-retrieve (which is asynchroneous)  might also lift the
requirement on async.el

-- 
Nico.

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: Drag images from Firefox to org-mode
  2013-10-17 14:48       ` Nicolas Richard
@ 2013-10-17 16:51         ` Rick Frankel
  2013-10-18  5:29           ` Nicolas Richard
  0 siblings, 1 reply; 40+ messages in thread
From: Rick Frankel @ 2013-10-17 16:51 UTC (permalink / raw)
  To: emacs-orgmode

On 2013-10-17 10:48, Nicolas Richard wrote:
> Rick Frankel <rick@rickster.com> writes:
> On 2013-10-17 08:43, Nicolas Richard wrote:
> Carsten Dominik <drostekirsten@gmail.com> writes:
> does anyone know how general this code is?  Does it works on
> different operating systems?
> We might want to include this into the Org core.
> 
> Since it is based on dnd, and since the documentation of dnd reads:
> ;; This file provides the generic handling of the drop part only.
> ;; Different DND backends (X11, W32, etc.) that handle the platform
> ;; specific DND parts call the functions here to do final delivery of
> ;; a drop.
> I'd highly suspect that the code is portable.
> 
> It does make use of `wget', which may not be avalalable on all systems
> (e.g., i believe os x only includes `curl' by default.) So it would
> need to allow configuration of the image fetch command.
> 
> 
> I'm sorry I missed that. Indeed, on the one "OS X" I ever tried to run
> wget on, it didn't have wget.
> 
> Perhaps url-retrieve can be used instead. I have no time right now to
> code it right, but here's a (synchronous) example with url + filetype +
> filename all hardcoded :
> 
> (with-current-buffer
> (url-retrieve-synchronously
> "http://www.cnrtl.fr/images/css/bandeau.jpg")
> (delete-region
> (point-min)
> (progn
> (re-search-forward "^$" nil 'move)
> (point)))
> (write-file
> (expand-file-name "~/tmp/foobar.jpg"))
> (pop-to-buffer
> (current-buffer)))
> 
> using url-retrieve (which is asynchroneous)  might also lift the
> requirement on async.el

One small problem, should be =(1+ (point))=, as the above leaves a
blank newline at the head of the jpg, making it invalid.

Here's an async implementation that seems to work ok:

#+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
(signal :error (cdr err))))
(delete-region
(point-min)
(progn
(re-search-forward "^$" nil 'move)
(1+ (point))))
(write-file
(expand-file-name (file-name-nondirectory url) destdir)
(pop-to-buffer
(current-buffer)))) `(,url ,destdir) nil t))
#+end_src

rick

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: Drag images from Firefox to org-mode
  2013-10-17 16:51         ` Rick Frankel
@ 2013-10-18  5:29           ` Nicolas Richard
  2013-10-18 14:44             ` Rick Frankel
  0 siblings, 1 reply; 40+ messages in thread
From: Nicolas Richard @ 2013-10-18  5:29 UTC (permalink / raw)
  To: Rick Frankel; +Cc: emacs-orgmode

Rick Frankel <rick@rickster.com> 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))

-- 
Nico.

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: Drag images from Firefox to org-mode
  2013-10-17 12:04 ` Carsten Dominik
  2013-10-17 12:43   ` Nicolas Richard
@ 2013-10-18  9:24   ` Oleh
  2013-10-18  9:28     ` Carsten Dominik
  2013-10-18  9:47     ` Nicolas Richard
  1 sibling, 2 replies; 40+ messages in thread
From: Oleh @ 2013-10-18  9:24 UTC (permalink / raw)
  To: Carsten Dominik; +Cc: org mode

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

Hi Carsten,

The dependencies are async, dnd and wget.

In case dnd doesn't work, there's a fallback to clipboard via
`current-kill'.

I've just added a custom variable that takes either 'wget or 'curl as
the download method.

I tried before url-retrieve, but it was giving me some garbage in
addition to the image. So unless I know reliably the format of the
garbage I can't use url-retrieve. But if I figure it out, I could add
url-retrieve as the third custom method.

The updates are at https://github.com/abo-abo/org-download

regards,
Oleh



On Thu, Oct 17, 2013 at 2:04 PM, Carsten Dominik <drostekirsten@gmail.com>wrote:

> Hi Oleh and others,
>
> does anyone know how general this code is?  Does it works on different
> operating systems?
> We might want to include this into the Org core.
>
> - Carsten
>
> On Oct 16, 2013, at 12:04 PM, Oleh <ohwoeowho@gmail.com> wrote:
>
> > Hi all,
> >
> > Here's a little hack that I use to make my life easier:
> >
> > (require 'async)
> > (eval-when-compile
> >   (require 'cl))
> > (defun org-store-image (link basedir)
> >   (async-start
> >    `(lambda() (shell-command
> >           ,(format "wget \"%s\" -P \"%s\""
> >                    link
> >                    (expand-file-name basedir))))
> >    (lexical-let ((cur-buf (current-buffer)))
> >      (lambda(x)
> >        (with-current-buffer cur-buf
> >          (org-display-inline-images))))))
> >
> > (defun org-store-image-clipboard (link)
> >   "Save image at address LINK to current directory's subdirectory DIR.
> > DIR is the name of the current level 0 heading."
> >   (interactive (list (current-kill 0)))
> >   (let ((filename (car (last (split-string link "/"))))
> >         (dir (save-excursion
> >                (org-up-heading-all (1- (org-current-level)))
> >                (substring-no-properties
> >                 (org-get-heading)))))
> >     (if (null (image-type-from-file-name filename))
> >         (message "not an image URL")
> >       (unless (file-exists-p (expand-file-name filename dir))
> >         (org-store-image link dir))
> >       (insert (format "[[./%s/%s]]" dir filename))
> >       (org-display-inline-images))))
> >
> > (setcdr (assoc "^\\(https?\\|ftp\\|file\\|nfs\\)://" dnd-protocol-alist)
> 'dnd-org-insert)
> >
> > (defun dnd-org-insert (uri action)
> >   (org-store-image-clipboard uri))
> >
> > When it's a plain image, I can just drag it from the browser to org-mode.
> > It will be downloaded in async to a subdirectory of the current
> directory and the link will be inserted
> > at point.
> > For stubborn images that are links I can right click to copy image
> location and call
> > `org-store-image-clipboard' interactively.
> >
> > I hope it's useful to someone and that I'm not re-implementing standard
> functionality.
> >
> > regards,
> > Oleh
> >
> > regards,
> > Oleh
>
>

[-- Attachment #2: Type: text/html, Size: 4074 bytes --]

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: Drag images from Firefox to org-mode
  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
  1 sibling, 1 reply; 40+ messages in thread
From: Carsten Dominik @ 2013-10-18  9:28 UTC (permalink / raw)
  To: Oleh; +Cc: org mode

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

Hi Oleh,

this is very nice thank you.

If you want, turn this into a patch to org.el.  If not, I will do so later.

- Carsten

On Oct 18, 2013, at 11:24 AM, Oleh <ohwoeowho@gmail.com> wrote:

> Hi Carsten,
> 
> The dependencies are async, dnd and wget.
> 
> In case dnd doesn't work, there's a fallback to clipboard via
> `current-kill'.
> 
> I've just added a custom variable that takes either 'wget or 'curl as
> the download method.
> 
> I tried before url-retrieve, but it was giving me some garbage in
> addition to the image. So unless I know reliably the format of the
> garbage I can't use url-retrieve. But if I figure it out, I could add
> url-retrieve as the third custom method.
> 
> The updates are at https://github.com/abo-abo/org-download
> 
> regards,
> Oleh
> 
> 
> 
> On Thu, Oct 17, 2013 at 2:04 PM, Carsten Dominik <drostekirsten@gmail.com> wrote:
> Hi Oleh and others,
> 
> does anyone know how general this code is?  Does it works on different operating systems?
> We might want to include this into the Org core.
> 
> - Carsten
> 
> On Oct 16, 2013, at 12:04 PM, Oleh <ohwoeowho@gmail.com> wrote:
> 
> > Hi all,
> >
> > Here's a little hack that I use to make my life easier:
> >
> > (require 'async)
> > (eval-when-compile
> >   (require 'cl))
> > (defun org-store-image (link basedir)
> >   (async-start
> >    `(lambda() (shell-command
> >           ,(format "wget \"%s\" -P \"%s\""
> >                    link
> >                    (expand-file-name basedir))))
> >    (lexical-let ((cur-buf (current-buffer)))
> >      (lambda(x)
> >        (with-current-buffer cur-buf
> >          (org-display-inline-images))))))
> >
> > (defun org-store-image-clipboard (link)
> >   "Save image at address LINK to current directory's subdirectory DIR.
> > DIR is the name of the current level 0 heading."
> >   (interactive (list (current-kill 0)))
> >   (let ((filename (car (last (split-string link "/"))))
> >         (dir (save-excursion
> >                (org-up-heading-all (1- (org-current-level)))
> >                (substring-no-properties
> >                 (org-get-heading)))))
> >     (if (null (image-type-from-file-name filename))
> >         (message "not an image URL")
> >       (unless (file-exists-p (expand-file-name filename dir))
> >         (org-store-image link dir))
> >       (insert (format "[[./%s/%s]]" dir filename))
> >       (org-display-inline-images))))
> >
> > (setcdr (assoc "^\\(https?\\|ftp\\|file\\|nfs\\)://" dnd-protocol-alist) 'dnd-org-insert)
> >
> > (defun dnd-org-insert (uri action)
> >   (org-store-image-clipboard uri))
> >
> > When it's a plain image, I can just drag it from the browser to org-mode.
> > It will be downloaded in async to a subdirectory of the current directory and the link will be inserted
> > at point.
> > For stubborn images that are links I can right click to copy image location and call
> > `org-store-image-clipboard' interactively.
> >
> > I hope it's useful to someone and that I'm not re-implementing standard functionality.
> >
> > regards,
> > Oleh
> >
> > regards,
> > Oleh
> 
> 


[-- Attachment #2: Message signed with OpenPGP using GPGMail --]
[-- Type: application/pgp-signature, Size: 455 bytes --]

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: Drag images from Firefox to org-mode
  2013-10-18  9:28     ` Carsten Dominik
@ 2013-10-18  9:33       ` Oleh
  0 siblings, 0 replies; 40+ messages in thread
From: Oleh @ 2013-10-18  9:33 UTC (permalink / raw)
  To: Carsten Dominik; +Cc: org mode

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

Hi Carsten,

You can commit it whenever you like.
I don't currently have access to Windows or OSX, so it's hard
to see if the code works everywhere.

regards,
Oleh


On Fri, Oct 18, 2013 at 11:28 AM, Carsten Dominik
<drostekirsten@gmail.com>wrote:

> Hi Oleh,
>
> this is very nice thank you.
>
> If you want, turn this into a patch to org.el.  If not, I will do so later.
>
> - Carsten
>
> On Oct 18, 2013, at 11:24 AM, Oleh <ohwoeowho@gmail.com> wrote:
>
> > Hi Carsten,
> >
> > The dependencies are async, dnd and wget.
> >
> > In case dnd doesn't work, there's a fallback to clipboard via
> > `current-kill'.
> >
> > I've just added a custom variable that takes either 'wget or 'curl as
> > the download method.
> >
> > I tried before url-retrieve, but it was giving me some garbage in
> > addition to the image. So unless I know reliably the format of the
> > garbage I can't use url-retrieve. But if I figure it out, I could add
> > url-retrieve as the third custom method.
> >
> > The updates are at https://github.com/abo-abo/org-download
> >
> > regards,
> > Oleh
> >
> >
> >
> > On Thu, Oct 17, 2013 at 2:04 PM, Carsten Dominik <
> drostekirsten@gmail.com> wrote:
> > Hi Oleh and others,
> >
> > does anyone know how general this code is?  Does it works on different
> operating systems?
> > We might want to include this into the Org core.
> >
> > - Carsten
> >
> > On Oct 16, 2013, at 12:04 PM, Oleh <ohwoeowho@gmail.com> wrote:
> >
> > > Hi all,
> > >
> > > Here's a little hack that I use to make my life easier:
> > >
> > > (require 'async)
> > > (eval-when-compile
> > >   (require 'cl))
> > > (defun org-store-image (link basedir)
> > >   (async-start
> > >    `(lambda() (shell-command
> > >           ,(format "wget \"%s\" -P \"%s\""
> > >                    link
> > >                    (expand-file-name basedir))))
> > >    (lexical-let ((cur-buf (current-buffer)))
> > >      (lambda(x)
> > >        (with-current-buffer cur-buf
> > >          (org-display-inline-images))))))
> > >
> > > (defun org-store-image-clipboard (link)
> > >   "Save image at address LINK to current directory's subdirectory DIR.
> > > DIR is the name of the current level 0 heading."
> > >   (interactive (list (current-kill 0)))
> > >   (let ((filename (car (last (split-string link "/"))))
> > >         (dir (save-excursion
> > >                (org-up-heading-all (1- (org-current-level)))
> > >                (substring-no-properties
> > >                 (org-get-heading)))))
> > >     (if (null (image-type-from-file-name filename))
> > >         (message "not an image URL")
> > >       (unless (file-exists-p (expand-file-name filename dir))
> > >         (org-store-image link dir))
> > >       (insert (format "[[./%s/%s]]" dir filename))
> > >       (org-display-inline-images))))
> > >
> > > (setcdr (assoc "^\\(https?\\|ftp\\|file\\|nfs\\)://"
> dnd-protocol-alist) 'dnd-org-insert)
> > >
> > > (defun dnd-org-insert (uri action)
> > >   (org-store-image-clipboard uri))
> > >
> > > When it's a plain image, I can just drag it from the browser to
> org-mode.
> > > It will be downloaded in async to a subdirectory of the current
> directory and the link will be inserted
> > > at point.
> > > For stubborn images that are links I can right click to copy image
> location and call
> > > `org-store-image-clipboard' interactively.
> > >
> > > I hope it's useful to someone and that I'm not re-implementing
> standard functionality.
> > >
> > > regards,
> > > Oleh
> > >
> > > regards,
> > > Oleh
> >
> >
>
>

[-- Attachment #2: Type: text/html, Size: 5074 bytes --]

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: Drag images from Firefox to org-mode
  2013-10-18  9:24   ` Oleh
  2013-10-18  9:28     ` Carsten Dominik
@ 2013-10-18  9:47     ` Nicolas Richard
  2013-10-18 10:04       ` Oleh
  1 sibling, 1 reply; 40+ messages in thread
From: Nicolas Richard @ 2013-10-18  9:47 UTC (permalink / raw)
  To: Oleh; +Cc: org mode, Carsten Dominik

Oleh <ohwoeowho@gmail.com> writes:
> I tried before url-retrieve, but it was giving me some garbage in
> addition to the image.

> But if I figure it out, I could add url-retrieve as the third custom
> method.

If the garbage is the HTTP headers, then that problem is solved in Rick
Frankel's code. If there's any other garbage, perhaps you have a test
case ?

-- 
Nico.

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: Drag images from Firefox to org-mode
  2013-10-18  9:47     ` Nicolas Richard
@ 2013-10-18 10:04       ` Oleh
  2013-10-18 10:23         ` Nicolas Richard
  2013-10-18 10:44         ` Nicolas Richard
  0 siblings, 2 replies; 40+ messages in thread
From: Oleh @ 2013-10-18 10:04 UTC (permalink / raw)
  To: Nicolas Richard; +Cc: org mode, Carsten Dominik

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

Hi Nicolas,

I've just tested Rick Frankel's code, and I got
a broken jpg (it's half green and distorted). It appears so
it org-mode inline and in eog, but it displays properly in its own Emacs
buffer.
Looks like it needs more work.

regards,
Oleh



On Fri, Oct 18, 2013 at 11:47 AM, Nicolas Richard <
theonewiththeevillook@yahoo.fr> wrote:

> Oleh <ohwoeowho@gmail.com> writes:
> > I tried before url-retrieve, but it was giving me some garbage in
> > addition to the image.
>
> > But if I figure it out, I could add url-retrieve as the third custom
> > method.
>
> If the garbage is the HTTP headers, then that problem is solved in Rick
> Frankel's code. If there's any other garbage, perhaps you have a test
> case ?
>
> --
> Nico.
>

[-- Attachment #2: Type: text/html, Size: 1372 bytes --]

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: Drag images from Firefox to org-mode
  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 10:44         ` Nicolas Richard
  1 sibling, 1 reply; 40+ messages in thread
From: Nicolas Richard @ 2013-10-18 10:23 UTC (permalink / raw)
  To: Oleh; +Cc: org mode, Carsten Dominik

Le 18/10/2013 12:04, Oleh a écrit :
> I've just tested Rick Frankel's code, and I got 
> a broken jpg (it's half green and distorted). It appears so
> it org-mode inline and in eog, but it displays properly in its own Emacs buffer.
> Looks like it needs more work.

Could you please put the link and the file you get on the internet so
they can be compared ?

I have no such problems and just tried to use the code in a new
'url-retrieve method to your org-download.el :
https://github.com/YoungFrog/org-download/tree/url-retrieve 
which worked fine for me

(in fact, I have problems with the async package : the emacs process
apparently doesn't stop ! but I don't think it's related to your code, I
know I have problems with inferior processes...)

-- 
Nico.

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: Drag images from Firefox to org-mode
  2013-10-18 10:04       ` Oleh
  2013-10-18 10:23         ` Nicolas Richard
@ 2013-10-18 10:44         ` Nicolas Richard
  1 sibling, 0 replies; 40+ messages in thread
From: Nicolas Richard @ 2013-10-18 10:44 UTC (permalink / raw)
  To: Oleh; +Cc: org mode, Carsten Dominik

Oleh <ohwoeowho@gmail.com> writes:
> I've just tested Rick Frankel's code, and I got 
> a broken jpg (it's half green and distorted). It appears so
> it org-mode inline and in eog, but it displays properly in its own
> Emacs buffer.
> Looks like it needs more work.

Perhaps you could test this version instead:
://github.com/YoungFrog/org-download/tree/url-retrieve-alt
It uses write-region instead of write-file and binds
coding-system-for-write to 'no-conversion

-- 
Nico.

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: Drag images from Firefox to org-mode
       [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
  0 siblings, 2 replies; 40+ messages in thread
From: Nicolas Richard @ 2013-10-18 11:00 UTC (permalink / raw)
  To: Oleh; +Cc: org mode

[re-adding emacs-orgmode@gnu.org to cc, I don't know when it got lost]

Le 18/10/2013 12:44, Oleh a écrit :
> I attach the downloaded google logo.

Thanks. diffing them, it looks pretty much like an encoding problem.

> I just tried your code and it works fine for the same image.

Did you try the url-retrieve or the url-retrieve-alt branch ?

In the former case, I'm surprised : it should have a similar problem IIUC.

-- 
Nico.

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: Drag images from Firefox to org-mode
  2013-10-18 11:00             ` Nicolas Richard
@ 2013-10-18 11:06               ` Oleh
  2013-10-18 15:03               ` Rick Frankel
  1 sibling, 0 replies; 40+ messages in thread
From: Oleh @ 2013-10-18 11:06 UTC (permalink / raw)
  To: Nicolas Richard; +Cc: org mode

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

>
>
> Did you try the url-retrieve or the url-retrieve-alt branch ?
>

The url-retrieve-alt branch.

Oleh

[-- Attachment #2: Type: text/html, Size: 375 bytes --]

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: Drag images from Firefox to org-mode
  2013-10-18  5:29           ` Nicolas Richard
@ 2013-10-18 14:44             ` Rick Frankel
  2013-10-18 15:39               ` Nicolas Richard
  0 siblings, 1 reply; 40+ messages in thread
From: Rick Frankel @ 2013-10-18 14:44 UTC (permalink / raw)
  To: Nicolas Richard; +Cc: emacs-orgmode

On 2013-10-18 01:29, Nicolas Richard wrote:
> Rick Frankel <rick@rickster.com> 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.)

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: Drag images from Firefox to org-mode
  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
  1 sibling, 1 reply; 40+ messages in thread
From: Rick Frankel @ 2013-10-18 15:03 UTC (permalink / raw)
  To: Nicolas Richard; +Cc: org mode, Oleh

On 2013-10-18 07:00, Nicolas Richard wrote:
> [re-adding emacs-orgmode@gnu.org to cc, I don't know when it got lost]
> 
> Le 18/10/2013 12:44, Oleh a écrit :
> I attach the downloaded google logo.

I missed an email here... can you send me the url to the image you are
having problems with? I just tried the google png on the home page
(logo11w.png), and have no problems. Also, please try the updated
version of fetch-image (in another thread, duplicated below).

FYI, I am use Emacs. 24.3 with the included url.el.


rick

#+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

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: Drag images from Firefox to org-mode
  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
  0 siblings, 2 replies; 40+ messages in thread
From: Nicolas Richard @ 2013-10-18 15:36 UTC (permalink / raw)
  To: Rick Frankel; +Cc: org mode, Oleh

Rick Frankel <rick@rickster.com> writes:
>> Le 18/10/2013 12:44, Oleh a écrit :
>> I attach the downloaded google logo.
>
> I missed an email here... can you send me the url to the image you are
> having problems with?

I received it by email, here it is :
http://cjoint.com/13oc/CJsrG1j2Szg.htm

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.

-- 
Nico.

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: Drag images from Firefox to org-mode
  2013-10-18 14:44             ` Rick Frankel
@ 2013-10-18 15:39               ` Nicolas Richard
  0 siblings, 0 replies; 40+ messages in thread
From: Nicolas Richard @ 2013-10-18 15:39 UTC (permalink / raw)
  To: Rick Frankel; +Cc: emacs-orgmode

Rick Frankel <rick@rickster.com> writes:

> On 2013-10-18 01:29, Nicolas Richard wrote:
>> Rick Frankel <rick@rickster.com> 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'.

Indeed, it is bizarre because err contains e.g. (error http 404), which is
claimed to be of the form ERROR-SYMBOL . DATA, but (car DATA) must be
a string according to (info "(elisp) Signaling Errors") :
,----
| For `error', the error message is
|      the CAR of DATA (that must be a string).
`----

> 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'?

No, I didn't know that (but I think doing things asynchronously has
added value).

> (let ((err (plist-get status :error)))
> (if err (error
> "\"%s\" %s." url
> (downcase (nth 2 (assq (nth 2 err) url-http-codes))))))

Nice

-- 
Nico.

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: Drag images from Firefox to org-mode
  2013-10-18 15:36                 ` Nicolas Richard
@ 2013-10-18 15:44                   ` Rick Frankel
  2013-10-18 18:24                   ` Rick Frankel
  1 sibling, 0 replies; 40+ messages in thread
From: Rick Frankel @ 2013-10-18 15:44 UTC (permalink / raw)
  To: Nicolas Richard; +Cc: org mode, Oleh

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.

got it. BTW, you might want to add the error message cleanup (i can't
do a branch/pull request from where i am right now).

Here's a diff:

--- org-download.el     2013-10-18 11:40:21.879753800 -0400
+++ org-download.el.new 2013-10-18 11:41:45.742415000 -0400
@@ -108,7 +108,8 @@
"Write current buffer to FILENAME and update inline images in BUFFER"
(let ((err (plist-get status :error)))
(if err
-               (signal :error (cdr err))))
+               (error "\"%s\" %s." url
+                      (downcase (nth 2 (assq (nth 2 err) 
url-http-codes))))))
(delete-region
(point-min)
(progn


rick

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: Drag images from Firefox to org-mode
  2013-10-18 15:36                 ` Nicolas Richard
  2013-10-18 15:44                   ` Rick Frankel
@ 2013-10-18 18:24                   ` Rick Frankel
  2013-10-18 19:22                     ` Oleh
  1 sibling, 1 reply; 40+ messages in thread
From: Rick Frankel @ 2013-10-18 18:24 UTC (permalink / raw)
  To: Nicolas Richard; +Cc: org mode, Oleh

[-- 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


^ permalink raw reply related	[flat|nested] 40+ messages in thread

* Re: Drag images from Firefox to org-mode
  2013-10-18 18:24                   ` Rick Frankel
@ 2013-10-18 19:22                     ` Oleh
  2013-10-18 20:45                       ` Rick Frankel
  0 siblings, 1 reply; 40+ messages in thread
From: Oleh @ 2013-10-18 19:22 UTC (permalink / raw)
  To: Rick Frankel; +Cc: Nicolas Richard, org mode

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

Thanks, Rick and Nico, I've merged the code.

All except the last bit from Rick: I can't think of any sensible
way to enter an image URL except pasting it.
And since link will be in the clipboard at some point, why not use
it straight away without prompting.

Maybe URL can be prompted for when called with a prefix arg,
but I don't know how to do that yet.

regards,
Oleh


On Fri, Oct 18, 2013 at 8:24 PM, Rick Frankel <rick@rickster.com> wrote:

> 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<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

[-- Attachment #2: Type: text/html, Size: 2142 bytes --]

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: Drag images from Firefox to org-mode
  2013-10-18 19:22                     ` Oleh
@ 2013-10-18 20:45                       ` Rick Frankel
  2013-10-19 10:34                         ` Oleh
  0 siblings, 1 reply; 40+ messages in thread
From: Rick Frankel @ 2013-10-18 20:45 UTC (permalink / raw)
  To: Oleh; +Cc: Nicolas Richard, org mode

On Fri, Oct 18, 2013 at 09:22:17PM +0200, Oleh wrote:
>    Thanks, Rick and Nico, I've merged the code.
>    All except the last bit from Rick: I can't think of any sensible
>    way to enter an image URL except pasting it.
>    And since link will be in the clipboard at some point, why not use
>    it straight away without prompting.
>    Maybe URL can be prompted for when called with a prefix arg,
>    but I don't know how to do that yet.

I strongly disagree, for a couple of reasons:

- Esp. on non-unix systems, the contents of the cut-buffer are not
  necessarily the same as the contents of the system clipboard. This
  is the problem I was having w/ your code on windows, where the
  cut-buffer contained the value of the current (or possibly last)
  selection, but the clipboard had a url.
- It is certainly possible to type in a url
- You might want to DND a url from another buffer.
- It is ergonomically wrong for an interactive function defined to
  take an argument (and called "download-image") to automagically use a
  value from somewhere else and not actually take an argument.

It would however, make sense for the function to use the current
clipboard contents if called with a prefix.

In the same vein, I was thinking about the automagically creating a
subdirectory based on a top level heading. This is also wrong for a
couple reasons:

- Creating a potentially =very long directory name with spaces= can
  cause multiple problems, both on unix and esp. non-unix systems,
  where spaces may not be allowed in directory names, or there may be
  limits on filename/directory length.
- The use of the top-level heading is arbitrary and may have nothing
  to do with the current context (for instance, i sometimes group
  together multiple documents to be output w/ export subtree in the
  same master document)

The default should be to save it in the same directory as the document
(which by the way, may not be in emacs's idea of the current
directory). Also, the filename should not be expanded to an absolute
path, which makes the document non-portable and will break publishing.

If you want the ability to create arbitrary output directories based
on document context, the customization can be made to accept a
function (lambda) as a value, which could then return a directory
prefix.

While i realize the current implementation meets your needs, if the
code is to be included in the org-mode distribution it should follow
emacs and org conventions and not contains any suprises.

I would be glad to make the above changes on tuesday when i get back
to the office. Please send me (again) the url of the github repos with
the current set of changes and I will update it so that it will allow
the current functionality but have less magical default behavior.

rick

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: Drag images from Firefox to org-mode
  2013-10-18 20:45                       ` Rick Frankel
@ 2013-10-19 10:34                         ` Oleh
  2013-10-20 20:42                           ` Nicolas Richard
  0 siblings, 1 reply; 40+ messages in thread
From: Oleh @ 2013-10-19 10:34 UTC (permalink / raw)
  To: Oleh, Nicolas Richard, org mode

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

Thanks for the feedback, Rick.

I strongly disagree, for a couple of reasons:
>
> - Esp. on non-unix systems, the contents of the cut-buffer are not
>   necessarily the same as the contents of the system clipboard. This
>   is the problem I was having w/ your code on windows, where the
>   cut-buffer contained the value of the current (or possibly last)
>   selection, but the clipboard had a url.
>

This can be fixed with a platform-aware function that checks both the
clipboard and the cut-buffer.


> - It is certainly possible to type in a url
>

URL to a site - maybe (still, probably 90% of people will copy it), but
an image where the relative path is just gibberish - certainly not.
And the point is that the clipboard is resorted to when drag-and-drop fails,
i.e. the image is a link and we want the link to the image, not the link
itself.
At that point, to get the address of the image, the user either clicks
"Copy Image Location" or "View Image". With "View Image" there's an extra
step of copying the URL from the address bar.
At this point, when URL is surely in the clipboard, so extra yanking and
confirming is just red tape.


> - You might want to DND a url from another buffer.
>
No problem - M-w in one buffer, and `org-download-image' in the other.


> - It is ergonomically wrong for an interactive function defined to
>   take an argument (and called "download-image") to automagically use a
>   value from somewhere else and not actually take an argument.
>
A new function with name `org-download-yank' can be added, no problem. And
Emacs is automagical by nature: I don't know why C-s C-w makes sense, but I
love it.


> It would however, make sense for the function to use the current
> clipboard contents if called with a prefix.
>
A customizable option can be added to configure this behavior.


>
> In the same vein, I was thinking about the automagically creating a
> subdirectory based on a top level heading. This is also wrong for a
> couple reasons:
>
> - Creating a potentially =very long directory name with spaces= can
>   cause multiple problems, both on unix and esp. non-unix systems,
>   where spaces may not be allowed in directory names, or there may be
>   limits on filename/directory length.
>
 This can be fixed by adding `org-download-max-filename-length`. When it
overflows, just resort to `default-directory' and provide a message/option
to fix.
By the way, which system doesn't allow for spaces in directory names?

- The use of the top-level heading is arbitrary and may have nothing
>   to do with the current context (for instance, i sometimes group
>   together multiple documents to be output w/ export subtree in the
>   same master document)
>
Can be configured with a different function name / file-local variable.
I just think that sorting inline links by headings for some level (not
necessarily top-level, configuration should be added) is a useful idea.

My use case is figures for weekly assignments for an online course:
each week has a top-level heading: all movies and slides go there, as
well as the figures for the assignments. I wouldn't want 15 weeks worth
of figures to be stored in one dir.


>
> The default should be to save it in the same directory as the document
> (which by the way, may not be in emacs's idea of the current
> directory). Also, the filename should not be expanded to an absolute
> path, which makes the document non-portable and will break publishing.
>
This is a very good remark.


>
> If you want the ability to create arbitrary output directories based
> on document context, the customization can be made to accept a
> function (lambda) as a value, which could then return a directory
> prefix.
>
This isn't an option for users with no elisp skills. Customization is a
nice way to show what kind of things can be done. And when a user
requires just a bit more tweaking after the customization, she can ask on
the list.

>
> While i realize the current implementation meets your needs, if the
> code is to be included in the org-mode distribution it should follow
> emacs and org conventions and not contains any suprises.
>
I agree of course. It's still work in progress.

>
> I would be glad to make the above changes on tuesday when i get back
> to the office. Please send me (again) the url of the github repos with
> the current set of changes and I will update it so that it will allow
> the current functionality but have less magical default behavior.
>
The repo is at https://github.com/abo-abo/org-download.
I'll probably add some fixes and functionality in the meantime.

regards,
Oleh

[-- Attachment #2: Type: text/html, Size: 7386 bytes --]

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: Drag images from Firefox to org-mode
  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
  0 siblings, 2 replies; 40+ messages in thread
From: Nicolas Richard @ 2013-10-20 20:42 UTC (permalink / raw)
  To: Oleh; +Cc: org mode

Oleh <ohwoeowho@gmail.com> writes:
> This can be fixed with a platform-aware function that checks both the
> clipboard and the cut-buffer.

I suggest to ask for a string with (read-string "URL: " nil nil default),
offering the content of the clipboard/kill ring/selections as the DEFAULT.
I think it's better to be explicit when downloading content, and there
isn't much overhead in asking for a confirmation.

> This can be fixed by adding `org-download-max-filename-length`. When
> it 
> overflows, just resort to `default-directory' and provide a
> message/option to fix.

I suggest that the right place for those files is as attachment, and
that's where customization should happen. The function choosing the
right place (i.e. org-download--dir) could be turned into a defvar or
defcustom which could then be set either to #'org-download--dir or
#'org-attach-dir.

> By the way, which system doesn't allow for spaces in directory names?

ISTR that MSDOS (except the latest versions) didn't allow spaces in
filenames :) Anyway, I tend to avoid spaces as much as I can in
filenames (including dirs)

>     If you want the ability to create arbitrary output directories
>     based
>     on document context, the customization can be made to accept a
>     function (lambda) as a value, which could then return a directory
>     prefix.
> This isn't an option for users with no elisp skills.

It is an option if there are enough examples/built-ins that can be used.
e.g. message-send-mail-function can be customized in this way, its
docstring gives a list of meaningful values.

-- 
Nico.

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: Drag images from Firefox to org-mode
  2013-10-20 20:42                           ` Nicolas Richard
@ 2013-10-21  7:24                             ` Oleh
  2013-10-23 17:13                             ` Rick Frankel
  1 sibling, 0 replies; 40+ messages in thread
From: Oleh @ 2013-10-21  7:24 UTC (permalink / raw)
  To: Nicolas Richard; +Cc: org mode

> I suggest to ask for a string with (read-string "URL: " nil nil default),
> offering the content of the clipboard/kill ring/selections as the DEFAULT.
> I think it's better to be explicit when downloading content, and there
> isn't much overhead in asking for a confirmation.

I've moved the clipboard stuff to `org-download-yank'.
`org-download-image' asks for the link now.

> > This can be fixed by adding `org-download-max-filename-length`. When
> > it
> > overflows, just resort to `default-directory' and provide a
> > message/option to fix.
>
> I suggest that the right place for those files is as attachment, and
> that's where customization should happen. The function choosing the
> right place (i.e. org-download--dir) could be turned into a defvar or
> defcustom which could then be set either to #'org-download--dir or
> #'org-attach-dir.

Yes, this should be added.

Oleh

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: Drag images from Firefox to org-mode
  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
  1 sibling, 1 reply; 40+ messages in thread
From: Rick Frankel @ 2013-10-23 17:13 UTC (permalink / raw)
  To: Nicolas Richard; +Cc: org mode, Oleh

On 2013-10-20 16:42, Nicolas Richard wrote:
> Oleh <ohwoeowho@gmail.com> writes:
> This can be fixed with a platform-aware function that checks both the
> clipboard and the cut-buffer.
> 
> I suggest to ask for a string with (read-string "URL: " nil nil 
> default),
> offering the content of the clipboard/kill ring/selections as the 
> DEFAULT.
> I think it's better to be explicit when downloading content, and there
> isn't much overhead in asking for a confirmation.

Agreed. Downloading stuff to your local machine should require a
confirmation step.

> This can be fixed by adding `org-download-max-filename-length`. When
> it
> overflows, just resort to `default-directory' and provide a
> message/option to fix.
> 
> I suggest that the right place for those files is as attachment, and
> that's where customization should happen. The function choosing the
> right place (i.e. org-download--dir) could be turned into a defvar or
> defcustom which could then be set either to #'org-download--dir or
> #'org-attach-dir.

Agreed. Also, the current "fix" (two variable and three functions!) is
overly complex. I propose that their be three options in the
defcustom:
- 'org-attach-directory (the default)
- user entered string
- a function (which could be set to `org-download-dir')

> By the way, which system doesn't allow for spaces in directory names?
> 
> ISTR that MSDOS (except the latest versions) didn't allow spaces in
> filenames :) Anyway, I tend to avoid spaces as much as I can in
> filenames (including dirs)

Also, sending files to external command/shell script for processing
often breaks if you have spaces in paths due to quoting issue.

> If you want the ability to create arbitrary output directories
> based
> on document context, the customization can be made to accept a
> function (lambda) as a value, which could then return a directory
> prefix.
> This isn't an option for users with no elisp skills.
> 
> It is an option if there are enough examples/built-ins that can be 
> used.
> e.g. message-send-mail-function can be customized in this way, its
> docstring gives a list of meaningful values.

Agreed.

rick

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: Drag images from Firefox to org-mode
  2013-10-23 17:13                             ` Rick Frankel
@ 2013-10-23 18:26                               ` Oleh
  2013-10-23 18:59                                 ` Torsten Wagner
  2014-01-04 15:01                                 ` Oleh
  0 siblings, 2 replies; 40+ messages in thread
From: Oleh @ 2013-10-23 18:26 UTC (permalink / raw)
  To: Rick Frankel; +Cc: Nicolas Richard, org mode

On Wed, Oct 23, 2013 at 7:13 PM, Rick Frankel <rick@rickster.com> wrote:
> On 2013-10-20 16:42, Nicolas Richard wrote:
>>
>> Oleh <ohwoeowho@gmail.com> writes:
>> This can be fixed with a platform-aware function that checks both the
>> clipboard and the cut-buffer.
>>
>> I suggest to ask for a string with (read-string "URL: " nil nil default),
>> offering the content of the clipboard/kill ring/selections as the DEFAULT.
>> I think it's better to be explicit when downloading content, and there
>> isn't much overhead in asking for a confirmation.
>
>
> Agreed. Downloading stuff to your local machine should require a
> confirmation step.

This is already in place.

>
>> This can be fixed by adding `org-download-max-filename-length`. When
>> it
>> overflows, just resort to `default-directory' and provide a
>> message/option to fix.
>>
>> I suggest that the right place for those files is as attachment, and
>> that's where customization should happen. The function choosing the
>> right place (i.e. org-download--dir) could be turned into a defvar or
>> defcustom which could then be set either to #'org-download--dir or
>> #'org-attach-dir.
>
>
> Agreed. Also, the current "fix" (two variable and three functions!) is
> overly complex.
Two variables can change independently in a meaningful way, giving
4 combinations with the possibility to alter just part of the setup on a file
local basis. Three functions specify the intent more clearly.

> I propose that their be three options in the
> defcustom:
> - 'org-attach-directory (the default)
> - user entered string
I don't get this one.

> - a function (which could be set to `org-download-dir')
This can be an option for `org-download-method'

>
>
>> By the way, which system doesn't allow for spaces in directory names?
>>
>> ISTR that MSDOS (except the latest versions) didn't allow spaces in
>> filenames :) Anyway, I tend to avoid spaces as much as I can in
>> filenames (including dirs)
>
>
> Also, sending files to external command/shell script for processing
> often breaks if you have spaces in paths due to quoting issue.
>
>
>> If you want the ability to create arbitrary output directories
>> based
>> on document context, the customization can be made to accept a
>> function (lambda) as a value, which could then return a directory
>> prefix.
>> This isn't an option for users with no elisp skills.
>>
>> It is an option if there are enough examples/built-ins that can be used.
>> e.g. message-send-mail-function can be customized in this way, its
>> docstring gives a list of meaningful values.
>
>
> Agreed.

There's still two functionality bits that I want to add:
local files and forwarding to default dnd handlers in the case it's
not an image.
But that can be added in later.
Please make the changes that you think are necessary and commit to org.

regards,
Oleh

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: Drag images from Firefox to org-mode
  2013-10-23 18:26                               ` Oleh
@ 2013-10-23 18:59                                 ` Torsten Wagner
  2013-10-23 20:01                                   ` Torsten Wagner
  2014-01-04 15:01                                 ` Oleh
  1 sibling, 1 reply; 40+ messages in thread
From: Torsten Wagner @ 2013-10-23 18:59 UTC (permalink / raw)
  To: Oleh; +Cc: Nicolas Richard, Rick Frankel, org mode

Hi,

just wonder if we can drag-and-drop images into org-mode could we
drag-and drop emails from e.g. thunderbird too. There is a plugin for
thunderbird call thunderlink which allows to generate org-mode
readable links.
However, drag and drop an email in a browser gives a address which looks like:

imap://user.name%2Borganisation.com@mailsystem.organisation.com:993/fetch>UID>/INBOX>1274

I tried to use this address to call it with thunderbird
via
thunderbird -mail <above url> but it failed.
with
permission denied: /INBOX

I guess it would be very attractive for many users if they could
create links to there email program by drag-and drop.

Just an idea

Torsten


On 23 October 2013 20:26, Oleh <ohwoeowho@gmail.com> wrote:
> On Wed, Oct 23, 2013 at 7:13 PM, Rick Frankel <rick@rickster.com> wrote:
>> On 2013-10-20 16:42, Nicolas Richard wrote:
>>>
>>> Oleh <ohwoeowho@gmail.com> writes:
>>> This can be fixed with a platform-aware function that checks both the
>>> clipboard and the cut-buffer.
>>>
>>> I suggest to ask for a string with (read-string "URL: " nil nil default),
>>> offering the content of the clipboard/kill ring/selections as the DEFAULT.
>>> I think it's better to be explicit when downloading content, and there
>>> isn't much overhead in asking for a confirmation.
>>
>>
>> Agreed. Downloading stuff to your local machine should require a
>> confirmation step.
>
> This is already in place.
>
>>
>>> This can be fixed by adding `org-download-max-filename-length`. When
>>> it
>>> overflows, just resort to `default-directory' and provide a
>>> message/option to fix.
>>>
>>> I suggest that the right place for those files is as attachment, and
>>> that's where customization should happen. The function choosing the
>>> right place (i.e. org-download--dir) could be turned into a defvar or
>>> defcustom which could then be set either to #'org-download--dir or
>>> #'org-attach-dir.
>>
>>
>> Agreed. Also, the current "fix" (two variable and three functions!) is
>> overly complex.
> Two variables can change independently in a meaningful way, giving
> 4 combinations with the possibility to alter just part of the setup on a file
> local basis. Three functions specify the intent more clearly.
>
>> I propose that their be three options in the
>> defcustom:
>> - 'org-attach-directory (the default)
>> - user entered string
> I don't get this one.
>
>> - a function (which could be set to `org-download-dir')
> This can be an option for `org-download-method'
>
>>
>>
>>> By the way, which system doesn't allow for spaces in directory names?
>>>
>>> ISTR that MSDOS (except the latest versions) didn't allow spaces in
>>> filenames :) Anyway, I tend to avoid spaces as much as I can in
>>> filenames (including dirs)
>>
>>
>> Also, sending files to external command/shell script for processing
>> often breaks if you have spaces in paths due to quoting issue.
>>
>>
>>> If you want the ability to create arbitrary output directories
>>> based
>>> on document context, the customization can be made to accept a
>>> function (lambda) as a value, which could then return a directory
>>> prefix.
>>> This isn't an option for users with no elisp skills.
>>>
>>> It is an option if there are enough examples/built-ins that can be used.
>>> e.g. message-send-mail-function can be customized in this way, its
>>> docstring gives a list of meaningful values.
>>
>>
>> Agreed.
>
> There's still two functionality bits that I want to add:
> local files and forwarding to default dnd handlers in the case it's
> not an image.
> But that can be added in later.
> Please make the changes that you think are necessary and commit to org.
>
> regards,
> Oleh
>

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: Drag images from Firefox to org-mode
  2013-10-23 18:59                                 ` Torsten Wagner
@ 2013-10-23 20:01                                   ` Torsten Wagner
  2013-10-24  6:47                                     ` Christian Moe
  0 siblings, 1 reply; 40+ messages in thread
From: Torsten Wagner @ 2013-10-23 20:01 UTC (permalink / raw)
  To: Oleh; +Cc: Nicolas Richard, Rick Frankel, org mode

Hi,

I have to correct myself, I could use the link created by thunderbird
drag and drop. I simply missed to put the link into quotes.

This works, thunderbird will open the message in a new view.
thunderbird -mails
"imap://user.name%2Borganisation.com@mailsystem.organisation.com:993/fetch>UID>/INBOX>1274"

However, the drawback with this, the url is rather fragile. It relies
on the folder and a simple number. I am not sure how much one could
rely that this number is always the same. Maybe imap or thunderbird
compress the list of emails by time effectively renumbering each email
in a folder. Furthermore, if I move the mail to another folder my
org-mode link would be broken too.

Thunderlink (a thunderbird plugin), instead is able to fetch the
message-id, which is unique and independent on the mail position. If
there is an interest to extend the drag and drop to emails, I could
ask the developer of thunderlink if there is a way to change the drag
and drop url to the message-id e.g. with the help of his plugin.
Thunderlink itself works already quite well with org-mode. The newest
version on git-hub allows to create direct org-mode links which one
could simply copy and paste from the clipboard. Guess only a small
amount of work would be needed to make it drag and drop ready too.

One could also think of saving the email as plain text in the org-mode
folder and create a link to that. However, I frighten this would
require more work. One idea would be to fetch the email by the message
id from the imap server from within emacs. But I guess this would
create a large amount of dependencies to make this working.

Greetings

Torsten



On 23 October 2013 20:59, Torsten Wagner <torsten.wagner@gmail.com> wrote:
> Hi,
>
> just wonder if we can drag-and-drop images into org-mode could we
> drag-and drop emails from e.g. thunderbird too. There is a plugin for
> thunderbird call thunderlink which allows to generate org-mode
> readable links.
> However, drag and drop an email in a browser gives a address which looks like:
>
> imap://user.name%2Borganisation.com@mailsystem.organisation.com:993/fetch>UID>/INBOX>1274
>
> I tried to use this address to call it with thunderbird
> via
> thunderbird -mail <above url> but it failed.
> with
> permission denied: /INBOX
>
> I guess it would be very attractive for many users if they could
> create links to there email program by drag-and drop.
>
> Just an idea
>
> Torsten
>
>
> On 23 October 2013 20:26, Oleh <ohwoeowho@gmail.com> wrote:
>> On Wed, Oct 23, 2013 at 7:13 PM, Rick Frankel <rick@rickster.com> wrote:
>>> On 2013-10-20 16:42, Nicolas Richard wrote:
>>>>
>>>> Oleh <ohwoeowho@gmail.com> writes:
>>>> This can be fixed with a platform-aware function that checks both the
>>>> clipboard and the cut-buffer.
>>>>
>>>> I suggest to ask for a string with (read-string "URL: " nil nil default),
>>>> offering the content of the clipboard/kill ring/selections as the DEFAULT.
>>>> I think it's better to be explicit when downloading content, and there
>>>> isn't much overhead in asking for a confirmation.
>>>
>>>
>>> Agreed. Downloading stuff to your local machine should require a
>>> confirmation step.
>>
>> This is already in place.
>>
>>>
>>>> This can be fixed by adding `org-download-max-filename-length`. When
>>>> it
>>>> overflows, just resort to `default-directory' and provide a
>>>> message/option to fix.
>>>>
>>>> I suggest that the right place for those files is as attachment, and
>>>> that's where customization should happen. The function choosing the
>>>> right place (i.e. org-download--dir) could be turned into a defvar or
>>>> defcustom which could then be set either to #'org-download--dir or
>>>> #'org-attach-dir.
>>>
>>>
>>> Agreed. Also, the current "fix" (two variable and three functions!) is
>>> overly complex.
>> Two variables can change independently in a meaningful way, giving
>> 4 combinations with the possibility to alter just part of the setup on a file
>> local basis. Three functions specify the intent more clearly.
>>
>>> I propose that their be three options in the
>>> defcustom:
>>> - 'org-attach-directory (the default)
>>> - user entered string
>> I don't get this one.
>>
>>> - a function (which could be set to `org-download-dir')
>> This can be an option for `org-download-method'
>>
>>>
>>>
>>>> By the way, which system doesn't allow for spaces in directory names?
>>>>
>>>> ISTR that MSDOS (except the latest versions) didn't allow spaces in
>>>> filenames :) Anyway, I tend to avoid spaces as much as I can in
>>>> filenames (including dirs)
>>>
>>>
>>> Also, sending files to external command/shell script for processing
>>> often breaks if you have spaces in paths due to quoting issue.
>>>
>>>
>>>> If you want the ability to create arbitrary output directories
>>>> based
>>>> on document context, the customization can be made to accept a
>>>> function (lambda) as a value, which could then return a directory
>>>> prefix.
>>>> This isn't an option for users with no elisp skills.
>>>>
>>>> It is an option if there are enough examples/built-ins that can be used.
>>>> e.g. message-send-mail-function can be customized in this way, its
>>>> docstring gives a list of meaningful values.
>>>
>>>
>>> Agreed.
>>
>> There's still two functionality bits that I want to add:
>> local files and forwarding to default dnd handlers in the case it's
>> not an image.
>> But that can be added in later.
>> Please make the changes that you think are necessary and commit to org.
>>
>> regards,
>> Oleh
>>

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: Drag images from Firefox to org-mode
  2013-10-23 20:01                                   ` Torsten Wagner
@ 2013-10-24  6:47                                     ` Christian Moe
  0 siblings, 0 replies; 40+ messages in thread
From: Christian Moe @ 2013-10-24  6:47 UTC (permalink / raw)
  To: Torsten Wagner; +Cc: org mode


Torsten Wagner writes:

> Thunderlink (a thunderbird plugin), instead is able to fetch the
> message-id, which is unique and independent on the mail position. If
> there is an interest to extend the drag and drop to emails, I could
> ask the developer of thunderlink if there is a way to change the drag
> and drop url to the message-id e.g. with the help of his plugin.
> Thunderlink itself works already quite well with org-mode. The newest
> version on git-hub allows to create direct org-mode links which one
> could simply copy and paste from the clipboard. Guess only a small
> amount of work would be needed to make it drag and drop ready too.
>

That's interesting. Last year I finally switched to mu4e after many
happy years with Thunderbird, in large part so I could finally store
robust links to messages from Org. If there's a solution for Thunderbird
I imagine many people would be glad to hear about it. (I don't think
I'll go back, though; mu4e rocks.)

Christian

^ permalink raw reply	[flat|nested] 40+ messages in thread

* Re: Drag images from Firefox to org-mode
  2013-10-23 18:26                               ` Oleh
  2013-10-23 18:59                                 ` Torsten Wagner
@ 2014-01-04 15:01                                 ` Oleh
  2014-01-04 15:17                                   ` Bastien
  1 sibling, 1 reply; 40+ messages in thread
From: Oleh @ 2014-01-04 15:01 UTC (permalink / raw)
  To: Rick Frankel; +Cc: Nicolas Richard, org mode

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

Hi all,

Here's a patch to add org-download to org-mode.
I've had the additional features (the screenshots and local files)
implemented some time ago,
but never got around to wrapping it up.
Feedback welcome.

regards
Oleh

> There's still two functionality bits that I want to add:
> local files and forwarding to default dnd handlers in the case it's
> not an image.
> But that can be added in later.
> Please make the changes that you think are necessary and commit to org.
>
> regards,
> Oleh

[-- Attachment #2: 0001-Add-image-drag-and-drop-and-screenshot-support-to-or.patch --]
[-- Type: text/x-patch, Size: 13220 bytes --]

From 6d76577e80f8af2795321a9cb0f0e92bb02b8341 Mon Sep 17 00:00:00 2001
From: Oleh Krehel <ohwoeowho@gmail.com>
Date: Sat, 4 Jan 2014 15:17:00 +0100
Subject: [PATCH] Add image drag-and-drop and screenshot support to org-mode.

---
 lisp/org-download.el | 336 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 336 insertions(+)
 create mode 100644 lisp/org-download.el

diff --git a/lisp/org-download.el b/lisp/org-download.el
new file mode 100644
index 0000000..39312cf
--- /dev/null
+++ b/lisp/org-download.el
@@ -0,0 +1,336 @@
+;;; org-download.el --- Image drag-and-drop for Emacs org-mode
+
+;; Copyright (C) 2014 Free Software Foundation, Inc.
+
+;; Author: Oleh Krehel
+;; Keywords: images, screenshots, download
+;; Homepage: http://orgmode.org
+
+;; This file is part of GNU Emacs.
+
+;; GNU Emacs is free software: you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
+
+;; GNU Emacs is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
+
+;;; Commentary:
+;;
+;; This extension facilitates moving images from point A to point B.
+;;
+;; Point A (the source) can be:
+;; 1. An image inside your browser that you can drag to Emacs.
+;; 2. An image on your file system that you can drag to Emacs.
+;; 3. A local or remote image address in kill-ring.
+;;    Use the `org-download-yank' command for this.
+;;    Remember that you can use "0 w" in `dired' to get an address.
+;; 4. An screenshot taken using `gnome-screenshot' or `scrot' or `gm'.
+;;    Use the `org-download-screenshot' command for this.
+;;    Customize the backend with  `org-download-screenshot-method'.
+;;
+;; Point B (the target) is an Emacs `org-mode' buffer where the inline
+;; link will be inserted.  Several customization options will determine
+;; where exactly on the file system the file will be stored.
+;;
+;; They are:
+;; `org-download-method':
+;; a. 'attach => use `org-mode' attachment machinery
+;; b. 'directory => construct the directory in two stages:
+;;    1. first part of the folder name is:
+;;       * either "." (current folder)
+;;       * or `org-download-image-dir' (if it's not nil).
+;;         `org-download-image-dir' becomes buffer-local when set,
+;;         so each file can customize this value, e.g with:
+;;         # -*- mode: Org; org-download-image-dir: ~/Pictures/foo; -*-
+;;    2. second part is:
+;;       * `org-download-heading-lvl' is nil => ""
+;;       * `org-download-heading-lvl' is n => the name of current
+;;         heading with level n. Level count starts with 0,
+;;         i.e. * is 0, ** is 1, *** is 2 etc.
+;;         `org-download-heading-lvl' becomes buffer-local when set,
+;;         so each file can customize this value, e.g with:
+;;         # -*- mode: Org; org-download-heading-lvl: nil; -*-
+;;
+;; `org-download-timestamp':
+;; optionally add a timestamp to the file name.
+;;
+;; Customize `org-download-backend' to choose between `url-retrieve'
+;; (the default) or `wget' or `curl'.
+;;
+;;; Code:
+\f
+
+(eval-when-compile
+  (require 'cl))
+(require 'url-parse)
+(require 'url-http)
+
+(defgroup org-download nil
+  "Image drag-and-drop for org-mode."
+  :group 'org
+  :prefix "org-download-")
+
+(defcustom org-download-method 'directory
+  "The way images should be stored."
+  :type '(choice
+          (const :tag "Directory" directory)
+          (const :tag "Attachment" attach))
+  :group 'org-download)
+
+(defcustom org-download-image-dir nil
+  "If set, images will be stored in this directory instead of \".\".
+See `org-download--dir-1' for more info."
+  :type '(choice
+          (const :tag "Default" nil)
+          (string :tag "Directory"))
+  :group 'org-download)
+(make-variable-buffer-local 'org-download-image-dir)
+
+(defcustom org-download-heading-lvl 0
+  "Heading level to be used in `org-download--dir-2'."
+  :group 'org-download)
+(make-variable-buffer-local 'org-download-heading-lvl)
+
+(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"
+  "This `format-time-string'-style string will be appended to the file name.
+Set this to \"\" if you don't want time stamps."
+  :type 'string
+  :group 'org-download)
+
+(defcustom org-download-screenshot-method "gnome-screenshot -a -f %s"
+  "The tool to capture screenshots."
+  :type '(choice
+          (const :tag "gnome-screenshot" "gnome-screenshot -a -f %s")
+          (const :tag "scrot" "scrot -s %s")
+          (const :tag "gm" "gm import %s"))
+  :group 'org-download)
+
+(defcustom org-download-image-width 0
+  "When non-zero add #+attr_html: :width tag to the image."
+  :type 'integer
+  :group 'org-download)
+
+(defun org-download-get-heading (lvl)
+  "Return the heading of the current entry's LVL level parent."
+  (save-excursion
+    (let ((cur-lvl (org-current-level)))
+      (unless (= cur-lvl 1)
+        (org-up-heading-all (- (1- (org-current-level)) lvl)))
+      (substring-no-properties
+       (org-get-heading)))))
+
+(defun org-download--dir-1 ()
+  "Return the first part of the directory path for `org-download--dir'.
+It's `org-download-image-dir', unless it's nil.  Then it's \".\"."
+  (or org-download-image-dir "."))
+
+(defun org-download--dir-2 ()
+  "Return the second part of the directory path for `org-download--dir'.
+Unless `org-download-heading-lvl' is nil, it's the name of the current
+`org-download-heading-lvl'-leveled heading.  Otherwise it's \"\"."
+  (and org-download-heading-lvl
+       (org-download-get-heading
+        org-download-heading-lvl)))
+
+(defun org-download--dir ()
+  "Return the directory path for image storage.
+
+The path is composed from `org-download--dir-1' and `org-download--dir-2'.
+The directory is created if it didn't exist before."
+  (let* ((part1 (org-download--dir-1))
+         (part2 (org-download--dir-2))
+         (dir (if part2
+                  (format "%s/%s" part1 part2)
+                part1)))
+    (unless (file-exists-p dir)
+      (make-directory dir t))
+    dir))
+
+(defun org-download--fullname (link)
+  "Return the file name where LINK will be saved to.
+
+It's affected by `org-download-timestamp' and `org-download--dir'."
+  (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
+            (file-name-sans-extension filename)
+            (format-time-string org-download-timestamp)
+            (file-name-extension filename))))
+
+(defun org-download--image (link filename)
+  "Save LINK to FILENAME asynchronously and show inline images in current buffer."
+  (when (string-match "^file://\\(.*\\)" link)
+    (setq link (url-unhex-string (match-string 1 link))))
+  (cond ((file-exists-p link)
+         (org-download--image/command "cp \"%s\" \"%s\"" 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."
+  (interactive)
+  (org-download-image (current-kill 0)))
+
+(defun org-download-screenshot ()
+  "Capture screenshot and insert the resulting file.
+The screenshot tool is determined by `org-download-screenshot-method'."
+  (interactive)
+  (let ((link "/tmp/screenshot.png"))
+    (shell-command (format org-download-screenshot-method link))
+    (org-download-image link)))
+
+(defun org-download-image (link)
+  "Save image at address LINK to `org-download--dir'."
+  (interactive "sUrl: ")
+  (let ((filename
+         (if (eq org-download-method 'attach)
+             (let ((org-download-image-dir (progn (require 'org-attach)
+                                                  (org-attach-dir t)))
+                   org-download-heading-lvl)
+               (org-download--fullname link))
+           (org-download--fullname link))))
+    (when (image-type-from-file-name filename)
+      (org-download--image link filename)
+      (when (eq org-download-method 'attach)
+        (org-attach-attach filename nil 'none))
+      (if (looking-back "^[ \t]+")
+          (delete-region (match-beginning 0) (match-end 0))
+        (newline))
+      (insert (format "#+DOWNLOADED: %s @ %s\n%s [[%s]]"
+                      link
+                      (format-time-string "%Y-%m-%d %H:%M:%S")
+                      (if (= org-download-image-width 0)
+                          ""
+                        (format "#+attr_html: :width %dpx\n" org-download-image-width))
+                      filename))
+      (org-display-inline-images))))
+
+(defun org-download--at-comment-p ()
+  "Check if current line begins with #+DOWLOADED:."
+  (save-excursion
+    (move-beginning-of-line nil)
+    (looking-at "#\\+DOWNLOADED:")))
+
+(defun org-download-delete ()
+  "Delete inline image link on current line, and the file that it points to."
+  (interactive)
+  (cond ((org-download--at-comment-p)
+         (delete-region (line-beginning-position)
+                        (line-end-position))
+         (org-download--delete (line-beginning-position)
+                               nil
+                               1))
+        ((region-active-p)
+         (org-download--delete (region-beginning)
+                               (region-end))
+         (delete-region (region-beginning)
+                        (region-end)))
+
+        (t (org-download--delete (line-beginning-position)
+                                 (line-end-position)))))
+
+(defun org-download--delete (beg end &optional times)
+  "Delete inline image links and the files they point to between BEG and END.
+
+When TIMES isn't nil, delete only TIMES links."
+  (unless times
+    (setq times most-positive-fixnum))
+  (save-excursion
+    (goto-char beg)
+    (while (and (>= (decf times) 0)
+                (re-search-forward "\\[\\[\\([^]]*\\)\\]\\]" end t))
+      (let ((str (match-string-no-properties 1)))
+        (delete-region (match-beginning 0)
+                       (match-end 0))
+        (when (file-exists-p str)
+          (delete-file str))))))
+
+(defun org-download-dnd (uri action)
+  "When in `org-mode' and URI points to image, download it.
+Otherwise, pass URI and ACTION back to dnd dispatch."
+  (if (eq major-mode 'org-mode)
+      ;; probably shouldn't redirect
+      (unless (org-download-image uri)
+        (message "not an image URL"))
+    ;; redirect to someone else
+    (let ((dnd-protocol-alist
+           (rassq-delete-all
+            'org-download-dnd
+            (copy-alist dnd-protocol-alist))))
+      (dnd-handle-one-url nil action uri))))
+
+(defun org-download-enable ()
+  "Enable org-download."
+  (unless (eq (cdr (assoc "^\\(https?\\|ftp\\|file\\|nfs\\)://" dnd-protocol-alist))
+              'org-download-dnd)
+    (setq dnd-protocol-alist
+          `(("^\\(https?\\|ftp\\|file\\|nfs\\)://" . org-download-dnd) ,@dnd-protocol-alist))))
+
+(defun org-download-disable ()
+  "Disable org-download."
+  (rassq-delete-all 'org-download-dnd dnd-protocol-alist))
+
+(org-download-enable)
+
+(provide 'org-download)
+
+;;; org-download.el ends here
-- 
1.8.4


^ permalink raw reply related	[flat|nested] 40+ messages in thread

* Re: Drag images from Firefox to org-mode
  2014-01-04 15:01                                 ` Oleh
@ 2014-01-04 15:17                                   ` Bastien
  0 siblings, 0 replies; 40+ messages in thread
From: Bastien @ 2014-01-04 15:17 UTC (permalink / raw)
  To: Oleh; +Cc: Nicolas Richard, Rick Frankel, org mode

Hi Oleh,

Oleh <ohwoeowho@gmail.com> writes:

> Here's a patch to add org-download to org-mode.

Great, thanks.

Please add this to contrib/lisp/ -- we try to keep Org's core
minimalistic because core is merged to Emacs.

Best,

-- 
 Bastien

^ permalink raw reply	[flat|nested] 40+ messages in thread

end of thread, other threads:[~2014-01-04 15:18 UTC | newest]

Thread overview: 40+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
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

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).