Commit | Line | Data |
---|---|---|
55535639 | 1 | ;;; paren.el --- highlight matching paren |
b578f267 | 2 | |
95ed7b42 | 3 | ;; Copyright (C) 1993, 1996, 2001, 2004, 2005 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 | ||
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 2, or (at your option) | |
14 | ;; any later version. | |
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 | |
b578f267 EN |
22 | ;; along with GNU Emacs; see the file COPYING. If not, write to the |
23 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
24 | ;; Boston, MA 02111-1307, USA. | |
5e28918b JB |
25 | |
26 | ;;; Commentary: | |
27 | ||
ca5b46fc EZ |
28 | ;; Put this into your ~/.emacs: |
29 | ||
30 | ;; (show-paren-mode t) | |
31 | ||
32 | ;; It will display highlighting on whatever paren matches the one | |
33 | ;; before or after point. | |
5e28918b JB |
34 | |
35 | ;;; Code: | |
36 | ||
173f2341 SM |
37 | (defgroup paren-showing nil |
38 | "Showing (un)matching of parens and expressions." | |
39 | :prefix "show-paren-" | |
40 | :group 'paren-matching) | |
41 | ||
eb0d9f08 | 42 | ;; This is the overlay used to highlight the matching paren. |
d1475aa1 | 43 | (defvar show-paren-overlay nil) |
673cc3c5 | 44 | ;; This is the overlay used to highlight the closeparen right before point. |
eb0d9f08 RS |
45 | (defvar show-paren-overlay-1 nil) |
46 | ||
173f2341 SM |
47 | (defcustom show-paren-style 'parenthesis |
48 | "*Style used when showing a matching paren. | |
49 | Valid styles are `parenthesis' (meaning show the matching paren), | |
50 | `expression' (meaning show the entire expression enclosed by the paren) and | |
51 | `mixed' (meaning show the matching paren if it is visible, and the expression | |
52 | otherwise)." | |
53 | :type '(choice (const parenthesis) (const expression) (const mixed)) | |
54 | :group 'paren-showing) | |
55 | ||
56 | (defcustom show-paren-delay | |
57 | (if (featurep 'lisp-float-type) (/ (float 1) (float 8)) 1) | |
58 | "*Time in seconds to delay before showing a matching paren." | |
59 | :type '(number :tag "seconds") | |
60 | :group 'paren-showing) | |
61 | ||
5780586e GM |
62 | (defcustom show-paren-priority 1000 |
63 | "*Priority of paren highlighting overlays." | |
64 | :type 'integer | |
65 | :group 'paren-showing | |
66 | :version "21.1") | |
f1180544 | 67 | |
2fe66dda RS |
68 | (defcustom show-paren-ring-bell-on-mismatch nil |
69 | "*If non-nil, beep if mismatched paren is detected." | |
70 | :type 'boolean | |
71 | :group 'paren-showing | |
72 | :version "20.3") | |
f1180544 | 73 | |
2ec46551 | 74 | (defface show-paren-match |
05e122fe MB |
75 | '((((class color) (background light)) |
76 | :background "turquoise") ; looks OK on tty (becomes cyan) | |
77 | (((class color) (background dark)) | |
78 | :background "steelblue3") ; looks OK on tty (becomes blue) | |
79 | (((background dark)) | |
80 | :background "grey50") | |
81 | (t | |
82 | :background "gray")) | |
173f2341 SM |
83 | "Show Paren mode face used for a matching paren." |
84 | :group 'faces | |
85 | :group 'paren-showing) | |
2ec46551 MB |
86 | ;; backward-compatibility alias |
87 | (put 'show-paren-match-face 'face-alias 'show-paren-match) | |
173f2341 | 88 | |
2ec46551 | 89 | (defface show-paren-mismatch |
173f2341 | 90 | '((((class color)) (:foreground "white" :background "purple")) |
2e8f6012 | 91 | (t (:inverse-video t))) |
173f2341 SM |
92 | "Show Paren mode face used for a mismatching paren." |
93 | :group 'faces | |
94 | :group 'paren-showing) | |
2ec46551 MB |
95 | ;; backward-compatibility alias |
96 | (put 'show-paren-mismatch-face 'face-alias 'show-paren-mismatch) | |
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 |
050d5fc2 KH |
112 | ;; Turn off the usual paren-matching method |
113 | ;; when this one is turned on. | |
114 | (if (local-variable-p 'show-paren-mode) | |
115 | (make-local-variable 'blink-matching-paren-on-screen) | |
116 | (kill-local-variable 'blink-matching-paren-on-screen)) | |
2b497eda | 117 | (setq blink-matching-paren-on-screen (not show-paren-mode)) |
050d5fc2 KH |
118 | |
119 | ;; Now enable or disable the mechanism. | |
120 | ;; First get rid of the old idle timer. | |
121 | (if show-paren-idle-timer | |
12eb8433 | 122 | (cancel-timer show-paren-idle-timer)) |
050d5fc2 KH |
123 | (setq show-paren-idle-timer nil) |
124 | ;; If show-paren-mode is enabled in some buffer now, | |
125 | ;; set up a new timer. | |
126 | (when (memq t (mapcar (lambda (buffer) | |
127 | (with-current-buffer buffer | |
128 | show-paren-mode)) | |
129 | (buffer-list))) | |
130 | (setq show-paren-idle-timer (run-with-idle-timer | |
131 | show-paren-delay t | |
132 | 'show-paren-function))) | |
2b497eda | 133 | (unless show-paren-mode |
050d5fc2 KH |
134 | (and show-paren-overlay |
135 | (eq (overlay-buffer show-paren-overlay) (current-buffer)) | |
136 | (delete-overlay show-paren-overlay)) | |
137 | (and show-paren-overlay-1 | |
138 | (eq (overlay-buffer show-paren-overlay-1) (current-buffer)) | |
2b497eda | 139 | (delete-overlay show-paren-overlay-1)))) |
12eb8433 | 140 | |
d1475aa1 JB |
141 | ;; Find the place to show, if there is one, |
142 | ;; and show it until input arrives. | |
673cc3c5 | 143 | (defun show-paren-function () |
050d5fc2 | 144 | (if show-paren-mode |
9fe9d55a | 145 | (let ((oldpos (point)) |
95ed7b42 SM |
146 | (dir (cond ((eq (syntax-class (syntax-after (1- (point)))) 5) -1) |
147 | ((eq (syntax-class (syntax-after (point))) 4) 1))) | |
9fe9d55a | 148 | pos mismatch face) |
173f2341 | 149 | ;; |
050d5fc2 KH |
150 | ;; Find the other end of the sexp. |
151 | (when dir | |
152 | (save-excursion | |
153 | (save-restriction | |
154 | ;; Determine the range within which to look for a match. | |
155 | (when blink-matching-paren-distance | |
156 | (narrow-to-region | |
157 | (max (point-min) (- (point) blink-matching-paren-distance)) | |
158 | (min (point-max) (+ (point) blink-matching-paren-distance)))) | |
159 | ;; Scan across one sexp within that range. | |
160 | ;; Errors or nil mean there is a mismatch. | |
161 | (condition-case () | |
162 | (setq pos (scan-sexps (point) dir)) | |
163 | (error (setq pos t mismatch t))) | |
6be7b127 RS |
164 | ;; Move back the other way and verify we get back to the |
165 | ;; starting point. If not, these two parens don't really match. | |
166 | ;; Maybe the one at point is escaped and doesn't really count. | |
167 | (when (integerp pos) | |
168 | (unless (condition-case () | |
169 | (eq (point) (scan-sexps pos (- dir))) | |
170 | (error nil)) | |
171 | (setq pos nil))) | |
050d5fc2 KH |
172 | ;; If found a "matching" paren, see if it is the right |
173 | ;; kind of paren to match the one we started at. | |
174 | (when (integerp pos) | |
175 | (let ((beg (min pos oldpos)) (end (max pos oldpos))) | |
95ed7b42 | 176 | (unless (eq (syntax-class (syntax-after beg)) 8) |
050d5fc2 | 177 | (setq mismatch |
96bda29d SM |
178 | (not (or (eq (char-before end) |
179 | ;; This can give nil. | |
180 | (cdr (syntax-after beg))) | |
181 | (eq (char-after beg) | |
182 | ;; This can give nil. | |
183 | (cdr (syntax-after (1- end))))))))))))) | |
173f2341 | 184 | ;; |
050d5fc2 KH |
185 | ;; Highlight the other end of the sexp, or unhighlight if none. |
186 | (if (not pos) | |
187 | (progn | |
188 | ;; If not at a paren that has a match, | |
189 | ;; turn off any previous paren highlighting. | |
190 | (and show-paren-overlay (overlay-buffer show-paren-overlay) | |
191 | (delete-overlay show-paren-overlay)) | |
192 | (and show-paren-overlay-1 (overlay-buffer show-paren-overlay-1) | |
193 | (delete-overlay show-paren-overlay-1))) | |
194 | ;; | |
195 | ;; Use the correct face. | |
196 | (if mismatch | |
197 | (progn | |
198 | (if show-paren-ring-bell-on-mismatch | |
199 | (beep)) | |
2ec46551 MB |
200 | (setq face 'show-paren-mismatch)) |
201 | (setq face 'show-paren-match)) | |
050d5fc2 KH |
202 | ;; |
203 | ;; If matching backwards, highlight the closeparen | |
204 | ;; before point as well as its matching open. | |
205 | ;; If matching forward, and the openparen is unbalanced, | |
206 | ;; highlight the paren at point to indicate misbalance. | |
207 | ;; Otherwise, turn off any such highlighting. | |
6afd5d68 | 208 | (if (and (not show-paren-highlight-openparen) (= dir 1) (integerp pos)) |
050d5fc2 KH |
209 | (when (and show-paren-overlay-1 |
210 | (overlay-buffer show-paren-overlay-1)) | |
211 | (delete-overlay show-paren-overlay-1)) | |
212 | (let ((from (if (= dir 1) | |
213 | (point) | |
214 | (forward-point -1))) | |
215 | (to (if (= dir 1) | |
216 | (forward-point 1) | |
217 | (point)))) | |
218 | (if show-paren-overlay-1 | |
219 | (move-overlay show-paren-overlay-1 from to (current-buffer)) | |
220 | (setq show-paren-overlay-1 (make-overlay from to))) | |
221 | ;; Always set the overlay face, since it varies. | |
5780586e | 222 | (overlay-put show-paren-overlay-1 'priority show-paren-priority) |
050d5fc2 KH |
223 | (overlay-put show-paren-overlay-1 'face face))) |
224 | ;; | |
225 | ;; Turn on highlighting for the matching paren, if found. | |
226 | ;; If it's an unmatched paren, turn off any such highlighting. | |
227 | (unless (integerp pos) | |
228 | (delete-overlay show-paren-overlay)) | |
229 | (let ((to (if (or (eq show-paren-style 'expression) | |
173f2341 SM |
230 | (and (eq show-paren-style 'mixed) |
231 | (not (pos-visible-in-window-p pos)))) | |
050d5fc2 KH |
232 | (point) |
233 | pos)) | |
234 | (from (if (or (eq show-paren-style 'expression) | |
235 | (and (eq show-paren-style 'mixed) | |
236 | (not (pos-visible-in-window-p pos)))) | |
237 | pos | |
238 | (save-excursion | |
239 | (goto-char pos) | |
240 | (forward-point (- dir)))))) | |
241 | (if show-paren-overlay | |
242 | (move-overlay show-paren-overlay from to (current-buffer)) | |
243 | (setq show-paren-overlay (make-overlay from to)))) | |
244 | ;; | |
245 | ;; Always set the overlay face, since it varies. | |
5780586e | 246 | (overlay-put show-paren-overlay 'priority show-paren-priority) |
050d5fc2 KH |
247 | (overlay-put show-paren-overlay 'face face))) |
248 | ;; show-paren-mode is nil in this buffer. | |
249 | (and show-paren-overlay | |
250 | (delete-overlay show-paren-overlay)) | |
251 | (and show-paren-overlay-1 | |
252 | (delete-overlay show-paren-overlay-1)))) | |
5e28918b | 253 | |
3a3236d2 JB |
254 | (provide 'paren) |
255 | ||
d8ac3d27 | 256 | ;; arch-tag: d0969b88-7ac0-4bd0-bd53-e73b892b86a9 |
3a3236d2 | 257 | ;;; paren.el ends here |