Commit | Line | Data |
---|---|---|
5e28918b | 1 | ;;; paren.el --- highlight matching paren. |
b578f267 | 2 | |
673cc3c5 | 3 | ;; Copyright (C) 1993, 1996 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 | ||
28 | ;; Load this and it will display highlighting on whatever | |
29 | ;; paren matches the one before or after point. | |
30 | ||
31 | ;;; Code: | |
32 | ||
173f2341 SM |
33 | (defgroup paren-showing nil |
34 | "Showing (un)matching of parens and expressions." | |
35 | :prefix "show-paren-" | |
36 | :group 'paren-matching) | |
37 | ||
eb0d9f08 | 38 | ;; This is the overlay used to highlight the matching paren. |
d1475aa1 | 39 | (defvar show-paren-overlay nil) |
673cc3c5 | 40 | ;; This is the overlay used to highlight the closeparen right before point. |
eb0d9f08 RS |
41 | (defvar show-paren-overlay-1 nil) |
42 | ||
173f2341 | 43 | (defcustom show-paren-mode nil |
050d5fc2 | 44 | "*Toggle Show Paren mode. |
173f2341 SM |
45 | When Show Paren mode is enabled, any matching parenthesis is highlighted |
46 | after `show-paren-delay' seconds of Emacs idle time. | |
757c19e8 DL |
47 | Setting this variable directly does not take effect; |
48 | use either \\[customize] or the function `show-paren-mode'." | |
173f2341 SM |
49 | :set (lambda (symbol value) |
50 | (show-paren-mode (or value 0))) | |
12eb8433 | 51 | :initialize 'custom-initialize-default |
173f2341 SM |
52 | :type 'boolean |
53 | :group 'paren-showing | |
54 | :require 'paren) | |
55 | ||
56 | (defcustom show-paren-style 'parenthesis | |
57 | "*Style used when showing a matching paren. | |
58 | Valid styles are `parenthesis' (meaning show the matching paren), | |
59 | `expression' (meaning show the entire expression enclosed by the paren) and | |
60 | `mixed' (meaning show the matching paren if it is visible, and the expression | |
61 | otherwise)." | |
62 | :type '(choice (const parenthesis) (const expression) (const mixed)) | |
63 | :group 'paren-showing) | |
64 | ||
65 | (defcustom show-paren-delay | |
66 | (if (featurep 'lisp-float-type) (/ (float 1) (float 8)) 1) | |
67 | "*Time in seconds to delay before showing a matching paren." | |
68 | :type '(number :tag "seconds") | |
69 | :group 'paren-showing) | |
70 | ||
2fe66dda RS |
71 | (defcustom show-paren-ring-bell-on-mismatch nil |
72 | "*If non-nil, beep if mismatched paren is detected." | |
73 | :type 'boolean | |
74 | :group 'paren-showing | |
75 | :version "20.3") | |
76 | ||
173f2341 SM |
77 | (defface show-paren-match-face |
78 | '((((class color)) (:background "turquoise")) | |
82b6a81f | 79 | (t (:background "gray"))) |
173f2341 SM |
80 | "Show Paren mode face used for a matching paren." |
81 | :group 'faces | |
82 | :group 'paren-showing) | |
83 | ||
84 | (defface show-paren-mismatch-face | |
85 | '((((class color)) (:foreground "white" :background "purple")) | |
86 | (t (:reverse-video t))) | |
87 | "Show Paren mode face used for a mismatching paren." | |
88 | :group 'faces | |
89 | :group 'paren-showing) | |
30322a27 | 90 | |
12eb8433 RS |
91 | (defvar show-paren-idle-timer nil) |
92 | ||
93 | ;;;###autoload | |
94 | (defun show-paren-mode (&optional arg) | |
95 | "Toggle Show Paren mode. | |
96 | With prefix ARG, turn Show Paren mode on if and only if ARG is positive. | |
97 | Returns the new status of Show Paren mode (non-nil means on). | |
98 | ||
99 | When Show Paren mode is enabled, any matching parenthesis is highlighted | |
100 | in `show-paren-style' after `show-paren-delay' seconds of Emacs idle time." | |
101 | (interactive "P") | |
050d5fc2 KH |
102 | (let ((on-p (if arg |
103 | (> (prefix-numeric-value arg) 0) | |
104 | (not show-paren-mode)))) | |
105 | (setq show-paren-mode on-p) | |
106 | ;; Turn off the usual paren-matching method | |
107 | ;; when this one is turned on. | |
108 | (if (local-variable-p 'show-paren-mode) | |
109 | (make-local-variable 'blink-matching-paren-on-screen) | |
110 | (kill-local-variable 'blink-matching-paren-on-screen)) | |
111 | (setq blink-matching-paren-on-screen (not on-p)) | |
112 | ||
113 | ;; Now enable or disable the mechanism. | |
114 | ;; First get rid of the old idle timer. | |
115 | (if show-paren-idle-timer | |
12eb8433 | 116 | (cancel-timer show-paren-idle-timer)) |
050d5fc2 KH |
117 | (setq show-paren-idle-timer nil) |
118 | ;; If show-paren-mode is enabled in some buffer now, | |
119 | ;; set up a new timer. | |
120 | (when (memq t (mapcar (lambda (buffer) | |
121 | (with-current-buffer buffer | |
122 | show-paren-mode)) | |
123 | (buffer-list))) | |
124 | (setq show-paren-idle-timer (run-with-idle-timer | |
125 | show-paren-delay t | |
126 | 'show-paren-function))) | |
127 | (unless on-p | |
128 | (and show-paren-overlay | |
129 | (eq (overlay-buffer show-paren-overlay) (current-buffer)) | |
130 | (delete-overlay show-paren-overlay)) | |
131 | (and show-paren-overlay-1 | |
132 | (eq (overlay-buffer show-paren-overlay-1) (current-buffer)) | |
133 | (delete-overlay show-paren-overlay-1))))) | |
12eb8433 | 134 | |
d1475aa1 JB |
135 | ;; Find the place to show, if there is one, |
136 | ;; and show it until input arrives. | |
673cc3c5 | 137 | (defun show-paren-function () |
050d5fc2 KH |
138 | (if show-paren-mode |
139 | (let (pos dir mismatch face (oldpos (point))) | |
140 | (cond ((eq (char-syntax (preceding-char)) ?\)) | |
141 | (setq dir -1)) | |
142 | ((eq (char-syntax (following-char)) ?\() | |
143 | (setq dir 1))) | |
173f2341 | 144 | ;; |
050d5fc2 KH |
145 | ;; Find the other end of the sexp. |
146 | (when dir | |
147 | (save-excursion | |
148 | (save-restriction | |
149 | ;; Determine the range within which to look for a match. | |
150 | (when blink-matching-paren-distance | |
151 | (narrow-to-region | |
152 | (max (point-min) (- (point) blink-matching-paren-distance)) | |
153 | (min (point-max) (+ (point) blink-matching-paren-distance)))) | |
154 | ;; Scan across one sexp within that range. | |
155 | ;; Errors or nil mean there is a mismatch. | |
156 | (condition-case () | |
157 | (setq pos (scan-sexps (point) dir)) | |
158 | (error (setq pos t mismatch t))) | |
159 | ;; If found a "matching" paren, see if it is the right | |
160 | ;; kind of paren to match the one we started at. | |
161 | (when (integerp pos) | |
162 | (let ((beg (min pos oldpos)) (end (max pos oldpos))) | |
163 | (when (/= (char-syntax (char-after beg)) ?\$) | |
164 | (setq mismatch | |
165 | (not (eq (char-before end) | |
166 | ;; This can give nil. | |
167 | (matching-paren (char-after beg))))))))))) | |
173f2341 | 168 | ;; |
050d5fc2 KH |
169 | ;; Highlight the other end of the sexp, or unhighlight if none. |
170 | (if (not pos) | |
171 | (progn | |
172 | ;; If not at a paren that has a match, | |
173 | ;; turn off any previous paren highlighting. | |
174 | (and show-paren-overlay (overlay-buffer show-paren-overlay) | |
175 | (delete-overlay show-paren-overlay)) | |
176 | (and show-paren-overlay-1 (overlay-buffer show-paren-overlay-1) | |
177 | (delete-overlay show-paren-overlay-1))) | |
178 | ;; | |
179 | ;; Use the correct face. | |
180 | (if mismatch | |
181 | (progn | |
182 | (if show-paren-ring-bell-on-mismatch | |
183 | (beep)) | |
184 | (setq face 'show-paren-mismatch-face)) | |
185 | (setq face 'show-paren-match-face)) | |
186 | ;; | |
187 | ;; If matching backwards, highlight the closeparen | |
188 | ;; before point as well as its matching open. | |
189 | ;; If matching forward, and the openparen is unbalanced, | |
190 | ;; highlight the paren at point to indicate misbalance. | |
191 | ;; Otherwise, turn off any such highlighting. | |
192 | (if (and (= dir 1) (integerp pos)) | |
193 | (when (and show-paren-overlay-1 | |
194 | (overlay-buffer show-paren-overlay-1)) | |
195 | (delete-overlay show-paren-overlay-1)) | |
196 | (let ((from (if (= dir 1) | |
197 | (point) | |
198 | (forward-point -1))) | |
199 | (to (if (= dir 1) | |
200 | (forward-point 1) | |
201 | (point)))) | |
202 | (if show-paren-overlay-1 | |
203 | (move-overlay show-paren-overlay-1 from to (current-buffer)) | |
204 | (setq show-paren-overlay-1 (make-overlay from to))) | |
205 | ;; Always set the overlay face, since it varies. | |
206 | (overlay-put show-paren-overlay-1 'face face))) | |
207 | ;; | |
208 | ;; Turn on highlighting for the matching paren, if found. | |
209 | ;; If it's an unmatched paren, turn off any such highlighting. | |
210 | (unless (integerp pos) | |
211 | (delete-overlay show-paren-overlay)) | |
212 | (let ((to (if (or (eq show-paren-style 'expression) | |
173f2341 SM |
213 | (and (eq show-paren-style 'mixed) |
214 | (not (pos-visible-in-window-p pos)))) | |
050d5fc2 KH |
215 | (point) |
216 | pos)) | |
217 | (from (if (or (eq show-paren-style 'expression) | |
218 | (and (eq show-paren-style 'mixed) | |
219 | (not (pos-visible-in-window-p pos)))) | |
220 | pos | |
221 | (save-excursion | |
222 | (goto-char pos) | |
223 | (forward-point (- dir)))))) | |
224 | (if show-paren-overlay | |
225 | (move-overlay show-paren-overlay from to (current-buffer)) | |
226 | (setq show-paren-overlay (make-overlay from to)))) | |
227 | ;; | |
228 | ;; Always set the overlay face, since it varies. | |
229 | (overlay-put show-paren-overlay 'face face))) | |
230 | ;; show-paren-mode is nil in this buffer. | |
231 | (and show-paren-overlay | |
232 | (delete-overlay show-paren-overlay)) | |
233 | (and show-paren-overlay-1 | |
234 | (delete-overlay show-paren-overlay-1)))) | |
5e28918b | 235 | |
3a3236d2 JB |
236 | (provide 'paren) |
237 | ||
12eb8433 RS |
238 | (if show-paren-mode |
239 | (show-paren-mode t)) | |
240 | ||
3a3236d2 | 241 | ;;; paren.el ends here |