1 ---------------------------------------------------------------------------
2 -- @author Julien Danjou <julien@danjou.info>
3 -- @copyright 2008 Julien Danjou
4 -- @release @AWESOME_VERSION@
5 ---------------------------------------------------------------------------
7 -- Grab environment we need
11 local loadstring
= loadstring
12 local loadfile
= loadfile
24 --- Utility module for awful
29 function deprecate(see
)
30 io
.stderr
:write("W: awful: function is deprecated")
32 io
.stderr
:write(", see " .. see
)
35 io
.stderr
:write(debug
.traceback())
38 --- Strip alpha part of color.
39 -- @param color The color.
40 -- @return The color without alpha channel.
41 function color_strip_alpha(color
)
42 if color
:len() == 9 then
43 color
= color
:sub(1, 7)
50 -- @param i An absolute index to fit into #t.
51 -- @return The object at new index.
53 while i
> t
do i
= i
- t
end
54 while i
< 1 do i
= i
+ t
end
58 --- Create a directory
59 -- @param dir The directory.
60 -- @return mkdir return code
62 return os
.execute("mkdir -p " .. dir
)
66 -- @param cmd The command.
67 -- @paran sn Enable startup-notification.
68 -- @param screen The screen where to spawn window.
69 -- @return The awesome.spawn return value.
70 function spawn(cmd
, sn
, screen
)
71 if cmd
and cmd
~= "" then
72 if sn
== nil then sn
= true end
73 return capi
.awesome
.spawn(cmd
, sn
, screen
or capi
.mouse
.screen
)
77 --- Read a program output and returns its output as a string.
78 -- @param cmd The command to run.
79 -- @return A string with the program output.
81 if cmd
and cmd
~= "" then
82 local f
, err
= io
.popen(cmd
, 'r')
84 local s
= f
:read("*all")
94 -- @return The return value of Lua code.
96 return assert(loadstring(s
))()
99 local xml_entity_names
= { ["'"] = "'", ["\""] = """, ["<"] = "<", [">"] = ">", ["&"] = "&" };
100 --- Escape a string from XML char.
101 -- Useful to set raw text in textbox.
102 -- @param text Text to escape.
103 -- @return Escape text.
104 function escape(text
)
105 return text
and text
:gsub("['&<>\"]", xml_entity_names
) or nil
108 local xml_entity_chars
= { lt
= "<", gt
= ">", nbsp
= " ", quot
= "\"", apos
= "'", ndash
= "-", mdash
= "-", amp
= "&" };
109 --- Unescape a string from entities.
110 -- @param text Text to unescape.
111 -- @return Unescaped text.
112 function unescape(text
)
113 return text
and text
:gsub("&(%a+);", xml_entity_chars
) or nil
116 --- Check if a file is a Lua valid file.
117 -- This is done by loading the content and compiling it with loadfile().
118 -- @param path The file path.
119 -- @return A function if everything is alright, a string with the error
121 function checkfile(path
)
122 local f
, e
= loadfile(path
)
123 -- Return function if function, otherwise return error.
124 if f
then return f
end
128 --- Try to restart awesome.
129 -- It checks if the configuration file is valid, and then restart if it's ok.
130 -- If it's not ok, the error will be returned.
131 -- @return Never return if awesome restart, or return a string error.
133 local c
= checkfile(capi
.awesome
.conffile
)
135 if type(c
) ~= "function" then
139 capi
.awesome
.restart()
142 --- Get the user's config or cache dir.
143 -- It first checks XDG_CONFIG_HOME / XDG_CACHE_HOME, but then goes with the
145 -- @param d The directory to get (either "config" or "cache").
146 -- @return A string containing the requested path.
148 if d
== "config" then
149 local dir
= os
.getenv("XDG_CONFIG_HOME")
151 return dir
.. "/awesome"
153 return os
.getenv("HOME") .. "/.config/awesome"
154 elseif d
== "cache" then
155 local dir
= os
.getenv("XDG_CACHE_HOME")
157 return dir
.. "/awesome"
159 return os
.getenv("HOME").."/.cache/awesome"
163 --- Check if file exists and is readable.
164 -- @param filename The file path
165 -- @return True if file exists and readable.
166 function file_readable(filename
)
167 local file
= io
.open(filename
)
175 local function subset_mask_apply(mask
, set
)
179 rtable
.insert(ret
, set
[i
])
185 local function subset_next(mask
)
187 while i
<= #mask
and mask
[i
] do
199 --- Return all subsets of a specific set.
200 -- This function, giving a set, will return all subset it.
201 -- For example, if we consider a set with value { 10, 15, 34 },
202 -- it will return a table containing 2^n set:
203 -- { }, { 10 }, { 15 }, { 34 }, { 10, 15 }, { 10, 34 }, etc.
205 -- @return A table with all subset.
206 function subsets(set
)
209 for i
= 1, #set
do mask
[i
] = false end
211 -- Insert the empty one
212 rtable
.insert(ret
, {})
214 while subset_next(mask
) do
215 rtable
.insert(ret
, subset_mask_apply(mask
, set
))
220 --- Join all tables given as parameters.
221 -- This will iterate all tables and insert all their keys into a new table.
222 -- @param args A list of tables to join
223 -- @return A new table containing all keys from the arguments.
224 function table.join(...)
228 for k
, v
in pairs(arg
[i
]) do
229 if type(k
) == "number" then
230 rtable
.insert(ret
, v
)
240 -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80