home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / emacs-19.28-src.tgz / tar.out / fsf / emacs / lisp / hideif.el < prev    next >
Lisp/Scheme  |  1996-09-28  |  33KB  |  1,033 lines

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