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

-- local nodecodes      = nodes.nodecodes
-- local whatsitcodes   = nodes.whatsitcodes
-- local glyph_code     = nodecodes.glyph
-- local whatsit_code   = nodecodes.whatsit
-- local user_code      = whatsitcodes.userdefined
--
-- local stringusernode = nodes.pool.userstring
--
-- local nuts           = nodes.nuts
-- local pool           = nuts.pool
--
-- local tonut          = nuts.tonut
-- local tonode         = nuts.tonode
-- local getid          = nuts.getid
-- local getprev        = nuts.getprev
-- local getsubtype     = nuts.getsubtype
-- local getchar        = nuts.getchar
-- local getfield       = nuts.getfield
--
-- local remove_node    = nuts.remove
-- local traverse_by_id = nuts.traverse_id
--
-- local signal         = pool.userids.signal
--
-- local is_punctuation = characters.is_punctuation
--
-- local actions = {
--     removepunctuation = function(head,n)
--         local prev = getprev(n)
--         if prev then
--             if getid(prev) == glyph_code then
--                 if is_punctuation[getchar(prev)] then
--                     head = remove_node(head,prev,true)
--                 end
--             end
--         end
--         return head
--     end
-- }
--
-- -- we can also use properties .. todo (saves pass)
--
-- typesetters.signals = { }
--
-- function typesetters.signals.handler(head)
--     local h = tonut(head)
--     local done = false
--     for n in traverse_by_id(whatsit_code,h) do
--         if getsubtype(n) == user_code and getfield(n,"user_id") == signal and getfield(n,"type") == 115 then
--             local action = actions[getfield(n,"value")]
--             if action then
--                 h = action(h,n)
--             end
--             h = remove_node(h,n,true)
--             done = true
--         end
--     end
--     if done then
--         return tonode(h), true
--     else
--         return head
--     end
-- end
--
-- local enabled = false
--
-- local function signal(what)
--     if not enabled then
--         nodes.tasks.prependaction("processors","normalizers", "typesetters.signals.handler")
--         enabled = true
--     end
--     context(stringusernode(signal,what))
-- end
--
-- interfaces.implement {
--     name      = "signal",
--     actions   = signal,
--     arguments = "string",
-- }

local insert, remove = table.insert, table.remove

local nodecodes   = nodes.nodecodes
local glyph_code  = nodecodes.glyph

local texnest     = tex.nest
local free_node   = node.free

local punctuation = characters.is_punctuation

local stack       = { }

local function pickup()
    local list = texnest[texnest.ptr]
    if list then
        local tail = list.tail
        if tail and tail.id == glyph_code and punctuation[tail.char] then
            local prev = tail.prev
            list.tail = prev
            if prev then
                prev.next = nil
            end
            list.tail = prev
            tail.prev = nil
            return tail
        end
    end
end

local actions = {
    remove = function(specification)
        local n = pickup()
        if n then
            free_node(n)
        end
    end,
    push = function(specification)
        local n = pickup()
        if n then
            insert(stack,n or false)
        end
    end,
    pop = function(specification)
        local n = remove(stack)
        if n then
            context(n)
        end
    end,
}

local function pickuppunctuation(specification)
    local action = actions[specification.action or "remove"]
    if action then
        action(specification)
    end
end

interfaces.implement {
    name      = "pickuppunctuation",
    actions   = pickuppunctuation,
    arguments = {
        {
            { "action" }
        }
    }
}