Mercurial > emacs
annotate lisp/paren.el @ 14300:f777822a5d81
(syms_of_lread): Set Vsource_directory here.
(init_lread): Not here.
author | Karl Heuer <kwzh@gnu.org> |
---|---|
date | Wed, 24 Jan 1996 23:40:40 +0000 |
parents | 83f275dcd93a |
children | 77a1345ec9ca |
rev | line source |
---|---|
3912 | 1 ;;; paren.el --- highlight matching paren. |
14169 | 2 |
3913 | 3 ;; Copyright (C) 1993 Free Software Foundation, Inc. |
3912 | 4 |
3977 | 5 ;; Author: rms@gnu.ai.mit.edu |
6 ;; Maintainer: FSF | |
7 ;; Keywords: languages, faces | |
3976 | 8 |
3912 | 9 ;; This file is part of GNU Emacs. |
10 | |
11 ;; GNU Emacs is free software; you can redistribute it and/or modify | |
12 ;; it under the terms of the GNU General Public License as published by | |
13 ;; the Free Software Foundation; either version 2, or (at your option) | |
14 ;; any later version. | |
15 | |
16 ;; GNU Emacs is distributed in the hope that it will be useful, | |
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 ;; GNU General Public License for more details. | |
20 | |
21 ;; You should have received a copy of the GNU General Public License | |
14169 | 22 ;; along with GNU Emacs; see the file COPYING. If not, write to the |
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
24 ;; Boston, MA 02111-1307, USA. | |
3912 | 25 |
26 ;;; Commentary: | |
27 | |
28 ;; Load this and it will display highlighting on whatever | |
29 ;; paren matches the one before or after point. | |
30 | |
31 ;;; Code: | |
32 | |
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 matching paren. |
3917 | 34 (defvar show-paren-overlay nil) |
4123
b05c50e08993
Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents:
4059
diff
changeset
|
35 ;; 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
|
36 ;; right before point. |
b05c50e08993
Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents:
4059
diff
changeset
|
37 (defvar show-paren-overlay-1 nil) |
b05c50e08993
Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents:
4059
diff
changeset
|
38 |
b05c50e08993
Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents:
4059
diff
changeset
|
39 (defvar show-paren-mismatch-face nil) |
3912 | 40 |
4183
a5f0a739d87d
(show-paren-face): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
4123
diff
changeset
|
41 (defvar show-paren-face 'region |
a5f0a739d87d
(show-paren-face): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
4123
diff
changeset
|
42 "*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
|
43 |
3917 | 44 ;; Find the place to show, if there is one, |
45 ;; and show it until input arrives. | |
46 (defun show-paren-command-hook () | |
4582
7b665693054c
(show-paren-command-hook): Do nothing if input is pending.
Richard M. Stallman <rms@gnu.org>
parents:
4565
diff
changeset
|
47 ;; Do nothing if no window system to display results with. |
5623
d571ad3c796e
(show-paren-command-hook): Do nothing when in kbd macro.
Richard M. Stallman <rms@gnu.org>
parents:
4582
diff
changeset
|
48 ;; Do nothing if executing keyboard macro. |
4582
7b665693054c
(show-paren-command-hook): Do nothing if input is pending.
Richard M. Stallman <rms@gnu.org>
parents:
4565
diff
changeset
|
49 ;; Do nothing if input is pending. |
12918
88c1220e3dec
Use post-command-idle-hook.
Richard M. Stallman <rms@gnu.org>
parents:
12871
diff
changeset
|
50 (if window-system |
4059
66d33e1bf65b
(show-paren-command-hook): Do nothing if not window-system.
Richard M. Stallman <rms@gnu.org>
parents:
3977
diff
changeset
|
51 (let (pos dir mismatch (oldpos (point)) |
4183
a5f0a739d87d
(show-paren-face): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
4123
diff
changeset
|
52 (face show-paren-face)) |
8022
fcf805b27bc5
(show-paren-command-hook): Handle close paren rather than open paren
Richard M. Stallman <rms@gnu.org>
parents:
8019
diff
changeset
|
53 (cond ((eq (char-syntax (preceding-char)) ?\)) |
fcf805b27bc5
(show-paren-command-hook): Handle close paren rather than open paren
Richard M. Stallman <rms@gnu.org>
parents:
8019
diff
changeset
|
54 (setq dir -1)) |
fcf805b27bc5
(show-paren-command-hook): Handle close paren rather than open paren
Richard M. Stallman <rms@gnu.org>
parents:
8019
diff
changeset
|
55 ((eq (char-syntax (following-char)) ?\() |
fcf805b27bc5
(show-paren-command-hook): Handle close paren rather than open paren
Richard M. Stallman <rms@gnu.org>
parents:
8019
diff
changeset
|
56 (setq dir 1))) |
4123
b05c50e08993
Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents:
4059
diff
changeset
|
57 (if dir |
b05c50e08993
Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents:
4059
diff
changeset
|
58 (save-excursion |
b05c50e08993
Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents:
4059
diff
changeset
|
59 (save-restriction |
b05c50e08993
Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents:
4059
diff
changeset
|
60 ;; 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
|
61 (if blink-matching-paren-distance |
b05c50e08993
Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents:
4059
diff
changeset
|
62 (narrow-to-region (max (point-min) |
b05c50e08993
Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents:
4059
diff
changeset
|
63 (- (point) blink-matching-paren-distance)) |
b05c50e08993
Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents:
4059
diff
changeset
|
64 (min (point-max) |
b05c50e08993
Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents:
4059
diff
changeset
|
65 (+ (point) blink-matching-paren-distance)))) |
b05c50e08993
Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents:
4059
diff
changeset
|
66 ;; 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
|
67 (condition-case () |
b05c50e08993
Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents:
4059
diff
changeset
|
68 (setq pos (scan-sexps (point) dir)) |
b05c50e08993
Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents:
4059
diff
changeset
|
69 (error nil)) |
b05c50e08993
Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents:
4059
diff
changeset
|
70 ;; 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
|
71 ;; 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
|
72 (if pos |
b05c50e08993
Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents:
4059
diff
changeset
|
73 (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
|
74 (and (/= (char-syntax (char-after beg)) ?\$) |
b05c50e08993
Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents:
4059
diff
changeset
|
75 (setq mismatch |
8510
8903669537b0
(show-paren-command-hook): Cope if matching-paren gives nil.
Richard M. Stallman <rms@gnu.org>
parents:
8050
diff
changeset
|
76 (not (eq (char-after (1- end)) |
8903669537b0
(show-paren-command-hook): Cope if matching-paren gives nil.
Richard M. Stallman <rms@gnu.org>
parents:
8050
diff
changeset
|
77 ;; This can give nil. |
8903669537b0
(show-paren-command-hook): Cope if matching-paren gives nil.
Richard M. Stallman <rms@gnu.org>
parents:
8050
diff
changeset
|
78 (matching-paren (char-after beg)))))))) |
4123
b05c50e08993
Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents:
4059
diff
changeset
|
79 ;; 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
|
80 ;; or print a message. |
b05c50e08993
Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents:
4059
diff
changeset
|
81 (if mismatch |
b05c50e08993
Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents:
4059
diff
changeset
|
82 (progn |
b05c50e08993
Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents:
4059
diff
changeset
|
83 (and (null show-paren-mismatch-face) |
b05c50e08993
Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents:
4059
diff
changeset
|
84 (x-display-color-p) |
10380
6bf5b024dffb
(show-paren-command-hook): Don't alter paren-mismatch face
Richard M. Stallman <rms@gnu.org>
parents:
8558
diff
changeset
|
85 (progn |
13492
24b73b514a8b
(show-paren-command-hook): Don't list `paren-mismatch'
Karl Heuer <kwzh@gnu.org>
parents:
12918
diff
changeset
|
86 (add-to-list 'facemenu-unlisted-faces |
24b73b514a8b
(show-paren-command-hook): Don't list `paren-mismatch'
Karl Heuer <kwzh@gnu.org>
parents:
12918
diff
changeset
|
87 'paren-mismatch) |
10380
6bf5b024dffb
(show-paren-command-hook): Don't alter paren-mismatch face
Richard M. Stallman <rms@gnu.org>
parents:
8558
diff
changeset
|
88 (make-face 'paren-mismatch) |
6bf5b024dffb
(show-paren-command-hook): Don't alter paren-mismatch face
Richard M. Stallman <rms@gnu.org>
parents:
8558
diff
changeset
|
89 (or (face-nontrivial-p 'paren-mismatch t) |
6bf5b024dffb
(show-paren-command-hook): Don't alter paren-mismatch face
Richard M. Stallman <rms@gnu.org>
parents:
8558
diff
changeset
|
90 (progn |
6bf5b024dffb
(show-paren-command-hook): Don't alter paren-mismatch face
Richard M. Stallman <rms@gnu.org>
parents:
8558
diff
changeset
|
91 (set-face-background 'paren-mismatch |
6bf5b024dffb
(show-paren-command-hook): Don't alter paren-mismatch face
Richard M. Stallman <rms@gnu.org>
parents:
8558
diff
changeset
|
92 "purple") |
6bf5b024dffb
(show-paren-command-hook): Don't alter paren-mismatch face
Richard M. Stallman <rms@gnu.org>
parents:
8558
diff
changeset
|
93 (set-face-foreground 'paren-mismatch |
6bf5b024dffb
(show-paren-command-hook): Don't alter paren-mismatch face
Richard M. Stallman <rms@gnu.org>
parents:
8558
diff
changeset
|
94 "white"))) |
6bf5b024dffb
(show-paren-command-hook): Don't alter paren-mismatch face
Richard M. Stallman <rms@gnu.org>
parents:
8558
diff
changeset
|
95 (setq show-paren-mismatch-face 'paren-mismatch))) |
4123
b05c50e08993
Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents:
4059
diff
changeset
|
96 (if show-paren-mismatch-face |
b05c50e08993
Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents:
4059
diff
changeset
|
97 (setq face show-paren-mismatch-face) |
b05c50e08993
Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents:
4059
diff
changeset
|
98 (message "Paren mismatch")))) |
b05c50e08993
Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents:
4059
diff
changeset
|
99 ))) |
4059
66d33e1bf65b
(show-paren-command-hook): Do nothing if not window-system.
Richard M. Stallman <rms@gnu.org>
parents:
3977
diff
changeset
|
100 (cond (pos |
4123
b05c50e08993
Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents:
4059
diff
changeset
|
101 (if (= dir -1) |
b05c50e08993
Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents:
4059
diff
changeset
|
102 ;; If matching backwards, highlight the closeparen |
b05c50e08993
Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents:
4059
diff
changeset
|
103 ;; 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
|
104 (progn |
b05c50e08993
Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents:
4059
diff
changeset
|
105 (if show-paren-overlay-1 |
4353
d4e116f25030
(show-paren-command-hook): Specify buffer for move-overay.
Richard M. Stallman <rms@gnu.org>
parents:
4183
diff
changeset
|
106 (move-overlay show-paren-overlay-1 |
d4e116f25030
(show-paren-command-hook): Specify buffer for move-overay.
Richard M. Stallman <rms@gnu.org>
parents:
4183
diff
changeset
|
107 (+ (point) dir) (point) |
d4e116f25030
(show-paren-command-hook): Specify buffer for move-overay.
Richard M. Stallman <rms@gnu.org>
parents:
4183
diff
changeset
|
108 (current-buffer)) |
4123
b05c50e08993
Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents:
4059
diff
changeset
|
109 (setq show-paren-overlay-1 |
8558
30f91aa9b364
(show-paren-command-hook): Set the face of an overlay
Richard M. Stallman <rms@gnu.org>
parents:
8510
diff
changeset
|
110 (make-overlay (- pos dir) pos))) |
30f91aa9b364
(show-paren-command-hook): Set the face of an overlay
Richard M. Stallman <rms@gnu.org>
parents:
8510
diff
changeset
|
111 ;; Always set the overlay face, since it varies. |
30f91aa9b364
(show-paren-command-hook): Set the face of an overlay
Richard M. Stallman <rms@gnu.org>
parents:
8510
diff
changeset
|
112 (overlay-put show-paren-overlay-1 'face face)) |
4123
b05c50e08993
Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents:
4059
diff
changeset
|
113 ;; Otherwise, turn off any such highlighting. |
b05c50e08993
Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents:
4059
diff
changeset
|
114 (and show-paren-overlay-1 |
b05c50e08993
Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents:
4059
diff
changeset
|
115 (overlay-buffer show-paren-overlay-1) |
b05c50e08993
Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents:
4059
diff
changeset
|
116 (delete-overlay show-paren-overlay-1))) |
b05c50e08993
Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents:
4059
diff
changeset
|
117 ;; 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
|
118 (if show-paren-overlay |
4353
d4e116f25030
(show-paren-command-hook): Specify buffer for move-overay.
Richard M. Stallman <rms@gnu.org>
parents:
4183
diff
changeset
|
119 (move-overlay show-paren-overlay (- pos dir) pos |
d4e116f25030
(show-paren-command-hook): Specify buffer for move-overay.
Richard M. Stallman <rms@gnu.org>
parents:
4183
diff
changeset
|
120 (current-buffer)) |
4059
66d33e1bf65b
(show-paren-command-hook): Do nothing if not window-system.
Richard M. Stallman <rms@gnu.org>
parents:
3977
diff
changeset
|
121 (setq show-paren-overlay |
8558
30f91aa9b364
(show-paren-command-hook): Set the face of an overlay
Richard M. Stallman <rms@gnu.org>
parents:
8510
diff
changeset
|
122 (make-overlay (- pos dir) pos))) |
30f91aa9b364
(show-paren-command-hook): Set the face of an overlay
Richard M. Stallman <rms@gnu.org>
parents:
8510
diff
changeset
|
123 ;; Always set the overlay face, since it varies. |
30f91aa9b364
(show-paren-command-hook): Set the face of an overlay
Richard M. Stallman <rms@gnu.org>
parents:
8510
diff
changeset
|
124 (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
|
125 (t |
4123
b05c50e08993
Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents:
4059
diff
changeset
|
126 ;; 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
|
127 ;; 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
|
128 (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
|
129 (delete-overlay show-paren-overlay)) |
b05c50e08993
Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents:
4059
diff
changeset
|
130 (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
|
131 (delete-overlay show-paren-overlay-1))))))) |
3912 | 132 |
4123
b05c50e08993
Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents:
4059
diff
changeset
|
133 (if window-system |
b05c50e08993
Enable the hook only if window-system.
Richard M. Stallman <rms@gnu.org>
parents:
4059
diff
changeset
|
134 (progn |
13805
bff931d8f21a
(window-setup-hook): Clear blink-matching-paren-on-screen;
Karl Heuer <kwzh@gnu.org>
parents:
13492
diff
changeset
|
135 (setq blink-matching-paren-on-screen nil) |
12918
88c1220e3dec
Use post-command-idle-hook.
Richard M. Stallman <rms@gnu.org>
parents:
12871
diff
changeset
|
136 (add-hook 'post-command-idle-hook 'show-paren-command-hook))) |
4442
cb81eadd0df5
(window-setup-hook): Add hook function.
Richard M. Stallman <rms@gnu.org>
parents:
4353
diff
changeset
|
137 ;;; This is in case paren.el is preloaded. |
cb81eadd0df5
(window-setup-hook): Add hook function.
Richard M. Stallman <rms@gnu.org>
parents:
4353
diff
changeset
|
138 (add-hook 'window-setup-hook |
cb81eadd0df5
(window-setup-hook): Add hook function.
Richard M. Stallman <rms@gnu.org>
parents:
4353
diff
changeset
|
139 (function (lambda () |
cb81eadd0df5
(window-setup-hook): Add hook function.
Richard M. Stallman <rms@gnu.org>
parents:
4353
diff
changeset
|
140 (if window-system |
cb81eadd0df5
(window-setup-hook): Add hook function.
Richard M. Stallman <rms@gnu.org>
parents:
4353
diff
changeset
|
141 (progn |
13805
bff931d8f21a
(window-setup-hook): Clear blink-matching-paren-on-screen;
Karl Heuer <kwzh@gnu.org>
parents:
13492
diff
changeset
|
142 (setq blink-matching-paren-on-screen nil) |
12918
88c1220e3dec
Use post-command-idle-hook.
Richard M. Stallman <rms@gnu.org>
parents:
12871
diff
changeset
|
143 (add-hook 'post-command-idle-hook |
4442
cb81eadd0df5
(window-setup-hook): Add hook function.
Richard M. Stallman <rms@gnu.org>
parents:
4353
diff
changeset
|
144 'show-paren-command-hook)))))) |
3919 | 145 (provide 'paren) |
146 | |
147 ;;; paren.el ends here |