Diff for /gforth/history.fs between versions 1.3 and 1.7

version 1.3, 1994/11/03 15:27:57 version 1.7, 1995/09/06 21:00:17
Line 1 Line 1
 \ History file support                                 16oct94py  \ History file support                                 16oct94py
   
 0 Value history  0 Value history
   
 2Variable forward^  2Variable forward^
 2Variable backward^  2Variable backward^
 2Variable end^  2Variable end^
   
 : get-history ( addr len -- wid )  : force-open ( addr len -- handle )
   2dup r/w open-file 0<    2dup r/w open-file 0<
   IF  drop r/w create-file throw  ELSE  nip nip  THEN    IF  drop r/w create-file throw  ELSE  nip nip  THEN ;
   to history  
   history file-size throw  : get-history ( addr len -- wid )
   2dup forward^ 2! 2dup backward^ 2! end^ 2! ;    force-open to history
     history file-size throw
 s" gforth.history" get-history    2dup forward^ 2! 2dup backward^ 2! end^ 2! ;
   
 \ moving in history file                               16oct94py  s" ~/.gforth-history" get-history
   
 : clear-line ( max span addr pos1 -- max addr )  : history-cold
   backspaces over spaces swap backspaces ;      Defers 'cold
       s" ~/.gforth-history" get-history ;
 : clear-tib ( max span addr pos -- max 0 addr 0 false )  
   clear-line 0 tuck dup ;  ' history-cold IS 'cold
   
 : get-line ( max addr -- max span addr pos dpos )  \ moving in history file                               16oct94py
   history file-position throw  backward^ 2!  
   2dup swap history read-line throw drop  : clear-line ( max span addr pos1 -- max addr )
   2dup type tuck    backspaces over spaces swap backspaces ;
   history file-position throw  forward^ 2! ;  
   : clear-tib ( max span addr pos -- max 0 addr 0 false )
 : next-line  ( max span addr pos1 -- max span addr pos2 false )    clear-line 0 tuck dup ;
   clear-line  
   forward^ 2@ history reposition-file throw  : hist-pos    ( -- ud )  history file-position throw ;
   get-line  0 ;  : hist-setpos ( ud -- )  history reposition-file throw ;
   
 : prev-line  ( max span addr pos1 -- max span addr pos2 false )  : get-line ( addr len -- len' flag )
   clear-line over 2 + negate s>d backward^ 2@ d+ 0. dmax    swap history read-line throw ;
   2dup history reposition-file throw  
   BEGIN   2over swap history read-line throw  WHILE  : next-line  ( max span addr pos1 -- max span addr pos2 false )
           >r history file-position throw    clear-line
           2dup backward^ 2@ d<  WHILE  2swap 2drop rdrop    forward^ 2@ 2dup hist-setpos backward^ 2!
   REPEAT  ELSE  >r history file-position throw  THEN    2dup get-line drop
   forward^ 2!  backward^ 2!  r> tuck 2dup type 0 ;    hist-pos  forward^ 2!
     tuck 2dup type 0 ;
 : ctrl  ( "<char>" -- ctrl-code )  
   char [char] @ - postpone Literal ; immediate  : prev-line  ( max span addr pos1 -- max span addr pos2 false )
     clear-line  backward^ 2@ forward^ 2!
 Create lfpad #lf c,    over 2 + negate s>d backward^ 2@ d+ 0. dmax 2dup hist-setpos
     BEGIN
 : (enter)  ( max span addr pos1 -- max span addr pos2 true )        backward^ 2!   2dup get-line  WHILE
   >r end^ 2@ history reposition-file throw        hist-pos 2dup forward^ 2@ d<  WHILE
   2dup swap history write-file throw        rot drop
   lfpad 1 history write-file throw    REPEAT  2drop  THEN
   history file-position throw 2dup backward^ 2! end^ 2!    tuck 2dup type 0 ;
   r> (ret) ;  
   : ctrl  ( "<char>" -- ctrl-code )
 \ some other key commands                              16oct94py    char [char] @ - postpone Literal ; immediate
   
 : first-pos  ( max span addr pos1 -- max span addr 0 0 )  Create lfpad #lf c,
   backspaces 0 0 ;  
 : end-pos  ( max span addr pos1 -- max span addr span 0 )  : (enter)  ( max span addr pos1 -- max span addr pos2 true )
   type-rest 2drop over 0 ;    >r end^ 2@ hist-setpos
     2dup swap history write-line throw
 : extract-word ( addr len -- addr' len' )  dup >r    hist-pos 2dup backward^ 2! end^ 2!
   BEGIN  1- dup 0>=  WHILE  2dup + c@ bl =  UNTIL  THEN  1+    r> (ret) ;
   tuck + r> rot - ;  
   \ some other key commands                              16oct94py
 Create prefix-found  0 , 0 ,  
   : first-pos  ( max span addr pos1 -- max span addr 0 0 )
 : word-lex ( nfa1 nfa2 -- -1/0/1 )    backspaces 0 0 ;
   dup 0=  IF  2drop 1  EXIT  THEN  : end-pos  ( max span addr pos1 -- max span addr span 0 )
   cell+ >r cell+ count $1F and    type-rest 2drop over 0 ;
   dup r@ c@ $1F and =  
   IF  r> char+ capscomp 0<=  EXIT  THEN  : extract-word ( addr len -- addr' len' )  dup >r
   nip r> c@ $1F and < ;    BEGIN  1- dup 0>=  WHILE  2dup + c@ bl =  UNTIL  THEN  1+
     tuck + r> rot - ;
 : search-prefix  ( addr len1 -- suffix len2 )  0 >r  context  
   BEGIN  BEGIN  dup @ over  cell - @ =  WHILE  cell -  REPEAT  Create prefix-found  0 , 0 ,
          dup >r -rot r> @ @  
          BEGIN  dup  WHILE  >r dup r@ cell+ c@ $1F and <=  : word-lex ( nfa1 nfa2 -- -1/0/1 )
                 IF  2dup r@ cell+ char+ capscomp  0=    dup 0=  IF  2drop 1  EXIT  THEN
                     IF  r> dup r@ word-lex    cell+ >r cell+ count $1F and
                         IF  dup prefix-found @ word-lex    dup r@ c@ $1F and =
                             0>= IF  rdrop dup >r  THEN    IF  r> char+ capscomp 0<=  EXIT  THEN
                         THEN >r    nip r> c@ $1F and < ;
                     THEN  
                 THEN  r> @  : search-voc ( addr len nfa1 nfa2 -- addr len nfa3 ) >r
          REPEAT  drop rot cell -  dup vp u> 0=      BEGIN  dup  WHILE  >r dup r@ cell+ c@ $1F and <=
   UNTIL  drop r> dup prefix-found ! ?dup          IF  2dup r@ cell+ char+ capscomp  0=
   IF    cell+ count $1F and rot /string rot drop              IF  r> dup r@ word-lex
   ELSE  2drop s" "  THEN  ;                  IF  dup prefix-found @ word-lex
                       0>= IF  rdrop dup >r  THEN
 : tab-expand ( max span addr pos1 -- max span addr pos2 0 )                  THEN >r
   prefix-found cell+ @  0 ?DO  (del)  LOOP              THEN
   2dup extract-word search-prefix          THEN  r> @
   dup prefix-found @ 0<> - prefix-found cell+ !      REPEAT drop r> ;
   bounds ?DO  I c@ (ins)  LOOP  
   prefix-found @ IF  bl (ins)  THEN  0 ;  : prefix-string ( addr len nfa -- addr' len' )
       dup prefix-found !  ?dup
 : kill-prefix  ( key -- key )      IF  cell+ count $1F and rot /string rot drop
   dup #tab <> IF  0 0 prefix-found 2!  THEN ;          dup 1+ prefix-found cell+ !
       ELSE
 ' kill-prefix IS everychar          2drop s" " prefix-found cell+ off
       THEN ;
 ' next-line  ctrl N cells ctrlkeys + !  
 ' prev-line  ctrl P cells ctrlkeys + !  : search-prefix  ( addr1 len1 -- addr2 len2 )
 ' clear-tib  ctrl K cells ctrlkeys + !      0 vp dup @ 1- cells over +
 ' first-pos  ctrl A cells ctrlkeys + !      DO  I 2@ <>
 ' end-pos    ctrl E cells ctrlkeys + !          IF  I cell+ @ @ swap  search-voc  THEN
 ' (enter)    #lf    cells ctrlkeys + !          [ -1 cells ] Literal +LOOP
 ' (enter)    #cr    cells ctrlkeys + !      prefix-string ;
 ' tab-expand #tab   cells ctrlkeys + !  
   : kill-expand ( max span addr pos1 -- max span addr pos2 )
       prefix-found cell+ @  0 ?DO  (del)  LOOP ;
   
   : tib-full? ( max span addr pos addr' len' -- max span addr pos addr1 u flag )
       5 pick over 4 pick + prefix-found @ 0<> - < ;
   
   : tab-expand ( max span addr pos1 -- max span addr pos2 0 )
       kill-expand  2dup extract-word search-prefix
       tib-full?
       IF    7 emit  2drop  0 0 prefix-found 2!
       ELSE  bounds ?DO  I c@ (ins)  LOOP  THEN
       prefix-found @ IF  bl (ins)  THEN  0 ;
   
   : kill-prefix  ( key -- key )
     dup #tab <> IF  0 0 prefix-found 2!  THEN ;
   
   ' kill-prefix IS everychar
   
   ' next-line  ctrl N cells ctrlkeys + !
   ' prev-line  ctrl P cells ctrlkeys + !
   ' clear-tib  ctrl K cells ctrlkeys + !
   ' first-pos  ctrl A cells ctrlkeys + !
   ' end-pos    ctrl E cells ctrlkeys + !
   ' (enter)    #lf    cells ctrlkeys + !
   ' (enter)    #cr    cells ctrlkeys + !
   ' tab-expand #tab   cells ctrlkeys + !

Removed from v.1.3  
changed lines
  Added in v.1.7


FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>