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

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


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