diff --git a/lib/gears/filesystem.lua b/lib/gears/filesystem.lua index eff5dbcf1..9c0e340fb 100644 --- a/lib/gears/filesystem.lua +++ b/lib/gears/filesystem.lua @@ -6,6 +6,8 @@ -- Grab environment we need local Gio = require("lgi").Gio +local gstring = require("gears.string") +local gtable = require("gears.table") local filesystem = {} @@ -66,6 +68,21 @@ function filesystem.get_xdg_cache_home() return (os.getenv("XDG_CACHE_HOME") or os.getenv("HOME") .. "/.cache") .. "/" end +--- Get the data home according to the XDG basedir specification. +-- @treturn string the data home (XDG_DATA_HOME) with a slash at the end. +function filesystem.get_xdg_data_home() + return (os.getenv("XDG_DATA_HOME") or os.getenv("HOME") .. "/.local/share") .. "/" +end + +--- Get the data dirs according to the XDG basedir specification. +-- @treturn table the data dirs (XDG_DATA_DIRS) with a slash at the end of each entry. +function filesystem.get_xdg_data_dirs() + local xdg_data_dirs = os.getenv("XDG_DATA_DIRS") or "/usr/share:/usr/local/share" + return gtable.map( + function(dir) return dir .. "/" end, + gstring.split(xdg_data_dirs, ":")) +end + --- Get the path to the user's config dir. -- This is the directory containing the configuration file ("rc.lua"). -- @return A string with the requested path with a slash at the end. diff --git a/lib/gears/string.lua b/lib/gears/string.lua index 00b181eb3..b11da8171 100644 --- a/lib/gears/string.lua +++ b/lib/gears/string.lua @@ -84,4 +84,26 @@ function gstring.query_to_pattern(q) end) return s end + +--- Split separates a string containing a delimiter into the list of +-- substrings between that delimiter. +-- @class function +-- @name split +-- @tparam string str String to be splitted +-- @tparam string delimiter Character where the string will be splitted +-- @treturn table list of the substrings +function gstring.split(str, delimiter) + local pattern = "(.-)" .. delimiter .. "()" + local result = {} + local n = 0 + local lastPos = 0 + for part, pos in string.gmatch(str, pattern) do + n = n + 1 + result[n] = part + lastPos = pos + end + result[n + 1] = string.sub(str, lastPos) + return result +end + return gstring diff --git a/lib/gears/table.lua b/lib/gears/table.lua index 95c528a0b..54ceab4be 100644 --- a/lib/gears/table.lua +++ b/lib/gears/table.lua @@ -212,5 +212,20 @@ function gtable.merge(t, set) return t end +--- Map a function to a table. The function is applied to each value +-- on the table, returning a table of the results. +-- @class function +-- @name map +-- @tparam function f the function to be applied to each value on the table +-- @tparam table tbl the container table whose values will be operated on +-- @treturn table Return a table of the results +function gtable.map(f, tbl) + local t = {} + for k,v in pairs(tbl) do + t[k] = f(v) + end + return t +end + return gtable diff --git a/lib/menubar/menu_gen.lua b/lib/menubar/menu_gen.lua index d1455d874..87cbeb206 100644 --- a/lib/menubar/menu_gen.lua +++ b/lib/menubar/menu_gen.lua @@ -7,6 +7,8 @@ --------------------------------------------------------------------------- -- Grab environment +local gtable = require("gears.table") +local gfilesystem = require("gears.filesystem") local utils = require("menubar.utils") local icon_theme = require("menubar.icon_theme") local pairs = pairs @@ -18,14 +20,16 @@ local menu_gen = {} -- Options section -local data_dir = os.getenv("XDG_DATA_HOME") -if not data_dir then - data_dir = os.getenv("HOME") .. '/.local/share' +--- Get the path to the directories where XDG menu applications are installed. +local function get_xdg_menu_dirs() + local dirs = gfilesystem.get_xdg_data_dirs() + table.insert(dirs, 1, gfilesystem.get_xdg_data_home()) + return gtable.map(function(dir) return dir .. 'applications/' end, dirs) end --- Specifies all directories where menubar should look for .desktop -- files. The search is recursive. -menu_gen.all_menu_dirs = { data_dir .. '/applications/', '/usr/share/applications/', '/usr/local/share/applications/' } +menu_gen.all_menu_dirs = get_xdg_menu_dirs() --- Specify the mapping of .desktop Categories section to the -- categories in the menubar. If "use" flag is set to false then any of