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