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