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-25 11:45:57 +02:00
|
|
|
local string = string
|
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
|
|
|
P.hooks = {}
|
|
|
|
P.myhooks = {}
|
2008-06-25 09:07:57 +02:00
|
|
|
P.prompt = {}
|
|
|
|
P.completion = {}
|
2008-06-19 14:17:00 +02:00
|
|
|
P.screen = {}
|
|
|
|
P.layout = {}
|
|
|
|
P.client = {}
|
|
|
|
P.tag = {}
|
2008-06-11 10:35:54 +02:00
|
|
|
|
2008-06-20 09:15:20 +02:00
|
|
|
--- Create a new userhook (for external libs).
|
|
|
|
-- @param name Hook name.
|
2008-06-11 10:35:54 +02:00
|
|
|
local function userhook_create(name)
|
|
|
|
P.myhooks[name] = {}
|
|
|
|
P.hooks[name] = function (f)
|
2008-06-20 09:15:20 +02:00
|
|
|
table.insert(P.myhooks[name], { callback = f })
|
2008-06-11 10:35:54 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-06-20 09:15:20 +02:00
|
|
|
--- Call a created userhook (for external libs).
|
|
|
|
-- @param name Hook name.
|
2008-06-11 10:35:54 +02:00
|
|
|
local function userhook_call(name, args)
|
2008-06-20 09:15:20 +02:00
|
|
|
for i, o in pairs(P.myhooks[name]) do
|
|
|
|
P.myhooks[name][i]['callback'](unpack(args))
|
2008-06-11 10:35:54 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-06-20 09:15:20 +02:00
|
|
|
--- Make i cycle.
|
|
|
|
-- @param t A length.
|
|
|
|
-- @param i An absolute index to fit into #t.
|
|
|
|
-- @return The object at new index.
|
|
|
|
local function cycle(t, i)
|
|
|
|
while i > t do i = i - t end
|
|
|
|
while i < 1 do i = i + t end
|
2008-05-20 15:39:47 +02:00
|
|
|
return i
|
|
|
|
end
|
|
|
|
|
2008-06-20 09:15:20 +02:00
|
|
|
--- Get a client by its relative index to the focused window.
|
|
|
|
-- @usage Set i to 1 to get next, -1 to get previous.
|
|
|
|
-- @param i The index.
|
|
|
|
-- @param c Optional client.
|
|
|
|
-- @return A client, or nil if no client is available.
|
|
|
|
function P.client.next(i, c)
|
2008-05-20 15:39:47 +02:00
|
|
|
-- Get currently focused client
|
2008-06-20 09:15:20 +02:00
|
|
|
local sel = c or client.focus_get()
|
|
|
|
if sel then
|
|
|
|
-- Get all visible clients
|
|
|
|
local cls = client.visible_get(sel:screen_get())
|
|
|
|
-- Loop upon each client
|
|
|
|
for idx, c in ipairs(cls) do
|
|
|
|
if c == sel then
|
|
|
|
-- Cycle
|
|
|
|
return cls[cycle(#cls, idx +i)]
|
|
|
|
end
|
2008-05-20 15:39:47 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-06-20 09:15:20 +02:00
|
|
|
--- Focus a client by its relative index.
|
|
|
|
-- @param i The index.
|
|
|
|
-- @param c Optional client.
|
|
|
|
function P.client.focus(i, c)
|
|
|
|
local target = P.client.next(i, c)
|
|
|
|
if target then
|
|
|
|
target:focus_set()
|
2008-05-20 15:39:47 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-06-20 09:15:20 +02:00
|
|
|
--- Swap a client by its relative index.
|
|
|
|
-- @param i The index.
|
|
|
|
-- @param c Optional client, otherwise focused one is used.
|
|
|
|
function P.client.swap(i, c)
|
|
|
|
local sel = c or client.focus_get()
|
|
|
|
local target = P.client.next(i, sel)
|
|
|
|
if target then
|
2008-06-20 11:06:59 +02:00
|
|
|
target:swap(sel)
|
2008-05-20 15:39:47 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-06-20 09:15:20 +02:00
|
|
|
--- Get the master window
|
|
|
|
-- @param screen Optional screen number, otherwise screen mouse is used.
|
|
|
|
-- @return The master window.
|
|
|
|
function P.client.master(screen)
|
|
|
|
local s = screen or mouse.screen_get()
|
2008-06-21 10:16:54 +02:00
|
|
|
return client.visible_get(s)[1]
|
2008-06-18 12:47:39 +02:00
|
|
|
end
|
|
|
|
|
2008-06-20 09:15:20 +02:00
|
|
|
--- Move/resize a client relative to current coordinates.
|
|
|
|
-- @param x The relative x coordinate.
|
|
|
|
-- @param y The relative y coordinate.
|
|
|
|
-- @param w The relative width.
|
|
|
|
-- @param h The relative height.
|
|
|
|
-- @param c The optional client, otherwise focused one is used.
|
|
|
|
function P.client.moveresize(x, y, w, h, c)
|
|
|
|
local sel = c or client.focus_get()
|
2008-06-06 01:17:41 +02:00
|
|
|
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-20 09:15:20 +02:00
|
|
|
--- Give the focus to a screen, and move pointer.
|
|
|
|
-- @param Screen number.
|
2008-06-19 14:17:00 +02:00
|
|
|
function P.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-06-20 11:13:02 +02:00
|
|
|
s = cycle(screen.count(), s + i)
|
|
|
|
screen.focus(s)
|
2008-05-21 16:59:39 +02:00
|
|
|
-- Move the mouse on the screen
|
2008-07-01 09:27:34 +02:00
|
|
|
mouse.coords_set(screen.coords_get(s))
|
2008-05-20 15:39:47 +02:00
|
|
|
end
|
|
|
|
|
2008-06-20 09:15:20 +02:00
|
|
|
--- Return a table with all visible tags
|
|
|
|
-- @param s Screen number.
|
|
|
|
-- @return A table with all selected tags.
|
2008-06-19 14:17:00 +02:00
|
|
|
function P.tag.selectedlist(s)
|
2008-06-09 21:43:09 +02:00
|
|
|
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-07-01 11:38:40 +02:00
|
|
|
if t.selected then
|
2008-06-20 09:15:20 +02:00
|
|
|
vtags[#vtags + 1] = t
|
2008-06-09 21:43:09 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
return vtags
|
|
|
|
end
|
|
|
|
|
2008-06-20 09:15:20 +02:00
|
|
|
--- Return only the first visible tag.
|
|
|
|
-- @param s Screen number.
|
2008-06-19 14:17:00 +02:00
|
|
|
function P.tag.selected(s)
|
|
|
|
return P.tag.selectedlist(s)[1]
|
2008-06-09 21:43:09 +02:00
|
|
|
end
|
|
|
|
|
2008-06-20 09:15:20 +02:00
|
|
|
--- Set master width factor.
|
|
|
|
-- @param mwfact Master width factor.
|
|
|
|
function P.tag.setmwfact(mwfact)
|
2008-06-19 14:17:00 +02:00
|
|
|
local t = P.tag.selected()
|
2008-05-23 11:51:53 +02:00
|
|
|
if t then
|
2008-07-01 13:59:40 +02:00
|
|
|
t.mwfact = mwfact
|
2008-05-23 11:51:53 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-06-20 09:15:20 +02:00
|
|
|
--- Increase master width factor.
|
|
|
|
-- @param add Value to add to master width factor.
|
|
|
|
function P.tag.incmwfact(add)
|
2008-06-19 14:17:00 +02:00
|
|
|
local t = P.tag.selected()
|
2008-05-20 15:39:47 +02:00
|
|
|
if t then
|
2008-07-01 13:59:40 +02:00
|
|
|
t.mwfact = t.mwfact + add
|
2008-05-20 15:39:47 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-06-20 09:15:20 +02:00
|
|
|
--- Set the number of master windows.
|
|
|
|
-- @param nmaster The number of master windows.
|
|
|
|
function P.tag.setnmaster(nmaster)
|
2008-06-19 14:17:00 +02:00
|
|
|
local t = P.tag.selected()
|
2008-05-23 11:51:53 +02:00
|
|
|
if t then
|
2008-07-01 14:17:07 +02:00
|
|
|
t.nmaster = nmaster
|
2008-05-23 11:51:53 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-06-20 09:15:20 +02:00
|
|
|
--- Increase the number of master windows.
|
|
|
|
-- @param add Value to add to number of master windows.
|
|
|
|
function P.tag.incnmaster(add)
|
2008-06-19 14:17:00 +02:00
|
|
|
local t = P.tag.selected()
|
2008-05-20 15:39:47 +02:00
|
|
|
if t then
|
2008-07-01 14:17:07 +02:00
|
|
|
t.nmaster = t.nmaster + add
|
2008-05-20 15:39:47 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-06-20 09:15:20 +02:00
|
|
|
--- Set number of column windows.
|
|
|
|
-- @param ncol The number of column.
|
|
|
|
function P.tag.setncol(ncol)
|
2008-06-19 14:17:00 +02:00
|
|
|
local t = P.tag.selected()
|
2008-05-23 11:51:53 +02:00
|
|
|
if t then
|
2008-06-20 09:15:20 +02:00
|
|
|
t:ncol_set(ncol)
|
2008-05-23 11:51:53 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-06-20 09:15:20 +02:00
|
|
|
--- Increase number of column windows.
|
|
|
|
-- @param add Value to add to number of column windows.
|
|
|
|
function P.tag.incncol(add)
|
2008-06-19 14:17:00 +02:00
|
|
|
local t = P.tag.selected()
|
2008-05-20 15:39:47 +02:00
|
|
|
if t then
|
2008-06-20 09:15:20 +02:00
|
|
|
t:ncol_set(t:ncol_get() + add)
|
2008-05-20 15:39:47 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-06-20 09:15:20 +02:00
|
|
|
--- View no tag.
|
|
|
|
-- @param Optional screen number.
|
|
|
|
function P.tag.viewnone(screen)
|
|
|
|
local tags = tag.get(screen or mouse.screen_get())
|
2008-06-18 16:04:49 +02:00
|
|
|
for i, t in pairs(tags) do
|
2008-07-01 11:38:40 +02:00
|
|
|
t.selected = false
|
2008-06-09 21:43:09 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-06-20 09:15:20 +02:00
|
|
|
--- View a tag by its index.
|
|
|
|
-- @param i The relative index to see.
|
|
|
|
-- @param screen Optional screen number.
|
|
|
|
function P.tag.viewidx(i, screen)
|
|
|
|
local tags = tag.geti(screen or mouse.screen_get())
|
2008-06-19 14:17:00 +02:00
|
|
|
local sel = P.tag.selected()
|
|
|
|
P.tag.viewnone()
|
2008-06-20 09:15:20 +02:00
|
|
|
for k, t in ipairs(tags) do
|
2008-05-20 15:39:47 +02:00
|
|
|
if t == sel then
|
2008-07-01 11:38:40 +02:00
|
|
|
tags[cycle(#tags, k + i)].selected = true
|
2008-05-20 15:39:47 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-06-20 09:15:20 +02:00
|
|
|
--- View next tag. This is the same as tag.viewidx(1).
|
2008-06-19 14:17:00 +02:00
|
|
|
function P.tag.viewnext()
|
|
|
|
return P.tag.viewidx(1)
|
2008-06-09 21:43:09 +02:00
|
|
|
end
|
|
|
|
|
2008-06-20 09:15:20 +02:00
|
|
|
--- View previous tag. This is the same a tag.viewidx(-1).
|
2008-06-19 14:17:00 +02:00
|
|
|
function P.tag.viewprev()
|
|
|
|
return P.tag.viewidx(-1)
|
2008-06-09 21:43:09 +02:00
|
|
|
end
|
|
|
|
|
2008-06-20 09:15:20 +02:00
|
|
|
--- View only a tag.
|
|
|
|
-- @param t The tag object.
|
2008-06-19 14:17:00 +02:00
|
|
|
function P.tag.viewonly(t)
|
|
|
|
P.tag.viewnone()
|
2008-07-01 11:38:40 +02:00
|
|
|
t.selected = true
|
2008-06-09 21:43:09 +02:00
|
|
|
end
|
|
|
|
|
2008-06-20 09:15:20 +02:00
|
|
|
--- View only a set of tags.
|
|
|
|
-- @param tags A table with tags to view only.
|
|
|
|
-- @param screen Optional screen number of the tags.
|
|
|
|
function P.tag.viewmore(tags, screen)
|
|
|
|
P.tag.viewnone(screen)
|
2008-06-18 16:04:49 +02:00
|
|
|
for i, t in pairs(tags) do
|
2008-07-01 11:38:40 +02:00
|
|
|
t.selected = true
|
2008-06-09 21:43:09 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-06-20 09:15:20 +02:00
|
|
|
--- Move a client to a tag.
|
|
|
|
-- @param target The tag to move the client to.
|
|
|
|
-- @para c Optional client to move, otherwise the focused one is used.
|
2008-06-19 14:17:00 +02:00
|
|
|
function P.client.movetotag(target, c)
|
2008-06-09 21:43:09 +02:00
|
|
|
local sel = c or client.focus_get();
|
2008-06-20 10:52:47 +02:00
|
|
|
local tags = tag.get(sel: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-25 09:03:29 +02:00
|
|
|
--- Toggle a tag on a client.
|
|
|
|
-- @param target The tag to toggle.
|
|
|
|
-- @param c Optional client to toggle, otherwise the focused one is used.
|
2008-06-19 14:17:00 +02:00
|
|
|
function P.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-25 09:03:29 +02:00
|
|
|
--- Toggle the floating status of a client.
|
|
|
|
-- @param c Optional client, the focused on if not set.
|
2008-06-19 14:17:00 +02:00
|
|
|
function P.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-25 09:03:29 +02:00
|
|
|
--- Move a client to a screen. Default is next screen, cycling.
|
|
|
|
-- @param c The client to move.
|
|
|
|
-- @param s The screen number, default to current + 1.
|
2008-06-19 14:17:00 +02:00
|
|
|
function P.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)
|
2008-07-01 09:27:34 +02:00
|
|
|
mouse.coords_set(screen.coords_get(s))
|
2008-07-01 09:34:39 +02:00
|
|
|
sel:focus_set()
|
2008-06-09 09:06:00 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-06-25 09:03:29 +02:00
|
|
|
--- Get the current layout name.
|
|
|
|
-- @param screen The screen number.
|
2008-06-19 14:17:00 +02:00
|
|
|
function P.layout.get(screen)
|
|
|
|
local t = P.tag.selected(screen)
|
2008-06-09 21:43:09 +02:00
|
|
|
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')
|
|
|
|
|
2008-06-25 09:03:29 +02:00
|
|
|
--- Mark a client, and then call 'marked' hook.
|
|
|
|
-- @param c The client to mark, the focused one if not specified.
|
|
|
|
-- @return True if the client has been marked. False if the client was already marked.
|
2008-06-19 14:17:00 +02:00
|
|
|
function P.client.mark (c)
|
2008-06-11 10:35:54 +02:00
|
|
|
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-25 09:03:29 +02:00
|
|
|
--- Unmark a client and then call 'unmarked' hook.
|
|
|
|
-- @param c The client to unmark, or the focused one if not specified.
|
|
|
|
-- @return True if the client has been unmarked. False if the client was not marked.
|
2008-06-19 14:17:00 +02:00
|
|
|
function P.client.unmark(c)
|
2008-06-11 10:35:54 +02:00
|
|
|
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-25 09:03:29 +02:00
|
|
|
--- Check if a client is marked.
|
|
|
|
-- @param c The client to check, or the focused one otherwise.
|
2008-06-19 14:17:00 +02:00
|
|
|
function P.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
|
|
|
|
end
|
2008-06-25 09:03:29 +02:00
|
|
|
return false
|
2008-06-11 08:52:07 +02:00
|
|
|
end
|
|
|
|
|
2008-06-25 09:03:29 +02:00
|
|
|
--- Toggle a client as marked.
|
|
|
|
-- @param c The client to toggle mark.
|
2008-06-19 14:17:00 +02:00
|
|
|
function P.client.togglemarked(c)
|
2008-06-11 10:35:54 +02:00
|
|
|
local cl = c or client.focus_get()
|
|
|
|
|
2008-06-19 14:17:00 +02:00
|
|
|
if not P.client.mark(c) then
|
|
|
|
P.client.unmark(c)
|
2008-06-11 08:52:07 +02:00
|
|
|
end
|
2008-06-11 10:35:54 +02:00
|
|
|
end
|
|
|
|
|
2008-06-25 09:03:29 +02:00
|
|
|
--- Return the marked clients and empty the marked table.
|
|
|
|
-- @return A table with all marked clients.
|
2008-06-19 14:17:00 +02:00
|
|
|
function P.client.getmarked()
|
2008-06-11 10:35:54 +02:00
|
|
|
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-25 09:03:29 +02:00
|
|
|
--- Change the layout of the current tag.
|
|
|
|
-- @param layouts A table of layouts.
|
|
|
|
-- @param i Relative index.
|
2008-06-19 14:17:00 +02:00
|
|
|
function P.layout.inc(layouts, i)
|
|
|
|
local t = P.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-19 14:17:00 +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-25 09:03:29 +02:00
|
|
|
--- Set the layout of the current tag by name.
|
|
|
|
-- @param layout Layout name.
|
2008-06-19 14:17:00 +02:00
|
|
|
function P.layout.set(layout)
|
|
|
|
local t = P.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-06-25 09:03:29 +02:00
|
|
|
for i, o in pairs(P.myhooks[name]) do
|
2008-05-31 15:47:00 +02:00
|
|
|
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-25 09:03:29 +02:00
|
|
|
--- Spawn a program.
|
|
|
|
-- @param cmd The command.
|
|
|
|
-- @return The os.execute() return value.
|
2008-06-19 14:17:00 +02:00
|
|
|
function P.spawn(cmd)
|
2008-05-20 15:39:47 +02:00
|
|
|
return os.execute(cmd .. "&")
|
|
|
|
end
|
|
|
|
|
2008-06-25 09:03:29 +02:00
|
|
|
--- Eval Lua code.
|
|
|
|
-- @return The return value of Lua code.
|
2008-06-19 14:17:00 +02:00
|
|
|
function P.eval(s)
|
2008-06-19 10:13:19 +02:00
|
|
|
return assert(loadstring("return " .. s))()
|
2008-06-13 17:51:28 +02:00
|
|
|
end
|
|
|
|
|
2008-06-25 09:03:29 +02:00
|
|
|
--- Use bash completion system to complete command and filename.
|
|
|
|
-- @param command The command line.
|
|
|
|
-- @param cur_pos The cursor position.
|
|
|
|
-- @paran ncomp The element number to complete.
|
|
|
|
-- @return The new commande and the new cursor position.
|
2008-06-19 18:45:46 +02:00
|
|
|
function P.completion.bash(command, cur_pos, ncomp)
|
2008-06-18 20:07:18 +02:00
|
|
|
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-25 09:07:57 +02:00
|
|
|
--- Draw the prompt text with a cursor.
|
2008-06-25 09:03:29 +02:00
|
|
|
-- @param text The text.
|
|
|
|
-- @param text_color The text color.
|
|
|
|
-- @param cursor_color The cursor color.
|
|
|
|
-- @param cursor_pos The cursor position.
|
2008-06-25 09:07:57 +02:00
|
|
|
local function prompt_text_with_cursor(text, text_color, cursor_color, cursor_pos)
|
2008-06-17 16:11:35 +02:00
|
|
|
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-25 09:03:29 +02:00
|
|
|
--- Run a prompt in a box.
|
|
|
|
-- @param args A table with optional arguments: cursor_fg, cursor_bg, prompt.
|
|
|
|
-- @param textbox The textbox to use for the prompt.
|
|
|
|
-- @param exe_callback The callback function to call with command as argument when finished.
|
|
|
|
-- @param completion_callback The callback function to call to get completion.
|
2008-06-25 09:07:57 +02:00
|
|
|
function P.prompt(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-27 20:24:30 +02:00
|
|
|
textbox.text = prompt .. prompt_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
|
2008-06-27 20:24:30 +02:00
|
|
|
textbox.text = ""
|
2008-06-18 12:46:12 +02:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
else
|
|
|
|
if key == "Return" then
|
2008-06-27 20:24:30 +02:00
|
|
|
textbox.text = ""
|
2008-06-18 12:46:12 +02:00
|
|
|
exe_callback(command)
|
|
|
|
return false
|
|
|
|
elseif key == "Escape" then
|
2008-06-27 20:24:30 +02:00
|
|
|
textbox.text = ""
|
2008-06-18 12:46:12 +02:00
|
|
|
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
|
2008-06-25 11:45:57 +02:00
|
|
|
if string.len(key) == 1 then
|
|
|
|
command = command:sub(1, cur_pos - 1) .. key .. command:sub(cur_pos)
|
|
|
|
cur_pos = cur_pos + 1
|
|
|
|
end
|
2008-06-17 16:54:46 +02:00
|
|
|
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-27 20:24:30 +02:00
|
|
|
textbox.text = prompt .. prompt_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-25 09:03:29 +02:00
|
|
|
--- Escape a string from XML char.
|
|
|
|
-- Useful to set raw text in textbox.
|
|
|
|
-- @param text Text to escape.
|
|
|
|
-- @return Escape text.
|
2008-06-19 14:17:00 +02:00
|
|
|
function P.escape(text)
|
2008-06-10 18:25:03 +02:00
|
|
|
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-25 09:03:29 +02:00
|
|
|
--- Unescape a string from entities.
|
|
|
|
-- @param text Text to unescape.
|
|
|
|
-- @return Unescaped text.
|
2008-06-19 14:17:00 +02:00
|
|
|
function P.unescape(text)
|
2008-06-11 15:49:57 +02:00
|
|
|
text = text:gsub("&", "&")
|
|
|
|
text = text:gsub("<", "<")
|
|
|
|
text = text:gsub(">", ">")
|
|
|
|
text = text:gsub("'", "'")
|
|
|
|
text = text:gsub(""", "\"")
|
|
|
|
return text
|
|
|
|
end
|
|
|
|
|
2008-05-20 15:39:47 +02:00
|
|
|
return P
|