Write fns-*.el in current directory instead of
[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
2fe66dda
RS
70(defcustom show-paren-ring-bell-on-mismatch nil
71 "*If non-nil, beep if mismatched paren is detected."
72 :type 'boolean
73 :group 'paren-showing
74 :version "20.3")
75
173f2341
SM
76(defface show-paren-match-face
77 '((((class color)) (:background "turquoise"))
82b6a81f 78 (t (:background "gray")))
173f2341
SM
79 "Show Paren mode face used for a matching paren."
80 :group 'faces
81 :group 'paren-showing)
82
83(defface show-paren-mismatch-face
84 '((((class color)) (:foreground "white" :background "purple"))
85 (t (:reverse-video t)))
86 "Show Paren mode face used for a mismatching paren."
87 :group 'faces
88 :group 'paren-showing)
30322a27 89
12eb8433
RS
90(defvar show-paren-idle-timer nil)
91
92;;;###autoload
93(defun show-paren-mode (&optional arg)
94 "Toggle Show Paren mode.
95With prefix ARG, turn Show Paren mode on if and only if ARG is positive.
96Returns the new status of Show Paren mode (non-nil means on).
97
98When Show Paren mode is enabled, any matching parenthesis is highlighted
99in `show-paren-style' after `show-paren-delay' seconds of Emacs idle time."
100 (interactive "P")
101 (when window-system
102 (let ((on-p (if arg
103 (> (prefix-numeric-value arg) 0)
104 (not show-paren-mode))))
105 (setq blink-matching-paren-on-screen (not on-p))
106 (when show-paren-idle-timer
107 (cancel-timer show-paren-idle-timer))
108 (if on-p
109 (setq show-paren-idle-timer (run-with-idle-timer
110 show-paren-delay t
111 'show-paren-function))
112 (and show-paren-overlay (overlay-buffer show-paren-overlay)
113 (delete-overlay show-paren-overlay))
114 (and show-paren-overlay-1 (overlay-buffer show-paren-overlay-1)
115 (delete-overlay show-paren-overlay-1)))
116 (setq show-paren-mode on-p))))
117
d1475aa1
JB
118;; Find the place to show, if there is one,
119;; and show it until input arrives.
673cc3c5 120(defun show-paren-function ()
bac79b51 121 ;; Do nothing if no window system to display results with.
e4b93bab 122 ;; Do nothing if executing keyboard macro.
bac79b51 123 ;; Do nothing if input is pending.
173f2341
SM
124 (when window-system
125 (let (pos dir mismatch face (oldpos (point)))
126 (cond ((eq (char-syntax (preceding-char)) ?\))
127 (setq dir -1))
128 ((eq (char-syntax (following-char)) ?\()
129 (setq dir 1)))
130 ;;
131 ;; Find the other end of the sexp.
132 (when dir
133 (save-excursion
134 (save-restriction
135 ;; Determine the range within which to look for a match.
136 (when blink-matching-paren-distance
137 (narrow-to-region
138 (max (point-min) (- (point) blink-matching-paren-distance))
139 (min (point-max) (+ (point) blink-matching-paren-distance))))
140 ;; Scan across one sexp within that range.
141 ;; Errors or nil mean there is a mismatch.
142 (condition-case ()
143 (setq pos (scan-sexps (point) dir))
144 (error (setq pos t mismatch t)))
145 ;; If found a "matching" paren, see if it is the right
146 ;; kind of paren to match the one we started at.
147 (when (integerp pos)
148 (let ((beg (min pos oldpos)) (end (max pos oldpos)))
149 (when (/= (char-syntax (char-after beg)) ?\$)
150 (setq mismatch
151 (not (eq (char-before end)
152 ;; This can give nil.
153 (matching-paren (char-after beg)))))))))))
154 ;;
155 ;; Highlight the other end of the sexp, or unhighlight if none.
156 (if (not pos)
157 (progn
158 ;; If not at a paren that has a match,
159 ;; turn off any previous paren highlighting.
160 (and show-paren-overlay (overlay-buffer show-paren-overlay)
161 (delete-overlay show-paren-overlay))
162 (and show-paren-overlay-1 (overlay-buffer show-paren-overlay-1)
163 (delete-overlay show-paren-overlay-1)))
164 ;;
165 ;; Use the correct face.
166 (if mismatch
2fe66dda
RS
167 (progn
168 (if show-paren-ring-bell-on-mismatch
169 (beep))
170 (setq face 'show-paren-mismatch-face))
173f2341
SM
171 (setq face 'show-paren-match-face))
172 ;;
173 ;; If matching backwards, highlight the closeparen
174 ;; before point as well as its matching open.
175 ;; If matching forward, and the openparen is unbalanced,
176 ;; highlight the paren at point to indicate misbalance.
177 ;; Otherwise, turn off any such highlighting.
178 (if (and (= dir 1) (integerp pos))
179 (when (and show-paren-overlay-1
180 (overlay-buffer show-paren-overlay-1))
181 (delete-overlay show-paren-overlay-1))
182 (let ((from (if (= dir 1)
183 (point)
184 (forward-point -1)))
185 (to (if (= dir 1)
186 (forward-point 1)
187 (point))))
188 (if show-paren-overlay-1
189 (move-overlay show-paren-overlay-1 from to (current-buffer))
190 (setq show-paren-overlay-1 (make-overlay from to)))
191 ;; Always set the overlay face, since it varies.
192 (overlay-put show-paren-overlay-1 'face face)))
193 ;;
194 ;; Turn on highlighting for the matching paren, if found.
195 ;; If it's an unmatched paren, turn off any such highlighting.
196 (unless (integerp pos)
197 (delete-overlay show-paren-overlay))
198 (let ((to (if (or (eq show-paren-style 'expression)
199 (and (eq show-paren-style 'mixed)
200 (not (pos-visible-in-window-p pos))))
201 (point)
202 pos))
203 (from (if (or (eq show-paren-style 'expression)
204 (and (eq show-paren-style 'mixed)
205 (not (pos-visible-in-window-p pos))))
206 pos
207 (save-excursion
208 (goto-char pos)
209 (forward-point (- dir))))))
210 (if show-paren-overlay
211 (move-overlay show-paren-overlay from to (current-buffer))
212 (setq show-paren-overlay (make-overlay from to))))
213 ;;
214 ;; Always set the overlay face, since it varies.
215 (overlay-put show-paren-overlay 'face face)))))
5e28918b 216
3a3236d2
JB
217(provide 'paren)
218
12eb8433
RS
219(if show-paren-mode
220 (show-paren-mode t))
221
3a3236d2 222;;; paren.el ends here