(bookmark-locate): ;;;###autoload this alias.
[emacs.git] / lisp / paren.el
blob810fa691980cb2bf14e7f59250d07d43086bbde2
1 ;;; paren.el --- highlight matching paren.
3 ;; Copyright (C) 1993, 1996 Free Software Foundation, Inc.
5 ;; Author: rms@gnu.ai.mit.edu
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 ;; Load this and it will display highlighting on whatever
29 ;; paren matches the one before or after point.
31 ;;; Code:
33 ;; This is the overlay used to highlight the matching paren.
34 (defvar show-paren-overlay nil)
35 ;; This is the overlay used to highlight the closeparen right before point.
36 (defvar show-paren-overlay-1 nil)
38 (defvar show-paren-mode nil)
39 (defvar show-paren-idle-timer nil)
41 (defvar show-paren-mismatch-face nil)
43 (defvar show-paren-delay (if (featurep 'lisp-float-type) 0.125 1)
44 "*Time in seconds to delay before showing the matching paren.")
46 (defvar show-paren-face 'region
47 "*Name of the face to use for showing the matching paren.")
49 ;;;###autoload
50 (defun show-paren-mode (&optional arg)
51 "Toggle Show Paren mode.
52 With prefix ARG, turn Show Paren mode on if and only if ARG is positive.
53 Returns the new status of Show Paren mode (non-nil means on).
55 When Show Paren mode is enabled, any matching parenthesis is highlighted
56 after `show-paren-delay' seconds of Emacs idle time."
57 (interactive "P")
58 (if window-system
59 (let ((on-p (if arg
60 (> (prefix-numeric-value arg) 0)
61 (not show-paren-mode))))
62 (setq blink-matching-paren-on-screen (not on-p))
63 (and show-paren-idle-timer (cancel-timer show-paren-idle-timer))
64 (if on-p
65 (setq show-paren-idle-timer (run-with-idle-timer 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))))
73 ;; Find the place to show, if there is one,
74 ;; and show it until input arrives.
75 (defun show-paren-function ()
76 ;; Do nothing if no window system to display results with.
77 ;; Do nothing if executing keyboard macro.
78 ;; Do nothing if input is pending.
79 (if window-system
80 (let (pos dir mismatch (oldpos (point))
81 (face show-paren-face))
82 (cond ((eq (char-syntax (preceding-char)) ?\))
83 (setq dir -1))
84 ((eq (char-syntax (following-char)) ?\()
85 (setq dir 1)))
86 (if dir
87 (save-excursion
88 (save-restriction
89 ;; Determine the range within which to look for a match.
90 (if blink-matching-paren-distance
91 (narrow-to-region (max (point-min)
92 (- (point) blink-matching-paren-distance))
93 (min (point-max)
94 (+ (point) blink-matching-paren-distance))))
95 ;; Scan across one sexp within that range.
96 (condition-case ()
97 (setq pos (scan-sexps (point) dir))
98 (error nil))
99 ;; See if the "matching" paren is the right kind of paren
100 ;; to match the one we started at.
101 (if pos
102 (let ((beg (min pos oldpos)) (end (max pos oldpos)))
103 (and (/= (char-syntax (char-after beg)) ?\$)
104 (setq mismatch
105 (not (eq (char-after (1- end))
106 ;; This can give nil.
107 (matching-paren (char-after beg))))))))
108 ;; If they don't properly match, use a different face,
109 ;; or print a message.
110 (if mismatch
111 (progn
112 (and (null show-paren-mismatch-face)
113 (x-display-color-p)
114 (progn
115 (add-to-list 'facemenu-unlisted-faces
116 'paren-mismatch)
117 (make-face 'paren-mismatch)
118 (or (face-nontrivial-p 'paren-mismatch t)
119 (progn
120 (set-face-background 'paren-mismatch
121 "purple")
122 (set-face-foreground 'paren-mismatch
123 "white")))
124 (setq show-paren-mismatch-face 'paren-mismatch)))
125 (if show-paren-mismatch-face
126 (setq face show-paren-mismatch-face)
127 (message "Paren mismatch"))))
129 (cond (pos
130 (if (= dir -1)
131 ;; If matching backwards, highlight the closeparen
132 ;; before point as well as its matching open.
133 (progn
134 (if show-paren-overlay-1
135 (move-overlay show-paren-overlay-1
136 (+ (point) dir) (point)
137 (current-buffer))
138 (setq show-paren-overlay-1
139 (make-overlay (+ (point) dir) (point))))
140 ;; Always set the overlay face, since it varies.
141 (overlay-put show-paren-overlay-1 'face face))
142 ;; Otherwise, turn off any such highlighting.
143 (and show-paren-overlay-1
144 (overlay-buffer show-paren-overlay-1)
145 (delete-overlay show-paren-overlay-1)))
146 ;; Turn on highlighting for the matching paren.
147 (if show-paren-overlay
148 (move-overlay show-paren-overlay (- pos dir) pos
149 (current-buffer))
150 (setq show-paren-overlay
151 (make-overlay (- pos dir) pos)))
152 ;; Always set the overlay face, since it varies.
153 (overlay-put show-paren-overlay 'face face))
155 ;; If not at a paren that has a match,
156 ;; turn off any previous paren highlighting.
157 (and show-paren-overlay (overlay-buffer show-paren-overlay)
158 (delete-overlay show-paren-overlay))
159 (and show-paren-overlay-1 (overlay-buffer show-paren-overlay-1)
160 (delete-overlay show-paren-overlay-1)))))))
162 ;;; For back compatibility we turn ourselves on if we're dumped or loaded.
163 (add-hook 'window-setup-hook '(lambda () (show-paren-mode t)))
164 (show-paren-mode t)
166 (provide 'paren)
168 ;;; paren.el ends here