Repository navigation
Expand file tree
/
Copy pathhooks.lua.in
More file actions
125 lines (116 loc) · 3.81 KB
/
Copy pathhooks.lua.in
File metadata and controls
125 lines (116 loc) · 3.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
---------------------------------------------------------------------------
-- @author Julien Danjou <julien@danjou.info>
-- @copyright 2008 Julien Danjou
-- @release @AWESOME_VERSION@
---------------------------------------------------------------------------
-- Grab environment we need
local pairs = pairs
local table = table
local ipairs = ipairs
local type = type
local math = math
local capi =
{
hooks = hooks
}
--- Hooks module for awful
module("awful.hooks")
-- User hook functions
user = {}
--- Create a new userhook (for external libs).
-- @param name Hook name.
function user.create(name)
_M[name] = {}
_M[name].callbacks = {}
_M[name].register = function (f)
table.insert(_M[name].callbacks, f)
end
_M[name].unregister = function (f)
for k, h in ipairs(_M[name].callbacks) do
if h == f then
table.remove(_M[name].callbacks, k)
break
end
end
end
end
--- Call a created userhook (for external libs).
-- @param name Hook name.
function user.call(name, ...)
for name, callback in pairs(_M[name].callbacks) do
callback(...)
end
end
-- Autodeclare awful.hooks.* functions
-- mapped to awesome hooks.* functions
for name, hook in pairs(capi.hooks) do
if name ~= 'timer' then
_M[name] = {}
_M[name].register = function (f)
if not _M[name].callbacks then
_M[name].callbacks = {}
hook(function (...)
for i, callback in ipairs(_M[name].callbacks) do
callback(...)
end
end)
end
table.insert(_M[name].callbacks, f)
end
_M[name].unregister = function (f)
if _M[name].callbacks then
for k, h in ipairs(_M[name].callbacks) do
if h == f then
table.remove(_M[name].callbacks, k)
break
end
end
end
end
else
_M[name] = {}
_M[name].register = function (time, f, runnow)
if type(time) ~= 'number' or type(f) ~= 'function' or time <= 0 then
return
end
local new_timer
if _M[name].timer then
-- Take the smallest between current and new
new_timer = math.min(time, _M[name].timer)
else
new_timer = time
end
if not _M[name].callbacks then
_M[name].callbacks = {}
end
if _M[name].timer ~= new_timer then
_M[name].timer = new_timer
hook(_M[name].timer, function (...)
for i, callback in ipairs(_M[name].callbacks) do
callback['counter'] = callback['counter'] + _M[name].timer
if callback['counter'] >= callback['timer'] then
callback['callback'](...)
callback['counter'] = 0
end
end
end)
end
if runnow then
table.insert(_M[name].callbacks, { callback = f, timer = time, counter = time })
else
table.insert(_M[name].callbacks, { callback = f, timer = time, counter = 0 })
end
end
_M[name].unregister = function (f)
if _M[name].callbacks then
for k, h in ipairs(_M[name].callbacks) do
if h.callback == f then
table.remove(_M[name].callbacks, k)
break
end
end
end
end
end
end
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80