\      ͻ
\       Lesson 5 Part 170  F-PC 3.5 Tutorial by Jack Brown 
\      ͼ

\ JB#EDIT.SEQ Part 2 of 4

\ This character will fill unused digit position
254 CONSTANT CHFL


\ This routine edits a counted string and converts to double number.
\ cur is cursor x y packed into one word.
\ We are using F-PC's LINEEDITOR ( x y a n -- flag )
: ED_CONVERT  ( adr n cur -- cur adr n dn )
        BEGIN DUP >R                     \ a n c  Position cursor.
          -ROT R> SPLIT 2OVER            \ c a n x y a n
          LINEEDITOR DROP                \ c a n  Edit string.
          OVER SKIP-BLANKS               \ c a n  Move up to non-blank
          ANY-SIGN?                      \ c a n a' flg
          >R 0 0 ROT -1                  \ c a n dn a' -1
          BEGIN  DPL !  CONVERT          \ c a n dn a"
            DUP C@  ASCII . =            \ c a n dn a" flg
            WHILE 0 REPEAT               \ c a n dn a" 0
            C@ DUP CHFL =
            SWAP BL = OR  NOT            \ c a n dn flag
        WHILE 2DROP R> DROP BEEP         \ c a n
              ASCII ? 2 PICK 1+ C! ROT   \ a n c    marks error
        REPEAT R> ?DNEGATE               \ c a n dn
        DPL @ 0< IF DPL OFF THEN ;       \ DPL=0 if .pt not entered


\ Fetch a double number using field with of n  using adr  for
\ and input buffer.  Invalid input is marked by ?  and user is
\ required to repeat until he makes a valid number.
: (#ED)  ( adr n -- dn )
        CUR@ ED_CONVERT               \ cur adr n dn
        >R >R                         \ Save double number.
        1+ ROT + CUR!                 \ Restore cursor.
        DROP R> R> ;                  \ Recover our number.


\  Ŀ
\    32 bit Variable Editing and 32 bit numeric input.    
\  


\ As above but field width is specified on the stack.
: WD#ED  ( adr w   -- )
        >R
        TPAD 1+ 32 CHFL FILL    \ blank input field.
        R@ TPAD C!
        DUP 2@ 2DUP D0=         \ Is number 0 ?
        IF   2DROP              \ if so provide blank field
        ELSE TUCK DABS          \ other wise
             <# #S ROT SIGN #>  \ format number and move
             TPAD 1+ SWAP R@    \ to the edit buffer.
             MIN CMOVE
        THEN
        TPAD  R>  (#ED) ROT 2! ;

\ Edit double number at current cursor position using default
\ field with of 12.   Input buffer is at TPAD
: D#ED   ( adr -- )
        12 WD#ED  ;

\ As above but cursor & field width are specified on the stack.
: XYWD#ED  ( adr x y w   -- )
        -ROT AT WD#ED ;


\ Input double number with field width on stack
\ and leave resulting double number on the parameter stack.
: WD#IN  ( w -- dn )
        0 0 SNUM 2!
            SNUM SWAP WD#ED
            SNUM 2@  ;

\ Input double number and leave on parameter stack.
: D#IN  ( -- dn )
        12 WD#IN ;


\ Input double number at cursor postion x y using a field width w
\ and leave the resulting double number on the parameter stack.
: XYWD#IN  ( x y w -- dn )
        -ROT AT WD#IN ;

