1 ;;; paren.el --- highlight matching paren.
3 ;; Copyright (C) 1993, 1996 Free Software Foundation, Inc.
5 ;; Author: rms@gnu.ai.mit.edu
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-mode nil
44 "Toggle Show Paren mode.
45 When Show Paren mode is enabled, any matching parenthesis is highlighted
46 after `show-paren-delay' seconds of Emacs idle time.
47 Setting this variable directly does not take effect;
48 use either \\[customize] or the function `show-paren-mode'."
49 :set
(lambda (symbol value
)
50 (show-paren-mode (or value
0)))
51 :initialize
'custom-initialize-default
56 (defcustom show-paren-style
'parenthesis
57 "*Style used when showing a matching paren.
58 Valid styles are `parenthesis' (meaning show the matching paren),
59 `expression' (meaning show the entire expression enclosed by the paren) and
60 `mixed' (meaning show the matching paren if it is visible, and the expression
62 :type
'(choice (const parenthesis
) (const expression
) (const mixed
))
63 :group
'paren-showing
)
65 (defcustom show-paren-delay
66 (if (featurep 'lisp-float-type
) (/ (float 1) (float 8)) 1)
67 "*Time in seconds to delay before showing a matching paren."
68 :type
'(number :tag
"seconds")
69 :group
'paren-showing
)
71 (defcustom show-paren-ring-bell-on-mismatch nil
72 "*If non-nil, beep if mismatched paren is detected."
77 (defface show-paren-match-face
78 '((((class color
)) (:background
"turquoise"))
79 (t (:background
"gray")))
80 "Show Paren mode face used for a matching paren."
82 :group
'paren-showing
)
84 (defface show-paren-mismatch-face
85 '((((class color
)) (:foreground
"white" :background
"purple"))
86 (t (:reverse-video t
)))
87 "Show Paren mode face used for a mismatching paren."
89 :group
'paren-showing
)
91 (defvar show-paren-idle-timer nil
)
94 (defun show-paren-mode (&optional arg
)
95 "Toggle Show Paren mode.
96 With prefix ARG, turn Show Paren mode on if and only if ARG is positive.
97 Returns the new status of Show Paren mode (non-nil means on).
99 When Show Paren mode is enabled, any matching parenthesis is highlighted
100 in `show-paren-style' after `show-paren-delay' seconds of Emacs idle time."
104 (> (prefix-numeric-value arg
) 0)
105 (not show-paren-mode
))))
106 (setq blink-matching-paren-on-screen
(not on-p
))
107 (when show-paren-idle-timer
108 (cancel-timer show-paren-idle-timer
))
110 (setq show-paren-idle-timer
(run-with-idle-timer
112 'show-paren-function
))
113 (and show-paren-overlay
(overlay-buffer show-paren-overlay
)
114 (delete-overlay show-paren-overlay
))
115 (and show-paren-overlay-1
(overlay-buffer show-paren-overlay-1
)
116 (delete-overlay show-paren-overlay-1
)))
117 (setq show-paren-mode on-p
))))
119 ;; Find the place to show, if there is one,
120 ;; and show it until input arrives.
121 (defun show-paren-function ()
122 ;; Do nothing if no window system to display results with.
123 ;; Do nothing if executing keyboard macro.
124 ;; Do nothing if input is pending.
126 (let (pos dir mismatch face
(oldpos (point)))
127 (cond ((eq (char-syntax (preceding-char)) ?\
))
129 ((eq (char-syntax (following-char)) ?\
()
132 ;; Find the other end of the sexp.
136 ;; Determine the range within which to look for a match.
137 (when blink-matching-paren-distance
139 (max (point-min) (- (point) blink-matching-paren-distance
))
140 (min (point-max) (+ (point) blink-matching-paren-distance
))))
141 ;; Scan across one sexp within that range.
142 ;; Errors or nil mean there is a mismatch.
144 (setq pos
(scan-sexps (point) dir
))
145 (error (setq pos t mismatch t
)))
146 ;; If found a "matching" paren, see if it is the right
147 ;; kind of paren to match the one we started at.
149 (let ((beg (min pos oldpos
)) (end (max pos oldpos
)))
150 (when (/= (char-syntax (char-after beg
)) ?\$
)
152 (not (eq (char-before end
)
153 ;; This can give nil.
154 (matching-paren (char-after beg
)))))))))))
156 ;; Highlight the other end of the sexp, or unhighlight if none.
159 ;; If not at a paren that has a match,
160 ;; turn off any previous paren highlighting.
161 (and show-paren-overlay
(overlay-buffer show-paren-overlay
)
162 (delete-overlay show-paren-overlay
))
163 (and show-paren-overlay-1
(overlay-buffer show-paren-overlay-1
)
164 (delete-overlay show-paren-overlay-1
)))
166 ;; Use the correct face.
169 (if show-paren-ring-bell-on-mismatch
171 (setq face
'show-paren-mismatch-face
))
172 (setq face
'show-paren-match-face
))
174 ;; If matching backwards, highlight the closeparen
175 ;; before point as well as its matching open.
176 ;; If matching forward, and the openparen is unbalanced,
177 ;; highlight the paren at point to indicate misbalance.
178 ;; Otherwise, turn off any such highlighting.
179 (if (and (= dir
1) (integerp pos
))
180 (when (and show-paren-overlay-1
181 (overlay-buffer show-paren-overlay-1
))
182 (delete-overlay show-paren-overlay-1
))
183 (let ((from (if (= dir
1)
189 (if show-paren-overlay-1
190 (move-overlay show-paren-overlay-1 from to
(current-buffer))
191 (setq show-paren-overlay-1
(make-overlay from to
)))
192 ;; Always set the overlay face, since it varies.
193 (overlay-put show-paren-overlay-1
'face face
)))
195 ;; Turn on highlighting for the matching paren, if found.
196 ;; If it's an unmatched paren, turn off any such highlighting.
197 (unless (integerp pos
)
198 (delete-overlay show-paren-overlay
))
199 (let ((to (if (or (eq show-paren-style
'expression
)
200 (and (eq show-paren-style
'mixed
)
201 (not (pos-visible-in-window-p pos
))))
204 (from (if (or (eq show-paren-style
'expression
)
205 (and (eq show-paren-style
'mixed
)
206 (not (pos-visible-in-window-p pos
))))
210 (forward-point (- dir
))))))
211 (if show-paren-overlay
212 (move-overlay show-paren-overlay from to
(current-buffer))
213 (setq show-paren-overlay
(make-overlay from to
))))
215 ;; Always set the overlay face, since it varies.
216 (overlay-put show-paren-overlay
'face face
)))))
223 ;;; paren.el ends here