2009-08-24 16:20:11 +02:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
-- @author Julien Danjou <julien@danjou.info>
|
|
|
|
-- @copyright 2009 Julien Danjou
|
|
|
|
-- @release @AWESOME_VERSION@
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
-- Grab environment we need
|
|
|
|
local client = client
|
2009-11-08 20:40:38 +01:00
|
|
|
local table = table
|
2009-08-24 16:20:11 +02:00
|
|
|
local type = type
|
|
|
|
local ipairs = ipairs
|
|
|
|
local pairs = pairs
|
|
|
|
local aclient = require("awful.client")
|
2009-08-27 12:43:00 +02:00
|
|
|
local atag = require("awful.tag")
|
2015-01-21 12:38:49 +01:00
|
|
|
local timer = require("gears.timer")
|
2009-08-24 16:20:11 +02:00
|
|
|
|
|
|
|
--- Apply rules to clients at startup.
|
2012-06-12 20:13:09 +02:00
|
|
|
-- awful.rules
|
|
|
|
local rules = {}
|
2009-08-24 16:20:11 +02:00
|
|
|
|
|
|
|
--- This is the global rules table.
|
|
|
|
-- <p>You should fill this table with your rule and properties to apply.
|
|
|
|
-- For example, if you want to set xterm maximized at startup, you can add:
|
|
|
|
-- <br/>
|
|
|
|
-- <code>
|
|
|
|
-- { rule = { class = "xterm" },
|
|
|
|
-- properties = { maximized_vertical = true, maximized_horizontal = true } }
|
|
|
|
-- </code>
|
|
|
|
-- </p>
|
|
|
|
-- <p>If you want to set mplayer floating at startup, you can add:
|
|
|
|
-- <br/>
|
2009-08-27 12:43:00 +02:00
|
|
|
-- <code>
|
2009-08-24 16:20:11 +02:00
|
|
|
-- { rule = { name = "MPlayer" },
|
|
|
|
-- properties = { floating = true } }
|
|
|
|
-- </code>
|
|
|
|
-- </p>
|
|
|
|
-- <p>If you want to put Firefox on a specific tag at startup, you
|
|
|
|
-- can add:
|
2009-08-27 12:43:00 +02:00
|
|
|
-- <br/>
|
|
|
|
-- <code>
|
2011-03-29 09:37:17 +02:00
|
|
|
-- { rule = { instance = "firefox" },
|
2009-08-24 16:20:11 +02:00
|
|
|
-- properties = { tag = mytagobject } }
|
2009-08-27 12:43:00 +02:00
|
|
|
-- </code>
|
|
|
|
-- </p>
|
|
|
|
-- <p>If you want to put Emacs on a specific tag at startup, and
|
|
|
|
-- immediately switch to that tag you can add:
|
|
|
|
-- <br/>
|
|
|
|
-- <code>
|
2011-03-29 09:37:17 +02:00
|
|
|
-- { rule = { class = "Emacs" },
|
2009-08-27 12:43:00 +02:00
|
|
|
-- properties = { tag = mytagobject, switchtotag = true } }
|
|
|
|
-- </code>
|
|
|
|
-- </p>
|
2009-11-09 19:12:14 +01:00
|
|
|
-- <p>If you want to apply a custom callback to execute when a rule matched,
|
|
|
|
-- for example to pause playing music from mpd when you start dosbox, you
|
2009-11-08 20:40:38 +01:00
|
|
|
-- can add:
|
|
|
|
-- <br/>
|
|
|
|
-- <code>
|
|
|
|
-- { rule = { class = "dosbox" },
|
2009-11-09 19:12:14 +01:00
|
|
|
-- callback = function(c)
|
|
|
|
-- awful.util.spawn('mpc pause')
|
|
|
|
-- end }
|
2009-11-08 20:40:38 +01:00
|
|
|
-- </code>
|
|
|
|
-- </p>
|
2009-08-24 16:20:11 +02:00
|
|
|
-- <p>Note that all "rule" entries need to match. If any of the entry does not
|
|
|
|
-- match, the rule won't be applied.</p>
|
|
|
|
-- <p>If a client matches multiple rules, their applied in the order they are
|
2009-08-24 16:43:00 +02:00
|
|
|
-- put in this global rules table. If the value of a rule is a string, then the
|
|
|
|
-- match function is used to determine if the client matches the rule.</p>
|
2012-07-06 17:30:35 +02:00
|
|
|
-- <p>If the value of a property is a function, that function gets called and
|
|
|
|
-- function's return value is used for the property.</p>
|
2009-08-24 16:20:11 +02:00
|
|
|
--
|
2010-09-13 22:48:16 +02:00
|
|
|
-- <p> To match multiple clients to a rule one need to use slightly different
|
|
|
|
-- syntax:
|
|
|
|
-- <br/>
|
|
|
|
-- <code>
|
|
|
|
-- { rule_any = { class = { "MPlayer", "Nitrogen" }, instance = { "xterm" } },
|
|
|
|
-- properties = { floating = true } }
|
|
|
|
-- </code>
|
|
|
|
-- </p>
|
|
|
|
--
|
2011-03-27 17:22:50 +02:00
|
|
|
-- <p> To match multiple clients with an exception one can couple 'except' or
|
|
|
|
-- 'except_any' with the rules:
|
|
|
|
-- <br/>
|
|
|
|
-- <code>
|
|
|
|
-- { rule = { class = "Firefox" },
|
|
|
|
-- except = { instance = "Navigator" },
|
|
|
|
-- properties = {floating = true},
|
|
|
|
-- },
|
|
|
|
-- </code>
|
|
|
|
-- <br/>
|
|
|
|
-- <code>
|
|
|
|
-- { rule_any = { class = { "Pidgin", "Xchat" } },
|
|
|
|
-- except_any = { role = { "conversation" } },
|
|
|
|
-- properties = { tag = tags[1][1] }
|
|
|
|
-- }
|
|
|
|
-- <br/>
|
|
|
|
-- <code>
|
|
|
|
-- { rule = {},
|
|
|
|
-- except_any = { class = { "Firefox", "Vim" } },
|
|
|
|
-- properties = { floating = true }
|
|
|
|
-- }
|
|
|
|
-- </code>
|
|
|
|
-- </p>
|
|
|
|
--
|
2009-08-24 16:20:11 +02:00
|
|
|
-- @class table
|
|
|
|
-- @name rules
|
2012-06-12 20:13:09 +02:00
|
|
|
rules.rules = {}
|
2009-08-24 16:20:11 +02:00
|
|
|
|
2014-03-09 22:03:32 +01:00
|
|
|
--- Check if a client matches a rule.
|
2009-08-24 16:20:11 +02:00
|
|
|
-- @param c The client.
|
|
|
|
-- @param rule The rule to check.
|
|
|
|
-- @return True if it matches, false otherwise.
|
2012-06-12 20:13:09 +02:00
|
|
|
function rules.match(c, rule)
|
2011-03-27 17:22:50 +02:00
|
|
|
if not rule then return false end
|
2009-08-24 16:20:11 +02:00
|
|
|
for field, value in pairs(rule) do
|
2009-08-24 16:43:00 +02:00
|
|
|
if c[field] then
|
2009-08-26 17:49:38 +02:00
|
|
|
if type(c[field]) == "string" then
|
|
|
|
if not c[field]:match(value) and c[field] ~= value then
|
|
|
|
return false
|
|
|
|
end
|
2009-08-24 16:43:00 +02:00
|
|
|
elseif c[field] ~= value then
|
|
|
|
return false
|
|
|
|
end
|
2009-09-03 19:21:59 +02:00
|
|
|
else
|
|
|
|
return false
|
2009-08-24 16:20:11 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2014-03-09 22:03:32 +01:00
|
|
|
--- Check if a client matches any part of a rule.
|
2010-09-13 22:48:16 +02:00
|
|
|
-- @param c The client.
|
2012-11-19 14:09:10 +01:00
|
|
|
-- @param rule The rule to check.
|
2010-09-13 22:48:16 +02:00
|
|
|
-- @return True if at least one rule is matched, false otherwise.
|
2012-06-12 20:13:09 +02:00
|
|
|
function rules.match_any(c, rule)
|
2011-03-27 17:22:50 +02:00
|
|
|
if not rule then return false end
|
2010-09-13 22:48:16 +02:00
|
|
|
for field, values in pairs(rule) do
|
|
|
|
if c[field] then
|
|
|
|
for _, value in ipairs(values) do
|
|
|
|
if c[field] == value then
|
|
|
|
return true
|
|
|
|
elseif type(c[field]) == "string" and c[field]:match(value) then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2014-03-09 22:03:32 +01:00
|
|
|
--- Get list of matching rules for a client.
|
|
|
|
-- @param c The client.
|
2014-04-13 18:06:49 +02:00
|
|
|
-- @param _rules The rules to check. List with "rule", "rule_any", "except" and
|
2014-03-09 22:03:32 +01:00
|
|
|
-- "except_any" keys.
|
|
|
|
-- @return The list of matched rules.
|
|
|
|
function rules.matching_rules(c, _rules)
|
|
|
|
local result = {}
|
|
|
|
for _, entry in ipairs(_rules) do
|
|
|
|
if (rules.match(c, entry.rule) or rules.match_any(c, entry.rule_any)) and
|
|
|
|
(not rules.match(c, entry.except) and not rules.match_any(c, entry.except_any)) then
|
|
|
|
table.insert(result, entry)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Check if a client matches a given set of rules.
|
|
|
|
-- @param c The client.
|
2014-04-13 18:06:49 +02:00
|
|
|
-- @param rules The rules to check. List with "rule", "rule_any", "except" and
|
2014-03-09 22:03:32 +01:00
|
|
|
-- "except_any" keys.
|
|
|
|
-- @return True if at least one rule is matched, false otherwise.
|
|
|
|
function rules.does_match(c, rules)
|
|
|
|
local result = rules.matching_rules(c, rules)
|
|
|
|
return #result == 0 and false or result
|
|
|
|
end
|
|
|
|
|
2014-03-16 01:45:31 +01:00
|
|
|
--- Apply awful.rules.rules to a client.
|
2009-08-24 16:20:11 +02:00
|
|
|
-- @param c The client.
|
2012-06-12 20:13:09 +02:00
|
|
|
function rules.apply(c)
|
2009-10-24 15:05:09 +02:00
|
|
|
local props = {}
|
2009-11-08 20:40:38 +01:00
|
|
|
local callbacks = {}
|
2014-03-09 22:03:32 +01:00
|
|
|
|
|
|
|
for _, entry in ipairs(rules.matching_rules(c, rules.rules)) do
|
|
|
|
if entry.properties then
|
|
|
|
for property, value in pairs(entry.properties) do
|
|
|
|
props[property] = value
|
2009-11-08 20:40:38 +01:00
|
|
|
end
|
2009-08-24 16:20:11 +02:00
|
|
|
end
|
2014-03-09 22:03:32 +01:00
|
|
|
if entry.callback then
|
|
|
|
table.insert(callbacks, entry.callback)
|
|
|
|
end
|
2009-08-24 16:20:11 +02:00
|
|
|
end
|
2009-10-24 15:05:09 +02:00
|
|
|
|
2014-03-16 01:45:31 +01:00
|
|
|
rules.execute(c, props, callbacks)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
--- Apply properties and callbacks to a client.
|
|
|
|
-- @param c The client.
|
|
|
|
-- @param props Properties to apply.
|
2014-04-01 10:48:18 +02:00
|
|
|
-- @param callbacks Callbacks to apply (optional).
|
2014-03-16 01:45:31 +01:00
|
|
|
function rules.execute(c, props, callbacks)
|
2009-10-24 15:05:09 +02:00
|
|
|
for property, value in pairs(props) do
|
2012-07-06 17:30:35 +02:00
|
|
|
if property ~= "focus" and type(value) == "function" then
|
|
|
|
value = value(c)
|
|
|
|
end
|
2009-10-24 15:05:09 +02:00
|
|
|
if property == "floating" then
|
|
|
|
aclient.floating.set(c, value)
|
|
|
|
elseif property == "tag" then
|
2012-10-26 01:26:59 +02:00
|
|
|
c.screen = atag.getscreen(value)
|
2014-03-24 00:18:35 +01:00
|
|
|
c:tags({ value })
|
2009-10-24 15:05:09 +02:00
|
|
|
elseif property == "switchtotag" and value and props.tag then
|
|
|
|
atag.viewonly(props.tag)
|
|
|
|
elseif property == "height" or property == "width" or
|
|
|
|
property == "x" or property == "y" then
|
|
|
|
local geo = c:geometry();
|
|
|
|
geo[property] = value
|
|
|
|
c:geometry(geo);
|
2010-08-26 17:48:00 +02:00
|
|
|
elseif property == "focus" then
|
|
|
|
-- This will be handled below
|
2009-10-24 15:05:09 +02:00
|
|
|
elseif type(c[property]) == "function" then
|
|
|
|
c[property](c, value)
|
|
|
|
else
|
|
|
|
c[property] = value
|
|
|
|
end
|
|
|
|
end
|
2009-11-08 20:40:38 +01:00
|
|
|
|
2014-03-16 01:45:31 +01:00
|
|
|
-- Apply all callbacks.
|
2014-04-01 10:48:18 +02:00
|
|
|
if callbacks then
|
|
|
|
for i, callback in pairs(callbacks) do
|
|
|
|
callback(c)
|
|
|
|
end
|
2009-11-08 20:40:38 +01:00
|
|
|
end
|
|
|
|
|
2009-10-24 15:05:09 +02:00
|
|
|
-- Do this at last so we do not erase things done by the focus
|
|
|
|
-- signal.
|
2012-07-06 17:30:35 +02:00
|
|
|
if props.focus and (type(props.focus) ~= "function" or props.focus(c)) then
|
2015-01-21 12:38:49 +01:00
|
|
|
local cb = function(c)
|
|
|
|
c:emit_signal('request::activate', "rules")
|
|
|
|
end
|
|
|
|
timer.delayed_call(cb, c)
|
2009-10-24 15:05:09 +02:00
|
|
|
end
|
2009-08-24 16:20:11 +02:00
|
|
|
end
|
|
|
|
|
2012-06-12 20:13:09 +02:00
|
|
|
client.connect_signal("manage", rules.apply)
|
2009-08-24 16:20:11 +02:00
|
|
|
|
2012-06-12 20:13:09 +02:00
|
|
|
return rules
|
|
|
|
|
2011-09-11 16:50:01 +02:00
|
|
|
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
|