annotate lisp/modes/hideif.el @ 2:ac2d302a0011 r19-15b2

Import from CVS: tag r19-15b2
author cvs
date Mon, 13 Aug 2007 08:46:35 +0200
parents 376386a54a3c
children 56c54cf7c5b6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1 ;;; hide-ifdef-mode.el --- hides selected code within ifdef.
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
3 ;; Copyright (C) 1988, 1994 Free Software Foundation, Inc.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
4
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
5 ;; Author: Dan LaLiberte <liberte@a.cs.uiuc.edu>
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
6 ;; Maintainer: FSF
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
7 ;; Keywords: c, outlines
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
8
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
9 ;; This file is part of XEmacs.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
10
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
11 ;; XEmacs is free software; you can redistribute it and/or modify it
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
12 ;; under the terms of the GNU General Public License as published by
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
13 ;; the Free Software Foundation; either version 2, or (at your option)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
14 ;; any later version.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
15
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
16 ;; XEmacs is distributed in the hope that it will be useful, but
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
19 ;; General Public License for more details.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
20
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
21 ;; You should have received a copy of the GNU General Public License
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
22 ;; along with XEmacs; see the file COPYING. If not, write to the Free
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
23 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
24 ;; 02111-1307, USA.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
25
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
26 ;;; Synched up with: FSF 19.34.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
27
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
28 ;;; Commentary:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
29
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
30 ;; To initialize, toggle the hide-ifdef minor mode with
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
31 ;;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
32 ;; M-x hide-ifdef-mode
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
33 ;;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
34 ;; This will set up key bindings and call hide-ifdef-mode-hook if it
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
35 ;; has a value. To explicitly hide ifdefs using a buffer-local
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
36 ;; define list (default empty), type
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
37 ;;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
38 ;; M-x hide-ifdefs or C-c @ h
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
39 ;;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
40 ;; Hide-ifdef suppresses the display of code that the preprocessor wouldn't
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
41 ;; pass through. The support of constant expressions in #if lines is
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
42 ;; limited to identifiers, parens, and the operators: &&, ||, !, and
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
43 ;; "defined". Please extend this.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
44 ;;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
45 ;; The hidden code is marked by ellipses (...). Be
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
46 ;; cautious when editing near ellipses, since the hidden text is
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
47 ;; still in the buffer, and you can move the point into it and modify
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
48 ;; text unawares. If you don't want to see the ellipses, set
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
49 ;; selective-display-ellipses to nil. But this can be dangerous.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
50 ;; You can make your buffer read-only while hide-ifdef-hiding by setting
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
51 ;; hide-ifdef-read-only to a non-nil value. You can toggle this
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
52 ;; variable with hide-ifdef-toggle-read-only (C-c @ C-q).
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
53 ;;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
54 ;; You can undo the effect of hide-ifdefs by typing
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
55 ;;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
56 ;; M-x show-ifdefs or C-c @ s
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
57 ;;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
58 ;; Use M-x hide-ifdef-define (C-c @ d) to define a symbol.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
59 ;; Use M-x hide-ifdef-undef (C-c @ u) to undefine a symbol.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
60 ;;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
61 ;; If you define or undefine a symbol while hide-ifdef-mode is in effect,
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
62 ;; the display will be updated. Only the define list for the current
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
63 ;; buffer will be affected. You can save changes to the local define
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
64 ;; list with hide-ifdef-set-define-alist. This adds entries
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
65 ;; to hide-ifdef-define-alist.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
66 ;;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
67 ;; If you have defined a hide-ifdef-mode-hook, you can set
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
68 ;; up a list of symbols that may be used by hide-ifdefs as in the
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
69 ;; following example:
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
70 ;;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
71 ;; (setq hide-ifdef-mode-hook
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
72 ;; '(lambda ()
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
73 ;; (if (not hide-ifdef-define-alist)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
74 ;; (setq hide-ifdef-define-alist
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
75 ;; '((list1 ONE TWO)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
76 ;; (list2 TWO THREE)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
77 ;; )))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
78 ;; (hide-ifdef-use-define-alist 'list2) ; use list2 by default
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
79 ;; ))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
80 ;;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
81 ;; You can call hide-ifdef-use-define-alist (C-c u) at any time to specify
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
82 ;; another list to use.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
83 ;;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
84 ;; To cause ifdefs to be hidden as soon as hide-ifdef-mode is called,
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
85 ;; set hide-ifdef-initially to non-nil.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
86 ;;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
87 ;; If you set hide-ifdef-lines to t, hide-ifdefs hides all the #ifdef lines.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
88 ;; In the absence of highlighting, that might be a bad idea. If you set
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
89 ;; hide-ifdef-lines to nil (the default), the surrounding preprocessor
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
90 ;; lines will be displayed. That can be confusing in its own
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
91 ;; right. Other variations on display are possible, but not much
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
92 ;; better.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
93 ;;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
94 ;; You can explicitly hide or show individual ifdef blocks irrespective
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
95 ;; of the define list by using hide-ifdef-block and show-ifdef-block.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
96 ;;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
97 ;; You can move the point between ifdefs with forward-ifdef, backward-ifdef,
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
98 ;; up-ifdef, down-ifdef, next-ifdef, and previous-ifdef.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
99 ;;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
100 ;; If you have minor-mode-alist in your mode line (the default) two labels
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
101 ;; may appear. "Ifdef" will appear when hide-ifdef-mode is active. "Hiding"
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
102 ;; will appear when text may be hidden ("hide-ifdef-hiding" is non-nil).
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
103 ;;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
104 ;; Written by Brian Marick, at Gould, Computer Systems Division, Urbana IL.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
105 ;; Extensively modified by Daniel LaLiberte (while at Gould).
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
106 ;;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
107 ;; You may freely modify and distribute this, but keep a record
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
108 ;; of modifications and send comments to:
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
109 ;; liberte@a.cs.uiuc.edu or ihnp4!uiucdcs!liberte
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
110 ;; I will continue to upgrade hide-ifdef-mode
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
111 ;; with your contributions.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
112
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
113 ;;; Code:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
114
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
115 (require 'cc-mode)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
116
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
117 (defvar hide-ifdef-mode-submap nil
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
118 "Keymap used with Hide-Ifdef mode.")
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
119
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
120 (defvar hide-ifdef-mode-map nil
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
121 "Keymap used with Hide-Ifdef mode.")
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
122
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
123 (defconst hide-ifdef-mode-prefix-key "\C-c@"
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
124 "Prefix key for all Hide-Ifdef mode commands.")
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
125
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
126 ;; Set up the submap that goes after the prefix key.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
127 (if hide-ifdef-mode-submap
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
128 () ; Don't redefine it.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
129 (setq hide-ifdef-mode-submap (make-sparse-keymap))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
130 (define-key hide-ifdef-mode-submap "d" 'hide-ifdef-define)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
131 (define-key hide-ifdef-mode-submap "u" 'hide-ifdef-undef)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
132 (define-key hide-ifdef-mode-submap "D" 'hide-ifdef-set-define-alist)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
133 (define-key hide-ifdef-mode-submap "U" 'hide-ifdef-use-define-alist)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
134
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
135 (define-key hide-ifdef-mode-submap "h" 'hide-ifdefs)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
136 (define-key hide-ifdef-mode-submap "s" 'show-ifdefs)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
137 (define-key hide-ifdef-mode-submap "\C-d" 'hide-ifdef-block)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
138 (define-key hide-ifdef-mode-submap "\C-s" 'show-ifdef-block)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
139
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
140 (define-key hide-ifdef-mode-submap "\C-q" 'hide-ifdef-toggle-read-only)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
141 (let ((where (where-is-internal 'toggle-read-only '(keymap) t)))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
142 (if where
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
143 (define-key hide-ifdef-mode-submap
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
144 where
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
145 'hide-ifdef-toggle-outside-read-only)))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
146 )
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
147
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
148 ;; Set up the mode's main map, which leads via the prefix key to the submap.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
149 (if hide-ifdef-mode-map
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
150 ()
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
151 (setq hide-ifdef-mode-map (make-sparse-keymap))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
152 (define-key hide-ifdef-mode-map hide-ifdef-mode-prefix-key
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
153 hide-ifdef-mode-submap))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
154
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
155 (defvar hide-ifdef-mode nil
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
156 "Non-nil when hide-ifdef-mode is activated.")
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
157
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
158 (defvar hide-ifdef-hiding nil
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
159 "Non-nil when text may be hidden.")
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
160
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
161 (or (assq 'hide-ifdef-hiding minor-mode-alist)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
162 (setq minor-mode-alist
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
163 (cons '(hide-ifdef-hiding " Hiding")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
164 minor-mode-alist)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
165
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
166 ;(or (assq 'hide-ifdef-mode minor-mode-alist)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
167 ; (setq minor-mode-alist
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
168 ; (cons '(hide-ifdef-mode " Ifdef")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
169 ; minor-mode-alist)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
170 ;; XEmacs: do it right.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
171 ;;;###autoload
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
172 (add-minor-mode 'hide-ifdef-mode " Ifdef")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
173
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
174 ;; fix c-mode syntax table so we can recognize whole symbols.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
175 (defvar hide-ifdef-syntax-table
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
176 (copy-syntax-table c-mode-syntax-table)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
177 "Syntax table used for tokenizing #if expressions.")
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
178
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
179 (modify-syntax-entry ?_ "w" hide-ifdef-syntax-table)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
180 (modify-syntax-entry ?& "." hide-ifdef-syntax-table)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
181 (modify-syntax-entry ?\| "." hide-ifdef-syntax-table)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
182
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
183 ;;;###autoload
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
184 (defun hide-ifdef-mode (arg)
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
185 "Toggle Hide-Ifdef mode. This is a minor mode, albeit a large one.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
186 With ARG, turn Hide-Ifdef mode on if arg is positive, off otherwise.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
187 In Hide-Ifdef mode, code within #ifdef constructs that the C preprocessor
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
188 would eliminate may be hidden from view. Several variables affect
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
189 how the hiding is done:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
190
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
191 hide-ifdef-env
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
192 An association list of defined and undefined symbols for the
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
193 current buffer. Initially, the global value of `hide-ifdef-env'
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
194 is used.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
195
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
196 hide-ifdef-define-alist
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
197 An association list of defined symbol lists.
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
198 Use `hide-ifdef-set-define-alist' to save the current `hide-ifdef-env'
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
199 and `hide-ifdef-use-define-alist' to set the current `hide-ifdef-env'
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
200 from one of the lists in `hide-ifdef-define-alist'.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
201
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
202 hide-ifdef-lines
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
203 Set to non-nil to not show #if, #ifdef, #ifndef, #else, and
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
204 #endif lines when hiding.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
205
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
206 hide-ifdef-initially
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
207 Indicates whether `hide-ifdefs' should be called when Hide-Ifdef mode
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
208 is activated.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
209
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
210 hide-ifdef-read-only
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
211 Set to non-nil if you want to make buffers read only while hiding.
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
212 After `show-ifdefs', read-only status is restored to previous value.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
213
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
214 \\{hide-ifdef-mode-map}"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
215
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
216 (interactive "P")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
217 (make-local-variable 'hide-ifdef-mode)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
218 (setq hide-ifdef-mode
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
219 (if (null arg)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
220 (not hide-ifdef-mode)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
221 (> (prefix-numeric-value arg) 0)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
222
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
223 ;; XEmacs change
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
224 (redraw-modeline)
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
225
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
226 (if hide-ifdef-mode
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
227 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
228 ; fix c-mode syntax table so we can recognize whole symbols.
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
229 ;; XEmacs: Maybe we don't need this any more with cc-mode? -sb
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
230 ;; (modify-syntax-entry ?_ "w")
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
231 ;; (modify-syntax-entry ?& ".")
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
232 ;; (modify-syntax-entry ?\| ".")
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
233
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
234 ; inherit global values
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
235 (make-local-variable 'hide-ifdef-env)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
236 (setq hide-ifdef-env (default-value 'hide-ifdef-env))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
237
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
238 (make-local-variable 'hide-ifdef-hiding)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
239 (setq hide-ifdef-hiding (default-value 'hide-ifdef-hiding))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
240
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
241 (make-local-variable 'hif-outside-read-only)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
242 (setq hif-outside-read-only buffer-read-only)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
243
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
244 (run-hooks 'hide-ifdef-mode-hook)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
245
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
246 (if hide-ifdef-initially
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
247 (hide-ifdefs)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
248 (show-ifdefs))
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
249 (message "Enter Hide-Ifdef mode")
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
250 )
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
251 ; else end hide-ifdef-mode
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
252 (if hide-ifdef-hiding
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
253 (show-ifdefs))
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
254 (message "Exit Hide-Ifdef mode")
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
255 ))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
256
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
257
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
258 ;; from outline.el with docstring fixed.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
259 (defun hif-outline-flag-region (from to flag)
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
260 "Hides or shows lines from FROM to TO, according to FLAG.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
261 If FLAG is \\n (newline character) then text is shown, while if FLAG is \\^M
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
262 \(control-M) the text is hidden."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
263 (let ((modp (buffer-modified-p)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
264 (unwind-protect (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
265 (subst-char-in-region from to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
266 (if (= flag ?\n) ?\^M ?\n)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
267 flag t) )
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
268 (set-buffer-modified-p modp))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
269 ))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
270
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
271 (defun hif-show-all ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
272 "Show all of the text in the current buffer."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
273 (interactive)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
274 (hif-outline-flag-region (point-min) (point-max) ?\n))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
275
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
276 ;; By putting this on after-revert-hook, we arrange that it only
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
277 ;; does anything when revert-buffer avoids turning off the mode.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
278 ;; (That can happen in VC.)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
279 (defun hif-before-revert-function ()
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
280 (and hide-ifdef-mode hide-ifdef-hiding
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
281 (hide-ifdefs t)))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
282 (add-hook 'after-revert-hook 'hif-before-revert-function)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
283
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
284 (defun hide-ifdef-region (start end)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
285 "START is the start of a #if or #else form. END is the ending part.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
286 Everything including these lines is made invisible."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
287 (hif-outline-flag-region start end ?\^M)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
288 )
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
289
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
290 (defun hif-show-ifdef-region (start end)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
291 "Everything between START and END is made visible."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
292 (hif-outline-flag-region start end ?\n)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
293 )
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
294
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
295
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
296
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
297 ;===%%SF%% evaluation (Start) ===
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
298
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
299 ;; It is not useful to set this to anything but `eval'.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
300 ;; In fact, the variable might as well be eliminated.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
301 (defvar hide-ifdef-evaluator 'eval
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
302 "The function to use to evaluate a form.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
303 The evaluator is given a canonical form and returns t if text under
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
304 that form should be displayed.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
305
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
306 (defvar hif-undefined-symbol nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
307 "...is by default considered to be false.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
308
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
309 (defvar hide-ifdef-env nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
310 "An alist of defined symbols and their values.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
311
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
312
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
313 (defun hif-set-var (var value)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
314 "Prepend (var value) pair to hide-ifdef-env."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
315 (setq hide-ifdef-env (cons (cons var value) hide-ifdef-env)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
316
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
317
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
318 (defun hif-lookup (var)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
319 ; (message "hif-lookup %s" var)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
320 (let ((val (assoc var hide-ifdef-env)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
321 (if val
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
322 (cdr val)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
323 hif-undefined-symbol)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
324
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
325 (defun hif-defined (var)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
326 (hif-lookup var)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
327 ; when #if expressions are fully supported, defined result should be 1
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
328 ; (if (assoc var hide-ifdef-env)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
329 ; 1
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
330 ; nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
331 )
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
332
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
333
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
334 ;===%%SF%% evaluation (End) ===
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
335
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
336
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
337
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
338 ;===%%SF%% parsing (Start) ===
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
339 ;;; The code that understands what ifs and ifdef in files look like.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
340
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
341 (defconst hif-cpp-prefix "\\(^\\|\r\\)[ \t]*#[ \t]*")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
342 (defconst hif-ifndef-regexp (concat hif-cpp-prefix "ifndef"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
343 (defconst hif-ifx-regexp (concat hif-cpp-prefix "if\\(n?def\\)?[ \t]+"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
344 (defconst hif-else-regexp (concat hif-cpp-prefix "else"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
345 (defconst hif-endif-regexp (concat hif-cpp-prefix "endif"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
346 (defconst hif-ifx-else-endif-regexp
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
347 (concat hif-ifx-regexp "\\|" hif-else-regexp "\\|" hif-endif-regexp))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
348
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
349
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
350 (defun hif-infix-to-prefix (token-list)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
351 "Convert list of tokens in infix into prefix list"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
352 ; (message "hif-infix-to-prefix: %s" token-list)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
353 (if (= 1 (length token-list))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
354 (` (hif-lookup (quote (, (car token-list)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
355 (hif-parse-if-exp token-list))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
356 )
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
357
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
358 ; pattern to match initial identifier, !, &&, ||, (, or ).
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
359 ; Added ==, + and -: garyo@avs.com 8/9/94
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
360 (defconst hif-token-regexp "^\\(!\\|&&\\|||\\|[!=]=\\|[()+-]\\|\\w+\\)")
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
361 (defconst hif-end-of-comment "\\*/")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
362
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
363
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
364 (defun hif-tokenize (expr-string)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
365 "Separate string into a list of tokens"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
366 (let ((token-list nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
367 (expr-start 0)
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
368 (expr-length (length expr-string))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
369 (current-syntax-table (syntax-table)))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
370 (unwind-protect
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
371 (progn
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
372 (set-syntax-table hide-ifdef-syntax-table)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
373 (while (< expr-start expr-length)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
374 ; (message "expr-start = %d" expr-start) (sit-for 1)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
375 (cond
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
376 ((string-match "^[ \t]+" expr-string expr-start)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
377 ;; skip whitespace
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
378 (setq expr-start (match-end 0))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
379 ;; stick newline in string so ^ matches on the next string-match
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
380 (aset expr-string (1- expr-start) ?\n))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
381
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
382 ((string-match "^/\\*" expr-string expr-start)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
383 (setq expr-start (match-end 0))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
384 (aset expr-string (1- expr-start) ?\n)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
385 (or
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
386 (string-match hif-end-of-comment
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
387 expr-string expr-start) ; eat comment
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
388 (string-match "$" expr-string expr-start)) ; multi-line comment
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
389 (setq expr-start (match-end 0))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
390 (aset expr-string (1- expr-start) ?\n))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
391
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
392 ((string-match "^//" expr-string expr-start)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
393 (string-match "$" expr-string expr-start)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
394 (setq expr-start (match-end 0)))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
395
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
396 ((string-match hif-token-regexp expr-string expr-start)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
397 (let ((token (substring expr-string expr-start (match-end 0))))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
398 (setq expr-start (match-end 0))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
399 (aset expr-string (1- expr-start) ?\n)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
400 ; (message "token: %s" token) (sit-for 1)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
401 (setq token-list
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
402 (cons
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
403 (cond
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
404 ((string-equal token "||") 'or)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
405 ((string-equal token "&&") 'and)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
406 ((string-equal token "==") 'equal)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
407 ((string-equal token "!=") 'hif-notequal)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
408 ((string-equal token "!") 'not)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
409 ((string-equal token "defined") 'hif-defined)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
410 ((string-equal token "(") 'lparen)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
411 ((string-equal token ")") 'rparen)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
412 ((string-equal token "+") 'hif-plus)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
413 ((string-equal token "-") 'hif-minus)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
414 (t (intern token)))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
415 token-list))))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
416 (t (error "Bad #if expression: %s" expr-string)))))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
417 (set-syntax-table current-syntax-table))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
418 (nreverse token-list)))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
419
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
420 ;;;-----------------------------------------------------------------
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
421 ;;; Translate C preprocessor #if expressions using recursive descent.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
422 ;;; This parser is limited to the operators &&, ||, !, and "defined".
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
423 ;;; Added ==, !=, +, and -. Gary Oberbrunner, garyo@avs.com, 8/9/94
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
424
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
425 (defun hif-parse-if-exp (token-list)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
426 "Parse the TOKEN-LIST. Return translated list in prefix form."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
427 (hif-nexttoken)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
428 (prog1
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
429 (hif-expr)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
430 (if token ; is there still a token?
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
431 (error "Error: unexpected token: %s" token))))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
432
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
433 (defun hif-nexttoken ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
434 "Pop the next token from token-list into the let variable \"token\"."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
435 (setq token (car token-list))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
436 (setq token-list (cdr token-list))
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
437 token)
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
438
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
439 (defun hif-expr ()
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
440 "Parse an expression as found in #if.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
441 expr : term | expr '||' term."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
442 (let ((result (hif-term)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
443 (while (eq token 'or)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
444 (hif-nexttoken)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
445 (setq result (list 'or result (hif-term))))
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
446 result))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
447
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
448 (defun hif-term ()
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
449 "Parse a term : eq-expr | term '&&' eq-expr."
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
450 (let ((result (hif-eq-expr)))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
451 (while (eq token 'and)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
452 (hif-nexttoken)
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
453 (setq result (list 'and result (hif-eq-expr))))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
454 result))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
455
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
456 (defun hif-eq-expr ()
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
457 "Parse an eq-expr : math | eq-expr '=='|'!=' math."
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
458 (let ((result (hif-math))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
459 (eq-token nil))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
460 (while (or (eq token 'equal) (eq token 'hif-notequal))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
461 (setq eq-token token)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
462 (hif-nexttoken)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
463 (setq result (list eq-token result (hif-math))))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
464 result))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
465
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
466 (defun hif-math ()
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
467 "Parse an expression with + or - and simpler things.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
468 math : factor | math '+|-' factor."
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
469 (let ((result (hif-factor))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
470 (math-op nil))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
471 (while (or (eq token 'hif-plus) (eq token 'hif-minus))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
472 (setq math-op token)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
473 (hif-nexttoken)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
474 (setq result (list math-op result (hif-factor))))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
475 result))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
476
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
477 (defun hif-factor ()
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
478 "Parse a factor: '!' factor | '(' expr ')' | 'defined(' id ')' | id."
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
479 (cond
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
480 ((eq token 'not)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
481 (hif-nexttoken)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
482 (list 'not (hif-factor)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
483
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
484 ((eq token 'lparen)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
485 (hif-nexttoken)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
486 (let ((result (hif-expr)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
487 (if (not (eq token 'rparen))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
488 (error "Bad token in parenthesized expression: %s" token)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
489 (hif-nexttoken)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
490 result)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
491
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
492 ((eq token 'hif-defined)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
493 (hif-nexttoken)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
494 (if (not (eq token 'lparen))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
495 (error "Error: expected \"(\" after \"defined\""))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
496 (hif-nexttoken)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
497 (let ((ident token))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
498 (if (memq token '(or and not hif-defined lparen rparen))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
499 (error "Error: unexpected token: %s" token))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
500 (hif-nexttoken)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
501 (if (not (eq token 'rparen))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
502 (error "Error: expected \")\" after identifier"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
503 (hif-nexttoken)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
504 (` (hif-defined (quote (, ident))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
505 ))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
506
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
507 (t ; identifier
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
508 (let ((ident token))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
509 (if (memq ident '(or and))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
510 (error "Error: missing identifier"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
511 (hif-nexttoken)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
512 (` (hif-lookup (quote (, ident))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
513 ))
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
514 ))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
515
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
516 (defun hif-mathify (val)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
517 "Treat VAL as a number: if it's t or nil, use 1 or 0."
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
518 (cond ((eq val t)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
519 1)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
520 ((null val)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
521 0)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
522 (t val)))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
523
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
524 (defun hif-plus (a b)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
525 "Like ordinary plus but treat t and nil as 1 and 0."
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
526 (+ (hif-mathify a) (hif-mathify b)))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
527 (defun hif-minus (a b)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
528 "Like ordinary minus but treat t and nil as 1 and 0."
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
529 (- (hif-mathify a) (hif-mathify b)))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
530 (defun hif-notequal (a b)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
531 "Like (not (equal A B)) but as one symbol."
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
532 (not (equal a b)))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
533
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
534 ;;;----------- end of parser -----------------------
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
535
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
536
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
537 (defun hif-canonicalize ()
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
538 "When at beginning of #ifX, returns a Lisp expression for its condition."
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
539 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
540 (let ((negate (looking-at hif-ifndef-regexp)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
541 (re-search-forward hif-ifx-regexp)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
542 (let* ((expr-string
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
543 (buffer-substring (point)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
544 (progn (skip-chars-forward "^\n\r") (point))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
545 (expr (hif-infix-to-prefix (hif-tokenize expr-string))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
546 ; (message "hif-canonicalized: %s" expr)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
547 (if negate
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
548 (list 'not expr)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
549 expr)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
550
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
551
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
552 (defun hif-find-any-ifX ()
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
553 "Move to next #if..., or #ifndef, at point or after."
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
554 ; (message "find ifX at %d" (point))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
555 (prog1
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
556 (re-search-forward hif-ifx-regexp (point-max) t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
557 (beginning-of-line)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
558
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
559
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
560 (defun hif-find-next-relevant ()
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
561 "Move to next #if..., #else, or #endif, after the current line."
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
562 ; (message "hif-find-next-relevant at %d" (point))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
563 (end-of-line)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
564 ; avoid infinite recursion by only going to beginning of line if match found
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
565 (if (re-search-forward hif-ifx-else-endif-regexp (point-max) t)
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
566 (beginning-of-line)))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
567
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
568 (defun hif-find-previous-relevant ()
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
569 "Move to previous #if..., #else, or #endif, before the current line."
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
570 ; (message "hif-find-previous-relevant at %d" (point))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
571 (beginning-of-line)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
572 ; avoid infinite recursion by only going to beginning of line if match found
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
573 (if (re-search-backward hif-ifx-else-endif-regexp (point-min) t)
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
574 (beginning-of-line)))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
575
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
576
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
577 (defun hif-looking-at-ifX () ;; Should eventually see #if
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
578 (looking-at hif-ifx-regexp))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
579 (defun hif-looking-at-endif ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
580 (looking-at hif-endif-regexp))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
581 (defun hif-looking-at-else ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
582 (looking-at hif-else-regexp))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
583
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
584
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
585
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
586 (defun hif-ifdef-to-endif ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
587 "If positioned at #ifX or #else form, skip to corresponding #endif."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
588 ; (message "hif-ifdef-to-endif at %d" (point)) (sit-for 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
589 (hif-find-next-relevant)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
590 (cond ((hif-looking-at-ifX)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
591 (hif-ifdef-to-endif) ; find endif of nested if
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
592 (hif-ifdef-to-endif)) ; find outer endif or else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
593 ((hif-looking-at-else)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
594 (hif-ifdef-to-endif)) ; find endif following else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
595 ((hif-looking-at-endif)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
596 'done)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
597 (t
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
598 (error "Mismatched #ifdef #endif pair"))))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
599
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
600
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
601 (defun hif-endif-to-ifdef ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
602 "If positioned at #endif form, skip backward to corresponding #ifX."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
603 ; (message "hif-endif-to-ifdef at %d" (point))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
604 (let ((start (point)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
605 (hif-find-previous-relevant)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
606 (if (= start (point))
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
607 (error "Mismatched #ifdef #endif pair")))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
608 (cond ((hif-looking-at-endif)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
609 (hif-endif-to-ifdef) ; find beginning of nested if
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
610 (hif-endif-to-ifdef)) ; find beginning of outer if or else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
611 ((hif-looking-at-else)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
612 (hif-endif-to-ifdef))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
613 ((hif-looking-at-ifX)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
614 'done)
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
615 (t))) ; never gets here
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
616
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
617
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
618 (defun forward-ifdef (&optional arg)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
619 "Move point to beginning of line of the next ifdef-endif.
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
620 With argument, do this that many times."
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
621 (interactive "p")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
622 (or arg (setq arg 1))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
623 (if (< arg 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
624 (backward-ifdef (- arg)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
625 (while (< 0 arg)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
626 (setq arg (- arg))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
627 (let ((start (point)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
628 (if (not (hif-looking-at-ifX))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
629 (hif-find-next-relevant))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
630 (if (hif-looking-at-ifX)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
631 (hif-ifdef-to-endif)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
632 (goto-char start)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
633 (error "No following #ifdef")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
634 ))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
635
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
636
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
637 (defun backward-ifdef (&optional arg)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
638 "Move point to beginning of the previous ifdef-endif.
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
639 With argument, do this that many times."
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
640 (interactive "p")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
641 (or arg (setq arg 1))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
642 (if (< arg 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
643 (forward-ifdef (- arg)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
644 (while (< 0 arg)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
645 (setq arg (1- arg))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
646 (beginning-of-line)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
647 (let ((start (point)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
648 (if (not (hif-looking-at-endif))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
649 (hif-find-previous-relevant))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
650 (if (hif-looking-at-endif)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
651 (hif-endif-to-ifdef)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
652 (goto-char start)
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
653 (error "No previous #ifdef")))))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
654
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
655
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
656 (defun down-ifdef ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
657 "Move point to beginning of nested ifdef or else-part."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
658 (interactive)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
659 (let ((start (point)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
660 (hif-find-next-relevant)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
661 (if (or (hif-looking-at-ifX) (hif-looking-at-else))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
662 ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
663 (goto-char start)
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
664 (error "No following #ifdef"))))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
665
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
666
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
667 (defun up-ifdef ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
668 "Move point to beginning of enclosing ifdef or else-part."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
669 (interactive)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
670 (beginning-of-line)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
671 (let ((start (point)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
672 (if (not (hif-looking-at-endif))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
673 (hif-find-previous-relevant))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
674 (if (hif-looking-at-endif)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
675 (hif-endif-to-ifdef))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
676 (if (= start (point))
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
677 (error "No previous #ifdef"))))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
678
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
679 (defun next-ifdef (&optional arg)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
680 "Move to the beginning of the next #ifX, #else, or #endif.
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
681 With argument, do this that many times."
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
682 (interactive "p")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
683 (or arg (setq arg 1))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
684 (if (< arg 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
685 (previous-ifdef (- arg)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
686 (while (< 0 arg)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
687 (setq arg (1- arg))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
688 (hif-find-next-relevant)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
689 (if (eolp)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
690 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
691 (beginning-of-line)
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
692 (error "No following #ifdefs, #elses, or #endifs")))))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
693
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
694 (defun previous-ifdef (&optional arg)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
695 "Move to the beginning of the previous #ifX, #else, or #endif.
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
696 With argument, do this that many times."
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
697 (interactive "p")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
698 (or arg (setq arg 1))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
699 (if (< arg 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
700 (next-ifdef (- arg)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
701 (while (< 0 arg)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
702 (setq arg (1- arg))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
703 (let ((start (point)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
704 (hif-find-previous-relevant)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
705 (if (= start (point))
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
706 (error "No previous #ifdefs, #elses, or #endifs")))))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
707
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
708
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
709 ;===%%SF%% parsing (End) ===
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
710
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
711
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
712 ;===%%SF%% hide-ifdef-hiding (Start) ===
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
713
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
714
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
715 ;;; A range is a structure with four components:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
716 ;;; ELSE-P True if there was an else clause for the ifdef.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
717 ;;; START The start of the range. (beginning of line)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
718 ;;; ELSE The else marker (beginning of line)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
719 ;;; Only valid if ELSE-P is true.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
720 ;;; END The end of the range. (beginning of line)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
721
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
722 (defun hif-make-range (else-p start end &optional else)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
723 (list else-p start else end))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
724
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
725 (defun hif-range-else-p (range) (elt range 0))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
726 (defun hif-range-start (range) (elt range 1))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
727 (defun hif-range-else (range) (elt range 2))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
728 (defun hif-range-end (range) (elt range 3))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
729
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
730
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
731
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
732 ;;; Find-Range
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
733 ;;; The workhorse, it delimits the #if region. Reasonably simple:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
734 ;;; Skip until an #else or #endif is found, remembering positions. If
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
735 ;;; an #else was found, skip some more, looking for the true #endif.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
736
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
737 (defun hif-find-range ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
738 "Returns a Range structure describing the current #if region.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
739 Point is left unchanged."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
740 ; (message "hif-find-range at %d" (point))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
741 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
742 (beginning-of-line)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
743 (let ((start (point))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
744 (else-p nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
745 (else nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
746 (end nil))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
747 ;; Part one. Look for either #endif or #else.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
748 ;; This loop-and-a-half dedicated to E. Dijkstra.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
749 (hif-find-next-relevant)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
750 (while (hif-looking-at-ifX) ; Skip nested ifdef
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
751 (hif-ifdef-to-endif)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
752 (hif-find-next-relevant))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
753 ;; Found either a #else or an #endif.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
754 (cond ((hif-looking-at-else)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
755 (setq else-p t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
756 (setq else (point)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
757 (t
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
758 (setq end (point)) ; (save-excursion (end-of-line) (point))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
759 ))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
760 ;; If found #else, look for #endif.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
761 (if else-p
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
762 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
763 (hif-find-next-relevant)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
764 (while (hif-looking-at-ifX) ; Skip nested ifdef
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
765 (hif-ifdef-to-endif)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
766 (hif-find-next-relevant))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
767 (if (hif-looking-at-else)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
768 (error "Found two elses in a row? Broken!"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
769 (setq end (point)) ; (save-excursion (end-of-line) (point))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
770 ))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
771 (hif-make-range else-p start end else))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
772
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
773
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
774 ;;; A bit slimy.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
775 ;;; NOTE: If there's an #ifdef at the beginning of the file, we can't
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
776 ;;; hide it. There's no previous newline to replace. If we added
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
777 ;;; one, we'd throw off all the counts. Feh.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
778
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
779 (defun hif-hide-line (point)
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
780 "Hide the line containing point. Does nothing if `hide-ifdef-lines' is nil."
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
781 (if hide-ifdef-lines
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
782 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
783 (goto-char point)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
784 (let ((modp (buffer-modified-p)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
785 (unwind-protect
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
786 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
787 (beginning-of-line)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
788 (if (not (= (point) 1))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
789 (hide-ifdef-region (1- (point)) (point))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
790 (set-buffer-modified-p modp))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
791 ))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
792 ))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
793
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
794
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
795 ;;; Hif-Possibly-Hide
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
796 ;;; There are four cases. The #ifX expression is "taken" if it
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
797 ;;; the hide-ifdef-evaluator returns T. Presumably, this means the code
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
798 ;;; inside the #ifdef would be included when the program was
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
799 ;;; compiled.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
800 ;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
801 ;;; Case 1: #ifX taken, and there's an #else.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
802 ;;; The #else part must be hidden. The #if (then) part must be
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
803 ;;; processed for nested #ifX's.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
804 ;;; Case 2: #ifX taken, and there's no #else.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
805 ;;; The #if part must be processed for nested #ifX's.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
806 ;;; Case 3: #ifX not taken, and there's an #else.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
807 ;;; The #if part must be hidden. The #else part must be processed
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
808 ;;; for nested #ifs.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
809 ;;; Case 4: #ifX not taken, and there's no #else.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
810 ;;; The #ifX part must be hidden.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
811 ;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
812 ;;; Further processing is done by narrowing to the relevant region
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
813 ;;; and just recursively calling hide-ifdef-guts.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
814 ;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
815 ;;; When hif-possibly-hide returns, point is at the end of the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
816 ;;; possibly-hidden range.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
817
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
818 (defun hif-recurse-on (start end)
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
819 "Call `hide-ifdef-guts' after narrowing to end of START line and END line."
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
820 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
821 (save-restriction
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
822 (goto-char start)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
823 (end-of-line)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
824 (narrow-to-region (point) end)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
825 (hide-ifdef-guts))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
826
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
827 (defun hif-possibly-hide ()
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
828 "Called at #ifX expression, this hides those parts that should be hidden.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
829 It uses the judgement of `hide-ifdef-evaluator'."
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
830 ; (message "hif-possibly-hide") (sit-for 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
831 (let ((test (hif-canonicalize))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
832 (range (hif-find-range)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
833 ; (message "test = %s" test) (sit-for 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
834
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
835 (hif-hide-line (hif-range-end range))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
836 (if (funcall hide-ifdef-evaluator test)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
837 (cond ((hif-range-else-p range) ; case 1
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
838 (hif-hide-line (hif-range-else range))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
839 (hide-ifdef-region (hif-range-else range)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
840 (1- (hif-range-end range)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
841 (hif-recurse-on (hif-range-start range)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
842 (hif-range-else range)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
843 (t ; case 2
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
844 (hif-recurse-on (hif-range-start range)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
845 (hif-range-end range))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
846 (cond ((hif-range-else-p range) ; case 3
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
847 (hif-hide-line (hif-range-else range))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
848 (hide-ifdef-region (hif-range-start range)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
849 (1- (hif-range-else range)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
850 (hif-recurse-on (hif-range-else range)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
851 (hif-range-end range)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
852 (t ; case 4
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
853 (hide-ifdef-region (point)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
854 (1- (hif-range-end range))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
855 ))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
856 (hif-hide-line (hif-range-start range)) ; Always hide start.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
857 (goto-char (hif-range-end range))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
858 (end-of-line)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
859 ))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
860
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
861
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
862
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
863 (defun hide-ifdef-guts ()
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
864 "Does most of the work of `hide-ifdefs'.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
865 It does not do the work that's pointless to redo on a recursive entry."
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
866 ; (message "hide-ifdef-guts")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
867 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
868 (goto-char (point-min))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
869 (while (hif-find-any-ifX)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
870 (hif-possibly-hide))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
871
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
872 ;===%%SF%% hide-ifdef-hiding (End) ===
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
873
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
874
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
875 ;===%%SF%% exports (Start) ===
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
876
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
877 ;;;###autoload
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
878 (defvar hide-ifdef-initially nil
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
879 "*Non-nil means call `hide-ifdefs' when Hide-Ifdef mode is first activated.")
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
880
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
881 ;;;###autoload
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
882 (defvar hide-ifdef-read-only nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
883 "*Set to non-nil if you want buffer to be read-only while hiding text.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
884
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
885 (defvar hif-outside-read-only nil
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
886 "Internal variable. Saves the value of `buffer-read-only' while hiding.")
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
887
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
888 ;;;###autoload
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
889 (defvar hide-ifdef-lines nil
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
890 "*Non-nil means hide the #ifX, #else, and #endif lines.")
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
891
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
892 (defun hide-ifdef-toggle-read-only ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
893 "Toggle hide-ifdef-read-only."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
894 (interactive)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
895 (setq hide-ifdef-read-only (not hide-ifdef-read-only))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
896 (message "Hide-Read-Only %s"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
897 (if hide-ifdef-read-only "ON" "OFF"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
898 (if hide-ifdef-hiding
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
899 (setq buffer-read-only (or hide-ifdef-read-only hif-outside-read-only)))
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
900 ;; XEmacs change
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
901 (redraw-modeline))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
902
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
903 (defun hide-ifdef-toggle-outside-read-only ()
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
904 "Replacement for `toggle-read-only' within Hide-Ifdef mode."
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
905 (interactive)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
906 (setq hif-outside-read-only (not hif-outside-read-only))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
907 (message "Read only %s"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
908 (if hif-outside-read-only "ON" "OFF"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
909 (setq buffer-read-only
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
910 (or (and hide-ifdef-hiding hide-ifdef-read-only)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
911 hif-outside-read-only)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
912 )
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
913 ;; XEmacs change
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
914 (redraw-modeline))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
915
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
916
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
917 (defun hide-ifdef-define (var)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
918 "Define a VAR so that #ifdef VAR would be included."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
919 (interactive "SDefine what? ")
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
920 (hif-set-var var 1)
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
921 (if hide-ifdef-hiding (hide-ifdefs)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
922
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
923 (defun hide-ifdef-undef (var)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
924 "Undefine a VAR so that #ifdef VAR would not be included."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
925 (interactive "SUndefine what? ")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
926 (hif-set-var var nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
927 (if hide-ifdef-hiding (hide-ifdefs)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
928
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
929
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
930 (defun hide-ifdefs (&optional nomsg)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
931 "Hide the contents of some #ifdefs.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
932 Assume that defined symbols have been added to `hide-ifdef-env'.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
933 The text hidden is the text that would not be included by the C
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
934 preprocessor if it were given the file with those symbols defined.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
935
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
936 Turn off hiding by calling `show-ifdefs'."
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
937
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
938 (interactive)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
939 (message "Hiding...")
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
940 (setq hif-outside-read-only buffer-read-only)
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
941 (if (not hide-ifdef-mode)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
942 (hide-ifdef-mode 1)) ; turn on hide-ifdef-mode
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
943 (if hide-ifdef-hiding
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
944 (show-ifdefs)) ; Otherwise, deep confusion.
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
945 (let ((inhibit-read-only t))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
946 (setq selective-display t)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
947 (setq hide-ifdef-hiding t)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
948 (hide-ifdef-guts))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
949 (setq buffer-read-only (or hide-ifdef-read-only hif-outside-read-only))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
950 (or nomsg
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
951 (message "Hiding done")))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
952
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
953
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
954 (defun show-ifdefs ()
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
955 "Cancel the effects of `hide-ifdef': show the contents of all #ifdefs."
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
956 (interactive)
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
957 (setq buffer-read-only hif-outside-read-only)
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
958 (setq selective-display nil) ; defaults
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
959 (let ((inhibit-read-only t))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
960 (hif-show-all))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
961 (setq hide-ifdef-hiding nil))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
962
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
963
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
964 (defun hif-find-ifdef-block ()
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
965 "Utility for hide and show `ifdef-block'.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
966 Set top and bottom of ifdef block."
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
967 (let (max-bottom)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
968 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
969 (beginning-of-line)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
970 (if (not (or (hif-looking-at-else) (hif-looking-at-ifX)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
971 (up-ifdef))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
972 (setq top (point))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
973 (hif-ifdef-to-endif)
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
974 (setq max-bottom (1- (point))))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
975 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
976 (beginning-of-line)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
977 (if (not (hif-looking-at-endif))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
978 (hif-find-next-relevant))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
979 (while (hif-looking-at-ifX)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
980 (hif-ifdef-to-endif)
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
981 (hif-find-next-relevant))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
982 (setq bottom (min max-bottom (1- (point)))))))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
983
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
984
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
985 (defun hide-ifdef-block ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
986 "Hide the ifdef block (true or false part) enclosing or before the cursor."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
987 (interactive)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
988 (if (not hide-ifdef-mode)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
989 (hide-ifdef-mode 1))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
990 (setq selective-display t)
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
991 (let (top bottom (inhibit-read-only t))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
992 (hif-find-ifdef-block) ; set top and bottom - dynamic scoping
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
993 (hide-ifdef-region top bottom)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
994 (if hide-ifdef-lines
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
995 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
996 (hif-hide-line top)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
997 (hif-hide-line (1+ bottom))))
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
998 (setq hide-ifdef-hiding t))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
999 (setq buffer-read-only (or hide-ifdef-read-only hif-outside-read-only)))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1000
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1001
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1002 (defun show-ifdef-block ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1003 "Show the ifdef block (true or false part) enclosing or before the cursor."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1004 (interactive)
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
1005 (let ((inhibit-read-only t))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1006 (if hide-ifdef-lines
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1007 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1008 (beginning-of-line)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1009 (hif-show-ifdef-region (1- (point)) (progn (end-of-line) (point))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1010
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1011 (let (top bottom)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1012 (hif-find-ifdef-block)
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
1013 (hif-show-ifdef-region (1- top) bottom)))))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1014
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1015
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
1016 ;;; definition alist support
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1017
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1018 (defvar hide-ifdef-define-alist nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1019 "A global assoc list of pre-defined symbol lists")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1020
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1021 (defun hif-compress-define-list (env)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1022 "Compress the define list ENV into a list of defined symbols only."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1023 (let ((defs (mapcar '(lambda (arg)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1024 (if (hif-lookup (car arg)) (car arg)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1025 env))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1026 (new-defs nil))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1027 (while defs
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1028 (if (car defs)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1029 (setq new-defs (cons (car defs) new-defs)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1030 (setq defs (cdr defs)))
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
1031 new-defs))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1032
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1033 (defun hide-ifdef-set-define-alist (name)
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
1034 "Set the association for NAME to `hide-ifdef-env'."
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1035 (interactive "SSet define list: ")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1036 (setq hide-ifdef-define-alist
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1037 (cons (cons name (hif-compress-define-list hide-ifdef-env))
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
1038 hide-ifdef-define-alist)))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1039
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1040 (defun hide-ifdef-use-define-alist (name)
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
1041 "Set `hide-ifdef-env' to the define list specified by NAME."
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1042 (interactive "SUse define list: ")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1043 (let ((define-list (assoc name hide-ifdef-define-alist)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1044 (if define-list
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1045 (setq hide-ifdef-env
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1046 (mapcar '(lambda (arg) (cons arg t))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1047 (cdr define-list)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1048 (error "No define list for %s" name))
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
1049 (if hide-ifdef-hiding (hide-ifdefs))))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1050
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
1051 (provide 'hideif)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
1052
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
1053 ;;; hideif.el ends here