File:  [gforth] / gforth / history.fs
Revision 1.6: download - view: text, annotated - select for diffs
Wed Jun 7 10:05:05 1995 UTC (25 years, 11 months ago) by anton
Branches: MAIN
CVS tags: HEAD
added tilde expansion to primitives
installation now allows parallel versions (like gcc).
The version string is now in only one place

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

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