(define-ibuffer-filter filename): If `dired-directory' is a list then
[bpt/emacs.git] / lisp / paren.el
CommitLineData
55535639 1;;; paren.el --- highlight matching paren
b578f267 2
0d30b337
TTN
3;; Copyright (C) 1993, 1996, 2001, 2002, 2003, 2004,
4;; 2005 Free Software Foundation, Inc.
5e28918b 5
5762abec 6;; Author: rms@gnu.org
e4c37df7
JB
7;; Maintainer: FSF
8;; Keywords: languages, faces
5e2325c9 9
5e28918b
JB
10;; This file is part of GNU Emacs.
11
12;; GNU Emacs is free software; you can redistribute it and/or modify
13;; it under the terms of the GNU General Public License as published by
14;; the Free Software Foundation; either version 2, or (at your option)
15;; any later version.
16
17;; GNU Emacs is distributed in the hope that it will be useful,
18;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20;; GNU General Public License for more details.
21
22;; You should have received a copy of the GNU General Public License
b578f267 23;; along with GNU Emacs; see the file COPYING. If not, write to the
086add15
LK
24;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25;; Boston, MA 02110-1301, USA.
5e28918b
JB
26
27;;; Commentary:
28
ca5b46fc
EZ
29;; Put this into your ~/.emacs:
30
31;; (show-paren-mode t)
32
33;; It will display highlighting on whatever paren matches the one
34;; before or after point.
5e28918b
JB
35
36;;; Code:
37
173f2341
SM
38(defgroup paren-showing nil
39 "Showing (un)matching of parens and expressions."
40 :prefix "show-paren-"
41 :group 'paren-matching)
42
eb0d9f08 43;; This is the overlay used to highlight the matching paren.
d1475aa1 44(defvar show-paren-overlay nil)
673cc3c5 45;; This is the overlay used to highlight the closeparen right before point.
eb0d9f08
RS
46(defvar show-paren-overlay-1 nil)
47
173f2341
SM
48(defcustom show-paren-style 'parenthesis
49 "*Style used when showing a matching paren.
50Valid styles are `parenthesis' (meaning show the matching paren),
51`expression' (meaning show the entire expression enclosed by the paren) and
52`mixed' (meaning show the matching paren if it is visible, and the expression
53otherwise)."
54 :type '(choice (const parenthesis) (const expression) (const mixed))
55 :group 'paren-showing)
56
57(defcustom show-paren-delay
58 (if (featurep 'lisp-float-type) (/ (float 1) (float 8)) 1)
59 "*Time in seconds to delay before showing a matching paren."
60 :type '(number :tag "seconds")
61 :group 'paren-showing)
62
5780586e
GM
63(defcustom show-paren-priority 1000
64 "*Priority of paren highlighting overlays."
65 :type 'integer
66 :group 'paren-showing
67 :version "21.1")
f1180544 68
2fe66dda
RS
69(defcustom show-paren-ring-bell-on-mismatch nil
70 "*If non-nil, beep if mismatched paren is detected."
71 :type 'boolean
72 :group 'paren-showing
73 :version "20.3")
f1180544 74
2ec46551 75(defface show-paren-match
05e122fe
MB
76 '((((class color) (background light))
77 :background "turquoise") ; looks OK on tty (becomes cyan)
78 (((class color) (background dark))
79 :background "steelblue3") ; looks OK on tty (becomes blue)
80 (((background dark))
81 :background "grey50")
82 (t
83 :background "gray"))
173f2341
SM
84 "Show Paren mode face used for a matching paren."
85 :group 'faces
86 :group 'paren-showing)
2ec46551
MB
87;; backward-compatibility alias
88(put 'show-paren-match-face 'face-alias 'show-paren-match)
173f2341 89
2ec46551 90(defface show-paren-mismatch
173f2341 91 '((((class color)) (:foreground "white" :background "purple"))
2e8f6012 92 (t (:inverse-video t)))
173f2341
SM
93 "Show Paren mode face used for a mismatching paren."
94 :group 'faces
95 :group 'paren-showing)
2ec46551
MB
96;; backward-compatibility alias
97(put 'show-paren-mismatch-face 'face-alias 'show-paren-mismatch)
30322a27 98
6afd5d68
JL
99(defvar show-paren-highlight-openparen t
100 "*Non-nil turns on openparen highlighting when matching forward.")
101
12eb8433
RS
102(defvar show-paren-idle-timer nil)
103
104;;;###autoload
2b497eda 105(define-minor-mode show-paren-mode
12eb8433
RS
106 "Toggle Show Paren mode.
107With prefix ARG, turn Show Paren mode on if and only if ARG is positive.
108Returns the new status of Show Paren mode (non-nil means on).
109
110When Show Paren mode is enabled, any matching parenthesis is highlighted
111in `show-paren-style' after `show-paren-delay' seconds of Emacs idle time."
83bfc0d9 112 :global t :group 'paren-showing
965cd29a 113 ;; Enable or disable the mechanism.
050d5fc2
KH
114 ;; First get rid of the old idle timer.
115 (if show-paren-idle-timer
12eb8433 116 (cancel-timer show-paren-idle-timer))
050d5fc2
KH
117 (setq show-paren-idle-timer nil)
118 ;; If show-paren-mode is enabled in some buffer now,
119 ;; set up a new timer.
120 (when (memq t (mapcar (lambda (buffer)
121 (with-current-buffer buffer
122 show-paren-mode))
123 (buffer-list)))
124 (setq show-paren-idle-timer (run-with-idle-timer
125 show-paren-delay t
126 'show-paren-function)))
2b497eda 127 (unless show-paren-mode
050d5fc2
KH
128 (and show-paren-overlay
129 (eq (overlay-buffer show-paren-overlay) (current-buffer))
130 (delete-overlay show-paren-overlay))
131 (and show-paren-overlay-1
132 (eq (overlay-buffer show-paren-overlay-1) (current-buffer))
2b497eda 133 (delete-overlay show-paren-overlay-1))))
12eb8433 134
d1475aa1
JB
135;; Find the place to show, if there is one,
136;; and show it until input arrives.
673cc3c5 137(defun show-paren-function ()
050d5fc2 138 (if show-paren-mode
9fe9d55a 139 (let ((oldpos (point))
95ed7b42
SM
140 (dir (cond ((eq (syntax-class (syntax-after (1- (point)))) 5) -1)
141 ((eq (syntax-class (syntax-after (point))) 4) 1)))
9fe9d55a 142 pos mismatch face)
173f2341 143 ;;
050d5fc2
KH
144 ;; Find the other end of the sexp.
145 (when dir
146 (save-excursion
147 (save-restriction
148 ;; Determine the range within which to look for a match.
149 (when blink-matching-paren-distance
150 (narrow-to-region
151 (max (point-min) (- (point) blink-matching-paren-distance))
152 (min (point-max) (+ (point) blink-matching-paren-distance))))
153 ;; Scan across one sexp within that range.
154 ;; Errors or nil mean there is a mismatch.
155 (condition-case ()
156 (setq pos (scan-sexps (point) dir))
157 (error (setq pos t mismatch t)))
6be7b127
RS
158 ;; Move back the other way and verify we get back to the
159 ;; starting point. If not, these two parens don't really match.
160 ;; Maybe the one at point is escaped and doesn't really count.
161 (when (integerp pos)
162 (unless (condition-case ()
163 (eq (point) (scan-sexps pos (- dir)))
164 (error nil))
165 (setq pos nil)))
050d5fc2
KH
166 ;; If found a "matching" paren, see if it is the right
167 ;; kind of paren to match the one we started at.
168 (when (integerp pos)
169 (let ((beg (min pos oldpos)) (end (max pos oldpos)))
95ed7b42 170 (unless (eq (syntax-class (syntax-after beg)) 8)
050d5fc2 171 (setq mismatch
96bda29d
SM
172 (not (or (eq (char-before end)
173 ;; This can give nil.
174 (cdr (syntax-after beg)))
175 (eq (char-after beg)
176 ;; This can give nil.
1793ac75
SM
177 (cdr (syntax-after (1- end))))
178 ;; The cdr might hold a new paren-class
179 ;; info rather than a matching-char info,
180 ;; in which case the two CDRs should match.
181 (eq (cdr (syntax-after (1- end)))
182 (cdr (syntax-after beg))))))))))))
173f2341 183 ;;
050d5fc2
KH
184 ;; Highlight the other end of the sexp, or unhighlight if none.
185 (if (not pos)
186 (progn
187 ;; If not at a paren that has a match,
188 ;; turn off any previous paren highlighting.
189 (and show-paren-overlay (overlay-buffer show-paren-overlay)
190 (delete-overlay show-paren-overlay))
191 (and show-paren-overlay-1 (overlay-buffer show-paren-overlay-1)
192 (delete-overlay show-paren-overlay-1)))
193 ;;
194 ;; Use the correct face.
195 (if mismatch
196 (progn
197 (if show-paren-ring-bell-on-mismatch
198 (beep))
2ec46551
MB
199 (setq face 'show-paren-mismatch))
200 (setq face 'show-paren-match))
050d5fc2
KH
201 ;;
202 ;; If matching backwards, highlight the closeparen
203 ;; before point as well as its matching open.
204 ;; If matching forward, and the openparen is unbalanced,
205 ;; highlight the paren at point to indicate misbalance.
206 ;; Otherwise, turn off any such highlighting.
6afd5d68 207 (if (and (not show-paren-highlight-openparen) (= dir 1) (integerp pos))
050d5fc2
KH
208 (when (and show-paren-overlay-1
209 (overlay-buffer show-paren-overlay-1))
210 (delete-overlay show-paren-overlay-1))
211 (let ((from (if (= dir 1)
212 (point)
213 (forward-point -1)))
214 (to (if (= dir 1)
215 (forward-point 1)
216 (point))))
217 (if show-paren-overlay-1
218 (move-overlay show-paren-overlay-1 from to (current-buffer))
219 (setq show-paren-overlay-1 (make-overlay from to)))
220 ;; Always set the overlay face, since it varies.
5780586e 221 (overlay-put show-paren-overlay-1 'priority show-paren-priority)
050d5fc2
KH
222 (overlay-put show-paren-overlay-1 'face face)))
223 ;;
224 ;; Turn on highlighting for the matching paren, if found.
225 ;; If it's an unmatched paren, turn off any such highlighting.
226 (unless (integerp pos)
227 (delete-overlay show-paren-overlay))
228 (let ((to (if (or (eq show-paren-style 'expression)
173f2341
SM
229 (and (eq show-paren-style 'mixed)
230 (not (pos-visible-in-window-p pos))))
050d5fc2
KH
231 (point)
232 pos))
233 (from (if (or (eq show-paren-style 'expression)
234 (and (eq show-paren-style 'mixed)
235 (not (pos-visible-in-window-p pos))))
236 pos
237 (save-excursion
238 (goto-char pos)
239 (forward-point (- dir))))))
240 (if show-paren-overlay
241 (move-overlay show-paren-overlay from to (current-buffer))
242 (setq show-paren-overlay (make-overlay from to))))
243 ;;
244 ;; Always set the overlay face, since it varies.
5780586e 245 (overlay-put show-paren-overlay 'priority show-paren-priority)
050d5fc2
KH
246 (overlay-put show-paren-overlay 'face face)))
247 ;; show-paren-mode is nil in this buffer.
248 (and show-paren-overlay
249 (delete-overlay show-paren-overlay))
250 (and show-paren-overlay-1
251 (delete-overlay show-paren-overlay-1))))
5e28918b 252
3a3236d2
JB
253(provide 'paren)
254
d8ac3d27 255;; arch-tag: d0969b88-7ac0-4bd0-bd53-e73b892b86a9
3a3236d2 256;;; paren.el ends here