From 76f71e39f583b7f8bb47a47b315f49af6f23886e Mon Sep 17 00:00:00 2001 From: Lars Ingebrigtsen Date: Wed, 29 Jul 2020 07:13:56 +0200 Subject: [PATCH] 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. --- doc/misc/eww.texi | 4 +++- etc/NEWS | 5 +++++ lisp/net/eww.el | 43 ++++++++++++++++++++++++++++++------------- 3 files changed, 38 insertions(+), 14 deletions(-) diff --git a/doc/misc/eww.texi b/doc/misc/eww.texi index 9bca0faa854..f9901b6fd78 100644 --- a/doc/misc/eww.texi +++ b/doc/misc/eww.texi @@ -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 diff --git a/etc/NEWS b/etc/NEWS index 650b95867fb..4c77162a256 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -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 diff --git a/lisp/net/eww.el b/lisp/net/eww.el index f4e3aa36c55..edb2f729c8b 100644 --- a/lisp/net/eww.el +++ b/lisp/net/eww.el @@ -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)) -- 2.39.2