]> code.delx.au - gnu-emacs/blobdiff - lisp/progmodes/cpp.el
(compilation-start): Pass nil as startfile to comint-exec.
[gnu-emacs] / lisp / progmodes / cpp.el
index d54cc0b51e08c3b953766a0135331ecf8ed40674..e85c4752412080c14fe6dd5766b7b5037b6c74f6 100644 (file)
@@ -1,15 +1,16 @@
-;;; cpp.el --- Highlight or hide text according to cpp conditionals.
+;;; cpp.el --- highlight or hide text according to cpp conditionals
 
-;; Copyright (C) 1994 Free Software Foundation
+;; Copyright (C) 1994, 1995, 2001, 2002, 2003, 2004, 2005, 2006, 2007
+;; Free Software Foundation
 
-;; Author: Per Abrahamsen <abraham@iesd.auc.dk>
+;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
 ;; Keywords: c, faces, tools
 
 ;; This file is part of GNU Emacs.
 
 ;; GNU Emacs is free software; you can redistribute it and/or modify
 ;; it under the terms of the GNU General Public License as published by
-;; the Free Software Foundation; either version 2, or (at your option)
+;; the Free Software Foundation; either version 3, or (at your option)
 ;; any later version.
 
 ;; GNU Emacs is distributed in the hope that it will be useful,
 ;; GNU General Public License for more details.
 
 ;; You should have received a copy of the GNU General Public License
-;; along with GNU Emacs; see the file COPYING.  If not, write to
-;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
+;; along with GNU Emacs; see the file COPYING.  If not, write to the
+;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+;; Boston, MA 02110-1301, USA.
 
 ;;; Commentary:
 
 ;; Parse a text for C preprocessor conditionals, and highlight or hide
 ;; the text inside the conditionals as you wish.
 
-;; You might want to bind keys as follows.
-;;
-;;   '(progn
-;;      (define-key c-mode-map "\C-c\C-x" 'cpp-parse-buffer)
-;;      (define-key-after (bar (lookup-key c-mode-map [ menu-bar c ]))
-;;        [ cpp-parse ] '("Parse Conditionals" . cpp-parse-buffer) 'up)))
+;; This package is inspired by Jim Coplien's delta editor for SCCS.
 
 ;;; Todo:
 
 ;; Should parse "#if" and "#elif" expressions and merge the faces
-;; somehow. 
+;; somehow.
 
 ;; Somehow it is sometimes possible to make changes near a read only
 ;; area which you can't undo.  Their are other strange effects in that
 ;;; Code:
 
 ;;; Customization:
+(defgroup cpp nil
+  "Highlight or hide text according to cpp conditionals."
+  :link '(custom-group-link :tag "Font Lock Faces group" font-lock-faces)
+  :group 'c
+  :prefix "cpp-")
+
+(defcustom cpp-config-file (convert-standard-filename ".cpp.el")
+  "*File name to save cpp configuration."
+  :type 'file
+  :group 'cpp)
+
+(define-widget 'cpp-face 'lazy
+  "Either a face or the special symbol 'invisible'."
+  :type '(choice (const invisible) (face)))
+
+(defcustom cpp-known-face 'invisible
+  "*Face used for known cpp symbols."
+  :type 'cpp-face
+  :group 'cpp)
+
+(defcustom cpp-unknown-face 'highlight
+  "*Face used for unknown cpp symbols."
+  :type 'cpp-face
+  :group 'cpp)
+
+(defcustom cpp-face-type 'light
+  "*Indicate what background face type you prefer.
+Can be either light or dark for color screens, mono for monochrome
+screens, and none if you don't use a window system and don't have
+a color-capable display."
+  :options '(light dark mono nil)
+  :type 'symbol
+  :group 'cpp)
+
+(defcustom cpp-known-writable t
+  "*Non-nil means you are allowed to modify the known conditionals."
+  :type 'boolean
+  :group 'cpp)
+
+(defcustom cpp-unknown-writable t
+  "*Non-nil means you are allowed to modify the unknown conditionals."
+  :type 'boolean
+  :group 'cpp)
+
+(defcustom cpp-edit-list nil
+  "Alist of cpp macros and information about how they should be displayed.
+Each entry is a list with the following elements:
+0. The name of the macro (a string).
+1. Face used for text that is `ifdef' the macro.
+2. Face used for text that is `ifndef' the macro.
+3. t, nil, or `both' depending on what text may be edited."
+  :type '(repeat (list (string :tag "Macro")
+                      (cpp-face :tag "True")
+                      (cpp-face :tag "False")
+                      (choice (const :tag "True branch writable" t)
+                              (const :tag "False branch writeable" nil)
+                              (const :tag "Both branches writeable" both))))
+  :group 'cpp)
 
-(defvar cpp-known-face 'invisible
-  "*Face used for known cpp symbols.")
+(defvar cpp-overlay-list nil)
+;; List of cpp overlays active in the current buffer.
+(make-variable-buffer-local 'cpp-overlay-list)
 
-(defvar cpp-unknown-face 'highlight
-  "*Face used for unknown cpp cymbols.")
+(defvar cpp-callback-data)
+(defvar cpp-state-stack)
 
-(defvar cpp-face-type 'light 
-  "*Indicate what background face type you prefer.
-Can be either light or dark for color screens, mono for monochrome
-screens, and none if you don't use a window system.")
+(defconst cpp-face-type-list
+  '(("light color background" . light)
+    ("dark color background" . dark)
+    ("monochrome" . mono)
+    ("tty" . none))
+  "Alist of strings and names of the defined face collections.")
+
+(defconst cpp-writable-list
+  ;; Names used for the writable property.
+  '(("writable" . t)
+    ("read-only" . nil)))
+
+(defvar cpp-button-event nil)
+;; This will be t in the callback for `cpp-make-button'.
+
+(defvar cpp-edit-buffer nil)
+;; Real buffer whose cpp display information we are editing.
+(make-variable-buffer-local 'cpp-edit-buffer)
 
-(defvar cpp-known-writable t
-  "*Non-nil means you are allowed to modify the known conditionals.")
+(defconst cpp-branch-list
+  ;; Alist of branches.
+  '(("false" . nil)
+    ("true" . t)
+    ("both" . both)))
 
-(defvar cpp-unknown-writable t
-  "*Non-nil means you are allowed to modify the unknown conditionals.")
+(defcustom cpp-face-default-list nil
+  "Alist of faces you can choose from for cpp conditionals.
+Each element has the form (STRING . FACE), where STRING
+serves as a name (for `cpp-highlight-buffer' only)
+and FACE is either a face (a symbol)
+or a cons cell (background-color . COLOR)."
+  :type '(repeat (cons string (choice face (cons (const background-color) string))))
+  :group 'cpp)
+
+(defcustom cpp-face-light-name-list
+  '("light gray" "light blue" "light cyan" "light yellow" "light pink"
+    "pale green" "beige" "orange" "magenta" "violet" "medium purple"
+    "turquoise")
+  "Background colors useful with dark foreground colors."
+  :type '(repeat string)
+  :group 'cpp)
+
+(defcustom cpp-face-dark-name-list
+  '("dim gray" "blue" "cyan" "yellow" "red"
+    "dark green" "brown" "dark orange" "dark khaki" "dark violet" "purple"
+    "dark turquoise")
+  "Background colors useful with light foreground colors."
+  :type '(repeat string)
+  :group 'cpp)
+
+(defcustom cpp-face-light-list nil
+  "Alist of names and faces to be used for light backgrounds."
+  :type '(repeat (cons string (choice face
+                                     (cons (const background-color) string))))
+  :group 'cpp)
+
+(defcustom cpp-face-dark-list nil
+  "Alist of names and faces to be used for dark backgrounds."
+  :type '(repeat (cons string (choice face
+                                     (cons (const background-color) string))))
+  :group 'cpp)
+
+(defcustom cpp-face-mono-list
+  '(("bold" . bold)
+    ("bold-italic" . bold-italic)
+    ("italic" . italic)
+    ("underline" . underline))
+  "Alist of names and faces to be used for monochrome screens."
+  :type '(repeat (cons string face))
+  :group 'cpp)
+
+(defcustom cpp-face-none-list
+   '(("default" . default)
+     ("invisible" . invisible))
+   "Alist of names and faces available even if you don't use a window system."
+  :type '(repeat (cons string cpp-face))
+  :group 'cpp)
+
+(defvar cpp-face-all-list
+  (append cpp-face-light-list
+         cpp-face-dark-list
+         cpp-face-mono-list
+         cpp-face-none-list)
+  "All faces used for highlighting text inside cpp conditionals.")
 
 ;;; Parse Buffer:
 
@@ -83,15 +213,20 @@ screens, and none if you don't use a window system.")
    "elif\\|else\\|endif\\)\\b\\)"))
 
 ;;;###autoload
-(defun cpp-parse-buffer (arg)
-  "Parse all conditionals in the current buffer end edit symbols.
-A prefix arg supress editing the symbols."
+(defun cpp-highlight-buffer (arg)
+  "Highlight C code according to preprocessor conditionals.
+This command pops up a buffer which you should edit to specify
+what kind of highlighting to use, and the criteria for highlighting.
+A prefix arg suppresses display of that buffer."
   (interactive "P")
+  (unless (or (eq t buffer-invisibility-spec)
+             (memq 'cpp buffer-invisibility-spec))
+    (add-to-invisibility-spec 'cpp))
   (setq cpp-parse-symbols nil)
   (cpp-parse-reset)
   (if (null cpp-edit-list)
       (cpp-edit-load))
-  (let (stack)
+  (let (cpp-state-stack)
     (save-excursion
       (goto-char (point-min))
       (cpp-progress-message "Parsing...")
@@ -129,36 +264,42 @@ A prefix arg supress editing the symbols."
                            (cpp-parse-close from to))
                          (cpp-parse-open t expr from to))
                         ((string-equal type "else")
-                         (or stack (cpp-parse-error "Top level #else"))
-                         (let ((entry (list (not (nth 0 (car stack)))
-                                            (nth 1 (car stack))
+                         (or cpp-state-stack
+                             (cpp-parse-error "Top level #else"))
+                         (let ((entry (list (not (nth 0 (car cpp-state-stack)))
+                                            (nth 1 (car cpp-state-stack))
                                             from to)))
                            (cpp-parse-close from to)
-                           (setq stack (cons entry stack))))
+                           (setq cpp-state-stack (cons entry cpp-state-stack))))
                         ((string-equal type "endif")
                          (cpp-parse-close from to))
                         (t
                          (cpp-parse-error "Parser error"))))))))
       (message "Parsing...done"))
-    (if stack
+    (if cpp-state-stack
       (save-excursion
-       (goto-char (nth 3 (car stack)))
+       (goto-char (nth 3 (car cpp-state-stack)))
        (cpp-parse-error "Unclosed conditional"))))
   (or arg
       (null cpp-parse-symbols)
       (cpp-parse-edit)))
 
 (defun cpp-parse-open (branch expr begin end)
-  ;; Push information about conditional to stack.
+  "Push information about conditional-beginning onto `cpp-state-stack'."
+  ;; Discard comments within this line.
   (while (string-match "\\b[ \t]*/\\*.*\\*/[ \t]*\\b" expr)
     (setq expr (concat (substring expr 0 (match-beginning 0))
                       (substring expr (match-end 0)))))
+  ;; If a comment starts on this line and continues past, discard it.
+  (if (string-match "\\b[ \t]*/\\*" expr)
+      (setq expr (substring expr 0 (match-beginning 0))))
+  ;; Delete any C++ comment from the line.
   (if (string-match "\\b[ \t]*\\(//.*\\)?$" expr)
       (setq expr (substring expr 0 (match-beginning 0))))
   (while (string-match "[ \t]+" expr)
       (setq expr (concat (substring expr 0 (match-beginning 0))
                         (substring expr (match-end 0)))))
-  (setq stack (cons (list branch expr begin end) stack))
+  (setq cpp-state-stack (cons (list branch expr begin end) cpp-state-stack))
   (or (member expr cpp-parse-symbols)
       (setq cpp-parse-symbols
            (cons expr cpp-parse-symbols)))
@@ -167,16 +308,16 @@ A prefix arg supress editing the symbols."
     (cpp-make-unknown-overlay begin end)))
 
 (defun cpp-parse-close (from to)
-  ;; Pop top of stack and create overlay.
-  (let ((entry (assoc (nth 1 (car stack)) cpp-edit-list))
-       (branch (nth 0 (car stack)))
-       (begin (nth 2 (car stack)))
-       (end (nth 3 (car stack))))
-    (setq stack (cdr stack))
+  ;; Pop top of cpp-state-stack and create overlay.
+  (let ((entry (assoc (nth 1 (car cpp-state-stack)) cpp-edit-list))
+       (branch (nth 0 (car cpp-state-stack)))
+       (begin (nth 2 (car cpp-state-stack)))
+       (end (nth 3 (car cpp-state-stack))))
+    (setq cpp-state-stack (cdr cpp-state-stack))
     (if entry
        (let ((face (nth (if branch 1 2) entry))
              (read-only (eq (not branch) (nth 3 entry)))
-             (priority (length stack))
+             (priority (length cpp-state-stack))
              (overlay (make-overlay end from)))
          (cpp-make-known-overlay from to)
          (setq cpp-overlay-list (cons overlay cpp-overlay-list))
@@ -193,7 +334,7 @@ A prefix arg supress editing the symbols."
 
 (defun cpp-parse-error (error)
   ;; Error message issued by the cpp parser.
-  (error (concat error " at line %d") (count-lines (point-min) (point))))
+  (error "%s at line %d" error (count-lines (point-min) (point))))
 
 (defun cpp-parse-reset ()
   "Reset display of cpp conditionals to normal."
@@ -207,7 +348,7 @@ A prefix arg supress editing the symbols."
   "Edit display information for cpp conditionals."
   (interactive)
   (or cpp-parse-symbols
-      (cpp-parse-buffer t))
+      (cpp-highlight-buffer t))
   (let ((buffer (current-buffer)))
     (pop-to-buffer "*CPP Edit*")
     (cpp-edit-mode)
@@ -216,10 +357,6 @@ A prefix arg supress editing the symbols."
 
 ;;; Overlays:
 
-(defvar cpp-overlay-list nil)
-;; List of cpp overlays active in the current buffer.
-(make-variable-buffer-local 'cpp-overlay-list)
-
 (defun cpp-make-known-overlay (start end)
   ;; Create an overlay for a known cpp command from START to END.
   (let ((overlay (make-overlay start end)))
@@ -239,7 +376,7 @@ A prefix arg supress editing the symbols."
     (cond ((eq cpp-unknown-face 'invisible)
           (cpp-make-overlay-hidden overlay))
          ((eq cpp-unknown-face 'default))
-         (t 
+         (t
           (overlay-put overlay 'face cpp-unknown-face)))
     (if cpp-unknown-writable
        ()
@@ -249,11 +386,9 @@ A prefix arg supress editing the symbols."
 
 (defun cpp-make-overlay-hidden (overlay)
   ;; Make overlay hidden and intangible.
-  (overlay-put overlay 'invisible t)
-  (overlay-put overlay 'intangible t)
-  ;; Unfortunately `intangible' is not implemented for overlays yet,
-  ;; so we make is read-only instead.
-  (overlay-put overlay 'modification-hooks '(cpp-signal-read-only)))
+  (overlay-put overlay 'invisible 'cpp)
+  (overlay-put overlay 'modification-hooks '(cpp-signal-read-only))
+  (overlay-put overlay 'insert-in-front-hooks '(cpp-signal-read-only)))
 
 (defun cpp-make-overlay-read-only (overlay)
   ;; Make overlay read only.
@@ -266,29 +401,23 @@ A prefix arg supress editing the symbols."
   (overlay-put overlay 'insert-in-front-hooks '(cpp-grow-overlay))
   (overlay-put overlay 'insert-behind-hooks '(cpp-grow-overlay)))
 
-(defun cpp-signal-read-only (overlay start end)
+(defun cpp-signal-read-only (overlay after start end &optional len)
   ;; Only allow deleting the whole overlay.
   ;; Trying to change a read-only overlay.
-  (if (or (< (overlay-start overlay) start)
-         (> (overlay-end overlay) end))
+  (if (and (not after)
+          (or (< (overlay-start overlay) start)
+              (> (overlay-end overlay) end)))
       (error "This text is read only")))
 
-(defun cpp-grow-overlay (overlay start end)
+(defun cpp-grow-overlay (overlay after start end &optional len)
   ;; Make OVERLAY grow to contain range START to END.
-  (move-overlay overlay
-               (min start (overlay-start overlay))
-               (max end (overlay-end overlay))))
+  (if after
+      (move-overlay overlay
+                   (min start (overlay-start overlay))
+                   (max end (overlay-end overlay)))))
 
 ;;; Edit Buffer:
 
-(defvar cpp-edit-list nil
-  "Alist of cpp macros and information about how they should be displayed.
-Each entry is a list with the following elements:
-0. The name of the macro (a string).
-1. Face used for text that is `ifdef' the macro.
-2. Face used for text that is `ifndef' the macro.
-3. `t', `nil', or `both' depending on what text may be edited.")
-
 (defvar cpp-edit-map nil)
 ;; Keymap for `cpp-edit-mode'.
 
@@ -331,17 +460,13 @@ Each entry is a list with the following elements:
   (define-key cpp-edit-map "q" 'bury-buffer)
   (define-key cpp-edit-map "Q" 'bury-buffer))
 
-(defvar cpp-edit-buffer nil)
-;; Real buffer whose cpp display information we are editing.
-(make-variable-buffer-local 'cpp-edit-buffer)
-
 (defvar cpp-edit-symbols nil)
 ;; Symbols defined in the edit buffer.
 (make-variable-buffer-local 'cpp-edit-symbols)
 
 (defun cpp-edit-mode ()
-  "Major mode for editing cpp display information.
-Click on objects to change them.  
+  "Major mode for editing the criteria for highlighting cpp conditionals.
+Click on objects to change them.
 You can also use the keyboard accelerators indicated like this: [K]ey."
   (kill-all-local-variables)
   (buffer-disable-undo)
@@ -355,7 +480,7 @@ You can also use the keyboard accelerators indicated like this: [K]ey."
   "Apply edited display information to original buffer."
   (interactive)
   (cpp-edit-home)
-  (cpp-parse-buffer t))
+  (cpp-highlight-buffer t))
 
 (defun cpp-edit-reset ()
   "Reset display information from original buffer."
@@ -372,16 +497,18 @@ You can also use the keyboard accelerators indicated like this: [K]ey."
     (erase-buffer)
     (insert "CPP Display Information for `")
     (cpp-make-button (buffer-name cpp-edit-buffer) 'cpp-edit-home)
-    (insert "' ")
-    (cpp-make-button "[H]ome" 'cpp-edit-home)
-    (insert " ")
-    (cpp-make-button "[A]pply" 'cpp-edit-apply)
-    (insert " ")
-    (cpp-make-button "[S]ave" 'cpp-edit-save)
-    (insert " ")
-    (cpp-make-button "[L]oad" 'cpp-edit-load)
-    (insert "\n\nClick mouse-2 on item you want to change or use\n"
-           "keyboard equivalent indicated with brackets like [T]his.\n\n")
+    (insert "'\n\nClick mouse-2 on item you want to change or use\n"
+           "or switch to this buffer and type the keyboard equivalents.\n"
+           "Keyboard equivalents are indicated with brackets like [T]his.\n\n")
+    (cpp-make-button "[H]ome (display the C file)" 'cpp-edit-home)
+    (insert "  ")
+    (cpp-make-button "[A]pply new settings" 'cpp-edit-apply)
+    (insert "\n")
+    (cpp-make-button "[S]ave settings" 'cpp-edit-save)
+    (insert "  ")
+    (cpp-make-button "[L]oad settings" 'cpp-edit-load)
+    (insert "\n\n")
+
     (insert "[B]ackground: ")
     (cpp-make-button (car (rassq cpp-face-type cpp-face-type-list))
                     'cpp-edit-background)
@@ -399,6 +526,8 @@ You can also use the keyboard accelerators indicated like this: [K]ey."
                     'cpp-edit-toggle-unknown)
     (insert (format "\n\n\n%39s: %14s %14s %7s\n\n" "Expression"
                    "[T]rue Face" "[F]alse Face" "[W]rite"))
+
+    (setq symbols (reverse symbols))
     (while symbols
       (let*  ((symbol (car symbols))
              (entry (assoc symbol cpp-edit-list))
@@ -413,8 +542,8 @@ You can also use the keyboard accelerators indicated like this: [K]ey."
                 (eq write 'both))
            (setq cpp-edit-list (delq entry cpp-edit-list)
                  entry nil))
-       
-       (if (> (length symbol) 29)
+
+       (if (> (length symbol) 39)
            (insert (substring symbol 0 39) ": ")
          (insert (format "%39s: " symbol)))
 
@@ -434,19 +563,23 @@ You can also use the keyboard accelerators indicated like this: [K]ey."
 (defun cpp-edit-load ()
   "Load cpp configuration."
   (interactive)
-  (cond ((file-readable-p ".cpp.el")
-        (load-file ".cpp.el"))
-       ((file-readable-p "~/.cpp.el")
-        (load-file ".cpp.el")))
-  (cpp-edit-reset))
+  (cond ((null init-file-user)
+        ;; If -q was specified, don't load any init files.
+        nil)
+       ((file-readable-p cpp-config-file)
+        (load-file cpp-config-file))
+       ((file-readable-p (concat "~/" cpp-config-file))
+        (load-file cpp-config-file)))
+  (if (eq major-mode 'cpp-edit-mode)
+      (cpp-edit-reset)))
 
 (defun cpp-edit-save ()
-  "Load cpp configuration."
+  "Save the current cpp configuration in a file."
   (interactive)
   (require 'pp)
   (save-excursion
     (set-buffer cpp-edit-buffer)
-    (let ((buffer (find-file-noselect ".cpp.el")))
+    (let ((buffer (find-file-noselect cpp-config-file)))
       (set-buffer buffer)
       (erase-buffer)
       (pp (list 'setq 'cpp-known-face
@@ -461,7 +594,7 @@ You can also use the keyboard accelerators indicated like this: [K]ey."
                (list 'quote cpp-unknown-writable)) buffer)
       (pp (list 'setq 'cpp-edit-list
                (list 'quote cpp-edit-list)) buffer)
-      (write-file ".cpp.el"))))
+      (write-file cpp-config-file))))
 
 (defun cpp-edit-home ()
   "Switch back to original buffer."
@@ -488,11 +621,6 @@ You can also use the keyboard accelerators indicated like this: [K]ey."
   (setq cpp-unknown-face (cpp-choose-face "Unknown face" cpp-unknown-face))
   (cpp-edit-reset))
 
-(defconst cpp-writable-list
-  ;; Names used for the writable property.
-  '(("writable" . t)
-    ("read-only" . nil)))
-
 (defun cpp-edit-toggle-known (arg)
   "Toggle writable status for known conditionals.
 With optional argument ARG, make them writable iff ARG is positive."
@@ -528,7 +656,7 @@ With optional argument ARG, make them writable iff ARG is positive."
   (interactive
    (let ((symbol (cpp-choose-symbol)))
      (list symbol
-          (cpp-choose-face "False face" 
+          (cpp-choose-face "False face"
                            (nth 2 (assoc symbol cpp-edit-list))))))
   (setcar (nthcdr 2 (cpp-edit-list-entry-get-or-create symbol)) face)
   (cpp-edit-reset))
@@ -554,14 +682,8 @@ BRANCH should be either nil (false branch), t (true branch) or 'both."
 (defun cpp-choose-symbol ()
   ;; Choose a symbol if called from keyboard, otherwise use the one clicked on.
   (if cpp-button-event
-      data
-    (completing-read "Symbol: " (mapcar 'list cpp-edit-symbols) nil t)))
-
-(defconst cpp-branch-list
-  ;; Alist of branches.
-  '(("false" . nil)
-    ("true" . t)
-    ("both" . both)))
+      cpp-callback-data
+    (completing-read "Symbol: " cpp-edit-symbols nil t)))
 
 (defun cpp-choose-branch ()
   ;; Choose a branch, either nil, t, or both.
@@ -572,7 +694,7 @@ BRANCH should be either nil (false branch), t (true branch) or 'both."
                cpp-branch-list))))
 
 (defun cpp-choose-face (prompt default)
-  ;; Choose a face from cpp-face-defalt-list.
+  ;; Choose a face from cpp-face-default-list.
   ;; PROMPT is what to say to the user.
   ;; DEFAULT is the default face.
   (or (if cpp-button-event
@@ -587,13 +709,6 @@ BRANCH should be either nil (false branch), t (true branch) or 'both."
                      cpp-face-all-list))))
       default))
 
-(defconst cpp-face-type-list
-  '(("light color background" . light)
-    ("dark color background" . dark)
-    ("monochrome" . mono)
-    ("tty" . none))
-  "Alist of strings and names of the defined face collections.")
-
 (defun cpp-choose-default-face (type)
   ;; Choose default face list for screen of TYPE.
   ;; Type must be one of the types defined in `cpp-face-type-list'.
@@ -637,14 +752,12 @@ BRANCH should be either nil (false branch), t (true branch) or 'both."
 
 ;;; Buttons:
 
-(defvar cpp-button-event nil)
-;; This will be t in the callback for `cpp-make-button'.
-
 (defun cpp-make-button (name callback &optional data face padding)
   ;; Create a button at point.
   ;; NAME is the name of the button.
   ;; CALLBACK is the function to call when the button is pushed.
-  ;; DATA will be available to CALLBACK as a free variable.
+  ;; DATA will be made available to CALLBACK
+  ;;in the free variable cpp-callback-data.
   ;; FACE means that NAME is the name of a face in `cpp-face-all-list'.
   ;; PADDING means NAME will be right justified at that length.
   (let ((name (format "%s" name))
@@ -670,6 +783,7 @@ BRANCH should be either nil (false branch), t (true branch) or 'both."
     (add-text-properties from to
                         (append (list 'face face)
                                 '(mouse-face highlight)
+                                '(help-echo "mouse-2: change/use this item")
                                 (list 'cpp-callback callback)
                                 (if data (list 'cpp-data data))))))
 
@@ -678,7 +792,7 @@ BRANCH should be either nil (false branch), t (true branch) or 'both."
   (interactive "@e")
   (set-buffer (window-buffer (posn-window (event-start event))))
   (let ((pos (posn-point (event-start event))))
-    (let ((data (get-text-property pos 'cpp-data))
+    (let ((cpp-callback-data (get-text-property pos 'cpp-data))
          (fun (get-text-property pos 'cpp-callback))
          (cpp-button-event event))
       (cond (fun
@@ -688,54 +802,12 @@ BRANCH should be either nil (false branch), t (true branch) or 'both."
 
 ;;; Faces:
 
-(defvar cpp-face-light-name-list
-  '("light gray" "light blue" "light cyan" "light yellow" "light pink"
-    "pale green" "beige" "orange" "magenta" "violet" "medium purple"
-    "turquoise")
-  "Background colours useful with dark foreground colors.")
-
-(defvar cpp-face-dark-name-list
-  '("dim gray" "blue" "cyan" "yellow" "red"
-    "dark green" "brown" "dark orange" "dark khaki" "dark violet" "purple"
-    "dark turquoise")
-  "Background colours useful with light foreground colors.")
-
-(defvar cpp-face-light-list nil
-  "Alist of names and faces to be used for light backgrounds.")
-
-(defvar cpp-face-dark-list nil
-  "Alist of names and faces to be used for dark backgrounds.")
-
-(defvar cpp-face-mono-list
-  '(("bold" . 'bold)
-    ("bold-italic" . 'bold-italic)
-    ("italic" . 'italic)
-    ("underline" . 'underline))
-  "Alist of names and faces to be used for monocrome screens.")
-
-(defvar cpp-face-none-list
-   '(("default" . default)
-     ("invisible" . invisible))
-   "Alist of names and faces available even if you don't use a window system.")
-
-(defvar cpp-face-all-list
-  (append cpp-face-light-list
-         cpp-face-dark-list
-         cpp-face-mono-list
-         cpp-face-none-list)
-  "All faces used for highligting text inside cpp conditionals.")
-
-(defvar cpp-face-default-list nil
-  "List of faces you can choose from for cpp conditionals.")
-
 (defun cpp-create-bg-face (color)
   ;; Create entry for face with background COLOR.
-  (let ((name (intern (concat "cpp " color))))
-    (make-face name)
-    (set-face-background name color)
-    (cons color name)))
+  (cons color (cons 'background-color color)))
 
-(cpp-choose-default-face (if window-system cpp-face-type 'none))
+(cpp-choose-default-face
+ (if (or window-system (display-color-p)) cpp-face-type 'none))
 
 (defun cpp-face-name (face)
   ;; Return the name of FACE from `cpp-face-all-list'.
@@ -759,4 +831,5 @@ BRANCH should be either nil (false branch), t (true branch) or 'both."
 
 (provide 'cpp)
 
+;;; arch-tag: fb7d433d-745d-495a-96f0-86908ab63f74
 ;;; cpp.el ends here