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