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