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