(LIBS): Link in shell32.lib.
[bpt/emacs.git] / lisp / paren.el
CommitLineData
5e28918b 1;;; paren.el --- highlight matching paren.
b578f267 2
673cc3c5 3;; Copyright (C) 1993, 1996 Free Software Foundation, Inc.
5e28918b 4
e4c37df7
JB
5;; Author: rms@gnu.ai.mit.edu
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
SM
43(defcustom show-paren-mode nil
44 "Toggle Show Paren mode.
45When Show Paren mode is enabled, any matching parenthesis is highlighted
46after `show-paren-delay' seconds of Emacs idle time.
47You must modify via \\[customize] for this variable to have an effect."
48 :set (lambda (symbol value)
49 (show-paren-mode (or value 0)))
12eb8433 50 :initialize 'custom-initialize-default
173f2341
SM
51 :type 'boolean
52 :group 'paren-showing
53 :require 'paren)
54
55(defcustom show-paren-style 'parenthesis
56 "*Style used when showing a matching paren.
57Valid styles are `parenthesis' (meaning show the matching paren),
58`expression' (meaning show the entire expression enclosed by the paren) and
59`mixed' (meaning show the matching paren if it is visible, and the expression
60otherwise)."
61 :type '(choice (const parenthesis) (const expression) (const mixed))
62 :group 'paren-showing)
63
64(defcustom show-paren-delay
65 (if (featurep 'lisp-float-type) (/ (float 1) (float 8)) 1)
66 "*Time in seconds to delay before showing a matching paren."
67 :type '(number :tag "seconds")
68 :group 'paren-showing)
69
70(defface show-paren-match-face
71 '((((class color)) (:background "turquoise"))
82b6a81f 72 (t (:background "gray")))
173f2341
SM
73 "Show Paren mode face used for a matching paren."
74 :group 'faces
75 :group 'paren-showing)
76
77(defface show-paren-mismatch-face
78 '((((class color)) (:foreground "white" :background "purple"))
79 (t (:reverse-video t)))
80 "Show Paren mode face used for a mismatching paren."
81 :group 'faces
82 :group 'paren-showing)
30322a27 83
12eb8433
RS
84(defvar show-paren-idle-timer nil)
85
86;;;###autoload
87(defun show-paren-mode (&optional arg)
88 "Toggle Show Paren mode.
89With prefix ARG, turn Show Paren mode on if and only if ARG is positive.
90Returns the new status of Show Paren mode (non-nil means on).
91
92When Show Paren mode is enabled, any matching parenthesis is highlighted
93in `show-paren-style' after `show-paren-delay' seconds of Emacs idle time."
94 (interactive "P")
95 (when window-system
96 (let ((on-p (if arg
97 (> (prefix-numeric-value arg) 0)
98 (not show-paren-mode))))
99 (setq blink-matching-paren-on-screen (not on-p))
100 (when show-paren-idle-timer
101 (cancel-timer show-paren-idle-timer))
102 (if on-p
103 (setq show-paren-idle-timer (run-with-idle-timer
104 show-paren-delay t
105 'show-paren-function))
106 (and show-paren-overlay (overlay-buffer show-paren-overlay)
107 (delete-overlay show-paren-overlay))
108 (and show-paren-overlay-1 (overlay-buffer show-paren-overlay-1)
109 (delete-overlay show-paren-overlay-1)))
110 (setq show-paren-mode on-p))))
111
d1475aa1
JB
112;; Find the place to show, if there is one,
113;; and show it until input arrives.
673cc3c5 114(defun show-paren-function ()
bac79b51 115 ;; Do nothing if no window system to display results with.
e4b93bab 116 ;; Do nothing if executing keyboard macro.
bac79b51 117 ;; Do nothing if input is pending.
173f2341
SM
118 (when window-system
119 (let (pos dir mismatch face (oldpos (point)))
120 (cond ((eq (char-syntax (preceding-char)) ?\))
121 (setq dir -1))
122 ((eq (char-syntax (following-char)) ?\()
123 (setq dir 1)))
124 ;;
125 ;; Find the other end of the sexp.
126 (when dir
127 (save-excursion
128 (save-restriction
129 ;; Determine the range within which to look for a match.
130 (when blink-matching-paren-distance
131 (narrow-to-region
132 (max (point-min) (- (point) blink-matching-paren-distance))
133 (min (point-max) (+ (point) blink-matching-paren-distance))))
134 ;; Scan across one sexp within that range.
135 ;; Errors or nil mean there is a mismatch.
136 (condition-case ()
137 (setq pos (scan-sexps (point) dir))
138 (error (setq pos t mismatch t)))
139 ;; If found a "matching" paren, see if it is the right
140 ;; kind of paren to match the one we started at.
141 (when (integerp pos)
142 (let ((beg (min pos oldpos)) (end (max pos oldpos)))
143 (when (/= (char-syntax (char-after beg)) ?\$)
144 (setq mismatch
145 (not (eq (char-before end)
146 ;; This can give nil.
147 (matching-paren (char-after beg)))))))))))
148 ;;
149 ;; Highlight the other end of the sexp, or unhighlight if none.
150 (if (not pos)
151 (progn
152 ;; If not at a paren that has a match,
153 ;; turn off any previous paren highlighting.
154 (and show-paren-overlay (overlay-buffer show-paren-overlay)
155 (delete-overlay show-paren-overlay))
156 (and show-paren-overlay-1 (overlay-buffer show-paren-overlay-1)
157 (delete-overlay show-paren-overlay-1)))
158 ;;
159 ;; Use the correct face.
160 (if mismatch
161 (setq face 'show-paren-mismatch-face)
162 (setq face 'show-paren-match-face))
163 ;;
164 ;; If matching backwards, highlight the closeparen
165 ;; before point as well as its matching open.
166 ;; If matching forward, and the openparen is unbalanced,
167 ;; highlight the paren at point to indicate misbalance.
168 ;; Otherwise, turn off any such highlighting.
169 (if (and (= dir 1) (integerp pos))
170 (when (and show-paren-overlay-1
171 (overlay-buffer show-paren-overlay-1))
172 (delete-overlay show-paren-overlay-1))
173 (let ((from (if (= dir 1)
174 (point)
175 (forward-point -1)))
176 (to (if (= dir 1)
177 (forward-point 1)
178 (point))))
179 (if show-paren-overlay-1
180 (move-overlay show-paren-overlay-1 from to (current-buffer))
181 (setq show-paren-overlay-1 (make-overlay from to)))
182 ;; Always set the overlay face, since it varies.
183 (overlay-put show-paren-overlay-1 'face face)))
184 ;;
185 ;; Turn on highlighting for the matching paren, if found.
186 ;; If it's an unmatched paren, turn off any such highlighting.
187 (unless (integerp pos)
188 (delete-overlay show-paren-overlay))
189 (let ((to (if (or (eq show-paren-style 'expression)
190 (and (eq show-paren-style 'mixed)
191 (not (pos-visible-in-window-p pos))))
192 (point)
193 pos))
194 (from (if (or (eq show-paren-style 'expression)
195 (and (eq show-paren-style 'mixed)
196 (not (pos-visible-in-window-p pos))))
197 pos
198 (save-excursion
199 (goto-char pos)
200 (forward-point (- dir))))))
201 (if show-paren-overlay
202 (move-overlay show-paren-overlay from to (current-buffer))
203 (setq show-paren-overlay (make-overlay from to))))
204 ;;
205 ;; Always set the overlay face, since it varies.
206 (overlay-put show-paren-overlay 'face face)))))
5e28918b 207
3a3236d2
JB
208(provide 'paren)
209
12eb8433
RS
210(if show-paren-mode
211 (show-paren-mode t))
212
3a3236d2 213;;; paren.el ends here