1 ;;; cpp.el --- highlight or hide text according to cpp conditionals
3 ;; Copyright (C) 1994-1995, 2001-2013 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/>.
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.
32 ;; Should parse "#if" and "#elif" expressions and merge the faces
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
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.
49 "Highlight or hide text according to cpp conditionals."
50 :link
'(custom-group-link :tag
"Font Lock Faces group" font-lock-faces
)
54 (defcustom cpp-config-file
(convert-standard-filename ".cpp.el")
55 "File name to save cpp configuration."
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."
68 (defcustom cpp-unknown-face
'highlight
69 "Face used for unknown cpp symbols."
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
)
82 (defcustom cpp-known-writable t
83 "Non-nil means you are allowed to modify the known conditionals."
87 (defcustom cpp-unknown-writable t
88 "Non-nil means you are allowed to modify the unknown conditionals."
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
))))
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
)
119 "Alist of strings and names of the defined face collections.")
121 (defconst cpp-writable-list
122 ;; Names used for the writable property.
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.
139 (defcustom cpp-face-default-list nil
140 "Alist of faces you can choose from for cpp conditionals.
141 Each element has the form (STRING . FACE), where STRING
142 serves as a name (for `cpp-highlight-buffer' only)
143 and FACE is either a face (a symbol)
144 or a cons cell (background-color . COLOR)."
145 :type
'(repeat (cons string
(choice face
(cons (const background-color
) string
))))
148 (defcustom cpp-face-light-name-list
149 '("light gray" "light blue" "light cyan" "light yellow" "light pink"
150 "pale green" "beige" "orange" "magenta" "violet" "medium purple"
152 "Background colors useful with dark foreground colors."
153 :type
'(repeat string
)
156 (defcustom cpp-face-dark-name-list
157 '("dim gray" "blue" "cyan" "yellow" "red"
158 "dark green" "brown" "dark orange" "dark khaki" "dark violet" "purple"
160 "Background colors useful with light foreground colors."
161 :type
'(repeat string
)
164 (defcustom cpp-face-light-list nil
165 "Alist of names and faces to be used for light backgrounds."
166 :type
'(repeat (cons string
(choice face
167 (cons (const background-color
) string
))))
170 (defcustom cpp-face-dark-list nil
171 "Alist of names and faces to be used for dark backgrounds."
172 :type
'(repeat (cons string
(choice face
173 (cons (const background-color
) string
))))
176 (defcustom cpp-face-mono-list
178 ("bold-italic" . bold-italic
)
180 ("underline" . underline
))
181 "Alist of names and faces to be used for monochrome screens."
182 :type
'(repeat (cons string face
))
185 (defcustom cpp-face-none-list
186 '(("default" . default
)
187 ("invisible" . invisible
))
188 "Alist of names and faces available even if you don't use a window system."
189 :type
'(repeat (cons string cpp-face
))
192 (defvar cpp-face-all-list
193 (append cpp-face-light-list
197 "All faces used for highlighting text inside cpp conditionals.")
201 (defvar cpp-parse-symbols nil
202 "List of cpp macros used in the local buffer.")
203 (make-variable-buffer-local 'cpp-parse-symbols
)
205 (defconst cpp-parse-regexp
206 ;; Regexp matching all tokens needed to find conditionals.
208 "'\\|\"\\|/\\*\\|//\\|"
209 "\\(^[ \t]*#[ \t]*\\(ifdef\\|ifndef\\|if\\|"
210 "elif\\|else\\|endif\\)\\b\\)"))
213 (defun cpp-highlight-buffer (arg)
214 "Highlight C code according to preprocessor conditionals.
215 This command pops up a buffer which you should edit to specify
216 what kind of highlighting to use, and the criteria for highlighting.
217 A prefix arg suppresses display of that buffer."
219 (unless (or (eq t buffer-invisibility-spec
)
220 (memq 'cpp buffer-invisibility-spec
))
221 (add-to-invisibility-spec 'cpp
))
222 (setq cpp-parse-symbols nil
)
224 (if (null cpp-edit-list
)
226 (let (cpp-state-stack)
228 (goto-char (point-min))
229 (cpp-progress-message "Parsing...")
230 (while (re-search-forward cpp-parse-regexp nil t
)
231 (cpp-progress-message "Parsing...%d%%"
232 (/ (* 100 (- (point) (point-min))) (buffer-size)))
233 (let ((match (buffer-substring (match-beginning 0) (match-end 0))))
234 (cond ((or (string-equal match
"'")
235 (string-equal match
"\""))
236 (goto-char (match-beginning 0))
239 (error (cpp-parse-error
240 "Unterminated string or character"))))
241 ((string-equal match
"/*")
242 (or (search-forward "*/" nil t
)
243 (error "Unterminated comment")))
244 ((string-equal match
"//")
245 (skip-chars-forward "^\n\r"))
248 (let ((from (match-beginning 1))
250 (type (buffer-substring (match-beginning 2)
252 (expr (buffer-substring (match-end 1) (point))))
253 (cond ((string-equal type
"ifdef")
254 (cpp-parse-open t expr from to
))
255 ((string-equal type
"ifndef")
256 (cpp-parse-open nil expr from to
))
257 ((string-equal type
"if")
258 (cpp-parse-open t expr from to
))
259 ((string-equal type
"elif")
260 (let (cpp-known-face cpp-unknown-face
)
261 (cpp-parse-close from to
))
262 (cpp-parse-open t expr from to
))
263 ((string-equal type
"else")
265 (cpp-parse-error "Top level #else"))
266 (let ((entry (list (not (nth 0 (car cpp-state-stack
)))
267 (nth 1 (car cpp-state-stack
))
269 (cpp-parse-close from to
)
270 (setq cpp-state-stack
(cons entry cpp-state-stack
))))
271 ((string-equal type
"endif")
272 (cpp-parse-close from to
))
274 (cpp-parse-error "Parser error"))))))))
275 (message "Parsing...done"))
278 (goto-char (nth 3 (car cpp-state-stack
)))
279 (cpp-parse-error "Unclosed conditional"))))
281 (null cpp-parse-symbols
)
284 (defun cpp-parse-open (branch expr begin end
)
285 "Push information about conditional-beginning onto `cpp-state-stack'."
286 ;; Discard comments within this line.
287 (while (string-match "\\b[ \t]*/\\*.*\\*/[ \t]*\\b" expr
)
288 (setq expr
(concat (substring expr
0 (match-beginning 0))
289 (substring expr
(match-end 0)))))
290 ;; If a comment starts on this line and continues past, discard it.
291 (if (string-match "\\b[ \t]*/\\*" expr
)
292 (setq expr
(substring expr
0 (match-beginning 0))))
293 ;; Delete any C++ comment from the line.
294 (if (string-match "\\b[ \t]*\\(//.*\\)?$" expr
)
295 (setq expr
(substring expr
0 (match-beginning 0))))
296 (while (string-match "[ \t]+" expr
)
297 (setq expr
(concat (substring expr
0 (match-beginning 0))
298 (substring expr
(match-end 0)))))
299 (setq cpp-state-stack
(cons (list branch expr begin end
) cpp-state-stack
))
300 (or (member expr cpp-parse-symbols
)
301 (setq cpp-parse-symbols
302 (cons expr cpp-parse-symbols
)))
303 (if (assoc expr cpp-edit-list
)
304 (cpp-make-known-overlay begin end
)
305 (cpp-make-unknown-overlay begin end
)))
307 (defun cpp-parse-close (from to
)
308 ;; Pop top of cpp-state-stack and create overlay.
309 (let ((entry (assoc (nth 1 (car cpp-state-stack
)) cpp-edit-list
))
310 (branch (nth 0 (car cpp-state-stack
)))
311 (end (nth 3 (car cpp-state-stack
))))
312 (setq cpp-state-stack
(cdr cpp-state-stack
))
314 (let ((face (nth (if branch
1 2) entry
))
315 (read-only (eq (not branch
) (nth 3 entry
)))
316 (priority (length cpp-state-stack
))
317 (overlay (make-overlay end from
)))
318 (cpp-make-known-overlay from to
)
319 (setq cpp-overlay-list
(cons overlay cpp-overlay-list
))
320 (if priority
(overlay-put overlay
'priority priority
))
321 (cond ((eq face
'invisible
)
322 (cpp-make-overlay-hidden overlay
))
325 (overlay-put overlay
'face face
)))
327 (cpp-make-overlay-read-only overlay
)
328 (cpp-make-overlay-sticky overlay
)))
329 (cpp-make-unknown-overlay from to
))))
331 (defun cpp-parse-error (error)
332 ;; Error message issued by the cpp parser.
333 (error "%s at line %d" error
(count-lines (point-min) (point))))
335 (defun cpp-parse-reset ()
336 "Reset display of cpp conditionals to normal."
338 (while cpp-overlay-list
339 (delete-overlay (car cpp-overlay-list
))
340 (setq cpp-overlay-list
(cdr cpp-overlay-list
))))
343 (defun cpp-parse-edit ()
344 "Edit display information for cpp conditionals."
346 (or cpp-parse-symbols
347 (cpp-highlight-buffer t
))
348 (let ((buffer (current-buffer)))
349 (pop-to-buffer "*CPP Edit*")
351 (setq cpp-edit-buffer buffer
)
356 (defun cpp-make-known-overlay (start end
)
357 ;; Create an overlay for a known cpp command from START to END.
358 (let ((overlay (make-overlay start end
)))
359 (if (eq cpp-known-face
'invisible
)
360 (cpp-make-overlay-hidden overlay
)
361 (or (eq cpp-known-face
'default
)
362 (overlay-put overlay
'face cpp-known-face
))
363 (if cpp-known-writable
365 (overlay-put overlay
'modification-hooks
'(cpp-signal-read-only))
366 (overlay-put overlay
'insert-in-front-hooks
'(cpp-signal-read-only))))
367 (setq cpp-overlay-list
(cons overlay cpp-overlay-list
))))
369 (defun cpp-make-unknown-overlay (start end
)
370 ;; Create an overlay for an unknown cpp command from START to END.
371 (let ((overlay (make-overlay start end
)))
372 (cond ((eq cpp-unknown-face
'invisible
)
373 (cpp-make-overlay-hidden overlay
))
374 ((eq cpp-unknown-face
'default
))
376 (overlay-put overlay
'face cpp-unknown-face
)))
377 (if cpp-unknown-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-overlay-hidden (overlay)
384 ;; Make overlay hidden and intangible.
385 (overlay-put overlay
'invisible
'cpp
)
386 (overlay-put overlay
'modification-hooks
'(cpp-signal-read-only))
387 (overlay-put overlay
'insert-in-front-hooks
'(cpp-signal-read-only)))
389 (defun cpp-make-overlay-read-only (overlay)
390 ;; Make overlay read only.
391 (overlay-put overlay
'modification-hooks
'(cpp-signal-read-only))
392 (overlay-put overlay
'insert-in-front-hooks
'(cpp-signal-read-only))
393 (overlay-put overlay
'insert-behind-hooks
'(cpp-signal-read-only)))
395 (defun cpp-make-overlay-sticky (overlay)
396 ;; Make OVERLAY grow when you insert text at either end.
397 (overlay-put overlay
'insert-in-front-hooks
'(cpp-grow-overlay))
398 (overlay-put overlay
'insert-behind-hooks
'(cpp-grow-overlay)))
400 (defun cpp-signal-read-only (overlay after start end
&optional _len
)
401 ;; Only allow deleting the whole overlay.
402 ;; Trying to change a read-only overlay.
404 (or (< (overlay-start overlay
) start
)
405 (> (overlay-end overlay
) end
)))
406 (error "This text is read only")))
408 (defun cpp-grow-overlay (overlay after start end
&optional _len
)
409 ;; Make OVERLAY grow to contain range START to END.
411 (move-overlay overlay
412 (min start
(overlay-start overlay
))
413 (max end
(overlay-end overlay
)))))
417 (defvar cpp-edit-mode-map
418 (let ((map (make-keymap)))
419 (suppress-keymap map
)
420 (define-key map
[ down-mouse-2
] 'cpp-push-button
)
421 (define-key map
[ mouse-2
] 'ignore
)
422 (define-key map
" " 'scroll-up-command
)
423 (define-key map
"\C-?" 'scroll-down-command
)
424 (define-key map
[ delete
] 'scroll-down
)
425 (define-key map
"\C-c\C-c" 'cpp-edit-apply
)
426 (define-key map
"a" 'cpp-edit-apply
)
427 (define-key map
"A" 'cpp-edit-apply
)
428 (define-key map
"r" 'cpp-edit-reset
)
429 (define-key map
"R" 'cpp-edit-reset
)
430 (define-key map
"s" 'cpp-edit-save
)
431 (define-key map
"S" 'cpp-edit-save
)
432 (define-key map
"l" 'cpp-edit-load
)
433 (define-key map
"L" 'cpp-edit-load
)
434 (define-key map
"h" 'cpp-edit-home
)
435 (define-key map
"H" 'cpp-edit-home
)
436 (define-key map
"b" 'cpp-edit-background
)
437 (define-key map
"B" 'cpp-edit-background
)
438 (define-key map
"k" 'cpp-edit-known
)
439 (define-key map
"K" 'cpp-edit-known
)
440 (define-key map
"u" 'cpp-edit-unknown
)
441 (define-key map
"u" 'cpp-edit-unknown
)
442 (define-key map
"t" 'cpp-edit-true
)
443 (define-key map
"T" 'cpp-edit-true
)
444 (define-key map
"f" 'cpp-edit-false
)
445 (define-key map
"F" 'cpp-edit-false
)
446 (define-key map
"w" 'cpp-edit-write
)
447 (define-key map
"W" 'cpp-edit-write
)
448 (define-key map
"X" 'cpp-edit-toggle-known
)
449 (define-key map
"x" 'cpp-edit-toggle-known
)
450 (define-key map
"Y" 'cpp-edit-toggle-unknown
)
451 (define-key map
"y" 'cpp-edit-toggle-unknown
)
452 (define-key map
"q" 'bury-buffer
)
453 (define-key map
"Q" 'bury-buffer
)
455 "Keymap for `cpp-edit-mode'.")
459 (defvar cpp-edit-symbols nil
)
460 ;; Symbols defined in the edit buffer.
461 (make-variable-buffer-local 'cpp-edit-symbols
)
463 (define-derived-mode cpp-edit-mode fundamental-mode
"CPP Edit"
464 "Major mode for editing the criteria for highlighting cpp conditionals.
465 Click on objects to change them.
466 You can also use the keyboard accelerators indicated like this: [K]ey."
467 (buffer-disable-undo)
469 (setq buffer-read-only t
))
471 (defun cpp-edit-apply ()
472 "Apply edited display information to original buffer."
475 (cpp-highlight-buffer t
))
477 (defun cpp-edit-reset ()
478 "Reset display information from original buffer."
480 (let ((buffer (current-buffer))
481 (buffer-read-only nil
)
482 (start (window-start))
485 (set-buffer cpp-edit-buffer
)
486 (setq symbols cpp-parse-symbols
)
488 (setq cpp-edit-symbols symbols
)
490 (insert "CPP Display Information for `")
491 (cpp-make-button (buffer-name cpp-edit-buffer
) 'cpp-edit-home
)
492 (insert "'\n\nClick mouse-2 on item you want to change or use\n"
493 "or switch to this buffer and type the keyboard equivalents.\n"
494 "Keyboard equivalents are indicated with brackets like [T]his.\n\n")
495 (cpp-make-button "[H]ome (display the C file)" 'cpp-edit-home
)
497 (cpp-make-button "[A]pply new settings" 'cpp-edit-apply
)
499 (cpp-make-button "[S]ave settings" 'cpp-edit-save
)
501 (cpp-make-button "[L]oad settings" 'cpp-edit-load
)
504 (insert "[B]ackground: ")
505 (cpp-make-button (car (rassq cpp-face-type cpp-face-type-list
))
506 'cpp-edit-background
)
507 (insert "\n[K]nown conditionals: ")
508 (cpp-make-button (cpp-face-name cpp-known-face
)
509 'cpp-edit-known nil t
)
511 (cpp-make-button (car (rassq cpp-known-writable cpp-writable-list
))
512 'cpp-edit-toggle-known
)
513 (insert "\n[U]nknown conditionals: ")
514 (cpp-make-button (cpp-face-name cpp-unknown-face
)
515 'cpp-edit-unknown nil t
)
517 (cpp-make-button (car (rassq cpp-unknown-writable cpp-writable-list
))
518 'cpp-edit-toggle-unknown
)
519 (insert (format "\n\n\n%39s: %14s %14s %7s\n\n" "Expression"
520 "[T]rue Face" "[F]alse Face" "[W]rite"))
522 (setq symbols
(reverse symbols
))
524 (let* ((symbol (car symbols
))
525 (entry (assoc symbol cpp-edit-list
))
527 (false (nth 2 entry
))
528 (write (if entry
(nth 3 entry
) 'both
)))
529 (setq symbols
(cdr symbols
))
531 (if (and entry
; Make default entries unknown.
532 (or (null true
) (eq true
'default
))
533 (or (null false
) (eq false
'default
))
535 (setq cpp-edit-list
(delq entry cpp-edit-list
)
538 (if (> (length symbol
) 39)
539 (insert (substring symbol
0 39) ": ")
540 (insert (format "%39s: " symbol
)))
542 (cpp-make-button (cpp-face-name true
)
543 'cpp-edit-true symbol t
14)
545 (cpp-make-button (cpp-face-name false
)
546 'cpp-edit-false symbol t
14)
548 (cpp-make-button (car (rassq write cpp-branch-list
))
549 'cpp-edit-write symbol nil
6)
552 (set-window-start nil start
)
555 (defun cpp-edit-load ()
556 "Load cpp configuration."
558 (cond ((null init-file-user
)
559 ;; If -q was specified, don't load any init files.
561 ((file-readable-p cpp-config-file
)
562 (load-file cpp-config-file
))
563 ((file-readable-p (concat "~/" cpp-config-file
))
564 (load-file cpp-config-file
)))
565 (if (derived-mode-p 'cpp-edit-mode
)
568 (defun cpp-edit-save ()
569 "Save the current cpp configuration in a file."
572 (with-current-buffer cpp-edit-buffer
573 (let ((buffer (find-file-noselect cpp-config-file
)))
576 (pp (list 'setq
'cpp-known-face
577 (list 'quote cpp-known-face
)) buffer
)
578 (pp (list 'setq
'cpp-unknown-face
579 (list 'quote cpp-unknown-face
)) buffer
)
580 (pp (list 'setq
'cpp-face-type
581 (list 'quote cpp-face-type
)) buffer
)
582 (pp (list 'setq
'cpp-known-writable
583 (list 'quote cpp-known-writable
)) buffer
)
584 (pp (list 'setq
'cpp-unknown-writable
585 (list 'quote cpp-unknown-writable
)) buffer
)
586 (pp (list 'setq
'cpp-edit-list
587 (list 'quote cpp-edit-list
)) buffer
)
588 (write-file cpp-config-file
))))
590 (defun cpp-edit-home ()
591 "Switch back to original buffer."
595 (pop-to-buffer cpp-edit-buffer
))
597 (defun cpp-edit-background ()
598 "Change default face collection."
600 (call-interactively 'cpp-choose-default-face
)
603 (defun cpp-edit-known ()
604 "Select default for known conditionals."
606 (setq cpp-known-face
(cpp-choose-face "Known face" cpp-known-face
))
609 (defun cpp-edit-unknown ()
610 "Select default for unknown conditionals."
612 (setq cpp-unknown-face
(cpp-choose-face "Unknown face" cpp-unknown-face
))
615 (defun cpp-edit-toggle-known (arg)
616 "Toggle writable status for known conditionals.
617 With optional argument ARG, make them writable if ARG is positive,
618 otherwise make them unwritable."
620 (if (or (and (null arg
) cpp-known-writable
)
621 (<= (prefix-numeric-value arg
) 0))
622 (setq cpp-known-writable nil
)
623 (setq cpp-known-writable t
))
626 (defun cpp-edit-toggle-unknown (arg)
627 "Toggle writable status for unknown conditionals.
628 With optional argument ARG, make them writable if ARG is positive,
629 otherwise make them unwritable."
631 (if (or (and (null arg
) cpp-unknown-writable
)
632 (<= (prefix-numeric-value arg
) 0))
633 (setq cpp-unknown-writable nil
)
634 (setq cpp-unknown-writable t
))
637 (defun cpp-edit-true (symbol face
)
638 "Select SYMBOL's true FACE used for highlighting taken conditionals."
640 (let ((symbol (cpp-choose-symbol)))
642 (cpp-choose-face "True face"
643 (nth 1 (assoc symbol cpp-edit-list
))))))
644 (setcar (nthcdr 1 (cpp-edit-list-entry-get-or-create symbol
)) face
)
647 (defun cpp-edit-false (symbol face
)
648 "Select SYMBOL's false FACE used for highlighting untaken conditionals."
650 (let ((symbol (cpp-choose-symbol)))
652 (cpp-choose-face "False face"
653 (nth 2 (assoc symbol cpp-edit-list
))))))
654 (setcar (nthcdr 2 (cpp-edit-list-entry-get-or-create symbol
)) face
)
657 (defun cpp-edit-write (symbol branch
)
658 "Set which branches of SYMBOL should be writable to BRANCH.
659 BRANCH should be either nil (false branch), t (true branch) or 'both."
660 (interactive (list (cpp-choose-symbol) (cpp-choose-branch)))
661 (setcar (nthcdr 3 (cpp-edit-list-entry-get-or-create symbol
)) branch
)
664 (defun cpp-edit-list-entry-get-or-create (symbol)
665 ;; Return the entry for SYMBOL in `cpp-edit-list'.
666 ;; If it does not exist, create it.
667 (let ((entry (assoc symbol cpp-edit-list
)))
669 (setq entry
(list symbol nil nil
'both nil
)
670 cpp-edit-list
(cons entry cpp-edit-list
)))
675 (defun cpp-choose-symbol ()
676 ;; Choose a symbol if called from keyboard, otherwise use the one clicked on.
679 (completing-read "Symbol: " cpp-edit-symbols nil t
)))
681 (defun cpp-choose-branch ()
682 ;; Choose a branch, either nil, t, or both.
684 (x-popup-menu cpp-button-event
685 (list "Branch" (cons "Branch" cpp-branch-list
)))
686 (cdr (assoc (completing-read "Branch: " cpp-branch-list nil t
)
689 (defun cpp-choose-face (prompt default
)
690 ;; Choose a face from cpp-face-default-list.
691 ;; PROMPT is what to say to the user.
692 ;; DEFAULT is the default face.
693 (or (if cpp-button-event
694 (x-popup-menu cpp-button-event
695 (list prompt
(cons prompt cpp-face-default-list
)))
696 (let ((name (car (rassq default cpp-face-default-list
))))
697 (cdr (assoc (completing-read (if name
699 " (default " name
"): ")
700 (concat prompt
": "))
701 cpp-face-default-list nil t
)
702 cpp-face-all-list
))))
705 (defun cpp-choose-default-face (type)
706 ;; Choose default face list for screen of TYPE.
707 ;; Type must be one of the types defined in `cpp-face-type-list'.
708 (interactive (list (if cpp-button-event
709 (x-popup-menu cpp-button-event
712 cpp-face-type-list
)))
713 (cdr (assoc (completing-read "Screen type: "
716 cpp-face-type-list
)))))
719 (if cpp-face-light-list
721 (setq cpp-face-light-list
722 (mapcar 'cpp-create-bg-face cpp-face-light-name-list
))
723 (setq cpp-face-all-list
724 (append cpp-face-all-list cpp-face-light-list
)))
725 (setq cpp-face-type
'light
)
726 (setq cpp-face-default-list
727 (append cpp-face-light-list cpp-face-none-list
)))
729 (if cpp-face-dark-list
731 (setq cpp-face-dark-list
732 (mapcar 'cpp-create-bg-face cpp-face-dark-name-list
))
733 (setq cpp-face-all-list
734 (append cpp-face-all-list cpp-face-dark-list
)))
735 (setq cpp-face-type
'dark
)
736 (setq cpp-face-default-list
737 (append cpp-face-dark-list cpp-face-none-list
)))
739 (setq cpp-face-type
'mono
)
740 (setq cpp-face-default-list
741 (append cpp-face-mono-list cpp-face-none-list
)))
743 (setq cpp-face-type
'none
)
744 (setq cpp-face-default-list cpp-face-none-list
))))
748 (defun cpp-make-button (name callback
&optional data face padding
)
749 ;; Create a button at point.
750 ;; NAME is the name of the button.
751 ;; CALLBACK is the function to call when the button is pushed.
752 ;; DATA will be made available to CALLBACK
753 ;;in the free variable cpp-callback-data.
754 ;; FACE means that NAME is the name of a face in `cpp-face-all-list'.
755 ;; PADDING means NAME will be right justified at that length.
756 (let ((name (format "%s" name
))
758 (cond ((null padding
)
761 ((> (length name
) padding
)
763 (insert (substring name
0 padding
)))
765 (insert (make-string (- padding
(length name
)) ?
))
771 (let ((check (cdr (assoc name cpp-face-all-list
))))
772 (if (memq check
'(default invisible
))
776 (add-text-properties from to
777 (append (list 'face face
)
778 '(mouse-face highlight
)
779 '(help-echo "mouse-2: change/use this item")
780 (list 'cpp-callback callback
)
781 (if data
(list 'cpp-data data
))))))
783 (defun cpp-push-button (event)
784 ;; Pushed a CPP button.
786 (set-buffer (window-buffer (posn-window (event-start event
))))
787 (let ((pos (posn-point (event-start event
))))
788 (let ((cpp-callback-data (get-text-property pos
'cpp-data
))
789 (fun (get-text-property pos
'cpp-callback
))
790 (cpp-button-event event
))
792 (call-interactively (get-text-property pos
'cpp-callback
)))
793 ((lookup-key global-map
[ down-mouse-2
])
794 (call-interactively (lookup-key global-map
[ down-mouse-2
])))))))
798 (defun cpp-create-bg-face (color)
799 ;; Create entry for face with background COLOR.
800 (cons color
(cons 'background-color color
)))
802 (cpp-choose-default-face
803 (if (or window-system
(display-color-p)) cpp-face-type
'none
))
805 (defun cpp-face-name (face)
806 ;; Return the name of FACE from `cpp-face-all-list'.
807 (let ((entry (rassq (if face face
'default
) cpp-face-all-list
)))
810 (format "<%s>" face
))))
814 (defvar cpp-progress-time
0)
815 ;; Last time we issued a progress message.
817 (defun cpp-progress-message (&rest args
)
818 ;; Report progress at most once a second. Take same ARGS as `message'.
819 (let ((time (nth 1 (current-time))))
820 (if (= time cpp-progress-time
)
822 (setq cpp-progress-time time
)
823 (apply 'message args
))))