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