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)

-----BEGIN PGP SIGNED MESSAGE-----
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
(url-copy-file
 "https://img1.doubanio.com/view/photo/s_ratio_poster/public/p2592521688.webp"
 (expand-file-name "~/kk.webp"))
#+end_src

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? "
                              newname)))))
       (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)
#+end_src

#+begin_src emacs-lisp
(url-copy-file-asynchronous
 "https://img1.doubanio.com/view/photo/s_ratio_poster/public/p2592521688.webp"
 (expand-file-name "~/kk.webp"))
#+end_src

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

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
      
-----BEGIN PGP SIGNATURE-----

iQFIBAEBCAAyFiEE8J9lDX1nSBmJJZFAG13xyVromsMFAl6phdUUHG51bWJjaGls
ZEBnbWFpbC5jb20ACgkQG13xyVromsNswQgAjuFtM/+WU0EXdpQOx3C4kymgUTex
jEZgw96JlfHSP3JcoOuA5Ll2F+nXShWwIeGJAe1aRgjLTy9O5XSZO6BdlxoN2QRB
RfrZvl5X5cJu9NIyiwhSYbIMM8+DJKHSp/nBGIimXwDoNU/ye43E4OmApVKfWjf0
TmMXUzLnXWSxdSubFe/M7SEGUaarFI5y2bNwytBMOQr7q1yY6UgynlB6Vda06mqb
BO/AnJgYtP69XvghK14NJ6/X8nol5oFijLtt67QUNKBCdUlp3tx/G+6lFebGruHS
kZ6PjTzoAwZOk5l7lTx9mG+dle1g/7rNce1TRVFP+cT1+qS5+7UHhwfnwg==
=pYDj
-----END PGP SIGNATURE-----


             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:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  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 \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* 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

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