2008-08-04 14:31:53 +02:00
|
|
|
---------------------------------------------------------------------------
|
2008-08-04 16:41:23 +02:00
|
|
|
-- @author Julien Danjou <julien@danjou.info>
|
2008-08-04 14:31:53 +02:00
|
|
|
-- @copyright 2008 Julien Danjou
|
|
|
|
---------------------------------------------------------------------------
|
2008-05-20 15:39:47 +02:00
|
|
|
|
|
|
|
-- Grab environment we need
|
2008-08-04 14:51:03 +02:00
|
|
|
local type = type
|
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-05-20 15:39:47 +02:00
|
|
|
local os = os
|
2008-06-18 20:07:18 +02:00
|
|
|
local io = io
|
2008-08-06 21:11:47 +02:00
|
|
|
local math = math
|
2008-08-01 00:10:04 +02:00
|
|
|
local setmetatable = setmetatable
|
2008-08-04 14:31:53 +02:00
|
|
|
local table = table
|
2008-08-07 15:48:26 +02:00
|
|
|
local otable = otable
|
2008-08-04 14:31:53 +02:00
|
|
|
local capi =
|
|
|
|
{
|
|
|
|
screen = screen,
|
|
|
|
client = client,
|
|
|
|
mouse = mouse,
|
|
|
|
titlebar = titlebar,
|
|
|
|
widget = widget,
|
|
|
|
hooks = hooks,
|
|
|
|
keygrabber = keygrabber
|
|
|
|
}
|
|
|
|
|
2008-08-04 16:41:23 +02:00
|
|
|
--- awful: AWesome Functions very UsefuL
|
2008-08-04 14:31:53 +02:00
|
|
|
module("awful")
|
2008-05-20 15:39:47 +02:00
|
|
|
|
2008-08-06 15:15:13 +02:00
|
|
|
-- Local variable handling theme
|
|
|
|
local theme = {}
|
|
|
|
|
|
|
|
-- Various public structures
|
|
|
|
beautiful = {}
|
2008-08-04 14:31:53 +02:00
|
|
|
hooks = {}
|
|
|
|
hooks.user = {}
|
|
|
|
prompt = {}
|
2008-08-11 19:47:23 +02:00
|
|
|
prompt.history = {}
|
2008-08-13 20:13:20 +02:00
|
|
|
prompt.history.data = {}
|
2008-08-04 14:31:53 +02:00
|
|
|
completion = {}
|
|
|
|
screen = {}
|
|
|
|
layout = {}
|
|
|
|
client = {}
|
2008-08-12 10:34:49 +02:00
|
|
|
client.data = {}
|
|
|
|
client.data.maximize = otable()
|
2008-08-04 14:31:53 +02:00
|
|
|
client.focus = {}
|
|
|
|
client.focus.history = {}
|
|
|
|
client.focus.history.data = {}
|
|
|
|
tag = {}
|
|
|
|
tag.history = {}
|
|
|
|
tag.history.data = {}
|
|
|
|
tag.history.data.past = {}
|
|
|
|
tag.history.data.current = {}
|
|
|
|
titlebar = {}
|
2008-08-07 15:48:26 +02:00
|
|
|
titlebar.data = otable()
|
2008-08-04 14:31:53 +02:00
|
|
|
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()
|
2008-08-12 12:08:20 +02:00
|
|
|
local sel = capi.client.focus
|
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-08-12 12:08:20 +02:00
|
|
|
if c then capi.client.focus = c end
|
2008-07-31 22:33:18 +02:00
|
|
|
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-12 12:08:20 +02:00
|
|
|
local sel = c or capi.client.focus
|
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-08-06 21:11:47 +02:00
|
|
|
--- Return true whether client B is in the right direction
|
|
|
|
-- compared to client A.
|
|
|
|
-- @param dir The direction.
|
|
|
|
-- @param cA The first client.
|
|
|
|
-- @param cB The second client.
|
|
|
|
-- @return True if B is in the direction of A.
|
|
|
|
local function is_in_direction(dir, cA, cB)
|
|
|
|
if dir == "up" then
|
|
|
|
return cA['y'] > cB['y']
|
|
|
|
elseif dir == "down" then
|
|
|
|
return cA['y'] < cB['y']
|
|
|
|
elseif dir == "left" then
|
|
|
|
return cA['x'] > cB['x']
|
|
|
|
elseif dir == "right" then
|
|
|
|
return cA['x'] < cB['x']
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Calculate distance between two points.
|
|
|
|
-- i.e: if we want to move to the right, we will take the right border
|
|
|
|
-- of the currently focused client and the left side of the checked client.
|
|
|
|
-- This avoid the focus of an upper client when you move to the right in a
|
|
|
|
-- tilebottom layout with nmaster=2 and 5 clients open, for instance.
|
|
|
|
-- @param dir The direction.
|
|
|
|
-- @param cA The first client.
|
|
|
|
-- @param cB The second client.
|
|
|
|
-- @return The distance between the clients.
|
|
|
|
local function calculate_distance(dir, cA, cB)
|
|
|
|
local xA = cA['x']
|
|
|
|
local xB = cB['x']
|
|
|
|
local yA = cA['y']
|
|
|
|
local yB = cB['y']
|
|
|
|
|
|
|
|
if dir == "up" then
|
|
|
|
yB = yB + cB['height']
|
|
|
|
elseif dir == "down" then
|
|
|
|
yA = yA + cA['height']
|
|
|
|
elseif dir == "left" then
|
|
|
|
xB = xB + cB['width']
|
|
|
|
elseif dir == "right" then
|
|
|
|
xA = xA + cA['width']
|
|
|
|
end
|
|
|
|
|
|
|
|
return math.sqrt(math.pow(xB - xA, 2) + math.pow(yB - yA, 2))
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Focus a client by the given direction.
|
|
|
|
-- @param dir The direction, can be either "up", "down", "left" or "right".
|
|
|
|
-- @param c Optional client.
|
|
|
|
function client.focusbydirection(dir, c)
|
2008-08-12 12:08:20 +02:00
|
|
|
local sel = c or capi.client.focus
|
2008-08-06 21:11:47 +02:00
|
|
|
if sel then
|
|
|
|
local coords = sel.coords
|
|
|
|
local dist, dist_min
|
|
|
|
local target = nil
|
|
|
|
local cls = capi.client.visible_get(sel.screen)
|
|
|
|
|
|
|
|
-- We check each client.
|
|
|
|
for i, c in ipairs(cls) do
|
|
|
|
-- Check coords to see if client is located in the right direction.
|
|
|
|
if is_in_direction(dir, coords, c.coords) then
|
|
|
|
|
|
|
|
-- Calculate distance between focused client and checked client.
|
|
|
|
dist = calculate_distance(dir, coords, c.coords)
|
|
|
|
|
|
|
|
-- If distance is shorter then keep the client.
|
|
|
|
if not target or dist < dist_min then
|
|
|
|
target = c
|
|
|
|
dist_min = dist
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- If we found a client to focus, then do it.
|
|
|
|
if target then
|
2008-08-12 12:08:20 +02:00
|
|
|
capi.client.focus = target
|
2008-08-06 21:11: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
|
2008-08-12 12:08:20 +02:00
|
|
|
capi.client.focus = target
|
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)
|
2008-08-12 12:08:20 +02:00
|
|
|
local sel = c or capi.client.focus
|
2008-08-04 14:31:53 +02:00
|
|
|
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)
|
2008-08-12 12:08:20 +02:00
|
|
|
local sel = c or capi.client.focus
|
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-08-11 17:14:02 +02:00
|
|
|
--- Maximize a client to use the full workarea.
|
2008-07-29 18:29:27 +02:00
|
|
|
-- @param c A client, or the focused one if nil.
|
2008-08-04 14:31:53 +02:00
|
|
|
function client.maximize(c)
|
2008-08-12 12:08:20 +02:00
|
|
|
local sel = c or capi.client.focus
|
2008-07-29 18:29:27 +02:00
|
|
|
if sel then
|
2008-08-11 17:14:02 +02:00
|
|
|
local ws = capi.screen[sel.screen].workarea
|
2008-07-29 18:29:27 +02:00
|
|
|
ws.width = ws.width - 2 * sel.border_width
|
|
|
|
ws.height = ws.height - 2 * sel.border_width
|
2008-08-12 10:34:49 +02:00
|
|
|
if sel.floating
|
|
|
|
and sel.coords.x == ws.x
|
|
|
|
and sel.coords.y == ws.y
|
|
|
|
and sel.coords.width == ws.width
|
|
|
|
and sel.coords.height == ws.height then
|
|
|
|
if client.data.maximize[sel] then
|
|
|
|
sel.floating = client.data.maximize[sel].floating
|
|
|
|
sel.coords = client.data.maximize[sel].coords
|
|
|
|
client.data.maximize[sel] = nil
|
|
|
|
end
|
|
|
|
else
|
|
|
|
client.data.maximize[sel] = { coords = sel.coords, floating = sel.floating }
|
|
|
|
sel.floating = true
|
|
|
|
sel.coords = ws
|
|
|
|
end
|
2008-07-29 18:29:27 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-08-12 10:34:49 +02:00
|
|
|
|
|
|
|
--- Erase eventual client data in maximize.
|
|
|
|
-- @param c The client.
|
|
|
|
local function client_maximize_clean(c)
|
|
|
|
client.data.maximize[c] = nil
|
|
|
|
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)
|
2008-08-12 17:35:00 +02:00
|
|
|
local s = cycle(capi.screen.count(), capi.mouse.screen + i)
|
2008-08-04 14:31:53 +02:00
|
|
|
local c = client.focus.history.get(s, 0)
|
2008-08-12 12:08:20 +02:00
|
|
|
if c then capi.client.focus = c end
|
2008-05-21 16:59:39 +02:00
|
|
|
-- Move the mouse on the screen
|
2008-08-11 17:14:02 +02:00
|
|
|
capi.mouse.coords = capi.screen[s].coords
|
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)
|
2008-08-13 18:12:26 +02:00
|
|
|
local curtags = capi.screen[screen]:tags()
|
2008-08-04 14:31:53 +02:00
|
|
|
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
|
2008-08-13 18:12:26 +02:00
|
|
|
local tags = capi.screen[s]:tags()
|
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
|
2008-08-13 18:12:26 +02:00
|
|
|
local tags = capi.screen[screen]:tags()
|
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)
|
2008-08-13 18:12:26 +02:00
|
|
|
local tags = capi.screen[screen or capi.mouse.screen]:tags()
|
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)
|
2008-08-13 18:12:26 +02:00
|
|
|
local tags = capi.screen[screen or capi.mouse.screen]:tags()
|
2008-08-04 14:31:53 +02:00
|
|
|
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)
|
2008-08-08 14:45:50 +02:00
|
|
|
tag.viewnone(t.screen)
|
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)
|
2008-08-12 12:08:20 +02:00
|
|
|
local sel = c or capi.client.focus
|
2008-08-08 17:07:31 +02:00
|
|
|
if sel then
|
|
|
|
-- Check that tag and client screen are identical
|
|
|
|
if sel.screen ~= target.screen then return end
|
2008-08-13 17:49:57 +02:00
|
|
|
sel:tags({ target })
|
2008-08-08 17:07:31 +02:00
|
|
|
end
|
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)
|
2008-08-12 12:08:20 +02:00
|
|
|
local sel = c or capi.client.focus
|
2008-07-28 16:09:01 +02:00
|
|
|
-- Check that tag and client screen are identical
|
2008-08-07 16:37:58 +02:00
|
|
|
if sel and sel.screen == target.screen then
|
2008-08-13 17:49:57 +02:00
|
|
|
local tags = sel:tags()
|
2008-08-07 16:37:58 +02:00
|
|
|
if tags[target] then
|
|
|
|
tags[target] = nil
|
|
|
|
else
|
|
|
|
tags[target] = target
|
2008-06-10 11:51:36 +02:00
|
|
|
end
|
2008-08-13 17:49:57 +02:00
|
|
|
sel:tags(tags)
|
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)
|
2008-08-12 12:08:20 +02:00
|
|
|
local sel = c or capi.client.focus
|
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)
|
2008-08-12 12:08:20 +02:00
|
|
|
local sel = c or capi.client.focus
|
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-11 17:14:02 +02:00
|
|
|
capi.mouse.coords = capi.screen[s].coords
|
2008-08-12 12:08:20 +02:00
|
|
|
capi.client.focus = sel
|
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
|
2008-08-04 14:51:03 +02:00
|
|
|
hooks[name].unregister = function (f)
|
|
|
|
for k, h in ipairs(hooks[name].callbacks) do
|
|
|
|
if h == f then
|
|
|
|
table.remove(hooks[name].callbacks, k)
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2008-08-04 14:31:53 +02:00
|
|
|
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
|
2008-08-10 08:04:09 +02:00
|
|
|
callback(...)
|
2008-08-04 14:31:53 +02:00
|
|
|
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)
|
2008-08-12 12:08:20 +02:00
|
|
|
local cl = c or capi.client.focus
|
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)
|
2008-08-12 12:08:20 +02:00
|
|
|
local cl = c or capi.client.focus
|
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)
|
2008-08-12 12:08:20 +02:00
|
|
|
local cl = c or capi.client.focus
|
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)
|
2008-08-12 12:08:20 +02:00
|
|
|
local cl = c or capi.client.focus
|
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
|
2008-08-06 21:10:10 +02:00
|
|
|
rev_layouts[v] = i
|
|
|
|
number_of_layouts = number_of_layouts + 1
|
2008-05-24 13:55:48 +02:00
|
|
|
end
|
|
|
|
if t then
|
2008-08-06 21:10:10 +02:00
|
|
|
local cur_layout = layout.get()
|
|
|
|
local new_layout_index = (rev_layouts[cur_layout] + i) % number_of_layouts
|
|
|
|
if new_layout_index == 0 then
|
|
|
|
new_layout_index = number_of_layouts
|
|
|
|
end
|
|
|
|
t.layout = 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-08-06 21:10:10 +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
|
2008-08-04 14:51:03 +02:00
|
|
|
hooks[name].unregister = function (f)
|
|
|
|
if hooks[name].callbacks then
|
|
|
|
for k, h in ipairs(hooks[name].callbacks) do
|
|
|
|
if h == f then
|
|
|
|
table.remove(hooks[name].callbacks, k)
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2008-05-31 15:47:00 +02:00
|
|
|
else
|
2008-08-04 14:31:53 +02:00
|
|
|
hooks[name] = {}
|
|
|
|
hooks[name].register = function (time, f, runnow)
|
2008-08-04 14:51:03 +02:00
|
|
|
if type(time) ~= 'number' or type(f) ~= 'function' then
|
|
|
|
return
|
|
|
|
end
|
2008-08-04 14:31:53 +02:00
|
|
|
if not hooks[name].callbacks then
|
|
|
|
hooks[name].callbacks = {}
|
|
|
|
hook(1, function (...)
|
2008-08-04 14:51:03 +02:00
|
|
|
for i, callback in ipairs(hooks[name].callbacks) do
|
2008-08-04 14:31:53 +02:00
|
|
|
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
|
2008-08-04 14:51:03 +02:00
|
|
|
hooks[name].unregister = function (f)
|
|
|
|
if hooks[name].callbacks then
|
|
|
|
for k, h in ipairs(hooks[name].callbacks) do
|
|
|
|
if h.callback == f then
|
|
|
|
table.remove(hooks[name].callbacks, k)
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2008-05-31 15:47:00 +02:00
|
|
|
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-08-14 00:22:50 +02:00
|
|
|
if cmd and cmd ~= "" then
|
|
|
|
return os.execute(cmd .. "&")
|
|
|
|
end
|
2008-05-20 15:39:47 +02:00
|
|
|
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-08-11 19:47:23 +02:00
|
|
|
--- Load history file in history table
|
2008-08-13 20:13:20 +02:00
|
|
|
-- @param id The prompt history identifier which is the path to the filename
|
|
|
|
-- @param max Optional parameter, the maximum number of entries in file
|
|
|
|
local function prompt_history_check_load(id, max)
|
|
|
|
if id and id ~= ""
|
|
|
|
and not prompt.history[id] then
|
|
|
|
prompt.history[id] = { max = 50, table = {} }
|
|
|
|
|
|
|
|
if max then
|
|
|
|
prompt.history[id].max = max
|
|
|
|
end
|
|
|
|
|
|
|
|
local f = io.open(id, "r")
|
|
|
|
|
|
|
|
-- Read history file
|
|
|
|
if f then
|
2008-08-11 19:47:23 +02:00
|
|
|
for line in f:lines() do
|
2008-08-13 20:13:20 +02:00
|
|
|
table.insert(prompt.history[id].table, line)
|
|
|
|
if #prompt.history[id].table >= prompt.history[id].max then
|
|
|
|
break
|
|
|
|
end
|
2008-08-11 20:49:37 +02:00
|
|
|
end
|
2008-08-13 20:13:20 +02:00
|
|
|
end
|
2008-08-11 19:47:23 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-08-13 20:13:20 +02:00
|
|
|
--- Save history table in history file
|
|
|
|
-- @param id The prompt history identifier
|
|
|
|
local function prompt_history_save(id)
|
|
|
|
if prompt.history[id] then
|
|
|
|
local f = assert(io.open(id, "w"))
|
|
|
|
for i = 1, math.min(#prompt.history[id].table, prompt.history[id].max) do
|
|
|
|
f:write(prompt.history[id].table[i] .. "\n")
|
|
|
|
end
|
|
|
|
f:close()
|
2008-08-11 19:47:23 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-08-13 20:13:20 +02:00
|
|
|
--- Return the number of items in history table regarding the id
|
|
|
|
-- @param id The prompt history identifier
|
|
|
|
-- @return the number of items in history table, -1 if history is disabled
|
|
|
|
local function prompt_history_items(id)
|
|
|
|
if prompt.history[id] then
|
|
|
|
return #prompt.history[id].table
|
|
|
|
else
|
|
|
|
return -1
|
2008-08-11 19:47:23 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Add an entry to the history file
|
2008-08-13 20:13:20 +02:00
|
|
|
-- @param id The prompt history identifier
|
2008-08-11 19:47:23 +02:00
|
|
|
-- @param command The command to add
|
2008-08-13 20:13:20 +02:00
|
|
|
local function prompt_history_add(id, command)
|
|
|
|
if prompt.history[id] then
|
2008-08-11 20:39:03 +02:00
|
|
|
if command ~= ""
|
2008-08-13 20:13:20 +02:00
|
|
|
and command ~= prompt.history[id].table[#prompt.history[id].table] then
|
|
|
|
table.insert(prompt.history[id].table, command)
|
2008-08-11 20:39:03 +02:00
|
|
|
|
|
|
|
-- Do not exceed our max_cmd
|
2008-08-13 20:13:20 +02:00
|
|
|
if #prompt.history[id].table > prompt.history[id].max then
|
|
|
|
table.remove(prompt.history[id].table, 1)
|
2008-08-11 20:39:03 +02:00
|
|
|
end
|
2008-08-13 20:13:20 +02:00
|
|
|
|
|
|
|
prompt_history_save(id)
|
2008-08-11 20:39:03 +02:00
|
|
|
end
|
2008-08-11 19:47:23 +02:00
|
|
|
end
|
|
|
|
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.
|
2008-08-11 19:47:23 +02:00
|
|
|
-- @return The new command 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.
|
2008-08-06 15:15:13 +02:00
|
|
|
-- @param args A table with optional arguments: fg_cursor, bg_cursor, prompt.
|
2008-06-25 09:03:29 +02:00
|
|
|
-- @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-13 20:13:20 +02:00
|
|
|
-- @param history_path Optional parameter: file path where the history should be saved, set nil to disable history
|
|
|
|
-- @param history_max Optional parameter: set the maximum entries in history file, 50 by default
|
2008-08-14 00:26:43 +02:00
|
|
|
-- @param done_callback Optional parameter: the callback function to always call without arguments, regardless of whether the prompt was cancelled.
|
|
|
|
function prompt.run(args, textbox, exe_callback, completion_callback, history_path, history_max, done_callback)
|
2008-08-06 15:15:13 +02:00
|
|
|
if not args then args = {} 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-08-11 19:47:23 +02:00
|
|
|
local prettyprompt = args.prompt or ""
|
2008-08-06 15:15:13 +02:00
|
|
|
local inv_col = args.fg_cursor or theme.fg_focus or "black"
|
|
|
|
local cur_col = args.bg_cursor or theme.bg_focus or "white"
|
2008-08-13 20:13:20 +02:00
|
|
|
|
|
|
|
prompt_history_check_load(history_path, history_max)
|
|
|
|
local history_index = prompt_history_items(history_path) + 1
|
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-08-11 19:47:23 +02:00
|
|
|
textbox.text = prettyprompt .. 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-08-14 00:26:43 +02:00
|
|
|
if done_callback then done_callback() end
|
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)
|
2008-08-14 00:26:43 +02:00
|
|
|
if done_callback then done_callback() end
|
2008-08-03 11:37:48 +02:00
|
|
|
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-08-13 20:13:20 +02:00
|
|
|
prompt_history_add(history_path, command)
|
2008-06-18 12:46:12 +02:00
|
|
|
exe_callback(command)
|
2008-08-14 00:26:43 +02:00
|
|
|
if done_callback then done_callback() end
|
2008-06-18 12:46:12 +02:00
|
|
|
return false
|
|
|
|
elseif key == "Escape" then
|
2008-06-27 20:24:30 +02:00
|
|
|
textbox.text = ""
|
2008-08-14 00:26:43 +02:00
|
|
|
if done_callback then done_callback() end
|
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
|
2008-08-11 19:47:23 +02:00
|
|
|
elseif key == "Up" then
|
2008-08-12 08:39:31 +02:00
|
|
|
if history_index > 1 then
|
|
|
|
history_index = history_index - 1
|
2008-08-11 19:47:23 +02:00
|
|
|
|
2008-08-13 20:13:20 +02:00
|
|
|
command = prompt.history[history_path].table[history_index]
|
2008-08-11 19:47:23 +02:00
|
|
|
cur_pos = #command + 2
|
|
|
|
end
|
|
|
|
elseif key == "Down" then
|
2008-08-13 20:13:20 +02:00
|
|
|
if history_index < prompt_history_items(history_path) then
|
2008-08-12 08:39:31 +02:00
|
|
|
history_index = history_index + 1
|
2008-08-11 19:47:23 +02:00
|
|
|
|
2008-08-13 20:13:20 +02:00
|
|
|
command = prompt.history[history_path].table[history_index]
|
|
|
|
cur_pos = #command + 2
|
|
|
|
elseif history_index == prompt_history_items(history_path) then
|
|
|
|
history_index = history_index + 1
|
2008-08-11 20:39:03 +02:00
|
|
|
|
2008-08-13 20:13:20 +02:00
|
|
|
command = ""
|
|
|
|
cur_pos = 1
|
2008-08-11 19:47:23 +02:00
|
|
|
end
|
2008-06-17 16:54:46 +02:00
|
|
|
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-08-11 19:47:23 +02:00
|
|
|
textbox.text = prettyprompt .. 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-08-04 16:21:12 +02:00
|
|
|
if text then
|
|
|
|
text = text:gsub("&", "&")
|
|
|
|
text = text:gsub("<", "<")
|
|
|
|
text = text:gsub(">", ">")
|
|
|
|
text = text:gsub("'", "'")
|
|
|
|
text = text:gsub("\"", """)
|
|
|
|
end
|
2008-06-10 18:25:03 +02:00
|
|
|
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-08-04 16:21:12 +02:00
|
|
|
if text then
|
|
|
|
text = text:gsub("&", "&")
|
|
|
|
text = text:gsub("<", "<")
|
|
|
|
text = text:gsub(">", ">")
|
|
|
|
text = text:gsub("'", "'")
|
|
|
|
text = text:gsub(""", "\"")
|
|
|
|
end
|
2008-06-11 15:49:57 +02:00
|
|
|
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.
|
2008-08-06 15:15:13 +02:00
|
|
|
-- @param args The arguments table.
|
|
|
|
-- bg_focus The background color for selected tag.
|
|
|
|
-- fg_focus The foreground color for selected tag.
|
|
|
|
-- bg_urgent The background color for urgent tags.
|
|
|
|
-- fg_urgent The foreground color for urgent tags.
|
2008-07-29 10:24:03 +02:00
|
|
|
-- @return A string to print.
|
2008-08-06 15:15:13 +02:00
|
|
|
function widget.taglist.label.all(t, args)
|
|
|
|
if not args then args = {} end
|
|
|
|
local fg_focus = args.fg_focus or theme.fg_focus
|
|
|
|
local bg_focus = args.bg_focus or theme.bg_focus
|
|
|
|
local fg_urgent = args.fg_urgent or theme.fg_urgent
|
|
|
|
local bg_urgent = args.bg_urgent or theme.bg_urgent
|
2008-08-13 22:48:41 +02:00
|
|
|
local taglist_squares = args.taglist_squares or theme.taglist_squares
|
2008-07-31 23:04:35 +02:00
|
|
|
local text
|
2008-07-29 17:48:39 +02:00
|
|
|
local background = ""
|
2008-08-12 12:08:20 +02:00
|
|
|
local sel = capi.client.focus
|
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
|
2008-08-13 17:49:57 +02:00
|
|
|
if sel and sel:tags()[t] then
|
2008-08-13 22:48:41 +02:00
|
|
|
if taglist_squares == nil or taglist_squares == true then
|
|
|
|
background = "resize=\"true\" image=\"@AWESOME_ICON_PATH@/taglist/squarefw.png\""
|
|
|
|
end
|
2008-08-01 10:26:13 +02:00
|
|
|
elseif bg_urgent and fg_urgent then
|
2008-08-13 17:55:10 +02:00
|
|
|
for k, c in pairs(t:clients()) do
|
2008-08-13 22:48:41 +02:00
|
|
|
if taglist_squares == nil or taglist_squares == true then
|
|
|
|
background = "resize=\"true\" image=\"@AWESOME_ICON_PATH@/taglist/squarew.png\""
|
|
|
|
end
|
2008-08-10 16:14:35 +02:00
|
|
|
if c.urgent then
|
2008-08-09 13:59:07 +02:00
|
|
|
bg_color = bg_urgent
|
|
|
|
fg_color = fg_urgent
|
|
|
|
break
|
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-08-11 03:26:31 +02:00
|
|
|
--- Return labels for a taglist widget with all *non empty* tags from screen.
|
|
|
|
-- It returns the tag name and set a special
|
|
|
|
-- foreground and background color for selected tags.
|
|
|
|
-- @param t The tag.
|
|
|
|
-- @param args The arguments table.
|
|
|
|
-- bg_focus The background color for selected tag.
|
|
|
|
-- fg_focus The foreground color for selected tag.
|
|
|
|
-- bg_urgent The background color for urgent tags.
|
|
|
|
-- fg_urgent The foreground color for urgent tags.
|
|
|
|
-- @return A string to print.
|
|
|
|
function widget.taglist.label.noempty(t, args)
|
2008-08-13 22:48:11 +02:00
|
|
|
if #t:clients() > 0 or t.selected then
|
2008-08-11 03:26:31 +02:00
|
|
|
if not args then args = {} end
|
|
|
|
local fg_focus = args.fg_focus or theme.fg_focus
|
|
|
|
local bg_focus = args.bg_focus or theme.bg_focus
|
|
|
|
local fg_urgent = args.fg_urgent or theme.fg_urgent
|
|
|
|
local bg_urgent = args.bg_urgent or theme.bg_urgent
|
|
|
|
local bg_color = nil
|
|
|
|
local fg_color = nil
|
|
|
|
local text
|
|
|
|
|
|
|
|
if t.selected then
|
|
|
|
bg_color = bg_focus
|
|
|
|
fg_color = fg_focus
|
|
|
|
end
|
|
|
|
if bg_urgent and fg_urgent then
|
2008-08-13 17:55:10 +02:00
|
|
|
for k, c in pairs(t:clients()) do
|
2008-08-11 03:26:31 +02:00
|
|
|
if c.urgent then
|
|
|
|
bg_color = bg_urgent
|
|
|
|
fg_color = fg_urgent
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if fg_color and bg_color then
|
|
|
|
text = "<bg color='" .. bg_color .. "'/> <span color='" .. fg_color .. "'>" .. escape(t.name) .. "</span> "
|
|
|
|
else
|
|
|
|
text = " " .. escape(t.name) .. " "
|
|
|
|
end
|
|
|
|
return text
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-08-06 15:15:13 +02:00
|
|
|
local function widget_tasklist_label_common(c, args)
|
|
|
|
if not args then args = {} end
|
|
|
|
local fg_focus = args.fg_focus or theme.fg_focus
|
|
|
|
local bg_focus = args.bg_focus or theme.bg_focus
|
|
|
|
local fg_urgent = args.fg_urgent or theme.fg_urgent
|
|
|
|
local bg_urgent = args.bg_urgent or theme.bg_urgent
|
2008-07-29 11:21:47 +02:00
|
|
|
local text = ""
|
2008-08-11 11:41:42 +02:00
|
|
|
local name
|
2008-07-29 11:21:47 +02:00
|
|
|
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-11 11:41:42 +02:00
|
|
|
if c.hidden then
|
2008-08-12 18:04:02 +02:00
|
|
|
name = escape(c.icon_name) or ""
|
2008-08-11 11:41:42 +02:00
|
|
|
else
|
2008-08-12 18:04:02 +02:00
|
|
|
name = escape(c.name) or ""
|
2008-08-11 11:41:42 +02:00
|
|
|
end
|
2008-08-12 12:08:20 +02:00
|
|
|
if capi.client.focus == c then
|
2008-08-11 11:41:42 +02:00
|
|
|
text = text .. " <bg color='"..bg_focus.."'/><span color='"..fg_focus.."'>"..name.."</span> "
|
2008-08-01 10:26:13 +02:00
|
|
|
elseif c.urgent and bg_urgent and fg_urgent then
|
2008-08-11 11:41:42 +02:00
|
|
|
text = text .. " <bg color='"..bg_urgent.."'/><span color='"..fg_urgent.."'>"..name.."</span> "
|
2008-07-29 11:21:47 +02:00
|
|
|
else
|
2008-08-11 11:41:42 +02:00
|
|
|
text = text .. " "..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.
|
2008-08-06 15:15:13 +02:00
|
|
|
-- @param args The arguments table.
|
|
|
|
-- bg_focus The background color for focused client.
|
|
|
|
-- fg_focus The foreground color for focused client.
|
|
|
|
-- bg_urgent The background color for urgent clients.
|
|
|
|
-- fg_urgent The foreground color for urgent clients.
|
|
|
|
-- @return A string to print.
|
|
|
|
function widget.tasklist.label.allscreen(c, screen, args)
|
|
|
|
return widget_tasklist_label_common(c, args)
|
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.
|
2008-08-06 15:15:13 +02:00
|
|
|
-- @param args The arguments table.
|
|
|
|
-- bg_focus The background color for focused client.
|
|
|
|
-- fg_focus The foreground color for focused client.
|
|
|
|
-- bg_urgent The background color for urgent clients.
|
|
|
|
-- fg_urgent The foreground color for urgent clients.
|
|
|
|
-- @return A string to print.
|
|
|
|
function widget.tasklist.label.alltags(c, screen, args)
|
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-06 15:15:13 +02:00
|
|
|
return widget_tasklist_label_common(c, args)
|
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.
|
2008-08-06 15:15:13 +02:00
|
|
|
-- @param args The arguments table.
|
|
|
|
-- bg_focus The background color for focused client.
|
|
|
|
-- fg_focus The foreground color for focused client.
|
|
|
|
-- bg_urgent The background color for urgent clients.
|
|
|
|
-- fg_urgent The foreground color for urgent clients.
|
|
|
|
-- @return A string to print.
|
|
|
|
function widget.tasklist.label.currenttags(c, screen, args)
|
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-13 18:12:26 +02:00
|
|
|
for k, t in ipairs(capi.screen[screen]:tags()) do
|
2008-08-13 17:49:57 +02:00
|
|
|
if t.selected and c:tags()[t] then
|
2008-08-06 15:15:13 +02:00
|
|
|
return widget_tasklist_label_common(c, args)
|
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-06 15:15:13 +02:00
|
|
|
if not args then args = {} end
|
2008-08-01 00:10:04 +02:00
|
|
|
-- Store colors
|
2008-08-04 14:31:53 +02:00
|
|
|
titlebar.data[c] = {}
|
2008-08-06 15:15:13 +02:00
|
|
|
titlebar.data[c].fg = args.fg or theme.fg_normal
|
|
|
|
titlebar.data[c].bg = args.bg or theme.bg_normal
|
|
|
|
titlebar.data[c].fg_focus = args.fg_focus or theme.fg_focus
|
|
|
|
titlebar.data[c].bg_focus = args.bg_focus or theme.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
|
|
|
local title = capi.widget({ type = "textbox", name = "title", align = "flex" })
|
2008-08-08 16:02:35 +02:00
|
|
|
title:mouse_add(capi.mouse({ }, 1, function (t) t.client:mouse_move() end))
|
|
|
|
title:mouse_add(capi.mouse({ args.modkey }, 3, function (t) t.client:mouse_resize() end))
|
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-08-08 16:02:35 +02:00
|
|
|
close_button:mouse_add(capi.mouse({ }, 1, function (t) t.client:kill() end))
|
2008-08-08 16:49:26 +02:00
|
|
|
|
|
|
|
tb.widgets =
|
|
|
|
{
|
|
|
|
capi.widget({ type = "appicon", name = "appicon", align = "left" }),
|
|
|
|
title,
|
|
|
|
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-08-08 16:49:26 +02:00
|
|
|
local widgets = c.titlebar.widgets
|
|
|
|
local title, close
|
|
|
|
for k, v in ipairs(widgets) do
|
|
|
|
if v.name == "title" then title = v end
|
|
|
|
if v.name == "close" then close = v end
|
|
|
|
if title and close then break end
|
|
|
|
end
|
|
|
|
if title then
|
|
|
|
title.text = " " .. escape(c.name)
|
2008-07-29 15:44:09 +02:00
|
|
|
end
|
2008-08-12 12:08:20 +02:00
|
|
|
if capi.client.focus == c then
|
2008-08-04 14:31:53 +02:00
|
|
|
c.titlebar.fg = titlebar.data[c].fg_focus
|
|
|
|
c.titlebar.bg = titlebar.data[c].bg_focus
|
2008-08-08 16:49:26 +02:00
|
|
|
if close then
|
|
|
|
close.text = "<bg image=\"@AWESOME_ICON_PATH@/titlebar/closer.png\" resize=\"true\"/>"
|
2008-07-29 15:44:09 +02:00
|
|
|
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-08-08 16:49:26 +02:00
|
|
|
if close then
|
|
|
|
close.text = "<bg image=\"@AWESOME_ICON_PATH@/titlebar/close.png\" resize=\"true\"/>"
|
2008-07-29 15:44:09 +02:00
|
|
|
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-08-06 15:15:13 +02:00
|
|
|
--- Set the beautiful theme if any.
|
|
|
|
-- @param The beautiful theme.
|
|
|
|
function beautiful.register(btheme)
|
|
|
|
if btheme then
|
|
|
|
theme = btheme
|
|
|
|
else
|
|
|
|
theme = {}
|
|
|
|
end
|
|
|
|
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-12 10:34:49 +02:00
|
|
|
hooks.unmanage.register(client_maximize_clean)
|
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)
|
2008-08-06 15:39:51 +02:00
|
|
|
|
|
|
|
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80
|