* bug#17854: The patch #3 of 3 for hideif.el, a lot of bug fixes and enhancements
@ 2014-06-26 13:51 Luke Lee
2014-06-26 16:56 ` Glenn Morris
` (2 more replies)
0 siblings, 3 replies; 20+ messages in thread
From: Luke Lee @ 2014-06-26 13:51 UTC (permalink / raw)
To: 17854
[-- Attachment #1.1: Type: text/plain, Size: 402 bytes --]
Hi all,
Attached is the bug fix and enhancements patchset #3 for hideifdef
mode to complete my earlier posted hideif.el on
http://www.emacswiki.org/emacs/hideif.el .
The earlier two patches are already submitted into Emacs trunk and
had reviewed by Stefan (Thanks Stefan!) Now I post the 3rd patch
here. Please help review my changes and give me your suggestions.
Thanks!
--
Best regards,
Luke Lee
[-- Attachment #1.2: Type: text/html, Size: 644 bytes --]
[-- Attachment #2: 0003-Other-hideif-enhancements-and-bug-fixes.patch --]
[-- Type: application/octet-stream, Size: 55529 bytes --]
From 9ecac67d011ac25904068606d9adc742c6eebeba Mon Sep 17 00:00:00 2001
From: Luke Lee <luke.yx.lee@gmail.com>
Date: Thu, 26 Jun 2014 21:29:25 +0800
Subject: [PATCH] Other hideif enhancements and bug fixes: * Add macro
evaluation function and key binding. * Merge continuous "..." into one. * Fix
original bug that sometimes fail to hide the correct portion in a long
ifdefs containing elif and else. * Support hide/show commands in a marked
region. * Expand top level for .h files to prevent re-inclusion protection
codes from being hidden. * Others.
---
lisp/progmodes/hideif.el | 839 ++++++++++++++++++++++++++++++-----------------
1 file changed, 542 insertions(+), 297 deletions(-)
diff --git a/lisp/progmodes/hideif.el b/lisp/progmodes/hideif.el
index b0ca4f0..a15c5b7 100644
--- a/lisp/progmodes/hideif.el
+++ b/lisp/progmodes/hideif.el
@@ -3,7 +3,7 @@
;; Copyright (C) 1988, 1994, 2001-2014 Free Software Foundation, Inc.
;; Author: Brian Marick
-;; Daniel LaLiberte <liberte@holonexus.org>
+;; Daniel LaLiberte <liberte@holonexus.org>
;; Maintainer: emacs-devel@gnu.org
;; Keywords: c, outlines
@@ -36,7 +36,7 @@
;;
;; Hide-ifdef suppresses the display of code that the preprocessor wouldn't
;; pass through. Support complete C/C++ expression and precedence.
-;; It will automatically scans for new #define symbols and macros on the way
+;; It will automatically scan for new #define symbols and macros on the way
;; parsing.
;;
;; The hidden code is marked by ellipses (...). Be
@@ -66,11 +66,11 @@
;;
;; (add-hook 'hide-ifdef-mode-hook
;; (lambda ()
-;; (unless 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
+;; (unless 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.
@@ -129,16 +129,44 @@
"Non-nil means shadow text instead of hiding it."
:type 'boolean
:group 'hide-ifdef
- :version "23.1")
+ :version "24.5")
(defface hide-ifdef-shadow '((t (:inherit shadow)))
"Face for shadowing ifdef blocks."
:group 'hide-ifdef
- :version "23.1")
+ :version "24.5")
(defcustom hide-ifdef-exclude-define-regexp nil
"Ignore #define names if those names match this exclusion pattern."
:type 'string)
+(defcustom hide-ifdef-expand-reinclusion-protection t
+ "When hiding header files, enabling this flag allows hideif always try to
+expand the re-inclusion protected ifdefs. Disabling this flag those headers
+are usually hidden to a top level #ifdef...#endif due to those defined symbols.
+
+Most C/C++ header files are wrapped with ifdefs to prevent re-inclusion of this
+form
+
+ ----- beginning of file -----
+ #ifdef _XXX_HEADER_FILE_INCLUDED_
+ #define _XXX_HEADER_FILE_INCLUDED_
+ ...
+ #endif
+ ----- end of file -----
+
+Hence, if we try to hide this header file twice, it will hide everything between
+the top level #ifdef ... #endif when you tried to hide this file the second time
+since _XXX_HEADER_FILE_INCLUDED_ is defined at the first time when you we hide
+it."
+ :type 'boolean
+ :group 'hide-ifdef)
+
+(defcustom hide-ifdef-header-regexp-pattern
+ "^.*\\.[hH]\\([hH]\\|[xX][xX]\\|[pP][pP]\\)?"
+ "C/C++ header file name patterns. Effective only if
+`hide-ifdef-expand-reinclusion-protection' is t."
+ :type 'string
+ :group 'hide-ifdef)
(defvar hide-ifdef-mode-submap
;; Set up the submap that goes after the prefix key.
@@ -152,6 +180,8 @@
(define-key map "s" 'show-ifdefs)
(define-key map "\C-d" 'hide-ifdef-block)
(define-key map "\C-s" 'show-ifdef-block)
+ (define-key map "e" 'hif-evaluate-macro)
+ (define-key map "C" 'hif-clear-all-ifdef-defined)
(define-key map "\C-q" 'hide-ifdef-toggle-read-only)
(define-key map "\C-w" 'hide-ifdef-toggle-shadowing)
@@ -204,7 +234,7 @@
(cons '(hide-ifdef-hiding " Hiding")
minor-mode-alist)))
-;; fix c-mode syntax table so we can recognize whole symbols.
+;; Fix c-mode syntax table so we can recognize whole symbols.
(defvar hide-ifdef-syntax-table
(let ((st (copy-syntax-table c-mode-syntax-table)))
(modify-syntax-entry ?_ "w" st)
@@ -215,6 +245,9 @@
(defvar hide-ifdef-env nil
"An alist of defined symbols and their values.")
+(defvar hide-ifdef-env-backup nil
+ "A backup variable to prevent `hide-ifdef-env' accidentally cleared by
+`hif-clear-all-ifdef-defined'.")
(defvar hif-outside-read-only nil
"Internal variable. Saves the value of `buffer-read-only' while hiding.")
@@ -232,53 +265,64 @@ that the C preprocessor would eliminate may be hidden from view.
Several variables affect how the hiding is done:
`hide-ifdef-env'
- An association list of defined and undefined symbols for the
- current buffer. Initially, the global value of `hide-ifdef-env'
- is used.
+ An association list of defined and undefined symbols for the
+ current project. Initially, the global value of `hide-ifdef-env'
+ is used. This variable was a buffer-local variable but is now a
+ global variable since we've extend hideif to support project-based
+ across all-buffers. To simulate the original buffer local behavior
+ we need to clear this variable (C-c @ C) then hide current buffer.
`hide-ifdef-define-alist'
- An association list of defined symbol lists.
+ An association list of defined symbol lists.
Use `hide-ifdef-set-define-alist' to save the current `hide-ifdef-env'
and `hide-ifdef-use-define-alist' to set the current `hide-ifdef-env'
from one of the lists in `hide-ifdef-define-alist'.
`hide-ifdef-lines'
- Set to non-nil to not show #if, #ifdef, #ifndef, #else, and
- #endif lines when hiding.
+ Set to non-nil to not show #if, #ifdef, #ifndef, #else, and
+ #endif lines when hiding.
`hide-ifdef-initially'
- Indicates whether `hide-ifdefs' should be called when Hide-Ifdef mode
- is activated.
+ Indicates whether `hide-ifdefs' should be called when Hide-Ifdef mode
+ is activated.
`hide-ifdef-read-only'
- Set to non-nil if you want to make buffers read only while hiding.
- After `show-ifdefs', read-only status is restored to previous value.
+ Set to non-nil if you want to make buffers read only while hiding.
+ After `show-ifdefs', read-only status is restored to previous value.
\\{hide-ifdef-mode-map}"
:group 'hide-ifdef :lighter " Ifdef"
(if hide-ifdef-mode
(progn
- ;; inherit global values
- (set (make-local-variable 'hide-ifdef-env)
- (default-value 'hide-ifdef-env))
- (set (make-local-variable 'hide-ifdef-hiding)
- (default-value 'hide-ifdef-hiding))
- (set (make-local-variable 'hif-outside-read-only) buffer-read-only)
- (set (make-local-variable 'line-move-ignore-invisible) t)
- (add-hook 'change-major-mode-hook
- (lambda () (hide-ifdef-mode -1)) nil t)
-
- (add-to-invisibility-spec '(hide-ifdef . t))
-
- (if hide-ifdef-initially
- (hide-ifdefs)
- (show-ifdefs)))
+ ;; inherit global values
+;; (set (make-local-variable 'hide-ifdef-env)
+;; (default-value 'hide-ifdef-env))
+ (set 'hide-ifdef-env (default-value 'hide-ifdef-env))
+ (set (make-local-variable 'hide-ifdef-hiding)
+ (default-value 'hide-ifdef-hiding))
+ (set (make-local-variable 'hif-outside-read-only) buffer-read-only)
+ (set (make-local-variable 'line-move-ignore-invisible) t)
+ (add-hook 'change-major-mode-hook
+ (lambda () (hide-ifdef-mode -1)) nil t)
+
+ (add-to-invisibility-spec '(hide-ifdef . t))
+
+ (if hide-ifdef-initially
+ (hide-ifdefs)
+ (show-ifdefs)))
;; else end hide-ifdef-mode
(kill-local-variable 'line-move-ignore-invisible)
(remove-from-invisibility-spec '(hide-ifdef . t))
(when hide-ifdef-hiding
(show-ifdefs))))
+(defun hif-clear-all-ifdef-defined ()
+ "Clear all variables in `hide-ifdef-env'. It will backup this variable to
+`hide-ifdef-env-backup' before clearing to prevent accidental clearance."
+ (interactive)
+ (when (y-or-n-p "Clear all #defined symbols?")
+ (setq hide-ifdef-env-backup hide-ifdef-env)
+ (setq hide-ifdef-env nil)))
(defun hif-show-all ()
"Show all of the text in the current buffer."
@@ -298,16 +342,62 @@ Several variables affect how the hiding is done:
(while (= (logand 1 (skip-chars-backward "\\\\")) 1)
(end-of-line 2)))
+(defun hif-merge-ifdef-region (start end)
+ "Merge nearby ifdef regions to form a bigger overlay. This will decrease
+the number of overlays created."
+ ;; 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' equals to nil while another part with 't,
+ ;; this case happens.
+ ;; TODO: Should we merge? or just create a container overlay? -- this can
+ ;; prevent `hideif-show-ifdef' expanding too many hidden contents since there
+ ;; is only a big overlay exists there without any smaller overlays.
+ (save-restriction
+ (widen) ; Otherwise `point-min' and `point-max' will be restricted and thus
+ ; fail to find neighbor overlays
+ (let ((begovrs (overlays-in
+ (max (- start 2) (point-min))
+ (max (- start 1) (point-min))))
+ (endovrs (overlays-in
+ (min (+ end 1) (point-max))
+ (min (+ end 2) (point-max))))
+ (ob nil)
+ (oe nil)
+ b e)
+ ;; Merge overlays before START
+ (dolist (o begovrs)
+ (when (overlay-get o 'hide-ifdef)
+ (setq b (min start (overlay-start o))
+ e (max end (overlay-end o)))
+ (move-overlay o b e)
+ (hif-merge-ifdef-region b e)
+ (setq ob o)))
+ ;; Merge overlays after END
+ (dolist (o endovrs)
+ (when (overlay-get o 'hide-ifdef)
+ (setq b (min start (overlay-start o))
+ e (max end (overlay-end o)))
+ (move-overlay o b e)
+ (hif-merge-ifdef-region b e)
+ (setf oe o)))
+ ;; If both START and END merging happens, merge into bigger one
+ (when (and ob oe)
+ (let ((b (min (overlay-start ob) (overlay-start oe)))
+ (e (max (overlay-end ob) (overlay-end oe))))
+ (delete-overlay oe)
+ (move-overlay ob b e)
+ (hif-merge-ifdef-region b e)))
+ (or ob oe))))
(defun hide-ifdef-region-internal (start end)
- (remove-overlays start end 'hide-ifdef t)
+ (unless (hif-merge-ifdef-region start end)
(let ((o (make-overlay start end)))
(overlay-put o 'hide-ifdef t)
(if hide-ifdef-shadow
- (overlay-put o 'face 'hide-ifdef-shadow)
- (overlay-put o 'invisible 'hide-ifdef))))
+ (overlay-put o 'face 'hide-ifdef-shadow)
+ (overlay-put o 'invisible 'hide-ifdef)))))
(defun hide-ifdef-region (start end)
- "START is the start of a #if or #else form. END is the ending part.
+ "START is the start of a #if, #elif, or #else form. END is the ending part.
Everything including these lines is made invisible."
(save-excursion
(goto-char start) (hif-end-of-line) (setq start (point))
@@ -316,7 +406,9 @@ Everything including these lines is made invisible."
(defun hif-show-ifdef-region (start end)
"Everything between START and END is made visible."
- (remove-overlays start end 'hide-ifdef t))
+ (let ((onum (length (overlays-in start end))))
+ (remove-overlays start end 'hide-ifdef t)
+ (/= onum (length (overlays-in start end)))))
;;===%%SF%% evaluation (Start) ===
@@ -341,11 +433,11 @@ that form should be displayed.")
(defun hif-lookup (var)
(or (when (bound-and-true-p semantic-c-takeover-hideif)
- (semantic-c-hideif-lookup var))
+ (semantic-c-hideif-lookup var))
(let ((val (assoc var hide-ifdef-env)))
- (if val
- (cdr val)
- hif-undefined-symbol))))
+ (if val
+ (cdr val)
+ hif-undefined-symbol))))
(defun hif-defined (var)
(cond
@@ -375,10 +467,8 @@ that form should be displayed.")
(concat hif-cpp-prefix "\\(if\\(n?def\\)?\\|elif\\|define\\)[ \t]+"))
(defconst hif-white-regexp "[ \t]*")
-(defconst hif-define-regexp
- (concat hif-cpp-prefix "\\(define\\|undef\\)"))
-(defconst hif-id-regexp
- (concat "[[:alpha:]_][[:alnum:]_]*"))
+(defconst hif-define-regexp (concat hif-cpp-prefix "\\(define\\|undef\\)"))
+(defconst hif-id-regexp (concat "[[:alpha:]_][[:alnum:]_]*"))
(defconst hif-macroref-regexp
(concat hif-white-regexp "\\(" hif-id-regexp "\\)" hif-white-regexp
"\\("
@@ -389,8 +479,8 @@ that form should be displayed.")
")"
"\\)?" ))
-;; Used to store the current token and the whole token list during parsing.
-;; Only bound dynamically.
+;; Store the current token and the whole token list during parsing.
+;; Bound dynamically.
(defvar hif-token)
(defvar hif-token-list)
@@ -463,23 +553,23 @@ that form should be displayed.")
(setq hif-simple-token-only t)
(with-syntax-table hide-ifdef-syntax-table
(save-excursion
- (goto-char start)
- (while (progn (forward-comment (point-max)) (< (point) end))
- ;; (message "expr-start = %d" expr-start) (sit-for 1)
- (cond
- ((looking-at "\\\\\n")
- (forward-char 2))
+ (goto-char start)
+ (while (progn (forward-comment (point-max)) (< (point) end))
+ ;; (message "expr-start = %d" expr-start) (sit-for 1)
+ (cond
+ ((looking-at "\\\\\n")
+ (forward-char 2))
((looking-at hif-string-literal-regexp)
(push (substring-no-properties (match-string 1)) token-list)
(goto-char (match-end 0)))
- ((looking-at hif-token-regexp)
+ ((looking-at hif-token-regexp)
(let ((token (buffer-substring-no-properties
(point) (match-end 0))))
- (goto-char (match-end 0))
- ;; (message "token: %s" token) (sit-for 1)
- (push
+ (goto-char (match-end 0))
+ ;; (message "token: %s" token) (sit-for 1)
+ (push
(or (cdr (assoc token hif-token-alist))
(if (string-equal token "defined") 'hif-defined)
;; TODO:
@@ -498,7 +588,9 @@ that form should be displayed.")
(setq hif-simple-token-only nil)))
token-list)))
- (t (error "Bad #if expression: %s" (buffer-string)))))))
+ ((looking-at "\r") ; Sometimes MS-Windows user will leave CR in
+ (forward-char 1)) ; the source code. Let's don't stuck here.
+ (t (error "Bad #if expression: %s" (buffer-string)))))))
(nreverse token-list)))
@@ -594,9 +686,9 @@ subsitituted"
((setq rep (hif-lookup tok))
(if (and (listp rep)
- (eq (car rep) 'hif-define-macro)) ;; a defined macro
+ (eq (car rep) 'hif-define-macro)) ; a defined macro
;; Recursively expand it
- (if (cadr rep) ;; Argument list is not nil
+ (if (cadr rep) ; Argument list is not nil
(if (not (eq (car remains) 'hif-lparen))
;; No argument, no invocation
tok
@@ -616,7 +708,7 @@ subsitituted"
result))
;; Argument list is nil, direct expansion
(setq rep (hif-expand-token-list
- (caddr rep) ;; Macro's token list
+ (caddr rep) ; Macro's token list
tok expand_list))
;; Replace all remaining references immediately
(setq remains (substitute tok rep remains))
@@ -627,12 +719,12 @@ subsitituted"
;;[2013-10-22 16:06:12 +0800] Must keep the token, removing
;; this token might results in an incomplete expression that
;; cannot be parsed further.
- ;;((= 1 (hif-defined tok)) ;; defined (hif-defined tok)=1,
+ ;;((= 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
+ (t ; Usual IDs
tok))
expanded))
@@ -654,28 +746,28 @@ is applied when invoking macros to prevent self-referencing macros."
"Parse an exprlist: expr { ',' expr}"
(let ((result (hif-expr)))
(if (eq hif-token 'hif-comma)
- (let ((temp (list result)))
- (while
- (progn
- (hif-nexttoken)
- (push (hif-expr) temp)
- (eq hif-token 'hif-comma)))
- (cons 'hif-comma (nreverse temp)))
+ (let ((temp (list result)))
+ (while
+ (progn
+ (hif-nexttoken)
+ (push (hif-expr) temp)
+ (eq hif-token 'hif-comma)))
+ (cons 'hif-comma (nreverse temp)))
result)))
(defun hif-expr ()
"Parse an expression as found in #if.
expr : or-expr | or-expr '?' expr ':' expr."
(let ((result (hif-or-expr))
- middle)
+ middle)
(while (eq hif-token 'hif-conditional)
(hif-nexttoken)
(setq middle (hif-expr))
(if (eq hif-token 'hif-colon)
- (progn
- (hif-nexttoken)
- (setq result (list 'hif-conditional result middle (hif-expr))))
- (error "Error: unexpected token: %s" hif-token)))
+ (progn
+ (hif-nexttoken)
+ (setq result (list 'hif-conditional result middle (hif-expr))))
+ (error "Error: unexpected token: %s" hif-token)))
result))
(defun hif-or-expr ()
@@ -721,7 +813,7 @@ is applied when invoking macros to prevent self-referencing macros."
(defun hif-eq-expr ()
"Parse an eq-expr : comp | eq-expr `=='|`!=' comp."
(let ((result (hif-comp-expr))
- (eq-token nil))
+ (eq-token nil))
(while (memq hif-token '(hif-equal hif-notequal))
(setq eq-token hif-token)
(hif-nexttoken)
@@ -764,7 +856,7 @@ is applied when invoking macros to prevent self-referencing macros."
"Parse an expression with *,/,%.
muldiv : factor | muldiv '*|/|%' factor."
(let ((result (hif-factor))
- (math-op nil))
+ (math-op nil))
(while (memq hif-token '(hif-multiply hif-divide hif-modulo))
(setq math-op hif-token)
(hif-nexttoken)
@@ -787,20 +879,20 @@ is applied when invoking macros to prevent self-referencing macros."
(hif-nexttoken)
(let ((result (hif-exprlist)))
(if (not (eq hif-token 'hif-rparen))
- (error "Bad token in parenthesized expression: %s" hif-token)
- (hif-nexttoken)
- result)))
+ (error "Bad token in parenthesized expression: %s" hif-token)
+ (hif-nexttoken)
+ result)))
((eq hif-token 'hif-defined)
(hif-nexttoken)
(let ((paren (when (eq hif-token 'hif-lparen) (hif-nexttoken) t))
- (ident hif-token))
+ (ident hif-token))
(if (memq hif-token '(or and not hif-defined hif-lparen hif-rparen))
- (error "Error: unexpected token: %s" hif-token))
+ (error "Error: unexpected token: %s" hif-token))
(when paren
- (hif-nexttoken)
+ (hif-nexttoken)
(unless (eq hif-token 'hif-rparen)
- (error "Error: expected \")\" after identifier")))
+ (error "Error: expected \")\" after identifier")))
(hif-nexttoken)
`(hif-defined (quote ,ident))))
@@ -813,7 +905,7 @@ is applied when invoking macros to prevent self-referencing macros."
((memq hif-token '(hif-minus hif-plus))
(list (prog1 hif-token (hif-nexttoken)) 0 (hif-factor)))
- (t ; identifier
+ (t ; identifier
(let ((ident hif-token))
(hif-nexttoken)
(if (eq hif-token 'hif-lparen)
@@ -822,7 +914,7 @@ is applied when invoking macros to prevent self-referencing macros."
(defun hif-get-argument-list (ident)
(let ((nest 0)
- (parmlist nil) ;; A "token" list of parameters, will later be parsed
+ (parmlist nil) ; A "token" list of parameters, will later be parsed
(parm nil))
(while (or (not (eq (hif-nexttoken) 'hif-rparen))
@@ -840,8 +932,8 @@ is applied when invoking macros to prevent self-referencing macros."
(setq parm nil)))
(push hif-token parm))
- (push (nreverse parm) parmlist) ;; Okay even if parm is nil
- (hif-nexttoken) ;; Drop the hif-rparen, get next token
+ (push (nreverse parm) parmlist) ; Okay even if PARM is nil
+ (hif-nexttoken) ; Drop the `hif-rparen', get next token
(nreverse parmlist)))
(defun hif-place-macro-invocation (ident)
@@ -904,8 +996,8 @@ preprocessing token."
(defun hif-mathify (val)
"Treat VAL as a number: if it's t or nil, use 1 or 0."
(cond ((eq val t) 1)
- ((null val) 0)
- (t val)))
+ ((null val) 0)
+ (t val)))
(defun hif-conditional (a b c)
(if (not (zerop (hif-mathify a))) (hif-mathify b) (hif-mathify c)))
@@ -955,7 +1047,7 @@ preprocessing token."
(defun hif-comma (&rest expr)
- "Evaluate a list of expr, return the result of the last item."
+ "Evaluate a list of EXPR, return the result of the last item."
(let ((result nil))
(dolist (e expr)
(ignore-errors
@@ -963,7 +1055,7 @@ preprocessing token."
result))
(defun hif-token-stringification (l)
- "Scan token list for 'hif-stringify' ('#') token and stringify the next
+ "Scan token list for `hif-stringify' ('#') token and stringify the next
token."
(let (result)
(while l
@@ -979,7 +1071,7 @@ token."
(nreverse result)))
(defun hif-token-concatenation (l)
- "Scan token list for 'hif-token-concat' ('##') token and concatenate two
+ "Scan token list for `hif-token-concat' ('##') token and concatenate two
tokens."
(let ((prev nil)
result)
@@ -1014,7 +1106,6 @@ tokens."
(cddr SA)))
(formal-parms (and macro (car macro)))
(macro-body (and macro (cadr macro)))
- (hide-ifdef-local-env nil) ; dynamic binding local table
actual-count
formal-count
actual
@@ -1023,10 +1114,10 @@ tokens."
(when (and actual-parms formal-parms macro-body)
;; For each actual parameter, evaluate each one and associate it
- ;; with the associated actual parameter, put it into local table and finally
+ ;; with an actual parameter, put it into local table and finally
;; evaluate the macro body.
(if (setq etc (eq (car formal-parms) 'hif-etc))
- ;; Take care of 'hif-etc first. Prefix 'hif-comma back if needed.
+ ;; Take care of `hif-etc' first. Prefix `hif-comma' back if needed.
(setq formal-parms (cdr formal-parms)))
(setq formal-count (length formal-parms)
actual-count (length actual-parms))
@@ -1037,9 +1128,9 @@ tokens."
(or etc
(error "Too many parameters for macro %S" macro-name))))
- ;; Perform token replacement on the macro-body on the parameters
+ ;; Perform token replacement on the MACRO-BODY with the parameters
(while (setq formal (pop formal-parms))
- ;; Prevent repetitive substitutation, thus cannot use 'subst'
+ ;; Prevent repetitive substitutation, thus cannot use `subst'
;; for example:
;; #define mac(a,b) (a+b)
;; #define testmac mac(b,y)
@@ -1051,7 +1142,7 @@ tokens."
;; 2. formal parm #2 'b' replaced by actual parm 'y', thus (b+b)
;; becomes (y+y).
(setq macro-body
- ;; Unlike 'subst', 'substitute' replace only the top level
+ ;; Unlike `subst', `substitute' replace only the top level
;; instead of the whole tree; more importantly, it's not
;; destructive.
(substitute (if (and etc (null formal-parms))
@@ -1078,7 +1169,7 @@ finally invoke the macro."
;;;----------- end of parser -----------------------
-(defun hif-canonicalize-tokens (regexp) ;; for debugging
+(defun hif-canonicalize-tokens (regexp) ; For debugging
"Return the expanded result of the scanned tokens."
(save-excursion
(re-search-forward regexp)
@@ -1105,7 +1196,7 @@ its condition."
(defined (string-match hif-ifxdef-regexp curr-regexp))
(negate (and defined
(string= (match-string 2 curr-regexp) "n")))
- (hif-simple-token-only nil) ;; Dynamic binding for `hif-tokenize'
+ (hif-simple-token-only nil) ; Dynamic binding for `hif-tokenize'
(tokens (hif-tokenize (point)
(progn (hif-end-of-line) (point)))))
(if defined
@@ -1139,7 +1230,7 @@ its condition."
(beginning-of-line)))
-(defun hif-looking-at-ifX () ;; Should eventually see #if
+(defun hif-looking-at-ifX () ; Should eventually see #if
(looking-at hif-ifx-regexp))
(defun hif-looking-at-endif ()
(looking-at hif-endif-regexp))
@@ -1155,16 +1246,16 @@ its condition."
;; (message "hif-ifdef-to-endif at %d" (point)) (sit-for 1)
(hif-find-next-relevant)
(cond ((hif-looking-at-ifX)
- (hif-ifdef-to-endif) ; find endif of nested if
- (hif-ifdef-to-endif)) ; find outer endif or else
+ (hif-ifdef-to-endif) ; Find endif of nested if
+ (hif-ifdef-to-endif)) ; Find outer endif or else
((hif-looking-at-elif)
(hif-ifdef-to-endif))
- ((hif-looking-at-else)
- (hif-ifdef-to-endif)) ; find endif following else
- ((hif-looking-at-endif)
- 'done)
- (t
- (error "Mismatched #ifdef #endif pair"))))
+ ((hif-looking-at-else)
+ (hif-ifdef-to-endif)) ; Find endif following else
+ ((hif-looking-at-endif)
+ 'done)
+ (t
+ (error "Mismatched #ifdef #endif pair"))))
(defun hif-endif-to-ifdef ()
@@ -1173,15 +1264,18 @@ its condition."
(let ((start (point)))
(hif-find-previous-relevant)
(if (= start (point))
- (error "Mismatched #ifdef #endif pair")))
+ (error "Mismatched #ifdef #endif pair")))
(cond ((hif-looking-at-endif)
- (hif-endif-to-ifdef) ; find beginning of nested if
- (hif-endif-to-ifdef)) ; find beginning of outer if or else
- ((hif-looking-at-else)
- (hif-endif-to-ifdef))
- ((hif-looking-at-ifX)
- 'done)
- (t))) ; never gets here
+ (hif-endif-to-ifdef) ; Find beginning of nested if
+ (hif-endif-to-ifdef)) ; Find beginning of outer if or else
+ ((hif-looking-at-elif)
+ (hif-endif-to-ifdef))
+ ((hif-looking-at-else)
+ (hif-endif-to-ifdef))
+ ((hif-looking-at-ifX)
+ 'done)
+ (t
+ (error "Mismatched #endif")))) ; never gets here
(defun forward-ifdef (&optional arg)
@@ -1193,12 +1287,12 @@ With argument, do this that many times."
(while (< 0 arg)
(setq arg (- arg))
(let ((start (point)))
- (unless (hif-looking-at-ifX)
- (hif-find-next-relevant))
- (if (hif-looking-at-ifX)
- (hif-ifdef-to-endif)
- (goto-char start)
- (error "No following #ifdef"))))))
+ (unless (hif-looking-at-ifX)
+ (hif-find-next-relevant))
+ (if (hif-looking-at-ifX)
+ (hif-ifdef-to-endif)
+ (goto-char start)
+ (error "No following #ifdef"))))))
(defun backward-ifdef (&optional arg)
@@ -1211,12 +1305,12 @@ With argument, do this that many times."
(setq arg (1- arg))
(beginning-of-line)
(let ((start (point)))
- (unless (hif-looking-at-endif)
- (hif-find-previous-relevant))
- (if (hif-looking-at-endif)
- (hif-endif-to-ifdef)
- (goto-char start)
- (error "No previous #ifdef"))))))
+ (unless (hif-looking-at-endif)
+ (hif-find-previous-relevant))
+ (if (hif-looking-at-endif)
+ (hif-endif-to-ifdef)
+ (goto-char start)
+ (error "No previous #ifdef"))))))
(defun down-ifdef ()
@@ -1225,9 +1319,9 @@ With argument, do this that many times."
(let ((start (point)))
(hif-find-next-relevant)
(if (or (hif-looking-at-ifX) (hif-looking-at-else))
- ()
- (goto-char start)
- (error "No following #ifdef"))))
+ ()
+ (goto-char start)
+ (error "No following #ifdef"))))
(defun up-ifdef ()
@@ -1238,9 +1332,9 @@ With argument, do this that many times."
(unless (hif-looking-at-endif)
(hif-find-previous-relevant))
(if (hif-looking-at-endif)
- (hif-endif-to-ifdef))
+ (hif-endif-to-ifdef))
(if (= start (point))
- (error "No previous #ifdef"))))
+ (error "No previous #ifdef"))))
(defun next-ifdef (&optional arg)
"Move to the beginning of the next #ifX, #else, or #endif.
@@ -1252,8 +1346,8 @@ With argument, do this that many times."
(setq arg (1- arg))
(hif-find-next-relevant)
(when (eolp)
- (beginning-of-line)
- (error "No following #ifdefs, #elses, or #endifs")))))
+ (beginning-of-line)
+ (error "No following #ifdefs, #elses, or #endifs")))))
(defun previous-ifdef (&optional arg)
"Move to the beginning of the previous #ifX, #else, or #endif.
@@ -1264,9 +1358,9 @@ With argument, do this that many times."
(while (< 0 arg)
(setq arg (1- arg))
(let ((start (point)))
- (hif-find-previous-relevant)
- (if (= start (point))
- (error "No previous #ifdefs, #elses, or #endifs"))))))
+ (hif-find-previous-relevant)
+ (if (= start (point))
+ (error "No previous #ifdefs, #elses, or #endifs"))))))
;;===%%SF%% parsing (End) ===
@@ -1275,26 +1369,26 @@ With argument, do this that many times."
;;===%%SF%% hide-ifdef-hiding (Start) ===
-;;; A range is a structure with four components:
-;;; ELSE-P True if there was an else clause for the ifdef.
-;;; START The start of the range. (beginning of line)
-;;; ELSE The else marker (beginning of line)
-;;; Only valid if ELSE-P is true.
-;;; END The end of the range. (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)
- (list start else end))
+(defsubst hif-make-range (start end &optional else elif)
+ (list start else end elif))
(defsubst hif-range-start (range) (elt range 0))
(defsubst hif-range-else (range) (elt range 1))
(defsubst hif-range-end (range) (elt range 2))
+(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.
@@ -1303,32 +1397,36 @@ Point is left unchanged."
(save-excursion
(beginning-of-line)
(let ((start (point))
- (else nil)
- (end nil))
- ;; Part one. Look for either #endif or #else.
+ (elif nil)
+ (else nil)
+ (end nil))
+ ;; Part one. Look for either #elif, #else or #endif.
;; This loop-and-a-half dedicated to E. Dijkstra.
+ (while (and (not else) (not end))
(while (progn
- (hif-find-next-relevant)
- (hif-looking-at-ifX)) ; Skip nested ifdef
- (hif-ifdef-to-endif))
- ;; Found either a #else or an #endif.
- (cond ((hif-looking-at-else)
- (setq else (point)))
- (t
- (setq end (point)))) ; (line-end-position)
+ (hif-find-next-relevant)
+ (hif-looking-at-ifX)) ; Skip nested ifdef
+ (hif-ifdef-to-endif))
+ ;; Found either a #else, #elif, or an #endif.
+ (cond ((hif-looking-at-elif)
+ (setq elif (nconc elif (list (point)))))
+ ((hif-looking-at-else)
+ (setq else (point)))
+ (t
+ (setq end (point)))))
;; If found #else, look for #endif.
(when else
- (while (progn
- (hif-find-next-relevant)
- (hif-looking-at-ifX)) ; Skip nested ifdef
- (hif-ifdef-to-endif))
- (if (hif-looking-at-else)
- (error "Found two elses in a row? Broken!"))
- (setq end (point))) ; (line-end-position)
- (hif-make-range start end else))))
+ (while (progn
+ (hif-find-next-relevant)
+ (hif-looking-at-ifX)) ; Skip nested ifdef
+ (hif-ifdef-to-endif))
+ (if (hif-looking-at-else)
+ (error "Found two elses in a row? Broken!"))
+ (setq end (point))) ; (line-end-position)
+ (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."
@@ -1339,78 +1437,184 @@ 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 #else.
-;;; The #if part must be hidden. The #else part must be processed
-;;; for nested #ifs.
-;;; Case 4: #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.
-
-(defun hif-recurse-on (start end)
+;; 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)
+
+(defun hif-recurse-on (start end &optional dont-go-eol)
"Call `hide-ifdef-guts' after narrowing to end of START line and END line."
(save-excursion
(save-restriction
(goto-char start)
- (end-of-line)
+ (unless dont-go-eol
+ (end-of-line))
(narrow-to-region (point) end)
- (hide-ifdef-guts))))
+ (let ((hif-recurse-level (1+ hif-recurse-level)))
+ (hide-ifdef-guts)))))
-(defun hif-possibly-hide ()
+(defun hif-possibly-hide (expand-reinclusion)
"Called at #ifX expression, this hides those parts that should be hidden.
It uses the judgment of `hide-ifdef-evaluator'."
;; (message "hif-possibly-hide") (sit-for 1)
- (let ((test (hif-canonicalize hif-ifx-regexp))
- (range (hif-find-range)))
+ (let* ((case-fold-search nil)
+ (test (hif-canonicalize hif-ifx-regexp))
+ (range (hif-find-range))
+ (elifs (hif-range-elif range))
+ (if-part t) ;; everytime we start from if-part
+ (complete nil))
;; (message "test = %s" test) (sit-for 1)
(hif-hide-line (hif-range-end range))
- (if (not (hif-not (funcall hide-ifdef-evaluator test)))
- (cond ((hif-range-else range) ; case 1
- (hif-hide-line (hif-range-else range))
- (hide-ifdef-region (hif-range-else range)
- (1- (hif-range-end range)))
- (hif-recurse-on (hif-range-start range)
- (hif-range-else range)))
- (t ; case 2
- (hif-recurse-on (hif-range-start range)
- (hif-range-end range))))
- (cond ((hif-range-else range) ; case 3
- (hif-hide-line (hif-range-else range))
- (hide-ifdef-region (hif-range-start range)
- (1- (hif-range-else range)))
- (hif-recurse-on (hif-range-else range)
- (hif-range-end range)))
- (t ; case 4
- (hide-ifdef-region (point)
- (1- (hif-range-end range))))))
+ (while (not complete)
+ (if (and (not (and expand-reinclusion if-part))
+ (hif-not (funcall hide-ifdef-evaluator test)))
+ ;; ifX/elif is FALSE
+ (if elifs
+ ;; Case 3 - Hide the #ifX and eval #elif
+ (let ((newstart (car elifs)))
+ (hif-hide-line (hif-range-start range))
+ (hide-ifdef-region (hif-range-start range)
+ (1- newstart))
+ (setcar range newstart)
+ (goto-char newstart)
+ (setq elifs (cdr elifs))
+ (setq test (hif-canonicalize hif-elif-regexp)))
+
+ ;; Check for #else
+ (cond ((hif-range-else range)
+ ;; Case 4 - #else block visible
+ (hif-hide-line (hif-range-else range))
+ (hide-ifdef-region (hif-range-start range)
+ (1- (hif-range-else range)))
+ (hif-recurse-on (hif-range-else range)
+ (hif-range-end range)))
+ (t
+ ;; Case 5 - No #else block, hide #ifX
+ (hide-ifdef-region (point)
+ (1- (hif-range-end range)))))
+ (setq complete t))
+
+ ;; ifX/elif is TRUE
+ (cond (elifs
+ ;; Luke fix: distinguish from #elif..#elif to #elif..#else
+ (let ((elif (car elifs)))
+ ;; hide all elifs
+ (hif-hide-line elif)
+ (hide-ifdef-region elif (1- (hif-range-end range)))
+ (hif-recurse-on (hif-range-start range)
+ elif)))
+ ((hif-range-else range)
+ ;; Case 1 - Hide #elif and #else blocks, recurse #ifX
+ (hif-hide-line (hif-range-else range))
+ (hide-ifdef-region (hif-range-else range)
+ (1- (hif-range-end range)))
+ (hif-recurse-on (hif-range-start range)
+ (hif-range-else range)))
+ (t
+ ;; Case 2 - No #else, just recurse #ifX
+ (hif-recurse-on (hif-range-start range)
+ (hif-range-end range))))
+ (setq complete t))
+ (setq if-part nil))
+
+ ;; complete = t
(hif-hide-line (hif-range-start range)) ; Always hide start.
(goto-char (hif-range-end range))
(end-of-line)))
+(defun hif-evaluate-region (start end)
+ (let* ((tokens (ignore-errors ; Prevent C statement things like
+ ; 'do { ... } while (0)'
+ (hif-tokenize start end)))
+ (expr (and tokens
+ (condition-case nil
+ (hif-parse-exp tokens)
+ (error
+ tokens))))
+ (result (funcall hide-ifdef-evaluator expr)))
+ result))
+
+(defun hif-evaluate-macro (rstart rend)
+ "Evaluate the macro expansion result for a region. If no region active,
+find the current #ifdefs and evaluate the result. Currently it support only
+math calculation, no string or argumented macros can be expanded."
+ (interactive "r")
+ (let ((case-fold-search nil))
+ (save-excursion
+ (unless mark-active
+ (setq rstart nil rend nil)
+ (beginning-of-line)
+ (when (and (re-search-forward hif-macro-expr-prefix-regexp nil t)
+ (string= "define" (match-string 2)))
+ (re-search-forward hif-macroref-regexp nil t)))
+ (let* ((start (or rstart (point)))
+ (end (or rend (progn (hif-end-of-line) (point))))
+ (defined nil)
+ (simple 't)
+ (tokens (ignore-errors ; Prevent C statement things like
+ ; 'do { ... } while (0)'
+ (hif-tokenize start end)))
+ (expr (or (and (<= (length tokens) 1) ; Simple token
+ (setq defined (assoc (car tokens) hide-ifdef-env))
+ (setq simple (atom (hif-lookup (car tokens))))
+ (hif-lookup (car tokens)))
+ (and tokens
+ (condition-case nil
+ (hif-parse-exp tokens)
+ (error
+ nil)))))
+ (result (funcall hide-ifdef-evaluator expr))
+ (exprstring (replace-regexp-in-string
+ ;; Trim off leading/trailing whites
+ "^[ \t]*\\([^ \t]+\\)[ \t]*" "\\1"
+ (replace-regexp-in-string
+ "\\(//.*\\)" "" ; Trim off end-of-line comments
+ (buffer-substring-no-properties start end)))))
+ (cond
+ ((and (<= (length tokens) 1) simple) ; Simple token
+ (if defined
+ (message "%S <= `%s'" result exprstring)
+ (message "`%s' is not defined" exprstring)))
+ ((integerp result)
+ (if (or (= 0 result) (= 1 result))
+ (message "%S <= `%s'" result exprstring)
+ (message "%S (0x%x) <= `%s'" result result exprstring)))
+ ((null result) (message "%S <= `%s'" 'false exprstring))
+ ((eq t result) (message "%S <= `%s'" 'true exprstring))
+ (t (message "%S <= `%s'" result exprstring)))
+ result))))
(defun hif-parse-macro-arglist (str)
"Parse argument list formatted as '( arg1 [ , argn] [...] )', including
the '...'. Return a list of the arguments, if '...' exists the first arg
will be hif-etc."
- (let* ((hif-simple-token-only nil) ;; Dynamic binding var for `hif-tokenize'
+ (let* ((hif-simple-token-only nil) ; Dynamic binding var for `hif-tokenize'
(tokenlist
(cdr (hif-tokenize
- (- (point) (length str)) (point)))) ; remove hif-lparen
+ (- (point) (length str)) (point)))) ; Remove `hif-lparen'
etc result token)
(while (not (eq (setq token (pop tokenlist)) 'hif-rparen))
(cond
@@ -1462,7 +1666,7 @@ will be hif-etc."
(name (and (re-search-forward hif-macroref-regexp max t)
(match-string 1)))
(parsed nil)
- (parmlist (and (match-string 3) ;; First arg id found
+ (parmlist (and (match-string 3) ; First arg id found
(hif-parse-macro-arglist (match-string 2)))))
(if defining
;; Ignore name (still need to return 't), or define the name
@@ -1472,7 +1676,7 @@ will be hif-etc."
(let* ((start (point))
(end (progn (hif-end-of-line) (point)))
- (hif-simple-token-only nil) ;; Dynamic binding
+ (hif-simple-token-only nil) ; Dynamic binding
(tokens
(and name
;; `hif-simple-token-only' is set/clear
@@ -1532,6 +1736,10 @@ It does not do the work that's pointless to redo on a recursive entry."
;; (message "hide-ifdef-guts")
(save-excursion
(let ((case-fold-search nil)
+ (expand-header (and hide-ifdef-expand-reinclusion-protection
+ (string-match hide-ifdef-header-regexp-pattern
+ (buffer-name (current-buffer)))
+ (zerop hif-recurse-level)))
min max)
(goto-char (point-min))
(setf min (point))
@@ -1539,7 +1747,7 @@ It does not do the work that's pointless to redo on a recursive entry."
(setf max (hif-find-any-ifX))
(hif-add-new-defines min max)
(if max
- (hif-possibly-hide))
+ (hif-possibly-hide expand-header))
(setf min (point))
while max))))
@@ -1553,7 +1761,7 @@ It does not do the work that's pointless to redo on a recursive entry."
(interactive)
(setq hide-ifdef-read-only (not hide-ifdef-read-only))
(message "Hide-Read-Only %s"
- (if hide-ifdef-read-only "ON" "OFF"))
+ (if hide-ifdef-read-only "ON" "OFF"))
(if hide-ifdef-hiding
(setq buffer-read-only (or hide-ifdef-read-only
hif-outside-read-only)))
@@ -1564,10 +1772,10 @@ It does not do the work that's pointless to redo on a recursive entry."
(interactive)
(setq hif-outside-read-only (not hif-outside-read-only))
(message "Read only %s"
- (if hif-outside-read-only "ON" "OFF"))
+ (if hif-outside-read-only "ON" "OFF"))
(setq buffer-read-only
- (or (and hide-ifdef-hiding hide-ifdef-read-only)
- hif-outside-read-only))
+ (or (and hide-ifdef-hiding hide-ifdef-read-only)
+ hif-outside-read-only))
(force-mode-line-update))
(defun hide-ifdef-toggle-shadowing ()
@@ -1579,31 +1787,37 @@ It does not do the work that's pointless to redo on a recursive entry."
(widen)
(dolist (overlay (overlays-in (point-min) (point-max)))
(when (overlay-get overlay 'hide-ifdef)
- (if hide-ifdef-shadow
- (progn
- (overlay-put overlay 'invisible nil)
- (overlay-put overlay 'face 'hide-ifdef-shadow))
- (overlay-put overlay 'face nil)
- (overlay-put overlay 'invisible 'hide-ifdef))))))
-
-(defun hide-ifdef-define (var)
- "Define a VAR so that #ifdef VAR would be included."
- (interactive "SDefine what? ")
- (hif-set-var var 1)
+ (if hide-ifdef-shadow
+ (progn
+ (overlay-put overlay 'invisible nil)
+ (overlay-put overlay 'face 'hide-ifdef-shadow))
+ (overlay-put overlay 'face nil)
+ (overlay-put overlay 'invisible 'hide-ifdef))))))
+
+(defun hide-ifdef-define (var val)
+ "Define a VAR, optionally to a specific value, so that #ifdef VAR
+would be included."
+ (interactive
+ (let* ((default (save-excursion
+ (beginning-of-line)
+ (cond ((looking-at hif-ifx-else-endif-regexp)
+ (forward-word 2)
+ (current-word 'strict))
+ (t
+ nil))))
+ (var (read-minibuffer "Define what? " default))
+ (val (read-from-minibuffer (format "Set %s to? (default 1): " var)
+ nil nil t nil "1")))
+ (list var val)))
+ (hif-set-var var (or val 1))
+ (message "%s set to %s" var (or val 1))
+ (sleep-for 1)
(if hide-ifdef-hiding (hide-ifdefs)))
(defun hif-undefine-symbol (var)
(setq hide-ifdef-env
(delete (assoc var hide-ifdef-env) hide-ifdef-env)))
-;;(defun hide-ifdef-undef (var)
-;; "Undefine a VAR so that #ifdef VAR would not be included."
-;; (interactive "SUndefine what? ")
-;; ;;(hif-set-var var nil);;Luke fixed: set it nil is still considered
-;; ;;defined so #ifdef VAR is still true.
-;; (hif-undefine-symbol var)
-;; (if hide-ifdef-hiding (hide-ifdefs)))
-
(defun hide-ifdef-undef (start end)
"Undefine a VAR so that #ifdef VAR would not be included."
(interactive "r")
@@ -1624,20 +1838,23 @@ It does not do the work that's pointless to redo on a recursive entry."
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
preprocessor if it were given the file with those symbols defined.
+If this command is prefixed, hide also the #ifdefs themselves.
Turn off hiding by calling `show-ifdefs'."
(interactive)
- (message "Hiding...")
+ (let ((hide-ifdef-lines current-prefix-arg))
+ (or nomsg
+ (message "Hiding..."))
(setq hif-outside-read-only buffer-read-only)
(unless hide-ifdef-mode (hide-ifdef-mode 1)) ; turn on hide-ifdef-mode
(if hide-ifdef-hiding
- (show-ifdefs)) ; Otherwise, deep confusion.
+ (show-ifdefs)) ; Otherwise, deep confusion.
(setq hide-ifdef-hiding t)
(hide-ifdef-guts)
(setq buffer-read-only (or hide-ifdef-read-only hif-outside-read-only))
(or nomsg
- (message "Hiding done")))
+ (message "Hiding done"))))
(defun show-ifdefs ()
@@ -1653,46 +1870,74 @@ Turn off hiding by calling `show-ifdefs'."
Return as (TOP . BOTTOM) the extent of ifdef block."
(let (max-bottom)
(cons (save-excursion
- (beginning-of-line)
- (unless (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)
- (unless (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 ()
- "Hide the ifdef block (true or false part) enclosing or before the cursor."
- (interactive)
- (unless hide-ifdef-mode (hide-ifdef-mode 1))
- (let ((top-bottom (hif-find-ifdef-block)))
- (hide-ifdef-region (car top-bottom) (cdr top-bottom))
- (when hide-ifdef-lines
- (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)))
-
-(defun show-ifdef-block ()
+ (beginning-of-line)
+ (unless (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)
+ (unless (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 (&optional start end)
+ "Hide the ifdef block (true or false part) enclosing or before the cursor.
+If prefixed, it will also hide #ifdefs themselves."
+ (interactive "r")
+ (let ((hide-ifdef-lines current-prefix-arg))
+ (if mark-active
+ (let ((hif-recurse-level (1+ hif-recurse-level)))
+ (hif-recurse-on start end t)
+ (setq mark-active nil))
+ (unless hide-ifdef-mode (hide-ifdef-mode 1))
+ (let ((top-bottom (hif-find-ifdef-block)))
+ (hide-ifdef-region (car top-bottom) (cdr top-bottom))
+ (when hide-ifdef-lines
+ (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)))))
+
+(defun show-ifdef-block (&optional start end)
"Show the ifdef block (true or false part) enclosing or before the cursor."
- (interactive)
- (let ((top-bottom (hif-find-ifdef-block)))
+ (interactive "r")
+ (if mark-active
+ (progn
+ (dolist (o (overlays-in start end))
+ (if (overlay-get o 'hide-ifdef)
+ (delete-overlay o)))
+ (setq mark-active nil))
+ (let ((top-bottom (condition-case nil
+ (hif-find-ifdef-block)
+ (error
+ nil)))
+ (ovrs (overlays-in (max (point-min) (1- (point)))
+ (min (point-max) (1+ (point)))))
+ (del nil))
+ (if top-bottom
(if hide-ifdef-lines
- (hif-show-ifdef-region
- (save-excursion
- (goto-char (car top-bottom)) (line-beginning-position))
- (save-excursion
- (goto-char (1+ (cdr top-bottom)))
- (hif-end-of-line) (point)))
- (hif-show-ifdef-region (1- (car top-bottom)) (cdr top-bottom)))))
+ (hif-show-ifdef-region
+ (save-excursion
+ (goto-char (car top-bottom)) (line-beginning-position))
+ (save-excursion
+ (goto-char (1+ (cdr top-bottom)))
+ (hif-end-of-line) (point)))
+ (setf del (hif-show-ifdef-region
+ (1- (car top-bottom)) (cdr top-bottom)))))
+ (if (not (and top-bottom
+ del))
+ (dolist (o ovrs)
+ ;;(dolist (o (overlays-in (1- (point)) (1+ (point))))
+ ;; (if (overlay-get o 'hide-ifdef) (message "%S" o)))
+ (if (overlay-get o 'hide-ifdef)
+ (delete-overlay o)))))))
;;; definition alist support
@@ -1710,21 +1955,21 @@ Return as (TOP . BOTTOM) the extent of ifdef block."
"Set the association for NAME to `hide-ifdef-env'."
(interactive "SSet define list: ")
(push (cons name (hif-compress-define-list hide-ifdef-env))
- hide-ifdef-define-alist))
+ hide-ifdef-define-alist))
(defun hide-ifdef-use-define-alist (name)
"Set `hide-ifdef-env' to the define list specified by NAME."
(interactive
(list (completing-read "Use define list: "
- (mapcar (lambda (x) (symbol-name (car x)))
+ (mapcar (lambda (x) (symbol-name (car x)))
hide-ifdef-define-alist)
nil t)))
(if (stringp name) (setq name (intern name)))
(let ((define-list (assoc name hide-ifdef-define-alist)))
(if define-list
- (setq hide-ifdef-env
- (mapcar (lambda (arg) (cons arg t))
- (cdr define-list)))
+ (setq hide-ifdef-env
+ (mapcar (lambda (arg) (cons arg t))
+ (cdr define-list)))
(error "No define list for %s" name))
(if hide-ifdef-hiding (hide-ifdefs))))
--
1.8.3.2
^ permalink raw reply related [flat|nested] 20+ messages in thread
* bug#17854: The patch #3 of 3 for hideif.el, a lot of bug fixes and enhancements
2014-06-26 13:51 bug#17854: The patch #3 of 3 for hideif.el, a lot of bug fixes and enhancements Luke Lee
@ 2014-06-26 16:56 ` Glenn Morris
2014-06-26 16:59 ` Glenn Morris
2014-06-27 9:26 ` Luke Lee
2014-06-27 9:37 ` Luke Lee
2014-07-07 9:04 ` bug#17854: Completed Luke Lee
2 siblings, 2 replies; 20+ messages in thread
From: Glenn Morris @ 2014-06-26 16:56 UTC (permalink / raw)
To: Luke Lee; +Cc: 17854
Some stylistic comments only:
It needs a ChangeLog entry, and probably a NEWS entry.
> -;; Daniel LaLiberte <liberte@holonexus.org>
> +;; Daniel LaLiberte <liberte@holonexus.org>
Please don't change existing whitespace in areas that you are not
otherwise touching.
> -;; (unless 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
> +;; (unless 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
Again, this is just whitespace.
> @@ -129,16 +129,44 @@
> "Non-nil means shadow text instead of hiding it."
> :type 'boolean
> :group 'hide-ifdef
> - :version "23.1")
> + :version "24.5")
>
> (defface hide-ifdef-shadow '((t (:inherit shadow)))
> "Face for shadowing ifdef blocks."
> :group 'hide-ifdef
> - :version "23.1")
> + :version "24.5")
Why is the :version changing, when the defaults are unchanged?
> (defcustom hide-ifdef-exclude-define-regexp nil
> "Ignore #define names if those names match this exclusion pattern."
> :type 'string)
> +(defcustom hide-ifdef-expand-reinclusion-protection t
> + "When hiding header files, enabling this flag allows hideif always try to
> +expand the re-inclusion protected ifdefs. Disabling this flag those headers
> +are usually hidden to a top level #ifdef...#endif due to those defined symbols
The first line of a doc-string should be a complete sentence that fits
in < 80 columns. All the doc should fit within the standard fill-column.
> + :type 'boolean
> + :group 'hide-ifdef)
> +
> +(defcustom hide-ifdef-header-regexp-pattern
> + "^.*\\.[hH]\\([hH]\\|[xX][xX]\\|[pP][pP]\\)?"
> + "C/C++ header file name patterns. Effective only if
> +`hide-ifdef-expand-reinclusion-protection' is t."
> + :type 'string
> + :group 'hide-ifdef)
Again, the first line of the doc should be a complete sentence.
New defcustoms need :version tags (and probably NEWS entries).
> +(defvar hide-ifdef-env-backup nil
> + "A backup variable to prevent `hide-ifdef-env' accidentally cleared by
> +`hif-clear-all-ifdef-defined'.")
First line of doc too long. Also, this is ungrammatical.
> `hide-ifdef-env'
> - An association list of defined and undefined symbols for the
> - current buffer. Initially, the global value of `hide-ifdef-env'
> - is used.
> + An association list of defined and undefined symbols for the
> + current project. Initially, the global value of `hide-ifdef-env'
> + is used. This variable was a buffer-local variable but is now a
> + global variable since we've extend hideif to support project-based
s/extend/extended.
"project-based across all-buffers" doesn't make sense.
I'm not sure that describing how things used to work is helpful.
> + across all-buffers. To simulate the original buffer local behavior
> + we need to clear this variable (C-c @ C) then hide current buffer.
> `hide-ifdef-define-alist'
> - An association list of defined symbol lists.
> + An association list of defined symbol lists.
whitespace.
> - Set to non-nil to not show #if, #ifdef, #ifndef, #else, and
> - #endif lines when hiding.
> + Set to non-nil to not show #if, #ifdef, #ifndef, #else, and
> + #endif lines when hiding.
whitespace.
At this point, I'll give up, and ask you to send a version that does not
have pointless whitespace changes. :)
^ permalink raw reply [flat|nested] 20+ messages in thread
* bug#17854: The patch #3 of 3 for hideif.el, a lot of bug fixes and enhancements
2014-06-26 16:56 ` Glenn Morris
@ 2014-06-26 16:59 ` Glenn Morris
2014-06-27 9:08 ` Luke Lee
2014-06-27 9:26 ` Luke Lee
1 sibling, 1 reply; 20+ messages in thread
From: Glenn Morris @ 2014-06-26 16:59 UTC (permalink / raw)
To: Luke Lee; +Cc: 17854
PS many of those comments apply equally to the change #2 you recently
installed in trunk. It needs a ChangeLog entry (see existing examples
for format) that lists every changed/new function/variable; probably a
NEWS entry; new defcustoms need :version tags; and some of the
doc-strings need fixing for length. Could you fix those things please?
^ permalink raw reply [flat|nested] 20+ messages in thread
* bug#17854: The patch #3 of 3 for hideif.el, a lot of bug fixes and enhancements
2014-06-26 16:59 ` Glenn Morris
@ 2014-06-27 9:08 ` Luke Lee
0 siblings, 0 replies; 20+ messages in thread
From: Luke Lee @ 2014-06-27 9:08 UTC (permalink / raw)
To: Glenn Morris; +Cc: 17854
[-- Attachment #1: Type: text/plain, Size: 491 bytes --]
Done. Will resubmit my patch #3 right away.
2014-06-27 0:59 GMT+08:00 Glenn Morris <rgm@gnu.org>:
>
> PS many of those comments apply equally to the change #2 you recently
> installed in trunk. It needs a ChangeLog entry (see existing examples
> for format) that lists every changed/new function/variable; probably a
> NEWS entry; new defcustoms need :version tags; and some of the
> doc-strings need fixing for length. Could you fix those things please?
>
>
--
Best regards,
Luke Lee
[-- Attachment #2: Type: text/html, Size: 856 bytes --]
^ permalink raw reply [flat|nested] 20+ messages in thread
* bug#17854: The patch #3 of 3 for hideif.el, a lot of bug fixes and enhancements
2014-06-26 16:56 ` Glenn Morris
2014-06-26 16:59 ` Glenn Morris
@ 2014-06-27 9:26 ` Luke Lee
1 sibling, 0 replies; 20+ messages in thread
From: Luke Lee @ 2014-06-27 9:26 UTC (permalink / raw)
To: 17854
[-- Attachment #1.1: Type: text/plain, Size: 4483 bytes --]
Resubmit my fixed patch #3 for stylistic changes with white space changes
removed (git diff -w) as attached.
[PATCH] Other hideif enhancements and bug fixes:
* Add macro evaluation function and key binding.
* Merge continuous "..." into one on hiding.
* Fix original hideif bug that sometimes fail to hide the correct portion
in a
long ifdefs containing #elif and #else.
* Support hide/show commands in a marked region.
* Expand top level #ifdefs for C/C++ header files to prevent header files
always got hidden.
* Others.
2014-06-27 0:56 GMT+08:00 Glenn Morris <rgm@gnu.org>:
>
> Some stylistic comments only:
>
> It needs a ChangeLog entry, and probably a NEWS entry.
>
> > -;; Daniel LaLiberte <liberte@holonexus.org>
> > +;; Daniel LaLiberte <liberte@holonexus.org>
>
> Please don't change existing whitespace in areas that you are not
> otherwise touching.
>
> > -;; (unless 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
> > +;; (unless 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
>
> Again, this is just whitespace.
>
> > @@ -129,16 +129,44 @@
> > "Non-nil means shadow text instead of hiding it."
> > :type 'boolean
> > :group 'hide-ifdef
> > - :version "23.1")
> > + :version "24.5")
> >
> > (defface hide-ifdef-shadow '((t (:inherit shadow)))
> > "Face for shadowing ifdef blocks."
> > :group 'hide-ifdef
> > - :version "23.1")
> > + :version "24.5")
>
> Why is the :version changing, when the defaults are unchanged?
>
> > (defcustom hide-ifdef-exclude-define-regexp nil
> > "Ignore #define names if those names match this exclusion pattern."
> > :type 'string)
> > +(defcustom hide-ifdef-expand-reinclusion-protection t
> > + "When hiding header files, enabling this flag allows hideif always
> try to
> > +expand the re-inclusion protected ifdefs. Disabling this flag those
> headers
> > +are usually hidden to a top level #ifdef...#endif due to those defined
> symbols
>
> The first line of a doc-string should be a complete sentence that fits
> in < 80 columns. All the doc should fit within the standard fill-column.
>
> > + :type 'boolean
> > + :group 'hide-ifdef)
> > +
> > +(defcustom hide-ifdef-header-regexp-pattern
> > + "^.*\\.[hH]\\([hH]\\|[xX][xX]\\|[pP][pP]\\)?"
> > + "C/C++ header file name patterns. Effective only if
> > +`hide-ifdef-expand-reinclusion-protection' is t."
> > + :type 'string
> > + :group 'hide-ifdef)
>
> Again, the first line of the doc should be a complete sentence.
> New defcustoms need :version tags (and probably NEWS entries).
>
> > +(defvar hide-ifdef-env-backup nil
> > + "A backup variable to prevent `hide-ifdef-env' accidentally cleared by
> > +`hif-clear-all-ifdef-defined'.")
>
> First line of doc too long. Also, this is ungrammatical.
>
> > `hide-ifdef-env'
> > - An association list of defined and undefined symbols for the
> > - current buffer. Initially, the global value of `hide-ifdef-env'
> > - is used.
> > + An association list of defined and undefined symbols for the
> > + current project. Initially, the global value of
> `hide-ifdef-env'
> > + is used. This variable was a buffer-local variable but is now a
> > + global variable since we've extend hideif to support
> project-based
>
> s/extend/extended.
> "project-based across all-buffers" doesn't make sense.
> I'm not sure that describing how things used to work is helpful.
>
> > + across all-buffers. To simulate the original buffer local
> behavior
> > + we need to clear this variable (C-c @ C) then hide current
> buffer.
>
> > `hide-ifdef-define-alist'
> > - An association list of defined symbol lists.
> > + An association list of defined symbol lists.
>
> whitespace.
>
> > - Set to non-nil to not show #if, #ifdef, #ifndef, #else, and
> > - #endif lines when hiding.
> > + Set to non-nil to not show #if, #ifdef, #ifndef, #else, and
> > + #endif lines when hiding.
>
> whitespace.
>
>
> At this point, I'll give up, and ask you to send a version that does not
> have pointless whitespace changes. :)
>
--
Best regards,
Luke Lee
[-- Attachment #1.2: Type: text/html, Size: 5820 bytes --]
[-- Attachment #2: 0003-Other-hideif-enhancements-and-bug-fixes.patch --]
[-- Type: application/octet-stream, Size: 28301 bytes --]
diff --git a/lisp/progmodes/hideif.el b/lisp/progmodes/hideif.el
index 4b78c08..5063710 100644
--- a/lisp/progmodes/hideif.el
+++ b/lisp/progmodes/hideif.el
@@ -138,7 +138,45 @@
(defcustom hide-ifdef-exclude-define-regexp nil
"Ignore #define names if those names match this exclusion pattern."
- :type 'string)
+ :type 'string
+ :version "24.4")
+
+(defcustom hide-ifdef-expand-reinclusion-protection t
+ "Prevent hiding the whole C/C++ header file protected by a big #ifdef..#endif.
+Most C/C++ headers are usually wrapped with ifdefs to prevent re-inclusion:
+
+ ----- beginning of file -----
+ #ifdef _XXX_HEADER_FILE_INCLUDED_
+ #define _XXX_HEADER_FILE_INCLUDED_
+ xxx
+ xxx
+ xxx...
+ #endif
+ ----- end of file -----
+
+If we try to hide this header file, for the first time hideif will find
+_XXX_HEADER_FILE_INCLUDED_ and have it defined. Everything between #ifdef
+to #endif are not hidden. For the second time since _XXX_HEADER_FILE_INCLUDED_
+is defined everything between the outermost #ifdef..#endif will be hidden:
+
+ ----- beginning of file -----
+ #ifdef _XXX_HEADER_FILE_INCLUDED_
+ ...
+ #endif
+ ----- end of file -----
+
+This is not the behavior we expected, we still would like to see the content
+of this header file. With this flag enabled we can have the outermost #if
+always not hidden."
+ :type 'boolean
+ :version "24.4")
+
+(defcustom hide-ifdef-header-regexp
+ "^.*\\.[hH]\\([hH]\\|[xX][xX]\\|[pP][pP]\\)?"
+ "C/C++ header file name patterns to determine if current buffer is a header.
+Effective only if `hide-ifdef-expand-reinclusion-protection' is t."
+ :type 'string
+ :group 'hide-ifdef)
(defvar hide-ifdef-mode-submap
;; Set up the submap that goes after the prefix key.
@@ -152,6 +190,8 @@
(define-key map "s" 'show-ifdefs)
(define-key map "\C-d" 'hide-ifdef-block)
(define-key map "\C-s" 'show-ifdef-block)
+ (define-key map "e" 'hif-evaluate-macro)
+ (define-key map "C" 'hif-clear-all-ifdef-defined)
(define-key map "\C-q" 'hide-ifdef-toggle-read-only)
(define-key map "\C-w" 'hide-ifdef-toggle-shadowing)
@@ -216,6 +256,11 @@
(defvar hide-ifdef-env nil
"An alist of defined symbols and their values.")
+(defvar hide-ifdef-env-backup nil
+ "This variable is a backup of the previously cleared `hide-ifdef-env'.
+This backup prevents any accidental clearance of `hide-fidef-env' by
+`hif-clear-all-ifdef-defined'.")
+
(defvar hif-outside-read-only nil
"Internal variable. Saves the value of `buffer-read-only' while hiding.")
@@ -233,8 +278,16 @@ Several variables affect how the hiding is done:
`hide-ifdef-env'
An association list of defined and undefined symbols for the
- current buffer. Initially, the global value of `hide-ifdef-env'
- is used.
+ current project. Initially, the global value of `hide-ifdef-env'
+ is used. This variable was a buffer-local variable which limits
+ hideif to parse only one C/C++ file at a time. We've extended
+ hideif to support parsing a C/C++ project containing multiple C/C++
+ source files opened simultaneously in different buffers. Therefore
+ `hide-ifdef-env' can no longer be buffer local but must be global.
+
+ We can still simulate the behavior of elder hideif versions (i.e.
+ `hide-ifdef-env' being buffer local) by clearing this variable
+ (C-c @ C) everytime before hiding current buffer.
`hide-ifdef-define-alist'
An association list of defined symbol lists.
@@ -259,8 +312,9 @@ Several variables affect how the hiding is done:
(if hide-ifdef-mode
(progn
;; inherit global values
- (set (make-local-variable 'hide-ifdef-env)
- (default-value 'hide-ifdef-env))
+;; (set (make-local-variable 'hide-ifdef-env)
+;; (default-value 'hide-ifdef-env))
+ (set 'hide-ifdef-env (default-value 'hide-ifdef-env))
(set (make-local-variable 'hide-ifdef-hiding)
(default-value 'hide-ifdef-hiding))
(set (make-local-variable 'hif-outside-read-only) buffer-read-only)
@@ -279,6 +333,14 @@ Several variables affect how the hiding is done:
(when hide-ifdef-hiding
(show-ifdefs))))
+(defun hif-clear-all-ifdef-defined ()
+ "Clears all symbols defined in `hide-ifdef-env'.
+It will backup this variable to `hide-ifdef-env-backup' before clearing to
+prevent accidental clearance."
+ (interactive)
+ (when (y-or-n-p "Clear all #defined symbols?")
+ (setq hide-ifdef-env-backup hide-ifdef-env)
+ (setq hide-ifdef-env nil)))
(defun hif-show-all ()
"Show all of the text in the current buffer."
@@ -298,16 +360,63 @@ Several variables affect how the hiding is done:
(while (= (logand 1 (skip-chars-backward "\\\\")) 1)
(end-of-line 2)))
+(defun hif-merge-ifdef-region (start end)
+ "This function merges nearby ifdef regions to form a bigger overlay.
+The region is defined by START and END. This will decrease the number of
+overlays created."
+ ;; 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' equals to nil while another part with 't,
+ ;; this case happens.
+ ;; TODO: Should we merge? or just create a container overlay? -- this can
+ ;; prevent `hideif-show-ifdef' expanding too many hidden contents since there
+ ;; is only a big overlay exists there without any smaller overlays.
+ (save-restriction
+ (widen) ; Otherwise `point-min' and `point-max' will be restricted and thus
+ ; fail to find neighbor overlays
+ (let ((begovrs (overlays-in
+ (max (- start 2) (point-min))
+ (max (- start 1) (point-min))))
+ (endovrs (overlays-in
+ (min (+ end 1) (point-max))
+ (min (+ end 2) (point-max))))
+ (ob nil)
+ (oe nil)
+ b e)
+ ;; Merge overlays before START
+ (dolist (o begovrs)
+ (when (overlay-get o 'hide-ifdef)
+ (setq b (min start (overlay-start o))
+ e (max end (overlay-end o)))
+ (move-overlay o b e)
+ (hif-merge-ifdef-region b e)
+ (setq ob o)))
+ ;; Merge overlays after END
+ (dolist (o endovrs)
+ (when (overlay-get o 'hide-ifdef)
+ (setq b (min start (overlay-start o))
+ e (max end (overlay-end o)))
+ (move-overlay o b e)
+ (hif-merge-ifdef-region b e)
+ (setf oe o)))
+ ;; If both START and END merging happens, merge into bigger one
+ (when (and ob oe)
+ (let ((b (min (overlay-start ob) (overlay-start oe)))
+ (e (max (overlay-end ob) (overlay-end oe))))
+ (delete-overlay oe)
+ (move-overlay ob b e)
+ (hif-merge-ifdef-region b e)))
+ (or ob oe))))
(defun hide-ifdef-region-internal (start end)
- (remove-overlays start end 'hide-ifdef t)
+ (unless (hif-merge-ifdef-region start end)
(let ((o (make-overlay start end)))
(overlay-put o 'hide-ifdef t)
(if hide-ifdef-shadow
(overlay-put o 'face 'hide-ifdef-shadow)
- (overlay-put o 'invisible 'hide-ifdef))))
+ (overlay-put o 'invisible 'hide-ifdef)))))
(defun hide-ifdef-region (start end)
- "START is the start of a #if or #else form. END is the ending part.
+ "START is the start of a #if, #elif, or #else form. END is the ending part.
Everything including these lines is made invisible."
(save-excursion
(goto-char start) (hif-end-of-line) (setq start (point))
@@ -316,7 +425,9 @@ Everything including these lines is made invisible."
(defun hif-show-ifdef-region (start end)
"Everything between START and END is made visible."
- (remove-overlays start end 'hide-ifdef t))
+ (let ((onum (length (overlays-in start end))))
+ (remove-overlays start end 'hide-ifdef t)
+ (/= onum (length (overlays-in start end)))))
;;===%%SF%% evaluation (Start) ===
@@ -375,10 +486,8 @@ that form should be displayed.")
(concat hif-cpp-prefix "\\(if\\(n?def\\)?\\|elif\\|define\\)[ \t]+"))
(defconst hif-white-regexp "[ \t]*")
-(defconst hif-define-regexp
- (concat hif-cpp-prefix "\\(define\\|undef\\)"))
-(defconst hif-id-regexp
- (concat "[[:alpha:]_][[:alnum:]_]*"))
+(defconst hif-define-regexp (concat hif-cpp-prefix "\\(define\\|undef\\)"))
+(defconst hif-id-regexp (concat "[[:alpha:]_][[:alnum:]_]*"))
(defconst hif-macroref-regexp
(concat hif-white-regexp "\\(" hif-id-regexp "\\)" hif-white-regexp
"\\("
@@ -499,6 +608,8 @@ that form should be displayed.")
(setq hif-simple-token-only nil)))
token-list)))
+ ((looking-at "\r") ; Sometimes MS-Windows user will leave CR in
+ (forward-char 1)) ; the source code. Let's don't stuck here.
(t (error "Bad #if expression: %s" (buffer-string)))))))
(nreverse token-list)))
@@ -1175,13 +1286,16 @@ Do this when cursor is at the beginning of `regexp' (i.e. #ifX)."
(if (= start (point))
(error "Mismatched #ifdef #endif pair")))
(cond ((hif-looking-at-endif)
- (hif-endif-to-ifdef) ; find beginning of nested if
- (hif-endif-to-ifdef)) ; find beginning of outer if or else
+ (hif-endif-to-ifdef) ; Find beginning of nested if
+ (hif-endif-to-ifdef)) ; Find beginning of outer if or else
+ ((hif-looking-at-elif)
+ (hif-endif-to-ifdef))
((hif-looking-at-else)
(hif-endif-to-ifdef))
((hif-looking-at-ifX)
'done)
- (t))) ; never gets here
+ (t
+ (error "Mismatched #endif")))) ; never gets here
(defun forward-ifdef (&optional arg)
@@ -1275,26 +1389,26 @@ With argument, do this that many times."
;;===%%SF%% hide-ifdef-hiding (Start) ===
-;;; A range is a structure with four components:
-;;; ELSE-P True if there was an else clause for the ifdef.
-;;; START The start of the range. (beginning of line)
-;;; ELSE The else marker (beginning of line)
-;;; Only valid if ELSE-P is true.
-;;; END The end of the range. (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)
- (list start else end))
+(defsubst hif-make-range (start end &optional else elif)
+ (list start else end elif))
(defsubst hif-range-start (range) (elt range 0))
(defsubst hif-range-else (range) (elt range 1))
(defsubst hif-range-end (range) (elt range 2))
+(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.
@@ -1303,19 +1417,23 @@ Point is left unchanged."
(save-excursion
(beginning-of-line)
(let ((start (point))
+ (elif nil)
(else nil)
(end nil))
- ;; Part one. Look for either #endif or #else.
+ ;; Part one. Look for either #elif, #else or #endif.
;; This loop-and-a-half dedicated to E. Dijkstra.
+ (while (and (not else) (not end))
(while (progn
(hif-find-next-relevant)
(hif-looking-at-ifX)) ; Skip nested ifdef
(hif-ifdef-to-endif))
- ;; Found either a #else or an #endif.
- (cond ((hif-looking-at-else)
+ ;; Found either a #else, #elif, or an #endif.
+ (cond ((hif-looking-at-elif)
+ (setq elif (nconc elif (list (point)))))
+ ((hif-looking-at-else)
(setq else (point)))
(t
- (setq end (point)))) ; (line-end-position)
+ (setq end (point)))))
;; If found #else, look for #endif.
(when else
(while (progn
@@ -1325,7 +1443,7 @@ Point is left unchanged."
(if (hif-looking-at-else)
(error "Found two elses in a row? Broken!"))
(setq end (point))) ; (line-end-position)
- (hif-make-range start end else))))
+ (hif-make-range start end else elif))))
;; A bit slimy.
@@ -1340,69 +1458,176 @@ Does nothing if `hide-ifdef-lines' is nil."
(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 #else.
-;;; The #if part must be hidden. The #else part must be processed
-;;; for nested #ifs.
-;;; Case 4: #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.
-
-(defun hif-recurse-on (start end)
+;; 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)
+
+(defun hif-recurse-on (start end &optional dont-go-eol)
"Call `hide-ifdef-guts' after narrowing to end of START line and END line."
(save-excursion
(save-restriction
(goto-char start)
- (end-of-line)
+ (unless dont-go-eol
+ (end-of-line))
(narrow-to-region (point) end)
- (hide-ifdef-guts))))
+ (let ((hif-recurse-level (1+ hif-recurse-level)))
+ (hide-ifdef-guts)))))
-(defun hif-possibly-hide ()
+(defun hif-possibly-hide (expand-reinclusion)
"Called at #ifX expression, this hides those parts that should be hidden.
It uses the judgment of `hide-ifdef-evaluator'."
;; (message "hif-possibly-hide") (sit-for 1)
- (let ((test (hif-canonicalize hif-ifx-regexp))
- (range (hif-find-range)))
+ (let* ((case-fold-search nil)
+ (test (hif-canonicalize hif-ifx-regexp))
+ (range (hif-find-range))
+ (elifs (hif-range-elif range))
+ (if-part t) ;; everytime we start from if-part
+ (complete nil))
;; (message "test = %s" test) (sit-for 1)
(hif-hide-line (hif-range-end range))
- (if (not (hif-not (funcall hide-ifdef-evaluator test)))
- (cond ((hif-range-else range) ; case 1
+ (while (not complete)
+ (if (and (not (and expand-reinclusion if-part))
+ (hif-not (funcall hide-ifdef-evaluator test)))
+ ;; ifX/elif is FALSE
+ (if elifs
+ ;; Case 3 - Hide the #ifX and eval #elif
+ (let ((newstart (car elifs)))
+ (hif-hide-line (hif-range-start range))
+ (hide-ifdef-region (hif-range-start range)
+ (1- newstart))
+ (setcar range newstart)
+ (goto-char newstart)
+ (setq elifs (cdr elifs))
+ (setq test (hif-canonicalize hif-elif-regexp)))
+
+ ;; Check for #else
+ (cond ((hif-range-else range)
+ ;; Case 4 - #else block visible
+ (hif-hide-line (hif-range-else range))
+ (hide-ifdef-region (hif-range-start range)
+ (1- (hif-range-else range)))
+ (hif-recurse-on (hif-range-else range)
+ (hif-range-end range)))
+ (t
+ ;; Case 5 - No #else block, hide #ifX
+ (hide-ifdef-region (point)
+ (1- (hif-range-end range)))))
+ (setq complete t))
+
+ ;; ifX/elif is TRUE
+ (cond (elifs
+ ;; Luke fix: distinguish from #elif..#elif to #elif..#else
+ (let ((elif (car elifs)))
+ ;; hide all elifs
+ (hif-hide-line elif)
+ (hide-ifdef-region elif (1- (hif-range-end range)))
+ (hif-recurse-on (hif-range-start range)
+ elif)))
+ ((hif-range-else range)
+ ;; Case 1 - Hide #elif and #else blocks, recurse #ifX
(hif-hide-line (hif-range-else range))
(hide-ifdef-region (hif-range-else range)
(1- (hif-range-end range)))
(hif-recurse-on (hif-range-start range)
(hif-range-else range)))
- (t ; case 2
+ (t
+ ;; Case 2 - No #else, just recurse #ifX
(hif-recurse-on (hif-range-start range)
(hif-range-end range))))
- (cond ((hif-range-else range) ; case 3
- (hif-hide-line (hif-range-else range))
- (hide-ifdef-region (hif-range-start range)
- (1- (hif-range-else range)))
- (hif-recurse-on (hif-range-else range)
- (hif-range-end range)))
- (t ; case 4
- (hide-ifdef-region (point)
- (1- (hif-range-end range))))))
+ (setq complete t))
+ (setq if-part nil))
+
+ ;; complete = t
(hif-hide-line (hif-range-start range)) ; Always hide start.
(goto-char (hif-range-end range))
(end-of-line)))
+(defun hif-evaluate-region (start end)
+ (let* ((tokens (ignore-errors ; Prevent C statement things like
+ ; 'do { ... } while (0)'
+ (hif-tokenize start end)))
+ (expr (and tokens
+ (condition-case nil
+ (hif-parse-exp tokens)
+ (error
+ tokens))))
+ (result (funcall hide-ifdef-evaluator expr)))
+ result))
+
+(defun hif-evaluate-macro (rstart rend)
+ "Evaluate the macro expansion result for a region.
+If no region active, find the current #ifdefs and evaluate the result. Currently
+it support only math calculations, strings or argumented macros can not be
+expanded."
+ (interactive "r")
+ (let ((case-fold-search nil))
+ (save-excursion
+ (unless mark-active
+ (setq rstart nil rend nil)
+ (beginning-of-line)
+ (when (and (re-search-forward hif-macro-expr-prefix-regexp nil t)
+ (string= "define" (match-string 2)))
+ (re-search-forward hif-macroref-regexp nil t)))
+ (let* ((start (or rstart (point)))
+ (end (or rend (progn (hif-end-of-line) (point))))
+ (defined nil)
+ (simple 't)
+ (tokens (ignore-errors ; Prevent C statement things like
+ ; 'do { ... } while (0)'
+ (hif-tokenize start end)))
+ (expr (or (and (<= (length tokens) 1) ; Simple token
+ (setq defined (assoc (car tokens) hide-ifdef-env))
+ (setq simple (atom (hif-lookup (car tokens))))
+ (hif-lookup (car tokens)))
+ (and tokens
+ (condition-case nil
+ (hif-parse-exp tokens)
+ (error
+ nil)))))
+ (result (funcall hide-ifdef-evaluator expr))
+ (exprstring (replace-regexp-in-string
+ ;; Trim off leading/trailing whites
+ "^[ \t]*\\([^ \t]+\\)[ \t]*" "\\1"
+ (replace-regexp-in-string
+ "\\(//.*\\)" "" ; Trim off end-of-line comments
+ (buffer-substring-no-properties start end)))))
+ (cond
+ ((and (<= (length tokens) 1) simple) ; Simple token
+ (if defined
+ (message "%S <= `%s'" result exprstring)
+ (message "`%s' is not defined" exprstring)))
+ ((integerp result)
+ (if (or (= 0 result) (= 1 result))
+ (message "%S <= `%s'" result exprstring)
+ (message "%S (0x%x) <= `%s'" result result exprstring)))
+ ((null result) (message "%S <= `%s'" 'false exprstring))
+ ((eq t result) (message "%S <= `%s'" 'true exprstring))
+ (t (message "%S <= `%s'" result exprstring)))
+ result))))
(defun hif-parse-macro-arglist (str)
"Parse argument list formatted as '( arg1 [ , argn] [...] )'.
@@ -1533,6 +1758,10 @@ It does not do the work that's pointless to redo on a recursive entry."
;; (message "hide-ifdef-guts")
(save-excursion
(let ((case-fold-search nil)
+ (expand-header (and hide-ifdef-expand-reinclusion-protection
+ (string-match hide-ifdef-header-regexp
+ (buffer-name (current-buffer)))
+ (zerop hif-recurse-level)))
min max)
(goto-char (point-min))
(setf min (point))
@@ -1540,7 +1769,7 @@ It does not do the work that's pointless to redo on a recursive entry."
(setf max (hif-find-any-ifX))
(hif-add-new-defines min max)
(if max
- (hif-possibly-hide))
+ (hif-possibly-hide expand-header))
(setf min (point))
while max))))
@@ -1587,17 +1816,30 @@ It does not do the work that's pointless to redo on a recursive entry."
(overlay-put overlay 'face nil)
(overlay-put overlay 'invisible 'hide-ifdef))))))
-(defun hide-ifdef-define (var)
- "Define a VAR so that #ifdef VAR would be included."
- (interactive "SDefine what? ")
- (hif-set-var var 1)
+(defun hide-ifdef-define (var val)
+ "Define a VAR optionally to a specific value VAL into `hide-ifdef-env'.
+This allows #ifdef VAR from being hidden."
+ (interactive
+ (let* ((default (save-excursion
+ (beginning-of-line)
+ (cond ((looking-at hif-ifx-else-endif-regexp)
+ (forward-word 2)
+ (current-word 'strict))
+ (t
+ nil))))
+ (var (read-minibuffer "Define what? " default))
+ (val (read-from-minibuffer (format "Set %s to? (default 1): " var)
+ nil nil t nil "1")))
+ (list var val)))
+ (hif-set-var var (or val 1))
+ (message "%s set to %s" var (or val 1))
+ (sleep-for 1)
(if hide-ifdef-hiding (hide-ifdefs)))
(defun hif-undefine-symbol (var)
(setq hide-ifdef-env
(delete (assoc var hide-ifdef-env) hide-ifdef-env)))
-
(defun hide-ifdef-undef (start end)
"Undefine a VAR so that #ifdef VAR would not be included."
(interactive "r")
@@ -1618,11 +1860,14 @@ It does not do the work that's pointless to redo on a recursive entry."
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
preprocessor if it were given the file with those symbols defined.
+If this command is prefixed, hide also the #ifdefs themselves.
Turn off hiding by calling `show-ifdefs'."
(interactive)
- (message "Hiding...")
+ (let ((hide-ifdef-lines current-prefix-arg))
+ (or nomsg
+ (message "Hiding..."))
(setq hif-outside-read-only buffer-read-only)
(unless hide-ifdef-mode (hide-ifdef-mode 1)) ; turn on hide-ifdef-mode
(if hide-ifdef-hiding
@@ -1631,7 +1876,7 @@ Turn off hiding by calling `show-ifdefs'."
(hide-ifdef-guts)
(setq buffer-read-only (or hide-ifdef-read-only hif-outside-read-only))
(or nomsg
- (message "Hiding done")))
+ (message "Hiding done"))))
(defun show-ifdefs ()
@@ -1663,9 +1908,15 @@ Return as (TOP . BOTTOM) the extent of ifdef block."
(min max-bottom (1- (point)))))))
-(defun hide-ifdef-block ()
- "Hide the ifdef block (true or false part) enclosing or before the cursor."
- (interactive)
+(defun hide-ifdef-block (&optional start end)
+ "Hide the ifdef block (true or false part) enclosing or before the cursor.
+If prefixed, it will also hide #ifdefs themselves."
+ (interactive "r")
+ (let ((hide-ifdef-lines current-prefix-arg))
+ (if mark-active
+ (let ((hif-recurse-level (1+ hif-recurse-level)))
+ (hif-recurse-on start end t)
+ (setq mark-active nil))
(unless hide-ifdef-mode (hide-ifdef-mode 1))
(let ((top-bottom (hif-find-ifdef-block)))
(hide-ifdef-region (car top-bottom) (cdr top-bottom))
@@ -1673,12 +1924,26 @@ Return as (TOP . BOTTOM) the extent of ifdef block."
(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)))
+ (setq buffer-read-only
+ (or hide-ifdef-read-only hif-outside-read-only)))))
-(defun show-ifdef-block ()
+(defun show-ifdef-block (&optional start end)
"Show the ifdef block (true or false part) enclosing or before the cursor."
- (interactive)
- (let ((top-bottom (hif-find-ifdef-block)))
+ (interactive "r")
+ (if mark-active
+ (progn
+ (dolist (o (overlays-in start end))
+ (if (overlay-get o 'hide-ifdef)
+ (delete-overlay o)))
+ (setq mark-active nil))
+ (let ((top-bottom (condition-case nil
+ (hif-find-ifdef-block)
+ (error
+ nil)))
+ (ovrs (overlays-in (max (point-min) (1- (point)))
+ (min (point-max) (1+ (point)))))
+ (del nil))
+ (if top-bottom
(if hide-ifdef-lines
(hif-show-ifdef-region
(save-excursion
@@ -1686,7 +1951,15 @@ Return as (TOP . BOTTOM) the extent of ifdef block."
(save-excursion
(goto-char (1+ (cdr top-bottom)))
(hif-end-of-line) (point)))
- (hif-show-ifdef-region (1- (car top-bottom)) (cdr top-bottom)))))
+ (setf del (hif-show-ifdef-region
+ (1- (car top-bottom)) (cdr top-bottom)))))
+ (if (not (and top-bottom
+ del))
+ (dolist (o ovrs)
+ ;;(dolist (o (overlays-in (1- (point)) (1+ (point))))
+ ;; (if (overlay-get o 'hide-ifdef) (message "%S" o)))
+ (if (overlay-get o 'hide-ifdef)
+ (delete-overlay o)))))))
;;; definition alist support
^ permalink raw reply related [flat|nested] 20+ messages in thread
* bug#17854: The patch #3 of 3 for hideif.el, a lot of bug fixes and enhancements
2014-06-26 13:51 bug#17854: The patch #3 of 3 for hideif.el, a lot of bug fixes and enhancements Luke Lee
2014-06-26 16:56 ` Glenn Morris
@ 2014-06-27 9:37 ` Luke Lee
2014-06-28 1:53 ` Glenn Morris
2014-07-07 9:04 ` bug#17854: Completed Luke Lee
2 siblings, 1 reply; 20+ messages in thread
From: Luke Lee @ 2014-06-27 9:37 UTC (permalink / raw)
To: 17854
[-- Attachment #1.1: Type: text/plain, Size: 4513 bytes --]
Resubmit my fixed patch #3 for stylistic changes with white space changes
removed (git diff -w) as attached.
[PATCH] Other hideif enhancements and bug fixes:
* Add macro evaluation function and key binding.
* Merge continuous "..." into one on hiding.
* Fix original hideif bug that sometimes fail to hide the correct portion
in a
long ifdefs containing #elif and #else.
* Support hide/show commands in a marked region.
* Expand top level #ifdefs for C/C++ header files to prevent header files
always got hidden.
* Others.
2014-06-27 0:56 GMT+08:00 Glenn Morris <rgm@gnu.org>:
> Some stylistic comments only:
>
> It needs a ChangeLog entry, and probably a NEWS entry.
>
> > -;; Daniel LaLiberte <liberte@holonexus.org>
> > +;; Daniel LaLiberte <liberte@holonexus.org>
>
> Please don't change existing whitespace in areas that you are not
> otherwise touching.
>
> > -;; (unless 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
> > +;; (unless 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
>
> Again, this is just whitespace.
>
> > @@ -129,16 +129,44 @@
> > "Non-nil means shadow text instead of hiding it."
> > :type 'boolean
> > :group 'hide-ifdef
> > - :version "23.1")
> > + :version "24.5")
> >
> > (defface hide-ifdef-shadow '((t (:inherit shadow)))
> > "Face for shadowing ifdef blocks."
> > :group 'hide-ifdef
> > - :version "23.1")
> > + :version "24.5")
>
> Why is the :version changing, when the defaults are unchanged?
>
> > (defcustom hide-ifdef-exclude-define-regexp nil
> > "Ignore #define names if those names match this exclusion pattern."
> > :type 'string)
> > +(defcustom hide-ifdef-expand-reinclusion-protection t
> > + "When hiding header files, enabling this flag allows hideif always
> try to
> > +expand the re-inclusion protected ifdefs. Disabling this flag those
> headers
> > +are usually hidden to a top level #ifdef...#endif due to those defined
> symbols
>
> The first line of a doc-string should be a complete sentence that fits
> in < 80 columns. All the doc should fit within the standard fill-column.
>
> > + :type 'boolean
> > + :group 'hide-ifdef)
> > +
> > +(defcustom hide-ifdef-header-regexp-pattern
> > + "^.*\\.[hH]\\([hH]\\|[xX][xX]\\|[pP][pP]\\)?"
> > + "C/C++ header file name patterns. Effective only if
> > +`hide-ifdef-expand-reinclusion-protection' is t."
> > + :type 'string
> > + :group 'hide-ifdef)
>
> Again, the first line of the doc should be a complete sentence.
> New defcustoms need :version tags (and probably NEWS entries).
>
> > +(defvar hide-ifdef-env-backup nil
> > + "A backup variable to prevent `hide-ifdef-env' accidentally cleared by
> > +`hif-clear-all-ifdef-defined'.")
>
> First line of doc too long. Also, this is ungrammatical.
>
> > `hide-ifdef-env'
> > - An association list of defined and undefined symbols for the
> > - current buffer. Initially, the global value of `hide-ifdef-env'
> > - is used.
> > + An association list of defined and undefined symbols for the
> > + current project. Initially, the global value of
> `hide-ifdef-env'
> > + is used. This variable was a buffer-local variable but is now a
> > + global variable since we've extend hideif to support
> project-based
>
> s/extend/extended.
> "project-based across all-buffers" doesn't make sense.
> I'm not sure that describing how things used to work is helpful.
>
> > + across all-buffers. To simulate the original buffer local
> behavior
> > + we need to clear this variable (C-c @ C) then hide current
> buffer.
>
> > `hide-ifdef-define-alist'
> > - An association list of defined symbol lists.
> > + An association list of defined symbol lists.
>
> whitespace.
>
> > - Set to non-nil to not show #if, #ifdef, #ifndef, #else, and
> > - #endif lines when hiding.
> > + Set to non-nil to not show #if, #ifdef, #ifndef, #else, and
> > + #endif lines when hiding.
>
> whitespace.
>
>
> At this point, I'll give up, and ask you to send a version that does not
> have pointless whitespace changes. :)
>
--
Best regards,
Luke Lee
--
Best regards,
Luke Lee
[-- Attachment #1.2: Type: text/html, Size: 6369 bytes --]
[-- Attachment #2: 0003-Other-hideif-enhancements-and-bug-fixes.patch --]
[-- Type: application/octet-stream, Size: 28301 bytes --]
diff --git a/lisp/progmodes/hideif.el b/lisp/progmodes/hideif.el
index 4b78c08..5063710 100644
--- a/lisp/progmodes/hideif.el
+++ b/lisp/progmodes/hideif.el
@@ -138,7 +138,45 @@
(defcustom hide-ifdef-exclude-define-regexp nil
"Ignore #define names if those names match this exclusion pattern."
- :type 'string)
+ :type 'string
+ :version "24.4")
+
+(defcustom hide-ifdef-expand-reinclusion-protection t
+ "Prevent hiding the whole C/C++ header file protected by a big #ifdef..#endif.
+Most C/C++ headers are usually wrapped with ifdefs to prevent re-inclusion:
+
+ ----- beginning of file -----
+ #ifdef _XXX_HEADER_FILE_INCLUDED_
+ #define _XXX_HEADER_FILE_INCLUDED_
+ xxx
+ xxx
+ xxx...
+ #endif
+ ----- end of file -----
+
+If we try to hide this header file, for the first time hideif will find
+_XXX_HEADER_FILE_INCLUDED_ and have it defined. Everything between #ifdef
+to #endif are not hidden. For the second time since _XXX_HEADER_FILE_INCLUDED_
+is defined everything between the outermost #ifdef..#endif will be hidden:
+
+ ----- beginning of file -----
+ #ifdef _XXX_HEADER_FILE_INCLUDED_
+ ...
+ #endif
+ ----- end of file -----
+
+This is not the behavior we expected, we still would like to see the content
+of this header file. With this flag enabled we can have the outermost #if
+always not hidden."
+ :type 'boolean
+ :version "24.4")
+
+(defcustom hide-ifdef-header-regexp
+ "^.*\\.[hH]\\([hH]\\|[xX][xX]\\|[pP][pP]\\)?"
+ "C/C++ header file name patterns to determine if current buffer is a header.
+Effective only if `hide-ifdef-expand-reinclusion-protection' is t."
+ :type 'string
+ :group 'hide-ifdef)
(defvar hide-ifdef-mode-submap
;; Set up the submap that goes after the prefix key.
@@ -152,6 +190,8 @@
(define-key map "s" 'show-ifdefs)
(define-key map "\C-d" 'hide-ifdef-block)
(define-key map "\C-s" 'show-ifdef-block)
+ (define-key map "e" 'hif-evaluate-macro)
+ (define-key map "C" 'hif-clear-all-ifdef-defined)
(define-key map "\C-q" 'hide-ifdef-toggle-read-only)
(define-key map "\C-w" 'hide-ifdef-toggle-shadowing)
@@ -216,6 +256,11 @@
(defvar hide-ifdef-env nil
"An alist of defined symbols and their values.")
+(defvar hide-ifdef-env-backup nil
+ "This variable is a backup of the previously cleared `hide-ifdef-env'.
+This backup prevents any accidental clearance of `hide-fidef-env' by
+`hif-clear-all-ifdef-defined'.")
+
(defvar hif-outside-read-only nil
"Internal variable. Saves the value of `buffer-read-only' while hiding.")
@@ -233,8 +278,16 @@ Several variables affect how the hiding is done:
`hide-ifdef-env'
An association list of defined and undefined symbols for the
- current buffer. Initially, the global value of `hide-ifdef-env'
- is used.
+ current project. Initially, the global value of `hide-ifdef-env'
+ is used. This variable was a buffer-local variable which limits
+ hideif to parse only one C/C++ file at a time. We've extended
+ hideif to support parsing a C/C++ project containing multiple C/C++
+ source files opened simultaneously in different buffers. Therefore
+ `hide-ifdef-env' can no longer be buffer local but must be global.
+
+ We can still simulate the behavior of elder hideif versions (i.e.
+ `hide-ifdef-env' being buffer local) by clearing this variable
+ (C-c @ C) everytime before hiding current buffer.
`hide-ifdef-define-alist'
An association list of defined symbol lists.
@@ -259,8 +312,9 @@ Several variables affect how the hiding is done:
(if hide-ifdef-mode
(progn
;; inherit global values
- (set (make-local-variable 'hide-ifdef-env)
- (default-value 'hide-ifdef-env))
+;; (set (make-local-variable 'hide-ifdef-env)
+;; (default-value 'hide-ifdef-env))
+ (set 'hide-ifdef-env (default-value 'hide-ifdef-env))
(set (make-local-variable 'hide-ifdef-hiding)
(default-value 'hide-ifdef-hiding))
(set (make-local-variable 'hif-outside-read-only) buffer-read-only)
@@ -279,6 +333,14 @@ Several variables affect how the hiding is done:
(when hide-ifdef-hiding
(show-ifdefs))))
+(defun hif-clear-all-ifdef-defined ()
+ "Clears all symbols defined in `hide-ifdef-env'.
+It will backup this variable to `hide-ifdef-env-backup' before clearing to
+prevent accidental clearance."
+ (interactive)
+ (when (y-or-n-p "Clear all #defined symbols?")
+ (setq hide-ifdef-env-backup hide-ifdef-env)
+ (setq hide-ifdef-env nil)))
(defun hif-show-all ()
"Show all of the text in the current buffer."
@@ -298,16 +360,63 @@ Several variables affect how the hiding is done:
(while (= (logand 1 (skip-chars-backward "\\\\")) 1)
(end-of-line 2)))
+(defun hif-merge-ifdef-region (start end)
+ "This function merges nearby ifdef regions to form a bigger overlay.
+The region is defined by START and END. This will decrease the number of
+overlays created."
+ ;; 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' equals to nil while another part with 't,
+ ;; this case happens.
+ ;; TODO: Should we merge? or just create a container overlay? -- this can
+ ;; prevent `hideif-show-ifdef' expanding too many hidden contents since there
+ ;; is only a big overlay exists there without any smaller overlays.
+ (save-restriction
+ (widen) ; Otherwise `point-min' and `point-max' will be restricted and thus
+ ; fail to find neighbor overlays
+ (let ((begovrs (overlays-in
+ (max (- start 2) (point-min))
+ (max (- start 1) (point-min))))
+ (endovrs (overlays-in
+ (min (+ end 1) (point-max))
+ (min (+ end 2) (point-max))))
+ (ob nil)
+ (oe nil)
+ b e)
+ ;; Merge overlays before START
+ (dolist (o begovrs)
+ (when (overlay-get o 'hide-ifdef)
+ (setq b (min start (overlay-start o))
+ e (max end (overlay-end o)))
+ (move-overlay o b e)
+ (hif-merge-ifdef-region b e)
+ (setq ob o)))
+ ;; Merge overlays after END
+ (dolist (o endovrs)
+ (when (overlay-get o 'hide-ifdef)
+ (setq b (min start (overlay-start o))
+ e (max end (overlay-end o)))
+ (move-overlay o b e)
+ (hif-merge-ifdef-region b e)
+ (setf oe o)))
+ ;; If both START and END merging happens, merge into bigger one
+ (when (and ob oe)
+ (let ((b (min (overlay-start ob) (overlay-start oe)))
+ (e (max (overlay-end ob) (overlay-end oe))))
+ (delete-overlay oe)
+ (move-overlay ob b e)
+ (hif-merge-ifdef-region b e)))
+ (or ob oe))))
(defun hide-ifdef-region-internal (start end)
- (remove-overlays start end 'hide-ifdef t)
+ (unless (hif-merge-ifdef-region start end)
(let ((o (make-overlay start end)))
(overlay-put o 'hide-ifdef t)
(if hide-ifdef-shadow
(overlay-put o 'face 'hide-ifdef-shadow)
- (overlay-put o 'invisible 'hide-ifdef))))
+ (overlay-put o 'invisible 'hide-ifdef)))))
(defun hide-ifdef-region (start end)
- "START is the start of a #if or #else form. END is the ending part.
+ "START is the start of a #if, #elif, or #else form. END is the ending part.
Everything including these lines is made invisible."
(save-excursion
(goto-char start) (hif-end-of-line) (setq start (point))
@@ -316,7 +425,9 @@ Everything including these lines is made invisible."
(defun hif-show-ifdef-region (start end)
"Everything between START and END is made visible."
- (remove-overlays start end 'hide-ifdef t))
+ (let ((onum (length (overlays-in start end))))
+ (remove-overlays start end 'hide-ifdef t)
+ (/= onum (length (overlays-in start end)))))
;;===%%SF%% evaluation (Start) ===
@@ -375,10 +486,8 @@ that form should be displayed.")
(concat hif-cpp-prefix "\\(if\\(n?def\\)?\\|elif\\|define\\)[ \t]+"))
(defconst hif-white-regexp "[ \t]*")
-(defconst hif-define-regexp
- (concat hif-cpp-prefix "\\(define\\|undef\\)"))
-(defconst hif-id-regexp
- (concat "[[:alpha:]_][[:alnum:]_]*"))
+(defconst hif-define-regexp (concat hif-cpp-prefix "\\(define\\|undef\\)"))
+(defconst hif-id-regexp (concat "[[:alpha:]_][[:alnum:]_]*"))
(defconst hif-macroref-regexp
(concat hif-white-regexp "\\(" hif-id-regexp "\\)" hif-white-regexp
"\\("
@@ -499,6 +608,8 @@ that form should be displayed.")
(setq hif-simple-token-only nil)))
token-list)))
+ ((looking-at "\r") ; Sometimes MS-Windows user will leave CR in
+ (forward-char 1)) ; the source code. Let's don't stuck here.
(t (error "Bad #if expression: %s" (buffer-string)))))))
(nreverse token-list)))
@@ -1175,13 +1286,16 @@ Do this when cursor is at the beginning of `regexp' (i.e. #ifX)."
(if (= start (point))
(error "Mismatched #ifdef #endif pair")))
(cond ((hif-looking-at-endif)
- (hif-endif-to-ifdef) ; find beginning of nested if
- (hif-endif-to-ifdef)) ; find beginning of outer if or else
+ (hif-endif-to-ifdef) ; Find beginning of nested if
+ (hif-endif-to-ifdef)) ; Find beginning of outer if or else
+ ((hif-looking-at-elif)
+ (hif-endif-to-ifdef))
((hif-looking-at-else)
(hif-endif-to-ifdef))
((hif-looking-at-ifX)
'done)
- (t))) ; never gets here
+ (t
+ (error "Mismatched #endif")))) ; never gets here
(defun forward-ifdef (&optional arg)
@@ -1275,26 +1389,26 @@ With argument, do this that many times."
;;===%%SF%% hide-ifdef-hiding (Start) ===
-;;; A range is a structure with four components:
-;;; ELSE-P True if there was an else clause for the ifdef.
-;;; START The start of the range. (beginning of line)
-;;; ELSE The else marker (beginning of line)
-;;; Only valid if ELSE-P is true.
-;;; END The end of the range. (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)
- (list start else end))
+(defsubst hif-make-range (start end &optional else elif)
+ (list start else end elif))
(defsubst hif-range-start (range) (elt range 0))
(defsubst hif-range-else (range) (elt range 1))
(defsubst hif-range-end (range) (elt range 2))
+(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.
@@ -1303,19 +1417,23 @@ Point is left unchanged."
(save-excursion
(beginning-of-line)
(let ((start (point))
+ (elif nil)
(else nil)
(end nil))
- ;; Part one. Look for either #endif or #else.
+ ;; Part one. Look for either #elif, #else or #endif.
;; This loop-and-a-half dedicated to E. Dijkstra.
+ (while (and (not else) (not end))
(while (progn
(hif-find-next-relevant)
(hif-looking-at-ifX)) ; Skip nested ifdef
(hif-ifdef-to-endif))
- ;; Found either a #else or an #endif.
- (cond ((hif-looking-at-else)
+ ;; Found either a #else, #elif, or an #endif.
+ (cond ((hif-looking-at-elif)
+ (setq elif (nconc elif (list (point)))))
+ ((hif-looking-at-else)
(setq else (point)))
(t
- (setq end (point)))) ; (line-end-position)
+ (setq end (point)))))
;; If found #else, look for #endif.
(when else
(while (progn
@@ -1325,7 +1443,7 @@ Point is left unchanged."
(if (hif-looking-at-else)
(error "Found two elses in a row? Broken!"))
(setq end (point))) ; (line-end-position)
- (hif-make-range start end else))))
+ (hif-make-range start end else elif))))
;; A bit slimy.
@@ -1340,69 +1458,176 @@ Does nothing if `hide-ifdef-lines' is nil."
(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 #else.
-;;; The #if part must be hidden. The #else part must be processed
-;;; for nested #ifs.
-;;; Case 4: #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.
-
-(defun hif-recurse-on (start end)
+;; 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)
+
+(defun hif-recurse-on (start end &optional dont-go-eol)
"Call `hide-ifdef-guts' after narrowing to end of START line and END line."
(save-excursion
(save-restriction
(goto-char start)
- (end-of-line)
+ (unless dont-go-eol
+ (end-of-line))
(narrow-to-region (point) end)
- (hide-ifdef-guts))))
+ (let ((hif-recurse-level (1+ hif-recurse-level)))
+ (hide-ifdef-guts)))))
-(defun hif-possibly-hide ()
+(defun hif-possibly-hide (expand-reinclusion)
"Called at #ifX expression, this hides those parts that should be hidden.
It uses the judgment of `hide-ifdef-evaluator'."
;; (message "hif-possibly-hide") (sit-for 1)
- (let ((test (hif-canonicalize hif-ifx-regexp))
- (range (hif-find-range)))
+ (let* ((case-fold-search nil)
+ (test (hif-canonicalize hif-ifx-regexp))
+ (range (hif-find-range))
+ (elifs (hif-range-elif range))
+ (if-part t) ;; everytime we start from if-part
+ (complete nil))
;; (message "test = %s" test) (sit-for 1)
(hif-hide-line (hif-range-end range))
- (if (not (hif-not (funcall hide-ifdef-evaluator test)))
- (cond ((hif-range-else range) ; case 1
+ (while (not complete)
+ (if (and (not (and expand-reinclusion if-part))
+ (hif-not (funcall hide-ifdef-evaluator test)))
+ ;; ifX/elif is FALSE
+ (if elifs
+ ;; Case 3 - Hide the #ifX and eval #elif
+ (let ((newstart (car elifs)))
+ (hif-hide-line (hif-range-start range))
+ (hide-ifdef-region (hif-range-start range)
+ (1- newstart))
+ (setcar range newstart)
+ (goto-char newstart)
+ (setq elifs (cdr elifs))
+ (setq test (hif-canonicalize hif-elif-regexp)))
+
+ ;; Check for #else
+ (cond ((hif-range-else range)
+ ;; Case 4 - #else block visible
+ (hif-hide-line (hif-range-else range))
+ (hide-ifdef-region (hif-range-start range)
+ (1- (hif-range-else range)))
+ (hif-recurse-on (hif-range-else range)
+ (hif-range-end range)))
+ (t
+ ;; Case 5 - No #else block, hide #ifX
+ (hide-ifdef-region (point)
+ (1- (hif-range-end range)))))
+ (setq complete t))
+
+ ;; ifX/elif is TRUE
+ (cond (elifs
+ ;; Luke fix: distinguish from #elif..#elif to #elif..#else
+ (let ((elif (car elifs)))
+ ;; hide all elifs
+ (hif-hide-line elif)
+ (hide-ifdef-region elif (1- (hif-range-end range)))
+ (hif-recurse-on (hif-range-start range)
+ elif)))
+ ((hif-range-else range)
+ ;; Case 1 - Hide #elif and #else blocks, recurse #ifX
(hif-hide-line (hif-range-else range))
(hide-ifdef-region (hif-range-else range)
(1- (hif-range-end range)))
(hif-recurse-on (hif-range-start range)
(hif-range-else range)))
- (t ; case 2
+ (t
+ ;; Case 2 - No #else, just recurse #ifX
(hif-recurse-on (hif-range-start range)
(hif-range-end range))))
- (cond ((hif-range-else range) ; case 3
- (hif-hide-line (hif-range-else range))
- (hide-ifdef-region (hif-range-start range)
- (1- (hif-range-else range)))
- (hif-recurse-on (hif-range-else range)
- (hif-range-end range)))
- (t ; case 4
- (hide-ifdef-region (point)
- (1- (hif-range-end range))))))
+ (setq complete t))
+ (setq if-part nil))
+
+ ;; complete = t
(hif-hide-line (hif-range-start range)) ; Always hide start.
(goto-char (hif-range-end range))
(end-of-line)))
+(defun hif-evaluate-region (start end)
+ (let* ((tokens (ignore-errors ; Prevent C statement things like
+ ; 'do { ... } while (0)'
+ (hif-tokenize start end)))
+ (expr (and tokens
+ (condition-case nil
+ (hif-parse-exp tokens)
+ (error
+ tokens))))
+ (result (funcall hide-ifdef-evaluator expr)))
+ result))
+
+(defun hif-evaluate-macro (rstart rend)
+ "Evaluate the macro expansion result for a region.
+If no region active, find the current #ifdefs and evaluate the result. Currently
+it support only math calculations, strings or argumented macros can not be
+expanded."
+ (interactive "r")
+ (let ((case-fold-search nil))
+ (save-excursion
+ (unless mark-active
+ (setq rstart nil rend nil)
+ (beginning-of-line)
+ (when (and (re-search-forward hif-macro-expr-prefix-regexp nil t)
+ (string= "define" (match-string 2)))
+ (re-search-forward hif-macroref-regexp nil t)))
+ (let* ((start (or rstart (point)))
+ (end (or rend (progn (hif-end-of-line) (point))))
+ (defined nil)
+ (simple 't)
+ (tokens (ignore-errors ; Prevent C statement things like
+ ; 'do { ... } while (0)'
+ (hif-tokenize start end)))
+ (expr (or (and (<= (length tokens) 1) ; Simple token
+ (setq defined (assoc (car tokens) hide-ifdef-env))
+ (setq simple (atom (hif-lookup (car tokens))))
+ (hif-lookup (car tokens)))
+ (and tokens
+ (condition-case nil
+ (hif-parse-exp tokens)
+ (error
+ nil)))))
+ (result (funcall hide-ifdef-evaluator expr))
+ (exprstring (replace-regexp-in-string
+ ;; Trim off leading/trailing whites
+ "^[ \t]*\\([^ \t]+\\)[ \t]*" "\\1"
+ (replace-regexp-in-string
+ "\\(//.*\\)" "" ; Trim off end-of-line comments
+ (buffer-substring-no-properties start end)))))
+ (cond
+ ((and (<= (length tokens) 1) simple) ; Simple token
+ (if defined
+ (message "%S <= `%s'" result exprstring)
+ (message "`%s' is not defined" exprstring)))
+ ((integerp result)
+ (if (or (= 0 result) (= 1 result))
+ (message "%S <= `%s'" result exprstring)
+ (message "%S (0x%x) <= `%s'" result result exprstring)))
+ ((null result) (message "%S <= `%s'" 'false exprstring))
+ ((eq t result) (message "%S <= `%s'" 'true exprstring))
+ (t (message "%S <= `%s'" result exprstring)))
+ result))))
(defun hif-parse-macro-arglist (str)
"Parse argument list formatted as '( arg1 [ , argn] [...] )'.
@@ -1533,6 +1758,10 @@ It does not do the work that's pointless to redo on a recursive entry."
;; (message "hide-ifdef-guts")
(save-excursion
(let ((case-fold-search nil)
+ (expand-header (and hide-ifdef-expand-reinclusion-protection
+ (string-match hide-ifdef-header-regexp
+ (buffer-name (current-buffer)))
+ (zerop hif-recurse-level)))
min max)
(goto-char (point-min))
(setf min (point))
@@ -1540,7 +1769,7 @@ It does not do the work that's pointless to redo on a recursive entry."
(setf max (hif-find-any-ifX))
(hif-add-new-defines min max)
(if max
- (hif-possibly-hide))
+ (hif-possibly-hide expand-header))
(setf min (point))
while max))))
@@ -1587,17 +1816,30 @@ It does not do the work that's pointless to redo on a recursive entry."
(overlay-put overlay 'face nil)
(overlay-put overlay 'invisible 'hide-ifdef))))))
-(defun hide-ifdef-define (var)
- "Define a VAR so that #ifdef VAR would be included."
- (interactive "SDefine what? ")
- (hif-set-var var 1)
+(defun hide-ifdef-define (var val)
+ "Define a VAR optionally to a specific value VAL into `hide-ifdef-env'.
+This allows #ifdef VAR from being hidden."
+ (interactive
+ (let* ((default (save-excursion
+ (beginning-of-line)
+ (cond ((looking-at hif-ifx-else-endif-regexp)
+ (forward-word 2)
+ (current-word 'strict))
+ (t
+ nil))))
+ (var (read-minibuffer "Define what? " default))
+ (val (read-from-minibuffer (format "Set %s to? (default 1): " var)
+ nil nil t nil "1")))
+ (list var val)))
+ (hif-set-var var (or val 1))
+ (message "%s set to %s" var (or val 1))
+ (sleep-for 1)
(if hide-ifdef-hiding (hide-ifdefs)))
(defun hif-undefine-symbol (var)
(setq hide-ifdef-env
(delete (assoc var hide-ifdef-env) hide-ifdef-env)))
-
(defun hide-ifdef-undef (start end)
"Undefine a VAR so that #ifdef VAR would not be included."
(interactive "r")
@@ -1618,11 +1860,14 @@ It does not do the work that's pointless to redo on a recursive entry."
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
preprocessor if it were given the file with those symbols defined.
+If this command is prefixed, hide also the #ifdefs themselves.
Turn off hiding by calling `show-ifdefs'."
(interactive)
- (message "Hiding...")
+ (let ((hide-ifdef-lines current-prefix-arg))
+ (or nomsg
+ (message "Hiding..."))
(setq hif-outside-read-only buffer-read-only)
(unless hide-ifdef-mode (hide-ifdef-mode 1)) ; turn on hide-ifdef-mode
(if hide-ifdef-hiding
@@ -1631,7 +1876,7 @@ Turn off hiding by calling `show-ifdefs'."
(hide-ifdef-guts)
(setq buffer-read-only (or hide-ifdef-read-only hif-outside-read-only))
(or nomsg
- (message "Hiding done")))
+ (message "Hiding done"))))
(defun show-ifdefs ()
@@ -1663,9 +1908,15 @@ Return as (TOP . BOTTOM) the extent of ifdef block."
(min max-bottom (1- (point)))))))
-(defun hide-ifdef-block ()
- "Hide the ifdef block (true or false part) enclosing or before the cursor."
- (interactive)
+(defun hide-ifdef-block (&optional start end)
+ "Hide the ifdef block (true or false part) enclosing or before the cursor.
+If prefixed, it will also hide #ifdefs themselves."
+ (interactive "r")
+ (let ((hide-ifdef-lines current-prefix-arg))
+ (if mark-active
+ (let ((hif-recurse-level (1+ hif-recurse-level)))
+ (hif-recurse-on start end t)
+ (setq mark-active nil))
(unless hide-ifdef-mode (hide-ifdef-mode 1))
(let ((top-bottom (hif-find-ifdef-block)))
(hide-ifdef-region (car top-bottom) (cdr top-bottom))
@@ -1673,12 +1924,26 @@ Return as (TOP . BOTTOM) the extent of ifdef block."
(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)))
+ (setq buffer-read-only
+ (or hide-ifdef-read-only hif-outside-read-only)))))
-(defun show-ifdef-block ()
+(defun show-ifdef-block (&optional start end)
"Show the ifdef block (true or false part) enclosing or before the cursor."
- (interactive)
- (let ((top-bottom (hif-find-ifdef-block)))
+ (interactive "r")
+ (if mark-active
+ (progn
+ (dolist (o (overlays-in start end))
+ (if (overlay-get o 'hide-ifdef)
+ (delete-overlay o)))
+ (setq mark-active nil))
+ (let ((top-bottom (condition-case nil
+ (hif-find-ifdef-block)
+ (error
+ nil)))
+ (ovrs (overlays-in (max (point-min) (1- (point)))
+ (min (point-max) (1+ (point)))))
+ (del nil))
+ (if top-bottom
(if hide-ifdef-lines
(hif-show-ifdef-region
(save-excursion
@@ -1686,7 +1951,15 @@ Return as (TOP . BOTTOM) the extent of ifdef block."
(save-excursion
(goto-char (1+ (cdr top-bottom)))
(hif-end-of-line) (point)))
- (hif-show-ifdef-region (1- (car top-bottom)) (cdr top-bottom)))))
+ (setf del (hif-show-ifdef-region
+ (1- (car top-bottom)) (cdr top-bottom)))))
+ (if (not (and top-bottom
+ del))
+ (dolist (o ovrs)
+ ;;(dolist (o (overlays-in (1- (point)) (1+ (point))))
+ ;; (if (overlay-get o 'hide-ifdef) (message "%S" o)))
+ (if (overlay-get o 'hide-ifdef)
+ (delete-overlay o)))))))
;;; definition alist support
^ permalink raw reply related [flat|nested] 20+ messages in thread
* bug#17854: The patch #3 of 3 for hideif.el, a lot of bug fixes and enhancements
2014-06-27 9:37 ` Luke Lee
@ 2014-06-28 1:53 ` Glenn Morris
2014-06-28 3:22 ` Glenn Morris
2014-06-30 14:42 ` Luke Lee
0 siblings, 2 replies; 20+ messages in thread
From: Glenn Morris @ 2014-06-28 1:53 UTC (permalink / raw)
To: Luke Lee; +Cc: 17854
> with white space changes removed (git diff -w) as attached.
I hope that the version you install will actually not change the
whitespace, rather than that just being hidden with diff -w.
> (defcustom hide-ifdef-exclude-define-regexp nil
> "Ignore #define names if those names match this exclusion pattern."
> - :type 'string)
> + :type 'string
> + :version "24.4")
Why is the :version changing?
In any case, it is the wrong :version, since this won't be in 24.4.
> +(defcustom hide-ifdef-expand-reinclusion-protection t
> + "Prevent hiding the whole C/C++ header file protected by a big #ifdef..#endif.
Suggestion:
"Non-nil means don't hide an entire header file enclosed by #ifndef...#endif."
> +Most C/C++ headers are usually wrapped with ifdefs to prevent re-inclusion:
> +
> + ----- beginning of file -----
> + #ifdef _XXX_HEADER_FILE_INCLUDED_
#ifndef?
> + #define _XXX_HEADER_FILE_INCLUDED_
> + xxx
> + xxx
> + xxx...
> + #endif
> + ----- end of file -----
> +
> +If we try to hide this header file, for the first time hideif will find
> +_XXX_HEADER_FILE_INCLUDED_ and have it defined. Everything between #ifdef
> +to #endif are not hidden.
Suggestion:
"The first time we visit such a file, _XXX_HEADER_FILE_INCLUDED_ is
undefined, and so nothing is hidden. The next time we visit it,
everything will be hidden."
> For the second time since _XXX_HEADER_FILE_INCLUDED_
> +is defined everything between the outermost #ifdef..#endif will be hidden:
> +
> + ----- beginning of file -----
> + #ifdef _XXX_HEADER_FILE_INCLUDED_
> + ...
> + #endif
> + ----- end of file -----
I don't think the example is necessary.
> +This is not the behavior we expected, we still would like to see the content
> +of this header file. With this flag enabled we can have the outermost #if
> +always not hidden."
Suggestion:
"This behavior is generally undesirable. If this option is non-nil, the
outermost #if is always visible."
(I wonder: why would anyone want to set this option to nil?)
> + :type 'boolean
> + :version "24.4")
24.5
(Also, new options are the kind of thing to mention in a brief NEWS entry.)
> +(defcustom hide-ifdef-header-regexp
> + "^.*\\.[hH]\\([hH]\\|[xX][xX]\\|[pP][pP]\\)?"
> + "C/C++ header file name patterns to determine if current buffer is a header.
> +Effective only if `hide-ifdef-expand-reinclusion-protection' is t."
> + :type 'string
> + :group 'hide-ifdef)
Missing :version.
> - current buffer. Initially, the global value of `hide-ifdef-env'
> - is used.
> + current project. Initially, the global value of `hide-ifdef-env'
> + is used. This variable was a buffer-local variable which limits
"," before which
> + hideif to parse only one C/C++ file at a time. We've extended
> + hideif to support parsing a C/C++ project containing multiple C/C++
> + source files opened simultaneously in different buffers. Therefore
> + `hide-ifdef-env' can no longer be buffer local but must be global.
> +
> + We can still simulate the behavior of elder hideif versions (i.e.
s/elder/older
> + `hide-ifdef-env' being buffer local) by clearing this variable
> + (C-c @ C) everytime before hiding current buffer.
Does all this detail need to be in the doc-string, or can it just be a
comment in the code?
(By the way, no need to make ChangeLog entries for comments.)
> +(defun hif-clear-all-ifdef-defined ()
> + "Clears all symbols defined in `hide-ifdef-env'.
> +It will backup this variable to `hide-ifdef-env-backup' before clearing to
> +prevent accidental clearance."
Is that backup really necessary, given that you ask for confirmation?
> + (interactive)
> + (when (y-or-n-p "Clear all #defined symbols?")
> + (setq hide-ifdef-env-backup hide-ifdef-env)
> + (setq hide-ifdef-env nil)))
>
> + ;; Genernally there is no need to call itself recursively since there should
Generally
> + ((looking-at "\r") ; Sometimes MS-Windows user will leave CR in
> + (forward-char 1)) ; the source code. Let's don't stuck here.
"Let's not get stuck here."
> -(defun hif-possibly-hide ()
> +(defun hif-possibly-hide (expand-reinclusion)
> "Called at #ifX expression, this hides those parts that should be hidden.
> It uses the judgment of `hide-ifdef-evaluator'."
Doc should say what the argument means.
> +(defun hif-evaluate-macro (rstart rend)
> + "Evaluate the macro expansion result for a region.
> +If no region active, find the current #ifdefs and evaluate the result. Currently
Use two spaces between sentences.
> +it support only math calculations, strings or argumented macros can not be
supports
> -(defun hide-ifdef-define (var)
> +(defun hide-ifdef-define (var val)
Why not make VAL optional?
> + "Define a VAR optionally to a specific value VAL into `hide-ifdef-env'.
> +This allows #ifdef VAR from being hidden."
Suggestion:
"Define VAR to VAL (default 1) in `hide-ifdef-env'."
> @@ -1618,11 +1860,14 @@ It does not do the work that's pointless to redo on a recursive entry."
> 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
> preprocessor if it were given the file with those symbols defined.
> +If this command is prefixed, hide also the #ifdefs themselves.
Suggestion:
"With optional prefix agument ARG, also hide the #ifdefs themselves."
> (interactive)
> - (message "Hiding...")
> + (let ((hide-ifdef-lines current-prefix-arg))
I think this should take an explicit prefix argument.
> +(defun hide-ifdef-block (&optional start end)
> + "Hide the ifdef block (true or false part) enclosing or before the cursor.
> +If prefixed, it will also hide #ifdefs themselves."
Suggestion:
"With optional prefix agument ARG, also hide the #ifdefs themselves."
> + (interactive "r")
> + (let ((hide-ifdef-lines current-prefix-arg))
I think this should explicitly take a prefix argument.
^ permalink raw reply [flat|nested] 20+ messages in thread
* bug#17854: The patch #3 of 3 for hideif.el, a lot of bug fixes and enhancements
2014-06-28 1:53 ` Glenn Morris
@ 2014-06-28 3:22 ` Glenn Morris
2014-06-30 14:42 ` Luke Lee
1 sibling, 0 replies; 20+ messages in thread
From: Glenn Morris @ 2014-06-28 3:22 UTC (permalink / raw)
To: Luke Lee; +Cc: 17854
Glenn Morris wrote:
>> +(defcustom hide-ifdef-header-regexp
>> + "^.*\\.[hH]\\([hH]\\|[xX][xX]\\|[pP][pP]\\)?"
This would be better written as
"\\.[hH]\\([hH]\\|[xX][xX]\\|[pP][pP]\\)?\\'"
(why not just case-fold?)
Also, you match it against buffer-name, which is fragile to things like
uniquify. Seems it would be better to match the file name.
^ permalink raw reply [flat|nested] 20+ messages in thread
* bug#17854: The patch #3 of 3 for hideif.el, a lot of bug fixes and enhancements
2014-06-28 1:53 ` Glenn Morris
2014-06-28 3:22 ` Glenn Morris
@ 2014-06-30 14:42 ` Luke Lee
2014-06-30 21:47 ` Stefan Monnier
2014-07-01 6:44 ` Glenn Morris
1 sibling, 2 replies; 20+ messages in thread
From: Luke Lee @ 2014-06-30 14:42 UTC (permalink / raw)
To: 17854
[-- Attachment #1: Type: text/plain, Size: 33291 bytes --]
>I hope that the version you install will actually not change the
>whitespace, rather than that just being hidden with diff -w.
Sure, I will. However, my hideif modification was splitted into 3
patches and I already submitted 2 so I'll keep those white
space changes that should went to the earlier 2 patches. I
learnt this rule "no white space change alone" too late. For
thosecodes that I did not touch I will keep the white spaces.
Sorry about that.
>> + :version "24.4")
>
>Why is the :version changing?
>In any case, it is the wrong :version, since this won't be in 24.4.
Isn't the Emacs trunk currently at version 24.4.XX.X? IIUC,
according to the document of ":version" shouldn't this be the
Emacs version that I introduced those variables? Since I
consider the 3 patches as one so I use current version.
>"This behavior is generally undesirable. If this option is non-nil, the
>outermost #if is always visible."
>
>(I wonder: why would anyone want to set this option to nil?)
IMHO usually don't, but I don't know if there will be cases that
some header files are written as:
#if XXXXXXXXXXX
...
#elif YYYYYYYYY
...
#elif ZZZZZZZZZZ
...
#endif
And each part is very long, maybe someone would like only one
portion to be visible? If so, maybe making this variable a buffer
local would make more sense? What do you think?
>This would be better written as
>
>"\\.[hH]\\([hH]\\|[xX][xX]\\|[pP][pP]\\)?\\'"
Thanks, followed
>(why not just case-fold?)
That variable is used only once in `hide-ifdef-guts' and it already
bound case fold as nil with `let' (for matching lower-cased ifdef)
so I use [hH] to match both cases.
But, after a second thought since it is a customizable
variable that users might forget about the upper/lower case
so using case-fold could help users from making this kind of
errors. So I change `hide-ifdef-guts' to use `let*' instead of
`let' and bind `case-fold-search' twice once with `'t' for this
and another with `nil' for lower-cased ifdef.
>Also, you match it against buffer-name, which is fragile to things like
>uniquify. Seems it would be better to match the file name.
Great! I changed it to `buffer-file-name' instead.
>> +(defun hif-clear-all-ifdef-defined ()
>> + "Clears all symbols defined in `hide-ifdef-env'.
>...
>Is that backup really necessary, given that you ask for confirmation?
When we hide 10+ headers and save 5000 symbols in `hide-ifdef-env',
it's really painful to clear it without saving it (I did that). Before I
added
my TODO: `automatically search for all header files using "#include"',
I would like to have this backup there. Once we can have #include
search works, it's okay to clear this variable without any backup.
>Suggestion:
>"With optional prefix agument ARG, also hide the #ifdefs themselves."
>> (interactive)
>> - (message "Hiding...")
>> + (let ((hide-ifdef-lines current-prefix-arg))
>
>I think this should take an explicit prefix argument.
Originally, I followed, adding optional argument "arg" added
and applied `interactive "P"'. Change all the callers to include
one more argument.
However, I need to keep the `current-prefix-arg' since locally I bind
"C-c @ C-h" (compare with the default hide-ifdefs shortcut key
"C-c @ h") for "strong-hide-ifdefs" to invoke hide-ifdefs with a prefix
command automatically:
(defun strong-hide-ifdefs ()
"Perform hideif-defs including the #ifdef themselves."
(interactive)
(let ((current-prefix-arg t))
(hide-ifdefs)))
Thus, "C-u C-c @ h" or the shorter "C-c @ C-h" both works.
If I use use ARG in the let binding of `hide-ifdefs', strong-hide-ifdefs
won't work so I kept the `current-prefix-arg'. Therefore, 'arg' become
an unused argument so I roll my code back. If you got better idea
about how to implement things like `strong-hide-ifdefs', please tell
me. Others might meet the same problem.
>> +(defun hide-ifdef-block (&optional start end)
>> + "Hide the ifdef block (true or false part) enclosing or before the
cursor.
>> +If prefixed, it will also hide #ifdefs themselves."
>
>Suggestion:
>"With optional prefix agument ARG, also hide the #ifdefs themselves."
It's nice but I can make it work with either `interactive "Pr"' or
`interactive "rP" so I keep it unchanged.
Also thanks for all those doc-string/comment corrections, I'm not a
good English tech writer :-) Everything you suggested is great
and I followed them all. The following is the new patch #3 in
plain texts.
Best regards,
Luke Lee
===========================================
diff --git a/lisp/progmodes/hideif.el b/lisp/progmodes/hideif.el
index 6585668..713d330 100644
--- a/lisp/progmodes/hideif.el
+++ b/lisp/progmodes/hideif.el
@@ -138,7 +138,38 @@
(defcustom hide-ifdef-exclude-define-regexp nil
"Ignore #define names if those names match this exclusion pattern."
- :type 'string)
+ :type 'string
+ :version "24.4")
+
+(defcustom hide-ifdef-expand-reinclusion-protection t
+ "Non-nil means don't hide an entire header file enclused by
#ifndef...#endif.
+Most C/C++ headers are usually wrapped with ifdefs to prevent re-inclusion:
+
+ ----- beginning of file -----
+ #ifndef _XXX_HEADER_FILE_INCLUDED_
+ #define _XXX_HEADER_FILE_INCLUDED_
+ xxx
+ xxx
+ xxx...
+ #endif
+ ----- end of file -----
+
+The first time we visit such a file, _XXX_HEADER_FILE_INCLUDED_ is
+undefined, and so nothing is hidden. The next time we visit it, everything
will
+be hidden.
+
+This behavior is generally undesirable. If this option is non-nil, the
outermost
+#if is always visible."
+ :type 'boolean
+ :version "24.4")
+
+(defcustom hide-ifdef-header-regexp
+ "\\.h\\(h\\|xx\\|pp\\)?"
+ "C/C++ header file name patterns to determine if current buffer is a
header.
+Effective only if `hide-ifdef-expand-reinclusion-protection' is t."
+ :type 'string
+ :group 'hide-ifdef
+ :version "24.4")
(defvar hide-ifdef-mode-submap
;; Set up the submap that goes after the prefix key.
@@ -152,6 +183,8 @@
(define-key map "s" 'show-ifdefs)
(define-key map "\C-d" 'hide-ifdef-block)
(define-key map "\C-s" 'show-ifdef-block)
+ (define-key map "e" 'hif-evaluate-macro)
+ (define-key map "C" 'hif-clear-all-ifdef-defined)
(define-key map "\C-q" 'hide-ifdef-toggle-read-only)
(define-key map "\C-w" 'hide-ifdef-toggle-shadowing)
@@ -216,6 +249,11 @@
(defvar hide-ifdef-env nil
"An alist of defined symbols and their values.")
+(defvar hide-ifdef-env-backup nil
+ "This variable is a backup of the previously cleared `hide-ifdef-env'.
+This backup prevents any accidental clearance of `hide-fidef-env' by
+`hif-clear-all-ifdef-defined'.")
+
(defvar hif-outside-read-only nil
"Internal variable. Saves the value of `buffer-read-only' while
hiding.")
@@ -233,8 +271,12 @@ Several variables affect how the hiding is done:
`hide-ifdef-env'
An association list of defined and undefined symbols for the
- current buffer. Initially, the global value of `hide-ifdef-env'
- is used.
+ current project. Initially, the global value of `hide-ifdef-env'
+ is used. This variable was a buffer-local variable, which limits
+ hideif to parse only one C/C++ file at a time. We've extended
+ hideif to support parsing a C/C++ project containing multiple C/C++
+ source files opened simultaneously in different buffers. Therefore
+ `hide-ifdef-env' can no longer be buffer local but must be global.
`hide-ifdef-define-alist'
An association list of defined symbol lists.
@@ -259,8 +301,14 @@ Several variables affect how the hiding is done:
(if hide-ifdef-mode
(progn
;; inherit global values
- (set (make-local-variable 'hide-ifdef-env)
- (default-value 'hide-ifdef-env))
+
+ ;; `hide-ifdef-env' is now a global variable.
+ ;; We can still simulate the behavior of older hideif versions
(i.e.
+ ;; `hide-ifdef-env' being buffer local) by clearing this variable
+ ;; (C-c @ C) everytime before hiding current buffer.
+;; (set (make-local-variable 'hide-ifdef-env)
+;; (default-value 'hide-ifdef-env))
+ (set 'hide-ifdef-env (default-value 'hide-ifdef-env))
(set (make-local-variable 'hide-ifdef-hiding)
(default-value 'hide-ifdef-hiding))
(set (make-local-variable 'hif-outside-read-only) buffer-read-only)
@@ -279,6 +327,14 @@ Several variables affect how the hiding is done:
(when hide-ifdef-hiding
(show-ifdefs))))
+(defun hif-clear-all-ifdef-defined ()
+ "Clears all symbols defined in `hide-ifdef-env'.
+It will backup this variable to `hide-ifdef-env-backup' before clearing to
+prevent accidental clearance."
+ (interactive)
+ (when (y-or-n-p "Clear all #defined symbols?")
+ (setq hide-ifdef-env-backup hide-ifdef-env)
+ (setq hide-ifdef-env nil)))
(defun hif-show-all ()
"Show all of the text in the current buffer."
@@ -298,16 +354,64 @@ Several variables affect how the hiding is done:
(while (= (logand 1 (skip-chars-backward "\\\\")) 1)
(end-of-line 2)))
+(defun hif-merge-ifdef-region (start end)
+ "This function merges nearby ifdef regions to form a bigger overlay.
+The region is defined by START and END. This will decrease the number of
+overlays created."
+ ;; Generally 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' equals to nil while another part with
't,
+ ;; this case happens.
+ ;; TODO: Should we merge? or just create a container overlay? -- this can
+ ;; prevent `hideif-show-ifdef' expanding too many hidden contents since
there
+ ;; is only a big overlay exists there without any smaller overlays.
+ (save-restriction
+ (widen) ; Otherwise `point-min' and `point-max' will be restricted and
thus
+ ; fail to find neighbor overlays
+ (let ((begovrs (overlays-in
+ (max (- start 2) (point-min))
+ (max (- start 1) (point-min))))
+ (endovrs (overlays-in
+ (min (+ end 1) (point-max))
+ (min (+ end 2) (point-max))))
+ (ob nil)
+ (oe nil)
+ b e)
+ ;; Merge overlays before START
+ (dolist (o begovrs)
+ (when (overlay-get o 'hide-ifdef)
+ (setq b (min start (overlay-start o))
+ e (max end (overlay-end o)))
+ (move-overlay o b e)
+ (hif-merge-ifdef-region b e)
+ (setq ob o)))
+ ;; Merge overlays after END
+ (dolist (o endovrs)
+ (when (overlay-get o 'hide-ifdef)
+ (setq b (min start (overlay-start o))
+ e (max end (overlay-end o)))
+ (move-overlay o b e)
+ (hif-merge-ifdef-region b e)
+ (setf oe o)))
+ ;; If both START and END merging happens, merge into bigger one
+ (when (and ob oe)
+ (let ((b (min (overlay-start ob) (overlay-start oe)))
+ (e (max (overlay-end ob) (overlay-end oe))))
+ (delete-overlay oe)
+ (move-overlay ob b e)
+ (hif-merge-ifdef-region b e)))
+ (or ob oe))))
+
(defun hide-ifdef-region-internal (start end)
- (remove-overlays start end 'hide-ifdef t)
+ (unless (hif-merge-ifdef-region start end)
(let ((o (make-overlay start end)))
(overlay-put o 'hide-ifdef t)
(if hide-ifdef-shadow
(overlay-put o 'face 'hide-ifdef-shadow)
- (overlay-put o 'invisible 'hide-ifdef))))
+ (overlay-put o 'invisible 'hide-ifdef)))))
(defun hide-ifdef-region (start end)
- "START is the start of a #if or #else form. END is the ending part.
+ "START is the start of a #if, #elif, or #else form. END is the ending
part.
Everything including these lines is made invisible."
(save-excursion
(goto-char start) (hif-end-of-line) (setq start (point))
@@ -316,7 +420,9 @@ Everything including these lines is made invisible."
(defun hif-show-ifdef-region (start end)
"Everything between START and END is made visible."
- (remove-overlays start end 'hide-ifdef t))
+ (let ((onum (length (overlays-in start end))))
+ (remove-overlays start end 'hide-ifdef t)
+ (/= onum (length (overlays-in start end)))))
;;===%%SF%% evaluation (Start) ===
@@ -375,10 +481,8 @@ that form should be displayed.")
(concat hif-cpp-prefix "\\(if\\(n?def\\)?\\|elif\\|define\\)[ \t]+"))
(defconst hif-white-regexp "[ \t]*")
-(defconst hif-define-regexp
- (concat hif-cpp-prefix "\\(define\\|undef\\)"))
-(defconst hif-id-regexp
- (concat "[[:alpha:]_][[:alnum:]_]*"))
+(defconst hif-define-regexp (concat hif-cpp-prefix
"\\(define\\|undef\\)"))
+(defconst hif-id-regexp (concat "[[:alpha:]_][[:alnum:]_]*"))
(defconst hif-macroref-regexp
(concat hif-white-regexp "\\(" hif-id-regexp "\\)" hif-white-regexp
"\\("
@@ -499,6 +603,8 @@ that form should be displayed.")
(setq hif-simple-token-only nil)))
token-list)))
+ ((looking-at "\r") ; Sometimes MS-Windows user will leave CR in
+ (forward-char 1)) ; the source code. Let's not get stuck here.
(t (error "Bad #if expression: %s" (buffer-string)))))))
(nreverse token-list)))
@@ -1173,13 +1279,16 @@ Do this when cursor is at the beginning of `regexp'
(i.e. #ifX)."
(if (= start (point))
(error "Mismatched #ifdef #endif pair")))
(cond ((hif-looking-at-endif)
- (hif-endif-to-ifdef) ; find beginning of nested if
- (hif-endif-to-ifdef)) ; find beginning of outer if or else
+ (hif-endif-to-ifdef) ; Find beginning of nested if
+ (hif-endif-to-ifdef)) ; Find beginning of outer if or else
+ ((hif-looking-at-elif)
+ (hif-endif-to-ifdef))
((hif-looking-at-else)
(hif-endif-to-ifdef))
((hif-looking-at-ifX)
'done)
- (t))) ; never gets here
+ (t
+ (error "Mismatched #endif")))) ; never gets here
(defun forward-ifdef (&optional arg)
@@ -1273,26 +1382,25 @@ With argument, do this that many times."
;;===%%SF%% hide-ifdef-hiding (Start) ===
-;;; A range is a structure with four components:
-;;; ELSE-P True if there was an else clause for the ifdef.
-;;; START The start of the range. (beginning of line)
-;;; ELSE The else marker (beginning of line)
-;;; Only valid if ELSE-P is true.
-;;; END The end of the range. (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)
- (list start else end))
+(defsubst hif-make-range (start end &optional else elif)
+ (list start else end elif))
(defsubst hif-range-start (range) (elt range 0))
(defsubst hif-range-else (range) (elt range 1))
(defsubst hif-range-end (range) (elt range 2))
+(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.
@@ -1301,19 +1409,23 @@ Point is left unchanged."
(save-excursion
(beginning-of-line)
(let ((start (point))
+ (elif nil)
(else nil)
(end nil))
- ;; Part one. Look for either #endif or #else.
+ ;; Part one. Look for either #elif, #else or #endif.
;; This loop-and-a-half dedicated to E. Dijkstra.
+ (while (and (not else) (not end))
(while (progn
(hif-find-next-relevant)
(hif-looking-at-ifX)) ; Skip nested ifdef
(hif-ifdef-to-endif))
- ;; Found either a #else or an #endif.
- (cond ((hif-looking-at-else)
+ ;; Found either a #else, #elif, or an #endif.
+ (cond ((hif-looking-at-elif)
+ (setq elif (nconc elif (list (point)))))
+ ((hif-looking-at-else)
(setq else (point)))
(t
- (setq end (point)))) ; (line-end-position)
+ (setq end (point)))))
;; If found #else, look for #endif.
(when else
(while (progn
@@ -1323,7 +1435,7 @@ Point is left unchanged."
(if (hif-looking-at-else)
(error "Found two elses in a row? Broken!"))
(setq end (point))) ; (line-end-position)
- (hif-make-range start end else))))
+ (hif-make-range start end else elif))))
;; A bit slimy.
@@ -1338,70 +1450,180 @@ Does nothing if `hide-ifdef-lines' is nil."
(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 #else.
-;;; The #if part must be hidden. The #else part must be processed
-;;; for nested #ifs.
-;;; Case 4: #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.
-
-(defun hif-recurse-on (start end)
+;; 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)
+
+(defun hif-recurse-on (start end &optional dont-go-eol)
"Call `hide-ifdef-guts' after narrowing to end of START line and END
line."
(save-excursion
(save-restriction
(goto-char start)
- (end-of-line)
+ (unless dont-go-eol
+ (end-of-line))
(narrow-to-region (point) end)
- (hide-ifdef-guts))))
+ (let ((hif-recurse-level (1+ hif-recurse-level)))
+ (hide-ifdef-guts)))))
-(defun hif-possibly-hide ()
+(defun hif-possibly-hide (expand-reinclusion)
"Called at #ifX expression, this hides those parts that should be hidden.
-It uses the judgment of `hide-ifdef-evaluator'."
+It uses the judgment of `hide-ifdef-evaluator'. EXPAND-REINCLUSION is a
flag
+indicating that we should expand the #ifdef even if it should be hidden.
+Refer to `hide-ifdef-expand-reinclusion-protection' for more details."
;; (message "hif-possibly-hide") (sit-for 1)
- (let ((test (hif-canonicalize hif-ifx-regexp))
- (range (hif-find-range)))
+ (let* ((case-fold-search nil)
+ (test (hif-canonicalize hif-ifx-regexp))
+ (range (hif-find-range))
+ (elifs (hif-range-elif range))
+ (if-part t) ; Everytime we start from if-part
+ (complete nil))
;; (message "test = %s" test) (sit-for 1)
(hif-hide-line (hif-range-end range))
- (if (not (hif-not (funcall hide-ifdef-evaluator test)))
- (cond ((hif-range-else range) ; case 1
+ (while (not complete)
+ (if (and (not (and expand-reinclusion if-part))
+ (hif-not (funcall hide-ifdef-evaluator test)))
+ ;; ifX/elif is FALSE
+ (if elifs
+ ;; Case 3 - Hide the #ifX and eval #elif
+ (let ((newstart (car elifs)))
+ (hif-hide-line (hif-range-start range))
+ (hide-ifdef-region (hif-range-start range)
+ (1- newstart))
+ (setcar range newstart)
+ (goto-char newstart)
+ (setq elifs (cdr elifs))
+ (setq test (hif-canonicalize hif-elif-regexp)))
+
+ ;; Check for #else
+ (cond ((hif-range-else range)
+ ;; Case 4 - #else block visible
+ (hif-hide-line (hif-range-else range))
+ (hide-ifdef-region (hif-range-start range)
+ (1- (hif-range-else range)))
+ (hif-recurse-on (hif-range-else range)
+ (hif-range-end range)))
+ (t
+ ;; Case 5 - No #else block, hide #ifX
+ (hide-ifdef-region (point)
+ (1- (hif-range-end range)))))
+ (setq complete t))
+
+ ;; ifX/elif is TRUE
+ (cond (elifs
+ ;; Luke fix: distinguish from #elif..#elif to #elif..#else
+ (let ((elif (car elifs)))
+ ;; hide all elifs
+ (hif-hide-line elif)
+ (hide-ifdef-region elif (1- (hif-range-end range)))
+ (hif-recurse-on (hif-range-start range)
+ elif)))
+ ((hif-range-else range)
+ ;; Case 1 - Hide #elif and #else blocks, recurse #ifX
(hif-hide-line (hif-range-else range))
(hide-ifdef-region (hif-range-else range)
(1- (hif-range-end range)))
(hif-recurse-on (hif-range-start range)
(hif-range-else range)))
- (t ; case 2
+ (t
+ ;; Case 2 - No #else, just recurse #ifX
(hif-recurse-on (hif-range-start range)
(hif-range-end range))))
- (cond ((hif-range-else range) ; case 3
- (hif-hide-line (hif-range-else range))
- (hide-ifdef-region (hif-range-start range)
- (1- (hif-range-else range)))
- (hif-recurse-on (hif-range-else range)
- (hif-range-end range)))
- (t ; case 4
- (hide-ifdef-region (point)
- (1- (hif-range-end range))))))
+ (setq complete t))
+ (setq if-part nil))
+
+ ;; complete = t
(hif-hide-line (hif-range-start range)) ; Always hide start.
(goto-char (hif-range-end range))
(end-of-line)))
+(defun hif-evaluate-region (start end)
+ (let* ((tokens (ignore-errors ; Prevent C statement things like
+ ; 'do { ... } while (0)'
+ (hif-tokenize start end)))
+ (expr (and tokens
+ (condition-case nil
+ (hif-parse-exp tokens)
+ (error
+ tokens))))
+ (result (funcall hide-ifdef-evaluator expr)))
+ result))
+
+(defun hif-evaluate-macro (rstart rend)
+ "Evaluate the macro expansion result for a region.
+If no region active, find the current #ifdefs and evaluate the result.
+Currently it supports only math calculations, strings or argumented macros
can
+not be expanded."
+ (interactive "r")
+ (let ((case-fold-search nil))
+ (save-excursion
+ (unless mark-active
+ (setq rstart nil rend nil)
+ (beginning-of-line)
+ (when (and (re-search-forward hif-macro-expr-prefix-regexp nil t)
+ (string= "define" (match-string 2)))
+ (re-search-forward hif-macroref-regexp nil t)))
+ (let* ((start (or rstart (point)))
+ (end (or rend (progn (hif-end-of-line) (point))))
+ (defined nil)
+ (simple 't)
+ (tokens (ignore-errors ; Prevent C statement things like
+ ; 'do { ... } while (0)'
+ (hif-tokenize start end)))
+ (expr (or (and (<= (length tokens) 1) ; Simple token
+ (setq defined (assoc (car tokens)
hide-ifdef-env))
+ (setq simple (atom (hif-lookup (car tokens))))
+ (hif-lookup (car tokens)))
+ (and tokens
+ (condition-case nil
+ (hif-parse-exp tokens)
+ (error
+ nil)))))
+ (result (funcall hide-ifdef-evaluator expr))
+ (exprstring (replace-regexp-in-string
+ ;; Trim off leading/trailing whites
+ "^[ \t]*\\([^ \t]+\\)[ \t]*" "\\1"
+ (replace-regexp-in-string
+ "\\(//.*\\)" "" ; Trim off end-of-line comments
+ (buffer-substring-no-properties start end)))))
+ (cond
+ ((and (<= (length tokens) 1) simple) ; Simple token
+ (if defined
+ (message "%S <= `%s'" result exprstring)
+ (message "`%s' is not defined" exprstring)))
+ ((integerp result)
+ (if (or (= 0 result) (= 1 result))
+ (message "%S <= `%s'" result exprstring)
+ (message "%S (0x%x) <= `%s'" result result exprstring)))
+ ((null result) (message "%S <= `%s'" 'false exprstring))
+ ((eq t result) (message "%S <= `%s'" 'true exprstring))
+ (t (message "%S <= `%s'" result exprstring)))
+ result))))
+
(defun hif-parse-macro-arglist (str)
"Parse argument list formatted as '( arg1 [ , argn] [...] )'.
The '...' is also included. Return a list of the arguments, if '...'
exists the
@@ -1529,7 +1751,12 @@ first arg will be `hif-etc'."
It does not do the work that's pointless to redo on a recursive entry."
;; (message "hide-ifdef-guts")
(save-excursion
- (let ((case-fold-search nil)
+ (let* ((case-fold-search t) ; Ignore case for
`hide-ifdef-header-regexp'
+ (expand-header (and hide-ifdef-expand-reinclusion-protection
+ (string-match hide-ifdef-header-regexp
+ (buffer-file-name))
+ (zerop hif-recurse-level)))
+ (case-fold-search nil)
min max)
(goto-char (point-min))
(setf min (point))
@@ -1537,7 +1764,7 @@ It does not do the work that's pointless to redo on a
recursive entry."
(setf max (hif-find-any-ifX))
(hif-add-new-defines min max)
(if max
- (hif-possibly-hide))
+ (hif-possibly-hide expand-header))
(setf min (point))
while max))))
@@ -1584,17 +1811,30 @@ It does not do the work that's pointless to redo on
a recursive entry."
(overlay-put overlay 'face nil)
(overlay-put overlay 'invisible 'hide-ifdef))))))
-(defun hide-ifdef-define (var)
- "Define a VAR so that #ifdef VAR would be included."
- (interactive "SDefine what? ")
- (hif-set-var var 1)
+(defun hide-ifdef-define (var &optional val)
+ "Define a VAR to VAL (DEFAULT 1) in `hide-ifdef-env'.
+This allows #ifdef VAR to be hidden."
+ (interactive
+ (let* ((default (save-excursion
+ (beginning-of-line)
+ (cond ((looking-at hif-ifx-else-endif-regexp)
+ (forward-word 2)
+ (current-word 'strict))
+ (t
+ nil))))
+ (var (read-minibuffer "Define what? " default))
+ (val (read-from-minibuffer (format "Set %s to? (default 1): "
var)
+ nil nil t nil "1")))
+ (list var val)))
+ (hif-set-var var (or val 1))
+ (message "%s set to %s" var (or val 1))
+ (sleep-for 1)
(if hide-ifdef-hiding (hide-ifdefs)))
(defun hif-undefine-symbol (var)
(setq hide-ifdef-env
(delete (assoc var hide-ifdef-env) hide-ifdef-env)))
-
(defun hide-ifdef-undef (start end)
"Undefine a VAR so that #ifdef VAR would not be included."
(interactive "r")
@@ -1615,20 +1855,23 @@ It does not do the work that's pointless to redo on
a recursive entry."
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
preprocessor if it were given the file with those symbols defined.
+With prefix command presents it will also hide the #ifdefs themselves.
Turn off hiding by calling `show-ifdefs'."
(interactive)
- (message "Hiding...")
+ (let ((hide-ifdef-lines current-prefix-arg))
+ (or nomsg
+ (message "Hiding..."))
(setq hif-outside-read-only buffer-read-only)
- (unless hide-ifdef-mode (hide-ifdef-mode 1)) ; turn on hide-ifdef-mode
+ (unless hide-ifdef-mode (hide-ifdef-mode 1)) ; Turn on hide-ifdef-mode
(if hide-ifdef-hiding
(show-ifdefs)) ; Otherwise, deep confusion.
(setq hide-ifdef-hiding t)
(hide-ifdef-guts)
(setq buffer-read-only (or hide-ifdef-read-only hif-outside-read-only))
(or nomsg
- (message "Hiding done")))
+ (message "Hiding done"))))
(defun show-ifdefs ()
@@ -1660,9 +1903,15 @@ Return as (TOP . BOTTOM) the extent of ifdef block."
(min max-bottom (1- (point)))))))
-(defun hide-ifdef-block ()
- "Hide the ifdef block (true or false part) enclosing or before the
cursor."
- (interactive)
+(defun hide-ifdef-block (&optional start end)
+ "Hide the ifdef block (true or false part) enclosing or before the
cursor.
+With prefix command presents it will also hide the #ifdefs themselves."
+ (interactive "r")
+ (let ((hide-ifdef-lines current-prefix-arg))
+ (if mark-active
+ (let ((hif-recurse-level (1+ hif-recurse-level)))
+ (hif-recurse-on start end t)
+ (setq mark-active nil))
(unless hide-ifdef-mode (hide-ifdef-mode 1))
(let ((top-bottom (hif-find-ifdef-block)))
(hide-ifdef-region (car top-bottom) (cdr top-bottom))
@@ -1670,12 +1919,26 @@ Return as (TOP . BOTTOM) the extent of ifdef block."
(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)))
+ (setq buffer-read-only
+ (or hide-ifdef-read-only hif-outside-read-only)))))
-(defun show-ifdef-block ()
+(defun show-ifdef-block (&optional start end)
"Show the ifdef block (true or false part) enclosing or before the
cursor."
- (interactive)
- (let ((top-bottom (hif-find-ifdef-block)))
+ (interactive "r")
+ (if mark-active
+ (progn
+ (dolist (o (overlays-in start end))
+ (if (overlay-get o 'hide-ifdef)
+ (delete-overlay o)))
+ (setq mark-active nil))
+ (let ((top-bottom (condition-case nil
+ (hif-find-ifdef-block)
+ (error
+ nil)))
+ (ovrs (overlays-in (max (point-min) (1- (point)))
+ (min (point-max) (1+ (point)))))
+ (del nil))
+ (if top-bottom
(if hide-ifdef-lines
(hif-show-ifdef-region
(save-excursion
@@ -1683,7 +1946,15 @@ Return as (TOP . BOTTOM) the extent of ifdef block."
(save-excursion
(goto-char (1+ (cdr top-bottom)))
(hif-end-of-line) (point)))
- (hif-show-ifdef-region (1- (car top-bottom)) (cdr top-bottom)))))
+ (setf del (hif-show-ifdef-region
+ (1- (car top-bottom)) (cdr top-bottom)))))
+ (if (not (and top-bottom
+ del))
+ (dolist (o ovrs)
+ ;;(dolist (o (overlays-in (1- (point)) (1+ (point))))
+ ;; (if (overlay-get o 'hide-ifdef) (message "%S" o)))
+ (if (overlay-get o 'hide-ifdef)
+ (delete-overlay o)))))))
;;; definition alist support
[-- Attachment #2: Type: text/html, Size: 44159 bytes --]
^ permalink raw reply related [flat|nested] 20+ messages in thread
* bug#17854: The patch #3 of 3 for hideif.el, a lot of bug fixes and enhancements
2014-06-30 14:42 ` Luke Lee
@ 2014-06-30 21:47 ` Stefan Monnier
2014-07-01 1:55 ` Luke Lee
2014-07-01 6:44 ` Glenn Morris
1 sibling, 1 reply; 20+ messages in thread
From: Stefan Monnier @ 2014-06-30 21:47 UTC (permalink / raw)
To: Luke Lee; +Cc: 17854
>> In any case, it is the wrong :version, since this won't be in 24.4.
> Isn't the Emacs trunk currently at version 24.4.XX.X? IIUC,
24.4.XX.X is "strictly greater than 24.4". More specifically NN.MM.50.Y
is the development code for NN.MM+1 and NN.MM.9X.Y is the pretest (aka
"beta test" for NN.MM+1.
IOW, the trunk will be released as 24.5.
Stefan
^ permalink raw reply [flat|nested] 20+ messages in thread
* bug#17854: The patch #3 of 3 for hideif.el, a lot of bug fixes and enhancements
2014-06-30 21:47 ` Stefan Monnier
@ 2014-07-01 1:55 ` Luke Lee
0 siblings, 0 replies; 20+ messages in thread
From: Luke Lee @ 2014-07-01 1:55 UTC (permalink / raw)
To: 17854
[-- Attachment #1: Type: text/plain, Size: 584 bytes --]
Understood, I will modify all related :version to 24.5 accordingly. Thanks!
Best regards,
Luke Lee
2014-07-01 5:47 GMT+08:00 Stefan Monnier <monnier@iro.umontreal.ca>:
> >> In any case, it is the wrong :version, since this won't be in 24.4.
> > Isn't the Emacs trunk currently at version 24.4.XX.X? IIUC,
>
> 24.4.XX.X is "strictly greater than 24.4". More specifically NN.MM.50.Y
> is the development code for NN.MM+1 and NN.MM.9X.Y is the pretest (aka
> "beta test" for NN.MM+1.
>
> IOW, the trunk will be released as 24.5.
>
>
> Stefan
>
--
Best regards,
Luke Lee
[-- Attachment #2: Type: text/html, Size: 1211 bytes --]
^ permalink raw reply [flat|nested] 20+ messages in thread
* bug#17854: The patch #3 of 3 for hideif.el, a lot of bug fixes and enhancements
2014-06-30 14:42 ` Luke Lee
2014-06-30 21:47 ` Stefan Monnier
@ 2014-07-01 6:44 ` Glenn Morris
2014-07-03 2:37 ` Luke Lee
1 sibling, 1 reply; 20+ messages in thread
From: Glenn Morris @ 2014-07-01 6:44 UTC (permalink / raw)
To: Luke Lee; +Cc: 17854
Luke Lee wrote:
> Sure, I will. However, my hideif modification was splitted into 3
> patches and I already submitted 2 so I'll keep those white
> space changes that should went to the earlier 2 patches. I
> learnt this rule "no white space change alone" too late. For
> thosecodes that I did not touch I will keep the white spaces.
> Sorry about that.
That's fine, no problem.
>>> + :version "24.4")
>>
>>Why is the :version changing?
I'm still confused as to why the :version is changing.
Did you change it in a previous patch and forget to add :version?
If so, you can just fix that now. No need to wait and put everything in
one big patch.
> IMHO usually don't, but I don't know if there will be cases that
> some header files are written as:
>
> #if XXXXXXXXXXX
> ...
> #elif YYYYYYYYY
> ...
> #elif ZZZZZZZZZZ
> ...
> #endif
>
> And each part is very long, maybe someone would like only one
> portion to be visible?
Ah, ok, makes sense.
> If so, maybe making this variable a buffer local would make more
> sense? What do you think?
Yes, perhaps. (I don't use hideif so I can't really comment...)
>>> (interactive)
>>> - (message "Hiding...")
>>> + (let ((hide-ifdef-lines current-prefix-arg))
>>
>>I think this should take an explicit prefix argument.
>
> Originally, I followed, adding optional argument "arg" added
> and applied `interactive "P"'. Change all the callers to include
> one more argument.
> However, I need to keep the `current-prefix-arg' since locally I bind
> "C-c @ C-h" (compare with the default hide-ifdefs shortcut key
> "C-c @ h") for "strong-hide-ifdefs" to invoke hide-ifdefs with a prefix
> command automatically:
>
> (defun strong-hide-ifdefs ()
> "Perform hideif-defs including the #ifdef themselves."
> (interactive)
> (let ((current-prefix-arg t))
> (hide-ifdefs)))
>
> Thus, "C-u C-c @ h" or the shorter "C-c @ C-h" both works.
> If I use use ARG in the let binding of `hide-ifdefs', strong-hide-ifdefs
> won't work so I kept the `current-prefix-arg'. Therefore, 'arg' become
> an unused argument so I roll my code back. If you got better idea
> about how to implement things like `strong-hide-ifdefs', please tell
> me. Others might meet the same problem.
I don't really follow.
It sounds like you want to call function A, which calls functions B,
which calls function C; and you want to change some aspect of how C
behaves? IMO yes, just giving A, B, and C explicit arguments would be better.
But you've thought about it more than I have.
>>> +(defun hide-ifdef-block (&optional start end)
>>> + "Hide the ifdef block (true or false part) enclosing or before the
> cursor.
>>> +If prefixed, it will also hide #ifdefs themselves."
>>
>>Suggestion:
>>"With optional prefix agument ARG, also hide the #ifdefs themselves."
>
> It's nice but I can make it work with either `interactive "Pr"' or
> `interactive "rP" so I keep it unchanged.
I don't follow. Why not just pick one?
> +(defcustom hide-ifdef-expand-reinclusion-protection t
> + "Non-nil means don't hide an entire header file enclused by
> #ifndef...#endif.
s/enclused/enclosed
> +undefined, and so nothing is hidden. The next time we visit it, everything
Two spaces between sentences (various places).
> +(defcustom hide-ifdef-header-regexp
> + "\\.h\\(h\\|xx\\|pp\\)?"
I think you want to anchor this with \\' at the end.
> +(defun hide-ifdef-define (var &optional val)
> + "Define a VAR to VAL (DEFAULT 1) in `hide-ifdef-env'.
s/DEFAULT/default
^ permalink raw reply [flat|nested] 20+ messages in thread
* bug#17854: The patch #3 of 3 for hideif.el, a lot of bug fixes and enhancements
2014-07-01 6:44 ` Glenn Morris
@ 2014-07-03 2:37 ` Luke Lee
2014-07-07 0:02 ` Glenn Morris
0 siblings, 1 reply; 20+ messages in thread
From: Luke Lee @ 2014-07-03 2:37 UTC (permalink / raw)
To: Glenn Morris; +Cc: 17854
[-- Attachment #1: Type: text/plain, Size: 29930 bytes --]
>>>
>>>Suggestion:
>>>"With optional prefix agument ARG, also hide the #ifdefs themselves."
>>
>> It's nice but I can make it work with either `interactive "Pr"' or
>> `interactive "rP" so I keep it unchanged.
>
> I don't follow. Why not just pick one?
I fixed that. Just use `interactive "P\nr"' then it works.
All other suggestions are followed, please take a look at the updated
patch as follows in plain texts.
====================================
diff --git a/lisp/progmodes/hideif.el b/lisp/progmodes/hideif.el
index f6562f3..ed76bd6 100644
--- a/lisp/progmodes/hideif.el
+++ b/lisp/progmodes/hideif.el
@@ -141,6 +141,36 @@
:type 'string
:version "24.5")
+(defcustom hide-ifdef-expand-reinclusion-protection t
+ "Non-nil means don't hide an entire header file enclosed by
#ifndef...#endif.
+Most C/C++ headers are usually wrapped with ifdefs to prevent re-inclusion:
+
+ ----- beginning of file -----
+ #ifndef _XXX_HEADER_FILE_INCLUDED_
+ #define _XXX_HEADER_FILE_INCLUDED_
+ xxx
+ xxx
+ xxx...
+ #endif
+ ----- end of file -----
+
+The first time we visit such a file, _XXX_HEADER_FILE_INCLUDED_ is
+undefined, and so nothing is hidden. The next time we visit it,
everything will
+be hidden.
+
+This behavior is generally undesirable. If this option is non-nil, the
outermost
+#if is always visible."
+ :type 'boolean
+ :version "24.5")
+
+(defcustom hide-ifdef-header-regexp
+ "\\.h\\(h\\|xx\\|pp\\)?\\'"
+ "C/C++ header file name patterns to determine if current buffer is a
header.
+Effective only if `hide-ifdef-expand-reinclusion-protection' is t."
+ :type 'string
+ :group 'hide-ifdef
+ :version "24.5")
+
(defvar hide-ifdef-mode-submap
;; Set up the submap that goes after the prefix key.
(let ((map (make-sparse-keymap)))
@@ -153,6 +183,8 @@
(define-key map "s" 'show-ifdefs)
(define-key map "\C-d" 'hide-ifdef-block)
(define-key map "\C-s" 'show-ifdef-block)
+ (define-key map "e" 'hif-evaluate-macro)
+ (define-key map "C" 'hif-clear-all-ifdef-defined)
(define-key map "\C-q" 'hide-ifdef-toggle-read-only)
(define-key map "\C-w" 'hide-ifdef-toggle-shadowing)
@@ -217,6 +249,11 @@
(defvar hide-ifdef-env nil
"An alist of defined symbols and their values.")
+(defvar hide-ifdef-env-backup nil
+ "This variable is a backup of the previously cleared `hide-ifdef-env'.
+This backup prevents any accidental clearance of `hide-fidef-env' by
+`hif-clear-all-ifdef-defined'.")
+
(defvar hif-outside-read-only nil
"Internal variable. Saves the value of `buffer-read-only' while
hiding.")
@@ -234,8 +271,12 @@ Several variables affect how the hiding is done:
`hide-ifdef-env'
An association list of defined and undefined symbols for the
- current buffer. Initially, the global value of `hide-ifdef-env'
- is used.
+ current project. Initially, the global value of `hide-ifdef-env'
+ is used. This variable was a buffer-local variable, which limits
+ hideif to parse only one C/C++ file at a time. We've extended
+ hideif to support parsing a C/C++ project containing multiple C/C++
+ source files opened simultaneously in different buffers. Therefore
+ `hide-ifdef-env' can no longer be buffer local but must be global.
`hide-ifdef-define-alist'
An association list of defined symbol lists.
@@ -260,8 +301,18 @@ Several variables affect how the hiding is done:
(if hide-ifdef-mode
(progn
;; inherit global values
- (set (make-local-variable 'hide-ifdef-env)
- (default-value 'hide-ifdef-env))
+
+ ;; `hide-ifdef-env' is now a global variable.
+ ;; We can still simulate the behavior of older hideif versions
(i.e.
+ ;; `hide-ifdef-env' being buffer local) by clearing this variable
+ ;; (C-c @ C) everytime before hiding current buffer.
+;; (set (make-local-variable 'hide-ifdef-env)
+;; (default-value 'hide-ifdef-env))
+ (set 'hide-ifdef-env (default-value 'hide-ifdef-env))
+ ;; Some C/C++ headers might have other ways to prevent reinclusion
and
+ ;; thus would like `hide-ifdef-expand-reinclusion-protection' to
be nil.
+ (set (make-local-variable
'hide-ifdef-expand-reinclusion-protection)
+ (default-value 'hide-ifdef-expand-reinclusion-protection))
(set (make-local-variable 'hide-ifdef-hiding)
(default-value 'hide-ifdef-hiding))
(set (make-local-variable 'hif-outside-read-only) buffer-read-only)
@@ -280,6 +331,14 @@ Several variables affect how the hiding is done:
(when hide-ifdef-hiding
(show-ifdefs))))
+(defun hif-clear-all-ifdef-defined ()
+ "Clears all symbols defined in `hide-ifdef-env'.
+It will backup this variable to `hide-ifdef-env-backup' before clearing to
+prevent accidental clearance."
+ (interactive)
+ (when (y-or-n-p "Clear all #defined symbols? ")
+ (setq hide-ifdef-env-backup hide-ifdef-env)
+ (setq hide-ifdef-env nil)))
(defun hif-show-all ()
"Show all of the text in the current buffer."
@@ -299,16 +358,64 @@ Several variables affect how the hiding is done:
(while (= (logand 1 (skip-chars-backward "\\\\")) 1)
(end-of-line 2)))
+(defun hif-merge-ifdef-region (start end)
+ "This function merges nearby ifdef regions to form a bigger overlay.
+The region is defined by START and END. This will decrease the number of
+overlays created."
+ ;; Generally 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' equals to nil while another part with
't,
+ ;; this case happens.
+ ;; TODO: Should we merge? or just create a container overlay? -- this can
+ ;; prevent `hideif-show-ifdef' expanding too many hidden contents since
there
+ ;; is only a big overlay exists there without any smaller overlays.
+ (save-restriction
+ (widen) ; Otherwise `point-min' and `point-max' will be restricted and
thus
+ ; fail to find neighbor overlays
+ (let ((begovrs (overlays-in
+ (max (- start 2) (point-min))
+ (max (- start 1) (point-min))))
+ (endovrs (overlays-in
+ (min (+ end 1) (point-max))
+ (min (+ end 2) (point-max))))
+ (ob nil)
+ (oe nil)
+ b e)
+ ;; Merge overlays before START
+ (dolist (o begovrs)
+ (when (overlay-get o 'hide-ifdef)
+ (setq b (min start (overlay-start o))
+ e (max end (overlay-end o)))
+ (move-overlay o b e)
+ (hif-merge-ifdef-region b e)
+ (setq ob o)))
+ ;; Merge overlays after END
+ (dolist (o endovrs)
+ (when (overlay-get o 'hide-ifdef)
+ (setq b (min start (overlay-start o))
+ e (max end (overlay-end o)))
+ (move-overlay o b e)
+ (hif-merge-ifdef-region b e)
+ (setf oe o)))
+ ;; If both START and END merging happens, merge into bigger one
+ (when (and ob oe)
+ (let ((b (min (overlay-start ob) (overlay-start oe)))
+ (e (max (overlay-end ob) (overlay-end oe))))
+ (delete-overlay oe)
+ (move-overlay ob b e)
+ (hif-merge-ifdef-region b e)))
+ (or ob oe))))
+
(defun hide-ifdef-region-internal (start end)
- (remove-overlays start end 'hide-ifdef t)
+ (unless (hif-merge-ifdef-region start end)
(let ((o (make-overlay start end)))
(overlay-put o 'hide-ifdef t)
(if hide-ifdef-shadow
(overlay-put o 'face 'hide-ifdef-shadow)
- (overlay-put o 'invisible 'hide-ifdef))))
+ (overlay-put o 'invisible 'hide-ifdef)))))
(defun hide-ifdef-region (start end)
- "START is the start of a #if or #else form. END is the ending part.
+ "START is the start of a #if, #elif, or #else form. END is the ending
part.
Everything including these lines is made invisible."
(save-excursion
(goto-char start) (hif-end-of-line) (setq start (point))
@@ -317,7 +424,9 @@ Everything including these lines is made invisible."
(defun hif-show-ifdef-region (start end)
"Everything between START and END is made visible."
- (remove-overlays start end 'hide-ifdef t))
+ (let ((onum (length (overlays-in start end))))
+ (remove-overlays start end 'hide-ifdef t)
+ (/= onum (length (overlays-in start end)))))
;;===%%SF%% evaluation (Start) ===
@@ -376,10 +485,8 @@ that form should be displayed.")
(concat hif-cpp-prefix "\\(if\\(n?def\\)?\\|elif\\|define\\)[ \t]+"))
(defconst hif-white-regexp "[ \t]*")
-(defconst hif-define-regexp
- (concat hif-cpp-prefix "\\(define\\|undef\\)"))
-(defconst hif-id-regexp
- (concat "[[:alpha:]_][[:alnum:]_]*"))
+(defconst hif-define-regexp (concat hif-cpp-prefix
"\\(define\\|undef\\)"))
+(defconst hif-id-regexp (concat "[[:alpha:]_][[:alnum:]_]*"))
(defconst hif-macroref-regexp
(concat hif-white-regexp "\\(" hif-id-regexp "\\)" hif-white-regexp
"\\("
@@ -500,6 +607,8 @@ that form should be displayed.")
(setq hif-simple-token-only nil)))
token-list)))
+ ((looking-at "\r") ; Sometimes MS-Windows user will leave CR in
+ (forward-char 1)) ; the source code. Let's not get stuck here.
(t (error "Bad #if expression: %s" (buffer-string)))))))
(nreverse token-list)))
@@ -573,7 +682,7 @@ that form should be displayed.")
(defun hif-expand-token-list (tokens &optional macroname expand_list)
"Perform expansion on TOKENS till everything expanded.
Self-reference (directly or indirectly) tokens are not expanded.
-EXPAND_LIST is the list of macro names currently being expanded, use for
+EXPAND_LIST is the list of macro names currently being expanded, used for
detecting self-reference."
(catch 'self-referencing
(let ((expanded nil)
@@ -1174,13 +1283,16 @@ Do this when cursor is at the beginning of `regexp'
(i.e. #ifX)."
(if (= start (point))
(error "Mismatched #ifdef #endif pair")))
(cond ((hif-looking-at-endif)
- (hif-endif-to-ifdef) ; find beginning of nested if
- (hif-endif-to-ifdef)) ; find beginning of outer if or else
+ (hif-endif-to-ifdef) ; Find beginning of nested if
+ (hif-endif-to-ifdef)) ; Find beginning of outer if or else
+ ((hif-looking-at-elif)
+ (hif-endif-to-ifdef))
((hif-looking-at-else)
(hif-endif-to-ifdef))
((hif-looking-at-ifX)
'done)
- (t))) ; never gets here
+ (t
+ (error "Mismatched #endif")))) ; never gets here
(defun forward-ifdef (&optional arg)
@@ -1274,26 +1386,25 @@ With argument, do this that many times."
;;===%%SF%% hide-ifdef-hiding (Start) ===
-;;; A range is a structure with four components:
-;;; ELSE-P True if there was an else clause for the ifdef.
-;;; START The start of the range. (beginning of line)
-;;; ELSE The else marker (beginning of line)
-;;; Only valid if ELSE-P is true.
-;;; END The end of the range. (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)
- (list start else end))
+(defsubst hif-make-range (start end &optional else elif)
+ (list start else end elif))
(defsubst hif-range-start (range) (elt range 0))
(defsubst hif-range-else (range) (elt range 1))
(defsubst hif-range-end (range) (elt range 2))
+(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.
@@ -1302,19 +1413,23 @@ Point is left unchanged."
(save-excursion
(beginning-of-line)
(let ((start (point))
+ (elif nil)
(else nil)
(end nil))
- ;; Part one. Look for either #endif or #else.
+ ;; Part one. Look for either #elif, #else or #endif.
;; This loop-and-a-half dedicated to E. Dijkstra.
+ (while (and (not else) (not end))
(while (progn
(hif-find-next-relevant)
(hif-looking-at-ifX)) ; Skip nested ifdef
(hif-ifdef-to-endif))
- ;; Found either a #else or an #endif.
- (cond ((hif-looking-at-else)
+ ;; Found either a #else, #elif, or an #endif.
+ (cond ((hif-looking-at-elif)
+ (setq elif (nconc elif (list (point)))))
+ ((hif-looking-at-else)
(setq else (point)))
(t
- (setq end (point)))) ; (line-end-position)
+ (setq end (point)))))
;; If found #else, look for #endif.
(when else
(while (progn
@@ -1324,7 +1439,7 @@ Point is left unchanged."
(if (hif-looking-at-else)
(error "Found two elses in a row? Broken!"))
(setq end (point))) ; (line-end-position)
- (hif-make-range start end else))))
+ (hif-make-range start end else elif))))
;; A bit slimy.
@@ -1339,70 +1454,180 @@ Does nothing if `hide-ifdef-lines' is nil."
(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 #else.
-;;; The #if part must be hidden. The #else part must be processed
-;;; for nested #ifs.
-;;; Case 4: #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.
-
-(defun hif-recurse-on (start end)
+;; 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)
+
+(defun hif-recurse-on (start end &optional dont-go-eol)
"Call `hide-ifdef-guts' after narrowing to end of START line and END
line."
(save-excursion
(save-restriction
(goto-char start)
- (end-of-line)
+ (unless dont-go-eol
+ (end-of-line))
(narrow-to-region (point) end)
- (hide-ifdef-guts))))
+ (let ((hif-recurse-level (1+ hif-recurse-level)))
+ (hide-ifdef-guts)))))
-(defun hif-possibly-hide ()
+(defun hif-possibly-hide (expand-reinclusion)
"Called at #ifX expression, this hides those parts that should be hidden.
-It uses the judgment of `hide-ifdef-evaluator'."
+It uses the judgment of `hide-ifdef-evaluator'. EXPAND-REINCLUSION is a
flag
+indicating that we should expand the #ifdef even if it should be hidden.
+Refer to `hide-ifdef-expand-reinclusion-protection' for more details."
;; (message "hif-possibly-hide") (sit-for 1)
- (let ((test (hif-canonicalize hif-ifx-regexp))
- (range (hif-find-range)))
+ (let* ((case-fold-search nil)
+ (test (hif-canonicalize hif-ifx-regexp))
+ (range (hif-find-range))
+ (elifs (hif-range-elif range))
+ (if-part t) ; Everytime we start from if-part
+ (complete nil))
;; (message "test = %s" test) (sit-for 1)
(hif-hide-line (hif-range-end range))
- (if (not (hif-not (funcall hide-ifdef-evaluator test)))
- (cond ((hif-range-else range) ; case 1
+ (while (not complete)
+ (if (and (not (and expand-reinclusion if-part))
+ (hif-not (funcall hide-ifdef-evaluator test)))
+ ;; ifX/elif is FALSE
+ (if elifs
+ ;; Case 3 - Hide the #ifX and eval #elif
+ (let ((newstart (car elifs)))
+ (hif-hide-line (hif-range-start range))
+ (hide-ifdef-region (hif-range-start range)
+ (1- newstart))
+ (setcar range newstart)
+ (goto-char newstart)
+ (setq elifs (cdr elifs))
+ (setq test (hif-canonicalize hif-elif-regexp)))
+
+ ;; Check for #else
+ (cond ((hif-range-else range)
+ ;; Case 4 - #else block visible
+ (hif-hide-line (hif-range-else range))
+ (hide-ifdef-region (hif-range-start range)
+ (1- (hif-range-else range)))
+ (hif-recurse-on (hif-range-else range)
+ (hif-range-end range)))
+ (t
+ ;; Case 5 - No #else block, hide #ifX
+ (hide-ifdef-region (point)
+ (1- (hif-range-end range)))))
+ (setq complete t))
+
+ ;; ifX/elif is TRUE
+ (cond (elifs
+ ;; Luke fix: distinguish from #elif..#elif to #elif..#else
+ (let ((elif (car elifs)))
+ ;; hide all elifs
+ (hif-hide-line elif)
+ (hide-ifdef-region elif (1- (hif-range-end range)))
+ (hif-recurse-on (hif-range-start range)
+ elif)))
+ ((hif-range-else range)
+ ;; Case 1 - Hide #elif and #else blocks, recurse #ifX
(hif-hide-line (hif-range-else range))
(hide-ifdef-region (hif-range-else range)
(1- (hif-range-end range)))
(hif-recurse-on (hif-range-start range)
(hif-range-else range)))
- (t ; case 2
+ (t
+ ;; Case 2 - No #else, just recurse #ifX
(hif-recurse-on (hif-range-start range)
(hif-range-end range))))
- (cond ((hif-range-else range) ; case 3
- (hif-hide-line (hif-range-else range))
- (hide-ifdef-region (hif-range-start range)
- (1- (hif-range-else range)))
- (hif-recurse-on (hif-range-else range)
- (hif-range-end range)))
- (t ; case 4
- (hide-ifdef-region (point)
- (1- (hif-range-end range))))))
+ (setq complete t))
+ (setq if-part nil))
+
+ ;; complete = t
(hif-hide-line (hif-range-start range)) ; Always hide start.
(goto-char (hif-range-end range))
(end-of-line)))
+(defun hif-evaluate-region (start end)
+ (let* ((tokens (ignore-errors ; Prevent C statement things like
+ ; 'do { ... } while (0)'
+ (hif-tokenize start end)))
+ (expr (and tokens
+ (condition-case nil
+ (hif-parse-exp tokens)
+ (error
+ tokens))))
+ (result (funcall hide-ifdef-evaluator expr)))
+ result))
+
+(defun hif-evaluate-macro (rstart rend)
+ "Evaluate the macro expansion result for a region.
+If no region active, find the current #ifdefs and evaluate the result.
+Currently it supports only math calculations, strings or argumented macros
can
+not be expanded."
+ (interactive "r")
+ (let ((case-fold-search nil))
+ (save-excursion
+ (unless mark-active
+ (setq rstart nil rend nil)
+ (beginning-of-line)
+ (when (and (re-search-forward hif-macro-expr-prefix-regexp nil t)
+ (string= "define" (match-string 2)))
+ (re-search-forward hif-macroref-regexp nil t)))
+ (let* ((start (or rstart (point)))
+ (end (or rend (progn (hif-end-of-line) (point))))
+ (defined nil)
+ (simple 't)
+ (tokens (ignore-errors ; Prevent C statement things like
+ ; 'do { ... } while (0)'
+ (hif-tokenize start end)))
+ (expr (or (and (<= (length tokens) 1) ; Simple token
+ (setq defined (assoc (car tokens)
hide-ifdef-env))
+ (setq simple (atom (hif-lookup (car tokens))))
+ (hif-lookup (car tokens)))
+ (and tokens
+ (condition-case nil
+ (hif-parse-exp tokens)
+ (error
+ nil)))))
+ (result (funcall hide-ifdef-evaluator expr))
+ (exprstring (replace-regexp-in-string
+ ;; Trim off leading/trailing whites
+ "^[ \t]*\\([^ \t]+\\)[ \t]*" "\\1"
+ (replace-regexp-in-string
+ "\\(//.*\\)" "" ; Trim off end-of-line comments
+ (buffer-substring-no-properties start end)))))
+ (cond
+ ((and (<= (length tokens) 1) simple) ; Simple token
+ (if defined
+ (message "%S <= `%s'" result exprstring)
+ (message "`%s' is not defined" exprstring)))
+ ((integerp result)
+ (if (or (= 0 result) (= 1 result))
+ (message "%S <= `%s'" result exprstring)
+ (message "%S (0x%x) <= `%s'" result result exprstring)))
+ ((null result) (message "%S <= `%s'" 'false exprstring))
+ ((eq t result) (message "%S <= `%s'" 'true exprstring))
+ (t (message "%S <= `%s'" result exprstring)))
+ result))))
+
(defun hif-parse-macro-arglist (str)
"Parse argument list formatted as '( arg1 [ , argn] [...] )'.
The '...' is also included. Return a list of the arguments, if '...'
exists the
@@ -1530,7 +1755,12 @@ first arg will be `hif-etc'."
It does not do the work that's pointless to redo on a recursive entry."
;; (message "hide-ifdef-guts")
(save-excursion
- (let ((case-fold-search nil)
+ (let* ((case-fold-search t) ; Ignore case for
`hide-ifdef-header-regexp'
+ (expand-header (and hide-ifdef-expand-reinclusion-protection
+ (string-match hide-ifdef-header-regexp
+ (buffer-file-name))
+ (zerop hif-recurse-level)))
+ (case-fold-search nil)
min max)
(goto-char (point-min))
(setf min (point))
@@ -1538,7 +1768,7 @@ It does not do the work that's pointless to redo on a
recursive entry."
(setf max (hif-find-any-ifX))
(hif-add-new-defines min max)
(if max
- (hif-possibly-hide))
+ (hif-possibly-hide expand-header))
(setf min (point))
while max))))
@@ -1585,17 +1815,30 @@ It does not do the work that's pointless to redo on
a recursive entry."
(overlay-put overlay 'face nil)
(overlay-put overlay 'invisible 'hide-ifdef))))))
-(defun hide-ifdef-define (var)
- "Define a VAR so that #ifdef VAR would be included."
- (interactive "SDefine what? ")
- (hif-set-var var 1)
+(defun hide-ifdef-define (var &optional val)
+ "Define a VAR to VAL (default 1) in `hide-ifdef-env'.
+This allows #ifdef VAR to be hidden."
+ (interactive
+ (let* ((default (save-excursion
+ (beginning-of-line)
+ (cond ((looking-at hif-ifx-else-endif-regexp)
+ (forward-word 2)
+ (current-word 'strict))
+ (t
+ nil))))
+ (var (read-minibuffer "Define what? " default))
+ (val (read-from-minibuffer (format "Set %s to? (default 1): "
var)
+ nil nil t nil "1")))
+ (list var val)))
+ (hif-set-var var (or val 1))
+ (message "%s set to %s" var (or val 1))
+ (sleep-for 1)
(if hide-ifdef-hiding (hide-ifdefs)))
(defun hif-undefine-symbol (var)
(setq hide-ifdef-env
(delete (assoc var hide-ifdef-env) hide-ifdef-env)))
-
(defun hide-ifdef-undef (start end)
"Undefine a VAR so that #ifdef VAR would not be included."
(interactive "r")
@@ -1616,20 +1859,23 @@ It does not do the work that's pointless to redo on
a recursive entry."
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
preprocessor if it were given the file with those symbols defined.
+With prefix command presents it will also hide the #ifdefs themselves.
Turn off hiding by calling `show-ifdefs'."
(interactive)
- (message "Hiding...")
+ (let ((hide-ifdef-lines current-prefix-arg))
+ (or nomsg
+ (message "Hiding..."))
(setq hif-outside-read-only buffer-read-only)
- (unless hide-ifdef-mode (hide-ifdef-mode 1)) ; turn on hide-ifdef-mode
+ (unless hide-ifdef-mode (hide-ifdef-mode 1)) ; Turn on hide-ifdef-mode
(if hide-ifdef-hiding
(show-ifdefs)) ; Otherwise, deep confusion.
(setq hide-ifdef-hiding t)
(hide-ifdef-guts)
(setq buffer-read-only (or hide-ifdef-read-only hif-outside-read-only))
(or nomsg
- (message "Hiding done")))
+ (message "Hiding done"))))
(defun show-ifdefs ()
@@ -1661,9 +1907,15 @@ Return as (TOP . BOTTOM) the extent of ifdef block."
(min max-bottom (1- (point)))))))
-(defun hide-ifdef-block ()
- "Hide the ifdef block (true or false part) enclosing or before the
cursor."
- (interactive)
+(defun hide-ifdef-block (&optional arg start end)
+ "Hide the ifdef block (true or false part) enclosing or before the
cursor.
+With optional prefix agument ARG, also hide the #ifdefs themselves."
+ (interactive "P\nr")
+ (let ((hide-ifdef-lines arg))
+ (if mark-active
+ (let ((hif-recurse-level (1+ hif-recurse-level)))
+ (hif-recurse-on start end t)
+ (setq mark-active nil))
(unless hide-ifdef-mode (hide-ifdef-mode 1))
(let ((top-bottom (hif-find-ifdef-block)))
(hide-ifdef-region (car top-bottom) (cdr top-bottom))
@@ -1671,12 +1923,26 @@ Return as (TOP . BOTTOM) the extent of ifdef block."
(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)))
+ (setq buffer-read-only
+ (or hide-ifdef-read-only hif-outside-read-only)))))
-(defun show-ifdef-block ()
+(defun show-ifdef-block (&optional start end)
"Show the ifdef block (true or false part) enclosing or before the
cursor."
- (interactive)
- (let ((top-bottom (hif-find-ifdef-block)))
+ (interactive "r")
+ (if mark-active
+ (progn
+ (dolist (o (overlays-in start end))
+ (if (overlay-get o 'hide-ifdef)
+ (delete-overlay o)))
+ (setq mark-active nil))
+ (let ((top-bottom (condition-case nil
+ (hif-find-ifdef-block)
+ (error
+ nil)))
+ (ovrs (overlays-in (max (point-min) (1- (point)))
+ (min (point-max) (1+ (point)))))
+ (del nil))
+ (if top-bottom
(if hide-ifdef-lines
(hif-show-ifdef-region
(save-excursion
@@ -1684,7 +1950,15 @@ Return as (TOP . BOTTOM) the extent of ifdef block."
(save-excursion
(goto-char (1+ (cdr top-bottom)))
(hif-end-of-line) (point)))
- (hif-show-ifdef-region (1- (car top-bottom)) (cdr top-bottom)))))
+ (setf del (hif-show-ifdef-region
+ (1- (car top-bottom)) (cdr top-bottom)))))
+ (if (not (and top-bottom
+ del))
+ (dolist (o ovrs)
+ ;;(dolist (o (overlays-in (1- (point)) (1+ (point))))
+ ;; (if (overlay-get o 'hide-ifdef) (message "%S" o)))
+ (if (overlay-get o 'hide-ifdef)
+ (delete-overlay o)))))))
;;; definition alist support
[-- Attachment #2: Type: text/html, Size: 38685 bytes --]
^ permalink raw reply related [flat|nested] 20+ messages in thread
* bug#17854: The patch #3 of 3 for hideif.el, a lot of bug fixes and enhancements
2014-07-03 2:37 ` Luke Lee
@ 2014-07-07 0:02 ` Glenn Morris
2014-07-07 1:00 ` Glenn Morris
2014-07-07 9:04 ` Luke Lee
0 siblings, 2 replies; 20+ messages in thread
From: Glenn Morris @ 2014-07-07 0:02 UTC (permalink / raw)
To: Luke Lee; +Cc: 17854
Thanks, I suggest you apply this to trunk now.
Remember the ChangeLog entry, and try to add a NEWS entry that
summarizes the changes that users will care about.
Then you can close this bug report, by sending a mail to 17854-done@debbugs.
Thanks!
^ permalink raw reply [flat|nested] 20+ messages in thread
* bug#17854: The patch #3 of 3 for hideif.el, a lot of bug fixes and enhancements
2014-07-07 0:02 ` Glenn Morris
@ 2014-07-07 1:00 ` Glenn Morris
2014-07-07 9:05 ` Luke Lee
2014-07-07 9:04 ` Luke Lee
1 sibling, 1 reply; 20+ messages in thread
From: Glenn Morris @ 2014-07-07 1:00 UTC (permalink / raw)
To: Luke Lee; +Cc: 17854
PS hideif.el doesn't have a maintainer - would you like to do it?
If so, just change the Maintainer: header in the file.
^ permalink raw reply [flat|nested] 20+ messages in thread
* bug#17854: Completed
2014-06-26 13:51 bug#17854: The patch #3 of 3 for hideif.el, a lot of bug fixes and enhancements Luke Lee
2014-06-26 16:56 ` Glenn Morris
2014-06-27 9:37 ` Luke Lee
@ 2014-07-07 9:04 ` Luke Lee
2 siblings, 0 replies; 20+ messages in thread
From: Luke Lee @ 2014-07-07 9:04 UTC (permalink / raw)
To: 17854-done
[-- Attachment #1: Type: text/plain, Size: 11 bytes --]
Completed.
[-- Attachment #2: Type: text/html, Size: 36 bytes --]
^ permalink raw reply [flat|nested] 20+ messages in thread
* bug#17854: The patch #3 of 3 for hideif.el, a lot of bug fixes and enhancements
2014-07-07 0:02 ` Glenn Morris
2014-07-07 1:00 ` Glenn Morris
@ 2014-07-07 9:04 ` Luke Lee
2014-07-07 16:10 ` Glenn Morris
1 sibling, 1 reply; 20+ messages in thread
From: Luke Lee @ 2014-07-07 9:04 UTC (permalink / raw)
To: 17854
[-- Attachment #1: Type: text/plain, Size: 368 bytes --]
Completed, thanks!
2014-07-07 8:02 GMT+08:00 Glenn Morris <rgm@gnu.org>:
>
> Thanks, I suggest you apply this to trunk now.
> Remember the ChangeLog entry, and try to add a NEWS entry that
> summarizes the changes that users will care about.
> Then you can close this bug report, by sending a mail to
> 17854-done@debbugs.
> Thanks!
>
--
Best regards,
Luke Lee
[-- Attachment #2: Type: text/html, Size: 716 bytes --]
^ permalink raw reply [flat|nested] 20+ messages in thread
* bug#17854: The patch #3 of 3 for hideif.el, a lot of bug fixes and enhancements
2014-07-07 1:00 ` Glenn Morris
@ 2014-07-07 9:05 ` Luke Lee
0 siblings, 0 replies; 20+ messages in thread
From: Luke Lee @ 2014-07-07 9:05 UTC (permalink / raw)
To: 17854
[-- Attachment #1: Type: text/plain, Size: 239 bytes --]
Sure! I'll be glad to.
2014-07-07 9:00 GMT+08:00 Glenn Morris <rgm@gnu.org>:
>
> PS hideif.el doesn't have a maintainer - would you like to do it?
> If so, just change the Maintainer: header in the file.
>
--
Best regards,
Luke Lee
[-- Attachment #2: Type: text/html, Size: 603 bytes --]
^ permalink raw reply [flat|nested] 20+ messages in thread
* bug#17854: The patch #3 of 3 for hideif.el, a lot of bug fixes and enhancements
2014-07-07 9:04 ` Luke Lee
@ 2014-07-07 16:10 ` Glenn Morris
2014-07-08 2:10 ` Luke Lee
0 siblings, 1 reply; 20+ messages in thread
From: Glenn Morris @ 2014-07-07 16:10 UTC (permalink / raw)
To: Luke Lee; +Cc: 17854
Thanks. One tiny comment - your ChangeLog entry should not start with
"lisp/". If you use `C-x 4 a' with your cursor in the right place in
hideif.el, it will start making a ChangeLog entry with the right format.
^ permalink raw reply [flat|nested] 20+ messages in thread
* bug#17854: The patch #3 of 3 for hideif.el, a lot of bug fixes and enhancements
2014-07-07 16:10 ` Glenn Morris
@ 2014-07-08 2:10 ` Luke Lee
0 siblings, 0 replies; 20+ messages in thread
From: Luke Lee @ 2014-07-08 2:10 UTC (permalink / raw)
To: Glenn Morris; +Cc: 17854
[-- Attachment #1: Type: text/plain, Size: 360 bytes --]
That's really convenient! I fixed that, thank you.
2014-07-08 0:10 GMT+08:00 Glenn Morris <rgm@gnu.org>:
>
> Thanks. One tiny comment - your ChangeLog entry should not start with
> "lisp/". If you use `C-x 4 a' with your cursor in the right place in
> hideif.el, it will start making a ChangeLog entry with the right format.
>
--
Best regards,
Luke Lee
[-- Attachment #2: Type: text/html, Size: 732 bytes --]
^ permalink raw reply [flat|nested] 20+ messages in thread
end of thread, other threads:[~2014-07-08 2:10 UTC | newest]
Thread overview: 20+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-06-26 13:51 bug#17854: The patch #3 of 3 for hideif.el, a lot of bug fixes and enhancements Luke Lee
2014-06-26 16:56 ` Glenn Morris
2014-06-26 16:59 ` Glenn Morris
2014-06-27 9:08 ` Luke Lee
2014-06-27 9:26 ` Luke Lee
2014-06-27 9:37 ` Luke Lee
2014-06-28 1:53 ` Glenn Morris
2014-06-28 3:22 ` Glenn Morris
2014-06-30 14:42 ` Luke Lee
2014-06-30 21:47 ` Stefan Monnier
2014-07-01 1:55 ` Luke Lee
2014-07-01 6:44 ` Glenn Morris
2014-07-03 2:37 ` Luke Lee
2014-07-07 0:02 ` Glenn Morris
2014-07-07 1:00 ` Glenn Morris
2014-07-07 9:05 ` Luke Lee
2014-07-07 9:04 ` Luke Lee
2014-07-07 16:10 ` Glenn Morris
2014-07-08 2:10 ` Luke Lee
2014-07-07 9:04 ` bug#17854: Completed Luke Lee
Code repositories for project(s) associated with this public inbox
https://git.savannah.gnu.org/cgit/emacs.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).