; Fix paths in MAINTAINERS
[emacs.git] / lisp / paren.el
blob53eb50077f25ce01cc3b046f60d3badc2d9c7197
1 ;;; paren.el --- highlight matching paren
3 ;; Copyright (C) 1993, 1996, 2001-2016 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 (define-obsolete-face-alias 'show-paren-match-face 'show-paren-match "22.1")
86 (define-obsolete-face-alias 'show-paren-mismatch-face
87 'show-paren-mismatch "22.1")
89 (defcustom show-paren-highlight-openparen t
90 "Non-nil turns on openparen highlighting when matching forward.
91 When nil, and point stands just before an open paren, the paren
92 is not highlighted, the cursor being regarded as adequate to mark
93 its position."
94 :type 'boolean)
96 (defvar show-paren--idle-timer nil)
97 (defvar show-paren--overlay
98 (let ((ol (make-overlay (point) (point) nil t))) (delete-overlay ol) ol)
99 "Overlay used to highlight the matching paren.")
100 (defvar show-paren--overlay-1
101 (let ((ol (make-overlay (point) (point) nil t))) (delete-overlay ol) ol)
102 "Overlay used to highlight the paren at point.")
105 ;;;###autoload
106 (define-minor-mode show-paren-mode
107 "Toggle visualization of matching parens (Show Paren mode).
108 With a prefix argument ARG, enable Show Paren mode if ARG is
109 positive, and disable it otherwise. If called from Lisp, enable
110 the mode if ARG is omitted or nil.
112 Show Paren mode is a global minor mode. When enabled, any
113 matching parenthesis is highlighted in `show-paren-style' after
114 `show-paren-delay' seconds of Emacs idle time."
115 :global t :group 'paren-showing
116 ;; Enable or disable the mechanism.
117 ;; First get rid of the old idle timer.
118 (when show-paren--idle-timer
119 (cancel-timer show-paren--idle-timer)
120 (setq show-paren--idle-timer nil))
121 (setq show-paren--idle-timer (run-with-idle-timer
122 show-paren-delay t
123 #'show-paren-function))
124 (unless show-paren-mode
125 (delete-overlay show-paren--overlay)
126 (delete-overlay show-paren--overlay-1)))
128 (defun show-paren--unescaped-p (pos)
129 "Determine whether the paren after POS is unescaped."
130 (save-excursion
131 (goto-char pos)
132 (= (logand (skip-syntax-backward "/\\") 1) 0)))
134 (defun show-paren--categorize-paren (pos)
135 "Determine whether the character after POS has paren syntax,
136 and if so, return a cons (DIR . OUTSIDE), where DIR is 1 for an
137 open paren, -1 for a close paren, and OUTSIDE is the buffer
138 position of the outside of the paren. If the character isn't a
139 paren, or it is an escaped paren, return nil."
140 (cond
141 ((and (eq (syntax-class (syntax-after pos)) 4)
142 (show-paren--unescaped-p pos))
143 (cons 1 pos))
144 ((and (eq (syntax-class (syntax-after pos)) 5)
145 (show-paren--unescaped-p pos))
146 (cons -1 (1+ pos)))))
148 (defun show-paren--locate-near-paren ()
149 "Locate an unescaped paren \"near\" point to show.
150 If one is found, return the cons (DIR . OUTSIDE), where DIR is 1
151 for an open paren, -1 for a close paren, and OUTSIDE is the buffer
152 position of the outside of the paren. Otherwise return nil."
153 (let* ((ind-pos (save-excursion (back-to-indentation) (point)))
154 (eol-pos
155 (save-excursion
156 (end-of-line) (skip-chars-backward " \t" ind-pos) (point)))
157 (before (show-paren--categorize-paren (1- (point))))
158 (after (show-paren--categorize-paren (point))))
159 (cond
160 ;; Point is immediately outside a paren.
161 ((eq (car before) -1) before)
162 ((eq (car after) 1) after)
163 ;; Point is immediately inside a paren.
164 ((and show-paren-when-point-inside-paren before))
165 ((and show-paren-when-point-inside-paren after))
166 ;; Point is in the whitespace before the code.
167 ((and show-paren-when-point-in-periphery
168 (<= (point) ind-pos))
169 (or (show-paren--categorize-paren ind-pos)
170 (show-paren--categorize-paren (1- eol-pos))))
171 ;; Point is in the whitespace after the code.
172 ((and show-paren-when-point-in-periphery
173 (>= (point) eol-pos))
174 (show-paren--categorize-paren (1- eol-pos))))))
176 (defvar show-paren-data-function #'show-paren--default
177 "Function to find the opener/closer \"near\" point and its match.
178 The function is called with no argument and should return either nil
179 if there's no opener/closer near point, or a list of the form
180 \(HERE-BEG HERE-END THERE-BEG THERE-END MISMATCH)
181 Where HERE-BEG..HERE-END is expected to be near point.")
183 (defun show-paren--default ()
184 (let* ((temp (show-paren--locate-near-paren))
185 (dir (car temp))
186 (outside (cdr temp))
187 pos mismatch here-beg here-end)
189 ;; Find the other end of the sexp.
190 (when dir
191 (setq here-beg (if (eq dir 1) outside (1- outside))
192 here-end (if (eq dir 1) (1+ outside) outside))
193 (save-restriction
194 ;; Determine the range within which to look for a match.
195 (when blink-matching-paren-distance
196 (narrow-to-region
197 (max (point-min) (- (point) blink-matching-paren-distance))
198 (min (point-max) (+ (point) blink-matching-paren-distance))))
199 ;; Scan across one sexp within that range.
200 ;; Errors or nil mean there is a mismatch.
201 (condition-case ()
202 (setq pos (scan-sexps outside dir))
203 (error (setq pos t mismatch t)))
204 ;; Move back the other way and verify we get back to the
205 ;; starting point. If not, these two parens don't really match.
206 ;; Maybe the one at point is escaped and doesn't really count,
207 ;; or one is inside a comment.
208 (when (integerp pos)
209 (unless (condition-case ()
210 (eq outside (scan-sexps pos (- dir)))
211 (error nil))
212 (setq pos nil)))
213 ;; If found a "matching" paren, see if it is the right
214 ;; kind of paren to match the one we started at.
215 (if (not (integerp pos))
216 (if mismatch (list here-beg here-end nil nil t))
217 (let ((beg (min pos outside)) (end (max pos outside)))
218 (unless (eq (syntax-class (syntax-after beg)) 8)
219 (setq mismatch
220 (not (or (eq (char-before end)
221 ;; This can give nil.
222 (cdr (syntax-after beg)))
223 (eq (char-after beg)
224 ;; This can give nil.
225 (cdr (syntax-after (1- end))))
226 ;; The cdr might hold a new paren-class
227 ;; info rather than a matching-char info,
228 ;; in which case the two CDRs should match.
229 (eq (cdr (syntax-after (1- end)))
230 (cdr (syntax-after beg)))))))
231 (list here-beg here-end
232 (if (= dir 1) (1- pos) pos)
233 (if (= dir 1) pos (1+ pos))
234 mismatch)))))))
236 ;; Find the place to show, if there is one,
237 ;; and show it until input arrives.
238 (defun show-paren-function ()
239 (let ((data (and show-paren-mode (funcall show-paren-data-function))))
240 (if (not data)
241 (progn
242 ;; If show-paren-mode is nil in this buffer or if not at a paren that
243 ;; has a match, turn off any previous paren highlighting.
244 (delete-overlay show-paren--overlay)
245 (delete-overlay show-paren--overlay-1))
247 ;; Found something to highlight.
248 (let* ((here-beg (nth 0 data))
249 (here-end (nth 1 data))
250 (there-beg (nth 2 data))
251 (there-end (nth 3 data))
252 (mismatch (nth 4 data))
253 (face
254 (if mismatch
255 (progn
256 (if show-paren-ring-bell-on-mismatch
257 (beep))
258 'show-paren-mismatch)
259 'show-paren-match)))
261 ;; If matching backwards, highlight the closeparen
262 ;; before point as well as its matching open.
263 ;; If matching forward, and the openparen is unbalanced,
264 ;; highlight the paren at point to indicate misbalance.
265 ;; Otherwise, turn off any such highlighting.
266 (if (or (not here-beg)
267 (and (not show-paren-highlight-openparen)
268 (> here-end (point))
269 (<= here-beg (point))
270 (integerp there-beg)))
271 (delete-overlay show-paren--overlay-1)
272 (move-overlay show-paren--overlay-1
273 here-beg here-end (current-buffer))
274 ;; Always set the overlay face, since it varies.
275 (overlay-put show-paren--overlay-1 'priority show-paren-priority)
276 (overlay-put show-paren--overlay-1 'face face))
278 ;; Turn on highlighting for the matching paren, if found.
279 ;; If it's an unmatched paren, turn off any such highlighting.
280 (if (not there-beg)
281 (delete-overlay show-paren--overlay)
282 (if (or (eq show-paren-style 'expression)
283 (and (eq show-paren-style 'mixed)
284 (let ((closest (if (< there-beg here-beg)
285 (1- there-end) (1+ there-beg))))
286 (not (pos-visible-in-window-p closest)))))
287 (move-overlay show-paren--overlay
288 (if (< there-beg here-beg) here-end here-beg)
289 (if (< there-beg here-beg) there-beg there-end)
290 (current-buffer))
291 (move-overlay show-paren--overlay
292 there-beg there-end (current-buffer)))
293 ;; Always set the overlay face, since it varies.
294 (overlay-put show-paren--overlay 'priority show-paren-priority)
295 (overlay-put show-paren--overlay 'face face))))))
297 (provide 'paren)
299 ;;; paren.el ends here