*** empty log message ***
[emacs.git] / lisp / paren.el
blob6c5f9dece99a6a37c0b2746046dae9ed801b8310
1 ;;; paren.el --- highlight matching paren
3 ;; Copyright (C) 1993, 1996, 2001 Free Software Foundation, Inc.
5 ;; Author: rms@gnu.org
6 ;; Maintainer: FSF
7 ;; Keywords: languages, faces
9 ;; This file is part of GNU Emacs.
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.
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.
21 ;; You should have received a copy of the GNU General Public License
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.
26 ;;; Commentary:
28 ;; Put this into your ~/.emacs:
30 ;; (show-paren-mode t)
32 ;; It will display highlighting on whatever paren matches the one
33 ;; before or after point.
35 ;;; Code:
37 (defgroup paren-showing nil
38 "Showing (un)matching of parens and expressions."
39 :prefix "show-paren-"
40 :group 'paren-matching)
42 ;; This is the overlay used to highlight the matching paren.
43 (defvar show-paren-overlay nil)
44 ;; This is the overlay used to highlight the closeparen right before point.
45 (defvar show-paren-overlay-1 nil)
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)
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)
62 (defcustom show-paren-priority 1000
63 "*Priority of paren highlighting overlays."
64 :type 'integer
65 :group 'paren-showing
66 :version "21.1")
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")
74 (defface show-paren-match-face
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")
82 :background "gray"))
83 "Show Paren mode face used for a matching paren."
84 :group 'faces
85 :group 'paren-showing)
87 (defface show-paren-mismatch-face
88 '((((class color)) (:foreground "white" :background "purple"))
89 (t (:inverse-video t)))
90 "Show Paren mode face used for a mismatching paren."
91 :group 'faces
92 :group 'paren-showing)
94 (defvar show-paren-highlight-openparen t
95 "*Non-nil turns on openparen highlighting when matching forward.")
97 (defvar show-paren-idle-timer nil)
99 ;;;###autoload
100 (define-minor-mode show-paren-mode
101 "Toggle Show Paren mode.
102 With prefix ARG, turn Show Paren mode on if and only if ARG is positive.
103 Returns the new status of Show Paren mode (non-nil means on).
105 When Show Paren mode is enabled, any matching parenthesis is highlighted
106 in `show-paren-style' after `show-paren-delay' seconds of Emacs idle time."
107 :global t :group 'paren-showing
108 ;; Turn off the usual paren-matching method
109 ;; when this one is turned on.
110 (if (local-variable-p 'show-paren-mode)
111 (make-local-variable 'blink-matching-paren-on-screen)
112 (kill-local-variable 'blink-matching-paren-on-screen))
113 (setq blink-matching-paren-on-screen (not show-paren-mode))
115 ;; Now enable or disable the mechanism.
116 ;; First get rid of the old idle timer.
117 (if show-paren-idle-timer
118 (cancel-timer show-paren-idle-timer))
119 (setq show-paren-idle-timer nil)
120 ;; If show-paren-mode is enabled in some buffer now,
121 ;; set up a new timer.
122 (when (memq t (mapcar (lambda (buffer)
123 (with-current-buffer buffer
124 show-paren-mode))
125 (buffer-list)))
126 (setq show-paren-idle-timer (run-with-idle-timer
127 show-paren-delay t
128 'show-paren-function)))
129 (unless show-paren-mode
130 (and show-paren-overlay
131 (eq (overlay-buffer show-paren-overlay) (current-buffer))
132 (delete-overlay show-paren-overlay))
133 (and show-paren-overlay-1
134 (eq (overlay-buffer show-paren-overlay-1) (current-buffer))
135 (delete-overlay show-paren-overlay-1))))
137 ;; Find the place to show, if there is one,
138 ;; and show it until input arrives.
139 (defun show-paren-function ()
140 (if show-paren-mode
141 (let ((oldpos (point))
142 (dir (cond ((eq (car (syntax-after (1- (point)))) 5) -1)
143 ((eq (car (syntax-after (point))) 4) 1)))
144 pos mismatch face)
146 ;; Find the other end of the sexp.
147 (when dir
148 (save-excursion
149 (save-restriction
150 ;; Determine the range within which to look for a match.
151 (when blink-matching-paren-distance
152 (narrow-to-region
153 (max (point-min) (- (point) blink-matching-paren-distance))
154 (min (point-max) (+ (point) blink-matching-paren-distance))))
155 ;; Scan across one sexp within that range.
156 ;; Errors or nil mean there is a mismatch.
157 (condition-case ()
158 (setq pos (scan-sexps (point) dir))
159 (error (setq pos t mismatch t)))
160 ;; Move back the other way and verify we get back to the
161 ;; starting point. If not, these two parens don't really match.
162 ;; Maybe the one at point is escaped and doesn't really count.
163 (when (integerp pos)
164 (unless (condition-case ()
165 (eq (point) (scan-sexps pos (- dir)))
166 (error nil))
167 (setq pos nil)))
168 ;; If found a "matching" paren, see if it is the right
169 ;; kind of paren to match the one we started at.
170 (when (integerp pos)
171 (let ((beg (min pos oldpos)) (end (max pos oldpos)))
172 (when (/= (char-syntax (char-after beg)) ?\$)
173 (setq mismatch
174 (not (eq (char-before end)
175 ;; This can give nil.
176 (matching-paren (char-after beg)))))))))))
178 ;; Highlight the other end of the sexp, or unhighlight if none.
179 (if (not pos)
180 (progn
181 ;; If not at a paren that has a match,
182 ;; turn off any previous paren highlighting.
183 (and show-paren-overlay (overlay-buffer show-paren-overlay)
184 (delete-overlay show-paren-overlay))
185 (and show-paren-overlay-1 (overlay-buffer show-paren-overlay-1)
186 (delete-overlay show-paren-overlay-1)))
188 ;; Use the correct face.
189 (if mismatch
190 (progn
191 (if show-paren-ring-bell-on-mismatch
192 (beep))
193 (setq face 'show-paren-mismatch-face))
194 (setq face 'show-paren-match-face))
196 ;; If matching backwards, highlight the closeparen
197 ;; before point as well as its matching open.
198 ;; If matching forward, and the openparen is unbalanced,
199 ;; highlight the paren at point to indicate misbalance.
200 ;; Otherwise, turn off any such highlighting.
201 (if (and (not show-paren-highlight-openparen) (= dir 1) (integerp pos))
202 (when (and show-paren-overlay-1
203 (overlay-buffer show-paren-overlay-1))
204 (delete-overlay show-paren-overlay-1))
205 (let ((from (if (= dir 1)
206 (point)
207 (forward-point -1)))
208 (to (if (= dir 1)
209 (forward-point 1)
210 (point))))
211 (if show-paren-overlay-1
212 (move-overlay show-paren-overlay-1 from to (current-buffer))
213 (setq show-paren-overlay-1 (make-overlay from to)))
214 ;; Always set the overlay face, since it varies.
215 (overlay-put show-paren-overlay-1 'priority show-paren-priority)
216 (overlay-put show-paren-overlay-1 'face face)))
218 ;; Turn on highlighting for the matching paren, if found.
219 ;; If it's an unmatched paren, turn off any such highlighting.
220 (unless (integerp pos)
221 (delete-overlay show-paren-overlay))
222 (let ((to (if (or (eq show-paren-style 'expression)
223 (and (eq show-paren-style 'mixed)
224 (not (pos-visible-in-window-p pos))))
225 (point)
226 pos))
227 (from (if (or (eq show-paren-style 'expression)
228 (and (eq show-paren-style 'mixed)
229 (not (pos-visible-in-window-p pos))))
231 (save-excursion
232 (goto-char pos)
233 (forward-point (- dir))))))
234 (if show-paren-overlay
235 (move-overlay show-paren-overlay from to (current-buffer))
236 (setq show-paren-overlay (make-overlay from to))))
238 ;; Always set the overlay face, since it varies.
239 (overlay-put show-paren-overlay 'priority show-paren-priority)
240 (overlay-put show-paren-overlay 'face face)))
241 ;; show-paren-mode is nil in this buffer.
242 (and show-paren-overlay
243 (delete-overlay show-paren-overlay))
244 (and show-paren-overlay-1
245 (delete-overlay show-paren-overlay-1))))
247 (provide 'paren)
249 ;;; arch-tag: d0969b88-7ac0-4bd0-bd53-e73b892b86a9
250 ;;; paren.el ends here