Some fixes to follow coding conventions in files maintained by FSF.
[bpt/emacs.git] / lisp / progmodes / hideif.el
index a28c43a..37f95f2 100644 (file)
@@ -1,8 +1,8 @@
-;;; hide-ifdef-mode.el --- hides selected code within ifdef.
+;;; hideif.el --- hides selected code within ifdef
 
-;;; Copyright (C) 1988, 1994 Free Software Foundation, Inc.
+;; Copyright (C) 1988, 1994 Free Software Foundation, Inc.
 
-;; Author: Dan LaLiberte <liberte@a.cs.uiuc.edu>
+;; Author: Daniel LaLiberte <liberte@holonexus.org>
 ;; Maintainer: FSF
 ;; Keywords: c, outlines
 
 ;; GNU General Public License for more details.
 
 ;; You should have received a copy of the GNU General Public License
-;; along with GNU Emacs; see the file COPYING.  If not, write to
-;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
+;; along with GNU Emacs; see the file COPYING.  If not, write to the
+;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+;; Boston, MA 02111-1307, USA.
 
 ;;; Commentary:
 
-;;; To initialize, toggle the hide-ifdef minor mode with
-;;;
-;;; M-x hide-ifdef-mode
-;;;
-;;; This will set up key bindings and call hide-ifdef-mode-hook if it
-;;; has a value.  To explicitly hide ifdefs using a buffer-local
-;;; define list (default empty), type
-;;;
-;;; M-x hide-ifdefs  or C-c @ h
-;;;
-;;; Hide-ifdef suppresses the display of code that the preprocessor wouldn't
-;;; pass through.  The support of constant expressions in #if lines is 
-;;; limited to identifiers, parens, and the operators: &&, ||, !, and
-;;; "defined".  Please extend this.
-;;;
-;;; The hidden code is marked by ellipses (...).  Be
-;;; cautious when editing near ellipses, since the hidden text is
-;;; still in the buffer, and you can move the point into it and modify
-;;; text unawares.  If you don't want to see the ellipses, set 
-;;; selective-display-ellipses to nil.  But this can be dangerous.
-;;; You can make your buffer read-only while hide-ifdef-hiding by setting
-;;; hide-ifdef-read-only to a non-nil value.  You can toggle this 
-;;; variable with hide-ifdef-toggle-read-only (C-c @ C-q).
-;;;
-;;; You can undo the effect of hide-ifdefs by typing
-;;;
-;;; M-x show-ifdefs  or C-c @ s
-;;;
-;;; Use M-x hide-ifdef-define (C-c @ d) to define a symbol.
-;;; Use M-x hide-ifdef-undef (C-c @ u) to undefine a symbol.
-;;;
-;;; If you define or undefine a symbol while hide-ifdef-mode is in effect,
-;;; the display will be updated.  Only the define list for the current
-;;; buffer will be affected.  You can save changes to the local define
-;;; list with hide-ifdef-set-define-alist.  This adds entries 
-;;; to hide-ifdef-define-alist.
-;;;
-;;; If you have defined a hide-ifdef-mode-hook, you can set
-;;; up a list of symbols that may be used by hide-ifdefs as in the
-;;; following example:
-;;;
-;;; (setq hide-ifdef-mode-hook
-;;;      '(lambda ()
-;;;     (if (not hide-ifdef-define-alist)
-;;;         (setq hide-ifdef-define-alist
-;;;              '((list1 ONE TWO)
-;;;                (list2 TWO THREE)
-;;;                )))
-;;;     (hide-ifdef-use-define-alist 'list2) ; use list2 by default
-;;;     ))
-;;;
-;;; You can call hide-ifdef-use-define-alist (C-c @ u) at any time to specify
-;;; another list to use.
-;;;
-;;; To cause ifdefs to be hidden as soon as hide-ifdef-mode is called,
-;;; set hide-ifdef-initially to non-nil.
-;;;
-;;; If you set hide-ifdef-lines to t, hide-ifdefs hides all the #ifdef lines.
-;;; In the absence of highlighting, that might be a bad idea.  If you set
-;;; hide-ifdef-lines to nil (the default), the surrounding preprocessor
-;;; lines will be displayed.  That can be confusing in its own
-;;; right.  Other variations on display are possible, but not much
-;;; better.
-;;;
-;;; You can explicitly hide or show individual ifdef blocks irrespective
-;;; of the define list by using hide-ifdef-block and show-ifdef-block.
-;;;
-;;; You can move the point between ifdefs with forward-ifdef, backward-ifdef,
-;;; up-ifdef, down-ifdef, next-ifdef, and previous-ifdef.
-;;;
-;;; If you have minor-mode-alist in your mode line (the default) two labels
-;;; may appear.  "Ifdef" will appear when hide-ifdef-mode is active.  "Hiding"
-;;; will appear when text may be hidden ("hide-ifdef-hiding" is non-nil).
-;;;
-;;; Written by Brian Marick, at Gould, Computer Systems Division, Urbana IL.
-;;; Extensively modified by Daniel LaLiberte (while at Gould).
-;;;
-;;; You may freely modify and distribute this, but keep a record
-;;; of modifications and send comments to:
-;;;     liberte@a.cs.uiuc.edu  or  ihnp4!uiucdcs!liberte
-;;; I will continue to upgrade hide-ifdef-mode
-;;; with your contributions.
+;; To initialize, toggle the hide-ifdef minor mode with
+;;
+;; M-x hide-ifdef-mode
+;;
+;; This will set up key bindings and call hide-ifdef-mode-hook if it
+;; has a value.  To explicitly hide ifdefs using a buffer-local
+;; define list (default empty), type
+;;
+;; M-x hide-ifdefs  or C-c @ h
+;;
+;; Hide-ifdef suppresses the display of code that the preprocessor wouldn't
+;; pass through.  The support of constant expressions in #if lines is 
+;; limited to identifiers, parens, and the operators: &&, ||, !, and
+;; "defined".  Please extend this.
+;;
+;; The hidden code is marked by ellipses (...).  Be
+;; cautious when editing near ellipses, since the hidden text is
+;; still in the buffer, and you can move the point into it and modify
+;; text unawares.  If you don't want to see the ellipses, set 
+;; selective-display-ellipses to nil.  But this can be dangerous.
+;; You can make your buffer read-only while hide-ifdef-hiding by setting
+;; hide-ifdef-read-only to a non-nil value.  You can toggle this 
+;; variable with hide-ifdef-toggle-read-only (C-c @ C-q).
+;;
+;; You can undo the effect of hide-ifdefs by typing
+;;
+;; M-x show-ifdefs  or C-c @ s
+;;
+;; Use M-x hide-ifdef-define (C-c @ d) to define a symbol.
+;; Use M-x hide-ifdef-undef (C-c @ u) to undefine a symbol.
+;;
+;; If you define or undefine a symbol while hide-ifdef-mode is in effect,
+;; the display will be updated.  Only the define list for the current
+;; buffer will be affected.  You can save changes to the local define
+;; list with hide-ifdef-set-define-alist.  This adds entries 
+;; to hide-ifdef-define-alist.
+;;
+;; If you have defined a hide-ifdef-mode-hook, you can set
+;; up a list of symbols that may be used by hide-ifdefs as in the
+;; following example:
+;;
+;; (setq hide-ifdef-mode-hook
+;;      (lambda ()
+;;      (if (not hide-ifdef-define-alist)
+;;          (setq hide-ifdef-define-alist
+;;               '((list1 ONE TWO)
+;;                 (list2 TWO THREE)
+;;                 )))
+;;      (hide-ifdef-use-define-alist 'list2) ; use list2 by default
+;;      ))
+;;
+;; You can call hide-ifdef-use-define-alist (C-c @ U) at any time to specify
+;; another list to use.
+;;
+;; To cause ifdefs to be hidden as soon as hide-ifdef-mode is called,
+;; set hide-ifdef-initially to non-nil.
+;;
+;; If you set hide-ifdef-lines to t, hide-ifdefs hides all the #ifdef lines.
+;; In the absence of highlighting, that might be a bad idea.  If you set
+;; hide-ifdef-lines to nil (the default), the surrounding preprocessor
+;; lines will be displayed.  That can be confusing in its own
+;; right.  Other variations on display are possible, but not much
+;; better.
+;;
+;; You can explicitly hide or show individual ifdef blocks irrespective
+;; of the define list by using hide-ifdef-block and show-ifdef-block.
+;;
+;; You can move the point between ifdefs with forward-ifdef, backward-ifdef,
+;; up-ifdef, down-ifdef, next-ifdef, and previous-ifdef.
+;;
+;; If you have minor-mode-alist in your mode line (the default) two labels
+;; may appear.  "Ifdef" will appear when hide-ifdef-mode is active.  "Hiding"
+;; will appear when text may be hidden ("hide-ifdef-hiding" is non-nil).
+;;
+;; Written by Brian Marick, at Gould, Computer Systems Division, Urbana IL.
+;; Extensively modified by Daniel LaLiberte (while at Gould).
+;;
+;; You may freely modify and distribute this, but keep a record
+;; of modifications and send comments to:
+;;      liberte@a.cs.uiuc.edu  or  ihnp4!uiucdcs!liberte
+;; I will continue to upgrade hide-ifdef-mode
+;; with your contributions.
 
 ;;; Code:
 
+(require 'cc-mode)
+
+(defgroup hide-ifdef nil
+  "Hide selected code within `ifdef'."
+  :group 'c)
+
 (defvar hide-ifdef-mode-submap nil
   "Keymap used with Hide-Ifdef mode.")
 
 
 ;; Set up the submap that goes after the prefix key.
 (if hide-ifdef-mode-submap
-    ()                         ; dont redefine it.
+    ()                         ; Don't redefine it.
   (setq hide-ifdef-mode-submap (make-sparse-keymap))
   (define-key hide-ifdef-mode-submap "d" 'hide-ifdef-define)
   (define-key hide-ifdef-mode-submap "u" 'hide-ifdef-undef)
   (define-key hide-ifdef-mode-map hide-ifdef-mode-prefix-key
     hide-ifdef-mode-submap))
 
+;; Autoload for the benefit of `make-mode-line-mouse-sensitive'.
+;;;###autoload
 (defvar hide-ifdef-mode nil
   "Non-nil when hide-ifdef-mode is activated.")
 
 (modify-syntax-entry ?& "." hide-ifdef-syntax-table)
 (modify-syntax-entry ?\| "." hide-ifdef-syntax-table)
 
+(defvar hide-ifdef-env nil
+  "An alist of defined symbols and their values.")
+
+(defvar hif-outside-read-only nil
+  "Internal variable.  Saves the value of `buffer-read-only' while hiding.")
+
 ;;;###autoload
 (defun hide-ifdef-mode (arg)
   "Toggle Hide-Ifdef mode.  This is a minor mode, albeit a large one.
-With ARG, turn Hide-Ifdef mode on iff arg is positive.
+With ARG, turn Hide-Ifdef mode on if arg is positive, off otherwise.
 In Hide-Ifdef mode, code within #ifdef constructs that the C preprocessor
 would eliminate may be hidden from view.  Several variables affect
 how the hiding is done:
@@ -237,19 +252,19 @@ hide-ifdef-read-only
        (if hide-ifdef-initially
            (hide-ifdefs)
          (show-ifdefs))
-       (message "Enter hide-ifdef-mode.")
+       (message "Enter Hide-Ifdef mode")
        )
      ; else end hide-ifdef-mode
     (if hide-ifdef-hiding
        (show-ifdefs))
-    (message "Exit hide-ifdef-mode.")
+    (message "Exit Hide-Ifdef mode")
     ))
   
 
 ;; from outline.el with docstring fixed.
 (defun hif-outline-flag-region (from to flag)
-  "Hides or shows lines from FROM to TO, according to FLAG.  If FLAG
-is \\n (newline character) then text is shown, while if FLAG is \\^M
+  "Hides or shows lines from FROM to TO, according to FLAG.
+If FLAG is \\n (newline character) then text is shown, while if FLAG is \\^M
 \(control-M) the text is hidden."
   (let ((modp (buffer-modified-p)))
     (unwind-protect (progn
@@ -264,6 +279,14 @@ is \\n (newline character) then text is shown, while if FLAG is \\^M
   (interactive)
   (hif-outline-flag-region (point-min) (point-max) ?\n))
 
+;; By putting this on after-revert-hook, we arrange that it only
+;; does anything when revert-buffer avoids turning off the mode.
+;; (That can happen in VC.)
+(defun hif-before-revert-function ()
+  (and hide-ifdef-mode hide-ifdef-hiding
+       (hide-ifdefs t)))
+(add-hook 'after-revert-hook 'hif-before-revert-function)
+
 (defun hide-ifdef-region (start end)
   "START is the start of a #if or #else form.  END is the ending part.
 Everything including these lines is made invisible."
@@ -279,16 +302,16 @@ Everything including these lines is made invisible."
 
 ;===%%SF%% evaluation (Start)  ===
 
+;; It is not useful to set this to anything but `eval'.
+;; In fact, the variable might as well be eliminated.
 (defvar hide-ifdef-evaluator 'eval
-  "The evaluator is given a canonical form and returns T if text under
+  "The function to use to evaluate a form.
+The evaluator is given a canonical form and returns t if text under
 that form should be displayed.")
 
 (defvar hif-undefined-symbol nil
   "...is by default considered to be false.")
 
-(defvar hide-ifdef-env nil
-  "An alist of defined symbols and their values.")
-
 
 (defun hif-set-var (var value)
   "Prepend (var value) pair to hide-ifdef-env."
@@ -326,18 +349,22 @@ that form should be displayed.")
 (defconst hif-ifx-else-endif-regexp
   (concat hif-ifx-regexp "\\|" hif-else-regexp "\\|" hif-endif-regexp))
 
+; Used to store the current token and the whole token list during parsing.
+; Only bound dynamically.
+(defvar hif-token)
+(defvar hif-token-list)
 
 (defun hif-infix-to-prefix (token-list)
   "Convert list of tokens in infix into prefix list"
-;  (message "hif-infix-to-prefix: %s" token-list)
+                                       ;  (message "hif-infix-to-prefix: %s" token-list)
   (if (= 1 (length token-list))
-      (` (hif-lookup (quote (, (car token-list)))))
+      `(hif-lookup (quote ,(car token-list)))
     (hif-parse-if-exp token-list))
   )
 
 ; pattern to match initial identifier, !, &&, ||, (, or ).
 ; Added ==, + and -: garyo@avs.com 8/9/94
-(defconst hif-token-regexp "^\\(!\\|&&\\|||\\|[!=]=\\|[()+-]\\|\\w+\\)")
+(defconst hif-token-regexp "^\\(&&\\|||\\|[!=]=\\|!\\|[()+-]\\|[<>]=?\\|\\w+\\)")
 (defconst hif-end-of-comment "\\*/")
 
 
@@ -389,6 +416,10 @@ that form should be displayed.")
                        ((string-equal token "defined") 'hif-defined)
                        ((string-equal token "(") 'lparen)
                        ((string-equal token ")") 'rparen)
+                       ((string-equal token ">") 'hif-greater)
+                       ((string-equal token "<") 'hif-less)
+                       ((string-equal token ">=") 'hif-greater-equal)
+                       ((string-equal token "<=") 'hif-less-equal)
                        ((string-equal token "+") 'hif-plus)
                        ((string-equal token "-") 'hif-minus)
                        (t (intern token)))
@@ -402,99 +433,97 @@ that form should be displayed.")
 ;;; This parser is limited to the operators &&, ||, !, and "defined".
 ;;; Added ==, !=, +, and -.  Gary Oberbrunner, garyo@avs.com, 8/9/94
 
-(defun hif-parse-if-exp (token-list)
+(defun hif-parse-if-exp (hif-token-list)
   "Parse the TOKEN-LIST.  Return translated list in prefix form."
   (hif-nexttoken)
   (prog1
       (hif-expr)
-    (if token ; is there still a token?
-       (error "Error: unexpected token: %s" token))))
+    (if hif-token ; is there still a token?
+       (error "Error: unexpected token: %s" hif-token))))
 
 (defun hif-nexttoken ()
-  "Pop the next token from token-list into the let variable \"token\"."
-  (setq token (car token-list))
-  (setq token-list (cdr token-list))
-  token)
+  "Pop the next token from token-list into the let variable \"hif-token\"."
+  (setq hif-token (car hif-token-list))
+  (setq hif-token-list (cdr hif-token-list))
+  hif-token)
 
 (defun hif-expr ()
-  "Parse and expression of the form
+  "Parse an expression as found in #if.
        expr : term | expr '||' term."
   (let ((result (hif-term)))
-    (while (eq  token 'or)
+    (while (eq hif-token 'or)
       (hif-nexttoken)
       (setq result (list 'or result (hif-term))))
   result))
 
 (defun hif-term ()
-  "Parse a term of the form
-       term : eq-expr | term '&&' eq-expr."
+  "Parse a term : eq-expr | term '&&' eq-expr."
   (let ((result (hif-eq-expr)))
-    (while (eq token 'and)
+    (while (eq hif-token 'and)
       (hif-nexttoken)
       (setq result (list 'and result (hif-eq-expr))))
     result))
 
 (defun hif-eq-expr ()
-  "Parse a term of the form
-       eq-expr : math | eq-expr '=='|'!=' math."
+  "Parse an eq-expr : math | eq-expr `=='|`!='|`<'|`>'|`>='|`<=' math."
   (let ((result (hif-math))
        (eq-token nil))
-    (while (or (eq token 'equal) (eq token 'hif-notequal))
-      (setq eq-token token)
+    (while (memq hif-token '(equal hif-notequal hif-greater hif-less
+                            hif-greater-equal hif-less-equal))
+      (setq eq-token hif-token)
       (hif-nexttoken)
       (setq result (list eq-token result (hif-math))))
     result))
 
 (defun hif-math ()
-  "Parse an expression of the form
+  "Parse an expression with + or - and simpler things.
        math : factor | math '+|-' factor."
   (let ((result (hif-factor))
        (math-op nil))
-    (while (or (eq  token 'hif-plus) (eq token 'hif-minus))
-      (setq math-op token)
+    (while (or (eq hif-token 'hif-plus) (eq hif-token 'hif-minus))
+      (setq math-op hif-token)
       (hif-nexttoken)
       (setq result (list math-op result (hif-factor))))
   result))
   
 (defun hif-factor ()
-  "Parse a factor of the form
-       factor : '!' factor | '(' expr ')' | 'defined(' id ')' | id."
+  "Parse a factor: '!' factor | '(' expr ')' | 'defined(' id ')' | id."
   (cond
-    ((eq token 'not)
-     (hif-nexttoken)
-     (list 'not (hif-factor)))
-
-    ((eq token 'lparen)
-     (hif-nexttoken)
-     (let ((result (hif-expr)))
-       (if (not (eq token 'rparen))
-          (error "Bad token in parenthesized expression: %s" token)
-        (hif-nexttoken)
-        result)))
-
-    ((eq token 'hif-defined)
-     (hif-nexttoken)
-     (if (not (eq token 'lparen))
-        (error "Error: expected \"(\" after \"defined\""))
-     (hif-nexttoken)
-     (let ((ident token))
-       (if (memq token '(or and not hif-defined lparen rparen))
-          (error "Error: unexpected token: %s" token))
-       (hif-nexttoken)
-       (if (not (eq token 'rparen))
-          (error "Error: expected \")\" after identifier"))
-       (hif-nexttoken)
-       (` (hif-defined (quote (, ident))))
-       ))
-
-    (t ; identifier
-      (let ((ident token))
-       (if (memq ident '(or and))
-           (error "Error: missing identifier"))
+   ((eq hif-token 'not)
+    (hif-nexttoken)
+    (list 'not (hif-factor)))
+
+   ((eq hif-token 'lparen)
+    (hif-nexttoken)
+    (let ((result (hif-expr)))
+      (if (not (eq hif-token 'rparen))
+         (error "Bad token in parenthesized expression: %s" hif-token)
        (hif-nexttoken)
-       (` (hif-lookup (quote (, ident))))
-       ))
-    ))
+       result)))
+
+   ((eq hif-token 'hif-defined)
+    (hif-nexttoken)
+    (if (not (eq hif-token 'lparen))
+       (error "Error: expected \"(\" after \"defined\""))
+    (hif-nexttoken)
+    (let ((ident hif-token))
+      (if (memq hif-token '(or and not hif-defined lparen rparen))
+         (error "Error: unexpected token: %s" hif-token))
+      (hif-nexttoken)
+      (if (not (eq hif-token 'rparen))
+         (error "Error: expected \")\" after identifier"))
+      (hif-nexttoken)
+      `(hif-defined (quote ,ident))
+      ))
+
+   (t                                  ; identifier
+    (let ((ident hif-token))
+      (if (memq ident '(or and))
+         (error "Error: missing identifier"))
+      (hif-nexttoken)
+      `(hif-lookup (quote ,ident))
+      ))
+   ))
 
 (defun hif-mathify (val)
   "Treat VAL as a number: if it's t or nil, use 1 or 0."
@@ -513,13 +542,23 @@ that form should be displayed.")
 (defun hif-notequal (a b)
   "Like (not (equal A B)) but as one symbol."
   (not (equal a b)))
-
+(defun hif-greater (a b)
+  "Simple comparison."
+  (> (hif-mathify a) (hif-mathify b)))
+(defun hif-less (a b)
+  "Simple comparison."
+  (< (hif-mathify a) (hif-mathify b)))
+(defun hif-greater-equal (a b)
+  "Simple comparison."
+  (>= (hif-mathify a) (hif-mathify b)))
+(defun hif-less-equal (a b)
+  "Simple comparison."
+  (<= (hif-mathify a) (hif-mathify b)))
 ;;;----------- end of parser -----------------------
 
 
 (defun hif-canonicalize ()
-  "When at beginning of #ifX, returns a canonical (evaluatable)
-       form for the expression."
+  "When at beginning of #ifX, returns a Lisp expression for its condition."
   (save-excursion
     (let ((negate (looking-at hif-ifndef-regexp)))
       (re-search-forward hif-ifx-regexp)
@@ -534,8 +573,7 @@ that form should be displayed.")
 
 
 (defun hif-find-any-ifX ()
-  "Position at beginning of next #if, #ifdef, or #ifndef, including one on
-this line."
+  "Move to next #if..., or #ifndef, at point or after."
 ;  (message "find ifX at %d" (point))
   (prog1
       (re-search-forward hif-ifx-regexp (point-max) t)
@@ -543,8 +581,7 @@ this line."
 
 
 (defun hif-find-next-relevant ()
-  "Position at beginning of next #ifdef, #ifndef, #else, #endif,
-NOT including one on this line."
+  "Move to next #if..., #else, or #endif, after the current line."
 ;  (message "hif-find-next-relevant at %d" (point))
   (end-of-line)
   ; avoid infinite recursion by only going to beginning of line if match found
@@ -552,8 +589,7 @@ NOT including one on this line."
       (beginning-of-line)))
 
 (defun hif-find-previous-relevant ()
-  "Position at beginning of previous #ifdef, #ifndef, #else, #endif,
-NOT including one on this line."
+  "Move to previous #if..., #else, or #endif, before the current line."
 ;  (message "hif-find-previous-relevant at %d" (point))
   (beginning-of-line)
   ; avoid infinite recursion by only going to beginning of line if match found
@@ -813,8 +849,8 @@ Point is left unchanged."
       (hide-ifdef-guts))))
 
 (defun hif-possibly-hide ()
-  "Called at #ifX expression, this hides those parts that should be
-hidden, according to judgement of `hide-ifdef-evaluator'."
+  "Called at #ifX expression, this hides those parts that should be hidden.
+It uses the judgement of `hide-ifdef-evaluator'."
 ;  (message "hif-possibly-hide") (sit-for 1)
     (let ((test (hif-canonicalize))
          (range (hif-find-range)))
@@ -849,8 +885,8 @@ hidden, according to judgement of `hide-ifdef-evaluator'."
 
 
 (defun hide-ifdef-guts ()
-  "Does the work of `hide-ifdefs', except for the work that's pointless
-to redo on a recursive entry."
+  "Does most of the work of `hide-ifdefs'.
+It does not do the work that's pointless to redo on a recursive entry."
 ;  (message "hide-ifdef-guts")
   (save-excursion
     (goto-char (point-min))
@@ -863,23 +899,22 @@ to redo on a recursive entry."
 ;===%%SF%% exports (Start)  ===
 
 ;;;###autoload
-(defvar hide-ifdef-initially nil
-  "*Non-nil if `hide-ifdefs' should be called when Hide-Ifdef mode
-is first activated.")
-
-(defvar hide-ifdef-hiding nil
-  "Non-nil if text might be hidden.")
+(defcustom hide-ifdef-initially nil
+  "*Non-nil means call `hide-ifdefs' when Hide-Ifdef mode is first activated."
+  :type 'boolean
+  :group 'hide-ifdef)
 
 ;;;###autoload
-(defvar hide-ifdef-read-only nil
-  "*Set to non-nil if you want buffer to be read-only while hiding text.")
-
-(defvar hif-outside-read-only nil
-  "Internal variable.  Saves the value of `buffer-read-only' while hiding.")
+(defcustom hide-ifdef-read-only nil
+  "*Set to non-nil if you want buffer to be read-only while hiding text."
+  :type 'boolean
+  :group 'hide-ifdef)
 
 ;;;###autoload
-(defvar hide-ifdef-lines nil
-  "*Set to t if you don't want to see the #ifX, #else, and #endif lines.")
+(defcustom hide-ifdef-lines nil
+  "*Non-nil means hide the #ifX, #else, and #endif lines."
+  :type 'boolean
+  :group 'hide-ifdef)
 
 (defun hide-ifdef-toggle-read-only ()
   "Toggle hide-ifdef-read-only."
@@ -892,7 +927,7 @@ is first activated.")
   (force-mode-line-update))
 
 (defun hide-ifdef-toggle-outside-read-only ()
-  "Replacement for `toggle-read-only' within Hide Ifdef mode."
+  "Replacement for `toggle-read-only' within Hide-Ifdef mode."
   (interactive)
   (setq hif-outside-read-only (not hif-outside-read-only))
   (message "Read only %s"
@@ -917,7 +952,7 @@ is first activated.")
   (if hide-ifdef-hiding (hide-ifdefs)))
 
 
-(defun hide-ifdefs ()
+(defun hide-ifdefs (&optional nomsg)
   "Hide the contents of some #ifdefs.  
 Assume that defined symbols have been added to `hide-ifdef-env'.  
 The text hidden is the text that would not be included by the C
@@ -927,6 +962,7 @@ Turn off hiding by calling `show-ifdefs'."
 
   (interactive)
   (message "Hiding...")
+  (setq hif-outside-read-only buffer-read-only)
   (if (not hide-ifdef-mode)
       (hide-ifdef-mode 1)) ; turn on hide-ifdef-mode
   (if hide-ifdef-hiding
@@ -936,11 +972,12 @@ Turn off hiding by calling `show-ifdefs'."
     (setq hide-ifdef-hiding t)
     (hide-ifdef-guts))
   (setq buffer-read-only (or hide-ifdef-read-only hif-outside-read-only))
-  (message "Hiding done"))
+  (or nomsg
+      (message "Hiding done")))
 
 
 (defun show-ifdefs ()
-  "Cancel the effects of `hide-ifdef'.  The contents of all #ifdefs is shown."
+  "Cancel the effects of `hide-ifdef': show the contents of all #ifdefs."
   (interactive)
   (setq buffer-read-only hif-outside-read-only)
   (setq selective-display nil) ; defaults
@@ -951,24 +988,23 @@ Turn off hiding by calling `show-ifdefs'."
 
 (defun hif-find-ifdef-block ()
   "Utility for hide and show `ifdef-block'.
-Set top and bottom of ifdef block."
+Return as (TOP . BOTTOM) the extent of ifdef block."
   (let (max-bottom)
-  (save-excursion
-    (beginning-of-line)
-    (if (not (or (hif-looking-at-else) (hif-looking-at-ifX)))
-       (up-ifdef))
-    (setq top (point))
-    (hif-ifdef-to-endif)
-    (setq max-bottom (1- (point))))
-  (save-excursion
-    (beginning-of-line)
-    (if (not (hif-looking-at-endif))
-       (hif-find-next-relevant))
-    (while (hif-looking-at-ifX)
-      (hif-ifdef-to-endif)
-      (hif-find-next-relevant))
-    (setq bottom (min max-bottom (1- (point))))))
-  )
+    (cons (save-excursion
+           (beginning-of-line)
+           (if (not (or (hif-looking-at-else) (hif-looking-at-ifX)))
+               (up-ifdef))
+           (prog1 (point)
+             (hif-ifdef-to-endif)
+             (setq max-bottom (1- (point)))))
+         (save-excursion
+           (beginning-of-line)
+           (if (not (hif-looking-at-endif))
+               (hif-find-next-relevant))
+           (while (hif-looking-at-ifX)
+             (hif-ifdef-to-endif)
+             (hif-find-next-relevant))
+           (min max-bottom (1- (point)))))))
 
 
 (defun hide-ifdef-block ()
@@ -977,13 +1013,13 @@ Set top and bottom of ifdef block."
   (if (not hide-ifdef-mode)
       (hide-ifdef-mode 1))
   (setq selective-display t)
-  (let (top bottom (inhibit-read-only t))
-    (hif-find-ifdef-block) ; set top and bottom - dynamic scoping
-    (hide-ifdef-region top bottom)
+  (let ((top-bottom (hif-find-ifdef-block))
+       (inhibit-read-only t))
+    (hide-ifdef-region (car top-bottom) (cdr top-bottom))
     (if hide-ifdef-lines
        (progn
-         (hif-hide-line top)
-         (hif-hide-line (1+ bottom))))
+         (hif-hide-line (car top-bottom))
+         (hif-hide-line (1+ (cdr top-bottom)))))
     (setq hide-ifdef-hiding t))
   (setq buffer-read-only (or hide-ifdef-read-only hif-outside-read-only)))
 
@@ -997,9 +1033,8 @@ Set top and bottom of ifdef block."
          (beginning-of-line)
          (hif-show-ifdef-region (1- (point)) (progn (end-of-line) (point))))
 
-      (let (top bottom)
-       (hif-find-ifdef-block)
-       (hif-show-ifdef-region (1- top) bottom)))))
+      (let ((top-bottom (hif-find-ifdef-block)))
+       (hif-show-ifdef-region (1- (car top-bottom)) (cdr top-bottom))))))
 
 
 ;;;  definition alist support
@@ -1009,8 +1044,8 @@ Set top and bottom of ifdef block."
 
 (defun hif-compress-define-list (env)
   "Compress the define list ENV into a list of defined symbols only."
-  (let ((defs (mapcar '(lambda (arg)
-                        (if (hif-lookup (car arg)) (car arg)))
+  (let ((defs (mapcar (lambda (arg)
+                       (if (hif-lookup (car arg)) (car arg)))
                      env))
        (new-defs nil))
     (while defs
@@ -1032,10 +1067,11 @@ Set top and bottom of ifdef block."
   (let ((define-list (assoc name hide-ifdef-define-alist)))
     (if define-list
        (setq hide-ifdef-env
-             (mapcar '(lambda (arg) (cons arg t))
+             (mapcar (lambda (arg) (cons arg t))
                      (cdr define-list)))
       (error "No define list for %s" name))
     (if hide-ifdef-hiding (hide-ifdefs))))
 
-;;; hideif.el ends here
+(provide 'hideif)
 
+;;; hideif.el ends here