]> code.delx.au - gnu-emacs/blob - lisp/progmodes/cpp.el
(cpp-face-light-list, cpp-face-dark-list): Fix
[gnu-emacs] / lisp / progmodes / cpp.el
1 ;;; cpp.el --- Highlight or hide text according to cpp conditionals.
2
3 ;; Copyright (C) 1994, 1995 Free Software Foundation
4
5 ;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
6 ;; Keywords: c, faces, tools
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
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.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
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.
19
20 ;; You should have received a copy of the GNU General Public License
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.
24
25 ;;; Commentary:
26
27 ;; Parse a text for C preprocessor conditionals, and highlight or hide
28 ;; the text inside the conditionals as you wish.
29
30 ;; This package is inspired by Jim Coplien's delta editor for SCCS.
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:
50 (defgroup cpp nil
51 "Highlight or hide text according to cpp conditionals."
52 :group 'C
53 :prefix "cpp-")
54
55 (defcustom cpp-config-file (convert-standard-filename ".cpp.el")
56 "*File name to save cpp configuration."
57 :type 'file
58 :group 'cpp)
59
60 (defcustom cpp-known-face 'invisible
61 "*Face used for known cpp symbols."
62 :type 'face
63 :group 'cpp)
64
65 (defcustom cpp-unknown-face 'highlight
66 "*Face used for unknown cpp symbols."
67 :type 'face
68 :group 'cpp)
69
70 (defcustom cpp-face-type 'light
71 "*Indicate what background face type you prefer.
72 Can be either light or dark for color screens, mono for monochrome
73 screens, and none if you don't use a window system."
74 :options '(light dark mono nil)
75 :type 'symbol
76 :group 'cpp)
77
78 (defcustom cpp-known-writable t
79 "*Non-nil means you are allowed to modify the known conditionals."
80 :type 'boolean
81 :group 'cpp)
82
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
89 "Alist of cpp macros and information about how they should be displayed.
90 Each entry is a list with the following elements:
91 0. The name of the macro (a string).
92 1. Face used for text that is `ifdef' the macro.
93 2. Face used for text that is `ifndef' the macro.
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)
100
101 (defvar cpp-overlay-list nil)
102 ;; List of cpp overlays active in the current buffer.
103 (make-variable-buffer-local 'cpp-overlay-list)
104
105 (defvar cpp-callback-data)
106 (defvar cpp-state-stack)
107
108 (defconst cpp-face-type-list
109 '(("light color background" . light)
110 ("dark color background" . dark)
111 ("monochrome" . mono)
112 ("tty" . none))
113 "Alist of strings and names of the defined face collections.")
114
115 (defconst cpp-writable-list
116 ;; Names used for the writable property.
117 '(("writable" . t)
118 ("read-only" . nil)))
119
120 (defvar cpp-button-event nil)
121 ;; This will be t in the callback for `cpp-make-button'.
122
123 (defvar cpp-edit-buffer nil)
124 ;; Real buffer whose cpp display information we are editing.
125 (make-variable-buffer-local 'cpp-edit-buffer)
126
127 (defconst cpp-branch-list
128 ;; Alist of branches.
129 '(("false" . nil)
130 ("true" . t)
131 ("both" . both)))
132
133 (defcustom cpp-face-default-list nil
134 "Alist of faces you can choose from for cpp conditionals.
135 Each element has the form (STRING . FACE), where STRING
136 serves as a name (for `cpp-highlight-buffer' only)
137 and FACE is either a face (a symbol)
138 or a cons cell (background-color . COLOR)."
139 :type '(repeat (cons string (choice face (cons (const background-color) string))))
140 :group 'cpp)
141
142 (defcustom cpp-face-light-name-list
143 '("light gray" "light blue" "light cyan" "light yellow" "light pink"
144 "pale green" "beige" "orange" "magenta" "violet" "medium purple"
145 "turquoise")
146 "Background colours useful with dark foreground colors."
147 :type '(repeat string)
148 :group 'cpp)
149
150 (defcustom cpp-face-dark-name-list
151 '("dim gray" "blue" "cyan" "yellow" "red"
152 "dark green" "brown" "dark orange" "dark khaki" "dark violet" "purple"
153 "dark turquoise")
154 "Background colours useful with light foreground colors."
155 :type '(repeat string)
156 :group 'cpp)
157
158 (defcustom cpp-face-light-list nil
159 "Alist of names and faces to be used for light backgrounds."
160 :type '(repeat (cons string (choice face
161 (cons (const background-color) string))))
162 :group 'cpp)
163
164 (defcustom cpp-face-dark-list nil
165 "Alist of names and faces to be used for dark backgrounds."
166 :type '(repeat (cons string (choice face
167 (cons (const background-color) string))))
168 :group 'cpp)
169
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
180 '(("default" . default)
181 ("invisible" . invisible))
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)
185
186 (defvar cpp-face-all-list
187 (append cpp-face-light-list
188 cpp-face-dark-list
189 cpp-face-mono-list
190 cpp-face-none-list)
191 "All faces used for highlighting text inside cpp conditionals.")
192
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
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.
211 A prefix arg suppresses display of that buffer."
212 (interactive "P")
213 (unless (or (eq t buffer-invisibility-spec)
214 (memq 'cpp buffer-invisibility-spec))
215 (add-to-invisibility-spec 'cpp))
216 (setq cpp-parse-symbols nil)
217 (cpp-parse-reset)
218 (if (null cpp-edit-list)
219 (cpp-edit-load))
220 (let (cpp-state-stack)
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")
258 (or cpp-state-stack
259 (cpp-parse-error "Top level #else"))
260 (let ((entry (list (not (nth 0 (car cpp-state-stack)))
261 (nth 1 (car cpp-state-stack))
262 from to)))
263 (cpp-parse-close from to)
264 (setq cpp-state-stack (cons entry cpp-state-stack))))
265 ((string-equal type "endif")
266 (cpp-parse-close from to))
267 (t
268 (cpp-parse-error "Parser error"))))))))
269 (message "Parsing...done"))
270 (if cpp-state-stack
271 (save-excursion
272 (goto-char (nth 3 (car cpp-state-stack)))
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)
279 "Push information about conditional-beginning onto `cpp-state-stack'."
280 ;; Discard comments within this line.
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)))))
284 ;; If a comment starts on this line and continues past, discard it.
285 (if (string-match "\\b[ \t]*/\\*" expr)
286 (setq expr (substring expr 0 (match-beginning 0))))
287 ;; Delete any C++ comment from the line.
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)))))
293 (setq cpp-state-stack (cons (list branch expr begin end) cpp-state-stack))
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)
302 ;; Pop top of cpp-state-stack and create overlay.
303 (let ((entry (assoc (nth 1 (car cpp-state-stack)) cpp-edit-list))
304 (branch (nth 0 (car cpp-state-stack)))
305 (begin (nth 2 (car cpp-state-stack)))
306 (end (nth 3 (car cpp-state-stack))))
307 (setq cpp-state-stack (cdr cpp-state-stack))
308 (if entry
309 (let ((face (nth (if branch 1 2) entry))
310 (read-only (eq (not branch) (nth 3 entry)))
311 (priority (length cpp-state-stack))
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.
328 (error "%s at line %d" error (count-lines (point-min) (point))))
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
342 (cpp-highlight-buffer t))
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.
380 (overlay-put overlay 'invisible 'cpp)
381 (overlay-put overlay 'intangible t)
382 ;; Unfortunately `intangible' is not implemented for overlays yet,
383 ;; so we make is read-only instead.
384 (overlay-put overlay 'modification-hooks '(cpp-signal-read-only))
385 (overlay-put overlay 'insert-in-front-hooks '(cpp-signal-read-only)))
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
398 (defun cpp-signal-read-only (overlay after start end &optional len)
399 ;; Only allow deleting the whole overlay.
400 ;; Trying to change a read-only overlay.
401 (if (and (not after)
402 (or (< (overlay-start overlay) start)
403 (> (overlay-end overlay) end)))
404 (error "This text is read only")))
405
406 (defun cpp-grow-overlay (overlay after start end &optional len)
407 ;; Make OVERLAY grow to contain range START to END.
408 (if after
409 (move-overlay overlay
410 (min start (overlay-start overlay))
411 (max end (overlay-end overlay)))))
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 ()
462 "Major mode for editing the criteria for highlighting cpp conditionals.
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)
477 (cpp-highlight-buffer t))
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)
494 (insert "'\n\nClick mouse-2 on item you want to change or use\n"
495 "or switch to this buffer and type the keyboard equivalents.\n"
496 "Keyboard equivalents are indicated with brackets like [T]his.\n\n")
497 (cpp-make-button "[H]ome (display the C file)" 'cpp-edit-home)
498 (insert " ")
499 (cpp-make-button "[A]pply new settings" 'cpp-edit-apply)
500 (insert "\n")
501 (cpp-make-button "[S]ave settings" 'cpp-edit-save)
502 (insert " ")
503 (cpp-make-button "[L]oad settings" 'cpp-edit-load)
504 (insert "\n\n")
505
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
538 (if (> (length symbol) 39)
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)
558 (cond ((null init-file-user)
559 ;; If -q was specified, don't load any init files.
560 nil)
561 ((file-readable-p cpp-config-file)
562 (load-file cpp-config-file))
563 ((file-readable-p (concat "~/" cpp-config-file))
564 (load-file cpp-config-file)))
565 (if (eq major-mode 'cpp-edit-mode)
566 (cpp-edit-reset)))
567
568 (defun cpp-edit-save ()
569 "Save the current cpp configuration in a file."
570 (interactive)
571 (require 'pp)
572 (save-excursion
573 (set-buffer cpp-edit-buffer)
574 (let ((buffer (find-file-noselect cpp-config-file)))
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)
589 (write-file cpp-config-file))))
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
677 cpp-callback-data
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.
751 ;; DATA will be made available to CALLBACK
752 ;;in the free variable cpp-callback-data.
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))))
786 (let ((cpp-callback-data (get-text-property pos 'cpp-data))
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.
798 (cons color (cons 'background-color color)))
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