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