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