2 -- Copyright (c) 2007, Bart Trojanowski <bart@jukie.net>
4 -- WMII event loop, in lua
6 -- http://www.jukie.net/~bart/blog/tag/wmiirc-lua
7 -- git://www.jukie.net/wmiirc-lua.git/
10 -- ========================================================================
12 -- ========================================================================
18 wmii.lua - WMII event-loop methods in lua
24 -- Write something to the wmii filesystem, in this case a key event.
25 wmii.write ("/event", "Key Mod1-j")
27 -- Set your wmii /ctl parameters
32 -- Configure wmii.lua parameters
34 xterm = 'x-terminal-emulator'
37 -- Now start the event loop
42 wmii.lua provides methods for replacing the stock sh-based wmiirc shipped with
43 wmii 3.6 and newer with a lua-based event loop.
45 It should be used by your wmiirc
54 -- ========================================================================
56 -- ========================================================================
58 local wmiidir
= os
.getenv("HOME") .. "/.wmii-3.5"
59 local wmiirc
= wmiidir
.. "/wmiirc"
61 package
.path
= wmiidir
.. "/core/?.lua;" ..
62 wmiidir
.. "/plugins/?.lua;" ..
64 package
.cpath
= wmiidir
.. "/core/?.so;" ..
65 wmiidir
.. "/plugins/?.so;" ..
68 local ixp
= require
"ixp"
69 local eventloop
= require
"eventloop"
70 local history
= require
"history"
72 local io
= require("io")
73 local os
= require("os")
74 local string = require("string")
75 local table = require("table")
76 local math
= require("math")
82 local package
= package
83 local require
= require
84 local tostring = tostring
85 local tonumber = tonumber
86 local setmetatable
= setmetatable
88 -- kinda silly, but there is no working liblua5.1-posix0 in ubuntu
89 -- so we make it optional
90 local have_posix
, posix
= pcall(require
,"posix")
97 -- but having posix is not enough as the API changes, so we try each one
98 if posix
.getprocessid
then
99 local stat
,rc
= pcall (posix
.getprocessid
, "pid")
104 if not myid
and posix
.getpid
then
105 local stat
,rc
= pcall (posix
.getpid
, "pid")
112 -- we were not able to get the PID, but we can create a random number
113 local now
= tonumber(os
.date("%s"))
115 myid
= math
.random(10000)
118 -- ========================================================================
120 -- ========================================================================
122 -- wmiir points to the wmiir executable
123 -- TODO: need to make sure that wmiir is in path, and if not find it
124 local wmiir
= "wmiir"
126 -- wmii_adr is the address we use when connecting using ixp
127 local wmii_adr
= os
.getenv("WMII_ADDRESS")
128 or ("unix!/tmp/ns." .. os
.getenv("USER") .. "."
129 .. os
.getenv("DISPLAY"):match("(:%d+)") .. "/wmii")
131 -- wmixp is the ixp context we use to talk to wmii
132 local wmixp
= ixp
.new(wmii_adr
)
134 -- history of previous views, view_hist[#view_hist] is the last one
135 local view_hist
= {} -- sorted with 1 being the oldest
136 local view_hist_max
= 50 -- max number to keep track of
138 -- allow for a client to be forced to a tag
139 local next_client_goes_to_tag
= nil
141 -- program and action histories
142 local prog_hist
= history
.new (20)
143 local action_hist
= history
.new(10)
145 -- where to find plugins
146 plugin_path
= os
.getenv("HOME") .. "/.wmii-3.5/plugins/?.so;"
147 .. os
.getenv("HOME") .. "/.wmii-3.5/plugins/?.lua;"
148 .. "/usr/local/lib/lua/5.1/wmii/?.so;"
149 .. "/usr/local/share/lua/5.1/wmii/?.lua;"
150 .. "/usr/lib/lua/5.1/wmii/?.so;"
151 .. "/usr/share/lua/5.1/wmii/?.lua"
153 -- where to find wmiirc (see find_wmiirc())
154 wmiirc_path
= os
.getenv("HOME") .. "/.wmii-3.5/wmiirc.lua;"
155 .. os
.getenv("HOME") .. "/.wmii-3.5/wmiirc;"
156 .. "/etc/X11/wmii-3.5/wmiirc.lua;"
157 .. "/etc/X11/wmii-3.5/wmiirc"
159 -- ========================================================================
161 -- ========================================================================
168 Log the message provided in C<str>
170 Currently just writes to io.stderr
175 if get_conf("debug") then
176 io
.stderr
:write (str
.. "\n")
183 =item find_wmiirc ( )
185 Locates the wmiirc script. It looks in ~/.wmii-3.5 and /etc/X11/wmii-3.5
186 for the first lua script bearing the name wmiirc.lua or wmiirc. Returns
191 function find_wmiirc()
193 for fn
in string.gmatch(wmiirc_path
, "[^;]+") do
194 -- try to locate the files locally
195 local file
= io
.open(fn
, "r")
197 local txt
= file
:read("*line")
199 if type(txt
) == 'string' and txt
:match("lua") then
208 -- ========================================================================
209 -- MAIN ACCESS FUNCTIONS
210 -- ========================================================================
215 =item ls ( dir, fmt )
217 List the wmii filesystem directory provided in C<dir>, in the format specified
220 Returns an iterator of TODO
224 function ls (dir
, fmt
)
225 local verbose
= fmt
and fmt
:match("l")
227 local s
= wmixp
:stat(dir
)
229 return function () return nil end
231 if s
.modestr
:match("^[^d]") then
233 return stat2str(verbose
, s
)
237 local itr
= wmixp
:idir (dir
)
248 return stat2str(verbose
, s
)
254 local function stat2str(verbose
, stat
)
256 return string.format("%s %s %s %5d %s %s", stat
.modestr
, stat
.uid
, stat
.gid
, stat
.length
, stat
.timestr
, stat
.name
)
258 if stat
.modestr
:match("^d") then
259 return stat
.name
.. "/"
266 -- ------------------------------------------------------------------------
267 -- read all contents of a wmii virtual file
269 return wmixp
:read (file
)
272 -- ------------------------------------------------------------------------
273 -- return an iterator which walks all the lines in the file
276 -- for event in wmii.iread("/ctl")
280 -- NOTE: don't use iread for files that could block, as this will interfere
281 -- with timer processing and event delivery. Instead fork off a process to
282 -- execute wmiir and read back the responses via callback.
283 function iread (file
)
284 return wmixp
:iread(file
)
287 -- ------------------------------------------------------------------------
288 -- create a wmii file, optionally write data to it
289 function create (file
, data
)
290 wmixp
:create(file
, data
)
293 -- ------------------------------------------------------------------------
294 -- remove a wmii file
295 function remove (file
)
299 -- ------------------------------------------------------------------------
300 -- write a value to a wmii virtual file system
301 function write (file
, value
)
302 wmixp
:write (file
, value
)
305 -- ------------------------------------------------------------------------
306 -- setup a table describing dmenu command
307 local function dmenu_cmd (prompt
, iterator
)
308 local cmdt
= { "dmenu", "-b" }
309 local fn
= get_ctl("font")
311 cmdt
[#cmdt
+1] = "-fn"
314 local normcolors
= get_ctl("normcolors")
316 local nf
, nb
= normcolors
:match("(#%x+)%s+(#%x+)%s#%x+")
318 cmdt
[#cmdt
+1] = "-nf"
319 cmdt
[#cmdt
+1] = "'" .. nf
.. "'"
322 cmdt
[#cmdt
+1] = "-nb"
323 cmdt
[#cmdt
+1] = "'" .. nb
.. "'"
326 local focuscolors
= get_ctl("focuscolors")
328 local sf
, sb
= focuscolors
:match("(#%x+)%s+(#%x+)%s#%x+")
330 cmdt
[#cmdt
+1] = "-sf"
331 cmdt
[#cmdt
+1] = "'" .. sf
.. "'"
334 cmdt
[#cmdt
+1] = "-sb"
335 cmdt
[#cmdt
+1] = "'" .. sb
.. "'"
340 cmdt
[#cmdt
+1] = "'" .. prompt
.. "'"
346 -- ------------------------------------------------------------------------
347 -- displays the menu given an table of entires, returns selected text
348 function menu (tbl
, prompt
)
349 local dmenu
= dmenu_cmd(prompt
)
351 local infile
= os
.tmpname()
352 local fh
= io
.open (infile
, "w+")
355 for i
,v
in pairs(tbl
) do
356 if type(i
) == 'number' and type(v
) == 'string' then
365 local outfile
= os
.tmpname()
367 dmenu
[#dmenu
+1] = "<"
368 dmenu
[#dmenu
+1] = infile
369 dmenu
[#dmenu
+1] = ">"
370 dmenu
[#dmenu
+1] = outfile
372 local cmd
= table.concat(dmenu
," ")
375 fh
= io
.open (outfile
, "r")
378 local sel
= fh
:read("*l")
384 -- ------------------------------------------------------------------------
385 -- displays the a tag selection menu, returns selected tag
387 local tags
= get_tags()
389 return menu(tags
, "tag:")
392 -- ------------------------------------------------------------------------
393 -- displays the a program menu, returns selected program
394 function prog_menu ()
395 local dmenu
= dmenu_cmd("cmd:")
397 local outfile
= os
.tmpname()
399 dmenu
[#dmenu
+1] = ">"
400 dmenu
[#dmenu
+1] = outfile
403 for n
in prog_hist
:walk_reverse_unique() do
404 hstt
[#hstt
+1] = "echo '" .. n
.. "' ; "
407 local cmd
= "(" .. table.concat(hstt
)
409 .. table.concat(dmenu
," ")
412 local fh
= io
.open (outfile
, "rb")
415 local prog
= fh
:read("*l")
421 -- ------------------------------------------------------------------------
422 -- displays the a program menu, returns selected program
426 for s
in wmixp
:idir ("/tag") do
427 if s
.name
and not (s
.name
== "sel") then
435 -- ------------------------------------------------------------------------
436 -- displays the a program menu, returns selected program
438 local v
= wmixp
:read("/ctl") or ""
439 return v
:match("view%s+(%S+)")
442 -- ------------------------------------------------------------------------
443 -- changes the current view to the name given
444 function set_view(sel
)
445 local cur
= get_view()
446 local all
= get_tags()
448 if #all
< 2 or sel
== cur
then
449 -- nothing to do if we have less then 2 tags
453 if not (type(sel
) == "string") then
454 error ("string argument expected")
458 write ("/ctl", "view " .. sel
)
461 -- ------------------------------------------------------------------------
462 -- changes the current view to the index given
463 function set_view_index(sel
)
464 local cur
= get_view()
465 local all
= get_tags()
468 -- nothing to do if we have less then 2 tags
472 local num
= tonumber (sel
)
474 error ("number argument expected")
477 local name
= all
[sel
]
478 if not name
or name
== cur
then
483 write ("/ctl", "view " .. name
)
486 -- ------------------------------------------------------------------------
487 -- chnages to current view by offset given
488 function set_view_ofs(jump
)
489 local cur
= get_view()
490 local all
= get_tags()
493 -- nothing to do if we have less then 2 tags
498 if (jump
< - #all
) or (jump
> #all
) then
499 error ("view selector is out of range")
502 -- find the one that's selected index
505 for i
,v
in pairs (all
) do
506 if v
== cur
then curi
= i
end
510 local newi
= math
.fmod(#all
+ curi
+ jump
- 1, #all
) + 1
511 if (newi
< - #all
) or (newi
> #all
) then
512 error ("error computng new view")
515 write ("/ctl", "view " .. all
[newi
])
518 -- ------------------------------------------------------------------------
519 -- toggle between last view and current view
520 function toggle_view()
521 local last
= view_hist
[#view_hist
]
527 -- ========================================================================
529 -- ========================================================================
531 local action_handlers
= {
532 man
= function (act
, args
)
533 local xterm
= get_conf("xterm") or "xterm"
535 if (not page
) or (not page
:match("%S")) then
536 page
= wmiidir
.. "/wmii.3lua"
538 local cmd
= xterm
.. " -e man " .. page
.. " &"
539 log (" executing: " .. cmd
)
544 write ("/ctl", "quit")
547 exec
= function (act
, args
)
548 local what
= args
or "wmii"
549 log (" asking wmii to exec " .. tostring(what
))
551 write ("/ctl", "exec " .. what
)
554 xlock
= function (act
)
555 local cmd
= get_conf("xlock") or "xscreensaver-command --lock"
561 local wmiirc
= find_wmiirc()
563 log (" executing: lua " .. wmiirc
)
566 posix
.exec ("/bin/sh", "-c", "exec lua wmiirc")
567 posix
.exec ("/usr/bin/lua", wmiirc
)
570 log("sorry cannot restart; you don't have lua's posix library.")
575 wmixp
:write ("/client/sel/ctl", "Urgent toggle")
580 -- TODO: consider storing list of executables around, and
581 -- this will then reinitialize that list
582 log (" TODO: rehash")
586 -- TODO: this should eventually update something on the /rbar
587 log (" TODO: status")
595 =item add_action_handler (action, fn)
597 Add an Alt-a action handler callback function, I<fn>, for the given action string I<action>.
601 function add_action_handler (action
, fn
)
603 if type(action
) ~= "string" or type(fn
) ~= "function" then
604 error ("expecting a string and a function")
607 if action_handlers
[action
] then
608 error ("action handler already exists for '" .. action
.. "'")
611 action_handlers
[action
] = fn
617 =item remove_action_handler (action)
619 Remove an action handler callback function for the given action string I<action>.
623 function remove_action_handler (action
)
625 action_handlers
[action
] = nil
628 -- ========================================================================
630 -- ========================================================================
632 function ke_fullscreen_toggle()
633 wmixp
:write ("/client/sel/ctl", "Fullscreen toggle")
636 function ke_view_starting_with_letter (letter
)
639 -- find the view name in history in reverse order
640 for i
=#view_hist
,1,-1 do
642 if letter
== v
:sub(1,1) then
648 -- otherwise just pick the first view that matches
649 local all
= get_tags()
650 for i
,v
in pairs(all
) do
651 if letter
== v
:sub(1,1) then
660 function ke_handle_action()
665 for n
in action_hist
:walk_reverse() do
667 actions
[#actions
+1] = n
673 for n
,v
in pairs(action_handlers
) do
675 actions
[#actions
+1] = n
680 local text
= menu(actions
, "action:")
682 log ("Action: " .. text
)
685 local si
= text
:find("%s")
687 act
,args
= string.match(text
.. " ", "(%w+)%s(.+)")
690 local fn
= action_handlers
[act
]
692 action_hist
:add (act
)
693 local r
, err
= pcall (fn
, act
, args
)
695 log ("WARNING: " .. tostring(err
))
703 local key_handlers
= {
704 ["*"] = function (key
)
708 -- execution and actions
709 ["Mod1-Return"] = function (key
)
710 local xterm
= get_conf("xterm") or "xterm"
711 log (" executing: " .. xterm
)
712 os
.execute (xterm
.. " &")
714 ["Mod1-Shift-Return"] = function (key
)
715 local tag = tag_menu()
717 local xterm
= get_conf("xterm") or "xterm"
718 log (" executing: " .. xterm
.. " on: " .. tag)
719 next_client_goes_to_tag
= tag
720 os
.execute (xterm
.. " &")
723 ["Mod1-a"] = function (key
)
726 ["Mod1-p"] = function (key
)
727 local prog
= prog_menu()
729 prog_hist
:add(prog
:match("(%w+)"))
730 log (" executing: " .. prog
)
731 os
.execute (prog
.. " &")
734 ["Mod1-Shift-p"] = function (key
)
735 local tag = tag_menu()
737 local prog
= prog_menu()
739 log (" executing: " .. prog
.. " on: " .. tag)
740 next_client_goes_to_tag
= tag
741 os
.execute (prog
.. " &")
745 ["Mod1-Shift-c"] = function (key
)
746 write ("/client/sel/ctl", "kill")
749 -- HJKL active selection
750 ["Mod1-h"] = function (key
)
751 write ("/tag/sel/ctl", "select left")
753 ["Mod1-l"] = function (key
)
754 write ("/tag/sel/ctl", "select right")
756 ["Mod1-j"] = function (key
)
757 write ("/tag/sel/ctl", "select down")
759 ["Mod1-k"] = function (key
)
760 write ("/tag/sel/ctl", "select up")
764 ["Mod1-Shift-h"] = function (key
)
765 write ("/tag/sel/ctl", "send sel left")
767 ["Mod1-Shift-l"] = function (key
)
768 write ("/tag/sel/ctl", "send sel right")
770 ["Mod1-Shift-j"] = function (key
)
771 write ("/tag/sel/ctl", "send sel down")
773 ["Mod1-Shift-k"] = function (key
)
774 write ("/tag/sel/ctl", "send sel up")
778 ["Mod1-space"] = function (key
)
779 write ("/tag/sel/ctl", "select toggle")
781 ["Mod1-Shift-space"] = function (key
)
782 write ("/tag/sel/ctl", "send sel toggle")
785 -- work spaces (# and @ are wildcards for numbers and letters)
786 ["Mod4-#"] = function (key
, num
)
787 -- first attempt to find a view that starts with the number requested
788 local num_str
= tostring(num
)
789 if not ke_view_starting_with_letter (num_str
) then
790 -- if we fail, then set it to the index requested
794 ["Mod4-Shift-#"] = function (key
, num
)
795 write ("/client/sel/tags", tostring(num
))
797 ["Mod4-@"] = function (key
, letter
)
798 ke_view_starting_with_letter (letter
)
800 ["Mod4-Shift-@"] = function (key
, letter
)
801 local all
= get_tags()
803 for i
,v
in pairs(all
) do
804 if letter
== v
:sub(1,1) then
805 write ("/client/sel/tags", v
)
810 ["Mod1-comma"] = function (key
)
813 ["Mod1-period"] = function (key
)
816 ["Mod1-r"] = function (key
)
817 -- got to the last view
821 -- switching views and retagging
822 ["Mod1-t"] = function (key
)
824 local tag = tag_menu()
829 ["Mod1-Shift-t"] = function (key
)
830 -- move selected client to a tag
831 local tag = tag_menu()
833 write ("/client/sel/tags", tag)
836 ["Mod1-Shift-r"] = function (key
)
837 -- move selected client to a tag, and follow
838 local tag = tag_menu()
840 -- get the current window id
841 local xid
= wmixp
:read("/client/sel/ctl") or ""
844 write("/client/sel/tags", tag)
846 -- if the client is still in this tag, then
847 -- it might have been a regexp tag... check
848 local test
= wmixp
:read("/client/sel/ctl")
849 if not test
or test
~= xid
then
850 -- if the window moved, follow it
855 ["Mod1-Control-t"] = function (key
)
856 log (" TODO: Mod1-Control-t: " .. key
)
860 ["Mod1-d"] = function (key
)
861 write("/tag/sel/ctl", "colmode sel default-max")
863 ["Mod1-s"] = function (key
)
864 write("/tag/sel/ctl", "colmode sel stack-max")
866 ["Mod1-m"] = function (key
)
867 write("/tag/sel/ctl", "colmode sel stack+max")
869 ["Mod1-f"] = function (key
)
870 ke_fullscreen_toggle()
873 -- changing client flags
874 ["Shift-Mod1-f"] = function (key
)
875 log ("setting flags")
877 local cli
= get_client ()
879 local flags
= { "suspend", "raw" }
880 local current_flags
= cli
:flags_string()
882 local what
= menu(flags
, "current flags: " .. current_flags
.. " toggle:")
886 ["Mod4-space"] = function (key
)
887 local cli
= get_client ()
895 =item add_key_handler (key, fn)
897 Add a keypress handler callback function, I<fn>, for the given key sequence I<key>.
901 function add_key_handler (key
, fn
)
903 if type(key
) ~= "string" or type(fn
) ~= "function" then
904 error ("expecting a string and a function")
907 if key_handlers
[key
] then
908 -- TODO: we may wish to allow multiple handlers for one keypress
909 error ("key handler already exists for '" .. key
.. "'")
912 key_handlers
[key
] = fn
918 =item remove_key_handler (key)
920 Remove an key handler callback function for the given key I<key>.
922 Returns the handler callback function.
926 function remove_key_handler (key
)
928 local fn
= key_handlers
[key
]
929 key_handlers
[key
] = nil
936 =item remap_key_handler (old_key, new_key)
938 Remove a key handler callback function from the given key I<old_key>,
939 and assign it to a new key I<new_key>.
943 function remap_key_handler (old_key
, new_key
)
945 local fn
= remove_key_handler(old_key
)
947 return add_key_handler (new_key
, fn
)
951 -- ------------------------------------------------------------------------
952 -- update the /keys wmii file with the list of all handlers
953 local alphabet
="abcdefghijklmnopqrstuvwxyz"
954 function update_active_keys ()
957 for x
,y
in pairs(key_handlers
) do
959 local i
= x
:find("#$")
963 t
[#t
+ 1] = x
:sub(1,i
-1) .. j
969 for j
=1,alphabet
:len() do
970 local a
= alphabet
:sub(j
,j
)
971 t
[#t
+ 1] = x
:sub(1,i
-1) .. a
974 t
[#t
+ 1] = tostring(x
)
979 local all_keys
= table.concat(t
, "\n")
980 --log ("setting /keys to...\n" .. all_keys .. "\n");
981 write ("/keys", all_keys
)
984 -- ------------------------------------------------------------------------
985 -- update the /lbar wmii file with the current tags
986 function update_displayed_tags ()
988 local fc
= get_ctl("focuscolors") or ""
989 local nc
= get_ctl("normcolors") or ""
991 -- build up a table of existing tags in the /lbar
994 for s
in wmixp
:idir ("/lbar") do
998 -- for all actual tags in use create any entries in /lbar we don't have
999 -- clear the old table entries if we have them
1000 local cur
= get_view()
1001 local all
= get_tags()
1003 for i
,v
in pairs(all
) do
1009 create ("/lbar/" .. v
, color
.. " " .. v
)
1011 write ("/lbar/" .. v
, color
.. " " .. v
)
1015 -- anything left in the old table should be removed now
1016 for i
,v
in pairs(old
) do
1023 -- ========================================================================
1025 -- ========================================================================
1027 local widget_ev_handlers
= {
1033 =item _handle_widget_event (ev, arg)
1035 Top-level event handler for redispatching events to widgets. This event
1036 handler is added for any widget event that currently has a widget registered
1039 Valid widget events are currently
1041 RightBarMouseDown <buttonnumber> <widgetname>
1042 RightBarClick <buttonnumber> <widgetname>
1044 the "Click" event is sent on mouseup.
1046 The callbacks are given only the button number as their argument, to avoid the
1052 local function _handle_widget_event (ev
, arg
)
1054 log("_handle_widget_event: " .. tostring(ev
) .. " - " .. tostring(arg
))
1056 -- parse arg to strip out our widget name
1057 local number,wname
= string.match(arg
, "(%d+)%s+(.+)")
1059 -- check our dispatch table for that widget
1061 log("Didn't find wname")
1065 local wtable
= widget_ev_handlers
[wname
]
1067 log("No widget cares about" .. wname
)
1071 local fn
= wtable
[ev
] or wtable
["*"]
1073 success
, err
= pcall( fn
, ev
, tonumber(number) )
1075 log("Callback had an error in _handle_widget_event: " .. tostring(err
) )
1079 log("no function found for " .. ev
)
1083 local ev_handlers
= {
1084 ["*"] = function (ev
, arg
)
1085 log ("ev: " .. tostring(ev
) .. " - " .. tostring(arg
))
1088 RightBarClick
= _handle_widget_event
,
1090 -- process timer events
1091 ProcessTimerEvents
= function (ev
, arg
)
1095 -- exit if another wmiirc started up
1096 Start
= function (ev
, arg
)
1098 if arg
== "wmiirc" then
1099 -- backwards compatibility with bash version
1100 log (" exiting; pid=" .. tostring(myid
))
1104 -- ignore if it came from us
1105 local pid
= string.match(arg
, "wmiirc (%d+)")
1107 local pid
= tonumber (pid
)
1108 if not (pid
== myid
) then
1109 log (" exiting; pid=" .. tostring(myid
))
1119 CreateTag
= function (ev
, arg
)
1120 local nc
= get_ctl("normcolors") or ""
1121 create ("/lbar/" .. arg
, nc
.. " " .. arg
)
1123 DestroyTag
= function (ev
, arg
)
1124 remove ("/lbar/" .. arg
)
1126 -- remove the tag from history
1128 for i
=#view_hist
,1,-1 do
1131 table.remove(view_hist
,i
)
1136 FocusTag
= function (ev
, arg
)
1137 local fc
= get_ctl("focuscolors") or ""
1138 create ("/lbar/" .. arg
, fc
.. " " .. arg
)
1139 write ("/lbar/" .. arg
, fc
.. " " .. arg
)
1141 UnfocusTag
= function (ev
, arg
)
1142 local nc
= get_ctl("normcolors") or ""
1143 create ("/lbar/" .. arg
, nc
.. " " .. arg
)
1144 write ("/lbar/" .. arg
, nc
.. " " .. arg
)
1146 -- don't duplicate the last entry
1147 if not (arg
== view_hist
[#view_hist
]) then
1148 view_hist
[#view_hist
+1] = arg
1150 -- limit to view_hist_max
1151 if #view_hist
> view_hist_max
then
1152 table.remove(view_hist
, 1)
1157 -- key event handling
1158 Key
= function (ev
, arg
)
1159 log ("Key: " .. arg
)
1161 -- can we find an exact match?
1162 local fn
= key_handlers
[arg
]
1164 local key
= arg
:gsub("-%d$", "-#")
1165 -- can we find a match with a # wild card for the number
1166 fn
= key_handlers
[key
]
1168 -- convert the trailing number to a number
1169 magic
= tonumber(arg
:match("-(%d)$"))
1173 local key
= arg
:gsub("-%a$", "-@")
1174 -- can we find a match with a @ wild card for a letter
1175 fn
= key_handlers
[key
]
1177 -- split off the trailing letter
1178 magic
= arg
:match("-(%a)$")
1182 -- everything else failed, try default match
1183 fn
= key_handlers
["*"]
1186 local r
, err
= pcall (fn
, arg
, magic
)
1188 log ("WARNING: " .. tostring(err
))
1193 -- mouse handling on the lbar
1194 LeftBarClick
= function (ev
, arg
)
1195 local button
,tag = string.match(arg
, "(%w+)%s+(%S+)")
1200 ClientFocus
= function (ev
, arg
)
1201 log ("ClientFocus: " .. arg
)
1202 client_focused (arg
)
1204 ColumnFocus
= function (ev
, arg
)
1205 log ("ColumnFocus: " .. arg
)
1209 CreateClient
= function (ev
, arg
)
1210 if next_client_goes_to_tag
then
1211 local tag = next_client_goes_to_tag
1213 next_client_goes_to_tag
= nil
1214 write ("/client/" .. cli
.. "/tags", tag)
1217 client_created (arg
)
1219 DestroyClient
= function (ev
, arg
)
1220 client_destoryed (arg
)
1224 UrgentTag
= function (ev
, arg
)
1225 log ("UrgentTag: " .. arg
)
1226 write ("/lbar/" .. arg
, "*" .. arg
);
1228 NotUrgentTag
= function (ev
, arg
)
1229 log ("NotUrgentTag: " .. arg
)
1230 write ("/lbar/" .. arg
, arg
);
1234 Unresponsive
= function (ev
, arg
)
1235 log ("Unresponsive: " .. arg
)
1236 -- TODO ask the user if it shoudl be killed off
1239 Notice
= function (ev
, arg
)
1240 log ("Notice: " .. arg
)
1241 -- TODO send to the message plugin (or implement there)
1250 =item add_widget_event_handler (wname, ev, fn)
1252 Add an event handler callback for the I<ev> event on the widget named I<wname>
1257 function add_widget_event_handler (wname
, ev
, fn
)
1258 if type(wname
) ~= "string" or type(ev
) ~= "string" or type(fn
) ~= "function" then
1259 error ("expecting string for widget name, string for event name and a function callback")
1262 -- Make sure the widget event handler is present
1263 if not ev_handlers
[ev
] then
1264 ev_handlers
[ev
] = _handle_widget_event
1267 if not widget_ev_handlers
[wname
] then
1268 widget_ev_handlers
[wname
] = { }
1271 if widget_ev_handlers
[wname
][ev
] then
1272 -- TODO: we may wish to allow multiple handlers for one event
1273 error ("event handler already exists on widget '" .. wname
.. "' for '" .. ev
.. "'")
1276 widget_ev_handlers
[wname
][ev
] = fn
1282 =item remove_widget_event_handler (wname, ev)
1284 Remove an event handler callback function for the I<ev> on the widget named I<wname>.
1288 function remove_event_handler (wname
, ev
)
1290 if not widget_ev_handlers
[wname
] then
1294 widget_ev_handlers
[wname
][ev
] = nil
1300 =item add_event_handler (ev, fn)
1302 Add an event handler callback function, I<fn>, for the given event I<ev>.
1306 -- TODO: Need to allow registering widgets for RightBar* events. Should probably be done with its own event table, though
1307 function add_event_handler (ev
, fn
)
1308 if type(ev
) ~= "string" or type(fn
) ~= "function" then
1309 error ("expecting a string and a function")
1312 if ev_handlers
[ev
] then
1313 -- TODO: we may wish to allow multiple handlers for one event
1314 error ("event handler already exists for '" .. ev
.. "'")
1318 ev_handlers
[ev
] = fn
1324 =item remove_event_handler (ev)
1326 Remove an event handler callback function for the given event I<ev>.
1330 function remove_event_handler (ev
)
1332 ev_handlers
[ev
] = nil
1336 -- ========================================================================
1337 -- MAIN INTERFACE FUNCTIONS
1338 -- ========================================================================
1341 xterm
= 'x-terminal-emulator',
1342 xlock
= "xscreensaver-command --lock",
1346 -- ------------------------------------------------------------------------
1347 -- write configuration to /ctl wmii file
1348 -- wmii.set_ctl({ "var" = "val", ...})
1349 -- wmii.set_ctl("var, "val")
1350 function set_ctl (first
,second
)
1351 if type(first
) == "table" and second
== nil then
1353 for x
, y
in pairs(first
) do
1354 write ("/ctl", x
.. " " .. y
)
1357 elseif type(first
) == "string" and type(second
) == "string" then
1358 write ("/ctl", first
.. " " .. second
)
1361 error ("expecting a table or two string arguments")
1365 -- ------------------------------------------------------------------------
1366 -- read a value from /ctl wmii file
1367 -- table = wmii.get_ctl()
1368 -- value = wmii.get_ctl("variable"
1369 function get_ctl (name
)
1372 for s
in iread("/ctl") do
1373 local var
,val
= s
:match("(%w+)%s+(.+)")
1385 -- ------------------------------------------------------------------------
1386 -- set an internal wmiirc.lua variable
1387 -- wmii.set_conf({ "var" = "val", ...})
1388 -- wmii.set_conf("var, "val")
1389 function set_conf (first
,second
)
1390 if type(first
) == "table" and second
== nil then
1392 for x
, y
in pairs(first
) do
1396 elseif type(first
) == "string"
1397 and (type(second
) == "string"
1398 or type(second
) == "number"
1399 or type(second
) == "boolean") then
1400 config
[first
] = second
1403 error ("expecting a table, or string and string/number as arguments")
1407 -- ------------------------------------------------------------------------
1408 -- read an internal wmiirc.lua variable
1409 function get_conf (name
)
1416 -- ========================================================================
1418 -- ========================================================================
1420 -- the event loop instance
1421 local el
= eventloop
.new()
1422 local event_read_fd
= -1
1424 -- ------------------------------------------------------------------------
1425 -- start/restart the core event reading process
1426 local function start_event_reader ()
1427 if event_read_fd
~= -1 then
1428 if el
:check_exec(event_read_fd
) then
1432 log("wmii: starting /event reading process")
1433 event_read_fd
= el
:add_exec (wmiir
.. " read /event",
1435 local line
= line
or "nil"
1437 -- try to split off the argument(s)
1438 local ev
,arg
= string.match(line
, "(%S+)%s+(.+)")
1443 -- now locate the handler function and call it
1444 local fn
= ev_handlers
[ev
] or ev_handlers
["*"]
1446 local r
, err
= pcall (fn
, ev
, arg
)
1448 log ("WARNING: " .. tostring(err
))
1453 log("wmii: ... fd=" .. tostring(event_read_fd
))
1456 -- ------------------------------------------------------------------------
1457 -- run the event loop and process events, this function does not exit
1458 function run_event_loop ()
1459 -- stop any other instance of wmiirc
1460 wmixp
:write ("/event", "Start wmiirc " .. tostring(myid
))
1462 log("wmii: updating lbar")
1464 update_displayed_tags ()
1466 log("wmii: updating rbar")
1468 update_displayed_widgets ()
1470 log("wmii: updating active keys")
1472 update_active_keys ()
1474 log("wmii: starting event loop")
1476 start_event_reader()
1477 local sleep_for
= process_timers()
1478 el
:run_loop(sleep_for
)
1482 -- ========================================================================
1484 -- ========================================================================
1486 api_version
= 0.1 -- the API version we export
1488 plugins
= {} -- all plugins that were loaded
1490 -- ------------------------------------------------------------------------
1491 -- plugin loader which also verifies the version of the api the plugin needs
1493 -- here is what it does
1494 -- - does a manual locate on the file using package.path
1495 -- - reads in the file w/o using the lua interpreter
1496 -- - locates api_version=X.Y string
1497 -- - makes sure that api_version requested can be satisfied
1498 -- - if the plugins is available it will set variables passed in
1499 -- - it then loads the plugin
1501 -- TODO: currently the api_version must be in an X.Y format, but we may want
1502 -- to expend this so plugins can say they want '0.1 | 1.3 | 2.0' etc
1504 function load_plugin(name
, vars
)
1505 local backup_path
= package
.path
or "./?.lua"
1507 log ("loading " .. name
)
1509 -- this is the version we want to find
1510 local api_major
, api_minor
= tostring(api_version
):match("(%d+)%.0*(%d+)")
1511 if (not api_major
) or (not api_minor
) then
1512 log ("WARNING: could not parse api_version in core/wmii.lua")
1516 -- first find the plugin file
1517 local s
, path_match
, full_name
, file
1518 for s
in string.gmatch(plugin_path
, "[^;]+") do
1519 -- try to locate the files locally
1520 local fn
= s
:gsub("%?", name
)
1521 file
= io
.open(fn
, "r")
1532 txt
= file
:read("*all")
1537 log ("WARNING: could not load plugin '" .. name
.. "'")
1541 -- find the api_version line
1542 local line
, plugin_version
1543 for line
in string.gmatch(txt
, "%s*api_version%s*=%s*%d+%.%d+%s*") do
1544 plugin_version
= line
:match("api_version%s*=%s*(%d+%.%d+)%s*")
1545 if plugin_version
then
1550 if not plugin_version
then
1551 log ("WARNING: could not find api_version string in plugin '" .. name
.. "'")
1555 -- decompose the version string
1556 local plugin_major
, plugin_minor
= plugin_version
:match("(%d+)%.0*(%d+)")
1557 if (not plugin_major
) or (not plugin_minor
) then
1558 log ("WARNING: could not parse api_version for '" .. name
.. "' plugin")
1562 -- make a version test
1563 if plugin_major
~= api_major
then
1564 log ("WARNING: " .. name
.. " plugin major version missmatch, is " .. plugin_version
1565 .. " (api " .. tonumber(api_version
) .. ")")
1569 if plugin_minor
> api_minor
then
1570 log ("WARNING: '" .. name
.. "' plugin minor version missmatch, is " .. plugin_version
1571 .. " (api " .. tonumber(api_version
) .. ")")
1575 -- the configuration parameters before loading
1576 if type(vars
) == "table" then
1578 for var
,val
in pairs(vars
) do
1579 local success
= pcall (set_conf
, name
.. "." .. var
, val
)
1581 log ("WARNING: bad variable {" .. tostring(var
) .. ", " .. tostring(val
) .. "} "
1582 .. "given; loading '" .. name
.. "' plugin failed.")
1588 -- actually load the module, but use only the path where we though it should be
1589 package
.path
= path_match
1590 local success
,what
= pcall (require
, name
)
1591 package
.path
= backup_path
1593 log ("WARNING: failed to load '" .. name
.. "' plugin")
1594 log (" - path: " .. tostring(path_match
))
1595 log (" - file: " .. tostring(full_name
))
1596 log (" - plugin's api_version: " .. tostring(plugin_version
))
1597 log (" - reason: " .. tostring(what
))
1602 log ("OK, plugin " .. name
.. " loaded, requested api v" .. plugin_version
)
1603 plugins
[name
] = what
1607 -- ------------------------------------------------------------------------
1612 -- ------------------------------------------------------------------------
1613 -- create a widget object and add it to the wmii /rbar
1616 -- widget = wmii.widget:new ("999_clock")
1617 -- widget = wmii.widget:new ("999_clock", clock_event_handler)
1618 function widget
:new (name
, fn
)
1621 if type(name
) == "string" then
1623 if type(fn
) == "function" then
1627 error ("expected name followed by an optional function as arguments")
1630 setmetatable (o
,self
)
1632 self
.__gc
= function (o
) o
:hide() end
1640 -- ------------------------------------------------------------------------
1641 -- stop and destroy the timer
1642 function widget
:delete ()
1643 widgets
[self
.name
] = nil
1647 -- ------------------------------------------------------------------------
1648 -- displays or updates the widget text
1652 -- w:show("foo", "#888888 #222222 #333333")
1653 -- w:show("foo", cell_fg .. " " .. cell_bg .. " " .. border)
1655 function widget
:show (txt
, colors
)
1656 local colors
= colors
or get_ctl("normcolors") or ""
1657 local txt
= txt
or self
.txt
or ""
1660 towrite
= colors
.. " " .. towrite
1662 if not self
.txt
then
1663 create ("/rbar/" .. self
.name
, towrite
)
1665 write ("/rbar/" .. self
.name
, towrite
)
1670 -- ------------------------------------------------------------------------
1671 -- hides a widget and removes it from the bar
1672 function widget
:hide ()
1674 remove ("/lbar/" .. self
.name
)
1682 =item widget:add_event_handler (ev, fn)
1684 Add an event handler callback for this widget, using I<fn> for event I<ev>
1689 function widget
:add_event_handler (ev
, fn
)
1690 add_widget_event_handler( self
.name
, ev
, fn
)
1694 -- ------------------------------------------------------------------------
1695 -- remove all /rbar entries that we don't have widget objects for
1696 function update_displayed_widgets ()
1697 -- colours for /rbar
1698 local nc
= get_ctl("normcolors") or ""
1700 -- build up a table of existing tags in the /lbar
1703 for s
in wmixp
:idir ("/rbar") do
1707 -- for all actual widgets in use we want to remove them from the old list
1709 for i
,v
in pairs(widgets
) do
1713 -- anything left in the old table should be removed now
1714 for i
,v
in pairs(old
) do
1721 -- ------------------------------------------------------------------------
1722 -- create a new program and for each line it generates call the callback function
1723 -- returns fd which can be passed to kill_exec()
1724 function add_exec (command
, callback
)
1725 return el
:add_exec (command
, callback
)
1728 -- ------------------------------------------------------------------------
1729 -- terminates a program spawned off by add_exec()
1730 function kill_exec (fd
)
1731 return el
:kill_exec (fd
)
1734 -- ------------------------------------------------------------------------
1739 -- ------------------------------------------------------------------------
1740 -- create a timer object and add it to the event loop
1743 -- timer:new (my_timer_fn)
1744 -- timer:new (my_timer_fn, 15)
1745 function timer
:new (fn
, seconds
)
1748 if type(fn
) == "function" then
1751 error ("expected function followed by an optional number as arguments")
1754 setmetatable (o
,self
)
1756 self
.__gc
= function (o
) o
:stop() end
1759 timers
[#timers
+1] = o
1767 -- ------------------------------------------------------------------------
1768 -- stop and destroy the timer
1769 function timer
:delete ()
1772 for i
,t
in pairs(timers
) do
1774 table.remove (timers
,i
)
1780 -- ------------------------------------------------------------------------
1781 -- run the timer given new interval
1782 function timer
:resched (seconds
)
1783 local seconds
= seconds
or self
.interval
1784 if not (type(seconds
) == "number") then
1785 error ("timer:resched expected number as argument")
1788 local now
= tonumber(os
.date("%s"))
1790 self
.interval
= seconds
1791 self
.next_time
= now
+ seconds
1793 -- resort the timer list
1794 table.sort (timers
, timer
.is_less_then
)
1797 -- helper for sorting timers
1798 function timer
:is_less_then(another
)
1799 if not self
.next_time
then
1800 return false -- another is smaller, nil means infinity
1802 elseif not another
.next_time
then
1803 return true -- self is smaller, nil means infinity
1805 elseif self
.next_time
< another
.next_time
then
1806 return true -- self is smaller than another
1809 return false -- another is smaller then self
1812 -- ------------------------------------------------------------------------
1814 function timer
:stop ()
1815 self
.next_time
= nil
1817 -- resort the timer list
1818 table.sort (timers
, timer
.is_less_then
)
1821 -- ------------------------------------------------------------------------
1822 -- figure out how long before the next event
1823 function time_before_next_timer_event()
1824 local tmr
= timers
[1]
1825 if tmr
and tmr
.next_time
then
1826 local now
= tonumber(os
.date("%s"))
1827 local seconds
= tmr
.next_time
- now
1832 return 0 -- sleep for ever
1835 -- ------------------------------------------------------------------------
1836 -- handle outstanding events
1837 function process_timers ()
1838 local now
= tonumber(os
.date("%s"))
1842 for i
,tmr
in pairs (timers
) do
1844 -- prune out removed timers
1845 table.remove(timers
,i
)
1848 elseif not tmr
.next_time
then
1849 -- break out once we find a timer that is stopped
1852 elseif tmr
.next_time
> now
then
1853 -- break out once we get to the future
1857 -- this one is good to go
1858 torun
[#torun
+1] = tmr
1861 for i
,tmr
in pairs (torun
) do
1863 local status
,new_interval
= pcall (tmr
.fn
, tmr
)
1865 new_interval
= new_interval
or self
.interval
1866 if new_interval
and (new_interval
~= -1) then
1867 tmr
:resched(new_interval
)
1870 log ("ERROR: " .. tostring(new_interval
))
1874 local sleep_for
= time_before_next_timer_event()
1878 -- ------------------------------------------------------------------------
1879 -- cleanup everything in preparation for exit() or exec()
1884 log ("wmii: stopping timer events")
1886 for i
,tmr
in pairs (timers
) do
1887 pcall (tmr
.delete
, tmr
)
1891 log ("wmii: terminating eventloop")
1893 pcall(el
.kill_all
,el
)
1895 log ("wmii: disposing of widgets")
1897 -- dispose of all widgets
1898 for i
,v
in pairs(widgets
) do
1903 -- FIXME: it doesn't seem to do what I want
1905 log ("wmii: releasing plugins")
1907 for i,p in pairs(plugins) do
1909 pcall (p.cleanup, p)
1915 log ("wmii: dormant")
1918 -- ========================================================================
1920 -- ========================================================================
1923 -- Notes on client tracking
1925 -- When a client is created wmii sends us a CreateClient message, and
1926 -- we in turn create a 'client' object and store it in the 'clients'
1927 -- table indexed by the client's ID.
1929 -- Each client object stores the following:
1930 -- .xid - the X client ID
1931 -- .pid - the process ID
1932 -- .prog - program object representing the process
1934 -- The client and program objects track the following modes for each program:
1937 -- - for each client window
1938 -- - Mod4-space toggles the state between normal and raw
1939 -- - Mod1-f raw also toggles the state
1940 -- - in raw mode all input goes to the client, except for Mod4-space
1941 -- - a focused client with raw mode enabled is put into raw mode
1944 -- - for each program
1945 -- - Mod1-f suspend toggles the state for current client's program
1946 -- - a focused client, whose program was previous suspended is resumed
1947 -- - an unfocused client, with suspend enabled, will be suspended
1948 -- - suspend/resume is done by sending the STOP/CONT signals to the PID
1951 function xid_to_pid (xid
)
1952 local cmd
= "xprop -id " .. tostring(xid
) .. " _NET_WM_PID"
1953 local file
= io
.popen (cmd
)
1954 local out
= file
:read("*a")
1956 local pid
= out
:match("^_NET_WM_PID.*%s+=%s+(%d+)%s+$")
1957 return tonumber(pid
)
1960 local focused_xid
= nil
1961 local clients
= {} -- table of client objects indexed by xid
1962 local programs
= {} -- table of program objects indexed by pid
1963 local mode_widget
= widget
:new ("999_client_mode")
1965 -- make programs table have weak values
1966 -- programs go away as soon as no clients point to it
1967 local programs_mt
= {}
1968 setmetatable(programs
, programs_mt
)
1969 programs_mt
.__mode
= 'v'
1973 function program
:new (pid
)
1976 setmetatable (o
,self
)
1978 self
.__gc
= function (old
) old
:cont() end
1979 -- initialize the new object
1983 o
.suspend
.toggle
= function (prog
)
1984 prog
.suspend
.enabled
= not prog
.suspend
.enabled
1986 o
.suspend
.enabled
= false -- if true, defocusing suspends (SIGSTOP)
1987 o
.suspend
.active
= true -- if true, focusing resumes (SIGCONT)
1991 function program
:stop ()
1992 if not self
.suspend
.active
then
1993 local cmd
= "kill -STOP " .. tostring(self
.pid
)
1994 log (" executing: " .. cmd
)
1996 self
.suspend
.active
= true
2000 function program
:cont ()
2001 if self
.suspend
.active
then
2002 local cmd
= "kill -CONT " .. tostring(self
.pid
)
2003 log (" executing: " .. cmd
)
2005 self
.suspend
.active
= false
2009 function get_program (pid
)
2010 local prog
= programs
[pid
]
2012 prog
= program
:new (pid
)
2013 programs
[pid
] = prog
2020 function client
:new (xid
)
2023 setmetatable (o
,self
)
2024 self
.__index
= function (t
,k
)
2025 if k
== 'suspend' then -- suspend mode is tracked per program
2026 return t
.prog
.suspend
2030 self
.__gc
= function (old
) old
.prog
=nil end
2031 -- initialize the new object
2033 o
.pid
= xid_to_pid(xid
)
2034 o
.prog
= get_program (o
.pid
)
2037 o
.raw
.toggle
= function (cli
)
2038 cli
.raw
.enabled
= not cli
.raw
.enabled
2041 o
.raw
.enabled
= false -- if true, raw mode enabled when client is focused
2045 function client
:stop ()
2046 if self
.suspend
.enabled
then
2051 function client
:cont ()
2055 function client
:set_raw_mode()
2056 if not self
or not self
.raw
.enabled
then -- normal mode
2057 update_active_keys ()
2059 write ("/keys", "Mod4-space")
2063 function client
:toggle(what
)
2064 if what
and self
[what
] then
2065 local ctl
= self
[what
]
2069 log ("xid=" .. tostring (xid
)
2070 .. " pid=" .. tostring (self
.pid
) .. " (" .. tostring (self
.prog
.pid
) .. ")"
2071 .. " what=" .. tostring (what
)
2072 .. " enabled=" .. tostring(ctl
["enabled"]))
2074 mode_widget
:show (self
:flags_string())
2077 function client
:flags_string()
2079 if self
.suspend
.enabled
then ret
= ret
.. "s" else ret
= ret
.. "-" end
2080 if self
.raw
.enabled
then ret
= ret
.. "r" else ret
= ret
.. "-" end
2084 function get_client (xid
)
2085 local xid
= xid
or wmixp
:read("/client/sel/ctl")
2086 local cli
= clients
[xid
]
2088 cli
= client
:new (xid
)
2094 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
2095 function client_created (xid
)
2096 log ("-client_created " .. tostring(xid
))
2097 return get_client(xid
)
2100 function client_destoryed (xid
)
2101 log ("-client_destoryed " .. tostring(xid
))
2102 if clients
[xid
] then
2103 local cli
= clients
[xid
]
2105 log (" del pid: " .. tostring(cli
.pid
))
2108 if focused_xid
== xid
then
2113 function client_focused (xid
)
2114 log ("-client_focused " .. tostring(xid
))
2115 -- return the current focused xid if nil is passed
2119 -- do nothing if the same xid
2120 if focused_xid
== xid
then
2124 local old
= clients
[focused_xid
]
2125 local new
= get_client(xid
)
2127 -- handle raw mode switch
2128 if not old
or ( old
and new
and old
.raw
.enabled
~= new
.raw
.enabled
) then
2132 -- do nothing if the same pid
2133 if old
and new
and old
.pid
== new
.pid
then
2134 mode_widget
:show (new
:flags_string())
2140 log (" old pid: " .. tostring(old.pid)
2141 .. " xid: " .. tostring(old.xid)
2142 .. " flags: " .. old:flags_string())
2149 log (" new pid: " .. tostring(new.pid)
2150 .. " xid: " .. tostring(new.xid)
2151 .. " flags: " .. new:flags_string())
2156 mode_widget
:show (new
:flags_string())
2162 -- ========================================================================
2164 -- ========================================================================
2177 Used to determine location of wmii's listen socket.
2183 L<wmii(1)>, L<lua(1)>
2187 Bart Trojanowski B<< <bart@jukie.net> >>
2189 =head1 COPYRIGHT AND LICENSE
2191 Copyright (c) 2007, Bart Trojanowski <bart@jukie.net>
2193 This is free software. You may redistribute copies of it under the terms of
2194 the GNU General Public License L<http://www.gnu.org/licenses/gpl.html>. There
2195 is NO WARRANTY, to the extent permitted by law.