Commit | Line | Data |
---|---|---|
55535639 | 1 | ;;; paren.el --- highlight matching paren |
b578f267 | 2 | |
73b0cd50 | 3 | ;; Copyright (C) 1993, 1996, 2001-2011 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 |
9201cc28 | 55 | "Time in seconds to delay before showing a matching paren." |
173f2341 SM |
56 | :type '(number :tag "seconds") |
57 | :group 'paren-showing) | |
58 | ||
5780586e | 59 | (defcustom show-paren-priority 1000 |
9201cc28 | 60 | "Priority of paren highlighting overlays." |
5780586e GM |
61 | :type 'integer |
62 | :group 'paren-showing | |
63 | :version "21.1") | |
f1180544 | 64 | |
2fe66dda | 65 | (defcustom show-paren-ring-bell-on-mismatch nil |
9201cc28 | 66 | "If non-nil, beep if mismatched paren is detected." |
2fe66dda RS |
67 | :type 'boolean |
68 | :group 'paren-showing | |
69 | :version "20.3") | |
f1180544 | 70 | |
3d3d2b8a JL |
71 | (defgroup paren-showing-faces nil |
72 | "Group for faces of Show Paren mode." | |
d6f0f3e0 RS |
73 | :group 'paren-showing |
74 | :group 'faces | |
75 | :version "22.1") | |
76 | ||
2ec46551 | 77 | (defface show-paren-match |
05e122fe MB |
78 | '((((class color) (background light)) |
79 | :background "turquoise") ; looks OK on tty (becomes cyan) | |
80 | (((class color) (background dark)) | |
81 | :background "steelblue3") ; looks OK on tty (becomes blue) | |
82 | (((background dark)) | |
83 | :background "grey50") | |
84 | (t | |
85 | :background "gray")) | |
173f2341 | 86 | "Show Paren mode face used for a matching paren." |
3d3d2b8a | 87 | :group 'paren-showing-faces) |
c4f6e489 | 88 | (define-obsolete-face-alias 'show-paren-match-face 'show-paren-match "22.1") |
173f2341 | 89 | |
2ec46551 | 90 | (defface show-paren-mismatch |
173f2341 | 91 | '((((class color)) (:foreground "white" :background "purple")) |
2e8f6012 | 92 | (t (:inverse-video t))) |
173f2341 | 93 | "Show Paren mode face used for a mismatching paren." |
3d3d2b8a | 94 | :group 'paren-showing-faces) |
c4f6e489 GM |
95 | (define-obsolete-face-alias 'show-paren-mismatch-face |
96 | 'show-paren-mismatch "22.1") | |
30322a27 | 97 | |
6afd5d68 JL |
98 | (defvar show-paren-highlight-openparen t |
99 | "*Non-nil turns on openparen highlighting when matching forward.") | |
100 | ||
12eb8433 RS |
101 | (defvar show-paren-idle-timer nil) |
102 | ||
103 | ;;;###autoload | |
2b497eda | 104 | (define-minor-mode show-paren-mode |
12eb8433 RS |
105 | "Toggle Show Paren mode. |
106 | With prefix ARG, turn Show Paren mode on if and only if ARG is positive. | |
107 | Returns the new status of Show Paren mode (non-nil means on). | |
108 | ||
109 | When Show Paren mode is enabled, any matching parenthesis is highlighted | |
110 | in `show-paren-style' after `show-paren-delay' seconds of Emacs idle time." | |
83bfc0d9 | 111 | :global t :group 'paren-showing |
965cd29a | 112 | ;; Enable or disable the mechanism. |
050d5fc2 KH |
113 | ;; First get rid of the old idle timer. |
114 | (if show-paren-idle-timer | |
12eb8433 | 115 | (cancel-timer show-paren-idle-timer)) |
050d5fc2 KH |
116 | (setq show-paren-idle-timer nil) |
117 | ;; If show-paren-mode is enabled in some buffer now, | |
118 | ;; set up a new timer. | |
119 | (when (memq t (mapcar (lambda (buffer) | |
120 | (with-current-buffer buffer | |
121 | show-paren-mode)) | |
122 | (buffer-list))) | |
123 | (setq show-paren-idle-timer (run-with-idle-timer | |
124 | show-paren-delay t | |
125 | 'show-paren-function))) | |
2b497eda | 126 | (unless show-paren-mode |
050d5fc2 KH |
127 | (and show-paren-overlay |
128 | (eq (overlay-buffer show-paren-overlay) (current-buffer)) | |
129 | (delete-overlay show-paren-overlay)) | |
130 | (and show-paren-overlay-1 | |
131 | (eq (overlay-buffer show-paren-overlay-1) (current-buffer)) | |
2b497eda | 132 | (delete-overlay show-paren-overlay-1)))) |
12eb8433 | 133 | |
d1475aa1 JB |
134 | ;; Find the place to show, if there is one, |
135 | ;; and show it until input arrives. | |
673cc3c5 | 136 | (defun show-paren-function () |
050d5fc2 | 137 | (if show-paren-mode |
056e44ef JL |
138 | (let* ((oldpos (point)) |
139 | (dir (cond ((eq (syntax-class (syntax-after (1- (point)))) 5) -1) | |
140 | ((eq (syntax-class (syntax-after (point))) 4) 1))) | |
141 | (unescaped | |
142 | (when dir | |
143 | ;; Verify an even number of quoting characters precede the paren. | |
144 | ;; Follow the same logic as in `blink-matching-open'. | |
145 | (= (if (= dir -1) 1 0) | |
146 | (logand 1 (- (point) | |
147 | (save-excursion | |
148 | (if (= dir -1) (forward-char -1)) | |
149 | (skip-syntax-backward "/\\") | |
150 | (point))))))) | |
151 | pos mismatch face) | |
173f2341 | 152 | ;; |
050d5fc2 | 153 | ;; Find the other end of the sexp. |
056e44ef | 154 | (when unescaped |
050d5fc2 KH |
155 | (save-excursion |
156 | (save-restriction | |
157 | ;; Determine the range within which to look for a match. | |
158 | (when blink-matching-paren-distance | |
159 | (narrow-to-region | |
160 | (max (point-min) (- (point) blink-matching-paren-distance)) | |
161 | (min (point-max) (+ (point) blink-matching-paren-distance)))) | |
162 | ;; Scan across one sexp within that range. | |
163 | ;; Errors or nil mean there is a mismatch. | |
164 | (condition-case () | |
165 | (setq pos (scan-sexps (point) dir)) | |
166 | (error (setq pos t mismatch t))) | |
6be7b127 RS |
167 | ;; Move back the other way and verify we get back to the |
168 | ;; starting point. If not, these two parens don't really match. | |
169 | ;; Maybe the one at point is escaped and doesn't really count. | |
170 | (when (integerp pos) | |
171 | (unless (condition-case () | |
172 | (eq (point) (scan-sexps pos (- dir))) | |
173 | (error nil)) | |
174 | (setq pos nil))) | |
050d5fc2 KH |
175 | ;; If found a "matching" paren, see if it is the right |
176 | ;; kind of paren to match the one we started at. | |
177 | (when (integerp pos) | |
178 | (let ((beg (min pos oldpos)) (end (max pos oldpos))) | |
95ed7b42 | 179 | (unless (eq (syntax-class (syntax-after beg)) 8) |
050d5fc2 | 180 | (setq mismatch |
96bda29d SM |
181 | (not (or (eq (char-before end) |
182 | ;; This can give nil. | |
183 | (cdr (syntax-after beg))) | |
184 | (eq (char-after beg) | |
185 | ;; This can give nil. | |
1793ac75 SM |
186 | (cdr (syntax-after (1- end)))) |
187 | ;; The cdr might hold a new paren-class | |
188 | ;; info rather than a matching-char info, | |
189 | ;; in which case the two CDRs should match. | |
190 | (eq (cdr (syntax-after (1- end))) | |
191 | (cdr (syntax-after beg)))))))))))) | |
173f2341 | 192 | ;; |
050d5fc2 KH |
193 | ;; Highlight the other end of the sexp, or unhighlight if none. |
194 | (if (not pos) | |
195 | (progn | |
196 | ;; If not at a paren that has a match, | |
197 | ;; turn off any previous paren highlighting. | |
198 | (and show-paren-overlay (overlay-buffer show-paren-overlay) | |
199 | (delete-overlay show-paren-overlay)) | |
200 | (and show-paren-overlay-1 (overlay-buffer show-paren-overlay-1) | |
201 | (delete-overlay show-paren-overlay-1))) | |
202 | ;; | |
203 | ;; Use the correct face. | |
204 | (if mismatch | |
205 | (progn | |
206 | (if show-paren-ring-bell-on-mismatch | |
207 | (beep)) | |
2ec46551 MB |
208 | (setq face 'show-paren-mismatch)) |
209 | (setq face 'show-paren-match)) | |
050d5fc2 KH |
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. | |
6afd5d68 | 216 | (if (and (not show-paren-highlight-openparen) (= dir 1) (integerp pos)) |
050d5fc2 KH |
217 | (when (and show-paren-overlay-1 |
218 | (overlay-buffer show-paren-overlay-1)) | |
219 | (delete-overlay show-paren-overlay-1)) | |
220 | (let ((from (if (= dir 1) | |
221 | (point) | |
673e5169 | 222 | (- (point) 1))) |
050d5fc2 | 223 | (to (if (= dir 1) |
673e5169 | 224 | (+ (point) 1) |
050d5fc2 KH |
225 | (point)))) |
226 | (if show-paren-overlay-1 | |
227 | (move-overlay show-paren-overlay-1 from to (current-buffer)) | |
f9914209 | 228 | (setq show-paren-overlay-1 (make-overlay from to nil t))) |
050d5fc2 | 229 | ;; Always set the overlay face, since it varies. |
5780586e | 230 | (overlay-put show-paren-overlay-1 'priority show-paren-priority) |
050d5fc2 KH |
231 | (overlay-put show-paren-overlay-1 'face face))) |
232 | ;; | |
233 | ;; Turn on highlighting for the matching paren, if found. | |
234 | ;; If it's an unmatched paren, turn off any such highlighting. | |
235 | (unless (integerp pos) | |
236 | (delete-overlay show-paren-overlay)) | |
237 | (let ((to (if (or (eq show-paren-style 'expression) | |
173f2341 SM |
238 | (and (eq show-paren-style 'mixed) |
239 | (not (pos-visible-in-window-p pos)))) | |
050d5fc2 KH |
240 | (point) |
241 | pos)) | |
242 | (from (if (or (eq show-paren-style 'expression) | |
243 | (and (eq show-paren-style 'mixed) | |
244 | (not (pos-visible-in-window-p pos)))) | |
245 | pos | |
246 | (save-excursion | |
247 | (goto-char pos) | |
673e5169 | 248 | (- (point) dir))))) |
050d5fc2 KH |
249 | (if show-paren-overlay |
250 | (move-overlay show-paren-overlay from to (current-buffer)) | |
f9914209 | 251 | (setq show-paren-overlay (make-overlay from to nil t)))) |
050d5fc2 KH |
252 | ;; |
253 | ;; Always set the overlay face, since it varies. | |
5780586e | 254 | (overlay-put show-paren-overlay 'priority show-paren-priority) |
f7e56094 | 255 | (overlay-put show-paren-overlay 'face face))) |
050d5fc2 KH |
256 | ;; show-paren-mode is nil in this buffer. |
257 | (and show-paren-overlay | |
258 | (delete-overlay show-paren-overlay)) | |
259 | (and show-paren-overlay-1 | |
260 | (delete-overlay show-paren-overlay-1)))) | |
5e28918b | 261 | |
3a3236d2 JB |
262 | (provide 'paren) |
263 | ||
264 | ;;; paren.el ends here |