Merge from emacs-24; up to 2013-01-03T02:31:36Z!rgm@gnu.org
[emacs.git] / lisp / paren.el
blob6f386573b01946e9bce0ea199ade0e86c9039b65
1 ;;; paren.el --- highlight matching paren
3 ;; Copyright (C) 1993, 1996, 2001-2013 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 (defcustom show-paren-style 'parenthesis
41 "Style used when showing a matching paren.
42 Valid styles are `parenthesis' (meaning show the matching paren),
43 `expression' (meaning show the entire expression enclosed by the paren) and
44 `mixed' (meaning show the matching paren if it is visible, and the expression
45 otherwise)."
46 :type '(choice (const parenthesis) (const expression) (const mixed))
47 :group 'paren-showing)
49 (defcustom show-paren-delay 0.125
50 "Time in seconds to delay before showing a matching paren.
51 If you change this without using customize while `show-paren-mode' is
52 active, you must toggle the mode off and on again for this to take effect."
53 :type '(number :tag "seconds")
54 :initialize 'custom-initialize-default
55 :set (lambda (sym val)
56 (if (not show-paren-mode)
57 (set sym val)
58 (show-paren-mode -1)
59 (set sym val)
60 (show-paren-mode 1)))
61 :group 'paren-showing)
63 (defcustom show-paren-priority 1000
64 "Priority of paren highlighting overlays."
65 :type 'integer
66 :group 'paren-showing
67 :version "21.1")
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")
75 (defgroup paren-showing-faces nil
76 "Group for faces of Show Paren mode."
77 :group 'paren-showing
78 :group 'faces
79 :version "22.1")
81 (defface show-paren-match
82 '((((class color) (background light))
83 :background "turquoise") ; looks OK on tty (becomes cyan)
84 (((class color) (background dark))
85 :background "steelblue3") ; looks OK on tty (becomes blue)
86 (((background dark))
87 :background "grey50")
89 :background "gray"))
90 "Show Paren mode face used for a matching paren."
91 :group 'paren-showing-faces)
92 (define-obsolete-face-alias 'show-paren-match-face 'show-paren-match "22.1")
94 (defface show-paren-mismatch
95 '((((class color)) (:foreground "white" :background "purple"))
96 (t (:inverse-video t)))
97 "Show Paren mode face used for a mismatching paren."
98 :group 'paren-showing-faces)
99 (define-obsolete-face-alias 'show-paren-mismatch-face
100 'show-paren-mismatch "22.1")
102 (defvar show-paren-highlight-openparen t
103 "Non-nil turns on openparen highlighting when matching forward.")
105 (defvar show-paren--idle-timer nil)
106 (defvar show-paren--overlay
107 (let ((ol (make-overlay (point) (point) nil t))) (delete-overlay ol) ol)
108 "Overlay used to highlight the matching paren.")
109 (defvar show-paren--overlay-1
110 (let ((ol (make-overlay (point) (point) nil t))) (delete-overlay ol) ol)
111 "Overlay used to highlight the paren at point.")
114 ;;;###autoload
115 (define-minor-mode show-paren-mode
116 "Toggle visualization of matching parens (Show Paren mode).
117 With a prefix argument ARG, enable Show Paren mode if ARG is
118 positive, and disable it otherwise. If called from Lisp, enable
119 the mode if ARG is omitted or nil.
121 Show Paren mode is a global minor mode. When enabled, any
122 matching parenthesis is highlighted in `show-paren-style' after
123 `show-paren-delay' seconds of Emacs idle time."
124 :global t :group 'paren-showing
125 ;; Enable or disable the mechanism.
126 ;; First get rid of the old idle timer.
127 (when show-paren--idle-timer
128 (cancel-timer show-paren--idle-timer)
129 (setq show-paren--idle-timer nil))
130 (setq show-paren--idle-timer (run-with-idle-timer
131 show-paren-delay t
132 #'show-paren-function))
133 (unless show-paren-mode
134 (delete-overlay show-paren--overlay)
135 (delete-overlay show-paren--overlay-1)))
137 (defvar show-paren-data-function #'show-paren--default
138 "Function to find the opener/closer at point and its match.
139 The function is called with no argument and should return either nil
140 if there's no opener/closer at point, or a list of the form
141 \(HERE-BEG HERE-END THERE-BEG THERE-END MISMATCH)
142 Where HERE-BEG..HERE-END is expected to be around point.")
144 (defun show-paren--default ()
145 (let* ((oldpos (point))
146 (dir (cond ((eq (syntax-class (syntax-after (1- (point)))) 5) -1)
147 ((eq (syntax-class (syntax-after (point))) 4) 1)))
148 (unescaped
149 (when dir
150 ;; Verify an even number of quoting characters precede the paren.
151 ;; Follow the same logic as in `blink-matching-open'.
152 (= (if (= dir -1) 1 0)
153 (logand 1 (- (point)
154 (save-excursion
155 (if (= dir -1) (forward-char -1))
156 (skip-syntax-backward "/\\")
157 (point)))))))
158 (here-beg (if (eq dir 1) (point) (1- (point))))
159 (here-end (if (eq dir 1) (1+ (point)) (point)))
160 pos mismatch)
162 ;; Find the other end of the sexp.
163 (when unescaped
164 (save-excursion
165 (save-restriction
166 ;; Determine the range within which to look for a match.
167 (when blink-matching-paren-distance
168 (narrow-to-region
169 (max (point-min) (- (point) blink-matching-paren-distance))
170 (min (point-max) (+ (point) blink-matching-paren-distance))))
171 ;; Scan across one sexp within that range.
172 ;; Errors or nil mean there is a mismatch.
173 (condition-case ()
174 (setq pos (scan-sexps (point) dir))
175 (error (setq pos t mismatch t)))
176 ;; Move back the other way and verify we get back to the
177 ;; starting point. If not, these two parens don't really match.
178 ;; Maybe the one at point is escaped and doesn't really count,
179 ;; or one is inside a comment.
180 (when (integerp pos)
181 (unless (condition-case ()
182 (eq (point) (scan-sexps pos (- dir)))
183 (error nil))
184 (setq pos nil)))
185 ;; If found a "matching" paren, see if it is the right
186 ;; kind of paren to match the one we started at.
187 (if (not (integerp pos))
188 (if mismatch (list here-beg here-end nil nil t))
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)))))))
203 (list here-beg here-end
204 (if (= dir 1) (1- pos) pos)
205 (if (= dir 1) pos (1+ pos))
206 mismatch))))))))
208 ;; Find the place to show, if there is one,
209 ;; and show it until input arrives.
210 (defun show-paren-function ()
211 (let ((data (and show-paren-mode (funcall show-paren-data-function))))
212 (if (not data)
213 (progn
214 ;; If show-paren-mode is nil in this buffer or if not at a paren that
215 ;; has a match, turn off any previous paren highlighting.
216 (delete-overlay show-paren--overlay)
217 (delete-overlay show-paren--overlay-1))
219 ;; Found something to highlight.
220 (let* ((here-beg (nth 0 data))
221 (here-end (nth 1 data))
222 (there-beg (nth 2 data))
223 (there-end (nth 3 data))
224 (mismatch (nth 4 data))
225 (face
226 (if mismatch
227 (progn
228 (if show-paren-ring-bell-on-mismatch
229 (beep))
230 'show-paren-mismatch)
231 'show-paren-match)))
233 ;; If matching backwards, highlight the closeparen
234 ;; before point as well as its matching open.
235 ;; If matching forward, and the openparen is unbalanced,
236 ;; highlight the paren at point to indicate misbalance.
237 ;; Otherwise, turn off any such highlighting.
238 (if (or (not here-beg)
239 (and (not show-paren-highlight-openparen)
240 (> here-end (point))
241 (integerp there-beg)))
242 (delete-overlay show-paren--overlay-1)
243 (move-overlay show-paren--overlay-1
244 here-beg here-end (current-buffer))
245 ;; Always set the overlay face, since it varies.
246 (overlay-put show-paren--overlay-1 'priority show-paren-priority)
247 (overlay-put show-paren--overlay-1 'face face))
249 ;; Turn on highlighting for the matching paren, if found.
250 ;; If it's an unmatched paren, turn off any such highlighting.
251 (if (not there-beg)
252 (delete-overlay show-paren--overlay)
253 (if (or (eq show-paren-style 'expression)
254 (and (eq show-paren-style 'mixed)
255 (let ((closest (if (< there-beg here-beg)
256 (1- there-end) (1+ there-beg))))
257 (not (pos-visible-in-window-p closest)))))
258 (move-overlay show-paren--overlay
259 (point)
260 (if (< there-beg here-beg) there-beg there-end)
261 (current-buffer))
262 (move-overlay show-paren--overlay
263 there-beg there-end (current-buffer)))
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))))))
268 (provide 'paren)
270 ;;; paren.el ends here