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