File: mkman.lua

package info (click to toggle)
notion 4.0.2%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 4,676 kB
  • sloc: ansic: 47,508; sh: 2,096; makefile: 603; perl: 270
file content (347 lines) | stat: -rw-r--r-- 7,662 bytes parent folder | download | duplicates (3)
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
--
-- build/mkman.lua
--
-- Translates bindings from Ion configuration into a listing for
-- manual pages.
--


-- Translations {{{

local translations={}

local function gettext(x)
    local t=translations[x]
    if not t or t=="" then
        return x
    else
        return t
    end
end

local function TR(x, ...)
    return string.format(gettext(x), ...)
end

local function read_translations(pofile)
    local f, err=io.open(pofile)
    if not f then
        error(err)
    end

    local msgid, msgstr, st, en

    for l in f:lines() do
        if string.find(l, "^msgid") then
            if msgid then
                assert(msgstr)
                translations[msgid]=msgstr
                msgstr=nil
            end
            st, en, msgid=string.find(l, '^msgid%s*"(.*)"%s*$')
        elseif string.find(l, "^msgstr") then
            assert(msgid and not msgstr)
            st, en, msgstr=string.find(l, '^msgstr%s*"(.*)"%s*$')
        elseif not (string.find(l, "^%s*#") or string.find(l, "^%s*$")) then
            local st, en, str=string.find(l, '^%s*"(.*)"%s*$')
            assert(msgid or msgstr)
            if not msgstr then
                msgid=msgid..str
            else
                msgstr=msgstr..str
            end
        end
    end

    if msgid then
        assert(msgstr)
        translations[msgid]=msgstr
    end

    f:close()
end

-- }}}


-- File parsing {{{

local function dobindings(f, bindings)
    local p={}
    local dummy = function() end

    p.META="Mod4+"
    p.ALTMETA="Mod4+Shift+"

    p.dopath=dummy
    p.defmenu=dummy
    p.defctxmenu=dummy
    p.menuentry=dummy
    p.submenu=dummy

    p.ioncore={ set=dummy }

    function p.bdoc(text)
        return {action = "doc", text = text}
    end

    function p.submap(kcb_, list)
        if not list then
            return function(lst)
                       return submap(kcb_, lst)
                   end
        end
        return {action = "submap", kcb = kcb_, submap = list}
    end

    local function putcmd(cmd, guard, t)
        t.cmd=cmd
        t.guard=guard
        return t
    end

    function p.kpress(keyspec, cmd, guard)
        return putcmd(cmd, guard, {action = "kpress", kcb = keyspec})
    end

    function p.kpress_wait(keyspec, cmd, guard)
        return putcmd(cmd, guard, {action = "kpress_wait", kcb = keyspec})
    end

    local function mact(act_, kcb_, cmd, guard)
        local st, en, kcb2_, area_=string.find(kcb_, "([^@]*)@(.*)")
        return putcmd(cmd, guard, {
            action = act_,
            kcb = (kcb2_ or kcb_),
            area = area_,
        })
    end

    function p.mclick(buttonspec, cmd, guard)
        return mact("mclick", buttonspec, cmd, guard)
    end

    function p.mdblclick(buttonspec, cmd, guard)
        return mact("mdblclick", buttonspec, cmd, guard)
    end

    function p.mpress(buttonspec, cmd, guard)
        return mact("mpress", buttonspec, cmd, guard)
    end

    function p.mdrag(buttonspec, cmd, guard)
        return mact("mdrag", buttonspec, cmd, guard)
    end

    function ins(t, v)
        if not t.seen then
            t.seen={}
        end

        if (not v.kcb) or v.submap then
            -- Submap rebinds are not presently handled
            table.insert(t, v)
        else
            local id=v.action..":"..v.kcb..":"..(v.area or "")
            local i=t.seen[id]
            if i then
                t[i].invalid=true
            end
            if v.cmd then
                table.insert(t, v)
                t.seen[id]=#t
            else
                -- Unbind only
                t.seen[id]=nil
            end
        end
    end

    function p.defbindings(context, bnd)
        if not bindings[context] then
            bindings[context]={}
        else
            -- Reset documentation
            table.insert(bindings[context], { action = "doc", text = nil })
        end

        for _, v in ipairs(bnd) do
            ins(bindings[context], v)
        end
    end

    local env=setmetatable({}, {
        __index=p,
        __newindex=function(x)
                       error("Setting global "..tostring(x))
                   end,
    })

    local fn, err

    if _ENV then
        fn, err=loadfile(f, nil, env)
    else
        fn, err=loadfile(f)
    end

    if not fn then
        error(err)
    end

    if not _ENV then
        setfenv(fn, env)
    end

    fn()
    return bindings
end

-- }}}


-- Binding output {{{

local function docgroup_bindings(bindings)
    local out={}
    local outi=0

    local function parsetable(t, prefix)
        for _, v in ipairs(t) do
            if not v.invalid then
                if v.kcb then
                    v.kcb=string.gsub(v.kcb, "AnyModifier%+", "")
                end
                if v.action=="doc" then
                    if outi==0 or #out[outi].bindings>0 then
                        outi=outi+1
                    end
                    out[outi]={doc=v.text, bindings={}}
                elseif v.submap then
                    parsetable(v.submap, prefix..v.kcb.." ")
                else
                    assert(out[outi])
                    v.kcb=prefix..v.kcb
                    table.insert(out[outi].bindings, v)
                end
            end
        end
    end

    if outi~=0 and #out[outi].bindings==0 then
        out[outi]=nil
    end

    parsetable(bindings, "")

    return out
end


local function combine_bindings(v)
    local nact={
        ["mpress"]=TR("press"),
        ["mclick"]=TR("click"),
        ["mdrag"]=TR("drag"),
        ["mdblclick"]=TR("double click"),
    }
    local first=true
    local s=""
    for _, b in ipairs(v.bindings) do
        if not first then
            s=s..', '
        end
        first=false
        if b.action=="kpress" or b.action=="kpress_wait" then
            s=s..b.kcb
        else
            if not b.area then
                s=s..TR("%s %s", b.kcb, nact[b.action])
            else
                s=s..TR("%s %s at %s", b.kcb, nact[b.action], b.area)
            end
        end
    end

    return s
end

local function write_bindings_man(db)
    local function write_binding_man(v)
        return '.TP\n.B '..combine_bindings(v)..'\n'..gettext(v.doc or "?")..'\n'
    end

    local s=""

    for _, v in ipairs(db) do
        if #(v.bindings)>0 then
            s=s..write_binding_man(v)
        end
    end

    return s
end

-- }}}

-- Main {{{

local infile
local outfile
local bindings={}
local replaces={}

local function doargs(a)
    local i=1
    while i<=#a do
        if a[i]=='-o' then
            outfile=a[i+1]
            i=i+2
        elseif a[i]=='-i' then
            infile=a[i+1]
            i=i+2
        elseif a[i]=='-D' then
            replaces[a[i+1]]=a[i+2]
            i=i+3
        elseif a[i]=='-po' then
            read_translations(a[i+1])
            i=i+2
        else
            dobindings(a[i], bindings)
            i=i+1
        end
    end
end

doargs(arg)

local f, err=io.open(infile)
if not f then
    error(err)
end

local of, oerr=io.open(outfile, 'w+')
if not of then
    error(oerr)
end

for l in f:lines() do
    l=string.gsub(l, '%s*BINDINGS:([%w%.%-]+)%s*',
                  function(s)
                      if not bindings[s] then
                          --error('No bindings for '..s)
                          return "?"
                      end
                      local db=docgroup_bindings(bindings[s])
                      return write_bindings_man(db)
                  end)

    for pat, rep in pairs(replaces) do
        l=string.gsub(l, pat, rep)
    end

    of:write(l..'\n')
end

-- }}}