Expand '~' in the path for beautiful.init
[awesome.git] / lib / beautiful.lua.in
blob54856821b266288648f1bdbabc138515ca0621d9
1 ----------------------------------------------------------------------------
2 -- @author Damien Leone <damien.leone@gmail.com>
3 -- @author Julien Danjou <julien@danjou.info>
4 -- @copyright 2008-2009 Damien Leone, Julien Danjou
5 -- @release @AWESOME_VERSION@
6 ----------------------------------------------------------------------------
8 -- Grab environment
9 local os = os
10 local print = print
11 local pcall = pcall
12 local pairs = pairs
13 local type = type
14 local dofile = dofile
15 local setmetatable = setmetatable
16 local util = require("awful.util")
17 local lgi = require("lgi")
18 local cairo = lgi.cairo
19 local Pango = lgi.Pango
20 local PangoCairo = lgi.PangoCairo
21 local capi =
23 screen = screen,
24 awesome = awesome
27 --- Theme library.
28 local beautiful = { mt = {} }
30 -- Local data
31 local theme = {}
32 local descs = setmetatable({}, { __mode = 'k' })
33 local fonts = setmetatable({}, { __mode = 'v' })
34 local active_font
36 local function load_font(name)
37 name = name or active_font
38 if name and type(name) ~= "string" and descs[name] then
39 name = descs[name]
40 end
41 if fonts[name] then
42 return fonts[name]
43 end
45 -- Load new font
46 local desc = Pango.FontDescription.from_string(name)
47 local ctx = PangoCairo.font_map_get_default():create_context()
49 -- Apply default values from the context (e.g. a default font size)
50 desc:merge(ctx:get_font_description(), false)
52 -- Calculate fond height
53 local metrics = ctx:get_metrics(desc, nil)
54 local height = math.ceil((metrics:get_ascent() + metrics:get_descent()) / Pango.SCALE)
56 local font = { name = name, description = desc, height = height }
57 fonts[name] = font
58 descs[desc] = name
59 return font
60 end
62 local function set_font(name)
63 active_font = load_font(name).name
64 end
66 function beautiful.get_font(name)
67 return load_font(name).description
68 end
70 function beautiful.get_font_height(name)
71 return load_font(name).height
72 end
74 --- Init function, should be runned at the beginning of configuration file.
75 -- @param path The theme file path.
76 function beautiful.init(path)
77 if path then
78 local success
80 -- try and grab user's $HOME directory and expand '~'
81 local homedir = os.getenv("HOME")
82 path = path:gsub("^~/", homedir .. "/")
84 success, theme = pcall(function() return dofile(path) end)
86 if not success then
87 return print("E: beautiful: error loading theme file " .. theme)
88 elseif theme then
89 -- expand '~'
90 if homedir then
91 for k, v in pairs(theme) do
92 if type(v) == "string" then theme[k] = v:gsub("^~/", homedir .. "/") end
93 end
94 end
96 if theme.font then set_font(theme.font) end
97 else
98 return print("E: beautiful: error loading theme file " .. path)
99 end
100 else
101 return print("E: beautiful: error loading theme: no path specified")
105 --- Get the current theme.
106 -- @return The current theme table.
107 function beautiful.get()
108 return theme
111 function beautiful.mt:__index(k)
112 return theme[k]
115 -- Set the default font
116 set_font("sans 8")
118 return setmetatable(beautiful, beautiful.mt)
120 -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80