Mercurial > emacs
annotate lisp/progmodes/cpp.el @ 27292:d0aba668c5af
(set_internal): Enter the new arg.
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Tue, 11 Jan 2000 22:16:55 +0000 |
parents | f3d8ff8877ff |
children | 6ab0eec080f8 |
rev | line source |
---|---|
8735 | 1 ;;; cpp.el --- Highlight or hide text according to cpp conditionals. |
2 | |
11480
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
3 ;; Copyright (C) 1994, 1995 Free Software Foundation |
8735 | 4 |
17981 | 5 ;; Author: Per Abrahamsen <abraham@dina.kvl.dk> |
8735 | 6 ;; Keywords: c, faces, tools |
7 | |
8736
fe48762e68de
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
8735
diff
changeset
|
8 ;; This file is part of GNU Emacs. |
8735 | 9 |
8736
fe48762e68de
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
8735
diff
changeset
|
10 ;; GNU Emacs is free software; you can redistribute it and/or modify |
8735 | 11 ;; it under the terms of the GNU General Public License as published by |
12 ;; the Free Software Foundation; either version 2, or (at your option) | |
13 ;; any later version. | |
8736
fe48762e68de
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
8735
diff
changeset
|
14 |
fe48762e68de
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
8735
diff
changeset
|
15 ;; GNU Emacs is distributed in the hope that it will be useful, |
8735 | 16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 ;; GNU General Public License for more details. | |
8736
fe48762e68de
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
8735
diff
changeset
|
19 |
8735 | 20 ;; You should have received a copy of the GNU General Public License |
14169 | 21 ;; along with GNU Emacs; see the file COPYING. If not, write to the |
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
23 ;; Boston, MA 02111-1307, USA. | |
8735 | 24 |
8736
fe48762e68de
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
8735
diff
changeset
|
25 ;;; Commentary: |
8735 | 26 |
27 ;; Parse a text for C preprocessor conditionals, and highlight or hide | |
28 ;; the text inside the conditionals as you wish. | |
29 | |
8740
714588372e06
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
8736
diff
changeset
|
30 ;; This package is inspired by Jim Coplien's delta editor for SCCS. |
8735 | 31 |
32 ;;; Todo: | |
33 | |
34 ;; Should parse "#if" and "#elif" expressions and merge the faces | |
35 ;; somehow. | |
36 | |
37 ;; Somehow it is sometimes possible to make changes near a read only | |
38 ;; area which you can't undo. Their are other strange effects in that | |
39 ;; area. | |
40 | |
41 ;; The Edit buffer should -- optionally -- appear in its own frame. | |
42 | |
43 ;; Conditionals seem to be rear-sticky. They shouldn't be. | |
44 | |
45 ;; Restore window configurations when exiting CPP Edit buffer. | |
46 | |
47 ;;; Code: | |
48 | |
49 ;;; Customization: | |
19009 | 50 (defgroup cpp nil |
51 "Highlight or hide text according to cpp conditionals." | |
52 :group 'C | |
53 :prefix "cpp-") | |
8735 | 54 |
19009 | 55 (defcustom cpp-config-file (convert-standard-filename ".cpp.el") |
56 "*File name to save cpp configuration." | |
57 :type 'file | |
58 :group 'cpp) | |
13911
3e9e8b468bc1
(cpp-config-file): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
11492
diff
changeset
|
59 |
19009 | 60 (defcustom cpp-known-face 'invisible |
61 "*Face used for known cpp symbols." | |
62 :type 'face | |
63 :group 'cpp) | |
8735 | 64 |
19009 | 65 (defcustom cpp-unknown-face 'highlight |
66 "*Face used for unknown cpp symbols." | |
67 :type 'face | |
68 :group 'cpp) | |
8735 | 69 |
19009 | 70 (defcustom cpp-face-type 'light |
8735 | 71 "*Indicate what background face type you prefer. |
72 Can be either light or dark for color screens, mono for monochrome | |
19009 | 73 screens, and none if you don't use a window system." |
74 :options '(light dark mono nil) | |
75 :type 'symbol | |
76 :group 'cpp) | |
8735 | 77 |
19009 | 78 (defcustom cpp-known-writable t |
79 "*Non-nil means you are allowed to modify the known conditionals." | |
80 :type 'boolean | |
81 :group 'cpp) | |
8735 | 82 |
19009 | 83 (defcustom cpp-unknown-writable t |
84 "*Non-nil means you are allowed to modify the unknown conditionals." | |
85 :type 'boolean | |
86 :group 'cpp) | |
87 | |
88 (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
|
89 "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
|
90 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
|
91 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
|
92 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
|
93 2. Face used for text that is `ifndef' the macro. |
19009 | 94 3. `t', `nil', or `both' depending on what text may be edited." |
95 :type '(repeat (list string face face | |
96 (choice (const t) | |
97 (const nil) | |
98 (const both)))) | |
99 :group 'cpp) | |
11480
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
100 |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
101 (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
|
102 ;; 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
|
103 (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
|
104 |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
105 (defvar cpp-callback-data) |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
106 (defvar cpp-state-stack) |
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 (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
|
109 '(("light color background" . light) |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
110 ("dark color background" . dark) |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
111 ("monochrome" . mono) |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
112 ("tty" . none)) |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
113 "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
|
114 |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
115 (defconst cpp-writable-list |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
116 ;; 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
|
117 '(("writable" . t) |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
118 ("read-only" . nil))) |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
119 |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
120 (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
|
121 ;; 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
|
122 |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
123 (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
|
124 ;; 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
|
125 (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
|
126 |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
127 (defconst cpp-branch-list |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
128 ;; Alist of branches. |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
129 '(("false" . nil) |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
130 ("true" . t) |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
131 ("both" . both))) |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
132 |
19009 | 133 (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
|
134 "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
|
135 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
|
136 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
|
137 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
|
138 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
|
139 :type '(repeat (cons string (choice face (cons (const background-color) string)))) |
19009 | 140 :group 'cpp) |
11480
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
141 |
19009 | 142 (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
|
143 '("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
|
144 "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
|
145 "turquoise") |
19009 | 146 "Background colours useful with dark foreground colors." |
147 :type '(repeat string) | |
148 :group 'cpp) | |
11480
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
149 |
19009 | 150 (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
|
151 '("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
|
152 "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
|
153 "dark turquoise") |
19009 | 154 "Background colours useful with light foreground colors." |
155 :type '(repeat string) | |
156 :group 'cpp) | |
11480
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
157 |
19009 | 158 (defcustom cpp-face-light-list nil |
159 "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
|
160 :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
|
161 (cons (const background-color) string)))) |
19009 | 162 :group 'cpp) |
11480
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
163 |
19009 | 164 (defcustom cpp-face-dark-list nil |
165 "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
|
166 :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
|
167 (cons (const background-color) string)))) |
19009 | 168 :group 'cpp) |
11480
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
169 |
19009 | 170 (defcustom cpp-face-mono-list |
171 '(("bold" . bold) | |
172 ("bold-italic" . bold-italic) | |
173 ("italic" . italic) | |
174 ("underline" . underline)) | |
175 "Alist of names and faces to be used for monochrome screens." | |
176 :type '(repeat (cons string face)) | |
177 :group 'cpp) | |
178 | |
179 (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
|
180 '(("default" . default) |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
181 ("invisible" . invisible)) |
19009 | 182 "Alist of names and faces available even if you don't use a window system." |
183 :type '(repeat (cons string face)) | |
184 :group 'cpp) | |
11480
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
185 |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
186 (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
|
187 (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
|
188 cpp-face-dark-list |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
189 cpp-face-mono-list |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
190 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
|
191 "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
|
192 |
8735 | 193 ;;; Parse Buffer: |
194 | |
195 (defvar cpp-parse-symbols nil | |
196 "List of cpp macros used in the local buffer.") | |
197 (make-variable-buffer-local 'cpp-parse-symbols) | |
198 | |
199 (defconst cpp-parse-regexp | |
200 ;; Regexp matching all tokens needed to find conditionals. | |
201 (concat | |
202 "'\\|\"\\|/\\*\\|//\\|" | |
203 "\\(^[ \t]*#[ \t]*\\(ifdef\\|ifndef\\|if\\|" | |
204 "elif\\|else\\|endif\\)\\b\\)")) | |
205 | |
206 ;;;###autoload | |
8741 | 207 (defun cpp-highlight-buffer (arg) |
208 "Highlight C code according to preprocessor conditionals. | |
209 This command pops up a buffer which you should edit to specify | |
210 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
|
211 A prefix arg suppresses display of that buffer." |
8735 | 212 (interactive "P") |
19129
35d85b50c3cb
(cpp-create-bg-face): Don't really make a face.
Richard M. Stallman <rms@gnu.org>
parents:
19009
diff
changeset
|
213 (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
|
214 (memq 'cpp buffer-invisibility-spec)) |
18071
bcdf720abb1a
(cpp-highlight-buffer): Make sure
Richard M. Stallman <rms@gnu.org>
parents:
17981
diff
changeset
|
215 (add-to-invisibility-spec 'cpp)) |
8735 | 216 (setq cpp-parse-symbols nil) |
217 (cpp-parse-reset) | |
218 (if (null cpp-edit-list) | |
219 (cpp-edit-load)) | |
11480
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
220 (let (cpp-state-stack) |
8735 | 221 (save-excursion |
222 (goto-char (point-min)) | |
223 (cpp-progress-message "Parsing...") | |
224 (while (re-search-forward cpp-parse-regexp nil t) | |
225 (cpp-progress-message "Parsing...%d%%" | |
226 (/ (* 100 (- (point) (point-min))) (buffer-size))) | |
227 (let ((match (buffer-substring (match-beginning 0) (match-end 0)))) | |
228 (cond ((or (string-equal match "'") | |
229 (string-equal match "\"")) | |
230 (goto-char (match-beginning 0)) | |
231 (condition-case nil | |
232 (forward-sexp) | |
233 (error (cpp-parse-error | |
234 "Unterminated string or character")))) | |
235 ((string-equal match "/*") | |
236 (or (search-forward "*/" nil t) | |
237 (error "Unterminated comment"))) | |
238 ((string-equal match "//") | |
239 (skip-chars-forward "^\n\r")) | |
240 (t | |
241 (end-of-line 1) | |
242 (let ((from (match-beginning 1)) | |
243 (to (1+ (point))) | |
244 (type (buffer-substring (match-beginning 2) | |
245 (match-end 2))) | |
246 (expr (buffer-substring (match-end 1) (point)))) | |
247 (cond ((string-equal type "ifdef") | |
248 (cpp-parse-open t expr from to)) | |
249 ((string-equal type "ifndef") | |
250 (cpp-parse-open nil expr from to)) | |
251 ((string-equal type "if") | |
252 (cpp-parse-open t expr from to)) | |
253 ((string-equal type "elif") | |
254 (let (cpp-known-face cpp-unknown-face) | |
255 (cpp-parse-close from to)) | |
256 (cpp-parse-open t expr from to)) | |
257 ((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
|
258 (or cpp-state-stack |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
259 (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
|
260 (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
|
261 (nth 1 (car cpp-state-stack)) |
8735 | 262 from to))) |
263 (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
|
264 (setq cpp-state-stack (cons entry cpp-state-stack)))) |
8735 | 265 ((string-equal type "endif") |
266 (cpp-parse-close from to)) | |
267 (t | |
268 (cpp-parse-error "Parser error")))))))) | |
269 (message "Parsing...done")) | |
11480
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
270 (if cpp-state-stack |
8735 | 271 (save-excursion |
11480
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
272 (goto-char (nth 3 (car cpp-state-stack))) |
8735 | 273 (cpp-parse-error "Unclosed conditional")))) |
274 (or arg | |
275 (null cpp-parse-symbols) | |
276 (cpp-parse-edit))) | |
277 | |
278 (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
|
279 "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
|
280 ;; Discard comments within this line. |
8735 | 281 (while (string-match "\\b[ \t]*/\\*.*\\*/[ \t]*\\b" expr) |
282 (setq expr (concat (substring expr 0 (match-beginning 0)) | |
283 (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
|
284 ;; 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
|
285 (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
|
286 (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
|
287 ;; Delete any C++ comment from the line. |
8735 | 288 (if (string-match "\\b[ \t]*\\(//.*\\)?$" expr) |
289 (setq expr (substring expr 0 (match-beginning 0)))) | |
290 (while (string-match "[ \t]+" expr) | |
291 (setq expr (concat (substring expr 0 (match-beginning 0)) | |
292 (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
|
293 (setq cpp-state-stack (cons (list branch expr begin end) cpp-state-stack)) |
8735 | 294 (or (member expr cpp-parse-symbols) |
295 (setq cpp-parse-symbols | |
296 (cons expr cpp-parse-symbols))) | |
297 (if (assoc expr cpp-edit-list) | |
298 (cpp-make-known-overlay begin end) | |
299 (cpp-make-unknown-overlay begin end))) | |
300 | |
301 (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
|
302 ;; 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
|
303 (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
|
304 (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
|
305 (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
|
306 (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
|
307 (setq cpp-state-stack (cdr cpp-state-stack)) |
8735 | 308 (if entry |
309 (let ((face (nth (if branch 1 2) entry)) | |
310 (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
|
311 (priority (length cpp-state-stack)) |
8735 | 312 (overlay (make-overlay end from))) |
313 (cpp-make-known-overlay from to) | |
314 (setq cpp-overlay-list (cons overlay cpp-overlay-list)) | |
315 (if priority (overlay-put overlay 'priority priority)) | |
316 (cond ((eq face 'invisible) | |
317 (cpp-make-overlay-hidden overlay)) | |
318 ((eq face 'default)) | |
319 (t | |
320 (overlay-put overlay 'face face))) | |
321 (if read-only | |
322 (cpp-make-overlay-read-only overlay) | |
323 (cpp-make-overlay-sticky overlay))) | |
324 (cpp-make-unknown-overlay from to)))) | |
325 | |
326 (defun cpp-parse-error (error) | |
327 ;; 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
|
328 (error "%s at line %d" error (count-lines (point-min) (point)))) |
8735 | 329 |
330 (defun cpp-parse-reset () | |
331 "Reset display of cpp conditionals to normal." | |
332 (interactive) | |
333 (while cpp-overlay-list | |
334 (delete-overlay (car cpp-overlay-list)) | |
335 (setq cpp-overlay-list (cdr cpp-overlay-list)))) | |
336 | |
337 ;;;###autoload | |
338 (defun cpp-parse-edit () | |
339 "Edit display information for cpp conditionals." | |
340 (interactive) | |
341 (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
|
342 (cpp-highlight-buffer t)) |
8735 | 343 (let ((buffer (current-buffer))) |
344 (pop-to-buffer "*CPP Edit*") | |
345 (cpp-edit-mode) | |
346 (setq cpp-edit-buffer buffer) | |
347 (cpp-edit-reset))) | |
348 | |
349 ;;; Overlays: | |
350 | |
351 (defun cpp-make-known-overlay (start end) | |
352 ;; Create an overlay for a known cpp command from START to END. | |
353 (let ((overlay (make-overlay start end))) | |
354 (if (eq cpp-known-face 'invisible) | |
355 (cpp-make-overlay-hidden overlay) | |
356 (or (eq cpp-known-face 'default) | |
357 (overlay-put overlay 'face cpp-known-face)) | |
358 (if cpp-known-writable | |
359 () | |
360 (overlay-put overlay 'modification-hooks '(cpp-signal-read-only)) | |
361 (overlay-put overlay 'insert-in-front-hooks '(cpp-signal-read-only)))) | |
362 (setq cpp-overlay-list (cons overlay cpp-overlay-list)))) | |
363 | |
364 (defun cpp-make-unknown-overlay (start end) | |
365 ;; Create an overlay for an unknown cpp command from START to END. | |
366 (let ((overlay (make-overlay start end))) | |
367 (cond ((eq cpp-unknown-face 'invisible) | |
368 (cpp-make-overlay-hidden overlay)) | |
369 ((eq cpp-unknown-face 'default)) | |
370 (t | |
371 (overlay-put overlay 'face cpp-unknown-face))) | |
372 (if cpp-unknown-writable | |
373 () | |
374 (overlay-put overlay 'modification-hooks '(cpp-signal-read-only)) | |
375 (overlay-put overlay 'insert-in-front-hooks '(cpp-signal-read-only))) | |
376 (setq cpp-overlay-list (cons overlay cpp-overlay-list)))) | |
377 | |
378 (defun cpp-make-overlay-hidden (overlay) | |
379 ;; Make overlay hidden and intangible. | |
18071
bcdf720abb1a
(cpp-highlight-buffer): Make sure
Richard M. Stallman <rms@gnu.org>
parents:
17981
diff
changeset
|
380 (overlay-put overlay 'invisible 'cpp) |
8735 | 381 (overlay-put overlay 'intangible t) |
382 ;; Unfortunately `intangible' is not implemented for overlays yet, | |
383 ;; so we make is read-only instead. | |
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. |
8735 | 463 Click on objects to change them. |
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)) | |
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 | |
651 (cpp-choose-face "False face" | |
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 |
8735 | 678 (completing-read "Symbol: " (mapcar 'list cpp-edit-symbols) nil t))) |
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) | |
689 ;; Choose a face from cpp-face-defalt-list. | |
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) | |
778 (list 'cpp-callback callback) | |
779 (if data (list 'cpp-data data)))))) | |
780 | |
781 (defun cpp-push-button (event) | |
782 ;; Pushed a CPP button. | |
783 (interactive "@e") | |
784 (set-buffer (window-buffer (posn-window (event-start event)))) | |
785 (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
|
786 (let ((cpp-callback-data (get-text-property pos 'cpp-data)) |
8735 | 787 (fun (get-text-property pos 'cpp-callback)) |
788 (cpp-button-event event)) | |
789 (cond (fun | |
790 (call-interactively (get-text-property pos 'cpp-callback))) | |
791 ((lookup-key global-map [ down-mouse-2]) | |
792 (call-interactively (lookup-key global-map [ down-mouse-2]))))))) | |
793 | |
794 ;;; Faces: | |
795 | |
796 (defun cpp-create-bg-face (color) | |
797 ;; 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
|
798 (cons color (cons 'background-color color))) |
8735 | 799 |
800 (cpp-choose-default-face (if window-system cpp-face-type 'none)) | |
801 | |
802 (defun cpp-face-name (face) | |
803 ;; Return the name of FACE from `cpp-face-all-list'. | |
804 (let ((entry (rassq (if face face 'default) cpp-face-all-list))) | |
805 (if entry | |
806 (car entry) | |
807 (format "<%s>" face)))) | |
808 | |
809 ;;; Utilities: | |
810 | |
811 (defvar cpp-progress-time 0) | |
812 ;; Last time we issued a progress message. | |
813 | |
814 (defun cpp-progress-message (&rest args) | |
815 ;; Report progress at most once a second. Take same ARGS as `message'. | |
816 (let ((time (nth 1 (current-time)))) | |
817 (if (= time cpp-progress-time) | |
818 () | |
819 (setq cpp-progress-time time) | |
820 (apply 'message args)))) | |
821 | |
822 (provide 'cpp) | |
823 | |
824 ;;; cpp.el ends here |