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