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