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]
66 function ioncore.evalmenu(menu, ...)
67 if type(menu)=="string" then
68 return ioncore.evalmenu(menus[menu], ...)
69 elseif type(menu)=="function" then
71 elseif type(menu)=="table" then
77 -- Use this function to define normal menu entries. The string \var{name}
78 -- is the string shown in the visual representation of menu. The
79 -- parameter \var{cmd} and \var{guard_or_opts} (when string) are similar
80 -- to those of \fnref{ioncore.defbindings}. If \var{guard_or_opts} is
81 -- a table, it may contains the \var{guard} field, and the \var{priority}
82 -- field, for controlling positioning of entries in context menus.
83 -- (The default priority is 1 for most entries, and -1 for auto-generated
85 function ioncore.menuentry(name, cmd, guard_or_opts)
89 if type(guard_or_opts)=="string" then
91 elseif type(guard_or_opts)=="table" then
96 local fn, gfn=ioncore.compile_cmd(cmd, guard)
99 name=ioncore.gettext(name),
108 -- Use this function to define menu entries for submenus. The parameter
109 -- \fnref{sub_or_name} is either a table of menu entries or the name
110 -- of an already defined menu. The initial menu entry to highlight can be
111 -- specified by \var{options.initial} as either an integer starting from 1,
112 -- or a function that returns such a number. Another option supported is
113 -- \var{options.noautoexpand} that will cause \fnref{mod_query.query_menu}
114 -- to not automatically expand this submenu.
115 function ioncore.submenu(name, sub_or_name, options)
116 return table.append({
117 name=ioncore.gettext(name),
118 submenu_fn=function()
119 return ioncore.evalmenu (sub_or_name)
128 -- Workspace and window lists {{{
130 local function addto(list)
131 return function(tgt, attr)
132 local e=menuentry(tgt:name(), function() tgt:goto() end)
134 table.insert(list, e)
139 local function sort(entries)
140 table.sort(entries, function(a, b) return a.name < b.name end)
144 function menus.windowlist()
146 ioncore.clientwin_i(addto(entries))
150 function menus.workspacelist()
152 local iter_=addto(entries)
154 local function iter(obj)
155 return (not obj_is(obj, "WGroupWS")
159 ioncore.region_i(iter)
164 local function focuslist(do_act)
167 local iter_=addto(entries)
169 local function iter(obj, attr)
170 if obj_is(obj, "WClientWin") then
177 local function iter_act(obj)
178 return iter(obj, "activity")
181 local function iter_foc(obj)
182 return (seen[obj] or iter(obj))
186 -- Windows with activity first
187 ioncore.activity_i(iter_act)
190 -- The ones that have been focused in their lifetime
191 ioncore.focushistory_i(iter_foc)
194 ioncore.clientwin_i(iter_foc)
199 menus.focuslist=function() return focuslist(true) end
200 menus.focuslist_=function() return focuslist(false) end
208 local function mplex_of(reg)
209 while reg and not obj_is(reg, "WMPlex") do
215 local function selectstyle(look, where)
218 local fname=ioncore.get_savefile('look')
220 local function writeit()
221 local f, err=io.open(fname, 'w')
223 mod_query.message(where, err)
225 f:write(string.format('dopath("%s")\n', look))
230 if not mod_query then
237 where=mplex_of(where)
243 query_message(where, TR("Cannot save selection."))
247 mod_query.query_yesno(where, TR("Save look selection in %s?", fname),
251 local function receive_styles(str)
256 if string.len(data)>ioncore.RESULT_DATA_LIMIT then
257 error(TR("Too much result data"))
259 str=coroutine.yield()
266 for look in string.gfind(data, "(look[-_][^\n]*)%.lua\n") do
267 if not found[look] then
269 table.insert(styles, look)
275 for _, look in ipairs(styles) do
277 table.insert(stylemenu, menuentry(look,
279 selectstyle(look_, where)
283 table.insert(stylemenu, menuentry(TR("Refresh list"),
284 ioncore.refresh_stylelist))
286 menus.stylemenu=stylemenu
291 -- Refresh list of known style files.
292 function ioncore.refresh_stylelist()
293 local cmd=ioncore.lookup_script("ion-completefile")
295 local path=ioncore.get_paths().searchpath
296 local function mkarg(s)
300 return (" "..string.shell_safe(s).."/look_"..
301 " "..string.shell_safe(s).."/look-")
306 cmd=cmd..string.gsub(path..":", "([^:]*):", mkarg)
308 ioncore.popen_bgread(cmd, coroutine.wrap(receive_styles))
318 local function classes(reg)
319 local function classes_(t)
320 if t.__parentclass then
321 classes_(t.__parentclass)
323 coroutine.yield(t.__typename)
325 return coroutine.wrap(function() classes_(reg) end)
329 local function modeparts(mode)
331 return function() return end
334 local f, s, v=string.gmatch(mode, "(%-?[^-]+)");
336 local function nxt(_, m)
338 return (v and (m .. v))
345 local function get_ctxmenu(reg, sub)
348 local function cp(m2)
350 for k, v in ipairs(m2) do
351 local v2=table.copy(v)
355 v2.func=function() return ofunc(reg, sub) end
358 if v2.submenu_fn then
359 local ofn=v2.submenu_fn
360 v2.submenu_fn=function() return cp(ofn()) end
369 local function add_ctxmenu(m2, use_label)
371 m=table.icat(m, cp(m2))
372 m.label=(use_label and m2.label) or m.label
376 local mgr=reg:manager()
377 local mgrname=(mgr and mgr:name()) or nil
378 local mode=(reg.mode and reg:mode())
380 for s in classes(reg) do
381 local nm="ctxmenu-"..s
382 add_ctxmenu(ioncore.evalmenu(nm), true)
383 for m in modeparts(mode) do
384 add_ctxmenu(ioncore.evalmenu(nm.."."..m), false)
387 add_ctxmenu(ioncore.evalmenu(nm.."@"..mgrname), false)
394 local function sortmenu(m)
397 for _, e in ipairs(m) do
398 e.priority=(e.priority or 1)+v
402 table.sort(m, function(e1, e2) return e1.priority > e2.priority end)
408 function menus.ctxmenu(reg, sub_or_chld)
411 -- First, stuff between reg (inclusive) and sub_or_chld (inclusive)
412 -- at the top level in the menu.
413 r=(sub_or_chld or reg)
414 while r and s~=reg do
415 local mm=get_ctxmenu(r, s)
416 m=((m and table.icat(mm, m)) or mm)
423 -- Then stuff below reg (exclusive) as submenus
425 local mm = get_ctxmenu(r, s)
427 local nm=mm.label or obj_typename(reg)
428 local tmp=ioncore.submenu(nm, sortmenu(mm), {priority=-1})
440 ioncore.refresh_stylelist()