summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/context/base/mkiv/typo-wrp.lua
blob: 07e34cd6c8f22b71423456226b2ac9deb35956a4 (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
if not modules then modules = { } end modules ['typo-wrp'] = {
    version   = 1.001,
    comment   = "companion to typo-wrp.mkiv",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

-- begin/end par wrapping stuff ... more to come

local nodecodes         = nodes.nodecodes

local glue_code         = nodecodes.glue
local penalty_code      = nodecodes.penalty
local parfill_skip_code = nodes.gluecodes.parfillskip
local user_penalty_code = nodes.penaltycodes.userpenalty

local nuts              = nodes.nuts
local tonut             = nodes.tonut
local tonode            = nodes.tonode

local find_node_tail    = nuts.tail
local getprev           = nuts.getprev
local getid             = nuts.getid
local getsubtype        = nuts.getsubtype
local getpenalty        = nuts.getpenalty
local remove            = nuts.remove

local enableaction      = nodes.tasks.enableaction

local wrappers          = { }
typesetters.wrappers    = wrappers

local trace_wrappers    = trackers.register("typesetters.wrappers",function(v) trace_wrappers = v end)

local report            = logs.reporter("paragraphs","wrappers")

-- we really need to pass tail too ... but then we need to check all the plugins
-- bah ... slowdown

local function remove_dangling_crlf(head,tail)
    if tail and getid(tail) == glue_code and getsubtype(tail) == parfill_skip_code then
        tail = getprev(tail)
        if tail and getid(tail) == penalty_code and getsubtype(tail) == user_penalty_code and getpenalty(tail) == 10000 then
            tail = getprev(tail)
            if tail and getid(tail) == penalty_code and getsubtype(tail) == user_penalty_code and getpenalty(tail) == -10000 then
                if tail == head then
                    -- can't happen
                else
                    if trace_wrappers then
                        report("removing a probably unwanted end-of-par break in line %s (guess)",tex.inputlineno)
                    end
                    remove(head,tail,true)
                    return head, tail, true
                end
            end
        end
    end
    return head, tail, false
end

function wrappers.handler(head)
    local head = tonut(head)
    if head then
        local tail = find_node_tail(head)
        local done = false
        head, tail, done = remove_dangling_crlf(head,tail) -- will be action chain
    end
    return head, true
end

interfaces.implement {
    name     = "enablecrlf",
    onlyonce = true,
    actions  = function()
        enableaction("processors","typesetters.wrappers.handler")
    end
}