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

@ -4,6 +4,11 @@ To develop / debug:
start neovim with `nvim --cmd "set rtp+=$(pwd)" .` to automatically load the files in project dir as if they were on path start neovim with `nvim --cmd "set rtp+=$(pwd)" .` to automatically load the files in project dir as if they were on path
next up:
* link following option (under cursor, next on line)
* next link on line function (actions)
* helper function to decide which one to use from A.open
## TODO: needed functionality ## TODO: needed functionality
* [ ] note creation (new anchor) * [ ] note creation (new anchor)
@ -27,11 +32,13 @@ start neovim with `nvim --cmd "set rtp+=$(pwd)" .` to automatically load the fi
* [x] zettel extension * [x] zettel extension
* [x] link style (wiki/markdown) * [x] link style (wiki/markdown)
* [ ] custom link style? * [ ] custom link style?
* [ ] link detection/following (under word, next on line)
* [ ] recursive dir lookup for zettel * [ ] recursive dir lookup for zettel
* [ ] zettel anchor regex * [ ] zettel anchor regex
## TODO: nice-to-haves ## TODO: nice-to-haves
* [ ] refactor parsers (md/wiki) to be tables of functions/regex in options, so e.g. valid link detection can call `options.parser.isValidLink(link)` or transformation `options.parser.styleLink(anchor, text)`
* [ ] completion engine (e.g. for `completion-nvim`, look in completion_buffers/completion-tags for reference) * [ ] completion engine (e.g. for `completion-nvim`, look in completion_buffers/completion-tags for reference)
* [ ] zettel caching for big directories * [ ] zettel caching for big directories
* [ ] backlinks (via rg for filename anchor?) * [ ] backlinks (via rg for filename anchor?)

View file

@ -1,11 +1,5 @@
local A = {} 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 -- Extracts a file name from a link and opens the corresponding file
-- in the current buffer. -- in the current buffer.
-- Takes an optional input parameter -- Takes an optional input parameter
@ -23,6 +17,12 @@ function A.open_selected()
A.open(A.get_link_under_cursor()) A.open(A.get_link_under_cursor())
end 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. -- Returns the word currently under cursor, the vim equivalent of yiW.
-- Takes an optional boolean flag to set the word being caught -- Takes an optional boolean flag to set the word being caught
-- to the vim equivalent of doing yiw, a more exclusive version. -- 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) local word = vim.fn.expand(c)
return word return word
end end
-- -- Returns the content of the line from the cursor onwards. -- -- Returns the content of the line from the cursor onwards.
-- function A.get_next_link_on_line() -- function A.get_next_link_on_line()
-- local line = vim.api.nvim_get_current_line() -- local line = vim.api.nvim_get_current_line()

View file

@ -1,14 +1,16 @@
local Opt = {} local Opt = {}
-- setting defaults -- setting defaults
local ZETTEL_EXTENSION = ".md" local ZETTEL_EXTENSION = ".md" -- ending of zettel files
local ZETTEL_LINK_STYLE = "markdown" local ZETTEL_LINK_STYLE = "markdown" -- "wiki", sets the style of link to use
local ANCHOR_SEPARATOR = "_" 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_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 -- constricted option sets
local ZETTEL_LINK_STYLE_OPTIONS = {markdown = true, wiki = true} 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
@ -31,6 +33,9 @@ function Opt.zettel()
must_contain(ZETTEL_LINK_STYLE_OPTIONS, options.link_style, must_contain(ZETTEL_LINK_STYLE_OPTIONS, options.link_style,
"zettel_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 return options
end end

View file

@ -43,4 +43,19 @@ describe("zettel options", function()
_G.vim.g.zettel_link_style = "idontbelong" _G.vim.g.zettel_link_style = "idontbelong"
assert.is_error(function() opt.zettel() end) assert.is_error(function() opt.zettel() end)
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) end)