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