1 ;;; paren.el --- highlight matching paren.
3 ;; Copyright (C) 1993, 1996 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 2, or (at your option)
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; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
28 ;; Load this and it will display highlighting on whatever
29 ;; paren matches the one before or after point.
33 (defgroup paren-showing nil
34 "Showing (un)matching of parens and expressions."
36 :group
'paren-matching
)
38 ;; This is the overlay used to highlight the matching paren.
39 (defvar show-paren-overlay nil
)
40 ;; This is the overlay used to highlight the closeparen right before point.
41 (defvar show-paren-overlay-1 nil
)
44 (defcustom show-paren-mode nil
45 "*Toggle Show Paren mode.
46 When Show Paren mode is enabled, any matching parenthesis is highlighted
47 after `show-paren-delay' seconds of Emacs idle time.
48 Setting this variable directly does not take effect;
49 use either \\[customize] or the function `show-paren-mode'."
50 :set
(lambda (symbol value
)
51 (show-paren-mode (or value
0)))
52 :initialize
'custom-initialize-default
57 (defcustom show-paren-style
'parenthesis
58 "*Style used when showing a matching paren.
59 Valid styles are `parenthesis' (meaning show the matching paren),
60 `expression' (meaning show the entire expression enclosed by the paren) and
61 `mixed' (meaning show the matching paren if it is visible, and the expression
63 :type
'(choice (const parenthesis
) (const expression
) (const mixed
))
64 :group
'paren-showing
)
66 (defcustom show-paren-delay
67 (if (featurep 'lisp-float-type
) (/ (float 1) (float 8)) 1)
68 "*Time in seconds to delay before showing a matching paren."
69 :type
'(number :tag
"seconds")
70 :group
'paren-showing
)
72 (defcustom show-paren-priority
1000
73 "*Priority of paren highlighting overlays."
78 (defcustom show-paren-ring-bell-on-mismatch nil
79 "*If non-nil, beep if mismatched paren is detected."
84 (defface show-paren-match-face
85 '((((class color
)) (:background
"turquoise"))
86 (t (:background
"gray")))
87 "Show Paren mode face used for a matching paren."
89 :group
'paren-showing
)
91 (defface show-paren-mismatch-face
92 '((((class color
)) (:foreground
"white" :background
"purple"))
93 (t (:reverse-video t
)))
94 "Show Paren mode face used for a mismatching paren."
96 :group
'paren-showing
)
98 (defvar show-paren-idle-timer nil
)
101 (defun show-paren-mode (&optional arg
)
102 "Toggle Show Paren mode.
103 With prefix ARG, turn Show Paren mode on if and only if ARG is positive.
104 Returns the new status of Show Paren mode (non-nil means on).
106 When Show Paren mode is enabled, any matching parenthesis is highlighted
107 in `show-paren-style' after `show-paren-delay' seconds of Emacs idle time."
110 (> (prefix-numeric-value arg
) 0)
111 (not show-paren-mode
))))
112 (setq show-paren-mode on-p
)
113 ;; Turn off the usual paren-matching method
114 ;; when this one is turned on.
115 (if (local-variable-p 'show-paren-mode
)
116 (make-local-variable 'blink-matching-paren-on-screen
)
117 (kill-local-variable 'blink-matching-paren-on-screen
))
118 (setq blink-matching-paren-on-screen
(not on-p
))
120 ;; Now enable or disable the mechanism.
121 ;; First get rid of the old idle timer.
122 (if show-paren-idle-timer
123 (cancel-timer show-paren-idle-timer
))
124 (setq show-paren-idle-timer nil
)
125 ;; If show-paren-mode is enabled in some buffer now,
126 ;; set up a new timer.
127 (when (memq t
(mapcar (lambda (buffer)
128 (with-current-buffer buffer
131 (setq show-paren-idle-timer
(run-with-idle-timer
133 'show-paren-function
)))
135 (and show-paren-overlay
136 (eq (overlay-buffer show-paren-overlay
) (current-buffer))
137 (delete-overlay show-paren-overlay
))
138 (and show-paren-overlay-1
139 (eq (overlay-buffer show-paren-overlay-1
) (current-buffer))
140 (delete-overlay show-paren-overlay-1
)))))
142 ;; Find the place to show, if there is one,
143 ;; and show it until input arrives.
144 (defun show-paren-function ()
146 (let (pos dir mismatch face
(oldpos (point)))
147 (cond ((eq (char-syntax (preceding-char)) ?\
))
149 ((eq (char-syntax (following-char)) ?\
()
152 ;; Find the other end of the sexp.
156 ;; Determine the range within which to look for a match.
157 (when blink-matching-paren-distance
159 (max (point-min) (- (point) blink-matching-paren-distance
))
160 (min (point-max) (+ (point) blink-matching-paren-distance
))))
161 ;; Scan across one sexp within that range.
162 ;; Errors or nil mean there is a mismatch.
164 (setq pos
(scan-sexps (point) dir
))
165 (error (setq pos t mismatch t
)))
166 ;; If found a "matching" paren, see if it is the right
167 ;; kind of paren to match the one we started at.
169 (let ((beg (min pos oldpos
)) (end (max pos oldpos
)))
170 (when (/= (char-syntax (char-after beg
)) ?\$
)
172 (not (eq (char-before end
)
173 ;; This can give nil.
174 (matching-paren (char-after beg
)))))))))))
176 ;; Highlight the other end of the sexp, or unhighlight if none.
179 ;; If not at a paren that has a match,
180 ;; turn off any previous paren highlighting.
181 (and show-paren-overlay
(overlay-buffer show-paren-overlay
)
182 (delete-overlay show-paren-overlay
))
183 (and show-paren-overlay-1
(overlay-buffer show-paren-overlay-1
)
184 (delete-overlay show-paren-overlay-1
)))
186 ;; Use the correct face.
189 (if show-paren-ring-bell-on-mismatch
191 (setq face
'show-paren-mismatch-face
))
192 (setq face
'show-paren-match-face
))
194 ;; If matching backwards, highlight the closeparen
195 ;; before point as well as its matching open.
196 ;; If matching forward, and the openparen is unbalanced,
197 ;; highlight the paren at point to indicate misbalance.
198 ;; Otherwise, turn off any such highlighting.
199 (if (and (= dir
1) (integerp pos
))
200 (when (and show-paren-overlay-1
201 (overlay-buffer show-paren-overlay-1
))
202 (delete-overlay show-paren-overlay-1
))
203 (let ((from (if (= dir
1)
209 (if show-paren-overlay-1
210 (move-overlay show-paren-overlay-1 from to
(current-buffer))
211 (setq show-paren-overlay-1
(make-overlay from to
)))
212 ;; Always set the overlay face, since it varies.
213 (overlay-put show-paren-overlay-1
'priority show-paren-priority
)
214 (overlay-put show-paren-overlay-1
'face face
)))
216 ;; Turn on highlighting for the matching paren, if found.
217 ;; If it's an unmatched paren, turn off any such highlighting.
218 (unless (integerp pos
)
219 (delete-overlay show-paren-overlay
))
220 (let ((to (if (or (eq show-paren-style
'expression
)
221 (and (eq show-paren-style
'mixed
)
222 (not (pos-visible-in-window-p pos
))))
225 (from (if (or (eq show-paren-style
'expression
)
226 (and (eq show-paren-style
'mixed
)
227 (not (pos-visible-in-window-p pos
))))
231 (forward-point (- dir
))))))
232 (if show-paren-overlay
233 (move-overlay show-paren-overlay from to
(current-buffer))
234 (setq show-paren-overlay
(make-overlay from to
))))
236 ;; Always set the overlay face, since it varies.
237 (overlay-put show-paren-overlay
'priority show-paren-priority
)
238 (overlay-put show-paren-overlay
'face face
)))
239 ;; show-paren-mode is nil in this buffer.
240 (and show-paren-overlay
241 (delete-overlay show-paren-overlay
))
242 (and show-paren-overlay-1
243 (delete-overlay show-paren-overlay-1
))))
250 ;;; paren.el ends here