More conversions to buffer display specifiers
[emacs.git] / lisp / paren.el
blobbdc15a66cc0363ef83b4ca4f63474e6a3518e273
1 ;;; paren.el --- highlight matching paren
3 ;; Copyright (C) 1993, 1996, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
4 ;; 2008, 2009, 2010 Free Software Foundation, Inc.
6 ;; Author: rms@gnu.org
7 ;; Maintainer: FSF
8 ;; Keywords: languages, faces
10 ;; This file is part of GNU Emacs.
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 3 of the License, or
15 ;; (at your option) any later version.
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.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;; Put this into your ~/.emacs:
29 ;; (show-paren-mode t)
31 ;; It will display highlighting on whatever paren matches the one
32 ;; before or after point.
34 ;;; Code:
36 (defgroup paren-showing nil
37 "Showing (un)matching of parens and expressions."
38 :prefix "show-paren-"
39 :group 'paren-matching)
41 ;; This is the overlay used to highlight the matching paren.
42 (defvar show-paren-overlay nil)
43 ;; This is the overlay used to highlight the closeparen right before point.
44 (defvar show-paren-overlay-1 nil)
46 (defcustom show-paren-style 'parenthesis
47 "Style used when showing a matching paren.
48 Valid styles are `parenthesis' (meaning show the matching paren),
49 `expression' (meaning show the entire expression enclosed by the paren) and
50 `mixed' (meaning show the matching paren if it is visible, and the expression
51 otherwise)."
52 :type '(choice (const parenthesis) (const expression) (const mixed))
53 :group 'paren-showing)
55 (defcustom show-paren-delay 0.125
56 "Time in seconds to delay before showing a matching paren."
57 :type '(number :tag "seconds")
58 :group 'paren-showing)
60 (defcustom show-paren-priority 1000
61 "Priority of paren highlighting overlays."
62 :type 'integer
63 :group 'paren-showing
64 :version "21.1")
66 (defcustom show-paren-ring-bell-on-mismatch nil
67 "If non-nil, beep if mismatched paren is detected."
68 :type 'boolean
69 :group 'paren-showing
70 :version "20.3")
72 (defgroup paren-showing-faces nil
73 "Group for faces of Show Paren mode."
74 :group 'paren-showing
75 :group 'faces
76 :version "22.1")
78 (defface show-paren-match
79 '((((class color) (background light))
80 :background "turquoise") ; looks OK on tty (becomes cyan)
81 (((class color) (background dark))
82 :background "steelblue3") ; looks OK on tty (becomes blue)
83 (((background dark))
84 :background "grey50")
86 :background "gray"))
87 "Show Paren mode face used for a matching paren."
88 :group 'paren-showing-faces)
89 (define-obsolete-face-alias 'show-paren-match-face 'show-paren-match "22.1")
91 (defface show-paren-mismatch
92 '((((class color)) (:foreground "white" :background "purple"))
93 (t (:inverse-video t)))
94 "Show Paren mode face used for a mismatching paren."
95 :group 'paren-showing-faces)
96 (define-obsolete-face-alias 'show-paren-mismatch-face
97 'show-paren-mismatch "22.1")
99 (defvar show-paren-highlight-openparen t
100 "*Non-nil turns on openparen highlighting when matching forward.")
102 (defvar show-paren-idle-timer nil)
104 ;;;###autoload
105 (define-minor-mode show-paren-mode
106 "Toggle Show Paren mode.
107 With prefix ARG, turn Show Paren mode on if and only if ARG is positive.
108 Returns the new status of Show Paren mode (non-nil means on).
110 When Show Paren mode is enabled, any matching parenthesis is highlighted
111 in `show-paren-style' after `show-paren-delay' seconds of Emacs idle time."
112 :global t :group 'paren-showing
113 ;; Enable or disable the mechanism.
114 ;; First get rid of the old idle timer.
115 (if show-paren-idle-timer
116 (cancel-timer show-paren-idle-timer))
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)))
127 (unless show-paren-mode
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))
133 (delete-overlay show-paren-overlay-1))))
135 ;; Find the place to show, if there is one,
136 ;; and show it until input arrives.
137 (defun show-paren-function ()
138 (if show-paren-mode
139 (let ((oldpos (point))
140 (dir (cond ((eq (syntax-class (syntax-after (1- (point)))) 5) -1)
141 ((eq (syntax-class (syntax-after (point))) 4) 1)))
142 pos mismatch face)
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)))
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)))
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)))
170 (unless (eq (syntax-class (syntax-after beg)) 8)
171 (setq mismatch
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.
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))))))))))))
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)))
194 ;; Use the correct face.
195 (if mismatch
196 (progn
197 (if show-paren-ring-bell-on-mismatch
198 (beep))
199 (setq face 'show-paren-mismatch))
200 (setq face 'show-paren-match))
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.
207 (if (and (not show-paren-highlight-openparen) (= dir 1) (integerp pos))
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 (- (point) 1)))
214 (to (if (= dir 1)
215 (+ (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 nil t)))
220 ;; Always set the overlay face, since it varies.
221 (overlay-put show-paren-overlay-1 'priority show-paren-priority)
222 (overlay-put show-paren-overlay-1 'face face)))
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)
229 (and (eq show-paren-style 'mixed)
230 (not (pos-visible-in-window-p pos))))
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))))
237 (save-excursion
238 (goto-char pos)
239 (- (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 nil t))))
244 ;; Always set the overlay face, since it varies.
245 (overlay-put show-paren-overlay 'priority show-paren-priority)
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))))
253 (provide 'paren)
255 ;;; paren.el ends here