]> git.decadent.org.uk Git - ion3.git/blob - ioncore/ioncore_winprops.lua
[svn-upgrade] Integrating new upstream version, ion3 (20070506)
[ion3.git] / ioncore / ioncore_winprops.lua
1 --
2 -- ion/share/ioncore_winprops.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 winprops={}
12
13 local function ifnil(...)
14     local n=#arg
15     local function nxt(_, i)
16         local j=i+1
17         if i==n then
18             return nil
19         else
20             local j=i+1
21             if not arg[j] then
22                 return nxt(nil, j)
23             else
24                 return j, arg[j]
25             end
26         end
27     end
28             
29     return nxt, nil, 0
30 end
31
32 local function ipairs_r(tab)
33     local function nxt(_, n)
34         if n==1 then
35             return nil
36         else
37             return n-1, tab[n-1]
38         end
39     end
40     return nxt, nil, #tab+1
41 end
42
43 --DOC
44 -- Find winprop table for \var{cwin}.
45 function ioncore.getwinprop(cwin)
46     local id=cwin:get_ident()
47     local props, prop
48
49     for _, c in ifnil(id.class, "*") do
50         for _, r in ifnil(id.role, "*") do
51             for _, i in ifnil(id.instance, "*") do
52                 --printpp(c, r, i)
53                 props={}
54                 pcall(function() props=winprops[c][r][i] or {} end)
55                 for idx, prop in ipairs_r(props) do
56                     if prop:match(cwin, id) then
57                         if prop.oneshot then
58                             table.remove(props, idx)
59                         end
60                         return prop
61                     end
62                 end
63             end
64         end
65     end
66 end
67
68 ioncore.set{_get_winprop=ioncore.getwinprop}
69
70 local function ensure_winproptab(class, role, instance)
71     if not winprops[class] then
72         winprops[class]={}
73     end
74     if not winprops[class][role] then
75         winprops[class][role]={}
76     end
77     if not winprops[class][role][instance] then
78         winprops[class][role][instance]={}
79     end
80 end    
81
82 local function do_add_winprop(class, role, instance, prop)
83     ensure_winproptab(class, role, instance)
84     table.insert(winprops[class][role][instance], prop)
85 end
86
87
88 --DOC
89 -- The basic name-based winprop matching criteria.
90 function ioncore.match_winprop_dflt(prop, cwin, id)
91     local function chkf(p, i)
92         if p==nil then
93             return true
94         else
95             return (p==(i and true)) 
96                            -- hack for nil
97         end
98     end
99     
100     if not chkf(prop.is_transient, id.is_transient) then
101         return false
102     end
103     
104     if not chkf(prop.is_dockapp, id.is_dockapp) then
105         return false
106     end
107     
108     if prop.name then
109         local nm=cwin:name()
110         if nm then
111             local st, en=string.find(nm, prop.name)
112             return (st and en)
113         else
114             return false
115         end
116     end
117     
118     return true
119 end
120
121
122 --DOC
123 -- Define a winprop. For more information, see section \ref{sec:winprops}.
124 function ioncore.defwinprop(list)
125     local list2 = {}
126     local class, role, instance = "*", "*", "*"
127     
128     for k, v in pairs(list) do
129         if k == "class" then
130             class = v
131         elseif k == "role" then
132             role = v
133         elseif k == "instance" then
134             instance = v
135         end
136         list2[k] = v
137     end
138     
139     if not list2.match then
140         list2.match=ioncore.match_winprop_dflt
141     end
142     
143     do_add_winprop(class, role, instance, list2)
144 end
145