*** empty log message ***
[emacs.git] / lisp / progmodes / hideif.el
bloba43b91ce091d3bb439f86ae644222570cf0ed028
1 ;;; hide-ifdef-mode.el --- ides selected code within ifdef.
3 ;;; Copyright (C) 1988 Brian Marick and Daniel LaLiberte
4 ;;; Written by Brian Marick, at Gould, Computer Systems Division, Urbana IL.
5 ;;; Extensively modified by Daniel LaLiberte (while at Gould).
6 ;;;
7 ;;; You may freely modify and distribute this, but keep a record
8 ;;; of modifications and send comments to:
9 ;;; liberte@a.cs.uiuc.edu or ihnp4!uiucdcs!liberte
10 ;;; I will continue to upgrade hide-ifdef-mode
11 ;;; with your contributions and will eventually offer it to FSF.
12 ;;;
13 ;;; $Header: hide-ifdef-mode.el,v 1.7 88/02/16 03:12:58 liberte Exp $
14 ;;;
15 ;;; $Log: hide-ifdef-mode.el,v $
16 ;;; Revision 1.7 88/02/16 03:12:58 liberte
17 ;;; Fixed comments and doc strings.
18 ;;; Added optional prefix arg for ifdef motion commands.
19 ;;;
20 ;;; Revision 1.6 88/02/05 00:36:18 liberte
21 ;;; Bug fixes.
22 ;;; 1. A multi-line comment that starts on an #ifdef line
23 ;;; now ends on that line.
24 ;;; 2. Fix bad function name: hide-hif-ifdef-toggle-read-only
25 ;;; 3. Make ifdef-block hiding work outside of ifdefs.
26 ;;;
27 ;;; Revision 1.5 88/01/31 23:19:31 liberte
28 ;;; Major clean up.
29 ;;; Prefix internal names with "hif-".
30 ;;;
31 ;;; Revision 1.4 88/01/30 14:09:38 liberte
32 ;;; Add hide-ifdef-hiding and hide-ifdef-mode to minor-mode-alist.
33 ;;;
34 ;;; Revision 1.3 88/01/29 00:38:19 liberte
35 ;;; Fix three bugs.
36 ;;; 1. Function "defined" is just like lookup.
37 ;;; 2. Skip to newline or cr in case text is hidden.
38 ;;; 3. Use car of token list if just one symbol.
39 ;;;
40 ;;; Revision 1.2 88/01/28 23:32:46 liberte
41 ;;; Use hide-ifdef-mode-prefix-key.
42 ;;; Copy current-local-map so other buffers do not get
43 ;;; hide-ifdef-mode bindings.
44 ;;;
45 ;;;--------------------------------------------------------------
46 ;;; To initialize, toggle the hide-ifdef minor mode with
47 ;;;
48 ;;; M-x hide-ifdef-mode
49 ;;;
50 ;;; This will set up key bindings and call hide-ifdef-mode-hook if it
51 ;;; has a value. To explicitly hide ifdefs using a buffer-local
52 ;;; define list (default empty), type
53 ;;;
54 ;;; M-x hide-ifdefs or C-c h
55 ;;;
56 ;;; Hide-ifdef suppresses the display of code that the preprocessor wouldn't
57 ;;; pass through. The support of constant expressions in #if lines is
58 ;;; limited to identifiers, parens, and the operators: &&, ||, !, and
59 ;;; "defined". Please extend this.
60 ;;;
61 ;;; The hidden code is marked by ellipses (...). Be
62 ;;; cautious when editing near ellipses, since the hidden text is
63 ;;; still in the buffer, and you can move the point into it and modify
64 ;;; text unawares. If you don't want to see the ellipses, set
65 ;;; selective-display-ellipses to nil. But this can be dangerous.
66 ;;; You can make your buffer read-only while hide-ifdef-hiding by setting
67 ;;; hide-ifdef-read-only to a non-nil value. You can toggle this
68 ;;; variable with hide-ifdef-toggle-read-only (C-c C-q).
69 ;;;
70 ;;; You can undo the effect of hide-ifdefs by typing
71 ;;;
72 ;;; M-x show-ifdefs or C-c s
73 ;;;
74 ;;; Use M-x hide-ifdef-define (C-c d) to define a symbol.
75 ;;; Use M-x hide-ifdef-undef (C-c u) to undefine a symbol.
76 ;;;
77 ;;; If you define or undefine a symbol while hide-ifdef-mode is in effect,
78 ;;; the display will be updated. Only the define list for the current
79 ;;; buffer will be affected. You can save changes to the local define
80 ;;; list with hide-ifdef-set-define-alist. This adds entries
81 ;;; to hide-ifdef-define-alist.
82 ;;;
83 ;;; If you have defined a hide-ifdef-mode-hook, you can set
84 ;;; up a list of symbols that may be used by hide-ifdefs as in the
85 ;;; following example:
86 ;;;
87 ;;; (setq hide-ifdef-mode-hook
88 ;;; '(lambda ()
89 ;;; (if (not hide-ifdef-define-alist)
90 ;;; (setq hide-ifdef-define-alist
91 ;;; '((list1 ONE TWO)
92 ;;; (list2 TWO THREE)
93 ;;; )))
94 ;;; (hide-ifdef-use-define-alist 'list2) ; use list2 by default
95 ;;; ))
96 ;;;
97 ;;; You can call hide-ifdef-use-define-alist (C-c u) at any time to specify
98 ;;; another list to use.
99 ;;;
100 ;;; To cause ifdefs to be hidden as soon as hide-ifdef-mode is called,
101 ;;; set hide-ifdef-initially to non-nil.
103 ;;; If you set hide-ifdef-lines to t, hide-ifdefs hides all the #ifdef lines.
104 ;;; In the absence of highlighting, that might be a bad idea. If you set
105 ;;; hide-ifdef-lines to nil (the default), the surrounding preprocessor
106 ;;; lines will be displayed. That can be confusing in its own
107 ;;; right. Other variations on display are possible, but not much
108 ;;; better.
110 ;;; You can explicitly hide or show individual ifdef blocks irrespective
111 ;;; of the define list by using hide-ifdef-block and show-ifdef-block.
113 ;;; You can move the point between ifdefs with forward-ifdef, backward-ifdef,
114 ;;; up-ifdef, down-ifdef, next-ifdef, and previous-ifdef.
116 ;;; If you have minor-mode-alist in your mode line (the default) two labels
117 ;;; may appear. "Ifdef" will appear when hide-ifdef-mode is active. "Hiding"
118 ;;; will appear when text may be hidden ("hide-ifdef-hiding" is non-nil).
122 (defvar hide-ifdef-mode-map nil
123 "Keymap used with Hide-Ifdef mode")
125 (defconst hide-ifdef-mode-prefix-key "\C-c"
126 "Prefix key for all Hide-Ifdef mode commands.")
128 (defvar hide-ifdef-mode-map-before nil
129 "Buffer-local variable to store a copy of the local keymap
130 before `hide-ifdef-mode' modifies it.")
132 (defun define-hide-ifdef-mode-map ()
133 (if hide-ifdef-mode-map
134 () ; dont redefine it.
135 (setq hide-ifdef-mode-map (make-sparse-keymap))
136 (define-key hide-ifdef-mode-map "d" 'hide-ifdef-define)
137 (define-key hide-ifdef-mode-map "u" 'hide-ifdef-undef)
138 (define-key hide-ifdef-mode-map "D" 'hide-ifdef-set-define-alist)
139 (define-key hide-ifdef-mode-map "U" 'hide-ifdef-use-define-alist)
141 (define-key hide-ifdef-mode-map "h" 'hide-ifdefs)
142 (define-key hide-ifdef-mode-map "s" 'show-ifdefs)
143 (define-key hide-ifdef-mode-map "\C-h" 'hide-ifdef-block)
144 (define-key hide-ifdef-mode-map "\C-s" 'show-ifdef-block)
146 (define-key hide-ifdef-mode-map "\C-f" 'forward-ifdef)
147 (define-key hide-ifdef-mode-map "\C-b" 'backward-ifdef)
148 (define-key hide-ifdef-mode-map "\C-d" 'down-ifdef)
149 (define-key hide-ifdef-mode-map "\C-u" 'up-ifdef)
150 (define-key hide-ifdef-mode-map "\C-n" 'next-ifdef)
151 (define-key hide-ifdef-mode-map "\C-p" 'previous-ifdef)
152 (define-key hide-ifdef-mode-map "\C-q" 'hide-ifdef-toggle-read-only)
153 (define-key hide-ifdef-mode-map
154 (where-is-internal 'toggle-read-only nil nil t)
155 'hide-ifdef-toggle-outside-read-only)
157 (fset 'hide-ifdef-mode-map hide-ifdef-mode-map) ; the function is the map
160 (defun hif-update-mode-line ()
161 "Update mode-line by setting buffer-modified to itself."
162 (set-buffer-modified-p (buffer-modified-p)))
164 (defvar hide-ifdef-mode nil
165 "non-nil when hide-ifdef-mode is activated.")
167 (defvar hide-ifdef-hiding nil
168 "non-nil when text may be hidden.")
170 (or (assq 'hide-ifdef-hiding minor-mode-alist)
171 (setq minor-mode-alist
172 (cons '(hide-ifdef-hiding " Hiding")
173 minor-mode-alist)))
175 (or (assq 'hide-ifdef-mode minor-mode-alist)
176 (setq minor-mode-alist
177 (cons '(hide-ifdef-mode " Ifdef")
178 minor-mode-alist)))
180 (defun hide-ifdef-mode (arg)
181 "Toggle Hide-Ifdef mode. This is a minor mode, albeit a large one.
182 With ARG, turn Hide-Ifdef mode on iff arg is positive.
183 In Hide-Ifdef mode, code within #ifdef constructs that the C preprocessor
184 would eliminate may be hidden from view. Several variables affect
185 how the hiding is done:
187 hide-ifdef-env
188 An association list of defined and undefined symbols for the
189 current buffer. Initially, the global value of `hide-ifdef-env'
190 is used.
192 hide-ifdef-define-alist
193 An association list of defined symbol lists.
194 Use `hide-ifdef-set-define-alist' to save the current `hide-ifdef-env'
195 and `hide-ifdef-use-define-alist' to set the current `hide-ifdef-env'
196 from one of the lists in `hide-ifdef-define-alist'.
198 hide-ifdef-lines
199 Set to non-nil to not show #if, #ifdef, #ifndef, #else, and
200 #endif lines when hiding.
202 hide-ifdef-initially
203 Indicates whether `hide-ifdefs' should be called when Hide-Ifdef mode
204 is activated.
206 hide-ifdef-read-only
207 Set to non-nil if you want to make buffers read only while hiding.
208 After `show-ifdefs', read-only status is restored to previous value.
210 \\{hide-ifdef-mode-map}"
212 (interactive "P")
213 (make-local-variable 'hide-ifdef-mode)
214 (setq hide-ifdef-mode
215 (if (null arg)
216 (not hide-ifdef-mode)
217 (> (prefix-numeric-value arg) 0)))
219 (hif-update-mode-line)
221 (if hide-ifdef-mode
222 (progn
223 ; fix c-mode syntax table so we can recognize whole symbols.
224 (modify-syntax-entry ?_ "w")
225 (modify-syntax-entry ?& ".")
226 (modify-syntax-entry ?\| ".")
228 ; inherit global values
229 (make-local-variable 'hide-ifdef-env)
230 (setq hide-ifdef-env (default-value 'hide-ifdef-env))
232 (make-local-variable 'hide-ifdef-hiding)
233 (setq hide-ifdef-hiding (default-value 'hide-ifdef-hiding))
235 (make-local-variable 'hif-outside-read-only)
236 (setq hif-outside-read-only buffer-read-only)
238 (make-local-variable 'hide-ifdef-mode-map-before)
239 (setq hide-ifdef-mode-map-before (current-local-map))
240 (use-local-map (copy-keymap (current-local-map)))
241 (local-unset-key hide-ifdef-mode-prefix-key)
242 (local-set-key hide-ifdef-mode-prefix-key 'hide-ifdef-mode-map)
243 (define-hide-ifdef-mode-map)
245 (run-hooks 'hide-ifdef-mode-hook)
247 (if hide-ifdef-initially
248 (hide-ifdefs)
249 (show-ifdefs))
250 (message "Enter hide-ifdef-mode.")
252 ; else end hide-ifdef-mode
253 (if hide-ifdef-hiding
254 (show-ifdefs))
255 (use-local-map hide-ifdef-mode-map-before)
256 (message "Exit hide-ifdef-mode.")
260 ;; from outline.el with docstring fixed.
261 (defun hif-outline-flag-region (from to flag)
262 "Hides or shows lines from FROM to TO, according to FLAG. If FLAG
263 is \\n (newline character) then text is shown, while if FLAG is \\^M
264 \(control-M) the text is hidden."
265 (let ((modp (buffer-modified-p)))
266 (unwind-protect (progn
267 (subst-char-in-region from to
268 (if (= flag ?\n) ?\^M ?\n)
269 flag t) )
270 (set-buffer-modified-p modp))
273 (defun hif-show-all ()
274 "Show all of the text in the current buffer."
275 (interactive)
276 (hif-outline-flag-region (point-min) (point-max) ?\n))
278 (defun hide-ifdef-region (start end)
279 "START is the start of a #if or #else form. END is the ending part.
280 Everything including these lines is made invisible."
281 (hif-outline-flag-region start end ?\^M)
284 (defun hif-show-ifdef-region (start end)
285 "Everything between START and END is made visible."
286 (hif-outline-flag-region start end ?\n)
291 ;===%%SF%% evaluation (Start) ===
293 (defvar hide-ifdef-evaluator 'eval
294 "The evaluator is given a canonical form and returns T if text under
295 that form should be displayed.")
297 (defvar hif-undefined-symbol nil
298 "...is by default considered to be false.")
300 (defvar hide-ifdef-env nil
301 "An alist of defined symbols and their values.")
304 (defun hif-set-var (var value)
305 "Prepend (var value) pair to hide-ifdef-env."
306 (setq hide-ifdef-env (cons (cons var value) hide-ifdef-env)))
309 (defun hif-lookup (var)
310 ; (message "hif-lookup %s" var)
311 (let ((val (assoc var hide-ifdef-env)))
312 (if val
313 (cdr val)
314 hif-undefined-symbol)))
316 (defun hif-defined (var)
317 (hif-lookup var)
318 ; when #if expressions are fully supported, defined result should be 1
319 ; (if (assoc var hide-ifdef-env)
321 ; nil)
325 ;===%%SF%% evaluation (End) ===
329 ;===%%SF%% parsing (Start) ===
330 ;;; The code that understands what ifs and ifdef in files look like.
332 (defconst hif-cpp-prefix "\\(^\\|\r\\)[ \t]*#[ \t]*")
333 (defconst hif-ifndef-regexp (concat hif-cpp-prefix "ifndef"))
334 (defconst hif-ifx-regexp (concat hif-cpp-prefix "if\\(n?def\\)?[ \t]+"))
335 (defconst hif-else-regexp (concat hif-cpp-prefix "else"))
336 (defconst hif-endif-regexp (concat hif-cpp-prefix "endif"))
337 (defconst hif-ifx-else-endif-regexp
338 (concat hif-ifx-regexp "\\|" hif-else-regexp "\\|" hif-endif-regexp))
341 (defun hif-infix-to-prefix (token-list)
342 "Convert list of tokens in infix into prefix list"
343 ; (message "hif-infix-to-prefix: %s" token-list)
344 (if (= 1 (length token-list))
345 (` (hif-lookup (quote (, (car token-list)))))
346 (hif-parse-if-exp token-list))
349 ; pattern to match initial identifier, !, &&, ||, (, or ).
350 (defconst hif-token-regexp "^\\(!\\|&&\\|||\\|[()]\\|\\w+\\)")
351 (defconst hif-end-of-comment "\\*/")
354 (defun hif-tokenize (expr-string)
355 "Separate string into a list of tokens"
356 (let ((token-list nil)
357 (expr-start 0)
358 (expr-length (length expr-string)))
360 (while (< expr-start expr-length)
361 ; (message "expr-start = %d" expr-start) (sit-for 1)
362 (cond
363 ((string-match "^[ \t]+" expr-string expr-start)
364 ; skip whitespace
365 (setq expr-start (match-end 0))
366 ; stick newline in string so ^ matches on the next string-match
367 (aset expr-string (1- expr-start) ?\n)
370 ((string-match "^/\\*" expr-string expr-start)
371 (setq expr-start (match-end 0))
372 (aset expr-string (1- expr-start) ?\n)
374 (string-match hif-end-of-comment
375 expr-string expr-start) ; eat comment
376 (string-match "$" expr-string expr-start)) ; multi-line comment
377 (setq expr-start (match-end 0))
378 (aset expr-string (1- expr-start) ?\n)
381 ((string-match hif-token-regexp expr-string expr-start)
382 (let ((token (substring expr-string expr-start (match-end 0))))
383 (setq expr-start (match-end 0))
384 (aset expr-string (1- expr-start) ?\n)
385 ; (message "token: %s" token) (sit-for 1)
386 (setq token-list
387 (cons
388 (cond
389 ((string-equal token "||") 'or)
390 ((string-equal token "&&") 'and)
391 ((string-equal token "!") 'not)
392 ((string-equal token "defined") 'hif-defined)
393 ((string-equal token "(") 'lparen)
394 ((string-equal token ")") 'rparen)
395 (t (intern token)))
396 token-list))
398 (t (error "Bad #if expression: %s" expr-string))
400 (nreverse token-list)
403 ;;;-----------------------------------------------------------------
404 ;;; Translate C preprocessor #if expressions using recursive descent.
405 ;;; This parser is limited to the operators &&, ||, !, and "defined".
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))))
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)
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))
430 (defun hif-term ()
431 "Parse a term of the form
432 term : factor | term '&&' factor."
433 (let ((result (hif-factor)))
434 (while (eq token 'and)
435 (hif-nexttoken)
436 (setq result (list 'and result (hif-factor))))
437 result))
439 (defun hif-factor ()
440 "Parse a factor of the form
441 factor : '!' factor | '(' expr ')' | 'defined(' id ')' | id."
442 (cond
443 ((eq token 'not)
444 (hif-nexttoken)
445 (list 'not (hif-factor)))
447 ((eq token 'lparen)
448 (hif-nexttoken)
449 (let ((result (hif-expr)))
450 (if (not (eq token 'rparen))
451 (error "Bad token in parenthesized expression: %s" token)
452 (hif-nexttoken)
453 result)))
455 ((eq token 'hif-defined)
456 (hif-nexttoken)
457 (if (not (eq token 'lparen))
458 (error "Error: expected \"(\" after \"defined\""))
459 (hif-nexttoken)
460 (let ((ident token))
461 (if (memq token '(or and not hif-defined lparen rparen))
462 (error "Error: unexpected token: %s" token))
463 (hif-nexttoken)
464 (if (not (eq token 'rparen))
465 (error "Error: expected \")\" after identifier"))
466 (hif-nexttoken)
467 (` (hif-defined (quote (, ident))))
470 (t ; identifier
471 (let ((ident token))
472 (if (memq ident '(or and))
473 (error "Error: missing identifier"))
474 (hif-nexttoken)
475 (` (hif-lookup (quote (, ident))))
479 ;;;----------- end of parser -----------------------
482 (defun hif-canonicalize ()
483 "When at beginning of #ifX, returns a canonical (evaluatable)
484 form for the expression."
485 (save-excursion
486 (let ((negate (looking-at hif-ifndef-regexp)))
487 (re-search-forward hif-ifx-regexp)
488 (let* ((expr-string
489 (buffer-substring (point)
490 (progn (skip-chars-forward "^\n\r") (point))))
491 (expr (hif-infix-to-prefix (hif-tokenize expr-string))))
492 ; (message "hif-canonicalized: %s" expr)
493 (if negate
494 (list 'not expr)
495 expr)))))
498 (defun hif-find-any-ifX ()
499 "Position at beginning of next #if, #ifdef, or #ifndef, including one on
500 this line."
501 ; (message "find ifX at %d" (point))
502 (prog1
503 (re-search-forward hif-ifx-regexp (point-max) t)
504 (beginning-of-line)))
507 (defun hif-find-next-relevant ()
508 "Position at beginning of next #ifdef, #ifndef, #else, #endif,
509 NOT including one on this line."
510 ; (message "hif-find-next-relevant at %d" (point))
511 (end-of-line)
512 ; avoid infinite recursion by only going to beginning of line if match found
513 (if (re-search-forward hif-ifx-else-endif-regexp (point-max) t)
514 (beginning-of-line)))
516 (defun hif-find-previous-relevant ()
517 "Position at beginning of previous #ifdef, #ifndef, #else, #endif,
518 NOT including one on this line."
519 ; (message "hif-find-previous-relevant at %d" (point))
520 (beginning-of-line)
521 ; avoid infinite recursion by only going to beginning of line if match found
522 (if (re-search-backward hif-ifx-else-endif-regexp (point-min) t)
523 (beginning-of-line)))
526 (defun hif-looking-at-ifX () ;; Should eventually see #if
527 (looking-at hif-ifx-regexp))
528 (defun hif-looking-at-endif ()
529 (looking-at hif-endif-regexp))
530 (defun hif-looking-at-else ()
531 (looking-at hif-else-regexp))
535 (defun hif-ifdef-to-endif ()
536 "If positioned at #ifX or #else form, skip to corresponding #endif."
537 ; (message "hif-ifdef-to-endif at %d" (point)) (sit-for 1)
538 (hif-find-next-relevant)
539 (cond ((hif-looking-at-ifX)
540 (hif-ifdef-to-endif) ; find endif of nested if
541 (hif-ifdef-to-endif)) ; find outer endif or else
542 ((hif-looking-at-else)
543 (hif-ifdef-to-endif)) ; find endif following else
544 ((hif-looking-at-endif)
545 'done)
547 (error "Missmatched #ifdef #endif pair"))))
550 (defun hif-endif-to-ifdef ()
551 "If positioned at #endif form, skip backward to corresponding #ifX."
552 ; (message "hif-endif-to-ifdef at %d" (point))
553 (let ((start (point)))
554 (hif-find-previous-relevant)
555 (if (= start (point))
556 (error "Missmatched #ifdef #endif pair")))
557 (cond ((hif-looking-at-endif)
558 (hif-endif-to-ifdef) ; find beginning of nested if
559 (hif-endif-to-ifdef)) ; find beginning of outer if or else
560 ((hif-looking-at-else)
561 (hif-endif-to-ifdef))
562 ((hif-looking-at-ifX)
563 'done)
564 (t ; never gets here)))
567 (defun forward-ifdef (&optional arg)
568 "Move point to beginning of line of the next ifdef-endif.
569 With argument, do this that many times."
570 (interactive "p")
571 (or arg (setq arg 1))
572 (if (< arg 0)
573 (backward-ifdef (- arg)))
574 (while (< 0 arg)
575 (setq arg (- arg))
576 (let ((start (point)))
577 (if (not (hif-looking-at-ifX))
578 (hif-find-next-relevant))
579 (if (hif-looking-at-ifX)
580 (hif-ifdef-to-endif)
581 (goto-char start)
582 (error "No following #ifdef")
583 ))))
586 (defun backward-ifdef (&optional arg)
587 "Move point to beginning of the previous ifdef-endif.
588 With argument, do this that many times."
589 (interactive "p")
590 (or arg (setq arg 1))
591 (if (< arg 0)
592 (forward-ifdef (- arg)))
593 (while (< 0 arg)
594 (setq arg (1- arg))
595 (beginning-of-line)
596 (let ((start (point)))
597 (if (not (hif-looking-at-endif))
598 (hif-find-previous-relevant))
599 (if (hif-looking-at-endif)
600 (hif-endif-to-ifdef)
601 (goto-char start)
602 (error "No previous #ifdef")))))
605 (defun down-ifdef ()
606 "Move point to beginning of nested ifdef or else-part."
607 (interactive)
608 (let ((start (point)))
609 (hif-find-next-relevant)
610 (if (or (hif-looking-at-ifX) (hif-looking-at-else))
612 (goto-char start)
613 (error "No following #ifdef"))))
616 (defun up-ifdef ()
617 "Move point to beginning of enclosing ifdef or else-part."
618 (interactive)
619 (beginning-of-line)
620 (let ((start (point)))
621 (if (not (hif-looking-at-endif))
622 (hif-find-previous-relevant))
623 (if (hif-looking-at-endif)
624 (hif-endif-to-ifdef))
625 (if (= start (point))
626 (error "No previous #ifdef"))))
628 (defun next-ifdef (&optional arg)
629 "Move to the beginning of the next #ifX, #else, or #endif.
630 With argument, do this that many times."
631 (interactive "p")
632 (or arg (setq arg 1))
633 (if (< arg 0)
634 (previous-ifdef (- arg)))
635 (while (< 0 arg)
636 (setq arg (1- arg))
637 (hif-find-next-relevant)
638 (if (eolp)
639 (progn
640 (beginning-of-line)
641 (error "No following #ifdefs, #elses, or #endifs")))))
643 (defun previous-ifdef (&optional arg)
644 "Move to the beginning of the previous #ifX, #else, or #endif.
645 With argument, do this that many times."
646 (interactive "p")
647 (or arg (setq arg 1))
648 (if (< arg 0)
649 (next-ifdef (- arg)))
650 (while (< 0 arg)
651 (setq arg (1- arg))
652 (let ((start (point)))
653 (hif-find-previous-relevant)
654 (if (= start (point))
655 (error "No previous #ifdefs, #elses, or #endifs")
656 ))))
659 ;===%%SF%% parsing (End) ===
662 ;===%%SF%% hide-ifdef-hiding (Start) ===
665 ;;; A range is a structure with four components:
666 ;;; ELSE-P True if there was an else clause for the ifdef.
667 ;;; START The start of the range. (beginning of line)
668 ;;; ELSE The else marker (beginning of line)
669 ;;; Only valid if ELSE-P is true.
670 ;;; END The end of the range. (beginning of line)
672 (defun hif-make-range (else-p start end &optional else)
673 (list else-p start else end))
675 (defun hif-range-else-p (range) (elt range 0))
676 (defun hif-range-start (range) (elt range 1))
677 (defun hif-range-else (range) (elt range 2))
678 (defun hif-range-end (range) (elt range 3))
682 ;;; Find-Range
683 ;;; The workhorse, it delimits the #if region. Reasonably simple:
684 ;;; Skip until an #else or #endif is found, remembering positions. If
685 ;;; an #else was found, skip some more, looking for the true #endif.
687 (defun hif-find-range ()
688 "Returns a Range structure describing the current #if region.
689 Point is left unchanged."
690 ; (message "hif-find-range at %d" (point))
691 (save-excursion
692 (beginning-of-line)
693 (let ((start (point))
694 (else-p nil)
695 (else nil)
696 (end nil))
697 ;; Part one. Look for either #endif or #else.
698 ;; This loop-and-a-half dedicated to E. Dijkstra.
699 (hif-find-next-relevant)
700 (while (hif-looking-at-ifX) ; Skip nested ifdef
701 (hif-ifdef-to-endif)
702 (hif-find-next-relevant))
703 ;; Found either a #else or an #endif.
704 (cond ((hif-looking-at-else)
705 (setq else-p t)
706 (setq else (point)))
708 (setq end (point)) ; (save-excursion (end-of-line) (point))
710 ;; If found #else, look for #endif.
711 (if else-p
712 (progn
713 (hif-find-next-relevant)
714 (while (hif-looking-at-ifX) ; Skip nested ifdef
715 (hif-ifdef-to-endif)
716 (hif-find-next-relevant))
717 (if (hif-looking-at-else)
718 (error "Found two elses in a row? Broken!"))
719 (setq end (point)) ; (save-excursion (end-of-line) (point))
721 (hif-make-range else-p start end else))))
724 ;;; A bit slimy.
725 ;;; NOTE: If there's an #ifdef at the beginning of the file, we can't
726 ;;; hide it. There's no previous newline to replace. If we added
727 ;;; one, we'd throw off all the counts. Feh.
729 (defun hif-hide-line (point)
730 "Hide the line containing point. Does nothing if `hide-ifdef-lines' is nil."
731 (if hide-ifdef-lines
732 (save-excursion
733 (goto-char point)
734 (let ((modp (buffer-modified-p)))
735 (unwind-protect
736 (progn
737 (beginning-of-line)
738 (if (not (= (point) 1))
739 (hide-ifdef-region (1- (point)) (point))))
740 (set-buffer-modified-p modp))
745 ;;; Hif-Possibly-Hide
746 ;;; There are four cases. The #ifX expression is "taken" if it
747 ;;; the hide-ifdef-evaluator returns T. Presumably, this means the code
748 ;;; inside the #ifdef would be included when the program was
749 ;;; compiled.
751 ;;; Case 1: #ifX taken, and there's an #else.
752 ;;; The #else part must be hidden. The #if (then) part must be
753 ;;; processed for nested #ifX's.
754 ;;; Case 2: #ifX taken, and there's no #else.
755 ;;; The #if part must be processed for nested #ifX's.
756 ;;; Case 3: #ifX not taken, and there's an #else.
757 ;;; The #if part must be hidden. The #else part must be processed
758 ;;; for nested #ifs.
759 ;;; Case 4: #ifX not taken, and there's no #else.
760 ;;; The #ifX part must be hidden.
762 ;;; Further processing is done by narrowing to the relevant region
763 ;;; and just recursively calling hide-ifdef-guts.
765 ;;; When hif-possibly-hide returns, point is at the end of the
766 ;;; possibly-hidden range.
768 (defun hif-recurse-on (start end)
769 "Call `hide-ifdef-guts' after narrowing to end of START line and END line."
770 (save-excursion
771 (save-restriction
772 (goto-char start)
773 (end-of-line)
774 (narrow-to-region (point) end)
775 (hide-ifdef-guts))))
777 (defun hif-possibly-hide ()
778 "Called at #ifX expression, this hides those parts that should be
779 hidden, according to judgement of `hide-ifdef-evaluator'."
780 ; (message "hif-possibly-hide") (sit-for 1)
781 (let ((test (hif-canonicalize))
782 (range (hif-find-range)))
783 ; (message "test = %s" test) (sit-for 1)
785 (hif-hide-line (hif-range-end range))
786 (if (funcall hide-ifdef-evaluator test)
787 (cond ((hif-range-else-p range) ; case 1
788 (hif-hide-line (hif-range-else range))
789 (hide-ifdef-region (hif-range-else range)
790 (1- (hif-range-end range)))
791 (hif-recurse-on (hif-range-start range)
792 (hif-range-else range)))
793 (t ; case 2
794 (hif-recurse-on (hif-range-start range)
795 (hif-range-end range))))
796 (cond ((hif-range-else-p range) ; case 3
797 (hif-hide-line (hif-range-else range))
798 (hide-ifdef-region (hif-range-start range)
799 (1- (hif-range-else range)))
800 (hif-recurse-on (hif-range-else range)
801 (hif-range-end range)))
802 (t ; case 4
803 (hide-ifdef-region (point)
804 (1- (hif-range-end range))))
806 (hif-hide-line (hif-range-start range)) ; Always hide start.
807 (goto-char (hif-range-end range))
808 (end-of-line)
813 (defun hide-ifdef-guts ()
814 "Does the work of `hide-ifdefs', except for the work that's pointless
815 to redo on a recursive entry."
816 ; (message "hide-ifdef-guts")
817 (save-excursion
818 (goto-char (point-min))
819 (while (hif-find-any-ifX)
820 (hif-possibly-hide))))
822 ;===%%SF%% hide-ifdef-hiding (End) ===
825 ;===%%SF%% exports (Start) ===
827 (defvar hide-ifdef-initially nil
828 "*Non-nil if `hide-ifdefs' should be called when Hide-Ifdef mode
829 is first activated.")
831 (defvar hide-ifdef-hiding nil
832 "Non-nil if text might be hidden.")
834 (defvar hide-ifdef-read-only nil
835 "*Set to non-nil if you want buffer to be read-only while hiding text.")
837 (defvar hif-outside-read-only nil
838 "Internal variable. Saves the value of `buffer-read-only' while hiding.")
840 (defvar hide-ifdef-lines nil
841 "*Set to t if you don't want to see the #ifX, #else, and #endif lines.")
843 (defun hide-ifdef-toggle-read-only ()
844 "Toggle hide-ifdef-read-only."
845 (interactive)
846 (setq hide-ifdef-read-only (not hide-ifdef-read-only))
847 (message "Hide-Read-Only %s"
848 (if hide-ifdef-read-only "ON" "OFF"))
849 (if hide-ifdef-hiding
850 (setq buffer-read-only (or hide-ifdef-read-only hif-outside-read-only)))
851 (hif-update-mode-line))
853 (defun hide-ifdef-toggle-outside-read-only ()
854 "Replacement for `toggle-read-only' within Hide-Ifdef mode."
855 (interactive)
856 (setq hif-outside-read-only (not hif-outside-read-only))
857 (message "Read only %s"
858 (if hif-outside-read-only "ON" "OFF"))
859 (setq buffer-read-only
860 (or (and hide-ifdef-hiding hide-ifdef-read-only)
861 hif-outside-read-only)
863 (hif-update-mode-line))
866 (defun hide-ifdef-define (var)
867 "Define a VAR so that #ifdef VAR would be included."
868 (interactive "SDefine what? ")
869 (hif-set-var var t)
870 (if hide-ifdef-hiding (hide-ifdefs)))
872 (defun hide-ifdef-undef (var)
873 "Undefine a VAR so that #ifdef VAR would not be included."
874 (interactive "SUndefine what? ")
875 (hif-set-var var nil)
876 (if hide-ifdef-hiding (hide-ifdefs)))
879 (defun hide-ifdefs ()
880 "Hide the contents of some #ifdefs. Assume that defined symbols have
881 been added to `hide-ifdef-env'. The text hidden is the text that would not
882 be included by the C preprocessor if it were given the file with those
883 symbols defined.
885 Turn off hiding by calling show-ifdef."
887 (interactive)
888 (message "Hiding...")
889 (if (not hide-ifdef-mode)
890 (hide-ifdef-mode 1)) ; turn on hide-ifdef-mode
891 (if hide-ifdef-hiding
892 (show-ifdefs)) ; Otherwise, deep confusion.
893 (if buffer-read-only (toggle-read-only)) ; make it writable temporarily
894 (setq selective-display t)
895 (setq hide-ifdef-hiding t)
896 (hide-ifdef-guts)
897 (if (or hide-ifdef-read-only hif-outside-read-only)
898 (toggle-read-only)) ; make it read only
899 (message "Hiding done"))
902 (defun show-ifdefs ()
903 "Cancel the effects of hide-ifdef. The contents of all #ifdefs is shown."
904 (interactive)
905 (if buffer-read-only (toggle-read-only)) ; make it writable temporarily
906 (setq selective-display nil) ; defaults
907 (hif-show-all)
908 (if hif-outside-read-only
909 (toggle-read-only)) ; make it read only
910 (setq hide-ifdef-hiding nil))
913 (defun hif-find-ifdef-block ()
914 "Utilitiy for hide and show ifdef-block. Set top and bottom of ifdef block."
915 (let (max-bottom)
916 (save-excursion
917 (beginning-of-line)
918 (if (not (or (hif-looking-at-else) (hif-looking-at-ifX)))
919 (up-ifdef))
920 (setq top (point))
921 (hif-ifdef-to-endif)
922 (setq max-bottom (1- (point))))
923 (save-excursion
924 (beginning-of-line)
925 (if (not (hif-looking-at-endif))
926 (hif-find-next-relevant))
927 (while (hif-looking-at-ifX)
928 (hif-ifdef-to-endif)
929 (hif-find-next-relevant))
930 (setq bottom (min max-bottom (1- (point))))))
934 (defun hide-ifdef-block ()
935 "Hide the ifdef block (true or false part) enclosing or before the cursor."
936 (interactive)
937 (if (not hide-ifdef-mode)
938 (hide-ifdef-mode 1))
939 (if buffer-read-only (toggle-read-only))
940 (setq selective-display t)
941 (let (top bottom)
942 (hif-find-ifdef-block) ; set top and bottom - dynamic scoping
943 (hide-ifdef-region top bottom)
944 (if hide-ifdef-lines
945 (progn
946 (hif-hide-line top)
947 (hif-hide-line (1+ bottom))))
948 (setq hide-ifdef-hiding t))
949 (if (or hide-ifdef-read-only hif-outside-read-only)
950 (toggle-read-only)))
953 (defun show-ifdef-block ()
954 "Show the ifdef block (true or false part) enclosing or before the cursor."
955 (interactive)
956 (let ((old-read-only buffer-read-only))
957 (if old-read-only (toggle-read-only))
958 (if hide-ifdef-lines
959 (save-excursion
960 (beginning-of-line)
961 (hif-show-ifdef-region (1- (point)) (progn (end-of-line) (point))))
963 (let (top bottom)
964 (hif-find-ifdef-block)
965 (hif-show-ifdef-region (1- top) bottom))
968 ; restore read only status since we dont know if all is shown.
969 (if old-read-only (toggle-read-only))))
972 ;;; defininition alist support
974 (defvar hide-ifdef-define-alist nil
975 "A global assoc list of pre-defined symbol lists")
977 (defun hif-compress-define-list (env)
978 "Compress the define list ENV into a list of defined symbols only."
979 (let ((defs (mapcar '(lambda (arg)
980 (if (hif-lookup (car arg)) (car arg)))
981 env))
982 (new-defs nil))
983 (while defs
984 (if (car defs)
985 (setq new-defs (cons (car defs) new-defs)))
986 (setq defs (cdr defs)))
987 new-defs))
989 (defun hide-ifdef-set-define-alist (name)
990 "Set the association for NAME to hide-ifdef-env."
991 (interactive "SSet define list: ")
992 (setq hide-ifdef-define-alist
993 (cons (cons name (hif-compress-define-list hide-ifdef-env))
994 hide-ifdef-define-alist)))
996 (defun hide-ifdef-use-define-alist (name)
997 "Set hide-ifdef-env to the define list specified by NAME."
998 (interactive "SUse define list: ")
999 (let ((define-list (assoc name hide-ifdef-define-alist)))
1000 (if define-list
1001 (setq hide-ifdef-env
1002 (mapcar '(lambda (arg) (cons arg t))
1003 (cdr define-list)))
1004 (error "No define list for %s" name))
1005 (if hide-ifdef-hiding (hide-ifdefs))))
1007 ;;; hideif.el ends here