update config for awesome 3.4
[~madcoder/dotfiles.git] / config / awesome / rc.lua
1 -- awesome 3 configuration file
2
3 -- Include awesome library, with lots of useful function!
4 require("awful")
5 require("eminent")
6 require("vicious")
7 require("beautiful")
8
9 terminal = "x-terminal-emulator"
10 lock     = 'xscreensaver-command -lock'
11 beautiful.init(awful.util.getdir("config").."/theme")
12
13 -- {{{ Modkeys
14 modkey = "Mod4"
15 shift = "Shift"
16 alt = "Mod1"
17 control = "Control"
18
19 k_n  = {}
20 k_m  = {modkey}
21 k_ms = {modkey, shift}
22 k_ma = {modkey, alt}
23 k_mc = {modkey, control}
24 k_a  = {alt}
25 k_ac = {alt, control}
26 k_c  = {control}
27 k_cs = {control, shift}
28 k_s  = {shift}
29
30 -- }}}
31
32 -- {{{ Markup helper functions
33 -- Inline markup is a tad ugly, so use these functions
34 -- to dynamically create markup.
35 function fg(color, text)
36     return '<span color="'..color..'">'..text..'</span>'
37 end
38
39 function heading(text)
40     return fg(beautiful.fg_focus, text)
41 end
42
43 -- }}}
44 -- {{{ Functions
45 -- Toggle whether we're viewing a tag
46 function tag_toggleview(tag)
47     tag:view(not tag:isselected())
48 end
49
50 -- Get the screen number we're on
51 function getscreen()
52     local sel = client.focus
53     return (sel and sel.screen) or mouse.screen
54 end
55
56 -- Move current client to a specific screen
57 function client_movetoscreen(i)
58     client.focus.screen = i
59 end
60
61 -- }}}
62 -- {{{ Set tag names
63
64 for s = 1, screen.count() do
65     eminent.newtag(s, 5)
66     for i = 1, 10 do
67         eminent.tag.name(i, s, 'work-'..i)
68     end
69 end
70
71 -- }}}
72 -- {{{ Taglist
73
74 maintaglist = {}
75 maintaglist.buttons = awful.util.table.join(
76     awful.button(k_n, 1, awful.tag.viewonly),
77     awful.button(k_s, 1, awful.client.toggletag)
78 )
79
80 -- }}}
81 -- {{{ Widgets
82
83 -- {{{ Load Average Widget
84
85 loadwidget = widget({ type = 'textbox' })
86 load_update = function()
87     -- Use /proc/loadavg to get the average system load on 1, 5 and 15 minute intervals
88     local f = io.open('/proc/loadavg')
89     local n = f:read()
90     f:close()
91
92     -- Find the third space
93     local pos = n:find(' ', n:find(' ', n:find(' ')+1)+1)
94
95     loadwidget.text = heading('Load')..': '..n:sub(1,pos-1)
96 end
97 load_update()
98
99 t = timer { timeout = 10 }
100 t:add_signal("timeout", load_update)
101 t:start()
102
103 -- }}}
104 -- {{{ CPU Usage Widget
105
106 cputextwidget = widget({ type = 'textbox' })
107 cpugraphwidget = awful.widget.graph{
108     width  = 40, height = 16,
109     layout = awful.widget.layout.horizontal.rightleft
110 }
111 cpugraphwidget:set_background_color('#333333')
112 cpugraphwidget:set_border_color('#0a0a0a')
113 cpugraphwidget:set_gradient_colors({ '#285577', '#285577', '#AEC6D8' })
114
115 vicious.register(cputextwidget, vicious.widgets.cpu,
116 function (widget, args)
117     local r = tonumber(args[1])
118     local percent = args[1]..'%'
119     if r < 10 then
120         percent = '0'..percent
121     end
122     if r < 25 then
123         percent = fg('green', percent)
124     elseif r < 50 then
125         percent = fg('yellow', percent)
126     elseif r < 75 then
127         percent = fg('orange', percent)
128     else
129         percent = fg('red', percent)
130     end
131     cpugraphwidget:add_value(r / 100)
132     return heading('CPU')..': '..percent..' '
133 end, 2)
134
135 -- }}}
136 -- {{{ CPU Graph Widget
137
138 -- }}}
139 -- {{{ Memory Usage Widget
140
141 memtextwidget = widget({ type = 'textbox' })
142
143 memtextwidget.text = heading('MEM')..': '
144 vicious.register(memtextwidget, vicious.widgets.mem, function (widget, args)
145     -- Add extra preceding zeroes when needed
146     local r = tonumber(args[1])
147     local percent = args[1]..'%'
148     if r < 10 then
149         percent = '0'..percent
150     end
151     if r < 50 then
152         percent = fg('green', percent)
153     elseif r < 80 then
154         percent = fg('orange', percent)
155     else
156         percent = fg('red', percent)
157     end
158     return heading('MEM')..': '..percent..' '..args[2]..'M'
159 end, 2)
160
161 -- }}}
162 -- {{{ spacers
163
164 rspacer = widget({ type = 'textbox' })
165 rspacer.text = " │ "
166
167 -- }}}
168 -- {{{ Clock
169
170 clockwidget = widget({ type = "textbox" })
171 vicious.register(clockwidget, vicious.widgets.date, fg("#dddddd", "%a %d %b - %H:%M"), 10)
172
173 -- }}}
174
175 mymenubox = widget{ type = "textbox" }
176
177 -- {{{ Statusbar
178
179 mainstatusbar = {}
180
181 for s = 1, screen.count() do
182     mainstatusbar[s] = awful.wibox{ position = "top", height = 18, screen = s }
183
184     mainstatusbar[s].widgets = {
185         {
186             awful.widget.taglist(s, awful.widget.taglist.label.noempty, maintaglist.buttons),
187             maintaglist,
188             awful.widget.prompt({ layout = awful.widget.layout.horizontal.leftright }),
189             layout = awful.widget.layout.horizontal.leftright
190         },
191         clockwidget, rspacer,
192         memtextwidget, rspacer,
193         cpugraphwidget, cputextwidget, rspacer,
194         loadwidget, rspacer,
195         layout = awful.widget.layout.horizontal.rightleft
196     }
197 end
198
199 -- }}}
200 -- }}}
201
202 -- {{{ Keys
203 ---- {{{ Global keys
204
205 local hist = os.getenv("HOME") .. "/.cache/awesome/history"
206 globalkeys = awful.util.table.join(
207     -- Mod+{A/S}: Switch to prev/next tag
208     awful.key(k_m, "Left",  eminent.tag.prev),
209     awful.key(k_m, "Right", eminent.tag.next),
210
211     -- Mod+Shift+{A/S}: Move window to Prev/Next tag
212     awful.key(k_ms, "Left", function()
213         awful.client.movetotag(eminent.tag.getprev())
214         eminent.tag.prev()
215     end),
216     awful.key(k_ms, "Right", function()
217         awful.client.movetotag(eminent.tag.getnext())
218         eminent.tag.next()
219     end),
220
221     -- Mod+Shift_{E/D}: move window to next/prev screen
222     awful.key(k_mc, "Right", function()
223        local s = getscreen() + 1
224        while s > screen.count() do
225            s = s-screen.count()
226        end
227        client_movetoscreen(s)
228     end),
229     awful.key(k_mc, "Left", function()
230        local s = getscreen() - 1
231        while s < 1 do
232            s = s+screen.count()
233        end
234        client_movetoscreen(s)
235     end),
236
237
238     -- Focus Prev/Next window
239     awful.key(k_m, "j",
240         function ()
241             awful.client.focus.byidx(1)
242             if client.focus then client.focus:raise() end
243         end),
244     awful.key(k_m, "k",
245         function ()
246             awful.client.focus.byidx(-1)
247             if client.focus then client.focus:raise() end
248         end),
249
250     -- Swap window with the Prev/Next one
251     awful.key(k_ms, "j", function () awful.client.swap.byidx(1) end),
252     awful.key(k_ms, "k", function () awful.client.swap.byidx(-1) end),
253
254     -- Mod+{E/D}: Switch to next/previous screen
255     awful.key(k_m, "Tab",  function () awful.screen.focus(1) end),
256     awful.key(k_ms, "Tab", function () awful.screen.focus(-1) end),
257
258     -- Mod+Enter: Launch a new terminal
259     awful.key(k_m,  "e",      function() awful.util.spawn("firefox") end),
260     awful.key(k_m,  "Return", function() awful.util.spawn(terminal) end),
261     awful.key(k_ac, "r", awesome.restart),
262     awful.key(k_m, "F12", function() awful.util.spawn(lock) end),
263     awful.key({}, "#148", function() awful.util.spawn("kcalc") end),
264
265     -- Layout manipulation
266     awful.key(k_m,  "l", function () awful.tag.incmwfact(0.05) end),
267     awful.key(k_m,  "h", function () awful.tag.incmwfact(-0.05) end),
268     awful.key(k_ms, "h", function () awful.tag.incnmaster(1) end),
269     awful.key(k_ms, "l", function () awful.tag.incnmaster(-1) end),
270     awful.key(k_mc, "h", function () awful.tag.incncol(1) end),
271     awful.key(k_mc, "l", function () awful.tag.incncol(-1) end),
272
273     -- Menu
274     awful.key(k_m, "r",
275         function ()
276             awful.prompt.run({ prompt = "Run: " },
277                              mymenubox,
278                              awful.util.spawn,
279                              awful.completion.shell,
280                              awful.util.getdir("cache").."/commands")
281         end),
282     awful.key(k_m, "F4",
283         function ()
284             awful.prompt.run({ prompt = "Run Lua code: " },
285                              mymenubox,
286                              awful.util.eval,
287                              awful.prompt.shell,
288                              awful.util.getdir("cache").."/lua_commands")
289     end),
290
291     awful.key({}, "#192", function() eminent.tag.goto(1, nil, true) end),
292     awful.key({}, "#193", function() eminent.tag.goto(2, nil, true) end),
293     awful.key({}, "#194", function() eminent.tag.goto(3, nil, true) end),
294     awful.key({}, "#195", function() eminent.tag.goto(4, nil, true) end),
295     awful.key({}, "#196", function() eminent.tag.goto(5, nil, true) end)
296 )
297
298 -- Mod+#: Switch to tag
299 -- Mod+Shift+#: Toggle tag display
300 -- Mod+Control+#: Move client to tag
301 -- Mod+Alt+#: Toggle client on tag
302
303 for i = 1, 10 do
304     globalkeys = awful.util.table.join(
305         globalkeys,
306         awful.key(k_m, i % 10,
307             function()
308                 eminent.tag.goto(i, nil, true)
309             end),
310
311         awful.key(k_ms, i % 10,
312             function ()
313                 local t = eminent.tag.getn(i, nil, true)
314                 if t ~= nil then t.selected = not t.selected end
315             end),
316         awful.key(k_mc, i % 10,
317             function ()
318                 local t = eminent.tag.getn(i, nil, true)
319                 if t ~= nil then awful.client.movetotag(t) end
320             end)
321     )
322 end
323
324 ---- }}}
325 ---- {{{ Client hotkeys
326
327 clientkeys = awful.util.table.join(
328     awful.key(k_m, "i", function (c)
329         if mymenubox.text then
330             mymenubox.text = ""
331         else
332             mymenubox.text = "Class: " .. c.class .. " Instance: ".. c.instance
333         end
334     end),
335
336     -- Client manipulation
337     awful.key(k_m, "c", function (c) c:kill() end),
338     awful.key(k_m, "o", awful.client.floating.toggle),
339     awful.key(k_m, "t", awful.client.togglemarked),
340     awful.key(k_m, "F11", function (c) c.fullscreen = not c.fullscreen end)
341 )
342
343 ---- }}}
344
345 root.keys(globalkeys)
346 -- }}}
347 -- {{{ signals
348
349 client.add_signal("focus", function (c)
350     if not awful.client.ismarked(c) then
351         c.border_color = beautiful.border_focus
352     end
353 end)
354
355 client.add_signal("unfocus", function (c)
356     if not awful.client.ismarked(c) then
357         c.border_color = beautiful.border_normal
358     end
359 end)
360
361 client.add_signal("marked", function (c)
362     c.border_color = beautiful.border_marked
363 end)
364
365 client.add_signal("unmarked", function (c)
366     c.border_color = beautiful.border_focus
367 end)
368
369 client.add_signal("manage", function (c, startup)
370     if not startup and awful.client.focus.filter(c) then
371         c.screen = mouse.screen
372     end
373
374     -- Add mouse bindings
375     c:buttons(awful.util.table.join(
376         awful.button({ }, 1, function (c) client.focus = c; c:raise() end),
377         awful.button(k_a, 1, awful.mouse.client.move),
378         awful.button(k_a, 3, awful.mouse.client.resize)
379     ))
380
381     -- Enable sloppy focus
382     c:add_signal("mouse::enter", function(c)
383         client.focus = c
384     end)
385
386     -- Create border
387     c.border_width = beautiful.border_width
388     c.border_color = beautiful.border_normal
389
390     -- Make certain windows floating
391     local class = c.class:lower()
392     if class:find('pinentry')
393     or class:find('kcalc')
394     or class:find('gajim')
395     then
396         c.floating = true
397     end
398
399     -- Focus new clients
400     client.focus = c
401     c:keys(clientkeys)
402     c.size_hints_honor = false
403 end)
404
405 -- Hook function to execute when arranging the screen
406 -- (tag switch, new client, etc)
407 client.add_signal("arrange", function (screen)
408     local sel = client.focus
409
410     if not sel then
411         sel = awful.client.focus.history.get(screen, 0)
412         if not sel then return end
413         client.focus = sel
414     end
415
416     local o = mouse.object_under_pointer()
417     if not o or (type(o) == "client" and o ~= sel) then
418         local g = sel:geometry()
419
420         mouse.coords { x = g.x + 5, y = g.y + 5 }
421     end
422 end)
423
424 -- }}}
425
426 awful.util.spawn("xkbcomp -w 0 -R/usr/share/X11/xkb /home/madcoder/.Xkeyboard :0")