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 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
|
--
-- ion/ioncore/ioncore_menudb.lua -- Routines for defining menus.
--
-- Copyright (c) Tuomo Valkonen 2004-2007.
--
-- See the included file LICENSE for details.
--
local ioncore=_G.ioncore
-- Table to hold defined menus.
local menus={}
-- Menu construction {{{
--DOC
-- Define a new menu with \var{name} being the menu's name and \var{tab}
-- being a table of menu entries. If \var{tab.append} is set, the entries
-- are appended to previously-defined ones, if possible.
function ioncore.defmenu(name, tab)
if menus[name] and type(tab)=="table" and tab.append then
if type(menus[name])~="table" then
ioncore.warn(TR("Unable to append to non-table menu"))
return
else
for k, v in ipairs(tab) do
table.insert(menus[name], v)
end
end
else
menus[name]=tab
end
end
--DOC
-- Returns a menu defined with \fnref{ioncore.defmenu}.
function ioncore.getmenu(name)
return menus[name]
end
--DOC
-- Define context menu for context \var{ctx}, \var{tab} being a table
-- of menu entries.
function ioncore.defctxmenu(ctx, ...)
local arg = {...}
local tab, add
if #arg>1 and type(arg[1])=="string" then
tab=arg[2]
tab.label=ioncore.gettext(arg[1])
else
tab=arg[1]
end
ioncore.defmenu("ctxmenu-"..ctx, tab)
end
--DOC
-- Returns a context menu defined with \fnref{ioncore.defctxmenu}.
function ioncore.getctxmenu(name)
return menus["ctxmenu-"..name]
end
function ioncore.evalmenu(menu, ...)
if type(menu)=="string" then
return ioncore.evalmenu(menus[menu], ...)
elseif type(menu)=="function" then
return menu(...)
elseif type(menu)=="table" then
return menu
end
end
--DOC
-- Use this function to define normal menu entries. The string \var{name}
-- is the string shown in the visual representation of menu. The
-- parameter \var{cmd} and \var{guard_or_opts} (when string) are similar
-- to those of \fnref{ioncore.defbindings}. If \var{guard_or_opts} is
-- a table, it may contains the \var{guard} field, and the \var{priority}
-- field, for controlling positioning of entries in context menus.
-- (The default priority is 1 for most entries, and -1 for auto-generated
-- submenus.)
function ioncore.menuentry(name, cmd, guard_or_opts)
local guard
local opts
if type(guard_or_opts)=="string" then
guard=guard_or_opts
elseif type(guard_or_opts)=="table" then
opts=guard_or_opts
guard=opts.guard
end
local fn, gfn=ioncore.compile_cmd(cmd, guard)
if fn then
return table.append({
name=ioncore.gettext(name),
func=fn,
guard_func=gfn,
}, opts or {})
end
end
--DOC
-- Use this function to define menu entries for submenus. The parameter
-- \fnref{sub_or_name} is either a table of menu entries or the name
-- of an already defined menu. The initial menu entry to highlight can be
-- specified by \var{options.initial} as either an integer starting from 1,
-- or a function that returns such a number. Another option supported is
-- \var{options.noautoexpand} that will cause \fnref{mod_query.query_menu}
-- to not automatically expand this submenu.
function ioncore.submenu(name, sub_or_name, options)
return table.append({
name=ioncore.gettext(name),
submenu_fn=function()
return ioncore.evalmenu (sub_or_name)
end,
}, options or {})
end
-- }}}
-- Workspace and window lists {{{
local function addto(list)
return function(tgt, attr)
local e=menuentry(tgt:name(), function() tgt:goto_focus() end)
e.attr=attr;
table.insert(list, e)
return true
end
end
local function sort(entries)
table.sort(entries, function(a, b) return a.name < b.name end)
return entries
end
function menus.windowlist()
local entries={}
ioncore.clientwin_i(addto(entries))
return sort(entries)
end
function menus.workspacelist()
local entries={}
local iter_=addto(entries)
local function iter(obj)
return (not obj_is(obj, "WGroupWS")
or iter_(obj))
end
ioncore.region_i(iter)
return sort(entries)
end
local function focuslist(do_act)
local entries={}
local seen={}
local iter_=addto(entries)
local function iter(obj, attr)
if obj_is(obj, "WClientWin") then
iter_(obj, attr)
seen[obj]=true
end
return true
end
local function iter_act(obj)
return iter(obj, "activity")
end
local function iter_foc(obj)
return (seen[obj] or iter(obj))
end
if do_act then
-- Windows with activity first
ioncore.activity_i(iter_act)
end
-- The ones that have been focused in their lifetime
ioncore.focushistory_i(iter_foc)
-- And then the rest
ioncore.clientwin_i(iter_foc)
return entries
end
menus.focuslist=function() return focuslist(true) end
menus.focuslist_=function() return focuslist(false) end
local function ws_or_fullscreen_of(reg)
local ws=ioncore.find_manager(reg, "WGroupWS")
local is_scratch=false
if not ws then
-- Fullscreen windows doesn't have a WGroupWS manager
-- but are the child of Screen
ws=obj_is(reg:parent(), "WScreen") and reg
-- Scratchpads can be a frame
local frame=ioncore.find_manager(reg, "WFrame")
is_scratch=mod_sp and frame and mod_sp.is_scratchpad(frame)
else
-- Or a ws
is_scratch=mod_sp and mod_sp.is_scratchpad(ws)
end
if not ws or is_scratch then
-- Ignore scratchpads
return false
else
return ws
end
end
function menus.workspacefocuslist()
local entries={}
local seen={}
local iter_=addto(entries)
local focused_ws=ws_or_fullscreen_of(ioncore.current())
-- Ignore the currently focused workspace
if focused_ws then
seen[focused_ws]=true
end
local function iter(reg)
local ws=ws_or_fullscreen_of(reg)
-- If we started in a scratchpad consider the first visited
-- workspace as the one we started in and ignore it
if not focused_ws and ws then
focused_ws=ws
seen[ws]=true
end
if ws and not seen[ws] then
iter_(ws)
seen[ws]=true
end
return true
end
-- Add workspaces which have had focus
ioncore.focushistory_i(iter)
-- Add the rest
ioncore.region_i(iter, "WGroupWS")
-- Add create workspace entry
local create_ws=menuentry(" -- Create new workspace -- ",
"mod_query.query_workspace(_)")
table.insert(entries, create_ws)
return entries
end
--DOC
-- Go to and return to a previously active workspace (if any).
--
-- Note that this function is asynchronous; the region will not
-- actually have received the focus when this function returns.
function ioncore.goto_previous_workspace()
local ws
local focused_ws=ws_or_fullscreen_of(ioncore.current())
local function iter(reg)
ws=ws_or_fullscreen_of(reg)
if ws and not (ws==focused_ws) then
return false
end
return true
end
-- Add workspaces which have had focus
ioncore.focushistory_i(iter)
if ws then
ws:goto_focus()
end
return ws
end
-- }}}
-- Style menu {{{
local function mplex_of(reg)
while reg and not obj_is(reg, "WMPlex") do
reg=reg:parent()
end
return reg
end
local function selectstyle(look, where)
dopath(look)
local fname=ioncore.get_savefile('look')
local function writeit()
local f, err=io.open(fname, 'w')
if not f then
mod_query.message(where, err)
else
f:write(string.format('dopath("%s")\n', look))
f:close()
end
end
if not mod_query then
if fname then
writeit()
end
return
end
where=mplex_of(where)
if not where then
return
end
if not fname then
query_message(where, TR("Cannot save selection."))
return
end
mod_query.query_yesno(where, TR("Save look selection in %s?", fname),
writeit)
end
local function receive_styles(str)
local data=""
while str do
data=data .. str
if string.len(data)>ioncore.RESULT_DATA_LIMIT then
error(TR("Too much result data"))
end
str=coroutine.yield()
end
local found={}
local styles={}
local stylemenu={}
for look in string.gmatch(data, "(look[-_][^\n]*)%.lua\n") do
if not found[look] then
found[look]=true
table.insert(styles, look)
end
end
table.sort(styles)
for _, look in ipairs(styles) do
local look_=look
table.insert(stylemenu, menuentry(look,
function(where)
selectstyle(look_, where)
end))
end
table.insert(stylemenu, menuentry(TR("Refresh list"),
ioncore.refresh_stylelist))
menus.stylemenu=stylemenu
end
--DOC
-- Refresh list of known style files.
function ioncore.refresh_stylelist()
local cmd=ioncore.lookup_script("ion-completefile")
if cmd then
local path=ioncore.get_paths().searchpath
local function mkarg(s)
if s=="" then
return ""
else
return (" "..string.shell_safe(s).."/look_"..
" "..string.shell_safe(s).."/look-")
end
return ""
end
cmd=cmd..string.gsub(path..":", "([^:]*):", mkarg)
ioncore.popen_bgread(cmd, coroutine.wrap(receive_styles))
end
end
-- }}}
-- Context menu {{{
local function classes(reg)
local function classes_(t)
if t.__parentclass then
classes_(t.__parentclass)
end
coroutine.yield(t.__typename)
end
return coroutine.wrap(function() classes_(reg) end)
end
local function modeparts(mode)
if not mode then
return function() return end
end
local f, s, v=string.gmatch(mode, "(%-?[^-]+)");
local function nxt(_, m)
v = f(s, v)
return (v and (m .. v))
end
return nxt, nil, ""
end
local function get_ctxmenu(reg, sub)
local m={}
local function cp(m2)
local m3={}
for k, v in ipairs(m2) do
local v2=table.copy(v)
if v2.func then
local ofunc=v2.func
v2.func=function() return ofunc(reg, sub) end
end
if v2.submenu_fn then
local ofn=v2.submenu_fn
v2.submenu_fn=function() return cp(ofn()) end
end
m3[k]=v2
end
m3.label=m2.label
return m3
end
local function add_ctxmenu(m2, use_label)
if m2 then
m=table.icat(m, cp(m2))
m.label=(use_label and m2.label) or m.label
end
end
local mgr=reg:manager()
local mgrname=(mgr and mgr:name()) or nil
local mode=(reg.mode and reg:mode())
for s in classes(reg) do
local nm="ctxmenu-"..s
add_ctxmenu(ioncore.evalmenu(nm), true)
for m in modeparts(mode) do
add_ctxmenu(ioncore.evalmenu(nm.."."..m), false)
end
if mgrname then
add_ctxmenu(ioncore.evalmenu(nm.."@"..mgrname), false)
end
end
return m
end
local function sortmenu(m)
local v=1/2
for _, e in ipairs(m) do
e.priority=(e.priority or 1)+v
v=v/2
end
table.sort(m, function(e1, e2) return e1.priority > e2.priority end)
return m
end
function menus.ctxmenu(reg, sub)
local m, r, s
if obj_is(sub, "WGroup") then
sub=(sub:bottom() or sub)
end
-- First, stuff between reg (inclusive) and sub_or_chld (inclusive)
-- at the top level in the menu.
r=(sub or reg)
while r and s~=reg do
local mm=get_ctxmenu(r, s)
m=((m and table.icat(mm, m)) or mm)
s=r
r=r:manager()
end
m=(m or {})
-- Then stuff below reg (exclusive) as submenus
while r do
local mm = get_ctxmenu(r, s)
if #mm>0 then
local nm=mm.label or obj_typename(reg)
local tmp=ioncore.submenu(nm, sortmenu(mm), {priority=-1})
table.insert(m, tmp)
end
s=r
r=r:manager()
end
return sortmenu(m)
end
-- }}}
ioncore.refresh_stylelist()
|