* lisp/progmodes/gdb-mi.el (gdb-breakpoints-list-handler-custom):
[emacs.git] / lisp / paren.el
blob8bd96b9317ad7e05d649357d08b9bf09d701c419
1 ;;; paren.el --- highlight matching paren
3 ;; Copyright (C) 1993, 1996, 2001-2011 Free Software Foundation, Inc.
5 ;; Author: rms@gnu.org
6 ;; Maintainer: FSF
7 ;; Keywords: languages, faces
9 ;; This file is part of GNU Emacs.
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 3 of the License, or
14 ;; (at your option) any later version.
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.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; Put this into your ~/.emacs:
28 ;; (show-paren-mode t)
30 ;; It will display highlighting on whatever paren matches the one
31 ;; before or after point.
33 ;;; Code:
35 (defgroup paren-showing nil
36 "Showing (un)matching of parens and expressions."
37 :prefix "show-paren-"
38 :group 'paren-matching)
40 ;; This is the overlay used to highlight the matching paren.
41 (defvar show-paren-overlay nil)
42 ;; This is the overlay used to highlight the closeparen right before point.
43 (defvar show-paren-overlay-1 nil)
45 (defcustom show-paren-style 'parenthesis
46 "Style used when showing a matching paren.
47 Valid 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
50 otherwise)."
51 :type '(choice (const parenthesis) (const expression) (const mixed))
52 :group 'paren-showing)
54 (defcustom show-paren-delay 0.125
55 "Time in seconds to delay before showing a matching paren."
56 :type '(number :tag "seconds")
57 :group 'paren-showing)
59 (defcustom show-paren-priority 1000
60 "Priority of paren highlighting overlays."
61 :type 'integer
62 :group 'paren-showing
63 :version "21.1")
65 (defcustom show-paren-ring-bell-on-mismatch nil
66 "If non-nil, beep if mismatched paren is detected."
67 :type 'boolean
68 :group 'paren-showing
69 :version "20.3")
71 (defgroup paren-showing-faces nil
72 "Group for faces of Show Paren mode."
73 :group 'paren-showing
74 :group 'faces
75 :version "22.1")
77 (defface show-paren-match
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")
85 :background "gray"))
86 "Show Paren mode face used for a matching paren."
87 :group 'paren-showing-faces)
88 (define-obsolete-face-alias 'show-paren-match-face 'show-paren-match "22.1")
90 (defface show-paren-mismatch
91 '((((class color)) (:foreground "white" :background "purple"))
92 (t (:inverse-video t)))
93 "Show Paren mode face used for a mismatching paren."
94 :group 'paren-showing-faces)
95 (define-obsolete-face-alias 'show-paren-mismatch-face
96 'show-paren-mismatch "22.1")
98 (defvar show-paren-highlight-openparen t
99 "*Non-nil turns on openparen highlighting when matching forward.")
101 (defvar show-paren-idle-timer nil)
103 ;;;###autoload
104 (define-minor-mode show-paren-mode
105 "Toggle Show Paren mode.
106 With prefix ARG, turn Show Paren mode on if and only if ARG is positive.
107 Returns the new status of Show Paren mode (non-nil means on).
109 When Show Paren mode is enabled, any matching parenthesis is highlighted
110 in `show-paren-style' after `show-paren-delay' seconds of Emacs idle time."
111 :global t :group 'paren-showing
112 ;; Enable or disable the mechanism.
113 ;; First get rid of the old idle timer.
114 (if show-paren-idle-timer
115 (cancel-timer show-paren-idle-timer))
116 (setq show-paren-idle-timer nil)
117 ;; If show-paren-mode is enabled in some buffer now,
118 ;; set up a new timer.
119 (when (memq t (mapcar (lambda (buffer)
120 (with-current-buffer buffer
121 show-paren-mode))
122 (buffer-list)))
123 (setq show-paren-idle-timer (run-with-idle-timer
124 show-paren-delay t
125 'show-paren-function)))
126 (unless show-paren-mode
127 (and show-paren-overlay
128 (eq (overlay-buffer show-paren-overlay) (current-buffer))
129 (delete-overlay show-paren-overlay))
130 (and show-paren-overlay-1
131 (eq (overlay-buffer show-paren-overlay-1) (current-buffer))
132 (delete-overlay show-paren-overlay-1))))
134 ;; Find the place to show, if there is one,
135 ;; and show it until input arrives.
136 (defun show-paren-function ()
137 (if show-paren-mode
138 (let ((oldpos (point))
139 (dir (cond ((eq (syntax-class (syntax-after (1- (point)))) 5) -1)
140 ((eq (syntax-class (syntax-after (point))) 4) 1)))
141 pos mismatch face)
143 ;; Find the other end of the sexp.
144 (when dir
145 (save-excursion
146 (save-restriction
147 ;; Determine the range within which to look for a match.
148 (when blink-matching-paren-distance
149 (narrow-to-region
150 (max (point-min) (- (point) blink-matching-paren-distance))
151 (min (point-max) (+ (point) blink-matching-paren-distance))))
152 ;; Scan across one sexp within that range.
153 ;; Errors or nil mean there is a mismatch.
154 (condition-case ()
155 (setq pos (scan-sexps (point) dir))
156 (error (setq pos t mismatch t)))
157 ;; Move back the other way and verify we get back to the
158 ;; starting point. If not, these two parens don't really match.
159 ;; Maybe the one at point is escaped and doesn't really count.
160 (when (integerp pos)
161 (unless (condition-case ()
162 (eq (point) (scan-sexps pos (- dir)))
163 (error nil))
164 (setq pos nil)))
165 ;; If found a "matching" paren, see if it is the right
166 ;; kind of paren to match the one we started at.
167 (when (integerp pos)
168 (let ((beg (min pos oldpos)) (end (max pos oldpos)))
169 (unless (eq (syntax-class (syntax-after beg)) 8)
170 (setq mismatch
171 (not (or (eq (char-before end)
172 ;; This can give nil.
173 (cdr (syntax-after beg)))
174 (eq (char-after beg)
175 ;; This can give nil.
176 (cdr (syntax-after (1- end))))
177 ;; The cdr might hold a new paren-class
178 ;; info rather than a matching-char info,
179 ;; in which case the two CDRs should match.
180 (eq (cdr (syntax-after (1- end)))
181 (cdr (syntax-after beg))))))))))))
183 ;; Highlight the other end of the sexp, or unhighlight if none.
184 (if (not pos)
185 (progn
186 ;; If not at a paren that has a match,
187 ;; turn off any previous paren highlighting.
188 (and show-paren-overlay (overlay-buffer show-paren-overlay)
189 (delete-overlay show-paren-overlay))
190 (and show-paren-overlay-1 (overlay-buffer show-paren-overlay-1)
191 (delete-overlay show-paren-overlay-1)))
193 ;; Use the correct face.
194 (if mismatch
195 (progn
196 (if show-paren-ring-bell-on-mismatch
197 (beep))
198 (setq face 'show-paren-mismatch))
199 (setq face 'show-paren-match))
201 ;; If matching backwards, highlight the closeparen
202 ;; before point as well as its matching open.
203 ;; If matching forward, and the openparen is unbalanced,
204 ;; highlight the paren at point to indicate misbalance.
205 ;; Otherwise, turn off any such highlighting.
206 (if (and (not show-paren-highlight-openparen) (= dir 1) (integerp pos))
207 (when (and show-paren-overlay-1
208 (overlay-buffer show-paren-overlay-1))
209 (delete-overlay show-paren-overlay-1))
210 (let ((from (if (= dir 1)
211 (point)
212 (- (point) 1)))
213 (to (if (= dir 1)
214 (+ (point) 1)
215 (point))))
216 (if show-paren-overlay-1
217 (move-overlay show-paren-overlay-1 from to (current-buffer))
218 (setq show-paren-overlay-1 (make-overlay from to nil t)))
219 ;; Always set the overlay face, since it varies.
220 (overlay-put show-paren-overlay-1 'priority show-paren-priority)
221 (overlay-put show-paren-overlay-1 'face face)))
223 ;; Turn on highlighting for the matching paren, if found.
224 ;; If it's an unmatched paren, turn off any such highlighting.
225 (unless (integerp pos)
226 (delete-overlay show-paren-overlay))
227 (let ((to (if (or (eq show-paren-style 'expression)
228 (and (eq show-paren-style 'mixed)
229 (not (pos-visible-in-window-p pos))))
230 (point)
231 pos))
232 (from (if (or (eq show-paren-style 'expression)
233 (and (eq show-paren-style 'mixed)
234 (not (pos-visible-in-window-p pos))))
236 (save-excursion
237 (goto-char pos)
238 (- (point) dir)))))
239 (if show-paren-overlay
240 (move-overlay show-paren-overlay from to (current-buffer))
241 (setq show-paren-overlay (make-overlay from to nil t))))
243 ;; Always set the overlay face, since it varies.
244 (overlay-put show-paren-overlay 'priority show-paren-priority)
245 (overlay-put show-paren-overlay 'face face)))
246 ;; show-paren-mode is nil in this buffer.
247 (and show-paren-overlay
248 (delete-overlay show-paren-overlay))
249 (and show-paren-overlay-1
250 (delete-overlay show-paren-overlay-1))))
252 (provide 'paren)
254 ;;; paren.el ends here