[lice @ shit loads of stuff]
[lice.git] / src / lisp / paren.lisp
blob50708c1160a4b1e35c9ff4ecc80068f5a89cc59b
1 ;;; paren.el --- highlight matching paren
3 ;; Copyright (C) 1993, 1996, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006 Free Software Foundation, Inc.
6 ;; Author: rms@gnu.org
7 ;; Maintainer: FSF
8 ;; Keywords: languages, faces
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
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.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
27 ;;; Commentary:
29 ;; Put this into your ~/.emacs:
31 ;; (show-paren-mode t)
33 ;; It will display highlighting on whatever paren matches the one
34 ;; before or after point.
36 ;;; Code:
38 (in-package "LICE")
40 (defgroup paren-showing nil
41 "Showing (un)matching of parens and expressions."
42 :prefix "show-paren-"
43 :group 'paren-matching)
45 ;; This is the overlay used to highlight the matching paren.
46 (defvar show-paren-overlay nil)
47 ;; This is the overlay used to highlight the closeparen right before point.
48 (defvar show-paren-overlay-1 nil)
50 (defcustom show-paren-style 'parenthesis
51 "*Style used when showing a matching paren.
52 Valid styles are `parenthesis' (meaning show the matching paren),
53 `expression' (meaning show the entire expression enclosed by the paren) and
54 `mixed' (meaning show the matching paren if it is visible, and the expression
55 otherwise)."
56 :type '(choice (const parenthesis) (const expression) (const mixed))
57 :group 'paren-showing)
59 (defcustom show-paren-delay
60 (if (featurep 'lisp-float-type) (/ (float 1) (float 8)) 1)
61 "*Time in seconds to delay before showing a matching paren."
62 :type '(number :tag "seconds")
63 :group 'paren-showing)
65 (defcustom show-paren-priority 1000
66 "*Priority of paren highlighting overlays."
67 :type 'integer
68 :group 'paren-showing
69 :version "21.1")
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")
77 (defgroup paren-showing-faces nil
78 "Group for faces of Show Paren mode."
79 :group 'paren-showing
80 :group 'faces
81 :version "22.1")
83 (defface show-paren-match
84 '((((class color) (background light))
85 :background "turquoise") ; looks OK on tty (becomes cyan)
86 (((class color) (background dark))
87 :background "steelblue3") ; looks OK on tty (becomes blue)
88 (((background dark))
89 :background "grey50")
91 :background "gray"))
92 "Show Paren mode face used for a matching paren."
93 :group 'paren-showing-faces)
94 ;; backward-compatibility alias
95 (put 'show-paren-match-face 'face-alias 'show-paren-match)
97 (defface show-paren-mismatch
98 '((((class color)) (:foreground "white" :background "purple"))
99 (t (:inverse-video t)))
100 "Show Paren mode face used for a mismatching paren."
101 :group 'paren-showing-faces)
102 ;; backward-compatibility alias
103 (put 'show-paren-mismatch-face 'face-alias 'show-paren-mismatch)
105 (defvar show-paren-highlight-openparen t
106 "*Non-nil turns on openparen highlighting when matching forward.")
108 (defvar show-paren-idle-timer nil)
110 ;;;###autoload
111 (define-minor-mode (show-paren-mode
112 "Toggle Show Paren mode.
113 With prefix ARG, turn Show Paren mode on if and only if ARG is positive.
114 Returns the new status of Show Paren mode (non-nil means on).
116 When Show Paren mode is enabled, any matching parenthesis is highlighted
117 in `show-paren-style' after `show-paren-delay' seconds of Emacs idle time."
118 :global t :group 'paren-showing)
119 ;; Enable or disable the mechanism.
120 ;; First get rid of the old idle timer.
121 (if show-paren-idle-timer
122 (cancel-timer show-paren-idle-timer))
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)))
133 (unless show-paren-mode
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))
139 (delete-overlay show-paren-overlay-1))))
141 ;; Find the place to show, if there is one,
142 ;; and show it until input arrives.
143 (defun show-paren-function ()
144 (el:if show-paren-mode
145 (let ((oldpos (point))
146 (dir (cond ((eq (syntax-class (syntax-after (1- (point)))) 5) -1)
147 ((eq (syntax-class (syntax-after (point))) 4) 1)))
148 pos mismatch face)
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)))
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)))
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)))
176 (unless (eq (syntax-class (syntax-after beg)) 8)
177 (setq mismatch
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))))
184 ;; The cdr might hold a new paren-class
185 ;; info rather than a matching-char info,
186 ;; in which case the two CDRs should match.
187 (eq (cdr (syntax-after (1- end)))
188 (cdr (syntax-after beg))))))))))))
190 ;; Highlight the other end of the sexp, or unhighlight if none.
191 (el:if (not pos)
192 (progn
193 ;; If not at a paren that has a match,
194 ;; turn off any previous paren highlighting.
195 (and show-paren-overlay (overlay-buffer show-paren-overlay)
196 (delete-overlay show-paren-overlay))
197 (and show-paren-overlay-1 (overlay-buffer show-paren-overlay-1)
198 (delete-overlay show-paren-overlay-1)))
200 ;; Use the correct face.
201 (el:if mismatch
202 (progn
203 (el:if show-paren-ring-bell-on-mismatch
204 (beep))
205 (setq face 'show-paren-mismatch))
206 (setq face 'show-paren-match))
208 ;; If matching backwards, highlight the closeparen
209 ;; before point as well as its matching open.
210 ;; If matching forward, and the openparen is unbalanced,
211 ;; highlight the paren at point to indicate misbalance.
212 ;; Otherwise, turn off any such highlighting.
213 (el:if (and (not show-paren-highlight-openparen) (= dir 1) (integerp pos))
214 (when (and show-paren-overlay-1
215 (overlay-buffer show-paren-overlay-1))
216 (delete-overlay show-paren-overlay-1))
217 (let ((from (el:if (= dir 1)
218 (point)
219 (forward-point -1)))
220 (to (el:if (= dir 1)
221 (forward-point 1)
222 (point))))
223 (el:if show-paren-overlay-1
224 (move-overlay show-paren-overlay-1 from to (current-buffer))
225 (setq show-paren-overlay-1 (make-overlay from to)))
226 ;; Always set the overlay face, since it varies.
227 (overlay-put show-paren-overlay-1 'priority show-paren-priority)
228 (overlay-put show-paren-overlay-1 'face face)))
230 ;; Turn on highlighting for the matching paren, if found.
231 ;; If it's an unmatched paren, turn off any such highlighting.
232 (unless (integerp pos)
233 (delete-overlay show-paren-overlay))
234 (let ((to (el:if (or (eq show-paren-style 'expression)
235 (and (eq show-paren-style 'mixed)
236 (not (pos-visible-in-window-p pos))))
237 (point)
238 pos))
239 (from (el:if (or (eq show-paren-style 'expression)
240 (and (eq show-paren-style 'mixed)
241 (not (pos-visible-in-window-p pos))))
243 (save-excursion
244 (goto-char pos)
245 (forward-point (- dir))))))
246 (el:if show-paren-overlay
247 (move-overlay show-paren-overlay from to (current-buffer))
248 (setq show-paren-overlay (make-overlay from to))))
250 ;; Always set the overlay face, since it varies.
251 (overlay-put show-paren-overlay 'priority show-paren-priority)
252 (overlay-put show-paren-overlay 'face face)))
253 ;; show-paren-mode is nil in this buffer.
254 (and show-paren-overlay
255 (delete-overlay show-paren-overlay))
256 (and show-paren-overlay-1
257 (delete-overlay show-paren-overlay-1))))
259 (provide 'paren)
261 ;;; paren.el ends here