home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / forth / compiler / fpc / source / card.seq < prev    next >
Encoding:
Text File  |  1990-07-24  |  22.7 KB  |  585 lines

  1. \ CARDFILE.SEQ  A simple cardfile program for F-PC      by Tom Zimmer
  2.  
  3. \ ┌────────────────────────────────────────────────────────────────────────┐
  4. \ │ Here is a simple Card File type database, as an example of how to make │
  5. \ │ a relatively small application.                                        │
  6. \ └────────────────────────────────────────────────────────────────────────┘
  7. \
  8. \               Compile with: TCOM CARD /OPT <Enter>
  9. \
  10. \ ***************************************************************************
  11. \ First colon (:) definition in an application is always the entry point.
  12.  
  13. : ebuffer       ( -- )
  14.                 empty-buffers ;
  15.  
  16. 16 constant l/blk
  17. 64 constant c/l
  18.  
  19.  0 value rolo-blk       \ current record #
  20.  0 value rolo-maxblk    \ highest valid record #
  21.  8 value rolo-x         \ left side of window
  22.  5 value rolo-y         \ right side of window
  23.  0 value rolo-curx      \ cursor positon column
  24.  0 value rolo-cury      \ cursor positon line
  25.  0 value rov/ins        \ overwrite/insert flag
  26.  0 value multi?         \ are we displaying multiple cards
  27.  
  28. 4 array curpossave      \ really a 2variable
  29. 4 array rrecloc
  30.  
  31. 32 array rlook.buf
  32. 0 value rfound
  33.  
  34. : ?ropen        ( --- f1 )      \ return bool true if ON FILE IS OPEN
  35.                 blkhndl >hndle @ 0< dup
  36.                 if      24 6 56 9 box&fill
  37.                         ."  There must be a file open to" bcr
  38.                         ."     perform this operation."
  39.                 then    ;
  40.  
  41. : set_curfound  ( a1 n1 -- )
  42.                 =: rolo-blk
  43.                 c/l /mod =: rolo-cury =: rolo-curx
  44.                 on> rfound ;
  45.  
  46.                 \ ** It wasn't in current record, search the rest ***
  47. : rfind_rest    ( -- )
  48.                 rolo-maxblk 1+ rolo-blk 1+ over min
  49.                 ?do     rrecloc 2@ at i 3 .r
  50.                         rlook.buf count i block b/buf
  51.                         search
  52.                         if      i set_curfound
  53.                                 leave
  54.                         else    drop
  55.                         then
  56.                 loop    ;
  57.  
  58. : set_found     ( a1 -- )
  59.                 1+ c/l /mod +!> rolo-cury +!> rolo-curx
  60.                 rolo-curx c/l /mod +!> rolo-cury =: rolo-curx
  61.                 on> rfound ;
  62.  
  63. : rfnext        ( --- )
  64.                 ?ropen ?exit
  65.                 cursor-off
  66.                 savescr
  67.                 15 11 60 13 box&fill
  68.                 space >rev ."   Searching for .... "
  69.                 rlook.buf count type 59 #out @ - spaces >norm
  70.                 off> rfound
  71.                 rlook.buf count                 \ look for this
  72.                 rolo-blk block                  \ address of record
  73.                                                 \ in this space
  74.                 rolo-curx 1+ c/l 1- min
  75.                 rolo-cury c/l * + b/buf 1- min
  76.                 dup>r + b/buf r> - 0max search
  77.                 if      \ *** we found text in current record, step to it ***
  78.                         set_found
  79.                 else    drop
  80.                         rfind_rest
  81.                 then
  82.                 rfound 0=
  83.                 if      29 14 50 16 box&fill
  84.                         space >rev ."   Text Not found  " >norm beep
  85.                         1 seconds
  86.                 then    restscr cursor-on ;
  87.  
  88. : rfind         ( --- )
  89.                 ?ropen ?exit
  90.                 savescr
  91.                 15 06 64 09 box&fill
  92.                 space >rev ."  Text to look for: " >norm
  93.                 ."   <Enter>=accept ESC=cancel"
  94.                 17 8 at rlook.buf count 20 swap over 1- min #expect
  95.                 span @ rlook.buf c!
  96.                 esc_flg @ 0= ( --- f1 )
  97.                 if      rfnext
  98.                 then restscr ;
  99.  
  100. handle rhndl
  101.  
  102. : empty_file    ( -- )          \ make the file new and empty
  103.                 flush
  104.                 rhndl blkhndl b/hcb cmove
  105.                 0 buffer b/buf blank
  106.                 update flush
  107.                 blkhndl hclose drop
  108.                 blkhndl hopen drop ;
  109.  
  110. : rnew          ( --- )
  111.                 savescr
  112.                 15 6 64 09 box&fill
  113.                 space >rev ."  Open/make a file: " >norm
  114.                 ."   <Enter>=accept ESC=cancel"
  115.                 rhndl clr-hcb
  116.                 17 8 at rhndl 1+ 29 expect
  117.                 esc_flg @
  118.                 if      restscr exit
  119.                 then    span @ rhndl c!
  120.                 rhndl c@ 0=
  121.                 if      " UNTITLED.BLK" ">$ rhndl $>handle
  122.                 then    0 rhndl count + c!
  123.                 rhndl hopen
  124.                 if      rhndl hcreate 0=
  125.                         if      empty_file
  126.                         else    20 6 60 8 box&fill
  127.                                 ." Could not CREATE file!" beep
  128.                                 1 seconds
  129.                         then
  130.                 else    20 6 60 8 box&fill
  131.                         ."  That file already exists, switching"
  132.                         2 seconds
  133.                         flush
  134.                         rhndl blkhndl b/hcb cmove
  135.                 then
  136.                 rhomer
  137.                 restscr
  138.                 init-rolodex ;
  139.  
  140. : rnewr         ( --- )         \ Append a new record to current database
  141.                 ?ropen ?exit
  142.                 rolo-maxblk 1+ buffer b/buf blank
  143.                 update flush
  144.                 init-rolodex
  145.                 rolo-maxblk =: rolo-blk
  146.                 rhome rhome ;
  147.  
  148. : rtoblk        ( --- )
  149.                 ?ropen ?exit
  150.                 savescr
  151.                 20 6 60 9 box&fill
  152.                 ."  Edit what record number? "
  153.                 tib 6 expect span @ #tib ! >in off
  154.                 bl word number? nip
  155.                 if      1- 0max rolo-maxblk min =: rolo-blk
  156.                         bcr ."  Selecting record " rolo-blk 1+ 3 .r 5 tenths
  157.                 else    bcr space >rev ."  INVALID RECORD# " >norm
  158.                         1 seconds
  159.                 then    restscr ;
  160.  
  161. : rdtgl         ( --- )         \ switch between display modes
  162.                 ?ropen ?exit
  163.                 multi? 0= =: multi? ;
  164.  
  165. : rquit         ( --- )         \ close the file and leave cardfile.
  166.                 0 23 at
  167.                 flush                   \ write any updated records to disk
  168.                 blkhndl hclose drop    \ close the file
  169.                 restscr                 \ restore the screen
  170.                 curpossave 2@ at
  171.                 ABORT ;                   \ and leave
  172.  
  173. : rdos          ( --- )
  174.                 flush
  175.                 savescr                 \ save the screen
  176.                 dark
  177.                 cr >rev ."  Type EXIT to return to F-PC. " >norm cr
  178.                 here dup off $sys dup 2 =
  179.                 if      ."  Couldn't find COMMAND.COM  "
  180.                 then    8 =
  181.                 if      ." Not enough memory to run DOS"
  182.                 then
  183.                 restscr ;
  184.  
  185. : .stat-rolodex ( --- )
  186.                 5 rolo-y 3 - at
  187.                 ."  Current Record is " #out @ #line @ rrecloc 2!
  188.                 rolo-blk 1+ 3 .r
  189.                 ."  of " rolo-maxblk 1+ 3 .r ."  Records"
  190.                 ."   Cursor is at Column " rolo-curx 2 .r
  191.                 ."  Line " rolo-cury 2 .r ;
  192.  
  193. : init-rolodex  ( --- )
  194.                 blkhndl endfile b/buf um/mod nip 1- 0max =: rolo-maxblk
  195.                 empty-buffers
  196.                 off> multi?
  197.                 off> rolo-blk
  198.                 off> rolo-curx
  199.                 off> rolo-cury
  200.                 rlook.buf off
  201.                 -1 =: mcol
  202.                 .menubar                        \ display the menubar
  203.                  off> mcol
  204.                                                 \ make status line
  205.                 4 rolo-y 4 - 2dup 72 2 d+ box&fill
  206.                                                 \ make edit border
  207.                 rolo-x 1- dup>r rolo-y 1- 2dup c/l 2+ l/blk 1+
  208.                 d+ dup>r box&fill
  209.                 r> r> 2dup 2+ swap at >rev ."  ESC=MENU " >norm
  210.                 3 - swap 2+ 2dup at eeol at
  211.                 ." Currently editing "
  212.                 blkhndl >hndle @ 0<
  213.                 if      ." NO FILE "
  214.                 else    blkhndl count type
  215.                 then    ;
  216.  
  217. : rchaddr       ( --- a1 )              \ address of current character
  218.                 rolo-curx rolo-cury c/l * + rolo-blk block + ;
  219.  
  220. : rfdel         ( --- )                 \ forward delete a character
  221.                 ?ropen multi? or ?exit
  222.                 rchaddr dup 1+ swap c/l rolo-curx - 2dup 2>r
  223.                 1- 0max cmove 2r> + 1- bl swap c! update ;
  224.  
  225. : rdel<>bl's    ( --- )                 \ delete NON-blanks to the right
  226.                 64 0
  227.                 do      rchaddr c@ bl = ?leave
  228.                         rfdel
  229.                 loop    ;
  230.  
  231. : rdelbl's      ( --- )
  232.                 64 0            \ delete blanks to the right
  233.                 do      rchaddr c@ bl <> ?leave
  234.                         rfdel
  235.                 loop    ;
  236.  
  237. : rwdel         ( --- )                 \ right word delete
  238.                 ?ropen multi? or ?exit
  239.                 rchaddr c@ bl <>
  240.                 if      rdel<>bl's
  241.                 then    rdelbl's ;
  242.  
  243. : rins          ( --- )                 \ insert toggle
  244.                 rov/ins 0= =: rov/ins
  245.                 rov/ins
  246.                 if      big-cursor
  247.                 else    norm-cursor then ;
  248.  
  249. : rhome         ( --- )
  250.                 off> rolo-curx ;
  251.  
  252. : rrhome        ( --- )                 \ go to BEGINNING of line
  253.                 rolo-curx 0=
  254.                 if      off> rolo-cury
  255.                 else    rhome
  256.                 then    ;
  257.  
  258. : rend          ( --- )                 \ go to END of line
  259.                 c/l 1- =: rolo-curx
  260.                 c/l 1- 0
  261.                 do      rchaddr 1- c@ bl <> ?leave
  262.                         rolo-curx 1- 0max =: rolo-curx
  263.                 loop    ;
  264.  
  265. : rhomer        ( --- )                 \ go to first record
  266.                 off> rolo-blk
  267.                 rrhome rrhome ;
  268.  
  269. : rendr         ( --- )                 \ go to last record
  270.                 rolo-maxblk =: rolo-blk
  271.                 rrhome rrhome ;
  272.  
  273. : rldel         ( --- )                 \ line delete
  274.                 ?ropen multi? or ?exit
  275.                 rhome  rchaddr dup c/l + swap
  276.                 rolo-blk block b/buf +
  277.                 dup>r over - cmove
  278.                    r> c/l - c/l blank update ;
  279.  
  280. : rnext         ( --- )
  281.                 rolo-blk 1+ rolo-maxblk min =: rolo-blk ;
  282.  
  283. : rprev         ( --- )
  284.                 rolo-blk 1- 0max =: rolo-blk ;
  285.  
  286. : rup           ( --- )                 \ go UP
  287.                 multi?
  288.                 if      rprev
  289.                 else    rolo-cury 1- 0max =: rolo-cury
  290.                 then    ;
  291.  
  292. : rdown         ( --- )                 \ go DOWN
  293.                 multi?
  294.                 if      rnext
  295.                 else    rolo-cury 1+ l/blk 1- min =: rolo-cury
  296.                 then    ;
  297.  
  298. : rnext2         ( --- )
  299.                 multi?
  300.                 if      l/blk 2/ 0 do rnext  loop
  301.                 else    rnext
  302.                 then    ;
  303.  
  304. : rprev2         ( --- )
  305.                 multi?
  306.                 if      l/blk 2/ 0 do rprev  loop
  307.                 else    rprev
  308.                 then    ;
  309.  
  310. : rret          ( --- )                 \ down to beginning of next line
  311.                 multi?
  312.                 if      rdtgl exit
  313.                 then    rov/ins         \ we are in insert mode
  314.                                         \ and last line empty
  315.                 rolo-blk block l/blk 1- c/l * + c/l bl skip nip 0= and
  316.                 if      rolo-curx 0=
  317.                         if      rchaddr dup c/l +
  318.                                 rolo-blk block b/buf +
  319.                                 over - 0max cmove>
  320.                                 rchaddr c/l blank update
  321.                         then
  322.                 then    rhome  rdown ;
  323.  
  324. : rright        ( --- )                 \ go RIGHT
  325.                 rolo-curx 1+ c/l 1- >
  326.                 if      rhome  rdown
  327.                 else    incr> rolo-curx then ;
  328.  
  329. : rleft         ( --- )                 \ go LEFT
  330.                 rolo-curx 1- 0<
  331.                 if      rolo-cury 0>
  332.                         if    rup rend then
  333.                 else    decr> rolo-curx
  334.                 then    ;
  335.  
  336. : rlword        ( --- )
  337.                 rleft
  338.                 begin   rchaddr c@ bl =         \ while on a blank
  339.                         rolo-curx rolo-cury or
  340.                         and                     \ and not at start of record
  341.                 while   rleft                   \ skip spaces
  342.                 repeat
  343.                 begin   rchaddr 1- c@ bl <>
  344.                         rolo-curx rolo-cury or
  345.                         and
  346.                 while   rleft
  347.                 repeat  ;
  348.  
  349. : rrword        ( --- )
  350.                 begin   rchaddr c@ bl <>
  351.                         rolo-curx c/l 1- =
  352.                         rolo-cury l/blk 1- = and 0= and
  353.                 while   rright
  354.                 repeat
  355.                 begin   rchaddr c@ bl =
  356.                         rolo-curx c/l 1- =
  357.                         rolo-cury l/blk 1- = and 0= and
  358.                 while   rright
  359.                 repeat  ;
  360.  
  361. : rinschar      ( c1 --- )      \ insert the character c1 in current record
  362.                 multi?
  363.                 if      drop
  364.                 else    rchaddr >r
  365.                         rov/ins
  366.                         if      r@ dup 1+ c/l rolo-curx - 1- cmove>
  367.                         then    r> c! update rright
  368.                 then    ;
  369.  
  370. : rbdel         ( --- )                 \ back delete
  371.                 ?ropen multi? or ?exit
  372.                 rolo-curx 0= rov/ins and
  373.                 if      rolo-cury 0<>
  374.                         rup rend rolo-curx 0= and
  375.                         if      rldel
  376.                         then
  377.                 else    rleft
  378.                         rov/ins
  379.                         if      rfdel
  380.                         else    bl rchaddr c! update
  381.                         then
  382.                 then    ;
  383.  
  384. : rtab          ( --- )
  385.                 ?ropen multi? or ?exit
  386.                 rolo-curx 8 mod 8 swap - 0
  387.                 ?do     rov/ins
  388.                         if      bl rinschar
  389.                         else    rright
  390.                         then
  391.                 loop    ;
  392.  
  393. : show_rolo_line ( a1 n1 -- a1 )
  394.                 over + dup rolo-maxblk <=
  395.                 if      dup rolo-blk = if >rev then
  396.                         block c/l type >norm
  397.                 else    drop c/l spaces
  398.                 then    ;
  399.  
  400. : show-roloblock ( -- )
  401.                 0 23 at         \ in case there is an error
  402.                 rolo-blk block
  403.                 l/blk 0
  404.                 do      rolo-x i rolo-y + at dup c/l type
  405.                         c/l +
  406.                 loop    drop ;
  407.  
  408. : show-rolodex  ( --- )
  409.                 cursor-off
  410.                 multi?
  411.                 if      rolo-blk l/blk 2/ - 0max
  412.                         l/blk 0
  413.                         do      rolo-x i rolo-y + at
  414.                                 i show_rolo_line
  415.                         loop    drop
  416.                 else    show-roloblock
  417.                         cursor-on
  418.                 then    ;
  419.  
  420. : rolo-show-cur ( --- )
  421.                 rolo-curx rolo-cury rolo-x rolo-y d+ at ;
  422.  
  423. : ?rcontrol     ( c1 --- c1 )           \ handle control characters
  424.                 dup bl >= ?exit
  425.                 dup 31 min exec:
  426. \               0 null  1 a     2 b     3 c     4 d     5 e     6 f
  427.                 noop    rlword  noop    rnext2  rright  rup   rrword
  428. \               7 g     8 h     9 i     LF      11 k    12 l    Enter
  429.                 rfdel   rbdel   rtab    noop    noop    noop    rret
  430. \               14 n    15 o    16 p    17 q    18 r    19 s    20 t
  431.                 noop    rnew    noop    noop    rprev2  rleft   rwdel
  432. \               21 u    22 v    23 w    24 x    25 y    26 z    Esc
  433.                 noop    rins    noop    rdown   rldel   noop    menu
  434. \               28 \    29 ]    30 ^    31 _
  435.                 noop    noop    noop    noop ;
  436.  
  437. : ?rfunc        ( c1 --- c1 )           \ handle function keys
  438.                 dup 128 < ?exit
  439.                 dup 142 max 248 min 142 -
  440.                 exec:
  441. \ 142                   baktab  AltQ    AltW    AltE    AltR    AltT
  442.                 noop    noop    noop    noop    noop    noop    noop
  443. \ 149           AltY    AltU    AltI    AltO    AltP
  444.                 noop    noop    noop    rnew    noop    noop    noop
  445. \ 156                           AltA    AltS    AltD    AltF    AltG
  446.                 noop    noop    noop    noop    rdtgl   noop   rtoblk
  447. \ 163           Alt-H   AltJ    AltK    AltL
  448.                 noop    noop    noop    noop    noop    noop    noop
  449. \ 170                           AltZ    AltX    AltC    AltV    AltB
  450.                 noop    noop    noop    noop    noop    noop    noop
  451. \ 177           AltN    AltM
  452.                 noop    noop    noop    noop    noop    noop    noop
  453. \ 184                                   F1      F2      F3      F4
  454.                 noop    noop    noop    noop    rdtgl   noop    rnewr
  455. \ 191           F5      F6      F7      F8      F9      F10
  456.                 noop    rfind   noop    noop    noop    rquit   noop
  457. \ 198                   Home    Up      PgUp            Left
  458.                 noop    rrhome  rup     rprev2  noop    rleft   noop
  459. \ 205           Right           End     Down    PgDn    Insert  Delete
  460.                 rright  noop   rend     rdown   rnext2  rins    rfdel
  461. \ 212           SF1     SF2     SF3     SF4     SF5     SF6     SF7
  462.                 noop    noop    noop    noop    noop    noop    noop
  463. \ 219           SF8     SF9     SF10    ^F1     ^F2     ^F3     ^F4
  464.                 noop    noop    noop    noop    noop    noop    noop
  465. \ 226           ^F5     ^F6     ^F7     ^F8     ^F9     ^F10    AF1
  466.                 noop    noop    noop    noop    noop    noop    noop
  467. \ 233           AF2     AF3     AF4     AF5     AF6     AF7     AF8
  468.                 noop    noop  ebuffer   noop    rfnext  noop    noop
  469. \ 240           AF9     AF10            ^Left   ^Right  ^END
  470.                 noop    noop    noop    rlword  rrword  rendr   noop
  471. \ 247           ^HOME   248
  472.                 rhomer  noop ;
  473.  
  474. : ?rchar        ( c1 --- c1 )           \ handle any character entry
  475.                 dup bl '~' between
  476.                 if      dup rinschar
  477.                 then    ;
  478.  
  479.                                                 \ c1 = keyboard character
  480. : rolokey       ( c1 --- )                      \ process a key
  481.                 ?rchar                          \ handle normal ascii
  482.                 ?rfunc                          \ function characters
  483.                 ?rcontrol                       \ control chars
  484.                 drop ;
  485.  
  486. : edit-rolodex  ( --- )
  487.                 begin   .stat-rolodex
  488.                         show-rolodex
  489.                         rolo-show-cur
  490.                         key rolokey
  491. \                        50 24 at ." Depth is = " depth h.
  492.                 again   ;
  493.  
  494. : rolodex       ( --- )
  495.                 read-write def-rwmode
  496.                 at? curpossave 2!               \ save cursor position
  497.                 empty-buffers
  498.                 savescr                         \ and screen contents
  499.                 dark                            \ clear the screen
  500.                 ?ds: sseg !                     \ initialize search segment
  501.                 tib 20 blank span off #tib off >in off
  502.                 blkhndl clr-hcb
  503.                 ?cs: dos_cmd_tail c@L 0<>
  504.                 if      dos_to_tib              \ move dosline to tib
  505.                         bl word blkhndl $>handle
  506.                         blkhndl hopen drop
  507.                 then
  508.                 init-menu
  509.                 init-rolodex
  510.                 10 rolo-x + l/blk 2/ rolo-y + at
  511.                 >rev ."   Press F then O to select a file to edit  " >norm
  512.                 13 rolo-x + l/blk 2/ rolo-y + 2+ at
  513.                 >rev ."   or Press F then Q to Quit Cardfile " >norm
  514.                 begin   blkhndl >hndle @ 0<
  515.                 while   menu
  516.                 repeat
  517.                 init-rolodex
  518.                 edit-rolodex ;
  519.  
  520. \       ┌─────────────────────────────────────────────────┐
  521. \       │ This section contains the menus for the rolodex │
  522. \       │ This is a good example of menu usage.           │
  523. \       └─────────────────────────────────────────────────┘
  524.  
  525. 5 newmenu rfile$                \ the FILE menu
  526.         menuline"  Open/make a file  Ctrl-O " rnew
  527.         menuline" ──────────────────────────" noop
  528.         menuline"  DOS commands             " rDOS
  529.         menuline" ──────────────────────────" noop
  530.         menuline"  Quit Cardfile        F10 " rquit
  531. endmenu
  532.  
  533. 2 newmenu redit$              \ the EDIT menu
  534.         menuline"  Create record           F4 " rnewr
  535.         menuline"  Revert to OLD Rec   Alt-F4 " empty-buffers
  536. endmenu
  537.  
  538. 6 newmenu rselect$              \ the SELECT menu
  539.         menuline"  Goto record#       Alt-G " rtoblk
  540.         menuline" ──────────────────────────" noop
  541.         menuline"  alt Display           F2 " rdtgl
  542.         menuline" ──────────────────────────" noop
  543.         menuline"  Find text             F6 " rfind
  544.         menuline"  Next find         Alt-F6 " rfnext
  545. endmenu
  546.  
  547. 3 newmenubar rbar ," File "     \ the menu bar contains only two items
  548.                   ," Edit "
  549.                   ," Select "
  550. endmenu
  551. create rlist    rfile$ ,-d      \ and two lists of functions
  552.                 redit$ ,-d
  553.                 rselect$ ,-d
  554.  
  555. \ initialize the default condition of the menu bar
  556.  
  557. : init-menu     ( --- )         \ the rolodex7 menu driver
  558.                 rbar        =: menubar
  559.                 rlist       =: menulist
  560.                 ['] rolokey is doother  ;      \ handle keys while in menu
  561.  
  562. : main          ( -- )
  563.                 vmode.set
  564.                 300 fudge !
  565.                 rolodex ;
  566.  
  567. ?DEFINIT 0= #IF         \ if the /NOINIT option enabled, then include this
  568.  
  569. : remain        ( -- )
  570.                 DECIMAL                         \ always select decimal
  571.                 INIT-CURSOR                     \ get intial cursor shape
  572.                 MARGIN_INIT                     \ initialize margins & TAB
  573.                 50 FUDGE !                      \ init MS timer, GUESS!!
  574.                 CAPS ON                         \ ignore cAsE
  575.                 ?DS: SSEG !                     \ init search segment
  576.                 DOSIO_INIT                      \ init EMIT, TYPE & SPACES
  577.                 $FFF0 SET_MEMORY                \ default to 64k code space
  578.                 DOS_TO_TIB                      \ move command tail to TIB
  579.                 COMSPEC_INIT                    \ init command specification
  580.                 main ;
  581.  
  582. #THEN
  583.  
  584.  
  585.