bug-reference.el 5.9 KB

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