Update copyright notices for 2013.
[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-2013 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 (end (nth 3 (car cpp-state-stack))))
312 (setq cpp-state-stack (cdr cpp-state-stack))
313 (if entry
314 (let ((face (nth (if branch 1 2) entry))
315 (read-only (eq (not branch) (nth 3 entry)))
316 (priority (length cpp-state-stack))
317 (overlay (make-overlay end from)))
318 (cpp-make-known-overlay from to)
319 (setq cpp-overlay-list (cons overlay cpp-overlay-list))
320 (if priority (overlay-put overlay 'priority priority))
321 (cond ((eq face 'invisible)
322 (cpp-make-overlay-hidden overlay))
323 ((eq face 'default))
324 (t
325 (overlay-put overlay 'face face)))
326 (if read-only
327 (cpp-make-overlay-read-only overlay)
328 (cpp-make-overlay-sticky overlay)))
329 (cpp-make-unknown-overlay from to))))
330
331 (defun cpp-parse-error (error)
332 ;; Error message issued by the cpp parser.
333 (error "%s at line %d" error (count-lines (point-min) (point))))
334
335 (defun cpp-parse-reset ()
336 "Reset display of cpp conditionals to normal."
337 (interactive)
338 (while cpp-overlay-list
339 (delete-overlay (car cpp-overlay-list))
340 (setq cpp-overlay-list (cdr cpp-overlay-list))))
341
342 ;;;###autoload
343 (defun cpp-parse-edit ()
344 "Edit display information for cpp conditionals."
345 (interactive)
346 (or cpp-parse-symbols
347 (cpp-highlight-buffer t))
348 (let ((buffer (current-buffer)))
349 (pop-to-buffer "*CPP Edit*")
350 (cpp-edit-mode)
351 (setq cpp-edit-buffer buffer)
352 (cpp-edit-reset)))
353
354 ;;; Overlays:
355
356 (defun cpp-make-known-overlay (start end)
357 ;; Create an overlay for a known cpp command from START to END.
358 (let ((overlay (make-overlay start end)))
359 (if (eq cpp-known-face 'invisible)
360 (cpp-make-overlay-hidden overlay)
361 (or (eq cpp-known-face 'default)
362 (overlay-put overlay 'face cpp-known-face))
363 (if cpp-known-writable
364 ()
365 (overlay-put overlay 'modification-hooks '(cpp-signal-read-only))
366 (overlay-put overlay 'insert-in-front-hooks '(cpp-signal-read-only))))
367 (setq cpp-overlay-list (cons overlay cpp-overlay-list))))
368
369 (defun cpp-make-unknown-overlay (start end)
370 ;; Create an overlay for an unknown cpp command from START to END.
371 (let ((overlay (make-overlay start end)))
372 (cond ((eq cpp-unknown-face 'invisible)
373 (cpp-make-overlay-hidden overlay))
374 ((eq cpp-unknown-face 'default))
375 (t
376 (overlay-put overlay 'face cpp-unknown-face)))
377 (if cpp-unknown-writable
378 ()
379 (overlay-put overlay 'modification-hooks '(cpp-signal-read-only))
380 (overlay-put overlay 'insert-in-front-hooks '(cpp-signal-read-only)))
381 (setq cpp-overlay-list (cons overlay cpp-overlay-list))))
382
383 (defun cpp-make-overlay-hidden (overlay)
384 ;; Make overlay hidden and intangible.
385 (overlay-put overlay 'invisible 'cpp)
386 (overlay-put overlay 'modification-hooks '(cpp-signal-read-only))
387 (overlay-put overlay 'insert-in-front-hooks '(cpp-signal-read-only)))
388
389 (defun cpp-make-overlay-read-only (overlay)
390 ;; Make overlay read only.
391 (overlay-put overlay 'modification-hooks '(cpp-signal-read-only))
392 (overlay-put overlay 'insert-in-front-hooks '(cpp-signal-read-only))
393 (overlay-put overlay 'insert-behind-hooks '(cpp-signal-read-only)))
394
395 (defun cpp-make-overlay-sticky (overlay)
396 ;; Make OVERLAY grow when you insert text at either end.
397 (overlay-put overlay 'insert-in-front-hooks '(cpp-grow-overlay))
398 (overlay-put overlay 'insert-behind-hooks '(cpp-grow-overlay)))
399
400 (defun cpp-signal-read-only (overlay after start end &optional _len)
401 ;; Only allow deleting the whole overlay.
402 ;; Trying to change a read-only overlay.
403 (if (and (not after)
404 (or (< (overlay-start overlay) start)
405 (> (overlay-end overlay) end)))
406 (error "This text is read only")))
407
408 (defun cpp-grow-overlay (overlay after start end &optional _len)
409 ;; Make OVERLAY grow to contain range START to END.
410 (if after
411 (move-overlay overlay
412 (min start (overlay-start overlay))
413 (max end (overlay-end overlay)))))
414
415 ;;; Edit Buffer:
416
417 (defvar cpp-edit-mode-map
418 (let ((map (make-keymap)))
419 (suppress-keymap map)
420 (define-key map [ down-mouse-2 ] 'cpp-push-button)
421 (define-key map [ mouse-2 ] 'ignore)
422 (define-key map " " 'scroll-up-command)
423 (define-key map "\C-?" 'scroll-down-command)
424 (define-key map [ delete ] 'scroll-down)
425 (define-key map "\C-c\C-c" 'cpp-edit-apply)
426 (define-key map "a" 'cpp-edit-apply)
427 (define-key map "A" 'cpp-edit-apply)
428 (define-key map "r" 'cpp-edit-reset)
429 (define-key map "R" 'cpp-edit-reset)
430 (define-key map "s" 'cpp-edit-save)
431 (define-key map "S" 'cpp-edit-save)
432 (define-key map "l" 'cpp-edit-load)
433 (define-key map "L" 'cpp-edit-load)
434 (define-key map "h" 'cpp-edit-home)
435 (define-key map "H" 'cpp-edit-home)
436 (define-key map "b" 'cpp-edit-background)
437 (define-key map "B" 'cpp-edit-background)
438 (define-key map "k" 'cpp-edit-known)
439 (define-key map "K" 'cpp-edit-known)
440 (define-key map "u" 'cpp-edit-unknown)
441 (define-key map "u" 'cpp-edit-unknown)
442 (define-key map "t" 'cpp-edit-true)
443 (define-key map "T" 'cpp-edit-true)
444 (define-key map "f" 'cpp-edit-false)
445 (define-key map "F" 'cpp-edit-false)
446 (define-key map "w" 'cpp-edit-write)
447 (define-key map "W" 'cpp-edit-write)
448 (define-key map "X" 'cpp-edit-toggle-known)
449 (define-key map "x" 'cpp-edit-toggle-known)
450 (define-key map "Y" 'cpp-edit-toggle-unknown)
451 (define-key map "y" 'cpp-edit-toggle-unknown)
452 (define-key map "q" 'bury-buffer)
453 (define-key map "Q" 'bury-buffer)
454 map)
455 "Keymap for `cpp-edit-mode'.")
456
457
458
459 (defvar cpp-edit-symbols nil)
460 ;; Symbols defined in the edit buffer.
461 (make-variable-buffer-local 'cpp-edit-symbols)
462
463 (define-derived-mode cpp-edit-mode fundamental-mode "CPP Edit"
464 "Major mode for editing the criteria for highlighting cpp conditionals.
465 Click on objects to change them.
466 You can also use the keyboard accelerators indicated like this: [K]ey."
467 (buffer-disable-undo)
468 (auto-save-mode -1)
469 (setq buffer-read-only t))
470
471 (defun cpp-edit-apply ()
472 "Apply edited display information to original buffer."
473 (interactive)
474 (cpp-edit-home)
475 (cpp-highlight-buffer t))
476
477 (defun cpp-edit-reset ()
478 "Reset display information from original buffer."
479 (interactive)
480 (let ((buffer (current-buffer))
481 (buffer-read-only nil)
482 (start (window-start))
483 (pos (point))
484 symbols)
485 (set-buffer cpp-edit-buffer)
486 (setq symbols cpp-parse-symbols)
487 (set-buffer buffer)
488 (setq cpp-edit-symbols symbols)
489 (erase-buffer)
490 (insert "CPP Display Information for `")
491 (cpp-make-button (buffer-name cpp-edit-buffer) 'cpp-edit-home)
492 (insert "'\n\nClick mouse-2 on item you want to change or use\n"
493 "or switch to this buffer and type the keyboard equivalents.\n"
494 "Keyboard equivalents are indicated with brackets like [T]his.\n\n")
495 (cpp-make-button "[H]ome (display the C file)" 'cpp-edit-home)
496 (insert " ")
497 (cpp-make-button "[A]pply new settings" 'cpp-edit-apply)
498 (insert "\n")
499 (cpp-make-button "[S]ave settings" 'cpp-edit-save)
500 (insert " ")
501 (cpp-make-button "[L]oad settings" 'cpp-edit-load)
502 (insert "\n\n")
503
504 (insert "[B]ackground: ")
505 (cpp-make-button (car (rassq cpp-face-type cpp-face-type-list))
506 'cpp-edit-background)
507 (insert "\n[K]nown conditionals: ")
508 (cpp-make-button (cpp-face-name cpp-known-face)
509 'cpp-edit-known nil t)
510 (insert " [X] ")
511 (cpp-make-button (car (rassq cpp-known-writable cpp-writable-list))
512 'cpp-edit-toggle-known)
513 (insert "\n[U]nknown conditionals: ")
514 (cpp-make-button (cpp-face-name cpp-unknown-face)
515 'cpp-edit-unknown nil t)
516 (insert " [Y] ")
517 (cpp-make-button (car (rassq cpp-unknown-writable cpp-writable-list))
518 'cpp-edit-toggle-unknown)
519 (insert (format "\n\n\n%39s: %14s %14s %7s\n\n" "Expression"
520 "[T]rue Face" "[F]alse Face" "[W]rite"))
521
522 (setq symbols (reverse symbols))
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 (derived-mode-p '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 (with-current-buffer cpp-edit-buffer
573 (let ((buffer (find-file-noselect cpp-config-file)))
574 (set-buffer buffer)
575 (erase-buffer)
576 (pp (list 'setq 'cpp-known-face
577 (list 'quote cpp-known-face)) buffer)
578 (pp (list 'setq 'cpp-unknown-face
579 (list 'quote cpp-unknown-face)) buffer)
580 (pp (list 'setq 'cpp-face-type
581 (list 'quote cpp-face-type)) buffer)
582 (pp (list 'setq 'cpp-known-writable
583 (list 'quote cpp-known-writable)) buffer)
584 (pp (list 'setq 'cpp-unknown-writable
585 (list 'quote cpp-unknown-writable)) buffer)
586 (pp (list 'setq 'cpp-edit-list
587 (list 'quote cpp-edit-list)) buffer)
588 (write-file cpp-config-file))))
589
590 (defun cpp-edit-home ()
591 "Switch back to original buffer."
592 (interactive)
593 (if cpp-button-event
594 (read-event))
595 (pop-to-buffer cpp-edit-buffer))
596
597 (defun cpp-edit-background ()
598 "Change default face collection."
599 (interactive)
600 (call-interactively 'cpp-choose-default-face)
601 (cpp-edit-reset))
602
603 (defun cpp-edit-known ()
604 "Select default for known conditionals."
605 (interactive)
606 (setq cpp-known-face (cpp-choose-face "Known face" cpp-known-face))
607 (cpp-edit-reset))
608
609 (defun cpp-edit-unknown ()
610 "Select default for unknown conditionals."
611 (interactive)
612 (setq cpp-unknown-face (cpp-choose-face "Unknown face" cpp-unknown-face))
613 (cpp-edit-reset))
614
615 (defun cpp-edit-toggle-known (arg)
616 "Toggle writable status for known conditionals.
617 With optional argument ARG, make them writable if ARG is positive,
618 otherwise make them unwritable."
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 if ARG is positive,
629 otherwise make them unwritable."
630 (interactive "@P")
631 (if (or (and (null arg) cpp-unknown-writable)
632 (<= (prefix-numeric-value arg) 0))
633 (setq cpp-unknown-writable nil)
634 (setq cpp-unknown-writable t))
635 (cpp-edit-reset))
636
637 (defun cpp-edit-true (symbol face)
638 "Select SYMBOL's true FACE used for highlighting taken conditionals."
639 (interactive
640 (let ((symbol (cpp-choose-symbol)))
641 (list symbol
642 (cpp-choose-face "True face"
643 (nth 1 (assoc symbol cpp-edit-list))))))
644 (setcar (nthcdr 1 (cpp-edit-list-entry-get-or-create symbol)) face)
645 (cpp-edit-reset))
646
647 (defun cpp-edit-false (symbol face)
648 "Select SYMBOL's false FACE used for highlighting untaken conditionals."
649 (interactive
650 (let ((symbol (cpp-choose-symbol)))
651 (list symbol
652 (cpp-choose-face "False face"
653 (nth 2 (assoc symbol cpp-edit-list))))))
654 (setcar (nthcdr 2 (cpp-edit-list-entry-get-or-create symbol)) face)
655 (cpp-edit-reset))
656
657 (defun cpp-edit-write (symbol branch)
658 "Set which branches of SYMBOL should be writable to BRANCH.
659 BRANCH should be either nil (false branch), t (true branch) or 'both."
660 (interactive (list (cpp-choose-symbol) (cpp-choose-branch)))
661 (setcar (nthcdr 3 (cpp-edit-list-entry-get-or-create symbol)) branch)
662 (cpp-edit-reset))
663
664 (defun cpp-edit-list-entry-get-or-create (symbol)
665 ;; Return the entry for SYMBOL in `cpp-edit-list'.
666 ;; If it does not exist, create it.
667 (let ((entry (assoc symbol cpp-edit-list)))
668 (or entry
669 (setq entry (list symbol nil nil 'both nil)
670 cpp-edit-list (cons entry cpp-edit-list)))
671 entry))
672
673 ;;; Prompts:
674
675 (defun cpp-choose-symbol ()
676 ;; Choose a symbol if called from keyboard, otherwise use the one clicked on.
677 (if cpp-button-event
678 cpp-callback-data
679 (completing-read "Symbol: " cpp-edit-symbols nil t)))
680
681 (defun cpp-choose-branch ()
682 ;; Choose a branch, either nil, t, or both.
683 (if cpp-button-event
684 (x-popup-menu cpp-button-event
685 (list "Branch" (cons "Branch" cpp-branch-list)))
686 (cdr (assoc (completing-read "Branch: " cpp-branch-list nil t)
687 cpp-branch-list))))
688
689 (defun cpp-choose-face (prompt default)
690 ;; Choose a face from cpp-face-default-list.
691 ;; PROMPT is what to say to the user.
692 ;; DEFAULT is the default face.
693 (or (if cpp-button-event
694 (x-popup-menu cpp-button-event
695 (list prompt (cons prompt cpp-face-default-list)))
696 (let ((name (car (rassq default cpp-face-default-list))))
697 (cdr (assoc (completing-read (if name
698 (concat prompt
699 " (default " name "): ")
700 (concat prompt ": "))
701 cpp-face-default-list nil t)
702 cpp-face-all-list))))
703 default))
704
705 (defun cpp-choose-default-face (type)
706 ;; Choose default face list for screen of TYPE.
707 ;; Type must be one of the types defined in `cpp-face-type-list'.
708 (interactive (list (if cpp-button-event
709 (x-popup-menu cpp-button-event
710 (list "Screen type"
711 (cons "Screen type"
712 cpp-face-type-list)))
713 (cdr (assoc (completing-read "Screen type: "
714 cpp-face-type-list
715 nil t)
716 cpp-face-type-list)))))
717 (cond ((null type))
718 ((eq type 'light)
719 (if cpp-face-light-list
720 ()
721 (setq cpp-face-light-list
722 (mapcar 'cpp-create-bg-face cpp-face-light-name-list))
723 (setq cpp-face-all-list
724 (append cpp-face-all-list cpp-face-light-list)))
725 (setq cpp-face-type 'light)
726 (setq cpp-face-default-list
727 (append cpp-face-light-list cpp-face-none-list)))
728 ((eq type 'dark)
729 (if cpp-face-dark-list
730 ()
731 (setq cpp-face-dark-list
732 (mapcar 'cpp-create-bg-face cpp-face-dark-name-list))
733 (setq cpp-face-all-list
734 (append cpp-face-all-list cpp-face-dark-list)))
735 (setq cpp-face-type 'dark)
736 (setq cpp-face-default-list
737 (append cpp-face-dark-list cpp-face-none-list)))
738 ((eq type 'mono)
739 (setq cpp-face-type 'mono)
740 (setq cpp-face-default-list
741 (append cpp-face-mono-list cpp-face-none-list)))
742 (t
743 (setq cpp-face-type 'none)
744 (setq cpp-face-default-list cpp-face-none-list))))
745
746 ;;; Buttons:
747
748 (defun cpp-make-button (name callback &optional data face padding)
749 ;; Create a button at point.
750 ;; NAME is the name of the button.
751 ;; CALLBACK is the function to call when the button is pushed.
752 ;; DATA will be made available to CALLBACK
753 ;;in the free variable cpp-callback-data.
754 ;; FACE means that NAME is the name of a face in `cpp-face-all-list'.
755 ;; PADDING means NAME will be right justified at that length.
756 (let ((name (format "%s" name))
757 from to)
758 (cond ((null padding)
759 (setq from (point))
760 (insert name))
761 ((> (length name) padding)
762 (setq from (point))
763 (insert (substring name 0 padding)))
764 (t
765 (insert (make-string (- padding (length name)) ? ))
766 (setq from (point))
767 (insert name)))
768 (setq to (point))
769 (setq face
770 (if face
771 (let ((check (cdr (assoc name cpp-face-all-list))))
772 (if (memq check '(default invisible))
773 'bold
774 check))
775 'bold))
776 (add-text-properties from to
777 (append (list 'face face)
778 '(mouse-face highlight)
779 '(help-echo "mouse-2: change/use this item")
780 (list 'cpp-callback callback)
781 (if data (list 'cpp-data data))))))
782
783 (defun cpp-push-button (event)
784 ;; Pushed a CPP button.
785 (interactive "@e")
786 (set-buffer (window-buffer (posn-window (event-start event))))
787 (let ((pos (posn-point (event-start event))))
788 (let ((cpp-callback-data (get-text-property pos 'cpp-data))
789 (fun (get-text-property pos 'cpp-callback))
790 (cpp-button-event event))
791 (cond (fun
792 (call-interactively (get-text-property pos 'cpp-callback)))
793 ((lookup-key global-map [ down-mouse-2])
794 (call-interactively (lookup-key global-map [ down-mouse-2])))))))
795
796 ;;; Faces:
797
798 (defun cpp-create-bg-face (color)
799 ;; Create entry for face with background COLOR.
800 (cons color (cons 'background-color color)))
801
802 (cpp-choose-default-face
803 (if (or window-system (display-color-p)) cpp-face-type 'none))
804
805 (defun cpp-face-name (face)
806 ;; Return the name of FACE from `cpp-face-all-list'.
807 (let ((entry (rassq (if face face 'default) cpp-face-all-list)))
808 (if entry
809 (car entry)
810 (format "<%s>" face))))
811
812 ;;; Utilities:
813
814 (defvar cpp-progress-time 0)
815 ;; Last time we issued a progress message.
816
817 (defun cpp-progress-message (&rest args)
818 ;; Report progress at most once a second. Take same ARGS as `message'.
819 (let ((time (nth 1 (current-time))))
820 (if (= time cpp-progress-time)
821 ()
822 (setq cpp-progress-time time)
823 (apply 'message args))))
824
825 (provide 'cpp)
826
827 ;;; cpp.el ends here