home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 200-299 / ff239.lzh / JGoodies / Brunjes / SpaceOrEscape < prev    next >
Text File  |  1989-08-21  |  4KB  |  112 lines

  1. \ Word to pause scrolling output from a program.
  2. \
  3. \ SpaceOrESC? ( -- )
  4. \
  5. \ This word is responsible for handling Space Bar presses to toggle the
  6. \ activity of a program (presumably for stopping the scrolling of text).
  7. \ It also looks for an ESCAPE key press.  If it is seen, an ABORT is done.
  8. \ Call this word from inside the loop of a program that prints
  9. \ a long time.
  10. \
  11. \ For example:
  12. \   : LONGPROG  ( -- )
  13. \       200 0 DO  i . ." Hello" cr SpaceOrEsc? LOOP
  14. \   ;
  15. \   LONGPROG  ( then hit space bar to stop and start output )
  16. \       ( hit ESCAPE to abort )
  17. \
  18.  
  19. ANEW SpaceOrEscape
  20.  
  21. Variable Escape.Flag   ( Used to tell if we should ABORT when we see ESC )
  22.     1    Escape.Flag ! ( By default, we ABORT when user presses ESCAPE )
  23.  
  24. : ?BL ( n -- )
  25. \
  26. \ This word looks at n (an ASCII character code).  If it is a BLANK (space),
  27. \ then this word waits for another space to be pressed and then returns.
  28. \ Note that this word eats the character passed to it!
  29. \
  30.    BL = IF                 ( TRUE if char typed was a BLank )
  31.           KEY DROP         ( Now throw away the char typed  )
  32.         THEN
  33. ;
  34.  
  35. : ?ESC ( n  --  n F | -- T | -- n | ABORT )
  36. \
  37. \ This word's action depends on the value of the variable Escape.Flag.
  38. \ If this variable is TRUE,  then n is compared to ESCAPE (ASCII 27).
  39. \ If n  = 27, then an ABORT is executed.
  40. \ If n <> 27, then n is returned.
  41. \ If Escape.Flag   is FALSE, then n is compared to ESCAPE (ASCII 27).
  42. \ If n  = 27, then TRUE is returned.
  43. \ If n <> 27, then n is returned under 0.
  44. \
  45.    DUP
  46.    Escape.Flag @
  47.    IF                      ( TRUE if pgmr wants to abort on ESCAPE )
  48.      27 =
  49.      IF                    ( TRUE if pgmr wants to abort & ESC pressed )
  50.        CR ." Program Aborted by ESCAPE key.  " DROP    ( drop ESC char )
  51.        ABORT
  52.      ELSE                  ( Pgmr wants to abort on ESC, but not pressed )
  53.      ( Do nothing! )       ( ESC not pressed -- return key struck )
  54.      THEN
  55.    ELSE                    ( Pgmr does NOT want to abort on ESCAPE )
  56.      27 =
  57.      IF                    ( TRUE if pgmr doesn't want to abort & ESC )
  58.        DROP                ( pressed )
  59.        1                   ( Indicate that ESCAPE was pressed )
  60.      ELSE
  61.        0                   ( Indicate that ESCAPE was NOT pressed )
  62.      THEN
  63.    THEN ;
  64.  
  65. : CheckItOut ( -- )
  66. \
  67. \ See if char typed demands handling or ignoring.
  68.   KEY ?ESC       ( only valid to have these two ?ESC and ?BL back to )
  69.   ?BL ;          ( back when Escape.Flag is TRUE, otherwise ?ESC might )
  70.                  ( return TWO values [one of them a flag, on top] )
  71.  
  72. : SpaceOrESC? ( -- )
  73. \
  74. \ This word is responsible for handling Space Bar presses to toggle the
  75. \ activity of a program (presumably for stopping the scrolling of text).
  76. \ It also looks for an ESCAPE key press.  If it is seen, an ABORT is done.
  77. \
  78.   ?TERMINAL IF CheckItOut THEN ;
  79.  
  80. ( The following three words are for example only and need not be compiled )
  81. ( unless you really want them to be.                                      )
  82.  
  83. \ : Test1  ( Immediately abort )
  84. \   1 Escape.Flag ! ( Abort on ESC )
  85. \   1000 0 DO I . SpaceorESC? loop ;
  86. \ : Test2  ( Completely ignore ABORT signal and continue processing )
  87. \   0 Escape.Flag ! ( No Abort on ESC )
  88. \   1000 0 DO I . ?TERMINAL
  89. \   IF
  90. \     KEY
  91. \     ?ESC IF ." Softly Aborting ... " ." Wait! "
  92. \          ELSE
  93. \            ?BL
  94. \          THEN
  95. \   THEN
  96. \   LOOP ;
  97. \ : Test3  ( Quickly perform abort, but not immediately )
  98. \   0 Escape.Flag ! ( No Abort on ESC )
  99. \   1000 0 DO I . ?TERMINAL
  100. \   IF
  101. \     KEY
  102. \     ?ESC IF ." Softly Aborting ... " ." OK, ready to ABORT. " ABORT
  103. \          ELSE
  104. \            ?BL
  105. \          THEN
  106. \   THEN
  107. \   LOOP ;
  108.  
  109.