]> git.eshelyaron.com Git - emacs.git/commitdiff
Allow predicates for matching in browse-url-handlers.
authorTassilo Horn <tsdh@gnu.org>
Fri, 8 May 2020 18:57:19 +0000 (20:57 +0200)
committerTassilo Horn <tsdh@gnu.org>
Fri, 8 May 2020 18:57:19 +0000 (20:57 +0200)
* lisp/net/browse-url.el (browse-url-handlers): Allow predicates for
matching in browse-url-handlers.  Adapt docs and customize type.
(browse-url-select-handler): Support predicates in addition to
regexes.
(browse-url--non-html-file-url-p): New defun.
(browse-url-default-handlers): Use above predicate entry instead of
two entries.

doc/emacs/misc.texi
etc/NEWS
lisp/net/browse-url.el

index d1854f31ff3b0a31a77ec6d87b4998bda7c5a51c..1336da12b072e983a03404f2ba52f17c1b618eb8 100644 (file)
@@ -2926,7 +2926,8 @@ specifies your default browser.
 @vindex browse-url-handlers
   You can define that certain URLs are browsed with other functions by
 customizing @code{browse-url-handlers}, an alist of regular
-expressions paired with functions to browse matching URLs.
+expressions or predicates paired with functions to browse matching
+URLs.
 
 For more information, view the package commentary by typing @kbd{C-h P
 browse-url @key{RET}}.
index ac93a76ff9595e4bbe53e8a085bcd9c49bc4de8f..9c71752b621914530f0c664749850b77776c51e2 100644 (file)
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -310,16 +310,26 @@ or use it in a custom ‘c-style’.
 *** Added support for custom URL handlers
 
 There is a new defvar 'browse-url-default-handlers' and a defcustom
-'browse-url-handlers' being alists with (REGEXP . FUNCTION) entries
-allowing to define different browsing FUNCTIONs depending on the URL
-to be browsed.  The defvar is for default handlers provided by Emacs
-itself or external packages, the defcustom is for the user (and allows
-for overriding the default handlers).
+'browse-url-handlers' being alists with (REGEXP-OR-PREDICATE
+. FUNCTION) entries allowing to define different browsing FUNCTIONs
+depending on the URL to be browsed.  The defvar is for default
+handlers provided by Emacs itself or external packages, the defcustom
+is for the user (and allows for overriding the default handlers).
 
 Formerly, one could do the same by setting
 'browse-url-browser-function' to such an alist.  This usage is still
 supported but deprecated.
 
+*** Categorization of browsing functions in internal vs. external
+
+All standard browsing functions such as 'browse-url-firefox',
+'browse-url-mail', or 'eww' have been categorized into internal (URL
+is browsed in Emacs) or external (an external application is spawned
+with the URL).  This is done by adding a 'browse-url-browser-kind'
+symbol property to the browsing functions.  With a new command
+'browse-url-with-browser-kind', an URL can explicitly be browsed with
+either an internal or external browser.
+
 \f
 * New Modes and Packages in Emacs 28.1
 
index d7b8521563ad38e32ed53bbd146e391d160b05e6..9073f8968314e8360be1608890570ccb24f155fa 100644 (file)
@@ -642,15 +642,16 @@ process), or nil (we don't know)."
 (put 'browse-url--browser 'browse-url-browser-kind
      #'browse-url--browser-kind-browser)
 
+(defun browse-url--non-html-file-url-p (url)
+  "Return non-nil if URL is a file:// URL of a non-HTML file."
+  (and (string-match-p "\\`file://" url)
+       (not (string-match-p "\\`file://.*\\.html?\\b" url))))
 
 ;;;###autoload
 (defvar browse-url-default-handlers
   '(("\\`mailto:" . browse-url--mailto)
     ("\\`man:" . browse-url--man)
-    ;; Render file:// URLs if they are HTML pages, otherwise just find
-    ;; the file.
-    ("\\`file://.*\\.html?\\b" . browse-url--browser)
-    ("\\`file://" . browse-url-emacs))
+    (browse-url--non-html-file-url-p . browse-url-emacs))
   "Like `browse-url-handlers' but populated by Emacs and packages.
 
 Emacs and external packages capable of browsing certain URLs
@@ -658,18 +659,20 @@ should place their entries in this alist rather than
 `browse-url-handlers' which is reserved for the user.")
 
 (defcustom browse-url-handlers nil
-  "An alist with elements of the form (REGEXP HANDLER).
-Each REGEXP is matched against the URL to be opened in turn and
-the first match's HANDLER is invoked with the URL.
+  "An alist with elements of the form (REGEXP-OR-PREDICATE . HANDLER).
+Each REGEXP-OR-PREDICATE is matched against the URL to be opened
+in turn and the first match's HANDLER is invoked with the URL.
 
 A HANDLER must be a function with the same arguments as
 `browse-url'.
 
-If no REGEXP matches, the same procedure is performed with the
-value of `browse-url-default-handlers'.  If there is also no
-match, the URL is opened using the value of
+If no REGEXP-OR-PREDICATE matches, the same procedure is
+performed with the value of `browse-url-default-handlers'.  If
+there is also no match, the URL is opened using the value of
 `browse-url-browser-function'."
-  :type '(alist :key-type (regexp :tag "Regexp")
+  :type '(alist :key-type (choice
+                           (regexp :tag "Regexp")
+                           (function :tag "Predicate"))
                 :value-type (function :tag "Handler"))
   :version "28.1")
 
@@ -688,7 +691,7 @@ Currently, it also consults `browse-url-browser-function' first
 if it is set to an alist, although this usage is deprecated since
 Emacs 28.1 and will be removed in a future release."
   (catch 'custom-url-handler
-    (dolist (regex-handler
+    (dolist (rxpred-handler
              (append
               ;; The alist choice of browse-url-browser-function
               ;; is deprecated since 28.1, so the (unless ...)
@@ -701,11 +704,15 @@ alist is deprecated.  Use `browse-url-handlers' instead.")
                 browse-url-browser-function)
               browse-url-handlers
               browse-url-default-handlers))
-      (when (and (or (null kind)
-                     (eq kind (browse-url--browser-kind
-                               (cdr regex-handler) url)))
-                 (string-match-p (car regex-handler) url))
-        (throw 'custom-url-handler (cdr regex-handler))))))
+      (let ((rx-or-pred (car rxpred-handler))
+            (handler (cdr rxpred-handler)))
+        (when (and (or (null kind)
+                       (eq kind (browse-url--browser-kind
+                                 handler url)))
+                   (if (functionp rx-or-pred)
+                       (funcall rx-or-pred url)
+                     (string-match-p rx-or-pred url)))
+          (throw 'custom-url-handler handler))))))
 
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ;; URL encoding