Refactor option setting with defaults

Now reads from an existing defaults table, which has the required keys:

'vimname', the setting name in vim itself (for the user)
'default', the value it should contain by default

and the optional key

'valid', if the option should be restricted to a certain valid value set
This commit is contained in:
Marty Oehme 2020-11-04 15:14:36 +01:00
parent ba033e2b24
commit 587fa47268
Signed by: Marty
GPG key ID: B7538B8F50A1C800

View file

@ -1,17 +1,27 @@
local Opt = {} local Opt = {}
-- setting defaults -- vim setting names and defaults
local ZETTEL_EXTENSION = ".md" -- ending of zettel files local zettel_defaults = {
local ZETTEL_LINK_STYLE = "markdown" -- "wiki", sets the style of link to use extension = {vimname = "zettel_extension", default = ".md"},
local ZETTEL_LINK_FOLLOWING = "cursor" -- "line", sets the distance ahead to look for zettel links link_style = {
local ANCHOR_SEPARATOR = "_" -- separtor between anchor and link text in md links vimname = "zettel_link_style",
default = "markdown",
valid = {markdown = true, wiki = true}
},
link_following = {
vimname = "zettel_link_following",
default = "cursor",
valid = {cursor = true, line = true}
}
}
local anchor_defaults = {
separator = {vimname = "zettel_anchor_separator", default = "_"}
}
-- remaining options
-- TODO zettel_root = vim.g["zettel_root"] or vim.b["zettel_root"] or "~/documents/notes", -- TODO zettel_root = vim.g["zettel_root"] or vim.b["zettel_root"] or "~/documents/notes",
-- TODO zettel_anchor_pattern = regex? -> needs custom creation function in `create_anchor` -- TODO zettel_anchor_pattern = regex? -> needs custom creation function in `create_anchor`
-- constricted option sets
local ZETTEL_LINK_STYLE_OPTIONS = {markdown = true, wiki = true}
local ZETTEL_LINK_FOLLOWING_OPTIONS = { cursor = true, line = true }
local function must_contain(set, value, name) local function must_contain(set, value, name)
if type(set) ~= "table" then return false end if type(set) ~= "table" then return false end
if not set[value] then if not set[value] then
@ -22,28 +32,25 @@ local function must_contain(set, value, name)
end end
end end
function Opt.zettel() local function get_options(defaults)
local options = {} local options = {}
options.extension = local def = defaults
vim.g["zettel_extension"] or vim.b["zettel_extension"] or for opt, v in pairs(def) do
ZETTEL_EXTENSION
options.link_style = vim.g["zettel_link_style"] or -- check for vim options set (globally or buffer), otherwise use default value
vim.b["zettel_link_style"] or ZETTEL_LINK_STYLE options[opt] = vim.g[def[opt].vimname] or vim.b[def[opt].vimname] or
must_contain(ZETTEL_LINK_STYLE_OPTIONS, options.link_style, def[opt].default
"zettel_link_style")
options.link_following = vim.g["zettel_link_following"] or vim.b["zettel_link_following"] or ZETTEL_LINK_FOLLOWING
must_contain(ZETTEL_LINK_FOLLOWING_OPTIONS, options.link_following, "zettel_link_following")
-- check correct option set for constrained value sets
if def[opt].valid then
must_contain(def[opt].valid, options[opt], def[opt].name)
end
end
return options return options
end end
function Opt.anchor() function Opt.zettel() return get_options(zettel_defaults) end
local options = {}
options.separator = vim.g["zettel_anchor_separator"] or function Opt.anchor() return get_options(anchor_defaults) end
vim.b["zettel_anchor_separator"] or ANCHOR_SEPARATOR
return options
end
return Opt return Opt