bug-reference.el 6.2 KB

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