File:  [gforth] / gforth / history.fs
Revision 1.5: download - view: text, annotated - select for diffs
Sun Apr 30 23:18:21 1995 UTC (26 years ago) by pazsan
Branches: MAIN
CVS tags: HEAD
Added ~/ expansion (uses env HOME)

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

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