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