home *** CD-ROM | disk | FTP | other *** search
/ PC Media 2 / PC MEDIA CD02.iso / share / prog / optasm / editor.bas < prev    next >
Encoding:
BASIC Source File  |  1993-05-11  |  3.6 KB  |  137 lines

  1.     DEFINT A-Z
  2.     DECLARE SUB ClearBuf ()
  3.     DECLARE SUB PrintString (Txt$, Row, Col, Colr)
  4.     DECLARE FUNCTION Editor (In$, StrPtr, Row, Col, Colr)
  5.     DECLARE FUNCTION WaitKey ()
  6.     '$INCLUDE: 'd:\misc\scancode.inc'
  7.  
  8.     CLS
  9.     In$ = STRING$(20, 32)
  10.     PRINT "["; In$; "]";
  11.     LSET In$ = "This is a test."
  12.     DO
  13.         Result = Editor(In$, StrPtr, 1, 2, 15)
  14.     LOOP UNTIL (Result = 13) OR (Result = 27)
  15.     In$ = RTRIM$(In$)
  16.     END
  17.  
  18. SUB ClearBuf
  19.  
  20.     DEF SEG = 0
  21.     Temp& = PEEK(&H41A) + (PEEK(&H41B) * 256&)
  22.     Head = Temp& AND &HFFFF&                'Clear keyboard buffer
  23.     POKE &H41C, Head AND 255                'by setting head ptr
  24.     POKE &H41D, Head \ 256                  'equal to tail ptr.
  25.  
  26. END SUB
  27.  
  28. FUNCTION Editor (In$, StrPtr, Row, Col, Colr)
  29.  
  30.     STATIC CsrSize                         'STATIC variables preserved
  31.     STATIC InsFlag                         'between CALLs.
  32.                                     
  33.     IF CsrSize = 0 THEN
  34.        DEF SEG = 0                         'Determine bottom scan line
  35.        IF PEEK(&H463) = &HB4 THEN          'for cursor, depending on
  36.          CsrSize = 12                     'display type.
  37.        ELSE
  38.          CsrSize = 7
  39.        END IF
  40.     END IF
  41.     
  42.     Length = LEN(In$)                      'Save length of In$
  43.     IF StrPtr = 0 THEN StrPtr = 1          'Start editing at first char
  44.     CALL PrintString(In$, Row, Col, Colr)
  45.  
  46.     IF InsFlag THEN
  47.        LOCATE , , 1, 0, CsrSize                 'Block cursor
  48.     ELSE
  49.        LOCATE , , 1, CsrSize - 1, CsrSize       'Underscore
  50.     END IF
  51.  
  52.     DO
  53.         LOCATE Row, Col + StrPtr - 1
  54.         K = WaitKey                            'Get a keystroke
  55.         SELECT CASE K
  56.             CASE Enter, Esc
  57.                 EXIT DO
  58.             CASE 8                            'Backspace
  59.                 IF StrPtr > 1 THEN
  60.                    StrPtr = StrPtr - 1
  61.                    LSET In$ = LEFT$(In$, StrPtr - 1) + MID$(In$, StrPtr + 1)
  62.                 END IF
  63.             CASE Home
  64.                 StrPtr = 1
  65.             CASE Left
  66.                 IF StrPtr > 1 THEN
  67.                    StrPtr = StrPtr - 1
  68.                 END IF
  69.             CASE Right
  70.                 IF StrPtr < Length THEN
  71.                    StrPtr = StrPtr + 1
  72.                 END IF
  73.             CASE EndKey
  74.                 FOR I = Length TO 1 STEP -1
  75.                     Char = ASC(MID$(In$, I, 1))
  76.                     IF (Char <> 32) THEN
  77.                       IF Char THEN
  78.                         EXIT FOR
  79.                       END IF
  80.                     END IF
  81.                 NEXT
  82.                 IF I THEN StrPtr = I + 1
  83.             CASE Ins
  84.                 InsFlag = InsFlag XOR -1
  85.                 IF InsFlag THEN
  86.                    LOCATE , , 1, 0, CsrSize
  87.                 ELSE
  88.                    LOCATE , , 1, CsrSize - 1, CsrSize
  89.                 END IF
  90.             CASE Del
  91.                 LSET In$ = LEFT$(In$, StrPtr - 1) + MID$(In$, StrPtr + 1)
  92.             CASE IS > 31
  93.                 K$ = CHR$(K)
  94.                 IF StrPtr <= Length THEN
  95.                    IF InsFlag THEN
  96.                      LSET In$ = LEFT$(In$, StrPtr - 1) + K$ + MID$(In$, StrPtr, Length - StrPtr)
  97.                      StrPtr = StrPtr + 1
  98.                    ELSE
  99.                      MID$(In$, StrPtr, 1) = K$
  100.                      StrPtr = StrPtr + 1
  101.                    END IF
  102.                 END IF
  103.             CASE ELSE
  104.                 EXIT DO
  105.         END SELECT
  106.         CALL PrintString(In$, Row, Col, Colr)
  107.     LOOP
  108.     Editor = K                             'Function returns scan code
  109.     LOCATE , , 0                           'of last key pressed.
  110.  
  111. END FUNCTION
  112.  
  113. SUB PrintString (A$, Row, Col, Colr)
  114.  
  115.     BG = (Colr AND 112) \ 16
  116.     FG = (Colr AND 128) \ 8 + (Colr AND 15)
  117.     COLOR FG, BG                            'Set new color...
  118.     LOCATE Row, Col                         'and location...
  119.     PRINT A$;                               'and print!
  120.  
  121. END SUB
  122.  
  123. FUNCTION WaitKey
  124.  
  125.     CALL ClearBuf                          'Clear kybd buffer.
  126.     DO
  127.         A$ = INKEY$                       'Wait for a keypress.
  128.     LOOP UNTIL LEN(A$)
  129.     IF LEN(A$) = 2 THEN                    'If an extended key...
  130.        WaitKey = -ASC(MID$(A$, 2, 1))      'Return negative code.
  131.     ELSE
  132.        WaitKey = ASC(A$)                   'Else, return keycode.
  133.     END IF
  134.  
  135. END FUNCTION
  136.  
  137.