File:  [gforth] / gforth / history.fs
Revision 1.54: download - view: text, annotated - select for diffs
Thu Jun 7 20:57:13 2007 UTC (14 years, 11 months ago) by pazsan
Branches: MAIN
CVS tags: HEAD
Fixes for cygwin

    1: \ command line edit and history support                 16oct94py
    2: 
    3: \ Copyright (C) 1995,2000,2003,2004,2005,2006 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: defer back-restore ( u -- )
   73: ' backspaces is back-restore
   74: 
   75: [IFDEF] x-width
   76: : clear-line ( max span addr pos1 -- max addr )
   77:   back-restore over over swap x-width spaces swap back-restore ;
   78: [ELSE]
   79: : clear-line ( max span addr pos1 -- max addr )
   80:   back-restore over spaces swap back-restore ;
   81: [THEN]
   82: \ : clear-tib ( max span addr pos -- max 0 addr 0 false )
   83: \   clear-line 0 tuck dup ;
   84: 
   85: : hist-pos    ( -- ud )  history file-position drop ( throw ) ;
   86: : hist-setpos ( ud -- )  history reposition-file drop ( throw ) ;
   87: 
   88: : get-line ( addr len -- len' flag )
   89:   swap history read-line throw ;
   90: 
   91: : next-line  ( max span addr pos1 -- max span addr pos2 false )
   92:   clear-line
   93:   forward^ 2@ 2dup hist-setpos backward^ 2!
   94:   2dup get-line drop
   95:   hist-pos  forward^ 2!
   96:   tuck 2dup type 0 ;
   97: 
   98: : find-prev-line ( max addr -- max span addr pos2 )
   99:   backward^ 2@ forward^ 2!
  100:   over 2 + negate s>d backward^ 2@ d+ 0. dmax 2dup hist-setpos
  101:   BEGIN
  102:       backward^ 2!   2dup get-line  WHILE
  103:       hist-pos 2dup forward^ 2@ d<  WHILE
  104:       rot drop
  105:   REPEAT  2drop  THEN  tuck ;
  106: 
  107: : prev-line  ( max span addr pos1 -- max span addr pos2 false )
  108:     clear-line find-prev-line 2dup type 0 ;
  109: 
  110: \ Create lfpad #lf c,
  111: 
  112: : (enter)  ( max span addr pos1 -- max span addr pos2 true )
  113:   >r end^ 2@ hist-setpos
  114:   2dup swap history write-line drop ( throw ) \ don't worry about errors
  115:   hist-pos 2dup backward^ 2! end^ 2!
  116:   r> (ret) ;
  117: 
  118: : extract-word ( addr len -- addr' len' )  dup >r
  119:   BEGIN  1- dup 0>=  WHILE  2dup + c@ bl =  UNTIL  THEN  1+
  120:   tuck + r> rot - ;
  121: 
  122: Create prefix-found  0 , 0 ,
  123: 
  124: : sgn ( n -- -1/0/1 )
  125:  dup 0= IF EXIT THEN  0< 2* 1+ ;
  126: 
  127: : capscomp  ( c_addr1 u c_addr2 -- n )
  128:  swap bounds
  129:  ?DO  dup c@ I c@ <>
  130:      IF  dup c@ toupper I c@ toupper =
  131:      ELSE  true  THEN  WHILE  1+  LOOP  drop 0
  132:  ELSE  c@ toupper I c@ toupper - unloop  THEN  sgn ;
  133: 
  134: : word-lex ( nfa1 nfa2 -- -1/0/1 )
  135:     dup 0=
  136:     IF
  137: 	2drop 1  EXIT
  138:     THEN
  139:     name>string 2>r name>string
  140:     dup r@ =
  141:     IF
  142: 	rdrop r> capscomp 0<= EXIT
  143:     THEN
  144:     r> <
  145:     nip rdrop ;
  146: 
  147: : search-voc ( addr len nfa1 nfa2 -- addr len nfa3 )
  148:     >r
  149:     BEGIN
  150: 	dup
  151:     WHILE
  152: 	>r dup r@ name>string nip <=
  153: 	IF
  154: 	    2dup r@ name>string drop capscomp  0=
  155: 	    IF
  156: 		r> dup r@ word-lex
  157: 		IF
  158: 		    dup prefix-found @ word-lex
  159: 		    0>=
  160: 		    IF
  161: 			rdrop dup >r
  162: 		    THEN
  163: 		THEN
  164: 		>r
  165: 	    THEN
  166: 	THEN
  167: 	r> @
  168:     REPEAT
  169:     drop r> ;
  170: 
  171: : prefix-off ( -- )  0 0 prefix-found 2! ;
  172: 
  173: : prefix-string ( addr len nfa -- addr' len' )
  174:     dup prefix-found !  ?dup
  175:     IF
  176: 	name>string rot /string rot drop
  177: 	dup 1+ prefix-found cell+ !
  178:     ELSE
  179: 	2drop s" " prefix-off
  180:     THEN ;
  181: 
  182: : search-prefix  ( addr1 len1 -- addr2 len2 )
  183:     0 vp dup @ 1- cells over +
  184:     DO  I 2@ <>
  185:         IF  I cell+ @ wordlist-id @ swap  search-voc  THEN
  186: 	[ -1 cells ] Literal +LOOP
  187:     prefix-string ;
  188: 
  189: : tib-full? ( max span addr pos addr' len' -- max span addr pos addr1 u flag )
  190:     5 pick over 4 pick + prefix-found @ 0<> - < ;
  191: 
  192: : kill-prefix  ( key -- key )
  193:   dup #tab <> IF  prefix-off  THEN ;
  194: 
  195: \ UTF-8 support
  196: 
  197: require utf-8.fs
  198: 
  199: [IFUNDEF] #esc  27 Constant #esc  [THEN]
  200: 
  201: Variable curpos
  202: 
  203: s" os-type" environment? [IF] s" cygwin" str= [IF]
  204: : save-cursor ( -- ) #esc emit '7 emit ;
  205: : restore-cursor ( -- ) #esc emit '8 emit ;
  206: : cur-correct ( addr u -- )  2drop ;
  207: [ELSE]
  208: : at-xy? ( -- x y )
  209:     key? drop
  210:     #esc emit ." [6n"  0 0
  211:     BEGIN  key dup 'R <>  WHILE
  212: 	    dup '; = IF  drop  swap  ELSE
  213: 		dup '0 '9 1+ within  IF  '0 - swap 10 * +  ELSE
  214: 		    drop  THEN  THEN
  215:     REPEAT  drop 1- swap 1- ;
  216: : cursor@ ( -- n )  at-xy? form nip * + ;
  217: : cursor! ( n -- )  form nip /mod at-xy ;
  218: : cur-correct  ( addr u -- )  x-width curpos @ + cursor@ -
  219:     form nip >r  r@ 2/ + r@ / r> * negate curpos +! ;
  220: : save-cursor ( -- )  cursor@ curpos ! ;
  221: : restore-cursor ( -- )  curpos @ cursor! ;
  222: [THEN]
  223: : .rest ( addr pos1 -- addr pos1 )
  224:     key? ?EXIT
  225:     restore-cursor 2dup type 2dup cur-correct ;
  226: : .all ( span addr pos1 -- span addr pos1 )
  227:     key? ?EXIT
  228:     restore-cursor >r 2dup swap type 2dup swap cur-correct r> ;
  229: : xback-restore ( u -- )
  230:     drop key? ?EXIT
  231:     restore-cursor ;
  232: 
  233: \ In the following, addr max is the buffer, addr span is the current
  234: \ string in the buffer, and pos1 is the cursor position in the buffer.
  235: 
  236: : <xins>  ( max span addr pos1 xc -- max span addr pos2 )
  237:     >r  2over r@ xc-size + u< IF  ( max span addr pos1 R:xc )
  238: 	rdrop bell  EXIT  THEN
  239:     >string over r@ xc-size + swap move
  240:     2dup chars + r@ swap r@ xc-size xc!+? 2drop drop
  241:     r> xc-size >r  rot r@ chars + -rot r> chars + ;
  242: : (xins)  ( max span addr pos1 xc -- max span addr pos2 )
  243:     <xins> .all .rest ;
  244: : xback  ( max span addr pos1 -- max span addr pos2 f )
  245:     dup  IF  over + xchar- over -  0 max .all .rest
  246:     ELSE  bell  THEN 0 ;
  247: : xforw  ( max span addr pos1 -- max span addr pos2 f )
  248:     2 pick over <> IF  over + xc@+ xemit over -  ELSE  bell  THEN 0 ;
  249: : (xdel)  ( max span addr pos1 -- max span addr pos2 )
  250:     over + dup xchar- tuck - >r over -
  251:     >string over r@ + -rot move
  252:     rot r> - -rot ;
  253: : ?xdel ( max span addr pos1 -- max span addr pos2 0 )
  254:   dup  IF  (xdel) .all 2 spaces .rest  THEN  0 ;
  255: : <xdel> ( max span addr pos1 -- max span addr pos2 0 )
  256:   2 pick over <>
  257:     IF  xforw drop (xdel) .all 2 spaces .rest
  258:     ELSE  bell  THEN  0 ;
  259: : xeof  2 pick over or 0=  IF  bye  ELSE  <xdel>  THEN ;
  260: 
  261: : xfirst-pos  ( max span addr pos1 -- max span addr 0 0 )
  262:   drop 0 .all .rest 0 ;
  263: : xend-pos  ( max span addr pos1 -- max span addr span 0 )
  264:   drop over .all 0 ;
  265: 
  266: 
  267: : xclear-line ( max span addr pos1 -- max addr )
  268:     drop restore-cursor swap spaces restore-cursor ;
  269: : xclear-tib ( max span addr pos -- max 0 addr 0 false )
  270:     xclear-line 0 tuck dup ;
  271: 
  272: : (xenter)  ( max span addr pos1 -- max span addr pos2 true )
  273:     >r end^ 2@ hist-setpos
  274:     2dup swap history write-line drop ( throw ) \ don't worry about errors
  275:     hist-pos 2dup backward^ 2! end^ 2!
  276:     r> .all space true ;
  277: 
  278: : xkill-expand ( max span addr pos1 -- max span addr pos2 )
  279:     prefix-found cell+ @ ?dup IF  >r
  280: 	r@ - >string over r@ + -rot move
  281: 	rot r@ - -rot .all r> spaces .rest THEN ;
  282: 
  283: : insert   ( string length buffer size -- )
  284:     rot over min >r  r@ - ( left over )
  285:     over dup r@ +  rot move   r> move  ;
  286: 
  287: : xtab-expand ( max span addr pos1 -- max span addr pos2 0 )
  288:     key? IF  #tab (xins) 0  EXIT  THEN
  289:     xkill-expand 2dup extract-word dup 0= IF  nip EXIT  THEN
  290:     search-prefix tib-full?
  291:     IF    bell  2drop  prefix-off
  292:     ELSE  dup >r
  293: 	2>r >string r@ + 2r> 2swap insert
  294: 	r@ + rot r> + -rot
  295:     THEN
  296:     prefix-found @ IF  bl (xins)  ELSE  .all .rest  THEN  0 ;
  297: 
  298: : xchar-history ( -- )
  299:     ['] xforw        ctrl F bindkey
  300:     ['] xback        ctrl B bindkey
  301:     ['] ?xdel        ctrl H bindkey
  302:     ['] xeof         ctrl D bindkey
  303:     ['] <xdel>       ctrl X bindkey
  304:     ['] xclear-tib   ctrl K bindkey
  305:     ['] xfirst-pos   ctrl A bindkey
  306:     ['] xend-pos     ctrl E bindkey
  307:     history IF  ['] (xenter)     #lf    bindkey  THEN
  308:     history IF  ['] (xenter)     #cr    bindkey  THEN
  309:     ['] xtab-expand  #tab   bindkey
  310:     ['] (xins)       IS insert-char
  311:     ['] kill-prefix  IS everychar
  312:     ['] save-cursor  IS everyline
  313:     ['] xback-restore IS back-restore
  314: ;
  315: 
  316: xchar-history
  317: 
  318: \ initializing history
  319: 
  320: : get-history ( addr len -- )
  321:     ['] force-open catch
  322:     ?dup-if
  323: 	\ !! >stderr
  324:         \ history-file type ." : " .error cr
  325: 	drop 2drop 0 to history
  326: 	['] false ['] false ['] (ret)
  327:     else
  328: 	to history
  329: 	history file-size throw
  330: 	2dup forward^ 2! 2dup backward^ 2! end^ 2!
  331: 	['] next-line ['] prev-line ['] (enter)
  332:     endif
  333:     dup #lf bindkey
  334:         #cr bindkey
  335:      ctrl P bindkey
  336:      ctrl N bindkey
  337: ;
  338: 
  339: : history-cold ( -- )
  340:     history-file get-history xchar-history ;
  341: 
  342: :noname ( -- )
  343:     defers 'cold
  344:     history-cold
  345: ; is 'cold
  346: 
  347: history-cold
  348: 

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