annotate lisp/paren.el @ 4073:c476d93af94b

*** empty log message ***
author Jim Meyering <jim@meyering.net>
date Tue, 13 Jul 1993 16:37:01 +0000
parents 66d33e1bf65b
children b05c50e08993
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3912
676fb29d1692 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1 ;;; paren.el --- highlight matching paren.
3913
d0f9cd48e8f6 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 3912
diff changeset
2 ;; Copyright (C) 1993 Free Software Foundation, Inc.
3912
676fb29d1692 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
3
3977
bb696888ccb7 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 3976
diff changeset
4 ;; Author: rms@gnu.ai.mit.edu
bb696888ccb7 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 3976
diff changeset
5 ;; Maintainer: FSF
bb696888ccb7 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 3976
diff changeset
6 ;; Keywords: languages, faces
3976
87addb9e55f8 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 3919
diff changeset
7
3912
676fb29d1692 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
8 ;; This file is part of GNU Emacs.
676fb29d1692 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
9
676fb29d1692 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
676fb29d1692 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
11 ;; it under the terms of the GNU General Public License as published by
676fb29d1692 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
12 ;; the Free Software Foundation; either version 2, or (at your option)
676fb29d1692 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
13 ;; any later version.
676fb29d1692 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
14
676fb29d1692 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
15 ;; GNU Emacs is distributed in the hope that it will be useful,
676fb29d1692 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
676fb29d1692 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
676fb29d1692 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
18 ;; GNU General Public License for more details.
676fb29d1692 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
19
676fb29d1692 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
20 ;; You should have received a copy of the GNU General Public License
676fb29d1692 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
21 ;; along with GNU Emacs; see the file COPYING. If not, write to
676fb29d1692 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
676fb29d1692 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
23
676fb29d1692 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
24 ;;; Commentary:
676fb29d1692 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
25
676fb29d1692 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
26 ;; Load this and it will display highlighting on whatever
676fb29d1692 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
27 ;; paren matches the one before or after point.
676fb29d1692 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
28
676fb29d1692 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
29 ;;; Code:
676fb29d1692 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
30
3917
00ea0fc45685 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 3913
diff changeset
31 (defvar show-paren-overlay nil)
3912
676fb29d1692 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
32
3917
00ea0fc45685 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 3913
diff changeset
33 ;; Find the place to show, if there is one,
00ea0fc45685 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 3913
diff changeset
34 ;; and show it until input arrives.
00ea0fc45685 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 3913
diff changeset
35 (defun show-paren-command-hook ()
4059
66d33e1bf65b (show-paren-command-hook): Do nothing if not window-system.
Richard M. Stallman <rms@gnu.org>
parents: 3977
diff changeset
36 (if window-system
66d33e1bf65b (show-paren-command-hook): Do nothing if not window-system.
Richard M. Stallman <rms@gnu.org>
parents: 3977
diff changeset
37 (let (pos dir mismatch (oldpos (point))
66d33e1bf65b (show-paren-command-hook): Do nothing if not window-system.
Richard M. Stallman <rms@gnu.org>
parents: 3977
diff changeset
38 (face (if (face-equal 'highlight 'region)
66d33e1bf65b (show-paren-command-hook): Do nothing if not window-system.
Richard M. Stallman <rms@gnu.org>
parents: 3977
diff changeset
39 'underline 'highlight)))
66d33e1bf65b (show-paren-command-hook): Do nothing if not window-system.
Richard M. Stallman <rms@gnu.org>
parents: 3977
diff changeset
40 (cond ((eq (char-syntax (following-char)) ?\()
66d33e1bf65b (show-paren-command-hook): Do nothing if not window-system.
Richard M. Stallman <rms@gnu.org>
parents: 3977
diff changeset
41 (setq dir 1))
66d33e1bf65b (show-paren-command-hook): Do nothing if not window-system.
Richard M. Stallman <rms@gnu.org>
parents: 3977
diff changeset
42 ((eq (char-syntax (preceding-char)) ?\))
66d33e1bf65b (show-paren-command-hook): Do nothing if not window-system.
Richard M. Stallman <rms@gnu.org>
parents: 3977
diff changeset
43 (setq dir -1)))
66d33e1bf65b (show-paren-command-hook): Do nothing if not window-system.
Richard M. Stallman <rms@gnu.org>
parents: 3977
diff changeset
44 (save-excursion
66d33e1bf65b (show-paren-command-hook): Do nothing if not window-system.
Richard M. Stallman <rms@gnu.org>
parents: 3977
diff changeset
45 (save-restriction
66d33e1bf65b (show-paren-command-hook): Do nothing if not window-system.
Richard M. Stallman <rms@gnu.org>
parents: 3977
diff changeset
46 ;; Determine the range within which to look for a match.
66d33e1bf65b (show-paren-command-hook): Do nothing if not window-system.
Richard M. Stallman <rms@gnu.org>
parents: 3977
diff changeset
47 (if blink-matching-paren-distance
66d33e1bf65b (show-paren-command-hook): Do nothing if not window-system.
Richard M. Stallman <rms@gnu.org>
parents: 3977
diff changeset
48 (narrow-to-region (max (point-min)
66d33e1bf65b (show-paren-command-hook): Do nothing if not window-system.
Richard M. Stallman <rms@gnu.org>
parents: 3977
diff changeset
49 (- (point) blink-matching-paren-distance))
66d33e1bf65b (show-paren-command-hook): Do nothing if not window-system.
Richard M. Stallman <rms@gnu.org>
parents: 3977
diff changeset
50 (min (point-max)
66d33e1bf65b (show-paren-command-hook): Do nothing if not window-system.
Richard M. Stallman <rms@gnu.org>
parents: 3977
diff changeset
51 (+ (point) blink-matching-paren-distance))))
66d33e1bf65b (show-paren-command-hook): Do nothing if not window-system.
Richard M. Stallman <rms@gnu.org>
parents: 3977
diff changeset
52 ;; Scan across one sexp within that range.
66d33e1bf65b (show-paren-command-hook): Do nothing if not window-system.
Richard M. Stallman <rms@gnu.org>
parents: 3977
diff changeset
53 (condition-case ()
66d33e1bf65b (show-paren-command-hook): Do nothing if not window-system.
Richard M. Stallman <rms@gnu.org>
parents: 3977
diff changeset
54 (setq pos (scan-sexps (point) dir))
66d33e1bf65b (show-paren-command-hook): Do nothing if not window-system.
Richard M. Stallman <rms@gnu.org>
parents: 3977
diff changeset
55 (error nil))
66d33e1bf65b (show-paren-command-hook): Do nothing if not window-system.
Richard M. Stallman <rms@gnu.org>
parents: 3977
diff changeset
56 ;; See if the "matching" paren is the right kind of paren
66d33e1bf65b (show-paren-command-hook): Do nothing if not window-system.
Richard M. Stallman <rms@gnu.org>
parents: 3977
diff changeset
57 ;; to match the one we started at.
66d33e1bf65b (show-paren-command-hook): Do nothing if not window-system.
Richard M. Stallman <rms@gnu.org>
parents: 3977
diff changeset
58 (if pos
66d33e1bf65b (show-paren-command-hook): Do nothing if not window-system.
Richard M. Stallman <rms@gnu.org>
parents: 3977
diff changeset
59 (let ((beg (min pos oldpos)) (end (max pos oldpos)))
66d33e1bf65b (show-paren-command-hook): Do nothing if not window-system.
Richard M. Stallman <rms@gnu.org>
parents: 3977
diff changeset
60 (and (/= (char-syntax (char-after beg)) ?\$)
66d33e1bf65b (show-paren-command-hook): Do nothing if not window-system.
Richard M. Stallman <rms@gnu.org>
parents: 3977
diff changeset
61 (setq mismatch
66d33e1bf65b (show-paren-command-hook): Do nothing if not window-system.
Richard M. Stallman <rms@gnu.org>
parents: 3977
diff changeset
62 (/= (char-after (1- end))
66d33e1bf65b (show-paren-command-hook): Do nothing if not window-system.
Richard M. Stallman <rms@gnu.org>
parents: 3977
diff changeset
63 (logand (lsh (aref (syntax-table)
66d33e1bf65b (show-paren-command-hook): Do nothing if not window-system.
Richard M. Stallman <rms@gnu.org>
parents: 3977
diff changeset
64 (char-after beg))
66d33e1bf65b (show-paren-command-hook): Do nothing if not window-system.
Richard M. Stallman <rms@gnu.org>
parents: 3977
diff changeset
65 -8)
66d33e1bf65b (show-paren-command-hook): Do nothing if not window-system.
Richard M. Stallman <rms@gnu.org>
parents: 3977
diff changeset
66 255))))))
66d33e1bf65b (show-paren-command-hook): Do nothing if not window-system.
Richard M. Stallman <rms@gnu.org>
parents: 3977
diff changeset
67 ;; If they don't properly match, don't show.
66d33e1bf65b (show-paren-command-hook): Do nothing if not window-system.
Richard M. Stallman <rms@gnu.org>
parents: 3977
diff changeset
68 (if mismatch
66d33e1bf65b (show-paren-command-hook): Do nothing if not window-system.
Richard M. Stallman <rms@gnu.org>
parents: 3977
diff changeset
69 (progn
66d33e1bf65b (show-paren-command-hook): Do nothing if not window-system.
Richard M. Stallman <rms@gnu.org>
parents: 3977
diff changeset
70 (message "Paren mismatch")
66d33e1bf65b (show-paren-command-hook): Do nothing if not window-system.
Richard M. Stallman <rms@gnu.org>
parents: 3977
diff changeset
71 ;;; (setq pos nil)
66d33e1bf65b (show-paren-command-hook): Do nothing if not window-system.
Richard M. Stallman <rms@gnu.org>
parents: 3977
diff changeset
72 ))))
66d33e1bf65b (show-paren-command-hook): Do nothing if not window-system.
Richard M. Stallman <rms@gnu.org>
parents: 3977
diff changeset
73 (cond (pos
66d33e1bf65b (show-paren-command-hook): Do nothing if not window-system.
Richard M. Stallman <rms@gnu.org>
parents: 3977
diff changeset
74 (if show-paren-overlay
66d33e1bf65b (show-paren-command-hook): Do nothing if not window-system.
Richard M. Stallman <rms@gnu.org>
parents: 3977
diff changeset
75 (move-overlay show-paren-overlay (- pos dir) pos)
66d33e1bf65b (show-paren-command-hook): Do nothing if not window-system.
Richard M. Stallman <rms@gnu.org>
parents: 3977
diff changeset
76 (setq show-paren-overlay
66d33e1bf65b (show-paren-command-hook): Do nothing if not window-system.
Richard M. Stallman <rms@gnu.org>
parents: 3977
diff changeset
77 (make-overlay (- pos dir) pos)))
66d33e1bf65b (show-paren-command-hook): Do nothing if not window-system.
Richard M. Stallman <rms@gnu.org>
parents: 3977
diff changeset
78 (overlay-put show-paren-overlay 'face face)
66d33e1bf65b (show-paren-command-hook): Do nothing if not window-system.
Richard M. Stallman <rms@gnu.org>
parents: 3977
diff changeset
79 ;;; This is code to blink the highlighting.
66d33e1bf65b (show-paren-command-hook): Do nothing if not window-system.
Richard M. Stallman <rms@gnu.org>
parents: 3977
diff changeset
80 ;;; It is desirable to avoid this because
66d33e1bf65b (show-paren-command-hook): Do nothing if not window-system.
Richard M. Stallman <rms@gnu.org>
parents: 3977
diff changeset
81 ;;; it would interfere with auto-save and gc when idle.
3912
676fb29d1692 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
82 ;;; (while (sit-for 1)
3917
00ea0fc45685 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 3913
diff changeset
83 ;;; (overlay-put show-paren-overlay
3912
676fb29d1692 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
84 ;;; 'face
3917
00ea0fc45685 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 3913
diff changeset
85 ;;; (if (overlay-get show-paren-overlay
3912
676fb29d1692 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
86 ;;; 'face)
676fb29d1692 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
87 ;;; nil face)))
4059
66d33e1bf65b (show-paren-command-hook): Do nothing if not window-system.
Richard M. Stallman <rms@gnu.org>
parents: 3977
diff changeset
88 )
66d33e1bf65b (show-paren-command-hook): Do nothing if not window-system.
Richard M. Stallman <rms@gnu.org>
parents: 3977
diff changeset
89 (t
66d33e1bf65b (show-paren-command-hook): Do nothing if not window-system.
Richard M. Stallman <rms@gnu.org>
parents: 3977
diff changeset
90 (and show-paren-overlay (overlay-buffer show-paren-overlay)
66d33e1bf65b (show-paren-command-hook): Do nothing if not window-system.
Richard M. Stallman <rms@gnu.org>
parents: 3977
diff changeset
91 (delete-overlay show-paren-overlay)))))))
3912
676fb29d1692 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
92
3917
00ea0fc45685 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 3913
diff changeset
93 (add-hook 'post-command-hook 'show-paren-command-hook)
3912
676fb29d1692 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
94
3919
bbac27613cee *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 3917
diff changeset
95 (provide 'paren)
bbac27613cee *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 3917
diff changeset
96
bbac27613cee *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 3917
diff changeset
97 ;;; paren.el ends here