Maybe skip advice of browse-url-browser-function
authorAlex Schroeder <alex@gnu.org>
Sat, 11 Sep 2021 20:38:55 +0000 (22:38 +0200)
committerAlex Schroeder <alex@gnu.org>
Sat, 11 Sep 2021 20:38:55 +0000 (22:38 +0200)
If the value of browse-url-browser-function is an alist instead of a
symbol, we're probably better off not advising it. Thus, check the
value of browse-url-browser-function before doing anything. Only
advise it if it is a function, i.e. fboundp.

elpher.el

index 7df595d..6bb36b3 100644 (file)
--- a/elpher.el
+++ b/elpher.el
@@ -2104,17 +2104,19 @@ supports the old protocol elpher, where the link is self-contained."
      '("^\\(gopher\\|finger\\|gemini\\)://" . elpher-browse-url-elpher))
   ;; Patch `browse-url-browser-function' for older ones. The value of
   ;; that variable is `browse-url-default-browser' by default, so
      '("^\\(gopher\\|finger\\|gemini\\)://" . elpher-browse-url-elpher))
   ;; Patch `browse-url-browser-function' for older ones. The value of
   ;; that variable is `browse-url-default-browser' by default, so
-  ;; that's the function that gets advised.
-  (advice-add browse-url-browser-function :before-while
-              (lambda (url &rest _args)
-               "Handle gemini, gopher, and finger schemes using Elpher."
-                (let ((scheme (downcase (car (split-string url ":" t)))))
-                  (if (member scheme '("gemini" "gopher" "finger"))
-                      ;; `elpher-go' always returns nil, which will stop the
-                      ;; advice chain here in a before-while
-                      (elpher-go url)
-                    ;; chain must continue, then return t.
-                    t)))))
+  ;; that's the function that gets advised. If the value is an alist,
+  ;; however, we don't know what to do. Better not interfere?
+  (when (fboundp browse-url-browser-function)
+    (advice-add browse-url-browser-function :before-while
+               (lambda (url &rest _args)
+                 "Handle gemini, gopher, and finger schemes using Elpher."
+                  (let ((scheme (downcase (car (split-string url ":" t)))))
+                    (if (member scheme '("gemini" "gopher" "finger"))
+                       ;; `elpher-go' always returns nil, which will stop the
+                       ;; advice chain here in a before-while
+                       (elpher-go url)
+                      ;; chain must continue, then return t.
+                      t))))))
 
 ;; Register "gemini://" as a URI scheme so `browse-url' does the right thing
 (with-eval-after-load 'thingatpt
 
 ;; Register "gemini://" as a URI scheme so `browse-url' does the right thing
 (with-eval-after-load 'thingatpt