3912
|
1 ;;; paren.el --- highlight matching paren.
|
3913
|
2 ;; Copyright (C) 1993 Free Software Foundation, Inc.
|
3912
|
3
|
3977
|
4 ;; Author: rms@gnu.ai.mit.edu
|
|
5 ;; Maintainer: FSF
|
|
6 ;; Keywords: languages, faces
|
3976
|
7
|
3912
|
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
|
3917
|
31 (defvar show-paren-overlay nil)
|
3912
|
32
|
3917
|
33 ;; Find the place to show, if there is one,
|
|
34 ;; and show it until input arrives.
|
|
35 (defun show-paren-command-hook ()
|
3912
|
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))))))
|
3917
|
66 ;; If they don't properly match, don't show.
|
3912
|
67 (if mismatch
|
3976
|
68 (progn
|
|
69 (message "Paren mismatch")
|
|
70 ;;; (setq pos nil)
|
|
71 ))))
|
3912
|
72 (cond (pos
|
3917
|
73 (if show-paren-overlay
|
|
74 (move-overlay show-paren-overlay (- pos dir) pos)
|
|
75 (setq show-paren-overlay
|
3912
|
76 (make-overlay (- pos dir) pos)))
|
3917
|
77 (overlay-put show-paren-overlay 'face face)
|
3912
|
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)
|
3917
|
82 ;;; (overlay-put show-paren-overlay
|
3912
|
83 ;;; 'face
|
3917
|
84 ;;; (if (overlay-get show-paren-overlay
|
3912
|
85 ;;; 'face)
|
|
86 ;;; nil face)))
|
|
87 )
|
|
88 (t
|
3917
|
89 (and show-paren-overlay (overlay-buffer show-paren-overlay)
|
|
90 (delete-overlay show-paren-overlay))))))
|
3912
|
91
|
3917
|
92 (add-hook 'post-command-hook 'show-paren-command-hook)
|
3912
|
93
|
3919
|
94 (provide 'paren)
|
|
95
|
|
96 ;;; paren.el ends here
|