4 -- Translates bindings from Ion configuration into a listing for
13 local function gettext(x)
14 local t=translations[x]
15 if not t or t=="" then
22 local function TR(x, ...)
23 return string.format(gettext(x), unpack(arg))
26 local function read_translations(pofile)
27 local f, err=io.open(pofile)
32 local msgid, msgstr, st, en
35 if string.find(l, "^msgid") then
38 translations[msgid]=msgstr
41 st, en, msgid=string.find(l, '^msgid%s*"(.*)"%s*$')
42 elseif string.find(l, "^msgstr") then
43 assert(msgid and not msgstr)
44 st, en, msgstr=string.find(l, '^msgstr%s*"(.*)"%s*$')
45 elseif not (string.find(l, "^%s*#") or string.find(l, "^%s*$")) then
46 local st, en, str=string.find(l, '^%s*"(.*)"%s*$')
47 assert(msgid or msgstr)
58 translations[msgid]=msgstr
69 local function dobindings(fn, bindings)
76 return {action = "doc", text = text}
79 function p.submap(kcb_, list)
82 return submap(kcb_, lst)
85 return {action = "kpress", kcb = kcb_, submap = list}
88 local function putcmd(cmd, guard, t)
94 function p.kpress(keyspec, cmd, guard)
95 return putcmd(cmd, guard, {action = "kpress", kcb = keyspec})
98 function p.kpress_wait(keyspec, cmd, guard)
99 return putcmd(cmd, guard, {action = "kpress_wait", kcb = keyspec})
102 local function mact(act_, kcb_, cmd, guard)
103 local st, en, kcb2_, area_=string.find(kcb_, "([^@]*)@(.*)")
104 return putcmd(cmd, guard, {
106 kcb = (kcb2_ or kcb_),
111 function p.mclick(buttonspec, cmd, guard)
112 return mact("mclick", buttonspec, cmd, guard)
115 function p.mdblclick(buttonspec, cmd, guard)
116 return mact("mdblclick", buttonspec, cmd, guard)
119 function p.mpress(buttonspec, cmd, guard)
120 return mact("mpress", buttonspec, cmd, guard)
123 function p.mdrag(buttonspec, cmd, guard)
124 return mact("mdrag", buttonspec, cmd, guard)
127 function p.defbindings(context, bnd)
128 if not bindings[context] then
129 bindings[context]=bnd
131 for _, v in ipairs(bnd) do
132 table.insert(bindings[context], v)
137 local function dummy()
145 p.ioncore={ set=dummy }
147 local env=setmetatable({}, {
149 __newindex=function(x)
150 error("Setting global "..tostring(x))
158 local function parsefile(f, bindings)
159 local fn, err=loadfile(f)
164 return dobindings(fn, bindings)
170 -- Binding output {{{
172 local function docgroup_bindings(bindings)
176 local function parsetable(t, prefix)
177 for _, v in ipairs(t) do
179 v.kcb=string.gsub(v.kcb, "AnyModifier%+", "")
181 if v.action=="doc" then
183 out[outi]={doc=v.text, bindings={}}
185 parsetable(v.submap, prefix..v.kcb.." ")
189 table.insert(out[outi].bindings, v)
194 parsetable(bindings, "")
200 local function combine_bindings(v)
202 ["mpress"]=TR("press"),
203 ["mclick"]=TR("click"),
204 ["mdrag"]=TR("drag"),
205 ["mdblclick"]=TR("double click"),
209 for _, b in ipairs(v.bindings) do
214 if b.action=="kpress" or b.action=="kpress_wait" then
218 s=s..TR("%s %s", b.kcb, nact[b.action])
220 s=s..TR("%s %s at %s", b.kcb, nact[b.action], b.area)
228 local function write_bindings_man(db)
229 local function write_binding_man(v)
230 return '.TP\n.B '..combine_bindings(v)..'\n'..gettext(v.doc)..'\n'
235 for _, v in ipairs(db) do
236 if #(v.bindings)>0 then
237 s=s..write_binding_man(v)
253 local function doargs(a)
259 elseif a[i]=='-i' then
262 elseif a[i]=='-D' then
263 replaces[a[i+1]]=a[i+2]
265 elseif a[i]=='-po' then
266 read_translations(a[i+1])
269 parsefile(a[i], bindings)
277 local f, err=io.open(infile)
282 local of, oerr=io.open(outfile, 'w+')
287 for l in f:lines() do
288 l=string.gsub(l, '%s*BINDINGS:([%w%.%-]+)%s*',
290 if not bindings[s] then
291 --error('No bindings for '..s)
294 local db=docgroup_bindings(bindings[s])
295 return write_bindings_man(db)
298 for pat, rep in pairs(replaces) do
299 l=string.gsub(l, pat, rep)