Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | \ ========== Copyright Header Begin ========================================== |
2 | \ | |
3 | \ Hypervisor Software File: editcmd.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 | \ editcmd.fth 2.17 06/12/15 | |
43 | \ Copyright 1985-1994 Bradley Forthware | |
44 | \ Copyright 2006 Sun Microsystems, Inc. All Rights Reserved | |
45 | \ Copyright Use is subject to license terms. | |
46 | ||
47 | headers | |
48 | forth definitions | |
49 | vocabulary keys-forth | |
50 | defer skey ' key is skey \ Perhaps override with an ekey-based word later | |
51 | ||
52 | hidden definitions | |
53 | ||
54 | headerless | |
55 | tuser keys ' keys-forth keys token! | |
56 | ||
57 | d# 32 buffer: name-buf | |
58 | ||
59 | : add-char-to-string ( str char -- ) | |
60 | over ( str char str ) | |
61 | count dup >r ( str char addr len ) | |
62 | + c! ( str ) | |
63 | r> 1+ swap c! | |
64 | ; | |
65 | : add-char-to-name ( str char -- ) | |
66 | ||
67 | dup bl u< if ( str char ) \ control character so translate to ^ form | |
68 | over ascii ^ add-char-to-string ( str char ) | |
69 | ascii a 1- + ( str char' ) add-char-to-string | |
70 | else | |
71 | \ Map the Delete key to the string "del" | |
72 | dup d# 127 = if drop " del" rot $cat exit then | |
73 | ||
74 | \ Map the Unicode Control Sequence Identifier to the string "ESC[" | |
75 | dup h# 9b = if drop " esc-[" rot $cat exit then | |
76 | ||
77 | \ Map the ISO 6429 (and ISO-8849-1) CS1 character | |
78 | \ "Private Use 1" (PU1==0x91) to the ask-layout command | |
79 | dup h# 91 = if drop " ask-layout" rot $cat exit then | |
80 | ||
81 | \ Map the out-of-band character into the string "ext" | |
82 | dup -1 = if drop " ext" rot $cat exit then | |
83 | ||
84 | add-char-to-string | |
85 | then | |
86 | ; | |
87 | defer not-found | |
88 | ||
89 | nuser lastchar \ most-recently-typed character | |
90 | : do-command ( prefix-string -- ) | |
91 | name-buf "copy | |
92 | name-buf lastchar @ add-char-to-name | |
93 | name-buf count keys token@ search-wordlist ( false | cfa true ) | |
94 | if execute else not-found then | |
95 | ; | |
96 | ||
97 | defer printable-char | |
98 | nuser finished \ is the line complete yet? | |
99 | ||
100 | : start-edit ( bufadr buflen bufmax line# position display? -- ) | |
101 | is display? | |
102 | >r | |
103 | line# ! | |
104 | is bufmax buflen ! is buf-start-adr | |
105 | buf-start-adr r> + is line-start-adr | |
106 | ||
107 | 0 is #before | |
108 | set-linelen | |
109 | ; | |
110 | : finish-edit ( -- length ) buflen @ ; | |
111 | : edit-command-loop ( -- ) | |
112 | finished off | |
113 | begin | |
114 | skey lastchar ! | |
115 | lastchar @ | |
116 | dup bl h# 7e between | |
117 | swap h# a0 h# fe between or | |
118 | if lastchar @ printable-char else nullstring do-command then | |
119 | finished @ until | |
120 | cr | |
121 | ; | |
122 | headerless | |
123 | ||
124 | : edit-buffer (s bufadr buflen bufmax line# position -- newlen ) | |
125 | true start-edit | |
126 | ||
127 | 0 display-line | |
128 | ||
129 | edit-command-loop | |
130 | ||
131 | finish-edit | |
132 | ; | |
133 | : edit-file (s addr len maxlen -- newlen ) 0 0 edit-buffer ; | |
134 | ||
135 | d# 512 /tib 2* max value hbufmax | |
136 | hbufmax buffer: hbuf-adr | |
137 | 0 value hbuflen | |
138 | : ensure-line-end ( -- ) | |
139 | \ Put a newline at the end of the last line if necessary | |
140 | hbuflen if | |
141 | hbuf-adr hbuflen + 1- c@ newline <> if | |
142 | newline hbuf-adr hbuflen + c! | |
143 | hbuflen 1+ is hbuflen | |
144 | then | |
145 | then | |
146 | ; | |
147 | : make-room ( needed -- ) | |
148 | 1+ hbufmax hbuflen - - ( shortfall ) | |
149 | dup 0> if ( shortfall ) \ Too little room at the end | |
150 | dup hbuf-adr + hbuf-adr hbuflen 3 pick - move ( shortfall ) | |
151 | hbuflen swap - is hbuflen | |
152 | else | |
153 | drop | |
154 | then | |
155 | \ hbuf-adr over + hbufmax rot - ( adr remaining ) | |
156 | \ hbufmax -rot bounds ?do ( next-line-adr ) | |
157 | \ i c@ newline = if | |
158 | \ drop i 1+ hbuf-adr - leave | |
159 | \ then | |
160 | \ loop ( shortfall next-line-adr ) | |
161 | \ dup hbuf-adr | |
162 | ensure-line-end | |
163 | ; | |
164 | : open-history ( needed -- buf len maxlen line# position ) | |
165 | make-room ( ) | |
166 | hbuf-adr hbuflen hbufmax 0 hbuflen | |
167 | ; | |
168 | : xaccept (s adr len -- actual ) | |
169 | (interactive? 0= if sys-accept exit then | |
170 | tuck dup hbufmax 1- > if ( len adr len ) | |
171 | 0 swap 0 0 ( len adr 0 len 0 0 ) | |
172 | else ( adr len ) | |
173 | open-history ( len adr hbuf hlen hmax line# position ) | |
174 | then | |
175 | ||
176 | true is accepting? | |
177 | edit-buffer is hbuflen ( len adr ) | |
178 | false is accepting? | |
179 | ||
180 | swap linelen @ min tuck ( len' adr len' ) | |
181 | line-start-adr -rot move ( len' ) | |
182 | ; | |
183 | : new-line-or-done ( -- ) | |
184 | accepting? if | |
185 | finished on | |
186 | line# @ -1 < if ?copyline then | |
187 | else | |
188 | new-line | |
189 | then | |
190 | ; | |
191 | ||
192 | : self-insert ( -- ) lastchar @ insert-character ; | |
193 | ||
194 | headers | |
195 | forth definitions | |
196 | ||
197 | defer (ask-layout ['] noop is (ask-layout ( -- ) | |
198 | ||
199 | keys-forth also definitions | |
200 | ||
201 | : ^f forward-character ; | |
202 | : ^b backward-character ; | |
203 | : ^a beginning-of-line ; | |
204 | \ : ^c finished on ; | |
205 | : ^e end-of-line ; | |
206 | : ^d erase-next-character ; | |
207 | : ^h erase-previous-character ; | |
208 | : ^i bl insert-character ; | |
209 | : ^j new-line-or-done ; | |
210 | : ^k kill-to-end-of-line ; | |
211 | : ^l list-file ; | |
212 | : ^m new-line-or-done ; | |
213 | : ^n next-line ; | |
214 | : ^o split-line ; | |
215 | : ^p previous-line ; | |
216 | : ^q quote-next-character ; | |
217 | : ^x finished on ; \ XXX for testing | |
218 | : ^y yank ; | |
219 | : esc-y yank ; \ XXX for testing | |
220 | ||
221 | : ^{ key lastchar ! [""] esc- do-command ; | |
222 | : esc-o only forth also definitions beep beep beep ; | |
223 | : esc-h erase-previous-word ; | |
224 | : esc-d erase-next-word ; | |
225 | : esc-f forward-word ; | |
226 | : esc-b backward-word ; | |
227 | : esc-^h erase-previous-word ; | |
228 | : esc-^d erase-next-word ; | |
229 | : esc-^f forward-word ; | |
230 | : esc-^b backward-word ; | |
231 | : esc-del erase-next-word ; | |
232 | ||
233 | \ ANSI cursor keys | |
234 | : esc-[ key lastchar ! [""] esc-[ do-command ; | |
235 | : esc-[A previous-line ; | |
236 | : esc-[B next-line ; | |
237 | : esc-[C forward-character ; | |
238 | : esc-[D backward-character ; | |
239 | : esc-[P erase-previous-character ; | |
240 | ||
241 | : ask-layout ( -- ) | |
242 | \ Use defer word to reach (ask-layout) in ui-cvars.fth | |
243 | (ask-layout | |
244 | \ Keep just-typed number from becoming a command line | |
245 | beginning-of-line kill-to-end-of-line | |
246 | ; | |
247 | ||
248 | hidden definitions | |
249 | headerless | |
250 | : emacs-edit | |
251 | ['] beep is not-found | |
252 | ['] insert-character is printable-char | |
253 | ['] xaccept is accept | |
254 | ; | |
255 | emacs-edit | |
256 | ||
257 | [ifexist] xref-find-hook | |
258 | ' keys-forth ' lose ' $find-word (patch | |
259 | [then] | |
260 | forth definitions | |
261 | chain: init ( -- ) emacs-edit ; | |
262 | headers |