all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* bug-reference.el: Allow custom handlers for opening URLs
@ 2020-05-03  8:50 Tassilo Horn
  2020-05-03 14:44 ` Stefan Monnier
  2020-05-05 19:55 ` Arash Esbati
  0 siblings, 2 replies; 15+ messages in thread
From: Tassilo Horn @ 2020-05-03  8:50 UTC (permalink / raw)
  To: emacs-devel

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 <tsdh@gnu.org>
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
defcustom.
(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
+`browse-url'.
+
+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))))))
 
 ;;;###autoload
 (define-minor-mode bug-reference-mode
-- 
2.26.2

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

Bye,
Tassilo



^ permalink raw reply related	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2020-05-05 19:55 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-05-03  8:50 bug-reference.el: Allow custom handlers for opening URLs Tassilo Horn
2020-05-03 14:44 ` Stefan Monnier
2020-05-03 15:24   ` Tassilo Horn
2020-05-03 20:39     ` Stefan Monnier
2020-05-04  9:41       ` Tassilo Horn
2020-05-04 15:32         ` Stefan Monnier
2020-05-04 17:09           ` Tassilo Horn
2020-05-04 18:54             ` Stefan Monnier
2020-05-05  7:06               ` Tassilo Horn
2020-05-05 13:49                 ` Stefan Monnier
2020-05-05 15:51                   ` Tassilo Horn
2020-05-05 17:44                     ` Stefan Monnier
2020-05-05 19:48                       ` browse-url.el: Custom handlers for certain URLs (was: bug-reference.el: Allow custom handlers for opening URLs) Tassilo Horn
2020-05-04  6:52     ` bug-reference.el: Allow custom handlers for opening URLs Yuri Khan
2020-05-05 19:55 ` Arash Esbati

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.