garbage collection for wmii.iread() userdata
[wmiirc-lua.git] / wmiirc.lua
blob05e6b196a102e7c26a78f04d99ba89a9a248a64d
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 = './.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 wmii.configure ({
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'
46 my_log("wmii: config set")
48 wmii.write ("/colrules", "/.*/ -> 58+42")
49 wmii.write ("/tagrules", "/XMMS.*/ -> ~\n"
50 .. "/MPlayer.*/ -> ~\n"
51 .. "/.*/ -> sel\n"
52 .. "/.*/ -> 1\n")
55 -- ------------------------------------------------------------------------
56 -- action handlers
58 local action_handlers = {
59 quit = function ()
60 wmii.write ("/ctl", "quit")
61 end,
63 exec = function (act, args)
64 local what = args or wmiirc
65 wmii.write ("/ctl", "exec " .. what)
66 end,
68 wmiirc = function ()
69 posix.exec ("lua", wmiirc)
70 end,
72 rehash = function ()
73 -- TODO: consider storing list of executables around, and
74 -- this will then reinitialize that list
75 my_log (" TODO: rehash")
76 end,
78 status = function ()
79 -- TODO: this should eventually update something on the /rbar
80 my_log (" TODO: status")
81 end
84 -- ------------------------------------------------------------------------
85 -- key handlers
87 local key_handlers = {
88 ["*"] = function (key)
89 my_log ("*: " .. key)
90 end,
92 -- execution and actions
93 ["Mod1-Return"] = function (key)
94 my_log (" executing: " .. config.xterm)
95 os.execute (config.xterm .. " &")
96 end,
97 ["Mod1-a"] = function (key)
98 local text = wmii.menu (action_handlers)
99 if text then
100 local act = text
101 local args = nil
102 local si = text:find("%s")
103 if si then
104 act,args = string.match(text .. " ", "(%w+)%s(.+)")
106 if act then
107 local fn = action_handlers[act]
108 if fn then
109 fn (act,args)
113 end,
114 ["Mod1-p"] = function (key)
115 local prog = wmii.progmenu()
116 if prog then
117 my_log (" executing: " .. prog)
118 os.execute (prog .. " &")
120 end,
121 ["Mod1-Shift-c"] = function (key)
122 wmii.write ("/client/sel/ctl", "kill")
123 end,
125 -- HJKL active selection
126 ["Mod1-h"] = function (key)
127 wmii.write ("/tag/sel/ctl", "select left")
128 end,
129 ["Mod1-l"] = function (key)
130 wmii.write ("/tag/sel/ctl", "select right")
131 end,
132 ["Mod1-j"] = function (key)
133 wmii.write ("/tag/sel/ctl", "select down")
134 end,
135 ["Mod1-k"] = function (key)
136 wmii.write ("/tag/sel/ctl", "select up")
137 end,
139 -- HJKL movement
140 ["Mod1-Shift-h"] = function (key)
141 wmii.write ("/tag/sel/ctl", "send sel left")
142 end,
143 ["Mod1-Shift-l"] = function (key)
144 wmii.write ("/tag/sel/ctl", "send sel right")
145 end,
146 ["Mod1-Shift-j"] = function (key)
147 wmii.write ("/tag/sel/ctl", "send sel down")
148 end,
149 ["Mod1-Shift-k"] = function (key)
150 wmii.write ("/tag/sel/ctl", "send sel up")
151 end,
153 -- floating vs tiled
154 ["Mod1-space"] = function (key)
155 wmii.write ("/tag/sel/ctl", "select toggle")
156 end,
157 ["Mod1-Shift-space"] = function (key)
158 wmii.write ("/tag/sel/ctl", "send sel toggle")
159 end,
161 -- work spaces
162 ["Mod4-#"] = function (key, num)
163 wmii.write ("/ctl", "view " .. tostring(num))
164 end,
165 ["Mod4-Shift-#"] = function (key, num)
166 wmii.write ("/client/sel/tags", tostring(num))
167 end,
170 -- switching views and retagging
171 ["Mod1-t"] = function (key)
172 local tag = wmii.tagmenu()
173 if tag then
174 wmii.write ("/ctl", "view " .. tag)
177 end,
178 ["Mod1-Shift-t"] = function (key)
179 local tag = wmii.tagmenu()
180 if tag then
181 local cli = wmii.read ("/client/sel/ctl")
182 wmii.write ("/client/" .. cli .. "/tags", tag)
184 end,
185 ["Mod1-Control-t"] = function (key)
186 my_log (" TODO: Mod1-Control-t: " .. key)
187 end,
189 -- column modes
190 ["Mod1-d"] = function (key)
191 wmii.write("/tag/sel/ctl", "colmode sel default")
192 end,
193 ["Mod1-s"] = function (key)
194 wmii.write("/tag/sel/ctl", "colmode sel stack")
195 end,
196 ["Mod1-m"] = function (key)
197 wmii.write("/tag/sel/ctl", "colmode sel max")
201 -- ------------------------------------------------------------------------
202 -- update the /keys wmii file with the list of all handlers
205 local t = {}
206 local x, y
207 for x,y in pairs(key_handlers) do
208 if x:find("%w") then
209 local i = x:find("#")
210 if i then
211 local j
212 for j=0,9 do
213 t[#t + 1]
214 = x:sub(1,i-1) .. j
216 else
217 t[#t + 1]
218 = tostring(x)
222 local all_keys = table.concat(t, "\n")
223 my_log ("setting /keys to...\n" .. all_keys .. "\n");
224 wmii.write ("/keys", all_keys)
228 -- ------------------------------------------------------------------------
229 -- event handlers
231 local ev_handlers = {
232 ["*"] = function (ev, arg)
233 my_log ("ev: " .. ev .. " - " .. arg)
234 end,
236 ClientMouseDown = function (ev, arg)
237 my_log ("ClientMouseDown: " .. arg)
238 end,
240 CreateTag = function (ev, arg)
241 my_log ("CreateTag: " .. arg)
242 end,
244 DestroyTag = function (ev, arg)
245 my_log ("DestroyTag: " .. arg)
246 end,
248 FocusTag = function (ev, arg)
249 my_log ("FocusTag: " .. arg)
250 end,
252 Key = function (ev, arg)
253 my_log ("Key: " .. arg)
254 local num = nil
255 -- can we find an exact match?
256 local fn = key_handlers[arg]
257 if not fn then
258 local key = arg:gsub("-%d+", "-#")
259 -- can we find a match with a # wild card for the number
260 fn = key_handlers[key]
261 if fn then
262 -- convert the trailing number to a number
263 num = tonumber(arg:match("-(%d+)"))
264 else
265 -- everything else failed, try default match
266 fn = key_handlers["*"]
269 if fn then
270 fn (arg, num)
272 end,
274 LeftBarClick = function (ev, arg)
275 my_log ("LeftBarClick: " .. arg)
276 end,
278 NotUrgentTag = function (ev, arg)
279 my_log ("NotUrgentTag: " .. arg)
280 end,
282 Start = function (ev, arg)
283 if arg == "wmiirc" then
284 posix.exit (0)
286 end,
288 UnfocusTag = function (ev, arg)
289 my_log ("UnfocusTag: " .. arg)
290 end,
292 UrgentTag = function (ev, arg)
293 my_log ("UrgentTag: " .. arg)
297 -- ------------------------------------------------------------------------
298 -- reading events
299 my_log("wmii: starting event loop")
300 local ev, arg
301 for ev, arg in wmii.ievents() do
303 local fn = ev_handlers[ev] or ev_handlers["*"]
304 if fn then
305 fn (ev, arg)
308 my_log("wmii: event loop exited")