bug-reference.el 5.8 KB

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