annotate lisp/paren.el @ 4183:a5f0a739d87d

(show-paren-face): New variable. (show-paren-command-hook): Use it. Call set-face-background properly.
author Richard M. Stallman <rms@gnu.org>
date Tue, 20 Jul 1993 05:59:51 +0000
parents b05c50e08993
children d4e116f25030
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
4123
b05c50e08993 Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents: 4059
diff changeset
31 ;; This is the overlay used to highlight the matching paren.
3917
00ea0fc45685 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 3913
diff changeset
32 (defvar show-paren-overlay nil)
4123
b05c50e08993 Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents: 4059
diff changeset
33 ;; This is the overlay used to highlight the closeparen
b05c50e08993 Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents: 4059
diff changeset
34 ;; right before point.
b05c50e08993 Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents: 4059
diff changeset
35 (defvar show-paren-overlay-1 nil)
b05c50e08993 Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents: 4059
diff changeset
36
b05c50e08993 Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents: 4059
diff changeset
37 (defvar show-paren-mismatch-face nil)
3912
676fb29d1692 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
38
4183
a5f0a739d87d (show-paren-face): New variable.
Richard M. Stallman <rms@gnu.org>
parents: 4123
diff changeset
39 (defvar show-paren-face 'region
a5f0a739d87d (show-paren-face): New variable.
Richard M. Stallman <rms@gnu.org>
parents: 4123
diff changeset
40 "*Name of face to use for showing the matching paren.")
a5f0a739d87d (show-paren-face): New variable.
Richard M. Stallman <rms@gnu.org>
parents: 4123
diff changeset
41
3917
00ea0fc45685 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 3913
diff changeset
42 ;; Find the place to show, if there is one,
00ea0fc45685 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 3913
diff changeset
43 ;; and show it until input arrives.
00ea0fc45685 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 3913
diff changeset
44 (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
45 (if window-system
66d33e1bf65b (show-paren-command-hook): Do nothing if not window-system.
Richard M. Stallman <rms@gnu.org>
parents: 3977
diff changeset
46 (let (pos dir mismatch (oldpos (point))
4183
a5f0a739d87d (show-paren-face): New variable.
Richard M. Stallman <rms@gnu.org>
parents: 4123
diff changeset
47 (face show-paren-face))
4059
66d33e1bf65b (show-paren-command-hook): Do nothing if not window-system.
Richard M. Stallman <rms@gnu.org>
parents: 3977
diff changeset
48 (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
49 (setq dir 1))
66d33e1bf65b (show-paren-command-hook): Do nothing if not window-system.
Richard M. Stallman <rms@gnu.org>
parents: 3977
diff changeset
50 ((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
51 (setq dir -1)))
4123
b05c50e08993 Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents: 4059
diff changeset
52 (if dir
b05c50e08993 Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents: 4059
diff changeset
53 (save-excursion
b05c50e08993 Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents: 4059
diff changeset
54 (save-restriction
b05c50e08993 Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents: 4059
diff changeset
55 ;; Determine the range within which to look for a match.
b05c50e08993 Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents: 4059
diff changeset
56 (if blink-matching-paren-distance
b05c50e08993 Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents: 4059
diff changeset
57 (narrow-to-region (max (point-min)
b05c50e08993 Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents: 4059
diff changeset
58 (- (point) blink-matching-paren-distance))
b05c50e08993 Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents: 4059
diff changeset
59 (min (point-max)
b05c50e08993 Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents: 4059
diff changeset
60 (+ (point) blink-matching-paren-distance))))
b05c50e08993 Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents: 4059
diff changeset
61 ;; Scan across one sexp within that range.
b05c50e08993 Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents: 4059
diff changeset
62 (condition-case ()
b05c50e08993 Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents: 4059
diff changeset
63 (setq pos (scan-sexps (point) dir))
b05c50e08993 Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents: 4059
diff changeset
64 (error nil))
b05c50e08993 Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents: 4059
diff changeset
65 ;; See if the "matching" paren is the right kind of paren
b05c50e08993 Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents: 4059
diff changeset
66 ;; to match the one we started at.
b05c50e08993 Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents: 4059
diff changeset
67 (if pos
b05c50e08993 Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents: 4059
diff changeset
68 (let ((beg (min pos oldpos)) (end (max pos oldpos)))
b05c50e08993 Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents: 4059
diff changeset
69 (and (/= (char-syntax (char-after beg)) ?\$)
b05c50e08993 Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents: 4059
diff changeset
70 (setq mismatch
b05c50e08993 Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents: 4059
diff changeset
71 (/= (char-after (1- end))
b05c50e08993 Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents: 4059
diff changeset
72 (logand (lsh (aref (syntax-table)
b05c50e08993 Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents: 4059
diff changeset
73 (char-after beg))
b05c50e08993 Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents: 4059
diff changeset
74 -8)
b05c50e08993 Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents: 4059
diff changeset
75 255))))))
b05c50e08993 Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents: 4059
diff changeset
76 ;; If they don't properly match, use a different face,
b05c50e08993 Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents: 4059
diff changeset
77 ;; or print a message.
b05c50e08993 Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents: 4059
diff changeset
78 (if mismatch
b05c50e08993 Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents: 4059
diff changeset
79 (progn
b05c50e08993 Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents: 4059
diff changeset
80 (and (null show-paren-mismatch-face)
b05c50e08993 Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents: 4059
diff changeset
81 (x-display-color-p)
b05c50e08993 Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents: 4059
diff changeset
82 (or (setq show-paren-mismatch-face
b05c50e08993 Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents: 4059
diff changeset
83 (internal-find-face 'paren-mismatch))
b05c50e08993 Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents: 4059
diff changeset
84 (progn
b05c50e08993 Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents: 4059
diff changeset
85 (setq show-paren-mismatch-face
b05c50e08993 Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents: 4059
diff changeset
86 (make-face 'paren-mismatch))
4183
a5f0a739d87d (show-paren-face): New variable.
Richard M. Stallman <rms@gnu.org>
parents: 4123
diff changeset
87 (set-face-background 'paren-mismatch
a5f0a739d87d (show-paren-face): New variable.
Richard M. Stallman <rms@gnu.org>
parents: 4123
diff changeset
88 "purple"))))
4123
b05c50e08993 Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents: 4059
diff changeset
89 (if show-paren-mismatch-face
b05c50e08993 Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents: 4059
diff changeset
90 (setq face show-paren-mismatch-face)
b05c50e08993 Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents: 4059
diff changeset
91 (message "Paren mismatch"))))
b05c50e08993 Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents: 4059
diff changeset
92 )))
4059
66d33e1bf65b (show-paren-command-hook): Do nothing if not window-system.
Richard M. Stallman <rms@gnu.org>
parents: 3977
diff changeset
93 (cond (pos
4123
b05c50e08993 Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents: 4059
diff changeset
94 (if (= dir -1)
b05c50e08993 Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents: 4059
diff changeset
95 ;; If matching backwards, highlight the closeparen
b05c50e08993 Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents: 4059
diff changeset
96 ;; before point as well as its matching open.
b05c50e08993 Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents: 4059
diff changeset
97 (progn
b05c50e08993 Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents: 4059
diff changeset
98 (if show-paren-overlay-1
b05c50e08993 Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents: 4059
diff changeset
99 (move-overlay show-paren-overlay-1 (+ (point) dir) (point))
b05c50e08993 Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents: 4059
diff changeset
100 (setq show-paren-overlay-1
b05c50e08993 Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents: 4059
diff changeset
101 (make-overlay (- pos dir) pos)))
b05c50e08993 Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents: 4059
diff changeset
102 (overlay-put show-paren-overlay-1 'face face))
b05c50e08993 Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents: 4059
diff changeset
103 ;; Otherwise, turn off any such highlighting.
b05c50e08993 Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents: 4059
diff changeset
104 (and show-paren-overlay-1
b05c50e08993 Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents: 4059
diff changeset
105 (overlay-buffer show-paren-overlay-1)
b05c50e08993 Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents: 4059
diff changeset
106 (delete-overlay show-paren-overlay-1)))
b05c50e08993 Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents: 4059
diff changeset
107 ;; Turn on highlighting for the matching paren.
4059
66d33e1bf65b (show-paren-command-hook): Do nothing if not window-system.
Richard M. Stallman <rms@gnu.org>
parents: 3977
diff changeset
108 (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
109 (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
110 (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
111 (make-overlay (- pos dir) pos)))
4123
b05c50e08993 Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents: 4059
diff changeset
112 (overlay-put show-paren-overlay 'face face))
4059
66d33e1bf65b (show-paren-command-hook): Do nothing if not window-system.
Richard M. Stallman <rms@gnu.org>
parents: 3977
diff changeset
113 (t
4123
b05c50e08993 Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents: 4059
diff changeset
114 ;; If not at a paren that has a match,
b05c50e08993 Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents: 4059
diff changeset
115 ;; turn off any previous paren highlighting.
4059
66d33e1bf65b (show-paren-command-hook): Do nothing if not window-system.
Richard M. Stallman <rms@gnu.org>
parents: 3977
diff changeset
116 (and show-paren-overlay (overlay-buffer show-paren-overlay)
4123
b05c50e08993 Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents: 4059
diff changeset
117 (delete-overlay show-paren-overlay))
b05c50e08993 Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents: 4059
diff changeset
118 (and show-paren-overlay-1 (overlay-buffer show-paren-overlay-1)
b05c50e08993 Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents: 4059
diff changeset
119 (delete-overlay show-paren-overlay-1)))))))
3912
676fb29d1692 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
120
4123
b05c50e08993 Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents: 4059
diff changeset
121 (if window-system
b05c50e08993 Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents: 4059
diff changeset
122 (progn
b05c50e08993 Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents: 4059
diff changeset
123 (setq blink-paren-function nil)
b05c50e08993 Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents: 4059
diff changeset
124 (add-hook 'post-command-hook 'show-paren-command-hook)))
3912
676fb29d1692 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
125
3919
bbac27613cee *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 3917
diff changeset
126 (provide 'paren)
bbac27613cee *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 3917
diff changeset
127
bbac27613cee *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 3917
diff changeset
128 ;;; paren.el ends here