paren.el 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. ;;; paren.el --- highlight matching paren
  2. ;; Copyright (C) 1993, 1996, 2001-2017 Free Software Foundation, Inc.
  3. ;; Author: rms@gnu.org
  4. ;; Maintainer: emacs-devel@gnu.org
  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. (defcustom show-paren-style 'parenthesis
  28. "Style used when showing a matching paren.
  29. Valid styles are `parenthesis' (meaning show the matching paren),
  30. `expression' (meaning show the entire expression enclosed by the paren) and
  31. `mixed' (meaning show the matching paren if it is visible, and the expression
  32. otherwise)."
  33. :type '(choice (const parenthesis) (const expression) (const mixed)))
  34. (defcustom show-paren-delay 0.125
  35. "Time in seconds to delay before showing a matching paren.
  36. If you change this without using customize while `show-paren-mode' is
  37. active, you must toggle the mode off and on again for this to take effect."
  38. :type '(number :tag "seconds")
  39. :initialize 'custom-initialize-default
  40. :set (lambda (sym val)
  41. (if (not show-paren-mode)
  42. (set sym val)
  43. (show-paren-mode -1)
  44. (set sym val)
  45. (show-paren-mode 1))))
  46. (defcustom show-paren-priority 1000
  47. "Priority of paren highlighting overlays."
  48. :type 'integer
  49. :version "21.1")
  50. (defcustom show-paren-ring-bell-on-mismatch nil
  51. "If non-nil, beep if mismatched paren is detected."
  52. :type 'boolean
  53. :version "20.3")
  54. (defcustom show-paren-when-point-inside-paren nil
  55. "If non-nil, show parens when point is just inside one.
  56. This will only be done when point isn't also just outside a paren."
  57. :type 'boolean
  58. :version "25.1")
  59. (defcustom show-paren-when-point-in-periphery nil
  60. "If non-nil, show parens when point is in the line's periphery.
  61. The periphery is at the beginning or end of a line or in any
  62. whitespace there."
  63. :type 'boolean
  64. :version "25.1")
  65. (defcustom show-paren-highlight-openparen t
  66. "Non-nil turns on openparen highlighting when matching forward.
  67. When nil, and point stands just before an open paren, the paren
  68. is not highlighted, the cursor being regarded as adequate to mark
  69. its position."
  70. :type 'boolean)
  71. (defvar show-paren--idle-timer nil)
  72. (defvar show-paren--overlay
  73. (let ((ol (make-overlay (point) (point) nil t))) (delete-overlay ol) ol)
  74. "Overlay used to highlight the matching paren.")
  75. (defvar show-paren--overlay-1
  76. (let ((ol (make-overlay (point) (point) nil t))) (delete-overlay ol) ol)
  77. "Overlay used to highlight the paren at point.")
  78. ;;;###autoload
  79. (define-minor-mode show-paren-mode
  80. "Toggle visualization of matching parens (Show Paren mode).
  81. With a prefix argument ARG, enable Show Paren mode if ARG is
  82. positive, and disable it otherwise. If called from Lisp, enable
  83. the mode if ARG is omitted or nil.
  84. Show Paren mode is a global minor mode. When enabled, any
  85. matching parenthesis is highlighted in `show-paren-style' after
  86. `show-paren-delay' seconds of Emacs idle time."
  87. :global t :group 'paren-showing
  88. ;; Enable or disable the mechanism.
  89. ;; First get rid of the old idle timer.
  90. (when show-paren--idle-timer
  91. (cancel-timer show-paren--idle-timer)
  92. (setq show-paren--idle-timer nil))
  93. (setq show-paren--idle-timer (run-with-idle-timer
  94. show-paren-delay t
  95. #'show-paren-function))
  96. (unless show-paren-mode
  97. (delete-overlay show-paren--overlay)
  98. (delete-overlay show-paren--overlay-1)))
  99. (defun show-paren--unescaped-p (pos)
  100. "Determine whether the paren after POS is unescaped."
  101. (save-excursion
  102. (goto-char pos)
  103. (= (logand (skip-syntax-backward "/\\") 1) 0)))
  104. (defun show-paren--categorize-paren (pos)
  105. "Determine whether the character after POS has paren syntax,
  106. and if so, return a cons (DIR . OUTSIDE), where DIR is 1 for an
  107. open paren, -1 for a close paren, and OUTSIDE is the buffer
  108. position of the outside of the paren. If the character isn't a
  109. paren, or it is an escaped paren, return nil."
  110. (cond
  111. ((and (eq (syntax-class (syntax-after pos)) 4)
  112. (show-paren--unescaped-p pos))
  113. (cons 1 pos))
  114. ((and (eq (syntax-class (syntax-after pos)) 5)
  115. (show-paren--unescaped-p pos))
  116. (cons -1 (1+ pos)))))
  117. (defun show-paren--locate-near-paren ()
  118. "Locate an unescaped paren \"near\" point to show.
  119. If one is found, return the cons (DIR . OUTSIDE), where DIR is 1
  120. for an open paren, -1 for a close paren, and OUTSIDE is the buffer
  121. position of the outside of the paren. Otherwise return nil."
  122. (let* ((ind-pos (save-excursion (back-to-indentation) (point)))
  123. (eol-pos
  124. (save-excursion
  125. (end-of-line) (skip-chars-backward " \t" ind-pos) (point)))
  126. (before (show-paren--categorize-paren (1- (point))))
  127. (after (show-paren--categorize-paren (point))))
  128. (cond
  129. ;; Point is immediately outside a paren.
  130. ((eq (car before) -1) before)
  131. ((eq (car after) 1) after)
  132. ;; Point is immediately inside a paren.
  133. ((and show-paren-when-point-inside-paren before))
  134. ((and show-paren-when-point-inside-paren after))
  135. ;; Point is in the whitespace before the code.
  136. ((and show-paren-when-point-in-periphery
  137. (<= (point) ind-pos))
  138. (or (show-paren--categorize-paren ind-pos)
  139. (show-paren--categorize-paren (1- eol-pos))))
  140. ;; Point is in the whitespace after the code.
  141. ((and show-paren-when-point-in-periphery
  142. (>= (point) eol-pos))
  143. (show-paren--categorize-paren (1- eol-pos))))))
  144. (defvar show-paren-data-function #'show-paren--default
  145. "Function to find the opener/closer \"near\" point and its match.
  146. The function is called with no argument and should return either nil
  147. if there's no opener/closer near point, or a list of the form
  148. \(HERE-BEG HERE-END THERE-BEG THERE-END MISMATCH)
  149. Where HERE-BEG..HERE-END is expected to be near point.")
  150. (defun show-paren--default ()
  151. "Finds the opener/closer near point and its match.
  152. It is the default value of `show-paren-data-function'."
  153. (let* ((temp (show-paren--locate-near-paren))
  154. (dir (car temp))
  155. (outside (cdr temp))
  156. pos mismatch here-beg here-end)
  157. ;;
  158. ;; Find the other end of the sexp.
  159. (when dir
  160. (setq here-beg (if (eq dir 1) outside (1- outside))
  161. here-end (if (eq dir 1) (1+ outside) outside))
  162. (save-restriction
  163. ;; Determine the range within which to look for a match.
  164. (when blink-matching-paren-distance
  165. (narrow-to-region
  166. (max (point-min) (- (point) blink-matching-paren-distance))
  167. (min (point-max) (+ (point) blink-matching-paren-distance))))
  168. ;; Scan across one sexp within that range.
  169. ;; Errors or nil mean there is a mismatch.
  170. (condition-case ()
  171. (setq pos (scan-sexps outside dir))
  172. (error (setq pos t mismatch t)))
  173. ;; Move back the other way and verify we get back to the
  174. ;; starting point. If not, these two parens don't really match.
  175. ;; Maybe the one at point is escaped and doesn't really count,
  176. ;; or one is inside a comment.
  177. (when (integerp pos)
  178. (unless (condition-case ()
  179. (eq outside (scan-sexps pos (- dir)))
  180. (error nil))
  181. (setq pos nil)))
  182. ;; If found a "matching" paren, see if it is the right
  183. ;; kind of paren to match the one we started at.
  184. (if (not (integerp pos))
  185. (if mismatch (list here-beg here-end nil nil t))
  186. (let ((beg (min pos outside)) (end (max pos outside)))
  187. (unless (eq (syntax-class (syntax-after beg)) 8)
  188. (setq mismatch
  189. (not (or (eq (char-before end)
  190. ;; This can give nil.
  191. (cdr (syntax-after beg)))
  192. (eq (char-after beg)
  193. ;; This can give nil.
  194. (cdr (syntax-after (1- end))))
  195. ;; The cdr might hold a new paren-class
  196. ;; info rather than a matching-char info,
  197. ;; in which case the two CDRs should match.
  198. (eq (cdr (syntax-after (1- end)))
  199. (cdr (syntax-after beg)))))))
  200. (list here-beg here-end
  201. (if (= dir 1) (1- pos) pos)
  202. (if (= dir 1) pos (1+ pos))
  203. mismatch)))))))
  204. (defun show-paren-function ()
  205. "Highlight the parentheses until the next input arrives."
  206. (let ((data (and show-paren-mode (funcall show-paren-data-function))))
  207. (if (not data)
  208. (progn
  209. ;; If show-paren-mode is nil in this buffer or if not at a paren that
  210. ;; has a match, turn off any previous paren highlighting.
  211. (delete-overlay show-paren--overlay)
  212. (delete-overlay show-paren--overlay-1))
  213. ;; Found something to highlight.
  214. (let* ((here-beg (nth 0 data))
  215. (here-end (nth 1 data))
  216. (there-beg (nth 2 data))
  217. (there-end (nth 3 data))
  218. (mismatch (nth 4 data))
  219. (highlight-expression
  220. (or (eq show-paren-style 'expression)
  221. (and there-beg
  222. (eq show-paren-style 'mixed)
  223. (let ((closest (if (< there-beg here-beg)
  224. (1- there-end) (1+ there-beg))))
  225. (not (pos-visible-in-window-p closest))))))
  226. (face
  227. (cond
  228. (mismatch
  229. (if show-paren-ring-bell-on-mismatch
  230. (beep))
  231. 'show-paren-mismatch)
  232. (highlight-expression 'show-paren-match-expression)
  233. (t 'show-paren-match))))
  234. ;;
  235. ;; If matching backwards, highlight the closeparen
  236. ;; before point as well as its matching open.
  237. ;; If matching forward, and the openparen is unbalanced,
  238. ;; highlight the paren at point to indicate misbalance.
  239. ;; Otherwise, turn off any such highlighting.
  240. (if (or (not here-beg)
  241. (and (not show-paren-highlight-openparen)
  242. (> here-end (point))
  243. (<= here-beg (point))
  244. (integerp there-beg)))
  245. (delete-overlay show-paren--overlay-1)
  246. (move-overlay show-paren--overlay-1
  247. here-beg here-end (current-buffer))
  248. ;; Always set the overlay face, since it varies.
  249. (overlay-put show-paren--overlay-1 'priority show-paren-priority)
  250. (overlay-put show-paren--overlay-1 'face face))
  251. ;;
  252. ;; Turn on highlighting for the matching paren, if found.
  253. ;; If it's an unmatched paren, turn off any such highlighting.
  254. (if (not there-beg)
  255. (delete-overlay show-paren--overlay)
  256. (if highlight-expression
  257. (move-overlay show-paren--overlay
  258. (if (< there-beg here-beg) here-end here-beg)
  259. (if (< there-beg here-beg) there-beg there-end)
  260. (current-buffer))
  261. (move-overlay show-paren--overlay
  262. there-beg there-end (current-buffer)))
  263. ;; Always set the overlay face, since it varies.
  264. (overlay-put show-paren--overlay 'priority show-paren-priority)
  265. (overlay-put show-paren--overlay 'face face))))))
  266. (provide 'paren)
  267. ;;; paren.el ends here