2008-05-20 15:39:47 +02:00
|
|
|
-----------------------------------------------
|
|
|
|
-- awful: AWesome Function very UsefuL --
|
|
|
|
-- Common useful awesome functions --
|
|
|
|
-- --
|
|
|
|
-- © 2008 Julien Danjou <julien@danjou.info> --
|
|
|
|
-----------------------------------------------
|
|
|
|
|
|
|
|
-- We usually are required as 'awful'
|
|
|
|
-- But that can be changed.
|
|
|
|
local P = {} -- package
|
|
|
|
if _REQUIREDNAME == nil then
|
|
|
|
awful = P
|
|
|
|
else
|
|
|
|
_G[_REQUIREDNAME] = P
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Grab environment we need
|
|
|
|
local ipairs = ipairs
|
|
|
|
local awesome = awesome
|
2008-05-27 16:07:17 +02:00
|
|
|
local screen = screen
|
2008-05-20 15:39:47 +02:00
|
|
|
local client = client
|
|
|
|
local tag = tag
|
|
|
|
local mouse = mouse
|
|
|
|
local os = os
|
|
|
|
|
|
|
|
-- Reset env
|
|
|
|
setfenv(1, P)
|
|
|
|
|
|
|
|
-- Function to the good value in table, cycling
|
|
|
|
function array_boundandcycle(t, i)
|
|
|
|
if i > #t then
|
|
|
|
i = 1
|
|
|
|
elseif i < 1 then
|
|
|
|
i = #t
|
|
|
|
end
|
|
|
|
return i
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
-- Function to get a client by its relative index:
|
|
|
|
-- set i to 1 to get next, -1 to get previous.
|
|
|
|
function client_next(i)
|
|
|
|
-- Get all visible clients
|
2008-05-23 13:17:02 +02:00
|
|
|
local cls = client.visible_get(mouse.screen_get())
|
2008-05-20 15:39:47 +02:00
|
|
|
-- Get currently focused client
|
|
|
|
local sel = client.focus_get()
|
|
|
|
if not sel then return end
|
|
|
|
-- Loop upon each client
|
|
|
|
for idx, c in ipairs(cls) do
|
|
|
|
if c == sel then
|
|
|
|
return cls[array_boundandcycle(cls, idx +i)]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Focus a client by its relative index.
|
|
|
|
function client_focus(i)
|
|
|
|
local c = client_next(i)
|
|
|
|
if c then
|
|
|
|
c:focus_set()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Swap a client by its relative index.
|
|
|
|
function client_swap(i)
|
|
|
|
local c = client_next(i)
|
|
|
|
local sel = client.focus_get()
|
|
|
|
if c and sel then
|
|
|
|
sel:swap(c)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function screen_focus(i)
|
|
|
|
local sel = client.focus_get()
|
|
|
|
local s
|
|
|
|
if sel then
|
|
|
|
s = sel:screen_get()
|
|
|
|
else
|
|
|
|
s = mouse.screen_get()
|
|
|
|
end
|
2008-05-27 16:07:17 +02:00
|
|
|
local count = screen.count()
|
2008-05-20 15:39:47 +02:00
|
|
|
s = s + i
|
|
|
|
if s < 1 then
|
|
|
|
s = count
|
|
|
|
elseif s > count then
|
|
|
|
s = 1
|
|
|
|
end
|
2008-05-27 16:07:17 +02:00
|
|
|
screen.focus(s)
|
2008-05-21 16:59:39 +02:00
|
|
|
-- Move the mouse on the screen
|
2008-05-27 16:07:17 +02:00
|
|
|
local screen_coords = screen.coords_get(s)
|
2008-05-21 16:59:39 +02:00
|
|
|
mouse.coords_set(screen_coords['x'], screen_coords['y'])
|
2008-05-20 15:39:47 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Return a table with all visible tags
|
2008-05-26 16:07:35 +02:00
|
|
|
function tag_selectedlist(s)
|
2008-05-20 15:39:47 +02:00
|
|
|
local idx = 1
|
2008-05-26 16:07:35 +02:00
|
|
|
local screen = s or mouse.screen_get()
|
2008-05-23 13:17:02 +02:00
|
|
|
local tags = tag.get(screen)
|
2008-05-20 15:39:47 +02:00
|
|
|
local vtags = {}
|
|
|
|
for i, t in ipairs(tags) do
|
|
|
|
if t:isselected() then
|
|
|
|
vtags[idx] = t
|
|
|
|
idx = idx + 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return vtags
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Return only the first element of all visible tags,
|
|
|
|
-- so that's the first visible tags.
|
2008-05-26 16:07:35 +02:00
|
|
|
function tag_selected(s)
|
|
|
|
return tag_selectedlist(s)[1]
|
2008-05-20 15:39:47 +02:00
|
|
|
end
|
|
|
|
|
2008-05-23 11:51:53 +02:00
|
|
|
-- Set master width factor
|
|
|
|
function tag_setmwfact(i)
|
2008-05-23 17:09:49 +02:00
|
|
|
local t = tag_selected()
|
2008-05-23 11:51:53 +02:00
|
|
|
if t then
|
|
|
|
t:mwfact_set(i)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-05-20 15:39:47 +02:00
|
|
|
-- Increase master width factor
|
|
|
|
function tag_incmwfact(i)
|
2008-05-23 17:09:49 +02:00
|
|
|
local t = tag_selected()
|
2008-05-20 15:39:47 +02:00
|
|
|
if t then
|
|
|
|
t:mwfact_set(t:mwfact_get() + i)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-05-23 11:51:53 +02:00
|
|
|
-- Set number of master windows
|
|
|
|
function tag_setnmaster(i)
|
2008-05-23 17:09:49 +02:00
|
|
|
local t = tag_selected()
|
2008-05-23 11:51:53 +02:00
|
|
|
if t then
|
|
|
|
t:nmaster_set(i)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-05-20 15:39:47 +02:00
|
|
|
-- Increase number of master windows
|
|
|
|
function tag_incnmaster(i)
|
2008-05-23 17:09:49 +02:00
|
|
|
local t = tag_selected()
|
2008-05-20 15:39:47 +02:00
|
|
|
if t then
|
|
|
|
t:nmaster_set(t:nmaster_get() + i)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-05-23 11:51:53 +02:00
|
|
|
-- Set number of column windows
|
|
|
|
function tag_setncol(i)
|
2008-05-23 17:09:49 +02:00
|
|
|
local t = tag_selected()
|
2008-05-23 11:51:53 +02:00
|
|
|
if t then
|
|
|
|
t:ncol_set(i)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-05-20 15:39:47 +02:00
|
|
|
-- Increase number of column windows
|
|
|
|
function tag_incncol(i)
|
2008-05-23 17:09:49 +02:00
|
|
|
local t = tag_selected()
|
2008-05-20 15:39:47 +02:00
|
|
|
if t then
|
|
|
|
t:ncol_set(t:ncol_get() + i)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- View no tag
|
|
|
|
function tag_viewnone()
|
2008-05-23 13:17:02 +02:00
|
|
|
local tags = tag.get(mouse.screen_get())
|
2008-05-20 15:39:47 +02:00
|
|
|
for i, t in ipairs(tags) do
|
|
|
|
t:view(false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function tag_viewidx(r)
|
2008-05-23 13:17:02 +02:00
|
|
|
local tags = tag.get(mouse.screen_get())
|
2008-05-23 17:09:49 +02:00
|
|
|
local sel = tag_selected()
|
2008-05-20 15:39:47 +02:00
|
|
|
tag_viewnone()
|
|
|
|
for i, t in ipairs(tags) do
|
|
|
|
if t == sel then
|
2008-05-22 08:56:57 +02:00
|
|
|
tags[array_boundandcycle(tags, i + r)]:view(true)
|
2008-05-20 15:39:47 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- View next tag
|
|
|
|
function tag_viewnext()
|
|
|
|
return tag_viewidx(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- View previous tag
|
|
|
|
function tag_viewprev()
|
|
|
|
return tag_viewidx(-1)
|
|
|
|
end
|
|
|
|
|
|
|
|
function tag_viewonly(t)
|
|
|
|
tag_viewnone()
|
|
|
|
t:view(true)
|
|
|
|
end
|
|
|
|
|
2008-05-23 11:51:53 +02:00
|
|
|
function tag_viewmore(tags)
|
|
|
|
tag_viewnone()
|
|
|
|
for i, t in ipairs(tags) do
|
|
|
|
t:view(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-05-20 15:39:47 +02:00
|
|
|
function client_movetotag(target, c)
|
|
|
|
local sel = c or client.focus_get();
|
2008-05-23 13:17:02 +02:00
|
|
|
local tags = tag.get(mouse.screen_get())
|
2008-05-20 15:39:47 +02:00
|
|
|
for i, t in ipairs(tags) do
|
|
|
|
sel:tag(t, false)
|
|
|
|
end
|
|
|
|
sel:tag(target, true)
|
|
|
|
end
|
|
|
|
|
|
|
|
function client_toggletag(target, c)
|
|
|
|
local sel = c or client.focus_get();
|
|
|
|
if sel then
|
|
|
|
sel:tag(target, not sel:istagged(target))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function client_togglefloating(c)
|
|
|
|
local sel = c or client.focus_get();
|
|
|
|
if sel then
|
|
|
|
sel:floating_set(not sel:floating_get())
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-05-26 16:07:35 +02:00
|
|
|
function layout_get(screen)
|
|
|
|
local t = tag_selected(screen)
|
2008-05-23 17:09:49 +02:00
|
|
|
if t then
|
|
|
|
return t:layout_get()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-05-24 13:55:48 +02:00
|
|
|
-- Function to change the layout of the current tag.
|
|
|
|
-- layouts = table of layouts (define in .awesomerc.lua)
|
|
|
|
-- i = relative index
|
|
|
|
function layout_inc(layouts, i)
|
|
|
|
local t = tag_selected()
|
|
|
|
local number_of_layouts = 0
|
|
|
|
local rev_layouts = {}
|
|
|
|
for i, v in ipairs(layouts) do
|
|
|
|
rev_layouts[v] = i
|
|
|
|
number_of_layouts = number_of_layouts + 1
|
|
|
|
end
|
|
|
|
if t then
|
|
|
|
local cur_layout = layout_get()
|
|
|
|
local new_layout_index = (rev_layouts[cur_layout] + i) % number_of_layouts
|
|
|
|
if new_layout_index == 0 then
|
|
|
|
new_layout_index = number_of_layouts
|
|
|
|
end
|
|
|
|
t:layout_set(layouts[new_layout_index])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- function to set the layout of the current tag by name.
|
|
|
|
function layout_set(layout)
|
|
|
|
local t = tag_selected()
|
|
|
|
if t then
|
|
|
|
t:layout_set(layout)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-05-20 15:39:47 +02:00
|
|
|
function spawn(cmd)
|
|
|
|
return os.execute(cmd .. "&")
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Export tags function
|
|
|
|
P.tag =
|
|
|
|
{
|
|
|
|
viewnone = tag_viewnone;
|
|
|
|
viewprev = tag_viewprev;
|
|
|
|
viewnext = tag_viewnext;
|
|
|
|
viewonly = tag_viewonly;
|
2008-05-23 11:51:53 +02:00
|
|
|
viewmore = tag_viewmore;
|
|
|
|
setmwfact = tag_setmwfact;
|
2008-05-20 15:39:47 +02:00
|
|
|
incmwfact = tag_incmwfact;
|
2008-05-23 11:51:53 +02:00
|
|
|
setncol = tag_setncol;
|
2008-05-20 15:39:47 +02:00
|
|
|
incncol = tag_incncol;
|
2008-05-23 11:51:53 +02:00
|
|
|
setnmaster = tag_setnmaster;
|
2008-05-20 15:39:47 +02:00
|
|
|
incnmaster = tag_incnmaster;
|
2008-05-23 17:09:49 +02:00
|
|
|
selected = tag_selected;
|
|
|
|
selectedlist = tag_selectedlist;
|
2008-05-20 15:39:47 +02:00
|
|
|
}
|
|
|
|
P.client =
|
|
|
|
{
|
|
|
|
next = client_next;
|
|
|
|
focus = client_focus;
|
|
|
|
swap = client_swap;
|
|
|
|
movetotag = client_movetotag;
|
|
|
|
toggletag = client_toggletag;
|
|
|
|
togglefloating = client_togglefloating;
|
|
|
|
}
|
|
|
|
P.screen =
|
|
|
|
{
|
|
|
|
focus = screen_focus;
|
|
|
|
}
|
2008-05-23 17:09:49 +02:00
|
|
|
P.layout =
|
|
|
|
{
|
2008-05-24 13:55:48 +02:00
|
|
|
get = layout_get;
|
|
|
|
set = layout_set;
|
|
|
|
inc = layout_inc;
|
2008-05-23 17:09:49 +02:00
|
|
|
}
|
2008-05-20 15:39:47 +02:00
|
|
|
P.spawn = spawn
|
|
|
|
|
|
|
|
return P
|