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