1 ;;; paren.el --- highlight matching paren.
3 ;; Copyright (C) 1993 Free Software Foundation, Inc.
5 ;; Author: rms@gnu.ai.mit.edu
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)
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.
28 ;; Load this and it will display highlighting on whatever
29 ;; paren matches the one before or after point.
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
36 ;; right before point.
37 (defvar show-paren-overlay-1 nil
)
39 (defvar show-paren-mismatch-face nil
)
41 (defvar show-paren-face
'region
42 "*Name of face to use for showing the matching paren.")
44 ;; Find the place to show, if there is one,
45 ;; and show it until input arrives.
46 (defun show-paren-command-hook ()
47 ;; Do nothing if no window system to display results with.
48 ;; Do nothing if executing keyboard macro.
49 ;; Do nothing if input is pending.
51 (let (pos dir mismatch
(oldpos (point))
52 (face show-paren-face
))
53 (cond ((eq (char-syntax (preceding-char)) ?\
))
55 ((eq (char-syntax (following-char)) ?\
()
60 ;; Determine the range within which to look for a match.
61 (if blink-matching-paren-distance
62 (narrow-to-region (max (point-min)
63 (- (point) blink-matching-paren-distance
))
65 (+ (point) blink-matching-paren-distance
))))
66 ;; Scan across one sexp within that range.
68 (setq pos
(scan-sexps (point) dir
))
70 ;; See if the "matching" paren is the right kind of paren
71 ;; to match the one we started at.
73 (let ((beg (min pos oldpos
)) (end (max pos oldpos
)))
74 (and (/= (char-syntax (char-after beg
)) ?\$
)
76 (not (eq (char-after (1- end
))
78 (matching-paren (char-after beg
))))))))
79 ;; If they don't properly match, use a different face,
80 ;; or print a message.
83 (and (null show-paren-mismatch-face
)
86 (add-to-list 'facemenu-unlisted-faces
88 (make-face 'paren-mismatch
)
89 (or (face-nontrivial-p 'paren-mismatch t
)
91 (set-face-background 'paren-mismatch
93 (set-face-foreground 'paren-mismatch
95 (setq show-paren-mismatch-face
'paren-mismatch
)))
96 (if show-paren-mismatch-face
97 (setq face show-paren-mismatch-face
)
98 (message "Paren mismatch"))))
102 ;; If matching backwards, highlight the closeparen
103 ;; before point as well as its matching open.
105 (if show-paren-overlay-1
106 (move-overlay show-paren-overlay-1
107 (+ (point) dir
) (point)
109 (setq show-paren-overlay-1
110 (make-overlay (+ (point) dir
) (point))))
111 ;; Always set the overlay face, since it varies.
112 (overlay-put show-paren-overlay-1
'face face
))
113 ;; Otherwise, turn off any such highlighting.
114 (and show-paren-overlay-1
115 (overlay-buffer show-paren-overlay-1
)
116 (delete-overlay show-paren-overlay-1
)))
117 ;; Turn on highlighting for the matching paren.
118 (if show-paren-overlay
119 (move-overlay show-paren-overlay
(- pos dir
) pos
121 (setq show-paren-overlay
122 (make-overlay (- pos dir
) pos
)))
123 ;; Always set the overlay face, since it varies.
124 (overlay-put show-paren-overlay
'face face
))
126 ;; If not at a paren that has a match,
127 ;; turn off any previous paren highlighting.
128 (and show-paren-overlay
(overlay-buffer show-paren-overlay
)
129 (delete-overlay show-paren-overlay
))
130 (and show-paren-overlay-1
(overlay-buffer show-paren-overlay-1
)
131 (delete-overlay show-paren-overlay-1
)))))))
135 (setq blink-matching-paren-on-screen nil
)
136 (run-with-idle-timer .1 t
'show-paren-command-hook
)))
137 ;;; This is in case paren.el is preloaded.
138 (add-hook 'window-setup-hook
142 (setq blink-matching-paren-on-screen nil
)
143 (run-with-idle-timer .1 t
'show-paren-command-hook
))))))
146 ;;; paren.el ends here