* net/eww.el (eww-download): New command and keystroke.
authorIvan Kanis <ivan@kanis.fr>
Tue, 25 Jun 2013 19:50:05 +0000 (21:50 +0200)
committerLars Magne Ingebrigtsen <larsi@gnus.org>
Tue, 25 Jun 2013 19:50:05 +0000 (21:50 +0200)
* net/eww.el (eww-make-unique-file-name): Create a unique file
name before saving to entering `y' accidentally asynchronously.

lisp/ChangeLog
lisp/net/eww.el

index 2f6f2df..1cba40d 100644 (file)
@@ -1,3 +1,12 @@
+2013-06-25  Lars Magne Ingebrigtsen  <larsi@gnus.org>
+
+       * net/eww.el (eww-make-unique-file-name): Create a unique file
+       name before saving to entering `y' accidentally asynchronously.
+
+2013-06-25  Ivan Kanis  <ivan@kanis.fr>
+
+       * net/eww.el (eww-download): New command and keystroke.
+
 2013-06-25  Lars Magne Ingebrigtsen  <larsi@gnus.org>
 
        * net/eww.el (eww-copy-page-url): Changed name of command.
index 9d6e427..2d6fe9a 100644 (file)
   :group 'eww
   :type 'string)
 
+(defcustom eww-download-path "~/Downloads/"
+  "Path where files will downloaded."
+  :version "24.4"
+  :group 'eww
+  :type 'string)
+
 (defface eww-form-submit
   '((((type x w32 ns) (class color))   ; Like default mode line
      :box (:line-width 2 :style released-button)
@@ -325,6 +331,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 "&" 'eww-browse-with-external-browser)
+    (define-key map "d" 'eww-download)
     (define-key map "w" 'eww-copy-page-url)
     map))
 
@@ -876,6 +883,40 @@ The browser to used is specified by the `shr-external-browser' variable."
   (message "%s" eww-current-url)
   (kill-new eww-current-url))
 
+(defun eww-download ()
+  "Download URL under point to `eww-download-directory'."
+  (interactive)
+  (let ((url (get-text-property (point) 'shr-url)))
+    (if (not url)
+        (message "No URL under point")
+      (url-retrieve url 'eww-download-callback (list url)))))
+
+(defun eww-download-callback (status url)
+  (unless (plist-get status :error)
+    (let* ((obj (url-generic-parse-url url))
+           (path (car (url-path-and-query obj)))
+           (file (eww-make-unique-file-name (file-name-nondirectory path)
+                                           eww-download-path)))
+      (write-file file)
+      (message "Saved %s" file))))
+
+(defun eww-make-unique-file-name (file directory)
+    (cond
+     ((zerop (length file))
+      (setq file "!"))
+     ((string-match "\\`[.]" file)
+      (setq file (concat "!" file))))
+    (let ((base file)
+         (count 1))
+      (while (file-exists-p (expand-file-name file directory))
+       (setq file
+             (if (string-match "\\`\\(.*\\)\\([.][^.]+\\)" file)
+                 (format "%s(%d)%s" (match-string 1 file)
+                         count (match-string 2 file))
+               (format "%s(%d)" file count)))
+       (setq count (1+ count)))
+      (expand-file-name file directory)))
+
 (provide 'eww)
 
 ;;; eww.el ends here