comparison lisp/progmodes/cpp.el @ 11480:5865f4bc9521

(cpp-edit-list): Move definition toward start of file. (cpp-face-type-list, cpp-writable-list, cpp-button-event) (cpp-edit-buffer, cpp-branch-list, cpp-face-default-list) (cpp-face-light-name-list, cpp-face-dark-name-list) (cpp-face-light-list, cpp-face-dark-list, cpp-face-mono-list) (cpp-face-none-list, cpp-face-all-list, cpp-overlay-list): Likewise. (cpp-highlight-buffer): Rename stack to cpp-state-stack. (cpp-parse-open, cpp-parse-close): Likewise. (cpp-push-button, cpp-choose-symbol): Rename data to cpp-callback-data. (cpp-state-stack, cpp-callback-data): Add defvars.
author Richard M. Stallman <rms@gnu.org>
date Tue, 18 Apr 1995 07:21:52 +0000
parents 0950bf9c8d06
children 2e09c796bf70
comparison
equal deleted inserted replaced
11479:266653a85b65 11480:5865f4bc9521
1 ;;; cpp.el --- Highlight or hide text according to cpp conditionals. 1 ;;; cpp.el --- Highlight or hide text according to cpp conditionals.
2 2
3 ;; Copyright (C) 1994 Free Software Foundation 3 ;; Copyright (C) 1994, 1995 Free Software Foundation
4 4
5 ;; Author: Per Abrahamsen <abraham@iesd.auc.dk> 5 ;; Author: Per Abrahamsen <abraham@iesd.auc.dk>
6 ;; Keywords: c, faces, tools 6 ;; Keywords: c, faces, tools
7 7
8 ;; This file is part of GNU Emacs. 8 ;; This file is part of GNU Emacs.
61 (defvar cpp-known-writable t 61 (defvar cpp-known-writable t
62 "*Non-nil means you are allowed to modify the known conditionals.") 62 "*Non-nil means you are allowed to modify the known conditionals.")
63 63
64 (defvar cpp-unknown-writable t 64 (defvar cpp-unknown-writable t
65 "*Non-nil means you are allowed to modify the unknown conditionals.") 65 "*Non-nil means you are allowed to modify the unknown conditionals.")
66
67 (defvar cpp-edit-list nil
68 "Alist of cpp macros and information about how they should be displayed.
69 Each entry is a list with the following elements:
70 0. The name of the macro (a string).
71 1. Face used for text that is `ifdef' the macro.
72 2. Face used for text that is `ifndef' the macro.
73 3. `t', `nil', or `both' depending on what text may be edited.")
74
75 (defvar cpp-overlay-list nil)
76 ;; List of cpp overlays active in the current buffer.
77 (make-variable-buffer-local 'cpp-overlay-list)
78
79 (defvar cpp-callback-data)
80 (defvar cpp-state-stack)
81
82 (defconst cpp-face-type-list
83 '(("light color background" . light)
84 ("dark color background" . dark)
85 ("monochrome" . mono)
86 ("tty" . none))
87 "Alist of strings and names of the defined face collections.")
88
89 (defconst cpp-writable-list
90 ;; Names used for the writable property.
91 '(("writable" . t)
92 ("read-only" . nil)))
93
94 (defvar cpp-button-event nil)
95 ;; This will be t in the callback for `cpp-make-button'.
96
97 (defvar cpp-edit-buffer nil)
98 ;; Real buffer whose cpp display information we are editing.
99 (make-variable-buffer-local 'cpp-edit-buffer)
100
101 (defconst cpp-branch-list
102 ;; Alist of branches.
103 '(("false" . nil)
104 ("true" . t)
105 ("both" . both)))
106
107 (defvar cpp-face-default-list nil
108 "List of faces you can choose from for cpp conditionals.")
109
110 (defvar cpp-face-light-name-list
111 '("light gray" "light blue" "light cyan" "light yellow" "light pink"
112 "pale green" "beige" "orange" "magenta" "violet" "medium purple"
113 "turquoise")
114 "Background colours useful with dark foreground colors.")
115
116 (defvar cpp-face-dark-name-list
117 '("dim gray" "blue" "cyan" "yellow" "red"
118 "dark green" "brown" "dark orange" "dark khaki" "dark violet" "purple"
119 "dark turquoise")
120 "Background colours useful with light foreground colors.")
121
122 (defvar cpp-face-light-list nil
123 "Alist of names and faces to be used for light backgrounds.")
124
125 (defvar cpp-face-dark-list nil
126 "Alist of names and faces to be used for dark backgrounds.")
127
128 (defvar cpp-face-mono-list
129 '(("bold" . 'bold)
130 ("bold-italic" . 'bold-italic)
131 ("italic" . 'italic)
132 ("underline" . 'underline))
133 "Alist of names and faces to be used for monocrome screens.")
134
135 (defvar cpp-face-none-list
136 '(("default" . default)
137 ("invisible" . invisible))
138 "Alist of names and faces available even if you don't use a window system.")
139
140 (defvar cpp-face-all-list
141 (append cpp-face-light-list
142 cpp-face-dark-list
143 cpp-face-mono-list
144 cpp-face-none-list)
145 "All faces used for highligting text inside cpp conditionals.")
66 146
67 ;;; Parse Buffer: 147 ;;; Parse Buffer:
68 148
69 (defvar cpp-parse-symbols nil 149 (defvar cpp-parse-symbols nil
70 "List of cpp macros used in the local buffer.") 150 "List of cpp macros used in the local buffer.")
86 (interactive "P") 166 (interactive "P")
87 (setq cpp-parse-symbols nil) 167 (setq cpp-parse-symbols nil)
88 (cpp-parse-reset) 168 (cpp-parse-reset)
89 (if (null cpp-edit-list) 169 (if (null cpp-edit-list)
90 (cpp-edit-load)) 170 (cpp-edit-load))
91 (let (stack) 171 (let (cpp-state-stack)
92 (save-excursion 172 (save-excursion
93 (goto-char (point-min)) 173 (goto-char (point-min))
94 (cpp-progress-message "Parsing...") 174 (cpp-progress-message "Parsing...")
95 (while (re-search-forward cpp-parse-regexp nil t) 175 (while (re-search-forward cpp-parse-regexp nil t)
96 (cpp-progress-message "Parsing...%d%%" 176 (cpp-progress-message "Parsing...%d%%"
124 ((string-equal type "elif") 204 ((string-equal type "elif")
125 (let (cpp-known-face cpp-unknown-face) 205 (let (cpp-known-face cpp-unknown-face)
126 (cpp-parse-close from to)) 206 (cpp-parse-close from to))
127 (cpp-parse-open t expr from to)) 207 (cpp-parse-open t expr from to))
128 ((string-equal type "else") 208 ((string-equal type "else")
129 (or stack (cpp-parse-error "Top level #else")) 209 (or cpp-state-stack
130 (let ((entry (list (not (nth 0 (car stack))) 210 (cpp-parse-error "Top level #else"))
131 (nth 1 (car stack)) 211 (let ((entry (list (not (nth 0 (car cpp-state-stack)))
212 (nth 1 (car cpp-state-stack))
132 from to))) 213 from to)))
133 (cpp-parse-close from to) 214 (cpp-parse-close from to)
134 (setq stack (cons entry stack)))) 215 (setq cpp-state-stack (cons entry cpp-state-stack))))
135 ((string-equal type "endif") 216 ((string-equal type "endif")
136 (cpp-parse-close from to)) 217 (cpp-parse-close from to))
137 (t 218 (t
138 (cpp-parse-error "Parser error")))))))) 219 (cpp-parse-error "Parser error"))))))))
139 (message "Parsing...done")) 220 (message "Parsing...done"))
140 (if stack 221 (if cpp-state-stack
141 (save-excursion 222 (save-excursion
142 (goto-char (nth 3 (car stack))) 223 (goto-char (nth 3 (car cpp-state-stack)))
143 (cpp-parse-error "Unclosed conditional")))) 224 (cpp-parse-error "Unclosed conditional"))))
144 (or arg 225 (or arg
145 (null cpp-parse-symbols) 226 (null cpp-parse-symbols)
146 (cpp-parse-edit))) 227 (cpp-parse-edit)))
147 228
148 (defun cpp-parse-open (branch expr begin end) 229 (defun cpp-parse-open (branch expr begin end)
149 "Push information about conditional-beginning onto stack." 230 "Push information about conditional-beginning onto `cpp-state-stack'."
150 ;; Discard comments within this line. 231 ;; Discard comments within this line.
151 (while (string-match "\\b[ \t]*/\\*.*\\*/[ \t]*\\b" expr) 232 (while (string-match "\\b[ \t]*/\\*.*\\*/[ \t]*\\b" expr)
152 (setq expr (concat (substring expr 0 (match-beginning 0)) 233 (setq expr (concat (substring expr 0 (match-beginning 0))
153 (substring expr (match-end 0))))) 234 (substring expr (match-end 0)))))
154 ;; If a comment starts on this line and continues past, discard it. 235 ;; If a comment starts on this line and continues past, discard it.
158 (if (string-match "\\b[ \t]*\\(//.*\\)?$" expr) 239 (if (string-match "\\b[ \t]*\\(//.*\\)?$" expr)
159 (setq expr (substring expr 0 (match-beginning 0)))) 240 (setq expr (substring expr 0 (match-beginning 0))))
160 (while (string-match "[ \t]+" expr) 241 (while (string-match "[ \t]+" expr)
161 (setq expr (concat (substring expr 0 (match-beginning 0)) 242 (setq expr (concat (substring expr 0 (match-beginning 0))
162 (substring expr (match-end 0))))) 243 (substring expr (match-end 0)))))
163 (setq stack (cons (list branch expr begin end) stack)) 244 (setq cpp-state-stack (cons (list branch expr begin end) cpp-state-stack))
164 (or (member expr cpp-parse-symbols) 245 (or (member expr cpp-parse-symbols)
165 (setq cpp-parse-symbols 246 (setq cpp-parse-symbols
166 (cons expr cpp-parse-symbols))) 247 (cons expr cpp-parse-symbols)))
167 (if (assoc expr cpp-edit-list) 248 (if (assoc expr cpp-edit-list)
168 (cpp-make-known-overlay begin end) 249 (cpp-make-known-overlay begin end)
169 (cpp-make-unknown-overlay begin end))) 250 (cpp-make-unknown-overlay begin end)))
170 251
171 (defun cpp-parse-close (from to) 252 (defun cpp-parse-close (from to)
172 ;; Pop top of stack and create overlay. 253 ;; Pop top of cpp-state-stack and create overlay.
173 (let ((entry (assoc (nth 1 (car stack)) cpp-edit-list)) 254 (let ((entry (assoc (nth 1 (car cpp-state-stack)) cpp-edit-list))
174 (branch (nth 0 (car stack))) 255 (branch (nth 0 (car cpp-state-stack)))
175 (begin (nth 2 (car stack))) 256 (begin (nth 2 (car cpp-state-stack)))
176 (end (nth 3 (car stack)))) 257 (end (nth 3 (car cpp-state-stack))))
177 (setq stack (cdr stack)) 258 (setq cpp-state-stack (cdr cpp-state-stack))
178 (if entry 259 (if entry
179 (let ((face (nth (if branch 1 2) entry)) 260 (let ((face (nth (if branch 1 2) entry))
180 (read-only (eq (not branch) (nth 3 entry))) 261 (read-only (eq (not branch) (nth 3 entry)))
181 (priority (length stack)) 262 (priority (length cpp-state-stack))
182 (overlay (make-overlay end from))) 263 (overlay (make-overlay end from)))
183 (cpp-make-known-overlay from to) 264 (cpp-make-known-overlay from to)
184 (setq cpp-overlay-list (cons overlay cpp-overlay-list)) 265 (setq cpp-overlay-list (cons overlay cpp-overlay-list))
185 (if priority (overlay-put overlay 'priority priority)) 266 (if priority (overlay-put overlay 'priority priority))
186 (cond ((eq face 'invisible) 267 (cond ((eq face 'invisible)
216 (setq cpp-edit-buffer buffer) 297 (setq cpp-edit-buffer buffer)
217 (cpp-edit-reset))) 298 (cpp-edit-reset)))
218 299
219 ;;; Overlays: 300 ;;; Overlays:
220 301
221 (defvar cpp-overlay-list nil)
222 ;; List of cpp overlays active in the current buffer.
223 (make-variable-buffer-local 'cpp-overlay-list)
224
225 (defun cpp-make-known-overlay (start end) 302 (defun cpp-make-known-overlay (start end)
226 ;; Create an overlay for a known cpp command from START to END. 303 ;; Create an overlay for a known cpp command from START to END.
227 (let ((overlay (make-overlay start end))) 304 (let ((overlay (make-overlay start end)))
228 (if (eq cpp-known-face 'invisible) 305 (if (eq cpp-known-face 'invisible)
229 (cpp-make-overlay-hidden overlay) 306 (cpp-make-overlay-hidden overlay)
280 (move-overlay overlay 357 (move-overlay overlay
281 (min start (overlay-start overlay)) 358 (min start (overlay-start overlay))
282 (max end (overlay-end overlay)))) 359 (max end (overlay-end overlay))))
283 360
284 ;;; Edit Buffer: 361 ;;; Edit Buffer:
285
286 (defvar cpp-edit-list nil
287 "Alist of cpp macros and information about how they should be displayed.
288 Each entry is a list with the following elements:
289 0. The name of the macro (a string).
290 1. Face used for text that is `ifdef' the macro.
291 2. Face used for text that is `ifndef' the macro.
292 3. `t', `nil', or `both' depending on what text may be edited.")
293 362
294 (defvar cpp-edit-map nil) 363 (defvar cpp-edit-map nil)
295 ;; Keymap for `cpp-edit-mode'. 364 ;; Keymap for `cpp-edit-mode'.
296 365
297 (if cpp-edit-map 366 (if cpp-edit-map
331 (define-key cpp-edit-map "Y" 'cpp-edit-toggle-unknown) 400 (define-key cpp-edit-map "Y" 'cpp-edit-toggle-unknown)
332 (define-key cpp-edit-map "y" 'cpp-edit-toggle-unknown) 401 (define-key cpp-edit-map "y" 'cpp-edit-toggle-unknown)
333 (define-key cpp-edit-map "q" 'bury-buffer) 402 (define-key cpp-edit-map "q" 'bury-buffer)
334 (define-key cpp-edit-map "Q" 'bury-buffer)) 403 (define-key cpp-edit-map "Q" 'bury-buffer))
335 404
336 (defvar cpp-edit-buffer nil)
337 ;; Real buffer whose cpp display information we are editing.
338 (make-variable-buffer-local 'cpp-edit-buffer)
339
340 (defvar cpp-edit-symbols nil) 405 (defvar cpp-edit-symbols nil)
341 ;; Symbols defined in the edit buffer. 406 ;; Symbols defined in the edit buffer.
342 (make-variable-buffer-local 'cpp-edit-symbols) 407 (make-variable-buffer-local 'cpp-edit-symbols)
343 408
344 (defun cpp-edit-mode () 409 (defun cpp-edit-mode ()
491 "Select default for unknown conditionals." 556 "Select default for unknown conditionals."
492 (interactive) 557 (interactive)
493 (setq cpp-unknown-face (cpp-choose-face "Unknown face" cpp-unknown-face)) 558 (setq cpp-unknown-face (cpp-choose-face "Unknown face" cpp-unknown-face))
494 (cpp-edit-reset)) 559 (cpp-edit-reset))
495 560
496 (defconst cpp-writable-list
497 ;; Names used for the writable property.
498 '(("writable" . t)
499 ("read-only" . nil)))
500
501 (defun cpp-edit-toggle-known (arg) 561 (defun cpp-edit-toggle-known (arg)
502 "Toggle writable status for known conditionals. 562 "Toggle writable status for known conditionals.
503 With optional argument ARG, make them writable iff ARG is positive." 563 With optional argument ARG, make them writable iff ARG is positive."
504 (interactive "@P") 564 (interactive "@P")
505 (if (or (and (null arg) cpp-known-writable) 565 (if (or (and (null arg) cpp-known-writable)
557 ;;; Prompts: 617 ;;; Prompts:
558 618
559 (defun cpp-choose-symbol () 619 (defun cpp-choose-symbol ()
560 ;; Choose a symbol if called from keyboard, otherwise use the one clicked on. 620 ;; Choose a symbol if called from keyboard, otherwise use the one clicked on.
561 (if cpp-button-event 621 (if cpp-button-event
562 data 622 cpp-callback-data
563 (completing-read "Symbol: " (mapcar 'list cpp-edit-symbols) nil t))) 623 (completing-read "Symbol: " (mapcar 'list cpp-edit-symbols) nil t)))
564
565 (defconst cpp-branch-list
566 ;; Alist of branches.
567 '(("false" . nil)
568 ("true" . t)
569 ("both" . both)))
570 624
571 (defun cpp-choose-branch () 625 (defun cpp-choose-branch ()
572 ;; Choose a branch, either nil, t, or both. 626 ;; Choose a branch, either nil, t, or both.
573 (if cpp-button-event 627 (if cpp-button-event
574 (x-popup-menu cpp-button-event 628 (x-popup-menu cpp-button-event
589 " (default " name "): ") 643 " (default " name "): ")
590 (concat prompt ": ")) 644 (concat prompt ": "))
591 cpp-face-default-list nil t) 645 cpp-face-default-list nil t)
592 cpp-face-all-list)))) 646 cpp-face-all-list))))
593 default)) 647 default))
594
595 (defconst cpp-face-type-list
596 '(("light color background" . light)
597 ("dark color background" . dark)
598 ("monochrome" . mono)
599 ("tty" . none))
600 "Alist of strings and names of the defined face collections.")
601 648
602 (defun cpp-choose-default-face (type) 649 (defun cpp-choose-default-face (type)
603 ;; Choose default face list for screen of TYPE. 650 ;; Choose default face list for screen of TYPE.
604 ;; Type must be one of the types defined in `cpp-face-type-list'. 651 ;; Type must be one of the types defined in `cpp-face-type-list'.
605 (interactive (list (if cpp-button-event 652 (interactive (list (if cpp-button-event
640 (setq cpp-face-type 'none) 687 (setq cpp-face-type 'none)
641 (setq cpp-face-default-list cpp-face-none-list)))) 688 (setq cpp-face-default-list cpp-face-none-list))))
642 689
643 ;;; Buttons: 690 ;;; Buttons:
644 691
645 (defvar cpp-button-event nil)
646 ;; This will be t in the callback for `cpp-make-button'.
647
648 (defun cpp-make-button (name callback &optional data face padding) 692 (defun cpp-make-button (name callback &optional data face padding)
649 ;; Create a button at point. 693 ;; Create a button at point.
650 ;; NAME is the name of the button. 694 ;; NAME is the name of the button.
651 ;; CALLBACK is the function to call when the button is pushed. 695 ;; CALLBACK is the function to call when the button is pushed.
652 ;; DATA will be available to CALLBACK as a free variable. 696 ;; DATA will be made available to CALLBACK
697 ;;in the free variable cpp-callback-data.
653 ;; FACE means that NAME is the name of a face in `cpp-face-all-list'. 698 ;; FACE means that NAME is the name of a face in `cpp-face-all-list'.
654 ;; PADDING means NAME will be right justified at that length. 699 ;; PADDING means NAME will be right justified at that length.
655 (let ((name (format "%s" name)) 700 (let ((name (format "%s" name))
656 from to) 701 from to)
657 (cond ((null padding) 702 (cond ((null padding)
681 (defun cpp-push-button (event) 726 (defun cpp-push-button (event)
682 ;; Pushed a CPP button. 727 ;; Pushed a CPP button.
683 (interactive "@e") 728 (interactive "@e")
684 (set-buffer (window-buffer (posn-window (event-start event)))) 729 (set-buffer (window-buffer (posn-window (event-start event))))
685 (let ((pos (posn-point (event-start event)))) 730 (let ((pos (posn-point (event-start event))))
686 (let ((data (get-text-property pos 'cpp-data)) 731 (let ((cpp-callback-data (get-text-property pos 'cpp-data))
687 (fun (get-text-property pos 'cpp-callback)) 732 (fun (get-text-property pos 'cpp-callback))
688 (cpp-button-event event)) 733 (cpp-button-event event))
689 (cond (fun 734 (cond (fun
690 (call-interactively (get-text-property pos 'cpp-callback))) 735 (call-interactively (get-text-property pos 'cpp-callback)))
691 ((lookup-key global-map [ down-mouse-2]) 736 ((lookup-key global-map [ down-mouse-2])
692 (call-interactively (lookup-key global-map [ down-mouse-2]))))))) 737 (call-interactively (lookup-key global-map [ down-mouse-2])))))))
693 738
694 ;;; Faces: 739 ;;; Faces:
695
696 (defvar cpp-face-light-name-list
697 '("light gray" "light blue" "light cyan" "light yellow" "light pink"
698 "pale green" "beige" "orange" "magenta" "violet" "medium purple"
699 "turquoise")
700 "Background colours useful with dark foreground colors.")
701
702 (defvar cpp-face-dark-name-list
703 '("dim gray" "blue" "cyan" "yellow" "red"
704 "dark green" "brown" "dark orange" "dark khaki" "dark violet" "purple"
705 "dark turquoise")
706 "Background colours useful with light foreground colors.")
707
708 (defvar cpp-face-light-list nil
709 "Alist of names and faces to be used for light backgrounds.")
710
711 (defvar cpp-face-dark-list nil
712 "Alist of names and faces to be used for dark backgrounds.")
713
714 (defvar cpp-face-mono-list
715 '(("bold" . 'bold)
716 ("bold-italic" . 'bold-italic)
717 ("italic" . 'italic)
718 ("underline" . 'underline))
719 "Alist of names and faces to be used for monocrome screens.")
720
721 (defvar cpp-face-none-list
722 '(("default" . default)
723 ("invisible" . invisible))
724 "Alist of names and faces available even if you don't use a window system.")
725
726 (defvar cpp-face-all-list
727 (append cpp-face-light-list
728 cpp-face-dark-list
729 cpp-face-mono-list
730 cpp-face-none-list)
731 "All faces used for highligting text inside cpp conditionals.")
732
733 (defvar cpp-face-default-list nil
734 "List of faces you can choose from for cpp conditionals.")
735 740
736 (defun cpp-create-bg-face (color) 741 (defun cpp-create-bg-face (color)
737 ;; Create entry for face with background COLOR. 742 ;; Create entry for face with background COLOR.
738 (let ((name (intern (concat "cpp " color)))) 743 (let ((name (intern (concat "cpp " color))))
739 (make-face name) 744 (make-face name)