summaryrefslogtreecommitdiff
path: root/tex/context/modules/mkxl/s-languages-goodies.lmt
blob: 7256fd35dc54bb93103a9d8e5916d2ce17969637 (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
if not modules then modules = { } end modules ['s-languages-goodies'] = {
    version   = 1.001,
    comment   = "companion to s-languages-goodies.mkxl",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

moduledata.languages         = moduledata.languages         or { }
moduledata.languages.goodies = moduledata.languages.goodies or { }

function moduledata.languages.goodies.show(specification)
    specification = interfaces.checkedspecification(specification)
    local filename = specification.file
    if filename and filename ~= "" then
        local list = table.load(resolvers.findfile(filename))
        if list then
            list = list.options
            if list then
                for i=1,#list do
                    local l = list[i]
                    local w = l.words
                    if w then
                        local pre = l.prefixes
                        local suf = l.suffixes
                        context.startsubject { title = table.concat(table.sortedkeys(l.patterns)," ") }
                            context(languages.strippedgoodiewords(w))
                            if pre then
                                context.blank()
                                context.bold("prefixes: ")
                                context(languages.strippedgoodiewords(pre))
                            end
                            if suf then
                                context.blank()
                                context.bold("suffixes: ")
                                context(languages.strippedgoodiewords(suf))
                            end
                        context.stopsubject()
                    end
                end
            end
        end
    end
end

local lpegmatch = lpeg.match

moduledata.languages.goodies.ligaturehandlers = { }

function moduledata.languages.goodies.ligatures(specification)

    specification  = interfaces.checkedspecification(specification)
    local language = specification.language
    local filename = specification.file

    if not language then
    elseif moduledata.languages.goodies.ligaturehandlers[language] then
    else
        -- fb ff ffb fff ffh ffi ffj ffk ffl fft fi fk fl ft
        local list    = specification.list or "ff fi fl ffi fff ffl"
        local hash    = table.tohash(lpeg.split(" ",list)) -- also strip
        local pattern = (1-lpeg.utfchartabletopattern(hash))^1 * lpeg.P(-1)
        local checked = { }

        moduledata.languages.goodies.ligaturehandlers[language] = function(original)
            if not checked[original] and not lpegmatch(pattern,original) then
                checked[original] = true
            end
            return original
        end

        languages.installhandler(language,"moduledata.languages.goodies.ligaturehandlers." .. language .. "")

        statistics.register(string.formatters["'% t' ligatures checked for language %a"](table.sortedkeys(hash), language), function()
            return next(checked) and table.concat(table.sortedkeys(checked)," ") or nil
        end)

        local applied = languages.appliedoptions[language]

        trackers.enable("languages.applied")

        if applied then
            statistics.register(string.formatters["options applied for language %a"](language), function()
                return next(applied) and table.concat(table.sortedkeys(applied)," ") or nil
            end)
            statistics.register(string.formatters["missed ligatures for language %a"](language), function()
                for k, v in next, applied do
                    checked[k] = nil
                end
                for k, v in next, hash do
                    checked[k] = nil
                end
                return next(checked) and table.concat(table.sortedkeys(checked)," ") or nil
            end)
         end

    end

end