Update the Commentary with installation instructions.
[bpt/emacs.git] / lisp / paren.el
CommitLineData
55535639 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
ca5b46fc
EZ
28;; Put this into your ~/.emacs:
29
30;; (show-paren-mode t)
31
32;; It will display highlighting on whatever paren matches the one
33;; before or after point.
5e28918b
JB
34
35;;; Code:
36
173f2341
SM
37(defgroup paren-showing nil
38 "Showing (un)matching of parens and expressions."
39 :prefix "show-paren-"
40 :group 'paren-matching)
41
eb0d9f08 42;; This is the overlay used to highlight the matching paren.
d1475aa1 43(defvar show-paren-overlay nil)
673cc3c5 44;; This is the overlay used to highlight the closeparen right before point.
eb0d9f08
RS
45(defvar show-paren-overlay-1 nil)
46
173f2341
SM
47(defcustom show-paren-style 'parenthesis
48 "*Style used when showing a matching paren.
49Valid styles are `parenthesis' (meaning show the matching paren),
50`expression' (meaning show the entire expression enclosed by the paren) and
51`mixed' (meaning show the matching paren if it is visible, and the expression
52otherwise)."
53 :type '(choice (const parenthesis) (const expression) (const mixed))
54 :group 'paren-showing)
55
56(defcustom show-paren-delay
57 (if (featurep 'lisp-float-type) (/ (float 1) (float 8)) 1)
58 "*Time in seconds to delay before showing a matching paren."
59 :type '(number :tag "seconds")
60 :group 'paren-showing)
61
5780586e
GM
62(defcustom show-paren-priority 1000
63 "*Priority of paren highlighting overlays."
64 :type 'integer
65 :group 'paren-showing
66 :version "21.1")
67
2fe66dda
RS
68(defcustom show-paren-ring-bell-on-mismatch nil
69 "*If non-nil, beep if mismatched paren is detected."
70 :type 'boolean
71 :group 'paren-showing
72 :version "20.3")
73
173f2341
SM
74(defface show-paren-match-face
75 '((((class color)) (:background "turquoise"))
82b6a81f 76 (t (:background "gray")))
173f2341
SM
77 "Show Paren mode face used for a matching paren."
78 :group 'faces
79 :group 'paren-showing)
80
81(defface show-paren-mismatch-face
82 '((((class color)) (:foreground "white" :background "purple"))
83 (t (:reverse-video t)))
84 "Show Paren mode face used for a mismatching paren."
85 :group 'faces
86 :group 'paren-showing)
30322a27 87
12eb8433
RS
88(defvar show-paren-idle-timer nil)
89
90;;;###autoload
2b497eda 91(define-minor-mode show-paren-mode
12eb8433
RS
92 "Toggle Show Paren mode.
93With prefix ARG, turn Show Paren mode on if and only if ARG is positive.
94Returns the new status of Show Paren mode (non-nil means on).
95
96When Show Paren mode is enabled, any matching parenthesis is highlighted
97in `show-paren-style' after `show-paren-delay' seconds of Emacs idle time."
83bfc0d9 98 :global t :group 'paren-showing
050d5fc2
KH
99 ;; Turn off the usual paren-matching method
100 ;; when this one is turned on.
101 (if (local-variable-p 'show-paren-mode)
102 (make-local-variable 'blink-matching-paren-on-screen)
103 (kill-local-variable 'blink-matching-paren-on-screen))
2b497eda 104 (setq blink-matching-paren-on-screen (not show-paren-mode))
050d5fc2
KH
105
106 ;; Now enable or disable the mechanism.
107 ;; First get rid of the old idle timer.
108 (if show-paren-idle-timer
12eb8433 109 (cancel-timer show-paren-idle-timer))
050d5fc2
KH
110 (setq show-paren-idle-timer nil)
111 ;; If show-paren-mode is enabled in some buffer now,
112 ;; set up a new timer.
113 (when (memq t (mapcar (lambda (buffer)
114 (with-current-buffer buffer
115 show-paren-mode))
116 (buffer-list)))
117 (setq show-paren-idle-timer (run-with-idle-timer
118 show-paren-delay t
119 'show-paren-function)))
2b497eda 120 (unless show-paren-mode
050d5fc2
KH
121 (and show-paren-overlay
122 (eq (overlay-buffer show-paren-overlay) (current-buffer))
123 (delete-overlay show-paren-overlay))
124 (and show-paren-overlay-1
125 (eq (overlay-buffer show-paren-overlay-1) (current-buffer))
2b497eda 126 (delete-overlay show-paren-overlay-1))))
12eb8433 127
d1475aa1
JB
128;; Find the place to show, if there is one,
129;; and show it until input arrives.
673cc3c5 130(defun show-paren-function ()
050d5fc2
KH
131 (if show-paren-mode
132 (let (pos dir mismatch face (oldpos (point)))
133 (cond ((eq (char-syntax (preceding-char)) ?\))
134 (setq dir -1))
135 ((eq (char-syntax (following-char)) ?\()
136 (setq dir 1)))
173f2341 137 ;;
050d5fc2
KH
138 ;; Find the other end of the sexp.
139 (when dir
140 (save-excursion
141 (save-restriction
142 ;; Determine the range within which to look for a match.
143 (when blink-matching-paren-distance
144 (narrow-to-region
145 (max (point-min) (- (point) blink-matching-paren-distance))
146 (min (point-max) (+ (point) blink-matching-paren-distance))))
147 ;; Scan across one sexp within that range.
148 ;; Errors or nil mean there is a mismatch.
149 (condition-case ()
150 (setq pos (scan-sexps (point) dir))
151 (error (setq pos t mismatch t)))
152 ;; If found a "matching" paren, see if it is the right
153 ;; kind of paren to match the one we started at.
154 (when (integerp pos)
155 (let ((beg (min pos oldpos)) (end (max pos oldpos)))
156 (when (/= (char-syntax (char-after beg)) ?\$)
157 (setq mismatch
158 (not (eq (char-before end)
159 ;; This can give nil.
160 (matching-paren (char-after beg)))))))))))
173f2341 161 ;;
050d5fc2
KH
162 ;; Highlight the other end of the sexp, or unhighlight if none.
163 (if (not pos)
164 (progn
165 ;; If not at a paren that has a match,
166 ;; turn off any previous paren highlighting.
167 (and show-paren-overlay (overlay-buffer show-paren-overlay)
168 (delete-overlay show-paren-overlay))
169 (and show-paren-overlay-1 (overlay-buffer show-paren-overlay-1)
170 (delete-overlay show-paren-overlay-1)))
171 ;;
172 ;; Use the correct face.
173 (if mismatch
174 (progn
175 (if show-paren-ring-bell-on-mismatch
176 (beep))
177 (setq face 'show-paren-mismatch-face))
178 (setq face 'show-paren-match-face))
179 ;;
180 ;; If matching backwards, highlight the closeparen
181 ;; before point as well as its matching open.
182 ;; If matching forward, and the openparen is unbalanced,
183 ;; highlight the paren at point to indicate misbalance.
184 ;; Otherwise, turn off any such highlighting.
185 (if (and (= dir 1) (integerp pos))
186 (when (and show-paren-overlay-1
187 (overlay-buffer show-paren-overlay-1))
188 (delete-overlay show-paren-overlay-1))
189 (let ((from (if (= dir 1)
190 (point)
191 (forward-point -1)))
192 (to (if (= dir 1)
193 (forward-point 1)
194 (point))))
195 (if show-paren-overlay-1
196 (move-overlay show-paren-overlay-1 from to (current-buffer))
197 (setq show-paren-overlay-1 (make-overlay from to)))
198 ;; Always set the overlay face, since it varies.
5780586e 199 (overlay-put show-paren-overlay-1 'priority show-paren-priority)
050d5fc2
KH
200 (overlay-put show-paren-overlay-1 'face face)))
201 ;;
202 ;; Turn on highlighting for the matching paren, if found.
203 ;; If it's an unmatched paren, turn off any such highlighting.
204 (unless (integerp pos)
205 (delete-overlay show-paren-overlay))
206 (let ((to (if (or (eq show-paren-style 'expression)
173f2341
SM
207 (and (eq show-paren-style 'mixed)
208 (not (pos-visible-in-window-p pos))))
050d5fc2
KH
209 (point)
210 pos))
211 (from (if (or (eq show-paren-style 'expression)
212 (and (eq show-paren-style 'mixed)
213 (not (pos-visible-in-window-p pos))))
214 pos
215 (save-excursion
216 (goto-char pos)
217 (forward-point (- dir))))))
218 (if show-paren-overlay
219 (move-overlay show-paren-overlay from to (current-buffer))
220 (setq show-paren-overlay (make-overlay from to))))
221 ;;
222 ;; Always set the overlay face, since it varies.
5780586e 223 (overlay-put show-paren-overlay 'priority show-paren-priority)
050d5fc2
KH
224 (overlay-put show-paren-overlay 'face face)))
225 ;; show-paren-mode is nil in this buffer.
226 (and show-paren-overlay
227 (delete-overlay show-paren-overlay))
228 (and show-paren-overlay-1
229 (delete-overlay show-paren-overlay-1))))
5e28918b 230
3a3236d2
JB
231(provide 'paren)
232
233;;; paren.el ends here