Some fixes to follow coding conventions in files maintained by FSF.
[bpt/emacs.git] / lisp / progmodes / hideif.el
index d995fc4..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.
 
-;; Author: Dan LaLiberte <liberte@a.cs.uiuc.edu>
+;; Author: Daniel LaLiberte <liberte@holonexus.org>
 ;; Maintainer: FSF
 ;; Keywords: c, outlines
 
@@ -67,7 +67,7 @@
 ;; following example:
 ;;
 ;; (setq hide-ifdef-mode-hook
-;;      '(lambda ()
+;;      (lambda ()
 ;;      (if (not hide-ifdef-define-alist)
 ;;          (setq hide-ifdef-define-alist
 ;;               '((list1 ONE TWO)
@@ -76,7 +76,7 @@
 ;;      (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
+;; 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,
 
 (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.")
 
   (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:
@@ -300,9 +312,6 @@ 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."
@@ -340,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 "\\*/")
 
 
@@ -403,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)))
@@ -416,25 +433,25 @@ 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 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))
@@ -442,17 +459,18 @@ that form should be displayed.")
 (defun hif-term ()
   "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 an 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))
@@ -462,8 +480,8 @@ that form should be displayed.")
        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))
@@ -471,41 +489,41 @@ that form should be displayed.")
 (defun hif-factor ()
   "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."
@@ -524,7 +542,18 @@ 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 -----------------------
 
 
@@ -870,19 +899,22 @@ It does not do the work that's pointless to redo on a recursive entry."
 ;===%%SF%% exports (Start)  ===
 
 ;;;###autoload
-(defvar hide-ifdef-initially nil
-  "*Non-nil means call `hide-ifdefs' when Hide-Ifdef mode is first activated.")
+(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
-  "*Non-nil means hide 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."
@@ -956,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 ()
@@ -982,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)))
 
@@ -1002,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
@@ -1014,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
@@ -1037,7 +1067,7 @@ 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))))
@@ -1045,4 +1075,3 @@ Set top and bottom of ifdef block."
 (provide 'hideif)
 
 ;;; hideif.el ends here
-