File:  [gforth] / gforth / history.fs
Revision 1.42: download - view: text, annotated - select for diffs
Sat Jan 29 16:55:30 2005 UTC (19 years, 2 months ago) by pazsan
Branches: MAIN
CVS tags: HEAD
Added --enable-force-cdiv

    1: \ command line edit and history support                 16oct94py
    2: 
    3: \ Copyright (C) 1995,2000,2003,2004 Free Software Foundation, Inc.
    4: 
    5: \ This file is part of Gforth.
    6: 
    7: \ Gforth is free software; you can redistribute it and/or
    8: \ modify it under the terms of the GNU General Public License
    9: \ as published by the Free Software Foundation; either version 2
   10: \ of the License, or (at your option) any later version.
   11: 
   12: \ This program is distributed in the hope that it will be useful,
   13: \ but WITHOUT ANY WARRANTY; without even the implied warranty of
   14: \ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   15: \ GNU General Public License for more details.
   16: 
   17: \ You should have received a copy of the GNU General Public License
   18: \ along with this program; if not, write to the Free Software
   19: \ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
   20: 
   21: :noname
   22:     char [char] @ - ;
   23: :noname
   24:     char [char] @ - postpone Literal ;
   25: interpret/compile: ctrl  ( "<char>" -- ctrl-code )
   26: 
   27: \ command line editing                                  16oct94py
   28: 
   29: : >string  ( span addr pos1 -- span addr pos1 addr2 len )
   30:   over 3 pick 2 pick chars /string ;
   31: 
   32: : bindkey ( xt key -- )  cells ctrlkeys + ! ;
   33: 
   34: \ history support                                       16oct94py
   35: 
   36: 0 Value history \ history file fid
   37: 
   38: 2Variable forward^
   39: 2Variable backward^
   40: 2Variable end^
   41: 
   42: : force-open ( addr len -- fid )
   43:     2dup r/w open-file
   44:     IF
   45: 	drop r/w create-file throw
   46:     ELSE
   47: 	nip nip
   48:     THEN ;
   49: 
   50: s" os-class" environment? [IF] s" unix" str= [ELSE] true [THEN] 
   51: [IF]
   52: : history-file ( -- addr u )
   53:     s" GFORTHHIST" getenv dup 0= IF
   54: 	2drop s" ~/.gforth-history"
   55:     THEN ;
   56: [ELSE]
   57: 
   58: : history-dir ( -- addr u )
   59:   s" TMP" getenv ?dup ?EXIT drop
   60:   s" TEMP" getenv ?dup ?EXIT drop
   61:   s" c:/" ;
   62: 
   63: : history-file ( -- addr u )
   64:   s" GFORTHHIST" getenv ?dup ?EXIT
   65:   drop
   66:   history-dir pad place
   67:   s" /ghist.fs" pad +place pad count ;
   68: [THEN]
   69: 
   70: \ moving in history file                               16oct94py
   71: 
   72: : clear-line ( max span addr pos1 -- max addr )
   73:   backspaces over spaces swap backspaces ;
   74: 
   75: \ : clear-tib ( max span addr pos -- max 0 addr 0 false )
   76: \   clear-line 0 tuck dup ;
   77: 
   78: : hist-pos    ( -- ud )  history file-position drop ( throw ) ;
   79: : hist-setpos ( ud -- )  history reposition-file drop ( throw ) ;
   80: 
   81: : get-line ( addr len -- len' flag )
   82:   swap history read-line throw ;
   83: 
   84: : next-line  ( max span addr pos1 -- max span addr pos2 false )
   85:   clear-line
   86:   forward^ 2@ 2dup hist-setpos backward^ 2!
   87:   2dup get-line drop
   88:   hist-pos  forward^ 2!
   89:   tuck 2dup type 0 ;
   90: 
   91: : find-prev-line ( max addr -- max span addr pos2 )
   92:   backward^ 2@ forward^ 2!
   93:   over 2 + negate s>d backward^ 2@ d+ 0. dmax 2dup hist-setpos
   94:   BEGIN
   95:       backward^ 2!   2dup get-line  WHILE
   96:       hist-pos 2dup forward^ 2@ d<  WHILE
   97:       rot drop
   98:   REPEAT  2drop  THEN  tuck ;
   99: 
  100: : prev-line  ( max span addr pos1 -- max span addr pos2 false )
  101:     clear-line find-prev-line 2dup type 0 ;
  102: 
  103: \ Create lfpad #lf c,
  104: 
  105: : (enter)  ( max span addr pos1 -- max span addr pos2 true )
  106:   >r end^ 2@ hist-setpos
  107:   2dup swap history write-line drop ( throw ) \ don't worry about errors
  108:   hist-pos 2dup backward^ 2! end^ 2!
  109:   r> (ret) ;
  110: 
  111: : extract-word ( addr len -- addr' len' )  dup >r
  112:   BEGIN  1- dup 0>=  WHILE  2dup + c@ bl =  UNTIL  THEN  1+
  113:   tuck + r> rot - ;
  114: 
  115: Create prefix-found  0 , 0 ,
  116: 
  117: : sgn ( n -- -1/0/1 )
  118:  dup 0= IF EXIT THEN  0< 2* 1+ ;
  119: 
  120: : capscomp  ( c_addr1 u c_addr2 -- n )
  121:  swap bounds
  122:  ?DO  dup c@ I c@ <>
  123:      IF  dup c@ toupper I c@ toupper =
  124:      ELSE  true  THEN  WHILE  1+  LOOP  drop 0
  125:  ELSE  c@ toupper I c@ toupper - unloop  THEN  sgn ;
  126: 
  127: : word-lex ( nfa1 nfa2 -- -1/0/1 )
  128:     dup 0=
  129:     IF
  130: 	2drop 1  EXIT
  131:     THEN
  132:     name>string 2>r name>string
  133:     dup r@ =
  134:     IF
  135: 	rdrop r> capscomp 0<= EXIT
  136:     THEN
  137:     r> <
  138:     nip rdrop ;
  139: 
  140: : search-voc ( addr len nfa1 nfa2 -- addr len nfa3 )
  141:     >r
  142:     BEGIN
  143: 	dup
  144:     WHILE
  145: 	>r dup r@ name>string nip <=
  146: 	IF
  147: 	    2dup r@ name>string drop capscomp  0=
  148: 	    IF
  149: 		r> dup r@ word-lex
  150: 		IF
  151: 		    dup prefix-found @ word-lex
  152: 		    0>=
  153: 		    IF
  154: 			rdrop dup >r
  155: 		    THEN
  156: 		THEN
  157: 		>r
  158: 	    THEN
  159: 	THEN
  160: 	r> @
  161:     REPEAT
  162:     drop r> ;
  163: 
  164: : prefix-off ( -- )  0 0 prefix-found 2! ;
  165: 
  166: : prefix-string ( addr len nfa -- addr' len' )
  167:     dup prefix-found !  ?dup
  168:     IF
  169: 	name>string rot /string rot drop
  170: 	dup 1+ prefix-found cell+ !
  171:     ELSE
  172: 	2drop s" " prefix-off
  173:     THEN ;
  174: 
  175: : search-prefix  ( addr1 len1 -- addr2 len2 )
  176:     0 vp dup @ 1- cells over +
  177:     DO  I 2@ <>
  178:         IF  I cell+ @ wordlist-id @ swap  search-voc  THEN
  179: 	[ -1 cells ] Literal +LOOP
  180:     prefix-string ;
  181: 
  182: : tib-full? ( max span addr pos addr' len' -- max span addr pos addr1 u flag )
  183:     5 pick over 4 pick + prefix-found @ 0<> - < ;
  184: 
  185: : kill-prefix  ( key -- key )
  186:   dup #tab <> IF  prefix-off  THEN ;
  187: 
  188: \ UTF-8 support
  189: 
  190: require utf-8.fs
  191: 
  192: [IFUNDEF] #esc  27 Constant #esc  [THEN]
  193: 
  194: : save-cursor ( -- )  #esc emit '7 emit ;
  195: : restore-cursor ( -- )  #esc emit '8 emit ;
  196: : .rest ( addr pos1 -- addr pos1 )
  197:     restore-cursor 2dup type ;
  198: : .all ( span addr pos1 -- span addr pos1 )
  199:     restore-cursor >r 2dup swap type r> ;
  200: 
  201: : <xins>  ( max span addr pos1 xc -- max span addr pos2 )
  202:     >r  2over r@ xc-size + u< IF  ( max span addr pos1 R:xc )
  203: 	rdrop bell  EXIT  THEN
  204:     >string over r@ xc-size + swap move
  205:     2dup chars + r@ swap r@ xc-size xc!+? 2drop drop
  206:     r> xc-size >r  rot r@ chars + -rot r> chars + ;
  207: : (xins)  ( max span addr pos1 xc -- max span addr pos2 )
  208:     <xins> .all .rest ;
  209: : xback  ( max span addr pos1 -- max span addr pos2 f )
  210:     dup  IF  over + xchar- over -  0 max .all .rest
  211:     ELSE  bell  THEN 0 ;
  212: : xforw  ( max span addr pos1 -- max span addr pos2 f )
  213:     2 pick over <> IF  over + xc@+ xemit over -  ELSE  bell  THEN 0 ;
  214: : (xdel)  ( max span addr pos1 -- max span addr pos2 )
  215:     over + dup xchar- tuck - >r over -
  216:     >string over r@ + -rot move
  217:     rot r> - -rot ;
  218: : ?xdel ( max span addr pos1 -- max span addr pos2 0 )
  219:   dup  IF  (xdel) .all 2 spaces .rest  THEN  0 ;
  220: : <xdel> ( max span addr pos1 -- max span addr pos2 0 )
  221:   2 pick over <>
  222:     IF  xforw drop (xdel) .all 2 spaces .rest
  223:     ELSE  bell  THEN  0 ;
  224: : xeof  2 pick over or 0=  IF  bye  ELSE  <xdel>  THEN ;
  225: 
  226: : xfirst-pos  ( max span addr pos1 -- max span addr 0 0 )
  227:   drop 0 .all .rest 0 ;
  228: : xend-pos  ( max span addr pos1 -- max span addr span 0 )
  229:   drop over .all 0 ;
  230: 
  231: 
  232: : xclear-line ( max span addr pos1 -- max addr )
  233:     drop restore-cursor swap spaces restore-cursor ;
  234: : xclear-tib ( max span addr pos -- max 0 addr 0 false )
  235:     xclear-line 0 tuck dup ;
  236: 
  237: : (xenter)  ( max span addr pos1 -- max span addr pos2 true )
  238:     >r end^ 2@ hist-setpos
  239:     2dup swap history write-line drop ( throw ) \ don't worry about errors
  240:     hist-pos 2dup backward^ 2! end^ 2!
  241:     r> .all space true ;
  242: 
  243: : xkill-expand ( max span addr pos1 -- max span addr pos2 )
  244:     prefix-found cell+ @ ?dup IF  >r
  245: 	r@ - >string over r@ + -rot move
  246: 	rot r@ - -rot .all r> spaces .rest THEN ;
  247: 
  248: : insert   ( string length buffer size -- )
  249:     rot over min >r  r@ - ( left over )
  250:     over dup r@ +  rot move   r> move  ;
  251: 
  252: : xtab-expand ( max span addr pos1 -- max span addr pos2 0 )
  253:     key? IF  #tab (xins) 0  EXIT  THEN
  254:     xkill-expand 2dup extract-word dup 0= IF  nip EXIT  THEN
  255:     search-prefix tib-full?
  256:     IF    bell  2drop  prefix-off
  257:     ELSE  dup >r
  258: 	2>r >string r@ + 2r> 2swap insert
  259: 	r@ + rot r> + -rot
  260:     THEN
  261:     prefix-found @ IF  bl (xins)  ELSE  .all .rest  THEN  0 ;
  262: 
  263: : xchar-history ( -- )
  264:     ['] xforw        ctrl F bindkey
  265:     ['] xback        ctrl B bindkey
  266:     ['] ?xdel        ctrl H bindkey
  267:     ['] xeof         ctrl D bindkey
  268:     ['] <xdel>       ctrl X bindkey
  269:     ['] xclear-tib   ctrl K bindkey
  270:     ['] xfirst-pos   ctrl A bindkey
  271:     ['] xend-pos     ctrl E bindkey
  272:     ['] (xenter)     #lf    bindkey
  273:     ['] (xenter)     #cr    bindkey
  274:     ['] xtab-expand  #tab   bindkey
  275:     ['] (xins)       IS insert-char
  276:     ['] kill-prefix  IS everychar
  277:     ['] save-cursor  IS everyline ;
  278: 
  279: xchar-history
  280: 
  281: \ initializing history
  282: 
  283: : get-history ( addr len -- )
  284:     ['] force-open catch
  285:     ?dup-if
  286: 	\ !! >stderr
  287:         \ history-file type ." : " .error cr
  288: 	drop 2drop
  289: 	['] false ['] false ['] (ret)
  290:     else
  291: 	to history
  292: 	history file-size throw
  293: 	2dup forward^ 2! 2dup backward^ 2! end^ 2!
  294: 	['] next-line ['] prev-line ['] (enter)
  295:     endif
  296:     dup #lf bindkey
  297:         #cr bindkey
  298:      ctrl P bindkey
  299:      ctrl N bindkey
  300: ;
  301: 
  302: : history-cold ( -- )
  303:     history-file get-history xchar-history ;
  304: 
  305: ' history-cold INIT8 chained
  306: history-cold
  307: 

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