38412
|
1 ;;; paren.el --- highlight matching paren
|
14169
|
2
|
104778
|
3 ;; Copyright (C) 1993, 1996, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
|
106815
|
4 ;; 2008, 2009, 2010 Free Software Foundation, Inc.
|
3912
|
5
|
25278
|
6 ;; Author: rms@gnu.org
|
3977
|
7 ;; Maintainer: FSF
|
|
8 ;; Keywords: languages, faces
|
3976
|
9
|
3912
|
10 ;; This file is part of GNU Emacs.
|
|
11
|
94678
|
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
|
3912
|
13 ;; it under the terms of the GNU General Public License as published by
|
94678
|
14 ;; the Free Software Foundation, either version 3 of the License, or
|
|
15 ;; (at your option) any later version.
|
3912
|
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
|
94678
|
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
|
3912
|
24
|
|
25 ;;; Commentary:
|
|
26
|
39979
|
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.
|
3912
|
33
|
|
34 ;;; Code:
|
|
35
|
18330
|
36 (defgroup paren-showing nil
|
|
37 "Showing (un)matching of parens and expressions."
|
|
38 :prefix "show-paren-"
|
|
39 :group 'paren-matching)
|
|
40
|
4123
|
41 ;; This is the overlay used to highlight the matching paren.
|
3917
|
42 (defvar show-paren-overlay nil)
|
14706
|
43 ;; This is the overlay used to highlight the closeparen right before point.
|
4123
|
44 (defvar show-paren-overlay-1 nil)
|
|
45
|
18330
|
46 (defcustom show-paren-style 'parenthesis
|
100171
|
47 "Style used when showing a matching paren.
|
18330
|
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)
|
100171
|
57 "Time in seconds to delay before showing a matching paren."
|
18330
|
58 :type '(number :tag "seconds")
|
|
59 :group 'paren-showing)
|
|
60
|
30787
|
61 (defcustom show-paren-priority 1000
|
100171
|
62 "Priority of paren highlighting overlays."
|
30787
|
63 :type 'integer
|
|
64 :group 'paren-showing
|
|
65 :version "21.1")
|
49597
|
66
|
21891
|
67 (defcustom show-paren-ring-bell-on-mismatch nil
|
100171
|
68 "If non-nil, beep if mismatched paren is detected."
|
21891
|
69 :type 'boolean
|
|
70 :group 'paren-showing
|
|
71 :version "20.3")
|
49597
|
72
|
67913
|
73 (defgroup paren-showing-faces nil
|
|
74 "Group for faces of Show Paren mode."
|
67837
|
75 :group 'paren-showing
|
|
76 :group 'faces
|
|
77 :version "22.1")
|
|
78
|
63202
|
79 (defface show-paren-match
|
40794
|
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"))
|
18330
|
88 "Show Paren mode face used for a matching paren."
|
67913
|
89 :group 'paren-showing-faces)
|
104778
|
90 (define-obsolete-face-alias 'show-paren-match-face 'show-paren-match "22.1")
|
18330
|
91
|
63202
|
92 (defface show-paren-mismatch
|
18330
|
93 '((((class color)) (:foreground "white" :background "purple"))
|
48607
|
94 (t (:inverse-video t)))
|
18330
|
95 "Show Paren mode face used for a mismatching paren."
|
67913
|
96 :group 'paren-showing-faces)
|
104778
|
97 (define-obsolete-face-alias 'show-paren-mismatch-face
|
|
98 'show-paren-mismatch "22.1")
|
4183
|
99
|
55176
|
100 (defvar show-paren-highlight-openparen t
|
|
101 "*Non-nil turns on openparen highlighting when matching forward.")
|
|
102
|
18582
|
103 (defvar show-paren-idle-timer nil)
|
|
104
|
|
105 ;;;###autoload
|
31976
|
106 (define-minor-mode show-paren-mode
|
18582
|
107 "Toggle Show Paren mode.
|
|
108 With prefix ARG, turn Show Paren mode on if and only if ARG is positive.
|
|
109 Returns the new status of Show Paren mode (non-nil means on).
|
|
110
|
|
111 When Show Paren mode is enabled, any matching parenthesis is highlighted
|
|
112 in `show-paren-style' after `show-paren-delay' seconds of Emacs idle time."
|
33194
|
113 :global t :group 'paren-showing
|
67153
8dc626ffc10d
(show-paren-mode): No longer change `blink-matching-paren-on-screen'.
Luc Teirlinck <teirllm@auburn.edu>
diff
changeset
|
114 ;; Enable or disable the mechanism.
|
25275
|
115 ;; First get rid of the old idle timer.
|
|
116 (if show-paren-idle-timer
|
18582
|
117 (cancel-timer show-paren-idle-timer))
|
25275
|
118 (setq show-paren-idle-timer nil)
|
|
119 ;; If show-paren-mode is enabled in some buffer now,
|
|
120 ;; set up a new timer.
|
|
121 (when (memq t (mapcar (lambda (buffer)
|
|
122 (with-current-buffer buffer
|
|
123 show-paren-mode))
|
|
124 (buffer-list)))
|
|
125 (setq show-paren-idle-timer (run-with-idle-timer
|
|
126 show-paren-delay t
|
|
127 'show-paren-function)))
|
31976
|
128 (unless show-paren-mode
|
25275
|
129 (and show-paren-overlay
|
|
130 (eq (overlay-buffer show-paren-overlay) (current-buffer))
|
|
131 (delete-overlay show-paren-overlay))
|
|
132 (and show-paren-overlay-1
|
|
133 (eq (overlay-buffer show-paren-overlay-1) (current-buffer))
|
31976
|
134 (delete-overlay show-paren-overlay-1))))
|
18582
|
135
|
3917
|
136 ;; Find the place to show, if there is one,
|
|
137 ;; and show it until input arrives.
|
14706
|
138 (defun show-paren-function ()
|
25275
|
139 (if show-paren-mode
|
47357
|
140 (let ((oldpos (point))
|
61672
d7252d4ac684
(show-paren-function): Use it to recognize parens that are
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
141 (dir (cond ((eq (syntax-class (syntax-after (1- (point)))) 5) -1)
|
d7252d4ac684
(show-paren-function): Use it to recognize parens that are
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
142 ((eq (syntax-class (syntax-after (point))) 4) 1)))
|
47357
|
143 pos mismatch face)
|
25275
|
144 ;;
|
|
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)))
|
43830
|
159 ;; Move back the other way and verify we get back to the
|
|
160 ;; starting point. If not, these two parens don't really match.
|
|
161 ;; Maybe the one at point is escaped and doesn't really count.
|
|
162 (when (integerp pos)
|
|
163 (unless (condition-case ()
|
|
164 (eq (point) (scan-sexps pos (- dir)))
|
|
165 (error nil))
|
|
166 (setq pos nil)))
|
25275
|
167 ;; If found a "matching" paren, see if it is the right
|
|
168 ;; kind of paren to match the one we started at.
|
|
169 (when (integerp pos)
|
|
170 (let ((beg (min pos oldpos)) (end (max pos oldpos)))
|
61672
d7252d4ac684
(show-paren-function): Use it to recognize parens that are
Stefan Monnier <monnier@iro.umontreal.ca>
diff
changeset
|
171 (unless (eq (syntax-class (syntax-after beg)) 8)
|
25275
|
172 (setq mismatch
|
58805
|
173 (not (or (eq (char-before end)
|
|
174 ;; This can give nil.
|
|
175 (cdr (syntax-after beg)))
|
|
176 (eq (char-after beg)
|
|
177 ;; This can give nil.
|
67052
|
178 (cdr (syntax-after (1- end))))
|
|
179 ;; The cdr might hold a new paren-class
|
|
180 ;; info rather than a matching-char info,
|
|
181 ;; in which case the two CDRs should match.
|
|
182 (eq (cdr (syntax-after (1- end)))
|
|
183 (cdr (syntax-after beg))))))))))))
|
18330
|
184 ;;
|
25275
|
185 ;; Highlight the other end of the sexp, or unhighlight if none.
|
|
186 (if (not pos)
|
21891
|
187 (progn
|
25275
|
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))
|
63202
|
200 (setq face 'show-paren-mismatch))
|
|
201 (setq face 'show-paren-match))
|
25275
|
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.
|
55176
|
208 (if (and (not show-paren-highlight-openparen) (= dir 1) (integerp pos))
|
25275
|
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)
|
102658
|
214 (- (point) 1)))
|
25275
|
215 (to (if (= dir 1)
|
102658
|
216 (+ (point) 1)
|
25275
|
217 (point))))
|
|
218 (if show-paren-overlay-1
|
|
219 (move-overlay show-paren-overlay-1 from to (current-buffer))
|
74359
|
220 (setq show-paren-overlay-1 (make-overlay from to nil t)))
|
25275
|
221 ;; Always set the overlay face, since it varies.
|
30787
|
222 (overlay-put show-paren-overlay-1 'priority show-paren-priority)
|
25275
|
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)
|
18330
|
230 (and (eq show-paren-style 'mixed)
|
|
231 (not (pos-visible-in-window-p pos))))
|
25275
|
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)
|
102658
|
240 (- (point) dir)))))
|
25275
|
241 (if show-paren-overlay
|
|
242 (move-overlay show-paren-overlay from to (current-buffer))
|
74359
|
243 (setq show-paren-overlay (make-overlay from to nil t))))
|
25275
|
244 ;;
|
|
245 ;; Always set the overlay face, since it varies.
|
30787
|
246 (overlay-put show-paren-overlay 'priority show-paren-priority)
|
77851
5d313ba2e8de
(show-paren-function): Undo 2007-04-19 and 2007-04-20 changes.
Chong Yidong <cyd@stupidchicken.com>
diff
changeset
|
247 (overlay-put show-paren-overlay 'face face)))
|
25275
|
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))))
|
3912
|
253
|
3919
|
254 (provide 'paren)
|
|
255
|
58416
|
256 ;; arch-tag: d0969b88-7ac0-4bd0-bd53-e73b892b86a9
|
3919
|
257 ;;; paren.el ends here
|