This repository has been archived by the owner on Jan 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
tagger.lua
301 lines (285 loc) · 8.16 KB
/
tagger.lua
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
-- Environment
local awful = require('awful')
local capi = {
tag = tag,
mouse = mouse,
screen = screen,
keygrabber = keygrabber,
client = client
}
local ipairs = ipairs
local pairs = pairs
local pcall = pcall
local print = print
local table = table
local string = string
module('tagger')
function string:split(delimiter)
local result = { }
local from = 1
local delim_from, delim_to = string.find( self, delimiter, from )
while delim_from do
table.insert( result, string.sub( self, from , delim_from-1 ) )
from = delim_to + 1
delim_from, delim_to = string.find( self, delimiter, from )
end
table.insert( result, string.sub( self, from ) )
return result
end
function string:find_any(patterns)
for _, p in pairs(patterns) do
if self:find(p) then
return true
end
end
return false
end
function tag2idx(tag) -- {{{
if not tag then return nil end
local tags = capi.screen[tag.screen]:tags()
for i, t in ipairs(tags) do
if t == tag then
return i
end
end
return nil
end
-- }}}
function name2idx(name, screen) -- {{{
screen = screen or capi.mouse.screen
local tags = capi.screen[screen]:tags()
for i, v in ipairs(tags) do
if v.name == name or v.name:match("^%d+/%d+:" .. name .. "$") then
return i
end
end
return nil
end
-- }}}
function apptag(name, props, client) -- {{{
local screen = client and client.screen or capi.mouse.screen
props = props or { }
props.name = props.name or name
local idx = name2idx(name, screen)
if not idx then
return add(screen, props)
end
return capi.screen[screen]:tags()[idx]
end
-- }}}
function add(scr, props) -- {{{
scr = scr or capi.mouse.screen
props = props or { }
local name = props.name or '.oO'
local tags = capi.screen[scr]:tags()
local t = capi.tag({ name = name })
local idx = tag2idx(awful.tag.selected())
idx = idx or 0
table.insert(tags, idx + 1, t)
t.screen = scr
capi.screen[scr]:tags(tags)
awful.tag.setproperty(t, "layout", props.layout or awful.layout.suit.tile)
awful.tag.setproperty(t, "mwfact", props.mwfact)
awful.tag.setproperty(t, "nmaster", props.nmaster)
awful.tag.setproperty(t, "ncol", props.ncol)
awful.tag.setproperty(t, "icon", props.icon)
if #(capi.screen[scr]:tags()) == 1 or props.switch then
awful.tag.viewonly(t)
end
update_names(scr)
return t
end
-- }}}
function clean(scr) -- {{{
local tags = capi.screen[scr]:tags()
local t2 = { }
for i, v in pairs(tags) do
if #(v:clients()) ~= 0 then
table.insert(t2, v)
else
if v.selected then
awful.tag.viewprev(capi.screen[v.screen])
end
end
end
if #t2 == 0 then
table.insert(t2, tags[1])
end
capi.screen[scr]:tags(t2)
end
-- }}}
function remove(scr, idx) -- {{{
scr = scr or capi.mouse.screen
idx = idx or tag2idx(awful.tag.selected(scr))
if not idx then return end
local t = capi.screen[scr]:tags()
if idx > #t or #t == 1 then return end
if #(t[idx]:clients()) ~= 0 then return end
if t[idx].selected then awful.tag.viewnext() end
table.remove(t, idx)
capi.screen[scr]:tags(t)
local ct = awful.tag.selected()
ct.selected = false
ct.selected = true
update_names(scr)
end
-- }}}
function update_names(scr) -- {{{
local t = capi.screen[scr]:tags()
for i, v in ipairs(t) do
if not v.name:match("^%d+/%d+:") then
v.name = i .. "/" .. #t .. ":" .. v.name
else
v.name = v.name:gsub("^(%d+/%d+):", i .. "/" .. #t ..":")
end
end
end
-- }}}
function rename(t) -- {{{
t = t or awful.tag.selected(capi.mouse.screen)
local name = t.name
t.name = t.name .. '_'
capi.keygrabber.run(function(mod, key, action)
if action ~= "press" then return true end
if key:len() == 1 and t.name:len() <= 20 then
t.name = t.name:sub(1, t.name:len() - 1) .. key .. '_'
elseif key == "BackSpace" and t.name:len() > 1 then
t.name = t.name:sub(1, t.name:len() - 2) .. '_'
elseif key == "Return" and t.name:len() > 1 then
t.name = t.name:sub(1, t.name:len() - 1)
update_names(t.screen)
return false
elseif key == "Escape" then
t.name = name
return false
end
return true
end)
end
-- }}}
local function move(t, idx) -- {{{
local tags = capi.screen[t.screen]:tags()
local idx_old = tag2idx(t)
local c = capi.client.focus
if not idx_old then return end
table.remove(tags, idx_old)
table.insert(tags, idx, t)
capi.screen[t.screen]:tags(tags)
update_names(t.screen)
capi.client.focus = c
end
-- }}}
local function moverel(t, off) -- {{{
local idx = tag2idx(t)
if not idx then return end
local idx_max = #(capi.screen[t.screen]:tags())
local idx_new = awful.util.cycle(idx_max, idx + off)
move(t, idx_new)
end
-- }}}
function moveleft(t) -- {{{
t = t or awful.tag.selected(capi.mouse.screen)
moverel(t, -1)
end
-- }}}
function moveright(t) -- {{{
t = t or awful.tag.selected(capi.mouse.screen)
moverel(t, 1)
end
-- }}}
local function movescreen(t, scr) -- {{{
if t.screen == scr or #(capi.screen[t.screen]:tags()) == 1 then return end
if t.selected then
awful.tag.viewnext(capi.screen[t.screen])
end
local clients = t:clients()
local oldscr = t.screen
local idx = tag2idx(awful.tag.selected(scr))
t.screen = scr
for i, v in pairs(clients) do
v.screen = scr
v:tags({ t })
end
local tags = capi.screen[scr]:tags()
local c = capi.client.focus
table.remove(tags)
table.insert(tags, idx, t)
capi.screen[scr]:tags(tags)
update_names(scr)
update_names(oldscr)
capi.client.focus = c
if not capi.client.focus or not capi.client.focus.visible then
capi.client.focus = awful.tag.selected(scr):clients()[1]
end
capi.mouse.screen = scr
awful.tag.viewonly(t)
end
-- }}}
local function movescreenrel(t, off) -- {{{
local idx = t.screen
local idx_max = capi.screen.count()
local idx_new = awful.util.cycle(idx_max, idx + off)
movescreen(t, idx_new)
end
-- }}}
function movescreenleft(t) -- {{{
t = t or awful.tag.selected(capi.mouse.screen)
movescreenrel(t, -1)
end
-- }}}
function movescreenright(t) -- {{{
t = t or awful.tag.selected(capi.mouse.screen)
movescreenrel(t, 1)
end
-- }}}
function match_names(scr, txtbox) -- {{{
local t = capi.screen[scr]:tags()
local txt = "_"
local txt_old = txtbox._text
local tag_old = awful.tag.selected(scr)
local apply_regex = function(r)
for i, v in ipairs(t) do
if v.name:find_any(r:split(" ")) then
awful.tag.setproperty(v, "hide", false)
else
awful.tag.setproperty(v, "hide", true)
end
end
end
apply_regex("")
capi.keygrabber.run(function(mod, key, action)
local rv = true
txtbox:set_text(txt)
if action ~= "press" then return true end
if key:len() == 1 and txt:len() <= 20 then
txt = txt:sub(1, txt:len() - 1) .. key .. "_"
elseif key == "BackSpace" and txt:len() > 1 then
txt = txt:sub(1, txt:len() - 2) .. "_"
elseif key == "Return" then
rv = false
elseif key == "Escape" then
txt = "_"
rv = false
end
local ok, r = pcall(function() apply_regex(txt:sub(1, txt:len() - 1)) end)
if not ok then
print(r)
end
if not rv then
local ok = pcall(function() txtbox:set_markup(txt_old) end)
if not ok then
txtbox:set_text(txt_old)
end
if awful.tag.getproperty(tag_old, "hide") then
for i, v in ipairs(t) do
if not awful.tag.getproperty(v, "hide") then
awful.tag.viewonly(v)
break
end
end
end
end
return rv
end)
end
-- }}}