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
|
2008-06-13 17:51:28 +02:00
|
|
|
local assert = assert
|
|
|
|
local loadstring = loadstring
|
2008-05-20 15:39:47 +02:00
|
|
|
local ipairs = ipairs
|
2008-05-31 15:47:00 +02:00
|
|
|
local pairs = pairs
|
2008-06-11 07:47:57 +02:00
|
|
|
local unpack = unpack
|
2008-05-20 15:39:47 +02:00
|
|
|
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
|
2008-06-09 21:43:09 +02:00
|
|
|
local tag = tag
|
2008-05-20 15:39:47 +02:00
|
|
|
local mouse = mouse
|
|
|
|
local os = os
|
2008-05-31 15:47:00 +02:00
|
|
|
local table = table
|
|
|
|
local hooks = hooks
|
2008-06-10 16:50:55 +02:00
|
|
|
local keygrabber = keygrabber
|
2008-06-18 20:07:18 +02:00
|
|
|
local io = io
|
2008-05-20 15:39:47 +02:00
|
|
|
|
|
|
|
-- Reset env
|
|
|
|
setfenv(1, P)
|
|
|
|
|
2008-06-11 10:35:54 +02:00
|
|
|
-- Hook functions, wrappers around awesome's hooks. functions so we
|
|
|
|
-- can easily add multiple functions per hook.
|
|
|
|
P.hooks = {}
|
|
|
|
P.myhooks = {}
|
|
|
|
|
|
|
|
-- Create a new userhook (for external libs)
|
|
|
|
local function userhook_create(name)
|
|
|
|
P.myhooks[name] = {}
|
|
|
|
P.hooks[name] = function (f)
|
|
|
|
table.insert(P.myhooks[name], {callback = f})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Call a created userhook (for external libs
|
|
|
|
local function userhook_call(name, args)
|
|
|
|
for i,o in pairs(P.myhooks[name]) do
|
|
|
|
P.myhooks[name][i]['callback'](unpack(args))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-05-20 15:39:47 +02:00
|
|
|
-- Function to the good value in table, cycling
|
2008-06-10 16:50:55 +02:00
|
|
|
local function array_boundandcycle(t, i)
|
2008-05-20 15:39:47 +02:00
|
|
|
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.
|
2008-06-10 16:50:55 +02:00
|
|
|
local function client_next(i)
|
2008-05-20 15:39:47 +02:00
|
|
|
-- 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
|
2008-06-10 10:18:56 +02:00
|
|
|
return cls[array_boundandcycle(cls, idx + i)]
|
2008-05-20 15:39:47 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Focus a client by its relative index.
|
2008-06-10 16:50:55 +02:00
|
|
|
local function client_focus(i)
|
2008-05-20 15:39:47 +02:00
|
|
|
local c = client_next(i)
|
|
|
|
if c then
|
|
|
|
c:focus_set()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Swap a client by its relative index.
|
2008-06-10 16:50:55 +02:00
|
|
|
local function client_swap(i)
|
2008-05-20 15:39:47 +02:00
|
|
|
local c = client_next(i)
|
|
|
|
local sel = client.focus_get()
|
|
|
|
if c and sel then
|
|
|
|
sel:swap(c)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-06-18 12:47:39 +02:00
|
|
|
local function client_master()
|
|
|
|
return client.visible_get(mouse.screen_get())[1]
|
|
|
|
end
|
|
|
|
|
2008-06-06 01:17:41 +02:00
|
|
|
-- Move/resize a client relativ to current coordinates.
|
2008-06-10 16:50:55 +02:00
|
|
|
local function client_moveresize(x, y, w, h)
|
2008-06-06 01:17:41 +02:00
|
|
|
local sel = client.focus_get()
|
|
|
|
local coords = sel:coords_get()
|
|
|
|
coords['x'] = coords['x'] + x
|
|
|
|
coords['y'] = coords['y'] + y
|
|
|
|
coords['width'] = coords['width'] + w
|
|
|
|
coords['height'] = coords['height'] + h
|
|
|
|
sel:coords_set(coords)
|
|
|
|
end
|
|
|
|
|
2008-06-10 16:50:55 +02:00
|
|
|
local function screen_focus(i)
|
2008-06-09 21:43:09 +02:00
|
|
|
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-06-09 21:43:09 +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
|
|
|
|
|
2008-06-09 21:43:09 +02:00
|
|
|
-- Return a table with all visible tags
|
2008-06-10 16:50:55 +02:00
|
|
|
local function tag_selectedlist(s)
|
2008-06-09 21:43:09 +02:00
|
|
|
local idx = 1
|
|
|
|
local screen = s or mouse.screen_get()
|
2008-06-18 17:56:24 +02:00
|
|
|
local tags = tag.geti(screen)
|
2008-06-09 21:43:09 +02:00
|
|
|
local vtags = {}
|
2008-06-18 16:04:49 +02:00
|
|
|
for i, t in pairs(tags) do
|
2008-06-09 21:43:09 +02:00
|
|
|
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-06-10 16:50:55 +02:00
|
|
|
local function tag_selected(s)
|
2008-06-09 21:43:09 +02:00
|
|
|
return tag_selectedlist(s)[1]
|
|
|
|
end
|
|
|
|
|
2008-05-23 11:51:53 +02:00
|
|
|
-- Set master width factor
|
2008-06-10 16:50:55 +02:00
|
|
|
local function tag_setmwfact(i)
|
2008-06-09 21:43:09 +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
|
2008-06-10 16:50:55 +02:00
|
|
|
local function tag_incmwfact(i)
|
2008-06-09 21:43:09 +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
|
2008-06-10 16:50:55 +02:00
|
|
|
local function tag_setnmaster(i)
|
2008-06-09 21:43:09 +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
|
2008-06-10 16:50:55 +02:00
|
|
|
local function tag_incnmaster(i)
|
2008-06-09 21:43:09 +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
|
2008-06-10 16:50:55 +02:00
|
|
|
local function tag_setncol(i)
|
2008-06-09 21:43:09 +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
|
2008-06-10 16:50:55 +02:00
|
|
|
local function tag_incncol(i)
|
2008-06-09 21:43:09 +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
|
|
|
|
|
2008-06-09 21:43:09 +02:00
|
|
|
-- View no tag
|
2008-06-10 16:50:55 +02:00
|
|
|
local function tag_viewnone()
|
2008-06-09 21:43:09 +02:00
|
|
|
local tags = tag.get(mouse.screen_get())
|
2008-06-18 16:04:49 +02:00
|
|
|
for i, t in pairs(tags) do
|
2008-06-09 21:43:09 +02:00
|
|
|
t:view(false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-06-10 16:50:55 +02:00
|
|
|
local function tag_viewidx(r)
|
2008-06-18 17:56:24 +02:00
|
|
|
local tags = tag.geti(mouse.screen_get())
|
2008-06-09 21:43:09 +02:00
|
|
|
local sel = tag_selected()
|
|
|
|
tag_viewnone()
|
2008-06-18 17:56:24 +02:00
|
|
|
for i, t in ipairs(tags) do
|
2008-05-20 15:39:47 +02:00
|
|
|
if t == sel then
|
2008-06-09 21:43:09 +02:00
|
|
|
tags[array_boundandcycle(tags, i + r)]:view(true)
|
2008-05-20 15:39:47 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-06-09 21:43:09 +02:00
|
|
|
-- View next tag
|
2008-06-10 16:50:55 +02:00
|
|
|
local function tag_viewnext()
|
2008-06-09 21:43:09 +02:00
|
|
|
return tag_viewidx(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- View previous tag
|
2008-06-10 16:50:55 +02:00
|
|
|
local function tag_viewprev()
|
2008-06-09 21:43:09 +02:00
|
|
|
return tag_viewidx(-1)
|
|
|
|
end
|
|
|
|
|
2008-06-10 16:50:55 +02:00
|
|
|
local function tag_viewonly(t)
|
2008-06-09 21:43:09 +02:00
|
|
|
tag_viewnone()
|
|
|
|
t:view(true)
|
|
|
|
end
|
|
|
|
|
2008-06-10 16:50:55 +02:00
|
|
|
local function tag_viewmore(tags)
|
2008-06-09 21:43:09 +02:00
|
|
|
tag_viewnone()
|
2008-06-18 16:04:49 +02:00
|
|
|
for i, t in pairs(tags) do
|
2008-06-09 21:43:09 +02:00
|
|
|
t:view(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-06-10 16:50:55 +02:00
|
|
|
local function client_movetotag(target, c)
|
2008-06-09 21:43:09 +02:00
|
|
|
local sel = c or client.focus_get();
|
|
|
|
local tags = tag.get(mouse.screen_get())
|
2008-06-18 17:56:24 +02:00
|
|
|
for k, t in pairs(tags) do
|
2008-06-09 21:43:09 +02:00
|
|
|
sel:tag(t, false)
|
|
|
|
end
|
|
|
|
sel:tag(target, true)
|
2008-05-20 15:39:47 +02:00
|
|
|
end
|
|
|
|
|
2008-06-10 16:50:55 +02:00
|
|
|
local function client_toggletag(target, c)
|
2008-06-09 21:43:09 +02:00
|
|
|
local sel = c or client.focus_get();
|
2008-06-10 11:51:36 +02:00
|
|
|
local toggle = false
|
2008-06-18 16:04:49 +02:00
|
|
|
if sel then
|
|
|
|
-- Count how many tags has the client
|
|
|
|
-- an only toggle tag if the client has at least one tag other than target
|
|
|
|
for k, v in pairs(tag.get(sel:screen_get())) do
|
|
|
|
if target ~= v and sel:istagged(v) then
|
|
|
|
toggle = true
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if toggle then
|
|
|
|
sel:tag(target, not sel:istagged(target))
|
2008-06-10 11:51:36 +02:00
|
|
|
end
|
2008-06-09 21:43:09 +02:00
|
|
|
end
|
2008-05-20 15:39:47 +02:00
|
|
|
end
|
|
|
|
|
2008-06-10 16:50:55 +02:00
|
|
|
local function client_togglefloating(c)
|
2008-05-20 15:39:47 +02:00
|
|
|
local sel = c or client.focus_get();
|
|
|
|
if sel then
|
|
|
|
sel:floating_set(not sel:floating_get())
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-06-09 09:06:00 +02:00
|
|
|
-- Move a client to a screen. Default is next screen, cycling.
|
2008-06-10 16:50:55 +02:00
|
|
|
local function client_movetoscreen(c, s)
|
2008-06-09 09:06:00 +02:00
|
|
|
local sel = c or client.focus_get();
|
|
|
|
if sel then
|
|
|
|
local sc = screen.count()
|
|
|
|
if not s then
|
|
|
|
s = sel:screen_get() + 1
|
|
|
|
end
|
|
|
|
if s > sc then s = 1 elseif s < 1 then s = sc end
|
|
|
|
sel:screen_set(s)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-06-10 16:50:55 +02:00
|
|
|
local function layout_get(screen)
|
2008-06-09 21:43:09 +02:00
|
|
|
local t = tag_selected(screen)
|
|
|
|
if t then
|
|
|
|
return t:layout_get()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-06-11 10:35:54 +02:00
|
|
|
-- Just set an awful mark to a client to move it later.
|
|
|
|
local awfulmarked = {}
|
|
|
|
userhook_create('marked')
|
|
|
|
userhook_create('unmarked')
|
|
|
|
|
|
|
|
-- Mark a client
|
|
|
|
local function client_mark (c)
|
|
|
|
local cl = c or client.focus_get()
|
|
|
|
if cl then
|
|
|
|
for k, v in pairs(awfulmarked) do
|
|
|
|
if cl == v then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
table.insert(awfulmarked, cl)
|
2008-06-11 08:52:07 +02:00
|
|
|
|
2008-06-11 10:35:54 +02:00
|
|
|
-- Call callback
|
|
|
|
userhook_call('marked', {cl})
|
|
|
|
return true
|
|
|
|
end
|
2008-06-11 08:52:07 +02:00
|
|
|
end
|
|
|
|
|
2008-06-11 10:35:54 +02:00
|
|
|
-- Unmark a client
|
|
|
|
local function client_unmark(c)
|
|
|
|
local cl = c or client.focus_get()
|
|
|
|
|
|
|
|
for k, v in pairs(awfulmarked) do
|
|
|
|
if cl == v then
|
|
|
|
table.remove(awfulmarked, k)
|
|
|
|
userhook_call('unmarked', {cl})
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return false
|
2008-06-11 08:52:07 +02:00
|
|
|
end
|
|
|
|
|
2008-06-11 10:35:54 +02:00
|
|
|
-- Check if marked
|
|
|
|
local function client_ismarked(c)
|
2008-06-11 08:52:07 +02:00
|
|
|
local cl = c or client.focus_get()
|
|
|
|
if cl then
|
2008-06-11 10:35:54 +02:00
|
|
|
for k, v in pairs(awfulmarked) do
|
2008-06-11 08:52:07 +02:00
|
|
|
if cl == v then
|
2008-06-11 10:35:54 +02:00
|
|
|
return true
|
2008-06-11 08:52:07 +02:00
|
|
|
end
|
|
|
|
end
|
2008-06-11 10:35:54 +02:00
|
|
|
|
|
|
|
return false
|
2008-06-11 08:52:07 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-06-11 10:35:54 +02:00
|
|
|
-- Toggle marked
|
|
|
|
local function client_togglemarked(c)
|
|
|
|
local cl = c or client.focus_get()
|
|
|
|
|
|
|
|
if not client_mark(c) then
|
|
|
|
client_unmark(c)
|
2008-06-11 08:52:07 +02:00
|
|
|
end
|
2008-06-11 10:35:54 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Return the marked clients and empty the table
|
|
|
|
local function client_getmarked ()
|
|
|
|
for k, v in pairs(awfulmarked) do
|
|
|
|
userhook_call('unmarked', {v})
|
|
|
|
end
|
|
|
|
|
|
|
|
t = awfulmarked
|
|
|
|
awfulmarked = {}
|
2008-06-11 08:52:07 +02:00
|
|
|
return t
|
|
|
|
end
|
|
|
|
|
2008-06-09 21:43:09 +02:00
|
|
|
-- Function to change the layout of the current tag.
|
2008-05-24 13:55:48 +02:00
|
|
|
-- layouts = table of layouts (define in .awesomerc.lua)
|
|
|
|
-- i = relative index
|
2008-06-10 16:50:55 +02:00
|
|
|
local function layout_inc(layouts, i)
|
2008-06-09 21:43:09 +02:00
|
|
|
local t = tag_selected()
|
2008-05-24 13:55:48 +02:00
|
|
|
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
|
2008-06-09 21:43:09 +02:00
|
|
|
local cur_layout = layout_get()
|
2008-05-24 13:55:48 +02:00
|
|
|
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
|
|
|
|
|
2008-06-09 21:43:09 +02:00
|
|
|
-- function to set the layout of the current tag by name.
|
2008-06-10 16:50:55 +02:00
|
|
|
local function layout_set(layout)
|
2008-06-09 21:43:09 +02:00
|
|
|
local t = tag_selected()
|
2008-05-24 13:55:48 +02:00
|
|
|
if t then
|
|
|
|
t:layout_set(layout)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-06-11 07:47:57 +02:00
|
|
|
P.hooks['userhook_create'] = userhook_create
|
|
|
|
P.hooks['userhook_call'] = userhook_call
|
|
|
|
|
2008-05-31 15:47:00 +02:00
|
|
|
for name, hook in pairs(hooks) do
|
|
|
|
if name ~= 'timer' then
|
|
|
|
P.hooks[name] = function (f)
|
|
|
|
|
|
|
|
if P.myhooks[name] == nil then
|
|
|
|
P.myhooks[name] = {}
|
|
|
|
hooks[name](function (...)
|
2008-06-06 01:17:41 +02:00
|
|
|
|
2008-05-31 15:47:00 +02:00
|
|
|
for i,o in pairs(P.myhooks[name]) do
|
|
|
|
P.myhooks[name][i]['callback'](...)
|
|
|
|
end
|
2008-06-06 01:17:41 +02:00
|
|
|
|
2008-05-31 15:47:00 +02:00
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
|
|
|
table.insert(P.myhooks[name], {callback = f})
|
|
|
|
end
|
|
|
|
else
|
|
|
|
P.hooks[name] = function (time, f, runnow)
|
|
|
|
|
|
|
|
if P.myhooks[name] == nil then
|
|
|
|
P.myhooks[name] = {}
|
|
|
|
hooks[name](1, function (...)
|
2008-06-06 01:17:41 +02:00
|
|
|
|
2008-05-31 15:47:00 +02:00
|
|
|
for i,o in pairs(P.myhooks[name]) do
|
|
|
|
if P.myhooks[name][i]['counter'] >= P.myhooks[name][i]['timer'] then
|
|
|
|
P.myhooks[name][i]['counter'] = 1
|
|
|
|
P.myhooks[name][i]['callback'](...)
|
|
|
|
else
|
|
|
|
P.myhooks[name][i]['counter'] = P.myhooks[name][i]['counter']+1
|
|
|
|
end
|
|
|
|
end
|
2008-06-06 01:17:41 +02:00
|
|
|
|
2008-05-31 15:47:00 +02:00
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
|
|
|
if runnow then
|
|
|
|
table.insert(P.myhooks[name], {callback = f, timer = time, counter = time})
|
|
|
|
else
|
|
|
|
table.insert(P.myhooks[name], {callback = f, timer = time, counter = 0})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-06-10 16:50:55 +02:00
|
|
|
local function spawn(cmd)
|
2008-05-20 15:39:47 +02:00
|
|
|
return os.execute(cmd .. "&")
|
|
|
|
end
|
|
|
|
|
2008-06-19 09:14:54 +02:00
|
|
|
local function eval(s)
|
|
|
|
return assert(loadstring("return" ..s))()
|
2008-06-13 17:51:28 +02:00
|
|
|
end
|
|
|
|
|
2008-06-18 20:07:18 +02:00
|
|
|
local function menu_completion_bash(command, cur_pos, ncomp)
|
|
|
|
local wstart = 1
|
|
|
|
local wend = 1
|
|
|
|
local words = {}
|
|
|
|
local cword_index = 0
|
|
|
|
local cword_start = 0
|
|
|
|
local cword_end = 0
|
|
|
|
local i = 1
|
|
|
|
local comptype = "file"
|
|
|
|
|
|
|
|
-- do nothing if we are on a letter, i.e. not at len + 1 or on a space
|
|
|
|
if cur_pos ~= #command + 1 and command:sub(cur_pos, cur_pos) ~= " " then
|
|
|
|
return command, cur_pos
|
2008-06-19 08:27:50 +02:00
|
|
|
elseif #command == 0 then
|
|
|
|
return command, cur_pos
|
2008-06-18 20:07:18 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
while wend <= #command do
|
|
|
|
wend = command:find(" ", wstart)
|
|
|
|
if not wend then wend = #command + 1 end
|
|
|
|
table.insert(words, command:sub(wstart, wend - 1))
|
|
|
|
if cur_pos >= wstart and cur_pos <= wend + 1 then
|
|
|
|
cword_start = wstart
|
|
|
|
cword_end = wend
|
|
|
|
cword_index = i
|
|
|
|
end
|
|
|
|
wstart = wend + 1
|
|
|
|
i = i + 1
|
|
|
|
end
|
|
|
|
|
|
|
|
if cword_index == 1 then
|
|
|
|
comptype = "command"
|
|
|
|
end
|
|
|
|
|
2008-06-18 20:13:35 +02:00
|
|
|
local c = io.popen("/usr/bin/env bash -c 'compgen -A " .. comptype .. " " .. words[cword_index] .. "'")
|
2008-06-18 20:07:18 +02:00
|
|
|
local output = {}
|
|
|
|
i = 0
|
|
|
|
while true do
|
|
|
|
local line = c:read("*line")
|
|
|
|
if not line then break end
|
|
|
|
table.insert(output, line)
|
|
|
|
end
|
|
|
|
|
|
|
|
c:close()
|
|
|
|
|
2008-06-18 20:32:54 +02:00
|
|
|
-- no completion, return
|
|
|
|
if #output == 0 then
|
|
|
|
return command, cur_pos
|
|
|
|
end
|
|
|
|
|
2008-06-18 20:07:18 +02:00
|
|
|
-- cycle
|
|
|
|
while ncomp > #output do
|
|
|
|
ncomp = ncomp - #output
|
|
|
|
end
|
|
|
|
|
|
|
|
local str = command:sub(1, cword_start - 1) .. output[ncomp] .. command:sub(cword_end)
|
|
|
|
cur_pos = cword_end + #output[ncomp] + 1
|
|
|
|
|
|
|
|
return str, cur_pos
|
|
|
|
end
|
|
|
|
|
2008-06-17 16:11:35 +02:00
|
|
|
-- Menu functions
|
|
|
|
local function menu_text_with_cursor(text, text_color, cursor_color, cursor_pos)
|
|
|
|
local char
|
|
|
|
if not text then text = "" end
|
|
|
|
if #text < cursor_pos then
|
|
|
|
char = " "
|
|
|
|
else
|
|
|
|
char = escape(text:sub(cursor_pos, cursor_pos))
|
|
|
|
end
|
|
|
|
local text_start = escape(text:sub(1, cursor_pos - 1))
|
|
|
|
local text_end = escape(text:sub(cursor_pos + 1))
|
|
|
|
return text_start .. "<span background=\"" .. cursor_color .. "\" foreground=\"" .. text_color .. "\">" .. char .. "</span>" .. text_end
|
|
|
|
end
|
|
|
|
|
2008-06-18 13:00:11 +02:00
|
|
|
local function menu(args, textbox, exe_callback, completion_callback)
|
2008-06-17 16:37:27 +02:00
|
|
|
if not args then return end
|
2008-06-10 16:50:55 +02:00
|
|
|
local command = ""
|
2008-06-18 20:07:18 +02:00
|
|
|
local command_before_comp
|
|
|
|
local cur_pos_before_comp
|
2008-06-17 16:37:27 +02:00
|
|
|
local prompt = args.prompt or ""
|
|
|
|
local inv_col = args.cursor_fg or "black"
|
|
|
|
local cur_col = args.cursor_bg or "white"
|
2008-06-18 13:00:11 +02:00
|
|
|
-- The cursor position
|
2008-06-17 16:11:35 +02:00
|
|
|
local cur_pos = 1
|
2008-06-18 13:00:11 +02:00
|
|
|
-- The completion element to use on completion request.
|
|
|
|
local ncomp = 1
|
2008-06-13 17:51:28 +02:00
|
|
|
if not textbox or not exe_callback then
|
|
|
|
return
|
|
|
|
end
|
2008-06-17 16:11:35 +02:00
|
|
|
textbox:set("text", prompt .. menu_text_with_cursor(text, inv_col, cur_col, cur_pos))
|
2008-06-10 16:50:55 +02:00
|
|
|
keygrabber.run(
|
|
|
|
function (mod, key)
|
2008-06-17 16:54:46 +02:00
|
|
|
local has_ctrl = false
|
|
|
|
|
|
|
|
-- Compute modifiers keys
|
|
|
|
for k, v in ipairs(mod) do
|
|
|
|
if v == "Control" then has_ctrl = true end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Get out cases
|
2008-06-18 12:46:12 +02:00
|
|
|
if has_ctrl then
|
|
|
|
if key == "g" then
|
|
|
|
textbox:set("text", "")
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
else
|
|
|
|
if key == "Return" then
|
|
|
|
textbox:set("text", "")
|
|
|
|
exe_callback(command)
|
|
|
|
return false
|
|
|
|
elseif key == "Escape" then
|
|
|
|
textbox:set("text", "")
|
|
|
|
return false
|
|
|
|
end
|
2008-06-17 16:11:35 +02:00
|
|
|
end
|
2008-06-17 16:54:46 +02:00
|
|
|
|
|
|
|
-- Control cases
|
|
|
|
if has_ctrl then
|
|
|
|
if key == "a" then
|
|
|
|
cur_pos = 1
|
|
|
|
elseif key == "e" then
|
|
|
|
cur_pos = #command + 1
|
2008-06-18 20:30:53 +02:00
|
|
|
elseif key == "w" then
|
|
|
|
local wstart = 1
|
|
|
|
local wend = 1
|
|
|
|
local cword_start = 1
|
|
|
|
local cword_end = 1
|
|
|
|
while wend < cur_pos do
|
|
|
|
wend = command:find(" ", wstart)
|
|
|
|
if not wend then wend = #command + 1 end
|
|
|
|
if cur_pos >= wstart and cur_pos <= wend + 1 then
|
|
|
|
cword_start = wstart
|
|
|
|
cword_end = cur_pos - 1
|
|
|
|
break
|
|
|
|
end
|
|
|
|
wstart = wend + 1
|
|
|
|
end
|
|
|
|
command = command:sub(1, cword_start - 1) .. command:sub(cword_end + 1)
|
|
|
|
cur_pos = cword_start
|
2008-06-17 16:54:46 +02:00
|
|
|
end
|
|
|
|
else
|
2008-06-18 13:00:11 +02:00
|
|
|
if completion_callback then
|
|
|
|
-- That's tab
|
|
|
|
if key:byte() == 9 then
|
2008-06-18 20:07:18 +02:00
|
|
|
if ncomp == 1 then
|
|
|
|
command_before_comp = command
|
|
|
|
cur_pos_before_comp = cur_pos
|
|
|
|
end
|
|
|
|
command, cur_pos = completion_callback(command_before_comp, cur_pos_before_comp, ncomp)
|
2008-06-18 13:00:11 +02:00
|
|
|
ncomp = ncomp + 1
|
2008-06-18 20:07:18 +02:00
|
|
|
key = ""
|
2008-06-18 13:00:11 +02:00
|
|
|
else
|
|
|
|
ncomp = 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-06-17 16:54:46 +02:00
|
|
|
-- Typin cases
|
2008-06-18 20:39:35 +02:00
|
|
|
if key == "Home" then
|
|
|
|
cur_pos = 1
|
|
|
|
elseif key == "End" then
|
|
|
|
cur_pos = #command + 1
|
|
|
|
elseif key == "BackSpace" then
|
2008-06-18 08:41:45 +02:00
|
|
|
if cur_pos > 1 then
|
|
|
|
command = command:sub(1, cur_pos - 2) .. command:sub(cur_pos)
|
|
|
|
cur_pos = cur_pos - 1
|
|
|
|
end
|
2008-06-17 16:54:46 +02:00
|
|
|
-- That's DEL
|
|
|
|
elseif key:byte() == 127 then
|
|
|
|
command = command:sub(1, cur_pos - 1) .. command:sub(cur_pos + 1)
|
|
|
|
elseif key == "Left" then
|
|
|
|
cur_pos = cur_pos - 1
|
|
|
|
elseif key == "Right" then
|
|
|
|
cur_pos = cur_pos + 1
|
|
|
|
else
|
|
|
|
command = command:sub(1, cur_pos - 1) .. key .. command:sub(cur_pos)
|
|
|
|
cur_pos = cur_pos + 1
|
|
|
|
end
|
|
|
|
if cur_pos < 1 then
|
|
|
|
cur_pos = 1
|
|
|
|
elseif cur_pos > #command + 1 then
|
|
|
|
cur_pos = #command + 1
|
|
|
|
end
|
2008-06-10 16:50:55 +02:00
|
|
|
end
|
2008-06-17 16:54:46 +02:00
|
|
|
|
|
|
|
-- Update textbox
|
2008-06-17 16:11:35 +02:00
|
|
|
textbox:set("text", prompt .. menu_text_with_cursor(command, inv_col, cur_col, cur_pos))
|
2008-06-17 16:54:46 +02:00
|
|
|
|
2008-06-10 16:50:55 +02:00
|
|
|
return true
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2008-06-10 18:25:03 +02:00
|
|
|
local function escape(text)
|
|
|
|
text = text:gsub("&", "&")
|
|
|
|
text = text:gsub("<", "<")
|
|
|
|
text = text:gsub(">", ">")
|
2008-06-11 15:49:57 +02:00
|
|
|
text = text:gsub("'", "'")
|
2008-06-10 18:25:03 +02:00
|
|
|
text = text:gsub("\"", """)
|
|
|
|
return text
|
|
|
|
end
|
|
|
|
|
2008-06-11 15:49:57 +02:00
|
|
|
local function unescape(text)
|
|
|
|
text = text:gsub("&", "&")
|
|
|
|
text = text:gsub("<", "<")
|
|
|
|
text = text:gsub(">", ">")
|
|
|
|
text = text:gsub("'", "'")
|
|
|
|
text = text:gsub(""", "\"")
|
|
|
|
return text
|
|
|
|
end
|
|
|
|
|
2008-06-09 21:43:09 +02:00
|
|
|
-- Export tags function
|
|
|
|
P.tag =
|
2008-05-20 15:39:47 +02:00
|
|
|
{
|
2008-06-09 21:43:09 +02:00
|
|
|
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;
|
2008-05-20 15:39:47 +02:00
|
|
|
}
|
|
|
|
P.client =
|
|
|
|
{
|
|
|
|
next = client_next;
|
|
|
|
focus = client_focus;
|
|
|
|
swap = client_swap;
|
2008-06-18 12:47:39 +02:00
|
|
|
master = client_master;
|
2008-06-09 21:43:09 +02:00
|
|
|
movetotag = client_movetotag;
|
|
|
|
toggletag = client_toggletag;
|
2008-05-20 15:39:47 +02:00
|
|
|
togglefloating = client_togglefloating;
|
2008-06-06 01:17:41 +02:00
|
|
|
moveresize = client_moveresize;
|
2008-06-09 09:06:00 +02:00
|
|
|
movetoscreen = client_movetoscreen;
|
2008-06-11 10:35:54 +02:00
|
|
|
mark = client_mark;
|
|
|
|
unmark = client_unmark;
|
|
|
|
ismarked = client_ismarked;
|
|
|
|
togglemarked = client_togglemarked;
|
|
|
|
getmarked = client_getmarked;
|
2008-05-20 15:39:47 +02:00
|
|
|
}
|
|
|
|
P.screen =
|
|
|
|
{
|
|
|
|
focus = screen_focus;
|
|
|
|
}
|
2008-05-23 17:09:49 +02:00
|
|
|
P.layout =
|
|
|
|
{
|
2008-06-09 21:43:09 +02:00
|
|
|
get = layout_get;
|
2008-05-24 13:55:48 +02:00
|
|
|
set = layout_set;
|
|
|
|
inc = layout_inc;
|
2008-05-23 17:09:49 +02:00
|
|
|
}
|
2008-06-18 20:07:18 +02:00
|
|
|
P.completion =
|
|
|
|
{
|
|
|
|
bash = menu_completion_bash;
|
|
|
|
}
|
2008-05-20 15:39:47 +02:00
|
|
|
P.spawn = spawn
|
2008-06-10 16:50:55 +02:00
|
|
|
P.menu = menu
|
2008-06-10 18:25:03 +02:00
|
|
|
P.escape = escape
|
2008-06-13 17:51:28 +02:00
|
|
|
P.eval = eval
|
2008-05-20 15:39:47 +02:00
|
|
|
|
|
|
|
return P
|