declare smobs in alloc.c
[bpt/emacs.git] / lisp / progmodes / bug-reference.el
CommitLineData
871968ca
GM
1;; bug-reference.el --- buttonize bug references
2
ba318903 3;; Copyright (C) 2008-2014 Free Software Foundation, Inc.
871968ca
GM
4
5;; Author: Tom Tromey <tromey@redhat.com>
6;; Created: 21 Mar 2007
7;; Keywords: tools
8
9;; This file is part of GNU Emacs.
10
b1fc2b50 11;; GNU Emacs is free software: you can redistribute it and/or modify
871968ca 12;; it under the terms of the GNU General Public License as published by
b1fc2b50
GM
13;; the Free Software Foundation, either version 3 of the License, or
14;; (at your option) any later version.
871968ca
GM
15
16;; GNU Emacs is distributed in the hope that it will be useful,
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19;; GNU General Public License for more details.
20
21;; You should have received a copy of the GNU General Public License
b1fc2b50 22;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
871968ca
GM
23
24;;; Commentary:
25
26;; This file provides minor modes for putting clickable overlays on
27;; references to bugs. A bug reference is text like "PR foo/29292";
28;; this is mapped to a URL using a user-supplied format.
29
30;; Two minor modes are provided. One works on any text in the buffer;
31;; the other operates only on comments and strings.
32
d0b822e3
SM
33;;; Code:
34
2b2c0794
GM
35(defgroup bug-reference nil
36 "Hyperlinking references to bug reports"
37 ;; Somewhat arbitrary, by analogy with eg goto-address.
38 :group 'comm)
39
871968ca
GM
40(defvar bug-reference-map
41 (let ((map (make-sparse-keymap)))
315eb96d 42 (define-key map [mouse-2] 'bug-reference-push-button)
871968ca
GM
43 (define-key map (kbd "C-c RET") 'bug-reference-push-button)
44 map)
45 "Keymap used by bug reference buttons.")
46
47;; E.g., "http://gcc.gnu.org/PR%s"
48(defvar bug-reference-url-format nil
49 "Format used to turn a bug number into a URL.
50The bug number is supplied as a string, so this should have a single %s.
dbb5e44a
SS
51This can also be a function designator; it is called without arguments
52 and should return a string.
53It can use `match-string' to get parts matched against
54`bug-reference-bug-regexp', specifically:
55 1. issue kind (bug, patch, rfe &c)
56 2. issue number.
57
95c6cc3e
SS
58There is no default setting for this, it must be set per file.
59If you set it to a symbol in the file Local Variables section,
60you need to add a `bug-reference-url-format' property to it:
61\(put 'my-bug-reference-url-format 'bug-reference-url-format t)
62so that it is considered safe, see `enable-local-variables'.")
871968ca 63
bffc6559 64;;;###autoload
95c6cc3e
SS
65(put 'bug-reference-url-format 'safe-local-variable
66 (lambda (s)
67 (or (stringp s)
68 (and (symbolp s)
69 (get s 'bug-reference-url-format)))))
bffc6559 70
2b2c0794 71(defcustom bug-reference-bug-regexp
b544fef2 72 "\\([Bb]ug ?#\\|[Pp]atch ?#\\|RFE ?#\\|PR [a-z-+]+/\\)\\([0-9]+\\(?:#[0-9]+\\)?\\)"
2b2c0794
GM
73 "Regular expression matching bug references.
74The second subexpression should match the bug reference (usually a number)."
75 :type 'string
76 :safe 'stringp
d1a1c7e6 77 :version "24.3" ; previously defconst
2b2c0794 78 :group 'bug-reference)
871968ca
GM
79
80(defun bug-reference-set-overlay-properties ()
81 "Set properties of bug reference overlays."
82 (put 'bug-reference 'evaporate t)
83 (put 'bug-reference 'face 'link)
84 (put 'bug-reference 'mouse-face 'highlight)
85 (put 'bug-reference 'help-echo "mouse-1, C-c RET: visit this bug")
86 (put 'bug-reference 'keymap bug-reference-map)
87 (put 'bug-reference 'follow-link t))
88
89(bug-reference-set-overlay-properties)
90
91(defun bug-reference-unfontify (start end)
92 "Remove bug reference overlays from region."
93 (dolist (o (overlays-in start end))
94 (when (eq (overlay-get o 'category) 'bug-reference)
95 (delete-overlay o))))
96
d4296db3
GM
97(defvar bug-reference-prog-mode)
98
871968ca
GM
99(defun bug-reference-fontify (start end)
100 "Apply bug reference overlays to region."
101 (save-excursion
102 (let ((beg-line (progn (goto-char start) (line-beginning-position)))
103 (end-line (progn (goto-char end) (line-end-position))))
104 ;; Remove old overlays.
105 (bug-reference-unfontify beg-line end-line)
106 (goto-char beg-line)
321d9c42
GM
107 (while (and (< (point) end-line)
108 (re-search-forward bug-reference-bug-regexp end-line 'move))
109 (when (or (not bug-reference-prog-mode)
110 ;; This tests for both comment and string syntax.
111 (nth 8 (syntax-ppss)))
112 (let ((overlay (make-overlay (match-beginning 0) (match-end 0)
113 nil t nil)))
114 (overlay-put overlay 'category 'bug-reference)
1cec8b5f
JL
115 ;; Don't put a link if format is undefined
116 (when bug-reference-url-format
dbb5e44a
SS
117 (overlay-put overlay 'bug-reference-url
118 (if (stringp bug-reference-url-format)
119 (format bug-reference-url-format
120 (match-string-no-properties 2))
121 (funcall bug-reference-url-format))))))))))
871968ca
GM
122
123;; Taken from button.el.
e02f48d7 124(defun bug-reference-push-button (&optional pos _use-mouse-action)
871968ca
GM
125 "Open URL corresponding to the bug reference at POS."
126 (interactive
127 (list (if (integerp last-command-event) (point) last-command-event)))
128 (if (and (not (integerp pos)) (eventp pos))
129 ;; POS is a mouse event; switch to the proper window/buffer
130 (let ((posn (event-start pos)))
131 (with-current-buffer (window-buffer (posn-window posn))
132 (bug-reference-push-button (posn-point posn) t)))
133 ;; POS is just normal position.
134 (dolist (o (overlays-at pos))
135 ;; It should only be possible to have one URL overlay.
136 (let ((url (overlay-get o 'bug-reference-url)))
137 (when url
138 (browse-url url))))))
139
140;;;###autoload
141(define-minor-mode bug-reference-mode
ac6c8639
CY
142 "Toggle hyperlinking bug references in the buffer (Bug Reference mode).
143With a prefix argument ARG, enable Bug Reference mode if ARG is
144positive, and disable it otherwise. If called from Lisp, enable
145the mode if ARG is omitted or nil."
871968ca
GM
146 nil
147 ""
148 nil
149 (if bug-reference-mode
1cec8b5f 150 (jit-lock-register #'bug-reference-fontify)
871968ca
GM
151 (jit-lock-unregister #'bug-reference-fontify)
152 (save-restriction
153 (widen)
154 (bug-reference-unfontify (point-min) (point-max)))))
155
156;;;###autoload
157(define-minor-mode bug-reference-prog-mode
158 "Like `bug-reference-mode', but only buttonize in comments and strings."
159 nil
160 ""
161 nil
162 (if bug-reference-prog-mode
1cec8b5f 163 (jit-lock-register #'bug-reference-fontify)
871968ca
GM
164 (jit-lock-unregister #'bug-reference-fontify)
165 (save-restriction
166 (widen)
167 (bug-reference-unfontify (point-min) (point-max)))))
168
d0b822e3 169(provide 'bug-reference)
871968ca 170;;; bug-reference.el ends here