Mercurial > emacs
annotate lisp/progmodes/cpp.el @ 18799:6c0d648d2cf8
(describe-current-coding-system): Use coding-category-iso-7-else
instead of coding-category-iso-else.
author | Kenichi Handa <handa@m17n.org> |
---|---|
date | Tue, 15 Jul 1997 08:24:47 +0000 |
parents | bcdf720abb1a |
children | 1493fc19f324 |
rev | line source |
---|---|
8735 | 1 ;;; cpp.el --- Highlight or hide text according to cpp conditionals. |
2 | |
11480
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
3 ;; Copyright (C) 1994, 1995 Free Software Foundation |
8735 | 4 |
17981 | 5 ;; Author: Per Abrahamsen <abraham@dina.kvl.dk> |
8735 | 6 ;; Keywords: c, faces, tools |
7 | |
8736
fe48762e68de
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
8735
diff
changeset
|
8 ;; This file is part of GNU Emacs. |
8735 | 9 |
8736
fe48762e68de
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
8735
diff
changeset
|
10 ;; GNU Emacs is free software; you can redistribute it and/or modify |
8735 | 11 ;; it under the terms of the GNU General Public License as published by |
12 ;; the Free Software Foundation; either version 2, or (at your option) | |
13 ;; any later version. | |
8736
fe48762e68de
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
8735
diff
changeset
|
14 |
fe48762e68de
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
8735
diff
changeset
|
15 ;; GNU Emacs is distributed in the hope that it will be useful, |
8735 | 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. | |
8736
fe48762e68de
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
8735
diff
changeset
|
19 |
8735 | 20 ;; You should have received a copy of the GNU General Public License |
14169 | 21 ;; along with GNU Emacs; see the file COPYING. If not, write to the |
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
23 ;; Boston, MA 02111-1307, USA. | |
8735 | 24 |
8736
fe48762e68de
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
8735
diff
changeset
|
25 ;;; Commentary: |
8735 | 26 |
27 ;; Parse a text for C preprocessor conditionals, and highlight or hide | |
28 ;; the text inside the conditionals as you wish. | |
29 | |
8740
714588372e06
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
8736
diff
changeset
|
30 ;; This package is inspired by Jim Coplien's delta editor for SCCS. |
8735 | 31 |
32 ;;; Todo: | |
33 | |
34 ;; Should parse "#if" and "#elif" expressions and merge the faces | |
35 ;; somehow. | |
36 | |
37 ;; Somehow it is sometimes possible to make changes near a read only | |
38 ;; area which you can't undo. Their are other strange effects in that | |
39 ;; area. | |
40 | |
41 ;; The Edit buffer should -- optionally -- appear in its own frame. | |
42 | |
43 ;; Conditionals seem to be rear-sticky. They shouldn't be. | |
44 | |
45 ;; Restore window configurations when exiting CPP Edit buffer. | |
46 | |
47 ;;; Code: | |
48 | |
49 ;;; Customization: | |
50 | |
13911
3e9e8b468bc1
(cpp-config-file): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
11492
diff
changeset
|
51 (defvar cpp-config-file (convert-standard-filename ".cpp.el") |
3e9e8b468bc1
(cpp-config-file): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
11492
diff
changeset
|
52 "*File name to save cpp configuration.") |
3e9e8b468bc1
(cpp-config-file): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
11492
diff
changeset
|
53 |
8735 | 54 (defvar cpp-known-face 'invisible |
55 "*Face used for known cpp symbols.") | |
56 | |
57 (defvar cpp-unknown-face 'highlight | |
13974
37cfc82fe02d
(cpp-unknown-face, cpp-face-mono-list, cpp-face-all-list):
Karl Heuer <kwzh@gnu.org>
parents:
13911
diff
changeset
|
58 "*Face used for unknown cpp symbols.") |
8735 | 59 |
60 (defvar cpp-face-type 'light | |
61 "*Indicate what background face type you prefer. | |
62 Can be either light or dark for color screens, mono for monochrome | |
63 screens, and none if you don't use a window system.") | |
64 | |
65 (defvar cpp-known-writable t | |
66 "*Non-nil means you are allowed to modify the known conditionals.") | |
67 | |
68 (defvar cpp-unknown-writable t | |
69 "*Non-nil means you are allowed to modify the unknown conditionals.") | |
70 | |
11480
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
71 (defvar cpp-edit-list nil |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
72 "Alist of cpp macros and information about how they should be displayed. |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
73 Each entry is a list with the following elements: |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
74 0. The name of the macro (a string). |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
75 1. Face used for text that is `ifdef' the macro. |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
76 2. Face used for text that is `ifndef' the macro. |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
77 3. `t', `nil', or `both' depending on what text may be edited.") |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
78 |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
79 (defvar cpp-overlay-list nil) |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
80 ;; List of cpp overlays active in the current buffer. |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
81 (make-variable-buffer-local 'cpp-overlay-list) |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
82 |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
83 (defvar cpp-callback-data) |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
84 (defvar cpp-state-stack) |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
85 |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
86 (defconst cpp-face-type-list |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
87 '(("light color background" . light) |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
88 ("dark color background" . dark) |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
89 ("monochrome" . mono) |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
90 ("tty" . none)) |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
91 "Alist of strings and names of the defined face collections.") |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
92 |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
93 (defconst cpp-writable-list |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
94 ;; Names used for the writable property. |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
95 '(("writable" . t) |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
96 ("read-only" . nil))) |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
97 |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
98 (defvar cpp-button-event nil) |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
99 ;; This will be t in the callback for `cpp-make-button'. |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
100 |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
101 (defvar cpp-edit-buffer nil) |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
102 ;; Real buffer whose cpp display information we are editing. |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
103 (make-variable-buffer-local 'cpp-edit-buffer) |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
104 |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
105 (defconst cpp-branch-list |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
106 ;; Alist of branches. |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
107 '(("false" . nil) |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
108 ("true" . t) |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
109 ("both" . both))) |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
110 |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
111 (defvar cpp-face-default-list nil |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
112 "List of faces you can choose from for cpp conditionals.") |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
113 |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
114 (defvar cpp-face-light-name-list |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
115 '("light gray" "light blue" "light cyan" "light yellow" "light pink" |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
116 "pale green" "beige" "orange" "magenta" "violet" "medium purple" |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
117 "turquoise") |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
118 "Background colours useful with dark foreground colors.") |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
119 |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
120 (defvar cpp-face-dark-name-list |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
121 '("dim gray" "blue" "cyan" "yellow" "red" |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
122 "dark green" "brown" "dark orange" "dark khaki" "dark violet" "purple" |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
123 "dark turquoise") |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
124 "Background colours useful with light foreground colors.") |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
125 |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
126 (defvar cpp-face-light-list nil |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
127 "Alist of names and faces to be used for light backgrounds.") |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
128 |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
129 (defvar cpp-face-dark-list nil |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
130 "Alist of names and faces to be used for dark backgrounds.") |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
131 |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
132 (defvar cpp-face-mono-list |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
133 '(("bold" . 'bold) |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
134 ("bold-italic" . 'bold-italic) |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
135 ("italic" . 'italic) |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
136 ("underline" . 'underline)) |
13974
37cfc82fe02d
(cpp-unknown-face, cpp-face-mono-list, cpp-face-all-list):
Karl Heuer <kwzh@gnu.org>
parents:
13911
diff
changeset
|
137 "Alist of names and faces to be used for monochrome screens.") |
11480
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
138 |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
139 (defvar cpp-face-none-list |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
140 '(("default" . default) |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
141 ("invisible" . invisible)) |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
142 "Alist of names and faces available even if you don't use a window system.") |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
143 |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
144 (defvar cpp-face-all-list |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
145 (append cpp-face-light-list |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
146 cpp-face-dark-list |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
147 cpp-face-mono-list |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
148 cpp-face-none-list) |
13974
37cfc82fe02d
(cpp-unknown-face, cpp-face-mono-list, cpp-face-all-list):
Karl Heuer <kwzh@gnu.org>
parents:
13911
diff
changeset
|
149 "All faces used for highlighting text inside cpp conditionals.") |
11480
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
150 |
8735 | 151 ;;; Parse Buffer: |
152 | |
153 (defvar cpp-parse-symbols nil | |
154 "List of cpp macros used in the local buffer.") | |
155 (make-variable-buffer-local 'cpp-parse-symbols) | |
156 | |
157 (defconst cpp-parse-regexp | |
158 ;; Regexp matching all tokens needed to find conditionals. | |
159 (concat | |
160 "'\\|\"\\|/\\*\\|//\\|" | |
161 "\\(^[ \t]*#[ \t]*\\(ifdef\\|ifndef\\|if\\|" | |
162 "elif\\|else\\|endif\\)\\b\\)")) | |
163 | |
164 ;;;###autoload | |
8741 | 165 (defun cpp-highlight-buffer (arg) |
166 "Highlight C code according to preprocessor conditionals. | |
167 This command pops up a buffer which you should edit to specify | |
168 what kind of highlighting to use, and the criteria for highlighting. | |
11456
0950bf9c8d06
(cpp-parse-open): Delete comments that go past end of line.
Richard M. Stallman <rms@gnu.org>
parents:
8741
diff
changeset
|
169 A prefix arg suppresses display of that buffer." |
8735 | 170 (interactive "P") |
18071
bcdf720abb1a
(cpp-highlight-buffer): Make sure
Richard M. Stallman <rms@gnu.org>
parents:
17981
diff
changeset
|
171 (unless (memq 'cpp buffer-invisibility-spec) |
bcdf720abb1a
(cpp-highlight-buffer): Make sure
Richard M. Stallman <rms@gnu.org>
parents:
17981
diff
changeset
|
172 (add-to-invisibility-spec 'cpp)) |
8735 | 173 (setq cpp-parse-symbols nil) |
174 (cpp-parse-reset) | |
175 (if (null cpp-edit-list) | |
176 (cpp-edit-load)) | |
11480
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
177 (let (cpp-state-stack) |
8735 | 178 (save-excursion |
179 (goto-char (point-min)) | |
180 (cpp-progress-message "Parsing...") | |
181 (while (re-search-forward cpp-parse-regexp nil t) | |
182 (cpp-progress-message "Parsing...%d%%" | |
183 (/ (* 100 (- (point) (point-min))) (buffer-size))) | |
184 (let ((match (buffer-substring (match-beginning 0) (match-end 0)))) | |
185 (cond ((or (string-equal match "'") | |
186 (string-equal match "\"")) | |
187 (goto-char (match-beginning 0)) | |
188 (condition-case nil | |
189 (forward-sexp) | |
190 (error (cpp-parse-error | |
191 "Unterminated string or character")))) | |
192 ((string-equal match "/*") | |
193 (or (search-forward "*/" nil t) | |
194 (error "Unterminated comment"))) | |
195 ((string-equal match "//") | |
196 (skip-chars-forward "^\n\r")) | |
197 (t | |
198 (end-of-line 1) | |
199 (let ((from (match-beginning 1)) | |
200 (to (1+ (point))) | |
201 (type (buffer-substring (match-beginning 2) | |
202 (match-end 2))) | |
203 (expr (buffer-substring (match-end 1) (point)))) | |
204 (cond ((string-equal type "ifdef") | |
205 (cpp-parse-open t expr from to)) | |
206 ((string-equal type "ifndef") | |
207 (cpp-parse-open nil expr from to)) | |
208 ((string-equal type "if") | |
209 (cpp-parse-open t expr from to)) | |
210 ((string-equal type "elif") | |
211 (let (cpp-known-face cpp-unknown-face) | |
212 (cpp-parse-close from to)) | |
213 (cpp-parse-open t expr from to)) | |
214 ((string-equal type "else") | |
11480
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
215 (or cpp-state-stack |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
216 (cpp-parse-error "Top level #else")) |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
217 (let ((entry (list (not (nth 0 (car cpp-state-stack))) |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
218 (nth 1 (car cpp-state-stack)) |
8735 | 219 from to))) |
220 (cpp-parse-close from to) | |
11480
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
221 (setq cpp-state-stack (cons entry cpp-state-stack)))) |
8735 | 222 ((string-equal type "endif") |
223 (cpp-parse-close from to)) | |
224 (t | |
225 (cpp-parse-error "Parser error")))))))) | |
226 (message "Parsing...done")) | |
11480
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
227 (if cpp-state-stack |
8735 | 228 (save-excursion |
11480
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
229 (goto-char (nth 3 (car cpp-state-stack))) |
8735 | 230 (cpp-parse-error "Unclosed conditional")))) |
231 (or arg | |
232 (null cpp-parse-symbols) | |
233 (cpp-parse-edit))) | |
234 | |
235 (defun cpp-parse-open (branch expr begin end) | |
11480
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
236 "Push information about conditional-beginning onto `cpp-state-stack'." |
11456
0950bf9c8d06
(cpp-parse-open): Delete comments that go past end of line.
Richard M. Stallman <rms@gnu.org>
parents:
8741
diff
changeset
|
237 ;; Discard comments within this line. |
8735 | 238 (while (string-match "\\b[ \t]*/\\*.*\\*/[ \t]*\\b" expr) |
239 (setq expr (concat (substring expr 0 (match-beginning 0)) | |
240 (substring expr (match-end 0))))) | |
11456
0950bf9c8d06
(cpp-parse-open): Delete comments that go past end of line.
Richard M. Stallman <rms@gnu.org>
parents:
8741
diff
changeset
|
241 ;; If a comment starts on this line and continues past, discard it. |
0950bf9c8d06
(cpp-parse-open): Delete comments that go past end of line.
Richard M. Stallman <rms@gnu.org>
parents:
8741
diff
changeset
|
242 (if (string-match "\\b[ \t]*/\\*" expr) |
0950bf9c8d06
(cpp-parse-open): Delete comments that go past end of line.
Richard M. Stallman <rms@gnu.org>
parents:
8741
diff
changeset
|
243 (setq expr (substring expr 0 (match-beginning 0)))) |
0950bf9c8d06
(cpp-parse-open): Delete comments that go past end of line.
Richard M. Stallman <rms@gnu.org>
parents:
8741
diff
changeset
|
244 ;; Delete any C++ comment from the line. |
8735 | 245 (if (string-match "\\b[ \t]*\\(//.*\\)?$" expr) |
246 (setq expr (substring expr 0 (match-beginning 0)))) | |
247 (while (string-match "[ \t]+" expr) | |
248 (setq expr (concat (substring expr 0 (match-beginning 0)) | |
249 (substring expr (match-end 0))))) | |
11480
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
250 (setq cpp-state-stack (cons (list branch expr begin end) cpp-state-stack)) |
8735 | 251 (or (member expr cpp-parse-symbols) |
252 (setq cpp-parse-symbols | |
253 (cons expr cpp-parse-symbols))) | |
254 (if (assoc expr cpp-edit-list) | |
255 (cpp-make-known-overlay begin end) | |
256 (cpp-make-unknown-overlay begin end))) | |
257 | |
258 (defun cpp-parse-close (from to) | |
11480
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
259 ;; Pop top of cpp-state-stack and create overlay. |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
260 (let ((entry (assoc (nth 1 (car cpp-state-stack)) cpp-edit-list)) |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
261 (branch (nth 0 (car cpp-state-stack))) |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
262 (begin (nth 2 (car cpp-state-stack))) |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
263 (end (nth 3 (car cpp-state-stack)))) |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
264 (setq cpp-state-stack (cdr cpp-state-stack)) |
8735 | 265 (if entry |
266 (let ((face (nth (if branch 1 2) entry)) | |
267 (read-only (eq (not branch) (nth 3 entry))) | |
11480
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
268 (priority (length cpp-state-stack)) |
8735 | 269 (overlay (make-overlay end from))) |
270 (cpp-make-known-overlay from to) | |
271 (setq cpp-overlay-list (cons overlay cpp-overlay-list)) | |
272 (if priority (overlay-put overlay 'priority priority)) | |
273 (cond ((eq face 'invisible) | |
274 (cpp-make-overlay-hidden overlay)) | |
275 ((eq face 'default)) | |
276 (t | |
277 (overlay-put overlay 'face face))) | |
278 (if read-only | |
279 (cpp-make-overlay-read-only overlay) | |
280 (cpp-make-overlay-sticky overlay))) | |
281 (cpp-make-unknown-overlay from to)))) | |
282 | |
283 (defun cpp-parse-error (error) | |
284 ;; Error message issued by the cpp parser. | |
14417
2b2e0cef30d5
(cpp-parse-error): Fix error format string.
Karl Heuer <kwzh@gnu.org>
parents:
14169
diff
changeset
|
285 (error "%s at line %d" error (count-lines (point-min) (point)))) |
8735 | 286 |
287 (defun cpp-parse-reset () | |
288 "Reset display of cpp conditionals to normal." | |
289 (interactive) | |
290 (while cpp-overlay-list | |
291 (delete-overlay (car cpp-overlay-list)) | |
292 (setq cpp-overlay-list (cdr cpp-overlay-list)))) | |
293 | |
294 ;;;###autoload | |
295 (defun cpp-parse-edit () | |
296 "Edit display information for cpp conditionals." | |
297 (interactive) | |
298 (or cpp-parse-symbols | |
11456
0950bf9c8d06
(cpp-parse-open): Delete comments that go past end of line.
Richard M. Stallman <rms@gnu.org>
parents:
8741
diff
changeset
|
299 (cpp-highlight-buffer t)) |
8735 | 300 (let ((buffer (current-buffer))) |
301 (pop-to-buffer "*CPP Edit*") | |
302 (cpp-edit-mode) | |
303 (setq cpp-edit-buffer buffer) | |
304 (cpp-edit-reset))) | |
305 | |
306 ;;; Overlays: | |
307 | |
308 (defun cpp-make-known-overlay (start end) | |
309 ;; Create an overlay for a known cpp command from START to END. | |
310 (let ((overlay (make-overlay start end))) | |
311 (if (eq cpp-known-face 'invisible) | |
312 (cpp-make-overlay-hidden overlay) | |
313 (or (eq cpp-known-face 'default) | |
314 (overlay-put overlay 'face cpp-known-face)) | |
315 (if cpp-known-writable | |
316 () | |
317 (overlay-put overlay 'modification-hooks '(cpp-signal-read-only)) | |
318 (overlay-put overlay 'insert-in-front-hooks '(cpp-signal-read-only)))) | |
319 (setq cpp-overlay-list (cons overlay cpp-overlay-list)))) | |
320 | |
321 (defun cpp-make-unknown-overlay (start end) | |
322 ;; Create an overlay for an unknown cpp command from START to END. | |
323 (let ((overlay (make-overlay start end))) | |
324 (cond ((eq cpp-unknown-face 'invisible) | |
325 (cpp-make-overlay-hidden overlay)) | |
326 ((eq cpp-unknown-face 'default)) | |
327 (t | |
328 (overlay-put overlay 'face cpp-unknown-face))) | |
329 (if cpp-unknown-writable | |
330 () | |
331 (overlay-put overlay 'modification-hooks '(cpp-signal-read-only)) | |
332 (overlay-put overlay 'insert-in-front-hooks '(cpp-signal-read-only))) | |
333 (setq cpp-overlay-list (cons overlay cpp-overlay-list)))) | |
334 | |
335 (defun cpp-make-overlay-hidden (overlay) | |
336 ;; Make overlay hidden and intangible. | |
18071
bcdf720abb1a
(cpp-highlight-buffer): Make sure
Richard M. Stallman <rms@gnu.org>
parents:
17981
diff
changeset
|
337 (overlay-put overlay 'invisible 'cpp) |
8735 | 338 (overlay-put overlay 'intangible t) |
339 ;; Unfortunately `intangible' is not implemented for overlays yet, | |
340 ;; so we make is read-only instead. | |
11492
2e09c796bf70
(cpp-edit-reset): Fix typo.
Richard M. Stallman <rms@gnu.org>
parents:
11480
diff
changeset
|
341 (overlay-put overlay 'modification-hooks '(cpp-signal-read-only)) |
2e09c796bf70
(cpp-edit-reset): Fix typo.
Richard M. Stallman <rms@gnu.org>
parents:
11480
diff
changeset
|
342 (overlay-put overlay 'insert-in-front-hooks '(cpp-signal-read-only))) |
8735 | 343 |
344 (defun cpp-make-overlay-read-only (overlay) | |
345 ;; Make overlay read only. | |
346 (overlay-put overlay 'modification-hooks '(cpp-signal-read-only)) | |
347 (overlay-put overlay 'insert-in-front-hooks '(cpp-signal-read-only)) | |
348 (overlay-put overlay 'insert-behind-hooks '(cpp-signal-read-only))) | |
349 | |
350 (defun cpp-make-overlay-sticky (overlay) | |
351 ;; Make OVERLAY grow when you insert text at either end. | |
352 (overlay-put overlay 'insert-in-front-hooks '(cpp-grow-overlay)) | |
353 (overlay-put overlay 'insert-behind-hooks '(cpp-grow-overlay))) | |
354 | |
11492
2e09c796bf70
(cpp-edit-reset): Fix typo.
Richard M. Stallman <rms@gnu.org>
parents:
11480
diff
changeset
|
355 (defun cpp-signal-read-only (overlay after start end &optional len) |
8735 | 356 ;; Only allow deleting the whole overlay. |
357 ;; Trying to change a read-only overlay. | |
11492
2e09c796bf70
(cpp-edit-reset): Fix typo.
Richard M. Stallman <rms@gnu.org>
parents:
11480
diff
changeset
|
358 (if (and (not after) |
2e09c796bf70
(cpp-edit-reset): Fix typo.
Richard M. Stallman <rms@gnu.org>
parents:
11480
diff
changeset
|
359 (or (< (overlay-start overlay) start) |
2e09c796bf70
(cpp-edit-reset): Fix typo.
Richard M. Stallman <rms@gnu.org>
parents:
11480
diff
changeset
|
360 (> (overlay-end overlay) end))) |
8735 | 361 (error "This text is read only"))) |
362 | |
11492
2e09c796bf70
(cpp-edit-reset): Fix typo.
Richard M. Stallman <rms@gnu.org>
parents:
11480
diff
changeset
|
363 (defun cpp-grow-overlay (overlay after start end &optional len) |
8735 | 364 ;; Make OVERLAY grow to contain range START to END. |
11492
2e09c796bf70
(cpp-edit-reset): Fix typo.
Richard M. Stallman <rms@gnu.org>
parents:
11480
diff
changeset
|
365 (if after |
2e09c796bf70
(cpp-edit-reset): Fix typo.
Richard M. Stallman <rms@gnu.org>
parents:
11480
diff
changeset
|
366 (move-overlay overlay |
2e09c796bf70
(cpp-edit-reset): Fix typo.
Richard M. Stallman <rms@gnu.org>
parents:
11480
diff
changeset
|
367 (min start (overlay-start overlay)) |
2e09c796bf70
(cpp-edit-reset): Fix typo.
Richard M. Stallman <rms@gnu.org>
parents:
11480
diff
changeset
|
368 (max end (overlay-end overlay))))) |
8735 | 369 |
370 ;;; Edit Buffer: | |
371 | |
372 (defvar cpp-edit-map nil) | |
373 ;; Keymap for `cpp-edit-mode'. | |
374 | |
375 (if cpp-edit-map | |
376 () | |
377 (setq cpp-edit-map (make-keymap)) | |
378 (suppress-keymap cpp-edit-map) | |
379 (define-key cpp-edit-map [ down-mouse-2 ] 'cpp-push-button) | |
380 (define-key cpp-edit-map [ mouse-2 ] 'ignore) | |
381 (define-key cpp-edit-map " " 'scroll-up) | |
382 (define-key cpp-edit-map "\C-?" 'scroll-down) | |
383 (define-key cpp-edit-map [ delete ] 'scroll-down) | |
384 (define-key cpp-edit-map "\C-c\C-c" 'cpp-edit-apply) | |
385 (define-key cpp-edit-map "a" 'cpp-edit-apply) | |
386 (define-key cpp-edit-map "A" 'cpp-edit-apply) | |
387 (define-key cpp-edit-map "r" 'cpp-edit-reset) | |
388 (define-key cpp-edit-map "R" 'cpp-edit-reset) | |
389 (define-key cpp-edit-map "s" 'cpp-edit-save) | |
390 (define-key cpp-edit-map "S" 'cpp-edit-save) | |
391 (define-key cpp-edit-map "l" 'cpp-edit-load) | |
392 (define-key cpp-edit-map "L" 'cpp-edit-load) | |
393 (define-key cpp-edit-map "h" 'cpp-edit-home) | |
394 (define-key cpp-edit-map "H" 'cpp-edit-home) | |
395 (define-key cpp-edit-map "b" 'cpp-edit-background) | |
396 (define-key cpp-edit-map "B" 'cpp-edit-background) | |
397 (define-key cpp-edit-map "k" 'cpp-edit-known) | |
398 (define-key cpp-edit-map "K" 'cpp-edit-known) | |
399 (define-key cpp-edit-map "u" 'cpp-edit-unknown) | |
400 (define-key cpp-edit-map "u" 'cpp-edit-unknown) | |
401 (define-key cpp-edit-map "t" 'cpp-edit-true) | |
402 (define-key cpp-edit-map "T" 'cpp-edit-true) | |
403 (define-key cpp-edit-map "f" 'cpp-edit-false) | |
404 (define-key cpp-edit-map "F" 'cpp-edit-false) | |
405 (define-key cpp-edit-map "w" 'cpp-edit-write) | |
406 (define-key cpp-edit-map "W" 'cpp-edit-write) | |
407 (define-key cpp-edit-map "X" 'cpp-edit-toggle-known) | |
408 (define-key cpp-edit-map "x" 'cpp-edit-toggle-known) | |
409 (define-key cpp-edit-map "Y" 'cpp-edit-toggle-unknown) | |
410 (define-key cpp-edit-map "y" 'cpp-edit-toggle-unknown) | |
411 (define-key cpp-edit-map "q" 'bury-buffer) | |
412 (define-key cpp-edit-map "Q" 'bury-buffer)) | |
413 | |
414 (defvar cpp-edit-symbols nil) | |
415 ;; Symbols defined in the edit buffer. | |
416 (make-variable-buffer-local 'cpp-edit-symbols) | |
417 | |
418 (defun cpp-edit-mode () | |
8741 | 419 "Major mode for editing the criteria for highlighting cpp conditionals. |
8735 | 420 Click on objects to change them. |
421 You can also use the keyboard accelerators indicated like this: [K]ey." | |
422 (kill-all-local-variables) | |
423 (buffer-disable-undo) | |
424 (auto-save-mode -1) | |
425 (setq buffer-read-only t) | |
426 (setq major-mode 'cpp-edit-mode) | |
427 (setq mode-name "CPP Edit") | |
428 (use-local-map cpp-edit-map)) | |
429 | |
430 (defun cpp-edit-apply () | |
431 "Apply edited display information to original buffer." | |
432 (interactive) | |
433 (cpp-edit-home) | |
11456
0950bf9c8d06
(cpp-parse-open): Delete comments that go past end of line.
Richard M. Stallman <rms@gnu.org>
parents:
8741
diff
changeset
|
434 (cpp-highlight-buffer t)) |
8735 | 435 |
436 (defun cpp-edit-reset () | |
437 "Reset display information from original buffer." | |
438 (interactive) | |
439 (let ((buffer (current-buffer)) | |
440 (buffer-read-only nil) | |
441 (start (window-start)) | |
442 (pos (point)) | |
443 symbols) | |
444 (set-buffer cpp-edit-buffer) | |
445 (setq symbols cpp-parse-symbols) | |
446 (set-buffer buffer) | |
447 (setq cpp-edit-symbols symbols) | |
448 (erase-buffer) | |
449 (insert "CPP Display Information for `") | |
450 (cpp-make-button (buffer-name cpp-edit-buffer) 'cpp-edit-home) | |
451 (insert "\n\nClick mouse-2 on item you want to change or use\n" | |
11456
0950bf9c8d06
(cpp-parse-open): Delete comments that go past end of line.
Richard M. Stallman <rms@gnu.org>
parents:
8741
diff
changeset
|
452 "or switch to this buffer and type the keyboard equivalents.\n" |
0950bf9c8d06
(cpp-parse-open): Delete comments that go past end of line.
Richard M. Stallman <rms@gnu.org>
parents:
8741
diff
changeset
|
453 "Keyboard equivalents are indicated with brackets like [T]his.\n\n") |
0950bf9c8d06
(cpp-parse-open): Delete comments that go past end of line.
Richard M. Stallman <rms@gnu.org>
parents:
8741
diff
changeset
|
454 (cpp-make-button "[H]ome (display the C file)" 'cpp-edit-home) |
0950bf9c8d06
(cpp-parse-open): Delete comments that go past end of line.
Richard M. Stallman <rms@gnu.org>
parents:
8741
diff
changeset
|
455 (insert " ") |
0950bf9c8d06
(cpp-parse-open): Delete comments that go past end of line.
Richard M. Stallman <rms@gnu.org>
parents:
8741
diff
changeset
|
456 (cpp-make-button "[A]pply new settings" 'cpp-edit-apply) |
0950bf9c8d06
(cpp-parse-open): Delete comments that go past end of line.
Richard M. Stallman <rms@gnu.org>
parents:
8741
diff
changeset
|
457 (insert "\n") |
0950bf9c8d06
(cpp-parse-open): Delete comments that go past end of line.
Richard M. Stallman <rms@gnu.org>
parents:
8741
diff
changeset
|
458 (cpp-make-button "[S]ave settings" 'cpp-edit-save) |
0950bf9c8d06
(cpp-parse-open): Delete comments that go past end of line.
Richard M. Stallman <rms@gnu.org>
parents:
8741
diff
changeset
|
459 (insert " ") |
0950bf9c8d06
(cpp-parse-open): Delete comments that go past end of line.
Richard M. Stallman <rms@gnu.org>
parents:
8741
diff
changeset
|
460 (cpp-make-button "[L]oad settings" 'cpp-edit-load) |
0950bf9c8d06
(cpp-parse-open): Delete comments that go past end of line.
Richard M. Stallman <rms@gnu.org>
parents:
8741
diff
changeset
|
461 (insert "\n\n") |
0950bf9c8d06
(cpp-parse-open): Delete comments that go past end of line.
Richard M. Stallman <rms@gnu.org>
parents:
8741
diff
changeset
|
462 |
8735 | 463 (insert "[B]ackground: ") |
464 (cpp-make-button (car (rassq cpp-face-type cpp-face-type-list)) | |
465 'cpp-edit-background) | |
466 (insert "\n[K]nown conditionals: ") | |
467 (cpp-make-button (cpp-face-name cpp-known-face) | |
468 'cpp-edit-known nil t) | |
469 (insert " [X] ") | |
470 (cpp-make-button (car (rassq cpp-known-writable cpp-writable-list)) | |
471 'cpp-edit-toggle-known) | |
472 (insert "\n[U]nknown conditionals: ") | |
473 (cpp-make-button (cpp-face-name cpp-unknown-face) | |
474 'cpp-edit-unknown nil t) | |
475 (insert " [Y] ") | |
476 (cpp-make-button (car (rassq cpp-unknown-writable cpp-writable-list)) | |
477 'cpp-edit-toggle-unknown) | |
478 (insert (format "\n\n\n%39s: %14s %14s %7s\n\n" "Expression" | |
479 "[T]rue Face" "[F]alse Face" "[W]rite")) | |
480 (while symbols | |
481 (let* ((symbol (car symbols)) | |
482 (entry (assoc symbol cpp-edit-list)) | |
483 (true (nth 1 entry)) | |
484 (false (nth 2 entry)) | |
485 (write (if entry (nth 3 entry) 'both))) | |
486 (setq symbols (cdr symbols)) | |
487 | |
488 (if (and entry ; Make default entries unknown. | |
489 (or (null true) (eq true 'default)) | |
490 (or (null false) (eq false 'default)) | |
491 (eq write 'both)) | |
492 (setq cpp-edit-list (delq entry cpp-edit-list) | |
493 entry nil)) | |
494 | |
11492
2e09c796bf70
(cpp-edit-reset): Fix typo.
Richard M. Stallman <rms@gnu.org>
parents:
11480
diff
changeset
|
495 (if (> (length symbol) 39) |
8735 | 496 (insert (substring symbol 0 39) ": ") |
497 (insert (format "%39s: " symbol))) | |
498 | |
499 (cpp-make-button (cpp-face-name true) | |
500 'cpp-edit-true symbol t 14) | |
501 (insert " ") | |
502 (cpp-make-button (cpp-face-name false) | |
503 'cpp-edit-false symbol t 14) | |
504 (insert " ") | |
505 (cpp-make-button (car (rassq write cpp-branch-list)) | |
506 'cpp-edit-write symbol nil 6) | |
507 (insert "\n"))) | |
508 (insert "\n\n") | |
509 (set-window-start nil start) | |
510 (goto-char pos))) | |
511 | |
512 (defun cpp-edit-load () | |
513 "Load cpp configuration." | |
514 (interactive) | |
16681
58b38425b463
(cpp-edit-load): Don't load anything if init-file-user is nil.
Richard M. Stallman <rms@gnu.org>
parents:
14417
diff
changeset
|
515 (cond ((null init-file-user) |
58b38425b463
(cpp-edit-load): Don't load anything if init-file-user is nil.
Richard M. Stallman <rms@gnu.org>
parents:
14417
diff
changeset
|
516 ;; If -q was specified, don't load any init files. |
58b38425b463
(cpp-edit-load): Don't load anything if init-file-user is nil.
Richard M. Stallman <rms@gnu.org>
parents:
14417
diff
changeset
|
517 nil) |
58b38425b463
(cpp-edit-load): Don't load anything if init-file-user is nil.
Richard M. Stallman <rms@gnu.org>
parents:
14417
diff
changeset
|
518 ((file-readable-p cpp-config-file) |
13911
3e9e8b468bc1
(cpp-config-file): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
11492
diff
changeset
|
519 (load-file cpp-config-file)) |
3e9e8b468bc1
(cpp-config-file): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
11492
diff
changeset
|
520 ((file-readable-p (concat "~/" cpp-config-file)) |
3e9e8b468bc1
(cpp-config-file): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
11492
diff
changeset
|
521 (load-file cpp-config-file))) |
8740
714588372e06
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
8736
diff
changeset
|
522 (if (eq major-mode 'cpp-edit-mode) |
714588372e06
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
8736
diff
changeset
|
523 (cpp-edit-reset))) |
8735 | 524 |
525 (defun cpp-edit-save () | |
16681
58b38425b463
(cpp-edit-load): Don't load anything if init-file-user is nil.
Richard M. Stallman <rms@gnu.org>
parents:
14417
diff
changeset
|
526 "Save the current cpp configuration in a file." |
8735 | 527 (interactive) |
528 (require 'pp) | |
529 (save-excursion | |
530 (set-buffer cpp-edit-buffer) | |
13911
3e9e8b468bc1
(cpp-config-file): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
11492
diff
changeset
|
531 (let ((buffer (find-file-noselect cpp-config-file))) |
8735 | 532 (set-buffer buffer) |
533 (erase-buffer) | |
534 (pp (list 'setq 'cpp-known-face | |
535 (list 'quote cpp-known-face)) buffer) | |
536 (pp (list 'setq 'cpp-unknown-face | |
537 (list 'quote cpp-unknown-face)) buffer) | |
538 (pp (list 'setq 'cpp-face-type | |
539 (list 'quote cpp-face-type)) buffer) | |
540 (pp (list 'setq 'cpp-known-writable | |
541 (list 'quote cpp-known-writable)) buffer) | |
542 (pp (list 'setq 'cpp-unknown-writable | |
543 (list 'quote cpp-unknown-writable)) buffer) | |
544 (pp (list 'setq 'cpp-edit-list | |
545 (list 'quote cpp-edit-list)) buffer) | |
13911
3e9e8b468bc1
(cpp-config-file): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
11492
diff
changeset
|
546 (write-file cpp-config-file)))) |
8735 | 547 |
548 (defun cpp-edit-home () | |
549 "Switch back to original buffer." | |
550 (interactive) | |
551 (if cpp-button-event | |
552 (read-event)) | |
553 (pop-to-buffer cpp-edit-buffer)) | |
554 | |
555 (defun cpp-edit-background () | |
556 "Change default face collection." | |
557 (interactive) | |
558 (call-interactively 'cpp-choose-default-face) | |
559 (cpp-edit-reset)) | |
560 | |
561 (defun cpp-edit-known () | |
562 "Select default for known conditionals." | |
563 (interactive) | |
564 (setq cpp-known-face (cpp-choose-face "Known face" cpp-known-face)) | |
565 (cpp-edit-reset)) | |
566 | |
567 (defun cpp-edit-unknown () | |
568 "Select default for unknown conditionals." | |
569 (interactive) | |
570 (setq cpp-unknown-face (cpp-choose-face "Unknown face" cpp-unknown-face)) | |
571 (cpp-edit-reset)) | |
572 | |
573 (defun cpp-edit-toggle-known (arg) | |
574 "Toggle writable status for known conditionals. | |
575 With optional argument ARG, make them writable iff ARG is positive." | |
576 (interactive "@P") | |
577 (if (or (and (null arg) cpp-known-writable) | |
578 (<= (prefix-numeric-value arg) 0)) | |
579 (setq cpp-known-writable nil) | |
580 (setq cpp-known-writable t)) | |
581 (cpp-edit-reset)) | |
582 | |
583 (defun cpp-edit-toggle-unknown (arg) | |
584 "Toggle writable status for unknown conditionals. | |
585 With optional argument ARG, make them writable iff ARG is positive." | |
586 (interactive "@P") | |
587 (if (or (and (null arg) cpp-unknown-writable) | |
588 (<= (prefix-numeric-value arg) 0)) | |
589 (setq cpp-unknown-writable nil) | |
590 (setq cpp-unknown-writable t)) | |
591 (cpp-edit-reset)) | |
592 | |
593 (defun cpp-edit-true (symbol face) | |
594 "Select SYMBOL's true FACE used for highlighting taken conditionals." | |
595 (interactive | |
596 (let ((symbol (cpp-choose-symbol))) | |
597 (list symbol | |
598 (cpp-choose-face "True face" | |
599 (nth 1 (assoc symbol cpp-edit-list)))))) | |
600 (setcar (nthcdr 1 (cpp-edit-list-entry-get-or-create symbol)) face) | |
601 (cpp-edit-reset)) | |
602 | |
603 (defun cpp-edit-false (symbol face) | |
604 "Select SYMBOL's false FACE used for highlighting untaken conditionals." | |
605 (interactive | |
606 (let ((symbol (cpp-choose-symbol))) | |
607 (list symbol | |
608 (cpp-choose-face "False face" | |
609 (nth 2 (assoc symbol cpp-edit-list)))))) | |
610 (setcar (nthcdr 2 (cpp-edit-list-entry-get-or-create symbol)) face) | |
611 (cpp-edit-reset)) | |
612 | |
613 (defun cpp-edit-write (symbol branch) | |
614 "Set which branches of SYMBOL should be writable to BRANCH. | |
615 BRANCH should be either nil (false branch), t (true branch) or 'both." | |
616 (interactive (list (cpp-choose-symbol) (cpp-choose-branch))) | |
617 (setcar (nthcdr 3 (cpp-edit-list-entry-get-or-create symbol)) branch) | |
618 (cpp-edit-reset)) | |
619 | |
620 (defun cpp-edit-list-entry-get-or-create (symbol) | |
621 ;; Return the entry for SYMBOL in `cpp-edit-list'. | |
622 ;; If it does not exist, create it. | |
623 (let ((entry (assoc symbol cpp-edit-list))) | |
624 (or entry | |
625 (setq entry (list symbol nil nil 'both nil) | |
626 cpp-edit-list (cons entry cpp-edit-list))) | |
627 entry)) | |
628 | |
629 ;;; Prompts: | |
630 | |
631 (defun cpp-choose-symbol () | |
632 ;; Choose a symbol if called from keyboard, otherwise use the one clicked on. | |
633 (if cpp-button-event | |
11480
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
634 cpp-callback-data |
8735 | 635 (completing-read "Symbol: " (mapcar 'list cpp-edit-symbols) nil t))) |
636 | |
637 (defun cpp-choose-branch () | |
638 ;; Choose a branch, either nil, t, or both. | |
639 (if cpp-button-event | |
640 (x-popup-menu cpp-button-event | |
641 (list "Branch" (cons "Branch" cpp-branch-list))) | |
642 (cdr (assoc (completing-read "Branch: " cpp-branch-list nil t) | |
643 cpp-branch-list)))) | |
644 | |
645 (defun cpp-choose-face (prompt default) | |
646 ;; Choose a face from cpp-face-defalt-list. | |
647 ;; PROMPT is what to say to the user. | |
648 ;; DEFAULT is the default face. | |
649 (or (if cpp-button-event | |
650 (x-popup-menu cpp-button-event | |
651 (list prompt (cons prompt cpp-face-default-list))) | |
652 (let ((name (car (rassq default cpp-face-default-list)))) | |
653 (cdr (assoc (completing-read (if name | |
654 (concat prompt | |
655 " (default " name "): ") | |
656 (concat prompt ": ")) | |
657 cpp-face-default-list nil t) | |
658 cpp-face-all-list)))) | |
659 default)) | |
660 | |
661 (defun cpp-choose-default-face (type) | |
662 ;; Choose default face list for screen of TYPE. | |
663 ;; Type must be one of the types defined in `cpp-face-type-list'. | |
664 (interactive (list (if cpp-button-event | |
665 (x-popup-menu cpp-button-event | |
666 (list "Screen type" | |
667 (cons "Screen type" | |
668 cpp-face-type-list))) | |
669 (cdr (assoc (completing-read "Screen type: " | |
670 cpp-face-type-list | |
671 nil t) | |
672 cpp-face-type-list))))) | |
673 (cond ((null type)) | |
674 ((eq type 'light) | |
675 (if cpp-face-light-list | |
676 () | |
677 (setq cpp-face-light-list | |
678 (mapcar 'cpp-create-bg-face cpp-face-light-name-list)) | |
679 (setq cpp-face-all-list | |
680 (append cpp-face-all-list cpp-face-light-list))) | |
681 (setq cpp-face-type 'light) | |
682 (setq cpp-face-default-list | |
683 (append cpp-face-light-list cpp-face-none-list))) | |
684 ((eq type 'dark) | |
685 (if cpp-face-dark-list | |
686 () | |
687 (setq cpp-face-dark-list | |
688 (mapcar 'cpp-create-bg-face cpp-face-dark-name-list)) | |
689 (setq cpp-face-all-list | |
690 (append cpp-face-all-list cpp-face-dark-list))) | |
691 (setq cpp-face-type 'dark) | |
692 (setq cpp-face-default-list | |
693 (append cpp-face-dark-list cpp-face-none-list))) | |
694 ((eq type 'mono) | |
695 (setq cpp-face-type 'mono) | |
696 (setq cpp-face-default-list | |
697 (append cpp-face-mono-list cpp-face-none-list))) | |
698 (t | |
699 (setq cpp-face-type 'none) | |
700 (setq cpp-face-default-list cpp-face-none-list)))) | |
701 | |
702 ;;; Buttons: | |
703 | |
704 (defun cpp-make-button (name callback &optional data face padding) | |
705 ;; Create a button at point. | |
706 ;; NAME is the name of the button. | |
707 ;; CALLBACK is the function to call when the button is pushed. | |
11480
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
708 ;; DATA will be made available to CALLBACK |
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
709 ;;in the free variable cpp-callback-data. |
8735 | 710 ;; FACE means that NAME is the name of a face in `cpp-face-all-list'. |
711 ;; PADDING means NAME will be right justified at that length. | |
712 (let ((name (format "%s" name)) | |
713 from to) | |
714 (cond ((null padding) | |
715 (setq from (point)) | |
716 (insert name)) | |
717 ((> (length name) padding) | |
718 (setq from (point)) | |
719 (insert (substring name 0 padding))) | |
720 (t | |
721 (insert (make-string (- padding (length name)) ? )) | |
722 (setq from (point)) | |
723 (insert name))) | |
724 (setq to (point)) | |
725 (setq face | |
726 (if face | |
727 (let ((check (cdr (assoc name cpp-face-all-list)))) | |
728 (if (memq check '(default invisible)) | |
729 'bold | |
730 check)) | |
731 'bold)) | |
732 (add-text-properties from to | |
733 (append (list 'face face) | |
734 '(mouse-face highlight) | |
735 (list 'cpp-callback callback) | |
736 (if data (list 'cpp-data data)))))) | |
737 | |
738 (defun cpp-push-button (event) | |
739 ;; Pushed a CPP button. | |
740 (interactive "@e") | |
741 (set-buffer (window-buffer (posn-window (event-start event)))) | |
742 (let ((pos (posn-point (event-start event)))) | |
11480
5865f4bc9521
(cpp-edit-list): Move definition toward start of file.
Richard M. Stallman <rms@gnu.org>
parents:
11456
diff
changeset
|
743 (let ((cpp-callback-data (get-text-property pos 'cpp-data)) |
8735 | 744 (fun (get-text-property pos 'cpp-callback)) |
745 (cpp-button-event event)) | |
746 (cond (fun | |
747 (call-interactively (get-text-property pos 'cpp-callback))) | |
748 ((lookup-key global-map [ down-mouse-2]) | |
749 (call-interactively (lookup-key global-map [ down-mouse-2]))))))) | |
750 | |
751 ;;; Faces: | |
752 | |
753 (defun cpp-create-bg-face (color) | |
754 ;; Create entry for face with background COLOR. | |
755 (let ((name (intern (concat "cpp " color)))) | |
756 (make-face name) | |
757 (set-face-background name color) | |
758 (cons color name))) | |
759 | |
760 (cpp-choose-default-face (if window-system cpp-face-type 'none)) | |
761 | |
762 (defun cpp-face-name (face) | |
763 ;; Return the name of FACE from `cpp-face-all-list'. | |
764 (let ((entry (rassq (if face face 'default) cpp-face-all-list))) | |
765 (if entry | |
766 (car entry) | |
767 (format "<%s>" face)))) | |
768 | |
769 ;;; Utilities: | |
770 | |
771 (defvar cpp-progress-time 0) | |
772 ;; Last time we issued a progress message. | |
773 | |
774 (defun cpp-progress-message (&rest args) | |
775 ;; Report progress at most once a second. Take same ARGS as `message'. | |
776 (let ((time (nth 1 (current-time)))) | |
777 (if (= time cpp-progress-time) | |
778 () | |
779 (setq cpp-progress-time time) | |
780 (apply 'message args)))) | |
781 | |
782 (provide 'cpp) | |
783 | |
784 ;;; cpp.el ends here |