File:  [gforth] / gforth / history.fs
Revision 1.22: download - view: text, annotated - select for diffs
Mon Jul 31 19:56:08 2000 UTC (20 years, 9 months ago) by pazsan
Branches: MAIN
CVS tags: HEAD
Removed BYE behavior of DEL key and Ctlr-D on empty lines
Added FSF style copyright notice to arch/386/asm.fs

    1: \ command line edit and history support                 16oct94py
    2: 
    3: \ Copyright (C) 1995 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., 675 Mass Ave, Cambridge, MA 02139, 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: : type-rest ( span addr pos1 -- span addr pos1 back )
   32:   >string tuck type ;
   33: : (del)  ( max span addr pos1 -- max span addr pos2 )
   34:   1- >string over 1+ -rot move
   35:   rot 1- -rot  #bs emit  type-rest bl emit 1+ backspaces ;
   36: : (ins)  ( max span addr pos1 char -- max span addr pos2 )
   37:   >r >string over 1+ swap move 2dup chars + r> swap c!
   38:   rot 1+ -rot type-rest 1- backspaces 1+ ;
   39: : ?del ( max span addr pos1 -- max span addr pos2 0 )
   40:   dup  IF  (del)  THEN  0 ;
   41: : (ret)  type-rest drop true space ;
   42: : back  dup  IF  1- #bs emit  ELSE  #bell emit  THEN 0 ;
   43: : forw 2 pick over <> IF  2dup + c@ emit 1+  ELSE  #bell emit  THEN 0 ;
   44: : eof ( max span addr pos1 -- max span addr pos2 0 )
   45:     2 pick over <>
   46:     IF  forw drop (del)  ELSE  #bell emit  THEN  0 ;
   47: 
   48: ' forw  ctrl F cells ctrlkeys + !
   49: ' back  ctrl B cells ctrlkeys + !
   50: ' ?del  ctrl H cells ctrlkeys + !
   51: ' eof   ctrl D cells ctrlkeys + !
   52: 
   53: ' (ins) IS insert-char
   54: 
   55: \ history support                                       16oct94py
   56: 
   57: 0 Value history \ history file fid
   58: 
   59: 2Variable forward^
   60: 2Variable backward^
   61: 2Variable end^
   62: 
   63: : force-open ( addr len -- fid )
   64:     2dup r/w open-file
   65:     IF
   66: 	drop r/w create-file throw
   67:     ELSE
   68: 	nip nip
   69:     THEN ;
   70: 
   71: s" os-class" environment? [IF] s" unix" compare 0= [ELSE] true [THEN] 
   72: [IF]
   73: : history-file ( -- addr u )
   74:     s" GFORTHHIST" getenv dup 0= IF
   75: 	2drop s" ~/.gforth-history"
   76:     THEN ;
   77: [ELSE]
   78: 
   79: : history-dir ( -- addr u )
   80:   s" TMP" getenv ?dup ?EXIT drop
   81:   s" TEMP" getenv ?dup ?EXIT drop
   82:   s" c:/" ;
   83: 
   84: : history-file ( -- addr u )
   85:   s" GFORTHHIST" getenv ?dup ?EXIT
   86:   drop
   87:   history-dir pad place
   88:   s" /ghist.fs" pad +place pad count ;
   89: [THEN]
   90: 
   91: \ moving in history file                               16oct94py
   92: 
   93: : clear-line ( max span addr pos1 -- max addr )
   94:   backspaces over spaces swap backspaces ;
   95: 
   96: : clear-tib ( max span addr pos -- max 0 addr 0 false )
   97:   clear-line 0 tuck dup ;
   98: 
   99: : hist-pos    ( -- ud )  history file-position throw ;
  100: : hist-setpos ( ud -- )  history reposition-file throw ;
  101: 
  102: : get-line ( addr len -- len' flag )
  103:   swap history read-line throw ;
  104: 
  105: : next-line  ( max span addr pos1 -- max span addr pos2 false )
  106:   clear-line
  107:   forward^ 2@ 2dup hist-setpos backward^ 2!
  108:   2dup get-line drop
  109:   hist-pos  forward^ 2!
  110:   tuck 2dup type 0 ;
  111: 
  112: : find-prev-line ( max addr -- max span addr pos2 )
  113:   backward^ 2@ forward^ 2!
  114:   over 2 + negate s>d backward^ 2@ d+ 0. dmax 2dup hist-setpos
  115:   BEGIN
  116:       backward^ 2!   2dup get-line  WHILE
  117:       hist-pos 2dup forward^ 2@ d<  WHILE
  118:       rot drop
  119:   REPEAT  2drop  THEN  tuck ;
  120: 
  121: : prev-line  ( max span addr pos1 -- max span addr pos2 false )
  122:     clear-line find-prev-line 2dup type 0 ;
  123: 
  124: Create lfpad #lf c,
  125: 
  126: : (enter)  ( max span addr pos1 -- max span addr pos2 true )
  127:   >r end^ 2@ hist-setpos
  128:   2dup swap history write-line throw
  129:   hist-pos 2dup backward^ 2! end^ 2!
  130:   r> (ret) ;
  131: 
  132: \ some other key commands                              16oct94py
  133: 
  134: : first-pos  ( max span addr pos1 -- max span addr 0 0 )
  135:   backspaces 0 0 ;
  136: : end-pos  ( max span addr pos1 -- max span addr span 0 )
  137:   type-rest 2drop over 0 ;
  138: 
  139: : extract-word ( addr len -- addr' len' )  dup >r
  140:   BEGIN  1- dup 0>=  WHILE  2dup + c@ bl =  UNTIL  THEN  1+
  141:   tuck + r> rot - ;
  142: 
  143: Create prefix-found  0 , 0 ,
  144: 
  145: : word-lex ( nfa1 nfa2 -- -1/0/1 )
  146:     dup 0=
  147:     IF
  148: 	2drop 1  EXIT
  149:     THEN
  150:     name>string 2>r name>string
  151:     dup r@ =
  152:     IF
  153: 	rdrop r> capscomp 0<= EXIT
  154:     THEN
  155:     r> <
  156:     nip rdrop ;
  157: 
  158: : search-voc ( addr len nfa1 nfa2 -- addr len nfa3 )
  159:     >r
  160:     BEGIN
  161: 	dup
  162:     WHILE
  163: 	>r dup r@ name>string nip <=
  164: 	IF
  165: 	    2dup r@ name>string drop capscomp  0=
  166: 	    IF
  167: 		r> dup r@ word-lex
  168: 		IF
  169: 		    dup prefix-found @ word-lex
  170: 		    0>=
  171: 		    IF
  172: 			rdrop dup >r
  173: 		    THEN
  174: 		THEN
  175: 		>r
  176: 	    THEN
  177: 	THEN
  178: 	r> @
  179:     REPEAT
  180:     drop r> ;
  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-found cell+ 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: : kill-expand ( max span addr pos1 -- max span addr pos2 )
  199:     prefix-found cell+ @  0 ?DO  (del)  LOOP ;
  200: 
  201: : tib-full? ( max span addr pos addr' len' -- max span addr pos addr1 u flag )
  202:     5 pick over 4 pick + prefix-found @ 0<> - < ;
  203: 
  204: : tab-expand ( max span addr pos1 -- max span addr pos2 0 )
  205:     kill-expand  2dup extract-word search-prefix
  206:     tib-full?
  207:     IF    7 emit  2drop  0 0 prefix-found 2!
  208:     ELSE  bounds ?DO  I c@ (ins)  LOOP  THEN
  209:     prefix-found @ IF  bl (ins)  THEN  0 ;
  210: 
  211: : kill-prefix  ( key -- key )
  212:   dup #tab <> IF  0 0 prefix-found 2!  THEN ;
  213: 
  214: ' kill-prefix IS everychar
  215: 
  216: ' next-line  ctrl N cells ctrlkeys + !
  217: ' prev-line  ctrl P cells ctrlkeys + !
  218: ' clear-tib  ctrl K cells ctrlkeys + !
  219: ' first-pos  ctrl A cells ctrlkeys + !
  220: ' end-pos    ctrl E cells ctrlkeys + !
  221: ' (enter)    #lf    cells ctrlkeys + !
  222: ' (enter)    #cr    cells ctrlkeys + !
  223: ' tab-expand #tab   cells ctrlkeys + !
  224: 
  225: \ initializing history
  226: 
  227: : get-history ( addr len -- )
  228:     ['] force-open catch
  229:     ?dup-if
  230: 	\ !! >stderr
  231:         \ history-file type ." : " .error cr
  232: 	drop 2drop
  233: 	['] false ['] false ['] (ret)
  234:     else
  235: 	to history
  236: 	history file-size throw
  237: 	2dup forward^ 2! 2dup backward^ 2! end^ 2!
  238: 	['] next-line ['] prev-line ['] (enter)
  239:     endif
  240:     dup
  241:     [ #lf    cells ctrlkeys + ]L !
  242:     [ #cr    cells ctrlkeys + ]L !
  243:     [ ctrl P cells ctrlkeys + ]L !
  244:     [ ctrl N cells ctrlkeys + ]L !
  245: ;
  246: 
  247: : history-cold ( -- )
  248:     history-file get-history ;
  249: 
  250: ' history-cold INIT8 chained
  251: history-cold
  252: 

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