Commit | Line | Data |
---|---|---|
55535639 | 1 | ;;; paren.el --- highlight matching paren |
b578f267 | 2 | |
0d30b337 | 3 | ;; Copyright (C) 1993, 1996, 2001, 2002, 2003, 2004, |
409cc4a3 | 4 | ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc. |
5e28918b | 5 | |
5762abec | 6 | ;; Author: rms@gnu.org |
e4c37df7 JB |
7 | ;; Maintainer: FSF |
8 | ;; Keywords: languages, faces | |
5e2325c9 | 9 | |
5e28918b JB |
10 | ;; This file is part of GNU Emacs. |
11 | ||
eb3fa2cf | 12 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
5e28918b | 13 | ;; it under the terms of the GNU General Public License as published by |
eb3fa2cf GM |
14 | ;; the Free Software Foundation, either version 3 of the License, or |
15 | ;; (at your option) any later version. | |
5e28918b JB |
16 | |
17 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
18 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 | ;; GNU General Public License for more details. | |
21 | ||
22 | ;; You should have received a copy of the GNU General Public License | |
eb3fa2cf | 23 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
5e28918b JB |
24 | |
25 | ;;; Commentary: | |
26 | ||
ca5b46fc EZ |
27 | ;; Put this into your ~/.emacs: |
28 | ||
29 | ;; (show-paren-mode t) | |
30 | ||
31 | ;; It will display highlighting on whatever paren matches the one | |
32 | ;; before or after point. | |
5e28918b JB |
33 | |
34 | ;;; Code: | |
35 | ||
173f2341 SM |
36 | (defgroup paren-showing nil |
37 | "Showing (un)matching of parens and expressions." | |
38 | :prefix "show-paren-" | |
39 | :group 'paren-matching) | |
40 | ||
eb0d9f08 | 41 | ;; This is the overlay used to highlight the matching paren. |
d1475aa1 | 42 | (defvar show-paren-overlay nil) |
673cc3c5 | 43 | ;; This is the overlay used to highlight the closeparen right before point. |
eb0d9f08 RS |
44 | (defvar show-paren-overlay-1 nil) |
45 | ||
173f2341 SM |
46 | (defcustom show-paren-style 'parenthesis |
47 | "*Style used when showing a matching paren. | |
48 | Valid styles are `parenthesis' (meaning show the matching paren), | |
49 | `expression' (meaning show the entire expression enclosed by the paren) and | |
50 | `mixed' (meaning show the matching paren if it is visible, and the expression | |
51 | otherwise)." | |
52 | :type '(choice (const parenthesis) (const expression) (const mixed)) | |
53 | :group 'paren-showing) | |
54 | ||
55 | (defcustom show-paren-delay | |
56 | (if (featurep 'lisp-float-type) (/ (float 1) (float 8)) 1) | |
57 | "*Time in seconds to delay before showing a matching paren." | |
58 | :type '(number :tag "seconds") | |
59 | :group 'paren-showing) | |
60 | ||
5780586e GM |
61 | (defcustom show-paren-priority 1000 |
62 | "*Priority of paren highlighting overlays." | |
63 | :type 'integer | |
64 | :group 'paren-showing | |
65 | :version "21.1") | |
f1180544 | 66 | |
2fe66dda RS |
67 | (defcustom show-paren-ring-bell-on-mismatch nil |
68 | "*If non-nil, beep if mismatched paren is detected." | |
69 | :type 'boolean | |
70 | :group 'paren-showing | |
71 | :version "20.3") | |
f1180544 | 72 | |
3d3d2b8a JL |
73 | (defgroup paren-showing-faces nil |
74 | "Group for faces of Show Paren mode." | |
d6f0f3e0 RS |
75 | :group 'paren-showing |
76 | :group 'faces | |
77 | :version "22.1") | |
78 | ||
2ec46551 | 79 | (defface show-paren-match |
05e122fe MB |
80 | '((((class color) (background light)) |
81 | :background "turquoise") ; looks OK on tty (becomes cyan) | |
82 | (((class color) (background dark)) | |
83 | :background "steelblue3") ; looks OK on tty (becomes blue) | |
84 | (((background dark)) | |
85 | :background "grey50") | |
86 | (t | |
87 | :background "gray")) | |
173f2341 | 88 | "Show Paren mode face used for a matching paren." |
3d3d2b8a | 89 | :group 'paren-showing-faces) |
2ec46551 MB |
90 | ;; backward-compatibility alias |
91 | (put 'show-paren-match-face 'face-alias 'show-paren-match) | |
173f2341 | 92 | |
2ec46551 | 93 | (defface show-paren-mismatch |
173f2341 | 94 | '((((class color)) (:foreground "white" :background "purple")) |
2e8f6012 | 95 | (t (:inverse-video t))) |
173f2341 | 96 | "Show Paren mode face used for a mismatching paren." |
3d3d2b8a | 97 | :group 'paren-showing-faces) |
2ec46551 MB |
98 | ;; backward-compatibility alias |
99 | (put 'show-paren-mismatch-face 'face-alias 'show-paren-mismatch) | |
30322a27 | 100 | |
6afd5d68 JL |
101 | (defvar show-paren-highlight-openparen t |
102 | "*Non-nil turns on openparen highlighting when matching forward.") | |
103 | ||
12eb8433 RS |
104 | (defvar show-paren-idle-timer nil) |
105 | ||
106 | ;;;###autoload | |
2b497eda | 107 | (define-minor-mode show-paren-mode |
12eb8433 RS |
108 | "Toggle Show Paren mode. |
109 | With prefix ARG, turn Show Paren mode on if and only if ARG is positive. | |
110 | Returns the new status of Show Paren mode (non-nil means on). | |
111 | ||
112 | When Show Paren mode is enabled, any matching parenthesis is highlighted | |
113 | in `show-paren-style' after `show-paren-delay' seconds of Emacs idle time." | |
83bfc0d9 | 114 | :global t :group 'paren-showing |
965cd29a | 115 | ;; Enable or disable the mechanism. |
050d5fc2 KH |
116 | ;; First get rid of the old idle timer. |
117 | (if show-paren-idle-timer | |
12eb8433 | 118 | (cancel-timer show-paren-idle-timer)) |
050d5fc2 KH |
119 | (setq show-paren-idle-timer nil) |
120 | ;; If show-paren-mode is enabled in some buffer now, | |
121 | ;; set up a new timer. | |
122 | (when (memq t (mapcar (lambda (buffer) | |
123 | (with-current-buffer buffer | |
124 | show-paren-mode)) | |
125 | (buffer-list))) | |
126 | (setq show-paren-idle-timer (run-with-idle-timer | |
127 | show-paren-delay t | |
128 | 'show-paren-function))) | |
2b497eda | 129 | (unless show-paren-mode |
050d5fc2 KH |
130 | (and show-paren-overlay |
131 | (eq (overlay-buffer show-paren-overlay) (current-buffer)) | |
132 | (delete-overlay show-paren-overlay)) | |
133 | (and show-paren-overlay-1 | |
134 | (eq (overlay-buffer show-paren-overlay-1) (current-buffer)) | |
2b497eda | 135 | (delete-overlay show-paren-overlay-1)))) |
12eb8433 | 136 | |
d1475aa1 JB |
137 | ;; Find the place to show, if there is one, |
138 | ;; and show it until input arrives. | |
673cc3c5 | 139 | (defun show-paren-function () |
050d5fc2 | 140 | (if show-paren-mode |
9fe9d55a | 141 | (let ((oldpos (point)) |
95ed7b42 SM |
142 | (dir (cond ((eq (syntax-class (syntax-after (1- (point)))) 5) -1) |
143 | ((eq (syntax-class (syntax-after (point))) 4) 1))) | |
9fe9d55a | 144 | pos mismatch face) |
173f2341 | 145 | ;; |
050d5fc2 KH |
146 | ;; Find the other end of the sexp. |
147 | (when dir | |
148 | (save-excursion | |
149 | (save-restriction | |
150 | ;; Determine the range within which to look for a match. | |
151 | (when blink-matching-paren-distance | |
152 | (narrow-to-region | |
153 | (max (point-min) (- (point) blink-matching-paren-distance)) | |
154 | (min (point-max) (+ (point) blink-matching-paren-distance)))) | |
155 | ;; Scan across one sexp within that range. | |
156 | ;; Errors or nil mean there is a mismatch. | |
157 | (condition-case () | |
158 | (setq pos (scan-sexps (point) dir)) | |
159 | (error (setq pos t mismatch t))) | |
6be7b127 RS |
160 | ;; Move back the other way and verify we get back to the |
161 | ;; starting point. If not, these two parens don't really match. | |
162 | ;; Maybe the one at point is escaped and doesn't really count. | |
163 | (when (integerp pos) | |
164 | (unless (condition-case () | |
165 | (eq (point) (scan-sexps pos (- dir))) | |
166 | (error nil)) | |
167 | (setq pos nil))) | |
050d5fc2 KH |
168 | ;; If found a "matching" paren, see if it is the right |
169 | ;; kind of paren to match the one we started at. | |
170 | (when (integerp pos) | |
171 | (let ((beg (min pos oldpos)) (end (max pos oldpos))) | |
95ed7b42 | 172 | (unless (eq (syntax-class (syntax-after beg)) 8) |
050d5fc2 | 173 | (setq mismatch |
96bda29d SM |
174 | (not (or (eq (char-before end) |
175 | ;; This can give nil. | |
176 | (cdr (syntax-after beg))) | |
177 | (eq (char-after beg) | |
178 | ;; This can give nil. | |
1793ac75 SM |
179 | (cdr (syntax-after (1- end)))) |
180 | ;; The cdr might hold a new paren-class | |
181 | ;; info rather than a matching-char info, | |
182 | ;; in which case the two CDRs should match. | |
183 | (eq (cdr (syntax-after (1- end))) | |
184 | (cdr (syntax-after beg)))))))))))) | |
173f2341 | 185 | ;; |
050d5fc2 KH |
186 | ;; Highlight the other end of the sexp, or unhighlight if none. |
187 | (if (not pos) | |
188 | (progn | |
189 | ;; If not at a paren that has a match, | |
190 | ;; turn off any previous paren highlighting. | |
191 | (and show-paren-overlay (overlay-buffer show-paren-overlay) | |
192 | (delete-overlay show-paren-overlay)) | |
193 | (and show-paren-overlay-1 (overlay-buffer show-paren-overlay-1) | |
194 | (delete-overlay show-paren-overlay-1))) | |
195 | ;; | |
196 | ;; Use the correct face. | |
197 | (if mismatch | |
198 | (progn | |
199 | (if show-paren-ring-bell-on-mismatch | |
200 | (beep)) | |
2ec46551 MB |
201 | (setq face 'show-paren-mismatch)) |
202 | (setq face 'show-paren-match)) | |
050d5fc2 KH |
203 | ;; |
204 | ;; If matching backwards, highlight the closeparen | |
205 | ;; before point as well as its matching open. | |
206 | ;; If matching forward, and the openparen is unbalanced, | |
207 | ;; highlight the paren at point to indicate misbalance. | |
208 | ;; Otherwise, turn off any such highlighting. | |
6afd5d68 | 209 | (if (and (not show-paren-highlight-openparen) (= dir 1) (integerp pos)) |
050d5fc2 KH |
210 | (when (and show-paren-overlay-1 |
211 | (overlay-buffer show-paren-overlay-1)) | |
212 | (delete-overlay show-paren-overlay-1)) | |
213 | (let ((from (if (= dir 1) | |
214 | (point) | |
215 | (forward-point -1))) | |
216 | (to (if (= dir 1) | |
217 | (forward-point 1) | |
218 | (point)))) | |
219 | (if show-paren-overlay-1 | |
220 | (move-overlay show-paren-overlay-1 from to (current-buffer)) | |
f9914209 | 221 | (setq show-paren-overlay-1 (make-overlay from to nil t))) |
050d5fc2 | 222 | ;; Always set the overlay face, since it varies. |
5780586e | 223 | (overlay-put show-paren-overlay-1 'priority show-paren-priority) |
050d5fc2 KH |
224 | (overlay-put show-paren-overlay-1 'face face))) |
225 | ;; | |
226 | ;; Turn on highlighting for the matching paren, if found. | |
227 | ;; If it's an unmatched paren, turn off any such highlighting. | |
228 | (unless (integerp pos) | |
229 | (delete-overlay show-paren-overlay)) | |
230 | (let ((to (if (or (eq show-paren-style 'expression) | |
173f2341 SM |
231 | (and (eq show-paren-style 'mixed) |
232 | (not (pos-visible-in-window-p pos)))) | |
050d5fc2 KH |
233 | (point) |
234 | pos)) | |
235 | (from (if (or (eq show-paren-style 'expression) | |
236 | (and (eq show-paren-style 'mixed) | |
237 | (not (pos-visible-in-window-p pos)))) | |
238 | pos | |
239 | (save-excursion | |
240 | (goto-char pos) | |
241 | (forward-point (- dir)))))) | |
242 | (if show-paren-overlay | |
243 | (move-overlay show-paren-overlay from to (current-buffer)) | |
f9914209 | 244 | (setq show-paren-overlay (make-overlay from to nil t)))) |
050d5fc2 KH |
245 | ;; |
246 | ;; Always set the overlay face, since it varies. | |
5780586e | 247 | (overlay-put show-paren-overlay 'priority show-paren-priority) |
f7e56094 | 248 | (overlay-put show-paren-overlay 'face face))) |
050d5fc2 KH |
249 | ;; show-paren-mode is nil in this buffer. |
250 | (and show-paren-overlay | |
251 | (delete-overlay show-paren-overlay)) | |
252 | (and show-paren-overlay-1 | |
253 | (delete-overlay show-paren-overlay-1)))) | |
5e28918b | 254 | |
3a3236d2 JB |
255 | (provide 'paren) |
256 | ||
d8ac3d27 | 257 | ;; arch-tag: d0969b88-7ac0-4bd0-bd53-e73b892b86a9 |
3a3236d2 | 258 | ;;; paren.el ends here |