awful: add helper to check configuration file syntax

Signed-off-by: Julien Danjou <julien@danjou.info>
This commit is contained in:
Julien Danjou 2008-09-30 15:50:41 +02:00
parent 116de39142
commit da52a7b197
2 changed files with 42 additions and 1 deletions

View File

@ -217,7 +217,10 @@ keybinding({ modkey }, "Escape", awful.tag.history.restore):add()
-- Standard program
keybinding({ modkey }, "Return", function () awful.util.spawn(terminal) end):add()
keybinding({ modkey, "Control" }, "r", awesome.restart):add()
keybinding({ modkey, "Control" }, "r", function ()
mypromptbox.text =
awful.util.escape(awful.util.restart())
end):add()
keybinding({ modkey, "Shift" }, "q", awesome.quit):add()
-- Client manipulation

View File

@ -11,6 +11,7 @@ local assert = assert
local loadstring = loadstring
local debug = debug
local print = print
local type = type
local capi =
{
awesome = awesome,
@ -109,4 +110,41 @@ function unescape(text)
return text
end
--- Check if a file is a Lua valid file.
-- This is done by loading the content and compiling it with loadstring().
-- @param path The file path.
-- @return A function if everything is alright, a string with the error
-- otherwise.
function checkfile(path)
if not path then return "E: no file given" end
local f = io.open(path)
if not f then
return "E: unable to open file " .. path
end
local s = f:read("*all")
f:close()
local f, e = loadstring(s)
-- Return function if function, otherwise return error.
if f then return f end
return e
end
--- Try to restart awesome.
-- It checks if the configuration file is valid, and then restart if it's ok.
-- If it's not ok, the error will be returned.
-- @return Never return if awesome restart, or return a string error.
function restart()
local c = checkfile(capi.awesome.conffile())
if type(c) ~= "function" then
return c
end
capi.awesome.restart()
end
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80