Annotation of gforth/history.fs, revision 1.79

1.11      pazsan      1: \ command line edit and history support                 16oct94py
1.4       pazsan      2: 
1.77      anton       3: \ Copyright (C) 1995,2000,2003,2004,2005,2006,2007,2008,2010,2011 Free Software Foundation, Inc.
1.8       anton       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
1.69      anton       9: \ as published by the Free Software Foundation, either version 3
1.8       anton      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
1.69      anton      18: \ along with this program. If not, see http://www.gnu.org/licenses/.
1.8       anton      19: 
1.66      anton      20: : ctrl-i ( "<char>" -- c )
                     21:     char toupper $40 xor ;
                     22: 
                     23: ' ctrl-i
1.11      pazsan     24: :noname
1.66      anton      25:     ctrl-i postpone Literal ;
1.12      anton      26: interpret/compile: ctrl  ( "<char>" -- ctrl-code )
1.11      pazsan     27: 
                     28: \ command line editing                                  16oct94py
                     29: 
                     30: : >string  ( span addr pos1 -- span addr pos1 addr2 len )
                     31:   over 3 pick 2 pick chars /string ;
                     32: 
1.27      pazsan     33: : bindkey ( xt key -- )  cells ctrlkeys + ! ;
                     34: 
1.11      pazsan     35: \ history support                                       16oct94py
                     36: 
1.13      anton      37: 0 Value history \ history file fid
1.4       pazsan     38: 
                     39: 2Variable forward^
                     40: 2Variable backward^
                     41: 2Variable end^
                     42: 
1.13      anton      43: : force-open ( addr len -- fid )
1.19      anton      44:     2dup r/w open-file
                     45:     IF
                     46:        drop r/w create-file throw
                     47:     ELSE
                     48:        nip nip
                     49:     THEN ;
1.4       pazsan     50: 
1.29      anton      51: s" os-class" environment? [IF] s" unix" str= [ELSE] true [THEN] 
1.15      jwilke     52: [IF]
1.16      pazsan     53: : history-file ( -- addr u )
1.14      pazsan     54:     s" GFORTHHIST" getenv dup 0= IF
                     55:        2drop s" ~/.gforth-history"
1.16      pazsan     56:     THEN ;
1.15      jwilke     57: [ELSE]
                     58: 
1.16      pazsan     59: : history-dir ( -- addr u )
1.15      jwilke     60:   s" TMP" getenv ?dup ?EXIT drop
                     61:   s" TEMP" getenv ?dup ?EXIT drop
                     62:   s" c:/" ;
                     63: 
1.16      pazsan     64: : history-file ( -- addr u )
1.15      jwilke     65:   s" GFORTHHIST" getenv ?dup ?EXIT
                     66:   drop
                     67:   history-dir pad place
1.16      pazsan     68:   s" /ghist.fs" pad +place pad count ;
                     69: [THEN]
1.15      jwilke     70: 
1.4       pazsan     71: \ moving in history file                               16oct94py
                     72: 
1.44      anton      73: defer back-restore ( u -- )
1.55      pazsan     74: defer cur-correct ( addr u -- )
                     75: ' backspaces IS back-restore
                     76: ' 2drop IS cur-correct
1.44      anton      77: 
1.63      pazsan     78: Variable linew
                     79: Variable screenw
1.67      anton      80: : linew-off  linew off cols screenw ! ;
1.63      pazsan     81: 
1.47      pazsan     82: [IFDEF] x-width
1.4       pazsan     83: : clear-line ( max span addr pos1 -- max addr )
1.63      pazsan     84:     drop linew @ back-restore over over swap x-width
                     85:     dup spaces back-restore nip linew off ;
1.47      pazsan     86: [ELSE]
                     87: : clear-line ( max span addr pos1 -- max addr )
                     88:   back-restore over spaces swap back-restore ;
                     89: [THEN]
1.41      anton      90: \ : clear-tib ( max span addr pos -- max 0 addr 0 false )
                     91: \   clear-line 0 tuck dup ;
1.4       pazsan     92: 
1.32      pazsan     93: : hist-pos    ( -- ud )  history file-position drop ( throw ) ;
                     94: : hist-setpos ( ud -- )  history reposition-file drop ( throw ) ;
1.4       pazsan     95: 
1.7       pazsan     96: : get-line ( addr len -- len' flag )
                     97:   swap history read-line throw ;
                     98: 
1.4       pazsan     99: : next-line  ( max span addr pos1 -- max span addr pos2 false )
                    100:   clear-line
1.7       pazsan    101:   forward^ 2@ 2dup hist-setpos backward^ 2!
                    102:   2dup get-line drop
                    103:   hist-pos  forward^ 2!
1.55      pazsan    104:   tuck 2dup type 2dup cur-correct 0 ;
1.4       pazsan    105: 
1.20      pazsan    106: : find-prev-line ( max addr -- max span addr pos2 )
                    107:   backward^ 2@ forward^ 2!
1.7       pazsan    108:   over 2 + negate s>d backward^ 2@ d+ 0. dmax 2dup hist-setpos
                    109:   BEGIN
                    110:       backward^ 2!   2dup get-line  WHILE
                    111:       hist-pos 2dup forward^ 2@ d<  WHILE
                    112:       rot drop
1.20      pazsan    113:   REPEAT  2drop  THEN  tuck ;
                    114: 
                    115: : prev-line  ( max span addr pos1 -- max span addr pos2 false )
1.55      pazsan    116:     clear-line find-prev-line 2dup type 2dup cur-correct 0 ;
1.4       pazsan    117: 
1.41      anton     118: \ Create lfpad #lf c,
1.4       pazsan    119: 
                    120: : (enter)  ( max span addr pos1 -- max span addr pos2 true )
1.78      pazsan    121:     >r 2dup swap -trailing nip IF
                    122:        end^ 2@ hist-setpos
                    123:        2dup swap history write-line drop
                    124:        hist-pos 2dup backward^ 2! end^ 2!
                    125:     THEN  r> (ret) ;
1.4       pazsan    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: 
1.30      anton     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: 
1.4       pazsan    143: : word-lex ( nfa1 nfa2 -- -1/0/1 )
1.9       anton     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 ;
1.4       pazsan    155: 
1.9       anton     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
1.7       pazsan    174:            THEN
1.9       anton     175:        THEN
                    176:        r> @
                    177:     REPEAT
                    178:     drop r> ;
1.7       pazsan    179: 
1.35      pazsan    180: : prefix-off ( -- )  0 0 prefix-found 2! ;
                    181: 
1.7       pazsan    182: : prefix-string ( addr len nfa -- addr' len' )
                    183:     dup prefix-found !  ?dup
1.9       anton     184:     IF
                    185:        name>string rot /string rot drop
1.7       pazsan    186:        dup 1+ prefix-found cell+ !
                    187:     ELSE
1.35      pazsan    188:        2drop s" " prefix-off
1.7       pazsan    189:     THEN ;
                    190: 
                    191: : search-prefix  ( addr1 len1 -- addr2 len2 )
                    192:     0 vp dup @ 1- cells over +
                    193:     DO  I 2@ <>
1.17      pazsan    194:         IF  I cell+ @ wordlist-id @ swap  search-voc  THEN
1.7       pazsan    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<> - < ;
1.4       pazsan    200: 
                    201: : kill-prefix  ( key -- key )
1.36      pazsan    202:   dup #tab <> IF  prefix-off  THEN ;
1.33      pazsan    203: 
                    204: \ UTF-8 support
                    205: 
1.38      anton     206: require utf-8.fs
1.33      pazsan    207: 
1.39      anton     208: [IFUNDEF] #esc  27 Constant #esc  [THEN]
                    209: 
1.79    ! pazsan    210: Defer at-deltaxy
        !           211: : vt100-at-deltaxy ( dx dy -- )
1.71      pazsan    212:     over 0< over 0= and IF  drop abs backspaces  EXIT  THEN
                    213:     base @ >r decimal
1.63      pazsan    214:     ?dup IF
                    215:        #esc emit '[ emit  dup abs 0 .r 0< IF  'A  ELSE  'B  THEN  emit
                    216:     THEN
                    217:     ?dup IF
                    218:        #esc emit '[ emit  dup abs 0 .r 0< IF  'D  ELSE  'C  THEN  emit
1.64      pazsan    219:     THEN  r> base ! ;
1.79    ! pazsan    220: ' vt100-at-deltaxy IS at-deltaxy
1.62      pazsan    221: 
1.63      pazsan    222: \ : cygwin? ( -- flag ) s" TERM" getenv s" cygwin" str= ;
                    223: \ : at-xy? ( -- x y )
                    224: \     key? drop \ make sure prep_terminal() is executed
                    225: \     #esc emit ." [6n"  0 0
                    226: \     BEGIN  key dup 'R <>  WHILE
                    227: \          dup '; = IF  drop  swap  ELSE
                    228: \              dup '0 '9 1+ within  IF  '0 - swap 10 * +  ELSE
                    229: \                  drop  THEN  THEN
                    230: \     REPEAT  drop 1- swap 1- ;
                    231: \ : cursor@ ( -- n )  at-xy? screenw @ * + ;
                    232: \ : cursor! ( n -- )  screenw @ /mod at-xy ;
                    233: : xcur-correct  ( addr u -- )  x-width linew ! ;
1.56      pazsan    234: 
1.55      pazsan    235: ' xcur-correct IS cur-correct
                    236: 
1.63      pazsan    237: : xback-restore ( u -- )
1.76      pazsan    238:     dup screenw @ mod 0= IF  1- 0 max  THEN
                    239:     \ correction for line=screenw, no wraparound then!
1.63      pazsan    240:     screenw @ /mod negate swap negate swap at-deltaxy ;
1.39      anton     241: : .rest ( addr pos1 -- addr pos1 )
1.63      pazsan    242:     linew @ xback-restore 2dup type 2dup cur-correct ;
1.39      anton     243: : .all ( span addr pos1 -- span addr pos1 )
1.63      pazsan    244:     linew @ xback-restore >r 2dup swap type 2dup swap cur-correct r> ;
1.44      anton     245: 
1.63      pazsan    246: : xretype ( max span addr pos1 -- max span addr pos1 f )
1.67      anton     247:     .all cols screenw @ >r screenw !
1.63      pazsan    248:     linew @ screenw @ / linew @ r@ / max
                    249:     screenw @ r> - * 0 max
                    250:     dup spaces linew +! .rest false ;
1.58      pazsan    251: 
1.44      anton     252: \ In the following, addr max is the buffer, addr span is the current
                    253: \ string in the buffer, and pos1 is the cursor position in the buffer.
1.39      anton     254: 
1.40      anton     255: : <xins>  ( max span addr pos1 xc -- max span addr pos2 )
                    256:     >r  2over r@ xc-size + u< IF  ( max span addr pos1 R:xc )
                    257:        rdrop bell  EXIT  THEN
                    258:     >string over r@ xc-size + swap move
                    259:     2dup chars + r@ swap r@ xc-size xc!+? 2drop drop
                    260:     r> xc-size >r  rot r@ chars + -rot r> chars + ;
                    261: : (xins)  ( max span addr pos1 xc -- max span addr pos2 )
1.64      pazsan    262:     <xins> key? 0= IF  .all .rest  THEN ;
1.40      anton     263: : xback  ( max span addr pos1 -- max span addr pos2 f )
                    264:     dup  IF  over + xchar- over -  0 max .all .rest
1.39      anton     265:     ELSE  bell  THEN 0 ;
1.40      anton     266: : xforw  ( max span addr pos1 -- max span addr pos2 f )
1.63      pazsan    267:     2 pick over <> IF  over + xc@+ xemit over -  ELSE  bell  THEN
                    268:     2dup cur-correct 0 ;
1.40      anton     269: : (xdel)  ( max span addr pos1 -- max span addr pos2 )
                    270:     over + dup xchar- tuck - >r over -
1.39      anton     271:     >string over r@ + -rot move
                    272:     rot r> - -rot ;
1.40      anton     273: : ?xdel ( max span addr pos1 -- max span addr pos2 0 )
1.63      pazsan    274:   dup  IF  (xdel) .all 2 spaces 2 linew +! .rest  THEN  0 ;
1.40      anton     275: : <xdel> ( max span addr pos1 -- max span addr pos2 0 )
1.39      anton     276:   2 pick over <>
1.63      pazsan    277:     IF  xforw drop (xdel) .all 2 spaces 2 linew +! .rest
1.39      anton     278:     ELSE  bell  THEN  0 ;
1.40      anton     279: : xeof  2 pick over or 0=  IF  bye  ELSE  <xdel>  THEN ;
1.39      anton     280: 
1.40      anton     281: : xfirst-pos  ( max span addr pos1 -- max span addr 0 0 )
1.39      anton     282:   drop 0 .all .rest 0 ;
1.40      anton     283: : xend-pos  ( max span addr pos1 -- max span addr span 0 )
1.39      anton     284:   drop over .all 0 ;
                    285: 
1.73      pazsan    286: : xclear-rest ( max span addr pos -- max pos addr pos false )
                    287:      rot >r tuck 2dup r> swap /string u8width dup spaces linew +! .all 0 ;
1.39      anton     288: 
1.40      anton     289: : (xenter)  ( max span addr pos1 -- max span addr pos2 true )
1.78      pazsan    290:     >r 2dup swap -trailing nip IF
                    291:        end^ 2@ hist-setpos
                    292:        2dup swap history write-line drop ( throw ) \ don't worry about errors
                    293:        hist-pos 2dup backward^ 2! end^ 2!
                    294:     THEN  r> .all space true ;
1.39      anton     295: 
1.40      anton     296: : xkill-expand ( max span addr pos1 -- max span addr pos2 )
1.39      anton     297:     prefix-found cell+ @ ?dup IF  >r
                    298:        r@ - >string over r@ + -rot move
1.63      pazsan    299:        rot r@ - -rot .all r@ spaces r> back-restore .rest THEN ;
1.39      anton     300: 
1.75      pazsan    301: [IFUNDEF] insert
1.39      anton     302: : insert   ( string length buffer size -- )
                    303:     rot over min >r  r@ - ( left over )
                    304:     over dup r@ +  rot move   r> move  ;
1.75      pazsan    305: [THEN]
1.39      anton     306: 
1.40      anton     307: : xtab-expand ( max span addr pos1 -- max span addr pos2 0 )
                    308:     key? IF  #tab (xins) 0  EXIT  THEN
                    309:     xkill-expand 2dup extract-word dup 0= IF  nip EXIT  THEN
1.39      anton     310:     search-prefix tib-full?
1.40      anton     311:     IF    bell  2drop  prefix-off
1.39      anton     312:     ELSE  dup >r
                    313:        2>r >string r@ + 2r> 2swap insert
                    314:        r@ + rot r> + -rot
                    315:     THEN
1.40      anton     316:     prefix-found @ IF  bl (xins)  ELSE  .all .rest  THEN  0 ;
1.39      anton     317: 
1.40      anton     318: : xchar-history ( -- )
                    319:     ['] xforw        ctrl F bindkey
                    320:     ['] xback        ctrl B bindkey
                    321:     ['] ?xdel        ctrl H bindkey
                    322:     ['] xeof         ctrl D bindkey
                    323:     ['] <xdel>       ctrl X bindkey
1.73      pazsan    324:     ['] xclear-rest  ctrl K bindkey
1.40      anton     325:     ['] xfirst-pos   ctrl A bindkey
                    326:     ['] xend-pos     ctrl E bindkey
1.61      pazsan    327:     ['] xretype      ctrl L bindkey
1.49      pazsan    328:     history IF  ['] (xenter)     #lf    bindkey  THEN
                    329:     history IF  ['] (xenter)     #cr    bindkey  THEN
1.40      anton     330:     ['] xtab-expand  #tab   bindkey
                    331:     ['] (xins)       IS insert-char
1.39      anton     332:     ['] kill-prefix  IS everychar
1.70      anton     333: [ifdef] everyline
1.63      pazsan    334:     ['] linew-off     IS everyline
1.70      anton     335: [endif]
1.44      anton     336:     ['] xback-restore IS back-restore
1.55      pazsan    337:     ['] xcur-correct  IS cur-correct
1.44      anton     338: ;
1.39      anton     339: 
1.40      anton     340: xchar-history
1.19      anton     341: 
                    342: \ initializing history
                    343: 
                    344: : get-history ( addr len -- )
                    345:     ['] force-open catch
                    346:     ?dup-if
                    347:        \ !! >stderr
1.21      pazsan    348:         \ history-file type ." : " .error cr
1.49      pazsan    349:        drop 2drop 0 to history
1.19      anton     350:        ['] false ['] false ['] (ret)
                    351:     else
                    352:        to history
                    353:        history file-size throw
                    354:        2dup forward^ 2! 2dup backward^ 2! end^ 2!
                    355:        ['] next-line ['] prev-line ['] (enter)
                    356:     endif
1.27      pazsan    357:     dup #lf bindkey
                    358:         #cr bindkey
                    359:      ctrl P bindkey
                    360:      ctrl N bindkey
1.19      anton     361: ;
                    362: 
                    363: : history-cold ( -- )
1.42      pazsan    364:     history-file get-history xchar-history ;
1.19      anton     365: 
1.48      anton     366: :noname ( -- )
                    367:     defers 'cold
                    368:     history-cold
                    369: ; is 'cold
                    370: 
1.19      anton     371: history-cold
                    372: 

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