home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / lucid / lemacs-19.6 / lisp / modes / hideif.el < prev    next >
Encoding:
Text File  |  1992-08-18  |  31.5 KB  |  1,039 lines

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