From 0fdb2dffa06d299d2c73d1a079c44e4c4f87fd8d Mon Sep 17 00:00:00 2001
From: Luke Lee <luke.yx.lee@gmail.com>
Date: Tue, 3 Dec 2013 12:31:04 +0800
Subject: [PATCH] Some cleanups for hideif.

---
 lisp/progmodes/hideif.el | 201 ++++++++++++++++++++++-------------------------
 1 file changed, 95 insertions(+), 106 deletions(-)

diff --git a/lisp/progmodes/hideif.el b/lisp/progmodes/hideif.el
index b89d42f..e5a2a3d 100644
--- a/lisp/progmodes/hideif.el
+++ b/lisp/progmodes/hideif.el
@@ -142,14 +142,16 @@
   :group 'hide-ifdef)

 (defcustom hide-ifdef-expand-re-inclusion-protection t
-  "When hiding header files, always expand the re-inclusion protecting ifdefs.
-Most header files are wrapped with ifdefs to prevent re-inclusion:
+  "When hiding header files, always expand the re-inclusion protected
+ifdefs. Most C/C++ header files are wrapped with ifdefs to prevent
+re-inclusion of this form
   #ifdef _XXX_HEADER_FILE_INCLUDED_
   #define _XXX_HEADER_FILE_INCLUDED_
   ...
   #endif
-hence if we try to hide ifdefs, it will hide everything when we tried
-the second time since _XXX_HEADER_FILE_INCLUDED_ already defined."
+Hence, if we try to hide ifdefs in the header file, it will hide
+everything when we tried the second time,since _XXX_HEADER_FILE_INCLUDED_
+is defined at the first time that we hide it."
   :type 'boolean
   :group 'hide-ifdef)

@@ -320,7 +322,7 @@ Several variables affect how the hiding is done:
   (while (= (logand 1 (skip-chars-backward "\\\\")) 1)
     (end-of-line 2)))

-(defun merge-ifdef-region (start end)
+(defun hif-merge-ifdef-region (start end)
   ;; Genernally there is no need to call itself recursively since there should originally
   ;; exists no un-merged regions; however, if a part of the file is hidden with 'hide-ifdef-lines'
   ;; as nil while another part with 't, this case happens.
@@ -346,7 +348,7 @@ total number of overlays created."
           (setq b (min start (overlay-start o))
                 e (max end (overlay-end o)))
           (move-overlay o b e)
-          (merge-ifdef-region b e)
+          (hif-merge-ifdef-region b e)
           (setq ob o)))
       ;; merge overlays after 'end'
       (dolist (o endovrs)
@@ -354,7 +356,7 @@ total number of overlays created."
           (setq b (min start (overlay-start o))
                 e (max end (overlay-end o)))
           (move-overlay o b e)
-          (merge-ifdef-region b e)
+          (hif-merge-ifdef-region b e)
           (setf oe o)))
       ;; if both 'start' 'end' merging happens, merge into bigger one
       (when (and ob oe)
@@ -362,11 +364,11 @@ total number of overlays created."
               (e (max (overlay-end ob) (overlay-end oe))))
           (delete-overlay oe)
           (move-overlay ob b e)
-          (merge-ifdef-region b e)))
+          (hif-merge-ifdef-region b e)))
       (or ob oe))))

 (defun hide-ifdef-region-internal (start end)
-  (unless (merge-ifdef-region start end)
+  (unless (hif-merge-ifdef-region start end)
     (let ((o (make-overlay start end)))
       (overlay-put o 'hide-ifdef t)
       (if hide-ifdef-shadow
@@ -451,7 +453,6 @@ that form should be displayed.")
                                       "\\(\\.\\.\\.\\)?" hif-white
                                     ")"
                                     "\\)?" ))
-;;(defconst hif-header-regexp "^.*\\.[hH]\\([hH]\\|[xX][xX]\\|[pP][pP]\\)?")

 ;; Used to store the current token and the whole token list during parsing.
 ;; Only bound dynamically.
@@ -541,37 +542,39 @@ that form should be displayed.")
            (t (error "Bad #if expression: %s" (buffer-string)))))))
     (nreverse token-list)))

-;;;-----------------------------------------------------------------
-;;; Translate C preprocessor #if expressions using recursive descent.
-;;; This parser is limited to the operators &&, ||, !, and "defined".
-;;; Added ==, !=, +, and -.  Gary Oberbrunner, garyo@avs.com, 8/9/94
-;;;
-;;; Implement the C language operator precedence table. Add all those
-;;; missing operators that could be used in macros. Luke Lee 2013-09-04
-
-;;;  | Operator Type        | Operator                                    | Associativity |
-;;;  +----------------------+---------------------------------------------+---------------+
-;;;  | Primary Expression   | () [] . -> expr++ expr--                    | left-to-right |
-;;;  | Unary Operators      | * & + - ! ~ ++expr --expr (typecast) sizeof | right-to-left |
-;;;  | Binary Operators     | * / %                                       | left-to-right |
-;;;  |                      | + -                                         |               |
-;;;  |                      | >> <<                                       |               |
-;;;  |                      | < > <= >=                                   |               |
-;;;  |                      | == !=                                       |               |
-;;;  |                      | &                                           |               |
-;;;  |                      | ^                                           |               |
-;;;  |                      | |                                           |               |
-;;;  |                      | &&                                          |               |
-;;;  |                      | ||                                          |               |
-;;;  | Ternary Operator     | ?:                                          | right-to-left |
-;;; x| Assignment Operators | = += -= *= /= %= >>= <<= &= ^= =            | right-to-left |
-;;;  | Comma                | ,                                           | left-to-right |
+;;------------------------------------------------------------------------
+;; Translate C preprocessor #if expressions using recursive descent.
+;; This parser is limited to the operators &&, ||, !, and "defined".
+;; Added ==, !=, +, and -.  Gary Oberbrunner, garyo@avs.com, 8/9/94
+;;
+;; Implement the C language operator precedence table. Add all those
+;; missing operators that could be used in macros. Luke Lee 2013-09-04
+
+;;  | Operator Type        | Operator                    | Associativity |
+;;  +----------------------+-----------------------------+---------------+
+;;  | Primary Expression   | () [] . -> expr++ expr--    | left-to-right |
+;;  | Unary Operators      | * & + - ! ~ ++expr --expr   | right-to-left |
+;;  |                      | (typecast) sizeof           |               |
+;;  | Binary Operators     | * / %                       | left-to-right |
+;;  |                      | + -                         |               |
+;;  |                      | >> <<                       |               |
+;;  |                      | < > <= >=                   |               |
+;;  |                      | == !=                       |               |
+;;  |                      | &                           |               |
+;;  |                      | ^                           |               |
+;;  |                      | |                           |               |
+;;  |                      | &&                          |               |
+;;  |                      | ||                          |               |
+;;  | Ternary Operator     | ?:                          | right-to-left |
+;; x| Assignment Operators | = += -= *= /= %= >>= <<= &= | right-to-left |
+;;  |                      | ^= =                        |               |
+;;  | Comma                | ,                           | left-to-right |

 (defsubst hif-nexttoken ()
   "Pop the next token from token-list into the let variable \"hif-token\"."
   (setq hif-token (pop hif-token-list)))

-(defsubst if-valid-identifier (id)
+(defsubst hif-if-valid-identifier (id)
   (not (or (numberp id)
            (stringp id))))

@@ -590,19 +593,27 @@ that form should be displayed.")
                   (progn
                     (setq tok (cadr tokens))
                     (if (eq (car tokens) 'hif-lparen)
-                        (if (and (if-valid-identifier tok)
+                        (if (and (hif-if-valid-identifier tok)
                                  (eq (caddr tokens) 'hif-rparen))
                             (setq tokens (cdddr tokens))
                           (error "operator \"defined\" requires an identifier"))
                       (setq tok (car tokens)
                             tokens (cdr tokens))
-                      (unless (if-valid-identifier tok)
+                      (unless (hif-if-valid-identifier tok)
                         (error "operator \"defined\" requires an identifier")))
                     (list 'hif-defined 'hif-lparen tok 'hif-rparen))
                 tok)))))
     result))

-(defun hif-expand-token-list (tokens &optional macroname reflist) ;; reference list to prevent self-referencing
+(defun hif-flatten (l)
+  "Flatten a tree"
+  (if (and l (atom l))
+      (list l)
+    (if (consp l)
+        (nconc (hif-flatten (car l)) (hif-flatten (cdr l))))))
+
+(defun hif-expand-token-list (tokens &optional macroname reflist)
+  ;; 'reflist' is the reference list to prevent self-referencing
   "Perform expansion till everything expanded. No self-reference expansion."
   (catch 'self-referencing
     (let ((expanded nil)
@@ -651,14 +662,15 @@ that form should be displayed.")
                    ;; lookup tok returns an atom
                    (list rep)))

-                ;;[2013-10-22 16:06:12 +0800] Must keep the token, removeing this token might results in a incomplete expression which cannot be parsed.
+                ;;[2013-10-22 16:06:12 +0800] Must keep the token, removeing this token might results in
+                ;;                            an incomplete expression that cannot be parsed further.
                 ;;((= 1 (hif-defined tok)) ;; defined (hif-defined tok)=1, but empty (hif-lookup tok)=nil, thus remove this token
                 ;; (setq remains (delete tok remains))
                 ;; nil)

                 (t ;; usual IDs
                  (list tok))))))
-      (flatten expanded))))
+      (hif-flatten expanded))))

 (defun hif-parse-if-exp (token-list &optional macroname)
   "Parse the TOKEN-LIST.  Return translated list in prefix form."
@@ -670,22 +682,6 @@ that form should be displayed.")
       (if hif-token ; is there still a token?
           (error "Error: unexpected token: %s" hif-token)))))

-;;;
-;;;To manually test these functions, try the following in scratch buffer:
-;;;
-;;;(hif-find-define (point)) ;; find only one #define
-;;;(hif-add-new-defines) ;; find all #defines
-;;;
-;;;#define macro ( arg1 , arg2 , arg3 ... ) arg1 + arg2 * arg3
-;;;
-;;;(assoc 'macro hide-ifdef-env)
-;;;
-;;;(hif-canonicalize-tokens hif-ifx-regexp)
-;;;(hif-expand-token-list (hif-canonicalize-tokens hif-ifx-regexp))
-;;;(hif-canonicalize hif-ifx-regexp)
-;;;
-;;;#if macro( (a,2,3), b<<1, (c&7,d) )
-;;;

 (defun hif-exprlist ()
   "Parse an exprlist: expr { ',' expr}"
@@ -975,12 +971,12 @@ we ignore them all here."
      ,(format "Like `%s' but treat t and nil as 1 and 0." fun)
      (,fun (hif-mathify a) (hif-mathify b))))

-(defun shiftleft (a b)
+(defun shiftleft-hif (a b)
   (if (< a 0)
       (ash a b)
     (lsh a b)))

-(defun shiftright (a b)
+(defun shiftright-hif (a b)
   (if (< a 0)
       (ash a (- 0 b))
     (lsh a (- 0 b))))
@@ -1000,8 +996,8 @@ we ignore them all here."
 (defalias 'hif-logior        (hif-mathify-binop logior))
 (defalias 'hif-logxor        (hif-mathify-binop logxor))
 (defalias 'hif-logand        (hif-mathify-binop logand))
-(defalias 'hif-shiftleft     (hif-mathify-binop shiftleft))
-(defalias 'hif-shiftright    (hif-mathify-binop shiftright))
+(defalias 'hif-shiftleft     (hif-mathify-binop shiftleft-hif))
+(defalias 'hif-shiftright    (hif-mathify-binop shiftright-hif))


 (defun hif-comma (&rest expr)
@@ -1049,14 +1045,7 @@ we ignore them all here."
         (append result (list prev))
       result)))

-(defun flatten (l)
-  "Flatten a tree"
-  (if (and l (atom l))
-      (list l)
-    (if (consp l)
-        (nconc (flatten (car l)) (flatten (cdr l))))))
-
-(defun delimit (l a)
+(defun hif-delimit (l a)
   "Delmit a list 'l' with delimiter atom 'd'"
   (nconc (mapcan (lambda (l)
                    (list l a))  (butlast l))
@@ -1109,12 +1098,12 @@ we ignore them all here."
               ;; Unlike 'subst', 'substitute' replace only the top level instead of the whole tree,
               ;; more importantly, it's not destructive.
               (if (and etc (null formal-parms))
-                  (substitute (delimit actual-parms 'hif-comma) formal macro-body)
+                  (substitute (hif-delimit actual-parms 'hif-comma) formal macro-body)
                 (substitute (car actual-parms) formal macro-body)))
         (setq actual-parms (cdr actual-parms)))

       ;; Replacement completed, flatten the whole token list,
-      (setq macro-body (flatten macro-body))
+      (setq macro-body (hif-flatten macro-body))

       ;; Stringification and token concatenation happens here
       (hif-token-concatenation (hif-token-stringification macro-body)))))
@@ -1324,11 +1313,11 @@ With argument, do this that many times."
 ;;===%%SF%% hide-ifdef-hiding (Start)  ===


-;;; A range is a structure with four components:
-;;; START       The start of the range. (beginning of line)
-;;; ELSE        The else marker (beginning of line)
-;;; END         The end of the range.  (beginning of line)
-;;; ELIF        A sequence of #elif markers (beginning of line)
+;; A range is a structure with four components:
+;; START       The start of the range. (beginning of line)
+;; ELSE        The else marker (beginning of line)
+;; END         The end of the range.  (beginning of line)
+;; ELIF        A sequence of #elif markers (beginning of line)

 (defsubst hif-make-range (start end &optional else elif)
   (list start else end elif))
@@ -1339,10 +1328,10 @@ With argument, do this that many times."
 (defsubst hif-range-elif (range) (elt range 3))


-;;; Find-Range
-;;; The workhorse, it delimits the #if region.  Reasonably simple:
-;;; Skip until an #else or #endif is found, remembering positions.  If
-;;; an #else was found, skip some more, looking for the true #endif.
+;; Find-Range
+;; The workhorse, it delimits the #if region.  Reasonably simple:
+;; Skip until an #else or #endif is found, remembering positions.  If
+;; an #else was found, skip some more, looking for the true #endif.

 (defun hif-find-range ()
   "Return a Range structure describing the current #if region.
@@ -1380,7 +1369,7 @@ Point is left unchanged."
       (hif-make-range start end else elif))))


-;;; A bit slimy.
+;; A bit slimy.

 (defun hif-hide-line (point)
   "Hide the line containing point.  Does nothing if `hide-ifdef-lines' is nil."
@@ -1391,31 +1380,31 @@ Point is left unchanged."
        (line-beginning-position) (progn (hif-end-of-line) (point))))))


-;;;  Hif-Possibly-Hide
-;;;  There are four cases.  The #ifX expression is "taken" if it
-;;;  the hide-ifdef-evaluator returns T.  Presumably, this means the code
-;;;  inside the #ifdef would be included when the program was
-;;;  compiled.
-;;;
-;;;  Case 1:  #ifX taken, and there's an #else.
-;;;     The #else part must be hidden.  The #if (then) part must be
-;;;     processed for nested #ifX's.
-;;;  Case 2:  #ifX taken, and there's no #else.
-;;;     The #if part must be processed for nested #ifX's.
-;;;  Case 3:  #ifX not taken, and there's an #elif
-;;;     The #if part must be hidden, and then evaluate
-;;;     the #elif condition like a new #ifX.
-;;;  Case 4:  #ifX not taken, and there's just an #else.
-;;;     The #if part must be hidden.  The #else part must be processed
-;;;     for nested #ifs.
-;;;  Case 5:  #ifX not taken, and there's no #else.
-;;;     The #ifX part must be hidden.
-;;;
-;;;  Further processing is done by narrowing to the relevant region
-;;;  and just recursively calling hide-ifdef-guts.
-;;;
-;;;  When hif-possibly-hide returns, point is at the end of the
-;;;  possibly-hidden range.
+;;  Hif-Possibly-Hide
+;;  There are four cases.  The #ifX expression is "taken" if it
+;;  the hide-ifdef-evaluator returns T.  Presumably, this means the code
+;;  inside the #ifdef would be included when the program was
+;;  compiled.
+;;
+;;  Case 1:  #ifX taken, and there's an #else.
+;;     The #else part must be hidden.  The #if (then) part must be
+;;     processed for nested #ifX's.
+;;  Case 2:  #ifX taken, and there's no #else.
+;;     The #if part must be processed for nested #ifX's.
+;;  Case 3:  #ifX not taken, and there's an #elif
+;;     The #if part must be hidden, and then evaluate
+;;     the #elif condition like a new #ifX.
+;;  Case 4:  #ifX not taken, and there's just an #else.
+;;     The #if part must be hidden.  The #else part must be processed
+;;     for nested #ifs.
+;;  Case 5:  #ifX not taken, and there's no #else.
+;;     The #ifX part must be hidden.
+;;
+;;  Further processing is done by narrowing to the relevant region
+;;  and just recursively calling hide-ifdef-guts.
+;;
+;;  When hif-possibly-hide returns, point is at the end of the
+;;  possibly-hidden range.

 (defvar hif-recurse-level 0)

--
1.8.3.24.g67b0998

