summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/context/base/strc-not.lua
blob: 32a836ba86c8dd0a87c6bf4dbf9553f185cf4b45 (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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
if not modules then modules = { } end modules ['strc-not'] = {
    version   = 1.001,
    comment   = "companion to strc-not.tex",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

local format = string.format
local next = next
local texsprint, texwrite, texcount = tex.sprint, tex.write, tex.count

local ctxcatcodes = tex.ctxcatcodes

local trace_notes = false  trackers.register("structure.notes", function(v) trace_notes = v end)

structure              = structure          or { }
structure.helpers      = structure.helpers  or { }
structure.lists        = structure.lists    or { }
structure.sections     = structure.sections or { }
structure.counters     = structure.counters or { }
structure.notes        = structure.notes    or { }

structure.notes.states    = structure.notes.states    or { }
structure.lists.enhancers = structure.lists.enhancers or { }

storage.register("structure/notes/states", structure.notes.states, "structure.notes.states")

local helpers  = structure.helpers
local lists    = structure.lists
local sections = structure.sections
local counters = structure.counters
local notes    = structure.notes

local notestates = structure.notes.states
local notedata   = { }

-- state: store, insert, postpone

function notes.store(tag,n)
    local nd = notedata[tag]
    if not nd then
        nd = { }
        notedata[tag] = nd
    end
    local nnd = #nd+1
    nd[nnd] = n
    local state = notestates[tag]
    if state.kind ~= "insert" then
        if trace_notes then
            logs.report("notes","storing %s with state %s as %s",tag,state.kind,nnd)
        end
        state.start = state.start or nnd
    end
    tex.write(#nd)
end

function notes.get(tag,n)
    local nd = notedata[tag]
    if nd then
        n = n or #notedata
        nd = nd[n or n]
        if nd then
            if trace_notes then
                logs.report("notes","getting %s of %s",n,tag)
            end
            return structure.lists.collected[nd]
        end
    end
end

function notes.define(tag,kind,number)
    local state = notes.setstate(tag,kind)
    state.number = number
end

function notes.save(tag,newkind)
    local state = notestates[tag]
    if state and not state.saved then
        state.saved = notedata[tag]
        state.savedkind = state.kind
        state.kind = newkind or state.kind
        notedata[tag] = { }
    end
end

function notes.restore(tag)
    local state = notestates[tag]
    if state and state.saved then
        state.saved = nil
        state.kind = state.savedkind
        notedata[tag] = state.saved
    end
end

function notes.setstate(tag,newkind)
    local state = notestates[tag]
    if trace_notes then
        logs.report("notes","setting state of %s from %s to %s",tag,(state and state.kind) or "unset",newkind)
    end
    if not state then
        state = {
            kind = newkind
        }
        notestates[tag] = state
    elseif newkind == "insert" then
        if not state.start then
            state.kind = newkind
        end
    else
        state.kind = newkind
    end
    --  state.start can already be set and will be set when an entry is added or flushed
    return state
end

function notes.getstate(tag)
    local state = notestates[tag]
    texsprint(ctxcatcodes,(state and state.kind ) or "unknown")
end

function notes.doifcontent(tag)
    local ok = notestates[tag]
    if ok then
        if ok.kind == "insert" then
            ok = tex.box[ok.number]
            if ok then
                ok = tbs.list
                ok = lst and lst.next
            end
        else
            ok = ok.start
        end
    end
    commands.doif(ok)
end

local function internal(tag,n)
    local nd = notes.get(tag,n)
    if nd then
        local r = nd.references
        if r then
            local i = r.internal
--~             return i and lists.internals[i]
            return i and jobreferences.internals[i]
        end
    end
    return nil
end

local function ordered(kind,name,n)
    local o = lists.ordered[kind]
    o = o and o[name]
    return o and o[n]
end

function notes.checkpagechange(tag) -- called before increment !
    local nd = notedata[tag] -- can be unset at first entry
    if nd then
        local current = ordered("note",tag,#nd)
        local nextone = ordered("note",tag,#nd+1)
        if nextone then
            -- we can use data from the previous pass
            if nextone.pagenumber.number > current.pagenumber.number then
                counters.reset(tag)
            end
        elseif current then
            -- we need to locate the next one, best guess
            if texcount.realpageno > current.pagenumber.number then
                counters.reset(tag)
            end
        end
    end
end

function notes.deltapage(tag,n)
    -- 0:unknown 1:textbefore, 2:textafter, 3:samepage
    local what = 0
    local li = internal(tag,n)
    if li then
        local metadata, pagenumber = li.metadata, li.pagenumber
        if metadata and pagenumber then
            local symbolpage = metadata.symbolpage or 0
            local notepage = pagenumber.number or 0
            if notepage > 0 and symbolpage > 0 then
                if notepage < symbolpage then
                    what = 1
                elseif notepage > symbolpage then
                    what = 2
                else
                    what = 3
                end
            end
        else
            -- might be a note that is not flushed due to to deep
            -- nesting in a vbox
            what = 3
        end
    end
    tex.write(what)
end

function notes.postpone()
    if trace_notes then
        logs.report("notes","postponing all insert notes")
    end
    for tag, state in next, notestates do
        if state.kind ~= "store" then
            notes.setstate(tag,"postpone")
        end
    end
end

function notes.setsymbolpage(tag,n)
    local nd = notes.get(tag,n)
    if nd then
        nd.metadata.symbolpage = texcount.realpageno
    end
end

function notes.getsymbolpage(tag,n)
    local nd = notes.get(tag,n)
    nd = nd and nd.metadata.symbolpage
    texwrite(nd or 0)
end

function notes.getnumberpage(tag,n)
    local li = internal(tag,n)
    li = li and li.pagenumber
    li = li and li.numbers
    li = li and li[1]
    texwrite(li or 0)
end

function notes.flush(tag,whatkind) -- store and postpone
    local state = notestates[tag]
    local kind = state.kind
    if kind == whatkind then
        if kind == "postpone" then
            local nd = notedata[tag]
            local ns = state.start -- first index
            if nd and ns then
                if trace_notes then
                    logs.report("notes","flushing state %s of %s from %s to %s",whatkind,tag,ns,#nd)
                end
                for i=ns,#nd do
                    texsprint(ctxcatcodes,format("\\handlenoteinsert{%s}{%s}",tag,i))
                end
            end
            state.start = nil
            state.kind = "insert"
        elseif kind == "store" then
            local nd = notedata[tag]
            local ns = state.start -- first index
            if trace_notes then
                logs.report("notes","flushing state %s of %s from %s to %s",whatkind,tag,ns,#nd)
            end
            if nd and ns then
                for i=ns,#nd do
                    texsprint(ctxcatcodes,format("\\handlenoteitself{%s}{%s}",tag,i))
                end
            end
            state.start = nil
        elseif trace_notes then
            logs.report("notes","not flushing state %s of %s",whatkind,tag)
        end
    elseif trace_notes then
        logs.report("notes","not flushing state %s of %s",whatkind,tag)
    end
end

function notes.flushpostponed()
    if trace_notes then
        logs.report("notes","flushing all postponed notes")
    end
    for tag, _ in next, notestates do
        notes.flush(tag,"postpone")
    end
end

function notes.resetpostponed()
    if trace_notes then
        logs.report("notes","resetting all postponed notes")
    end
    for tag, state in next, notestates do
        if state.kind == "postpone" then
            state.start = nil
            state.kind = "insert"
        end
    end
end

function notes.title(tag,n)
    structure.lists.savedtitle(tag,notedata[tag][n])
end

function notes.number(tag,n,spec)
    structure.lists.savedprefixednumber(tag,notedata[tag][n])
end