2 -- ion/share/ioncore-bindings.lua
4 -- Copyright (c) Tuomo Valkonen 2004-2007.
6 -- See the included file LICENSE for details.
9 local ioncore=_G.ioncore
11 local warn=ioncore.warn
13 local compiled2str=setmetatable({}, {__mode="k"})
16 -- Compile string \var{cmd} into a bindable function. Within \var{cmd}, the
17 -- variable ''\var{_}'' (underscore) can be used to refer to the object
18 -- that was selecting for the bound action and chosen to handle it.
19 -- The variable ''\var{_sub}'' refers to a ''currently active'' sub-object
20 -- of \var{_}, or a sub-object where the action loading to the binding
21 -- being called actually occured.
23 -- The string \var{guard} maybe set to pose limits on \code{_sub}. Currently
24 -- supported guards are \code{_sub:non-nil} and \code{_sub:WFoobar}, where
25 -- \type{WFoobar} is a class.
26 function ioncore.compile_cmd(cmd, guard)
31 local st, en, condition=string.find(guard, "^_sub:([%w-_]+)$")
34 st, en, condition=string.find(guard, "^_chld:([%w-_]+)$")
41 ioncore.warn_traced(TR("Invalid guard %s.", guard))
42 elseif condition=="non-nil" then
43 guardcode='if not '..sub..' then return end; '
45 guardcode='if not obj_is('..sub..', "'..condition..'") then return end; '
48 local gfncode="return function(_, _sub, _chld) "..guardcode.." return true end"
49 local gfn, gerr=loadstring(gfncode, guardcode)
51 ioncore.warn_traced(TR("Error compiling guard: %s", gerr))
56 local function guarded(gfn, fn)
60 return function(_, _sub, _chld)
61 if gfn(_, _sub, _chld) then
68 if type(cmd)=="string" then
69 local fncode=("return function(_, _sub, _chld) local d = "
71 local fn, err=loadstring(fncode, cmd)
73 ioncore.warn_traced(TR("Error in command string: ")..err)
77 return guarded(gfn, fn())
78 elseif type(cmd)=="function" then
79 return guarded(gfn, cmd)
82 ioncore.warn_traced(TR("Invalid command"))
86 local function putcmd(cmd, guard, tab)
89 func=ioncore.compile_cmd(cmd, guard)
90 if type(func)~="function" then
103 -- Used to enter documentation among bindings so that other programs
104 -- can read it. Does nothing.
105 function ioncore.bdoc(text)
106 return {action = "doc", text = text}
110 -- Returns a function that creates a submap binding description table.
111 -- When the key press action \var{keyspec} occurs, Ioncore will wait for
112 -- a further key presse and act according to the submap.
113 -- For details, see section \ref{sec:bindings}.
114 function ioncore.submap(kcb_, list)
117 return submap(kcb_, lst)
120 return {action = "kpress", kcb = kcb_, submap = list}
124 -- Creates a binding description table for the action of pressing a key given
125 -- by \var{keyspec} (with possible modifiers) to the function \var{func}.
126 -- For more information on bindings, see section \ref{sec:bindings}.
127 function ioncore.kpress(keyspec, cmd, guard)
128 return putcmd(cmd, guard, {action = "kpress", kcb = keyspec})
132 -- This is similar to \fnref{kpress} but after calling \var{cmd},
133 -- Ioncore waits for all modifiers to be released before processing
134 -- any further actions.
135 -- For more information on bindings, see section \ref{sec:bindings}.
136 function ioncore.kpress_wait(keyspec, cmd, guard)
137 return putcmd(cmd, guard, {action = "kpress_wait", kcb = keyspec})
141 -- Submap enter event for bindings.
142 function ioncore.submap_enter(cmd, guard)
143 return putcmd(cmd, guard, {action = "submap_enter"})
147 -- Submap modifier release event for bindings.
148 function ioncore.submap_wait(cmd, guard)
149 return putcmd(cmd, guard, {action = "submap_wait"})
153 -- Submap leave event for bindings.
154 --function ioncore.submap_leave(cmd, guard)
155 -- return putcmd(cmd, guard, {action = "submap_leave"})
158 local function mact(act_, kcb_, cmd, guard)
159 local st, en, kcb2_, area_=string.find(kcb_, "([^@]*)@(.*)")
160 return putcmd(cmd, guard, {
162 kcb = (kcb2_ or kcb_),
168 -- Creates a binding description table for the action of clicking a mouse
169 -- button while possible modifier keys are pressed,
170 -- both given by \var{buttonspec}, to the function \var{func}.
171 -- For more information, see section \ref{sec:bindings}.
172 function ioncore.mclick(buttonspec, cmd, guard)
173 return mact("mclick", buttonspec, cmd, guard)
177 -- Similar to \fnref{mclick} but for double-click.
178 -- Also see section \ref{sec:bindings}.
179 function ioncore.mdblclick(buttonspec, cmd, guard)
180 return mact("mdblclick", buttonspec, cmd, guard)
184 -- Similar to \fnref{mclick} but for just pressing the mouse button.
185 -- Also see section \ref{sec:bindings}.
186 function ioncore.mpress(buttonspec, cmd, guard)
187 return mact("mpress", buttonspec, cmd, guard)
191 -- Creates a binding description table for the action of moving the mouse
192 -- (or other pointing device) while the button given by \var{buttonspec}
193 -- is held pressed and the modifiers given by \var{buttonspec} were pressed
194 -- when the button was initially pressed.
195 -- Also see section \ref{sec:bindings}.
196 function ioncore.mdrag(buttonspec, cmd, guard)
197 return mact("mdrag", buttonspec, cmd, guard)
201 -- Define bindings for context \var{context}. Here \var{binding} is
202 -- a table composed of entries created with \fnref{ioncore.kpress},
203 -- etc.; see section \ref{sec:bindings} for details.
204 function ioncore.defbindings(context, bindings)
205 local function filterdoc(b)
207 for k, v in ipairs(b) do
211 v2.submap=filterdoc(v2.submap)
213 if v2.action~="doc" then
219 return ioncore.do_defbindings(context, filterdoc(bindings))
222 local function bindings_get_cmds(map)
223 for k, v in pairs(map) do
225 v.cmd=compiled2str[v.func]
228 bindings_get_cmds(v.submap)
234 -- Get a table of all bindings.
235 function ioncore.getbindings(maybe_context)
236 local bindings=ioncore.do_getbindings()
237 if maybe_context then
238 bindings_get_cmds(bindings[maybe_context])
239 return bindings[maybe_context]
241 for k, v in pairs(bindings) do