1 local setmetatable
= setmetatable
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"
24 -- Return the current mode for a given window widget
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")
31 return current_modes
[win
] or default_modes
[win
] or default_mode
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")
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
)
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")
55 default_modes
[win
] = mode
58 setmetatable(_M
, { __call
= function(_
, ...) return set(...) end })
60 -- vim: ft=lua:et:sw=4:ts=8:sts=4:tw=80