Merge pull request #75 from SeekingMeaning/0BSD
[pforth] / fth / save-input.fth
CommitLineData
08689895 1\ SAVE-INPUT and RESTORE-INPUT
95b5d453
HE
2\
3\ This code is part of pForth.
4\
1f99f95d
S
5\ Permission to use, copy, modify, and/or distribute this
6\ software for any purpose with or without fee is hereby granted.
7\
8\ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
9\ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
10\ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
11\ THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
12\ CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
13\ FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
14\ CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15\ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
08689895
HE
16
17anew task-save-input.fth
18
19private{
20
95b5d453 21: SAVE-BUFFER ( -- column source-id 2 ) >in @ source-id 2 ;
08689895 22
95b5d453
HE
23\ Restore >IN from COLUMN unless COLUMN is too large. Valid values
24\ for COLUMN are from 0 to (including) the length of SOURCE plus one.
25: RESTORE-COLUMN ( column -- flag )
26 source nip 1+ over u<
09d08b41
HE
27 IF drop true
28 ELSE >in ! false
29 THEN
08689895
HE
30;
31
32\ Return the file-position of the beginning of the current line in
33\ file SOURCE-ID. Assume that the current line is stored in SOURCE
34\ and that the current file-position is at an end-of-line (or
35\ end-of-file).
95b5d453 36: LINE-START-POSITION ( -- ud )
09d08b41
HE
37 source-id file-position throw
38 \ unless at end-of-file, subtract newline
39 source-id file-size throw 2over d= 0= IF 1 s>d d- THEN
40 \ subtract line length
41 source nip s>d d-
08689895
HE
42;
43
95b5d453 44: SAVE-FILE ( column line filepos:ud source-id 5 -- )
09d08b41
HE
45 >in @
46 source-line-number@
47 line-start-position
48 source-id
49 5
08689895
HE
50;
51
95b5d453
HE
52: RESTORE-FILE ( column line filepos:ud -- flag )
53 source-id reposition-file IF 2drop true EXIT THEN
54 refill 0= IF 2drop true EXIT THEN
09d08b41
HE
55 source-line-number!
56 restore-column
08689895
HE
57;
58
95b5d453 59: NDROP ( n*x n -- ) 0 ?DO drop LOOP ;
08689895
HE
60
61}private
62
63\ Source Stack
64\ EVALUATE >IN SourceID=(-1) 2
65\ keyboard >IN SourceID=(0) 2
66\ file >IN lineNumber filePos SourceID=(fileID) 5
67: SAVE-INPUT ( -- column {line filepos}? source-id n )
95b5d453
HE
68 source-id CASE
69 -1 OF save-buffer ENDOF
70 0 OF save-buffer ENDOF
71 drop save-file EXIT
72 ENDCASE
08689895
HE
73;
74
75: RESTORE-INPUT ( column {line filepos}? source-id n -- flag )
95b5d453 76 over source-id <> IF ndrop true EXIT THEN
09d08b41 77 drop
95b5d453
HE
78 CASE
79 -1 OF restore-column ENDOF
80 0 OF restore-column ENDOF
81 drop restore-file EXIT
82 ENDCASE
08689895
HE
83;
84
85privatize