Commit | Line | Data |
---|---|---|
55535639 | 1 | ;;; paren.el --- highlight matching paren |
b578f267 | 2 | |
ab422c4d | 3 | ;; Copyright (C) 1993, 1996, 2001-2013 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 | ||
eb0d9f08 | 40 | ;; This is the overlay used to highlight the matching paren. |
d1475aa1 | 41 | (defvar show-paren-overlay nil) |
673cc3c5 | 42 | ;; This is the overlay used to highlight the closeparen right before point. |
eb0d9f08 RS |
43 | (defvar show-paren-overlay-1 nil) |
44 | ||
173f2341 | 45 | (defcustom show-paren-style 'parenthesis |
9201cc28 | 46 | "Style used when showing a matching paren. |
173f2341 SM |
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) | |
53 | ||
9a0dd02d | 54 | (defcustom show-paren-delay 0.125 |
69ba1f04 GM |
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." | |
173f2341 | 58 | :type '(number :tag "seconds") |
69ba1f04 GM |
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))) | |
173f2341 SM |
66 | :group 'paren-showing) |
67 | ||
5780586e | 68 | (defcustom show-paren-priority 1000 |
9201cc28 | 69 | "Priority of paren highlighting overlays." |
5780586e GM |
70 | :type 'integer |
71 | :group 'paren-showing | |
72 | :version "21.1") | |
f1180544 | 73 | |
2fe66dda | 74 | (defcustom show-paren-ring-bell-on-mismatch nil |
9201cc28 | 75 | "If non-nil, beep if mismatched paren is detected." |
2fe66dda RS |
76 | :type 'boolean |
77 | :group 'paren-showing | |
78 | :version "20.3") | |
f1180544 | 79 | |
3d3d2b8a JL |
80 | (defgroup paren-showing-faces nil |
81 | "Group for faces of Show Paren mode." | |
d6f0f3e0 RS |
82 | :group 'paren-showing |
83 | :group 'faces | |
84 | :version "22.1") | |
85 | ||
2ec46551 | 86 | (defface show-paren-match |
05e122fe MB |
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") | |
93 | (t | |
94 | :background "gray")) | |
173f2341 | 95 | "Show Paren mode face used for a matching paren." |
3d3d2b8a | 96 | :group 'paren-showing-faces) |
c4f6e489 | 97 | (define-obsolete-face-alias 'show-paren-match-face 'show-paren-match "22.1") |
173f2341 | 98 | |
2ec46551 | 99 | (defface show-paren-mismatch |
173f2341 | 100 | '((((class color)) (:foreground "white" :background "purple")) |
2e8f6012 | 101 | (t (:inverse-video t))) |
173f2341 | 102 | "Show Paren mode face used for a mismatching paren." |
3d3d2b8a | 103 | :group 'paren-showing-faces) |
c4f6e489 GM |
104 | (define-obsolete-face-alias 'show-paren-mismatch-face |
105 | 'show-paren-mismatch "22.1") | |
30322a27 | 106 | |
6afd5d68 | 107 | (defvar show-paren-highlight-openparen t |
fb7ada5f | 108 | "Non-nil turns on openparen highlighting when matching forward.") |
6afd5d68 | 109 | |
12eb8433 RS |
110 | (defvar show-paren-idle-timer nil) |
111 | ||
112 | ;;;###autoload | |
2b497eda | 113 | (define-minor-mode show-paren-mode |
06e21633 CY |
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. | |
118 | ||
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." | |
83bfc0d9 | 122 | :global t :group 'paren-showing |
965cd29a | 123 | ;; Enable or disable the mechanism. |
050d5fc2 KH |
124 | ;; First get rid of the old idle timer. |
125 | (if show-paren-idle-timer | |
12eb8433 | 126 | (cancel-timer show-paren-idle-timer)) |
050d5fc2 KH |
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))) | |
2b497eda | 137 | (unless show-paren-mode |
050d5fc2 KH |
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)) | |
2b497eda | 143 | (delete-overlay show-paren-overlay-1)))) |
12eb8433 | 144 | |
d1475aa1 JB |
145 | ;; Find the place to show, if there is one, |
146 | ;; and show it until input arrives. | |
673cc3c5 | 147 | (defun show-paren-function () |
050d5fc2 | 148 | (if show-paren-mode |
056e44ef JL |
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) | |
173f2341 | 163 | ;; |
050d5fc2 | 164 | ;; Find the other end of the sexp. |
056e44ef | 165 | (when unescaped |
050d5fc2 KH |
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))) | |
6be7b127 RS |
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))) | |
050d5fc2 KH |
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))) | |
95ed7b42 | 190 | (unless (eq (syntax-class (syntax-after beg)) 8) |
050d5fc2 | 191 | (setq mismatch |
96bda29d SM |
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. | |
1793ac75 SM |
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)))))))))))) | |
173f2341 | 203 | ;; |
050d5fc2 KH |
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))) | |
213 | ;; | |
214 | ;; Use the correct face. | |
215 | (if mismatch | |
216 | (progn | |
217 | (if show-paren-ring-bell-on-mismatch | |
218 | (beep)) | |
2ec46551 MB |
219 | (setq face 'show-paren-mismatch)) |
220 | (setq face 'show-paren-match)) | |
050d5fc2 KH |
221 | ;; |
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. | |
6afd5d68 | 227 | (if (and (not show-paren-highlight-openparen) (= dir 1) (integerp pos)) |
050d5fc2 KH |
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) | |
673e5169 | 233 | (- (point) 1))) |
050d5fc2 | 234 | (to (if (= dir 1) |
673e5169 | 235 | (+ (point) 1) |
050d5fc2 KH |
236 | (point)))) |
237 | (if show-paren-overlay-1 | |
238 | (move-overlay show-paren-overlay-1 from to (current-buffer)) | |
f9914209 | 239 | (setq show-paren-overlay-1 (make-overlay from to nil t))) |
050d5fc2 | 240 | ;; Always set the overlay face, since it varies. |
5780586e | 241 | (overlay-put show-paren-overlay-1 'priority show-paren-priority) |
050d5fc2 KH |
242 | (overlay-put show-paren-overlay-1 'face face))) |
243 | ;; | |
244 | ;; Turn on highlighting for the matching paren, if found. | |
245 | ;; If it's an unmatched paren, turn off any such highlighting. | |
d6f9c03f BG |
246 | (if (not (integerp pos)) |
247 | (when show-paren-overlay (delete-overlay show-paren-overlay)) | |
248 | (let ((to (if (or (eq show-paren-style 'expression) | |
050d5fc2 KH |
249 | (and (eq show-paren-style 'mixed) |
250 | (not (pos-visible-in-window-p pos)))) | |
d6f9c03f BG |
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)))) | |
256 | 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)) | |
3d4147ba BG |
262 | (setq show-paren-overlay (make-overlay from to nil t)))) |
263 | ;; Always set the overlay face, since it varies. | |
264 | (overlay-put show-paren-overlay 'priority show-paren-priority) | |
265 | (overlay-put show-paren-overlay 'face face)))) | |
050d5fc2 KH |
266 | ;; show-paren-mode is nil in this buffer. |
267 | (and show-paren-overlay | |
268 | (delete-overlay show-paren-overlay)) | |
269 | (and show-paren-overlay-1 | |
270 | (delete-overlay show-paren-overlay-1)))) | |
5e28918b | 271 | |
3a3236d2 JB |
272 | (provide 'paren) |
273 | ||
274 | ;;; paren.el ends here |