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