diff options
| author | Tassilo Horn | 2020-05-08 20:57:19 +0200 |
|---|---|---|
| committer | Tassilo Horn | 2020-05-08 20:57:19 +0200 |
| commit | 909591a4b25901392686f1cf68870260d3016afe (patch) | |
| tree | 3b33377bf96c5c37d39e6240b47aa014f07052a5 | |
| parent | 39b2a598d27809c524a123fd53db71783693071e (diff) | |
| download | emacs-909591a4b25901392686f1cf68870260d3016afe.tar.gz emacs-909591a4b25901392686f1cf68870260d3016afe.zip | |
Allow predicates for matching in browse-url-handlers.
* 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.
| -rw-r--r-- | doc/emacs/misc.texi | 3 | ||||
| -rw-r--r-- | etc/NEWS | 20 | ||||
| -rw-r--r-- | lisp/net/browse-url.el | 41 |
3 files changed, 41 insertions, 23 deletions
diff --git a/doc/emacs/misc.texi b/doc/emacs/misc.texi index d1854f31ff3..1336da12b07 100644 --- a/doc/emacs/misc.texi +++ b/doc/emacs/misc.texi | |||
| @@ -2926,7 +2926,8 @@ specifies your default browser. | |||
| 2926 | @vindex browse-url-handlers | 2926 | @vindex browse-url-handlers |
| 2927 | You can define that certain URLs are browsed with other functions by | 2927 | You can define that certain URLs are browsed with other functions by |
| 2928 | customizing @code{browse-url-handlers}, an alist of regular | 2928 | customizing @code{browse-url-handlers}, an alist of regular |
| 2929 | expressions paired with functions to browse matching URLs. | 2929 | expressions or predicates paired with functions to browse matching |
| 2930 | URLs. | ||
| 2930 | 2931 | ||
| 2931 | For more information, view the package commentary by typing @kbd{C-h P | 2932 | For more information, view the package commentary by typing @kbd{C-h P |
| 2932 | browse-url @key{RET}}. | 2933 | browse-url @key{RET}}. |
| @@ -310,16 +310,26 @@ or use it in a custom ‘c-style’. | |||
| 310 | *** Added support for custom URL handlers | 310 | *** Added support for custom URL handlers |
| 311 | 311 | ||
| 312 | There is a new defvar 'browse-url-default-handlers' and a defcustom | 312 | There is a new defvar 'browse-url-default-handlers' and a defcustom |
| 313 | 'browse-url-handlers' being alists with (REGEXP . FUNCTION) entries | 313 | 'browse-url-handlers' being alists with (REGEXP-OR-PREDICATE |
| 314 | allowing to define different browsing FUNCTIONs depending on the URL | 314 | . FUNCTION) entries allowing to define different browsing FUNCTIONs |
| 315 | to be browsed. The defvar is for default handlers provided by Emacs | 315 | depending on the URL to be browsed. The defvar is for default |
| 316 | itself or external packages, the defcustom is for the user (and allows | 316 | handlers provided by Emacs itself or external packages, the defcustom |
| 317 | for overriding the default handlers). | 317 | is for the user (and allows for overriding the default handlers). |
| 318 | 318 | ||
| 319 | Formerly, one could do the same by setting | 319 | Formerly, one could do the same by setting |
| 320 | 'browse-url-browser-function' to such an alist. This usage is still | 320 | 'browse-url-browser-function' to such an alist. This usage is still |
| 321 | supported but deprecated. | 321 | supported but deprecated. |
| 322 | 322 | ||
| 323 | *** Categorization of browsing functions in internal vs. external | ||
| 324 | |||
| 325 | All standard browsing functions such as 'browse-url-firefox', | ||
| 326 | 'browse-url-mail', or 'eww' have been categorized into internal (URL | ||
| 327 | is browsed in Emacs) or external (an external application is spawned | ||
| 328 | with the URL). This is done by adding a 'browse-url-browser-kind' | ||
| 329 | symbol property to the browsing functions. With a new command | ||
| 330 | 'browse-url-with-browser-kind', an URL can explicitly be browsed with | ||
| 331 | either an internal or external browser. | ||
| 332 | |||
| 323 | 333 | ||
| 324 | * New Modes and Packages in Emacs 28.1 | 334 | * New Modes and Packages in Emacs 28.1 |
| 325 | 335 | ||
diff --git a/lisp/net/browse-url.el b/lisp/net/browse-url.el index d7b8521563a..9073f896831 100644 --- a/lisp/net/browse-url.el +++ b/lisp/net/browse-url.el | |||
| @@ -642,15 +642,16 @@ process), or nil (we don't know)." | |||
| 642 | (put 'browse-url--browser 'browse-url-browser-kind | 642 | (put 'browse-url--browser 'browse-url-browser-kind |
| 643 | #'browse-url--browser-kind-browser) | 643 | #'browse-url--browser-kind-browser) |
| 644 | 644 | ||
| 645 | (defun browse-url--non-html-file-url-p (url) | ||
| 646 | "Return non-nil if URL is a file:// URL of a non-HTML file." | ||
| 647 | (and (string-match-p "\\`file://" url) | ||
| 648 | (not (string-match-p "\\`file://.*\\.html?\\b" url)))) | ||
| 645 | 649 | ||
| 646 | ;;;###autoload | 650 | ;;;###autoload |
| 647 | (defvar browse-url-default-handlers | 651 | (defvar browse-url-default-handlers |
| 648 | '(("\\`mailto:" . browse-url--mailto) | 652 | '(("\\`mailto:" . browse-url--mailto) |
| 649 | ("\\`man:" . browse-url--man) | 653 | ("\\`man:" . browse-url--man) |
| 650 | ;; Render file:// URLs if they are HTML pages, otherwise just find | 654 | (browse-url--non-html-file-url-p . browse-url-emacs)) |
| 651 | ;; the file. | ||
| 652 | ("\\`file://.*\\.html?\\b" . browse-url--browser) | ||
| 653 | ("\\`file://" . browse-url-emacs)) | ||
| 654 | "Like `browse-url-handlers' but populated by Emacs and packages. | 655 | "Like `browse-url-handlers' but populated by Emacs and packages. |
| 655 | 656 | ||
| 656 | Emacs and external packages capable of browsing certain URLs | 657 | Emacs and external packages capable of browsing certain URLs |
| @@ -658,18 +659,20 @@ should place their entries in this alist rather than | |||
| 658 | `browse-url-handlers' which is reserved for the user.") | 659 | `browse-url-handlers' which is reserved for the user.") |
| 659 | 660 | ||
| 660 | (defcustom browse-url-handlers nil | 661 | (defcustom browse-url-handlers nil |
| 661 | "An alist with elements of the form (REGEXP HANDLER). | 662 | "An alist with elements of the form (REGEXP-OR-PREDICATE . HANDLER). |
| 662 | Each REGEXP is matched against the URL to be opened in turn and | 663 | Each REGEXP-OR-PREDICATE is matched against the URL to be opened |
| 663 | the first match's HANDLER is invoked with the URL. | 664 | in turn and the first match's HANDLER is invoked with the URL. |
| 664 | 665 | ||
| 665 | A HANDLER must be a function with the same arguments as | 666 | A HANDLER must be a function with the same arguments as |
| 666 | `browse-url'. | 667 | `browse-url'. |
| 667 | 668 | ||
| 668 | If no REGEXP matches, the same procedure is performed with the | 669 | If no REGEXP-OR-PREDICATE matches, the same procedure is |
| 669 | value of `browse-url-default-handlers'. If there is also no | 670 | performed with the value of `browse-url-default-handlers'. If |
| 670 | match, the URL is opened using the value of | 671 | there is also no match, the URL is opened using the value of |
| 671 | `browse-url-browser-function'." | 672 | `browse-url-browser-function'." |
| 672 | :type '(alist :key-type (regexp :tag "Regexp") | 673 | :type '(alist :key-type (choice |
| 674 | (regexp :tag "Regexp") | ||
| 675 | (function :tag "Predicate")) | ||
| 673 | :value-type (function :tag "Handler")) | 676 | :value-type (function :tag "Handler")) |
| 674 | :version "28.1") | 677 | :version "28.1") |
| 675 | 678 | ||
| @@ -688,7 +691,7 @@ Currently, it also consults `browse-url-browser-function' first | |||
| 688 | if it is set to an alist, although this usage is deprecated since | 691 | if it is set to an alist, although this usage is deprecated since |
| 689 | Emacs 28.1 and will be removed in a future release." | 692 | Emacs 28.1 and will be removed in a future release." |
| 690 | (catch 'custom-url-handler | 693 | (catch 'custom-url-handler |
| 691 | (dolist (regex-handler | 694 | (dolist (rxpred-handler |
| 692 | (append | 695 | (append |
| 693 | ;; The alist choice of browse-url-browser-function | 696 | ;; The alist choice of browse-url-browser-function |
| 694 | ;; is deprecated since 28.1, so the (unless ...) | 697 | ;; is deprecated since 28.1, so the (unless ...) |
| @@ -701,11 +704,15 @@ alist is deprecated. Use `browse-url-handlers' instead.") | |||
| 701 | browse-url-browser-function) | 704 | browse-url-browser-function) |
| 702 | browse-url-handlers | 705 | browse-url-handlers |
| 703 | browse-url-default-handlers)) | 706 | browse-url-default-handlers)) |
| 704 | (when (and (or (null kind) | 707 | (let ((rx-or-pred (car rxpred-handler)) |
| 705 | (eq kind (browse-url--browser-kind | 708 | (handler (cdr rxpred-handler))) |
| 706 | (cdr regex-handler) url))) | 709 | (when (and (or (null kind) |
| 707 | (string-match-p (car regex-handler) url)) | 710 | (eq kind (browse-url--browser-kind |
| 708 | (throw 'custom-url-handler (cdr regex-handler)))))) | 711 | handler url))) |
| 712 | (if (functionp rx-or-pred) | ||
| 713 | (funcall rx-or-pred url) | ||
| 714 | (string-match-p rx-or-pred url))) | ||
| 715 | (throw 'custom-url-handler handler)))))) | ||
| 709 | 716 | ||
| 710 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | 717 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 711 | ;; URL encoding | 718 | ;; URL encoding |