File:  [gforth] / gforth / history.fs
Revision 1.68: download - view: text, annotated - select for diffs
Mon Dec 31 17:34:58 2007 UTC (14 years, 4 months ago) by anton
Branches: MAIN
CVS tags: HEAD
updated copyright years

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

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