2 -- ion/ioncore/ioncore_menudb.lua -- Routines for defining menus.
4 -- Copyright (c) Tuomo Valkonen 2004-2007.
6 -- Ion is free software; you can redistribute it and/or modify it under
7 -- the terms of the GNU Lesser General Public License as published by
8 -- the Free Software Foundation; either version 2.1 of the License, or
9 -- (at your option) any later version.
12 local ioncore=_G.ioncore
15 -- Table to hold defined menus.
19 -- Menu construction {{{
22 -- Define a new menu with \var{name} being the menu's name and \var{tab}
23 -- being a table of menu entries. If \var{tab.append} is set, the entries
24 -- are appended to previously-defined ones, if possible.
25 function ioncore.defmenu(name, tab)
26 if menus[name] and type(tab)=="table" and tab.append then
27 if type(menus[name])~="table" then
28 ioncore.warn(TR("Unable to append to non-table menu"))
31 for k, v in ipairs(tab) do
32 table.insert(menus[name], v)
41 -- Returns a menu defined with \fnref{ioncore.defmenu}.
42 function ioncore.getmenu(name)
47 -- Define context menu for context \var{ctx}, \var{tab} being a table
49 function ioncore.defctxmenu(ctx, ...)
51 if #arg>1 and type(arg[1])=="string" then
53 tab.label=ioncore.gettext(arg[1])
57 ioncore.defmenu("ctxmenu-"..ctx, tab)
61 -- Returns a context menu defined with \fnref{ioncore.defctxmenu}.
62 function ioncore.getctxmenu(name)
63 return menus["ctxmenu-"..name]
67 function ioncore.evalmenu(menu, args)
68 if type(menu)=="string" then
69 return ioncore.evalmenu(menus[menu], args)
70 elseif type(menu)=="function" then
72 return menu(unpack(args))
76 elseif type(menu)=="table" then
83 -- Use this function to define normal menu entries. The string \var{name}
84 -- is the string shown in the visual representation of menu, and the
85 -- parameter \var{cmd} and \var{guard} are similar to those of
86 -- \fnref{ioncore.defbindings}.
87 function ioncore.menuentry(name, cmd, guard)
88 local fn, gfn=ioncore.compile_cmd(cmd, guard)
90 return {name=ioncore.gettext(name), func=fn, guard_func=gfn}
95 -- Use this function to define menu entries for submenus. The parameter
96 -- \fnref{sub_or_name} is either a table of menu entries or the name
97 -- of an already defined menu. The initial menu entry to highlight can be
98 -- specified by \var{options.initial} as either an integer starting from 1,
99 -- or a function that returns such a number. Another option supported is
100 -- \var{options.noautoexpand} that will cause \fnref{mod_query.query_menu}
101 -- to not automatically expand this submenu.
102 function ioncore.submenu(name, sub_or_name, options)
107 name=ioncore.gettext(name),
108 submenu_fn=function()
109 return ioncore.evalmenu(sub_or_name)
111 initial=options.initial,
112 noautoexpand=options.noautoexpand,
120 -- Workspace and window lists {{{
122 local function addto(list)
123 return function(tgt, attr)
124 local e=menuentry(tgt:name(), function() tgt:goto() end)
126 table.insert(list, e)
131 local function sort(entries)
132 table.sort(entries, function(a, b) return a.name < b.name end)
136 function menus.windowlist()
138 ioncore.clientwin_i(addto(entries))
142 function menus.workspacelist()
144 local iter_=addto(entries)
146 local function iter(obj)
147 return (not obj_is(obj, "WGroupWS")
151 ioncore.region_i(iter)
156 local function focuslist(do_act)
159 local iter_=addto(entries)
161 local function iter(obj, attr)
162 if obj_is(obj, "WClientWin") then
169 local function iter_act(obj)
170 return iter(obj, "activity")
173 local function iter_foc(obj)
174 return (seen[obj] or iter(obj))
178 ioncore.activity_i(iter_act)
181 ioncore.focushistory_i(iter_foc)
186 menus.focuslist=function() return focuslist(true) end
187 menus.focuslist_=function() return focuslist(false) end
195 local function mplex_of(reg)
196 while reg and not obj_is(reg, "WMPlex") do
202 local function selectstyle(look, where)
205 local fname=ioncore.get_savefile('look')
207 local function writeit()
208 local f, err=io.open(fname, 'w')
210 mod_query.message(where, err)
212 f:write(string.format('dopath("%s")\n', look))
217 if not mod_query then
224 where=mplex_of(where)
230 query_message(where, TR("Cannot save selection."))
234 mod_query.query_yesno(where, TR("Save look selection in %s?", fname),
238 local function receive_styles(str)
243 if string.len(data)>ioncore.RESULT_DATA_LIMIT then
244 error(TR("Too much result data"))
246 str=coroutine.yield()
253 for look in string.gfind(data, "(look[-_][^\n]*)%.lua\n") do
254 if not found[look] then
256 table.insert(styles, look)
262 for _, look in ipairs(styles) do
264 table.insert(stylemenu, menuentry(look,
266 selectstyle(look_, where)
270 table.insert(stylemenu, menuentry(TR("Refresh list"),
271 ioncore.refresh_stylelist))
273 menus.stylemenu=stylemenu
278 -- Refresh list of known style files.
279 function ioncore.refresh_stylelist()
280 local cmd=ioncore.lookup_script("ion-completefile")
282 local path=ioncore.get_paths().searchpath
283 local function mkarg(s)
287 return (" "..string.shell_safe(s).."/look_"..
288 " "..string.shell_safe(s).."/look-")
293 cmd=cmd..string.gsub(path..":", "([^:]*):", mkarg)
295 ioncore.popen_bgread(cmd, coroutine.wrap(receive_styles))
305 local function classes(reg)
306 local function classes_(t)
307 if t.__parentclass then
308 classes_(t.__parentclass)
310 coroutine.yield(t.__typename)
312 return coroutine.wrap(function() classes_(reg) end)
316 local function modeparts(mode)
318 return function() return end
321 local f, s, v=string.gmatch(mode, "(%-?[^-]+)");
323 local function nxt(_, m)
325 return (v and (m .. v))
332 local function get_ctxmenu(reg, sub, is_par)
335 local function cp(m2)
337 for k, v in ipairs(m2) do
338 local v2=table.copy(v)
342 v2.func=function() return ofunc(reg, sub) end
345 if v2.submenu_fn then
346 local ofn=v2.submenu_fn
347 v2.submenu_fn=function() return cp(ofn()) end
356 local function add_ctxmenu(m2, use_label)
363 m.label=(use_label and m2.label) or m.label
367 local mgr=reg:manager()
368 local mgrname=(mgr and mgr:name()) or nil
369 local mode=(reg.mode and reg:mode())
371 for s in classes(reg) do
372 local nm="ctxmenu-"..s
373 add_ctxmenu(ioncore.evalmenu(nm), true)
374 for m in modeparts(mode) do
375 add_ctxmenu(ioncore.evalmenu(nm.."."..m), false)
378 add_ctxmenu(ioncore.evalmenu(nm.."@"..mgrname), false)
384 function menus.ctxmenu(reg, sub)
385 local m=get_ctxmenu(reg, sub, false);
391 local mm = get_ctxmenu(reg, sub, true)
393 local nm=mm.label or obj_typename(reg)
394 table.insert(m, ioncore.submenu(nm, mm))
405 ioncore.refresh_stylelist()