m-system-readers.mkxl /size: 3791 b    last modification: 2021-10-28 13:51
1%D \module
2%D   [       file=m-system-readers,
3%D        version=2021.02.13,
4%D          title=\CONTEXT\ Modules,
5%D       subtitle=Reading from files,
6%D         author=Hans Hagen,
7%D           date=\currentdate,
8%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
9
10%C This module is part of the \CONTEXT\ macro||package and is
11%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
12%C details.
13
14\startmodule[system-readers]
15
16% \unprotect
17%
18% \installcorenamespace {markedlines}
19%
20% \permanent\protected\def\startmarkedlines[#1]%
21%   {\edef\scratchstring{#1}%
22%    \scratchcounter\numexpr\inputlineno+\plusone\relax
23%    \gobbleuntilandfinalize\stopmarkedlines}
24%
25% \permanent\protected\def\stopmarkedlines
26%   {\edefcsname\??markedlines\scratchstring\endcsname
27%      {{\the\scratchcounter}{\the\numexpr\inputlineno+\minusone\relax}}}
28%
29% \protected\def\firstmarkedline#1%
30%   {\numexpr
31%      \ifcsname\??markedlines#1\endcsname
32%        \normalexpanded{\noexpand\firstoftwoarguments\lastnamedcs}%
33%      \else
34%        \zerocount
35%     \fi
36%    \relax}
37%
38% \protected\def\lastmarkedline#1%
39%   {\numexpr
40%      \ifcsname\??markedlines#1\endcsname
41%        \normalexpanded{\noexpand\secondoftwoarguments\lastnamedcs}%
42%      \else
43%        \zerocount
44%     \fi
45%    \relax}
46%
47% \protected\def\markedlines#1%
48%   {\ifcsname\??markedlines\scratchstring\endcsname\lastnamedcs\else{0}{0}\fi}
49%
50% \protect
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     -- usage     = "value",
78        arguments = "string",
79        actions   = function(tag)
80            local m = marked[tag]
81         -- return m and m[1] or ""
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{m-system-readers.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