emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: stardiviner <numbchild@gmail.com>
To: Org Mode <emacs-orgmode@gnu.org>
Subject: [PATCH] make org-attach-url download function as an option
Date: Wed, 29 Apr 2020 21:49:09 +0800	[thread overview]
Message-ID: <87zhaupfl6.fsf@gmail.com> (raw)

Hash: SHA256

I found when network is bad and slow, or the download file is big, the
org-attach-url will suspend Emacs for a long time. User might have to cancel
downloading, and start again later.

I hope to make "org-attach-url" download file asynchronously. But function
org-attach-attach hardcoded this function for 'url method. Here is a patch to
make it into an option. So user can specify a custom function to download file.

# ==============================================================================

BTW, I'm wandering why function "url-retrieve" download file is not complete and
failed. Bellowing code snippets are my try:

Use function ~url-copy-file~ works fine:

#+begin_src emacs-lisp :eval no
 (expand-file-name "~/kk.webp"))

I check out function ~url-copy-file~ implement, try to implement an async version like this:

#+begin_src emacs-lisp
(defun url-copy-file-asynchronous (url newname &optional ok-if-already-exists &rest _ignored)
  "Copy URL to NEWNAME.  Both arguments must be strings.
Signal a `file-already-exists' error if file NEWNAME already
exists, unless a third argument OK-IF-ALREADY-EXISTS is supplied
and non-nil.  An integer as third argument means request
confirmation if NEWNAME already exists."
  (and (file-exists-p newname)
       (or (not ok-if-already-exists)
           (and (integerp ok-if-already-exists)
                (not (yes-or-no-p
                      (format "File %s already exists; copy to it anyway? "
       (signal 'file-already-exists (list "File already exists" newname)))
  (let* ((buffer (or (url-retrieve url (lambda (&rest ignored)))
                     (signal 'file-missing
                             (list "Opening URL"
                                   "No such file or directory" url))))
         (handle (with-current-buffer buffer
                   (mm-dissect-buffer t))))
    (let ((mm-attachment-file-modes (default-file-modes)))
      (mm-save-part-to-file handle newname))
    (kill-buffer buffer)
    (mm-destroy-parts handle)))

(put 'copy-file 'url-file-handlers #'url-copy-file-asynchronous)

#+begin_src emacs-lisp
 (expand-file-name "~/kk.webp"))

But it download seems incomplete and failed. Don't know why.

Also there are extra async libraries support download file asynchronously. Like
"deferred", "async" etc. Those libraries can be used for user define their own

Is you have better idea, please tell me, thanks in advance.

- -- 
[ stardiviner ]
       I try to make every word tell the meaning that I want to express.

       Blog: https://stardiviner.github.io/
       IRC(freenode): stardiviner, Matrix: stardiviner
       GPG: F09F650D7D674819892591401B5DF1C95AE89AC3


             reply	other threads:[~2020-04-29 14:01 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-04-29 13:49 stardiviner [this message]
2020-05-24 14:01 ` [PATCH] make org-attach-url download function as an option Bastien
2020-05-25  0:12   ` stardiviner
2020-07-15  9:05   ` [QUESTION] " stardiviner
2020-07-16  0:52     ` Ihor Radchenko
2020-07-16 22:54       ` stardiviner
2020-07-16 23:20         ` Ihor Radchenko

Reply instructions:

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

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

  Avoid top-posting and favor interleaved quoting:

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

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

  git send-email \
    --in-reply-to=87zhaupfl6.fsf@gmail.com \
    --to=numbchild@gmail.com \
    --cc=emacs-orgmode@gnu.org \


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


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