summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/context/base/mkiv/buff-par.lua
blob: 33ed4baf06ff0f1ec742e29848ccc934207cbfde (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
if not modules then modules = { } end modules ['buff-par'] = {
    version   = 1.001,
    comment   = "companion to buff-ini.mkiv",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

local tonumber = tonumber
local insert, remove, find, gmatch, match = table.insert, table.remove, string.find, string.gmatch, string.match
local fullstrip, formatters = string.fullstrip, string.formatters

local trace_parallel  = false  trackers.register("buffers.parallel", function(v) trace_parallel = v end)

local report_parallel = logs.reporter("buffers","parallel")

local variables         = interfaces.variables
local v_all             = variables.all

local parallel          = buffers.parallel or { }
buffers.parallel        = parallel

local settings_to_array = utilities.parsers.settings_to_array

local context           = context
local implement         = interfaces.implement

local data              = { }

function parallel.define(category,tags)
    local tags = settings_to_array(tags)
    local entries = { }
    data[category] = {
        tags    = tags,
        entries = entries,
    }
    for i=1,#tags do
        entries[tags[i]] = {
            lines  = { },
            number = 0,
        }
    end
end

function parallel.reset(category,tags)
    if not tags or tags == "" or tags == v_all then
        tags = table.keys(entries)
    else
        tags = settings_to_array(tags)
    end
    for i=1,#tags do
        entries[tags[i]] = {
            lines  = { },
            number = 0,
        }
    end
end

function parallel.next(category)
    local dc = data[category]
    local tags = dc.tags
    local entries = dc.entries
    for i=1,#tags do
        insert(entries[tags[i]].lines, { })
    end
end

function parallel.save(category,tag,content,frombuffer)
    if frombuffer then
        content = buffers.raw(content)
    end
    local dc = data[category]
    if not dc then
        report_parallel("unknown category %a",category)
        return
    end
    local entries = dc.entries[tag]
    if not entries then
        report_parallel("unknown entry %a",tag)
        return
    end
    local lines = entries.lines
    if not lines then
        return
    end
    local line = lines[#lines]
    if not line then
        return
    end
    -- maybe no strip
    -- use lpeg
    if find(content,"%s*%[") then
        local done = false

        local function flush(content,label)
            if done then
                line = { }
                insert(lines,line)
            else
                done = true
            end
            line.content = fullstrip(content)
            line.label   = label
        end


        local leading, rest = match(content,"^%s*([^%[]+)(.*)$")
        if leading then
            if leading ~= "" then
                flush(leading)
            end
            content = rest
        end
        for label, content in gmatch(content,"%s*%[(.-)%]%s*([^%[]+)") do
            if trace_parallel and label ~= "" then
                report_parallel("reference found of category %a, tag %a, label %a",category,tag,label)
            end
            flush(content,label)
        end
    else
        line.content = fullstrip(content)
        line.label = ""
    end
    -- print("[["..line.content.."]]")
end

function parallel.hassomecontent(category,tags)
    local dc = data[category]
    if not dc then
        return false
    end
    local entries = dc.entries
    if not tags or tags == "" or tags == v_all then
        tags = table.keys(entries)
    else
        tags = utilities.parsers.settings_to_array(tags)
    end
    for t=1,#tags do
        local tag = tags[t]
        local lines = entries[tag].lines
        for i=1,#lines do
            local content = lines[i].content
            if content and content ~= "" then
                return true
            end
        end
    end
    return false
end

local ctx_doflushparallel = context.doflushparallel
local f_content           = formatters["\\input{%s}"]
local save_byscheme       = resolvers.savers.byscheme

function parallel.place(category,tags,options)
    local dc = data[category]
    if not dc then
        return
    end
    local entries   = dc.entries
    local tags      = utilities.parsers.settings_to_array(tags)
    local options   = utilities.parsers.settings_to_hash(options) -- options can be hash too
    local start     = tonumber(options.start)
    local n         = tonumber(options.n)
    local criterium = options.criterium
    local max       = 1
    if n then
        max = n
    elseif criterium == v_all then
        max = 0
        for t=1,#tags do
            local tag = tags[t]
            local lines = entries[tag].lines
            if #lines > max then
                max = #lines
            end
        end
    end
    for i=1,max do
        for t=1,#tags do
            local tag = tags[t]
            local entry = entries[tag]
            if entry then
                local lines   = entry.lines
                local number  = entry.number + 1
                entry.number  = number
                local line    = remove(lines,1)
                local content = line and line.content
                local label   = line and line.label or ""
                if content then
                    local virtual = save_byscheme("virtual","parallel",content)
                    ctx_doflushparallel(tag,1,number,label,f_content(virtual))
                else
                    ctx_doflushparallel(tag,0,number,"","")
                end
            end
        end
    end
end

-- interface

implement {
    name      = "defineparallel",
    actions   = parallel.define,
    arguments = { "string", "string" }
}

implement {
    name      = "nextparallel",
    actions   = parallel.next,
    arguments = "string"
}

implement {
    name      = "saveparallel",
    actions   = parallel.save,
    arguments = { "string", "string", "string", true },
}

implement {
    name      = "placeparallel",
    actions   = parallel.place,
    arguments = {
        "string",
        "string",
        {
            { "start" },
            { "n" },
            { "criterium" },
            { "setups" },
        }
    }
}

implement {
    name      = "resetparallel",
    actions   = parallel.reset,
    arguments = { "string", "string" }
}

implement {
    name      = "doifelseparallel",
    actions   = { parallel.hassomecontent, commands.doifelse } ,
    arguments = { "string", "string" }
}