Annotation of gforth/history.fs, revision 1.23

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

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