2008-08-04 14:31:53 +02:00
|
|
|
---------------------------------------------------------------------------
|
2008-08-04 16:41:23 +02:00
|
|
|
-- @author Lucas de Vries <lucasdevries@gmail.com>
|
|
|
|
-- @author Julien Danjou <julien@danjou.info>
|
2008-08-04 14:31:53 +02:00
|
|
|
-- @copyright 2008 Julien Danjou, Lucas de Vries
|
2008-08-18 11:41:58 +02:00
|
|
|
-- @release @AWESOME_VERSION@
|
2008-08-04 14:31:53 +02:00
|
|
|
---------------------------------------------------------------------------
|
2008-06-10 10:18:56 +02:00
|
|
|
|
|
|
|
-- Grab environment we need
|
2008-08-04 14:31:53 +02:00
|
|
|
local capi = { client = client }
|
2008-06-10 10:18:56 +02:00
|
|
|
local table = table
|
|
|
|
local pairs = pairs
|
2008-08-04 14:31:53 +02:00
|
|
|
local awful = require('awful')
|
2008-09-30 16:39:43 +02:00
|
|
|
local otable = otable
|
2008-06-10 10:18:56 +02:00
|
|
|
|
2008-11-07 13:31:32 +01:00
|
|
|
--- Fabulous tabs
|
2008-08-04 14:31:53 +02:00
|
|
|
module("tabulous")
|
2008-06-10 10:18:56 +02:00
|
|
|
|
2008-09-30 16:39:43 +02:00
|
|
|
local tabbed_tags = otable() -- all tab tables, indexed by tab
|
|
|
|
local tabbed = {} -- the current tag tab table
|
2008-08-28 10:36:21 +02:00
|
|
|
|
|
|
|
local active_tag
|
2008-06-10 10:18:56 +02:00
|
|
|
|
2008-06-25 19:28:47 +02:00
|
|
|
-- Hook creation
|
2008-08-04 14:31:53 +02:00
|
|
|
awful.hooks.user.create('tabbed')
|
|
|
|
awful.hooks.user.create('untabbed')
|
|
|
|
awful.hooks.user.create('tabdisplay')
|
|
|
|
awful.hooks.user.create('tabhide')
|
2008-06-11 07:47:57 +02:00
|
|
|
|
|
|
|
---------------
|
|
|
|
-- Functions --
|
|
|
|
---------------
|
|
|
|
|
2008-06-25 19:28:47 +02:00
|
|
|
--- Find a key in a table.
|
|
|
|
-- @param table The table to look into
|
|
|
|
-- @param value The value to find.
|
|
|
|
-- @return The key or nil if not found.
|
2008-08-04 14:31:53 +02:00
|
|
|
function findkey(table, value)
|
2008-06-25 19:28:47 +02:00
|
|
|
for k, v in pairs(table) do
|
|
|
|
if v == value then
|
|
|
|
return k
|
2008-06-10 10:18:56 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-08-04 14:31:53 +02:00
|
|
|
--- Swaand select which client in tab is displayed.
|
2008-06-25 19:28:47 +02:00
|
|
|
-- @param tabindex The tab index.
|
|
|
|
-- @param cl The client to show.
|
2008-08-04 14:31:53 +02:00
|
|
|
function display(tabindex, cl)
|
2008-06-11 07:47:57 +02:00
|
|
|
local p = tabbed[tabindex][1]
|
|
|
|
if cl and p ~= cl then
|
2008-08-30 06:44:15 +02:00
|
|
|
tabbed[tabindex][1] = cl
|
|
|
|
|
2008-07-01 19:43:23 +02:00
|
|
|
cl.hide = false
|
2008-06-11 07:47:57 +02:00
|
|
|
cl:swap(p)
|
2008-07-01 19:43:23 +02:00
|
|
|
p.hide = true
|
2008-08-28 11:29:54 +02:00
|
|
|
capi.client.focus = cl
|
2008-08-04 14:31:53 +02:00
|
|
|
awful.hooks.user.call('tabhide', p)
|
|
|
|
awful.hooks.user.call('tabdisplay', cl)
|
2008-06-10 10:18:56 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-06-25 19:28:47 +02:00
|
|
|
--- Check if the client is in the given tabindex.
|
|
|
|
-- @param tabindex The tab index.
|
|
|
|
-- @param cl The client
|
|
|
|
-- @return The key.
|
2008-08-04 14:31:53 +02:00
|
|
|
function tabindex_check(tabindex, cl)
|
2008-08-28 11:29:54 +02:00
|
|
|
local c = cl or capi.client.focus
|
2008-08-30 06:44:15 +02:00
|
|
|
return findkey(tabbed[tabindex][2], c)
|
2008-06-11 07:47:57 +02:00
|
|
|
end
|
2008-06-10 10:18:56 +02:00
|
|
|
|
2008-06-25 19:28:47 +02:00
|
|
|
--- Find the tab index or return nil if not tabbed.
|
|
|
|
-- @param cl The client to search.
|
|
|
|
-- @return The tab index.
|
2008-08-04 14:31:53 +02:00
|
|
|
function tabindex_get(cl)
|
2008-08-28 11:29:54 +02:00
|
|
|
local c = cl or capi.client.focus
|
2008-06-11 07:47:57 +02:00
|
|
|
|
|
|
|
for tabindex, tabdisplay in pairs(tabbed) do
|
|
|
|
-- Loop through all tab displays
|
2008-08-04 14:31:53 +02:00
|
|
|
local me = tabindex_check(tabindex, c)
|
2008-06-10 10:18:56 +02:00
|
|
|
|
2008-06-11 07:47:57 +02:00
|
|
|
if me ~= nil then
|
|
|
|
return tabindex
|
|
|
|
end
|
2008-06-10 10:18:56 +02:00
|
|
|
end
|
2008-06-11 07:47:57 +02:00
|
|
|
|
|
|
|
return nil
|
2008-06-10 10:18:56 +02:00
|
|
|
end
|
|
|
|
|
2008-06-25 19:28:47 +02:00
|
|
|
--- Get all clients on a tabbed display.
|
|
|
|
-- @param tabindex The tab index.
|
|
|
|
-- @return All tabbed clients.
|
2008-08-04 14:31:53 +02:00
|
|
|
function clients_get(tabindex)
|
2008-06-11 12:57:12 +02:00
|
|
|
if tabbed[tabindex] == nil then return nil end
|
2008-06-11 12:32:50 +02:00
|
|
|
return tabbed[tabindex][2]
|
2008-06-11 07:47:57 +02:00
|
|
|
end
|
|
|
|
|
2008-06-25 19:28:47 +02:00
|
|
|
--- Get the displayed client on a tabbed display.
|
|
|
|
-- @param tabindex The tab index.
|
|
|
|
-- @return The displayed client.
|
2008-08-04 14:31:53 +02:00
|
|
|
function displayed_get(tabindex)
|
2008-06-11 12:57:12 +02:00
|
|
|
if tabbed[tabindex] == nil then return nil end
|
2008-06-11 12:32:50 +02:00
|
|
|
return tabbed[tabindex][1]
|
|
|
|
end
|
2008-06-11 07:47:57 +02:00
|
|
|
|
2008-06-25 19:28:47 +02:00
|
|
|
--- Get a client by tab number.
|
|
|
|
-- @param tabindex The tab index.
|
|
|
|
-- @param pos The position in the tab.
|
|
|
|
-- @return The client at the given position.
|
2008-08-04 14:31:53 +02:00
|
|
|
function position_get(tabindex, pos)
|
2008-06-11 12:32:50 +02:00
|
|
|
if tabbed[tabindex] == nil then return nil end
|
|
|
|
return tabbed[tabindex][2][pos]
|
2008-06-10 10:18:56 +02:00
|
|
|
end
|
|
|
|
|
2008-06-25 19:28:47 +02:00
|
|
|
--- Get the next client in a tab display.
|
|
|
|
-- @param tabindex The tab index.
|
|
|
|
-- @param cl The current client.
|
|
|
|
-- @return The next client.
|
2008-08-04 14:31:53 +02:00
|
|
|
function next(tabindex, cl)
|
2008-06-11 07:47:57 +02:00
|
|
|
local c = cl or tabbed[tabindex][1]
|
|
|
|
|
2008-08-04 14:31:53 +02:00
|
|
|
local i = findkey(tabbed[tabindex][2], c)
|
2008-06-11 07:47:57 +02:00
|
|
|
|
|
|
|
if i == nil then
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
|
|
|
if tabbed[tabindex][2][i+1] == nil then
|
|
|
|
return tabbed[tabindex][2][1]
|
2008-06-10 10:18:56 +02:00
|
|
|
end
|
2008-06-25 19:28:47 +02:00
|
|
|
|
|
|
|
return tabbed[tabindex][2][i + 1]
|
2008-06-10 10:18:56 +02:00
|
|
|
end
|
|
|
|
|
2008-06-25 19:28:47 +02:00
|
|
|
--- Get the previous client in a tabdisplay
|
|
|
|
-- @param tabindex The tab index.
|
|
|
|
-- @param cl The current client.
|
|
|
|
-- @return The previous client.
|
2008-08-04 14:31:53 +02:00
|
|
|
function prev(tabindex, cl)
|
2008-06-11 07:47:57 +02:00
|
|
|
local c = cl or tabbed[tabindex][1]
|
|
|
|
|
2008-08-04 14:31:53 +02:00
|
|
|
local i = findkey(tabbed[tabindex][2], c)
|
2008-06-11 07:47:57 +02:00
|
|
|
|
|
|
|
if i == nil then
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
|
|
|
if tabbed[tabindex][2][i-1] == nil then
|
|
|
|
return tabbed[tabindex][2][table.maxn(tabbed[tabindex][2])]
|
2008-06-10 10:18:56 +02:00
|
|
|
end
|
2008-06-25 19:28:47 +02:00
|
|
|
|
|
|
|
return tabbed[tabindex][2][i - 1]
|
2008-06-10 10:18:56 +02:00
|
|
|
end
|
|
|
|
|
2008-06-25 19:28:47 +02:00
|
|
|
--- Remove a client from a tabbed display.
|
|
|
|
-- @param cl The client to remove.
|
|
|
|
-- @return True if the client has been untabbed.
|
2008-08-04 14:31:53 +02:00
|
|
|
function untab(cl)
|
2008-08-28 11:29:54 +02:00
|
|
|
local c = cl or capi.client.focus
|
2008-08-04 14:31:53 +02:00
|
|
|
local tabindex = tabindex_get(c)
|
2008-06-25 19:28:47 +02:00
|
|
|
|
2008-06-11 07:47:57 +02:00
|
|
|
if tabindex == nil then return false end
|
|
|
|
|
2008-08-04 14:31:53 +02:00
|
|
|
local cindex = findkey(tabbed[tabindex][2], c)
|
2008-06-11 07:47:57 +02:00
|
|
|
|
|
|
|
if tabbed[tabindex][1] == c then
|
2008-08-10 08:04:09 +02:00
|
|
|
display(tabindex, next(tabindex, c))
|
2008-06-10 10:18:56 +02:00
|
|
|
end
|
2008-06-11 07:47:57 +02:00
|
|
|
|
|
|
|
table.remove(tabbed[tabindex][2], cindex)
|
|
|
|
|
2008-06-11 11:09:23 +02:00
|
|
|
if table.maxn(tabbed[tabindex][2]) == 0 then
|
2008-06-11 07:47:57 +02:00
|
|
|
-- Table is empty now, remove the tabbed display
|
|
|
|
table.remove(tabbed, tabindex)
|
|
|
|
end
|
|
|
|
|
2008-07-01 19:43:23 +02:00
|
|
|
c.hide = false
|
2008-08-04 14:31:53 +02:00
|
|
|
awful.hooks.user.call('untabbed', c)
|
2008-06-10 10:18:56 +02:00
|
|
|
end
|
|
|
|
|
2008-06-25 19:28:47 +02:00
|
|
|
--- Untab all clients in a tabbed display.
|
|
|
|
-- @param tabindex The tab index.
|
2008-08-04 14:31:53 +02:00
|
|
|
function untab_all(tabindex)
|
2008-06-11 12:57:12 +02:00
|
|
|
for i,c in pairs(tabbed[tabindex][2]) do
|
2008-07-01 19:43:23 +02:00
|
|
|
c.hide = false
|
2008-08-04 14:31:53 +02:00
|
|
|
awful.hooks.user.call('untabbed', c)
|
2008-06-11 12:57:12 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
if tabbed[tabindex] ~= nil then
|
|
|
|
table.remove(tabbed, tabindex)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-06-25 19:28:47 +02:00
|
|
|
--- Create a new tabbed display with client as the master.
|
|
|
|
-- @param cl The client to set into the tab, focused one otherwise.
|
|
|
|
-- @return The created tab index.
|
2008-08-04 14:31:53 +02:00
|
|
|
function tab_create(cl)
|
2008-08-28 11:29:54 +02:00
|
|
|
local c = cl or capi.client.focus
|
2008-06-11 12:32:50 +02:00
|
|
|
|
2008-06-25 19:28:47 +02:00
|
|
|
if not c then return end
|
|
|
|
|
2008-06-11 12:32:50 +02:00
|
|
|
table.insert(tabbed, {
|
2008-06-25 19:28:47 +02:00
|
|
|
c, -- Window currently being displayed
|
2008-06-11 12:32:50 +02:00
|
|
|
{ c } -- List of windows in tabbed display
|
|
|
|
})
|
|
|
|
|
2008-08-04 14:31:53 +02:00
|
|
|
awful.hooks.user.call('tabbed', c)
|
|
|
|
return tabindex_get(c)
|
2008-06-11 12:32:50 +02:00
|
|
|
end
|
|
|
|
|
2008-06-25 19:28:47 +02:00
|
|
|
--- Add a client to a tabbed display.
|
|
|
|
-- @param tabindex The tab index.
|
|
|
|
-- @param cl The client to add, or the focused one otherwise.
|
2008-08-04 14:31:53 +02:00
|
|
|
function tab(tabindex, cl)
|
2008-08-28 11:29:54 +02:00
|
|
|
local c = cl or capi.client.focus
|
2008-06-11 12:32:50 +02:00
|
|
|
|
|
|
|
if tabbed[tabindex] ~= nil then
|
2008-08-04 14:31:53 +02:00
|
|
|
local x = tabindex_get(c)
|
2008-06-11 12:32:50 +02:00
|
|
|
|
|
|
|
if x == nil then
|
|
|
|
-- Add untabbed client to tabindex
|
|
|
|
table.insert(tabbed[tabindex][2], c)
|
2008-08-04 14:31:53 +02:00
|
|
|
display(tabindex, c)
|
2008-06-11 12:32:50 +02:00
|
|
|
|
2008-08-04 14:31:53 +02:00
|
|
|
awful.hooks.user.call('tabbed', c)
|
2008-06-11 13:24:38 +02:00
|
|
|
elseif x ~= tabindex then
|
2008-06-11 12:32:50 +02:00
|
|
|
-- Merge two tabbed views
|
2008-06-11 13:24:38 +02:00
|
|
|
local cc = tabbed[tabindex][1]
|
2008-08-04 14:31:53 +02:00
|
|
|
local clients = clients_get(x)
|
|
|
|
untab_all(x)
|
2008-06-11 12:32:50 +02:00
|
|
|
|
2008-08-04 14:31:53 +02:00
|
|
|
tabindex = tabindex_get(cc)
|
2008-06-11 13:24:38 +02:00
|
|
|
|
2008-06-11 12:57:12 +02:00
|
|
|
for i,b in pairs(clients) do
|
2008-08-04 14:31:53 +02:00
|
|
|
tab(tabindex, b)
|
2008-06-11 12:32:50 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-06-25 19:28:47 +02:00
|
|
|
--- Start autotabbing, this automatically tabs new clients when the current
|
|
|
|
-- client is tabbed.
|
2008-08-04 14:31:53 +02:00
|
|
|
function autotab_start()
|
|
|
|
awful.hooks.manage.register(function (c)
|
2008-08-28 11:29:54 +02:00
|
|
|
local sel = capi.client.focus
|
2008-08-04 14:31:53 +02:00
|
|
|
local index = tabindex_get(sel)
|
2008-06-11 07:47:57 +02:00
|
|
|
|
|
|
|
if index ~= nil then
|
|
|
|
-- Currently focussed client is tabbed,
|
|
|
|
-- add the new window to the tabbed display
|
2008-08-04 14:31:53 +02:00
|
|
|
tab(index, c)
|
2008-06-11 07:47:57 +02:00
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2008-08-30 06:44:15 +02:00
|
|
|
--- Update the tabbing when current tags changes, by unactivating
|
2008-08-28 11:29:54 +02:00
|
|
|
-- all tabs that are not in the current tag (and activating the good one)
|
|
|
|
function update_tabbing()
|
2008-08-28 10:36:21 +02:00
|
|
|
-- do nothing if nothing changed
|
2008-09-30 16:39:43 +02:00
|
|
|
if active_tag == awful.tag.selected() then return end
|
2008-08-28 10:36:21 +02:00
|
|
|
|
|
|
|
-- needed for initialisation
|
2008-09-30 16:39:43 +02:00
|
|
|
active_tag = active_tag or awful.tag.selected()
|
2008-08-28 10:36:21 +02:00
|
|
|
|
|
|
|
-- save the tabbed list for the old active tag
|
|
|
|
tabbed_tags[active_tag] = tabbed
|
|
|
|
|
|
|
|
-- update the active tag and switch to the new tabbed list
|
2008-09-30 16:39:43 +02:00
|
|
|
active_tag = awful.tag.selected()
|
2008-08-28 10:36:21 +02:00
|
|
|
tabbed = tabbed_tags[active_tag] or {}
|
|
|
|
|
|
|
|
-- update show/hide on the clients
|
|
|
|
for tag,tabs in pairs(tabbed_tags) do
|
|
|
|
if tag == active_tag then
|
|
|
|
-- hide all clients that are not tab masters
|
|
|
|
for i,tab in pairs(tabs) do
|
|
|
|
for i,c in pairs(tab[2]) do
|
|
|
|
c.hide = true
|
|
|
|
end
|
|
|
|
tab[1].hide = false
|
|
|
|
end
|
|
|
|
else
|
|
|
|
-- unhide all clients
|
|
|
|
for i,tab in pairs(tabs) do
|
|
|
|
for i,c in pairs(tab[2]) do
|
|
|
|
c.hide = false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-08-30 06:44:15 +02:00
|
|
|
--- Keep tabulous in sync when a client is focused but was hidden in a tab
|
|
|
|
-- (show the client and hide the previous one)
|
|
|
|
function hook_focus(cl)
|
|
|
|
local c = cl or awful.client.focus
|
|
|
|
local i = tabindex_get(c)
|
|
|
|
if i and tabbed[i][1] ~= c then
|
|
|
|
display(i, c)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Keep tabulous in sync when focus change
|
|
|
|
awful.hooks.focus.register(hook_focus)
|
2008-08-28 11:29:54 +02:00
|
|
|
-- update the tabbing when the tags changes
|
2008-08-28 10:36:21 +02:00
|
|
|
awful.hooks.arrange.register(update_tabbing)
|
|
|
|
|
2008-06-11 07:47:57 +02:00
|
|
|
-- Set up hook so we don't leave lost hidden clients
|
2008-08-04 14:31:53 +02:00
|
|
|
awful.hooks.unmanage.register(untab)
|
2008-08-06 15:39:51 +02:00
|
|
|
|
2008-08-28 10:36:21 +02:00
|
|
|
|
2008-08-06 15:39:51 +02:00
|
|
|
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80
|