[gforth] / gforth / history.fs  

gforth: gforth/history.fs


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

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help