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