paren.el 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. ;;; paren.el --- highlight matching paren
  2. ;; Copyright (C) 1993, 1996, 2001-2012 Free Software Foundation, Inc.
  3. ;; Author: rms@gnu.org
  4. ;; Maintainer: FSF
  5. ;; Keywords: languages, faces
  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. ;; Put this into your ~/.emacs:
  19. ;; (show-paren-mode t)
  20. ;; It will display highlighting on whatever paren matches the one
  21. ;; before or after point.
  22. ;;; Code:
  23. (defgroup paren-showing nil
  24. "Showing (un)matching of parens and expressions."
  25. :prefix "show-paren-"
  26. :group 'paren-matching)
  27. ;; This is the overlay used to highlight the matching paren.
  28. (defvar show-paren-overlay nil)
  29. ;; This is the overlay used to highlight the closeparen right before point.
  30. (defvar show-paren-overlay-1 nil)
  31. (defcustom show-paren-style 'parenthesis
  32. "Style used when showing a matching paren.
  33. Valid styles are `parenthesis' (meaning show the matching paren),
  34. `expression' (meaning show the entire expression enclosed by the paren) and
  35. `mixed' (meaning show the matching paren if it is visible, and the expression
  36. otherwise)."
  37. :type '(choice (const parenthesis) (const expression) (const mixed))
  38. :group 'paren-showing)
  39. (defcustom show-paren-delay 0.125
  40. "Time in seconds to delay before showing a matching paren."
  41. :type '(number :tag "seconds")
  42. :group 'paren-showing)
  43. (defcustom show-paren-priority 1000
  44. "Priority of paren highlighting overlays."
  45. :type 'integer
  46. :group 'paren-showing
  47. :version "21.1")
  48. (defcustom show-paren-ring-bell-on-mismatch nil
  49. "If non-nil, beep if mismatched paren is detected."
  50. :type 'boolean
  51. :group 'paren-showing
  52. :version "20.3")
  53. (defgroup paren-showing-faces nil
  54. "Group for faces of Show Paren mode."
  55. :group 'paren-showing
  56. :group 'faces
  57. :version "22.1")
  58. (defface show-paren-match
  59. '((((class color) (background light))
  60. :background "turquoise") ; looks OK on tty (becomes cyan)
  61. (((class color) (background dark))
  62. :background "steelblue3") ; looks OK on tty (becomes blue)
  63. (((background dark))
  64. :background "grey50")
  65. (t
  66. :background "gray"))
  67. "Show Paren mode face used for a matching paren."
  68. :group 'paren-showing-faces)
  69. (define-obsolete-face-alias 'show-paren-match-face 'show-paren-match "22.1")
  70. (defface show-paren-mismatch
  71. '((((class color)) (:foreground "white" :background "purple"))
  72. (t (:inverse-video t)))
  73. "Show Paren mode face used for a mismatching paren."
  74. :group 'paren-showing-faces)
  75. (define-obsolete-face-alias 'show-paren-mismatch-face
  76. 'show-paren-mismatch "22.1")
  77. (defvar show-paren-highlight-openparen t
  78. "*Non-nil turns on openparen highlighting when matching forward.")
  79. (defvar show-paren-idle-timer nil)
  80. ;;;###autoload
  81. (define-minor-mode show-paren-mode
  82. "Toggle visualization of matching parens (Show Paren mode).
  83. With a prefix argument ARG, enable Show Paren mode if ARG is
  84. positive, and disable it otherwise. If called from Lisp, enable
  85. the mode if ARG is omitted or nil.
  86. Show Paren mode is a global minor mode. When enabled, any
  87. matching parenthesis is highlighted in `show-paren-style' after
  88. `show-paren-delay' seconds of Emacs idle time."
  89. :global t :group 'paren-showing
  90. ;; Enable or disable the mechanism.
  91. ;; First get rid of the old idle timer.
  92. (if show-paren-idle-timer
  93. (cancel-timer show-paren-idle-timer))
  94. (setq show-paren-idle-timer nil)
  95. ;; If show-paren-mode is enabled in some buffer now,
  96. ;; set up a new timer.
  97. (when (memq t (mapcar (lambda (buffer)
  98. (with-current-buffer buffer
  99. show-paren-mode))
  100. (buffer-list)))
  101. (setq show-paren-idle-timer (run-with-idle-timer
  102. show-paren-delay t
  103. 'show-paren-function)))
  104. (unless show-paren-mode
  105. (and show-paren-overlay
  106. (eq (overlay-buffer show-paren-overlay) (current-buffer))
  107. (delete-overlay show-paren-overlay))
  108. (and show-paren-overlay-1
  109. (eq (overlay-buffer show-paren-overlay-1) (current-buffer))
  110. (delete-overlay show-paren-overlay-1))))
  111. ;; Find the place to show, if there is one,
  112. ;; and show it until input arrives.
  113. (defun show-paren-function ()
  114. (if show-paren-mode
  115. (let* ((oldpos (point))
  116. (dir (cond ((eq (syntax-class (syntax-after (1- (point)))) 5) -1)
  117. ((eq (syntax-class (syntax-after (point))) 4) 1)))
  118. (unescaped
  119. (when dir
  120. ;; Verify an even number of quoting characters precede the paren.
  121. ;; Follow the same logic as in `blink-matching-open'.
  122. (= (if (= dir -1) 1 0)
  123. (logand 1 (- (point)
  124. (save-excursion
  125. (if (= dir -1) (forward-char -1))
  126. (skip-syntax-backward "/\\")
  127. (point)))))))
  128. pos mismatch face)
  129. ;;
  130. ;; Find the other end of the sexp.
  131. (when unescaped
  132. (save-excursion
  133. (save-restriction
  134. ;; Determine the range within which to look for a match.
  135. (when blink-matching-paren-distance
  136. (narrow-to-region
  137. (max (point-min) (- (point) blink-matching-paren-distance))
  138. (min (point-max) (+ (point) blink-matching-paren-distance))))
  139. ;; Scan across one sexp within that range.
  140. ;; Errors or nil mean there is a mismatch.
  141. (condition-case ()
  142. (setq pos (scan-sexps (point) dir))
  143. (error (setq pos t mismatch t)))
  144. ;; Move back the other way and verify we get back to the
  145. ;; starting point. If not, these two parens don't really match.
  146. ;; Maybe the one at point is escaped and doesn't really count.
  147. (when (integerp pos)
  148. (unless (condition-case ()
  149. (eq (point) (scan-sexps pos (- dir)))
  150. (error nil))
  151. (setq pos nil)))
  152. ;; If found a "matching" paren, see if it is the right
  153. ;; kind of paren to match the one we started at.
  154. (when (integerp pos)
  155. (let ((beg (min pos oldpos)) (end (max pos oldpos)))
  156. (unless (eq (syntax-class (syntax-after beg)) 8)
  157. (setq mismatch
  158. (not (or (eq (char-before end)
  159. ;; This can give nil.
  160. (cdr (syntax-after beg)))
  161. (eq (char-after beg)
  162. ;; This can give nil.
  163. (cdr (syntax-after (1- end))))
  164. ;; The cdr might hold a new paren-class
  165. ;; info rather than a matching-char info,
  166. ;; in which case the two CDRs should match.
  167. (eq (cdr (syntax-after (1- end)))
  168. (cdr (syntax-after beg))))))))))))
  169. ;;
  170. ;; Highlight the other end of the sexp, or unhighlight if none.
  171. (if (not pos)
  172. (progn
  173. ;; If not at a paren that has a match,
  174. ;; turn off any previous paren highlighting.
  175. (and show-paren-overlay (overlay-buffer show-paren-overlay)
  176. (delete-overlay show-paren-overlay))
  177. (and show-paren-overlay-1 (overlay-buffer show-paren-overlay-1)
  178. (delete-overlay show-paren-overlay-1)))
  179. ;;
  180. ;; Use the correct face.
  181. (if mismatch
  182. (progn
  183. (if show-paren-ring-bell-on-mismatch
  184. (beep))
  185. (setq face 'show-paren-mismatch))
  186. (setq face 'show-paren-match))
  187. ;;
  188. ;; If matching backwards, highlight the closeparen
  189. ;; before point as well as its matching open.
  190. ;; If matching forward, and the openparen is unbalanced,
  191. ;; highlight the paren at point to indicate misbalance.
  192. ;; Otherwise, turn off any such highlighting.
  193. (if (and (not show-paren-highlight-openparen) (= dir 1) (integerp pos))
  194. (when (and show-paren-overlay-1
  195. (overlay-buffer show-paren-overlay-1))
  196. (delete-overlay show-paren-overlay-1))
  197. (let ((from (if (= dir 1)
  198. (point)
  199. (- (point) 1)))
  200. (to (if (= dir 1)
  201. (+ (point) 1)
  202. (point))))
  203. (if show-paren-overlay-1
  204. (move-overlay show-paren-overlay-1 from to (current-buffer))
  205. (setq show-paren-overlay-1 (make-overlay from to nil t)))
  206. ;; Always set the overlay face, since it varies.
  207. (overlay-put show-paren-overlay-1 'priority show-paren-priority)
  208. (overlay-put show-paren-overlay-1 'face face)))
  209. ;;
  210. ;; Turn on highlighting for the matching paren, if found.
  211. ;; If it's an unmatched paren, turn off any such highlighting.
  212. (unless (integerp pos)
  213. (delete-overlay show-paren-overlay))
  214. (let ((to (if (or (eq show-paren-style 'expression)
  215. (and (eq show-paren-style 'mixed)
  216. (not (pos-visible-in-window-p pos))))
  217. (point)
  218. pos))
  219. (from (if (or (eq show-paren-style 'expression)
  220. (and (eq show-paren-style 'mixed)
  221. (not (pos-visible-in-window-p pos))))
  222. pos
  223. (save-excursion
  224. (goto-char pos)
  225. (- (point) dir)))))
  226. (if show-paren-overlay
  227. (move-overlay show-paren-overlay from to (current-buffer))
  228. (setq show-paren-overlay (make-overlay from to nil t))))
  229. ;;
  230. ;; Always set the overlay face, since it varies.
  231. (overlay-put show-paren-overlay 'priority show-paren-priority)
  232. (overlay-put show-paren-overlay 'face face)))
  233. ;; show-paren-mode is nil in this buffer.
  234. (and show-paren-overlay
  235. (delete-overlay show-paren-overlay))
  236. (and show-paren-overlay-1
  237. (delete-overlay show-paren-overlay-1))))
  238. (provide 'paren)
  239. ;;; paren.el ends here