2008-08-04 14:31:53 +02:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
-- awful: AWesome Functions very UsefuL
|
|
|
|
--
|
|
|
|
-- @author Julien Danjou <julien@danjou.info>
|
|
|
|
-- @copyright 2008 Julien Danjou
|
|
|
|
---------------------------------------------------------------------------
|
2008-05-20 15:39:47 +02:00
|
|
|
|
|
|
|
-- 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 os = os
|
2008-06-18 20:07:18 +02:00
|
|
|
local io = io
|
2008-08-01 00:10:04 +02:00
|
|
|
local setmetatable = setmetatable
|
2008-08-04 14:31:53 +02:00
|
|
|
local table = table
|
|
|
|
local capi =
|
|
|
|
{
|
|
|
|
screen = screen,
|
|
|
|
client = client,
|
|
|
|
tag = tag,
|
|
|
|
mouse = mouse,
|
|
|
|
titlebar = titlebar,
|
|
|
|
widget = widget,
|
|
|
|
hooks = hooks,
|
|
|
|
keygrabber = keygrabber
|
|
|
|
}
|
|
|
|
|
|
|
|
module("awful")
|
2008-05-20 15:39:47 +02:00
|
|
|
|
2008-08-01 00:10:04 +02:00
|
|
|
-- The ObjectTable class
|
|
|
|
ObjectTable = {}
|
|
|
|
ObjectTable.__index = ObjectTable
|
|
|
|
--- Lookup in a table indexed by objects
|
|
|
|
function ObjectTable.__index(self, key)
|
|
|
|
for k, v in pairs(self) do
|
|
|
|
if k == key then
|
|
|
|
return v
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function ObjectTable.new()
|
|
|
|
local o = {}
|
|
|
|
setmetatable(o, ObjectTable)
|
|
|
|
return o
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Various structure
|
2008-08-04 14:31:53 +02:00
|
|
|
hooks = {}
|
|
|
|
hooks.user = {}
|
|
|
|
prompt = {}
|
|
|
|
completion = {}
|
|
|
|
screen = {}
|
|
|
|
layout = {}
|
|
|
|
client = {}
|
|
|
|
client.focus = {}
|
|
|
|
client.focus.history = {}
|
|
|
|
client.focus.history.data = {}
|
|
|
|
tag = {}
|
|
|
|
tag.history = {}
|
|
|
|
tag.history.data = {}
|
|
|
|
tag.history.data.past = {}
|
|
|
|
tag.history.data.current = {}
|
|
|
|
titlebar = {}
|
|
|
|
titlebar.data = ObjectTable.new()
|
|
|
|
widget = {}
|
|
|
|
widget.taglist = {}
|
|
|
|
widget.taglist.label = {}
|
|
|
|
widget.tasklist = {}
|
|
|
|
widget.tasklist.label = {}
|
2008-06-11 10:35:54 +02:00
|
|
|
|
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-07-31 22:33:18 +02:00
|
|
|
--- Remove a client from the focus history
|
|
|
|
-- @param c The client that must be removed.
|
2008-08-04 14:31:53 +02:00
|
|
|
function client.focus.history.delete(c)
|
|
|
|
for k, v in ipairs(client.focus.history.data) do
|
2008-07-31 22:33:18 +02:00
|
|
|
if v == c then
|
2008-08-04 14:31:53 +02:00
|
|
|
table.remove(client.focus.history.data, k)
|
2008-07-31 22:33:18 +02:00
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Update client focus history.
|
|
|
|
-- @param c The client that has been focused.
|
2008-08-04 14:31:53 +02:00
|
|
|
function client.focus.history.add(c)
|
2008-07-31 22:33:18 +02:00
|
|
|
-- Remove the client if its in stack
|
2008-08-04 14:31:53 +02:00
|
|
|
client.focus.history.delete(c)
|
2008-07-31 22:33:18 +02:00
|
|
|
-- Record the client has latest focused
|
2008-08-04 14:31:53 +02:00
|
|
|
table.insert(client.focus.history.data, 1, c)
|
2008-07-31 22:33:18 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
--- Get the latest focused client for a screen in history.
|
|
|
|
-- @param screen The screen number to look for.
|
|
|
|
-- @param idx The index: 0 will return first candidate,
|
|
|
|
-- 1 will return second, etc.
|
|
|
|
-- @return A client.
|
2008-08-04 14:31:53 +02:00
|
|
|
function client.focus.history.get(screen, idx)
|
2008-07-31 22:33:18 +02:00
|
|
|
-- When this counter is equal to idx, we return the client
|
|
|
|
local counter = 0
|
2008-08-04 14:31:53 +02:00
|
|
|
local vc = capi.client.visible_get(screen)
|
|
|
|
for k, c in ipairs(client.focus.history.data) do
|
2008-07-31 22:33:18 +02:00
|
|
|
if c.screen == screen then
|
|
|
|
for j, vcc in ipairs(vc) do
|
|
|
|
if vcc == c then
|
|
|
|
if counter == idx then
|
|
|
|
return c
|
|
|
|
end
|
|
|
|
-- We found one, increment the counter only.
|
|
|
|
counter = counter + 1
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
-- Argh nobody found in history, give the first one visible if there is one
|
|
|
|
if counter == 0 then
|
|
|
|
return vc[1]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Focus the previous client in history.
|
2008-08-04 14:31:53 +02:00
|
|
|
function client.focus.history.previous()
|
|
|
|
local sel = capi.client.focus_get()
|
2008-07-31 22:33:18 +02:00
|
|
|
local s
|
|
|
|
if sel then
|
|
|
|
s = sel.screen
|
|
|
|
else
|
2008-08-04 14:31:53 +02:00
|
|
|
s = capi.mouse.screen
|
2008-07-31 22:33:18 +02:00
|
|
|
end
|
2008-08-04 14:31:53 +02:00
|
|
|
local c = client.focus.history.get(s, 1)
|
2008-07-31 22:33:18 +02:00
|
|
|
if c then c:focus_set() end
|
|
|
|
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.
|
2008-08-04 14:31:53 +02:00
|
|
|
function client.next(i, c)
|
2008-05-20 15:39:47 +02:00
|
|
|
-- Get currently focused client
|
2008-08-04 14:31:53 +02:00
|
|
|
local sel = c or capi.client.focus_get()
|
2008-06-20 09:15:20 +02:00
|
|
|
if sel then
|
|
|
|
-- Get all visible clients
|
2008-08-04 14:31:53 +02:00
|
|
|
local cls = capi.client.visible_get(sel.screen)
|
2008-06-20 09:15:20 +02:00
|
|
|
-- 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.
|
2008-08-04 14:31:53 +02:00
|
|
|
function client.focusbyidx(i, c)
|
|
|
|
local target = client.next(i, c)
|
2008-06-20 09:15:20 +02:00
|
|
|
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.
|
2008-08-04 14:31:53 +02:00
|
|
|
function client.swap(i, c)
|
|
|
|
local sel = c or capi.client.focus_get()
|
|
|
|
local target = client.next(i, sel)
|
2008-06-20 09:15:20 +02:00
|
|
|
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-08-04 14:31:53 +02:00
|
|
|
--- Get the master window.
|
2008-06-20 09:15:20 +02:00
|
|
|
-- @param screen Optional screen number, otherwise screen mouse is used.
|
|
|
|
-- @return The master window.
|
2008-08-04 14:31:53 +02:00
|
|
|
function client.master(screen)
|
|
|
|
local s = screen or capi.mouse.screen
|
|
|
|
return capi.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.
|
2008-08-04 14:31:53 +02:00
|
|
|
function client.moveresize(x, y, w, h, c)
|
|
|
|
local sel = c or capi.client.focus_get()
|
2008-07-01 22:08:27 +02:00
|
|
|
local coords = sel.coords
|
2008-06-06 01:17:41 +02:00
|
|
|
coords['x'] = coords['x'] + x
|
|
|
|
coords['y'] = coords['y'] + y
|
|
|
|
coords['width'] = coords['width'] + w
|
|
|
|
coords['height'] = coords['height'] + h
|
2008-07-01 22:08:27 +02:00
|
|
|
sel.coords = coords
|
2008-06-06 01:17:41 +02:00
|
|
|
end
|
|
|
|
|
2008-07-29 18:29:27 +02:00
|
|
|
--- Maximize a client to use the full workspace area.
|
|
|
|
-- @param c A client, or the focused one if nil.
|
2008-08-04 14:31:53 +02:00
|
|
|
function client.maximize(c)
|
|
|
|
local sel = c or capi.client.focus_get()
|
2008-07-29 18:29:27 +02:00
|
|
|
if sel then
|
|
|
|
sel.floating = true
|
2008-08-04 14:31:53 +02:00
|
|
|
local ws = capi.screen.workspace_get(sel.screen)
|
2008-07-29 18:29:27 +02:00
|
|
|
ws.width = ws.width - 2 * sel.border_width
|
|
|
|
ws.height = ws.height - 2 * sel.border_width
|
|
|
|
sel.coords = ws
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-06-20 09:15:20 +02:00
|
|
|
--- Give the focus to a screen, and move pointer.
|
|
|
|
-- @param Screen number.
|
2008-08-04 14:31:53 +02:00
|
|
|
function screen.focus(i)
|
|
|
|
local sel = capi.client.focus_get()
|
2008-06-09 21:43:09 +02:00
|
|
|
local s
|
|
|
|
if sel then
|
2008-07-01 21:39:00 +02:00
|
|
|
s = sel.screen
|
2008-06-09 21:43:09 +02:00
|
|
|
else
|
2008-08-04 14:31:53 +02:00
|
|
|
s = capi.mouse.screen
|
2008-06-09 21:43:09 +02:00
|
|
|
end
|
2008-08-04 14:31:53 +02:00
|
|
|
s = cycle(capi.screen.count(), s + i)
|
|
|
|
local c = client.focus.history.get(s, 0)
|
2008-07-31 22:33:18 +02:00
|
|
|
if c then c:focus_set() end
|
2008-05-21 16:59:39 +02:00
|
|
|
-- Move the mouse on the screen
|
2008-08-04 14:31:53 +02:00
|
|
|
capi.mouse.coords = capi.screen.coords_get(s)
|
2008-05-20 15:39:47 +02:00
|
|
|
end
|
|
|
|
|
2008-07-30 15:37:05 +02:00
|
|
|
--- Compare 2 tables of tags.
|
|
|
|
-- @param a The first table.
|
|
|
|
-- @param b The second table of tags.
|
|
|
|
-- @return True if the tables are identical, false otherwise.
|
|
|
|
local function tag_compare_select(a, b)
|
|
|
|
if not a or not b then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
-- Quick size comparison
|
|
|
|
if #a ~= #b then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
for ka, va in pairs(a) do
|
|
|
|
if b[ka] ~= va.selected then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
for kb, vb in pairs(b) do
|
|
|
|
if a[kb].selected ~= vb then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Update the tag history.
|
|
|
|
-- @param screen The screen number.
|
2008-08-04 14:31:53 +02:00
|
|
|
function tag.history.update(screen)
|
|
|
|
local curtags = capi.tag.geti(screen)
|
|
|
|
if not tag_compare_select(curtags, tag.history.data.current[screen]) then
|
|
|
|
tag.history.data.past[screen] = tag.history.data.current[screen]
|
|
|
|
tag.history.data.current[screen] = {}
|
2008-07-30 15:37:05 +02:00
|
|
|
for k, v in ipairs(curtags) do
|
2008-08-04 14:31:53 +02:00
|
|
|
tag.history.data.current[screen][k] = v.selected
|
2008-07-30 15:37:05 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Revert tag history.
|
|
|
|
-- @param screen The screen number.
|
2008-08-04 14:31:53 +02:00
|
|
|
function tag.history.restore(screen)
|
|
|
|
local s = screen or capi.mouse.screen
|
|
|
|
local tags = capi.tag.geti(s)
|
2008-07-30 15:37:05 +02:00
|
|
|
for k, t in pairs(tags) do
|
2008-08-04 14:31:53 +02:00
|
|
|
t.selected = tag.history.data.past[s][k]
|
2008-07-30 15:37:05 +02:00
|
|
|
end
|
|
|
|
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-08-04 14:31:53 +02:00
|
|
|
function tag.selectedlist(s)
|
|
|
|
local screen = s or capi.mouse.screen
|
|
|
|
local tags = capi.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-08-04 14:31:53 +02:00
|
|
|
function tag.selected(s)
|
|
|
|
return 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.
|
2008-08-04 14:31:53 +02:00
|
|
|
function tag.setmwfact(mwfact)
|
|
|
|
local t = 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.
|
2008-08-04 14:31:53 +02:00
|
|
|
function tag.incmwfact(add)
|
|
|
|
local t = 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.
|
2008-08-04 14:31:53 +02:00
|
|
|
function tag.setnmaster(nmaster)
|
|
|
|
local t = 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.
|
2008-08-04 14:31:53 +02:00
|
|
|
function tag.incnmaster(add)
|
|
|
|
local t = 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.
|
2008-08-04 14:31:53 +02:00
|
|
|
function tag.setncol(ncol)
|
|
|
|
local t = tag.selected()
|
2008-05-23 11:51:53 +02:00
|
|
|
if t then
|
2008-07-01 14:23:06 +02:00
|
|
|
t.ncol = 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.
|
2008-08-04 14:31:53 +02:00
|
|
|
function tag.incncol(add)
|
|
|
|
local t = tag.selected()
|
2008-05-20 15:39:47 +02:00
|
|
|
if t then
|
2008-07-01 14:23:06 +02:00
|
|
|
t.ncol = t.ncol + 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.
|
2008-08-04 14:31:53 +02:00
|
|
|
function tag.viewnone(screen)
|
|
|
|
local tags = capi.tag.get(screen or capi.mouse.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 = 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.
|
2008-08-04 14:31:53 +02:00
|
|
|
function tag.viewidx(i, screen)
|
|
|
|
local tags = capi.tag.geti(screen or capi.mouse.screen)
|
|
|
|
local sel = tag.selected()
|
|
|
|
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-08-04 14:31:53 +02:00
|
|
|
function tag.viewnext()
|
|
|
|
return 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-08-04 14:31:53 +02:00
|
|
|
function tag.viewprev()
|
|
|
|
return 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-08-04 14:31:53 +02:00
|
|
|
function tag.viewonly(t)
|
|
|
|
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.
|
2008-08-04 14:31:53 +02:00
|
|
|
function tag.viewmore(tags, screen)
|
|
|
|
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-08-04 14:31:53 +02:00
|
|
|
function client.movetotag(target, c)
|
|
|
|
local sel = c or capi.client.focus_get();
|
2008-07-28 16:09:01 +02:00
|
|
|
-- Check that tag and client screen are identical
|
2008-07-28 16:06:06 +02:00
|
|
|
if sel.screen ~= target.screen then return end
|
2008-08-04 14:31:53 +02:00
|
|
|
local tags = capi.tag.get(sel.screen)
|
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-08-04 14:31:53 +02:00
|
|
|
function client.toggletag(target, c)
|
|
|
|
local sel = c or capi.client.focus_get();
|
2008-07-28 16:09:01 +02:00
|
|
|
-- Check that tag and client screen are identical
|
|
|
|
if sel.screen ~= target.screen then return end
|
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
|
2008-08-04 14:31:53 +02:00
|
|
|
for k, v in pairs(capi.tag.get(sel.screen)) do
|
2008-06-18 16:04:49 +02:00
|
|
|
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-08-04 14:31:53 +02:00
|
|
|
function client.togglefloating(c)
|
|
|
|
local sel = c or capi.client.focus_get();
|
2008-05-20 15:39:47 +02:00
|
|
|
if sel then
|
2008-07-01 19:55:18 +02:00
|
|
|
sel.floating = not sel.floating
|
2008-05-20 15:39:47 +02:00
|
|
|
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-08-04 14:31:53 +02:00
|
|
|
function client.movetoscreen(c, s)
|
|
|
|
local sel = c or capi.client.focus_get();
|
2008-06-09 09:06:00 +02:00
|
|
|
if sel then
|
2008-08-04 14:31:53 +02:00
|
|
|
local sc = capi.screen.count()
|
2008-06-09 09:06:00 +02:00
|
|
|
if not s then
|
2008-07-01 21:39:00 +02:00
|
|
|
s = sel.screen + 1
|
2008-06-09 09:06:00 +02:00
|
|
|
end
|
|
|
|
if s > sc then s = 1 elseif s < 1 then s = sc end
|
2008-07-01 21:39:00 +02:00
|
|
|
sel.screen = s
|
2008-08-04 14:31:53 +02:00
|
|
|
capi.mouse.coords = capi.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-08-04 14:31:53 +02:00
|
|
|
function layout.get(screen)
|
|
|
|
local t = tag.selected(screen)
|
2008-06-09 21:43:09 +02:00
|
|
|
if t then
|
2008-07-01 15:06:20 +02:00
|
|
|
return t.layout
|
2008-06-09 21:43:09 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-08-04 14:31:53 +02:00
|
|
|
--- Create a new userhook (for external libs).
|
|
|
|
-- @param name Hook name.
|
|
|
|
function hooks.user.create(name)
|
|
|
|
hooks[name] = {}
|
|
|
|
hooks[name].callbacks = {}
|
|
|
|
hooks[name].register = function (f)
|
|
|
|
table.insert(hooks[name].callbacks, f)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Call a created userhook (for external libs).
|
|
|
|
-- @param name Hook name.
|
|
|
|
function hooks.user.call(name, ...)
|
|
|
|
for name, callback in pairs(hooks[name].callbacks) do
|
|
|
|
callback(unpack(args))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-06-11 10:35:54 +02:00
|
|
|
-- Just set an awful mark to a client to move it later.
|
|
|
|
local awfulmarked = {}
|
2008-08-04 14:31:53 +02:00
|
|
|
hooks.user.create('marked')
|
|
|
|
hooks.user.create('unmarked')
|
2008-06-11 10:35:54 +02:00
|
|
|
|
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-08-04 14:31:53 +02:00
|
|
|
function client.mark (c)
|
|
|
|
local cl = c or capi.client.focus_get()
|
2008-06-11 10:35:54 +02:00
|
|
|
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
|
2008-08-04 14:31:53 +02:00
|
|
|
hooks.user.call('marked', cl)
|
2008-06-11 10:35:54 +02:00
|
|
|
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-08-04 14:31:53 +02:00
|
|
|
function client.unmark(c)
|
|
|
|
local cl = c or capi.client.focus_get()
|
2008-06-11 10:35:54 +02:00
|
|
|
|
|
|
|
for k, v in pairs(awfulmarked) do
|
|
|
|
if cl == v then
|
|
|
|
table.remove(awfulmarked, k)
|
2008-08-04 14:31:53 +02:00
|
|
|
hooks.user.call('unmarked', cl)
|
2008-06-11 10:35:54 +02:00
|
|
|
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-08-04 14:31:53 +02:00
|
|
|
function client.ismarked(c)
|
|
|
|
local cl = c or capi.client.focus_get()
|
2008-06-11 08:52:07 +02:00
|
|
|
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-08-04 14:31:53 +02:00
|
|
|
function client.togglemarked(c)
|
|
|
|
local cl = c or capi.client.focus_get()
|
2008-06-11 10:35:54 +02:00
|
|
|
|
2008-08-04 14:31:53 +02:00
|
|
|
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
|
|
|
|
|
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-08-04 14:31:53 +02:00
|
|
|
function client.getmarked()
|
2008-06-11 10:35:54 +02:00
|
|
|
for k, v in pairs(awfulmarked) do
|
2008-08-04 14:31:53 +02:00
|
|
|
hooks.user.call('unmarked', v)
|
2008-06-11 10:35:54 +02:00
|
|
|
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-08-04 14:31:53 +02:00
|
|
|
function layout.inc(layouts, i)
|
|
|
|
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-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
|
2008-07-01 15:06:20 +02:00
|
|
|
t.layout = layouts[new_layout_index]
|
2008-05-24 13:55:48 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-06-25 09:03:29 +02:00
|
|
|
--- Set the layout of the current tag by name.
|
|
|
|
-- @param layout Layout name.
|
2008-08-04 14:31:53 +02:00
|
|
|
function layout.set(layout)
|
|
|
|
local t = tag.selected()
|
2008-05-24 13:55:48 +02:00
|
|
|
if t then
|
2008-07-01 15:06:20 +02:00
|
|
|
t.layout = layout
|
2008-05-24 13:55:48 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-08-04 14:31:53 +02:00
|
|
|
-- Autodeclare awful.hooks.* functions
|
|
|
|
-- mapped to awesome hooks.* functions
|
|
|
|
for name, hook in pairs(capi.hooks) do
|
2008-05-31 15:47:00 +02:00
|
|
|
if name ~= 'timer' then
|
2008-08-04 14:31:53 +02:00
|
|
|
hooks[name] = {}
|
|
|
|
hooks[name].register = function (f)
|
|
|
|
if not hooks[name].callbacks then
|
|
|
|
hooks[name].callbacks = {}
|
|
|
|
hook(function (...)
|
|
|
|
for i, callback in ipairs(hooks[name].callbacks) do
|
|
|
|
callback(...)
|
2008-05-31 15:47:00 +02:00
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2008-08-04 14:31:53 +02:00
|
|
|
table.insert(hooks[name].callbacks, f)
|
2008-05-31 15:47:00 +02:00
|
|
|
end
|
|
|
|
else
|
2008-08-04 14:31:53 +02:00
|
|
|
hooks[name] = {}
|
|
|
|
hooks[name].register = function (time, f, runnow)
|
|
|
|
if not hooks[name].callbacks then
|
|
|
|
hooks[name].callbacks = {}
|
|
|
|
hook(1, function (...)
|
|
|
|
for i, callback in pairs(hooks[name].callbacks) do
|
|
|
|
if callback['counter'] >= callback['timer'] then
|
|
|
|
callback['counter'] = 1
|
|
|
|
callback['callback'](...)
|
2008-05-31 15:47:00 +02:00
|
|
|
else
|
2008-08-04 14:31:53 +02:00
|
|
|
callback['counter'] = callback['counter'] + 1
|
2008-05-31 15:47:00 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
|
|
|
if runnow then
|
2008-08-04 14:31:53 +02:00
|
|
|
table.insert(hooks[name].callbacks, { callback = f, timer = time, counter = time })
|
2008-05-31 15:47:00 +02:00
|
|
|
else
|
2008-08-04 14:31:53 +02:00
|
|
|
table.insert(hooks[name].callbacks, { callback = f, timer = time, counter = 0 })
|
2008-05-31 15:47:00 +02:00
|
|
|
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-08-04 14:31:53 +02:00
|
|
|
function 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-08-04 14:31:53 +02:00
|
|
|
function 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-08-04 14:31:53 +02:00
|
|
|
function 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-08-04 14:31:53 +02:00
|
|
|
function 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-08-04 14:31:53 +02:00
|
|
|
capi.keygrabber.run(
|
2008-06-10 16:50:55 +02:00
|
|
|
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
|
2008-08-03 11:37:48 +02:00
|
|
|
if key == "c" or key == "g" then
|
2008-06-27 20:24:30 +02:00
|
|
|
textbox.text = ""
|
2008-06-18 12:46:12 +02:00
|
|
|
return false
|
2008-08-03 11:37:48 +02:00
|
|
|
elseif key == "j" or key == "m" then
|
|
|
|
textbox.text = ""
|
|
|
|
exec_callback(command)
|
|
|
|
return false
|
2008-06-18 12:46:12 +02:00
|
|
|
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
|
2008-08-03 11:37:48 +02:00
|
|
|
elseif key == "b" then
|
|
|
|
if cur_pos > 1 then
|
|
|
|
cur_pos = cur_pos - 1
|
|
|
|
end
|
|
|
|
elseif key == "d" then
|
|
|
|
if cur_pos <= #command then
|
|
|
|
command = command:sub(1, cur_pos - 1) .. command:sub(cur_pos + 1)
|
|
|
|
end
|
2008-06-17 16:54:46 +02:00
|
|
|
elseif key == "e" then
|
|
|
|
cur_pos = #command + 1
|
2008-08-03 11:37:48 +02:00
|
|
|
elseif key == "f" then
|
|
|
|
if cur_pos <= #command then
|
|
|
|
cur_pos = cur_pos + 1
|
|
|
|
end
|
|
|
|
elseif key == "h" then
|
|
|
|
if cur_pos > 1 then
|
|
|
|
command = command:sub(1, cur_pos - 2) .. command:sub(cur_pos)
|
|
|
|
cur_pos = cur_pos - 1
|
|
|
|
end
|
|
|
|
elseif key == "k" then
|
|
|
|
command = command:sub(1, cur_pos - 1)
|
|
|
|
elseif key == "u" then
|
|
|
|
command = command:sub(cur_pos, #command)
|
|
|
|
cur_pos = 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-07-01 14:48:48 +02:00
|
|
|
-- len() is UTF-8 aware but #key is not,
|
|
|
|
-- so check that we have one UTF-8 char but advance the cursor of # position
|
|
|
|
if key:len() == 1 then
|
2008-06-25 11:45:57 +02:00
|
|
|
command = command:sub(1, cur_pos - 1) .. key .. command:sub(cur_pos)
|
2008-07-01 14:48:48 +02:00
|
|
|
cur_pos = cur_pos + #key
|
2008-06-25 11:45:57 +02:00
|
|
|
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-08-04 14:31:53 +02:00
|
|
|
function 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-08-04 14:31:53 +02:00
|
|
|
function 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-07-29 11:23:52 +02:00
|
|
|
--- Return labels for a taglist widget with all tag from screen.
|
2008-07-29 10:24:03 +02:00
|
|
|
-- It returns the tag name and set a special
|
|
|
|
-- foreground and background color for selected tags.
|
|
|
|
-- @param t The tag.
|
|
|
|
-- @param bg_focus The background color for selected tag.
|
|
|
|
-- @param fg_focus The foreground color for selected tag.
|
2008-07-31 23:04:35 +02:00
|
|
|
-- @param bg_urgent The background color for urgent tags.
|
|
|
|
-- @param fg_urgent The foreground color for urgent tags.
|
2008-07-29 10:24:03 +02:00
|
|
|
-- @return A string to print.
|
2008-08-04 14:31:53 +02:00
|
|
|
function widget.taglist.label.all(t, bg_focus, fg_focus, bg_urgent, fg_urgent)
|
2008-07-31 23:04:35 +02:00
|
|
|
local text
|
2008-07-29 17:48:39 +02:00
|
|
|
local background = ""
|
2008-08-04 14:31:53 +02:00
|
|
|
local sel = capi.client.focus_get()
|
2008-07-31 23:04:35 +02:00
|
|
|
local bg_color = nil
|
|
|
|
local fg_color = nil
|
2008-08-01 10:46:25 +02:00
|
|
|
if t.selected then
|
2008-07-31 23:04:35 +02:00
|
|
|
bg_color = bg_focus
|
|
|
|
fg_color = fg_focus
|
2008-08-01 10:46:25 +02:00
|
|
|
end
|
|
|
|
if sel and sel:istagged(t) then
|
2008-07-29 17:48:39 +02:00
|
|
|
background = "resize=\"true\" image=\"@AWESOME_ICON_PATH@/taglist/squarefw.png\""
|
2008-08-01 10:26:13 +02:00
|
|
|
elseif bg_urgent and fg_urgent then
|
2008-08-04 14:31:53 +02:00
|
|
|
for k, c in pairs(capi.client.get()) do
|
2008-08-01 10:44:25 +02:00
|
|
|
if c:istagged(t) then
|
|
|
|
background = "resize=\"true\" image=\"@AWESOME_ICON_PATH@/taglist/squarew.png\""
|
|
|
|
if c.urgent then
|
|
|
|
bg_color = bg_urgent
|
|
|
|
fg_color = fg_urgent
|
|
|
|
end
|
2008-07-29 17:48:39 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2008-07-31 23:04:35 +02:00
|
|
|
if bg_color and fg_color then
|
2008-08-04 14:31:53 +02:00
|
|
|
text = "<bg "..background.." color='"..bg_color.."'/> <span color='"..fg_color.."'>"..escape(t.name).."</span> "
|
2008-07-29 10:24:03 +02:00
|
|
|
else
|
2008-08-04 14:31:53 +02:00
|
|
|
text = " <bg "..background.." />"..escape(t.name).." "
|
2008-07-29 10:24:03 +02:00
|
|
|
end
|
|
|
|
return text
|
|
|
|
end
|
|
|
|
|
2008-07-31 23:04:35 +02:00
|
|
|
local function widget_tasklist_label_common(c, bg_focus, fg_focus, bg_urgent, fg_urgent)
|
2008-07-29 11:21:47 +02:00
|
|
|
local text = ""
|
|
|
|
if c.floating then
|
2008-07-29 17:49:32 +02:00
|
|
|
text = "<bg image=\"@AWESOME_ICON_PATH@/tasklist/floatingw.png\" align=\"right\"/>"
|
2008-07-29 11:21:47 +02:00
|
|
|
end
|
2008-08-04 14:31:53 +02:00
|
|
|
if capi.client.focus_get() == c then
|
|
|
|
text = text .. " <bg color='"..bg_focus.."'/><span color='"..fg_focus.."'>"..escape(c.name).."</span> "
|
2008-08-01 10:26:13 +02:00
|
|
|
elseif c.urgent and bg_urgent and fg_urgent then
|
2008-08-04 14:31:53 +02:00
|
|
|
text = text .. " <bg color='"..bg_urgent.."'/><span color='"..fg_urgent.."'>"..escape(c.name).."</span> "
|
2008-07-29 11:21:47 +02:00
|
|
|
else
|
2008-08-04 14:31:53 +02:00
|
|
|
text = text .. " "..escape(c.name).." "
|
2008-07-29 11:21:47 +02:00
|
|
|
end
|
|
|
|
return text
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Return labels for a tasklist widget with clients from all tags and screen.
|
|
|
|
-- It returns the client name and set a special
|
|
|
|
-- foreground and background color for focused client.
|
|
|
|
-- It also puts a special icon for floating windows.
|
|
|
|
-- @param c The client.
|
|
|
|
-- @param screen The screen we are drawing on.
|
|
|
|
-- @param bg_focus The background color for focused client.
|
|
|
|
-- @param fg_focus The foreground color for focused client.
|
2008-07-31 23:04:35 +02:00
|
|
|
-- @param bg_urgent The background color for urgent clients.
|
|
|
|
-- @param fg_urgent The foreground color for urgent clients.
|
2008-07-29 11:21:47 +02:00
|
|
|
-- @return A string to pring.
|
2008-08-04 14:31:53 +02:00
|
|
|
function widget.tasklist.label.allscreen(c, screen, bg_focus, fg_focus, bg_urgent, fg_urgent)
|
2008-07-31 23:04:35 +02:00
|
|
|
return widget_tasklist_label_common(c, bg_focus, fg_focus, bg_urgent, fg_urgent)
|
2008-07-29 11:21:47 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
--- Return labels for a tasklist widget with clients from all tags.
|
|
|
|
-- It returns the client name and set a special
|
|
|
|
-- foreground and background color for focused client.
|
|
|
|
-- It also puts a special icon for floating windows.
|
|
|
|
-- @param c The client.
|
|
|
|
-- @param screen The screen we are drawing on.
|
|
|
|
-- @param bg_focus The background color for focused client.
|
|
|
|
-- @param fg_focus The foreground color for focused client.
|
2008-07-31 23:04:35 +02:00
|
|
|
-- @param bg_urgent The background color for urgent clients.
|
|
|
|
-- @param fg_urgent The foreground color for urgent clients.
|
2008-07-29 11:21:47 +02:00
|
|
|
-- @return A string to pring.
|
2008-08-04 14:31:53 +02:00
|
|
|
function widget.tasklist.label.alltags(c, screen, bg_focus, fg_focus, bg_urgent, fg_urgent)
|
2008-07-29 11:21:47 +02:00
|
|
|
-- Only print client on the same screen as this widget
|
|
|
|
if c.screen ~= screen then return end
|
2008-07-31 23:04:35 +02:00
|
|
|
return widget_tasklist_label_common(c, bg_focus, fg_focus, bg_urgent, fg_urgent)
|
2008-07-29 11:21:47 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
--- Return labels for a tasklist widget with clients from currently selected tags.
|
|
|
|
-- It returns the client name and set a special
|
|
|
|
-- foreground and background color for focused client.
|
|
|
|
-- It also puts a special icon for floating windows.
|
|
|
|
-- @param c The client.
|
|
|
|
-- @param screen The screen we are drawing on.
|
|
|
|
-- @param bg_focus The background color for focused client.
|
|
|
|
-- @param fg_focus The foreground color for focused client.
|
2008-07-31 23:04:35 +02:00
|
|
|
-- @param bg_urgent The background color for urgent clients.
|
|
|
|
-- @param fg_urgent The foreground color for urgent clients.
|
2008-07-29 11:21:47 +02:00
|
|
|
-- @return A string to pring.
|
2008-08-04 14:31:53 +02:00
|
|
|
function widget.tasklist.label.currenttags(c, screen, bg_focus, fg_focus, bg_urgent, fg_urgent)
|
2008-07-29 11:21:47 +02:00
|
|
|
-- Only print client on the same screen as this widget
|
|
|
|
if c.screen ~= screen then return end
|
2008-08-04 14:31:53 +02:00
|
|
|
for k, t in pairs(capi.tag.get(screen)) do
|
2008-07-29 11:21:47 +02:00
|
|
|
if t.selected and c:istagged(t) then
|
2008-07-31 23:04:35 +02:00
|
|
|
return widget_tasklist_label_common(c, bg_focus, fg_focus, bg_urgent, fg_urgent)
|
2008-07-29 11:21:47 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-07-29 15:44:09 +02:00
|
|
|
--- Create a standard titlebar.
|
2008-08-01 00:10:04 +02:00
|
|
|
-- @param c The client.
|
2008-07-29 15:44:09 +02:00
|
|
|
-- @param args Arguments.
|
2008-08-01 00:10:04 +02:00
|
|
|
-- fg: the foreground color.
|
2008-07-29 15:44:09 +02:00
|
|
|
-- bg: the background color.
|
2008-08-01 00:10:04 +02:00
|
|
|
-- fg_focus: the foreground color for focused window.
|
|
|
|
-- fg_focus: the background color for focused window.
|
2008-08-04 14:31:53 +02:00
|
|
|
function titlebar.add(c, args)
|
2008-08-01 00:10:04 +02:00
|
|
|
-- Store colors
|
2008-08-04 14:31:53 +02:00
|
|
|
titlebar.data[c] = {}
|
|
|
|
titlebar.data[c].fg = args.fg
|
|
|
|
titlebar.data[c].bg = args.bg
|
|
|
|
titlebar.data[c].fg_focus = args.fg_focus
|
|
|
|
titlebar.data[c].bg_focus = args.bg_focus
|
2008-08-01 00:10:04 +02:00
|
|
|
|
|
|
|
-- Built args
|
2008-07-29 16:56:38 +02:00
|
|
|
local targs = {}
|
|
|
|
if args.fg then targs.fg = args.fg end
|
|
|
|
if args.bg then targs.bg = args.bg end
|
2008-08-04 14:31:53 +02:00
|
|
|
local tb = capi.titlebar(targs)
|
2008-08-01 00:10:04 +02:00
|
|
|
|
2008-08-04 14:31:53 +02:00
|
|
|
tb:widget_add(capi.widget({ type = "appicon", name = "appicon", align = "left" }))
|
2008-08-01 00:10:04 +02:00
|
|
|
|
2008-08-04 14:31:53 +02:00
|
|
|
local title = capi.widget({ type = "textbox", name = "title", align = "flex" })
|
2008-07-29 16:56:38 +02:00
|
|
|
title:mouse_add(mouse({ args.modkey }, 1, function (t) t:client_get():mouse_move() end))
|
|
|
|
title:mouse_add(mouse({ args.modkey }, 3, function (t) t:client_get():mouse_resize() end))
|
|
|
|
tb:widget_add(title)
|
2008-08-01 00:10:04 +02:00
|
|
|
|
2008-08-04 14:31:53 +02:00
|
|
|
local close_button= capi.widget({ type = "textbox", name = "close", align = "right" })
|
2008-07-29 15:44:09 +02:00
|
|
|
close_button:mouse_add(mouse({ }, 1, function (t) t:client_get():kill() end))
|
|
|
|
tb:widget_add(close_button)
|
2008-08-01 00:10:04 +02:00
|
|
|
|
2008-08-04 14:31:53 +02:00
|
|
|
titlebar.update(c)
|
2008-08-01 00:10:04 +02:00
|
|
|
|
|
|
|
c.titlebar = tb
|
2008-07-29 15:44:09 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
--- Update a titlebar. This should be called in some hooks.
|
|
|
|
-- @param c The client to update.
|
2008-08-04 14:31:53 +02:00
|
|
|
function titlebar.update(c)
|
|
|
|
if c.titlebar and titlebar.data[c] then
|
2008-07-29 15:44:09 +02:00
|
|
|
local widgets = c.titlebar:widget_get()
|
|
|
|
if widgets.title then
|
2008-08-04 14:31:53 +02:00
|
|
|
widgets.title.text = " " .. escape(c.name)
|
2008-07-29 15:44:09 +02:00
|
|
|
end
|
2008-08-04 14:31:53 +02:00
|
|
|
if capi.client.focus_get() == c then
|
|
|
|
c.titlebar.fg = titlebar.data[c].fg_focus
|
|
|
|
c.titlebar.bg = titlebar.data[c].bg_focus
|
2008-07-29 15:44:09 +02:00
|
|
|
if widgets.close then
|
|
|
|
widgets.close.text = "<bg image=\"@AWESOME_ICON_PATH@/titlebar/closer.png\" resize=\"true\"/>"
|
|
|
|
end
|
|
|
|
else
|
2008-08-04 14:31:53 +02:00
|
|
|
c.titlebar.fg = titlebar.data[c].fg
|
|
|
|
c.titlebar.bg = titlebar.data[c].bg
|
2008-07-29 15:44:09 +02:00
|
|
|
if widgets.close then
|
|
|
|
widgets.close.text = "<bg image=\"@AWESOME_ICON_PATH@/titlebar/close.png\" resize=\"true\"/>"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-08-01 00:10:04 +02:00
|
|
|
--- Remove a titlebar from a client.
|
|
|
|
-- @param c The client.
|
2008-08-04 14:31:53 +02:00
|
|
|
function titlebar.remove(c)
|
2008-08-01 00:10:04 +02:00
|
|
|
c.titlebar = nil
|
2008-08-04 14:31:53 +02:00
|
|
|
titlebar.data[c] = nil
|
2008-08-01 00:10:04 +02:00
|
|
|
end
|
|
|
|
|
2008-07-31 23:44:33 +02:00
|
|
|
-- Register standards hooks
|
2008-08-04 14:31:53 +02:00
|
|
|
hooks.arrange.register(tag.history.update)
|
2008-07-31 23:44:33 +02:00
|
|
|
|
2008-08-04 14:31:53 +02:00
|
|
|
hooks.focus.register(client.focus.history.add)
|
|
|
|
hooks.unmanage.register(client.focus.history.delete)
|
2008-08-01 00:10:04 +02:00
|
|
|
|
2008-08-04 14:31:53 +02:00
|
|
|
hooks.focus.register(titlebar.update)
|
|
|
|
hooks.unfocus.register(titlebar.update)
|
|
|
|
hooks.titleupdate.register(titlebar.update)
|
|
|
|
hooks.unmanage.register(titlebar.remove)
|