1 ;;; paren.el --- highlight matching paren.
2 ;; Copyright (C) 1993 Free Software Foundation, Inc.
4 ;; Author: rms@gnu.ai.mit.edu
6 ;; Keywords: languages, faces
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
26 ;; Load this and it will display highlighting on whatever
27 ;; paren matches the one before or after point.
31 ;; This is the overlay used to highlight the matching paren.
32 (defvar show-paren-overlay nil
)
33 ;; This is the overlay used to highlight the closeparen
34 ;; right before point.
35 (defvar show-paren-overlay-1 nil
)
37 (defvar show-paren-mismatch-face nil
)
39 (defvar show-paren-face
'region
40 "*Name of face to use for showing the matching paren.")
42 ;; Find the place to show, if there is one,
43 ;; and show it until input arrives.
44 (defun show-paren-command-hook ()
45 ;; Do nothing if no window system to display results with.
46 ;; Do nothing if executing keyboard macro.
47 ;; Do nothing if input is pending.
48 (if (and window-system
(not executing-kbd-macro
) (sit-for 0))
49 (let (pos dir mismatch
(oldpos (point))
50 (face show-paren-face
))
51 (cond ((eq (char-syntax (following-char)) ?\
()
53 ((eq (char-syntax (preceding-char)) ?\
))
58 ;; Determine the range within which to look for a match.
59 (if blink-matching-paren-distance
60 (narrow-to-region (max (point-min)
61 (- (point) blink-matching-paren-distance
))
63 (+ (point) blink-matching-paren-distance
))))
64 ;; Scan across one sexp within that range.
66 (setq pos
(scan-sexps (point) dir
))
68 ;; See if the "matching" paren is the right kind of paren
69 ;; to match the one we started at.
71 (let ((beg (min pos oldpos
)) (end (max pos oldpos
)))
72 (and (/= (char-syntax (char-after beg
)) ?\$
)
74 (/= (char-after (1- end
))
75 (logand (lsh (aref (syntax-table)
79 ;; If they don't properly match, use a different face,
80 ;; or print a message.
83 (and (null show-paren-mismatch-face
)
85 (or (setq show-paren-mismatch-face
86 (internal-find-face 'paren-mismatch
))
88 (make-face 'paren-mismatch
)
89 (setq show-paren-mismatch-face
91 (set-face-background 'paren-mismatch
93 (if show-paren-mismatch-face
94 (setq face show-paren-mismatch-face
)
95 (message "Paren mismatch"))))
99 ;; If matching backwards, highlight the closeparen
100 ;; before point as well as its matching open.
102 (if show-paren-overlay-1
103 (move-overlay show-paren-overlay-1
104 (+ (point) dir
) (point)
106 (setq show-paren-overlay-1
107 (make-overlay (- pos dir
) pos
)))
108 (overlay-put show-paren-overlay-1
'face face
))
109 ;; Otherwise, turn off any such highlighting.
110 (and show-paren-overlay-1
111 (overlay-buffer show-paren-overlay-1
)
112 (delete-overlay show-paren-overlay-1
)))
113 ;; Turn on highlighting for the matching paren.
114 (if show-paren-overlay
115 (move-overlay show-paren-overlay
(- pos dir
) pos
117 (setq show-paren-overlay
118 (make-overlay (- pos dir
) pos
)))
119 (overlay-put show-paren-overlay
'face face
))
121 ;; If not at a paren that has a match,
122 ;; turn off any previous paren highlighting.
123 (and show-paren-overlay
(overlay-buffer show-paren-overlay
)
124 (delete-overlay show-paren-overlay
))
125 (and show-paren-overlay-1
(overlay-buffer show-paren-overlay-1
)
126 (delete-overlay show-paren-overlay-1
)))))))
130 (setq blink-paren-function nil
)
131 (add-hook 'post-command-hook
'show-paren-command-hook
)))
132 ;;; This is in case paren.el is preloaded.
133 (add-hook 'window-setup-hook
137 (setq blink-paren-function nil
)
138 (add-hook 'post-command-hook
139 'show-paren-command-hook
))))))
142 ;;; paren.el ends here