emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] make org-attach-url download function as an option


From: stardiviner
Subject: [PATCH] make org-attach-url download function as an option
Date: Wed, 29 Apr 2020 21:49:09 +0800
User-agent: mu4e 1.4; emacs 28.0.50

-----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 via email to

[Prev in Thread] Current Thread [Next in Thread]