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
)
43 (defcustom show-paren-style
'parenthesis
44 "*Style used when showing a matching paren.
45 Valid styles are `parenthesis' (meaning show the matching paren),
46 `expression' (meaning show the entire expression enclosed by the paren) and
47 `mixed' (meaning show the matching paren if it is visible, and the expression
49 :type
'(choice (const parenthesis
) (const expression
) (const mixed
))
50 :group
'paren-showing
)
52 (defcustom show-paren-delay
53 (if (featurep 'lisp-float-type
) (/ (float 1) (float 8)) 1)
54 "*Time in seconds to delay before showing a matching paren."
55 :type
'(number :tag
"seconds")
56 :group
'paren-showing
)
58 (defcustom show-paren-priority
1000
59 "*Priority of paren highlighting overlays."
64 (defcustom show-paren-ring-bell-on-mismatch nil
65 "*If non-nil, beep if mismatched paren is detected."
70 (defface show-paren-match-face
71 '((((class color
)) (:background
"turquoise"))
72 (t (:background
"gray")))
73 "Show Paren mode face used for a matching paren."
75 :group
'paren-showing
)
77 (defface show-paren-mismatch-face
78 '((((class color
)) (:foreground
"white" :background
"purple"))
79 (t (:reverse-video t
)))
80 "Show Paren mode face used for a mismatching paren."
82 :group
'paren-showing
)
84 (defvar show-paren-idle-timer nil
)
87 (define-minor-mode show-paren-mode
88 "Toggle Show Paren mode.
89 With prefix ARG, turn Show Paren mode on if and only if ARG is positive.
90 Returns the new status of Show Paren mode (non-nil means on).
92 When Show Paren mode is enabled, any matching parenthesis is highlighted
93 in `show-paren-style' after `show-paren-delay' seconds of Emacs idle time."
94 :global t
:group
'paren-showing
95 ;; Turn off the usual paren-matching method
96 ;; when this one is turned on.
97 (if (local-variable-p 'show-paren-mode
)
98 (make-local-variable 'blink-matching-paren-on-screen
)
99 (kill-local-variable 'blink-matching-paren-on-screen
))
100 (setq blink-matching-paren-on-screen
(not show-paren-mode
))
102 ;; Now enable or disable the mechanism.
103 ;; First get rid of the old idle timer.
104 (if show-paren-idle-timer
105 (cancel-timer show-paren-idle-timer
))
106 (setq show-paren-idle-timer nil
)
107 ;; If show-paren-mode is enabled in some buffer now,
108 ;; set up a new timer.
109 (when (memq t
(mapcar (lambda (buffer)
110 (with-current-buffer buffer
113 (setq show-paren-idle-timer
(run-with-idle-timer
115 'show-paren-function
)))
116 (unless show-paren-mode
117 (and show-paren-overlay
118 (eq (overlay-buffer show-paren-overlay
) (current-buffer))
119 (delete-overlay show-paren-overlay
))
120 (and show-paren-overlay-1
121 (eq (overlay-buffer show-paren-overlay-1
) (current-buffer))
122 (delete-overlay show-paren-overlay-1
))))
124 ;; Find the place to show, if there is one,
125 ;; and show it until input arrives.
126 (defun show-paren-function ()
128 (let (pos dir mismatch face
(oldpos (point)))
129 (cond ((eq (char-syntax (preceding-char)) ?\
))
131 ((eq (char-syntax (following-char)) ?\
()
134 ;; Find the other end of the sexp.
138 ;; Determine the range within which to look for a match.
139 (when blink-matching-paren-distance
141 (max (point-min) (- (point) blink-matching-paren-distance
))
142 (min (point-max) (+ (point) blink-matching-paren-distance
))))
143 ;; Scan across one sexp within that range.
144 ;; Errors or nil mean there is a mismatch.
146 (setq pos
(scan-sexps (point) dir
))
147 (error (setq pos t mismatch t
)))
148 ;; If found a "matching" paren, see if it is the right
149 ;; kind of paren to match the one we started at.
151 (let ((beg (min pos oldpos
)) (end (max pos oldpos
)))
152 (when (/= (char-syntax (char-after beg
)) ?\$
)
154 (not (eq (char-before end
)
155 ;; This can give nil.
156 (matching-paren (char-after beg
)))))))))))
158 ;; Highlight the other end of the sexp, or unhighlight if none.
161 ;; If not at a paren that has a match,
162 ;; turn off any previous paren highlighting.
163 (and show-paren-overlay
(overlay-buffer show-paren-overlay
)
164 (delete-overlay show-paren-overlay
))
165 (and show-paren-overlay-1
(overlay-buffer show-paren-overlay-1
)
166 (delete-overlay show-paren-overlay-1
)))
168 ;; Use the correct face.
171 (if show-paren-ring-bell-on-mismatch
173 (setq face
'show-paren-mismatch-face
))
174 (setq face
'show-paren-match-face
))
176 ;; If matching backwards, highlight the closeparen
177 ;; before point as well as its matching open.
178 ;; If matching forward, and the openparen is unbalanced,
179 ;; highlight the paren at point to indicate misbalance.
180 ;; Otherwise, turn off any such highlighting.
181 (if (and (= dir
1) (integerp pos
))
182 (when (and show-paren-overlay-1
183 (overlay-buffer show-paren-overlay-1
))
184 (delete-overlay show-paren-overlay-1
))
185 (let ((from (if (= dir
1)
191 (if show-paren-overlay-1
192 (move-overlay show-paren-overlay-1 from to
(current-buffer))
193 (setq show-paren-overlay-1
(make-overlay from to
)))
194 ;; Always set the overlay face, since it varies.
195 (overlay-put show-paren-overlay-1
'priority show-paren-priority
)
196 (overlay-put show-paren-overlay-1
'face face
)))
198 ;; Turn on highlighting for the matching paren, if found.
199 ;; If it's an unmatched paren, turn off any such highlighting.
200 (unless (integerp pos
)
201 (delete-overlay show-paren-overlay
))
202 (let ((to (if (or (eq show-paren-style
'expression
)
203 (and (eq show-paren-style
'mixed
)
204 (not (pos-visible-in-window-p pos
))))
207 (from (if (or (eq show-paren-style
'expression
)
208 (and (eq show-paren-style
'mixed
)
209 (not (pos-visible-in-window-p pos
))))
213 (forward-point (- dir
))))))
214 (if show-paren-overlay
215 (move-overlay show-paren-overlay from to
(current-buffer))
216 (setq show-paren-overlay
(make-overlay from to
))))
218 ;; Always set the overlay face, since it varies.
219 (overlay-put show-paren-overlay
'priority show-paren-priority
)
220 (overlay-put show-paren-overlay
'face face
)))
221 ;; show-paren-mode is nil in this buffer.
222 (and show-paren-overlay
223 (delete-overlay show-paren-overlay
))
224 (and show-paren-overlay-1
225 (delete-overlay show-paren-overlay-1
))))
229 ;;; paren.el ends here