diff options
| author | Luke Lee | 2014-06-27 16:25:21 +0800 |
|---|---|---|
| committer | Luke Lee | 2014-06-27 16:25:21 +0800 |
| commit | d9824c5167e8cc22da66c1bcd6bdcd450ab7775e (patch) | |
| tree | 403811685cea0dc0de34b96caa4d8932a910af3a | |
| parent | da313ecaea3f2fad5eee6582dba5e0cc95ee5ea0 (diff) | |
| download | emacs-d9824c5167e8cc22da66c1bcd6bdcd450ab7775e.tar.gz emacs-d9824c5167e8cc22da66c1bcd6bdcd450ab7775e.zip | |
* lisp/progmodes/hideif.el: Style fixes. Fix doc-strings and comment style,
also add a change log entry for the latest hideif.el changes.
| -rw-r--r-- | lisp/ChangeLog | 26 | ||||
| -rw-r--r-- | lisp/progmodes/hideif.el | 138 |
2 files changed, 92 insertions, 72 deletions
diff --git a/lisp/ChangeLog b/lisp/ChangeLog index 9fb230813c7..d36aec303f1 100644 --- a/lisp/ChangeLog +++ b/lisp/ChangeLog | |||
| @@ -1,3 +1,29 @@ | |||
| 1 | 2014-06-26 Luke Lee <luke.yx.lee@gmail.com> | ||
| 2 | |||
| 3 | * lisp/progmodes/hideif.el (hif-string-to-number): Fix return value bug. | ||
| 4 | (hif-simple-token-only, hif-tokenize): Commented in detail mainly for | ||
| 5 | performance enhancements. | ||
| 6 | (hif-parse-if-exp): Rename to `hif-parse-exp'. Enhanced for macro | ||
| 7 | expansion. | ||
| 8 | (hif-factor, hif-string-concatenation, intern-safe): Support string | ||
| 9 | concatenation and argumented macro expansion. | ||
| 10 | (hif-if-valid-identifier-p, hif-define-operator, hif-flatten) | ||
| 11 | (hif-expand-token-list, hif-get-argument-list, hif-define-macro) | ||
| 12 | (hif-delimit, hif-macro-supply-arguments, hif-invoke, hif-canonicalize) | ||
| 13 | (hif-canonicalize-tokens, hif-place-macro-invocation) | ||
| 14 | (hif-parse-macro-arglist): Mostly new functions for supporting | ||
| 15 | argumented macro expansion. | ||
| 16 | (hif-string-concatenation, hif-stringify, hif-token-concat) | ||
| 17 | (hif-token-stringification, hif-token-concatenation): Stringification | ||
| 18 | and concatenation. | ||
| 19 | (hif-find-next-relevant): Fix comments. | ||
| 20 | (hif-ifdef-to-endif, hif-looking-at-elif, hif-hide-line): Bug fix for | ||
| 21 | some cases involving #elif. | ||
| 22 | (hif-find-define, hif-add-new-defines): New functions for automatically | ||
| 23 | scanning of defined symbols. | ||
| 24 | (hide-ifdef-guts): Fix for defined symbol auto scanning. | ||
| 25 | (hide-ifdef-undef): Fix behavior to match CPP. | ||
| 26 | |||
| 1 | 2014-06-26 Glenn Morris <rgm@gnu.org> | 27 | 2014-06-26 Glenn Morris <rgm@gnu.org> |
| 2 | 28 | ||
| 3 | * Makefile.in (update-authors): Update for moved authors.el. | 29 | * Makefile.in (update-authors): Update for moved authors.el. |
diff --git a/lisp/progmodes/hideif.el b/lisp/progmodes/hideif.el index b0ca4f0cdd0..4b78c08690a 100644 --- a/lisp/progmodes/hideif.el +++ b/lisp/progmodes/hideif.el | |||
| @@ -36,7 +36,7 @@ | |||
| 36 | ;; | 36 | ;; |
| 37 | ;; Hide-ifdef suppresses the display of code that the preprocessor wouldn't | 37 | ;; Hide-ifdef suppresses the display of code that the preprocessor wouldn't |
| 38 | ;; pass through. Support complete C/C++ expression and precedence. | 38 | ;; pass through. Support complete C/C++ expression and precedence. |
| 39 | ;; It will automatically scans for new #define symbols and macros on the way | 39 | ;; It will automatically scan for new #define symbols and macros on the way |
| 40 | ;; parsing. | 40 | ;; parsing. |
| 41 | ;; | 41 | ;; |
| 42 | ;; The hidden code is marked by ellipses (...). Be | 42 | ;; The hidden code is marked by ellipses (...). Be |
| @@ -204,7 +204,7 @@ | |||
| 204 | (cons '(hide-ifdef-hiding " Hiding") | 204 | (cons '(hide-ifdef-hiding " Hiding") |
| 205 | minor-mode-alist))) | 205 | minor-mode-alist))) |
| 206 | 206 | ||
| 207 | ;; fix c-mode syntax table so we can recognize whole symbols. | 207 | ;; Fix c-mode syntax table so we can recognize whole symbols. |
| 208 | (defvar hide-ifdef-syntax-table | 208 | (defvar hide-ifdef-syntax-table |
| 209 | (let ((st (copy-syntax-table c-mode-syntax-table))) | 209 | (let ((st (copy-syntax-table c-mode-syntax-table))) |
| 210 | (modify-syntax-entry ?_ "w" st) | 210 | (modify-syntax-entry ?_ "w" st) |
| @@ -333,7 +333,7 @@ that form should be displayed.") | |||
| 333 | 333 | ||
| 334 | 334 | ||
| 335 | (defun hif-set-var (var value) | 335 | (defun hif-set-var (var value) |
| 336 | "Prepend (var value) pair to `hide-ifdef-env'." | 336 | "Prepend (VAR VALUE) pair to `hide-ifdef-env'." |
| 337 | (setq hide-ifdef-env (cons (cons var value) hide-ifdef-env))) | 337 | (setq hide-ifdef-env (cons (cons var value) hide-ifdef-env))) |
| 338 | 338 | ||
| 339 | (declare-function semantic-c-hideif-lookup "semantic/bovine/c" (var)) | 339 | (declare-function semantic-c-hideif-lookup "semantic/bovine/c" (var)) |
| @@ -389,8 +389,8 @@ that form should be displayed.") | |||
| 389 | ")" | 389 | ")" |
| 390 | "\\)?" )) | 390 | "\\)?" )) |
| 391 | 391 | ||
| 392 | ;; Used to store the current token and the whole token list during parsing. | 392 | ;; Store the current token and the whole token list during parsing. |
| 393 | ;; Only bound dynamically. | 393 | ;; Bound dynamically. |
| 394 | (defvar hif-token) | 394 | (defvar hif-token) |
| 395 | (defvar hif-token-list) | 395 | (defvar hif-token-list) |
| 396 | 396 | ||
| @@ -456,6 +456,7 @@ that form should be displayed.") | |||
| 456 | ;; speeding up macro evaluation on those very simple cases like integers or | 456 | ;; speeding up macro evaluation on those very simple cases like integers or |
| 457 | ;; literals. | 457 | ;; literals. |
| 458 | ;; Check the long comments before `hif-find-define' for more details. [lukelee] | 458 | ;; Check the long comments before `hif-find-define' for more details. [lukelee] |
| 459 | (defvar hif-simple-token-only) | ||
| 459 | 460 | ||
| 460 | (defun hif-tokenize (start end) | 461 | (defun hif-tokenize (start end) |
| 461 | "Separate string between START and END into a list of tokens." | 462 | "Separate string between START and END into a list of tokens." |
| @@ -539,8 +540,7 @@ that form should be displayed.") | |||
| 539 | (stringp id)))) | 540 | (stringp id)))) |
| 540 | 541 | ||
| 541 | (defun hif-define-operator (tokens) | 542 | (defun hif-define-operator (tokens) |
| 542 | "`Upgrade' hif-define xxx to '(hif-define xxx)' so that it won't be | 543 | "`Upgrade' hif-define xxx to '(hif-define xxx)' so it won't be subsitituted." |
| 543 | subsitituted" | ||
| 544 | (let ((result nil) | 544 | (let ((result nil) |
| 545 | (tok nil)) | 545 | (tok nil)) |
| 546 | (while (setq tok (pop tokens)) | 546 | (while (setq tok (pop tokens)) |
| @@ -563,15 +563,17 @@ subsitituted" | |||
| 563 | (nreverse result))) | 563 | (nreverse result))) |
| 564 | 564 | ||
| 565 | (defun hif-flatten (l) | 565 | (defun hif-flatten (l) |
| 566 | "Flatten a tree" | 566 | "Flatten a tree." |
| 567 | (apply #'nconc | 567 | (apply #'nconc |
| 568 | (mapcar (lambda (x) (if (listp x) | 568 | (mapcar (lambda (x) (if (listp x) |
| 569 | (hif-flatten x) | 569 | (hif-flatten x) |
| 570 | (list x))) l))) | 570 | (list x))) l))) |
| 571 | 571 | ||
| 572 | (defun hif-expand-token-list (tokens &optional macroname expand_list) | 572 | (defun hif-expand-token-list (tokens &optional macroname expand_list) |
| 573 | "Perform expansion till everything expanded. No self-reference expansion. | 573 | "Perform expansion on TOKENS till everything expanded. |
| 574 | EXPAND_LIST is the list of macro names currently being expanded." | 574 | Self-reference (directly or indirectly) tokens are not expanded. |
| 575 | EXPAND_LIST is the list of macro names currently being expanded, use for | ||
| 576 | detecting self-reference." | ||
| 575 | (catch 'self-referencing | 577 | (catch 'self-referencing |
| 576 | (let ((expanded nil) | 578 | (let ((expanded nil) |
| 577 | (remains (hif-define-operator | 579 | (remains (hif-define-operator |
| @@ -594,9 +596,9 @@ subsitituted" | |||
| 594 | 596 | ||
| 595 | ((setq rep (hif-lookup tok)) | 597 | ((setq rep (hif-lookup tok)) |
| 596 | (if (and (listp rep) | 598 | (if (and (listp rep) |
| 597 | (eq (car rep) 'hif-define-macro)) ;; a defined macro | 599 | (eq (car rep) 'hif-define-macro)) ; A defined macro |
| 598 | ;; Recursively expand it | 600 | ;; Recursively expand it |
| 599 | (if (cadr rep) ;; Argument list is not nil | 601 | (if (cadr rep) ; Argument list is not nil |
| 600 | (if (not (eq (car remains) 'hif-lparen)) | 602 | (if (not (eq (car remains) 'hif-lparen)) |
| 601 | ;; No argument, no invocation | 603 | ;; No argument, no invocation |
| 602 | tok | 604 | tok |
| @@ -616,7 +618,7 @@ subsitituted" | |||
| 616 | result)) | 618 | result)) |
| 617 | ;; Argument list is nil, direct expansion | 619 | ;; Argument list is nil, direct expansion |
| 618 | (setq rep (hif-expand-token-list | 620 | (setq rep (hif-expand-token-list |
| 619 | (caddr rep) ;; Macro's token list | 621 | (caddr rep) ; Macro's token list |
| 620 | tok expand_list)) | 622 | tok expand_list)) |
| 621 | ;; Replace all remaining references immediately | 623 | ;; Replace all remaining references immediately |
| 622 | (setq remains (substitute tok rep remains)) | 624 | (setq remains (substitute tok rep remains)) |
| @@ -627,12 +629,12 @@ subsitituted" | |||
| 627 | ;;[2013-10-22 16:06:12 +0800] Must keep the token, removing | 629 | ;;[2013-10-22 16:06:12 +0800] Must keep the token, removing |
| 628 | ;; this token might results in an incomplete expression that | 630 | ;; this token might results in an incomplete expression that |
| 629 | ;; cannot be parsed further. | 631 | ;; cannot be parsed further. |
| 630 | ;;((= 1 (hif-defined tok)) ;; defined (hif-defined tok)=1, | 632 | ;;((= 1 (hif-defined tok)) ; defined (hif-defined tok)=1, |
| 631 | ;; ;;but empty (hif-lookup tok)=nil, thus remove this token | 633 | ;; ;;but empty (hif-lookup tok)=nil, thus remove this token |
| 632 | ;; (setq remains (delete tok remains)) | 634 | ;; (setq remains (delete tok remains)) |
| 633 | ;; nil) | 635 | ;; nil) |
| 634 | 636 | ||
| 635 | (t ;; Usual IDs | 637 | (t ; Usual IDs |
| 636 | tok)) | 638 | tok)) |
| 637 | 639 | ||
| 638 | expanded)) | 640 | expanded)) |
| @@ -640,8 +642,9 @@ subsitituted" | |||
| 640 | (hif-flatten (nreverse expanded))))) | 642 | (hif-flatten (nreverse expanded))))) |
| 641 | 643 | ||
| 642 | (defun hif-parse-exp (token-list &optional macroname) | 644 | (defun hif-parse-exp (token-list &optional macroname) |
| 643 | "Parse the TOKEN-LIST. Return translated list in prefix form. MACRONAME | 645 | "Parse the TOKEN-LIST. |
| 644 | is applied when invoking macros to prevent self-referencing macros." | 646 | Return translated list in prefix form. MACRONAME is applied when invoking |
| 647 | macros to prevent self-reference." | ||
| 645 | (let ((hif-token-list (hif-expand-token-list token-list macroname))) | 648 | (let ((hif-token-list (hif-expand-token-list token-list macroname))) |
| 646 | (hif-nexttoken) | 649 | (hif-nexttoken) |
| 647 | (prog1 | 650 | (prog1 |
| @@ -651,7 +654,7 @@ is applied when invoking macros to prevent self-referencing macros." | |||
| 651 | (error "Error: unexpected token: %s" hif-token))))) | 654 | (error "Error: unexpected token: %s" hif-token))))) |
| 652 | 655 | ||
| 653 | (defun hif-exprlist () | 656 | (defun hif-exprlist () |
| 654 | "Parse an exprlist: expr { ',' expr}" | 657 | "Parse an exprlist: expr { ',' expr}." |
| 655 | (let ((result (hif-expr))) | 658 | (let ((result (hif-expr))) |
| 656 | (if (eq hif-token 'hif-comma) | 659 | (if (eq hif-token 'hif-comma) |
| 657 | (let ((temp (list result))) | 660 | (let ((temp (list result))) |
| @@ -772,8 +775,9 @@ is applied when invoking macros to prevent self-referencing macros." | |||
| 772 | result)) | 775 | result)) |
| 773 | 776 | ||
| 774 | (defun hif-factor () | 777 | (defun hif-factor () |
| 775 | "Parse a factor: '!' factor | '~' factor | '(' expr ')' | | 778 | "Parse a factor. |
| 776 | 'defined(' id ')' | 'id(parmlist)' | strings | id." | 779 | factor : '!' factor | '~' factor | '(' expr ')' | 'defined(' id ')' | |
| 780 | 'id(parmlist)' | strings | id." | ||
| 777 | (cond | 781 | (cond |
| 778 | ((eq hif-token 'hif-not) | 782 | ((eq hif-token 'hif-not) |
| 779 | (hif-nexttoken) | 783 | (hif-nexttoken) |
| @@ -822,7 +826,7 @@ is applied when invoking macros to prevent self-referencing macros." | |||
| 822 | 826 | ||
| 823 | (defun hif-get-argument-list (ident) | 827 | (defun hif-get-argument-list (ident) |
| 824 | (let ((nest 0) | 828 | (let ((nest 0) |
| 825 | (parmlist nil) ;; A "token" list of parameters, will later be parsed | 829 | (parmlist nil) ; A "token" list of parameters, will later be parsed |
| 826 | (parm nil)) | 830 | (parm nil)) |
| 827 | 831 | ||
| 828 | (while (or (not (eq (hif-nexttoken) 'hif-rparen)) | 832 | (while (or (not (eq (hif-nexttoken) 'hif-rparen)) |
| @@ -840,8 +844,8 @@ is applied when invoking macros to prevent self-referencing macros." | |||
| 840 | (setq parm nil))) | 844 | (setq parm nil))) |
| 841 | (push hif-token parm)) | 845 | (push hif-token parm)) |
| 842 | 846 | ||
| 843 | (push (nreverse parm) parmlist) ;; Okay even if parm is nil | 847 | (push (nreverse parm) parmlist) ; Okay even if PARM is nil |
| 844 | (hif-nexttoken) ;; Drop the hif-rparen, get next token | 848 | (hif-nexttoken) ; Drop the `hif-rparen', get next token |
| 845 | (nreverse parmlist))) | 849 | (nreverse parmlist))) |
| 846 | 850 | ||
| 847 | (defun hif-place-macro-invocation (ident) | 851 | (defun hif-place-macro-invocation (ident) |
| @@ -849,7 +853,7 @@ is applied when invoking macros to prevent self-referencing macros." | |||
| 849 | `(hif-invoke (quote ,ident) (quote ,parmlist)))) | 853 | `(hif-invoke (quote ,ident) (quote ,parmlist)))) |
| 850 | 854 | ||
| 851 | (defun hif-string-concatenation () | 855 | (defun hif-string-concatenation () |
| 852 | "Parse concatenated strings: string | strings string" | 856 | "Parse concatenated strings: string | strings string." |
| 853 | (let ((result (substring-no-properties hif-token))) | 857 | (let ((result (substring-no-properties hif-token))) |
| 854 | (while (stringp (hif-nexttoken)) | 858 | (while (stringp (hif-nexttoken)) |
| 855 | (setq result (concat | 859 | (setq result (concat |
| @@ -858,11 +862,11 @@ is applied when invoking macros to prevent self-referencing macros." | |||
| 858 | result)) | 862 | result)) |
| 859 | 863 | ||
| 860 | (defun hif-define-macro (parmlist token-body) | 864 | (defun hif-define-macro (parmlist token-body) |
| 861 | "A marker for defined macro with arguments, cannot be evaluated alone with | 865 | "A marker for defined macro with arguments. |
| 862 | no parameters inputed." | 866 | This macro cannot be evaluated alone without parameters inputed." |
| 863 | ;;TODO: input arguments at run time, use minibuffer to query all arguments | 867 | ;;TODO: input arguments at run time, use minibuffer to query all arguments |
| 864 | (error | 868 | (error |
| 865 | "Argumented macro cannot be evaluated without passing any parameter.")) | 869 | "Argumented macro cannot be evaluated without passing any parameter")) |
| 866 | 870 | ||
| 867 | (defun hif-stringify (a) | 871 | (defun hif-stringify (a) |
| 868 | "Stringify a number, string or symbol." | 872 | "Stringify a number, string or symbol." |
| @@ -881,11 +885,11 @@ no parameters inputed." | |||
| 881 | (intern str))) | 885 | (intern str))) |
| 882 | 886 | ||
| 883 | (defun hif-token-concat (a b) | 887 | (defun hif-token-concat (a b) |
| 884 | "Concatenate two tokens into a longer token, currently support only simple | 888 | "Concatenate two tokens into a longer token. |
| 885 | token concatenation. Also support weird (but valid) token concatenation like | 889 | Currently support only simple token concatenation. Also support weird (but |
| 886 | '>' ## '>' becomes '>>'. Here we take care only those that can be evaluated | 890 | valid) token concatenation like '>' ## '>' becomes '>>'. Here we take care only |
| 887 | during preprocessing time and ignore all those that can only be evaluated at | 891 | those that can be evaluated during preprocessing time and ignore all those that |
| 888 | C(++) runtime (like '++', '--' and '+='...)." | 892 | can only be evaluated at C(++) runtime (like '++', '--' and '+='...)." |
| 889 | (if (or (memq a hif-valid-token-list) | 893 | (if (or (memq a hif-valid-token-list) |
| 890 | (memq b hif-valid-token-list)) | 894 | (memq b hif-valid-token-list)) |
| 891 | (let* ((ra (car (rassq a hif-token-alist))) | 895 | (let* ((ra (car (rassq a hif-token-alist))) |
| @@ -895,7 +899,7 @@ C(++) runtime (like '++', '--' and '+='...)." | |||
| 895 | (or result | 899 | (or result |
| 896 | ;;(error "Invalid token to concatenate") | 900 | ;;(error "Invalid token to concatenate") |
| 897 | (error "Concatenating \"%s\" and \"%s\" does not give a valid \ | 901 | (error "Concatenating \"%s\" and \"%s\" does not give a valid \ |
| 898 | preprocessing token." | 902 | preprocessing token" |
| 899 | (or ra (symbol-name a)) | 903 | (or ra (symbol-name a)) |
| 900 | (or rb (symbol-name b))))) | 904 | (or rb (symbol-name b))))) |
| 901 | (intern-safe (concat (hif-stringify a) | 905 | (intern-safe (concat (hif-stringify a) |
| @@ -955,7 +959,7 @@ preprocessing token." | |||
| 955 | 959 | ||
| 956 | 960 | ||
| 957 | (defun hif-comma (&rest expr) | 961 | (defun hif-comma (&rest expr) |
| 958 | "Evaluate a list of expr, return the result of the last item." | 962 | "Evaluate a list of EXPR, return the result of the last item." |
| 959 | (let ((result nil)) | 963 | (let ((result nil)) |
| 960 | (dolist (e expr) | 964 | (dolist (e expr) |
| 961 | (ignore-errors | 965 | (ignore-errors |
| @@ -963,8 +967,7 @@ preprocessing token." | |||
| 963 | result)) | 967 | result)) |
| 964 | 968 | ||
| 965 | (defun hif-token-stringification (l) | 969 | (defun hif-token-stringification (l) |
| 966 | "Scan token list for 'hif-stringify' ('#') token and stringify the next | 970 | "Scan token list for `hif-stringify' ('#') token and stringify the next token." |
| 967 | token." | ||
| 968 | (let (result) | 971 | (let (result) |
| 969 | (while l | 972 | (while l |
| 970 | (push (if (eq (car l) 'hif-stringify) | 973 | (push (if (eq (car l) 'hif-stringify) |
| @@ -979,8 +982,7 @@ token." | |||
| 979 | (nreverse result))) | 982 | (nreverse result))) |
| 980 | 983 | ||
| 981 | (defun hif-token-concatenation (l) | 984 | (defun hif-token-concatenation (l) |
| 982 | "Scan token list for 'hif-token-concat' ('##') token and concatenate two | 985 | "Scan token list for `hif-token-concat' ('##') token and concatenate two tokens." |
| 983 | tokens." | ||
| 984 | (let ((prev nil) | 986 | (let ((prev nil) |
| 985 | result) | 987 | result) |
| 986 | (while l | 988 | (while l |
| @@ -1014,7 +1016,6 @@ tokens." | |||
| 1014 | (cddr SA))) | 1016 | (cddr SA))) |
| 1015 | (formal-parms (and macro (car macro))) | 1017 | (formal-parms (and macro (car macro))) |
| 1016 | (macro-body (and macro (cadr macro))) | 1018 | (macro-body (and macro (cadr macro))) |
| 1017 | (hide-ifdef-local-env nil) ; dynamic binding local table | ||
| 1018 | actual-count | 1019 | actual-count |
| 1019 | formal-count | 1020 | formal-count |
| 1020 | actual | 1021 | actual |
| @@ -1023,10 +1024,10 @@ tokens." | |||
| 1023 | 1024 | ||
| 1024 | (when (and actual-parms formal-parms macro-body) | 1025 | (when (and actual-parms formal-parms macro-body) |
| 1025 | ;; For each actual parameter, evaluate each one and associate it | 1026 | ;; For each actual parameter, evaluate each one and associate it |
| 1026 | ;; with the associated actual parameter, put it into local table and finally | 1027 | ;; with an actual parameter, put it into local table and finally |
| 1027 | ;; evaluate the macro body. | 1028 | ;; evaluate the macro body. |
| 1028 | (if (setq etc (eq (car formal-parms) 'hif-etc)) | 1029 | (if (setq etc (eq (car formal-parms) 'hif-etc)) |
| 1029 | ;; Take care of 'hif-etc first. Prefix 'hif-comma back if needed. | 1030 | ;; Take care of `hif-etc' first. Prefix `hif-comma' back if needed. |
| 1030 | (setq formal-parms (cdr formal-parms))) | 1031 | (setq formal-parms (cdr formal-parms))) |
| 1031 | (setq formal-count (length formal-parms) | 1032 | (setq formal-count (length formal-parms) |
| 1032 | actual-count (length actual-parms)) | 1033 | actual-count (length actual-parms)) |
| @@ -1037,9 +1038,9 @@ tokens." | |||
| 1037 | (or etc | 1038 | (or etc |
| 1038 | (error "Too many parameters for macro %S" macro-name)))) | 1039 | (error "Too many parameters for macro %S" macro-name)))) |
| 1039 | 1040 | ||
| 1040 | ;; Perform token replacement on the macro-body on the parameters | 1041 | ;; Perform token replacement on the MACRO-BODY with the parameters |
| 1041 | (while (setq formal (pop formal-parms)) | 1042 | (while (setq formal (pop formal-parms)) |
| 1042 | ;; Prevent repetitive substitutation, thus cannot use 'subst' | 1043 | ;; Prevent repetitive substitutation, thus cannot use `subst' |
| 1043 | ;; for example: | 1044 | ;; for example: |
| 1044 | ;; #define mac(a,b) (a+b) | 1045 | ;; #define mac(a,b) (a+b) |
| 1045 | ;; #define testmac mac(b,y) | 1046 | ;; #define testmac mac(b,y) |
| @@ -1051,7 +1052,7 @@ tokens." | |||
| 1051 | ;; 2. formal parm #2 'b' replaced by actual parm 'y', thus (b+b) | 1052 | ;; 2. formal parm #2 'b' replaced by actual parm 'y', thus (b+b) |
| 1052 | ;; becomes (y+y). | 1053 | ;; becomes (y+y). |
| 1053 | (setq macro-body | 1054 | (setq macro-body |
| 1054 | ;; Unlike 'subst', 'substitute' replace only the top level | 1055 | ;; Unlike `subst', `substitute' replace only the top level |
| 1055 | ;; instead of the whole tree; more importantly, it's not | 1056 | ;; instead of the whole tree; more importantly, it's not |
| 1056 | ;; destructive. | 1057 | ;; destructive. |
| 1057 | (substitute (if (and etc (null formal-parms)) | 1058 | (substitute (if (and etc (null formal-parms)) |
| @@ -1067,8 +1068,7 @@ tokens." | |||
| 1067 | (hif-token-concatenation (hif-token-stringification macro-body))))) | 1068 | (hif-token-concatenation (hif-token-stringification macro-body))))) |
| 1068 | 1069 | ||
| 1069 | (defun hif-invoke (macro-name actual-parms) | 1070 | (defun hif-invoke (macro-name actual-parms) |
| 1070 | "Invoke a macro by first expanding it, then reparse the macro-body, | 1071 | "Invoke a macro by expanding it, reparse macro-body and finally invoke it." |
| 1071 | finally invoke the macro." | ||
| 1072 | ;; Reparse the macro body and evaluate it | 1072 | ;; Reparse the macro body and evaluate it |
| 1073 | (funcall hide-ifdef-evaluator | 1073 | (funcall hide-ifdef-evaluator |
| 1074 | (hif-parse-exp | 1074 | (hif-parse-exp |
| @@ -1078,7 +1078,7 @@ finally invoke the macro." | |||
| 1078 | ;;;----------- end of parser ----------------------- | 1078 | ;;;----------- end of parser ----------------------- |
| 1079 | 1079 | ||
| 1080 | 1080 | ||
| 1081 | (defun hif-canonicalize-tokens (regexp) ;; for debugging | 1081 | (defun hif-canonicalize-tokens (regexp) ; For debugging |
| 1082 | "Return the expanded result of the scanned tokens." | 1082 | "Return the expanded result of the scanned tokens." |
| 1083 | (save-excursion | 1083 | (save-excursion |
| 1084 | (re-search-forward regexp) | 1084 | (re-search-forward regexp) |
| @@ -1096,8 +1096,8 @@ finally invoke the macro." | |||
| 1096 | tokens))) | 1096 | tokens))) |
| 1097 | 1097 | ||
| 1098 | (defun hif-canonicalize (regexp) | 1098 | (defun hif-canonicalize (regexp) |
| 1099 | "When at beginning of `regexp' (i.e. #ifX), return a Lisp expression for | 1099 | "Return a Lisp expression for its condition by scanning current buffer. |
| 1100 | its condition." | 1100 | Do this when cursor is at the beginning of `regexp' (i.e. #ifX)." |
| 1101 | (let ((case-fold-search nil)) | 1101 | (let ((case-fold-search nil)) |
| 1102 | (save-excursion | 1102 | (save-excursion |
| 1103 | (re-search-forward regexp) | 1103 | (re-search-forward regexp) |
| @@ -1105,7 +1105,7 @@ its condition." | |||
| 1105 | (defined (string-match hif-ifxdef-regexp curr-regexp)) | 1105 | (defined (string-match hif-ifxdef-regexp curr-regexp)) |
| 1106 | (negate (and defined | 1106 | (negate (and defined |
| 1107 | (string= (match-string 2 curr-regexp) "n"))) | 1107 | (string= (match-string 2 curr-regexp) "n"))) |
| 1108 | (hif-simple-token-only nil) ;; Dynamic binding for `hif-tokenize' | 1108 | (hif-simple-token-only nil) ; Dynamic binding for `hif-tokenize' |
| 1109 | (tokens (hif-tokenize (point) | 1109 | (tokens (hif-tokenize (point) |
| 1110 | (progn (hif-end-of-line) (point))))) | 1110 | (progn (hif-end-of-line) (point))))) |
| 1111 | (if defined | 1111 | (if defined |
| @@ -1139,8 +1139,8 @@ its condition." | |||
| 1139 | (beginning-of-line))) | 1139 | (beginning-of-line))) |
| 1140 | 1140 | ||
| 1141 | 1141 | ||
| 1142 | (defun hif-looking-at-ifX () ;; Should eventually see #if | 1142 | (defun hif-looking-at-ifX () |
| 1143 | (looking-at hif-ifx-regexp)) | 1143 | (looking-at hif-ifx-regexp)) ; Should eventually see #if |
| 1144 | (defun hif-looking-at-endif () | 1144 | (defun hif-looking-at-endif () |
| 1145 | (looking-at hif-endif-regexp)) | 1145 | (looking-at hif-endif-regexp)) |
| 1146 | (defun hif-looking-at-else () | 1146 | (defun hif-looking-at-else () |
| @@ -1155,12 +1155,12 @@ its condition." | |||
| 1155 | ;; (message "hif-ifdef-to-endif at %d" (point)) (sit-for 1) | 1155 | ;; (message "hif-ifdef-to-endif at %d" (point)) (sit-for 1) |
| 1156 | (hif-find-next-relevant) | 1156 | (hif-find-next-relevant) |
| 1157 | (cond ((hif-looking-at-ifX) | 1157 | (cond ((hif-looking-at-ifX) |
| 1158 | (hif-ifdef-to-endif) ; find endif of nested if | 1158 | (hif-ifdef-to-endif) ; Find endif of nested if |
| 1159 | (hif-ifdef-to-endif)) ; find outer endif or else | 1159 | (hif-ifdef-to-endif)) ; Find outer endif or else |
| 1160 | ((hif-looking-at-elif) | 1160 | ((hif-looking-at-elif) |
| 1161 | (hif-ifdef-to-endif)) | 1161 | (hif-ifdef-to-endif)) |
| 1162 | ((hif-looking-at-else) | 1162 | ((hif-looking-at-else) |
| 1163 | (hif-ifdef-to-endif)) ; find endif following else | 1163 | (hif-ifdef-to-endif)) ; Find endif following else |
| 1164 | ((hif-looking-at-endif) | 1164 | ((hif-looking-at-endif) |
| 1165 | 'done) | 1165 | 'done) |
| 1166 | (t | 1166 | (t |
| @@ -1328,10 +1328,11 @@ Point is left unchanged." | |||
| 1328 | (hif-make-range start end else)))) | 1328 | (hif-make-range start end else)))) |
| 1329 | 1329 | ||
| 1330 | 1330 | ||
| 1331 | ;;; A bit slimy. | 1331 | ;; A bit slimy. |
| 1332 | 1332 | ||
| 1333 | (defun hif-hide-line (point) | 1333 | (defun hif-hide-line (point) |
| 1334 | "Hide the line containing point. Does nothing if `hide-ifdef-lines' is nil." | 1334 | "Hide the line containing point. |
| 1335 | Does nothing if `hide-ifdef-lines' is nil." | ||
| 1335 | (when hide-ifdef-lines | 1336 | (when hide-ifdef-lines |
| 1336 | (save-excursion | 1337 | (save-excursion |
| 1337 | (goto-char point) | 1338 | (goto-char point) |
| @@ -1404,13 +1405,13 @@ It uses the judgment of `hide-ifdef-evaluator'." | |||
| 1404 | (end-of-line))) | 1405 | (end-of-line))) |
| 1405 | 1406 | ||
| 1406 | (defun hif-parse-macro-arglist (str) | 1407 | (defun hif-parse-macro-arglist (str) |
| 1407 | "Parse argument list formatted as '( arg1 [ , argn] [...] )', including | 1408 | "Parse argument list formatted as '( arg1 [ , argn] [...] )'. |
| 1408 | the '...'. Return a list of the arguments, if '...' exists the first arg | 1409 | The '...' is also included. Return a list of the arguments, if '...' exists the |
| 1409 | will be hif-etc." | 1410 | first arg will be `hif-etc'." |
| 1410 | (let* ((hif-simple-token-only nil) ;; Dynamic binding var for `hif-tokenize' | 1411 | (let* ((hif-simple-token-only nil) ; Dynamic binding var for `hif-tokenize' |
| 1411 | (tokenlist | 1412 | (tokenlist |
| 1412 | (cdr (hif-tokenize | 1413 | (cdr (hif-tokenize |
| 1413 | (- (point) (length str)) (point)))) ; remove hif-lparen | 1414 | (- (point) (length str)) (point)))) ; Remove `hif-lparen' |
| 1414 | etc result token) | 1415 | etc result token) |
| 1415 | (while (not (eq (setq token (pop tokenlist)) 'hif-rparen)) | 1416 | (while (not (eq (setq token (pop tokenlist)) 'hif-rparen)) |
| 1416 | (cond | 1417 | (cond |
| @@ -1462,7 +1463,7 @@ will be hif-etc." | |||
| 1462 | (name (and (re-search-forward hif-macroref-regexp max t) | 1463 | (name (and (re-search-forward hif-macroref-regexp max t) |
| 1463 | (match-string 1))) | 1464 | (match-string 1))) |
| 1464 | (parsed nil) | 1465 | (parsed nil) |
| 1465 | (parmlist (and (match-string 3) ;; First arg id found | 1466 | (parmlist (and (match-string 3) ; First arg id found |
| 1466 | (hif-parse-macro-arglist (match-string 2))))) | 1467 | (hif-parse-macro-arglist (match-string 2))))) |
| 1467 | (if defining | 1468 | (if defining |
| 1468 | ;; Ignore name (still need to return 't), or define the name | 1469 | ;; Ignore name (still need to return 't), or define the name |
| @@ -1472,7 +1473,7 @@ will be hif-etc." | |||
| 1472 | 1473 | ||
| 1473 | (let* ((start (point)) | 1474 | (let* ((start (point)) |
| 1474 | (end (progn (hif-end-of-line) (point))) | 1475 | (end (progn (hif-end-of-line) (point))) |
| 1475 | (hif-simple-token-only nil) ;; Dynamic binding | 1476 | (hif-simple-token-only nil) ; Dynamic binding |
| 1476 | (tokens | 1477 | (tokens |
| 1477 | (and name | 1478 | (and name |
| 1478 | ;; `hif-simple-token-only' is set/clear | 1479 | ;; `hif-simple-token-only' is set/clear |
| @@ -1516,7 +1517,7 @@ will be hif-etc." | |||
| 1516 | 1517 | ||
| 1517 | 1518 | ||
| 1518 | (defun hif-add-new-defines (&optional min max) | 1519 | (defun hif-add-new-defines (&optional min max) |
| 1519 | "Scan and add all #define macros between MIN and MAX" | 1520 | "Scan and add all #define macros between MIN and MAX." |
| 1520 | (interactive) | 1521 | (interactive) |
| 1521 | (save-excursion | 1522 | (save-excursion |
| 1522 | (save-restriction | 1523 | (save-restriction |
| @@ -1596,13 +1597,6 @@ It does not do the work that's pointless to redo on a recursive entry." | |||
| 1596 | (setq hide-ifdef-env | 1597 | (setq hide-ifdef-env |
| 1597 | (delete (assoc var hide-ifdef-env) hide-ifdef-env))) | 1598 | (delete (assoc var hide-ifdef-env) hide-ifdef-env))) |
| 1598 | 1599 | ||
| 1599 | ;;(defun hide-ifdef-undef (var) | ||
| 1600 | ;; "Undefine a VAR so that #ifdef VAR would not be included." | ||
| 1601 | ;; (interactive "SUndefine what? ") | ||
| 1602 | ;; ;;(hif-set-var var nil);;Luke fixed: set it nil is still considered | ||
| 1603 | ;; ;;defined so #ifdef VAR is still true. | ||
| 1604 | ;; (hif-undefine-symbol var) | ||
| 1605 | ;; (if hide-ifdef-hiding (hide-ifdefs))) | ||
| 1606 | 1600 | ||
| 1607 | (defun hide-ifdef-undef (start end) | 1601 | (defun hide-ifdef-undef (start end) |
| 1608 | "Undefine a VAR so that #ifdef VAR would not be included." | 1602 | "Undefine a VAR so that #ifdef VAR would not be included." |