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