*** empty log message ***
[bpt/emacs.git] / lisp / paren.el
CommitLineData
5e28918b 1;;; paren.el --- highlight matching paren.
b578f267 2
673cc3c5 3;; Copyright (C) 1993, 1996 Free Software Foundation, Inc.
5e28918b 4
5762abec 5;; Author: rms@gnu.org
e4c37df7
JB
6;; Maintainer: FSF
7;; Keywords: languages, faces
5e2325c9 8
5e28918b
JB
9;; This file is part of GNU Emacs.
10
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)
14;; any later version.
15
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.
20
21;; You should have received a copy of the GNU General Public License
b578f267
EN
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.
5e28918b
JB
25
26;;; Commentary:
27
28;; Load this and it will display highlighting on whatever
29;; paren matches the one before or after point.
30
31;;; Code:
32
173f2341
SM
33(defgroup paren-showing nil
34 "Showing (un)matching of parens and expressions."
35 :prefix "show-paren-"
36 :group 'paren-matching)
37
eb0d9f08 38;; This is the overlay used to highlight the matching paren.
d1475aa1 39(defvar show-paren-overlay nil)
673cc3c5 40;; This is the overlay used to highlight the closeparen right before point.
eb0d9f08
RS
41(defvar show-paren-overlay-1 nil)
42
d279baa8 43;;;###autoload
173f2341 44(defcustom show-paren-mode nil
050d5fc2 45 "*Toggle Show Paren mode.
173f2341
SM
46When Show Paren mode is enabled, any matching parenthesis is highlighted
47after `show-paren-delay' seconds of Emacs idle time.
757c19e8
DL
48Setting this variable directly does not take effect;
49use either \\[customize] or the function `show-paren-mode'."
173f2341
SM
50 :set (lambda (symbol value)
51 (show-paren-mode (or value 0)))
12eb8433 52 :initialize 'custom-initialize-default
173f2341
SM
53 :type 'boolean
54 :group 'paren-showing
55 :require 'paren)
56
57(defcustom show-paren-style 'parenthesis
58 "*Style used when showing a matching paren.
59Valid 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
62otherwise)."
63 :type '(choice (const parenthesis) (const expression) (const mixed))
64 :group 'paren-showing)
65
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)
71
2fe66dda
RS
72(defcustom show-paren-ring-bell-on-mismatch nil
73 "*If non-nil, beep if mismatched paren is detected."
74 :type 'boolean
75 :group 'paren-showing
76 :version "20.3")
77
173f2341
SM
78(defface show-paren-match-face
79 '((((class color)) (:background "turquoise"))
82b6a81f 80 (t (:background "gray")))
173f2341
SM
81 "Show Paren mode face used for a matching paren."
82 :group 'faces
83 :group 'paren-showing)
84
85(defface show-paren-mismatch-face
86 '((((class color)) (:foreground "white" :background "purple"))
87 (t (:reverse-video t)))
88 "Show Paren mode face used for a mismatching paren."
89 :group 'faces
90 :group 'paren-showing)
30322a27 91
12eb8433
RS
92(defvar show-paren-idle-timer nil)
93
94;;;###autoload
95(defun show-paren-mode (&optional arg)
96 "Toggle Show Paren mode.
97With prefix ARG, turn Show Paren mode on if and only if ARG is positive.
98Returns the new status of Show Paren mode (non-nil means on).
99
100When Show Paren mode is enabled, any matching parenthesis is highlighted
101in `show-paren-style' after `show-paren-delay' seconds of Emacs idle time."
102 (interactive "P")
050d5fc2
KH
103 (let ((on-p (if arg
104 (> (prefix-numeric-value arg) 0)
105 (not show-paren-mode))))
106 (setq show-paren-mode on-p)
107 ;; Turn off the usual paren-matching method
108 ;; when this one is turned on.
109 (if (local-variable-p 'show-paren-mode)
110 (make-local-variable 'blink-matching-paren-on-screen)
111 (kill-local-variable 'blink-matching-paren-on-screen))
112 (setq blink-matching-paren-on-screen (not on-p))
113
114 ;; Now enable or disable the mechanism.
115 ;; First get rid of the old idle timer.
116 (if show-paren-idle-timer
12eb8433 117 (cancel-timer show-paren-idle-timer))
050d5fc2
KH
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
123 show-paren-mode))
124 (buffer-list)))
125 (setq show-paren-idle-timer (run-with-idle-timer
126 show-paren-delay t
127 'show-paren-function)))
128 (unless on-p
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)))))
12eb8433 135
d1475aa1
JB
136;; Find the place to show, if there is one,
137;; and show it until input arrives.
673cc3c5 138(defun show-paren-function ()
050d5fc2
KH
139 (if show-paren-mode
140 (let (pos dir mismatch face (oldpos (point)))
141 (cond ((eq (char-syntax (preceding-char)) ?\))
142 (setq dir -1))
143 ((eq (char-syntax (following-char)) ?\()
144 (setq dir 1)))
173f2341 145 ;;
050d5fc2
KH
146 ;; Find the other end of the sexp.
147 (when dir
148 (save-excursion
149 (save-restriction
150 ;; Determine the range within which to look for a match.
151 (when blink-matching-paren-distance
152 (narrow-to-region
153 (max (point-min) (- (point) blink-matching-paren-distance))
154 (min (point-max) (+ (point) blink-matching-paren-distance))))
155 ;; Scan across one sexp within that range.
156 ;; Errors or nil mean there is a mismatch.
157 (condition-case ()
158 (setq pos (scan-sexps (point) dir))
159 (error (setq pos t mismatch t)))
160 ;; If found a "matching" paren, see if it is the right
161 ;; kind of paren to match the one we started at.
162 (when (integerp pos)
163 (let ((beg (min pos oldpos)) (end (max pos oldpos)))
164 (when (/= (char-syntax (char-after beg)) ?\$)
165 (setq mismatch
166 (not (eq (char-before end)
167 ;; This can give nil.
168 (matching-paren (char-after beg)))))))))))
173f2341 169 ;;
050d5fc2
KH
170 ;; Highlight the other end of the sexp, or unhighlight if none.
171 (if (not pos)
172 (progn
173 ;; If not at a paren that has a match,
174 ;; turn off any previous paren highlighting.
175 (and show-paren-overlay (overlay-buffer show-paren-overlay)
176 (delete-overlay show-paren-overlay))
177 (and show-paren-overlay-1 (overlay-buffer show-paren-overlay-1)
178 (delete-overlay show-paren-overlay-1)))
179 ;;
180 ;; Use the correct face.
181 (if mismatch
182 (progn
183 (if show-paren-ring-bell-on-mismatch
184 (beep))
185 (setq face 'show-paren-mismatch-face))
186 (setq face 'show-paren-match-face))
187 ;;
188 ;; If matching backwards, highlight the closeparen
189 ;; before point as well as its matching open.
190 ;; If matching forward, and the openparen is unbalanced,
191 ;; highlight the paren at point to indicate misbalance.
192 ;; Otherwise, turn off any such highlighting.
193 (if (and (= dir 1) (integerp pos))
194 (when (and show-paren-overlay-1
195 (overlay-buffer show-paren-overlay-1))
196 (delete-overlay show-paren-overlay-1))
197 (let ((from (if (= dir 1)
198 (point)
199 (forward-point -1)))
200 (to (if (= dir 1)
201 (forward-point 1)
202 (point))))
203 (if show-paren-overlay-1
204 (move-overlay show-paren-overlay-1 from to (current-buffer))
205 (setq show-paren-overlay-1 (make-overlay from to)))
206 ;; Always set the overlay face, since it varies.
207 (overlay-put show-paren-overlay-1 'face face)))
208 ;;
209 ;; Turn on highlighting for the matching paren, if found.
210 ;; If it's an unmatched paren, turn off any such highlighting.
211 (unless (integerp pos)
212 (delete-overlay show-paren-overlay))
213 (let ((to (if (or (eq show-paren-style 'expression)
173f2341
SM
214 (and (eq show-paren-style 'mixed)
215 (not (pos-visible-in-window-p pos))))
050d5fc2
KH
216 (point)
217 pos))
218 (from (if (or (eq show-paren-style 'expression)
219 (and (eq show-paren-style 'mixed)
220 (not (pos-visible-in-window-p pos))))
221 pos
222 (save-excursion
223 (goto-char pos)
224 (forward-point (- dir))))))
225 (if show-paren-overlay
226 (move-overlay show-paren-overlay from to (current-buffer))
227 (setq show-paren-overlay (make-overlay from to))))
228 ;;
229 ;; Always set the overlay face, since it varies.
230 (overlay-put show-paren-overlay 'face face)))
231 ;; show-paren-mode is nil in this buffer.
232 (and show-paren-overlay
233 (delete-overlay show-paren-overlay))
234 (and show-paren-overlay-1
235 (delete-overlay show-paren-overlay-1))))
5e28918b 236
3a3236d2
JB
237(provide 'paren)
238
12eb8433
RS
239(if show-paren-mode
240 (show-paren-mode t))
241
3a3236d2 242;;; paren.el ends here