From: Stefan Kangas Date: Wed, 18 Dec 2024 03:14:25 +0000 (+0100) Subject: New user option browse-url-transform-alist X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=5aca052cec1a6005b6ee72e394161d80caf08feb;p=emacs.git New user option browse-url-transform-alist * lisp/net/browse-url.el (browse-url-transform-alist): New user option. (browse-url): Transform URL before loading if above new option is set. (cherry picked from commit 14a3dd6a30f7a720f29d51f2df8f8ab8138989a0) --- diff --git a/etc/NEWS b/etc/NEWS index ba7a6eacadb..7e86bb36ba3 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -217,6 +217,12 @@ modal editing packages. ** Browse URL +*** New user option 'browse-url-transform-alist'. +This user option is an alist that allows transforming URLs before asking +a web browser to load them. For example, it could be used like this: + (add-to-list 'browse-url-transform-alist + '("www.google.com" . "www.duckduckgo.com")) + *** New function 'browse-url-qutebrowser' for the Qutebrowser. For better integration with the Qutebrowser, set 'browse-url(-secondary)-browser-function' to 'browse-url-qutebrowser'. diff --git a/lisp/net/browse-url.el b/lisp/net/browse-url.el index df5886aa383..92ca27505a7 100644 --- a/lisp/net/browse-url.el +++ b/lisp/net/browse-url.el @@ -262,6 +262,23 @@ be used instead." :version "30.1" :type 'regexp) +(defcustom browse-url-transform-alist nil + "Alist of transformations to apply to URLs before loading it. +Each element has the form (ORIG . REPLACEMENT), where ORIG is a regular +expression and REPLACEMENT is the replacement text. Every element will +be tested in turn, allowing more than one transformation to be made. + +Note that ORIG and REPLACEMENT are passed as arguments to +`string-match', so you can, for example, use match groups in ORIG and +backreferences in REPLACEMENT." + :type '(choice + (const :tag "None" nil) + (alist + :tag "Alist mapping from regexp to replacement" + :key-type (regexp :tag "Regexp") + :value-type (regexp :tag "Replacement"))) + :version "31.1") + (defcustom browse-url-browser-display nil "The X display for running the browser, if not same as Emacs's." :type '(choice string (const :tag "Default" nil))) @@ -922,6 +939,10 @@ invert the prefix arg instead." (interactive (browse-url-interactive-arg "URL: ")) (unless (called-interactively-p 'interactive) (setq args (or args (list browse-url-new-window-flag)))) + (when browse-url-transform-alist + (dolist (trans browse-url-transform-alist) + (when (string-match (car trans) url) + (setq url (replace-match (cdr trans) nil t url))))) (when (and url-handler-mode (not (file-name-absolute-p url)) (not (string-match "\\`[a-z]+:" url)))