*** empty log message ***
[bpt/emacs.git] / lisp / paren.el
CommitLineData
5e28918b 1;;; paren.el --- highlight matching paren.
e788f291 2;; Copyright (C) 1993 Free Software Foundation, Inc.
5e28918b 3
5e2325c9
JB
4;;; Author: rms@gnu.ai.mit.edu
5;;; Maintainer: FSF
6;;; Keywords: languages, faces
7
5e28918b
JB
8;; This file is part of GNU Emacs.
9
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.
14
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.
19
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.
23
24;;; Commentary:
25
26;; Load this and it will display highlighting on whatever
27;; paren matches the one before or after point.
28
29;;; Code:
30
d1475aa1 31(defvar show-paren-overlay nil)
5e28918b 32
d1475aa1
JB
33;; Find the place to show, if there is one,
34;; and show it until input arrives.
35(defun show-paren-command-hook ()
5e28918b
JB
36 (let (pos dir mismatch (oldpos (point))
37 (face (if (face-equal 'highlight 'region)
38 'underline 'highlight)))
39 (cond ((eq (char-syntax (following-char)) ?\()
40 (setq dir 1))
41 ((eq (char-syntax (preceding-char)) ?\))
42 (setq dir -1)))
43 (save-excursion
44 (save-restriction
45 ;; Determine the range within which to look for a match.
46 (if blink-matching-paren-distance
47 (narrow-to-region (max (point-min)
48 (- (point) blink-matching-paren-distance))
49 (min (point-max)
50 (+ (point) blink-matching-paren-distance))))
51 ;; Scan across one sexp within that range.
52 (condition-case ()
53 (setq pos (scan-sexps (point) dir))
54 (error nil))
55 ;; See if the "matching" paren is the right kind of paren
56 ;; to match the one we started at.
57 (if pos
58 (let ((beg (min pos oldpos)) (end (max pos oldpos)))
59 (and (/= (char-syntax (char-after beg)) ?\$)
60 (setq mismatch
61 (/= (char-after (1- end))
62 (logand (lsh (aref (syntax-table)
63 (char-after beg))
64 -8)
65 255))))))
d1475aa1 66 ;; If they don't properly match, don't show.
5e28918b 67 (if mismatch
5e2325c9
JB
68 (progn
69 (message "Paren mismatch")
70;;; (setq pos nil)
71 ))))
5e28918b 72 (cond (pos
d1475aa1
JB
73 (if show-paren-overlay
74 (move-overlay show-paren-overlay (- pos dir) pos)
75 (setq show-paren-overlay
5e28918b 76 (make-overlay (- pos dir) pos)))
d1475aa1 77 (overlay-put show-paren-overlay 'face face)
5e28918b
JB
78;;; This is code to blink the highlighting.
79;;; It is desirable to avoid this because
80;;; it would interfere with auto-save and gc when idle.
81;;; (while (sit-for 1)
d1475aa1 82;;; (overlay-put show-paren-overlay
5e28918b 83;;; 'face
d1475aa1 84;;; (if (overlay-get show-paren-overlay
5e28918b
JB
85;;; 'face)
86;;; nil face)))
87 )
88 (t
d1475aa1
JB
89 (and show-paren-overlay (overlay-buffer show-paren-overlay)
90 (delete-overlay show-paren-overlay))))))
5e28918b 91
d1475aa1 92(add-hook 'post-command-hook 'show-paren-command-hook)
5e28918b 93
3a3236d2
JB
94(provide 'paren)
95
96;;; paren.el ends here