File:  [gforth] / gforth / history.fs
Revision 1.3: download - view: text, annotated - select for diffs
Thu Nov 3 15:27:57 1994 UTC (26 years, 6 months ago) by pazsan
Branches: MAIN
CVS tags: HEAD
Enhanced command exxpanding (now all dictionaries in search order)

    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:   2dup history reposition-file throw
   40:   BEGIN   2over swap history read-line throw  WHILE
   41:           >r history file-position throw
   42: 	  2dup backward^ 2@ d<  WHILE  2swap 2drop rdrop
   43:   REPEAT  ELSE  >r history file-position throw  THEN
   44:   forward^ 2!  backward^ 2!  r> tuck 2dup type 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 )  0 >r  context
   79:   BEGIN  BEGIN  dup @ over  cell - @ =  WHILE  cell -  REPEAT
   80:          dup >r -rot r> @ @
   81:          BEGIN  dup  WHILE  >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 rot cell -  dup vp u> 0=
   90:   UNTIL  drop r> dup prefix-found ! ?dup
   91:   IF    cell+ count $1F and rot /string rot drop
   92:   ELSE  2drop s" "  THEN  ;
   93: 
   94: : tab-expand ( max span addr pos1 -- max span addr pos2 0 )
   95:   prefix-found cell+ @  0 ?DO  (del)  LOOP
   96:   2dup extract-word search-prefix
   97:   dup prefix-found @ 0<> - prefix-found cell+ !
   98:   bounds ?DO  I c@ (ins)  LOOP
   99:   prefix-found @ IF  bl (ins)  THEN  0 ;
  100: 
  101: : kill-prefix  ( key -- key )
  102:   dup #tab <> IF  0 0 prefix-found 2!  THEN ;
  103: 
  104: ' kill-prefix IS everychar
  105: 
  106: ' next-line  ctrl N cells ctrlkeys + !
  107: ' prev-line  ctrl P cells ctrlkeys + !
  108: ' clear-tib  ctrl K cells ctrlkeys + !
  109: ' first-pos  ctrl A cells ctrlkeys + !
  110: ' end-pos    ctrl E cells ctrlkeys + !
  111: ' (enter)    #lf    cells ctrlkeys + !
  112: ' (enter)    #cr    cells ctrlkeys + !
  113: ' tab-expand #tab   cells ctrlkeys + !

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