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

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help