no longer need the ixp.so symlink
[wmiirc-lua.git] / wmiirc.lua
blob8498d63434010fa223e3af9b38d6ba50a94c9cff
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 print ('-----------------------')
16 -- this is us
17 local wmiirc = os.getenv("HOME") .. "/.wmii-3.5/wmiirc"
19 -- debug
20 function my_log (str)
21 io.stderr:write (str .. "\n")
22 end
24 -- load wmii.lua
25 my_log("wmii: loading wmii.lua")
26 package.path = package.path .. ";" .. os.getenv("HOME") .. "/.wmii-3.5/?.lua"
27 require "wmii"
29 my_log("wmii: wmii.lua loaded")
31 -- stop any other instance of wmiirc
32 wmii.write ("/event", "Start wmiirc")
34 -- this is the base configuration
35 local config = {
36 xterm = 'x-terminal-emulator'
38 my_log("wmii: setting confg")
39 local wconfig = {
40 view = 1,
41 border = 1,
42 font = '-windows-proggytiny-medium-r-normal--10-80-96-96-c-60-iso8859-1',
43 focuscolors = '#FFFFaa #007700 #88ff88',
44 normcolors = '#888888 #222222 #333333',
45 grabmod = 'Mod1'
47 wmii.configure (wconfig)
49 my_log("wmii: config set")
51 wmii.write ("/colrules", "/.*/ -> 58+42")
52 wmii.write ("/tagrules", "/XMMS.*/ -> ~\n"
53 .. "/MPlayer.*/ -> ~\n"
54 .. "/.*/ -> sel\n"
55 .. "/.*/ -> 1\n")
58 -- ------------------------------------------------------------------------
59 -- action handlers
61 local action_handlers = {
62 quit = function ()
63 wmii.write ("/ctl", "quit")
64 end,
66 exec = function (act, args)
67 local what = args or wmiirc
68 wmii.write ("/ctl", "exec " .. what)
69 end,
71 wmiirc = function ()
72 posix.exec ("lua", wmiirc)
73 end,
75 rehash = function ()
76 -- TODO: consider storing list of executables around, and
77 -- this will then reinitialize that list
78 my_log (" TODO: rehash")
79 end,
81 status = function ()
82 -- TODO: this should eventually update something on the /rbar
83 my_log (" TODO: status")
84 end
87 -- ------------------------------------------------------------------------
88 -- key handlers
90 local key_handlers = {
91 ["*"] = function (key)
92 my_log ("*: " .. key)
93 end,
95 -- execution and actions
96 ["Mod1-Return"] = function (key)
97 my_log (" executing: " .. config.xterm)
98 os.execute (config.xterm .. " &")
99 end,
100 ["Mod1-a"] = function (key)
101 local text = wmii.menu (action_handlers)
102 if text then
103 local act = text
104 local args = nil
105 local si = text:find("%s")
106 if si then
107 act,args = string.match(text .. " ", "(%w+)%s(.+)")
109 if act then
110 local fn = action_handlers[act]
111 if fn then
112 fn (act,args)
116 end,
117 ["Mod1-p"] = function (key)
118 local prog = wmii.progmenu()
119 if prog then
120 my_log (" executing: " .. prog)
121 os.execute (prog .. " &")
123 end,
124 ["Mod1-Shift-c"] = function (key)
125 wmii.write ("/client/sel/ctl", "kill")
126 end,
128 -- HJKL active selection
129 ["Mod1-h"] = function (key)
130 wmii.write ("/tag/sel/ctl", "select left")
131 end,
132 ["Mod1-l"] = function (key)
133 wmii.write ("/tag/sel/ctl", "select right")
134 end,
135 ["Mod1-j"] = function (key)
136 wmii.write ("/tag/sel/ctl", "select down")
137 end,
138 ["Mod1-k"] = function (key)
139 wmii.write ("/tag/sel/ctl", "select up")
140 end,
142 -- HJKL movement
143 ["Mod1-Shift-h"] = function (key)
144 wmii.write ("/tag/sel/ctl", "send sel left")
145 end,
146 ["Mod1-Shift-l"] = function (key)
147 wmii.write ("/tag/sel/ctl", "send sel right")
148 end,
149 ["Mod1-Shift-j"] = function (key)
150 wmii.write ("/tag/sel/ctl", "send sel down")
151 end,
152 ["Mod1-Shift-k"] = function (key)
153 wmii.write ("/tag/sel/ctl", "send sel up")
154 end,
156 -- floating vs tiled
157 ["Mod1-space"] = function (key)
158 wmii.write ("/tag/sel/ctl", "select toggle")
159 end,
160 ["Mod1-Shift-space"] = function (key)
161 wmii.write ("/tag/sel/ctl", "send sel toggle")
162 end,
164 -- work spaces
165 ["Mod4-#"] = function (key, num)
166 wmii.write ("/ctl", "view " .. tostring(num))
167 end,
168 ["Mod4-Shift-#"] = function (key, num)
169 wmii.write ("/client/sel/tags", tostring(num))
170 end,
173 -- switching views and retagging
174 ["Mod1-t"] = function (key)
175 local tag = wmii.tagmenu()
176 if tag then
177 wmii.write ("/ctl", "view " .. tag)
180 end,
181 ["Mod1-Shift-t"] = function (key)
182 local tag = wmii.tagmenu()
183 if tag then
184 local cli = wmii.read ("/client/sel/ctl")
185 wmii.write ("/client/" .. cli .. "/tags", tag)
187 end,
188 ["Mod1-Control-t"] = function (key)
189 my_log (" TODO: Mod1-Control-t: " .. key)
190 end,
192 -- column modes
193 ["Mod1-d"] = function (key)
194 wmii.write("/tag/sel/ctl", "colmode sel default")
195 end,
196 ["Mod1-s"] = function (key)
197 wmii.write("/tag/sel/ctl", "colmode sel stack")
198 end,
199 ["Mod1-m"] = function (key)
200 wmii.write("/tag/sel/ctl", "colmode sel max")
204 -- ------------------------------------------------------------------------
205 -- update the /keys wmii file with the list of all handlers
208 local t = {}
209 local x, y
210 for x,y in pairs(key_handlers) do
211 if x:find("%w") then
212 local i = x:find("#")
213 if i then
214 local j
215 for j=0,9 do
216 t[#t + 1]
217 = x:sub(1,i-1) .. j
219 else
220 t[#t + 1]
221 = tostring(x)
225 local all_keys = table.concat(t, "\n")
226 my_log ("setting /keys to...\n" .. all_keys .. "\n");
227 wmii.write ("/keys", all_keys)
231 -- ------------------------------------------------------------------------
232 -- event handlers
234 local ev_handlers = {
235 ["*"] = function (ev, arg)
236 my_log ("ev: " .. ev .. " - " .. arg)
237 end,
239 -- exit if another wmiirc started up
240 Start = function (ev, arg)
241 if arg == "wmiirc" then
242 posix.exit (0)
244 end,
246 -- tag management
247 CreateTag = function (ev, arg)
248 wmii.create ("/lbar/" .. arg,
249 wconfig.normcolors .. " " .. arg)
250 end,
251 DestroyTag = function (ev, arg)
252 wmii.remove ("/lbar/" .. arg)
253 end,
255 FocusTag = function (ev, arg)
256 my_log ("FocusTag: " .. arg)
257 wmii.create ("/lbar/" .. arg,
258 wconfig.focuscolors .. " " .. arg)
259 wmii.write ("/lbar/" .. arg,
260 wconfig.focuscolors .. " " .. arg)
261 end,
262 UnfocusTag = function (ev, arg)
263 my_log ("UnfocusTag: " .. arg)
264 wmii.create ("/lbar/" .. arg,
265 wconfig.normcolors .. " " .. arg)
266 wmii.write ("/lbar/" .. arg,
267 wconfig.normcolors .. " " .. arg)
268 end,
270 -- key event handling
271 Key = function (ev, arg)
272 my_log ("Key: " .. arg)
273 local num = nil
274 -- can we find an exact match?
275 local fn = key_handlers[arg]
276 if not fn then
277 local key = arg:gsub("-%d+", "-#")
278 -- can we find a match with a # wild card for the number
279 fn = key_handlers[key]
280 if fn then
281 -- convert the trailing number to a number
282 num = tonumber(arg:match("-(%d+)"))
283 else
284 -- everything else failed, try default match
285 fn = key_handlers["*"]
288 if fn then
289 fn (arg, num)
291 end,
293 -- mouse handling
294 ClientMouseDown = function (ev, arg)
295 my_log ("ClientMouseDown: " .. arg)
296 end,
297 LeftBarClick = function (ev, arg)
298 my_log ("LeftBarClick: " .. arg)
299 -- wmiir xwrite /ctl view "$@"
300 end,
302 -- urgent tag?
303 UrgentTag = function (ev, arg)
304 my_log ("UrgentTag: " .. arg)
305 -- wmiir xwrite "/lbar/$@" "*$@"
306 end,
307 NotUrgentTag = function (ev, arg)
308 my_log ("NotUrgentTag: " .. arg)
309 -- wmiir xwrite "/lbar/$@" "$@"
314 -- ------------------------------------------------------------------------
315 -- reading events
316 my_log("wmii: starting event loop")
317 local ev, arg
318 for ev, arg in wmii.ievents() do
320 local fn = ev_handlers[ev] or ev_handlers["*"]
321 if fn then
322 fn (ev, arg)
325 my_log("wmii: event loop exited")