awful.prompt: "Fix" for multi-byte characters
[awesome.git] / lib / awful / rules.lua.in
blobc536c3537a190d7d43b02fbcdb29a1c2ec1b6812
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 table = table
10 local type = type
11 local ipairs = ipairs
12 local pairs = pairs
13 local aclient = require("awful.client")
14 local atag = require("awful.tag")
16 --- Apply rules to clients at startup.
17 module("awful.rules")
19 --- This is the global rules table.
20 -- <p>You should fill this table with your rule and properties to apply.
21 -- For example, if you want to set xterm maximized at startup, you can add:
22 -- <br/>
23 -- <code>
24 -- { rule = { class = "xterm" },
25 -- properties = { maximized_vertical = true, maximized_horizontal = true } }
26 -- </code>
27 -- </p>
28 -- <p>If you want to set mplayer floating at startup, you can add:
29 -- <br/>
30 -- <code>
31 -- { rule = { name = "MPlayer" },
32 -- properties = { floating = true } }
33 -- </code>
34 -- </p>
35 -- <p>If you want to put Firefox on a specific tag at startup, you
36 -- can add:
37 -- <br/>
38 -- <code>
39 -- { rule = { instance = "firefox" }
40 -- properties = { tag = mytagobject } }
41 -- </code>
42 -- </p>
43 -- <p>If you want to put Emacs on a specific tag at startup, and
44 -- immediately switch to that tag you can add:
45 -- <br/>
46 -- <code>
47 -- { rule = { class = "Emacs" }
48 -- properties = { tag = mytagobject, switchtotag = true } }
49 -- </code>
50 -- </p>
51 -- <p>If you want to apply a custom callback to execute when a rule matched, you
52 -- can add:
53 -- <br/>
54 -- <code>
55 -- { rule = { class = "dosbox" },
56 -- callback = awful.placement.centered }
57 -- </code>
58 -- </p>
59 -- <p>Note that all "rule" entries need to match. If any of the entry does not
60 -- match, the rule won't be applied.</p>
61 -- <p>If a client matches multiple rules, their applied in the order they are
62 -- put in this global rules table. If the value of a rule is a string, then the
63 -- match function is used to determine if the client matches the rule.</p>
65 -- @class table
66 -- @name rules
67 rules = {}
69 --- Check if a client match a rule.
70 -- @param c The client.
71 -- @param rule The rule to check.
72 -- @return True if it matches, false otherwise.
73 function match(c, rule)
74 for field, value in pairs(rule) do
75 if c[field] then
76 if type(c[field]) == "string" then
77 if not c[field]:match(value) and c[field] ~= value then
78 return false
79 end
80 elseif c[field] ~= value then
81 return false
82 end
83 else
84 return false
85 end
86 end
87 return true
88 end
90 --- Apply rules to a client.
91 -- @param c The client.
92 function apply(c)
93 local props = {}
94 local callbacks = {}
95 for _, entry in ipairs(rules) do
96 if match(c, entry.rule) then
97 if entry.properties then
98 for property, value in pairs(entry.properties) do
99 props[property] = value
102 if entry.callback then
103 table.insert(callbacks, entry.callback)
108 for property, value in pairs(props) do
109 if property == "floating" then
110 aclient.floating.set(c, value)
111 elseif property == "tag" then
112 c:tags({ value })
113 c.screen = value.screen
114 elseif property == "switchtotag" and value and props.tag then
115 atag.viewonly(props.tag)
116 elseif property == "height" or property == "width" or
117 property == "x" or property == "y" then
118 local geo = c:geometry();
119 geo[property] = value
120 c:geometry(geo);
121 elseif type(c[property]) == "function" then
122 c[property](c, value)
123 else
124 c[property] = value
128 -- If untagged, stick the client on the current one.
129 if #c:tags() == 0 then
130 atag.withcurrent(c)
133 -- Apply all callbacks from matched rules.
134 for i, callback in pairs(callbacks) do
135 callback(c)
138 -- Do this at last so we do not erase things done by the focus
139 -- signal.
140 if props.focus then
141 client.focus = c
145 client.add_signal("manage", apply)
146 client.remove_signal("manage", atag.withcurrent)
148 -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80