Commit | Line | Data |
---|---|---|
55535639 | 1 | ;;; paren.el --- highlight matching paren |
b578f267 | 2 | |
ba318903 | 3 | ;; Copyright (C) 1993, 1996, 2001-2014 Free Software Foundation, Inc. |
5e28918b | 4 | |
5762abec | 5 | ;; Author: rms@gnu.org |
e4c37df7 JB |
6 | ;; Maintainer: FSF |
7 | ;; Keywords: languages, faces | |
5e2325c9 | 8 | |
5e28918b JB |
9 | ;; This file is part of GNU Emacs. |
10 | ||
eb3fa2cf | 11 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
5e28918b | 12 | ;; it under the terms of the GNU General Public License as published by |
eb3fa2cf GM |
13 | ;; the Free Software Foundation, either version 3 of the License, or |
14 | ;; (at your option) any later version. | |
5e28918b JB |
15 | |
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. | |
20 | ||
21 | ;; You should have received a copy of the GNU General Public License | |
eb3fa2cf | 22 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
5e28918b JB |
23 | |
24 | ;;; Commentary: | |
25 | ||
ca5b46fc EZ |
26 | ;; Put this into your ~/.emacs: |
27 | ||
28 | ;; (show-paren-mode t) | |
29 | ||
30 | ;; It will display highlighting on whatever paren matches the one | |
31 | ;; before or after point. | |
5e28918b JB |
32 | |
33 | ;;; Code: | |
34 | ||
173f2341 SM |
35 | (defgroup paren-showing nil |
36 | "Showing (un)matching of parens and expressions." | |
37 | :prefix "show-paren-" | |
38 | :group 'paren-matching) | |
39 | ||
173f2341 | 40 | (defcustom show-paren-style 'parenthesis |
9201cc28 | 41 | "Style used when showing a matching paren. |
173f2341 SM |
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) | |
48 | ||
9a0dd02d | 49 | (defcustom show-paren-delay 0.125 |
69ba1f04 GM |
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." | |
173f2341 | 53 | :type '(number :tag "seconds") |
69ba1f04 GM |
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))) | |
173f2341 SM |
61 | :group 'paren-showing) |
62 | ||
5780586e | 63 | (defcustom show-paren-priority 1000 |
9201cc28 | 64 | "Priority of paren highlighting overlays." |
5780586e GM |
65 | :type 'integer |
66 | :group 'paren-showing | |
67 | :version "21.1") | |
f1180544 | 68 | |
2fe66dda | 69 | (defcustom show-paren-ring-bell-on-mismatch nil |
9201cc28 | 70 | "If non-nil, beep if mismatched paren is detected." |
2fe66dda RS |
71 | :type 'boolean |
72 | :group 'paren-showing | |
73 | :version "20.3") | |
f1180544 | 74 | |
c4f6e489 | 75 | (define-obsolete-face-alias 'show-paren-match-face 'show-paren-match "22.1") |
173f2341 | 76 | |
c4f6e489 GM |
77 | (define-obsolete-face-alias 'show-paren-mismatch-face |
78 | 'show-paren-mismatch "22.1") | |
30322a27 | 79 | |
6afd5d68 | 80 | (defvar show-paren-highlight-openparen t |
fb7ada5f | 81 | "Non-nil turns on openparen highlighting when matching forward.") |
6afd5d68 | 82 | |
9190b35b SM |
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.") | |
90 | ||
12eb8433 RS |
91 | |
92 | ;;;###autoload | |
2b497eda | 93 | (define-minor-mode show-paren-mode |
06e21633 CY |
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. | |
98 | ||
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." | |
83bfc0d9 | 102 | :global t :group 'paren-showing |
9190b35b SM |
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))) | |
12eb8433 | 114 | |
4f8d1cf6 SM |
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.") | |
121 | ||
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) | |
139 | ;; | |
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. | |
9190b35b SM |
156 | ;; Maybe the one at point is escaped and doesn't really count, |
157 | ;; or one is inside a comment. | |
4f8d1cf6 SM |
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)))))))) | |
185 | ||
d1475aa1 JB |
186 | ;; Find the place to show, if there is one, |
187 | ;; and show it until input arrives. | |
673cc3c5 | 188 | (defun show-paren-function () |
9190b35b SM |
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)) | |
196 | ||
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)) | |
4f8d1cf6 | 202 | (mismatch (nth 4 data)) |
9190b35b SM |
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))) | |
210 | ;; | |
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)) | |
226 | ;; | |
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)))))) | |
5e28918b | 245 | |
3a3236d2 JB |
246 | (provide 'paren) |
247 | ||
248 | ;;; paren.el ends here |