aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStefan Monnier2001-11-28 04:12:47 +0000
committerStefan Monnier2001-11-28 04:12:47 +0000
commit067d92a1b8fd31c464bbff20a16ccee0fb56a95f (patch)
tree24466c68fbe29294c108bd2ec59d2d7a35178851
parentaa3b4ded23a702a250ca209f603edd92d2f395a5 (diff)
downloademacs-067d92a1b8fd31c464bbff20a16ccee0fb56a95f.tar.gz
emacs-067d92a1b8fd31c464bbff20a16ccee0fb56a95f.zip
Misc cleanup.
(hide-ifdef-mode-submap): Put the def in the defvar. Use substitute-key-definition. (hide-ifdef-mode): Use define-minor-mode. (hif-outline-flag-region): Remove. (hif-show-all): Define in terms of hif-show-ifdef-region. (hif-after-revert-function): Rename from hif-before-revert-function. (hide-ifdef-region, hif-show-ifdef-region): Use overlays. (hif-tokenize): Use with-syntax-table. (hif-make-range): Use `else' also for `else-p'. (hif-range-else-p): Remove. (hif-find-range): Simplify. (hif-hide-line): Don't bother preserving buffer-modified-p. (hide-ifdefs, show-ifdefs, hide-ifdef-block, show-ifdef-block): Don't use selective-display and inhibit-read-only.
-rw-r--r--lisp/progmodes/hideif.el625
1 files changed, 258 insertions, 367 deletions
diff --git a/lisp/progmodes/hideif.el b/lisp/progmodes/hideif.el
index 37f95f25041..2e21af69340 100644
--- a/lisp/progmodes/hideif.el
+++ b/lisp/progmodes/hideif.el
@@ -43,8 +43,7 @@
43;; The hidden code is marked by ellipses (...). Be 43;; The hidden code is marked by ellipses (...). Be
44;; cautious when editing near ellipses, since the hidden text is 44;; cautious when editing near ellipses, since the hidden text is
45;; still in the buffer, and you can move the point into it and modify 45;; still in the buffer, and you can move the point into it and modify
46;; text unawares. If you don't want to see the ellipses, set 46;; text unawares.
47;; selective-display-ellipses to nil. But this can be dangerous.
48;; You can make your buffer read-only while hide-ifdef-hiding by setting 47;; You can make your buffer read-only while hide-ifdef-hiding by setting
49;; hide-ifdef-read-only to a non-nil value. You can toggle this 48;; hide-ifdef-read-only to a non-nil value. You can toggle this
50;; variable with hide-ifdef-toggle-read-only (C-c @ C-q). 49;; variable with hide-ifdef-toggle-read-only (C-c @ C-q).
@@ -66,15 +65,13 @@
66;; up a list of symbols that may be used by hide-ifdefs as in the 65;; up a list of symbols that may be used by hide-ifdefs as in the
67;; following example: 66;; following example:
68;; 67;;
69;; (setq hide-ifdef-mode-hook 68;; (add-hook 'hide-ifdef-mode-hook
70;; (lambda () 69;; (lambda ()
71;; (if (not hide-ifdef-define-alist) 70;; (unless hide-ifdef-define-alist
72;; (setq hide-ifdef-define-alist 71;; (setq hide-ifdef-define-alist
73;; '((list1 ONE TWO) 72;; '((list1 ONE TWO)
74;; (list2 TWO THREE) 73;; (list2 TWO THREE))))
75;; ))) 74;; (hide-ifdef-use-define-alist 'list2))) ; use list2 by default
76;; (hide-ifdef-use-define-alist 'list2) ; use list2 by default
77;; ))
78;; 75;;
79;; You can call hide-ifdef-use-define-alist (C-c @ U) at any time to specify 76;; You can call hide-ifdef-use-define-alist (C-c @ U) at any time to specify
80;; another list to use. 77;; another list to use.
@@ -116,77 +113,52 @@
116 "Hide selected code within `ifdef'." 113 "Hide selected code within `ifdef'."
117 :group 'c) 114 :group 'c)
118 115
119(defvar hide-ifdef-mode-submap nil 116(defvar hide-ifdef-mode-submap
120 "Keymap used with Hide-Ifdef mode.") 117 ;; Set up the submap that goes after the prefix key.
121 118 (let ((map (make-sparse-keymap)))
122(defvar hide-ifdef-mode-map nil 119 (define-key map "d" 'hide-ifdef-define)
123 "Keymap used with Hide-Ifdef mode.") 120 (define-key map "u" 'hide-ifdef-undef)
121 (define-key map "D" 'hide-ifdef-set-define-alist)
122 (define-key map "U" 'hide-ifdef-use-define-alist)
123
124 (define-key map "h" 'hide-ifdefs)
125 (define-key map "s" 'show-ifdefs)
126 (define-key map "\C-d" 'hide-ifdef-block)
127 (define-key map "\C-s" 'show-ifdef-block)
128
129 (define-key map "\C-q" 'hide-ifdef-toggle-read-only)
130 (substitute-key-definition
131 'toggle-read-only 'hide-ifdef-toggle-outside-read-only map)
132 map)
133 "Keymap used by `hide-ifdef-mode' under `hide-ifdef-mode-prefix-key'.")
124 134
125(defconst hide-ifdef-mode-prefix-key "\C-c@" 135(defconst hide-ifdef-mode-prefix-key "\C-c@"
126 "Prefix key for all Hide-Ifdef mode commands.") 136 "Prefix key for all Hide-Ifdef mode commands.")
127 137
128;; Set up the submap that goes after the prefix key. 138(defvar hide-ifdef-mode-map
129(if hide-ifdef-mode-submap 139 ;; Set up the mode's main map, which leads via the prefix key to the submap.
130 () ; Don't redefine it. 140 (let ((map (make-sparse-keymap)))
131 (setq hide-ifdef-mode-submap (make-sparse-keymap)) 141 (define-key map hide-ifdef-mode-prefix-key hide-ifdef-mode-submap)
132 (define-key hide-ifdef-mode-submap "d" 'hide-ifdef-define) 142 map)
133 (define-key hide-ifdef-mode-submap "u" 'hide-ifdef-undef) 143 "Keymap used with `hide-ifdef-mode'.")
134 (define-key hide-ifdef-mode-submap "D" 'hide-ifdef-set-define-alist)
135 (define-key hide-ifdef-mode-submap "U" 'hide-ifdef-use-define-alist)
136
137 (define-key hide-ifdef-mode-submap "h" 'hide-ifdefs)
138 (define-key hide-ifdef-mode-submap "s" 'show-ifdefs)
139 (define-key hide-ifdef-mode-submap "\C-d" 'hide-ifdef-block)
140 (define-key hide-ifdef-mode-submap "\C-s" 'show-ifdef-block)
141
142 (define-key hide-ifdef-mode-submap "\C-q" 'hide-ifdef-toggle-read-only)
143 (let ((where (where-is-internal 'toggle-read-only '(keymap) t)))
144 (if where
145 (define-key hide-ifdef-mode-submap
146 where
147 'hide-ifdef-toggle-outside-read-only)))
148 )
149
150;; Set up the mode's main map, which leads via the prefix key to the submap.
151(if hide-ifdef-mode-map
152 ()
153 (setq hide-ifdef-mode-map (make-sparse-keymap))
154 (define-key hide-ifdef-mode-map hide-ifdef-mode-prefix-key
155 hide-ifdef-mode-submap))
156
157;; Autoload for the benefit of `make-mode-line-mouse-sensitive'.
158;;;###autoload
159(defvar hide-ifdef-mode nil
160 "Non-nil when hide-ifdef-mode is activated.")
161 144
162(defvar hide-ifdef-hiding nil 145(defvar hide-ifdef-hiding nil
163 "Non-nil when text may be hidden.") 146 "Non-nil when text may be hidden.")
164 147
165;; Arrange to use the mode's map when the mode is enabled.
166(or (assq 'hide-ifdef-mode minor-mode-map-alist)
167 (setq minor-mode-map-alist
168 (cons (cons 'hide-ifdef-mode hide-ifdef-mode-map)
169 minor-mode-map-alist)))
170
171(or (assq 'hide-ifdef-hiding minor-mode-alist) 148(or (assq 'hide-ifdef-hiding minor-mode-alist)
172 (setq minor-mode-alist 149 (setq minor-mode-alist
173 (cons '(hide-ifdef-hiding " Hiding") 150 (cons '(hide-ifdef-hiding " Hiding")
174 minor-mode-alist))) 151 minor-mode-alist)))
175 152
176(or (assq 'hide-ifdef-mode minor-mode-alist)
177 (setq minor-mode-alist
178 (cons '(hide-ifdef-mode " Ifdef")
179 minor-mode-alist)))
180
181;; fix c-mode syntax table so we can recognize whole symbols. 153;; fix c-mode syntax table so we can recognize whole symbols.
182(defvar hide-ifdef-syntax-table 154(defvar hide-ifdef-syntax-table
183 (copy-syntax-table c-mode-syntax-table) 155 (let ((st (copy-syntax-table c-mode-syntax-table)))
156 (modify-syntax-entry ?_ "w" st)
157 (modify-syntax-entry ?& "." st)
158 (modify-syntax-entry ?\| "." st)
159 st)
184 "Syntax table used for tokenizing #if expressions.") 160 "Syntax table used for tokenizing #if expressions.")
185 161
186(modify-syntax-entry ?_ "w" hide-ifdef-syntax-table)
187(modify-syntax-entry ?& "." hide-ifdef-syntax-table)
188(modify-syntax-entry ?\| "." hide-ifdef-syntax-table)
189
190(defvar hide-ifdef-env nil 162(defvar hide-ifdef-env nil
191 "An alist of defined symbols and their values.") 163 "An alist of defined symbols and their values.")
192 164
@@ -194,113 +166,86 @@
194 "Internal variable. Saves the value of `buffer-read-only' while hiding.") 166 "Internal variable. Saves the value of `buffer-read-only' while hiding.")
195 167
196;;;###autoload 168;;;###autoload
197(defun hide-ifdef-mode (arg) 169(define-minor-mode hide-ifdef-mode
198 "Toggle Hide-Ifdef mode. This is a minor mode, albeit a large one. 170 "Toggle Hide-Ifdef mode. This is a minor mode, albeit a large one.
199With ARG, turn Hide-Ifdef mode on if arg is positive, off otherwise. 171With ARG, turn Hide-Ifdef mode on if arg is positive, off otherwise.
200In Hide-Ifdef mode, code within #ifdef constructs that the C preprocessor 172In Hide-Ifdef mode, code within #ifdef constructs that the C preprocessor
201would eliminate may be hidden from view. Several variables affect 173would eliminate may be hidden from view. Several variables affect
202how the hiding is done: 174how the hiding is done:
203 175
204hide-ifdef-env 176`hide-ifdef-env'
205 An association list of defined and undefined symbols for the 177 An association list of defined and undefined symbols for the
206 current buffer. Initially, the global value of `hide-ifdef-env' 178 current buffer. Initially, the global value of `hide-ifdef-env'
207 is used. 179 is used.
208 180
209hide-ifdef-define-alist 181`hide-ifdef-define-alist'
210 An association list of defined symbol lists. 182 An association list of defined symbol lists.
211 Use `hide-ifdef-set-define-alist' to save the current `hide-ifdef-env' 183 Use `hide-ifdef-set-define-alist' to save the current `hide-ifdef-env'
212 and `hide-ifdef-use-define-alist' to set the current `hide-ifdef-env' 184 and `hide-ifdef-use-define-alist' to set the current `hide-ifdef-env'
213 from one of the lists in `hide-ifdef-define-alist'. 185 from one of the lists in `hide-ifdef-define-alist'.
214 186
215hide-ifdef-lines 187`hide-ifdef-lines'
216 Set to non-nil to not show #if, #ifdef, #ifndef, #else, and 188 Set to non-nil to not show #if, #ifdef, #ifndef, #else, and
217 #endif lines when hiding. 189 #endif lines when hiding.
218 190
219hide-ifdef-initially 191`hide-ifdef-initially'
220 Indicates whether `hide-ifdefs' should be called when Hide-Ifdef mode 192 Indicates whether `hide-ifdefs' should be called when Hide-Ifdef mode
221 is activated. 193 is activated.
222 194
223hide-ifdef-read-only 195`hide-ifdef-read-only'
224 Set to non-nil if you want to make buffers read only while hiding. 196 Set to non-nil if you want to make buffers read only while hiding.
225 After `show-ifdefs', read-only status is restored to previous value. 197 After `show-ifdefs', read-only status is restored to previous value.
226 198
227\\{hide-ifdef-mode-map}" 199\\{hide-ifdef-mode-map}"
228 200 nil " Ifdef" nil
229 (interactive "P")
230 (make-local-variable 'hide-ifdef-mode)
231 (setq hide-ifdef-mode
232 (if (null arg)
233 (not hide-ifdef-mode)
234 (> (prefix-numeric-value arg) 0)))
235
236 (force-mode-line-update)
237
238 (if hide-ifdef-mode 201 (if hide-ifdef-mode
239 (progn 202 (progn
240 ; inherit global values 203 ;; inherit global values
241 (make-local-variable 'hide-ifdef-env) 204 (set (make-local-variable 'hide-ifdef-env)
242 (setq hide-ifdef-env (default-value 'hide-ifdef-env)) 205 (default-value 'hide-ifdef-env))
243 206 (set (make-local-variable 'hide-ifdef-hiding)
244 (make-local-variable 'hide-ifdef-hiding) 207 (default-value 'hide-ifdef-hiding))
245 (setq hide-ifdef-hiding (default-value 'hide-ifdef-hiding)) 208 (set (make-local-variable 'hif-outside-read-only) buffer-read-only)
246
247 (make-local-variable 'hif-outside-read-only)
248 (setq hif-outside-read-only buffer-read-only)
249 209
250 (run-hooks 'hide-ifdef-mode-hook) 210 (add-to-invisibility-spec '(hide-ifdef . t))
251 211
252 (if hide-ifdef-initially 212 (if hide-ifdef-initially
253 (hide-ifdefs) 213 (hide-ifdefs)
254 (show-ifdefs)) 214 (show-ifdefs)))
255 (message "Enter Hide-Ifdef mode") 215 ;; else end hide-ifdef-mode
256 )
257 ; else end hide-ifdef-mode
258 (if hide-ifdef-hiding 216 (if hide-ifdef-hiding
259 (show-ifdefs)) 217 (show-ifdefs))))
260 (message "Exit Hide-Ifdef mode")
261 ))
262 218
263 219
264;; from outline.el with docstring fixed.
265(defun hif-outline-flag-region (from to flag)
266 "Hides or shows lines from FROM to TO, according to FLAG.
267If FLAG is \\n (newline character) then text is shown, while if FLAG is \\^M
268\(control-M) the text is hidden."
269 (let ((modp (buffer-modified-p)))
270 (unwind-protect (progn
271 (subst-char-in-region from to
272 (if (= flag ?\n) ?\^M ?\n)
273 flag t) )
274 (set-buffer-modified-p modp))
275 ))
276
277(defun hif-show-all () 220(defun hif-show-all ()
278 "Show all of the text in the current buffer." 221 "Show all of the text in the current buffer."
279 (interactive) 222 (interactive)
280 (hif-outline-flag-region (point-min) (point-max) ?\n)) 223 (hif-show-ifdef-region (point-min) (point-max)))
281 224
282;; By putting this on after-revert-hook, we arrange that it only 225;; By putting this on after-revert-hook, we arrange that it only
283;; does anything when revert-buffer avoids turning off the mode. 226;; does anything when revert-buffer avoids turning off the mode.
284;; (That can happen in VC.) 227;; (That can happen in VC.)
285(defun hif-before-revert-function () 228(defun hif-after-revert-function ()
286 (and hide-ifdef-mode hide-ifdef-hiding 229 (and hide-ifdef-mode hide-ifdef-hiding
287 (hide-ifdefs t))) 230 (hide-ifdefs t)))
288(add-hook 'after-revert-hook 'hif-before-revert-function) 231(add-hook 'after-revert-hook 'hif-after-revert-function)
289 232
290(defun hide-ifdef-region (start end) 233(defun hide-ifdef-region (start end)
291 "START is the start of a #if or #else form. END is the ending part. 234 "START is the start of a #if or #else form. END is the ending part.
292Everything including these lines is made invisible." 235Everything including these lines is made invisible."
293 (hif-outline-flag-region start end ?\^M) 236 (save-excursion
294 ) 237 (goto-char start) (end-of-line) (setq start (point))
238 (goto-char end) (end-of-line) (setq end (point))
239 (remove-overlays start end 'invisible 'hide-ifdef)
240 (let ((o (make-overlay start end)))
241 (overlay-put o 'invisible 'hide-ifdef))))
295 242
296(defun hif-show-ifdef-region (start end) 243(defun hif-show-ifdef-region (start end)
297 "Everything between START and END is made visible." 244 "Everything between START and END is made visible."
298 (hif-outline-flag-region start end ?\n) 245 (remove-overlays start end 'invisible 'hide-ifdef))
299 )
300
301 246
302 247
303;===%%SF%% evaluation (Start) === 248;;===%%SF%% evaluation (Start) ===
304 249
305;; It is not useful to set this to anything but `eval'. 250;; It is not useful to set this to anything but `eval'.
306;; In fact, the variable might as well be eliminated. 251;; In fact, the variable might as well be eliminated.
@@ -319,7 +264,7 @@ that form should be displayed.")
319 264
320 265
321(defun hif-lookup (var) 266(defun hif-lookup (var)
322; (message "hif-lookup %s" var) 267 ;; (message "hif-lookup %s" var)
323 (let ((val (assoc var hide-ifdef-env))) 268 (let ((val (assoc var hide-ifdef-env)))
324 (if val 269 (if val
325 (cdr val) 270 (cdr val)
@@ -327,18 +272,18 @@ that form should be displayed.")
327 272
328(defun hif-defined (var) 273(defun hif-defined (var)
329 (hif-lookup var) 274 (hif-lookup var)
330 ; when #if expressions are fully supported, defined result should be 1 275 ;; when #if expressions are fully supported, defined result should be 1
331 ; (if (assoc var hide-ifdef-env) 276 ;; (if (assoc var hide-ifdef-env)
332 ; 1 277 ;; 1
333 ; nil) 278 ;; nil)
334) 279 )
335 280
336 281
337;===%%SF%% evaluation (End) === 282;;===%%SF%% evaluation (End) ===
338 283
339 284
340 285
341;===%%SF%% parsing (Start) === 286;;===%%SF%% parsing (Start) ===
342;;; The code that understands what ifs and ifdef in files look like. 287;;; The code that understands what ifs and ifdef in files look like.
343 288
344(defconst hif-cpp-prefix "\\(^\\|\r\\)[ \t]*#[ \t]*") 289(defconst hif-cpp-prefix "\\(^\\|\r\\)[ \t]*#[ \t]*")
@@ -349,83 +294,76 @@ that form should be displayed.")
349(defconst hif-ifx-else-endif-regexp 294(defconst hif-ifx-else-endif-regexp
350 (concat hif-ifx-regexp "\\|" hif-else-regexp "\\|" hif-endif-regexp)) 295 (concat hif-ifx-regexp "\\|" hif-else-regexp "\\|" hif-endif-regexp))
351 296
352; Used to store the current token and the whole token list during parsing. 297;; Used to store the current token and the whole token list during parsing.
353; Only bound dynamically. 298;; Only bound dynamically.
354(defvar hif-token) 299(defvar hif-token)
355(defvar hif-token-list) 300(defvar hif-token-list)
356 301
357(defun hif-infix-to-prefix (token-list) 302(defun hif-infix-to-prefix (token-list)
358 "Convert list of tokens in infix into prefix list" 303 "Convert list of tokens in infix into prefix list."
359 ; (message "hif-infix-to-prefix: %s" token-list) 304 ;; (message "hif-infix-to-prefix: %s" token-list)
360 (if (= 1 (length token-list)) 305 (if (= 1 (length token-list))
361 `(hif-lookup (quote ,(car token-list))) 306 `(hif-lookup (quote ,(car token-list)))
362 (hif-parse-if-exp token-list)) 307 (hif-parse-if-exp token-list)))
363 )
364 308
365; pattern to match initial identifier, !, &&, ||, (, or ). 309;; pattern to match initial identifier, !, &&, ||, (, or ).
366; Added ==, + and -: garyo@avs.com 8/9/94 310;; Added ==, + and -: garyo@avs.com 8/9/94
367(defconst hif-token-regexp "^\\(&&\\|||\\|[!=]=\\|!\\|[()+-]\\|[<>]=?\\|\\w+\\)") 311(defconst hif-token-regexp "^\\(&&\\|||\\|[!=]=\\|!\\|[()+-]\\|[<>]=?\\|\\w+\\)")
368(defconst hif-end-of-comment "\\*/") 312(defconst hif-end-of-comment "\\*/")
369 313
370 314
371(defun hif-tokenize (expr-string) 315(defun hif-tokenize (expr-string)
372 "Separate string into a list of tokens" 316 "Separate string into a list of tokens."
373 (let ((token-list nil) 317 (let ((token-list nil)
374 (expr-start 0) 318 (expr-start 0)
375 (expr-length (length expr-string)) 319 (expr-length (length expr-string)))
376 (current-syntax-table (syntax-table))) 320 (with-syntax-table hide-ifdef-syntax-table
377 (unwind-protect 321 (while (< expr-start expr-length)
378 (progn 322 ;; (message "expr-start = %d" expr-start) (sit-for 1)
379 (set-syntax-table hide-ifdef-syntax-table) 323 (cond
380 (while (< expr-start expr-length) 324 ((string-match "^[ \t]+" expr-string expr-start)
381; (message "expr-start = %d" expr-start) (sit-for 1) 325 ;; skip whitespace
382 (cond 326 (setq expr-start (match-end 0))
383 ((string-match "^[ \t]+" expr-string expr-start) 327 ;; stick newline in string so ^ matches on the next string-match
384 ;; skip whitespace 328 (aset expr-string (1- expr-start) ?\n))
385 (setq expr-start (match-end 0)) 329
386 ;; stick newline in string so ^ matches on the next string-match 330 ((string-match "^/\\*" expr-string expr-start)
387 (aset expr-string (1- expr-start) ?\n)) 331 (setq expr-start (match-end 0))
388 332 (aset expr-string (1- expr-start) ?\n)
389 ((string-match "^/\\*" expr-string expr-start) 333 (or
390 (setq expr-start (match-end 0)) 334 (string-match hif-end-of-comment
391 (aset expr-string (1- expr-start) ?\n) 335 expr-string expr-start) ; eat comment
392 (or 336 (string-match "$" expr-string expr-start)) ; multi-line comment
393 (string-match hif-end-of-comment 337 (setq expr-start (match-end 0))
394 expr-string expr-start) ; eat comment 338 (aset expr-string (1- expr-start) ?\n))
395 (string-match "$" expr-string expr-start)) ; multi-line comment 339
396 (setq expr-start (match-end 0)) 340 ((string-match "^//" expr-string expr-start)
397 (aset expr-string (1- expr-start) ?\n)) 341 (string-match "$" expr-string expr-start)
398 342 (setq expr-start (match-end 0)))
399 ((string-match "^//" expr-string expr-start) 343
400 (string-match "$" expr-string expr-start) 344 ((string-match hif-token-regexp expr-string expr-start)
401 (setq expr-start (match-end 0))) 345 (let ((token (substring expr-string expr-start (match-end 0))))
402 346 (setq expr-start (match-end 0))
403 ((string-match hif-token-regexp expr-string expr-start) 347 (aset expr-string (1- expr-start) ?\n)
404 (let ((token (substring expr-string expr-start (match-end 0)))) 348 ;; (message "token: %s" token) (sit-for 1)
405 (setq expr-start (match-end 0)) 349 (push (cond
406 (aset expr-string (1- expr-start) ?\n) 350 ((string-equal token "||") 'or)
407; (message "token: %s" token) (sit-for 1) 351 ((string-equal token "&&") 'and)
408 (setq token-list 352 ((string-equal token "==") 'equal)
409 (cons 353 ((string-equal token "!=") 'hif-notequal)
410 (cond 354 ((string-equal token "!") 'not)
411 ((string-equal token "||") 'or) 355 ((string-equal token "defined") 'hif-defined)
412 ((string-equal token "&&") 'and) 356 ((string-equal token "(") 'lparen)
413 ((string-equal token "==") 'equal) 357 ((string-equal token ")") 'rparen)
414 ((string-equal token "!=") 'hif-notequal) 358 ((string-equal token ">") 'hif-greater)
415 ((string-equal token "!") 'not) 359 ((string-equal token "<") 'hif-less)
416 ((string-equal token "defined") 'hif-defined) 360 ((string-equal token ">=") 'hif-greater-equal)
417 ((string-equal token "(") 'lparen) 361 ((string-equal token "<=") 'hif-less-equal)
418 ((string-equal token ")") 'rparen) 362 ((string-equal token "+") 'hif-plus)
419 ((string-equal token ">") 'hif-greater) 363 ((string-equal token "-") 'hif-minus)
420 ((string-equal token "<") 'hif-less) 364 (t (intern token)))
421 ((string-equal token ">=") 'hif-greater-equal) 365 token-list)))
422 ((string-equal token "<=") 'hif-less-equal) 366 (t (error "Bad #if expression: %s" expr-string)))))
423 ((string-equal token "+") 'hif-plus)
424 ((string-equal token "-") 'hif-minus)
425 (t (intern token)))
426 token-list))))
427 (t (error "Bad #if expression: %s" expr-string)))))
428 (set-syntax-table current-syntax-table))
429 (nreverse token-list))) 367 (nreverse token-list)))
430 368
431;;;----------------------------------------------------------------- 369;;;-----------------------------------------------------------------
@@ -510,27 +448,22 @@ that form should be displayed.")
510 (if (memq hif-token '(or and not hif-defined lparen rparen)) 448 (if (memq hif-token '(or and not hif-defined lparen rparen))
511 (error "Error: unexpected token: %s" hif-token)) 449 (error "Error: unexpected token: %s" hif-token))
512 (hif-nexttoken) 450 (hif-nexttoken)
513 (if (not (eq hif-token 'rparen)) 451 (unless (eq hif-token 'rparen)
514 (error "Error: expected \")\" after identifier")) 452 (error "Error: expected \")\" after identifier"))
515 (hif-nexttoken) 453 (hif-nexttoken)
516 `(hif-defined (quote ,ident)) 454 `(hif-defined (quote ,ident))))
517 ))
518 455
519 (t ; identifier 456 (t ; identifier
520 (let ((ident hif-token)) 457 (let ((ident hif-token))
521 (if (memq ident '(or and)) 458 (if (memq ident '(or and))
522 (error "Error: missing identifier")) 459 (error "Error: missing identifier"))
523 (hif-nexttoken) 460 (hif-nexttoken)
524 `(hif-lookup (quote ,ident)) 461 `(hif-lookup (quote ,ident))))))
525 ))
526 ))
527 462
528(defun hif-mathify (val) 463(defun hif-mathify (val)
529 "Treat VAL as a number: if it's t or nil, use 1 or 0." 464 "Treat VAL as a number: if it's t or nil, use 1 or 0."
530 (cond ((eq val t) 465 (cond ((eq val t) 1)
531 1) 466 ((null val) 0)
532 ((null val)
533 0)
534 (t val))) 467 (t val)))
535 468
536(defun hif-plus (a b) 469(defun hif-plus (a b)
@@ -558,7 +491,7 @@ that form should be displayed.")
558 491
559 492
560(defun hif-canonicalize () 493(defun hif-canonicalize ()
561 "When at beginning of #ifX, returns a Lisp expression for its condition." 494 "When at beginning of #ifX, return a Lisp expression for its condition."
562 (save-excursion 495 (save-excursion
563 (let ((negate (looking-at hif-ifndef-regexp))) 496 (let ((negate (looking-at hif-ifndef-regexp)))
564 (re-search-forward hif-ifx-regexp) 497 (re-search-forward hif-ifx-regexp)
@@ -566,7 +499,7 @@ that form should be displayed.")
566 (buffer-substring (point) 499 (buffer-substring (point)
567 (progn (skip-chars-forward "^\n\r") (point)))) 500 (progn (skip-chars-forward "^\n\r") (point))))
568 (expr (hif-infix-to-prefix (hif-tokenize expr-string)))) 501 (expr (hif-infix-to-prefix (hif-tokenize expr-string))))
569; (message "hif-canonicalized: %s" expr) 502 ;; (message "hif-canonicalized: %s" expr)
570 (if negate 503 (if negate
571 (list 'not expr) 504 (list 'not expr)
572 expr))))) 505 expr)))))
@@ -574,7 +507,7 @@ that form should be displayed.")
574 507
575(defun hif-find-any-ifX () 508(defun hif-find-any-ifX ()
576 "Move to next #if..., or #ifndef, at point or after." 509 "Move to next #if..., or #ifndef, at point or after."
577; (message "find ifX at %d" (point)) 510 ;; (message "find ifX at %d" (point))
578 (prog1 511 (prog1
579 (re-search-forward hif-ifx-regexp (point-max) t) 512 (re-search-forward hif-ifx-regexp (point-max) t)
580 (beginning-of-line))) 513 (beginning-of-line)))
@@ -582,17 +515,17 @@ that form should be displayed.")
582 515
583(defun hif-find-next-relevant () 516(defun hif-find-next-relevant ()
584 "Move to next #if..., #else, or #endif, after the current line." 517 "Move to next #if..., #else, or #endif, after the current line."
585; (message "hif-find-next-relevant at %d" (point)) 518 ;; (message "hif-find-next-relevant at %d" (point))
586 (end-of-line) 519 (end-of-line)
587 ; avoid infinite recursion by only going to beginning of line if match found 520 ;; avoid infinite recursion by only going to beginning of line if match found
588 (if (re-search-forward hif-ifx-else-endif-regexp (point-max) t) 521 (if (re-search-forward hif-ifx-else-endif-regexp (point-max) t)
589 (beginning-of-line))) 522 (beginning-of-line)))
590 523
591(defun hif-find-previous-relevant () 524(defun hif-find-previous-relevant ()
592 "Move to previous #if..., #else, or #endif, before the current line." 525 "Move to previous #if..., #else, or #endif, before the current line."
593; (message "hif-find-previous-relevant at %d" (point)) 526 ;; (message "hif-find-previous-relevant at %d" (point))
594 (beginning-of-line) 527 (beginning-of-line)
595 ; avoid infinite recursion by only going to beginning of line if match found 528 ;; avoid infinite recursion by only going to beginning of line if match found
596 (if (re-search-backward hif-ifx-else-endif-regexp (point-min) t) 529 (if (re-search-backward hif-ifx-else-endif-regexp (point-min) t)
597 (beginning-of-line))) 530 (beginning-of-line)))
598 531
@@ -608,7 +541,7 @@ that form should be displayed.")
608 541
609(defun hif-ifdef-to-endif () 542(defun hif-ifdef-to-endif ()
610 "If positioned at #ifX or #else form, skip to corresponding #endif." 543 "If positioned at #ifX or #else form, skip to corresponding #endif."
611; (message "hif-ifdef-to-endif at %d" (point)) (sit-for 1) 544 ;; (message "hif-ifdef-to-endif at %d" (point)) (sit-for 1)
612 (hif-find-next-relevant) 545 (hif-find-next-relevant)
613 (cond ((hif-looking-at-ifX) 546 (cond ((hif-looking-at-ifX)
614 (hif-ifdef-to-endif) ; find endif of nested if 547 (hif-ifdef-to-endif) ; find endif of nested if
@@ -623,7 +556,7 @@ that form should be displayed.")
623 556
624(defun hif-endif-to-ifdef () 557(defun hif-endif-to-ifdef ()
625 "If positioned at #endif form, skip backward to corresponding #ifX." 558 "If positioned at #endif form, skip backward to corresponding #ifX."
626; (message "hif-endif-to-ifdef at %d" (point)) 559 ;; (message "hif-endif-to-ifdef at %d" (point))
627 (let ((start (point))) 560 (let ((start (point)))
628 (hif-find-previous-relevant) 561 (hif-find-previous-relevant)
629 (if (= start (point)) 562 (if (= start (point))
@@ -643,18 +576,16 @@ that form should be displayed.")
643With argument, do this that many times." 576With argument, do this that many times."
644 (interactive "p") 577 (interactive "p")
645 (or arg (setq arg 1)) 578 (or arg (setq arg 1))
646 (if (< arg 0) 579 (if (< arg 0) (backward-ifdef (- arg))
647 (backward-ifdef (- arg))) 580 (while (< 0 arg)
648 (while (< 0 arg) 581 (setq arg (- arg))
649 (setq arg (- arg)) 582 (let ((start (point)))
650 (let ((start (point))) 583 (unless (hif-looking-at-ifX)
651 (if (not (hif-looking-at-ifX))
652 (hif-find-next-relevant)) 584 (hif-find-next-relevant))
653 (if (hif-looking-at-ifX) 585 (if (hif-looking-at-ifX)
654 (hif-ifdef-to-endif) 586 (hif-ifdef-to-endif)
655 (goto-char start) 587 (goto-char start)
656 (error "No following #ifdef") 588 (error "No following #ifdef"))))))
657 ))))
658 589
659 590
660(defun backward-ifdef (&optional arg) 591(defun backward-ifdef (&optional arg)
@@ -662,18 +593,17 @@ With argument, do this that many times."
662With argument, do this that many times." 593With argument, do this that many times."
663 (interactive "p") 594 (interactive "p")
664 (or arg (setq arg 1)) 595 (or arg (setq arg 1))
665 (if (< arg 0) 596 (if (< arg 0) (forward-ifdef (- arg))
666 (forward-ifdef (- arg))) 597 (while (< 0 arg)
667 (while (< 0 arg) 598 (setq arg (1- arg))
668 (setq arg (1- arg)) 599 (beginning-of-line)
669 (beginning-of-line) 600 (let ((start (point)))
670 (let ((start (point))) 601 (unless (hif-looking-at-endif)
671 (if (not (hif-looking-at-endif))
672 (hif-find-previous-relevant)) 602 (hif-find-previous-relevant))
673 (if (hif-looking-at-endif) 603 (if (hif-looking-at-endif)
674 (hif-endif-to-ifdef) 604 (hif-endif-to-ifdef)
675 (goto-char start) 605 (goto-char start)
676 (error "No previous #ifdef"))))) 606 (error "No previous #ifdef"))))))
677 607
678 608
679(defun down-ifdef () 609(defun down-ifdef ()
@@ -692,8 +622,8 @@ With argument, do this that many times."
692 (interactive) 622 (interactive)
693 (beginning-of-line) 623 (beginning-of-line)
694 (let ((start (point))) 624 (let ((start (point)))
695 (if (not (hif-looking-at-endif)) 625 (unless (hif-looking-at-endif)
696 (hif-find-previous-relevant)) 626 (hif-find-previous-relevant))
697 (if (hif-looking-at-endif) 627 (if (hif-looking-at-endif)
698 (hif-endif-to-ifdef)) 628 (hif-endif-to-ifdef))
699 (if (= start (point)) 629 (if (= start (point))
@@ -704,36 +634,32 @@ With argument, do this that many times."
704With argument, do this that many times." 634With argument, do this that many times."
705 (interactive "p") 635 (interactive "p")
706 (or arg (setq arg 1)) 636 (or arg (setq arg 1))
707 (if (< arg 0) 637 (if (< arg 0) (previous-ifdef (- arg))
708 (previous-ifdef (- arg))) 638 (while (< 0 arg)
709 (while (< 0 arg) 639 (setq arg (1- arg))
710 (setq arg (1- arg)) 640 (hif-find-next-relevant)
711 (hif-find-next-relevant) 641 (when (eolp)
712 (if (eolp) 642 (beginning-of-line)
713 (progn 643 (error "No following #ifdefs, #elses, or #endifs")))))
714 (beginning-of-line)
715 (error "No following #ifdefs, #elses, or #endifs")))))
716 644
717(defun previous-ifdef (&optional arg) 645(defun previous-ifdef (&optional arg)
718 "Move to the beginning of the previous #ifX, #else, or #endif. 646 "Move to the beginning of the previous #ifX, #else, or #endif.
719With argument, do this that many times." 647With argument, do this that many times."
720 (interactive "p") 648 (interactive "p")
721 (or arg (setq arg 1)) 649 (or arg (setq arg 1))
722 (if (< arg 0) 650 (if (< arg 0) (next-ifdef (- arg))
723 (next-ifdef (- arg))) 651 (while (< 0 arg)
724 (while (< 0 arg) 652 (setq arg (1- arg))
725 (setq arg (1- arg)) 653 (let ((start (point)))
726 (let ((start (point))) 654 (hif-find-previous-relevant)
727 (hif-find-previous-relevant) 655 (if (= start (point))
728 (if (= start (point)) 656 (error "No previous #ifdefs, #elses, or #endifs"))))))
729 (error "No previous #ifdefs, #elses, or #endifs")
730 ))))
731 657
732 658
733;===%%SF%% parsing (End) === 659;;===%%SF%% parsing (End) ===
734 660
735 661
736;===%%SF%% hide-ifdef-hiding (Start) === 662;;===%%SF%% hide-ifdef-hiding (Start) ===
737 663
738 664
739;;; A range is a structure with four components: 665;;; A range is a structure with four components:
@@ -743,13 +669,12 @@ With argument, do this that many times."
743;;; Only valid if ELSE-P is true. 669;;; Only valid if ELSE-P is true.
744;;; END The end of the range. (beginning of line) 670;;; END The end of the range. (beginning of line)
745 671
746(defun hif-make-range (else-p start end &optional else) 672(defun hif-make-range (start end &optional else)
747 (list else-p start else end)) 673 (list start else end))
748 674
749(defun hif-range-else-p (range) (elt range 0)) 675(defun hif-range-start (range) (elt range 0))
750(defun hif-range-start (range) (elt range 1)) 676(defun hif-range-else (range) (elt range 1))
751(defun hif-range-else (range) (elt range 2)) 677(defun hif-range-end (range) (elt range 2))
752(defun hif-range-end (range) (elt range 3))
753 678
754 679
755 680
@@ -759,40 +684,35 @@ With argument, do this that many times."
759;;; an #else was found, skip some more, looking for the true #endif. 684;;; an #else was found, skip some more, looking for the true #endif.
760 685
761(defun hif-find-range () 686(defun hif-find-range ()
762 "Returns a Range structure describing the current #if region. 687 "Return a Range structure describing the current #if region.
763Point is left unchanged." 688Point is left unchanged."
764; (message "hif-find-range at %d" (point)) 689 ;; (message "hif-find-range at %d" (point))
765 (save-excursion 690 (save-excursion
766 (beginning-of-line) 691 (beginning-of-line)
767 (let ((start (point)) 692 (let ((start (point))
768 (else-p nil)
769 (else nil) 693 (else nil)
770 (end nil)) 694 (end nil))
771 ;; Part one. Look for either #endif or #else. 695 ;; Part one. Look for either #endif or #else.
772 ;; This loop-and-a-half dedicated to E. Dijkstra. 696 ;; This loop-and-a-half dedicated to E. Dijkstra.
773 (hif-find-next-relevant) 697 (while (progn
774 (while (hif-looking-at-ifX) ; Skip nested ifdef 698 (hif-find-next-relevant)
775 (hif-ifdef-to-endif) 699 (hif-looking-at-ifX)) ; Skip nested ifdef
776 (hif-find-next-relevant)) 700 (hif-ifdef-to-endif))
777 ;; Found either a #else or an #endif. 701 ;; Found either a #else or an #endif.
778 (cond ((hif-looking-at-else) 702 (cond ((hif-looking-at-else)
779 (setq else-p t)
780 (setq else (point))) 703 (setq else (point)))
781 (t 704 (t
782 (setq end (point)) ; (save-excursion (end-of-line) (point)) 705 (setq end (point)))) ; (save-excursion (end-of-line) (point))
783 ))
784 ;; If found #else, look for #endif. 706 ;; If found #else, look for #endif.
785 (if else-p 707 (when else
786 (progn 708 (while (progn
787 (hif-find-next-relevant) 709 (hif-find-next-relevant)
788 (while (hif-looking-at-ifX) ; Skip nested ifdef 710 (hif-looking-at-ifX)) ; Skip nested ifdef
789 (hif-ifdef-to-endif) 711 (hif-ifdef-to-endif))
790 (hif-find-next-relevant)) 712 (if (hif-looking-at-else)
791 (if (hif-looking-at-else) 713 (error "Found two elses in a row? Broken!"))
792 (error "Found two elses in a row? Broken!")) 714 (setq end (point))) ; (save-excursion (end-of-line) (point))
793 (setq end (point)) ; (save-excursion (end-of-line) (point)) 715 (hif-make-range start end else))))
794 ))
795 (hif-make-range else-p start end else))))
796 716
797 717
798;;; A bit slimy. 718;;; A bit slimy.
@@ -805,15 +725,7 @@ Point is left unchanged."
805 (if hide-ifdef-lines 725 (if hide-ifdef-lines
806 (save-excursion 726 (save-excursion
807 (goto-char point) 727 (goto-char point)
808 (let ((modp (buffer-modified-p))) 728 (hide-ifdef-region (line-end-position 0) (line-end-position)))))
809 (unwind-protect
810 (progn
811 (beginning-of-line)
812 (if (not (= (point) 1))
813 (hide-ifdef-region (1- (point)) (point))))
814 (set-buffer-modified-p modp))
815 ))
816 ))
817 729
818 730
819;;; Hif-Possibly-Hide 731;;; Hif-Possibly-Hide
@@ -851,52 +763,50 @@ Point is left unchanged."
851(defun hif-possibly-hide () 763(defun hif-possibly-hide ()
852 "Called at #ifX expression, this hides those parts that should be hidden. 764 "Called at #ifX expression, this hides those parts that should be hidden.
853It uses the judgement of `hide-ifdef-evaluator'." 765It uses the judgement of `hide-ifdef-evaluator'."
854; (message "hif-possibly-hide") (sit-for 1) 766 ;; (message "hif-possibly-hide") (sit-for 1)
855 (let ((test (hif-canonicalize)) 767 (let ((test (hif-canonicalize))
856 (range (hif-find-range))) 768 (range (hif-find-range)))
857; (message "test = %s" test) (sit-for 1) 769 ;; (message "test = %s" test) (sit-for 1)
858 770
859 (hif-hide-line (hif-range-end range)) 771 (hif-hide-line (hif-range-end range))
860 (if (funcall hide-ifdef-evaluator test) 772 (if (funcall hide-ifdef-evaluator test)
861 (cond ((hif-range-else-p range) ; case 1 773 (cond ((hif-range-else range) ; case 1
862 (hif-hide-line (hif-range-else range))
863 (hide-ifdef-region (hif-range-else range)
864 (1- (hif-range-end range)))
865 (hif-recurse-on (hif-range-start range)
866 (hif-range-else range)))
867 (t ; case 2
868 (hif-recurse-on (hif-range-start range)
869 (hif-range-end range))))
870 (cond ((hif-range-else-p range) ; case 3
871 (hif-hide-line (hif-range-else range)) 774 (hif-hide-line (hif-range-else range))
872 (hide-ifdef-region (hif-range-start range) 775 (hide-ifdef-region (hif-range-else range)
873 (1- (hif-range-else range))) 776 (1- (hif-range-end range)))
874 (hif-recurse-on (hif-range-else range) 777 (hif-recurse-on (hif-range-start range)
875 (hif-range-end range))) 778 (hif-range-else range)))
876 (t ; case 4 779 (t ; case 2
877 (hide-ifdef-region (point) 780 (hif-recurse-on (hif-range-start range)
878 (1- (hif-range-end range)))) 781 (hif-range-end range))))
879 )) 782 (cond ((hif-range-else range) ; case 3
880 (hif-hide-line (hif-range-start range)) ; Always hide start. 783 (hif-hide-line (hif-range-else range))
881 (goto-char (hif-range-end range)) 784 (hide-ifdef-region (hif-range-start range)
882 (end-of-line) 785 (1- (hif-range-else range)))
883 )) 786 (hif-recurse-on (hif-range-else range)
787 (hif-range-end range)))
788 (t ; case 4
789 (hide-ifdef-region (point)
790 (1- (hif-range-end range))))))
791 (hif-hide-line (hif-range-start range)) ; Always hide start.
792 (goto-char (hif-range-end range))
793 (end-of-line)))
884 794
885 795
886 796
887(defun hide-ifdef-guts () 797(defun hide-ifdef-guts ()
888 "Does most of the work of `hide-ifdefs'. 798 "Does most of the work of `hide-ifdefs'.
889It does not do the work that's pointless to redo on a recursive entry." 799It does not do the work that's pointless to redo on a recursive entry."
890; (message "hide-ifdef-guts") 800 ;; (message "hide-ifdef-guts")
891 (save-excursion 801 (save-excursion
892 (goto-char (point-min)) 802 (goto-char (point-min))
893 (while (hif-find-any-ifX) 803 (while (hif-find-any-ifX)
894 (hif-possibly-hide)))) 804 (hif-possibly-hide))))
895 805
896;===%%SF%% hide-ifdef-hiding (End) === 806;;===%%SF%% hide-ifdef-hiding (End) ===
897 807
898 808
899;===%%SF%% exports (Start) === 809;;===%%SF%% exports (Start) ===
900 810
901;;;###autoload 811;;;###autoload
902(defcustom hide-ifdef-initially nil 812(defcustom hide-ifdef-initially nil
@@ -917,7 +827,7 @@ It does not do the work that's pointless to redo on a recursive entry."
917 :group 'hide-ifdef) 827 :group 'hide-ifdef)
918 828
919(defun hide-ifdef-toggle-read-only () 829(defun hide-ifdef-toggle-read-only ()
920 "Toggle hide-ifdef-read-only." 830 "Toggle `hide-ifdef-read-only'."
921 (interactive) 831 (interactive)
922 (setq hide-ifdef-read-only (not hide-ifdef-read-only)) 832 (setq hide-ifdef-read-only (not hide-ifdef-read-only))
923 (message "Hide-Read-Only %s" 833 (message "Hide-Read-Only %s"
@@ -934,8 +844,7 @@ It does not do the work that's pointless to redo on a recursive entry."
934 (if hif-outside-read-only "ON" "OFF")) 844 (if hif-outside-read-only "ON" "OFF"))
935 (setq buffer-read-only 845 (setq buffer-read-only
936 (or (and hide-ifdef-hiding hide-ifdef-read-only) 846 (or (and hide-ifdef-hiding hide-ifdef-read-only)
937 hif-outside-read-only) 847 hif-outside-read-only))
938 )
939 (force-mode-line-update)) 848 (force-mode-line-update))
940 849
941 850
@@ -963,14 +872,11 @@ Turn off hiding by calling `show-ifdefs'."
963 (interactive) 872 (interactive)
964 (message "Hiding...") 873 (message "Hiding...")
965 (setq hif-outside-read-only buffer-read-only) 874 (setq hif-outside-read-only buffer-read-only)
966 (if (not hide-ifdef-mode) 875 (unless hide-ifdef-mode (hide-ifdef-mode 1)) ; turn on hide-ifdef-mode
967 (hide-ifdef-mode 1)) ; turn on hide-ifdef-mode
968 (if hide-ifdef-hiding 876 (if hide-ifdef-hiding
969 (show-ifdefs)) ; Otherwise, deep confusion. 877 (show-ifdefs)) ; Otherwise, deep confusion.
970 (let ((inhibit-read-only t)) 878 (setq hide-ifdef-hiding t)
971 (setq selective-display t) 879 (hide-ifdef-guts)
972 (setq hide-ifdef-hiding t)
973 (hide-ifdef-guts))
974 (setq buffer-read-only (or hide-ifdef-read-only hif-outside-read-only)) 880 (setq buffer-read-only (or hide-ifdef-read-only hif-outside-read-only))
975 (or nomsg 881 (or nomsg
976 (message "Hiding done"))) 882 (message "Hiding done")))
@@ -980,9 +886,7 @@ Turn off hiding by calling `show-ifdefs'."
980 "Cancel the effects of `hide-ifdef': show the contents of all #ifdefs." 886 "Cancel the effects of `hide-ifdef': show the contents of all #ifdefs."
981 (interactive) 887 (interactive)
982 (setq buffer-read-only hif-outside-read-only) 888 (setq buffer-read-only hif-outside-read-only)
983 (setq selective-display nil) ; defaults 889 (hif-show-all)
984 (let ((inhibit-read-only t))
985 (hif-show-all))
986 (setq hide-ifdef-hiding nil)) 890 (setq hide-ifdef-hiding nil))
987 891
988 892
@@ -992,15 +896,15 @@ Return as (TOP . BOTTOM) the extent of ifdef block."
992 (let (max-bottom) 896 (let (max-bottom)
993 (cons (save-excursion 897 (cons (save-excursion
994 (beginning-of-line) 898 (beginning-of-line)
995 (if (not (or (hif-looking-at-else) (hif-looking-at-ifX))) 899 (unless (or (hif-looking-at-else) (hif-looking-at-ifX))
996 (up-ifdef)) 900 (up-ifdef))
997 (prog1 (point) 901 (prog1 (point)
998 (hif-ifdef-to-endif) 902 (hif-ifdef-to-endif)
999 (setq max-bottom (1- (point))))) 903 (setq max-bottom (1- (point)))))
1000 (save-excursion 904 (save-excursion
1001 (beginning-of-line) 905 (beginning-of-line)
1002 (if (not (hif-looking-at-endif)) 906 (unless (hif-looking-at-endif)
1003 (hif-find-next-relevant)) 907 (hif-find-next-relevant))
1004 (while (hif-looking-at-ifX) 908 (while (hif-looking-at-ifX)
1005 (hif-ifdef-to-endif) 909 (hif-ifdef-to-endif)
1006 (hif-find-next-relevant)) 910 (hif-find-next-relevant))
@@ -1010,16 +914,12 @@ Return as (TOP . BOTTOM) the extent of ifdef block."
1010(defun hide-ifdef-block () 914(defun hide-ifdef-block ()
1011 "Hide the ifdef block (true or false part) enclosing or before the cursor." 915 "Hide the ifdef block (true or false part) enclosing or before the cursor."
1012 (interactive) 916 (interactive)
1013 (if (not hide-ifdef-mode) 917 (unless hide-ifdef-mode (hide-ifdef-mode 1))
1014 (hide-ifdef-mode 1)) 918 (let ((top-bottom (hif-find-ifdef-block)))
1015 (setq selective-display t)
1016 (let ((top-bottom (hif-find-ifdef-block))
1017 (inhibit-read-only t))
1018 (hide-ifdef-region (car top-bottom) (cdr top-bottom)) 919 (hide-ifdef-region (car top-bottom) (cdr top-bottom))
1019 (if hide-ifdef-lines 920 (when hide-ifdef-lines
1020 (progn 921 (hif-hide-line (car top-bottom))
1021 (hif-hide-line (car top-bottom)) 922 (hif-hide-line (1+ (cdr top-bottom))))
1022 (hif-hide-line (1+ (cdr top-bottom)))))
1023 (setq hide-ifdef-hiding t)) 923 (setq hide-ifdef-hiding t))
1024 (setq buffer-read-only (or hide-ifdef-read-only hif-outside-read-only))) 924 (setq buffer-read-only (or hide-ifdef-read-only hif-outside-read-only)))
1025 925
@@ -1027,39 +927,30 @@ Return as (TOP . BOTTOM) the extent of ifdef block."
1027(defun show-ifdef-block () 927(defun show-ifdef-block ()
1028 "Show the ifdef block (true or false part) enclosing or before the cursor." 928 "Show the ifdef block (true or false part) enclosing or before the cursor."
1029 (interactive) 929 (interactive)
1030 (let ((inhibit-read-only t)) 930 (if hide-ifdef-lines
1031 (if hide-ifdef-lines 931 (save-excursion
1032 (save-excursion 932 (beginning-of-line)
1033 (beginning-of-line) 933 (hif-show-ifdef-region (1- (point)) (progn (end-of-line) (point))))
1034 (hif-show-ifdef-region (1- (point)) (progn (end-of-line) (point)))) 934 (let ((top-bottom (hif-find-ifdef-block)))
1035 935 (hif-show-ifdef-region (1- (car top-bottom)) (cdr top-bottom)))))
1036 (let ((top-bottom (hif-find-ifdef-block)))
1037 (hif-show-ifdef-region (1- (car top-bottom)) (cdr top-bottom))))))
1038 936
1039 937
1040;;; definition alist support 938;;; definition alist support
1041 939
1042(defvar hide-ifdef-define-alist nil 940(defvar hide-ifdef-define-alist nil
1043 "A global assoc list of pre-defined symbol lists") 941 "A global assoc list of pre-defined symbol lists.")
1044 942
1045(defun hif-compress-define-list (env) 943(defun hif-compress-define-list (env)
1046 "Compress the define list ENV into a list of defined symbols only." 944 "Compress the define list ENV into a list of defined symbols only."
1047 (let ((defs (mapcar (lambda (arg) 945 (let ((new-defs nil))
1048 (if (hif-lookup (car arg)) (car arg))) 946 (dolist (def env new-defs)
1049 env)) 947 (if (hif-lookup (car def)) (push (car env) new-defs)))))
1050 (new-defs nil))
1051 (while defs
1052 (if (car defs)
1053 (setq new-defs (cons (car defs) new-defs)))
1054 (setq defs (cdr defs)))
1055 new-defs))
1056 948
1057(defun hide-ifdef-set-define-alist (name) 949(defun hide-ifdef-set-define-alist (name)
1058 "Set the association for NAME to `hide-ifdef-env'." 950 "Set the association for NAME to `hide-ifdef-env'."
1059 (interactive "SSet define list: ") 951 (interactive "SSet define list: ")
1060 (setq hide-ifdef-define-alist 952 (push (cons name (hif-compress-define-list hide-ifdef-env))
1061 (cons (cons name (hif-compress-define-list hide-ifdef-env)) 953 hide-ifdef-define-alist))
1062 hide-ifdef-define-alist)))
1063 954
1064(defun hide-ifdef-use-define-alist (name) 955(defun hide-ifdef-use-define-alist (name)
1065 "Set `hide-ifdef-env' to the define list specified by NAME." 956 "Set `hide-ifdef-env' to the define list specified by NAME."