comparison lisp/progmodes/hideif.el @ 23440:21dd0dd62240

(hide-ifdef-env, hif-outside-read-only): Move declaration before first use. (hif-parse-if-exp, hif-nexttoken, hif-expr, hif-term, hif-eq-expr, hif-math, hif-factor): Rename token to hif-token and token-list to hif-token-list. (hif-token, hif-token-list): Declare them. (hif-find-ifdef-block): Return cons of top and bottom point instead of setting dynamically bound variables. (hide-ifdef-block, show-ifdef-block): Use return value of hif-find-ifdef-block.
author Andreas Schwab <schwab@suse.de>
date Tue, 13 Oct 1998 09:52:54 +0000
parents 6f55b3849106
children c8067b54bd55
comparison
equal deleted inserted replaced
23439:72d75218d971 23440:21dd0dd62240
182 "Syntax table used for tokenizing #if expressions.") 182 "Syntax table used for tokenizing #if expressions.")
183 183
184 (modify-syntax-entry ?_ "w" hide-ifdef-syntax-table) 184 (modify-syntax-entry ?_ "w" hide-ifdef-syntax-table)
185 (modify-syntax-entry ?& "." hide-ifdef-syntax-table) 185 (modify-syntax-entry ?& "." hide-ifdef-syntax-table)
186 (modify-syntax-entry ?\| "." hide-ifdef-syntax-table) 186 (modify-syntax-entry ?\| "." hide-ifdef-syntax-table)
187
188 (defvar hide-ifdef-env nil
189 "An alist of defined symbols and their values.")
190
191 (defvar hif-outside-read-only nil
192 "Internal variable. Saves the value of `buffer-read-only' while hiding.")
187 193
188 ;;;###autoload 194 ;;;###autoload
189 (defun hide-ifdef-mode (arg) 195 (defun hide-ifdef-mode (arg)
190 "Toggle Hide-Ifdef mode. This is a minor mode, albeit a large one. 196 "Toggle Hide-Ifdef mode. This is a minor mode, albeit a large one.
191 With ARG, turn Hide-Ifdef mode on if arg is positive, off otherwise. 197 With ARG, turn Hide-Ifdef mode on if arg is positive, off otherwise.
302 that form should be displayed.") 308 that form should be displayed.")
303 309
304 (defvar hif-undefined-symbol nil 310 (defvar hif-undefined-symbol nil
305 "...is by default considered to be false.") 311 "...is by default considered to be false.")
306 312
307 (defvar hide-ifdef-env nil
308 "An alist of defined symbols and their values.")
309
310 313
311 (defun hif-set-var (var value) 314 (defun hif-set-var (var value)
312 "Prepend (var value) pair to hide-ifdef-env." 315 "Prepend (var value) pair to hide-ifdef-env."
313 (setq hide-ifdef-env (cons (cons var value) hide-ifdef-env))) 316 (setq hide-ifdef-env (cons (cons var value) hide-ifdef-env)))
314 317
342 (defconst hif-else-regexp (concat hif-cpp-prefix "else")) 345 (defconst hif-else-regexp (concat hif-cpp-prefix "else"))
343 (defconst hif-endif-regexp (concat hif-cpp-prefix "endif")) 346 (defconst hif-endif-regexp (concat hif-cpp-prefix "endif"))
344 (defconst hif-ifx-else-endif-regexp 347 (defconst hif-ifx-else-endif-regexp
345 (concat hif-ifx-regexp "\\|" hif-else-regexp "\\|" hif-endif-regexp)) 348 (concat hif-ifx-regexp "\\|" hif-else-regexp "\\|" hif-endif-regexp))
346 349
350 ; Used to store the current token and the whole token list during parsing.
351 ; Only bound dynamically.
352 (defvar hif-token)
353 (defvar hif-token-list)
347 354
348 (defun hif-infix-to-prefix (token-list) 355 (defun hif-infix-to-prefix (token-list)
349 "Convert list of tokens in infix into prefix list" 356 "Convert list of tokens in infix into prefix list"
350 ; (message "hif-infix-to-prefix: %s" token-list) 357 ; (message "hif-infix-to-prefix: %s" token-list)
351 (if (= 1 (length token-list)) 358 (if (= 1 (length token-list))
422 ;;;----------------------------------------------------------------- 429 ;;;-----------------------------------------------------------------
423 ;;; Translate C preprocessor #if expressions using recursive descent. 430 ;;; Translate C preprocessor #if expressions using recursive descent.
424 ;;; This parser is limited to the operators &&, ||, !, and "defined". 431 ;;; This parser is limited to the operators &&, ||, !, and "defined".
425 ;;; Added ==, !=, +, and -. Gary Oberbrunner, garyo@avs.com, 8/9/94 432 ;;; Added ==, !=, +, and -. Gary Oberbrunner, garyo@avs.com, 8/9/94
426 433
427 (defun hif-parse-if-exp (token-list) 434 (defun hif-parse-if-exp (hif-token-list)
428 "Parse the TOKEN-LIST. Return translated list in prefix form." 435 "Parse the TOKEN-LIST. Return translated list in prefix form."
429 (hif-nexttoken) 436 (hif-nexttoken)
430 (prog1 437 (prog1
431 (hif-expr) 438 (hif-expr)
432 (if token ; is there still a token? 439 (if hif-token ; is there still a token?
433 (error "Error: unexpected token: %s" token)))) 440 (error "Error: unexpected token: %s" hif-token))))
434 441
435 (defun hif-nexttoken () 442 (defun hif-nexttoken ()
436 "Pop the next token from token-list into the let variable \"token\"." 443 "Pop the next token from token-list into the let variable \"hif-token\"."
437 (setq token (car token-list)) 444 (setq hif-token (car hif-token-list))
438 (setq token-list (cdr token-list)) 445 (setq hif-token-list (cdr hif-token-list))
439 token) 446 hif-token)
440 447
441 (defun hif-expr () 448 (defun hif-expr ()
442 "Parse an expression as found in #if. 449 "Parse an expression as found in #if.
443 expr : term | expr '||' term." 450 expr : term | expr '||' term."
444 (let ((result (hif-term))) 451 (let ((result (hif-term)))
445 (while (eq token 'or) 452 (while (eq hif-token 'or)
446 (hif-nexttoken) 453 (hif-nexttoken)
447 (setq result (list 'or result (hif-term)))) 454 (setq result (list 'or result (hif-term))))
448 result)) 455 result))
449 456
450 (defun hif-term () 457 (defun hif-term ()
451 "Parse a term : eq-expr | term '&&' eq-expr." 458 "Parse a term : eq-expr | term '&&' eq-expr."
452 (let ((result (hif-eq-expr))) 459 (let ((result (hif-eq-expr)))
453 (while (eq token 'and) 460 (while (eq hif-token 'and)
454 (hif-nexttoken) 461 (hif-nexttoken)
455 (setq result (list 'and result (hif-eq-expr)))) 462 (setq result (list 'and result (hif-eq-expr))))
456 result)) 463 result))
457 464
458 (defun hif-eq-expr () 465 (defun hif-eq-expr ()
459 "Parse an eq-expr : math | eq-expr `=='|`!='|`<'|`>'|`>='|`<=' math." 466 "Parse an eq-expr : math | eq-expr `=='|`!='|`<'|`>'|`>='|`<=' math."
460 (let ((result (hif-math)) 467 (let ((result (hif-math))
461 (eq-token nil)) 468 (eq-token nil))
462 (while (memq token '(equal hif-notequal hif-greater hif-less 469 (while (memq hif-token '(equal hif-notequal hif-greater hif-less
463 hif-greater-equal hif-less-equal)) 470 hif-greater-equal hif-less-equal))
464 (setq eq-token token) 471 (setq eq-token hif-token)
465 (hif-nexttoken) 472 (hif-nexttoken)
466 (setq result (list eq-token result (hif-math)))) 473 (setq result (list eq-token result (hif-math))))
467 result)) 474 result))
468 475
469 (defun hif-math () 476 (defun hif-math ()
470 "Parse an expression with + or - and simpler things. 477 "Parse an expression with + or - and simpler things.
471 math : factor | math '+|-' factor." 478 math : factor | math '+|-' factor."
472 (let ((result (hif-factor)) 479 (let ((result (hif-factor))
473 (math-op nil)) 480 (math-op nil))
474 (while (or (eq token 'hif-plus) (eq token 'hif-minus)) 481 (while (or (eq hif-token 'hif-plus) (eq hif-token 'hif-minus))
475 (setq math-op token) 482 (setq math-op hif-token)
476 (hif-nexttoken) 483 (hif-nexttoken)
477 (setq result (list math-op result (hif-factor)))) 484 (setq result (list math-op result (hif-factor))))
478 result)) 485 result))
479 486
480 (defun hif-factor () 487 (defun hif-factor ()
481 "Parse a factor: '!' factor | '(' expr ')' | 'defined(' id ')' | id." 488 "Parse a factor: '!' factor | '(' expr ')' | 'defined(' id ')' | id."
482 (cond 489 (cond
483 ((eq token 'not) 490 ((eq hif-token 'not)
484 (hif-nexttoken) 491 (hif-nexttoken)
485 (list 'not (hif-factor))) 492 (list 'not (hif-factor)))
486 493
487 ((eq token 'lparen) 494 ((eq hif-token 'lparen)
488 (hif-nexttoken) 495 (hif-nexttoken)
489 (let ((result (hif-expr))) 496 (let ((result (hif-expr)))
490 (if (not (eq token 'rparen)) 497 (if (not (eq hif-token 'rparen))
491 (error "Bad token in parenthesized expression: %s" token) 498 (error "Bad token in parenthesized expression: %s" hif-token)
492 (hif-nexttoken) 499 (hif-nexttoken)
493 result))) 500 result)))
494 501
495 ((eq token 'hif-defined) 502 ((eq hif-token 'hif-defined)
496 (hif-nexttoken) 503 (hif-nexttoken)
497 (if (not (eq token 'lparen)) 504 (if (not (eq hif-token 'lparen))
498 (error "Error: expected \"(\" after \"defined\"")) 505 (error "Error: expected \"(\" after \"defined\""))
499 (hif-nexttoken) 506 (hif-nexttoken)
500 (let ((ident token)) 507 (let ((ident hif-token))
501 (if (memq token '(or and not hif-defined lparen rparen)) 508 (if (memq hif-token '(or and not hif-defined lparen rparen))
502 (error "Error: unexpected token: %s" token)) 509 (error "Error: unexpected token: %s" hif-token))
503 (hif-nexttoken) 510 (hif-nexttoken)
504 (if (not (eq token 'rparen)) 511 (if (not (eq hif-token 'rparen))
505 (error "Error: expected \")\" after identifier")) 512 (error "Error: expected \")\" after identifier"))
506 (hif-nexttoken) 513 (hif-nexttoken)
507 (` (hif-defined (quote (, ident)))) 514 (` (hif-defined (quote (, ident))))
508 )) 515 ))
509 516
510 (t ; identifier 517 (t ; identifier
511 (let ((ident token)) 518 (let ((ident hif-token))
512 (if (memq ident '(or and)) 519 (if (memq ident '(or and))
513 (error "Error: missing identifier")) 520 (error "Error: missing identifier"))
514 (hif-nexttoken) 521 (hif-nexttoken)
515 (` (hif-lookup (quote (, ident)))) 522 (` (hif-lookup (quote (, ident))))
516 )) 523 ))
899 (defcustom hide-ifdef-read-only nil 906 (defcustom hide-ifdef-read-only nil
900 "*Set to non-nil if you want buffer to be read-only while hiding text." 907 "*Set to non-nil if you want buffer to be read-only while hiding text."
901 :type 'boolean 908 :type 'boolean
902 :group 'hide-ifdef) 909 :group 'hide-ifdef)
903 910
904 (defvar hif-outside-read-only nil
905 "Internal variable. Saves the value of `buffer-read-only' while hiding.")
906
907 ;;;###autoload 911 ;;;###autoload
908 (defcustom hide-ifdef-lines nil 912 (defcustom hide-ifdef-lines nil
909 "*Non-nil means hide the #ifX, #else, and #endif lines." 913 "*Non-nil means hide the #ifX, #else, and #endif lines."
910 :type 'boolean 914 :type 'boolean
911 :group 'hide-ifdef) 915 :group 'hide-ifdef)
980 (setq hide-ifdef-hiding nil)) 984 (setq hide-ifdef-hiding nil))
981 985
982 986
983 (defun hif-find-ifdef-block () 987 (defun hif-find-ifdef-block ()
984 "Utility for hide and show `ifdef-block'. 988 "Utility for hide and show `ifdef-block'.
985 Set top and bottom of ifdef block." 989 Return as (TOP . BOTTOM) the extent of ifdef block."
986 (let (max-bottom) 990 (let (max-bottom)
987 (save-excursion 991 (cons (save-excursion
988 (beginning-of-line) 992 (beginning-of-line)
989 (if (not (or (hif-looking-at-else) (hif-looking-at-ifX))) 993 (if (not (or (hif-looking-at-else) (hif-looking-at-ifX)))
990 (up-ifdef)) 994 (up-ifdef))
991 (setq top (point)) 995 (prog1 (point)
992 (hif-ifdef-to-endif) 996 (hif-ifdef-to-endif)
993 (setq max-bottom (1- (point)))) 997 (setq max-bottom (1- (point)))))
994 (save-excursion 998 (save-excursion
995 (beginning-of-line) 999 (beginning-of-line)
996 (if (not (hif-looking-at-endif)) 1000 (if (not (hif-looking-at-endif))
997 (hif-find-next-relevant)) 1001 (hif-find-next-relevant))
998 (while (hif-looking-at-ifX) 1002 (while (hif-looking-at-ifX)
999 (hif-ifdef-to-endif) 1003 (hif-ifdef-to-endif)
1000 (hif-find-next-relevant)) 1004 (hif-find-next-relevant))
1001 (setq bottom (min max-bottom (1- (point)))))) 1005 (min max-bottom (1- (point)))))))
1002 )
1003 1006
1004 1007
1005 (defun hide-ifdef-block () 1008 (defun hide-ifdef-block ()
1006 "Hide the ifdef block (true or false part) enclosing or before the cursor." 1009 "Hide the ifdef block (true or false part) enclosing or before the cursor."
1007 (interactive) 1010 (interactive)
1008 (if (not hide-ifdef-mode) 1011 (if (not hide-ifdef-mode)
1009 (hide-ifdef-mode 1)) 1012 (hide-ifdef-mode 1))
1010 (setq selective-display t) 1013 (setq selective-display t)
1011 (let (top bottom (inhibit-read-only t)) 1014 (let ((top-bottom (hif-find-ifdef-block))
1012 (hif-find-ifdef-block) ; set top and bottom - dynamic scoping 1015 (inhibit-read-only t))
1013 (hide-ifdef-region top bottom) 1016 (hide-ifdef-region (car top-bottom) (cdr top-bottom))
1014 (if hide-ifdef-lines 1017 (if hide-ifdef-lines
1015 (progn 1018 (progn
1016 (hif-hide-line top) 1019 (hif-hide-line (car top-bottom))
1017 (hif-hide-line (1+ bottom)))) 1020 (hif-hide-line (1+ (cdr top-bottom)))))
1018 (setq hide-ifdef-hiding t)) 1021 (setq hide-ifdef-hiding t))
1019 (setq buffer-read-only (or hide-ifdef-read-only hif-outside-read-only))) 1022 (setq buffer-read-only (or hide-ifdef-read-only hif-outside-read-only)))
1020 1023
1021 1024
1022 (defun show-ifdef-block () 1025 (defun show-ifdef-block ()
1026 (if hide-ifdef-lines 1029 (if hide-ifdef-lines
1027 (save-excursion 1030 (save-excursion
1028 (beginning-of-line) 1031 (beginning-of-line)
1029 (hif-show-ifdef-region (1- (point)) (progn (end-of-line) (point)))) 1032 (hif-show-ifdef-region (1- (point)) (progn (end-of-line) (point))))
1030 1033
1031 (let (top bottom) 1034 (let ((top-bottom (hif-find-ifdef-block)))
1032 (hif-find-ifdef-block) 1035 (hif-show-ifdef-region (1- (car top-bottom)) (cdr top-bottom))))))
1033 (hif-show-ifdef-region (1- top) bottom)))))
1034 1036
1035 1037
1036 ;;; definition alist support 1038 ;;; definition alist support
1037 1039
1038 (defvar hide-ifdef-define-alist nil 1040 (defvar hide-ifdef-define-alist nil