(window-setup-hook): Add hook function.
[emacs.git] / lisp / paren.el
blob1c517bdf500817712a43ccd22f790ba6bfd0fa8b
1 ;;; paren.el --- highlight matching paren.
2 ;; Copyright (C) 1993 Free Software Foundation, Inc.
4 ;; Author: rms@gnu.ai.mit.edu
5 ;; Maintainer: FSF
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)
13 ;; any later version.
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.
24 ;;; Commentary:
26 ;; Load this and it will display highlighting on whatever
27 ;; paren matches the one before or after point.
29 ;;; Code:
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 (if window-system
46 (let (pos dir mismatch (oldpos (point))
47 (face show-paren-face))
48 (cond ((eq (char-syntax (following-char)) ?\()
49 (setq dir 1))
50 ((eq (char-syntax (preceding-char)) ?\))
51 (setq dir -1)))
52 (if dir
53 (save-excursion
54 (save-restriction
55 ;; Determine the range within which to look for a match.
56 (if blink-matching-paren-distance
57 (narrow-to-region (max (point-min)
58 (- (point) blink-matching-paren-distance))
59 (min (point-max)
60 (+ (point) blink-matching-paren-distance))))
61 ;; Scan across one sexp within that range.
62 (condition-case ()
63 (setq pos (scan-sexps (point) dir))
64 (error nil))
65 ;; See if the "matching" paren is the right kind of paren
66 ;; to match the one we started at.
67 (if pos
68 (let ((beg (min pos oldpos)) (end (max pos oldpos)))
69 (and (/= (char-syntax (char-after beg)) ?\$)
70 (setq mismatch
71 (/= (char-after (1- end))
72 (logand (lsh (aref (syntax-table)
73 (char-after beg))
74 -8)
75 255))))))
76 ;; If they don't properly match, use a different face,
77 ;; or print a message.
78 (if mismatch
79 (progn
80 (and (null show-paren-mismatch-face)
81 (x-display-color-p)
82 (or (setq show-paren-mismatch-face
83 (internal-find-face 'paren-mismatch))
84 (progn
85 (setq show-paren-mismatch-face
86 (make-face 'paren-mismatch))
87 (set-face-background 'paren-mismatch
88 "purple"))))
89 (if show-paren-mismatch-face
90 (setq face show-paren-mismatch-face)
91 (message "Paren mismatch"))))
92 )))
93 (cond (pos
94 (if (= dir -1)
95 ;; If matching backwards, highlight the closeparen
96 ;; before point as well as its matching open.
97 (progn
98 (if show-paren-overlay-1
99 (move-overlay show-paren-overlay-1
100 (+ (point) dir) (point)
101 (current-buffer))
102 (setq show-paren-overlay-1
103 (make-overlay (- pos dir) pos)))
104 (overlay-put show-paren-overlay-1 'face face))
105 ;; Otherwise, turn off any such highlighting.
106 (and show-paren-overlay-1
107 (overlay-buffer show-paren-overlay-1)
108 (delete-overlay show-paren-overlay-1)))
109 ;; Turn on highlighting for the matching paren.
110 (if show-paren-overlay
111 (move-overlay show-paren-overlay (- pos dir) pos
112 (current-buffer))
113 (setq show-paren-overlay
114 (make-overlay (- pos dir) pos)))
115 (overlay-put show-paren-overlay 'face face))
117 ;; If not at a paren that has a match,
118 ;; turn off any previous paren highlighting.
119 (and show-paren-overlay (overlay-buffer show-paren-overlay)
120 (delete-overlay show-paren-overlay))
121 (and show-paren-overlay-1 (overlay-buffer show-paren-overlay-1)
122 (delete-overlay show-paren-overlay-1)))))))
124 (if window-system
125 (progn
126 (setq blink-paren-function nil)
127 (add-hook 'post-command-hook 'show-paren-command-hook)))
128 ;;; This is in case paren.el is preloaded.
129 (add-hook 'window-setup-hook
130 (function (lambda ()
131 (if window-system
132 (progn
133 (setq blink-paren-function nil)
134 (add-hook 'post-command-hook
135 'show-paren-command-hook))))))
136 (provide 'paren)
138 ;;; paren.el ends here