1 ;;; paren.el --- highlight matching paren
3 ;; Copyright (C) 1993, 1996, 2001-2012 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 :type
'(number :tag
"seconds")
57 :group
'paren-showing
)
59 (defcustom show-paren-priority
1000
60 "Priority of paren highlighting overlays."
65 (defcustom show-paren-ring-bell-on-mismatch nil
66 "If non-nil, beep if mismatched paren is detected."
71 (defgroup paren-showing-faces nil
72 "Group for faces of Show Paren mode."
77 (defface show-paren-match
78 '((((class color
) (background light
))
79 :background
"turquoise") ; looks OK on tty (becomes cyan)
80 (((class color
) (background dark
))
81 :background
"steelblue3") ; looks OK on tty (becomes blue)
86 "Show Paren mode face used for a matching paren."
87 :group
'paren-showing-faces
)
88 (define-obsolete-face-alias 'show-paren-match-face
'show-paren-match
"22.1")
90 (defface show-paren-mismatch
91 '((((class color
)) (:foreground
"white" :background
"purple"))
92 (t (:inverse-video t
)))
93 "Show Paren mode face used for a mismatching paren."
94 :group
'paren-showing-faces
)
95 (define-obsolete-face-alias 'show-paren-mismatch-face
96 'show-paren-mismatch
"22.1")
98 (defvar show-paren-highlight-openparen t
99 "Non-nil turns on openparen highlighting when matching forward.")
101 (defvar show-paren-idle-timer nil
)
104 (define-minor-mode show-paren-mode
105 "Toggle visualization of matching parens (Show Paren mode).
106 With a prefix argument ARG, enable Show Paren mode if ARG is
107 positive, and disable it otherwise. If called from Lisp, enable
108 the mode if ARG is omitted or nil.
110 Show Paren mode is a global minor mode. When enabled, any
111 matching parenthesis is highlighted in `show-paren-style' after
112 `show-paren-delay' seconds of Emacs idle time."
113 :global t
:group
'paren-showing
114 ;; Enable or disable the mechanism.
115 ;; First get rid of the old idle timer.
116 (if show-paren-idle-timer
117 (cancel-timer show-paren-idle-timer
))
118 (setq show-paren-idle-timer nil
)
119 ;; If show-paren-mode is enabled in some buffer now,
120 ;; set up a new timer.
121 (when (memq t
(mapcar (lambda (buffer)
122 (with-current-buffer buffer
125 (setq show-paren-idle-timer
(run-with-idle-timer
127 'show-paren-function
)))
128 (unless show-paren-mode
129 (and show-paren-overlay
130 (eq (overlay-buffer show-paren-overlay
) (current-buffer))
131 (delete-overlay show-paren-overlay
))
132 (and show-paren-overlay-1
133 (eq (overlay-buffer show-paren-overlay-1
) (current-buffer))
134 (delete-overlay show-paren-overlay-1
))))
136 ;; Find the place to show, if there is one,
137 ;; and show it until input arrives.
138 (defun show-paren-function ()
140 (let* ((oldpos (point))
141 (dir (cond ((eq (syntax-class (syntax-after (1- (point)))) 5) -
1)
142 ((eq (syntax-class (syntax-after (point))) 4) 1)))
145 ;; Verify an even number of quoting characters precede the paren.
146 ;; Follow the same logic as in `blink-matching-open'.
147 (= (if (= dir -
1) 1 0)
150 (if (= dir -
1) (forward-char -
1))
151 (skip-syntax-backward "/\\")
155 ;; Find the other end of the sexp.
159 ;; Determine the range within which to look for a match.
160 (when blink-matching-paren-distance
162 (max (point-min) (- (point) blink-matching-paren-distance
))
163 (min (point-max) (+ (point) blink-matching-paren-distance
))))
164 ;; Scan across one sexp within that range.
165 ;; Errors or nil mean there is a mismatch.
167 (setq pos
(scan-sexps (point) dir
))
168 (error (setq pos t mismatch t
)))
169 ;; Move back the other way and verify we get back to the
170 ;; starting point. If not, these two parens don't really match.
171 ;; Maybe the one at point is escaped and doesn't really count.
173 (unless (condition-case ()
174 (eq (point) (scan-sexps pos
(- dir
)))
177 ;; If found a "matching" paren, see if it is the right
178 ;; kind of paren to match the one we started at.
180 (let ((beg (min pos oldpos
)) (end (max pos oldpos
)))
181 (unless (eq (syntax-class (syntax-after beg
)) 8)
183 (not (or (eq (char-before end
)
184 ;; This can give nil.
185 (cdr (syntax-after beg
)))
187 ;; This can give nil.
188 (cdr (syntax-after (1- end
))))
189 ;; The cdr might hold a new paren-class
190 ;; info rather than a matching-char info,
191 ;; in which case the two CDRs should match.
192 (eq (cdr (syntax-after (1- end
)))
193 (cdr (syntax-after beg
))))))))))))
195 ;; Highlight the other end of the sexp, or unhighlight if none.
198 ;; If not at a paren that has a match,
199 ;; turn off any previous paren highlighting.
200 (and show-paren-overlay
(overlay-buffer show-paren-overlay
)
201 (delete-overlay show-paren-overlay
))
202 (and show-paren-overlay-1
(overlay-buffer show-paren-overlay-1
)
203 (delete-overlay show-paren-overlay-1
)))
205 ;; Use the correct face.
208 (if show-paren-ring-bell-on-mismatch
210 (setq face
'show-paren-mismatch
))
211 (setq face
'show-paren-match
))
213 ;; If matching backwards, highlight the closeparen
214 ;; before point as well as its matching open.
215 ;; If matching forward, and the openparen is unbalanced,
216 ;; highlight the paren at point to indicate misbalance.
217 ;; Otherwise, turn off any such highlighting.
218 (if (and (not show-paren-highlight-openparen
) (= dir
1) (integerp pos
))
219 (when (and show-paren-overlay-1
220 (overlay-buffer show-paren-overlay-1
))
221 (delete-overlay show-paren-overlay-1
))
222 (let ((from (if (= dir
1)
228 (if show-paren-overlay-1
229 (move-overlay show-paren-overlay-1 from to
(current-buffer))
230 (setq show-paren-overlay-1
(make-overlay from to nil t
)))
231 ;; Always set the overlay face, since it varies.
232 (overlay-put show-paren-overlay-1
'priority show-paren-priority
)
233 (overlay-put show-paren-overlay-1
'face face
)))
235 ;; Turn on highlighting for the matching paren, if found.
236 ;; If it's an unmatched paren, turn off any such highlighting.
237 (unless (integerp pos
)
238 (delete-overlay show-paren-overlay
))
239 (let ((to (if (or (eq show-paren-style
'expression
)
240 (and (eq show-paren-style
'mixed
)
241 (not (pos-visible-in-window-p pos
))))
244 (from (if (or (eq show-paren-style
'expression
)
245 (and (eq show-paren-style
'mixed
)
246 (not (pos-visible-in-window-p pos
))))
251 (if show-paren-overlay
252 (move-overlay show-paren-overlay from to
(current-buffer))
253 (setq show-paren-overlay
(make-overlay from to nil t
))))
255 ;; Always set the overlay face, since it varies.
256 (overlay-put show-paren-overlay
'priority show-paren-priority
)
257 (overlay-put show-paren-overlay
'face face
)))
258 ;; show-paren-mode is nil in this buffer.
259 (and show-paren-overlay
260 (delete-overlay show-paren-overlay
))
261 (and show-paren-overlay-1
262 (delete-overlay show-paren-overlay-1
))))
266 ;;; paren.el ends here