this creates the /lbar nodes
[wmiirc-lua.git] / wmiirc.lua
blob319e42546342a1f0d4db275039eefd038eaa5b5a
1 #!/usr/bin/env lua
2 --
3 -- Copyrigh (c) 2007, Bart Trojanowski <bart@jukie.net>
4 --
5 -- Some stuff below will eventually go to a separate file, and configuration
6 -- will remain here similar to the split between the wmii+ruby wmiirc and
7 -- wmiirc-config. For now I just want to get the feel of how things will
8 -- work in lua.
9 --
10 -- git://www.jukie.net/wmiirc-lua.git/
12 require "posix"
14 -- this is us
15 local wmiirc = os.getenv("HOME") .. "/.wmii-3.5/wmiirc"
17 -- debug
18 function my_log (str)
19 io.stderr:write (str .. "\n")
20 end
22 -- load wmii.lua
23 my_log("wmii: loading wmii.lua")
24 package.path = os.getenv("HOME") .. '/.wmii-3.5/?.lua;' .. package.path
25 require "wmii"
27 my_log("wmii: wmii.lua loaded")
29 -- stop any other instance of wmiirc
30 wmii.write ("/event", "Start wmiirc")
32 -- this is the base configuration
33 local config = {
34 xterm = 'x-terminal-emulator'
36 my_log("wmii: setting confg")
37 local wconfig = {
38 view = 1,
39 border = 1,
40 font = '-windows-proggytiny-medium-r-normal--10-80-96-96-c-60-iso8859-1',
41 focuscolors = '#FFFFaa #007700 #88ff88',
42 normcolors = '#888888 #222222 #333333',
43 grabmod = 'Mod1'
45 wmii.configure (wconfig)
47 my_log("wmii: config set")
49 wmii.write ("/colrules", "/.*/ -> 58+42")
50 wmii.write ("/tagrules", "/XMMS.*/ -> ~\n"
51 .. "/MPlayer.*/ -> ~\n"
52 .. "/.*/ -> sel\n"
53 .. "/.*/ -> 1\n")
56 -- ------------------------------------------------------------------------
57 -- action handlers
59 local action_handlers = {
60 quit = function ()
61 wmii.write ("/ctl", "quit")
62 end,
64 exec = function (act, args)
65 local what = args or wmiirc
66 wmii.write ("/ctl", "exec " .. what)
67 end,
69 wmiirc = function ()
70 posix.exec ("lua", wmiirc)
71 end,
73 rehash = function ()
74 -- TODO: consider storing list of executables around, and
75 -- this will then reinitialize that list
76 my_log (" TODO: rehash")
77 end,
79 status = function ()
80 -- TODO: this should eventually update something on the /rbar
81 my_log (" TODO: status")
82 end
85 -- ------------------------------------------------------------------------
86 -- key handlers
88 local key_handlers = {
89 ["*"] = function (key)
90 my_log ("*: " .. key)
91 end,
93 -- execution and actions
94 ["Mod1-Return"] = function (key)
95 my_log (" executing: " .. config.xterm)
96 os.execute (config.xterm .. " &")
97 end,
98 ["Mod1-a"] = function (key)
99 local text = wmii.menu (action_handlers)
100 if text then
101 local act = text
102 local args = nil
103 local si = text:find("%s")
104 if si then
105 act,args = string.match(text .. " ", "(%w+)%s(.+)")
107 if act then
108 local fn = action_handlers[act]
109 if fn then
110 fn (act,args)
114 end,
115 ["Mod1-p"] = function (key)
116 local prog = wmii.progmenu()
117 if prog then
118 my_log (" executing: " .. prog)
119 os.execute (prog .. " &")
121 end,
122 ["Mod1-Shift-c"] = function (key)
123 wmii.write ("/client/sel/ctl", "kill")
124 end,
126 -- HJKL active selection
127 ["Mod1-h"] = function (key)
128 wmii.write ("/tag/sel/ctl", "select left")
129 end,
130 ["Mod1-l"] = function (key)
131 wmii.write ("/tag/sel/ctl", "select right")
132 end,
133 ["Mod1-j"] = function (key)
134 wmii.write ("/tag/sel/ctl", "select down")
135 end,
136 ["Mod1-k"] = function (key)
137 wmii.write ("/tag/sel/ctl", "select up")
138 end,
140 -- HJKL movement
141 ["Mod1-Shift-h"] = function (key)
142 wmii.write ("/tag/sel/ctl", "send sel left")
143 end,
144 ["Mod1-Shift-l"] = function (key)
145 wmii.write ("/tag/sel/ctl", "send sel right")
146 end,
147 ["Mod1-Shift-j"] = function (key)
148 wmii.write ("/tag/sel/ctl", "send sel down")
149 end,
150 ["Mod1-Shift-k"] = function (key)
151 wmii.write ("/tag/sel/ctl", "send sel up")
152 end,
154 -- floating vs tiled
155 ["Mod1-space"] = function (key)
156 wmii.write ("/tag/sel/ctl", "select toggle")
157 end,
158 ["Mod1-Shift-space"] = function (key)
159 wmii.write ("/tag/sel/ctl", "send sel toggle")
160 end,
162 -- work spaces
163 ["Mod4-#"] = function (key, num)
164 wmii.write ("/ctl", "view " .. tostring(num))
165 end,
166 ["Mod4-Shift-#"] = function (key, num)
167 wmii.write ("/client/sel/tags", tostring(num))
168 end,
171 -- switching views and retagging
172 ["Mod1-t"] = function (key)
173 local tag = wmii.tagmenu()
174 if tag then
175 wmii.write ("/ctl", "view " .. tag)
178 end,
179 ["Mod1-Shift-t"] = function (key)
180 local tag = wmii.tagmenu()
181 if tag then
182 local cli = wmii.read ("/client/sel/ctl")
183 wmii.write ("/client/" .. cli .. "/tags", tag)
185 end,
186 ["Mod1-Control-t"] = function (key)
187 my_log (" TODO: Mod1-Control-t: " .. key)
188 end,
190 -- column modes
191 ["Mod1-d"] = function (key)
192 wmii.write("/tag/sel/ctl", "colmode sel default")
193 end,
194 ["Mod1-s"] = function (key)
195 wmii.write("/tag/sel/ctl", "colmode sel stack")
196 end,
197 ["Mod1-m"] = function (key)
198 wmii.write("/tag/sel/ctl", "colmode sel max")
202 -- ------------------------------------------------------------------------
203 -- update the /keys wmii file with the list of all handlers
206 local t = {}
207 local x, y
208 for x,y in pairs(key_handlers) do
209 if x:find("%w") then
210 local i = x:find("#")
211 if i then
212 local j
213 for j=0,9 do
214 t[#t + 1]
215 = x:sub(1,i-1) .. j
217 else
218 t[#t + 1]
219 = tostring(x)
223 local all_keys = table.concat(t, "\n")
224 my_log ("setting /keys to...\n" .. all_keys .. "\n");
225 wmii.write ("/keys", all_keys)
229 -- ------------------------------------------------------------------------
230 -- event handlers
232 local ev_handlers = {
233 ["*"] = function (ev, arg)
234 my_log ("ev: " .. ev .. " - " .. arg)
235 end,
237 -- exit if another wmiirc started up
238 Start = function (ev, arg)
239 if arg == "wmiirc" then
240 posix.exit (0)
242 end,
244 -- tag management
245 CreateTag = function (ev, arg)
246 wmii.create ("/lbar/" .. arg,
247 wconfig.normcolors .. " " .. arg)
248 end,
249 DestroyTag = function (ev, arg)
250 wmii.remove ("/lbar/" .. arg)
251 end,
253 FocusTag = function (ev, arg)
254 my_log ("FocusTag: " .. arg)
255 wmii.create ("/lbar/" .. arg,
256 wconfig.focuscolors .. " " .. arg)
257 wmii.write ("/lbar/" .. arg,
258 wconfig.focuscolors .. " " .. arg)
259 end,
260 UnfocusTag = function (ev, arg)
261 my_log ("UnfocusTag: " .. arg)
262 wmii.create ("/lbar/" .. arg,
263 wconfig.normcolors .. " " .. arg)
264 wmii.write ("/lbar/" .. arg,
265 wconfig.normcolors .. " " .. arg)
266 end,
268 -- key event handling
269 Key = function (ev, arg)
270 my_log ("Key: " .. arg)
271 local num = nil
272 -- can we find an exact match?
273 local fn = key_handlers[arg]
274 if not fn then
275 local key = arg:gsub("-%d+", "-#")
276 -- can we find a match with a # wild card for the number
277 fn = key_handlers[key]
278 if fn then
279 -- convert the trailing number to a number
280 num = tonumber(arg:match("-(%d+)"))
281 else
282 -- everything else failed, try default match
283 fn = key_handlers["*"]
286 if fn then
287 fn (arg, num)
289 end,
291 -- mouse handling
292 ClientMouseDown = function (ev, arg)
293 my_log ("ClientMouseDown: " .. arg)
294 end,
295 LeftBarClick = function (ev, arg)
296 my_log ("LeftBarClick: " .. arg)
297 -- wmiir xwrite /ctl view "$@"
298 end,
300 -- urgent tag?
301 UrgentTag = function (ev, arg)
302 my_log ("UrgentTag: " .. arg)
303 -- wmiir xwrite "/lbar/$@" "*$@"
304 end,
305 NotUrgentTag = function (ev, arg)
306 my_log ("NotUrgentTag: " .. arg)
307 -- wmiir xwrite "/lbar/$@" "$@"
312 -- ------------------------------------------------------------------------
313 -- reading events
314 my_log("wmii: starting event loop")
315 local ev, arg
316 for ev, arg in wmii.ievents() do
318 local fn = ev_handlers[ev] or ev_handlers["*"]
319 if fn then
320 fn (ev, arg)
323 my_log("wmii: event loop exited")