2 -- ion/share/ioncore-bindings.lua
4 -- Copyright (c) Tuomo Valkonen 2004-2007.
6 -- Ion is free software; you can redistribute it and/or modify it under
7 -- the terms of the GNU Lesser General Public License as published by
8 -- the Free Software Foundation; either version 2.1 of the License, or
9 -- (at your option) any later version.
12 local ioncore=_G.ioncore
14 local warn=ioncore.warn
16 local compiled2str=setmetatable({}, {__mode="k"})
19 -- Compile string \var{cmd} into a bindable function. Within \var{cmd}, the
20 -- variable ''\var{_}'' (underscore) can be used to refer to the object
21 -- that was selecting for the bound action and chosen to handle it.
22 -- The variable ''\var{_sub}'' refers to a ''currently active'' sub-object
23 -- of \var{_}, or a sub-object where the action loading to the binding
24 -- being called actually occured.
26 -- The string \var{guard} maybe set to pose limits on \code{_sub}. Currently
27 -- supported guards are \code{_sub:non-nil} and \code{_sub:WFoobar}, where
28 -- \type{WFoobar} is a class.
29 function ioncore.compile_cmd(cmd, guard)
34 local st, en, condition=string.find(guard, "^_sub:([%w-_]+)$")
37 st, en, condition=string.find(guard, "^_chld:([%w-_]+)$")
44 ioncore.warn_traced(TR("Invalid guard %s.", guard))
45 elseif condition=="non-nil" then
46 guardcode='if not '..sub..' then return end; '
48 guardcode='if not obj_is('..sub..', "'..condition..'") then return end; '
51 local gfncode="return function(_, _sub, _chld) "..guardcode.." return true end"
52 local gfn, gerr=loadstring(gfncode, guardcode)
54 ioncore.warn_traced(TR("Error compiling guard: %s", gerr))
59 local function guarded(gfn, fn)
63 return function(_, _sub, _chld)
64 if gfn(_, _sub, _chld) then
71 if type(cmd)=="string" then
72 local fncode=("return function(_, _sub, _chld) local d = "
74 local fn, err=loadstring(fncode, cmd)
76 ioncore.warn_traced(TR("Error in command string: ")..err)
80 return guarded(gfn, fn())
81 elseif type(cmd)=="function" then
82 return guarded(gfn, cmd)
85 ioncore.warn_traced(TR("Invalid command"))
89 local function putcmd(cmd, guard, tab)
92 func=ioncore.compile_cmd(cmd, guard)
93 if type(func)~="function" then
106 -- Used to enter documentation among bindings so that other programs
107 -- can read it. Does nothing.
108 function ioncore.bdoc(text)
109 return {action = "doc", text = text}
113 -- Returns a function that creates a submap binding description table.
114 -- When the key press action \var{keyspec} occurs, Ioncore will wait for
115 -- a further key presse and act according to the submap.
116 -- For details, see section \ref{sec:bindings}.
117 function ioncore.submap(kcb_, list)
120 return submap(kcb_, lst)
123 return {action = "kpress", kcb = kcb_, submap = list}
127 -- Creates a binding description table for the action of pressing a key given
128 -- by \var{keyspec} (with possible modifiers) to the function \var{func}.
129 -- For more information on bindings, see section \ref{sec:bindings}.
130 function ioncore.kpress(keyspec, cmd, guard)
131 return putcmd(cmd, guard, {action = "kpress", kcb = keyspec})
135 -- This is similar to \fnref{kpress} but after calling \var{cmd},
136 -- Ioncore waits for all modifiers to be released before processing
137 -- any further actions.
138 -- For more information on bindings, see section \ref{sec:bindings}.
139 function ioncore.kpress_wait(keyspec, cmd, guard)
140 return putcmd(cmd, guard, {action = "kpress_wait", kcb = keyspec})
143 local function mact(act_, kcb_, cmd, guard)
144 local st, en, kcb2_, area_=string.find(kcb_, "([^@]*)@(.*)")
145 return putcmd(cmd, guard, {
147 kcb = (kcb2_ or kcb_),
153 -- Creates a binding description table for the action of clicking a mouse
154 -- button while possible modifier keys are pressed,
155 -- both given by \var{buttonspec}, to the function \var{func}.
156 -- For more information, see section \ref{sec:bindings}.
157 function ioncore.mclick(buttonspec, cmd, guard)
158 return mact("mclick", buttonspec, cmd, guard)
162 -- Similar to \fnref{mclick} but for double-click.
163 -- Also see section \ref{sec:bindings}.
164 function ioncore.mdblclick(buttonspec, cmd, guard)
165 return mact("mdblclick", buttonspec, cmd, guard)
169 -- Similar to \fnref{mclick} but for just pressing the mouse button.
170 -- Also see section \ref{sec:bindings}.
171 function ioncore.mpress(buttonspec, cmd, guard)
172 return mact("mpress", buttonspec, cmd, guard)
176 -- Creates a binding description table for the action of moving the mouse
177 -- (or other pointing device) while the button given by \var{buttonspec}
178 -- is held pressed and the modifiers given by \var{buttonspec} were pressed
179 -- when the button was initially pressed.
180 -- Also see section \ref{sec:bindings}.
181 function ioncore.mdrag(buttonspec, cmd, guard)
182 return mact("mdrag", buttonspec, cmd, guard)
186 -- Define bindings for context \var{context}. Here \var{binding} is
187 -- a table composed of entries created with \fnref{ioncore.kpress},
188 -- etc.; see section \ref{sec:bindings} for details.
189 function ioncore.defbindings(context, bindings)
190 local function filterdoc(b)
192 for k, v in ipairs(b) do
196 v2.submap=filterdoc(v2.submap)
198 if v2.action~="doc" then
204 return ioncore.do_defbindings(context, filterdoc(bindings))
207 local function bindings_get_cmds(map)
208 for k, v in pairs(map) do
210 v.cmd=compiled2str[v.func]
213 bindings_get_cmds(v.submap)
219 -- Get a table of all bindings.
220 function ioncore.getbindings(maybe_context)
221 local bindings=ioncore.do_getbindings()
222 if maybe_context then
223 bindings_get_cmds(bindings[maybe_context])
224 return bindings[maybe_context]
226 for k, v in pairs(bindings) do