(top-level): Revert previous change: require cl when compiling.
[emacs.git] / lisp / progmodes / cpp.el
blobd468954e59b26115f18826313ecaed2b3d848c8a
1 ;;; cpp.el --- highlight or hide text according to cpp conditionals
3 ;; Copyright (C) 1994, 1995, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
4 ;; Free Software Foundation
6 ;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
7 ;; Keywords: c, faces, tools
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; Parse a text for C preprocessor conditionals, and highlight or hide
27 ;; the text inside the conditionals as you wish.
29 ;; This package is inspired by Jim Coplien's delta editor for SCCS.
31 ;;; Todo:
33 ;; Should parse "#if" and "#elif" expressions and merge the faces
34 ;; somehow.
36 ;; Somehow it is sometimes possible to make changes near a read only
37 ;; area which you can't undo. Their are other strange effects in that
38 ;; area.
40 ;; The Edit buffer should -- optionally -- appear in its own frame.
42 ;; Conditionals seem to be rear-sticky. They shouldn't be.
44 ;; Restore window configurations when exiting CPP Edit buffer.
46 ;;; Code:
48 ;;; Customization:
49 (defgroup cpp nil
50 "Highlight or hide text according to cpp conditionals."
51 :link '(custom-group-link :tag "Font Lock Faces group" font-lock-faces)
52 :group 'c
53 :prefix "cpp-")
55 (defcustom cpp-config-file (convert-standard-filename ".cpp.el")
56 "*File name to save cpp configuration."
57 :type 'file
58 :group 'cpp)
60 (define-widget 'cpp-face 'lazy
61 "Either a face or the special symbol 'invisible'."
62 :type '(choice (const invisible) (face)))
64 (defcustom cpp-known-face 'invisible
65 "*Face used for known cpp symbols."
66 :type 'cpp-face
67 :group 'cpp)
69 (defcustom cpp-unknown-face 'highlight
70 "*Face used for unknown cpp symbols."
71 :type 'cpp-face
72 :group 'cpp)
74 (defcustom cpp-face-type 'light
75 "*Indicate what background face type you prefer.
76 Can be either light or dark for color screens, mono for monochrome
77 screens, and none if you don't use a window system and don't have
78 a color-capable display."
79 :options '(light dark mono nil)
80 :type 'symbol
81 :group 'cpp)
83 (defcustom cpp-known-writable t
84 "*Non-nil means you are allowed to modify the known conditionals."
85 :type 'boolean
86 :group 'cpp)
88 (defcustom cpp-unknown-writable t
89 "*Non-nil means you are allowed to modify the unknown conditionals."
90 :type 'boolean
91 :group 'cpp)
93 (defcustom cpp-edit-list nil
94 "Alist of cpp macros and information about how they should be displayed.
95 Each entry is a list with the following elements:
96 0. The name of the macro (a string).
97 1. Face used for text that is `ifdef' the macro.
98 2. Face used for text that is `ifndef' the macro.
99 3. t, nil, or `both' depending on what text may be edited."
100 :type '(repeat (list (string :tag "Macro")
101 (cpp-face :tag "True")
102 (cpp-face :tag "False")
103 (choice (const :tag "True branch writable" t)
104 (const :tag "False branch writeable" nil)
105 (const :tag "Both branches writeable" both))))
106 :group 'cpp)
108 (defvar cpp-overlay-list nil)
109 ;; List of cpp overlays active in the current buffer.
110 (make-variable-buffer-local 'cpp-overlay-list)
112 (defvar cpp-callback-data)
113 (defvar cpp-state-stack)
115 (defconst cpp-face-type-list
116 '(("light color background" . light)
117 ("dark color background" . dark)
118 ("monochrome" . mono)
119 ("tty" . none))
120 "Alist of strings and names of the defined face collections.")
122 (defconst cpp-writable-list
123 ;; Names used for the writable property.
124 '(("writable" . t)
125 ("read-only" . nil)))
127 (defvar cpp-button-event nil)
128 ;; This will be t in the callback for `cpp-make-button'.
130 (defvar cpp-edit-buffer nil)
131 ;; Real buffer whose cpp display information we are editing.
132 (make-variable-buffer-local 'cpp-edit-buffer)
134 (defconst cpp-branch-list
135 ;; Alist of branches.
136 '(("false" . nil)
137 ("true" . t)
138 ("both" . both)))
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 '(repeat (cons string (choice face (cons (const background-color) string))))
147 :group 'cpp)
149 (defcustom cpp-face-light-name-list
150 '("light gray" "light blue" "light cyan" "light yellow" "light pink"
151 "pale green" "beige" "orange" "magenta" "violet" "medium purple"
152 "turquoise")
153 "Background colors useful with dark foreground colors."
154 :type '(repeat string)
155 :group 'cpp)
157 (defcustom cpp-face-dark-name-list
158 '("dim gray" "blue" "cyan" "yellow" "red"
159 "dark green" "brown" "dark orange" "dark khaki" "dark violet" "purple"
160 "dark turquoise")
161 "Background colors useful with light foreground colors."
162 :type '(repeat string)
163 :group 'cpp)
165 (defcustom cpp-face-light-list nil
166 "Alist of names and faces to be used for light backgrounds."
167 :type '(repeat (cons string (choice face
168 (cons (const background-color) string))))
169 :group 'cpp)
171 (defcustom cpp-face-dark-list nil
172 "Alist of names and faces to be used for dark backgrounds."
173 :type '(repeat (cons string (choice face
174 (cons (const background-color) string))))
175 :group 'cpp)
177 (defcustom cpp-face-mono-list
178 '(("bold" . bold)
179 ("bold-italic" . bold-italic)
180 ("italic" . italic)
181 ("underline" . underline))
182 "Alist of names and faces to be used for monochrome screens."
183 :type '(repeat (cons string face))
184 :group 'cpp)
186 (defcustom cpp-face-none-list
187 '(("default" . default)
188 ("invisible" . invisible))
189 "Alist of names and faces available even if you don't use a window system."
190 :type '(repeat (cons string cpp-face))
191 :group 'cpp)
193 (defvar cpp-face-all-list
194 (append cpp-face-light-list
195 cpp-face-dark-list
196 cpp-face-mono-list
197 cpp-face-none-list)
198 "All faces used for highlighting text inside cpp conditionals.")
200 ;;; Parse Buffer:
202 (defvar cpp-parse-symbols nil
203 "List of cpp macros used in the local buffer.")
204 (make-variable-buffer-local 'cpp-parse-symbols)
206 (defconst cpp-parse-regexp
207 ;; Regexp matching all tokens needed to find conditionals.
208 (concat
209 "'\\|\"\\|/\\*\\|//\\|"
210 "\\(^[ \t]*#[ \t]*\\(ifdef\\|ifndef\\|if\\|"
211 "elif\\|else\\|endif\\)\\b\\)"))
213 ;;;###autoload
214 (defun cpp-highlight-buffer (arg)
215 "Highlight C code according to preprocessor conditionals.
216 This command pops up a buffer which you should edit to specify
217 what kind of highlighting to use, and the criteria for highlighting.
218 A prefix arg suppresses display of that buffer."
219 (interactive "P")
220 (unless (or (eq t buffer-invisibility-spec)
221 (memq 'cpp buffer-invisibility-spec))
222 (add-to-invisibility-spec 'cpp))
223 (setq cpp-parse-symbols nil)
224 (cpp-parse-reset)
225 (if (null cpp-edit-list)
226 (cpp-edit-load))
227 (let (cpp-state-stack)
228 (save-excursion
229 (goto-char (point-min))
230 (cpp-progress-message "Parsing...")
231 (while (re-search-forward cpp-parse-regexp nil t)
232 (cpp-progress-message "Parsing...%d%%"
233 (/ (* 100 (- (point) (point-min))) (buffer-size)))
234 (let ((match (buffer-substring (match-beginning 0) (match-end 0))))
235 (cond ((or (string-equal match "'")
236 (string-equal match "\""))
237 (goto-char (match-beginning 0))
238 (condition-case nil
239 (forward-sexp)
240 (error (cpp-parse-error
241 "Unterminated string or character"))))
242 ((string-equal match "/*")
243 (or (search-forward "*/" nil t)
244 (error "Unterminated comment")))
245 ((string-equal match "//")
246 (skip-chars-forward "^\n\r"))
248 (end-of-line 1)
249 (let ((from (match-beginning 1))
250 (to (1+ (point)))
251 (type (buffer-substring (match-beginning 2)
252 (match-end 2)))
253 (expr (buffer-substring (match-end 1) (point))))
254 (cond ((string-equal type "ifdef")
255 (cpp-parse-open t expr from to))
256 ((string-equal type "ifndef")
257 (cpp-parse-open nil expr from to))
258 ((string-equal type "if")
259 (cpp-parse-open t expr from to))
260 ((string-equal type "elif")
261 (let (cpp-known-face cpp-unknown-face)
262 (cpp-parse-close from to))
263 (cpp-parse-open t expr from to))
264 ((string-equal type "else")
265 (or cpp-state-stack
266 (cpp-parse-error "Top level #else"))
267 (let ((entry (list (not (nth 0 (car cpp-state-stack)))
268 (nth 1 (car cpp-state-stack))
269 from to)))
270 (cpp-parse-close from to)
271 (setq cpp-state-stack (cons entry cpp-state-stack))))
272 ((string-equal type "endif")
273 (cpp-parse-close from to))
275 (cpp-parse-error "Parser error"))))))))
276 (message "Parsing...done"))
277 (if cpp-state-stack
278 (save-excursion
279 (goto-char (nth 3 (car cpp-state-stack)))
280 (cpp-parse-error "Unclosed conditional"))))
281 (or arg
282 (null cpp-parse-symbols)
283 (cpp-parse-edit)))
285 (defun cpp-parse-open (branch expr begin end)
286 "Push information about conditional-beginning onto `cpp-state-stack'."
287 ;; Discard comments within this line.
288 (while (string-match "\\b[ \t]*/\\*.*\\*/[ \t]*\\b" expr)
289 (setq expr (concat (substring expr 0 (match-beginning 0))
290 (substring expr (match-end 0)))))
291 ;; If a comment starts on this line and continues past, discard it.
292 (if (string-match "\\b[ \t]*/\\*" expr)
293 (setq expr (substring expr 0 (match-beginning 0))))
294 ;; Delete any C++ comment from the line.
295 (if (string-match "\\b[ \t]*\\(//.*\\)?$" expr)
296 (setq expr (substring expr 0 (match-beginning 0))))
297 (while (string-match "[ \t]+" expr)
298 (setq expr (concat (substring expr 0 (match-beginning 0))
299 (substring expr (match-end 0)))))
300 (setq cpp-state-stack (cons (list branch expr begin end) cpp-state-stack))
301 (or (member expr cpp-parse-symbols)
302 (setq cpp-parse-symbols
303 (cons expr cpp-parse-symbols)))
304 (if (assoc expr cpp-edit-list)
305 (cpp-make-known-overlay begin end)
306 (cpp-make-unknown-overlay begin end)))
308 (defun cpp-parse-close (from to)
309 ;; Pop top of cpp-state-stack and create overlay.
310 (let ((entry (assoc (nth 1 (car cpp-state-stack)) cpp-edit-list))
311 (branch (nth 0 (car cpp-state-stack)))
312 (begin (nth 2 (car cpp-state-stack)))
313 (end (nth 3 (car cpp-state-stack))))
314 (setq cpp-state-stack (cdr cpp-state-stack))
315 (if entry
316 (let ((face (nth (if branch 1 2) entry))
317 (read-only (eq (not branch) (nth 3 entry)))
318 (priority (length cpp-state-stack))
319 (overlay (make-overlay end from)))
320 (cpp-make-known-overlay from to)
321 (setq cpp-overlay-list (cons overlay cpp-overlay-list))
322 (if priority (overlay-put overlay 'priority priority))
323 (cond ((eq face 'invisible)
324 (cpp-make-overlay-hidden overlay))
325 ((eq face 'default))
327 (overlay-put overlay 'face face)))
328 (if read-only
329 (cpp-make-overlay-read-only overlay)
330 (cpp-make-overlay-sticky overlay)))
331 (cpp-make-unknown-overlay from to))))
333 (defun cpp-parse-error (error)
334 ;; Error message issued by the cpp parser.
335 (error "%s at line %d" error (count-lines (point-min) (point))))
337 (defun cpp-parse-reset ()
338 "Reset display of cpp conditionals to normal."
339 (interactive)
340 (while cpp-overlay-list
341 (delete-overlay (car cpp-overlay-list))
342 (setq cpp-overlay-list (cdr cpp-overlay-list))))
344 ;;;###autoload
345 (defun cpp-parse-edit ()
346 "Edit display information for cpp conditionals."
347 (interactive)
348 (or cpp-parse-symbols
349 (cpp-highlight-buffer t))
350 (let ((buffer (current-buffer)))
351 (pop-to-buffer "*CPP Edit*")
352 (cpp-edit-mode)
353 (setq cpp-edit-buffer buffer)
354 (cpp-edit-reset)))
356 ;;; Overlays:
358 (defun cpp-make-known-overlay (start end)
359 ;; Create an overlay for a known cpp command from START to END.
360 (let ((overlay (make-overlay start end)))
361 (if (eq cpp-known-face 'invisible)
362 (cpp-make-overlay-hidden overlay)
363 (or (eq cpp-known-face 'default)
364 (overlay-put overlay 'face cpp-known-face))
365 (if cpp-known-writable
367 (overlay-put overlay 'modification-hooks '(cpp-signal-read-only))
368 (overlay-put overlay 'insert-in-front-hooks '(cpp-signal-read-only))))
369 (setq cpp-overlay-list (cons overlay cpp-overlay-list))))
371 (defun cpp-make-unknown-overlay (start end)
372 ;; Create an overlay for an unknown cpp command from START to END.
373 (let ((overlay (make-overlay start end)))
374 (cond ((eq cpp-unknown-face 'invisible)
375 (cpp-make-overlay-hidden overlay))
376 ((eq cpp-unknown-face 'default))
378 (overlay-put overlay 'face cpp-unknown-face)))
379 (if cpp-unknown-writable
381 (overlay-put overlay 'modification-hooks '(cpp-signal-read-only))
382 (overlay-put overlay 'insert-in-front-hooks '(cpp-signal-read-only)))
383 (setq cpp-overlay-list (cons overlay cpp-overlay-list))))
385 (defun cpp-make-overlay-hidden (overlay)
386 ;; Make overlay hidden and intangible.
387 (overlay-put overlay 'invisible 'cpp)
388 (overlay-put overlay 'modification-hooks '(cpp-signal-read-only))
389 (overlay-put overlay 'insert-in-front-hooks '(cpp-signal-read-only)))
391 (defun cpp-make-overlay-read-only (overlay)
392 ;; Make overlay read only.
393 (overlay-put overlay 'modification-hooks '(cpp-signal-read-only))
394 (overlay-put overlay 'insert-in-front-hooks '(cpp-signal-read-only))
395 (overlay-put overlay 'insert-behind-hooks '(cpp-signal-read-only)))
397 (defun cpp-make-overlay-sticky (overlay)
398 ;; Make OVERLAY grow when you insert text at either end.
399 (overlay-put overlay 'insert-in-front-hooks '(cpp-grow-overlay))
400 (overlay-put overlay 'insert-behind-hooks '(cpp-grow-overlay)))
402 (defun cpp-signal-read-only (overlay after start end &optional len)
403 ;; Only allow deleting the whole overlay.
404 ;; Trying to change a read-only overlay.
405 (if (and (not after)
406 (or (< (overlay-start overlay) start)
407 (> (overlay-end overlay) end)))
408 (error "This text is read only")))
410 (defun cpp-grow-overlay (overlay after start end &optional len)
411 ;; Make OVERLAY grow to contain range START to END.
412 (if after
413 (move-overlay overlay
414 (min start (overlay-start overlay))
415 (max end (overlay-end overlay)))))
417 ;;; Edit Buffer:
419 (defvar cpp-edit-map nil)
420 ;; Keymap for `cpp-edit-mode'.
422 (if cpp-edit-map
424 (setq cpp-edit-map (make-keymap))
425 (suppress-keymap cpp-edit-map)
426 (define-key cpp-edit-map [ down-mouse-2 ] 'cpp-push-button)
427 (define-key cpp-edit-map [ mouse-2 ] 'ignore)
428 (define-key cpp-edit-map " " 'scroll-up)
429 (define-key cpp-edit-map "\C-?" 'scroll-down)
430 (define-key cpp-edit-map [ delete ] 'scroll-down)
431 (define-key cpp-edit-map "\C-c\C-c" 'cpp-edit-apply)
432 (define-key cpp-edit-map "a" 'cpp-edit-apply)
433 (define-key cpp-edit-map "A" 'cpp-edit-apply)
434 (define-key cpp-edit-map "r" 'cpp-edit-reset)
435 (define-key cpp-edit-map "R" 'cpp-edit-reset)
436 (define-key cpp-edit-map "s" 'cpp-edit-save)
437 (define-key cpp-edit-map "S" 'cpp-edit-save)
438 (define-key cpp-edit-map "l" 'cpp-edit-load)
439 (define-key cpp-edit-map "L" 'cpp-edit-load)
440 (define-key cpp-edit-map "h" 'cpp-edit-home)
441 (define-key cpp-edit-map "H" 'cpp-edit-home)
442 (define-key cpp-edit-map "b" 'cpp-edit-background)
443 (define-key cpp-edit-map "B" 'cpp-edit-background)
444 (define-key cpp-edit-map "k" 'cpp-edit-known)
445 (define-key cpp-edit-map "K" 'cpp-edit-known)
446 (define-key cpp-edit-map "u" 'cpp-edit-unknown)
447 (define-key cpp-edit-map "u" 'cpp-edit-unknown)
448 (define-key cpp-edit-map "t" 'cpp-edit-true)
449 (define-key cpp-edit-map "T" 'cpp-edit-true)
450 (define-key cpp-edit-map "f" 'cpp-edit-false)
451 (define-key cpp-edit-map "F" 'cpp-edit-false)
452 (define-key cpp-edit-map "w" 'cpp-edit-write)
453 (define-key cpp-edit-map "W" 'cpp-edit-write)
454 (define-key cpp-edit-map "X" 'cpp-edit-toggle-known)
455 (define-key cpp-edit-map "x" 'cpp-edit-toggle-known)
456 (define-key cpp-edit-map "Y" 'cpp-edit-toggle-unknown)
457 (define-key cpp-edit-map "y" 'cpp-edit-toggle-unknown)
458 (define-key cpp-edit-map "q" 'bury-buffer)
459 (define-key cpp-edit-map "Q" 'bury-buffer))
461 (defvar cpp-edit-symbols nil)
462 ;; Symbols defined in the edit buffer.
463 (make-variable-buffer-local 'cpp-edit-symbols)
465 (defun cpp-edit-mode ()
466 "Major mode for editing the criteria for highlighting cpp conditionals.
467 Click on objects to change them.
468 You can also use the keyboard accelerators indicated like this: [K]ey."
469 (kill-all-local-variables)
470 (buffer-disable-undo)
471 (auto-save-mode -1)
472 (setq buffer-read-only t)
473 (setq major-mode 'cpp-edit-mode)
474 (setq mode-name "CPP Edit")
475 (use-local-map cpp-edit-map))
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 (eq major-mode '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 (save-excursion
579 (set-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))))
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))
604 (defun cpp-edit-background ()
605 "Change default face collection."
606 (interactive)
607 (call-interactively 'cpp-choose-default-face)
608 (cpp-edit-reset))
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))
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))
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))
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))
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))
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))
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))
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))
680 ;;; Prompts:
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)))
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))))
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))
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
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
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)))
750 (setq cpp-face-type 'none)
751 (setq cpp-face-default-list cpp-face-none-list))))
753 ;;; Buttons:
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)))
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))))))
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])))))))
803 ;;; Faces:
805 (defun cpp-create-bg-face (color)
806 ;; Create entry for face with background COLOR.
807 (cons color (cons 'background-color color)))
809 (cpp-choose-default-face
810 (if (or window-system (display-color-p)) cpp-face-type 'none))
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))))
819 ;;; Utilities:
821 (defvar cpp-progress-time 0)
822 ;; Last time we issued a progress message.
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)
829 (setq cpp-progress-time time)
830 (apply 'message args))))
832 (provide 'cpp)
834 ;; arch-tag: fb7d433d-745d-495a-96f0-86908ab63f74
835 ;;; cpp.el ends here