Update copyright year to 2014 by running admin/update-copyright.
[emacs.git] / lisp / progmodes / cpp.el
blobc5fa5398dccb4c152188792cdce4cf374aec0106
1 ;;; cpp.el --- highlight or hide text according to cpp conditionals
3 ;; Copyright (C) 1994-1995, 2001-2014 Free Software Foundation, Inc.
5 ;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
6 ;; Keywords: c, faces, tools
8 ;; This file is part of GNU Emacs.
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.
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.
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/>.
23 ;;; Commentary:
25 ;; Parse a text for C preprocessor conditionals, and highlight or hide
26 ;; the text inside the conditionals as you wish.
28 ;; This package is inspired by Jim Coplien's delta editor for SCCS.
30 ;;; Todo:
32 ;; Should parse "#if" and "#elif" expressions and merge the faces
33 ;; somehow.
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.
39 ;; The Edit buffer should -- optionally -- appear in its own frame.
41 ;; Conditionals seem to be rear-sticky. They shouldn't be.
43 ;; Restore window configurations when exiting CPP Edit buffer.
45 ;;; Code:
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-")
54 (defcustom cpp-config-file (convert-standard-filename ".cpp.el")
55 "File name to save cpp configuration."
56 :type 'file
57 :group 'cpp)
59 (define-widget 'cpp-face 'lazy
60 "Either a face or the special symbol 'invisible'."
61 :type '(choice (const invisible) (face)))
63 (defcustom cpp-known-face 'invisible
64 "Face used for known cpp symbols."
65 :type 'cpp-face
66 :group 'cpp)
68 (defcustom cpp-unknown-face 'highlight
69 "Face used for unknown cpp symbols."
70 :type 'cpp-face
71 :group 'cpp)
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)
82 (defcustom cpp-known-writable t
83 "Non-nil means you are allowed to modify the known conditionals."
84 :type 'boolean
85 :group 'cpp)
87 (defcustom cpp-unknown-writable t
88 "Non-nil means you are allowed to modify the unknown conditionals."
89 :type 'boolean
90 :group 'cpp)
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)
107 (defvar cpp-overlay-list nil)
108 ;; List of cpp overlays active in the current buffer.
109 (make-variable-buffer-local 'cpp-overlay-list)
111 (defvar cpp-callback-data)
112 (defvar cpp-state-stack)
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.")
121 (defconst cpp-writable-list
122 ;; Names used for the writable property.
123 '(("writable" . t)
124 ("read-only" . nil)))
126 (defvar cpp-button-event nil)
127 ;; This will be t in the callback for `cpp-make-button'.
129 (defvar cpp-edit-buffer nil)
130 ;; Real buffer whose cpp display information we are editing.
131 (make-variable-buffer-local 'cpp-edit-buffer)
133 (defconst cpp-branch-list
134 ;; Alist of branches.
135 '(("false" . nil)
136 ("true" . t)
137 ("both" . both)))
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)
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)
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)
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)
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)
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)
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)
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.")
204 ;;; Parse Buffer:
206 (defvar cpp-parse-symbols nil
207 "List of cpp macros used in the local buffer.")
208 (make-variable-buffer-local 'cpp-parse-symbols)
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\\)"))
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 (/ (* 100 (- (point) (point-min))) (buffer-size)))
238 (let ((match (buffer-substring (match-beginning 0) (match-end 0))))
239 (cond ((or (string-equal match "'")
240 (string-equal match "\""))
241 (goto-char (match-beginning 0))
242 (condition-case nil
243 (forward-sexp)
244 (error (cpp-parse-error
245 "Unterminated string or character"))))
246 ((string-equal match "/*")
247 (or (search-forward "*/" nil t)
248 (error "Unterminated comment")))
249 ((string-equal match "//")
250 (skip-chars-forward "^\n\r"))
252 (end-of-line 1)
253 (let ((from (match-beginning 1))
254 (to (1+ (point)))
255 (type (buffer-substring (match-beginning 2)
256 (match-end 2)))
257 (expr (buffer-substring (match-end 1) (point))))
258 (cond ((string-equal type "ifdef")
259 (cpp-parse-open t expr from to))
260 ((string-equal type "ifndef")
261 (cpp-parse-open nil expr from to))
262 ((string-equal type "if")
263 (cpp-parse-open t expr from to))
264 ((string-equal type "elif")
265 (let (cpp-known-face cpp-unknown-face)
266 (cpp-parse-close from to))
267 (cpp-parse-open t expr from to))
268 ((string-equal type "else")
269 (or cpp-state-stack
270 (cpp-parse-error "Top level #else"))
271 (let ((entry (list (not (nth 0 (car cpp-state-stack)))
272 (nth 1 (car cpp-state-stack))
273 from to)))
274 (cpp-parse-close from to)
275 (setq cpp-state-stack (cons entry cpp-state-stack))))
276 ((string-equal type "endif")
277 (cpp-parse-close from to))
279 (cpp-parse-error "Parser error"))))))))
280 (message "Parsing...done"))
281 (if cpp-state-stack
282 (save-excursion
283 (goto-char (nth 3 (car cpp-state-stack)))
284 (cpp-parse-error "Unclosed conditional"))))
285 (or arg
286 (null cpp-parse-symbols)
287 (cpp-parse-edit)))
289 (defun cpp-parse-open (branch expr begin end)
290 "Push information about conditional-beginning onto `cpp-state-stack'."
291 ;; Discard comments within this line.
292 (while (string-match "\\b[ \t]*/\\*.*\\*/[ \t]*\\b" expr)
293 (setq expr (concat (substring expr 0 (match-beginning 0))
294 (substring expr (match-end 0)))))
295 ;; If a comment starts on this line and continues past, discard it.
296 (if (string-match "\\b[ \t]*/\\*" expr)
297 (setq expr (substring expr 0 (match-beginning 0))))
298 ;; Delete any C++ comment from the line.
299 (if (string-match "\\b[ \t]*\\(//.*\\)?$" expr)
300 (setq expr (substring expr 0 (match-beginning 0))))
301 (while (string-match "[ \t]+" expr)
302 (setq expr (concat (substring expr 0 (match-beginning 0))
303 (substring expr (match-end 0)))))
304 (setq cpp-state-stack (cons (list branch expr begin end) cpp-state-stack))
305 (or (member expr cpp-parse-symbols)
306 (setq cpp-parse-symbols
307 (cons expr cpp-parse-symbols)))
308 (if (assoc expr cpp-edit-list)
309 (cpp-make-known-overlay begin end)
310 (cpp-make-unknown-overlay begin end)))
312 (defun cpp-parse-close (from to)
313 ;; Pop top of cpp-state-stack and create overlay.
314 (let ((entry (assoc (nth 1 (car cpp-state-stack)) cpp-edit-list))
315 (branch (nth 0 (car cpp-state-stack)))
316 (end (nth 3 (car cpp-state-stack))))
317 (setq cpp-state-stack (cdr cpp-state-stack))
318 (if entry
319 (let ((face (nth (if branch 1 2) entry))
320 (read-only (eq (not branch) (nth 3 entry)))
321 (priority (length cpp-state-stack))
322 (overlay (make-overlay end from)))
323 (cpp-make-known-overlay from to)
324 (setq cpp-overlay-list (cons overlay cpp-overlay-list))
325 (if priority (overlay-put overlay 'priority priority))
326 (cond ((eq face 'invisible)
327 (cpp-make-overlay-hidden overlay))
328 ((eq face 'default))
330 (overlay-put overlay 'face face)))
331 (if read-only
332 (cpp-make-overlay-read-only overlay)
333 (cpp-make-overlay-sticky overlay)))
334 (cpp-make-unknown-overlay from to))))
336 (defun cpp-parse-error (error)
337 ;; Error message issued by the cpp parser.
338 (error "%s at line %d" error (count-lines (point-min) (point))))
340 (defun cpp-parse-reset ()
341 "Reset display of cpp conditionals to normal."
342 (interactive)
343 (while cpp-overlay-list
344 (delete-overlay (car cpp-overlay-list))
345 (setq cpp-overlay-list (cdr cpp-overlay-list))))
347 ;;;###autoload
348 (defun cpp-parse-edit ()
349 "Edit display information for cpp conditionals."
350 (interactive)
351 (or cpp-parse-symbols
352 (cpp-highlight-buffer t))
353 (let ((buffer (current-buffer)))
354 (pop-to-buffer "*CPP Edit*")
355 (cpp-edit-mode)
356 (setq cpp-edit-buffer buffer)
357 (cpp-edit-reset)))
359 ;;; Overlays:
361 (defun cpp-make-known-overlay (start end)
362 ;; Create an overlay for a known cpp command from START to END.
363 (let ((overlay (make-overlay start end)))
364 (if (eq cpp-known-face 'invisible)
365 (cpp-make-overlay-hidden overlay)
366 (or (eq cpp-known-face 'default)
367 (overlay-put overlay 'face cpp-known-face))
368 (if cpp-known-writable
370 (overlay-put overlay 'modification-hooks '(cpp-signal-read-only))
371 (overlay-put overlay 'insert-in-front-hooks '(cpp-signal-read-only))))
372 (setq cpp-overlay-list (cons overlay cpp-overlay-list))))
374 (defun cpp-make-unknown-overlay (start end)
375 ;; Create an overlay for an unknown cpp command from START to END.
376 (let ((overlay (make-overlay start end)))
377 (cond ((eq cpp-unknown-face 'invisible)
378 (cpp-make-overlay-hidden overlay))
379 ((eq cpp-unknown-face 'default))
381 (overlay-put overlay 'face cpp-unknown-face)))
382 (if cpp-unknown-writable
384 (overlay-put overlay 'modification-hooks '(cpp-signal-read-only))
385 (overlay-put overlay 'insert-in-front-hooks '(cpp-signal-read-only)))
386 (setq cpp-overlay-list (cons overlay cpp-overlay-list))))
388 (defun cpp-make-overlay-hidden (overlay)
389 ;; Make overlay hidden and intangible.
390 (overlay-put overlay 'invisible 'cpp)
391 (overlay-put overlay 'modification-hooks '(cpp-signal-read-only))
392 (overlay-put overlay 'insert-in-front-hooks '(cpp-signal-read-only)))
394 (defun cpp-make-overlay-read-only (overlay)
395 ;; Make overlay read only.
396 (overlay-put overlay 'modification-hooks '(cpp-signal-read-only))
397 (overlay-put overlay 'insert-in-front-hooks '(cpp-signal-read-only))
398 (overlay-put overlay 'insert-behind-hooks '(cpp-signal-read-only)))
400 (defun cpp-make-overlay-sticky (overlay)
401 ;; Make OVERLAY grow when you insert text at either end.
402 (overlay-put overlay 'insert-in-front-hooks '(cpp-grow-overlay))
403 (overlay-put overlay 'insert-behind-hooks '(cpp-grow-overlay)))
405 (defun cpp-signal-read-only (overlay after start end &optional _len)
406 ;; Only allow deleting the whole overlay.
407 ;; Trying to change a read-only overlay.
408 (if (and (not after)
409 (or (< (overlay-start overlay) start)
410 (> (overlay-end overlay) end)))
411 (error "This text is read only")))
413 (defun cpp-grow-overlay (overlay after start end &optional _len)
414 ;; Make OVERLAY grow to contain range START to END.
415 (if after
416 (move-overlay overlay
417 (min start (overlay-start overlay))
418 (max end (overlay-end overlay)))))
420 ;;; Edit Buffer:
422 (defvar cpp-edit-mode-map
423 (let ((map (make-keymap)))
424 (suppress-keymap map)
425 (define-key map [ down-mouse-2 ] 'cpp-push-button)
426 (define-key map [ mouse-2 ] 'ignore)
427 (define-key map " " 'scroll-up-command)
428 (define-key map [?\S-\ ] 'scroll-down-command)
429 (define-key map "\C-?" 'scroll-down-command)
430 (define-key map [ delete ] 'scroll-down)
431 (define-key map "\C-c\C-c" 'cpp-edit-apply)
432 (define-key map "a" 'cpp-edit-apply)
433 (define-key map "A" 'cpp-edit-apply)
434 (define-key map "r" 'cpp-edit-reset)
435 (define-key map "R" 'cpp-edit-reset)
436 (define-key map "s" 'cpp-edit-save)
437 (define-key map "S" 'cpp-edit-save)
438 (define-key map "l" 'cpp-edit-load)
439 (define-key map "L" 'cpp-edit-load)
440 (define-key map "h" 'cpp-edit-home)
441 (define-key map "H" 'cpp-edit-home)
442 (define-key map "b" 'cpp-edit-background)
443 (define-key map "B" 'cpp-edit-background)
444 (define-key map "k" 'cpp-edit-known)
445 (define-key map "K" 'cpp-edit-known)
446 (define-key map "u" 'cpp-edit-unknown)
447 (define-key map "u" 'cpp-edit-unknown)
448 (define-key map "t" 'cpp-edit-true)
449 (define-key map "T" 'cpp-edit-true)
450 (define-key map "f" 'cpp-edit-false)
451 (define-key map "F" 'cpp-edit-false)
452 (define-key map "w" 'cpp-edit-write)
453 (define-key map "W" 'cpp-edit-write)
454 (define-key map "X" 'cpp-edit-toggle-known)
455 (define-key map "x" 'cpp-edit-toggle-known)
456 (define-key map "Y" 'cpp-edit-toggle-unknown)
457 (define-key map "y" 'cpp-edit-toggle-unknown)
458 (define-key map "q" 'bury-buffer)
459 (define-key map "Q" 'bury-buffer)
460 map)
461 "Keymap for `cpp-edit-mode'.")
465 (defvar cpp-edit-symbols nil)
466 ;; Symbols defined in the edit buffer.
467 (make-variable-buffer-local 'cpp-edit-symbols)
469 (define-derived-mode cpp-edit-mode fundamental-mode "CPP Edit"
470 "Major mode for editing the criteria for highlighting cpp conditionals.
471 Click on objects to change them.
472 You can also use the keyboard accelerators indicated like this: [K]ey."
473 (buffer-disable-undo)
474 (auto-save-mode -1)
475 (setq buffer-read-only t))
477 (defun cpp-edit-apply ()
478 "Apply edited display information to original buffer."
479 (interactive)
480 (cpp-edit-home)
481 (cpp-highlight-buffer t))
483 (defun cpp-edit-reset ()
484 "Reset display information from original buffer."
485 (interactive)
486 (let ((buffer (current-buffer))
487 (buffer-read-only nil)
488 (start (window-start))
489 (pos (point))
490 symbols)
491 (set-buffer cpp-edit-buffer)
492 (setq symbols cpp-parse-symbols)
493 (set-buffer buffer)
494 (setq cpp-edit-symbols symbols)
495 (erase-buffer)
496 (insert "CPP Display Information for `")
497 (cpp-make-button (buffer-name cpp-edit-buffer) 'cpp-edit-home)
498 (insert "'\n\nClick mouse-2 on item you want to change or use\n"
499 "or switch to this buffer and type the keyboard equivalents.\n"
500 "Keyboard equivalents are indicated with brackets like [T]his.\n\n")
501 (cpp-make-button "[H]ome (display the C file)" 'cpp-edit-home)
502 (insert " ")
503 (cpp-make-button "[A]pply new settings" 'cpp-edit-apply)
504 (insert "\n")
505 (cpp-make-button "[S]ave settings" 'cpp-edit-save)
506 (insert " ")
507 (cpp-make-button "[L]oad settings" 'cpp-edit-load)
508 (insert "\n\n")
510 (insert "[B]ackground: ")
511 (cpp-make-button (car (rassq cpp-face-type cpp-face-type-list))
512 'cpp-edit-background)
513 (insert "\n[K]nown conditionals: ")
514 (cpp-make-button (cpp-face-name cpp-known-face)
515 'cpp-edit-known nil t)
516 (insert " [X] ")
517 (cpp-make-button (car (rassq cpp-known-writable cpp-writable-list))
518 'cpp-edit-toggle-known)
519 (insert "\n[U]nknown conditionals: ")
520 (cpp-make-button (cpp-face-name cpp-unknown-face)
521 'cpp-edit-unknown nil t)
522 (insert " [Y] ")
523 (cpp-make-button (car (rassq cpp-unknown-writable cpp-writable-list))
524 'cpp-edit-toggle-unknown)
525 (insert (format "\n\n\n%39s: %14s %14s %7s\n\n" "Expression"
526 "[T]rue Face" "[F]alse Face" "[W]rite"))
528 (setq symbols (reverse symbols))
529 (while symbols
530 (let* ((symbol (car symbols))
531 (entry (assoc symbol cpp-edit-list))
532 (true (nth 1 entry))
533 (false (nth 2 entry))
534 (write (if entry (nth 3 entry) 'both)))
535 (setq symbols (cdr symbols))
537 (if (and entry ; Make default entries unknown.
538 (or (null true) (eq true 'default))
539 (or (null false) (eq false 'default))
540 (eq write 'both))
541 (setq cpp-edit-list (delq entry cpp-edit-list)
542 entry nil))
544 (if (> (length symbol) 39)
545 (insert (substring symbol 0 39) ": ")
546 (insert (format "%39s: " symbol)))
548 (cpp-make-button (cpp-face-name true)
549 'cpp-edit-true symbol t 14)
550 (insert " ")
551 (cpp-make-button (cpp-face-name false)
552 'cpp-edit-false symbol t 14)
553 (insert " ")
554 (cpp-make-button (car (rassq write cpp-branch-list))
555 'cpp-edit-write symbol nil 6)
556 (insert "\n")))
557 (insert "\n\n")
558 (set-window-start nil start)
559 (goto-char pos)))
561 (defun cpp-edit-load ()
562 "Load cpp configuration."
563 (interactive)
564 (cond ((null init-file-user)
565 ;; If -q was specified, don't load any init files.
566 nil)
567 ((file-readable-p cpp-config-file)
568 (load-file cpp-config-file))
569 ((file-readable-p (concat "~/" cpp-config-file))
570 (load-file cpp-config-file)))
571 (if (derived-mode-p 'cpp-edit-mode)
572 (cpp-edit-reset)))
574 (defun cpp-edit-save ()
575 "Save the current cpp configuration in a file."
576 (interactive)
577 (require 'pp)
578 (with-current-buffer cpp-edit-buffer
579 (let ((buffer (find-file-noselect cpp-config-file)))
580 (set-buffer buffer)
581 (erase-buffer)
582 (pp (list 'setq 'cpp-known-face
583 (list 'quote cpp-known-face)) buffer)
584 (pp (list 'setq 'cpp-unknown-face
585 (list 'quote cpp-unknown-face)) buffer)
586 (pp (list 'setq 'cpp-face-type
587 (list 'quote cpp-face-type)) buffer)
588 (pp (list 'setq 'cpp-known-writable
589 (list 'quote cpp-known-writable)) buffer)
590 (pp (list 'setq 'cpp-unknown-writable
591 (list 'quote cpp-unknown-writable)) buffer)
592 (pp (list 'setq 'cpp-edit-list
593 (list 'quote cpp-edit-list)) buffer)
594 (write-file cpp-config-file))))
596 (defun cpp-edit-home ()
597 "Switch back to original buffer."
598 (interactive)
599 (if cpp-button-event
600 (read-event))
601 (pop-to-buffer cpp-edit-buffer))
603 (defun cpp-edit-background ()
604 "Change default face collection."
605 (interactive)
606 (call-interactively 'cpp-choose-default-face)
607 (cpp-edit-reset))
609 (defun cpp-edit-known ()
610 "Select default for known conditionals."
611 (interactive)
612 (setq cpp-known-face (cpp-choose-face "Known face" cpp-known-face))
613 (cpp-edit-reset))
615 (defun cpp-edit-unknown ()
616 "Select default for unknown conditionals."
617 (interactive)
618 (setq cpp-unknown-face (cpp-choose-face "Unknown face" cpp-unknown-face))
619 (cpp-edit-reset))
621 (defun cpp-edit-toggle-known (arg)
622 "Toggle writable status for known conditionals.
623 With optional argument ARG, make them writable if ARG is positive,
624 otherwise make them unwritable."
625 (interactive "@P")
626 (if (or (and (null arg) cpp-known-writable)
627 (<= (prefix-numeric-value arg) 0))
628 (setq cpp-known-writable nil)
629 (setq cpp-known-writable t))
630 (cpp-edit-reset))
632 (defun cpp-edit-toggle-unknown (arg)
633 "Toggle writable status for unknown conditionals.
634 With optional argument ARG, make them writable if ARG is positive,
635 otherwise make them unwritable."
636 (interactive "@P")
637 (if (or (and (null arg) cpp-unknown-writable)
638 (<= (prefix-numeric-value arg) 0))
639 (setq cpp-unknown-writable nil)
640 (setq cpp-unknown-writable t))
641 (cpp-edit-reset))
643 (defun cpp-edit-true (symbol face)
644 "Select SYMBOL's true FACE used for highlighting taken conditionals."
645 (interactive
646 (let ((symbol (cpp-choose-symbol)))
647 (list symbol
648 (cpp-choose-face "True face"
649 (nth 1 (assoc symbol cpp-edit-list))))))
650 (setcar (nthcdr 1 (cpp-edit-list-entry-get-or-create symbol)) face)
651 (cpp-edit-reset))
653 (defun cpp-edit-false (symbol face)
654 "Select SYMBOL's false FACE used for highlighting untaken conditionals."
655 (interactive
656 (let ((symbol (cpp-choose-symbol)))
657 (list symbol
658 (cpp-choose-face "False face"
659 (nth 2 (assoc symbol cpp-edit-list))))))
660 (setcar (nthcdr 2 (cpp-edit-list-entry-get-or-create symbol)) face)
661 (cpp-edit-reset))
663 (defun cpp-edit-write (symbol branch)
664 "Set which branches of SYMBOL should be writable to BRANCH.
665 BRANCH should be either nil (false branch), t (true branch) or 'both."
666 (interactive (list (cpp-choose-symbol) (cpp-choose-branch)))
667 (setcar (nthcdr 3 (cpp-edit-list-entry-get-or-create symbol)) branch)
668 (cpp-edit-reset))
670 (defun cpp-edit-list-entry-get-or-create (symbol)
671 ;; Return the entry for SYMBOL in `cpp-edit-list'.
672 ;; If it does not exist, create it.
673 (let ((entry (assoc symbol cpp-edit-list)))
674 (or entry
675 (setq entry (list symbol nil nil 'both nil)
676 cpp-edit-list (cons entry cpp-edit-list)))
677 entry))
679 ;;; Prompts:
681 (defun cpp-choose-symbol ()
682 ;; Choose a symbol if called from keyboard, otherwise use the one clicked on.
683 (if cpp-button-event
684 cpp-callback-data
685 (completing-read "Symbol: " cpp-edit-symbols nil t)))
687 (defun cpp-choose-branch ()
688 ;; Choose a branch, either nil, t, or both.
689 (if cpp-button-event
690 (x-popup-menu cpp-button-event
691 (list "Branch" (cons "Branch" cpp-branch-list)))
692 (cdr (assoc (completing-read "Branch: " cpp-branch-list nil t)
693 cpp-branch-list))))
695 (defun cpp-choose-face (prompt default)
696 ;; Choose a face from cpp-face-default-list.
697 ;; PROMPT is what to say to the user.
698 ;; DEFAULT is the default face.
699 (or (if cpp-button-event
700 (x-popup-menu cpp-button-event
701 (list prompt (cons prompt cpp-face-default-list)))
702 (let ((name (car (rassq default cpp-face-default-list))))
703 (cdr (assoc (completing-read (if name
704 (concat prompt
705 " (default " name "): ")
706 (concat prompt ": "))
707 cpp-face-default-list nil t)
708 cpp-face-all-list))))
709 default))
711 (defun cpp-choose-default-face (type)
712 ;; Choose default face list for screen of TYPE.
713 ;; Type must be one of the types defined in `cpp-face-type-list'.
714 (interactive (list (if cpp-button-event
715 (x-popup-menu cpp-button-event
716 (list "Screen type"
717 (cons "Screen type"
718 cpp-face-type-list)))
719 (cdr (assoc (completing-read "Screen type: "
720 cpp-face-type-list
721 nil t)
722 cpp-face-type-list)))))
723 (cond ((null type))
724 ((eq type 'light)
725 (if cpp-face-light-list
727 (setq cpp-face-light-list
728 (mapcar 'cpp-create-bg-face cpp-face-light-name-list))
729 (setq cpp-face-all-list
730 (append cpp-face-all-list cpp-face-light-list)))
731 (setq cpp-face-type 'light)
732 (setq cpp-face-default-list
733 (append cpp-face-light-list cpp-face-none-list)))
734 ((eq type 'dark)
735 (if cpp-face-dark-list
737 (setq cpp-face-dark-list
738 (mapcar 'cpp-create-bg-face cpp-face-dark-name-list))
739 (setq cpp-face-all-list
740 (append cpp-face-all-list cpp-face-dark-list)))
741 (setq cpp-face-type 'dark)
742 (setq cpp-face-default-list
743 (append cpp-face-dark-list cpp-face-none-list)))
744 ((eq type 'mono)
745 (setq cpp-face-type 'mono)
746 (setq cpp-face-default-list
747 (append cpp-face-mono-list cpp-face-none-list)))
749 (setq cpp-face-type 'none)
750 (setq cpp-face-default-list cpp-face-none-list))))
752 ;;; Buttons:
754 (defun cpp-make-button (name callback &optional data face padding)
755 ;; Create a button at point.
756 ;; NAME is the name of the button.
757 ;; CALLBACK is the function to call when the button is pushed.
758 ;; DATA will be made available to CALLBACK
759 ;;in the free variable cpp-callback-data.
760 ;; FACE means that NAME is the name of a face in `cpp-face-all-list'.
761 ;; PADDING means NAME will be right justified at that length.
762 (let ((name (format "%s" name))
763 from to)
764 (cond ((null padding)
765 (setq from (point))
766 (insert name))
767 ((> (length name) padding)
768 (setq from (point))
769 (insert (substring name 0 padding)))
771 (insert (make-string (- padding (length name)) ? ))
772 (setq from (point))
773 (insert name)))
774 (setq to (point))
775 (setq face
776 (if face
777 (let ((check (cdr (assoc name cpp-face-all-list))))
778 (if (memq check '(default invisible))
779 'bold
780 check))
781 'bold))
782 (add-text-properties from to
783 (append (list 'face face)
784 '(mouse-face highlight)
785 '(help-echo "mouse-2: change/use this item")
786 (list 'cpp-callback callback)
787 (if data (list 'cpp-data data))))))
789 (defun cpp-push-button (event)
790 ;; Pushed a CPP button.
791 (interactive "@e")
792 (set-buffer (window-buffer (posn-window (event-start event))))
793 (let ((pos (posn-point (event-start event))))
794 (let ((cpp-callback-data (get-text-property pos 'cpp-data))
795 (fun (get-text-property pos 'cpp-callback))
796 (cpp-button-event event))
797 (cond (fun
798 (call-interactively (get-text-property pos 'cpp-callback)))
799 ((lookup-key global-map [ down-mouse-2])
800 (call-interactively (lookup-key global-map [ down-mouse-2])))))))
802 ;;; Faces:
804 (defun cpp-create-bg-face (color)
805 ;; Create entry for face with background COLOR.
806 (cons color (cons 'background-color color)))
808 (cpp-choose-default-face
809 (if (or window-system (display-color-p)) cpp-face-type 'none))
811 (defun cpp-face-name (face)
812 ;; Return the name of FACE from `cpp-face-all-list'.
813 (let ((entry (rassq (if face face 'default) cpp-face-all-list)))
814 (if entry
815 (car entry)
816 (format "<%s>" face))))
818 ;;; Utilities:
820 (defvar cpp-progress-time 0)
821 ;; Last time we issued a progress message.
823 (defun cpp-progress-message (&rest args)
824 ;; Report progress at most once a second. Take same ARGS as `message'.
825 (let ((time (nth 1 (current-time))))
826 (if (= time cpp-progress-time)
828 (setq cpp-progress-time time)
829 (apply 'message args))))
831 (provide 'cpp)
833 ;;; cpp.el ends here