* lisp/vc/vc-hg.el (vc-hg-unregister): New function (tiny change)
[emacs.git] / lisp / paren.el
blobb6b08016ab75b04e74044af9d5be9ceee49cad11
1 ;;; paren.el --- highlight matching paren
3 ;; Copyright (C) 1993, 1996, 2001-2014 Free Software Foundation, Inc.
5 ;; Author: rms@gnu.org
6 ;; Maintainer: emacs-devel@gnu.org
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 (define-obsolete-face-alias 'show-paren-match-face 'show-paren-match "22.1")
77 (define-obsolete-face-alias 'show-paren-mismatch-face
78 'show-paren-mismatch "22.1")
80 (defvar show-paren-highlight-openparen t
81 "Non-nil turns on openparen highlighting when matching forward.")
83 (defvar show-paren--idle-timer nil)
84 (defvar show-paren--overlay
85 (let ((ol (make-overlay (point) (point) nil t))) (delete-overlay ol) ol)
86 "Overlay used to highlight the matching paren.")
87 (defvar show-paren--overlay-1
88 (let ((ol (make-overlay (point) (point) nil t))) (delete-overlay ol) ol)
89 "Overlay used to highlight the paren at point.")
92 ;;;###autoload
93 (define-minor-mode show-paren-mode
94 "Toggle visualization of matching parens (Show Paren mode).
95 With a prefix argument ARG, enable Show Paren mode if ARG is
96 positive, and disable it otherwise. If called from Lisp, enable
97 the mode if ARG is omitted or nil.
99 Show Paren mode is a global minor mode. When enabled, any
100 matching parenthesis is highlighted in `show-paren-style' after
101 `show-paren-delay' seconds of Emacs idle time."
102 :global t :group 'paren-showing
103 ;; Enable or disable the mechanism.
104 ;; First get rid of the old idle timer.
105 (when show-paren--idle-timer
106 (cancel-timer show-paren--idle-timer)
107 (setq show-paren--idle-timer nil))
108 (setq show-paren--idle-timer (run-with-idle-timer
109 show-paren-delay t
110 #'show-paren-function))
111 (unless show-paren-mode
112 (delete-overlay show-paren--overlay)
113 (delete-overlay show-paren--overlay-1)))
115 (defvar show-paren-data-function #'show-paren--default
116 "Function to find the opener/closer at point and its match.
117 The function is called with no argument and should return either nil
118 if there's no opener/closer at point, or a list of the form
119 \(HERE-BEG HERE-END THERE-BEG THERE-END MISMATCH)
120 Where HERE-BEG..HERE-END is expected to be around point.")
122 (defun show-paren--default ()
123 (let* ((oldpos (point))
124 (dir (cond ((eq (syntax-class (syntax-after (1- (point)))) 5) -1)
125 ((eq (syntax-class (syntax-after (point))) 4) 1)))
126 (unescaped
127 (when dir
128 ;; Verify an even number of quoting characters precede the paren.
129 ;; Follow the same logic as in `blink-matching-open'.
130 (= (if (= dir -1) 1 0)
131 (logand 1 (- (point)
132 (save-excursion
133 (if (= dir -1) (forward-char -1))
134 (skip-syntax-backward "/\\")
135 (point)))))))
136 (here-beg (if (eq dir 1) (point) (1- (point))))
137 (here-end (if (eq dir 1) (1+ (point)) (point)))
138 pos mismatch)
140 ;; Find the other end of the sexp.
141 (when unescaped
142 (save-excursion
143 (save-restriction
144 ;; Determine the range within which to look for a match.
145 (when blink-matching-paren-distance
146 (narrow-to-region
147 (max (point-min) (- (point) blink-matching-paren-distance))
148 (min (point-max) (+ (point) blink-matching-paren-distance))))
149 ;; Scan across one sexp within that range.
150 ;; Errors or nil mean there is a mismatch.
151 (condition-case ()
152 (setq pos (scan-sexps (point) dir))
153 (error (setq pos t mismatch t)))
154 ;; Move back the other way and verify we get back to the
155 ;; starting point. If not, these two parens don't really match.
156 ;; Maybe the one at point is escaped and doesn't really count,
157 ;; or one is inside a comment.
158 (when (integerp pos)
159 (unless (condition-case ()
160 (eq (point) (scan-sexps pos (- dir)))
161 (error nil))
162 (setq pos nil)))
163 ;; If found a "matching" paren, see if it is the right
164 ;; kind of paren to match the one we started at.
165 (if (not (integerp pos))
166 (if mismatch (list here-beg here-end nil nil t))
167 (let ((beg (min pos oldpos)) (end (max pos oldpos)))
168 (unless (eq (syntax-class (syntax-after beg)) 8)
169 (setq mismatch
170 (not (or (eq (char-before end)
171 ;; This can give nil.
172 (cdr (syntax-after beg)))
173 (eq (char-after beg)
174 ;; This can give nil.
175 (cdr (syntax-after (1- end))))
176 ;; The cdr might hold a new paren-class
177 ;; info rather than a matching-char info,
178 ;; in which case the two CDRs should match.
179 (eq (cdr (syntax-after (1- end)))
180 (cdr (syntax-after beg)))))))
181 (list here-beg here-end
182 (if (= dir 1) (1- pos) pos)
183 (if (= dir 1) pos (1+ pos))
184 mismatch))))))))
186 ;; Find the place to show, if there is one,
187 ;; and show it until input arrives.
188 (defun show-paren-function ()
189 (let ((data (and show-paren-mode (funcall show-paren-data-function))))
190 (if (not data)
191 (progn
192 ;; If show-paren-mode is nil in this buffer or if not at a paren that
193 ;; has a match, turn off any previous paren highlighting.
194 (delete-overlay show-paren--overlay)
195 (delete-overlay show-paren--overlay-1))
197 ;; Found something to highlight.
198 (let* ((here-beg (nth 0 data))
199 (here-end (nth 1 data))
200 (there-beg (nth 2 data))
201 (there-end (nth 3 data))
202 (mismatch (nth 4 data))
203 (face
204 (if mismatch
205 (progn
206 (if show-paren-ring-bell-on-mismatch
207 (beep))
208 'show-paren-mismatch)
209 'show-paren-match)))
211 ;; If matching backwards, highlight the closeparen
212 ;; before point as well as its matching open.
213 ;; If matching forward, and the openparen is unbalanced,
214 ;; highlight the paren at point to indicate misbalance.
215 ;; Otherwise, turn off any such highlighting.
216 (if (or (not here-beg)
217 (and (not show-paren-highlight-openparen)
218 (> here-end (point))
219 (integerp there-beg)))
220 (delete-overlay show-paren--overlay-1)
221 (move-overlay show-paren--overlay-1
222 here-beg here-end (current-buffer))
223 ;; Always set the overlay face, since it varies.
224 (overlay-put show-paren--overlay-1 'priority show-paren-priority)
225 (overlay-put show-paren--overlay-1 'face face))
227 ;; Turn on highlighting for the matching paren, if found.
228 ;; If it's an unmatched paren, turn off any such highlighting.
229 (if (not there-beg)
230 (delete-overlay show-paren--overlay)
231 (if (or (eq show-paren-style 'expression)
232 (and (eq show-paren-style 'mixed)
233 (let ((closest (if (< there-beg here-beg)
234 (1- there-end) (1+ there-beg))))
235 (not (pos-visible-in-window-p closest)))))
236 (move-overlay show-paren--overlay
237 (point)
238 (if (< there-beg here-beg) there-beg there-end)
239 (current-buffer))
240 (move-overlay show-paren--overlay
241 there-beg there-end (current-buffer)))
242 ;; Always set the overlay face, since it varies.
243 (overlay-put show-paren--overlay 'priority show-paren-priority)
244 (overlay-put show-paren--overlay 'face face))))))
246 (provide 'paren)
248 ;;; paren.el ends here