Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | \ ========== Copyright Header Begin ========================================== |
2 | \ | |
3 | \ Hypervisor Software File: fileed.fth | |
4 | \ | |
5 | \ Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved. | |
6 | \ | |
7 | \ - Do no alter or remove copyright notices | |
8 | \ | |
9 | \ - Redistribution and use of this software in source and binary forms, with | |
10 | \ or without modification, are permitted provided that the following | |
11 | \ conditions are met: | |
12 | \ | |
13 | \ - Redistribution of source code must retain the above copyright notice, | |
14 | \ this list of conditions and the following disclaimer. | |
15 | \ | |
16 | \ - Redistribution in binary form must reproduce the above copyright notice, | |
17 | \ this list of conditions and the following disclaimer in the | |
18 | \ documentation and/or other materials provided with the distribution. | |
19 | \ | |
20 | \ Neither the name of Sun Microsystems, Inc. or the names of contributors | |
21 | \ may be used to endorse or promote products derived from this software | |
22 | \ without specific prior written permission. | |
23 | \ | |
24 | \ This software is provided "AS IS," without a warranty of any kind. | |
25 | \ ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, | |
26 | \ INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A | |
27 | \ PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN | |
28 | \ MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR | |
29 | \ ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR | |
30 | \ DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN | |
31 | \ OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR | |
32 | \ FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE | |
33 | \ DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, | |
34 | \ ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF | |
35 | \ SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. | |
36 | \ | |
37 | \ You acknowledge that this software is not designed, licensed or | |
38 | \ intended for use in the design, construction, operation or maintenance of | |
39 | \ any nuclear facility. | |
40 | \ | |
41 | \ ========== Copyright Header End ============================================ | |
42 | \ fileed.fth 2.7 99/05/04 | |
43 | \ Copyright 1985-1994 Bradley Forthware | |
44 | ||
45 | \ Command line editing. See "install-line-editing" for functions | |
46 | \ implemented and key bindings | |
47 | ||
48 | only forth also hidden also | |
49 | hidden definitions | |
50 | ||
51 | decimal | |
52 | ||
53 | headerless | |
54 | ||
55 | \ Variables and values describing the state of the edit | |
56 | ||
57 | 0 value buf-start-adr \ address of start of input buffer | |
58 | nuser buflen \ current size of input buffer | |
59 | 0 value bufmax \ maximum size of input buffer | |
60 | ||
61 | 0 value line-start-adr \ address of start of input buffer | |
62 | nuser linelen \ current size of input line | |
63 | ||
64 | 0 value #before \ position of cursor within line | |
65 | ||
66 | nuser line# | |
67 | ||
68 | true value display? \ Turns display update on or off | |
69 | false value accepting? \ Turns line number display on or off | |
70 | false value deny-history? \ Turns off history access for security | |
71 | ||
72 | \ Positonal information derived from the basic information | |
73 | ||
74 | : #after ( -- n ) linelen @ #before - ; | |
75 | : cursor-adr ( -- adr ) line-start-adr #before + ; | |
76 | : after ( -- adr len ) cursor-adr #after ; | |
77 | : buf-extent ( -- adr len ) buf-start-adr buflen @ ; | |
78 | : buf#after ( -- n ) buf-extent + cursor-adr - ; | |
79 | : line-end-adr ( -- adr ) after + ; | |
80 | ||
81 | : on-command-line? ( -- flag ) \ True when cursor is on the last line | |
82 | accepting? buf-extent + line-end-adr = and | |
83 | ; | |
84 | ||
85 | : beep ( -- ) display? if control G (emit then ; | |
86 | ||
87 | \ Move backward n positions | |
88 | : -chars ( n -- ) 0 ?do display? if bs (emit -1 #out +! then loop ; | |
89 | ||
90 | \ Move forward n positions (retyping the characters as we move over them) | |
91 | : +chars ( n -- ) display? if cursor-adr swap type else drop then ; | |
92 | ||
93 | : .spaces ( n -- ) display? if spaces else drop then ; | |
94 | ||
95 | \ Redisplay the remainder of the line, clearing out "#deleted" spaces | |
96 | \ at then end. This is used after having deleted "#deleted" characters | |
97 | \ at the cursor position. | |
98 | : .trailing ( #deleted -- ) | |
99 | #after +chars dup .spaces -chars #after -chars | |
100 | ; | |
101 | ||
102 | \ Move forward "#chars" positions, but stop at the end of the line. | |
103 | : forward-characters ( #chars -- ) | |
104 | #after min dup +chars #before + is #before | |
105 | ; | |
106 | ||
107 | \ Move backward "#chars" positions, but stop at the beginning of the line. | |
108 | : backward-characters ( #chars -- ) | |
109 | #before min dup -chars #before swap - is #before | |
110 | ; | |
111 | ||
112 | 81 buffer: kill-buffer | |
113 | ||
114 | \ Deletes "#chars" characters after the cursor. This affects the characters | |
115 | \ in the buffer, but does not update the screen display. It will delete | |
116 | \ newline characters the same as any others. | |
117 | ||
118 | : (erase-characters) ( #chars -- ) | |
119 | >r | |
120 | r@ 1 > if cursor-adr r@ kill-buffer place then | |
121 | cursor-adr dup r@ + swap buf#after r@ - cmove \ Remove from buffer | |
122 | r> negate buflen +! | |
123 | ; | |
124 | ||
125 | \ Inserts characters from "adr len" into the buffer, up to the amount | |
126 | \ of space remaining in the buffer. #inserted is the number that | |
127 | \ were actually inserted. Does not update the display. | |
128 | ||
129 | : (insert-characters) ( adr len -- #inserted ) | |
130 | dup buflen @ + bufmax <= if ( adr len ) | |
131 | dup buflen +! dup linelen +! ( adr len ) | |
132 | cursor-adr 2dup + ( adr len src-addr dst-addr ) | |
133 | buf#after 3 pick - cmove> ( adr len ) | |
134 | tuck cursor-adr swap cmove ( len=#inserted ) | |
135 | else | |
136 | 2drop 0 ( 0 ) | |
137 | then | |
138 | ; | |
139 | ||
140 | \ Finds the line length. Used after moving to a new line. Internal. | |
141 | ||
142 | : update-linelen ( -- ) | |
143 | buf#after 0 ?do | |
144 | cursor-adr i ca+ c@ newline = ?leave | |
145 | 1 linelen +! | |
146 | loop | |
147 | ; | |
148 | : set-linelen ( -- ) 0 linelen ! update-linelen ; | |
149 | ||
150 | : (to-command-line) ( -- ) | |
151 | 0 is #before | |
152 | begin | |
153 | line# @ 0< | |
154 | while | |
155 | line-end-adr 1+ is line-start-adr | |
156 | set-linelen | |
157 | 1 line# +! | |
158 | repeat | |
159 | ; | |
160 | ||
161 | : ?copyline ( -- ) | |
162 | line# @ 0< if | |
163 | #before line-start-adr linelen @ ( cursor adr len ) | |
164 | (to-command-line) ( cursor adr len ) | |
165 | #after if | |
166 | #after (erase-characters) | |
167 | 0 linelen ! | |
168 | then ( cursor adr len ) | |
169 | (insert-characters) drop ( cursor ) | |
170 | is #before | |
171 | then | |
172 | ; | |
173 | ||
174 | \ Erases characters within a line and redisplays the rest of the line. | |
175 | \ "#chars" must not be more than "#after" | |
176 | ||
177 | : erase-characters ( #chars -- ) | |
178 | ?copyline dup (erase-characters) dup negate linelen +! .trailing | |
179 | ; | |
180 | ||
181 | \ Inserts characters from "adr len" into the buffer, and redisplays | |
182 | \ the rest of the line. | |
183 | ||
184 | : insert-characters ( adr len -- ) | |
185 | ?copyline | |
186 | (insert-characters) ( #inserted ) forward-characters 0 .trailing | |
187 | ; | |
188 | ||
189 | nuser ch \ One-element array used to convert character to "adr len" | |
190 | : insert-character ( char -- ) ch c! ch 1 insert-characters ; | |
191 | ||
192 | : forward-character ( -- ) 1 forward-characters ; | |
193 | ||
194 | : backward-character ( -- ) 1 backward-characters ; | |
195 | ||
196 | : erase-next-character ( -- ) #after 1 min erase-characters ; | |
197 | ||
198 | : erase-previous-character ( -- ) | |
199 | #before 1 min dup backward-characters erase-characters | |
200 | ; | |
201 | ||
202 | : beginning-of-line ( -- ) #before backward-characters ; | |
203 | ||
204 | : end-of-line ( -- ) #after forward-characters ; | |
205 | ||
206 | : beginning-of-file ( -- ) | |
207 | 0 line# ! | |
208 | buf-start-adr is line-start-adr | |
209 | 0 is #before | |
210 | set-linelen | |
211 | ; | |
212 | ||
213 | \ EMACS-style "kill-line". If executed in the middle of a line, kills | |
214 | \ the rest of the line. If executed at the end of a line, kills the | |
215 | \ "newline", thus joining the next line to the end of the current one. | |
216 | ||
217 | : kill-to-end-of-line ( -- ) | |
218 | #after ?dup if | |
219 | erase-characters \ Kill rest of line | |
220 | else | |
221 | accepting? 0= if | |
222 | buf#after 1 min (erase-characters) \ Join lines | |
223 | update-linelen 0 .trailing | |
224 | then | |
225 | then | |
226 | ; | |
227 | ||
228 | \ Displays a line number. | |
229 | : .num ( n -- ) | |
230 | accepting? display? 0= or if | |
231 | drop | |
232 | else | |
233 | push-decimal | |
234 | (cr 4 u.r ." : " | |
235 | pop-base | |
236 | then | |
237 | ; | |
238 | ||
239 | \ Displays the current line number. | |
240 | : .line# ( -- ) line# @ .num ; | |
241 | ||
242 | \ Redisplays the current line | |
243 | : retype-line ( -- ) | |
244 | cr .line# line-start-adr #before type 0 .trailing | |
245 | ; | |
246 | ||
247 | \ Locates the beginning of the previous (blank-delimited) word. | |
248 | \ Doesn't move the cursor or change the display. Internal. | |
249 | ||
250 | : find-previous-word ( -- adr ) | |
251 | line-start-adr dup cursor-adr 1- ?do ( linestart ) | |
252 | i c@ bl <> if drop i leave then | |
253 | -1 +loop | |
254 | ( nonblank-adr ) | |
255 | line-start-adr dup rot ?do ( linestart ) | |
256 | i c@ bl = if drop i 1+ leave then | |
257 | -1 +loop | |
258 | ; | |
259 | ||
260 | \ Locates the beginning of the next (blank-delimited) word. | |
261 | \ Doesn't move the cursor or change the display. Internal. | |
262 | ||
263 | : find-next-word ( -- adr ) | |
264 | line-end-adr dup cursor-adr ?do ( bufend-adr ) | |
265 | i c@ bl = if drop i leave then | |
266 | loop | |
267 | line-end-adr dup rot ?do ( bufend-adr ) | |
268 | i c@ bl <> if drop i leave then | |
269 | loop | |
270 | ; | |
271 | ||
272 | \ Displays a line in-place, erasing any characters left over from the | |
273 | \ line that was previously displayed there. Leaves the cursor at | |
274 | \ the end of the line. Internal. | |
275 | ||
276 | : display-line ( previous-length -- ) | |
277 | 0 is #before \ Cursor to beginning of line ( prev-len ) | |
278 | ||
279 | \ Find the end of the line | |
280 | set-linelen ( prev-len ) | |
281 | ||
282 | \ Display the line | |
283 | display? if ( prev-len ) | |
284 | .line# ( prev-len ) | |
285 | after type ( prev-len ) | |
286 | linelen @ - 0 max dup .spaces -chars ( ) | |
287 | else ( prev-len ) | |
288 | drop ( ) | |
289 | then | |
290 | ||
291 | linelen @ is #before \ Leave cursor at the end of the line | |
292 | ; | |
293 | ||
294 | : last-line? ( -- flag ) line-end-adr buf-extent + >= ; | |
295 | ||
296 | \ Goes to the next line, if there is one, and scrolls the display. | |
297 | : next-line ( -- ) | |
298 | accepting? deny-history? and if exit then | |
299 | last-line? 0= if | |
300 | beginning-of-line #after ( previous-length ) | |
301 | line-end-adr 1+ is line-start-adr | |
302 | 1 line# +! | |
303 | \ Scroll if editing a file | |
304 | accepting? 0= display? and if drop cr 0 then | |
305 | display-line | |
306 | then | |
307 | ; | |
308 | ||
309 | \ Goes to the previous line, displaying it "in-place" on the same screen line. | |
310 | : previous-line ( -- ) | |
311 | accepting? deny-history? and if exit then | |
312 | buf-start-adr line-start-adr < if | |
313 | beginning-of-line #after ( previous-length ) | |
314 | buf-start-adr line-start-adr 1- 2dup = if | |
315 | is line-start-adr drop | |
316 | else | |
317 | do | |
318 | i is line-start-adr | |
319 | i -1 ca+ c@ newline = ?leave | |
320 | -1 +loop | |
321 | then | |
322 | -1 line# +! | |
323 | display-line | |
324 | then | |
325 | ; | |
326 | ||
327 | \ : forward-lines ( #lines -- ) 0 ?do next-line loop ; | |
328 | \ : backward-lines ( #lines -- ) 0 ?do previous-line loop ; | |
329 | ||
330 | ||
331 | \ This is used by the command completion package; it ought to be elsewhere, | |
332 | \ and it also should find the end of the word without going there. | |
333 | : end-of-word ( -- ) | |
334 | after bounds ?do | |
335 | i c@ bl = ?leave forward-character | |
336 | loop | |
337 | ; | |
338 | : forward-word ( -- ) find-next-word cursor-adr - forward-characters ; | |
339 | : backward-word ( -- ) | |
340 | cursor-adr find-previous-word - backward-characters | |
341 | ; | |
342 | : erase-next-word ( -- ) find-next-word cursor-adr - erase-characters ; | |
343 | : erase-previous-word ( -- ) | |
344 | cursor-adr backward-word cursor-adr - erase-characters | |
345 | ; | |
346 | : quote-next-character ( -- ) key insert-character ; | |
347 | : split-line ( -- ) | |
348 | accepting? 0= if | |
349 | newline ch c! ch 1 (insert-characters) if | |
350 | #after ( previous-#after ) | |
351 | #before linelen ! ( previous-#after ) | |
352 | .trailing \ Erase the rest of the line | |
353 | then | |
354 | else | |
355 | beep | |
356 | then | |
357 | ; | |
358 | : new-line ( -- ) split-line next-line ; | |
359 | : list-file ( -- ) | |
360 | accepting? deny-history? and if exit then | |
361 | cr | |
362 | 0 dup .num | |
363 | buf-extent bounds ?do | |
364 | i c@ dup newline = if | |
365 | drop cr exit? ?leave 1+ dup .num | |
366 | else | |
367 | emit | |
368 | then | |
369 | loop | |
370 | drop | |
371 | retype-line | |
372 | ; | |
373 | : yank ( -- ) kill-buffer count insert-characters ; | |
374 | ||
375 | headers |