diff options
author | Tassilo Horn <tsdh@gnu.org> | 2020-05-04 11:24:08 +0200 |
---|---|---|
committer | Tassilo Horn <tsdh@gnu.org> | 2020-05-04 11:24:08 +0200 |
commit | 8046f46252b5e23a9fa157660efb5c6fc0ea82e2 (patch) | |
tree | f07b973db4fe7ae867805d3b82dc9f7817bfdf0e | |
parent | 106936415d2892fc373842b3aedd805096fc9e0e (diff) | |
download | emacs-8046f46252b5e23a9fa157660efb5c6fc0ea82e2.tar.gz |
Allow for custom URL handlers in browse-url.
* lisp/net/browse-url.el (browse-url-handlers): New defcustom.
(browse-url): Use it.
-rw-r--r-- | lisp/net/browse-url.el | 31 |
1 files changed, 25 insertions, 6 deletions
diff --git a/lisp/net/browse-url.el b/lisp/net/browse-url.el index 7aad44b2876..ab0be7b913e 100644 --- a/lisp/net/browse-url.el +++ b/lisp/net/browse-url.el @@ -175,6 +175,23 @@ Also see `browse-url-secondary-browser-function'." :type browse-url--browser-defcustom-type :version "24.1") +;;;#autoload +(defcustom browse-url-handlers + `(("\\`mailto:" . ,browse-url-mailto-function) + ("\\`man:" . ,browse-url-man-function)) + "An alist with elements of the form (REGEXP HANDLER). +Each REGEXP is matched against each URL to be opened in turn and +the first match's HANDLER is invoked with the URL. + +A HANDLER must either be a function with the same arguments as +`browse-url' or a variable whos value is such a function. + +If no REGEXP matches, the bug reference URL is opened using the +value of `browse-url-browser-function'." + :type '(alist :key-type (regexp :tag "Regexp") + :value-type (function :tag "Handler")) + :version "28.1") + (defcustom browse-url-secondary-browser-function 'browse-url-default-browser "Function used to launch an alternative browser. This is usually an external browser (that is, not eww or w3m), @@ -786,12 +803,14 @@ as ARGS." (not (string-match "\\`[a-z]+:" url))) (setq url (expand-file-name url))) (let ((process-environment (copy-sequence process-environment)) - (function (or (and (string-match "\\`mailto:" url) - browse-url-mailto-function) - (and (string-match "\\`man:" url) - browse-url-man-function) - browse-url-browser-function)) - ;; Ensure that `default-directory' exists and is readable (b#6077). + (function + (catch 'custom-url-handler + (dolist (regex-handler browse-url-handlers) + (when (string-match-p (car regex-handler) url) + (throw 'custom-url-handler (cdr regex-handler)))) + ;; No special handler found. + browse-url-browser-function)) + ;; Ensure that `default-directory' exists and is readable (bug#6077). (default-directory (or (unhandled-file-name-directory default-directory) (expand-file-name "~/")))) ;; When connected to various displays, be careful to use the display of |