Annotation of gforth/history.fs, revision 1.1

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

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