Re: [O] Drag images from Firefox to org-mode

2014-01-04 Thread Bastien
Hi Oleh,

Oleh  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



Re: [O] Drag images from Firefox to org-mode

2014-01-04 Thread Oleh
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
From 6d76577e80f8af2795321a9cb0f0e92bb02b8341 Mon Sep 17 00:00:00 2001
From: Oleh Krehel 
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 000..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 .
+
+;;; 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:
+
+
+(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 \"%

Re: [O] Drag images from Firefox to org-mode

2013-10-23 Thread Christian Moe

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



Re: [O] Drag images from Firefox to org-mode

2013-10-23 Thread Torsten Wagner
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@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  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@mailsystem.organisation.com:993/fetch>UID>/INBOX>1274
>
> I tried to use this address to call it with thunderbird
> via
> thunderbird -mail  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  wrote:
>> On Wed, Oct 23, 2013 at 7:13 PM, Rick Frankel  wrote:
>>> On 2013-10-20 16:42, Nicolas Richard wrote:

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

Re: [O] Drag images from Firefox to org-mode

2013-10-23 Thread Torsten Wagner
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@mailsystem.organisation.com:993/fetch>UID>/INBOX>1274

I tried to use this address to call it with thunderbird
via
thunderbird -mail  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  wrote:
> On Wed, Oct 23, 2013 at 7:13 PM, Rick Frankel  wrote:
>> On 2013-10-20 16:42, Nicolas Richard wrote:
>>>
>>> Oleh  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
>



Re: [O] Drag images from Firefox to org-mode

2013-10-23 Thread Oleh
On Wed, Oct 23, 2013 at 7:13 PM, Rick Frankel  wrote:
> On 2013-10-20 16:42, Nicolas Richard wrote:
>>
>> Oleh  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



Re: [O] Drag images from Firefox to org-mode

2013-10-23 Thread Rick Frankel

On 2013-10-20 16:42, Nicolas Richard wrote:

Oleh  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



Re: [O] Drag images from Firefox to org-mode

2013-10-21 Thread Oleh
> 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



Re: [O] Drag images from Firefox to org-mode

2013-10-20 Thread Nicolas Richard
Oleh  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.



Re: [O] Drag images from Firefox to org-mode

2013-10-19 Thread Oleh
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


Re: [O] Drag images from Firefox to org-mode

2013-10-18 Thread Rick Frankel
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







Re: [O] Drag images from Firefox to org-mode

2013-10-18 Thread Oleh
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  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
> ).
>
> Again, sorry I can't do a branch and pull request, don't have push
> access to github from here.
>
> rick


Re: [O] Drag images from Firefox to org-mode

2013-10-18 Thread Rick Frankel

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.

rickFrom 818b6a661087bfabb7363e06249bb925740f9616 Mon Sep 17 00:00:00 2001
From: Rick Frankel 
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 add

Re: [O] Drag images from Firefox to org-mode

2013-10-18 Thread Rick Frankel

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



Re: [O] Drag images from Firefox to org-mode

2013-10-18 Thread Nicolas Richard
Rick Frankel  writes:

> On 2013-10-18 01:29, Nicolas Richard wrote:
>> Rick Frankel  writes:
>> One small problem, should be =(1+ (point))=, as the above leaves a
>> blank newline at the head of the jpg, making it invalid.
>> 
>> Oops, yes [Initially I had (search-forward "\n\n"), which worked
>> fine,... then changed my mind and didn't test. Silly me.]
>> 
>> Your code downloaded two images easily, but
>> (signal :error (cdr err
>> signals a weird error for me (something like: "error in
>> process filter: if: peculiar error: http, 404"). I suggest:
>> 
>> (error "Error fetching URL %s: %s" url (cdr err))
>
> That seems fine. I was just following the suggestion in the doc string
> for `url-retrieve'.

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.



Re: [O] Drag images from Firefox to org-mode

2013-10-18 Thread Nicolas Richard
Rick Frankel  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.



Re: [O] Drag images from Firefox to org-mode

2013-10-18 Thread Rick Frankel

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



Re: [O] Drag images from Firefox to org-mode

2013-10-18 Thread Rick Frankel

On 2013-10-18 01:29, Nicolas Richard wrote:

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

Oops, yes [Initially I had (search-forward "\n\n"), which worked
fine,... then changed my mind and didn't test. Silly me.]

Your code downloaded two images easily, but
(signal :error (cdr err
signals a weird error for me (something like: "error in
process filter: if: peculiar error: http, 404"). I suggest:

(error "Error fetching URL %s: %s" url (cdr err))


That seems fine. I was just following the suggestion in the doc string
for `url-retrieve'.

BTW, did you know that org already has a function which works
perfectly for this purpose (well, it's synchronous, but otherwise...)
`org-feed-get-feed'?

It already has support for url-retrieve, curl and wget.

Here's an implementation using it (which does not handle errors):

#+BEGIN_SRC emacs-lisp
(require 'org-feed)
(defun fetch-image (url &optional destdir)
(with-current-buffer (org-feed-get-feed url)
(write-file
(expand-file-name (file-name-nondirectory url) destdir))
(when (display-graphic-p) (pop-to-buffer (current-buffer)
#+END_SRC

and the current implementation with a nicer error message:

#+BEGIN_SRC emacs-lisp
(defun fetch-image (url &optional destdir)
(url-retrieve
url
(lambda (status url destdir)
(let ((err (plist-get status :error)))
(if err (error
"\"%s\" %s." url
(downcase (nth 2 (assq (nth 2 err) url-http-codes))
(delete-region
(point-min)
(progn
(re-search-forward "^$" nil 'move)
(1+ (point
(write-file
(expand-file-name (file-name-nondirectory url) destdir))
(when (display-graphic-p) (pop-to-buffer (current-buffer
`(,url ,destdir) nil t))
#+END_SRC

both are fixed to only display the buffer if running under a window
system (which can grok images.)



Re: [O] Drag images from Firefox to org-mode

2013-10-18 Thread Oleh
>
>
> Did you try the url-retrieve or the url-retrieve-alt branch ?
>

The url-retrieve-alt branch.

Oleh


Re: [O] Drag images from Firefox to org-mode

2013-10-18 Thread Nicolas Richard
[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.




Re: [O] Drag images from Firefox to org-mode

2013-10-18 Thread Nicolas Richard
Oleh  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.



Re: [O] Drag images from Firefox to org-mode

2013-10-18 Thread Nicolas Richard
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.



Re: [O] Drag images from Firefox to org-mode

2013-10-18 Thread Oleh
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 <
theonewiththeevill...@yahoo.fr> wrote:

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


Re: [O] Drag images from Firefox to org-mode

2013-10-18 Thread Nicolas Richard
Oleh  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.



Re: [O] Drag images from Firefox to org-mode

2013-10-18 Thread Oleh
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
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  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 <
> drostekirs...@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  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
> >
> >
>
>


Re: [O] Drag images from Firefox to org-mode

2013-10-18 Thread Carsten Dominik
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  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  
> 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  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
> 
> 



signature.asc
Description: Message signed with OpenPGP using GPGMail


Re: [O] Drag images from Firefox to org-mode

2013-10-18 Thread Oleh
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 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  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
>
>


Re: [O] Drag images from Firefox to org-mode

2013-10-17 Thread Nicolas Richard
Rick Frankel  writes:
> One small problem, should be =(1+ (point))=, as the above leaves a
> blank newline at the head of the jpg, making it invalid.

Oops, yes [Initially I had (search-forward "\n\n"), which worked
fine,... then changed my mind and didn't test. Silly me.]

Your code downloaded two images easily, but
> (signal :error (cdr err
signals a weird error for me (something like: "error in
process filter: if: peculiar error: http, 404"). I suggest:

(error "Error fetching URL %s: %s" url (cdr err))

-- 
Nico.



Re: [O] Drag images from Firefox to org-mode

2013-10-17 Thread Rick Frankel

On 2013-10-17 10:48, Nicolas Richard wrote:

Rick Frankel  writes:
On 2013-10-17 08:43, Nicolas Richard wrote:
Carsten Dominik  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



Re: [O] Drag images from Firefox to org-mode

2013-10-17 Thread Nicolas Richard
Rick Frankel  writes:
> On 2013-10-17 08:43, Nicolas Richard wrote:
>> Carsten Dominik  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.



Re: [O] Drag images from Firefox to org-mode

2013-10-17 Thread Rick Frankel

On 2013-10-17 08:43, Nicolas Richard wrote:

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



Re: [O] Drag images from Firefox to org-mode

2013-10-17 Thread Nicolas Richard
Carsten Dominik  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.



Re: [O] Drag images from Firefox to org-mode

2013-10-17 Thread Carsten Dominik
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  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



signature.asc
Description: Message signed with OpenPGP using GPGMail


Re: [O] Drag images from Firefox to org-mode

2013-10-17 Thread Samuel Loury

Marcin Borkowski  writes:

> Dnia 2013-10-16, o godz. 17:52:51
> Oleh  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


signature.asc
Description: PGP signature


Re: [O] Drag images from Firefox to org-mode

2013-10-16 Thread Eric Abrahamsen
Oleh  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!




Re: [O] Drag images from Firefox to org-mode

2013-10-16 Thread Marcin Borkowski
Dnia 2013-10-16, o godz. 17:52:51
Oleh  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



Re: [O] Drag images from Firefox to org-mode

2013-10-16 Thread Oleh
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  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 
> 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  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
>>>
>>
>>
>


Re: [O] Drag images from Firefox to org-mode

2013-10-16 Thread Xebar Saram
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 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  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
>>
>
>


Re: [O] Drag images from Firefox to org-mode

2013-10-16 Thread Torsten Wagner
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  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
>


[O] Drag images from Firefox to org-mode

2013-10-16 Thread Oleh
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