Rename imports and local vars to unify more

This commit is contained in:
Marty Oehme 2021-05-03 17:29:42 +02:00
parent 3f3e5ec7c2
commit 7c6882ac16
Signed by: Marty
GPG key ID: B7538B8F50A1C800
3 changed files with 16 additions and 13 deletions

View file

@ -16,6 +16,7 @@ start neovim with `nvim --cmd "set rtp+=$(pwd)" .` to automatically load the fi
* [ ] list filenames * [ ] list filenames
* [x] link following (to existing anchor) * [x] link following (to existing anchor)
* [x] fallback to filename if anchor invalid / not found * [x] fallback to filename if anchor invalid / not found
* [ ] maintain location list of previous jumps
* [ ] link creation (to existing note) * [ ] link creation (to existing note)
* [ ] list existing * [ ] list existing
* [ ] create link (md / wiki) * [ ] create link (md / wiki)

View file

@ -1,15 +1,15 @@
local A = {} local A = {}
local o = require 'zettelkasten.options' local o = require 'zettelkasten.options'
local link = require 'zettelkasten.link' local l = require 'zettelkasten.link'
local files = require 'zettelkasten.files' local f = require 'zettelkasten.files'
-- Opens the link passed in in the editor's current buffer. -- Opens the link passed in in the editor's current buffer.
-- Requires a link object passed in. -- Requires a link object passed in.
function A.open(zlink) function A.open(link)
if not zlink or not zlink.ref then return end if not link or not link.ref then return end
local fname = files.get_zettel_by_anchor(zlink.anchor) or local fname = f.get_zettel_by_anchor(link.anchor) or
files.get_zettel_by_ref(zlink.ref) or zlink.ref f.get_zettel_by_ref(link.ref) or link.ref
vim.api.nvim_command(string.format("edit %s", fname)) vim.api.nvim_command(string.format("edit %s", fname))
end end
@ -21,7 +21,7 @@ function A.open_selected(style)
local st = style or o.link().following local st = style or o.link().following
local curpos = vim.api.nvim_win_get_cursor(0)[2] local curpos = vim.api.nvim_win_get_cursor(0)[2]
local links = link.extract_all(vim.api.nvim_get_current_line()) local links = l.extract_all(vim.api.nvim_get_current_line())
local ln local ln
if st == 'line' then if st == 'line' then
@ -38,8 +38,10 @@ function A.create_link() return end
-- Returns the link currently under cursor, roughly the vim equivalent of yiW. -- Returns the link currently under cursor, roughly the vim equivalent of yiW.
-- Works for links containing spaces in their text or reference link. -- Works for links containing spaces in their text or reference link.
function A.get_link_under_cursor(links, curpos) function A.get_link_under_cursor(links, curpos)
for _, l in pairs(links) do for _, link in pairs(links) do
if l.startpos <= curpos + 1 and l.endpos > curpos then return l end if link.startpos <= curpos + 1 and link.endpos > curpos then
return link
end
end end
return nil return nil
end end

View file

@ -8,8 +8,8 @@ local parsers = {
ref = "%[.-%]%((.-)%)", ref = "%[.-%]%((.-)%)",
text = "%[(.-)%]%(.-%)", text = "%[(.-)%]%(.-%)",
style_func = function(anchor, text, extension) style_func = function(anchor, text, extension)
local link = (a.prepend(anchor, L.urlify(L.trimmed(text)))) local link = (a.prepend(anchor, L.urlify(L.trim(text))))
return "[" .. L.trimmed(text) .. "](" .. link .. extension .. ")" return "[" .. L.trim(text) .. "](" .. link .. extension .. ")"
end end
}, },
wiki = { wiki = {
@ -17,7 +17,7 @@ local parsers = {
text = "%[%[.-|?(.-)%]%]", text = "%[%[.-|?(.-)%]%]",
style_func = function(anchor, text) style_func = function(anchor, text)
local pipetxt = "" local pipetxt = ""
text = L.trimmed(text) text = L.trim(text)
if text and text ~= "" then pipetxt = "|" .. text end if text and text ~= "" then pipetxt = "|" .. text end
return "[[" .. anchor .. pipetxt .. "]]" return "[[" .. anchor .. pipetxt .. "]]"
@ -38,7 +38,7 @@ function L.append_extension(text) return text .. o.zettel().extension end
-- Returns text with surrounding whitespace trimmed. Returns empty string -- Returns text with surrounding whitespace trimmed. Returns empty string
-- if only whitespace. -- if only whitespace.
function L.trimmed(text) function L.trim(text)
if not text then return end if not text then return end
return text:match '^()%s*$' and '' or text:match '^%s*(.*%S)' return text:match '^()%s*$' and '' or text:match '^%s*(.*%S)'
end end