awful.rules: apply accumulated rule properties (FS#669)
[awesome.git] / lib / awful / rules.lua.in
blob55bcc08f4e78260a6105b0485e355e389bd22462
1 ---------------------------------------------------------------------------
2 -- @author Julien Danjou <julien@danjou.info>
3 -- @copyright 2009 Julien Danjou
4 -- @release @AWESOME_VERSION@
5 ---------------------------------------------------------------------------
7 -- Grab environment we need
8 local client = client
9 local type = type
10 local ipairs = ipairs
11 local pairs = pairs
12 local aclient = require("awful.client")
13 local atag = require("awful.tag")
15 --- Apply rules to clients at startup.
16 module("awful.rules")
18 --- This is the global rules table.
19 -- <p>You should fill this table with your rule and properties to apply.
20 -- For example, if you want to set xterm maximized at startup, you can add:
21 -- <br/>
22 -- <code>
23 -- { rule = { class = "xterm" },
24 -- properties = { maximized_vertical = true, maximized_horizontal = true } }
25 -- </code>
26 -- </p>
27 -- <p>If you want to set mplayer floating at startup, you can add:
28 -- <br/>
29 -- <code>
30 -- { rule = { name = "MPlayer" },
31 -- properties = { floating = true } }
32 -- </code>
33 -- </p>
34 -- <p>If you want to put Firefox on a specific tag at startup, you
35 -- can add:
36 -- <br/>
37 -- <code>
38 -- { rule = { instance = "firefox" }
39 -- properties = { tag = mytagobject } }
40 -- </code>
41 -- </p>
42 -- <p>If you want to put Emacs on a specific tag at startup, and
43 -- immediately switch to that tag you can add:
44 -- <br/>
45 -- <code>
46 -- { rule = { class = "Emacs" }
47 -- properties = { tag = mytagobject, switchtotag = true } }
48 -- </code>
49 -- </p>
50 -- <p>Note that all "rule" entries need to match. If any of the entry does not
51 -- match, the rule won't be applied.</p>
52 -- <p>If a client matches multiple rules, their applied in the order they are
53 -- put in this global rules table. If the value of a rule is a string, then the
54 -- match function is used to determine if the client matches the rule.</p>
56 -- @class table
57 -- @name rules
58 rules = {}
60 --- Check if a client match a rule.
61 -- @param c The client.
62 -- @param rule The rule to check.
63 -- @return True if it matches, false otherwise.
64 function match(c, rule)
65 for field, value in pairs(rule) do
66 if c[field] then
67 if type(c[field]) == "string" then
68 if not c[field]:match(value) and c[field] ~= value then
69 return false
70 end
71 elseif c[field] ~= value then
72 return false
73 end
74 else
75 return false
76 end
77 end
78 return true
79 end
81 --- Apply rules to a client.
82 -- @param c The client.
83 function apply(c)
84 local props = {}
85 for _, entry in ipairs(rules) do
86 if match(c, entry.rule) then
87 for property, value in pairs(entry.properties) do
88 props[property] = value
89 end
90 end
91 end
93 for property, value in pairs(props) do
94 if property == "floating" then
95 aclient.floating.set(c, value)
96 elseif property == "tag" then
97 aclient.movetotag(value, c)
98 elseif property == "switchtotag" and value and props.tag then
99 atag.viewonly(props.tag)
100 elseif property == "height" or property == "width" or
101 property == "x" or property == "y" then
102 local geo = c:geometry();
103 geo[property] = value
104 c:geometry(geo);
105 elseif type(c[property]) == "function" then
106 c[property](c, value)
107 else
108 c[property] = value
111 -- Do this at last so we do not erase things done by the focus
112 -- signal.
113 if props.focus then
114 client.focus = c
118 client.add_signal("manage", apply)
120 -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80