Add zettel link following style option

`g:zettel_link_following` can be set to `cursor` or `line` to change the
way zk detect the closest link to follow to (or create a new link for).

Setting it to `cursor` will look for the current word under the cursor and
decide if it is a followable link. `line` will look from the current
cursor position to the end of the line instead.
This commit is contained in:
Marty Oehme 2020-11-01 22:18:40 +01:00
parent aef7d29997
commit 59574a17e9
Signed by: Marty
GPG key ID: B7538B8F50A1C800
4 changed files with 37 additions and 9 deletions

View file

@ -1,11 +1,5 @@
local A = {}
-- Return only the link reference portion of a markdown/wiki style link
function A.extract_link(input)
if not input then return end
return input:match("%[%[(.+)|?.*%]%]") or input:match("%[.*%]%((.+)%)")
end
-- Extracts a file name from a link and opens the corresponding file
-- in the current buffer.
-- Takes an optional input parameter
@ -23,6 +17,12 @@ function A.open_selected()
A.open(A.get_link_under_cursor())
end
-- Return only the link reference portion of a markdown/wiki style link
function A.extract_link(input)
if not input then return end
return input:match("%[%[(.+)|?.*%]%]") or input:match("%[.*%]%((.+)%)")
end
-- Returns the word currently under cursor, the vim equivalent of yiW.
-- Takes an optional boolean flag to set the word being caught
-- to the vim equivalent of doing yiw, a more exclusive version.
@ -32,6 +32,7 @@ function A.get_link_under_cursor(small)
local word = vim.fn.expand(c)
return word
end
-- -- Returns the content of the line from the cursor onwards.
-- function A.get_next_link_on_line()
-- local line = vim.api.nvim_get_current_line()

View file

@ -1,14 +1,16 @@
local Opt = {}
-- setting defaults
local ZETTEL_EXTENSION = ".md"
local ZETTEL_LINK_STYLE = "markdown"
local ANCHOR_SEPARATOR = "_"
local ZETTEL_EXTENSION = ".md" -- ending of zettel files
local ZETTEL_LINK_STYLE = "markdown" -- "wiki", sets the style of link to use
local ZETTEL_LINK_FOLLOWING = "cursor" -- "line", sets the distance ahead to look for zettel links
local ANCHOR_SEPARATOR = "_" -- separtor between anchor and link text in md links
-- 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`
-- 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)
if type(set) ~= "table" then return false end
@ -31,6 +33,9 @@ function Opt.zettel()
must_contain(ZETTEL_LINK_STYLE_OPTIONS, options.link_style,
"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")
return options
end

View file

@ -43,4 +43,19 @@ describe("zettel options", function()
_G.vim.g.zettel_link_style = "idontbelong"
assert.is_error(function() opt.zettel() end)
end)
it("should return the global link following if set in vim", function()
_G.vim.g.zettel_link_following = "line"
assert.same("line", opt.zettel().link_following)
end)
it("should return the buffer link following if set in vim", function()
_G.vim.b.zettel_link_following = "line"
assert.same("line", opt.zettel().link_following)
end)
it("should return the default link following if not set in vim",
function() assert.same("cursor", opt.zettel().link_following) end)
it("should error on entries other than markdown/wiki", function()
_G.vim.g.zettel_link_following = "idontbelong"
assert.is_error(function() opt.zettel() end)
end)
end)