-;;; cpp.el --- Highlight or hide text according to cpp conditionals.
+;;; cpp.el --- highlight or hide text according to cpp conditionals
-;; Copyright (C) 1994, 1995 Free Software Foundation
+;; Copyright (C) 1994, 1995, 2001, 2002, 2003, 2004, 2005
+;; 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 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:
;;; 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:
-
-(defvar cpp-known-face 'invisible
- "*Face used for known cpp symbols.")
-
-(defvar cpp-unknown-face 'highlight
- "*Face used for unknown cpp cymbols.")
-
-(defvar cpp-face-type 'light
+(defgroup cpp nil
+ "Highlight or hide text according to cpp conditionals."
+ :group 'c
+ :prefix "cpp-")
+
+(defcustom cpp-config-file (convert-standard-filename ".cpp.el")
+ "*File name to save cpp configuration."
+ :type 'file
+ :group 'cpp)
+
+(defcustom cpp-known-face 'invisible
+ "*Face used for known cpp symbols."
+ :type 'face
+ :group 'cpp)
+
+(defcustom cpp-unknown-face 'highlight
+ "*Face used for unknown cpp symbols."
+ :type '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.")
-
-(defvar cpp-known-writable t
- "*Non-nil means you are allowed to modify the known conditionals.")
-
-(defvar cpp-unknown-writable t
- "*Non-nil means you are allowed to modify the unknown conditionals.")
-
-(defvar cpp-edit-list nil
+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.")
+3. t, nil, or `both' depending on what text may be edited."
+ :type '(repeat (list string face face
+ (choice (const t)
+ (const nil)
+ (const both))))
+ :group 'cpp)
(defvar cpp-overlay-list nil)
;; List of cpp overlays active in the current buffer.
("true" . t)
("both" . both)))
-(defvar cpp-face-default-list nil
- "List of faces you can choose from for cpp 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)
-(defvar cpp-face-light-name-list
+(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 colours useful with dark foreground colors.")
+ "Background colors useful with dark foreground colors."
+ :type '(repeat string)
+ :group 'cpp)
-(defvar cpp-face-dark-name-list
+(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 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
+ "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.")
+ "Alist of names and faces available even if you don't use a window system."
+ :type '(repeat (cons string 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 highligting text inside cpp conditionals.")
+ "All faces used for highlighting text inside cpp conditionals.")
;;; Parse Buffer:
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)
(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."
(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
()
(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.
(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:
(defun cpp-edit-mode ()
"Major mode for editing the criteria for highlighting cpp conditionals.
-Click on objects to change them.
+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)
(erase-buffer)
(insert "CPP Display Information for `")
(cpp-make-button (buffer-name cpp-edit-buffer) 'cpp-edit-home)
- (insert "\n\nClick mouse-2 on item you want to change or use\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)
(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)))
(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")))
+ (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
(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."
(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))
;; Choose a symbol if called from keyboard, otherwise use the one clicked on.
(if cpp-button-event
cpp-callback-data
- (completing-read "Symbol: " (mapcar 'list cpp-edit-symbols) nil t)))
+ (completing-read "Symbol: " cpp-edit-symbols nil t)))
(defun cpp-choose-branch ()
;; Choose a branch, either nil, t, 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
(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))))))
(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'.
(provide 'cpp)
+;;; arch-tag: fb7d433d-745d-495a-96f0-86908ab63f74
;;; cpp.el ends here