Subject: bug#75052: 31; browse-url-transform-alist is not used by secondary browser

From: Daniel Mendler <[email-masked]>
Date: Sat, 04 Jan 2025 12:59:00 +0100

Daniel Mendler <[email-masked]> writes:

> Yes, I'll send a patch and then you can take another look.

Updated patch attached to this mail.

> Daniel

>From a859519c83e73b9a61795c2c9200e1b853136b9d Mon Sep 17 00:00:00 2001
From: Daniel Mendler <[email-masked]>
Date: Tue, 24 Dec 2024 01:31:35 +0100
Subject: [PATCH] Call browser functions via `browse-url'

`browse-url' applies `browse-url-transform-alist'
transformations to the URL argument and initializes appropriate
environment variables.  Therefore always call browser functions
via `browse-url' and not directly, by let-binding
`browse-url-browser-function'.

* lisp/net/browse-url.el (browse-url-with-browser-kind)
(browse-url-button-open, browse-url-button-open-url):
* lisp/net/shr.el (shr-browse-url):
* lisp/net/eww.el (eww-browse-with-external-browser):
* lisp/gnus/gnus-sum.el (gnus-summary-browse-url):
* lisp/emacs-lisp/package.el (package-browse-url): Let-bind
`browse-url-browser-function' and call `browse-url'.
---
 lisp/emacs-lisp/package.el |  6 ++++--
 lisp/gnus/gnus-sum.el      | 10 ++++++----
 lisp/net/browse-url.el     | 23 +++++++++++++++--------
 lisp/net/eww.el            |  3 ++-
 lisp/net/shr.el            |  9 +++++----
 5 files changed, 32 insertions(+), 19 deletions(-)

diff --git a/lisp/emacs-lisp/package.el b/lisp/emacs-lisp/package.el
index b4a33db1a77..6a04108b9f8 100644
--- a/lisp/emacs-lisp/package.el
+++ b/lisp/emacs-lisp/package.el
@@ -4692,8 +4692,10 @@ package-browse-url
   (let ((url (cdr (assoc :url (package-desc-extras desc)))))
     (unless url
       (user-error "No website for %s" (package-desc-name desc)))
-    (if secondary
-        (funcall browse-url-secondary-browser-function url)
+    (let ((browse-url-browser-function
+           (if secondary
+               browse-url-secondary-browser-function
+             browse-url-browser-function)))
       (browse-url url))))
 
 (declare-function ietf-drums-parse-address "ietf-drums"
diff --git a/lisp/gnus/gnus-sum.el b/lisp/gnus/gnus-sum.el
index c909d9cfd5c..06367b7293e 100644
--- a/lisp/gnus/gnus-sum.el
+++ b/lisp/gnus/gnus-sum.el
@@ -9408,7 +9408,7 @@ gnus-shorten-url
                            (concat "#" target)))))
       (concat host (string-truncate-left rest (- max (length host)))))))
 
-(defun gnus-summary-browse-url (&optional external)
+(defun gnus-summary-browse-url (&optional secondary)
   "Scan the current article body for links, and offer to browse them.
 
 Links are opened using `browse-url' unless a prefix argument is
@@ -9429,9 +9429,11 @@ gnus-summary-browse-url
 				 (gnus-shorten-url (car urls) 40))
 		  urls nil t nil nil (car urls))))))
     (if target
-	(if external
-	    (funcall browse-url-secondary-browser-function target)
-	  (browse-url target))
+        (let ((browse-url-browser-function
+               (if secondary
+                   browse-url-secondary-browser-function
+                 browse-url-browser-function)))
+          (browse-url target))
       (message "No URLs found."))))
 
 (defun gnus-summary-isearch-article (&optional regexp-p)
diff --git a/lisp/net/browse-url.el b/lisp/net/browse-url.el
index b9610fec150..e67c2fd79da 100644
--- a/lisp/net/browse-url.el
+++ b/lisp/net/browse-url.el
@@ -999,7 +999,10 @@ browse-url-with-browser-kind
                    browse-url-secondary-browser-function
                    #'browse-url-default-browser
                    #'eww))))
-    (funcall function url arg)))
+    (let ((browse-url-browser-function function)
+          (browse-url-handlers nil)
+          (browse-url-default-handlers nil))
+      (browse-url url))))
 
 ;;;###autoload
 (defun browse-url-at-mouse (event)
@@ -1786,17 +1789,19 @@ browse-url-add-buttons
                                          browse-url-data ,(match-string 0)))))))
 
 ;;;###autoload
-(defun browse-url-button-open (&optional external mouse-event)
+(defun browse-url-button-open (&optional secondary mouse-event)
   "Follow the link under point using `browse-url'.
-If EXTERNAL (the prefix if used interactively), open with the
-external browser instead of the default one."
+If SECONDARY (the prefix if used interactively), open with the
+secondary browser instead of the default one."
   (interactive (list current-prefix-arg last-nonmenu-event))
   (mouse-set-point mouse-event)
   (let ((url (get-text-property (point) 'browse-url-data)))
     (unless url
       (error "No URL under point"))
-    (if external
-        (funcall browse-url-secondary-browser-function url)
+    (let ((browse-url-browser-function
+           (if secondary
+               browse-url-secondary-browser-function
+             browse-url-browser-function)))
       (browse-url url))))
 
 ;;;###autoload
@@ -1804,8 +1809,10 @@ browse-url-button-open-url
   "Open URL using `browse-url'.
 If `current-prefix-arg' is non-nil, use
 `browse-url-secondary-browser-function' instead."
-  (if current-prefix-arg
-      (funcall browse-url-secondary-browser-function url)
+  (let ((browse-url-browser-function
+         (if current-prefix-arg
+             browse-url-secondary-browser-function
+           browse-url-browser-function)))
     (browse-url url)))
 
 (defun browse-url-button-copy ()
diff --git a/lisp/net/eww.el b/lisp/net/eww.el
index 95513a67acd..df9e5ad88a7 100644
--- a/lisp/net/eww.el
+++ b/lisp/net/eww.el
@@ -2166,7 +2166,8 @@ eww-browse-with-external-browser
   (setq url (or url (plist-get eww-data :url)))
   (if (eq 'external (browse-url--browser-kind
                      browse-url-secondary-browser-function url))
-      (funcall browse-url-secondary-browser-function url)
+      (let ((browse-url-browser-function browse-url-secondary-browser-function))
+        (browse-url url))
     (browse-url-with-browser-kind 'external url)))
 
 (defun eww-remove-tracking (url)
diff --git a/lisp/net/shr.el b/lisp/net/shr.el
index 24c779524dc..c2d01bd2e0d 100644
--- a/lisp/net/shr.el
+++ b/lisp/net/shr.el
@@ -1097,9 +1097,9 @@ shr-mouse-browse-url-new-window
   (mouse-set-point ev)
   (shr-browse-url nil nil t))
 
-(defun shr-browse-url (&optional external mouse-event new-window)
+(defun shr-browse-url (&optional secondary mouse-event new-window)
   "Browse the URL at point using `browse-url'.
-If EXTERNAL is non-nil (interactively, the prefix argument), browse
+If SECONDARY is non-nil (interactively, the prefix argument), browse
 the URL using `browse-url-secondary-browser-function'.
 If this function is invoked by a mouse click, it will browse the URL
 at the position of the click.  Optional argument MOUSE-EVENT describes
@@ -1110,8 +1110,9 @@ shr-browse-url
     (cond
      ((not url)
       (message "No link under point"))
-     (external
-      (funcall browse-url-secondary-browser-function url)
+     (secondary
+      (let ((browse-url-browser-function browse-url-secondary-browser-function))
+        (browse-url url))
       (shr--blink-link))
      (t
       (browse-url url (xor new-window browse-url-new-window-flag))))))
-- 
2.45.2