2001-07-15 16:15:35 +00:00
|
|
|
;;; hideif.el --- hides selected code within ifdef
|
1992-07-16 07:28:05 +00:00
|
|
|
|
2005-04-04 09:25:36 +00:00
|
|
|
;; Copyright (C) 1988, 1994, 2001, 2002, 2005 Free Software Foundation, Inc.
|
1992-07-22 04:22:42 +00:00
|
|
|
|
2000-08-15 12:39:23 +00:00
|
|
|
;; Author: Daniel LaLiberte <liberte@holonexus.org>
|
1994-05-04 23:20:44 +00:00
|
|
|
;; Maintainer: FSF
|
1995-04-14 23:24:11 +00:00
|
|
|
;; Keywords: c, outlines
|
1992-07-16 07:28:05 +00:00
|
|
|
|
1992-07-29 05:22:04 +00:00
|
|
|
;; This file is part of GNU Emacs.
|
|
|
|
|
|
|
|
;; 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 2, 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
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;; GNU General Public License for more details.
|
|
|
|
|
|
|
|
;; You should have received a copy of the GNU General Public License
|
1996-01-14 07:34:30 +00:00
|
|
|
;; along with GNU Emacs; see the file COPYING. If not, write to the
|
|
|
|
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
;; Boston, MA 02111-1307, USA.
|
1992-07-29 05:22:04 +00:00
|
|
|
|
1992-07-16 07:28:05 +00:00
|
|
|
;;; Commentary:
|
|
|
|
|
1996-01-14 07:34:30 +00:00
|
|
|
;; To initialize, toggle the hide-ifdef minor mode with
|
|
|
|
;;
|
|
|
|
;; M-x hide-ifdef-mode
|
|
|
|
;;
|
|
|
|
;; This will set up key bindings and call hide-ifdef-mode-hook if it
|
|
|
|
;; has a value. To explicitly hide ifdefs using a buffer-local
|
|
|
|
;; define list (default empty), type
|
|
|
|
;;
|
|
|
|
;; M-x hide-ifdefs or C-c @ h
|
|
|
|
;;
|
|
|
|
;; Hide-ifdef suppresses the display of code that the preprocessor wouldn't
|
2003-02-04 13:24:35 +00:00
|
|
|
;; pass through. The support of constant expressions in #if lines is
|
1996-01-14 07:34:30 +00:00
|
|
|
;; limited to identifiers, parens, and the operators: &&, ||, !, and
|
|
|
|
;; "defined". Please extend this.
|
|
|
|
;;
|
|
|
|
;; The hidden code is marked by ellipses (...). Be
|
|
|
|
;; cautious when editing near ellipses, since the hidden text is
|
|
|
|
;; still in the buffer, and you can move the point into it and modify
|
2001-11-28 04:12:47 +00:00
|
|
|
;; text unawares.
|
1996-01-14 07:34:30 +00:00
|
|
|
;; You can make your buffer read-only while hide-ifdef-hiding by setting
|
2003-02-04 13:24:35 +00:00
|
|
|
;; hide-ifdef-read-only to a non-nil value. You can toggle this
|
1996-01-14 07:34:30 +00:00
|
|
|
;; variable with hide-ifdef-toggle-read-only (C-c @ C-q).
|
|
|
|
;;
|
|
|
|
;; You can undo the effect of hide-ifdefs by typing
|
|
|
|
;;
|
|
|
|
;; M-x show-ifdefs or C-c @ s
|
|
|
|
;;
|
|
|
|
;; Use M-x hide-ifdef-define (C-c @ d) to define a symbol.
|
|
|
|
;; Use M-x hide-ifdef-undef (C-c @ u) to undefine a symbol.
|
|
|
|
;;
|
|
|
|
;; If you define or undefine a symbol while hide-ifdef-mode is in effect,
|
|
|
|
;; the display will be updated. Only the define list for the current
|
|
|
|
;; buffer will be affected. You can save changes to the local define
|
2003-02-04 13:24:35 +00:00
|
|
|
;; list with hide-ifdef-set-define-alist. This adds entries
|
1996-01-14 07:34:30 +00:00
|
|
|
;; to hide-ifdef-define-alist.
|
|
|
|
;;
|
|
|
|
;; If you have defined a hide-ifdef-mode-hook, you can set
|
|
|
|
;; up a list of symbols that may be used by hide-ifdefs as in the
|
|
|
|
;; following example:
|
|
|
|
;;
|
2001-11-28 04:12:47 +00:00
|
|
|
;; (add-hook 'hide-ifdef-mode-hook
|
2000-06-12 05:22:26 +00:00
|
|
|
;; (lambda ()
|
2001-11-28 04:12:47 +00:00
|
|
|
;; (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
|
1996-01-14 07:34:30 +00:00
|
|
|
;;
|
2000-07-19 15:51:05 +00:00
|
|
|
;; You can call hide-ifdef-use-define-alist (C-c @ U) at any time to specify
|
1996-01-14 07:34:30 +00:00
|
|
|
;; another list to use.
|
|
|
|
;;
|
|
|
|
;; To cause ifdefs to be hidden as soon as hide-ifdef-mode is called,
|
|
|
|
;; set hide-ifdef-initially to non-nil.
|
|
|
|
;;
|
|
|
|
;; If you set hide-ifdef-lines to t, hide-ifdefs hides all the #ifdef lines.
|
|
|
|
;; In the absence of highlighting, that might be a bad idea. If you set
|
|
|
|
;; hide-ifdef-lines to nil (the default), the surrounding preprocessor
|
|
|
|
;; lines will be displayed. That can be confusing in its own
|
|
|
|
;; right. Other variations on display are possible, but not much
|
|
|
|
;; better.
|
|
|
|
;;
|
|
|
|
;; You can explicitly hide or show individual ifdef blocks irrespective
|
|
|
|
;; of the define list by using hide-ifdef-block and show-ifdef-block.
|
|
|
|
;;
|
|
|
|
;; You can move the point between ifdefs with forward-ifdef, backward-ifdef,
|
|
|
|
;; up-ifdef, down-ifdef, next-ifdef, and previous-ifdef.
|
|
|
|
;;
|
|
|
|
;; If you have minor-mode-alist in your mode line (the default) two labels
|
|
|
|
;; may appear. "Ifdef" will appear when hide-ifdef-mode is active. "Hiding"
|
|
|
|
;; will appear when text may be hidden ("hide-ifdef-hiding" is non-nil).
|
|
|
|
;;
|
|
|
|
;; Written by Brian Marick, at Gould, Computer Systems Division, Urbana IL.
|
|
|
|
;; Extensively modified by Daniel LaLiberte (while at Gould).
|
|
|
|
;;
|
|
|
|
;; You may freely modify and distribute this, but keep a record
|
|
|
|
;; of modifications and send comments to:
|
|
|
|
;; liberte@a.cs.uiuc.edu or ihnp4!uiucdcs!liberte
|
|
|
|
;; I will continue to upgrade hide-ifdef-mode
|
|
|
|
;; with your contributions.
|
1989-08-05 03:56:31 +00:00
|
|
|
|
1992-07-16 07:28:05 +00:00
|
|
|
;;; Code:
|
1989-08-05 03:56:31 +00:00
|
|
|
|
1995-10-26 03:13:43 +00:00
|
|
|
(require 'cc-mode)
|
|
|
|
|
1998-01-26 11:29:46 +00:00
|
|
|
(defgroup hide-ifdef nil
|
|
|
|
"Hide selected code within `ifdef'."
|
|
|
|
:group 'c)
|
|
|
|
|
2001-11-28 04:12:47 +00:00
|
|
|
(defvar hide-ifdef-mode-submap
|
|
|
|
;; Set up the submap that goes after the prefix key.
|
|
|
|
(let ((map (make-sparse-keymap)))
|
|
|
|
(define-key map "d" 'hide-ifdef-define)
|
|
|
|
(define-key map "u" 'hide-ifdef-undef)
|
|
|
|
(define-key map "D" 'hide-ifdef-set-define-alist)
|
|
|
|
(define-key map "U" 'hide-ifdef-use-define-alist)
|
|
|
|
|
|
|
|
(define-key map "h" 'hide-ifdefs)
|
|
|
|
(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 "\C-q" 'hide-ifdef-toggle-read-only)
|
|
|
|
(substitute-key-definition
|
|
|
|
'toggle-read-only 'hide-ifdef-toggle-outside-read-only map)
|
|
|
|
map)
|
|
|
|
"Keymap used by `hide-ifdef-mode' under `hide-ifdef-mode-prefix-key'.")
|
1989-08-05 03:56:31 +00:00
|
|
|
|
1995-05-02 03:57:27 +00:00
|
|
|
(defconst hide-ifdef-mode-prefix-key "\C-c@"
|
1991-03-06 21:14:11 +00:00
|
|
|
"Prefix key for all Hide-Ifdef mode commands.")
|
1989-08-05 03:56:31 +00:00
|
|
|
|
2001-11-28 04:12:47 +00:00
|
|
|
(defvar hide-ifdef-mode-map
|
|
|
|
;; Set up the mode's main map, which leads via the prefix key to the submap.
|
|
|
|
(let ((map (make-sparse-keymap)))
|
|
|
|
(define-key map hide-ifdef-mode-prefix-key hide-ifdef-mode-submap)
|
|
|
|
map)
|
|
|
|
"Keymap used with `hide-ifdef-mode'.")
|
1989-08-05 03:56:31 +00:00
|
|
|
|
|
|
|
(defvar hide-ifdef-hiding nil
|
1994-05-04 23:20:44 +00:00
|
|
|
"Non-nil when text may be hidden.")
|
|
|
|
|
1989-08-05 03:56:31 +00:00
|
|
|
(or (assq 'hide-ifdef-hiding minor-mode-alist)
|
|
|
|
(setq minor-mode-alist
|
|
|
|
(cons '(hide-ifdef-hiding " Hiding")
|
|
|
|
minor-mode-alist)))
|
|
|
|
|
1994-05-07 01:42:48 +00:00
|
|
|
;; fix c-mode syntax table so we can recognize whole symbols.
|
|
|
|
(defvar hide-ifdef-syntax-table
|
2001-11-28 04:12:47 +00:00
|
|
|
(let ((st (copy-syntax-table c-mode-syntax-table)))
|
|
|
|
(modify-syntax-entry ?_ "w" st)
|
|
|
|
(modify-syntax-entry ?& "." st)
|
|
|
|
(modify-syntax-entry ?\| "." st)
|
|
|
|
st)
|
1994-05-07 01:42:48 +00:00
|
|
|
"Syntax table used for tokenizing #if expressions.")
|
|
|
|
|
1998-10-13 09:52:54 +00:00
|
|
|
(defvar hide-ifdef-env nil
|
|
|
|
"An alist of defined symbols and their values.")
|
|
|
|
|
|
|
|
(defvar hif-outside-read-only nil
|
|
|
|
"Internal variable. Saves the value of `buffer-read-only' while hiding.")
|
|
|
|
|
1992-08-19 03:54:46 +00:00
|
|
|
;;;###autoload
|
2001-11-28 04:12:47 +00:00
|
|
|
(define-minor-mode hide-ifdef-mode
|
1991-03-06 21:14:11 +00:00
|
|
|
"Toggle Hide-Ifdef mode. This is a minor mode, albeit a large one.
|
1996-05-05 17:27:24 +00:00
|
|
|
With ARG, turn Hide-Ifdef mode on if arg is positive, off otherwise.
|
1991-03-06 21:14:11 +00:00
|
|
|
In Hide-Ifdef mode, code within #ifdef constructs that the C preprocessor
|
1989-08-05 03:56:31 +00:00
|
|
|
would eliminate may be hidden from view. Several variables affect
|
|
|
|
how the hiding is done:
|
|
|
|
|
2001-11-28 04:12:47 +00:00
|
|
|
`hide-ifdef-env'
|
1989-08-05 03:56:31 +00:00
|
|
|
An association list of defined and undefined symbols for the
|
1991-03-06 21:14:11 +00:00
|
|
|
current buffer. Initially, the global value of `hide-ifdef-env'
|
|
|
|
is used.
|
1989-08-05 03:56:31 +00:00
|
|
|
|
2001-11-28 04:12:47 +00:00
|
|
|
`hide-ifdef-define-alist'
|
2003-02-04 13:24:35 +00:00
|
|
|
An association list of defined symbol lists.
|
1991-03-06 21:14:11 +00:00
|
|
|
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'.
|
1989-08-05 03:56:31 +00:00
|
|
|
|
2001-11-28 04:12:47 +00:00
|
|
|
`hide-ifdef-lines'
|
1989-08-05 03:56:31 +00:00
|
|
|
Set to non-nil to not show #if, #ifdef, #ifndef, #else, and
|
|
|
|
#endif lines when hiding.
|
|
|
|
|
2001-11-28 04:12:47 +00:00
|
|
|
`hide-ifdef-initially'
|
1991-03-06 21:14:11 +00:00
|
|
|
Indicates whether `hide-ifdefs' should be called when Hide-Ifdef mode
|
1989-08-05 03:56:31 +00:00
|
|
|
is activated.
|
|
|
|
|
2001-11-28 04:12:47 +00:00
|
|
|
`hide-ifdef-read-only'
|
1989-08-05 03:56:31 +00:00
|
|
|
Set to non-nil if you want to make buffers read only while hiding.
|
1991-03-06 21:14:11 +00:00
|
|
|
After `show-ifdefs', read-only status is restored to previous value.
|
1989-08-05 03:56:31 +00:00
|
|
|
|
|
|
|
\\{hide-ifdef-mode-map}"
|
2005-04-04 09:25:36 +00:00
|
|
|
:group 'hide-ifdef :lighter " Ifdef"
|
1989-08-05 03:56:31 +00:00
|
|
|
(if hide-ifdef-mode
|
|
|
|
(progn
|
2001-11-28 04:12:47 +00:00
|
|
|
;; 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)
|
2001-11-28 20:53:28 +00:00
|
|
|
(set (make-local-variable 'line-move-ignore-invisible) t)
|
2001-11-30 23:58:24 +00:00
|
|
|
(add-hook 'change-major-mode-hook
|
|
|
|
(lambda () (hide-ifdef-mode -1)) nil t)
|
1989-08-05 03:56:31 +00:00
|
|
|
|
2001-11-28 04:12:47 +00:00
|
|
|
(add-to-invisibility-spec '(hide-ifdef . t))
|
1989-08-05 03:56:31 +00:00
|
|
|
|
|
|
|
(if hide-ifdef-initially
|
|
|
|
(hide-ifdefs)
|
2001-11-28 04:12:47 +00:00
|
|
|
(show-ifdefs)))
|
|
|
|
;; else end hide-ifdef-mode
|
2001-11-28 20:53:28 +00:00
|
|
|
(kill-local-variable 'line-move-ignore-invisible)
|
2001-11-30 23:58:24 +00:00
|
|
|
(remove-from-invisibility-spec '(hide-ifdef . t))
|
1989-08-05 03:56:31 +00:00
|
|
|
(if hide-ifdef-hiding
|
2001-11-28 04:12:47 +00:00
|
|
|
(show-ifdefs))))
|
2003-02-04 13:24:35 +00:00
|
|
|
|
1989-08-05 03:56:31 +00:00
|
|
|
|
|
|
|
(defun hif-show-all ()
|
|
|
|
"Show all of the text in the current buffer."
|
|
|
|
(interactive)
|
2001-11-28 04:12:47 +00:00
|
|
|
(hif-show-ifdef-region (point-min) (point-max)))
|
1989-08-05 03:56:31 +00:00
|
|
|
|
1995-12-25 20:44:18 +00:00
|
|
|
;; By putting this on after-revert-hook, we arrange that it only
|
|
|
|
;; does anything when revert-buffer avoids turning off the mode.
|
|
|
|
;; (That can happen in VC.)
|
2001-11-28 04:12:47 +00:00
|
|
|
(defun hif-after-revert-function ()
|
1995-12-25 20:44:18 +00:00
|
|
|
(and hide-ifdef-mode hide-ifdef-hiding
|
|
|
|
(hide-ifdefs t)))
|
2001-11-28 04:12:47 +00:00
|
|
|
(add-hook 'after-revert-hook 'hif-after-revert-function)
|
1995-12-25 20:44:18 +00:00
|
|
|
|
2001-11-28 20:53:28 +00:00
|
|
|
(defun hif-end-of-line ()
|
|
|
|
(end-of-line)
|
|
|
|
(while (= (logand 1 (skip-chars-backward "\\\\")) 1)
|
|
|
|
(end-of-line 2)))
|
|
|
|
|
|
|
|
(defun hide-ifdef-region-internal (start end)
|
|
|
|
(remove-overlays start end 'invisible 'hide-ifdef)
|
|
|
|
(let ((o (make-overlay start end)))
|
|
|
|
(overlay-put o 'invisible 'hide-ifdef)))
|
|
|
|
|
1989-08-05 03:56:31 +00:00
|
|
|
(defun hide-ifdef-region (start end)
|
|
|
|
"START is the start of a #if or #else form. END is the ending part.
|
|
|
|
Everything including these lines is made invisible."
|
2001-11-28 04:12:47 +00:00
|
|
|
(save-excursion
|
2001-11-28 20:53:28 +00:00
|
|
|
(goto-char start) (hif-end-of-line) (setq start (point))
|
|
|
|
(goto-char end) (hif-end-of-line) (setq end (point))
|
|
|
|
(hide-ifdef-region-internal start end)))
|
1989-08-05 03:56:31 +00:00
|
|
|
|
|
|
|
(defun hif-show-ifdef-region (start end)
|
|
|
|
"Everything between START and END is made visible."
|
2001-11-28 04:12:47 +00:00
|
|
|
(remove-overlays start end 'invisible 'hide-ifdef))
|
1989-08-05 03:56:31 +00:00
|
|
|
|
|
|
|
|
2001-11-28 04:12:47 +00:00
|
|
|
;;===%%SF%% evaluation (Start) ===
|
1989-08-05 03:56:31 +00:00
|
|
|
|
1995-12-25 20:44:18 +00:00
|
|
|
;; It is not useful to set this to anything but `eval'.
|
|
|
|
;; In fact, the variable might as well be eliminated.
|
1989-08-05 03:56:31 +00:00
|
|
|
(defvar hide-ifdef-evaluator 'eval
|
1995-12-25 20:44:18 +00:00
|
|
|
"The function to use to evaluate a form.
|
|
|
|
The evaluator is given a canonical form and returns t if text under
|
1989-08-05 03:56:31 +00:00
|
|
|
that form should be displayed.")
|
|
|
|
|
|
|
|
(defvar hif-undefined-symbol nil
|
|
|
|
"...is by default considered to be false.")
|
|
|
|
|
|
|
|
|
|
|
|
(defun hif-set-var (var value)
|
|
|
|
"Prepend (var value) pair to hide-ifdef-env."
|
|
|
|
(setq hide-ifdef-env (cons (cons var value) hide-ifdef-env)))
|
|
|
|
|
|
|
|
|
|
|
|
(defun hif-lookup (var)
|
2001-11-28 04:12:47 +00:00
|
|
|
;; (message "hif-lookup %s" var)
|
1989-08-05 03:56:31 +00:00
|
|
|
(let ((val (assoc var hide-ifdef-env)))
|
|
|
|
(if val
|
|
|
|
(cdr val)
|
|
|
|
hif-undefined-symbol)))
|
|
|
|
|
|
|
|
(defun hif-defined (var)
|
2001-11-28 20:53:28 +00:00
|
|
|
(if (assoc var hide-ifdef-env) 1 0))
|
1989-08-05 03:56:31 +00:00
|
|
|
|
2001-11-28 04:12:47 +00:00
|
|
|
;;===%%SF%% evaluation (End) ===
|
1989-08-05 03:56:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2001-11-28 04:12:47 +00:00
|
|
|
;;===%%SF%% parsing (Start) ===
|
1989-08-05 03:56:31 +00:00
|
|
|
;;; The code that understands what ifs and ifdef in files look like.
|
|
|
|
|
|
|
|
(defconst hif-cpp-prefix "\\(^\\|\r\\)[ \t]*#[ \t]*")
|
|
|
|
(defconst hif-ifndef-regexp (concat hif-cpp-prefix "ifndef"))
|
|
|
|
(defconst hif-ifx-regexp (concat hif-cpp-prefix "if\\(n?def\\)?[ \t]+"))
|
|
|
|
(defconst hif-else-regexp (concat hif-cpp-prefix "else"))
|
|
|
|
(defconst hif-endif-regexp (concat hif-cpp-prefix "endif"))
|
|
|
|
(defconst hif-ifx-else-endif-regexp
|
|
|
|
(concat hif-ifx-regexp "\\|" hif-else-regexp "\\|" hif-endif-regexp))
|
|
|
|
|
2001-11-28 04:12:47 +00:00
|
|
|
;; Used to store the current token and the whole token list during parsing.
|
|
|
|
;; Only bound dynamically.
|
1998-10-13 09:52:54 +00:00
|
|
|
(defvar hif-token)
|
|
|
|
(defvar hif-token-list)
|
1989-08-05 03:56:31 +00:00
|
|
|
|
2001-11-28 04:12:47 +00:00
|
|
|
;; pattern to match initial identifier, !, &&, ||, (, or ).
|
|
|
|
;; Added ==, + and -: garyo@avs.com 8/9/94
|
2001-11-28 20:53:28 +00:00
|
|
|
(defconst hif-token-regexp
|
2002-03-06 15:12:01 +00:00
|
|
|
"\\(&&\\|||\\|[!=]=\\|!\\|[()+?:-]\\|[<>]=?\\|\\w+\\)")
|
1989-08-05 03:56:31 +00:00
|
|
|
|
2001-11-28 20:53:28 +00:00
|
|
|
(defun hif-tokenize (start end)
|
|
|
|
"Separate string between START and END into a list of tokens."
|
|
|
|
(let ((token-list nil))
|
2001-11-28 04:12:47 +00:00
|
|
|
(with-syntax-table hide-ifdef-syntax-table
|
2001-11-28 20:53:28 +00:00
|
|
|
(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))
|
|
|
|
|
|
|
|
((looking-at hif-token-regexp)
|
|
|
|
(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)
|
2002-03-06 15:12:01 +00:00
|
|
|
((string-equal token "?") 'hif-conditional)
|
|
|
|
((string-equal token ":") 'hif-colon)
|
2001-11-28 20:53:28 +00:00
|
|
|
((string-match "\\`[0-9]*\\'" token)
|
|
|
|
(string-to-number token))
|
|
|
|
(t (intern token)))
|
|
|
|
token-list)))
|
|
|
|
(t (error "Bad #if expression: %s" (buffer-string)))))))
|
1994-05-07 01:42:48 +00:00
|
|
|
(nreverse token-list)))
|
1989-08-05 03:56:31 +00:00
|
|
|
|
|
|
|
;;;-----------------------------------------------------------------
|
|
|
|
;;; Translate C preprocessor #if expressions using recursive descent.
|
|
|
|
;;; This parser is limited to the operators &&, ||, !, and "defined".
|
Handle ==, !=, + and -.
(hif-tokenize): Handle ==, !=, + and -.
(hif-token-regexp): Handle ==, !=, + and -.
(hif-notequal, hif-plus, hif-minus, hif-mathify): New functions.
(hif-math, hif-eq-expr): New functions.
(hif-term): Call hif-eq-expr.
(hide-ifdef-define): Pass 1 to hif-set-var.
1994-09-19 05:29:15 +00:00
|
|
|
;;; Added ==, !=, +, and -. Gary Oberbrunner, garyo@avs.com, 8/9/94
|
1989-08-05 03:56:31 +00:00
|
|
|
|
2001-12-01 18:20:52 +00:00
|
|
|
(defsubst hif-nexttoken ()
|
|
|
|
"Pop the next token from token-list into the let variable \"hif-token\"."
|
|
|
|
(setq hif-token (pop hif-token-list)))
|
|
|
|
|
1998-10-13 09:52:54 +00:00
|
|
|
(defun hif-parse-if-exp (hif-token-list)
|
1989-08-05 03:56:31 +00:00
|
|
|
"Parse the TOKEN-LIST. Return translated list in prefix form."
|
|
|
|
(hif-nexttoken)
|
|
|
|
(prog1
|
|
|
|
(hif-expr)
|
1998-10-13 09:52:54 +00:00
|
|
|
(if hif-token ; is there still a token?
|
|
|
|
(error "Error: unexpected token: %s" hif-token))))
|
1989-08-05 03:56:31 +00:00
|
|
|
|
|
|
|
(defun hif-expr ()
|
1995-12-25 20:44:18 +00:00
|
|
|
"Parse an expression as found in #if.
|
2002-03-06 15:12:01 +00:00
|
|
|
expr : or-expr | or-expr '?' expr ':' expr."
|
|
|
|
(let ((result (hif-or-expr))
|
|
|
|
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)))
|
|
|
|
result))
|
|
|
|
|
|
|
|
(defun hif-or-expr ()
|
|
|
|
"Parse n or-expr : and-expr | or-expr '||' and-expr."
|
|
|
|
(let ((result (hif-and-expr)))
|
1998-10-13 09:52:54 +00:00
|
|
|
(while (eq hif-token 'or)
|
1989-08-05 03:56:31 +00:00
|
|
|
(hif-nexttoken)
|
2002-03-06 15:12:01 +00:00
|
|
|
(setq result (list 'hif-or result (hif-and-expr))))
|
1991-03-06 21:14:11 +00:00
|
|
|
result))
|
1989-08-05 03:56:31 +00:00
|
|
|
|
2002-03-06 15:12:01 +00:00
|
|
|
(defun hif-and-expr ()
|
|
|
|
"Parse an and-expr : eq-expr | and-expr '&&' eq-expr."
|
Handle ==, !=, + and -.
(hif-tokenize): Handle ==, !=, + and -.
(hif-token-regexp): Handle ==, !=, + and -.
(hif-notequal, hif-plus, hif-minus, hif-mathify): New functions.
(hif-math, hif-eq-expr): New functions.
(hif-term): Call hif-eq-expr.
(hide-ifdef-define): Pass 1 to hif-set-var.
1994-09-19 05:29:15 +00:00
|
|
|
(let ((result (hif-eq-expr)))
|
1998-10-13 09:52:54 +00:00
|
|
|
(while (eq hif-token 'and)
|
1989-08-05 03:56:31 +00:00
|
|
|
(hif-nexttoken)
|
2001-11-28 20:53:28 +00:00
|
|
|
(setq result (list 'hif-and result (hif-eq-expr))))
|
1991-03-06 21:14:11 +00:00
|
|
|
result))
|
1989-08-05 03:56:31 +00:00
|
|
|
|
Handle ==, !=, + and -.
(hif-tokenize): Handle ==, !=, + and -.
(hif-token-regexp): Handle ==, !=, + and -.
(hif-notequal, hif-plus, hif-minus, hif-mathify): New functions.
(hif-math, hif-eq-expr): New functions.
(hif-term): Call hif-eq-expr.
(hide-ifdef-define): Pass 1 to hif-set-var.
1994-09-19 05:29:15 +00:00
|
|
|
(defun hif-eq-expr ()
|
1997-08-25 17:06:42 +00:00
|
|
|
"Parse an eq-expr : math | eq-expr `=='|`!='|`<'|`>'|`>='|`<=' math."
|
Handle ==, !=, + and -.
(hif-tokenize): Handle ==, !=, + and -.
(hif-token-regexp): Handle ==, !=, + and -.
(hif-notequal, hif-plus, hif-minus, hif-mathify): New functions.
(hif-math, hif-eq-expr): New functions.
(hif-term): Call hif-eq-expr.
(hide-ifdef-define): Pass 1 to hif-set-var.
1994-09-19 05:29:15 +00:00
|
|
|
(let ((result (hif-math))
|
|
|
|
(eq-token nil))
|
1998-10-13 09:52:54 +00:00
|
|
|
(while (memq hif-token '(equal hif-notequal hif-greater hif-less
|
|
|
|
hif-greater-equal hif-less-equal))
|
|
|
|
(setq eq-token hif-token)
|
Handle ==, !=, + and -.
(hif-tokenize): Handle ==, !=, + and -.
(hif-token-regexp): Handle ==, !=, + and -.
(hif-notequal, hif-plus, hif-minus, hif-mathify): New functions.
(hif-math, hif-eq-expr): New functions.
(hif-term): Call hif-eq-expr.
(hide-ifdef-define): Pass 1 to hif-set-var.
1994-09-19 05:29:15 +00:00
|
|
|
(hif-nexttoken)
|
|
|
|
(setq result (list eq-token result (hif-math))))
|
|
|
|
result))
|
|
|
|
|
|
|
|
(defun hif-math ()
|
1995-12-25 20:44:18 +00:00
|
|
|
"Parse an expression with + or - and simpler things.
|
Handle ==, !=, + and -.
(hif-tokenize): Handle ==, !=, + and -.
(hif-token-regexp): Handle ==, !=, + and -.
(hif-notequal, hif-plus, hif-minus, hif-mathify): New functions.
(hif-math, hif-eq-expr): New functions.
(hif-term): Call hif-eq-expr.
(hide-ifdef-define): Pass 1 to hif-set-var.
1994-09-19 05:29:15 +00:00
|
|
|
math : factor | math '+|-' factor."
|
|
|
|
(let ((result (hif-factor))
|
|
|
|
(math-op nil))
|
2001-11-28 20:53:28 +00:00
|
|
|
(while (memq hif-token '(hif-plus hif-minus))
|
1998-10-13 09:52:54 +00:00
|
|
|
(setq math-op hif-token)
|
Handle ==, !=, + and -.
(hif-tokenize): Handle ==, !=, + and -.
(hif-token-regexp): Handle ==, !=, + and -.
(hif-notequal, hif-plus, hif-minus, hif-mathify): New functions.
(hif-math, hif-eq-expr): New functions.
(hif-term): Call hif-eq-expr.
(hide-ifdef-define): Pass 1 to hif-set-var.
1994-09-19 05:29:15 +00:00
|
|
|
(hif-nexttoken)
|
|
|
|
(setq result (list math-op result (hif-factor))))
|
|
|
|
result))
|
2003-02-04 13:24:35 +00:00
|
|
|
|
1989-08-05 03:56:31 +00:00
|
|
|
(defun hif-factor ()
|
1995-12-25 20:44:18 +00:00
|
|
|
"Parse a factor: '!' factor | '(' expr ')' | 'defined(' id ')' | id."
|
1989-08-05 03:56:31 +00:00
|
|
|
(cond
|
1999-11-21 14:50:21 +00:00
|
|
|
((eq hif-token 'not)
|
|
|
|
(hif-nexttoken)
|
2001-11-28 20:53:28 +00:00
|
|
|
(list 'hif-not (hif-factor)))
|
1999-11-21 14:50:21 +00:00
|
|
|
|
|
|
|
((eq hif-token 'lparen)
|
|
|
|
(hif-nexttoken)
|
|
|
|
(let ((result (hif-expr)))
|
|
|
|
(if (not (eq hif-token 'rparen))
|
|
|
|
(error "Bad token in parenthesized expression: %s" hif-token)
|
1989-08-05 03:56:31 +00:00
|
|
|
(hif-nexttoken)
|
1999-11-21 14:50:21 +00:00
|
|
|
result)))
|
|
|
|
|
|
|
|
((eq hif-token 'hif-defined)
|
|
|
|
(hif-nexttoken)
|
2001-11-28 20:53:28 +00:00
|
|
|
(let ((paren (when (eq hif-token 'lparen) (hif-nexttoken) t))
|
|
|
|
(ident hif-token))
|
1999-11-21 14:50:21 +00:00
|
|
|
(if (memq hif-token '(or and not hif-defined lparen rparen))
|
|
|
|
(error "Error: unexpected token: %s" hif-token))
|
2001-11-28 20:53:28 +00:00
|
|
|
(when paren
|
|
|
|
(hif-nexttoken)
|
|
|
|
(unless (eq hif-token 'rparen)
|
|
|
|
(error "Error: expected \")\" after identifier")))
|
1999-11-21 14:50:21 +00:00
|
|
|
(hif-nexttoken)
|
2001-11-28 04:12:47 +00:00
|
|
|
`(hif-defined (quote ,ident))))
|
1999-11-21 14:50:21 +00:00
|
|
|
|
2001-11-28 20:53:28 +00:00
|
|
|
((numberp hif-token)
|
|
|
|
(prog1 hif-token (hif-nexttoken)))
|
|
|
|
|
1999-11-21 14:50:21 +00:00
|
|
|
(t ; identifier
|
|
|
|
(let ((ident hif-token))
|
|
|
|
(if (memq ident '(or and))
|
|
|
|
(error "Error: missing identifier"))
|
|
|
|
(hif-nexttoken)
|
2001-11-28 04:12:47 +00:00
|
|
|
`(hif-lookup (quote ,ident))))))
|
1989-08-05 03:56:31 +00:00
|
|
|
|
Handle ==, !=, + and -.
(hif-tokenize): Handle ==, !=, + and -.
(hif-token-regexp): Handle ==, !=, + and -.
(hif-notequal, hif-plus, hif-minus, hif-mathify): New functions.
(hif-math, hif-eq-expr): New functions.
(hif-term): Call hif-eq-expr.
(hide-ifdef-define): Pass 1 to hif-set-var.
1994-09-19 05:29:15 +00:00
|
|
|
(defun hif-mathify (val)
|
|
|
|
"Treat VAL as a number: if it's t or nil, use 1 or 0."
|
2001-11-28 04:12:47 +00:00
|
|
|
(cond ((eq val t) 1)
|
|
|
|
((null val) 0)
|
Handle ==, !=, + and -.
(hif-tokenize): Handle ==, !=, + and -.
(hif-token-regexp): Handle ==, !=, + and -.
(hif-notequal, hif-plus, hif-minus, hif-mathify): New functions.
(hif-math, hif-eq-expr): New functions.
(hif-term): Call hif-eq-expr.
(hide-ifdef-define): Pass 1 to hif-set-var.
1994-09-19 05:29:15 +00:00
|
|
|
(t val)))
|
|
|
|
|
2002-03-06 15:12:01 +00:00
|
|
|
(defun hif-conditional (a b c)
|
|
|
|
(if (not (zerop (hif-mathify a))) (hif-mathify b) (hif-mathify c)))
|
2001-11-28 20:53:28 +00:00
|
|
|
(defun hif-and (a b)
|
|
|
|
(and (not (zerop (hif-mathify a))) (not (zerop (hif-mathify b)))))
|
|
|
|
(defun hif-or (a b)
|
|
|
|
(or (not (zerop (hif-mathify a))) (not (zerop (hif-mathify b)))))
|
|
|
|
(defun hif-not (a)
|
|
|
|
(zerop (hif-mathify a)))
|
Handle ==, !=, + and -.
(hif-tokenize): Handle ==, !=, + and -.
(hif-token-regexp): Handle ==, !=, + and -.
(hif-notequal, hif-plus, hif-minus, hif-mathify): New functions.
(hif-math, hif-eq-expr): New functions.
(hif-term): Call hif-eq-expr.
(hide-ifdef-define): Pass 1 to hif-set-var.
1994-09-19 05:29:15 +00:00
|
|
|
(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)))
|
1997-08-25 17:06:42 +00:00
|
|
|
(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)))
|
1989-08-05 03:56:31 +00:00
|
|
|
;;;----------- end of parser -----------------------
|
|
|
|
|
|
|
|
|
|
|
|
(defun hif-canonicalize ()
|
2001-11-28 04:12:47 +00:00
|
|
|
"When at beginning of #ifX, return a Lisp expression for its condition."
|
1989-08-05 03:56:31 +00:00
|
|
|
(save-excursion
|
|
|
|
(let ((negate (looking-at hif-ifndef-regexp)))
|
|
|
|
(re-search-forward hif-ifx-regexp)
|
2001-11-28 20:53:28 +00:00
|
|
|
(let* ((tokens (hif-tokenize (point)
|
|
|
|
(progn (hif-end-of-line) (point))))
|
|
|
|
(expr (hif-parse-if-exp tokens)))
|
2001-11-28 04:12:47 +00:00
|
|
|
;; (message "hif-canonicalized: %s" expr)
|
1989-08-05 03:56:31 +00:00
|
|
|
(if negate
|
2001-11-28 20:53:28 +00:00
|
|
|
(list 'hif-not expr)
|
1989-08-05 03:56:31 +00:00
|
|
|
expr)))))
|
|
|
|
|
|
|
|
|
|
|
|
(defun hif-find-any-ifX ()
|
1995-12-25 20:44:18 +00:00
|
|
|
"Move to next #if..., or #ifndef, at point or after."
|
2001-11-28 04:12:47 +00:00
|
|
|
;; (message "find ifX at %d" (point))
|
1989-08-05 03:56:31 +00:00
|
|
|
(prog1
|
|
|
|
(re-search-forward hif-ifx-regexp (point-max) t)
|
|
|
|
(beginning-of-line)))
|
|
|
|
|
|
|
|
|
|
|
|
(defun hif-find-next-relevant ()
|
1995-12-25 20:44:18 +00:00
|
|
|
"Move to next #if..., #else, or #endif, after the current line."
|
2001-11-28 04:12:47 +00:00
|
|
|
;; (message "hif-find-next-relevant at %d" (point))
|
1989-08-05 03:56:31 +00:00
|
|
|
(end-of-line)
|
2001-11-28 04:12:47 +00:00
|
|
|
;; avoid infinite recursion by only going to beginning of line if match found
|
1989-08-05 03:56:31 +00:00
|
|
|
(if (re-search-forward hif-ifx-else-endif-regexp (point-max) t)
|
1991-03-06 21:14:11 +00:00
|
|
|
(beginning-of-line)))
|
1989-08-05 03:56:31 +00:00
|
|
|
|
|
|
|
(defun hif-find-previous-relevant ()
|
1995-12-25 20:44:18 +00:00
|
|
|
"Move to previous #if..., #else, or #endif, before the current line."
|
2001-11-28 04:12:47 +00:00
|
|
|
;; (message "hif-find-previous-relevant at %d" (point))
|
1989-08-05 03:56:31 +00:00
|
|
|
(beginning-of-line)
|
2001-11-28 04:12:47 +00:00
|
|
|
;; avoid infinite recursion by only going to beginning of line if match found
|
1989-08-05 03:56:31 +00:00
|
|
|
(if (re-search-backward hif-ifx-else-endif-regexp (point-min) t)
|
1991-03-06 21:14:11 +00:00
|
|
|
(beginning-of-line)))
|
1989-08-05 03:56:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
(defun hif-looking-at-ifX () ;; Should eventually see #if
|
|
|
|
(looking-at hif-ifx-regexp))
|
|
|
|
(defun hif-looking-at-endif ()
|
|
|
|
(looking-at hif-endif-regexp))
|
|
|
|
(defun hif-looking-at-else ()
|
|
|
|
(looking-at hif-else-regexp))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(defun hif-ifdef-to-endif ()
|
|
|
|
"If positioned at #ifX or #else form, skip to corresponding #endif."
|
2001-11-28 04:12:47 +00:00
|
|
|
;; (message "hif-ifdef-to-endif at %d" (point)) (sit-for 1)
|
1989-08-05 03:56:31 +00:00
|
|
|
(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-looking-at-else)
|
|
|
|
(hif-ifdef-to-endif)) ; find endif following else
|
|
|
|
((hif-looking-at-endif)
|
|
|
|
'done)
|
|
|
|
(t
|
1993-06-09 11:59:12 +00:00
|
|
|
(error "Mismatched #ifdef #endif pair"))))
|
1989-08-05 03:56:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
(defun hif-endif-to-ifdef ()
|
|
|
|
"If positioned at #endif form, skip backward to corresponding #ifX."
|
2001-11-28 04:12:47 +00:00
|
|
|
;; (message "hif-endif-to-ifdef at %d" (point))
|
1989-08-05 03:56:31 +00:00
|
|
|
(let ((start (point)))
|
|
|
|
(hif-find-previous-relevant)
|
|
|
|
(if (= start (point))
|
1993-06-09 11:59:12 +00:00
|
|
|
(error "Mismatched #ifdef #endif pair")))
|
1989-08-05 03:56:31 +00:00
|
|
|
(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)
|
1992-08-04 04:09:07 +00:00
|
|
|
(t))) ; never gets here
|
1989-08-05 03:56:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
(defun forward-ifdef (&optional arg)
|
|
|
|
"Move point to beginning of line of the next ifdef-endif.
|
1991-03-06 21:14:11 +00:00
|
|
|
With argument, do this that many times."
|
1989-08-05 03:56:31 +00:00
|
|
|
(interactive "p")
|
|
|
|
(or arg (setq arg 1))
|
2001-11-28 04:12:47 +00:00
|
|
|
(if (< arg 0) (backward-ifdef (- arg))
|
|
|
|
(while (< 0 arg)
|
|
|
|
(setq arg (- arg))
|
|
|
|
(let ((start (point)))
|
|
|
|
(unless (hif-looking-at-ifX)
|
1989-08-05 03:56:31 +00:00
|
|
|
(hif-find-next-relevant))
|
2001-11-28 04:12:47 +00:00
|
|
|
(if (hif-looking-at-ifX)
|
|
|
|
(hif-ifdef-to-endif)
|
|
|
|
(goto-char start)
|
|
|
|
(error "No following #ifdef"))))))
|
1989-08-05 03:56:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
(defun backward-ifdef (&optional arg)
|
|
|
|
"Move point to beginning of the previous ifdef-endif.
|
1991-03-06 21:14:11 +00:00
|
|
|
With argument, do this that many times."
|
1989-08-05 03:56:31 +00:00
|
|
|
(interactive "p")
|
|
|
|
(or arg (setq arg 1))
|
2001-11-28 04:12:47 +00:00
|
|
|
(if (< arg 0) (forward-ifdef (- arg))
|
|
|
|
(while (< 0 arg)
|
|
|
|
(setq arg (1- arg))
|
|
|
|
(beginning-of-line)
|
|
|
|
(let ((start (point)))
|
|
|
|
(unless (hif-looking-at-endif)
|
1989-08-05 03:56:31 +00:00
|
|
|
(hif-find-previous-relevant))
|
2001-11-28 04:12:47 +00:00
|
|
|
(if (hif-looking-at-endif)
|
|
|
|
(hif-endif-to-ifdef)
|
|
|
|
(goto-char start)
|
|
|
|
(error "No previous #ifdef"))))))
|
1989-08-05 03:56:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
(defun down-ifdef ()
|
|
|
|
"Move point to beginning of nested ifdef or else-part."
|
|
|
|
(interactive)
|
|
|
|
(let ((start (point)))
|
|
|
|
(hif-find-next-relevant)
|
|
|
|
(if (or (hif-looking-at-ifX) (hif-looking-at-else))
|
|
|
|
()
|
|
|
|
(goto-char start)
|
1991-03-06 21:14:11 +00:00
|
|
|
(error "No following #ifdef"))))
|
1989-08-05 03:56:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
(defun up-ifdef ()
|
|
|
|
"Move point to beginning of enclosing ifdef or else-part."
|
|
|
|
(interactive)
|
|
|
|
(beginning-of-line)
|
|
|
|
(let ((start (point)))
|
2001-11-28 04:12:47 +00:00
|
|
|
(unless (hif-looking-at-endif)
|
|
|
|
(hif-find-previous-relevant))
|
1989-08-05 03:56:31 +00:00
|
|
|
(if (hif-looking-at-endif)
|
|
|
|
(hif-endif-to-ifdef))
|
|
|
|
(if (= start (point))
|
1991-03-06 21:14:11 +00:00
|
|
|
(error "No previous #ifdef"))))
|
1989-08-05 03:56:31 +00:00
|
|
|
|
|
|
|
(defun next-ifdef (&optional arg)
|
|
|
|
"Move to the beginning of the next #ifX, #else, or #endif.
|
1991-03-06 21:14:11 +00:00
|
|
|
With argument, do this that many times."
|
1989-08-05 03:56:31 +00:00
|
|
|
(interactive "p")
|
|
|
|
(or arg (setq arg 1))
|
2001-11-28 04:12:47 +00:00
|
|
|
(if (< arg 0) (previous-ifdef (- arg))
|
|
|
|
(while (< 0 arg)
|
|
|
|
(setq arg (1- arg))
|
|
|
|
(hif-find-next-relevant)
|
|
|
|
(when (eolp)
|
|
|
|
(beginning-of-line)
|
|
|
|
(error "No following #ifdefs, #elses, or #endifs")))))
|
1989-08-05 03:56:31 +00:00
|
|
|
|
|
|
|
(defun previous-ifdef (&optional arg)
|
|
|
|
"Move to the beginning of the previous #ifX, #else, or #endif.
|
1991-03-06 21:14:11 +00:00
|
|
|
With argument, do this that many times."
|
1989-08-05 03:56:31 +00:00
|
|
|
(interactive "p")
|
|
|
|
(or arg (setq arg 1))
|
2001-11-28 04:12:47 +00:00
|
|
|
(if (< arg 0) (next-ifdef (- arg))
|
|
|
|
(while (< 0 arg)
|
|
|
|
(setq arg (1- arg))
|
|
|
|
(let ((start (point)))
|
|
|
|
(hif-find-previous-relevant)
|
|
|
|
(if (= start (point))
|
|
|
|
(error "No previous #ifdefs, #elses, or #endifs"))))))
|
1989-08-05 03:56:31 +00:00
|
|
|
|
|
|
|
|
2001-11-28 04:12:47 +00:00
|
|
|
;;===%%SF%% parsing (End) ===
|
1989-08-05 03:56:31 +00:00
|
|
|
|
|
|
|
|
2001-11-28 04:12:47 +00:00
|
|
|
;;===%%SF%% hide-ifdef-hiding (Start) ===
|
1989-08-05 03:56:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
;;; 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)
|
|
|
|
|
2001-11-28 20:53:28 +00:00
|
|
|
(defsubst hif-make-range (start end &optional else)
|
2001-11-28 04:12:47 +00:00
|
|
|
(list start else end))
|
1989-08-05 03:56:31 +00:00
|
|
|
|
2001-11-28 20:53:28 +00:00
|
|
|
(defsubst hif-range-start (range) (elt range 0))
|
|
|
|
(defsubst hif-range-else (range) (elt range 1))
|
|
|
|
(defsubst hif-range-end (range) (elt range 2))
|
1989-08-05 03:56:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;;; 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 ()
|
2001-11-28 04:12:47 +00:00
|
|
|
"Return a Range structure describing the current #if region.
|
1989-08-05 03:56:31 +00:00
|
|
|
Point is left unchanged."
|
2001-11-28 04:12:47 +00:00
|
|
|
;; (message "hif-find-range at %d" (point))
|
1989-08-05 03:56:31 +00:00
|
|
|
(save-excursion
|
|
|
|
(beginning-of-line)
|
|
|
|
(let ((start (point))
|
|
|
|
(else nil)
|
|
|
|
(end nil))
|
|
|
|
;; Part one. Look for either #endif or #else.
|
|
|
|
;; This loop-and-a-half dedicated to E. Dijkstra.
|
2001-11-28 04:12:47 +00:00
|
|
|
(while (progn
|
|
|
|
(hif-find-next-relevant)
|
|
|
|
(hif-looking-at-ifX)) ; Skip nested ifdef
|
|
|
|
(hif-ifdef-to-endif))
|
1989-08-05 03:56:31 +00:00
|
|
|
;; Found either a #else or an #endif.
|
|
|
|
(cond ((hif-looking-at-else)
|
|
|
|
(setq else (point)))
|
|
|
|
(t
|
2001-11-28 04:12:47 +00:00
|
|
|
(setq end (point)))) ; (save-excursion (end-of-line) (point))
|
1989-08-05 03:56:31 +00:00
|
|
|
;; If found #else, look for #endif.
|
2001-11-28 04:12:47 +00:00
|
|
|
(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))) ; (save-excursion (end-of-line) (point))
|
|
|
|
(hif-make-range start end else))))
|
1989-08-05 03:56:31 +00:00
|
|
|
|
2003-02-04 13:24:35 +00:00
|
|
|
|
1989-08-05 03:56:31 +00:00
|
|
|
;;; A bit slimy.
|
|
|
|
|
|
|
|
(defun hif-hide-line (point)
|
1991-03-06 21:14:11 +00:00
|
|
|
"Hide the line containing point. Does nothing if `hide-ifdef-lines' is nil."
|
1989-08-05 03:56:31 +00:00
|
|
|
(if hide-ifdef-lines
|
|
|
|
(save-excursion
|
|
|
|
(goto-char point)
|
2001-11-28 20:53:28 +00:00
|
|
|
(hide-ifdef-region-internal (line-beginning-position)
|
|
|
|
(progn (hif-end-of-line) (point))))))
|
2003-02-04 13:24:35 +00:00
|
|
|
|
1989-08-05 03:56:31 +00:00
|
|
|
|
|
|
|
;;; 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
|
2003-02-04 13:24:35 +00:00
|
|
|
;;; compiled.
|
1989-08-05 03:56:31 +00:00
|
|
|
;;;
|
|
|
|
;;; 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)
|
1991-03-06 21:14:11 +00:00
|
|
|
"Call `hide-ifdef-guts' after narrowing to end of START line and END line."
|
1989-08-05 03:56:31 +00:00
|
|
|
(save-excursion
|
|
|
|
(save-restriction
|
|
|
|
(goto-char start)
|
|
|
|
(end-of-line)
|
|
|
|
(narrow-to-region (point) end)
|
|
|
|
(hide-ifdef-guts))))
|
|
|
|
|
|
|
|
(defun hif-possibly-hide ()
|
1995-12-25 20:44:18 +00:00
|
|
|
"Called at #ifX expression, this hides those parts that should be hidden.
|
|
|
|
It uses the judgement of `hide-ifdef-evaluator'."
|
2001-11-28 04:12:47 +00:00
|
|
|
;; (message "hif-possibly-hide") (sit-for 1)
|
|
|
|
(let ((test (hif-canonicalize))
|
|
|
|
(range (hif-find-range)))
|
|
|
|
;; (message "test = %s" test) (sit-for 1)
|
2003-02-04 13:24:35 +00:00
|
|
|
|
2001-11-28 04:12:47 +00:00
|
|
|
(hif-hide-line (hif-range-end range))
|
2001-11-28 20:53:28 +00:00
|
|
|
(if (not (hif-not (funcall hide-ifdef-evaluator test)))
|
|
|
|
(cond ((hif-range-else range) ; case 1
|
1989-08-05 03:56:31 +00:00
|
|
|
(hif-hide-line (hif-range-else range))
|
2001-11-28 20:53:28 +00:00
|
|
|
(hide-ifdef-region (hif-range-else range)
|
2001-11-28 04:12:47 +00:00
|
|
|
(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))))))
|
|
|
|
(hif-hide-line (hif-range-start range)) ; Always hide start.
|
|
|
|
(goto-char (hif-range-end range))
|
|
|
|
(end-of-line)))
|
1989-08-05 03:56:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(defun hide-ifdef-guts ()
|
1995-12-25 20:44:18 +00:00
|
|
|
"Does most of the work of `hide-ifdefs'.
|
|
|
|
It does not do the work that's pointless to redo on a recursive entry."
|
2001-11-28 04:12:47 +00:00
|
|
|
;; (message "hide-ifdef-guts")
|
1989-08-05 03:56:31 +00:00
|
|
|
(save-excursion
|
|
|
|
(goto-char (point-min))
|
|
|
|
(while (hif-find-any-ifX)
|
|
|
|
(hif-possibly-hide))))
|
|
|
|
|
2001-11-28 04:12:47 +00:00
|
|
|
;;===%%SF%% hide-ifdef-hiding (End) ===
|
1989-08-05 03:56:31 +00:00
|
|
|
|
|
|
|
|
2001-11-28 04:12:47 +00:00
|
|
|
;;===%%SF%% exports (Start) ===
|
1989-08-05 03:56:31 +00:00
|
|
|
|
1992-08-19 03:54:46 +00:00
|
|
|
;;;###autoload
|
1998-01-26 11:29:46 +00:00
|
|
|
(defcustom hide-ifdef-initially nil
|
|
|
|
"*Non-nil means call `hide-ifdefs' when Hide-Ifdef mode is first activated."
|
|
|
|
:type 'boolean
|
|
|
|
:group 'hide-ifdef)
|
1989-08-05 03:56:31 +00:00
|
|
|
|
1992-08-19 03:54:46 +00:00
|
|
|
;;;###autoload
|
1998-01-26 11:29:46 +00:00
|
|
|
(defcustom hide-ifdef-read-only nil
|
|
|
|
"*Set to non-nil if you want buffer to be read-only while hiding text."
|
|
|
|
:type 'boolean
|
|
|
|
:group 'hide-ifdef)
|
1989-08-05 03:56:31 +00:00
|
|
|
|
1992-08-19 03:54:46 +00:00
|
|
|
;;;###autoload
|
1998-01-26 11:29:46 +00:00
|
|
|
(defcustom hide-ifdef-lines nil
|
|
|
|
"*Non-nil means hide the #ifX, #else, and #endif lines."
|
|
|
|
:type 'boolean
|
|
|
|
:group 'hide-ifdef)
|
1989-08-05 03:56:31 +00:00
|
|
|
|
|
|
|
(defun hide-ifdef-toggle-read-only ()
|
2001-11-28 04:12:47 +00:00
|
|
|
"Toggle `hide-ifdef-read-only'."
|
1989-08-05 03:56:31 +00:00
|
|
|
(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-hiding
|
|
|
|
(setq buffer-read-only (or hide-ifdef-read-only hif-outside-read-only)))
|
1995-04-25 22:29:17 +00:00
|
|
|
(force-mode-line-update))
|
1989-08-05 03:56:31 +00:00
|
|
|
|
|
|
|
(defun hide-ifdef-toggle-outside-read-only ()
|
1995-12-25 20:44:18 +00:00
|
|
|
"Replacement for `toggle-read-only' within Hide-Ifdef mode."
|
1989-08-05 03:56:31 +00:00
|
|
|
(interactive)
|
|
|
|
(setq hif-outside-read-only (not hif-outside-read-only))
|
|
|
|
(message "Read only %s"
|
|
|
|
(if hif-outside-read-only "ON" "OFF"))
|
|
|
|
(setq buffer-read-only
|
|
|
|
(or (and hide-ifdef-hiding hide-ifdef-read-only)
|
2001-11-28 04:12:47 +00:00
|
|
|
hif-outside-read-only))
|
1995-04-25 22:29:17 +00:00
|
|
|
(force-mode-line-update))
|
1989-08-05 03:56:31 +00:00
|
|
|
|
2003-02-04 13:24:35 +00:00
|
|
|
|
1989-08-05 03:56:31 +00:00
|
|
|
(defun hide-ifdef-define (var)
|
|
|
|
"Define a VAR so that #ifdef VAR would be included."
|
|
|
|
(interactive "SDefine what? ")
|
Handle ==, !=, + and -.
(hif-tokenize): Handle ==, !=, + and -.
(hif-token-regexp): Handle ==, !=, + and -.
(hif-notequal, hif-plus, hif-minus, hif-mathify): New functions.
(hif-math, hif-eq-expr): New functions.
(hif-term): Call hif-eq-expr.
(hide-ifdef-define): Pass 1 to hif-set-var.
1994-09-19 05:29:15 +00:00
|
|
|
(hif-set-var var 1)
|
1989-08-05 03:56:31 +00:00
|
|
|
(if hide-ifdef-hiding (hide-ifdefs)))
|
|
|
|
|
|
|
|
(defun hide-ifdef-undef (var)
|
|
|
|
"Undefine a VAR so that #ifdef VAR would not be included."
|
|
|
|
(interactive "SUndefine what? ")
|
|
|
|
(hif-set-var var nil)
|
|
|
|
(if hide-ifdef-hiding (hide-ifdefs)))
|
|
|
|
|
|
|
|
|
1995-12-25 20:44:18 +00:00
|
|
|
(defun hide-ifdefs (&optional nomsg)
|
2003-02-04 13:24:35 +00:00
|
|
|
"Hide the contents of some #ifdefs.
|
|
|
|
Assume that defined symbols have been added to `hide-ifdef-env'.
|
1992-10-25 21:05:35 +00:00
|
|
|
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.
|
1989-08-05 03:56:31 +00:00
|
|
|
|
1994-07-29 20:00:00 +00:00
|
|
|
Turn off hiding by calling `show-ifdefs'."
|
1989-08-05 03:56:31 +00:00
|
|
|
|
|
|
|
(interactive)
|
|
|
|
(message "Hiding...")
|
1995-12-25 21:03:10 +00:00
|
|
|
(setq hif-outside-read-only buffer-read-only)
|
2001-11-28 04:12:47 +00:00
|
|
|
(unless hide-ifdef-mode (hide-ifdef-mode 1)) ; turn on hide-ifdef-mode
|
1989-08-05 03:56:31 +00:00
|
|
|
(if hide-ifdef-hiding
|
|
|
|
(show-ifdefs)) ; Otherwise, deep confusion.
|
2001-11-28 04:12:47 +00:00
|
|
|
(setq hide-ifdef-hiding t)
|
|
|
|
(hide-ifdef-guts)
|
1994-05-04 23:20:44 +00:00
|
|
|
(setq buffer-read-only (or hide-ifdef-read-only hif-outside-read-only))
|
1995-12-25 20:44:18 +00:00
|
|
|
(or nomsg
|
|
|
|
(message "Hiding done")))
|
1989-08-05 03:56:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
(defun show-ifdefs ()
|
1995-12-25 20:44:18 +00:00
|
|
|
"Cancel the effects of `hide-ifdef': show the contents of all #ifdefs."
|
1989-08-05 03:56:31 +00:00
|
|
|
(interactive)
|
1994-05-04 23:20:44 +00:00
|
|
|
(setq buffer-read-only hif-outside-read-only)
|
2001-11-28 04:12:47 +00:00
|
|
|
(hif-show-all)
|
1991-03-06 21:14:11 +00:00
|
|
|
(setq hide-ifdef-hiding nil))
|
1989-08-05 03:56:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
(defun hif-find-ifdef-block ()
|
1993-06-09 11:59:12 +00:00
|
|
|
"Utility for hide and show `ifdef-block'.
|
1998-10-13 09:52:54 +00:00
|
|
|
Return as (TOP . BOTTOM) the extent of ifdef block."
|
1989-08-05 03:56:31 +00:00
|
|
|
(let (max-bottom)
|
1998-10-13 09:52:54 +00:00
|
|
|
(cons (save-excursion
|
|
|
|
(beginning-of-line)
|
2001-11-28 04:12:47 +00:00
|
|
|
(unless (or (hif-looking-at-else) (hif-looking-at-ifX))
|
|
|
|
(up-ifdef))
|
1998-10-13 09:52:54 +00:00
|
|
|
(prog1 (point)
|
|
|
|
(hif-ifdef-to-endif)
|
|
|
|
(setq max-bottom (1- (point)))))
|
|
|
|
(save-excursion
|
|
|
|
(beginning-of-line)
|
2001-11-28 04:12:47 +00:00
|
|
|
(unless (hif-looking-at-endif)
|
|
|
|
(hif-find-next-relevant))
|
1998-10-13 09:52:54 +00:00
|
|
|
(while (hif-looking-at-ifX)
|
|
|
|
(hif-ifdef-to-endif)
|
|
|
|
(hif-find-next-relevant))
|
|
|
|
(min max-bottom (1- (point)))))))
|
1989-08-05 03:56:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
(defun hide-ifdef-block ()
|
|
|
|
"Hide the ifdef block (true or false part) enclosing or before the cursor."
|
|
|
|
(interactive)
|
2001-11-28 04:12:47 +00:00
|
|
|
(unless hide-ifdef-mode (hide-ifdef-mode 1))
|
|
|
|
(let ((top-bottom (hif-find-ifdef-block)))
|
1998-10-13 09:52:54 +00:00
|
|
|
(hide-ifdef-region (car top-bottom) (cdr top-bottom))
|
2001-11-28 04:12:47 +00:00
|
|
|
(when hide-ifdef-lines
|
|
|
|
(hif-hide-line (car top-bottom))
|
|
|
|
(hif-hide-line (1+ (cdr top-bottom))))
|
1991-03-06 21:14:11 +00:00
|
|
|
(setq hide-ifdef-hiding t))
|
1994-05-04 23:20:44 +00:00
|
|
|
(setq buffer-read-only (or hide-ifdef-read-only hif-outside-read-only)))
|
1989-08-05 03:56:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
(defun show-ifdef-block ()
|
|
|
|
"Show the ifdef block (true or false part) enclosing or before the cursor."
|
|
|
|
(interactive)
|
2001-11-28 04:12:47 +00:00
|
|
|
(if hide-ifdef-lines
|
|
|
|
(save-excursion
|
|
|
|
(beginning-of-line)
|
|
|
|
(hif-show-ifdef-region (1- (point)) (progn (end-of-line) (point))))
|
|
|
|
(let ((top-bottom (hif-find-ifdef-block)))
|
|
|
|
(hif-show-ifdef-region (1- (car top-bottom)) (cdr top-bottom)))))
|
1989-08-05 03:56:31 +00:00
|
|
|
|
|
|
|
|
1993-06-09 11:59:12 +00:00
|
|
|
;;; definition alist support
|
1989-08-05 03:56:31 +00:00
|
|
|
|
|
|
|
(defvar hide-ifdef-define-alist nil
|
2001-11-28 04:12:47 +00:00
|
|
|
"A global assoc list of pre-defined symbol lists.")
|
1989-08-05 03:56:31 +00:00
|
|
|
|
|
|
|
(defun hif-compress-define-list (env)
|
|
|
|
"Compress the define list ENV into a list of defined symbols only."
|
2001-11-28 04:12:47 +00:00
|
|
|
(let ((new-defs nil))
|
|
|
|
(dolist (def env new-defs)
|
|
|
|
(if (hif-lookup (car def)) (push (car env) new-defs)))))
|
1989-08-05 03:56:31 +00:00
|
|
|
|
|
|
|
(defun hide-ifdef-set-define-alist (name)
|
1992-10-25 21:05:35 +00:00
|
|
|
"Set the association for NAME to `hide-ifdef-env'."
|
1989-08-05 03:56:31 +00:00
|
|
|
(interactive "SSet define list: ")
|
2001-11-28 04:12:47 +00:00
|
|
|
(push (cons name (hif-compress-define-list hide-ifdef-env))
|
|
|
|
hide-ifdef-define-alist))
|
1989-08-05 03:56:31 +00:00
|
|
|
|
|
|
|
(defun hide-ifdef-use-define-alist (name)
|
1992-10-25 21:05:35 +00:00
|
|
|
"Set `hide-ifdef-env' to the define list specified by NAME."
|
2005-02-22 20:52:39 +00:00
|
|
|
(interactive
|
|
|
|
(list (completing-read "Use define list: "
|
|
|
|
hide-ifdef-define-alist nil t)))
|
|
|
|
(if (stringp name) (setq name (intern name)))
|
1989-08-05 03:56:31 +00:00
|
|
|
(let ((define-list (assoc name hide-ifdef-define-alist)))
|
|
|
|
(if define-list
|
|
|
|
(setq hide-ifdef-env
|
2000-06-12 05:22:26 +00:00
|
|
|
(mapcar (lambda (arg) (cons arg t))
|
1989-08-05 03:56:31 +00:00
|
|
|
(cdr define-list)))
|
|
|
|
(error "No define list for %s" name))
|
1991-03-06 21:14:11 +00:00
|
|
|
(if hide-ifdef-hiding (hide-ifdefs))))
|
1989-08-05 03:56:31 +00:00
|
|
|
|
1995-10-26 03:13:43 +00:00
|
|
|
(provide 'hideif)
|
|
|
|
|
2003-09-01 15:45:59 +00:00
|
|
|
;;; arch-tag: c6381d17-a59a-483a-b945-658f22277981
|
1992-05-30 23:12:08 +00:00
|
|
|
;;; hideif.el ends here
|