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