Annotation of gforth/history.fs, revision 1.4

1.4     ! pazsan      1: \ History file support                                 16oct94py
        !             2: 
        !             3: 0 Value history
        !             4: 
        !             5: 2Variable forward^
        !             6: 2Variable backward^
        !             7: 2Variable end^
        !             8: 
        !             9: : get-history ( addr len -- wid )
        !            10:   2dup r/w open-file 0<
        !            11:   IF  drop r/w create-file throw  ELSE  nip nip  THEN
        !            12:   to history
        !            13:   history file-size throw
        !            14:   2dup forward^ 2! 2dup backward^ 2! end^ 2! ;
        !            15: 
        !            16: s" gforth.history" get-history
        !            17: 
        !            18: : history-cold  Defers 'cold
        !            19:   s" gforth.history" get-history ;
        !            20: 
        !            21: ' history-cold IS 'cold
        !            22: 
        !            23: \ moving in history file                               16oct94py
        !            24: 
        !            25: : clear-line ( max span addr pos1 -- max addr )
        !            26:   backspaces over spaces swap backspaces ;
        !            27: 
        !            28: : clear-tib ( max span addr pos -- max 0 addr 0 false )
        !            29:   clear-line 0 tuck dup ;
        !            30: 
        !            31: : get-line ( max addr -- max span addr pos dpos )
        !            32:   history file-position throw  backward^ 2!
        !            33:   2dup swap history read-line throw drop
        !            34:   2dup type tuck
        !            35:   history file-position throw  forward^ 2! ;
        !            36: 
        !            37: : next-line  ( max span addr pos1 -- max span addr pos2 false )
        !            38:   clear-line
        !            39:   forward^ 2@ history reposition-file throw
        !            40:   get-line  0 ;
        !            41: 
        !            42: : prev-line  ( max span addr pos1 -- max span addr pos2 false )
        !            43:   clear-line over 2 + negate s>d backward^ 2@ d+ 0. dmax
        !            44:   2dup history reposition-file throw
        !            45:   BEGIN   2over swap history read-line throw  WHILE
        !            46:           >r history file-position throw
        !            47:          2dup backward^ 2@ d<  WHILE  2swap 2drop rdrop
        !            48:   REPEAT  ELSE  >r history file-position throw  THEN
        !            49:   forward^ 2!  backward^ 2!  r> tuck 2dup type 0 ;
        !            50: 
        !            51: : ctrl  ( "<char>" -- ctrl-code )
        !            52:   char [char] @ - postpone Literal ; immediate
        !            53: 
        !            54: Create lfpad #lf c,
        !            55: 
        !            56: : (enter)  ( max span addr pos1 -- max span addr pos2 true )
        !            57:   >r end^ 2@ history reposition-file throw
        !            58:   2dup swap history write-file throw
        !            59:   lfpad 1 history write-file throw
        !            60:   history file-position throw 2dup backward^ 2! end^ 2!
        !            61:   r> (ret) ;
        !            62: 
        !            63: \ some other key commands                              16oct94py
        !            64: 
        !            65: : first-pos  ( max span addr pos1 -- max span addr 0 0 )
        !            66:   backspaces 0 0 ;
        !            67: : end-pos  ( max span addr pos1 -- max span addr span 0 )
        !            68:   type-rest 2drop over 0 ;
        !            69: 
        !            70: : extract-word ( addr len -- addr' len' )  dup >r
        !            71:   BEGIN  1- dup 0>=  WHILE  2dup + c@ bl =  UNTIL  THEN  1+
        !            72:   tuck + r> rot - ;
        !            73: 
        !            74: Create prefix-found  0 , 0 ,
        !            75: 
        !            76: : word-lex ( nfa1 nfa2 -- -1/0/1 )
        !            77:   dup 0=  IF  2drop 1  EXIT  THEN
        !            78:   cell+ >r cell+ count $1F and
        !            79:   dup r@ c@ $1F and =
        !            80:   IF  r> char+ capscomp 0<=  EXIT  THEN
        !            81:   nip r> c@ $1F and < ;
        !            82: 
        !            83: : search-prefix  ( addr len1 -- suffix len2 )  0 >r  context
        !            84:   BEGIN  BEGIN  dup @ over  cell - @ =  WHILE  cell -  REPEAT
        !            85:          dup >r -rot r> @ @
        !            86:          BEGIN  dup  WHILE  >r dup r@ cell+ c@ $1F and <=
        !            87:                 IF  2dup r@ cell+ char+ capscomp  0=
        !            88:                     IF  r> dup r@ word-lex
        !            89:                         IF  dup prefix-found @ word-lex
        !            90:                             0>= IF  rdrop dup >r  THEN
        !            91:                         THEN >r
        !            92:                     THEN
        !            93:                 THEN  r> @
        !            94:          REPEAT  drop rot cell -  dup vp u> 0=
        !            95:   UNTIL  drop r> dup prefix-found ! ?dup
        !            96:   IF    cell+ count $1F and rot /string rot drop
        !            97:   ELSE  2drop s" "  THEN  ;
        !            98: 
        !            99: : tab-expand ( max span addr pos1 -- max span addr pos2 0 )
        !           100:   prefix-found cell+ @  0 ?DO  (del)  LOOP
        !           101:   2dup extract-word search-prefix
        !           102:   dup prefix-found @ 0<> - prefix-found cell+ !
        !           103:   bounds ?DO  I c@ (ins)  LOOP
        !           104:   prefix-found @ IF  bl (ins)  THEN  0 ;
        !           105: 
        !           106: : kill-prefix  ( key -- key )
        !           107:   dup #tab <> IF  0 0 prefix-found 2!  THEN ;
        !           108: 
        !           109: ' kill-prefix IS everychar
        !           110: 
        !           111: ' next-line  ctrl N cells ctrlkeys + !
        !           112: ' prev-line  ctrl P cells ctrlkeys + !
        !           113: ' clear-tib  ctrl K cells ctrlkeys + !
        !           114: ' first-pos  ctrl A cells ctrlkeys + !
        !           115: ' end-pos    ctrl E cells ctrlkeys + !
        !           116: ' (enter)    #lf    cells ctrlkeys + !
        !           117: ' (enter)    #cr    cells ctrlkeys + !
        !           118: ' tab-expand #tab   cells ctrlkeys + !

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