From fdcb0f2a93fdfa6289e373f1268b2c9bc908d21b Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Fri, 30 Apr 2021 16:28:22 +0200 Subject: [PATCH] Add opening zettel by reference if no anchor found Opens zettel by linked value, most often a direct path link, but will also look through the root dir to find a(n exactly) matching basename to open. If nothing is found in the root dir will open a new file to write with the corresponding name. Careful, if the reference is a full path definition, and the file does not exist, it will still open the corresponding file at the correct location but when attempting to save will generally complain if parts of the path are missing. They have to be created manually or in some other place, this is outside the scope of this plugin. --- README.md | 1 + lua/zettelkasten/action.lua | 3 ++- lua/zettelkasten/action_spec.lua | 3 ++- lua/zettelkasten/files.lua | 4 ++++ 4 files changed, 9 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 63a2fac..3f8aa94 100644 --- a/README.md +++ b/README.md @@ -81,6 +81,7 @@ next up: * i.e. if 2030101200 exists move to 2030101159, 2030101158, ... * if moving backwards, we do not take away id space from *future* note creation * if moving forwards, every zettel created within a minute would delay next zettel creation *another* minute +* to decide: should zettel creation create a zettel in current working dir or at zettel root dir? or set by option? * [ ] (CODE) switch -- comments to --- doc comments for function descriptions etc diff --git a/lua/zettelkasten/action.lua b/lua/zettelkasten/action.lua index 5c77270..0319193 100644 --- a/lua/zettelkasten/action.lua +++ b/lua/zettelkasten/action.lua @@ -10,7 +10,8 @@ local BIGNUMBER = 10000000 -- Requires a link object passed in. function A.open(zlink) if not zlink or not zlink.ref then return end - local fname = list.get_zettel_by_anchor(zlink.anchor) or zlink.ref + local fname = list.get_zettel_by_anchor(zlink.anchor) or + list.get_zettel_by_ref(zlink.ref) or zlink.ref vim.api.nvim_command(string.format("edit %s", fname)) end diff --git a/lua/zettelkasten/action_spec.lua b/lua/zettelkasten/action_spec.lua index dc0077e..95b3c2f 100644 --- a/lua/zettelkasten/action_spec.lua +++ b/lua/zettelkasten/action_spec.lua @@ -17,7 +17,8 @@ describe("open", function() vim.fn = {expand = function() end} assert.is_not_error(action.open) end) - it("should use the anchor to open the corresponding zettel", function() + it("should first use the anchor to open the corresponding zettel", + function() vim.api = {nvim_command = mock(function() end)} local ls = stub(require 'zettelkasten.files', "get_zettel_by_anchor") diff --git a/lua/zettelkasten/files.lua b/lua/zettelkasten/files.lua index 1e23065..8b6d987 100644 --- a/lua/zettelkasten/files.lua +++ b/lua/zettelkasten/files.lua @@ -73,6 +73,10 @@ function ls.get_zettel_by_anchor(anchor, files) end -- Returns the path to the zettel defined by a reference link. +-- Will prefer a fully matching path to only matching basename +-- of a file, but if only basename is found will return first +-- matching one. +-- -- Takes a set of files as optional variable in. -- If no set provided will use the (recursive) results -- of zettel_root directory.