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