]> git.eshelyaron.com Git - emacs.git/commitdiff
New user option browse-url-transform-alist
authorStefan Kangas <stefankangas@gmail.com>
Wed, 18 Dec 2024 03:14:25 +0000 (04:14 +0100)
committerEshel Yaron <me@eshelyaron.com>
Mon, 23 Dec 2024 15:13:20 +0000 (16:13 +0100)
* 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)

etc/NEWS
lisp/net/browse-url.el

index ba7a6eacadbd7366e670348f679c5e883e2809e9..7e86bb36ba3a3d156aed5a039b43a520fdaf24c8 100644 (file)
--- 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'.
index df5886aa383cd1d98896c28282bfe61e8709dc5a..92ca27505a7db9371043f9ed4e4e5305bf0831dd 100644 (file)
@@ -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)))