[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bug-reference.el: Allow custom handlers for opening URLs

From: Tassilo Horn
Subject: bug-reference.el: Allow custom handlers for opening URLs
Date: Sun, 03 May 2020 10:50:43 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux)

Hi all,

currently all bug references are opened using `browse-url'.  I'd like to
have a way to override that behavior, e.g., so that I can use the
debbugs package for reports for GNU packages using the debbugs tracker.

Below is a patch which makes that possible.  Is it ok to commit that?
Something to improve?

Oh, and how do I correctly write #'my-function in a docstring so that
the ' doesn't get displayed differently?

--8<---------------cut here---------------start------------->8---
>From 8450db635e52a56addccd435bc03449d5caa0a2b Mon Sep 17 00:00:00 2001
From: Tassilo Horn <address@hidden>
Date: Sun, 3 May 2020 10:39:47 +0200
Subject: [PATCH] Allow customizing how bug reference URLs are opened.

* lisp/progmodes/bug-reference.el (bug-reference-url-handlers): New
(bug-reference-push-button): Use it.
(bug-reference-open-with-debbugs): New function.
 lisp/progmodes/bug-reference.el | 36 +++++++++++++++++++++++++++++++--
 1 file changed, 34 insertions(+), 2 deletions(-)

diff --git a/lisp/progmodes/bug-reference.el b/lisp/progmodes/bug-reference.el
index 02af263ec3..cc862eb160 100644
--- a/lisp/progmodes/bug-reference.el
+++ b/lisp/progmodes/bug-reference.el
@@ -122,6 +122,31 @@ bug-reference-fontify
                                        (match-string-no-properties 2))
                              (funcall bug-reference-url-format))))))))))
+(defcustom bug-reference-url-handlers nil
+  "An alist with elements of the form (REGEXP HANDLER).
+Each REGEXP is matched against a bug reference URL in turn and
+the first match's HANDLER function is invoked with the URL.
+If no REGEXP matches, the bug reference URL is opened using
+For example, to open GNU bug reports using the debbugs ELPA
+package, you could use an entry like this.
+  (\"https://debbugs.gnu.org/cgi/bugreport\\\\.cgi\";
+   . #'bug-reference-open-with-debbugs)"
+  :type '(alist :key-type (regexp :tag "Regexp")
+                :value-type (function :tag "Handler"))
+  :version "28.1"
+  :group 'bug-reference)
+(defun bug-reference-open-with-debbugs (url)
+  (unless (fboundp #'debbugs-gnu-bugs)
+    (error "The debbugs package is not installed"))
+  (if (string-match "bug=\\([0-9]+\\)" url)
+      (debbugs-gnu-bugs (string-to-number (match-string 1 url)))
+    (error "The URL %s contains no bug number" url)))
 ;; Taken from button.el.
 (defun bug-reference-push-button (&optional pos _use-mouse-action)
   "Open URL corresponding to the bug reference at POS."
@@ -135,9 +160,16 @@ bug-reference-push-button
     ;; POS is just normal position.
     (dolist (o (overlays-at pos))
       ;; It should only be possible to have one URL overlay.
-      (let ((url (overlay-get o 'bug-reference-url)))
+      (let ((url (overlay-get o 'bug-reference-url))
+            handler)
        (when url
-         (browse-url url))))))
+          (setq handler
+                (catch 'handler-set
+                  (dolist (regex-handler bug-reference-url-handlers)
+                    (when (string-match-p (car regex-handler) url)
+                      (throw 'handler-set (cdr regex-handler))))
+                  #'browse-url))
+         (funcall handler url))))))
 (define-minor-mode bug-reference-mode

--8<---------------cut here---------------end--------------->8---


reply via email to

[Prev in Thread] Current Thread [Next in Thread]