Merge branch 'master' into comment-cache
[emacs.git] / lisp / paren.el
bloba8ac09cf9166ac681970b2b97af254d6e171ef22
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 (let* ((temp (show-paren--locate-near-paren))
180 (dir (car temp))
181 (outside (cdr temp))
182 pos mismatch here-beg here-end)
184 ;; Find the other end of the sexp.
185 (when dir
186 (setq here-beg (if (eq dir 1) outside (1- outside))
187 here-end (if (eq dir 1) (1+ outside) outside))
188 (save-restriction
189 ;; Determine the range within which to look for a match.
190 (when blink-matching-paren-distance
191 (narrow-to-region
192 (max (point-min) (- (point) blink-matching-paren-distance))
193 (min (point-max) (+ (point) blink-matching-paren-distance))))
194 ;; Scan across one sexp within that range.
195 ;; Errors or nil mean there is a mismatch.
196 (condition-case ()
197 (setq pos (scan-sexps outside dir))
198 (error (setq pos t mismatch t)))
199 ;; Move back the other way and verify we get back to the
200 ;; starting point. If not, these two parens don't really match.
201 ;; Maybe the one at point is escaped and doesn't really count,
202 ;; or one is inside a comment.
203 (when (integerp pos)
204 (unless (condition-case ()
205 (eq outside (scan-sexps pos (- dir)))
206 (error nil))
207 (setq pos nil)))
208 ;; If found a "matching" paren, see if it is the right
209 ;; kind of paren to match the one we started at.
210 (if (not (integerp pos))
211 (if mismatch (list here-beg here-end nil nil t))
212 (let ((beg (min pos outside)) (end (max pos outside)))
213 (unless (eq (syntax-class (syntax-after beg)) 8)
214 (setq mismatch
215 (not (or (eq (char-before end)
216 ;; This can give nil.
217 (cdr (syntax-after beg)))
218 (eq (char-after beg)
219 ;; This can give nil.
220 (cdr (syntax-after (1- end))))
221 ;; The cdr might hold a new paren-class
222 ;; info rather than a matching-char info,
223 ;; in which case the two CDRs should match.
224 (eq (cdr (syntax-after (1- end)))
225 (cdr (syntax-after beg)))))))
226 (list here-beg here-end
227 (if (= dir 1) (1- pos) pos)
228 (if (= dir 1) pos (1+ pos))
229 mismatch)))))))
231 ;; Find the place to show, if there is one,
232 ;; and show it until input arrives.
233 (defun show-paren-function ()
234 (let ((data (and show-paren-mode (funcall show-paren-data-function))))
235 (if (not data)
236 (progn
237 ;; If show-paren-mode is nil in this buffer or if not at a paren that
238 ;; has a match, turn off any previous paren highlighting.
239 (delete-overlay show-paren--overlay)
240 (delete-overlay show-paren--overlay-1))
242 ;; Found something to highlight.
243 (let* ((here-beg (nth 0 data))
244 (here-end (nth 1 data))
245 (there-beg (nth 2 data))
246 (there-end (nth 3 data))
247 (mismatch (nth 4 data))
248 (face
249 (if mismatch
250 (progn
251 (if show-paren-ring-bell-on-mismatch
252 (beep))
253 'show-paren-mismatch)
254 'show-paren-match)))
256 ;; If matching backwards, highlight the closeparen
257 ;; before point as well as its matching open.
258 ;; If matching forward, and the openparen is unbalanced,
259 ;; highlight the paren at point to indicate misbalance.
260 ;; Otherwise, turn off any such highlighting.
261 (if (or (not here-beg)
262 (and (not show-paren-highlight-openparen)
263 (> here-end (point))
264 (<= here-beg (point))
265 (integerp there-beg)))
266 (delete-overlay show-paren--overlay-1)
267 (move-overlay show-paren--overlay-1
268 here-beg here-end (current-buffer))
269 ;; Always set the overlay face, since it varies.
270 (overlay-put show-paren--overlay-1 'priority show-paren-priority)
271 (overlay-put show-paren--overlay-1 'face face))
273 ;; Turn on highlighting for the matching paren, if found.
274 ;; If it's an unmatched paren, turn off any such highlighting.
275 (if (not there-beg)
276 (delete-overlay show-paren--overlay)
277 (if (or (eq show-paren-style 'expression)
278 (and (eq show-paren-style 'mixed)
279 (let ((closest (if (< there-beg here-beg)
280 (1- there-end) (1+ there-beg))))
281 (not (pos-visible-in-window-p closest)))))
282 (move-overlay show-paren--overlay
283 (if (< there-beg here-beg) here-end here-beg)
284 (if (< there-beg here-beg) there-beg there-end)
285 (current-buffer))
286 (move-overlay show-paren--overlay
287 there-beg there-end (current-buffer)))
288 ;; Always set the overlay face, since it varies.
289 (overlay-put show-paren--overlay 'priority show-paren-priority)
290 (overlay-put show-paren--overlay 'face face))))))
292 (provide 'paren)
294 ;;; paren.el ends here