Merge from emacs-24 branch; up to 2012-05-01T18:47:23Z!rgm@gnu.org
[bpt/emacs.git] / lisp / progmodes / bug-reference.el
1 ;; bug-reference.el --- buttonize bug references
2
3 ;; Copyright (C) 2008-2012 Free Software Foundation, Inc.
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
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
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
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
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
33 ;;; Code:
34
35 (defvar bug-reference-map
36 (let ((map (make-sparse-keymap)))
37 (define-key map [mouse-2] 'bug-reference-push-button)
38 (define-key map (kbd "C-c RET") 'bug-reference-push-button)
39 map)
40 "Keymap used by bug reference buttons.")
41
42 ;; E.g., "http://gcc.gnu.org/PR%s"
43 (defvar bug-reference-url-format nil
44 "Format used to turn a bug number into a URL.
45 The bug number is supplied as a string, so this should have a single %s.
46 This can also be a function designator; it is called without arguments
47 and should return a string.
48 It can use `match-string' to get parts matched against
49 `bug-reference-bug-regexp', specifically:
50 1. issue kind (bug, patch, rfe &c)
51 2. issue number.
52
53 There is no default setting for this, it must be set per file.
54 If you set it to a symbol in the file Local Variables section,
55 you need to add a `bug-reference-url-format' property to it:
56 \(put 'my-bug-reference-url-format 'bug-reference-url-format t)
57 so that it is considered safe, see `enable-local-variables'.")
58
59 ;;;###autoload
60 (put 'bug-reference-url-format 'safe-local-variable
61 (lambda (s)
62 (or (stringp s)
63 (and (symbolp s)
64 (get s 'bug-reference-url-format)))))
65
66 (defconst bug-reference-bug-regexp
67 "\\([Bb]ug ?#\\|[Pp]atch ?#\\|RFE ?#\\|PR [a-z-+]+/\\)\\([0-9]+\\(?:#[0-9]+\\)?\\)"
68 "Regular expression which matches bug references.")
69
70 (defun bug-reference-set-overlay-properties ()
71 "Set properties of bug reference overlays."
72 (put 'bug-reference 'evaporate t)
73 (put 'bug-reference 'face 'link)
74 (put 'bug-reference 'mouse-face 'highlight)
75 (put 'bug-reference 'help-echo "mouse-1, C-c RET: visit this bug")
76 (put 'bug-reference 'keymap bug-reference-map)
77 (put 'bug-reference 'follow-link t))
78
79 (bug-reference-set-overlay-properties)
80
81 (defun bug-reference-unfontify (start end)
82 "Remove bug reference overlays from region."
83 (dolist (o (overlays-in start end))
84 (when (eq (overlay-get o 'category) 'bug-reference)
85 (delete-overlay o))))
86
87 (defvar bug-reference-prog-mode)
88
89 (defun bug-reference-fontify (start end)
90 "Apply bug reference overlays to region."
91 (save-excursion
92 (let ((beg-line (progn (goto-char start) (line-beginning-position)))
93 (end-line (progn (goto-char end) (line-end-position))))
94 ;; Remove old overlays.
95 (bug-reference-unfontify beg-line end-line)
96 (goto-char beg-line)
97 (while (and (< (point) end-line)
98 (re-search-forward bug-reference-bug-regexp end-line 'move))
99 (when (or (not bug-reference-prog-mode)
100 ;; This tests for both comment and string syntax.
101 (nth 8 (syntax-ppss)))
102 (let ((overlay (make-overlay (match-beginning 0) (match-end 0)
103 nil t nil)))
104 (overlay-put overlay 'category 'bug-reference)
105 ;; Don't put a link if format is undefined
106 (when bug-reference-url-format
107 (overlay-put overlay 'bug-reference-url
108 (if (stringp bug-reference-url-format)
109 (format bug-reference-url-format
110 (match-string-no-properties 2))
111 (funcall bug-reference-url-format))))))))))
112
113 ;; Taken from button.el.
114 (defun bug-reference-push-button (&optional pos _use-mouse-action)
115 "Open URL corresponding to the bug reference at POS."
116 (interactive
117 (list (if (integerp last-command-event) (point) last-command-event)))
118 (if (and (not (integerp pos)) (eventp pos))
119 ;; POS is a mouse event; switch to the proper window/buffer
120 (let ((posn (event-start pos)))
121 (with-current-buffer (window-buffer (posn-window posn))
122 (bug-reference-push-button (posn-point posn) t)))
123 ;; POS is just normal position.
124 (dolist (o (overlays-at pos))
125 ;; It should only be possible to have one URL overlay.
126 (let ((url (overlay-get o 'bug-reference-url)))
127 (when url
128 (browse-url url))))))
129
130 ;;;###autoload
131 (define-minor-mode bug-reference-mode
132 "Toggle hyperlinking bug references in the buffer (Bug Reference mode).
133 With a prefix argument ARG, enable Bug Reference mode if ARG is
134 positive, and disable it otherwise. If called from Lisp, enable
135 the mode if ARG is omitted or nil."
136 nil
137 ""
138 nil
139 (if bug-reference-mode
140 (jit-lock-register #'bug-reference-fontify)
141 (jit-lock-unregister #'bug-reference-fontify)
142 (save-restriction
143 (widen)
144 (bug-reference-unfontify (point-min) (point-max)))))
145
146 ;;;###autoload
147 (define-minor-mode bug-reference-prog-mode
148 "Like `bug-reference-mode', but only buttonize in comments and strings."
149 nil
150 ""
151 nil
152 (if bug-reference-prog-mode
153 (jit-lock-register #'bug-reference-fontify)
154 (jit-lock-unregister #'bug-reference-fontify)
155 (save-restriction
156 (widen)
157 (bug-reference-unfontify (point-min) (point-max)))))
158
159 (provide 'bug-reference)
160 ;;; bug-reference.el ends here