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