1 ;;; paren.el --- highlight matching paren
3 ;; Copyright (C) 1993, 1996, 2001-2013 Free Software Foundation, Inc.
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/>.
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.
35 (defgroup paren-showing nil
36 "Showing (un)matching of parens and expressions."
38 :group
'paren-matching
)
40 ;; This is the overlay used to highlight the matching paren.
41 (defvar show-paren-overlay nil
)
42 ;; This is the overlay used to highlight the closeparen right before point.
43 (defvar show-paren-overlay-1 nil
)
45 (defcustom show-paren-style
'parenthesis
46 "Style used when showing a matching paren.
47 Valid styles are `parenthesis' (meaning show the matching paren),
48 `expression' (meaning show the entire expression enclosed by the paren) and
49 `mixed' (meaning show the matching paren if it is visible, and the expression
51 :type
'(choice (const parenthesis
) (const expression
) (const mixed
))
52 :group
'paren-showing
)
54 (defcustom show-paren-delay
0.125
55 "Time in seconds to delay before showing a matching paren.
56 If you change this without using customize while `show-paren-mode' is
57 active, you must toggle the mode off and on again for this to take effect."
58 :type
'(number :tag
"seconds")
59 :initialize
'custom-initialize-default
60 :set
(lambda (sym val
)
61 (if (not show-paren-mode
)
66 :group
'paren-showing
)
68 (defcustom show-paren-priority
1000
69 "Priority of paren highlighting overlays."
74 (defcustom show-paren-ring-bell-on-mismatch nil
75 "If non-nil, beep if mismatched paren is detected."
80 (defgroup paren-showing-faces nil
81 "Group for faces of Show Paren mode."
86 (defface show-paren-match
87 '((((class color
) (background light
))
88 :background
"turquoise") ; looks OK on tty (becomes cyan)
89 (((class color
) (background dark
))
90 :background
"steelblue3") ; looks OK on tty (becomes blue)
95 "Show Paren mode face used for a matching paren."
96 :group
'paren-showing-faces
)
97 (define-obsolete-face-alias 'show-paren-match-face
'show-paren-match
"22.1")
99 (defface show-paren-mismatch
100 '((((class color
)) (:foreground
"white" :background
"purple"))
101 (t (:inverse-video t
)))
102 "Show Paren mode face used for a mismatching paren."
103 :group
'paren-showing-faces
)
104 (define-obsolete-face-alias 'show-paren-mismatch-face
105 'show-paren-mismatch
"22.1")
107 (defvar show-paren-highlight-openparen t
108 "Non-nil turns on openparen highlighting when matching forward.")
110 (defvar show-paren-idle-timer nil
)
113 (define-minor-mode show-paren-mode
114 "Toggle visualization of matching parens (Show Paren mode).
115 With a prefix argument ARG, enable Show Paren mode if ARG is
116 positive, and disable it otherwise. If called from Lisp, enable
117 the mode if ARG is omitted or nil.
119 Show Paren mode is a global minor mode. When enabled, any
120 matching parenthesis is highlighted in `show-paren-style' after
121 `show-paren-delay' seconds of Emacs idle time."
122 :global t
:group
'paren-showing
123 ;; Enable or disable the mechanism.
124 ;; First get rid of the old idle timer.
125 (if show-paren-idle-timer
126 (cancel-timer show-paren-idle-timer
))
127 (setq show-paren-idle-timer nil
)
128 ;; If show-paren-mode is enabled in some buffer now,
129 ;; set up a new timer.
130 (when (memq t
(mapcar (lambda (buffer)
131 (with-current-buffer buffer
134 (setq show-paren-idle-timer
(run-with-idle-timer
136 'show-paren-function
)))
137 (unless show-paren-mode
138 (and show-paren-overlay
139 (eq (overlay-buffer show-paren-overlay
) (current-buffer))
140 (delete-overlay show-paren-overlay
))
141 (and show-paren-overlay-1
142 (eq (overlay-buffer show-paren-overlay-1
) (current-buffer))
143 (delete-overlay show-paren-overlay-1
))))
145 ;; Find the place to show, if there is one,
146 ;; and show it until input arrives.
147 (defun show-paren-function ()
149 (let* ((oldpos (point))
150 (dir (cond ((eq (syntax-class (syntax-after (1- (point)))) 5) -
1)
151 ((eq (syntax-class (syntax-after (point))) 4) 1)))
154 ;; Verify an even number of quoting characters precede the paren.
155 ;; Follow the same logic as in `blink-matching-open'.
156 (= (if (= dir -
1) 1 0)
159 (if (= dir -
1) (forward-char -
1))
160 (skip-syntax-backward "/\\")
164 ;; Find the other end of the sexp.
168 ;; Determine the range within which to look for a match.
169 (when blink-matching-paren-distance
171 (max (point-min) (- (point) blink-matching-paren-distance
))
172 (min (point-max) (+ (point) blink-matching-paren-distance
))))
173 ;; Scan across one sexp within that range.
174 ;; Errors or nil mean there is a mismatch.
176 (setq pos
(scan-sexps (point) dir
))
177 (error (setq pos t mismatch t
)))
178 ;; Move back the other way and verify we get back to the
179 ;; starting point. If not, these two parens don't really match.
180 ;; Maybe the one at point is escaped and doesn't really count.
182 (unless (condition-case ()
183 (eq (point) (scan-sexps pos
(- dir
)))
186 ;; If found a "matching" paren, see if it is the right
187 ;; kind of paren to match the one we started at.
189 (let ((beg (min pos oldpos
)) (end (max pos oldpos
)))
190 (unless (eq (syntax-class (syntax-after beg
)) 8)
192 (not (or (eq (char-before end
)
193 ;; This can give nil.
194 (cdr (syntax-after beg
)))
196 ;; This can give nil.
197 (cdr (syntax-after (1- end
))))
198 ;; The cdr might hold a new paren-class
199 ;; info rather than a matching-char info,
200 ;; in which case the two CDRs should match.
201 (eq (cdr (syntax-after (1- end
)))
202 (cdr (syntax-after beg
))))))))))))
204 ;; Highlight the other end of the sexp, or unhighlight if none.
207 ;; If not at a paren that has a match,
208 ;; turn off any previous paren highlighting.
209 (and show-paren-overlay
(overlay-buffer show-paren-overlay
)
210 (delete-overlay show-paren-overlay
))
211 (and show-paren-overlay-1
(overlay-buffer show-paren-overlay-1
)
212 (delete-overlay show-paren-overlay-1
)))
214 ;; Use the correct face.
217 (if show-paren-ring-bell-on-mismatch
219 (setq face
'show-paren-mismatch
))
220 (setq face
'show-paren-match
))
222 ;; If matching backwards, highlight the closeparen
223 ;; before point as well as its matching open.
224 ;; If matching forward, and the openparen is unbalanced,
225 ;; highlight the paren at point to indicate misbalance.
226 ;; Otherwise, turn off any such highlighting.
227 (if (and (not show-paren-highlight-openparen
) (= dir
1) (integerp pos
))
228 (when (and show-paren-overlay-1
229 (overlay-buffer show-paren-overlay-1
))
230 (delete-overlay show-paren-overlay-1
))
231 (let ((from (if (= dir
1)
237 (if show-paren-overlay-1
238 (move-overlay show-paren-overlay-1 from to
(current-buffer))
239 (setq show-paren-overlay-1
(make-overlay from to nil t
)))
240 ;; Always set the overlay face, since it varies.
241 (overlay-put show-paren-overlay-1
'priority show-paren-priority
)
242 (overlay-put show-paren-overlay-1
'face face
)))
244 ;; Turn on highlighting for the matching paren, if found.
245 ;; If it's an unmatched paren, turn off any such highlighting.
246 (if (not (integerp pos
))
247 (when show-paren-overlay
(delete-overlay show-paren-overlay
))
248 (let ((to (if (or (eq show-paren-style
'expression
)
249 (and (eq show-paren-style
'mixed
)
250 (not (pos-visible-in-window-p pos
))))
253 (from (if (or (eq show-paren-style
'expression
)
254 (and (eq show-paren-style
'mixed
)
255 (not (pos-visible-in-window-p pos
))))
260 (if show-paren-overlay
261 (move-overlay show-paren-overlay from to
(current-buffer))
262 (setq show-paren-overlay
(make-overlay from to nil t
))))
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 ;; show-paren-mode is nil in this buffer.
267 (and show-paren-overlay
268 (delete-overlay show-paren-overlay
))
269 (and show-paren-overlay-1
270 (delete-overlay show-paren-overlay-1
))))
274 ;;; paren.el ends here