]> git.decadent.org.uk Git - ion3.git/blob - ioncore/ioncore_winprops.lua
[svn-inject] Installing original source of ion3
[ion3.git] / ioncore / ioncore_winprops.lua
1 --
2 -- ion/share/ioncore_winprops.lua
3 -- 
4 -- Copyright (c) Tuomo Valkonen 2004-2006.
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_fn(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     local nm=cwin:name()
95     if not prop.name then
96         return true
97     elseif nm then
98         local st, en=string.find(nm, prop.name)
99         return (st and en)
100     else
101         return false
102     end
103 end
104
105
106 --DOC
107 -- Define a winprop. For more information, see section \ref{sec:winprops}.
108 function ioncore.defwinprop(list)
109     local list2 = {}
110     local class, role, instance = "*", "*", "*"
111     
112     for k, v in pairs(list) do
113         if k == "class" then
114             class = v
115         elseif k == "role" then
116             role = v
117         elseif k == "instance" then
118             instance = v
119         end
120         list2[k] = v
121     end
122     
123     if not list2.match then
124         list2.match=ioncore.match_winprop_name
125     end
126     
127     do_add_winprop(class, role, instance, name, list2)
128 end
129