Commit 76f71e39 authored by Lars Ingebrigtsen's avatar Lars Ingebrigtsen

Make eww use the XDG download directory

* lisp/net/eww.el (erc--download-directory): New function (bug#41030).
(eww-download-directory): Use it.
(eww-download): Use it.
(eww-download-callback): Adjust parameters.
parent bcf486c6
Pipeline #6160 failed with stage
in 17 seconds
......@@ -135,7 +135,9 @@ HTML-specified colors or not. This sets the @code{shr-use-colors} variable.
A URL can be downloaded with @kbd{d} (@code{eww-download}). This
will download the link under point if there is one, or else the URL of
the current page. The file will be written to the directory specified
in @code{eww-download-directory} (default: @file{~/Downloads/}).
by @code{eww-download-directory} (default: @file{~/Downloads/}, if it
exists; otherwise as specified by the @samp{DOWNLOAD} @acronym{XDG}
directory)).
@findex eww-back-url
@findex eww-forward-url
......
......@@ -492,6 +492,11 @@ behavior of rendering as wide as the window-width allows. If
** EWW
+++
*** 'eww-download-directory' will now use the XDG location, if defined.
However, if ~/Downloads/ already exists, that will continue to be
used.
---
*** The command 'eww-follow-link' now supports custom mailto handlers.
The function that is invoked when clicking on or otherwise following a
......
......@@ -32,6 +32,7 @@
(require 'thingatpt)
(require 'url)
(require 'url-queue)
(require 'xdg)
(eval-when-compile (require 'subr-x))
(defgroup eww nil
......@@ -55,11 +56,24 @@
:group 'eww
:type 'string)
(defcustom eww-download-directory "~/Downloads/"
"Directory where files will downloaded."
:version "24.4"
(defun erc--download-directory ()
"Return the name of the download directory.
If ~/Downloads/ exists, that will be used, and if not, the
DOWNLOAD XDG user directory will be returned. If that's
undefined, ~/Downloads/ is returned anyway."
(or (and (file-exists-p "~/Downloads/")
"~/Downloads/")
(when-let ((dir (xdg-user-dir "DOWNLOAD")))
(file-name-as-directory dir))
"~/Downloads/"))
(defcustom eww-download-directory 'erc--download-directory
"Directory where files will downloaded.
This should either be a directory name or a function (called with
no parameters) that returns a directory name."
:version "28.1"
:group 'eww
:type 'directory)
:type '(choice directory function))
;;;###autoload
(defcustom eww-suggest-uris
......@@ -1632,20 +1646,23 @@ Differences in #targets are ignored."
"Download URL to `eww-download-directory'.
Use link at point if there is one, else the current page's URL."
(interactive)
(access-file eww-download-directory "Download failed")
(let ((url (or (get-text-property (point) 'shr-url)
(eww-current-url))))
(if (not url)
(message "No URL under point")
(url-retrieve url #'eww-download-callback (list url)))))
(defun eww-download-callback (status url)
(let ((dir (if (stringp eww-download-directory)
eww-download-directory
(funcall eww-download-directory))))
(access-file dir "Download failed")
(let ((url (or (get-text-property (point) 'shr-url)
(eww-current-url))))
(if (not url)
(message "No URL under point")
(url-retrieve url #'eww-download-callback (list url dir))))))
(defun eww-download-callback (status url dir)
(unless (plist-get status :error)
(let* ((obj (url-generic-parse-url url))
(path (directory-file-name (car (url-path-and-query obj))))
(file (eww-make-unique-file-name
(eww-decode-url-file-name (file-name-nondirectory path))
eww-download-directory)))
dir)))
(goto-char (point-min))
(re-search-forward "\r?\n\r?\n")
(let ((coding-system-for-write 'no-conversion))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment