]> code.delx.au - gnu-emacs/blob - lisp/progmodes/hideif.el
Use frame-parameters, not font-lock-background-mode and font-lock-display-type.
[gnu-emacs] / lisp / progmodes / hideif.el
1 ;;; hide-ifdef-mode.el --- hides selected code within ifdef.
2
3 ;;; Copyright (C) 1988, 1994 Free Software Foundation, Inc.
4
5 ;; Author: Dan LaLiberte <liberte@a.cs.uiuc.edu>
6 ;; Maintainer: FSF
7 ;; Keywords: c, outlines
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to
23 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24
25 ;;; Commentary:
26
27 ;;; To initialize, toggle the hide-ifdef minor mode with
28 ;;;
29 ;;; M-x hide-ifdef-mode
30 ;;;
31 ;;; This will set up key bindings and call hide-ifdef-mode-hook if it
32 ;;; has a value. To explicitly hide ifdefs using a buffer-local
33 ;;; define list (default empty), type
34 ;;;
35 ;;; M-x hide-ifdefs or C-c @ h
36 ;;;
37 ;;; Hide-ifdef suppresses the display of code that the preprocessor wouldn't
38 ;;; pass through. The support of constant expressions in #if lines is
39 ;;; limited to identifiers, parens, and the operators: &&, ||, !, and
40 ;;; "defined". Please extend this.
41 ;;;
42 ;;; The hidden code is marked by ellipses (...). Be
43 ;;; cautious when editing near ellipses, since the hidden text is
44 ;;; still in the buffer, and you can move the point into it and modify
45 ;;; text unawares. If you don't want to see the ellipses, set
46 ;;; selective-display-ellipses to nil. But this can be dangerous.
47 ;;; You can make your buffer read-only while hide-ifdef-hiding by setting
48 ;;; hide-ifdef-read-only to a non-nil value. You can toggle this
49 ;;; variable with hide-ifdef-toggle-read-only (C-c @ C-q).
50 ;;;
51 ;;; You can undo the effect of hide-ifdefs by typing
52 ;;;
53 ;;; M-x show-ifdefs or C-c @ s
54 ;;;
55 ;;; Use M-x hide-ifdef-define (C-c @ d) to define a symbol.
56 ;;; Use M-x hide-ifdef-undef (C-c @ u) to undefine a symbol.
57 ;;;
58 ;;; If you define or undefine a symbol while hide-ifdef-mode is in effect,
59 ;;; the display will be updated. Only the define list for the current
60 ;;; buffer will be affected. You can save changes to the local define
61 ;;; list with hide-ifdef-set-define-alist. This adds entries
62 ;;; to hide-ifdef-define-alist.
63 ;;;
64 ;;; If you have defined a hide-ifdef-mode-hook, you can set
65 ;;; up a list of symbols that may be used by hide-ifdefs as in the
66 ;;; following example:
67 ;;;
68 ;;; (setq hide-ifdef-mode-hook
69 ;;; '(lambda ()
70 ;;; (if (not hide-ifdef-define-alist)
71 ;;; (setq hide-ifdef-define-alist
72 ;;; '((list1 ONE TWO)
73 ;;; (list2 TWO THREE)
74 ;;; )))
75 ;;; (hide-ifdef-use-define-alist 'list2) ; use list2 by default
76 ;;; ))
77 ;;;
78 ;;; You can call hide-ifdef-use-define-alist (C-c @ u) at any time to specify
79 ;;; another list to use.
80 ;;;
81 ;;; To cause ifdefs to be hidden as soon as hide-ifdef-mode is called,
82 ;;; set hide-ifdef-initially to non-nil.
83 ;;;
84 ;;; If you set hide-ifdef-lines to t, hide-ifdefs hides all the #ifdef lines.
85 ;;; In the absence of highlighting, that might be a bad idea. If you set
86 ;;; hide-ifdef-lines to nil (the default), the surrounding preprocessor
87 ;;; lines will be displayed. That can be confusing in its own
88 ;;; right. Other variations on display are possible, but not much
89 ;;; better.
90 ;;;
91 ;;; You can explicitly hide or show individual ifdef blocks irrespective
92 ;;; of the define list by using hide-ifdef-block and show-ifdef-block.
93 ;;;
94 ;;; You can move the point between ifdefs with forward-ifdef, backward-ifdef,
95 ;;; up-ifdef, down-ifdef, next-ifdef, and previous-ifdef.
96 ;;;
97 ;;; If you have minor-mode-alist in your mode line (the default) two labels
98 ;;; may appear. "Ifdef" will appear when hide-ifdef-mode is active. "Hiding"
99 ;;; will appear when text may be hidden ("hide-ifdef-hiding" is non-nil).
100 ;;;
101 ;;; Written by Brian Marick, at Gould, Computer Systems Division, Urbana IL.
102 ;;; Extensively modified by Daniel LaLiberte (while at Gould).
103 ;;;
104 ;;; You may freely modify and distribute this, but keep a record
105 ;;; of modifications and send comments to:
106 ;;; liberte@a.cs.uiuc.edu or ihnp4!uiucdcs!liberte
107 ;;; I will continue to upgrade hide-ifdef-mode
108 ;;; with your contributions.
109
110 ;;; Code:
111
112 (require 'cc-mode)
113
114 (defvar hide-ifdef-mode-submap nil
115 "Keymap used with Hide-Ifdef mode.")
116
117 (defvar hide-ifdef-mode-map nil
118 "Keymap used with Hide-Ifdef mode.")
119
120 (defconst hide-ifdef-mode-prefix-key "\C-c@"
121 "Prefix key for all Hide-Ifdef mode commands.")
122
123 ;; Set up the submap that goes after the prefix key.
124 (if hide-ifdef-mode-submap
125 () ; dont redefine it.
126 (setq hide-ifdef-mode-submap (make-sparse-keymap))
127 (define-key hide-ifdef-mode-submap "d" 'hide-ifdef-define)
128 (define-key hide-ifdef-mode-submap "u" 'hide-ifdef-undef)
129 (define-key hide-ifdef-mode-submap "D" 'hide-ifdef-set-define-alist)
130 (define-key hide-ifdef-mode-submap "U" 'hide-ifdef-use-define-alist)
131
132 (define-key hide-ifdef-mode-submap "h" 'hide-ifdefs)
133 (define-key hide-ifdef-mode-submap "s" 'show-ifdefs)
134 (define-key hide-ifdef-mode-submap "\C-d" 'hide-ifdef-block)
135 (define-key hide-ifdef-mode-submap "\C-s" 'show-ifdef-block)
136
137 (define-key hide-ifdef-mode-submap "\C-q" 'hide-ifdef-toggle-read-only)
138 (let ((where (where-is-internal 'toggle-read-only '(keymap) t)))
139 (if where
140 (define-key hide-ifdef-mode-submap
141 where
142 'hide-ifdef-toggle-outside-read-only)))
143 )
144
145 ;; Set up the mode's main map, which leads via the prefix key to the submap.
146 (if hide-ifdef-mode-map
147 ()
148 (setq hide-ifdef-mode-map (make-sparse-keymap))
149 (define-key hide-ifdef-mode-map hide-ifdef-mode-prefix-key
150 hide-ifdef-mode-submap))
151
152 (defvar hide-ifdef-mode nil
153 "Non-nil when hide-ifdef-mode is activated.")
154
155 (defvar hide-ifdef-hiding nil
156 "Non-nil when text may be hidden.")
157
158 ;; Arrange to use the mode's map when the mode is enabled.
159 (or (assq 'hide-ifdef-mode minor-mode-map-alist)
160 (setq minor-mode-map-alist
161 (cons (cons 'hide-ifdef-mode hide-ifdef-mode-map)
162 minor-mode-map-alist)))
163
164 (or (assq 'hide-ifdef-hiding minor-mode-alist)
165 (setq minor-mode-alist
166 (cons '(hide-ifdef-hiding " Hiding")
167 minor-mode-alist)))
168
169 (or (assq 'hide-ifdef-mode minor-mode-alist)
170 (setq minor-mode-alist
171 (cons '(hide-ifdef-mode " Ifdef")
172 minor-mode-alist)))
173
174 ;; fix c-mode syntax table so we can recognize whole symbols.
175 (defvar hide-ifdef-syntax-table
176 (copy-syntax-table c-mode-syntax-table)
177 "Syntax table used for tokenizing #if expressions.")
178
179 (modify-syntax-entry ?_ "w" hide-ifdef-syntax-table)
180 (modify-syntax-entry ?& "." hide-ifdef-syntax-table)
181 (modify-syntax-entry ?\| "." hide-ifdef-syntax-table)
182
183 ;;;###autoload
184 (defun hide-ifdef-mode (arg)
185 "Toggle Hide-Ifdef mode. This is a minor mode, albeit a large one.
186 With ARG, turn Hide-Ifdef mode on iff arg is positive.
187 In Hide-Ifdef mode, code within #ifdef constructs that the C preprocessor
188 would eliminate may be hidden from view. Several variables affect
189 how the hiding is done:
190
191 hide-ifdef-env
192 An association list of defined and undefined symbols for the
193 current buffer. Initially, the global value of `hide-ifdef-env'
194 is used.
195
196 hide-ifdef-define-alist
197 An association list of defined symbol lists.
198 Use `hide-ifdef-set-define-alist' to save the current `hide-ifdef-env'
199 and `hide-ifdef-use-define-alist' to set the current `hide-ifdef-env'
200 from one of the lists in `hide-ifdef-define-alist'.
201
202 hide-ifdef-lines
203 Set to non-nil to not show #if, #ifdef, #ifndef, #else, and
204 #endif lines when hiding.
205
206 hide-ifdef-initially
207 Indicates whether `hide-ifdefs' should be called when Hide-Ifdef mode
208 is activated.
209
210 hide-ifdef-read-only
211 Set to non-nil if you want to make buffers read only while hiding.
212 After `show-ifdefs', read-only status is restored to previous value.
213
214 \\{hide-ifdef-mode-map}"
215
216 (interactive "P")
217 (make-local-variable 'hide-ifdef-mode)
218 (setq hide-ifdef-mode
219 (if (null arg)
220 (not hide-ifdef-mode)
221 (> (prefix-numeric-value arg) 0)))
222
223 (force-mode-line-update)
224
225 (if hide-ifdef-mode
226 (progn
227 ; inherit global values
228 (make-local-variable 'hide-ifdef-env)
229 (setq hide-ifdef-env (default-value 'hide-ifdef-env))
230
231 (make-local-variable 'hide-ifdef-hiding)
232 (setq hide-ifdef-hiding (default-value 'hide-ifdef-hiding))
233
234 (make-local-variable 'hif-outside-read-only)
235 (setq hif-outside-read-only buffer-read-only)
236
237 (run-hooks 'hide-ifdef-mode-hook)
238
239 (if hide-ifdef-initially
240 (hide-ifdefs)
241 (show-ifdefs))
242 (message "Enter hide-ifdef-mode.")
243 )
244 ; else end hide-ifdef-mode
245 (if hide-ifdef-hiding
246 (show-ifdefs))
247 (message "Exit hide-ifdef-mode.")
248 ))
249
250
251 ;; from outline.el with docstring fixed.
252 (defun hif-outline-flag-region (from to flag)
253 "Hides or shows lines from FROM to TO, according to FLAG. If FLAG
254 is \\n (newline character) then text is shown, while if FLAG is \\^M
255 \(control-M) the text is hidden."
256 (let ((modp (buffer-modified-p)))
257 (unwind-protect (progn
258 (subst-char-in-region from to
259 (if (= flag ?\n) ?\^M ?\n)
260 flag t) )
261 (set-buffer-modified-p modp))
262 ))
263
264 (defun hif-show-all ()
265 "Show all of the text in the current buffer."
266 (interactive)
267 (hif-outline-flag-region (point-min) (point-max) ?\n))
268
269 (defun hide-ifdef-region (start end)
270 "START is the start of a #if or #else form. END is the ending part.
271 Everything including these lines is made invisible."
272 (hif-outline-flag-region start end ?\^M)
273 )
274
275 (defun hif-show-ifdef-region (start end)
276 "Everything between START and END is made visible."
277 (hif-outline-flag-region start end ?\n)
278 )
279
280
281
282 ;===%%SF%% evaluation (Start) ===
283
284 (defvar hide-ifdef-evaluator 'eval
285 "The evaluator is given a canonical form and returns T if text under
286 that form should be displayed.")
287
288 (defvar hif-undefined-symbol nil
289 "...is by default considered to be false.")
290
291 (defvar hide-ifdef-env nil
292 "An alist of defined symbols and their values.")
293
294
295 (defun hif-set-var (var value)
296 "Prepend (var value) pair to hide-ifdef-env."
297 (setq hide-ifdef-env (cons (cons var value) hide-ifdef-env)))
298
299
300 (defun hif-lookup (var)
301 ; (message "hif-lookup %s" var)
302 (let ((val (assoc var hide-ifdef-env)))
303 (if val
304 (cdr val)
305 hif-undefined-symbol)))
306
307 (defun hif-defined (var)
308 (hif-lookup var)
309 ; when #if expressions are fully supported, defined result should be 1
310 ; (if (assoc var hide-ifdef-env)
311 ; 1
312 ; nil)
313 )
314
315
316 ;===%%SF%% evaluation (End) ===
317
318
319
320 ;===%%SF%% parsing (Start) ===
321 ;;; The code that understands what ifs and ifdef in files look like.
322
323 (defconst hif-cpp-prefix "\\(^\\|\r\\)[ \t]*#[ \t]*")
324 (defconst hif-ifndef-regexp (concat hif-cpp-prefix "ifndef"))
325 (defconst hif-ifx-regexp (concat hif-cpp-prefix "if\\(n?def\\)?[ \t]+"))
326 (defconst hif-else-regexp (concat hif-cpp-prefix "else"))
327 (defconst hif-endif-regexp (concat hif-cpp-prefix "endif"))
328 (defconst hif-ifx-else-endif-regexp
329 (concat hif-ifx-regexp "\\|" hif-else-regexp "\\|" hif-endif-regexp))
330
331
332 (defun hif-infix-to-prefix (token-list)
333 "Convert list of tokens in infix into prefix list"
334 ; (message "hif-infix-to-prefix: %s" token-list)
335 (if (= 1 (length token-list))
336 (` (hif-lookup (quote (, (car token-list)))))
337 (hif-parse-if-exp token-list))
338 )
339
340 ; pattern to match initial identifier, !, &&, ||, (, or ).
341 ; Added ==, + and -: garyo@avs.com 8/9/94
342 (defconst hif-token-regexp "^\\(!\\|&&\\|||\\|[!=]=\\|[()+-]\\|\\w+\\)")
343 (defconst hif-end-of-comment "\\*/")
344
345
346 (defun hif-tokenize (expr-string)
347 "Separate string into a list of tokens"
348 (let ((token-list nil)
349 (expr-start 0)
350 (expr-length (length expr-string))
351 (current-syntax-table (syntax-table)))
352 (unwind-protect
353 (progn
354 (set-syntax-table hide-ifdef-syntax-table)
355 (while (< expr-start expr-length)
356 ; (message "expr-start = %d" expr-start) (sit-for 1)
357 (cond
358 ((string-match "^[ \t]+" expr-string expr-start)
359 ;; skip whitespace
360 (setq expr-start (match-end 0))
361 ;; stick newline in string so ^ matches on the next string-match
362 (aset expr-string (1- expr-start) ?\n))
363
364 ((string-match "^/\\*" expr-string expr-start)
365 (setq expr-start (match-end 0))
366 (aset expr-string (1- expr-start) ?\n)
367 (or
368 (string-match hif-end-of-comment
369 expr-string expr-start) ; eat comment
370 (string-match "$" expr-string expr-start)) ; multi-line comment
371 (setq expr-start (match-end 0))
372 (aset expr-string (1- expr-start) ?\n))
373
374 ((string-match "^//" expr-string expr-start)
375 (string-match "$" expr-string expr-start)
376 (setq expr-start (match-end 0)))
377
378 ((string-match hif-token-regexp expr-string expr-start)
379 (let ((token (substring expr-string expr-start (match-end 0))))
380 (setq expr-start (match-end 0))
381 (aset expr-string (1- expr-start) ?\n)
382 ; (message "token: %s" token) (sit-for 1)
383 (setq token-list
384 (cons
385 (cond
386 ((string-equal token "||") 'or)
387 ((string-equal token "&&") 'and)
388 ((string-equal token "==") 'equal)
389 ((string-equal token "!=") 'hif-notequal)
390 ((string-equal token "!") 'not)
391 ((string-equal token "defined") 'hif-defined)
392 ((string-equal token "(") 'lparen)
393 ((string-equal token ")") 'rparen)
394 ((string-equal token "+") 'hif-plus)
395 ((string-equal token "-") 'hif-minus)
396 (t (intern token)))
397 token-list))))
398 (t (error "Bad #if expression: %s" expr-string)))))
399 (set-syntax-table current-syntax-table))
400 (nreverse token-list)))
401
402 ;;;-----------------------------------------------------------------
403 ;;; Translate C preprocessor #if expressions using recursive descent.
404 ;;; This parser is limited to the operators &&, ||, !, and "defined".
405 ;;; Added ==, !=, +, and -. Gary Oberbrunner, garyo@avs.com, 8/9/94
406
407 (defun hif-parse-if-exp (token-list)
408 "Parse the TOKEN-LIST. Return translated list in prefix form."
409 (hif-nexttoken)
410 (prog1
411 (hif-expr)
412 (if token ; is there still a token?
413 (error "Error: unexpected token: %s" token))))
414
415 (defun hif-nexttoken ()
416 "Pop the next token from token-list into the let variable \"token\"."
417 (setq token (car token-list))
418 (setq token-list (cdr token-list))
419 token)
420
421 (defun hif-expr ()
422 "Parse and expression of the form
423 expr : term | expr '||' term."
424 (let ((result (hif-term)))
425 (while (eq token 'or)
426 (hif-nexttoken)
427 (setq result (list 'or result (hif-term))))
428 result))
429
430 (defun hif-term ()
431 "Parse a term of the form
432 term : eq-expr | term '&&' eq-expr."
433 (let ((result (hif-eq-expr)))
434 (while (eq token 'and)
435 (hif-nexttoken)
436 (setq result (list 'and result (hif-eq-expr))))
437 result))
438
439 (defun hif-eq-expr ()
440 "Parse a term of the form
441 eq-expr : math | eq-expr '=='|'!=' math."
442 (let ((result (hif-math))
443 (eq-token nil))
444 (while (or (eq token 'equal) (eq token 'hif-notequal))
445 (setq eq-token token)
446 (hif-nexttoken)
447 (setq result (list eq-token result (hif-math))))
448 result))
449
450 (defun hif-math ()
451 "Parse an expression of the form
452 math : factor | math '+|-' factor."
453 (let ((result (hif-factor))
454 (math-op nil))
455 (while (or (eq token 'hif-plus) (eq token 'hif-minus))
456 (setq math-op token)
457 (hif-nexttoken)
458 (setq result (list math-op result (hif-factor))))
459 result))
460
461 (defun hif-factor ()
462 "Parse a factor of the form
463 factor : '!' factor | '(' expr ')' | 'defined(' id ')' | id."
464 (cond
465 ((eq token 'not)
466 (hif-nexttoken)
467 (list 'not (hif-factor)))
468
469 ((eq token 'lparen)
470 (hif-nexttoken)
471 (let ((result (hif-expr)))
472 (if (not (eq token 'rparen))
473 (error "Bad token in parenthesized expression: %s" token)
474 (hif-nexttoken)
475 result)))
476
477 ((eq token 'hif-defined)
478 (hif-nexttoken)
479 (if (not (eq token 'lparen))
480 (error "Error: expected \"(\" after \"defined\""))
481 (hif-nexttoken)
482 (let ((ident token))
483 (if (memq token '(or and not hif-defined lparen rparen))
484 (error "Error: unexpected token: %s" token))
485 (hif-nexttoken)
486 (if (not (eq token 'rparen))
487 (error "Error: expected \")\" after identifier"))
488 (hif-nexttoken)
489 (` (hif-defined (quote (, ident))))
490 ))
491
492 (t ; identifier
493 (let ((ident token))
494 (if (memq ident '(or and))
495 (error "Error: missing identifier"))
496 (hif-nexttoken)
497 (` (hif-lookup (quote (, ident))))
498 ))
499 ))
500
501 (defun hif-mathify (val)
502 "Treat VAL as a number: if it's t or nil, use 1 or 0."
503 (cond ((eq val t)
504 1)
505 ((null val)
506 0)
507 (t val)))
508
509 (defun hif-plus (a b)
510 "Like ordinary plus but treat t and nil as 1 and 0."
511 (+ (hif-mathify a) (hif-mathify b)))
512 (defun hif-minus (a b)
513 "Like ordinary minus but treat t and nil as 1 and 0."
514 (- (hif-mathify a) (hif-mathify b)))
515 (defun hif-notequal (a b)
516 "Like (not (equal A B)) but as one symbol."
517 (not (equal a b)))
518
519 ;;;----------- end of parser -----------------------
520
521
522 (defun hif-canonicalize ()
523 "When at beginning of #ifX, returns a canonical (evaluatable)
524 form for the expression."
525 (save-excursion
526 (let ((negate (looking-at hif-ifndef-regexp)))
527 (re-search-forward hif-ifx-regexp)
528 (let* ((expr-string
529 (buffer-substring (point)
530 (progn (skip-chars-forward "^\n\r") (point))))
531 (expr (hif-infix-to-prefix (hif-tokenize expr-string))))
532 ; (message "hif-canonicalized: %s" expr)
533 (if negate
534 (list 'not expr)
535 expr)))))
536
537
538 (defun hif-find-any-ifX ()
539 "Position at beginning of next #if, #ifdef, or #ifndef, including one on
540 this line."
541 ; (message "find ifX at %d" (point))
542 (prog1
543 (re-search-forward hif-ifx-regexp (point-max) t)
544 (beginning-of-line)))
545
546
547 (defun hif-find-next-relevant ()
548 "Position at beginning of next #ifdef, #ifndef, #else, #endif,
549 NOT including one on this line."
550 ; (message "hif-find-next-relevant at %d" (point))
551 (end-of-line)
552 ; avoid infinite recursion by only going to beginning of line if match found
553 (if (re-search-forward hif-ifx-else-endif-regexp (point-max) t)
554 (beginning-of-line)))
555
556 (defun hif-find-previous-relevant ()
557 "Position at beginning of previous #ifdef, #ifndef, #else, #endif,
558 NOT including one on this line."
559 ; (message "hif-find-previous-relevant at %d" (point))
560 (beginning-of-line)
561 ; avoid infinite recursion by only going to beginning of line if match found
562 (if (re-search-backward hif-ifx-else-endif-regexp (point-min) t)
563 (beginning-of-line)))
564
565
566 (defun hif-looking-at-ifX () ;; Should eventually see #if
567 (looking-at hif-ifx-regexp))
568 (defun hif-looking-at-endif ()
569 (looking-at hif-endif-regexp))
570 (defun hif-looking-at-else ()
571 (looking-at hif-else-regexp))
572
573
574
575 (defun hif-ifdef-to-endif ()
576 "If positioned at #ifX or #else form, skip to corresponding #endif."
577 ; (message "hif-ifdef-to-endif at %d" (point)) (sit-for 1)
578 (hif-find-next-relevant)
579 (cond ((hif-looking-at-ifX)
580 (hif-ifdef-to-endif) ; find endif of nested if
581 (hif-ifdef-to-endif)) ; find outer endif or else
582 ((hif-looking-at-else)
583 (hif-ifdef-to-endif)) ; find endif following else
584 ((hif-looking-at-endif)
585 'done)
586 (t
587 (error "Mismatched #ifdef #endif pair"))))
588
589
590 (defun hif-endif-to-ifdef ()
591 "If positioned at #endif form, skip backward to corresponding #ifX."
592 ; (message "hif-endif-to-ifdef at %d" (point))
593 (let ((start (point)))
594 (hif-find-previous-relevant)
595 (if (= start (point))
596 (error "Mismatched #ifdef #endif pair")))
597 (cond ((hif-looking-at-endif)
598 (hif-endif-to-ifdef) ; find beginning of nested if
599 (hif-endif-to-ifdef)) ; find beginning of outer if or else
600 ((hif-looking-at-else)
601 (hif-endif-to-ifdef))
602 ((hif-looking-at-ifX)
603 'done)
604 (t))) ; never gets here
605
606
607 (defun forward-ifdef (&optional arg)
608 "Move point to beginning of line of the next ifdef-endif.
609 With argument, do this that many times."
610 (interactive "p")
611 (or arg (setq arg 1))
612 (if (< arg 0)
613 (backward-ifdef (- arg)))
614 (while (< 0 arg)
615 (setq arg (- arg))
616 (let ((start (point)))
617 (if (not (hif-looking-at-ifX))
618 (hif-find-next-relevant))
619 (if (hif-looking-at-ifX)
620 (hif-ifdef-to-endif)
621 (goto-char start)
622 (error "No following #ifdef")
623 ))))
624
625
626 (defun backward-ifdef (&optional arg)
627 "Move point to beginning of the previous ifdef-endif.
628 With argument, do this that many times."
629 (interactive "p")
630 (or arg (setq arg 1))
631 (if (< arg 0)
632 (forward-ifdef (- arg)))
633 (while (< 0 arg)
634 (setq arg (1- arg))
635 (beginning-of-line)
636 (let ((start (point)))
637 (if (not (hif-looking-at-endif))
638 (hif-find-previous-relevant))
639 (if (hif-looking-at-endif)
640 (hif-endif-to-ifdef)
641 (goto-char start)
642 (error "No previous #ifdef")))))
643
644
645 (defun down-ifdef ()
646 "Move point to beginning of nested ifdef or else-part."
647 (interactive)
648 (let ((start (point)))
649 (hif-find-next-relevant)
650 (if (or (hif-looking-at-ifX) (hif-looking-at-else))
651 ()
652 (goto-char start)
653 (error "No following #ifdef"))))
654
655
656 (defun up-ifdef ()
657 "Move point to beginning of enclosing ifdef or else-part."
658 (interactive)
659 (beginning-of-line)
660 (let ((start (point)))
661 (if (not (hif-looking-at-endif))
662 (hif-find-previous-relevant))
663 (if (hif-looking-at-endif)
664 (hif-endif-to-ifdef))
665 (if (= start (point))
666 (error "No previous #ifdef"))))
667
668 (defun next-ifdef (&optional arg)
669 "Move to the beginning of the next #ifX, #else, or #endif.
670 With argument, do this that many times."
671 (interactive "p")
672 (or arg (setq arg 1))
673 (if (< arg 0)
674 (previous-ifdef (- arg)))
675 (while (< 0 arg)
676 (setq arg (1- arg))
677 (hif-find-next-relevant)
678 (if (eolp)
679 (progn
680 (beginning-of-line)
681 (error "No following #ifdefs, #elses, or #endifs")))))
682
683 (defun previous-ifdef (&optional arg)
684 "Move to the beginning of the previous #ifX, #else, or #endif.
685 With argument, do this that many times."
686 (interactive "p")
687 (or arg (setq arg 1))
688 (if (< arg 0)
689 (next-ifdef (- arg)))
690 (while (< 0 arg)
691 (setq arg (1- arg))
692 (let ((start (point)))
693 (hif-find-previous-relevant)
694 (if (= start (point))
695 (error "No previous #ifdefs, #elses, or #endifs")
696 ))))
697
698
699 ;===%%SF%% parsing (End) ===
700
701
702 ;===%%SF%% hide-ifdef-hiding (Start) ===
703
704
705 ;;; A range is a structure with four components:
706 ;;; ELSE-P True if there was an else clause for the ifdef.
707 ;;; START The start of the range. (beginning of line)
708 ;;; ELSE The else marker (beginning of line)
709 ;;; Only valid if ELSE-P is true.
710 ;;; END The end of the range. (beginning of line)
711
712 (defun hif-make-range (else-p start end &optional else)
713 (list else-p start else end))
714
715 (defun hif-range-else-p (range) (elt range 0))
716 (defun hif-range-start (range) (elt range 1))
717 (defun hif-range-else (range) (elt range 2))
718 (defun hif-range-end (range) (elt range 3))
719
720
721
722 ;;; Find-Range
723 ;;; The workhorse, it delimits the #if region. Reasonably simple:
724 ;;; Skip until an #else or #endif is found, remembering positions. If
725 ;;; an #else was found, skip some more, looking for the true #endif.
726
727 (defun hif-find-range ()
728 "Returns a Range structure describing the current #if region.
729 Point is left unchanged."
730 ; (message "hif-find-range at %d" (point))
731 (save-excursion
732 (beginning-of-line)
733 (let ((start (point))
734 (else-p nil)
735 (else nil)
736 (end nil))
737 ;; Part one. Look for either #endif or #else.
738 ;; This loop-and-a-half dedicated to E. Dijkstra.
739 (hif-find-next-relevant)
740 (while (hif-looking-at-ifX) ; Skip nested ifdef
741 (hif-ifdef-to-endif)
742 (hif-find-next-relevant))
743 ;; Found either a #else or an #endif.
744 (cond ((hif-looking-at-else)
745 (setq else-p t)
746 (setq else (point)))
747 (t
748 (setq end (point)) ; (save-excursion (end-of-line) (point))
749 ))
750 ;; If found #else, look for #endif.
751 (if else-p
752 (progn
753 (hif-find-next-relevant)
754 (while (hif-looking-at-ifX) ; Skip nested ifdef
755 (hif-ifdef-to-endif)
756 (hif-find-next-relevant))
757 (if (hif-looking-at-else)
758 (error "Found two elses in a row? Broken!"))
759 (setq end (point)) ; (save-excursion (end-of-line) (point))
760 ))
761 (hif-make-range else-p start end else))))
762
763
764 ;;; A bit slimy.
765 ;;; NOTE: If there's an #ifdef at the beginning of the file, we can't
766 ;;; hide it. There's no previous newline to replace. If we added
767 ;;; one, we'd throw off all the counts. Feh.
768
769 (defun hif-hide-line (point)
770 "Hide the line containing point. Does nothing if `hide-ifdef-lines' is nil."
771 (if hide-ifdef-lines
772 (save-excursion
773 (goto-char point)
774 (let ((modp (buffer-modified-p)))
775 (unwind-protect
776 (progn
777 (beginning-of-line)
778 (if (not (= (point) 1))
779 (hide-ifdef-region (1- (point)) (point))))
780 (set-buffer-modified-p modp))
781 ))
782 ))
783
784
785 ;;; Hif-Possibly-Hide
786 ;;; There are four cases. The #ifX expression is "taken" if it
787 ;;; the hide-ifdef-evaluator returns T. Presumably, this means the code
788 ;;; inside the #ifdef would be included when the program was
789 ;;; compiled.
790 ;;;
791 ;;; Case 1: #ifX taken, and there's an #else.
792 ;;; The #else part must be hidden. The #if (then) part must be
793 ;;; processed for nested #ifX's.
794 ;;; Case 2: #ifX taken, and there's no #else.
795 ;;; The #if part must be processed for nested #ifX's.
796 ;;; Case 3: #ifX not taken, and there's an #else.
797 ;;; The #if part must be hidden. The #else part must be processed
798 ;;; for nested #ifs.
799 ;;; Case 4: #ifX not taken, and there's no #else.
800 ;;; The #ifX part must be hidden.
801 ;;;
802 ;;; Further processing is done by narrowing to the relevant region
803 ;;; and just recursively calling hide-ifdef-guts.
804 ;;;
805 ;;; When hif-possibly-hide returns, point is at the end of the
806 ;;; possibly-hidden range.
807
808 (defun hif-recurse-on (start end)
809 "Call `hide-ifdef-guts' after narrowing to end of START line and END line."
810 (save-excursion
811 (save-restriction
812 (goto-char start)
813 (end-of-line)
814 (narrow-to-region (point) end)
815 (hide-ifdef-guts))))
816
817 (defun hif-possibly-hide ()
818 "Called at #ifX expression, this hides those parts that should be
819 hidden, according to judgement of `hide-ifdef-evaluator'."
820 ; (message "hif-possibly-hide") (sit-for 1)
821 (let ((test (hif-canonicalize))
822 (range (hif-find-range)))
823 ; (message "test = %s" test) (sit-for 1)
824
825 (hif-hide-line (hif-range-end range))
826 (if (funcall hide-ifdef-evaluator test)
827 (cond ((hif-range-else-p range) ; case 1
828 (hif-hide-line (hif-range-else range))
829 (hide-ifdef-region (hif-range-else range)
830 (1- (hif-range-end range)))
831 (hif-recurse-on (hif-range-start range)
832 (hif-range-else range)))
833 (t ; case 2
834 (hif-recurse-on (hif-range-start range)
835 (hif-range-end range))))
836 (cond ((hif-range-else-p range) ; case 3
837 (hif-hide-line (hif-range-else range))
838 (hide-ifdef-region (hif-range-start range)
839 (1- (hif-range-else range)))
840 (hif-recurse-on (hif-range-else range)
841 (hif-range-end range)))
842 (t ; case 4
843 (hide-ifdef-region (point)
844 (1- (hif-range-end range))))
845 ))
846 (hif-hide-line (hif-range-start range)) ; Always hide start.
847 (goto-char (hif-range-end range))
848 (end-of-line)
849 ))
850
851
852
853 (defun hide-ifdef-guts ()
854 "Does the work of `hide-ifdefs', except for the work that's pointless
855 to redo on a recursive entry."
856 ; (message "hide-ifdef-guts")
857 (save-excursion
858 (goto-char (point-min))
859 (while (hif-find-any-ifX)
860 (hif-possibly-hide))))
861
862 ;===%%SF%% hide-ifdef-hiding (End) ===
863
864
865 ;===%%SF%% exports (Start) ===
866
867 ;;;###autoload
868 (defvar hide-ifdef-initially nil
869 "*Non-nil if `hide-ifdefs' should be called when Hide-Ifdef mode
870 is first activated.")
871
872 ;;;###autoload
873 (defvar hide-ifdef-read-only nil
874 "*Set to non-nil if you want buffer to be read-only while hiding text.")
875
876 (defvar hif-outside-read-only nil
877 "Internal variable. Saves the value of `buffer-read-only' while hiding.")
878
879 ;;;###autoload
880 (defvar hide-ifdef-lines nil
881 "*Set to t if you don't want to see the #ifX, #else, and #endif lines.")
882
883 (defun hide-ifdef-toggle-read-only ()
884 "Toggle hide-ifdef-read-only."
885 (interactive)
886 (setq hide-ifdef-read-only (not hide-ifdef-read-only))
887 (message "Hide-Read-Only %s"
888 (if hide-ifdef-read-only "ON" "OFF"))
889 (if hide-ifdef-hiding
890 (setq buffer-read-only (or hide-ifdef-read-only hif-outside-read-only)))
891 (force-mode-line-update))
892
893 (defun hide-ifdef-toggle-outside-read-only ()
894 "Replacement for `toggle-read-only' within Hide Ifdef mode."
895 (interactive)
896 (setq hif-outside-read-only (not hif-outside-read-only))
897 (message "Read only %s"
898 (if hif-outside-read-only "ON" "OFF"))
899 (setq buffer-read-only
900 (or (and hide-ifdef-hiding hide-ifdef-read-only)
901 hif-outside-read-only)
902 )
903 (force-mode-line-update))
904
905
906 (defun hide-ifdef-define (var)
907 "Define a VAR so that #ifdef VAR would be included."
908 (interactive "SDefine what? ")
909 (hif-set-var var 1)
910 (if hide-ifdef-hiding (hide-ifdefs)))
911
912 (defun hide-ifdef-undef (var)
913 "Undefine a VAR so that #ifdef VAR would not be included."
914 (interactive "SUndefine what? ")
915 (hif-set-var var nil)
916 (if hide-ifdef-hiding (hide-ifdefs)))
917
918
919 (defun hide-ifdefs ()
920 "Hide the contents of some #ifdefs.
921 Assume that defined symbols have been added to `hide-ifdef-env'.
922 The text hidden is the text that would not be included by the C
923 preprocessor if it were given the file with those symbols defined.
924
925 Turn off hiding by calling `show-ifdefs'."
926
927 (interactive)
928 (message "Hiding...")
929 (if (not hide-ifdef-mode)
930 (hide-ifdef-mode 1)) ; turn on hide-ifdef-mode
931 (if hide-ifdef-hiding
932 (show-ifdefs)) ; Otherwise, deep confusion.
933 (let ((inhibit-read-only t))
934 (setq selective-display t)
935 (setq hide-ifdef-hiding t)
936 (hide-ifdef-guts))
937 (setq buffer-read-only (or hide-ifdef-read-only hif-outside-read-only))
938 (message "Hiding done"))
939
940
941 (defun show-ifdefs ()
942 "Cancel the effects of `hide-ifdef'. The contents of all #ifdefs is shown."
943 (interactive)
944 (setq buffer-read-only hif-outside-read-only)
945 (setq selective-display nil) ; defaults
946 (let ((inhibit-read-only t))
947 (hif-show-all))
948 (setq hide-ifdef-hiding nil))
949
950
951 (defun hif-find-ifdef-block ()
952 "Utility for hide and show `ifdef-block'.
953 Set top and bottom of ifdef block."
954 (let (max-bottom)
955 (save-excursion
956 (beginning-of-line)
957 (if (not (or (hif-looking-at-else) (hif-looking-at-ifX)))
958 (up-ifdef))
959 (setq top (point))
960 (hif-ifdef-to-endif)
961 (setq max-bottom (1- (point))))
962 (save-excursion
963 (beginning-of-line)
964 (if (not (hif-looking-at-endif))
965 (hif-find-next-relevant))
966 (while (hif-looking-at-ifX)
967 (hif-ifdef-to-endif)
968 (hif-find-next-relevant))
969 (setq bottom (min max-bottom (1- (point))))))
970 )
971
972
973 (defun hide-ifdef-block ()
974 "Hide the ifdef block (true or false part) enclosing or before the cursor."
975 (interactive)
976 (if (not hide-ifdef-mode)
977 (hide-ifdef-mode 1))
978 (setq selective-display t)
979 (let (top bottom (inhibit-read-only t))
980 (hif-find-ifdef-block) ; set top and bottom - dynamic scoping
981 (hide-ifdef-region top bottom)
982 (if hide-ifdef-lines
983 (progn
984 (hif-hide-line top)
985 (hif-hide-line (1+ bottom))))
986 (setq hide-ifdef-hiding t))
987 (setq buffer-read-only (or hide-ifdef-read-only hif-outside-read-only)))
988
989
990 (defun show-ifdef-block ()
991 "Show the ifdef block (true or false part) enclosing or before the cursor."
992 (interactive)
993 (let ((inhibit-read-only t))
994 (if hide-ifdef-lines
995 (save-excursion
996 (beginning-of-line)
997 (hif-show-ifdef-region (1- (point)) (progn (end-of-line) (point))))
998
999 (let (top bottom)
1000 (hif-find-ifdef-block)
1001 (hif-show-ifdef-region (1- top) bottom)))))
1002
1003
1004 ;;; definition alist support
1005
1006 (defvar hide-ifdef-define-alist nil
1007 "A global assoc list of pre-defined symbol lists")
1008
1009 (defun hif-compress-define-list (env)
1010 "Compress the define list ENV into a list of defined symbols only."
1011 (let ((defs (mapcar '(lambda (arg)
1012 (if (hif-lookup (car arg)) (car arg)))
1013 env))
1014 (new-defs nil))
1015 (while defs
1016 (if (car defs)
1017 (setq new-defs (cons (car defs) new-defs)))
1018 (setq defs (cdr defs)))
1019 new-defs))
1020
1021 (defun hide-ifdef-set-define-alist (name)
1022 "Set the association for NAME to `hide-ifdef-env'."
1023 (interactive "SSet define list: ")
1024 (setq hide-ifdef-define-alist
1025 (cons (cons name (hif-compress-define-list hide-ifdef-env))
1026 hide-ifdef-define-alist)))
1027
1028 (defun hide-ifdef-use-define-alist (name)
1029 "Set `hide-ifdef-env' to the define list specified by NAME."
1030 (interactive "SUse define list: ")
1031 (let ((define-list (assoc name hide-ifdef-define-alist)))
1032 (if define-list
1033 (setq hide-ifdef-env
1034 (mapcar '(lambda (arg) (cons arg t))
1035 (cdr define-list)))
1036 (error "No define list for %s" name))
1037 (if hide-ifdef-hiding (hide-ifdefs))))
1038
1039 (provide 'hideif)
1040
1041 ;;; hideif.el ends here
1042