Also handle escaping when string begins with a quote
[emacs.git] / lisp / progmodes / cpp.el
blob7d641ab47f09da125d1d2c31d6f0c0b33eaef9aa
1 ;;; cpp.el --- highlight or hide text according to cpp conditionals
3 ;; Copyright (C) 1994-1995, 2001-2016 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 (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"))
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))
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)))
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)))
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))
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))))
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))))
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))))
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)))
360 ;;; Overlays:
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
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))))
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))
382 (overlay-put overlay 'face cpp-unknown-face)))
383 (if cpp-unknown-writable
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))))
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)))
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)))
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)))
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")))
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)))))
421 ;;; Edit Buffer:
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'.")
466 (defvar cpp-edit-symbols nil)
467 ;; Symbols defined in the edit buffer.
468 (make-variable-buffer-local 'cpp-edit-symbols)
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))
478 (defun cpp-edit-apply ()
479 "Apply edited display information to original buffer."
480 (interactive)
481 (cpp-edit-home)
482 (cpp-highlight-buffer t))
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 (substitute-command-keys "CPP Display Information for `"))
498 (cpp-make-button (buffer-name cpp-edit-buffer) 'cpp-edit-home)
499 (insert (substitute-command-keys
500 "'\n\nClick mouse-2 on item you want to change or use\n")
501 "or switch to this buffer and type the keyboard equivalents.\n"
502 "Keyboard equivalents are indicated with brackets like [T]his.\n\n")
503 (cpp-make-button "[H]ome (display the C file)" 'cpp-edit-home)
504 (insert " ")
505 (cpp-make-button "[A]pply new settings" 'cpp-edit-apply)
506 (insert "\n")
507 (cpp-make-button "[S]ave settings" 'cpp-edit-save)
508 (insert " ")
509 (cpp-make-button "[L]oad settings" 'cpp-edit-load)
510 (insert "\n\n")
512 (insert "[B]ackground: ")
513 (cpp-make-button (car (rassq cpp-face-type cpp-face-type-list))
514 'cpp-edit-background)
515 (insert "\n[K]nown conditionals: ")
516 (cpp-make-button (cpp-face-name cpp-known-face)
517 'cpp-edit-known nil t)
518 (insert " [X] ")
519 (cpp-make-button (car (rassq cpp-known-writable cpp-writable-list))
520 'cpp-edit-toggle-known)
521 (insert "\n[U]nknown conditionals: ")
522 (cpp-make-button (cpp-face-name cpp-unknown-face)
523 'cpp-edit-unknown nil t)
524 (insert " [Y] ")
525 (cpp-make-button (car (rassq cpp-unknown-writable cpp-writable-list))
526 'cpp-edit-toggle-unknown)
527 (insert (format "\n\n\n%39s: %14s %14s %7s\n\n" "Expression"
528 "[T]rue Face" "[F]alse Face" "[W]rite"))
530 (setq symbols (reverse symbols))
531 (while symbols
532 (let* ((symbol (car symbols))
533 (entry (assoc symbol cpp-edit-list))
534 (true (nth 1 entry))
535 (false (nth 2 entry))
536 (write (if entry (nth 3 entry) 'both)))
537 (setq symbols (cdr symbols))
539 (if (and entry ; Make default entries unknown.
540 (or (null true) (eq true 'default))
541 (or (null false) (eq false 'default))
542 (eq write 'both))
543 (setq cpp-edit-list (delq entry cpp-edit-list)
544 entry nil))
546 (if (> (length symbol) 39)
547 (insert (substring symbol 0 39) ": ")
548 (insert (format "%39s: " symbol)))
550 (cpp-make-button (cpp-face-name true)
551 'cpp-edit-true symbol t 14)
552 (insert " ")
553 (cpp-make-button (cpp-face-name false)
554 'cpp-edit-false symbol t 14)
555 (insert " ")
556 (cpp-make-button (car (rassq write cpp-branch-list))
557 'cpp-edit-write symbol nil 6)
558 (insert "\n")))
559 (insert "\n\n")
560 (set-window-start nil start)
561 (goto-char pos)))
563 (defun cpp-edit-load ()
564 "Load cpp configuration."
565 (interactive)
566 (cond ((null init-file-user)
567 ;; If -q was specified, don't load any init files.
568 nil)
569 ((file-readable-p cpp-config-file)
570 (load-file cpp-config-file))
571 ((file-readable-p (concat "~/" cpp-config-file))
572 (load-file cpp-config-file)))
573 (if (derived-mode-p 'cpp-edit-mode)
574 (cpp-edit-reset)))
576 (defun cpp-edit-save ()
577 "Save the current cpp configuration in a file."
578 (interactive)
579 (require 'pp)
580 (with-current-buffer cpp-edit-buffer
581 (let ((buffer (find-file-noselect cpp-config-file)))
582 (set-buffer buffer)
583 (erase-buffer)
584 (pp (list 'setq 'cpp-known-face
585 (list 'quote cpp-known-face)) buffer)
586 (pp (list 'setq 'cpp-unknown-face
587 (list 'quote cpp-unknown-face)) buffer)
588 (pp (list 'setq 'cpp-face-type
589 (list 'quote cpp-face-type)) buffer)
590 (pp (list 'setq 'cpp-known-writable
591 (list 'quote cpp-known-writable)) buffer)
592 (pp (list 'setq 'cpp-unknown-writable
593 (list 'quote cpp-unknown-writable)) buffer)
594 (pp (list 'setq 'cpp-edit-list
595 (list 'quote cpp-edit-list)) buffer)
596 (write-file cpp-config-file))))
598 (defun cpp-edit-home ()
599 "Switch back to original buffer."
600 (interactive)
601 (if cpp-button-event
602 (read-event))
603 (pop-to-buffer cpp-edit-buffer))
605 (defun cpp-edit-background ()
606 "Change default face collection."
607 (interactive)
608 (call-interactively 'cpp-choose-default-face)
609 (cpp-edit-reset))
611 (defun cpp-edit-known ()
612 "Select default for known conditionals."
613 (interactive)
614 (setq cpp-known-face (cpp-choose-face "Known face" cpp-known-face))
615 (cpp-edit-reset))
617 (defun cpp-edit-unknown ()
618 "Select default for unknown conditionals."
619 (interactive)
620 (setq cpp-unknown-face (cpp-choose-face "Unknown face" cpp-unknown-face))
621 (cpp-edit-reset))
623 (defun cpp-edit-toggle-known (arg)
624 "Toggle writable status for known conditionals.
625 With optional argument ARG, make them writable if ARG is positive,
626 otherwise make them unwritable."
627 (interactive "@P")
628 (if (or (and (null arg) cpp-known-writable)
629 (<= (prefix-numeric-value arg) 0))
630 (setq cpp-known-writable nil)
631 (setq cpp-known-writable t))
632 (cpp-edit-reset))
634 (defun cpp-edit-toggle-unknown (arg)
635 "Toggle writable status for unknown conditionals.
636 With optional argument ARG, make them writable if ARG is positive,
637 otherwise make them unwritable."
638 (interactive "@P")
639 (if (or (and (null arg) cpp-unknown-writable)
640 (<= (prefix-numeric-value arg) 0))
641 (setq cpp-unknown-writable nil)
642 (setq cpp-unknown-writable t))
643 (cpp-edit-reset))
645 (defun cpp-edit-true (symbol face)
646 "Select SYMBOL's true FACE used for highlighting taken conditionals."
647 (interactive
648 (let ((symbol (cpp-choose-symbol)))
649 (list symbol
650 (cpp-choose-face "True face"
651 (nth 1 (assoc symbol cpp-edit-list))))))
652 (setcar (nthcdr 1 (cpp-edit-list-entry-get-or-create symbol)) face)
653 (cpp-edit-reset))
655 (defun cpp-edit-false (symbol face)
656 "Select SYMBOL's false FACE used for highlighting untaken conditionals."
657 (interactive
658 (let ((symbol (cpp-choose-symbol)))
659 (list symbol
660 (cpp-choose-face "False face"
661 (nth 2 (assoc symbol cpp-edit-list))))))
662 (setcar (nthcdr 2 (cpp-edit-list-entry-get-or-create symbol)) face)
663 (cpp-edit-reset))
665 (defun cpp-edit-write (symbol branch)
666 "Set which branches of SYMBOL should be writable to BRANCH.
667 BRANCH should be either nil (false branch), t (true branch) or `both'."
668 (interactive (list (cpp-choose-symbol) (cpp-choose-branch)))
669 (setcar (nthcdr 3 (cpp-edit-list-entry-get-or-create symbol)) branch)
670 (cpp-edit-reset))
672 (defun cpp-edit-list-entry-get-or-create (symbol)
673 ;; Return the entry for SYMBOL in `cpp-edit-list'.
674 ;; If it does not exist, create it.
675 (let ((entry (assoc symbol cpp-edit-list)))
676 (or entry
677 (setq entry (list symbol nil nil 'both nil)
678 cpp-edit-list (cons entry cpp-edit-list)))
679 entry))
681 ;;; Prompts:
683 (defun cpp-choose-symbol ()
684 ;; Choose a symbol if called from keyboard, otherwise use the one clicked on.
685 (if cpp-button-event
686 cpp-callback-data
687 (completing-read "Symbol: " cpp-edit-symbols nil t)))
689 (defun cpp-choose-branch ()
690 ;; Choose a branch, either nil, t, or both.
691 (if cpp-button-event
692 (x-popup-menu cpp-button-event
693 (list "Branch" (cons "Branch" cpp-branch-list)))
694 (cdr (assoc (completing-read "Branch: " cpp-branch-list nil t)
695 cpp-branch-list))))
697 (defun cpp-choose-face (prompt default)
698 ;; Choose a face from cpp-face-default-list.
699 ;; PROMPT is what to say to the user.
700 ;; DEFAULT is the default face.
701 (or (if cpp-button-event
702 (x-popup-menu cpp-button-event
703 (list prompt (cons prompt cpp-face-default-list)))
704 (let ((name (car (rassq default cpp-face-default-list))))
705 (cdr (assoc (completing-read (if name
706 (concat prompt
707 " (default " name "): ")
708 (concat prompt ": "))
709 cpp-face-default-list nil t)
710 cpp-face-all-list))))
711 default))
713 (defun cpp-choose-default-face (type)
714 ;; Choose default face list for screen of TYPE.
715 ;; Type must be one of the types defined in `cpp-face-type-list'.
716 (interactive (list (if cpp-button-event
717 (x-popup-menu cpp-button-event
718 (list "Screen type"
719 (cons "Screen type"
720 cpp-face-type-list)))
721 (cdr (assoc (completing-read "Screen type: "
722 cpp-face-type-list
723 nil t)
724 cpp-face-type-list)))))
725 (cond ((null type))
726 ((eq type 'light)
727 (if cpp-face-light-list
729 (setq cpp-face-light-list
730 (mapcar 'cpp-create-bg-face cpp-face-light-name-list))
731 (setq cpp-face-all-list
732 (append cpp-face-all-list cpp-face-light-list)))
733 (setq cpp-face-type 'light)
734 (setq cpp-face-default-list
735 (append cpp-face-light-list cpp-face-none-list)))
736 ((eq type 'dark)
737 (if cpp-face-dark-list
739 (setq cpp-face-dark-list
740 (mapcar 'cpp-create-bg-face cpp-face-dark-name-list))
741 (setq cpp-face-all-list
742 (append cpp-face-all-list cpp-face-dark-list)))
743 (setq cpp-face-type 'dark)
744 (setq cpp-face-default-list
745 (append cpp-face-dark-list cpp-face-none-list)))
746 ((eq type 'mono)
747 (setq cpp-face-type 'mono)
748 (setq cpp-face-default-list
749 (append cpp-face-mono-list cpp-face-none-list)))
751 (setq cpp-face-type 'none)
752 (setq cpp-face-default-list cpp-face-none-list))))
754 ;;; Buttons:
756 (defun cpp-make-button (name callback &optional data face padding)
757 ;; Create a button at point.
758 ;; NAME is the name of the button.
759 ;; CALLBACK is the function to call when the button is pushed.
760 ;; DATA will be made available to CALLBACK
761 ;;in the free variable cpp-callback-data.
762 ;; FACE means that NAME is the name of a face in `cpp-face-all-list'.
763 ;; PADDING means NAME will be right justified at that length.
764 (let ((name (format "%s" name))
765 from to)
766 (cond ((null padding)
767 (setq from (point))
768 (insert name))
769 ((> (length name) padding)
770 (setq from (point))
771 (insert (substring name 0 padding)))
773 (insert (make-string (- padding (length name)) ? ))
774 (setq from (point))
775 (insert name)))
776 (setq to (point))
777 (setq face
778 (if face
779 (let ((check (cdr (assoc name cpp-face-all-list))))
780 (if (memq check '(default invisible))
781 'bold
782 check))
783 'bold))
784 (add-text-properties from to
785 (append (list 'face face)
786 '(mouse-face highlight)
787 '(help-echo "mouse-2: change/use this item")
788 (list 'cpp-callback callback)
789 (if data (list 'cpp-data data))))))
791 (defun cpp-push-button (event)
792 ;; Pushed a CPP button.
793 (interactive "@e")
794 (set-buffer (window-buffer (posn-window (event-start event))))
795 (let ((pos (posn-point (event-start event))))
796 (let ((cpp-callback-data (get-text-property pos 'cpp-data))
797 (fun (get-text-property pos 'cpp-callback))
798 (cpp-button-event event))
799 (cond (fun
800 (call-interactively (get-text-property pos 'cpp-callback)))
801 ((lookup-key global-map [ down-mouse-2])
802 (call-interactively (lookup-key global-map [ down-mouse-2])))))))
804 ;;; Faces:
806 (defun cpp-create-bg-face (color)
807 ;; Create entry for face with background COLOR.
808 (cons color (cons 'background-color color)))
810 (cpp-choose-default-face
811 (if (or window-system (display-color-p)) cpp-face-type 'none))
813 (defun cpp-face-name (face)
814 ;; Return the name of FACE from `cpp-face-all-list'.
815 (let ((entry (rassq (if face face 'default) cpp-face-all-list)))
816 (if entry
817 (car entry)
818 (format "<%s>" face))))
820 ;;; Utilities:
822 (defvar cpp-progress-time 0)
823 ;; Last time we issued a progress message.
825 (defun cpp-progress-message (&rest args)
826 ;; Report progress at most once a second. Take same ARGS as `message'.
827 (let ((time (nth 1 (current-time))))
828 (if (= time cpp-progress-time)
830 (setq cpp-progress-time time)
831 (apply 'message args))))
833 (provide 'cpp)
835 ;;; cpp.el ends here