>From 8a251f6f85ffa9ba3cf48d2c95ac27eeda58ac4d Mon Sep 17 00:00:00 2001 From: Ivan Kanis Date: Mon, 24 Jun 2013 18:15:05 +0200 Subject: [PATCH] use url-retrieve to download files --- emacs/misc/eww.el | 38 +++++++++++++++++++++++++++++++++++++- 1 files changed, 37 insertions(+), 1 deletions(-) diff --git a/emacs/misc/eww.el b/emacs/misc/eww.el index 7b37eda..96e70b1 100644 --- a/emacs/misc/eww.el +++ b/emacs/misc/eww.el @@ -50,6 +50,20 @@ :group 'eww :type 'string) +(defcustom eww-download-path "~" + "Path where files will downloaded." + :version "24.4" + :group 'eww + :type 'string) + +(defcustom eww-download-function nil + "Function used to download a link. +The function takes one argument. It is the URL of the link. It is +called in `shr-download'." + :version "24.4" + :group 'shr + :type 'function) + (defface eww-form-submit '((((type x w32 ns) (class color)) ; Like default mode line :box (:line-width 2 :style released-button) @@ -323,6 +337,7 @@ word(s) will be searched for via `eww-search-prefix'." (define-key map "u" 'eww-up-url) (define-key map "t" 'eww-top-url) (define-key map "w" 'eww-browse-with-external-browser) + (define-key map "d" 'eww-download) (define-key map "y" 'eww-yank-page-url) map)) @@ -845,6 +860,27 @@ The browser to used is specified by the `shr-external-browser' variable." (interactive) (message eww-current-url) (kill-new eww-current-url)) + +(defun eww-download () + "Download URL under point. +It calls `shr-dowload-function' with argument URL if it's non-nil." + (interactive) + (let ((url (get-text-property (point) 'shr-url))) + (if (not url) + (message "No URL under point") + (if eww-download-function + (funcall eww-download-function url) + (url-retrieve url 'eww-download-callback (list url)))))) + +(defun eww-download-callback (status url) + "Download callback." + (unless (plist-get status :error) + (let* ((obj (url-generic-parse-url url)) + (path (car (url-path-and-query obj))) + (file (file-name-nondirectory path))) + (write-file (expand-file-name file eww-download-path)) + (message (expand-file-name file eww-download-path)) + (message "File %s saved." file)))) + (provide 'eww) -;;; eww.el ends here -- 1.7.1