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

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help