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