Mercurial > emacs
annotate lisp/progmodes/cpp.el @ 67159:4de8eb0fda27
*** empty log message ***
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Sun, 27 Nov 2005 19:29:09 +0000 |
parents | a11fdee52c05 |
children | fdc28fc82a27 |
rev | line source |
---|---|
38436
b174db545cfd
Some fixes to follow coding conventions.
Pavel Janík <Pavel@Janik.cz>
parents:
38078
diff
changeset
|
1 ;;; cpp.el --- highlight or hide text according to cpp conditionals |
8735 | 2 |
64699
629afbe74e61
Update copyright for release of 22.1 for progmodes directory.
Nick Roberts <nickrob@snap.net.nz>
parents:
64085
diff
changeset
|
3 ;; Copyright (C) 1994, 1995, 2001, 2002, 2003, 2004, 2005 |
629afbe74e61
Update copyright for release of 22.1 for progmodes directory.
Nick Roberts <nickrob@snap.net.nz>
parents:
64085
diff
changeset
|
4 ;; Free Software Foundation |
8735 | 5 |
17981 | 6 ;; Author: Per Abrahamsen <abraham@dina.kvl.dk> |
8735 | 7 ;; Keywords: c, faces, tools |
8 | |
8736
fe48762e68de
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
8735
diff
changeset
|
9 ;; This file is part of GNU Emacs. |
8735 | 10 |
8736
fe48762e68de
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
8735
diff
changeset
|
11 ;; GNU Emacs is free software; you can redistribute it and/or modify |
8735 | 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. | |
8736
fe48762e68de
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
8735
diff
changeset
|
15 |
fe48762e68de
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
8735
diff
changeset
|
16 ;; GNU Emacs is distributed in the hope that it will be useful, |
8735 | 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. | |
8736
fe48762e68de
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
8735
diff
changeset
|
20 |
8735 | 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 |
64085 | 23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
24 ;; Boston, MA 02110-1301, USA. | |
8735 | 25 |
8736
fe48762e68de
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
8735
diff
changeset
|
26 ;;; Commentary: |
8735 | 27 |
28 ;; Parse a text for C preprocessor conditionals, and highlight or hide | |
29 ;; the text inside the conditionals as you wish. | |
30 | |
8740
714588372e06
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
8736
diff
changeset
|
31 ;; This package is inspired by Jim Coplien's delta editor for SCCS. |
8735 | 32 |
33 ;;; Todo: | |
34 | |
35 ;; Should parse "#if" and "#elif" expressions and merge the faces | |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
45431
diff
changeset
|
36 ;; somehow. |
8735 | 37 |
38 ;; Somehow it is sometimes possible to make changes near a read only | |
39 ;; area which you can't undo. Their are other strange effects in that | |
40 ;; area. | |
41 | |
42 ;; The Edit buffer should -- optionally -- appear in its own frame. | |
43 | |
44 ;; Conditionals seem to be rear-sticky. They shouldn't be. | |
45 | |
46 ;; Restore window configurations when exiting CPP Edit buffer. | |
47 | |
48 ;;; Code: | |
49 | |
50 ;;; Customization: | |
19009 | 51 (defgroup cpp nil |
52 "Highlight or hide text according to cpp conditionals." | |
66963
a11fdee52c05
Add :link (custom-group-link font-lock-faces) to defgroup.
Juri Linkov <juri@jurta.org>
parents:
64699
diff
changeset
|
53 :link '(custom-group-link :tag "Font Lock Faces group" font-lock-faces) |
28467
6ab0eec080f8
Change customization group to `c' from `C'.
Dave Love <fx@gnu.org>
parents:
23271
diff
changeset
|
54 :group 'c |
19009 | 55 :prefix "cpp-") |
8735 | 56 |
19009 | 57 (defcustom cpp-config-file (convert-standard-filename ".cpp.el") |
58 "*File name to save cpp configuration." | |
59 :type 'file | |
60 :group 'cpp) | |
13911
3e9e8b468bc1
(cpp-config-file): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
11492
diff
changeset
|
61 |
19009 | 62 (defcustom cpp-known-face 'invisible |
63 "*Face used for known cpp symbols." | |
64 :type 'face | |
65 :group 'cpp) | |
8735 | 66 |
19009 | 67 (defcustom cpp-unknown-face 'highlight |
68 "*Face used for unknown cpp symbols." | |
69 :type 'face | |
70 :group 'cpp) | |
8735 | 71 |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
45431
diff
changeset
|
72 (defcustom cpp-face-type 'light |
8735 | 73 "*Indicate what background face type you prefer. |
74 Can be either light or dark for color screens, mono for monochrome | |
30541
d5e4d3d5012c
(toplevel): Support faces on tty's.
Eli Zaretskii <eliz@gnu.org>
parents:
28467
diff
changeset
|
75 screens, and none if you don't use a window system and don't have |
d5e4d3d5012c
(toplevel): Support faces on tty's.
Eli Zaretskii <eliz@gnu.org>
parents:
28467
diff
changeset
|
76 a color-capable display." |
19009 | 77 :options '(light dark mono nil) |
78 :type 'symbol | |
79 :group 'cpp) | |
8735 | 80 |
19009 | 81 (defcustom cpp-known-writable t |
82 "*Non-nil means you are allowed to modify the known conditionals." | |
83 :type 'boolean | |
84 :group 'cpp) | |
8735 | 85 |
19009 | 86 (defcustom cpp-unknown-writable t |
87 "*Non-nil means you are allowed to modify the unknown conditionals." | |
88 :type 'boolean | |
89 :group 'cpp) | |
90 | |
91 (defcustom cpp-edit-list nil | |
11480
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
92 "Alist of cpp macros and information about how they should be displayed. |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
93 Each entry is a list with the following elements: |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
94 0. The name of the macro (a string). |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
95 1. Face used for text that is `ifdef' the macro. |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
96 2. Face used for text that is `ifndef' the macro. |
50858
54347668e2e9
(cpp-edit-list): Don't quote nil and t in docstrings.
Juanma Barranquero <lekktu@gmail.com>
parents:
49598
diff
changeset
|
97 3. t, nil, or `both' depending on what text may be edited." |
19009 | 98 :type '(repeat (list string face face |
99 (choice (const t) | |
100 (const nil) | |
101 (const both)))) | |
102 :group 'cpp) | |
11480
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
103 |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
104 (defvar cpp-overlay-list nil) |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
105 ;; List of cpp overlays active in the current buffer. |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
106 (make-variable-buffer-local 'cpp-overlay-list) |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
107 |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
108 (defvar cpp-callback-data) |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
109 (defvar cpp-state-stack) |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
110 |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
111 (defconst cpp-face-type-list |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
112 '(("light color background" . light) |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
113 ("dark color background" . dark) |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
114 ("monochrome" . mono) |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
115 ("tty" . none)) |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
116 "Alist of strings and names of the defined face collections.") |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
117 |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
118 (defconst cpp-writable-list |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
119 ;; Names used for the writable property. |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
120 '(("writable" . t) |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
121 ("read-only" . nil))) |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
122 |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
123 (defvar cpp-button-event nil) |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
124 ;; This will be t in the callback for `cpp-make-button'. |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
125 |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
126 (defvar cpp-edit-buffer nil) |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
127 ;; Real buffer whose cpp display information we are editing. |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
128 (make-variable-buffer-local 'cpp-edit-buffer) |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
129 |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
130 (defconst cpp-branch-list |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
131 ;; Alist of branches. |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
132 '(("false" . nil) |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
133 ("true" . t) |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
134 ("both" . both))) |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
135 |
19009 | 136 (defcustom cpp-face-default-list nil |
19129
35d85b50c3cb
(cpp-create-bg-face): Don't really make a face.
Richard M. Stallman <rms@gnu.org>
parents:
19009
diff
changeset
|
137 "Alist of faces you can choose from for cpp conditionals. |
35d85b50c3cb
(cpp-create-bg-face): Don't really make a face.
Richard M. Stallman <rms@gnu.org>
parents:
19009
diff
changeset
|
138 Each element has the form (STRING . FACE), where STRING |
35d85b50c3cb
(cpp-create-bg-face): Don't really make a face.
Richard M. Stallman <rms@gnu.org>
parents:
19009
diff
changeset
|
139 serves as a name (for `cpp-highlight-buffer' only) |
35d85b50c3cb
(cpp-create-bg-face): Don't really make a face.
Richard M. Stallman <rms@gnu.org>
parents:
19009
diff
changeset
|
140 and FACE is either a face (a symbol) |
35d85b50c3cb
(cpp-create-bg-face): Don't really make a face.
Richard M. Stallman <rms@gnu.org>
parents:
19009
diff
changeset
|
141 or a cons cell (background-color . COLOR)." |
35d85b50c3cb
(cpp-create-bg-face): Don't really make a face.
Richard M. Stallman <rms@gnu.org>
parents:
19009
diff
changeset
|
142 :type '(repeat (cons string (choice face (cons (const background-color) string)))) |
19009 | 143 :group 'cpp) |
11480
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
144 |
19009 | 145 (defcustom cpp-face-light-name-list |
11480
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
146 '("light gray" "light blue" "light cyan" "light yellow" "light pink" |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
147 "pale green" "beige" "orange" "magenta" "violet" "medium purple" |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
148 "turquoise") |
63275
7c5cfb705766
(cpp-face-light-name-list, cpp-face-dark-name-list): Fix spellings in
Juanma Barranquero <lekktu@gmail.com>
parents:
52401
diff
changeset
|
149 "Background colors useful with dark foreground colors." |
19009 | 150 :type '(repeat string) |
151 :group 'cpp) | |
11480
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
152 |
19009 | 153 (defcustom cpp-face-dark-name-list |
11480
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
154 '("dim gray" "blue" "cyan" "yellow" "red" |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
155 "dark green" "brown" "dark orange" "dark khaki" "dark violet" "purple" |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
156 "dark turquoise") |
63275
7c5cfb705766
(cpp-face-light-name-list, cpp-face-dark-name-list): Fix spellings in
Juanma Barranquero <lekktu@gmail.com>
parents:
52401
diff
changeset
|
157 "Background colors useful with light foreground colors." |
19009 | 158 :type '(repeat string) |
159 :group 'cpp) | |
11480
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
160 |
19009 | 161 (defcustom cpp-face-light-list nil |
162 "Alist of names and faces to be used for light backgrounds." | |
23271
f3d8ff8877ff
(cpp-face-light-list, cpp-face-dark-list): Fix
Andreas Schwab <schwab@suse.de>
parents:
19130
diff
changeset
|
163 :type '(repeat (cons string (choice face |
f3d8ff8877ff
(cpp-face-light-list, cpp-face-dark-list): Fix
Andreas Schwab <schwab@suse.de>
parents:
19130
diff
changeset
|
164 (cons (const background-color) string)))) |
19009 | 165 :group 'cpp) |
11480
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
166 |
19009 | 167 (defcustom cpp-face-dark-list nil |
168 "Alist of names and faces to be used for dark backgrounds." | |
23271
f3d8ff8877ff
(cpp-face-light-list, cpp-face-dark-list): Fix
Andreas Schwab <schwab@suse.de>
parents:
19130
diff
changeset
|
169 :type '(repeat (cons string (choice face |
f3d8ff8877ff
(cpp-face-light-list, cpp-face-dark-list): Fix
Andreas Schwab <schwab@suse.de>
parents:
19130
diff
changeset
|
170 (cons (const background-color) string)))) |
19009 | 171 :group 'cpp) |
11480
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
172 |
19009 | 173 (defcustom cpp-face-mono-list |
174 '(("bold" . bold) | |
175 ("bold-italic" . bold-italic) | |
176 ("italic" . italic) | |
177 ("underline" . underline)) | |
178 "Alist of names and faces to be used for monochrome screens." | |
179 :type '(repeat (cons string face)) | |
180 :group 'cpp) | |
181 | |
182 (defcustom cpp-face-none-list | |
11480
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
183 '(("default" . default) |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
184 ("invisible" . invisible)) |
19009 | 185 "Alist of names and faces available even if you don't use a window system." |
186 :type '(repeat (cons string face)) | |
187 :group 'cpp) | |
11480
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
188 |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
189 (defvar cpp-face-all-list |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
190 (append cpp-face-light-list |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
191 cpp-face-dark-list |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
192 cpp-face-mono-list |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
193 cpp-face-none-list) |
13974
37cfc82fe02d
(cpp-unknown-face, cpp-face-mono-list, cpp-face-all-list):
Karl Heuer <kwzh@gnu.org>
parents:
13911
diff
changeset
|
194 "All faces used for highlighting text inside cpp conditionals.") |
11480
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
195 |
8735 | 196 ;;; Parse Buffer: |
197 | |
198 (defvar cpp-parse-symbols nil | |
199 "List of cpp macros used in the local buffer.") | |
200 (make-variable-buffer-local 'cpp-parse-symbols) | |
201 | |
202 (defconst cpp-parse-regexp | |
203 ;; Regexp matching all tokens needed to find conditionals. | |
204 (concat | |
205 "'\\|\"\\|/\\*\\|//\\|" | |
206 "\\(^[ \t]*#[ \t]*\\(ifdef\\|ifndef\\|if\\|" | |
207 "elif\\|else\\|endif\\)\\b\\)")) | |
208 | |
209 ;;;###autoload | |
8741 | 210 (defun cpp-highlight-buffer (arg) |
211 "Highlight C code according to preprocessor conditionals. | |
212 This command pops up a buffer which you should edit to specify | |
213 what kind of highlighting to use, and the criteria for highlighting. | |
11456
0950bf9c8d06
(cpp-parse-open): Delete comments that go past end of line.
Richard M. Stallman <rms@gnu.org>
parents:
8741
diff
changeset
|
214 A prefix arg suppresses display of that buffer." |
8735 | 215 (interactive "P") |
19129
35d85b50c3cb
(cpp-create-bg-face): Don't really make a face.
Richard M. Stallman <rms@gnu.org>
parents:
19009
diff
changeset
|
216 (unless (or (eq t buffer-invisibility-spec) |
35d85b50c3cb
(cpp-create-bg-face): Don't really make a face.
Richard M. Stallman <rms@gnu.org>
parents:
19009
diff
changeset
|
217 (memq 'cpp buffer-invisibility-spec)) |
18071
bcdf720abb1a
(cpp-highlight-buffer): Make sure
Richard M. Stallman <rms@gnu.org>
parents:
17981
diff
changeset
|
218 (add-to-invisibility-spec 'cpp)) |
8735 | 219 (setq cpp-parse-symbols nil) |
220 (cpp-parse-reset) | |
221 (if (null cpp-edit-list) | |
222 (cpp-edit-load)) | |
11480
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
223 (let (cpp-state-stack) |
8735 | 224 (save-excursion |
225 (goto-char (point-min)) | |
226 (cpp-progress-message "Parsing...") | |
227 (while (re-search-forward cpp-parse-regexp nil t) | |
228 (cpp-progress-message "Parsing...%d%%" | |
229 (/ (* 100 (- (point) (point-min))) (buffer-size))) | |
230 (let ((match (buffer-substring (match-beginning 0) (match-end 0)))) | |
231 (cond ((or (string-equal match "'") | |
232 (string-equal match "\"")) | |
233 (goto-char (match-beginning 0)) | |
234 (condition-case nil | |
235 (forward-sexp) | |
236 (error (cpp-parse-error | |
237 "Unterminated string or character")))) | |
238 ((string-equal match "/*") | |
239 (or (search-forward "*/" nil t) | |
240 (error "Unterminated comment"))) | |
241 ((string-equal match "//") | |
242 (skip-chars-forward "^\n\r")) | |
243 (t | |
244 (end-of-line 1) | |
245 (let ((from (match-beginning 1)) | |
246 (to (1+ (point))) | |
247 (type (buffer-substring (match-beginning 2) | |
248 (match-end 2))) | |
249 (expr (buffer-substring (match-end 1) (point)))) | |
250 (cond ((string-equal type "ifdef") | |
251 (cpp-parse-open t expr from to)) | |
252 ((string-equal type "ifndef") | |
253 (cpp-parse-open nil expr from to)) | |
254 ((string-equal type "if") | |
255 (cpp-parse-open t expr from to)) | |
256 ((string-equal type "elif") | |
257 (let (cpp-known-face cpp-unknown-face) | |
258 (cpp-parse-close from to)) | |
259 (cpp-parse-open t expr from to)) | |
260 ((string-equal type "else") | |
11480
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
261 (or cpp-state-stack |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
262 (cpp-parse-error "Top level #else")) |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
263 (let ((entry (list (not (nth 0 (car cpp-state-stack))) |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
264 (nth 1 (car cpp-state-stack)) |
8735 | 265 from to))) |
266 (cpp-parse-close from to) | |
11480
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
267 (setq cpp-state-stack (cons entry cpp-state-stack)))) |
8735 | 268 ((string-equal type "endif") |
269 (cpp-parse-close from to)) | |
270 (t | |
271 (cpp-parse-error "Parser error")))))))) | |
272 (message "Parsing...done")) | |
11480
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
273 (if cpp-state-stack |
8735 | 274 (save-excursion |
11480
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
275 (goto-char (nth 3 (car cpp-state-stack))) |
8735 | 276 (cpp-parse-error "Unclosed conditional")))) |
277 (or arg | |
278 (null cpp-parse-symbols) | |
279 (cpp-parse-edit))) | |
280 | |
281 (defun cpp-parse-open (branch expr begin end) | |
11480
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
282 "Push information about conditional-beginning onto `cpp-state-stack'." |
11456
0950bf9c8d06
(cpp-parse-open): Delete comments that go past end of line.
Richard M. Stallman <rms@gnu.org>
parents:
8741
diff
changeset
|
283 ;; Discard comments within this line. |
8735 | 284 (while (string-match "\\b[ \t]*/\\*.*\\*/[ \t]*\\b" expr) |
285 (setq expr (concat (substring expr 0 (match-beginning 0)) | |
286 (substring expr (match-end 0))))) | |
11456
0950bf9c8d06
(cpp-parse-open): Delete comments that go past end of line.
Richard M. Stallman <rms@gnu.org>
parents:
8741
diff
changeset
|
287 ;; If a comment starts on this line and continues past, discard it. |
0950bf9c8d06
(cpp-parse-open): Delete comments that go past end of line.
Richard M. Stallman <rms@gnu.org>
parents:
8741
diff
changeset
|
288 (if (string-match "\\b[ \t]*/\\*" expr) |
0950bf9c8d06
(cpp-parse-open): Delete comments that go past end of line.
Richard M. Stallman <rms@gnu.org>
parents:
8741
diff
changeset
|
289 (setq expr (substring expr 0 (match-beginning 0)))) |
0950bf9c8d06
(cpp-parse-open): Delete comments that go past end of line.
Richard M. Stallman <rms@gnu.org>
parents:
8741
diff
changeset
|
290 ;; Delete any C++ comment from the line. |
8735 | 291 (if (string-match "\\b[ \t]*\\(//.*\\)?$" expr) |
292 (setq expr (substring expr 0 (match-beginning 0)))) | |
293 (while (string-match "[ \t]+" expr) | |
294 (setq expr (concat (substring expr 0 (match-beginning 0)) | |
295 (substring expr (match-end 0))))) | |
11480
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
296 (setq cpp-state-stack (cons (list branch expr begin end) cpp-state-stack)) |
8735 | 297 (or (member expr cpp-parse-symbols) |
298 (setq cpp-parse-symbols | |
299 (cons expr cpp-parse-symbols))) | |
300 (if (assoc expr cpp-edit-list) | |
301 (cpp-make-known-overlay begin end) | |
302 (cpp-make-unknown-overlay begin end))) | |
303 | |
304 (defun cpp-parse-close (from to) | |
11480
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
305 ;; Pop top of cpp-state-stack and create overlay. |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
306 (let ((entry (assoc (nth 1 (car cpp-state-stack)) cpp-edit-list)) |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
307 (branch (nth 0 (car cpp-state-stack))) |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
308 (begin (nth 2 (car cpp-state-stack))) |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
309 (end (nth 3 (car cpp-state-stack)))) |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
310 (setq cpp-state-stack (cdr cpp-state-stack)) |
8735 | 311 (if entry |
312 (let ((face (nth (if branch 1 2) entry)) | |
313 (read-only (eq (not branch) (nth 3 entry))) | |
11480
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
314 (priority (length cpp-state-stack)) |
8735 | 315 (overlay (make-overlay end from))) |
316 (cpp-make-known-overlay from to) | |
317 (setq cpp-overlay-list (cons overlay cpp-overlay-list)) | |
318 (if priority (overlay-put overlay 'priority priority)) | |
319 (cond ((eq face 'invisible) | |
320 (cpp-make-overlay-hidden overlay)) | |
321 ((eq face 'default)) | |
322 (t | |
323 (overlay-put overlay 'face face))) | |
324 (if read-only | |
325 (cpp-make-overlay-read-only overlay) | |
326 (cpp-make-overlay-sticky overlay))) | |
327 (cpp-make-unknown-overlay from to)))) | |
328 | |
329 (defun cpp-parse-error (error) | |
330 ;; Error message issued by the cpp parser. | |
14417
2b2e0cef30d5
(cpp-parse-error): Fix error format string.
Karl Heuer <kwzh@gnu.org>
parents:
14169
diff
changeset
|
331 (error "%s at line %d" error (count-lines (point-min) (point)))) |
8735 | 332 |
333 (defun cpp-parse-reset () | |
334 "Reset display of cpp conditionals to normal." | |
335 (interactive) | |
336 (while cpp-overlay-list | |
337 (delete-overlay (car cpp-overlay-list)) | |
338 (setq cpp-overlay-list (cdr cpp-overlay-list)))) | |
339 | |
340 ;;;###autoload | |
341 (defun cpp-parse-edit () | |
342 "Edit display information for cpp conditionals." | |
343 (interactive) | |
344 (or cpp-parse-symbols | |
11456
0950bf9c8d06
(cpp-parse-open): Delete comments that go past end of line.
Richard M. Stallman <rms@gnu.org>
parents:
8741
diff
changeset
|
345 (cpp-highlight-buffer t)) |
8735 | 346 (let ((buffer (current-buffer))) |
347 (pop-to-buffer "*CPP Edit*") | |
348 (cpp-edit-mode) | |
349 (setq cpp-edit-buffer buffer) | |
350 (cpp-edit-reset))) | |
351 | |
352 ;;; Overlays: | |
353 | |
354 (defun cpp-make-known-overlay (start end) | |
355 ;; Create an overlay for a known cpp command from START to END. | |
356 (let ((overlay (make-overlay start end))) | |
357 (if (eq cpp-known-face 'invisible) | |
358 (cpp-make-overlay-hidden overlay) | |
359 (or (eq cpp-known-face 'default) | |
360 (overlay-put overlay 'face cpp-known-face)) | |
361 (if cpp-known-writable | |
362 () | |
363 (overlay-put overlay 'modification-hooks '(cpp-signal-read-only)) | |
364 (overlay-put overlay 'insert-in-front-hooks '(cpp-signal-read-only)))) | |
365 (setq cpp-overlay-list (cons overlay cpp-overlay-list)))) | |
366 | |
367 (defun cpp-make-unknown-overlay (start end) | |
368 ;; Create an overlay for an unknown cpp command from START to END. | |
369 (let ((overlay (make-overlay start end))) | |
370 (cond ((eq cpp-unknown-face 'invisible) | |
371 (cpp-make-overlay-hidden overlay)) | |
372 ((eq cpp-unknown-face 'default)) | |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
45431
diff
changeset
|
373 (t |
8735 | 374 (overlay-put overlay 'face cpp-unknown-face))) |
375 (if cpp-unknown-writable | |
376 () | |
377 (overlay-put overlay 'modification-hooks '(cpp-signal-read-only)) | |
378 (overlay-put overlay 'insert-in-front-hooks '(cpp-signal-read-only))) | |
379 (setq cpp-overlay-list (cons overlay cpp-overlay-list)))) | |
380 | |
381 (defun cpp-make-overlay-hidden (overlay) | |
382 ;; Make overlay hidden and intangible. | |
18071
bcdf720abb1a
(cpp-highlight-buffer): Make sure
Richard M. Stallman <rms@gnu.org>
parents:
17981
diff
changeset
|
383 (overlay-put overlay 'invisible 'cpp) |
11492
2e09c796bf70
(cpp-edit-reset): Fix typo.
Richard M. Stallman <rms@gnu.org>
parents:
11480
diff
changeset
|
384 (overlay-put overlay 'modification-hooks '(cpp-signal-read-only)) |
2e09c796bf70
(cpp-edit-reset): Fix typo.
Richard M. Stallman <rms@gnu.org>
parents:
11480
diff
changeset
|
385 (overlay-put overlay 'insert-in-front-hooks '(cpp-signal-read-only))) |
8735 | 386 |
387 (defun cpp-make-overlay-read-only (overlay) | |
388 ;; Make overlay read only. | |
389 (overlay-put overlay 'modification-hooks '(cpp-signal-read-only)) | |
390 (overlay-put overlay 'insert-in-front-hooks '(cpp-signal-read-only)) | |
391 (overlay-put overlay 'insert-behind-hooks '(cpp-signal-read-only))) | |
392 | |
393 (defun cpp-make-overlay-sticky (overlay) | |
394 ;; Make OVERLAY grow when you insert text at either end. | |
395 (overlay-put overlay 'insert-in-front-hooks '(cpp-grow-overlay)) | |
396 (overlay-put overlay 'insert-behind-hooks '(cpp-grow-overlay))) | |
397 | |
11492
2e09c796bf70
(cpp-edit-reset): Fix typo.
Richard M. Stallman <rms@gnu.org>
parents:
11480
diff
changeset
|
398 (defun cpp-signal-read-only (overlay after start end &optional len) |
8735 | 399 ;; Only allow deleting the whole overlay. |
400 ;; Trying to change a read-only overlay. | |
11492
2e09c796bf70
(cpp-edit-reset): Fix typo.
Richard M. Stallman <rms@gnu.org>
parents:
11480
diff
changeset
|
401 (if (and (not after) |
2e09c796bf70
(cpp-edit-reset): Fix typo.
Richard M. Stallman <rms@gnu.org>
parents:
11480
diff
changeset
|
402 (or (< (overlay-start overlay) start) |
2e09c796bf70
(cpp-edit-reset): Fix typo.
Richard M. Stallman <rms@gnu.org>
parents:
11480
diff
changeset
|
403 (> (overlay-end overlay) end))) |
8735 | 404 (error "This text is read only"))) |
405 | |
11492
2e09c796bf70
(cpp-edit-reset): Fix typo.
Richard M. Stallman <rms@gnu.org>
parents:
11480
diff
changeset
|
406 (defun cpp-grow-overlay (overlay after start end &optional len) |
8735 | 407 ;; Make OVERLAY grow to contain range START to END. |
11492
2e09c796bf70
(cpp-edit-reset): Fix typo.
Richard M. Stallman <rms@gnu.org>
parents:
11480
diff
changeset
|
408 (if after |
2e09c796bf70
(cpp-edit-reset): Fix typo.
Richard M. Stallman <rms@gnu.org>
parents:
11480
diff
changeset
|
409 (move-overlay overlay |
2e09c796bf70
(cpp-edit-reset): Fix typo.
Richard M. Stallman <rms@gnu.org>
parents:
11480
diff
changeset
|
410 (min start (overlay-start overlay)) |
2e09c796bf70
(cpp-edit-reset): Fix typo.
Richard M. Stallman <rms@gnu.org>
parents:
11480
diff
changeset
|
411 (max end (overlay-end overlay))))) |
8735 | 412 |
413 ;;; Edit Buffer: | |
414 | |
415 (defvar cpp-edit-map nil) | |
416 ;; Keymap for `cpp-edit-mode'. | |
417 | |
418 (if cpp-edit-map | |
419 () | |
420 (setq cpp-edit-map (make-keymap)) | |
421 (suppress-keymap cpp-edit-map) | |
422 (define-key cpp-edit-map [ down-mouse-2 ] 'cpp-push-button) | |
423 (define-key cpp-edit-map [ mouse-2 ] 'ignore) | |
424 (define-key cpp-edit-map " " 'scroll-up) | |
425 (define-key cpp-edit-map "\C-?" 'scroll-down) | |
426 (define-key cpp-edit-map [ delete ] 'scroll-down) | |
427 (define-key cpp-edit-map "\C-c\C-c" 'cpp-edit-apply) | |
428 (define-key cpp-edit-map "a" 'cpp-edit-apply) | |
429 (define-key cpp-edit-map "A" 'cpp-edit-apply) | |
430 (define-key cpp-edit-map "r" 'cpp-edit-reset) | |
431 (define-key cpp-edit-map "R" 'cpp-edit-reset) | |
432 (define-key cpp-edit-map "s" 'cpp-edit-save) | |
433 (define-key cpp-edit-map "S" 'cpp-edit-save) | |
434 (define-key cpp-edit-map "l" 'cpp-edit-load) | |
435 (define-key cpp-edit-map "L" 'cpp-edit-load) | |
436 (define-key cpp-edit-map "h" 'cpp-edit-home) | |
437 (define-key cpp-edit-map "H" 'cpp-edit-home) | |
438 (define-key cpp-edit-map "b" 'cpp-edit-background) | |
439 (define-key cpp-edit-map "B" 'cpp-edit-background) | |
440 (define-key cpp-edit-map "k" 'cpp-edit-known) | |
441 (define-key cpp-edit-map "K" 'cpp-edit-known) | |
442 (define-key cpp-edit-map "u" 'cpp-edit-unknown) | |
443 (define-key cpp-edit-map "u" 'cpp-edit-unknown) | |
444 (define-key cpp-edit-map "t" 'cpp-edit-true) | |
445 (define-key cpp-edit-map "T" 'cpp-edit-true) | |
446 (define-key cpp-edit-map "f" 'cpp-edit-false) | |
447 (define-key cpp-edit-map "F" 'cpp-edit-false) | |
448 (define-key cpp-edit-map "w" 'cpp-edit-write) | |
449 (define-key cpp-edit-map "W" 'cpp-edit-write) | |
450 (define-key cpp-edit-map "X" 'cpp-edit-toggle-known) | |
451 (define-key cpp-edit-map "x" 'cpp-edit-toggle-known) | |
452 (define-key cpp-edit-map "Y" 'cpp-edit-toggle-unknown) | |
453 (define-key cpp-edit-map "y" 'cpp-edit-toggle-unknown) | |
454 (define-key cpp-edit-map "q" 'bury-buffer) | |
455 (define-key cpp-edit-map "Q" 'bury-buffer)) | |
456 | |
457 (defvar cpp-edit-symbols nil) | |
458 ;; Symbols defined in the edit buffer. | |
459 (make-variable-buffer-local 'cpp-edit-symbols) | |
460 | |
461 (defun cpp-edit-mode () | |
8741 | 462 "Major mode for editing the criteria for highlighting cpp conditionals. |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
45431
diff
changeset
|
463 Click on objects to change them. |
8735 | 464 You can also use the keyboard accelerators indicated like this: [K]ey." |
465 (kill-all-local-variables) | |
466 (buffer-disable-undo) | |
467 (auto-save-mode -1) | |
468 (setq buffer-read-only t) | |
469 (setq major-mode 'cpp-edit-mode) | |
470 (setq mode-name "CPP Edit") | |
471 (use-local-map cpp-edit-map)) | |
472 | |
473 (defun cpp-edit-apply () | |
474 "Apply edited display information to original buffer." | |
475 (interactive) | |
476 (cpp-edit-home) | |
11456
0950bf9c8d06
(cpp-parse-open): Delete comments that go past end of line.
Richard M. Stallman <rms@gnu.org>
parents:
8741
diff
changeset
|
477 (cpp-highlight-buffer t)) |
8735 | 478 |
479 (defun cpp-edit-reset () | |
480 "Reset display information from original buffer." | |
481 (interactive) | |
482 (let ((buffer (current-buffer)) | |
483 (buffer-read-only nil) | |
484 (start (window-start)) | |
485 (pos (point)) | |
486 symbols) | |
487 (set-buffer cpp-edit-buffer) | |
488 (setq symbols cpp-parse-symbols) | |
489 (set-buffer buffer) | |
490 (setq cpp-edit-symbols symbols) | |
491 (erase-buffer) | |
492 (insert "CPP Display Information for `") | |
493 (cpp-make-button (buffer-name cpp-edit-buffer) 'cpp-edit-home) | |
19130
0c228cae75b5
(cpp-edit-reset): Add a close-quote after the file name.
Richard M. Stallman <rms@gnu.org>
parents:
19129
diff
changeset
|
494 (insert "'\n\nClick mouse-2 on item you want to change or use\n" |
11456
0950bf9c8d06
(cpp-parse-open): Delete comments that go past end of line.
Richard M. Stallman <rms@gnu.org>
parents:
8741
diff
changeset
|
495 "or switch to this buffer and type the keyboard equivalents.\n" |
0950bf9c8d06
(cpp-parse-open): Delete comments that go past end of line.
Richard M. Stallman <rms@gnu.org>
parents:
8741
diff
changeset
|
496 "Keyboard equivalents are indicated with brackets like [T]his.\n\n") |
0950bf9c8d06
(cpp-parse-open): Delete comments that go past end of line.
Richard M. Stallman <rms@gnu.org>
parents:
8741
diff
changeset
|
497 (cpp-make-button "[H]ome (display the C file)" 'cpp-edit-home) |
0950bf9c8d06
(cpp-parse-open): Delete comments that go past end of line.
Richard M. Stallman <rms@gnu.org>
parents:
8741
diff
changeset
|
498 (insert " ") |
0950bf9c8d06
(cpp-parse-open): Delete comments that go past end of line.
Richard M. Stallman <rms@gnu.org>
parents:
8741
diff
changeset
|
499 (cpp-make-button "[A]pply new settings" 'cpp-edit-apply) |
0950bf9c8d06
(cpp-parse-open): Delete comments that go past end of line.
Richard M. Stallman <rms@gnu.org>
parents:
8741
diff
changeset
|
500 (insert "\n") |
0950bf9c8d06
(cpp-parse-open): Delete comments that go past end of line.
Richard M. Stallman <rms@gnu.org>
parents:
8741
diff
changeset
|
501 (cpp-make-button "[S]ave settings" 'cpp-edit-save) |
0950bf9c8d06
(cpp-parse-open): Delete comments that go past end of line.
Richard M. Stallman <rms@gnu.org>
parents:
8741
diff
changeset
|
502 (insert " ") |
0950bf9c8d06
(cpp-parse-open): Delete comments that go past end of line.
Richard M. Stallman <rms@gnu.org>
parents:
8741
diff
changeset
|
503 (cpp-make-button "[L]oad settings" 'cpp-edit-load) |
0950bf9c8d06
(cpp-parse-open): Delete comments that go past end of line.
Richard M. Stallman <rms@gnu.org>
parents:
8741
diff
changeset
|
504 (insert "\n\n") |
0950bf9c8d06
(cpp-parse-open): Delete comments that go past end of line.
Richard M. Stallman <rms@gnu.org>
parents:
8741
diff
changeset
|
505 |
8735 | 506 (insert "[B]ackground: ") |
507 (cpp-make-button (car (rassq cpp-face-type cpp-face-type-list)) | |
508 'cpp-edit-background) | |
509 (insert "\n[K]nown conditionals: ") | |
510 (cpp-make-button (cpp-face-name cpp-known-face) | |
511 'cpp-edit-known nil t) | |
512 (insert " [X] ") | |
513 (cpp-make-button (car (rassq cpp-known-writable cpp-writable-list)) | |
514 'cpp-edit-toggle-known) | |
515 (insert "\n[U]nknown conditionals: ") | |
516 (cpp-make-button (cpp-face-name cpp-unknown-face) | |
517 'cpp-edit-unknown nil t) | |
518 (insert " [Y] ") | |
519 (cpp-make-button (car (rassq cpp-unknown-writable cpp-writable-list)) | |
520 'cpp-edit-toggle-unknown) | |
521 (insert (format "\n\n\n%39s: %14s %14s %7s\n\n" "Expression" | |
522 "[T]rue Face" "[F]alse Face" "[W]rite")) | |
523 (while symbols | |
524 (let* ((symbol (car symbols)) | |
525 (entry (assoc symbol cpp-edit-list)) | |
526 (true (nth 1 entry)) | |
527 (false (nth 2 entry)) | |
528 (write (if entry (nth 3 entry) 'both))) | |
529 (setq symbols (cdr symbols)) | |
530 | |
531 (if (and entry ; Make default entries unknown. | |
532 (or (null true) (eq true 'default)) | |
533 (or (null false) (eq false 'default)) | |
534 (eq write 'both)) | |
535 (setq cpp-edit-list (delq entry cpp-edit-list) | |
536 entry nil)) | |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
45431
diff
changeset
|
537 |
11492
2e09c796bf70
(cpp-edit-reset): Fix typo.
Richard M. Stallman <rms@gnu.org>
parents:
11480
diff
changeset
|
538 (if (> (length symbol) 39) |
8735 | 539 (insert (substring symbol 0 39) ": ") |
540 (insert (format "%39s: " symbol))) | |
541 | |
542 (cpp-make-button (cpp-face-name true) | |
543 'cpp-edit-true symbol t 14) | |
544 (insert " ") | |
545 (cpp-make-button (cpp-face-name false) | |
546 'cpp-edit-false symbol t 14) | |
547 (insert " ") | |
548 (cpp-make-button (car (rassq write cpp-branch-list)) | |
549 'cpp-edit-write symbol nil 6) | |
550 (insert "\n"))) | |
551 (insert "\n\n") | |
552 (set-window-start nil start) | |
553 (goto-char pos))) | |
554 | |
555 (defun cpp-edit-load () | |
556 "Load cpp configuration." | |
557 (interactive) | |
16681
58b38425b463
(cpp-edit-load): Don't load anything if init-file-user is nil.
Richard M. Stallman <rms@gnu.org>
parents:
14417
diff
changeset
|
558 (cond ((null init-file-user) |
58b38425b463
(cpp-edit-load): Don't load anything if init-file-user is nil.
Richard M. Stallman <rms@gnu.org>
parents:
14417
diff
changeset
|
559 ;; If -q was specified, don't load any init files. |
58b38425b463
(cpp-edit-load): Don't load anything if init-file-user is nil.
Richard M. Stallman <rms@gnu.org>
parents:
14417
diff
changeset
|
560 nil) |
58b38425b463
(cpp-edit-load): Don't load anything if init-file-user is nil.
Richard M. Stallman <rms@gnu.org>
parents:
14417
diff
changeset
|
561 ((file-readable-p cpp-config-file) |
13911
3e9e8b468bc1
(cpp-config-file): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
11492
diff
changeset
|
562 (load-file cpp-config-file)) |
3e9e8b468bc1
(cpp-config-file): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
11492
diff
changeset
|
563 ((file-readable-p (concat "~/" cpp-config-file)) |
3e9e8b468bc1
(cpp-config-file): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
11492
diff
changeset
|
564 (load-file cpp-config-file))) |
8740
714588372e06
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
8736
diff
changeset
|
565 (if (eq major-mode 'cpp-edit-mode) |
714588372e06
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
8736
diff
changeset
|
566 (cpp-edit-reset))) |
8735 | 567 |
568 (defun cpp-edit-save () | |
16681
58b38425b463
(cpp-edit-load): Don't load anything if init-file-user is nil.
Richard M. Stallman <rms@gnu.org>
parents:
14417
diff
changeset
|
569 "Save the current cpp configuration in a file." |
8735 | 570 (interactive) |
571 (require 'pp) | |
572 (save-excursion | |
573 (set-buffer cpp-edit-buffer) | |
13911
3e9e8b468bc1
(cpp-config-file): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
11492
diff
changeset
|
574 (let ((buffer (find-file-noselect cpp-config-file))) |
8735 | 575 (set-buffer buffer) |
576 (erase-buffer) | |
577 (pp (list 'setq 'cpp-known-face | |
578 (list 'quote cpp-known-face)) buffer) | |
579 (pp (list 'setq 'cpp-unknown-face | |
580 (list 'quote cpp-unknown-face)) buffer) | |
581 (pp (list 'setq 'cpp-face-type | |
582 (list 'quote cpp-face-type)) buffer) | |
583 (pp (list 'setq 'cpp-known-writable | |
584 (list 'quote cpp-known-writable)) buffer) | |
585 (pp (list 'setq 'cpp-unknown-writable | |
586 (list 'quote cpp-unknown-writable)) buffer) | |
587 (pp (list 'setq 'cpp-edit-list | |
588 (list 'quote cpp-edit-list)) buffer) | |
13911
3e9e8b468bc1
(cpp-config-file): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
11492
diff
changeset
|
589 (write-file cpp-config-file)))) |
8735 | 590 |
591 (defun cpp-edit-home () | |
592 "Switch back to original buffer." | |
593 (interactive) | |
594 (if cpp-button-event | |
595 (read-event)) | |
596 (pop-to-buffer cpp-edit-buffer)) | |
597 | |
598 (defun cpp-edit-background () | |
599 "Change default face collection." | |
600 (interactive) | |
601 (call-interactively 'cpp-choose-default-face) | |
602 (cpp-edit-reset)) | |
603 | |
604 (defun cpp-edit-known () | |
605 "Select default for known conditionals." | |
606 (interactive) | |
607 (setq cpp-known-face (cpp-choose-face "Known face" cpp-known-face)) | |
608 (cpp-edit-reset)) | |
609 | |
610 (defun cpp-edit-unknown () | |
611 "Select default for unknown conditionals." | |
612 (interactive) | |
613 (setq cpp-unknown-face (cpp-choose-face "Unknown face" cpp-unknown-face)) | |
614 (cpp-edit-reset)) | |
615 | |
616 (defun cpp-edit-toggle-known (arg) | |
617 "Toggle writable status for known conditionals. | |
618 With optional argument ARG, make them writable iff ARG is positive." | |
619 (interactive "@P") | |
620 (if (or (and (null arg) cpp-known-writable) | |
621 (<= (prefix-numeric-value arg) 0)) | |
622 (setq cpp-known-writable nil) | |
623 (setq cpp-known-writable t)) | |
624 (cpp-edit-reset)) | |
625 | |
626 (defun cpp-edit-toggle-unknown (arg) | |
627 "Toggle writable status for unknown conditionals. | |
628 With optional argument ARG, make them writable iff ARG is positive." | |
629 (interactive "@P") | |
630 (if (or (and (null arg) cpp-unknown-writable) | |
631 (<= (prefix-numeric-value arg) 0)) | |
632 (setq cpp-unknown-writable nil) | |
633 (setq cpp-unknown-writable t)) | |
634 (cpp-edit-reset)) | |
635 | |
636 (defun cpp-edit-true (symbol face) | |
637 "Select SYMBOL's true FACE used for highlighting taken conditionals." | |
638 (interactive | |
639 (let ((symbol (cpp-choose-symbol))) | |
640 (list symbol | |
641 (cpp-choose-face "True face" | |
642 (nth 1 (assoc symbol cpp-edit-list)))))) | |
643 (setcar (nthcdr 1 (cpp-edit-list-entry-get-or-create symbol)) face) | |
644 (cpp-edit-reset)) | |
645 | |
646 (defun cpp-edit-false (symbol face) | |
647 "Select SYMBOL's false FACE used for highlighting untaken conditionals." | |
648 (interactive | |
649 (let ((symbol (cpp-choose-symbol))) | |
650 (list symbol | |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
45431
diff
changeset
|
651 (cpp-choose-face "False face" |
8735 | 652 (nth 2 (assoc symbol cpp-edit-list)))))) |
653 (setcar (nthcdr 2 (cpp-edit-list-entry-get-or-create symbol)) face) | |
654 (cpp-edit-reset)) | |
655 | |
656 (defun cpp-edit-write (symbol branch) | |
657 "Set which branches of SYMBOL should be writable to BRANCH. | |
658 BRANCH should be either nil (false branch), t (true branch) or 'both." | |
659 (interactive (list (cpp-choose-symbol) (cpp-choose-branch))) | |
660 (setcar (nthcdr 3 (cpp-edit-list-entry-get-or-create symbol)) branch) | |
661 (cpp-edit-reset)) | |
662 | |
663 (defun cpp-edit-list-entry-get-or-create (symbol) | |
664 ;; Return the entry for SYMBOL in `cpp-edit-list'. | |
665 ;; If it does not exist, create it. | |
666 (let ((entry (assoc symbol cpp-edit-list))) | |
667 (or entry | |
668 (setq entry (list symbol nil nil 'both nil) | |
669 cpp-edit-list (cons entry cpp-edit-list))) | |
670 entry)) | |
671 | |
672 ;;; Prompts: | |
673 | |
674 (defun cpp-choose-symbol () | |
675 ;; Choose a symbol if called from keyboard, otherwise use the one clicked on. | |
676 (if cpp-button-event | |
11480
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
677 cpp-callback-data |
45431
7505ed4a9b60
(cpp-choose-symbol): Don't cons unnecessarily.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
40955
diff
changeset
|
678 (completing-read "Symbol: " cpp-edit-symbols nil t))) |
8735 | 679 |
680 (defun cpp-choose-branch () | |
681 ;; Choose a branch, either nil, t, or both. | |
682 (if cpp-button-event | |
683 (x-popup-menu cpp-button-event | |
684 (list "Branch" (cons "Branch" cpp-branch-list))) | |
685 (cdr (assoc (completing-read "Branch: " cpp-branch-list nil t) | |
686 cpp-branch-list)))) | |
687 | |
688 (defun cpp-choose-face (prompt default) | |
40955
eb0bdaed72a8
(cpp-choose-face): Fix typo.
Pavel Janík <Pavel@Janik.cz>
parents:
38436
diff
changeset
|
689 ;; Choose a face from cpp-face-default-list. |
8735 | 690 ;; PROMPT is what to say to the user. |
691 ;; DEFAULT is the default face. | |
692 (or (if cpp-button-event | |
693 (x-popup-menu cpp-button-event | |
694 (list prompt (cons prompt cpp-face-default-list))) | |
695 (let ((name (car (rassq default cpp-face-default-list)))) | |
696 (cdr (assoc (completing-read (if name | |
697 (concat prompt | |
698 " (default " name "): ") | |
699 (concat prompt ": ")) | |
700 cpp-face-default-list nil t) | |
701 cpp-face-all-list)))) | |
702 default)) | |
703 | |
704 (defun cpp-choose-default-face (type) | |
705 ;; Choose default face list for screen of TYPE. | |
706 ;; Type must be one of the types defined in `cpp-face-type-list'. | |
707 (interactive (list (if cpp-button-event | |
708 (x-popup-menu cpp-button-event | |
709 (list "Screen type" | |
710 (cons "Screen type" | |
711 cpp-face-type-list))) | |
712 (cdr (assoc (completing-read "Screen type: " | |
713 cpp-face-type-list | |
714 nil t) | |
715 cpp-face-type-list))))) | |
716 (cond ((null type)) | |
717 ((eq type 'light) | |
718 (if cpp-face-light-list | |
719 () | |
720 (setq cpp-face-light-list | |
721 (mapcar 'cpp-create-bg-face cpp-face-light-name-list)) | |
722 (setq cpp-face-all-list | |
723 (append cpp-face-all-list cpp-face-light-list))) | |
724 (setq cpp-face-type 'light) | |
725 (setq cpp-face-default-list | |
726 (append cpp-face-light-list cpp-face-none-list))) | |
727 ((eq type 'dark) | |
728 (if cpp-face-dark-list | |
729 () | |
730 (setq cpp-face-dark-list | |
731 (mapcar 'cpp-create-bg-face cpp-face-dark-name-list)) | |
732 (setq cpp-face-all-list | |
733 (append cpp-face-all-list cpp-face-dark-list))) | |
734 (setq cpp-face-type 'dark) | |
735 (setq cpp-face-default-list | |
736 (append cpp-face-dark-list cpp-face-none-list))) | |
737 ((eq type 'mono) | |
738 (setq cpp-face-type 'mono) | |
739 (setq cpp-face-default-list | |
740 (append cpp-face-mono-list cpp-face-none-list))) | |
741 (t | |
742 (setq cpp-face-type 'none) | |
743 (setq cpp-face-default-list cpp-face-none-list)))) | |
744 | |
745 ;;; Buttons: | |
746 | |
747 (defun cpp-make-button (name callback &optional data face padding) | |
748 ;; Create a button at point. | |
749 ;; NAME is the name of the button. | |
750 ;; CALLBACK is the function to call when the button is pushed. | |
11480
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
751 ;; DATA will be made available to CALLBACK |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
752 ;;in the free variable cpp-callback-data. |
8735 | 753 ;; FACE means that NAME is the name of a face in `cpp-face-all-list'. |
754 ;; PADDING means NAME will be right justified at that length. | |
755 (let ((name (format "%s" name)) | |
756 from to) | |
757 (cond ((null padding) | |
758 (setq from (point)) | |
759 (insert name)) | |
760 ((> (length name) padding) | |
761 (setq from (point)) | |
762 (insert (substring name 0 padding))) | |
763 (t | |
764 (insert (make-string (- padding (length name)) ? )) | |
765 (setq from (point)) | |
766 (insert name))) | |
767 (setq to (point)) | |
768 (setq face | |
769 (if face | |
770 (let ((check (cdr (assoc name cpp-face-all-list)))) | |
771 (if (memq check '(default invisible)) | |
772 'bold | |
773 check)) | |
774 'bold)) | |
775 (add-text-properties from to | |
776 (append (list 'face face) | |
777 '(mouse-face highlight) | |
38078
8bc0292b0367
(cpp-make-button): Add help-echo to mouse-highlighted text.
Eli Zaretskii <eliz@gnu.org>
parents:
30541
diff
changeset
|
778 '(help-echo "mouse-2: change/use this item") |
8735 | 779 (list 'cpp-callback callback) |
780 (if data (list 'cpp-data data)))))) | |
781 | |
782 (defun cpp-push-button (event) | |
783 ;; Pushed a CPP button. | |
784 (interactive "@e") | |
785 (set-buffer (window-buffer (posn-window (event-start event)))) | |
786 (let ((pos (posn-point (event-start event)))) | |
11480
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
787 (let ((cpp-callback-data (get-text-property pos 'cpp-data)) |
8735 | 788 (fun (get-text-property pos 'cpp-callback)) |
789 (cpp-button-event event)) | |
790 (cond (fun | |
791 (call-interactively (get-text-property pos 'cpp-callback))) | |
792 ((lookup-key global-map [ down-mouse-2]) | |
793 (call-interactively (lookup-key global-map [ down-mouse-2]))))))) | |
794 | |
795 ;;; Faces: | |
796 | |
797 (defun cpp-create-bg-face (color) | |
798 ;; Create entry for face with background COLOR. | |
19129
35d85b50c3cb
(cpp-create-bg-face): Don't really make a face.
Richard M. Stallman <rms@gnu.org>
parents:
19009
diff
changeset
|
799 (cons color (cons 'background-color color))) |
8735 | 800 |
30541
d5e4d3d5012c
(toplevel): Support faces on tty's.
Eli Zaretskii <eliz@gnu.org>
parents:
28467
diff
changeset
|
801 (cpp-choose-default-face |
d5e4d3d5012c
(toplevel): Support faces on tty's.
Eli Zaretskii <eliz@gnu.org>
parents:
28467
diff
changeset
|
802 (if (or window-system (display-color-p)) cpp-face-type 'none)) |
8735 | 803 |
804 (defun cpp-face-name (face) | |
805 ;; Return the name of FACE from `cpp-face-all-list'. | |
806 (let ((entry (rassq (if face face 'default) cpp-face-all-list))) | |
807 (if entry | |
808 (car entry) | |
809 (format "<%s>" face)))) | |
810 | |
811 ;;; Utilities: | |
812 | |
813 (defvar cpp-progress-time 0) | |
814 ;; Last time we issued a progress message. | |
815 | |
816 (defun cpp-progress-message (&rest args) | |
817 ;; Report progress at most once a second. Take same ARGS as `message'. | |
818 (let ((time (nth 1 (current-time)))) | |
819 (if (= time cpp-progress-time) | |
820 () | |
821 (setq cpp-progress-time time) | |
822 (apply 'message args)))) | |
823 | |
824 (provide 'cpp) | |
825 | |
52401 | 826 ;;; arch-tag: fb7d433d-745d-495a-96f0-86908ab63f74 |
8735 | 827 ;;; cpp.el ends here |