From 59574a17e97009083f6c5050c7f34b075fa82e0a Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Sun, 1 Nov 2020 22:18:40 +0100 Subject: [PATCH] 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. --- README.md | 7 +++++++ lua/zettelkasten/action.lua | 13 +++++++------ lua/zettelkasten/options.lua | 11 ++++++++--- lua/zettelkasten/options_spec.lua | 15 +++++++++++++++ 4 files changed, 37 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 6b52243..a9ea1c0 100644 --- a/README.md +++ b/README.md @@ -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 +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 * [ ] 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] link style (wiki/markdown) * [ ] custom link style? + * [ ] link detection/following (under word, next on line) * [ ] recursive dir lookup for zettel * [ ] zettel anchor regex ## 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) * [ ] zettel caching for big directories * [ ] backlinks (via rg for filename anchor?) diff --git a/lua/zettelkasten/action.lua b/lua/zettelkasten/action.lua index 441ae12..0f3dbd4 100644 --- a/lua/zettelkasten/action.lua +++ b/lua/zettelkasten/action.lua @@ -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() diff --git a/lua/zettelkasten/options.lua b/lua/zettelkasten/options.lua index bf80889..108edfc 100644 --- a/lua/zettelkasten/options.lua +++ b/lua/zettelkasten/options.lua @@ -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 diff --git a/lua/zettelkasten/options_spec.lua b/lua/zettelkasten/options_spec.lua index f1dda71..2a97239 100644 --- a/lua/zettelkasten/options_spec.lua +++ b/lua/zettelkasten/options_spec.lua @@ -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)