Fix the del_word function to delete left one word & retain cursor pos
[luakit.git] / lib / mode.lua
blobf6793eef2991e143d5d7f8e613fe5fb629acbabf
1 local setmetatable = setmetatable
2 local type = type
3 local io = io
4 local debug = debug
6 module("mode")
8 -- Internal mode to revert to if all else fails
9 local default_mode = "normal"
11 -- Weak table of windows modes
12 local current_modes = {}
13 setmetatable(current_modes, { __mode = "k" })
15 -- Weak table of default modes on a per-window basis
16 local default_modes = {}
17 setmetatable(default_modes, { __mode = "k" })
19 -- Check if `win` is a window widget
20 function iswindow(win)
21 return type(win) == "widget" and win.type =="window"
22 end
24 -- Return the current mode for a given window widget
25 function get(win)
26 if not iswindow(win) then
27 io.stderr:write("W: mode.lua: attempt to set mode on non-window widget.\n")
28 io.stderr:write(debug.traceback() .. "\n")
29 return nil
30 end
31 return current_modes[win] or default_modes[win] or default_mode
32 end
34 -- Set the current mode for a given window widget
35 function set(win, mode)
36 if not iswindow(win) then
37 io.stderr:write("W: mode.lua: attempt to get mode on non-window widget.\n")
38 io.stderr:write(debug.traceback() .. "\n")
39 return nil
40 end
41 local mode = mode or default_modes[win] or default_mode
42 current_modes[win] = mode
43 -- Raise mode change signal on window widget
44 win:emit_signal("mode-changed", mode)
45 return mode
46 end
48 -- Set the default mode for a given window widget
49 function set_default(win, mode)
50 if not iswindow(win) then
51 io.stderr:write("W: mode.lua: attempt to set default mode on non-window widget.\n")
52 io.stderr:write(debug.traceback() .. "\n")
53 return nil
54 end
55 default_modes[win] = mode
56 end
58 setmetatable(_M, { __call = function(_, ...) return set(...) end })
60 -- vim: ft=lua:et:sw=4:ts=8:sts=4:tw=80