Add 2012 to FSF copyright years for Emacs files
[bpt/emacs.git] / lisp / paren.el
CommitLineData
55535639 1;;; paren.el --- highlight matching paren
b578f267 2
acaf905b 3;; Copyright (C) 1993, 1996, 2001-2012 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
eb3fa2cf 11;; GNU Emacs is free software: you can redistribute it and/or modify
5e28918b 12;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
13;; the Free Software Foundation, either version 3 of the License, or
14;; (at your option) any later version.
5e28918b
JB
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
eb3fa2cf 22;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
5e28918b
JB
23
24;;; Commentary:
25
ca5b46fc
EZ
26;; Put this into your ~/.emacs:
27
28;; (show-paren-mode t)
29
30;; It will display highlighting on whatever paren matches the one
31;; before or after point.
5e28918b
JB
32
33;;; Code:
34
173f2341
SM
35(defgroup paren-showing nil
36 "Showing (un)matching of parens and expressions."
37 :prefix "show-paren-"
38 :group 'paren-matching)
39
eb0d9f08 40;; This is the overlay used to highlight the matching paren.
d1475aa1 41(defvar show-paren-overlay nil)
673cc3c5 42;; This is the overlay used to highlight the closeparen right before point.
eb0d9f08
RS
43(defvar show-paren-overlay-1 nil)
44
173f2341 45(defcustom show-paren-style 'parenthesis
9201cc28 46 "Style used when showing a matching paren.
173f2341
SM
47Valid styles are `parenthesis' (meaning show the matching paren),
48`expression' (meaning show the entire expression enclosed by the paren) and
49`mixed' (meaning show the matching paren if it is visible, and the expression
50otherwise)."
51 :type '(choice (const parenthesis) (const expression) (const mixed))
52 :group 'paren-showing)
53
9a0dd02d 54(defcustom show-paren-delay 0.125
9201cc28 55 "Time in seconds to delay before showing a matching paren."
173f2341
SM
56 :type '(number :tag "seconds")
57 :group 'paren-showing)
58
5780586e 59(defcustom show-paren-priority 1000
9201cc28 60 "Priority of paren highlighting overlays."
5780586e
GM
61 :type 'integer
62 :group 'paren-showing
63 :version "21.1")
f1180544 64
2fe66dda 65(defcustom show-paren-ring-bell-on-mismatch nil
9201cc28 66 "If non-nil, beep if mismatched paren is detected."
2fe66dda
RS
67 :type 'boolean
68 :group 'paren-showing
69 :version "20.3")
f1180544 70
3d3d2b8a
JL
71(defgroup paren-showing-faces nil
72 "Group for faces of Show Paren mode."
d6f0f3e0
RS
73 :group 'paren-showing
74 :group 'faces
75 :version "22.1")
76
2ec46551 77(defface show-paren-match
05e122fe
MB
78 '((((class color) (background light))
79 :background "turquoise") ; looks OK on tty (becomes cyan)
80 (((class color) (background dark))
81 :background "steelblue3") ; looks OK on tty (becomes blue)
82 (((background dark))
83 :background "grey50")
84 (t
85 :background "gray"))
173f2341 86 "Show Paren mode face used for a matching paren."
3d3d2b8a 87 :group 'paren-showing-faces)
c4f6e489 88(define-obsolete-face-alias 'show-paren-match-face 'show-paren-match "22.1")
173f2341 89
2ec46551 90(defface show-paren-mismatch
173f2341 91 '((((class color)) (:foreground "white" :background "purple"))
2e8f6012 92 (t (:inverse-video t)))
173f2341 93 "Show Paren mode face used for a mismatching paren."
3d3d2b8a 94 :group 'paren-showing-faces)
c4f6e489
GM
95(define-obsolete-face-alias 'show-paren-mismatch-face
96 'show-paren-mismatch "22.1")
30322a27 97
6afd5d68
JL
98(defvar show-paren-highlight-openparen t
99 "*Non-nil turns on openparen highlighting when matching forward.")
100
12eb8433
RS
101(defvar show-paren-idle-timer nil)
102
103;;;###autoload
2b497eda 104(define-minor-mode show-paren-mode
06e21633
CY
105 "Toggle visualization of matching parens (Show Paren mode).
106With a prefix argument ARG, enable Show Paren mode if ARG is
107positive, and disable it otherwise. If called from Lisp, enable
108the mode if ARG is omitted or nil.
109
110Show Paren mode is a global minor mode. When enabled, any
111matching parenthesis is highlighted in `show-paren-style' after
112`show-paren-delay' seconds of Emacs idle time."
83bfc0d9 113 :global t :group 'paren-showing
965cd29a 114 ;; Enable or disable the mechanism.
050d5fc2
KH
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)))
2b497eda 128 (unless show-paren-mode
050d5fc2
KH
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))
2b497eda 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 139 (if show-paren-mode
056e44ef
JL
140 (let* ((oldpos (point))
141 (dir (cond ((eq (syntax-class (syntax-after (1- (point)))) 5) -1)
142 ((eq (syntax-class (syntax-after (point))) 4) 1)))
143 (unescaped
144 (when dir
145 ;; Verify an even number of quoting characters precede the paren.
146 ;; Follow the same logic as in `blink-matching-open'.
147 (= (if (= dir -1) 1 0)
148 (logand 1 (- (point)
149 (save-excursion
150 (if (= dir -1) (forward-char -1))
151 (skip-syntax-backward "/\\")
152 (point)))))))
153 pos mismatch face)
173f2341 154 ;;
050d5fc2 155 ;; Find the other end of the sexp.
056e44ef 156 (when unescaped
050d5fc2
KH
157 (save-excursion
158 (save-restriction
159 ;; Determine the range within which to look for a match.
160 (when blink-matching-paren-distance
161 (narrow-to-region
162 (max (point-min) (- (point) blink-matching-paren-distance))
163 (min (point-max) (+ (point) blink-matching-paren-distance))))
164 ;; Scan across one sexp within that range.
165 ;; Errors or nil mean there is a mismatch.
166 (condition-case ()
167 (setq pos (scan-sexps (point) dir))
168 (error (setq pos t mismatch t)))
6be7b127
RS
169 ;; Move back the other way and verify we get back to the
170 ;; starting point. If not, these two parens don't really match.
171 ;; Maybe the one at point is escaped and doesn't really count.
172 (when (integerp pos)
173 (unless (condition-case ()
174 (eq (point) (scan-sexps pos (- dir)))
175 (error nil))
176 (setq pos nil)))
050d5fc2
KH
177 ;; If found a "matching" paren, see if it is the right
178 ;; kind of paren to match the one we started at.
179 (when (integerp pos)
180 (let ((beg (min pos oldpos)) (end (max pos oldpos)))
95ed7b42 181 (unless (eq (syntax-class (syntax-after beg)) 8)
050d5fc2 182 (setq mismatch
96bda29d
SM
183 (not (or (eq (char-before end)
184 ;; This can give nil.
185 (cdr (syntax-after beg)))
186 (eq (char-after beg)
187 ;; This can give nil.
1793ac75
SM
188 (cdr (syntax-after (1- end))))
189 ;; The cdr might hold a new paren-class
190 ;; info rather than a matching-char info,
191 ;; in which case the two CDRs should match.
192 (eq (cdr (syntax-after (1- end)))
193 (cdr (syntax-after beg))))))))))))
173f2341 194 ;;
050d5fc2
KH
195 ;; Highlight the other end of the sexp, or unhighlight if none.
196 (if (not pos)
197 (progn
198 ;; If not at a paren that has a match,
199 ;; turn off any previous paren highlighting.
200 (and show-paren-overlay (overlay-buffer show-paren-overlay)
201 (delete-overlay show-paren-overlay))
202 (and show-paren-overlay-1 (overlay-buffer show-paren-overlay-1)
203 (delete-overlay show-paren-overlay-1)))
204 ;;
205 ;; Use the correct face.
206 (if mismatch
207 (progn
208 (if show-paren-ring-bell-on-mismatch
209 (beep))
2ec46551
MB
210 (setq face 'show-paren-mismatch))
211 (setq face 'show-paren-match))
050d5fc2
KH
212 ;;
213 ;; If matching backwards, highlight the closeparen
214 ;; before point as well as its matching open.
215 ;; If matching forward, and the openparen is unbalanced,
216 ;; highlight the paren at point to indicate misbalance.
217 ;; Otherwise, turn off any such highlighting.
6afd5d68 218 (if (and (not show-paren-highlight-openparen) (= dir 1) (integerp pos))
050d5fc2
KH
219 (when (and show-paren-overlay-1
220 (overlay-buffer show-paren-overlay-1))
221 (delete-overlay show-paren-overlay-1))
222 (let ((from (if (= dir 1)
223 (point)
673e5169 224 (- (point) 1)))
050d5fc2 225 (to (if (= dir 1)
673e5169 226 (+ (point) 1)
050d5fc2
KH
227 (point))))
228 (if show-paren-overlay-1
229 (move-overlay show-paren-overlay-1 from to (current-buffer))
f9914209 230 (setq show-paren-overlay-1 (make-overlay from to nil t)))
050d5fc2 231 ;; Always set the overlay face, since it varies.
5780586e 232 (overlay-put show-paren-overlay-1 'priority show-paren-priority)
050d5fc2
KH
233 (overlay-put show-paren-overlay-1 'face face)))
234 ;;
235 ;; Turn on highlighting for the matching paren, if found.
236 ;; If it's an unmatched paren, turn off any such highlighting.
237 (unless (integerp pos)
238 (delete-overlay show-paren-overlay))
239 (let ((to (if (or (eq show-paren-style 'expression)
173f2341
SM
240 (and (eq show-paren-style 'mixed)
241 (not (pos-visible-in-window-p pos))))
050d5fc2
KH
242 (point)
243 pos))
244 (from (if (or (eq show-paren-style 'expression)
245 (and (eq show-paren-style 'mixed)
246 (not (pos-visible-in-window-p pos))))
247 pos
248 (save-excursion
249 (goto-char pos)
673e5169 250 (- (point) dir)))))
050d5fc2
KH
251 (if show-paren-overlay
252 (move-overlay show-paren-overlay from to (current-buffer))
f9914209 253 (setq show-paren-overlay (make-overlay from to nil t))))
050d5fc2
KH
254 ;;
255 ;; Always set the overlay face, since it varies.
5780586e 256 (overlay-put show-paren-overlay 'priority show-paren-priority)
f7e56094 257 (overlay-put show-paren-overlay 'face face)))
050d5fc2
KH
258 ;; show-paren-mode is nil in this buffer.
259 (and show-paren-overlay
260 (delete-overlay show-paren-overlay))
261 (and show-paren-overlay-1
262 (delete-overlay show-paren-overlay-1))))
5e28918b 263
3a3236d2
JB
264(provide 'paren)
265
266;;; paren.el ends here