1
2
3
4
5
6
7
8
9
10
11
12
13
14\startmodule[systemreaders]
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52\startluacode
53
54 local createtoken = tokens.create
55 local gobbletoken = tokens.gobble
56 local integer_value = tokens.values.integer
57
58 local marked = { }
59
60 interfaces.implement {
61 name = "startmarkedlines",
62 public = true,
63 protected = true,
64 arguments = "optional",
65 actions = function(tag)
66 local start = status.readstate.linenumber + 1
67 gobbletoken(createtoken("startmarkedlines"),createtoken("stopmarkedlines"))
68 local state = status.readstate
69 marked[tag] = { state.filename, start, state.linenumber - 1 }
70 end
71 }
72
73
74 interfaces.implement {
75 name = "markedfilename",
76 public = true,
77
78 arguments = "string",
79 actions = function(tag)
80 local m = marked[tag]
81
82 context(m and m[1] or "")
83 end
84 }
85
86 interfaces.implement {
87 name = "firstmarkedline",
88 public = true,
89 usage = "value",
90 arguments = "string",
91 actions = function(tag)
92 local m = marked[tag]
93 return integer_value, m and m[2] or 0
94 end
95 }
96
97 interfaces.implement {
98 name = "lastmarkedline",
99 public = true,
100 usage = "value",
101 arguments = "string",
102 actions = function(tag)
103 local m = marked[tag]
104 return integer_value, m and m[3] or 0
105 end
106 }
107
108\stopluacode
109
110\continueifinputfile{msystemreaders.mkxl}
111
112\starttext
113
114\startmarkedlines[test]
115SOME LINE 1
116SOME LINE 2
117SOME LINE 3
118SOME LINE 4
119\stopmarkedlines
120
121\startmarkedlines[more]
122SOME MORE 1
123SOME MORE 2
124SOME MORE 3
125SOME MORE 4
126\stopmarkedlines
127
128\begingroup
129 \newlocalread\myreada
130 \immediate\openin\myreada {\markedfilename{test}}
131 \dostepwiserecurse{\lastmarkedline{test}}{\firstmarkedline{test}}{1}{
132 \readline\myreada line #1 to \scratchstring #1 : \scratchstring \par
133 }
134 \immediate\closein\myreada
135\endgroup
136
137\blank
138
139\begingroup
140 \newlocalread\myreada
141 \immediate\openin\myreada {\markedfilename{more}}
142 \dostepwiserecurse{\firstmarkedline{more}}{\lastmarkedline{more}}{1}{
143 \read \myreada line #1 to \scratchstring #1 : \scratchstring \par
144 }
145 \immediate\closein\myreada
146\endgroup
147
148\stoptext
149 |