;;; hideif.el --- hides selected code within ifdef
-;; Copyright (C) 1988, 1994, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
-;; 2008 Free Software Foundation, Inc.
+;; Copyright (C) 1988, 1994, 2001-2012 Free Software Foundation, Inc.
-;; Author: Daniel LaLiberte <liberte@holonexus.org>
+;; Author: Brian Marick
+;; Daniel LaLiberte <liberte@holonexus.org>
;; Maintainer: FSF
;; Keywords: c, outlines
;; This file is part of GNU Emacs.
-;; GNU Emacs is free software; you can redistribute it and/or modify
+;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
-;; the Free Software Foundation; either version 3, or (at your option)
-;; any later version.
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
-;; along with GNU Emacs; see the file COPYING. If not, write to the
-;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
-;; Boston, MA 02110-1301, USA.
+;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
(easy-menu-define hide-ifdef-mode-menu hide-ifdef-mode-map
"Menu for `hide-ifdef-mode'."
'("Hide-Ifdef"
- ["Hide some ifdefs" hide-ifdefs t]
- ["Show all ifdefs" show-ifdefs t]
- ["Hide ifdef block" hide-ifdef-block t]
- ["Show ifdef block" show-ifdef-block t]
- ["Define a variable" hide-ifdef-define t]
- ["Define an alist" hide-ifdef-set-define-alist t]
- ["Use an alist" hide-ifdef-use-define-alist t]
- ["Undefine a variable" hide-ifdef-undef t]
+ ["Hide some ifdefs" hide-ifdefs
+ :help "Hide the contents of some #ifdefs"]
+ ["Show all ifdefs" show-ifdefs
+ :help "Cancel the effects of `hide-ifdef': show the contents of all #ifdefs"]
+ ["Hide ifdef block" hide-ifdef-block
+ :help "Hide the ifdef block (true or false part) enclosing or before the cursor"]
+ ["Show ifdef block" show-ifdef-block
+ :help "Show the ifdef block (true or false part) enclosing or before the cursor"]
+ ["Define a variable..." hide-ifdef-define
+ :help "Define a VAR so that #ifdef VAR would be included"]
+ ["Undefine a variable..." hide-ifdef-undef
+ :help "Undefine a VAR so that #ifdef VAR would not be included"]
+ ["Define an alist..." hide-ifdef-set-define-alist
+ :help "Set the association for NAME to `hide-ifdef-env'"]
+ ["Use an alist..." hide-ifdef-use-define-alist
+ :help "Set `hide-ifdef-env' to the define list specified by NAME"]
["Toggle read only" hide-ifdef-toggle-read-only
- :style toggle :selected hide-ifdef-read-only]
+ :style toggle :selected hide-ifdef-read-only
+ :help "Buffer should be read-only while hiding text"]
["Toggle shadowing" hide-ifdef-toggle-shadowing
- :style toggle :selected hide-ifdef-shadow]))
+ :style toggle :selected hide-ifdef-shadow
+ :help "Text should be shadowed instead of hidden"]))
(defvar hide-ifdef-hiding nil
"Non-nil when text may be hidden.")
;;;###autoload
(define-minor-mode hide-ifdef-mode
- "Toggle Hide-Ifdef mode. This is a minor mode, albeit a large one.
-With ARG, turn Hide-Ifdef mode on if arg is positive, off otherwise.
-In Hide-Ifdef mode, code within #ifdef constructs that the C preprocessor
-would eliminate may be hidden from view. Several variables affect
-how the hiding is done:
+ "Toggle features to hide/show #ifdef blocks (Hide-Ifdef mode).
+With a prefix argument ARG, enable Hide-Ifdef mode if ARG is
+positive, and disable it otherwise. If called from Lisp, enable
+the mode if ARG is omitted or nil.
+
+Hide-Ifdef mode is a buffer-local minor mode for use with C and
+C-like major modes. When enabled, code within #ifdef constructs
+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
;; else end hide-ifdef-mode
(kill-local-variable 'line-move-ignore-invisible)
(remove-from-invisibility-spec '(hide-ifdef . t))
- (if hide-ifdef-hiding
- (show-ifdefs))))
+ (when hide-ifdef-hiding
+ (show-ifdefs))))
(defun hif-show-all ()
(defvar hif-token)
(defvar hif-token-list)
-;; pattern to match initial identifier, !, &&, ||, (, or ).
-;; Added ==, + and -: garyo@avs.com 8/9/94
+(defconst hif-token-alist
+ '(("||" . or)
+ ("&&" . and)
+ ("|" . hif-logior)
+ ("&" . hif-logand)
+ ("==" . equal)
+ ("!=" . hif-notequal)
+ ("!" . not)
+ ("(" . lparen)
+ (")" . rparen)
+ (">" . hif-greater)
+ ("<" . hif-less)
+ (">=" . hif-greater-equal)
+ ("<=" . hif-less-equal)
+ ("+" . hif-plus)
+ ("-" . hif-minus)
+ ("?" . hif-conditional)
+ (":" . hif-colon)))
+
(defconst hif-token-regexp
- "\\(&&\\|||\\|[!=]=\\|!\\|[()+?:-]\\|[<>]=?\\|\\w+\\)")
+ (concat (regexp-opt (mapcar 'car hif-token-alist)) "\\|\\w+"))
(defun hif-tokenize (start end)
"Separate string between START and END into a list of tokens."
(let ((token (buffer-substring (point) (match-end 0))))
(goto-char (match-end 0))
;; (message "token: %s" token) (sit-for 1)
- (push (cond
- ((string-equal token "||") 'or)
- ((string-equal token "&&") 'and)
- ((string-equal token "==") 'equal)
- ((string-equal token "!=") 'hif-notequal)
- ((string-equal token "!") 'not)
- ((string-equal token "defined") 'hif-defined)
- ((string-equal token "(") 'lparen)
- ((string-equal token ")") 'rparen)
- ((string-equal token ">") 'hif-greater)
- ((string-equal token "<") 'hif-less)
- ((string-equal token ">=") 'hif-greater-equal)
- ((string-equal token "<=") 'hif-less-equal)
- ((string-equal token "+") 'hif-plus)
- ((string-equal token "-") 'hif-minus)
- ((string-equal token "?") 'hif-conditional)
- ((string-equal token ":") 'hif-colon)
- ((string-match "\\`[0-9]*\\'" token)
- (string-to-number token))
- (t (intern token)))
+ (push (or (cdr (assoc token hif-token-alist))
+ (if (string-equal token "defined") 'hif-defined)
+ (if (string-match "\\`[0-9]*\\'" token)
+ (string-to-number token))
+ (intern token))
token-list)))
(t (error "Bad #if expression: %s" (buffer-string)))))))
(nreverse token-list)))
"Pop the next token from token-list into the let variable \"hif-token\"."
(setq hif-token (pop hif-token-list)))
-(defun hif-parse-if-exp (hif-token-list)
+(defun hif-parse-if-exp (token-list)
"Parse the TOKEN-LIST. Return translated list in prefix form."
- (hif-nexttoken)
- (prog1
- (hif-expr)
- (if hif-token ; is there still a token?
- (error "Error: unexpected token: %s" hif-token))))
+ (let ((hif-token-list token-list))
+ (hif-nexttoken)
+ (prog1
+ (hif-expr)
+ (if hif-token ; is there still a token?
+ (error "Error: unexpected token: %s" hif-token)))))
(defun hif-expr ()
"Parse an expression as found in #if.
math : factor | math '+|-' factor."
(let ((result (hif-factor))
(math-op nil))
- (while (memq hif-token '(hif-plus hif-minus))
+ (while (memq hif-token '(hif-plus hif-minus hif-logior hif-logand))
(setq math-op hif-token)
(hif-nexttoken)
(setq result (list math-op result (hif-factor))))
((numberp hif-token)
(prog1 hif-token (hif-nexttoken)))
+ ;; Unary plus/minus.
+ ((memq hif-token '(hif-minus hif-plus))
+ (list (prog1 hif-token (hif-nexttoken)) 0 (hif-factor)))
+
(t ; identifier
(let ((ident hif-token))
(if (memq ident '(or and))
(or (not (zerop (hif-mathify a))) (not (zerop (hif-mathify b)))))
(defun hif-not (a)
(zerop (hif-mathify a)))
-(defun hif-plus (a b)
- "Like ordinary plus but treat t and nil as 1 and 0."
- (+ (hif-mathify a) (hif-mathify b)))
-(defun hif-minus (a b)
- "Like ordinary minus but treat t and nil as 1 and 0."
- (- (hif-mathify a) (hif-mathify b)))
-(defun hif-notequal (a b)
- "Like (not (equal A B)) but as one symbol."
- (not (equal a b)))
-(defun hif-greater (a b)
- "Simple comparison."
- (> (hif-mathify a) (hif-mathify b)))
-(defun hif-less (a b)
- "Simple comparison."
- (< (hif-mathify a) (hif-mathify b)))
-(defun hif-greater-equal (a b)
- "Simple comparison."
- (>= (hif-mathify a) (hif-mathify b)))
-(defun hif-less-equal (a b)
- "Simple comparison."
- (<= (hif-mathify a) (hif-mathify b)))
+
+(defmacro hif-mathify-binop (fun)
+ `(lambda (a b)
+ ,(format "Like `%s' but treat t and nil as 1 and 0." fun)
+ (,fun (hif-mathify a) (hif-mathify b))))
+
+(defalias 'hif-plus (hif-mathify-binop +))
+(defalias 'hif-minus (hif-mathify-binop -))
+(defalias 'hif-notequal (hif-mathify-binop /=))
+(defalias 'hif-greater (hif-mathify-binop >))
+(defalias 'hif-less (hif-mathify-binop <))
+(defalias 'hif-greater-equal (hif-mathify-binop >=))
+(defalias 'hif-less-equal (hif-mathify-binop <=))
+(defalias 'hif-logior (hif-mathify-binop logior))
+(defalias 'hif-logand (hif-mathify-binop logand))
+
;;;----------- end of parser -----------------------
(cond ((hif-looking-at-else)
(setq else (point)))
(t
- (setq end (point)))) ; (save-excursion (end-of-line) (point))
+ (setq end (point)))) ; (line-end-position)
;; If found #else, look for #endif.
(when else
(while (progn
(hif-ifdef-to-endif))
(if (hif-looking-at-else)
(error "Found two elses in a row? Broken!"))
- (setq end (point))) ; (save-excursion (end-of-line) (point))
+ (setq end (point))) ; (line-end-position)
(hif-make-range start end else))))
(defun hif-possibly-hide ()
"Called at #ifX expression, this hides those parts that should be hidden.
-It uses the judgement of `hide-ifdef-evaluator'."
+It uses the judgment of `hide-ifdef-evaluator'."
;; (message "hif-possibly-hide") (sit-for 1)
(let ((test (hif-canonicalize))
(range (hif-find-range)))
(provide 'hideif)
-;; arch-tag: c6381d17-a59a-483a-b945-658f22277981
;;; hideif.el ends here