]> git.decadent.org.uk Git - ion3.git/blob - ioncore/ioncore_bindings.lua
[svn-upgrade] Integrating new upstream version, ion3 (20070608)
[ion3.git] / ioncore / ioncore_bindings.lua
1 --
2 -- ion/share/ioncore-bindings.lua
3 -- 
4 -- Copyright (c) Tuomo Valkonen 2004-2007.
5 --
6 -- See the included file LICENSE for details.
7 --
8
9 local ioncore=_G.ioncore
10
11 local warn=ioncore.warn
12
13 local compiled2str=setmetatable({}, {__mode="k"})
14
15 --DOC
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.
22 -- 
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)
27     local guardcode=""
28     local gfn=nil
29     
30     if guard then
31         local st, en, condition=string.find(guard, "^_sub:([%w-_]+)$")
32         local sub='_sub'
33         if not condition then
34             st, en, condition=string.find(guard, "^_chld:([%w-_]+)$")
35             if condition then
36                 sub='_chld'
37             end
38         end
39         
40         if not condition then
41             ioncore.warn_traced(TR("Invalid guard %s.", guard))
42         elseif condition=="non-nil" then
43             guardcode='if not '..sub..' then return end; '
44         else
45             guardcode='if not obj_is('..sub..', "'..condition..'") then return end; '
46         end
47         
48         local gfncode="return function(_, _sub, _chld) "..guardcode.." return true end"
49         local gfn, gerr=loadstring(gfncode, guardcode)
50         if not gfn then
51             ioncore.warn_traced(TR("Error compiling guard: %s", gerr))
52         end
53         gfn=gfn()
54     end
55
56     local function guarded(gfn, fn)
57         if not gfn then
58             return fn
59         else
60             return function(_, _sub, _chld) 
61                 if gfn(_, _sub, _chld) then 
62                     cmd(_, _sub, _chld) 
63                 end
64             end
65         end
66     end
67     
68     if type(cmd)=="string" then
69         local fncode=("return function(_, _sub, _chld) local d = "
70                        ..cmd.." end")
71         local fn, err=loadstring(fncode, cmd)
72         if not fn then
73             ioncore.warn_traced(TR("Error in command string: ")..err)
74             return
75         end
76         compiled2str[fn]=cmd
77         return guarded(gfn, fn())
78     elseif type(cmd)=="function" then
79         return guarded(gfn, cmd)
80     end
81
82     ioncore.warn_traced(TR("Invalid command"))
83 end
84
85
86 local function putcmd(cmd, guard, tab)
87     local func
88     if cmd then
89         func=ioncore.compile_cmd(cmd, guard)
90         if type(func)~="function" then
91             return
92         end
93     end
94     
95     tab.func=func
96     tab.cmdstr=cmd
97     tab.guard=guard
98     
99     return tab
100 end
101
102 --DOC
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}
107 end
108
109 --DOC
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(keyspec, list)
115     if not list then
116         return function(lst)
117                    return submap(keyspec, lst)
118                end
119     end
120     return {action = "kpress", kcb = keyspec, submap = list}
121 end
122
123 --DOC
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{cmd}.
126 -- The \var{guard} controls when the binding can be called.
127 -- For more informationp see Section \ref{sec:bindings}.
128 function ioncore.kpress(keyspec, cmd, guard)
129     return putcmd(cmd, guard, {action = "kpress", kcb = keyspec})
130 end
131
132 --DOC
133 -- This is similar to \fnref{ioncore.kpress} but after calling \var{cmd}, 
134 -- Ioncore waits for all modifiers to be released before processing
135 -- any further actions.
136 -- For more information on bindings, see Section \ref{sec:bindings}.
137 function ioncore.kpress_wait(keyspec, cmd, guard)
138     return putcmd(cmd, guard, {action = "kpress_wait", kcb = keyspec})
139 end
140
141 --DOC
142 -- Submap enter event for bindings.
143 function ioncore.submap_enter(cmd, guard)
144     return putcmd(cmd, guard, {action = "submap_enter"})
145 end
146
147 --DOC
148 -- Submap modifier release event for bindings.
149 function ioncore.submap_wait(cmd, guard)
150     return putcmd(cmd, guard, {action = "submap_wait"})
151 end
152
153 -- DOC
154 -- Submap leave event for bindings.
155 --function ioncore.submap_leave(cmd, guard)
156 --    return putcmd(cmd, guard, {action = "submap_leave"})
157 --end
158
159 local function mact(act_, kcb_, cmd, guard)
160     local st, en, kcb2_, area_=string.find(kcb_, "([^@]*)@(.*)")
161     return putcmd(cmd, guard, {
162         action = act_,
163         kcb = (kcb2_ or kcb_),
164         area = area_,
165     })
166 end
167
168 --DOC
169 -- Creates a binding description table for the action of clicking a mouse 
170 -- button while possible modifier keys are pressed,
171 -- both given by \var{buttonspec}, to the function \var{cmd}.
172 -- For more information, see Section \ref{sec:bindings}.
173 function ioncore.mclick(buttonspec, cmd, guard)
174     return mact("mclick", buttonspec, cmd, guard)
175 end
176
177 --DOC
178 -- Similar to \fnref{ioncore.mclick} but for double-click.
179 -- Also see Section \ref{sec:bindings}.
180 function ioncore.mdblclick(buttonspec, cmd, guard)
181     return mact("mdblclick", buttonspec, cmd, guard)
182 end
183
184 --DOC
185 -- Similar to \fnref{ioncore.mclick} but for just pressing the mouse button.
186 -- Also see Section \ref{sec:bindings}.
187 function ioncore.mpress(buttonspec, cmd, guard)
188     return mact("mpress", buttonspec, cmd, guard)
189 end
190
191 --DOC
192 -- Creates a binding description table for the action of moving the mouse
193 -- (or other pointing device) while the button given by \var{buttonspec}
194 -- is held pressed and the modifiers given by \var{buttonspec} were pressed
195 -- when the button was initially pressed.
196 -- Also see section \ref{sec:bindings}.
197 function ioncore.mdrag(buttonspec, cmd, guard)
198     return mact("mdrag", buttonspec, cmd, guard)
199 end
200
201 --DOC
202 -- Define bindings for context \var{context}. Here \var{binding} is
203 -- a table composed of entries created with \fnref{ioncore.kpress}, 
204 -- etc.; see Section \ref{sec:bindings} for details.
205 function ioncore.defbindings(context, bindings)
206     local function filterdoc(b)
207         local t={}
208         for k, v in ipairs(b) do
209             local v2=v
210             if v2.submap then
211                 v2=table.copy(v)
212                 v2.submap=filterdoc(v2.submap)
213             end
214             if v2.action~="doc" then
215                 table.insert(t, v2)
216             end
217         end
218         return t
219     end
220     return ioncore.do_defbindings(context, filterdoc(bindings))
221 end
222
223 local function bindings_get_cmds(map)
224     for k, v in pairs(map) do
225         if v.func then
226             v.cmd=compiled2str[v.func]
227         end
228         if v.submap then
229             bindings_get_cmds(v.submap)
230         end
231     end
232 end
233
234 --DOC
235 -- Get a table of all bindings.
236 function ioncore.getbindings(maybe_context)
237     local bindings=ioncore.do_getbindings()
238     if maybe_context then
239         bindings_get_cmds(bindings[maybe_context])
240         return bindings[maybe_context]
241     else
242         for k, v in pairs(bindings) do
243             bindings_get_cmds(v)
244         end
245         return bindings
246     end
247 end
248
249