From da52a7b197bdf0b46b4938980121eceb3327f15f Mon Sep 17 00:00:00 2001 From: Julien Danjou Date: Tue, 30 Sep 2008 15:50:41 +0200 Subject: [PATCH] awful: add helper to check configuration file syntax Signed-off-by: Julien Danjou --- awesomerc.lua.in | 5 ++++- lib/awful/util.lua.in | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/awesomerc.lua.in b/awesomerc.lua.in index af9d3b81..6eec8ab4 100644 --- a/awesomerc.lua.in +++ b/awesomerc.lua.in @@ -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 diff --git a/lib/awful/util.lua.in b/lib/awful/util.lua.in index be2369bf..295cebac 100644 --- a/lib/awful/util.lua.in +++ b/lib/awful/util.lua.in @@ -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