vc-hooks.el workaround for bug#11490
[emacs.git] / lisp / paren.el
blobab856380d3f6dcf8d6c0c8ba4a59857b4614991a
1 ;;; paren.el --- highlight matching paren
3 ;; Copyright (C) 1993, 1996, 2001-2012 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 If you change this without using customize while `show-paren-mode' is
57 active, you must toggle the mode off and on again for this to take effect."
58 :type '(number :tag "seconds")
59 :initialize 'custom-initialize-default
60 :set (lambda (sym val)
61 (if (not show-paren-mode)
62 (set sym val)
63 (show-paren-mode -1)
64 (set sym val)
65 (show-paren-mode 1)))
66 :group 'paren-showing)
68 (defcustom show-paren-priority 1000
69 "Priority of paren highlighting overlays."
70 :type 'integer
71 :group 'paren-showing
72 :version "21.1")
74 (defcustom show-paren-ring-bell-on-mismatch nil
75 "If non-nil, beep if mismatched paren is detected."
76 :type 'boolean
77 :group 'paren-showing
78 :version "20.3")
80 (defgroup paren-showing-faces nil
81 "Group for faces of Show Paren mode."
82 :group 'paren-showing
83 :group 'faces
84 :version "22.1")
86 (defface show-paren-match
87 '((((class color) (background light))
88 :background "turquoise") ; looks OK on tty (becomes cyan)
89 (((class color) (background dark))
90 :background "steelblue3") ; looks OK on tty (becomes blue)
91 (((background dark))
92 :background "grey50")
94 :background "gray"))
95 "Show Paren mode face used for a matching paren."
96 :group 'paren-showing-faces)
97 (define-obsolete-face-alias 'show-paren-match-face 'show-paren-match "22.1")
99 (defface show-paren-mismatch
100 '((((class color)) (:foreground "white" :background "purple"))
101 (t (:inverse-video t)))
102 "Show Paren mode face used for a mismatching paren."
103 :group 'paren-showing-faces)
104 (define-obsolete-face-alias 'show-paren-mismatch-face
105 'show-paren-mismatch "22.1")
107 (defvar show-paren-highlight-openparen t
108 "Non-nil turns on openparen highlighting when matching forward.")
110 (defvar show-paren-idle-timer nil)
112 ;;;###autoload
113 (define-minor-mode show-paren-mode
114 "Toggle visualization of matching parens (Show Paren mode).
115 With a prefix argument ARG, enable Show Paren mode if ARG is
116 positive, and disable it otherwise. If called from Lisp, enable
117 the mode if ARG is omitted or nil.
119 Show Paren mode is a global minor mode. When enabled, any
120 matching parenthesis is highlighted in `show-paren-style' after
121 `show-paren-delay' seconds of Emacs idle time."
122 :global t :group 'paren-showing
123 ;; Enable or disable the mechanism.
124 ;; First get rid of the old idle timer.
125 (if show-paren-idle-timer
126 (cancel-timer show-paren-idle-timer))
127 (setq show-paren-idle-timer nil)
128 ;; If show-paren-mode is enabled in some buffer now,
129 ;; set up a new timer.
130 (when (memq t (mapcar (lambda (buffer)
131 (with-current-buffer buffer
132 show-paren-mode))
133 (buffer-list)))
134 (setq show-paren-idle-timer (run-with-idle-timer
135 show-paren-delay t
136 'show-paren-function)))
137 (unless show-paren-mode
138 (and show-paren-overlay
139 (eq (overlay-buffer show-paren-overlay) (current-buffer))
140 (delete-overlay show-paren-overlay))
141 (and show-paren-overlay-1
142 (eq (overlay-buffer show-paren-overlay-1) (current-buffer))
143 (delete-overlay show-paren-overlay-1))))
145 ;; Find the place to show, if there is one,
146 ;; and show it until input arrives.
147 (defun show-paren-function ()
148 (if show-paren-mode
149 (let* ((oldpos (point))
150 (dir (cond ((eq (syntax-class (syntax-after (1- (point)))) 5) -1)
151 ((eq (syntax-class (syntax-after (point))) 4) 1)))
152 (unescaped
153 (when dir
154 ;; Verify an even number of quoting characters precede the paren.
155 ;; Follow the same logic as in `blink-matching-open'.
156 (= (if (= dir -1) 1 0)
157 (logand 1 (- (point)
158 (save-excursion
159 (if (= dir -1) (forward-char -1))
160 (skip-syntax-backward "/\\")
161 (point)))))))
162 pos mismatch face)
164 ;; Find the other end of the sexp.
165 (when unescaped
166 (save-excursion
167 (save-restriction
168 ;; Determine the range within which to look for a match.
169 (when blink-matching-paren-distance
170 (narrow-to-region
171 (max (point-min) (- (point) blink-matching-paren-distance))
172 (min (point-max) (+ (point) blink-matching-paren-distance))))
173 ;; Scan across one sexp within that range.
174 ;; Errors or nil mean there is a mismatch.
175 (condition-case ()
176 (setq pos (scan-sexps (point) dir))
177 (error (setq pos t mismatch t)))
178 ;; Move back the other way and verify we get back to the
179 ;; starting point. If not, these two parens don't really match.
180 ;; Maybe the one at point is escaped and doesn't really count.
181 (when (integerp pos)
182 (unless (condition-case ()
183 (eq (point) (scan-sexps pos (- dir)))
184 (error nil))
185 (setq pos nil)))
186 ;; If found a "matching" paren, see if it is the right
187 ;; kind of paren to match the one we started at.
188 (when (integerp pos)
189 (let ((beg (min pos oldpos)) (end (max pos oldpos)))
190 (unless (eq (syntax-class (syntax-after beg)) 8)
191 (setq mismatch
192 (not (or (eq (char-before end)
193 ;; This can give nil.
194 (cdr (syntax-after beg)))
195 (eq (char-after beg)
196 ;; This can give nil.
197 (cdr (syntax-after (1- end))))
198 ;; The cdr might hold a new paren-class
199 ;; info rather than a matching-char info,
200 ;; in which case the two CDRs should match.
201 (eq (cdr (syntax-after (1- end)))
202 (cdr (syntax-after beg))))))))))))
204 ;; Highlight the other end of the sexp, or unhighlight if none.
205 (if (not pos)
206 (progn
207 ;; If not at a paren that has a match,
208 ;; turn off any previous paren highlighting.
209 (and show-paren-overlay (overlay-buffer show-paren-overlay)
210 (delete-overlay show-paren-overlay))
211 (and show-paren-overlay-1 (overlay-buffer show-paren-overlay-1)
212 (delete-overlay show-paren-overlay-1)))
214 ;; Use the correct face.
215 (if mismatch
216 (progn
217 (if show-paren-ring-bell-on-mismatch
218 (beep))
219 (setq face 'show-paren-mismatch))
220 (setq face 'show-paren-match))
222 ;; If matching backwards, highlight the closeparen
223 ;; before point as well as its matching open.
224 ;; If matching forward, and the openparen is unbalanced,
225 ;; highlight the paren at point to indicate misbalance.
226 ;; Otherwise, turn off any such highlighting.
227 (if (and (not show-paren-highlight-openparen) (= dir 1) (integerp pos))
228 (when (and show-paren-overlay-1
229 (overlay-buffer show-paren-overlay-1))
230 (delete-overlay show-paren-overlay-1))
231 (let ((from (if (= dir 1)
232 (point)
233 (- (point) 1)))
234 (to (if (= dir 1)
235 (+ (point) 1)
236 (point))))
237 (if show-paren-overlay-1
238 (move-overlay show-paren-overlay-1 from to (current-buffer))
239 (setq show-paren-overlay-1 (make-overlay from to nil t)))
240 ;; Always set the overlay face, since it varies.
241 (overlay-put show-paren-overlay-1 'priority show-paren-priority)
242 (overlay-put show-paren-overlay-1 'face face)))
244 ;; Turn on highlighting for the matching paren, if found.
245 ;; If it's an unmatched paren, turn off any such highlighting.
246 (unless (integerp pos)
247 (delete-overlay show-paren-overlay))
248 (let ((to (if (or (eq show-paren-style 'expression)
249 (and (eq show-paren-style 'mixed)
250 (not (pos-visible-in-window-p pos))))
251 (point)
252 pos))
253 (from (if (or (eq show-paren-style 'expression)
254 (and (eq show-paren-style 'mixed)
255 (not (pos-visible-in-window-p pos))))
257 (save-excursion
258 (goto-char pos)
259 (- (point) dir)))))
260 (if show-paren-overlay
261 (move-overlay show-paren-overlay from to (current-buffer))
262 (setq show-paren-overlay (make-overlay from to nil t))))
264 ;; Always set the overlay face, since it varies.
265 (overlay-put show-paren-overlay 'priority show-paren-priority)
266 (overlay-put show-paren-overlay 'face face)))
267 ;; show-paren-mode is nil in this buffer.
268 (and show-paren-overlay
269 (delete-overlay show-paren-overlay))
270 (and show-paren-overlay-1
271 (delete-overlay show-paren-overlay-1))))
273 (provide 'paren)
275 ;;; paren.el ends here