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

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help