spac-hor.lua /size: 1353 b    last modification: 2020-07-01 14:35
1if not modules then modules = { } end modules ['spac-hor'] = {
2    version   = 1.001,
3    comment   = "companion to spac-hor.mkiv",
4    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
5    copyright = "PRAGMA ADE / ConTeXt Development Team",
6    license   = "see context related readme files"
7}
8
9local lpegmatch, P, C = lpeg.match, lpeg.P, lpeg.C
10
11local context  = context
12
13local chardata = characters.data
14
15local p_check  = P("the ") * (P("letter") + P("character")) * P(" ") * lpeg.patterns.utf8byte -- is a capture already
16
17local can_have_space = table.tohash {
18    "lu", "ll", "lt", "lm", "lo", -- letters
19 -- "mn", "mc", "me",             -- marks
20    "nd", "nl", "no",             -- numbers
21    "ps", "pi",                   -- initial
22 -- "pe", "pf",                   -- final
23 -- "pc", "pd", "po",             -- punctuation
24    "sm", "sc", "sk", "so",       -- symbols
25 -- "zs", "zl", "zp",             -- separators
26 -- "cc", "cf", "cs", "co", "cn", -- others
27}
28
29local function autonextspace(str) -- todo: make a real not intrusive lookahead
30    local b = lpegmatch(p_check,str)
31    if b then
32        local d = chardata[b]
33        if d and can_have_space[d.category] then
34            context.space()
35        end
36    end
37end
38
39interfaces.implement {
40    name      = "autonextspace",
41    actions   = autonextspace,
42    arguments = "string",
43}
44