awful: simplify function declaration

Signed-off-by: Julien Danjou <julien@danjou.info>
This commit is contained in:
Julien Danjou 2008-06-19 14:17:00 +02:00
parent b800cffd5b
commit 26b38535ca
1 changed files with 65 additions and 112 deletions

177
awful.lua
View File

@ -38,6 +38,12 @@ setfenv(1, P)
-- can easily add multiple functions per hook. -- can easily add multiple functions per hook.
P.hooks = {} P.hooks = {}
P.myhooks = {} P.myhooks = {}
P.menu = {}
P.screen = {}
P.layout = {}
P.menu.completion = {}
P.client = {}
P.tag = {}
-- Create a new userhook (for external libs) -- Create a new userhook (for external libs)
local function userhook_create(name) local function userhook_create(name)
@ -66,7 +72,7 @@ end
-- Function to get a client by its relative index: -- Function to get a client by its relative index:
-- set i to 1 to get next, -1 to get previous. -- set i to 1 to get next, -1 to get previous.
local function client_next(i) function P.client.next(i)
-- Get all visible clients -- Get all visible clients
local cls = client.visible_get(mouse.screen_get()) local cls = client.visible_get(mouse.screen_get())
-- Get currently focused client -- Get currently focused client
@ -81,28 +87,28 @@ local function client_next(i)
end end
-- Focus a client by its relative index. -- Focus a client by its relative index.
local function client_focus(i) function P.client.focus(i)
local c = client_next(i) local c = P.client.next(i)
if c then if c then
c:focus_set() c:focus_set()
end end
end end
-- Swap a client by its relative index. -- Swap a client by its relative index.
local function client_swap(i) function P.client.swap(i)
local c = client_next(i) local c = P.client.next(i)
local sel = client.focus_get() local sel = client.focus_get()
if c and sel then if c and sel then
sel:swap(c) sel:swap(c)
end end
end end
local function client_master() function P.client.master()
return client.visible_get(mouse.screen_get())[1] return client.visible_get(mouse.screen_get())[1]
end end
-- Move/resize a client relativ to current coordinates. -- Move/resize a client relativ to current coordinates.
local function client_moveresize(x, y, w, h) function P.client.moveresize(x, y, w, h)
local sel = client.focus_get() local sel = client.focus_get()
local coords = sel:coords_get() local coords = sel:coords_get()
coords['x'] = coords['x'] + x coords['x'] = coords['x'] + x
@ -112,7 +118,7 @@ local function client_moveresize(x, y, w, h)
sel:coords_set(coords) sel:coords_set(coords)
end end
local function screen_focus(i) function P.screen.focus(i)
local sel = client.focus_get() local sel = client.focus_get()
local s local s
if sel then if sel then
@ -134,7 +140,7 @@ local function screen_focus(i)
end end
-- Return a table with all visible tags -- Return a table with all visible tags
local function tag_selectedlist(s) function P.tag.selectedlist(s)
local idx = 1 local idx = 1
local screen = s or mouse.screen_get() local screen = s or mouse.screen_get()
local tags = tag.geti(screen) local tags = tag.geti(screen)
@ -150,70 +156,70 @@ end
-- Return only the first element of all visible tags, -- Return only the first element of all visible tags,
-- so that's the first visible tags. -- so that's the first visible tags.
local function tag_selected(s) function P.tag.selected(s)
return tag_selectedlist(s)[1] return P.tag.selectedlist(s)[1]
end end
-- Set master width factor -- Set master width factor
local function tag_setmwfact(i) function P.tag.setmwfact(i)
local t = tag_selected() local t = P.tag.selected()
if t then if t then
t:mwfact_set(i) t:mwfact_set(i)
end end
end end
-- Increase master width factor -- Increase master width factor
local function tag_incmwfact(i) function P.tag.incmwfact(i)
local t = tag_selected() local t = P.tag.selected()
if t then if t then
t:mwfact_set(t:mwfact_get() + i) t:mwfact_set(t:mwfact_get() + i)
end end
end end
-- Set number of master windows -- Set number of master windows
local function tag_setnmaster(i) function P.tag.setnmaster(i)
local t = tag_selected() local t = P.tag.selected()
if t then if t then
t:nmaster_set(i) t:nmaster_set(i)
end end
end end
-- Increase number of master windows -- Increase number of master windows
local function tag_incnmaster(i) function P.tag.incnmaster(i)
local t = tag_selected() local t = P.tag.selected()
if t then if t then
t:nmaster_set(t:nmaster_get() + i) t:nmaster_set(t:nmaster_get() + i)
end end
end end
-- Set number of column windows -- Set number of column windows
local function tag_setncol(i) function P.tag.setncol(i)
local t = tag_selected() local t = P.tag.selected()
if t then if t then
t:ncol_set(i) t:ncol_set(i)
end end
end end
-- Increase number of column windows -- Increase number of column windows
local function tag_incncol(i) function P.tag.incncol(i)
local t = tag_selected() local t = P.tag.selected()
if t then if t then
t:ncol_set(t:ncol_get() + i) t:ncol_set(t:ncol_get() + i)
end end
end end
-- View no tag -- View no tag
local function tag_viewnone() function P.tag.viewnone()
local tags = tag.get(mouse.screen_get()) local tags = tag.get(mouse.screen_get())
for i, t in pairs(tags) do for i, t in pairs(tags) do
t:view(false) t:view(false)
end end
end end
local function tag_viewidx(r) function P.tag.viewidx(r)
local tags = tag.geti(mouse.screen_get()) local tags = tag.geti(mouse.screen_get())
local sel = tag_selected() local sel = P.tag.selected()
tag_viewnone() P.tag.viewnone()
for i, t in ipairs(tags) do for i, t in ipairs(tags) do
if t == sel then if t == sel then
tags[array_boundandcycle(tags, i + r)]:view(true) tags[array_boundandcycle(tags, i + r)]:view(true)
@ -222,28 +228,28 @@ local function tag_viewidx(r)
end end
-- View next tag -- View next tag
local function tag_viewnext() function P.tag.viewnext()
return tag_viewidx(1) return P.tag.viewidx(1)
end end
-- View previous tag -- View previous tag
local function tag_viewprev() function P.tag.viewprev()
return tag_viewidx(-1) return P.tag.viewidx(-1)
end end
local function tag_viewonly(t) function P.tag.viewonly(t)
tag_viewnone() P.tag.viewnone()
t:view(true) t:view(true)
end end
local function tag_viewmore(tags) function P.tag.viewmore(tags)
tag_viewnone() P.tag.viewnone()
for i, t in pairs(tags) do for i, t in pairs(tags) do
t:view(true) t:view(true)
end end
end end
local function client_movetotag(target, c) function P.client.movetotag(target, c)
local sel = c or client.focus_get(); local sel = c or client.focus_get();
local tags = tag.get(mouse.screen_get()) local tags = tag.get(mouse.screen_get())
for k, t in pairs(tags) do for k, t in pairs(tags) do
@ -252,7 +258,7 @@ local function client_movetotag(target, c)
sel:tag(target, true) sel:tag(target, true)
end end
local function client_toggletag(target, c) function P.client.toggletag(target, c)
local sel = c or client.focus_get(); local sel = c or client.focus_get();
local toggle = false local toggle = false
if sel then if sel then
@ -270,7 +276,7 @@ local function client_toggletag(target, c)
end end
end end
local function client_togglefloating(c) function P.client.togglefloating(c)
local sel = c or client.focus_get(); local sel = c or client.focus_get();
if sel then if sel then
sel:floating_set(not sel:floating_get()) sel:floating_set(not sel:floating_get())
@ -278,7 +284,7 @@ local function client_togglefloating(c)
end end
-- Move a client to a screen. Default is next screen, cycling. -- Move a client to a screen. Default is next screen, cycling.
local function client_movetoscreen(c, s) function P.client.movetoscreen(c, s)
local sel = c or client.focus_get(); local sel = c or client.focus_get();
if sel then if sel then
local sc = screen.count() local sc = screen.count()
@ -290,8 +296,8 @@ local function client_movetoscreen(c, s)
end end
end end
local function layout_get(screen) function P.layout.get(screen)
local t = tag_selected(screen) local t = P.tag.selected(screen)
if t then if t then
return t:layout_get() return t:layout_get()
end end
@ -303,7 +309,7 @@ userhook_create('marked')
userhook_create('unmarked') userhook_create('unmarked')
-- Mark a client -- Mark a client
local function client_mark (c) function P.client.mark (c)
local cl = c or client.focus_get() local cl = c or client.focus_get()
if cl then if cl then
for k, v in pairs(awfulmarked) do for k, v in pairs(awfulmarked) do
@ -321,7 +327,7 @@ local function client_mark (c)
end end
-- Unmark a client -- Unmark a client
local function client_unmark(c) function P.client.unmark(c)
local cl = c or client.focus_get() local cl = c or client.focus_get()
for k, v in pairs(awfulmarked) do for k, v in pairs(awfulmarked) do
@ -336,7 +342,7 @@ local function client_unmark(c)
end end
-- Check if marked -- Check if marked
local function client_ismarked(c) function P.client.ismarked(c)
local cl = c or client.focus_get() local cl = c or client.focus_get()
if cl then if cl then
for k, v in pairs(awfulmarked) do for k, v in pairs(awfulmarked) do
@ -350,16 +356,16 @@ local function client_ismarked(c)
end end
-- Toggle marked -- Toggle marked
local function client_togglemarked(c) function P.client.togglemarked(c)
local cl = c or client.focus_get() local cl = c or client.focus_get()
if not client_mark(c) then if not P.client.mark(c) then
client_unmark(c) P.client.unmark(c)
end end
end end
-- Return the marked clients and empty the table -- Return the marked clients and empty the table
local function client_getmarked () function P.client.getmarked()
for k, v in pairs(awfulmarked) do for k, v in pairs(awfulmarked) do
userhook_call('unmarked', {v}) userhook_call('unmarked', {v})
end end
@ -372,8 +378,8 @@ end
-- Function to change the layout of the current tag. -- Function to change the layout of the current tag.
-- layouts = table of layouts (define in .awesomerc.lua) -- layouts = table of layouts (define in .awesomerc.lua)
-- i = relative index -- i = relative index
local function layout_inc(layouts, i) function P.layout.inc(layouts, i)
local t = tag_selected() local t = P.tag.selected()
local number_of_layouts = 0 local number_of_layouts = 0
local rev_layouts = {} local rev_layouts = {}
for i, v in ipairs(layouts) do for i, v in ipairs(layouts) do
@ -381,7 +387,7 @@ local function layout_inc(layouts, i)
number_of_layouts = number_of_layouts + 1 number_of_layouts = number_of_layouts + 1
end end
if t then if t then
local cur_layout = layout_get() local cur_layout = layout.get()
local new_layout_index = (rev_layouts[cur_layout] + i) % number_of_layouts local new_layout_index = (rev_layouts[cur_layout] + i) % number_of_layouts
if new_layout_index == 0 then if new_layout_index == 0 then
new_layout_index = number_of_layouts new_layout_index = number_of_layouts
@ -391,8 +397,8 @@ local function layout_inc(layouts, i)
end end
-- function to set the layout of the current tag by name. -- function to set the layout of the current tag by name.
local function layout_set(layout) function P.layout.set(layout)
local t = tag_selected() local t = P.tag.selected()
if t then if t then
t:layout_set(layout) t:layout_set(layout)
end end
@ -446,15 +452,15 @@ for name, hook in pairs(hooks) do
end end
end end
local function spawn(cmd) function P.spawn(cmd)
return os.execute(cmd .. "&") return os.execute(cmd .. "&")
end end
local function eval(s) function P.eval(s)
return assert(loadstring("return " .. s))() return assert(loadstring("return " .. s))()
end end
local function menu_completion_bash(command, cur_pos, ncomp) function P.menu.completion.bash(command, cur_pos, ncomp)
local wstart = 1 local wstart = 1
local wend = 1 local wend = 1
local words = {} local words = {}
@ -529,7 +535,7 @@ local function menu_text_with_cursor(text, text_color, cursor_color, cursor_pos)
return text_start .. "<span background=\"" .. cursor_color .. "\" foreground=\"" .. text_color .. "\">" .. char .. "</span>" .. text_end return text_start .. "<span background=\"" .. cursor_color .. "\" foreground=\"" .. text_color .. "\">" .. char .. "</span>" .. text_end
end end
local function menu(args, textbox, exe_callback, completion_callback) function P.menu(args, textbox, exe_callback, completion_callback)
if not args then return end if not args then return end
local command = "" local command = ""
local command_before_comp local command_before_comp
@ -646,7 +652,7 @@ local function menu(args, textbox, exe_callback, completion_callback)
end) end)
end end
local function escape(text) function P.escape(text)
text = text:gsub("&", "&amp;") text = text:gsub("&", "&amp;")
text = text:gsub("<", "&lt;") text = text:gsub("<", "&lt;")
text = text:gsub(">", "&gt;") text = text:gsub(">", "&gt;")
@ -655,7 +661,7 @@ local function escape(text)
return text return text
end end
local function unescape(text) function P.unescape(text)
text = text:gsub("&amp;", "&") text = text:gsub("&amp;", "&")
text = text:gsub("&lt;", "<") text = text:gsub("&lt;", "<")
text = text:gsub("&gt;", ">") text = text:gsub("&gt;", ">")
@ -664,57 +670,4 @@ local function unescape(text)
return text return text
end end
-- Export tags function
P.tag =
{
viewnone = tag_viewnone;
viewprev = tag_viewprev;
viewnext = tag_viewnext;
viewonly = tag_viewonly;
viewmore = tag_viewmore;
setmwfact = tag_setmwfact;
incmwfact = tag_incmwfact;
setncol = tag_setncol;
incncol = tag_incncol;
setnmaster = tag_setnmaster;
incnmaster = tag_incnmaster;
selected = tag_selected;
selectedlist = tag_selectedlist;
}
P.client =
{
next = client_next;
focus = client_focus;
swap = client_swap;
master = client_master;
movetotag = client_movetotag;
toggletag = client_toggletag;
togglefloating = client_togglefloating;
moveresize = client_moveresize;
movetoscreen = client_movetoscreen;
mark = client_mark;
unmark = client_unmark;
ismarked = client_ismarked;
togglemarked = client_togglemarked;
getmarked = client_getmarked;
}
P.screen =
{
focus = screen_focus;
}
P.layout =
{
get = layout_get;
set = layout_set;
inc = layout_inc;
}
P.completion =
{
bash = menu_completion_bash;
}
P.spawn = spawn
P.menu = menu
P.escape = escape
P.eval = eval
return P return P