Remove bignumber allocation, refactor open function

Switched out manual BIGNUMBER creation for the lua inbuilt `math.huge`.
Refactored `open_selected` a tiny bit in preparation for automatic link
creation.
This commit is contained in:
Marty Oehme 2021-04-30 17:32:03 +02:00
parent f615dd0fbd
commit 3f3e5ec7c2
Signed by: Marty
GPG Key ID: B7538B8F50A1C800
3 changed files with 13 additions and 9 deletions

View File

@ -67,6 +67,7 @@ start neovim with `nvim --cmd "set rtp+=$(pwd)" .` to automatically load the fi
* [ ] add missing anchors * [ ] add missing anchors
* [ ] 'rename' anchor (goes against stability?) * [ ] 'rename' anchor (goes against stability?)
* [ ] recognize duplicate anchors (in directory, when listing, etc) * [ ] recognize duplicate anchors (in directory, when listing, etc)
* [ ] potentially warn user
* [ ] provide option to rename and automatically change backlinks * [ ] provide option to rename and automatically change backlinks
* [ ] zettel 'lens' (preview first headline + content of linked zettel through floating window etc, on keypress) * [ ] zettel 'lens' (preview first headline + content of linked zettel through floating window etc, on keypress)
* [ ] support *both* md-style and wiki-style links at the same time * [ ] support *both* md-style and wiki-style links at the same time

View File

@ -2,16 +2,14 @@ local A = {}
local o = require 'zettelkasten.options' local o = require 'zettelkasten.options'
local link = require 'zettelkasten.link' local link = require 'zettelkasten.link'
local list = require 'zettelkasten.files' local files = require 'zettelkasten.files'
local BIGNUMBER = 10000000
-- 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(zlink)
if not zlink or not zlink.ref then return end if not zlink or not zlink.ref then return end
local fname = list.get_zettel_by_anchor(zlink.anchor) or local fname = files.get_zettel_by_anchor(zlink.anchor) or
list.get_zettel_by_ref(zlink.ref) or zlink.ref files.get_zettel_by_ref(zlink.ref) or zlink.ref
vim.api.nvim_command(string.format("edit %s", fname)) vim.api.nvim_command(string.format("edit %s", fname))
end end
@ -25,13 +23,18 @@ function A.open_selected(style)
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 = link.extract_all(vim.api.nvim_get_current_line())
local ln
if st == 'line' then if st == 'line' then
A.open(A.get_next_link_on_line(links, curpos)) ln = A.get_next_link_on_line(links, curpos)
elseif st == 'cursor' then elseif st == 'cursor' then
A.open(A.get_link_under_cursor(links, curpos)) ln = A.get_link_under_cursor(links, curpos)
end end
A.open(ln)
end end
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)
@ -43,7 +46,7 @@ end
-- Returns the next link of the current line from the cursor onwards. -- Returns the next link of the current line from the cursor onwards.
function A.get_next_link_on_line(links, curpos) function A.get_next_link_on_line(links, curpos)
local nearestpos = BIGNUMBER local nearestpos = math.huge
local nearestlink local nearestlink
for _, ln in pairs(links) do for _, ln in pairs(links) do
if ln.endpos > curpos and ln.endpos < nearestpos then if ln.endpos > curpos and ln.endpos < nearestpos then

View File

@ -10,7 +10,7 @@ local action = require 'zettelkasten.action'
-- table. -- table.
-- Recurses into subdirectories if recursive argument is true. -- Recurses into subdirectories if recursive argument is true.
function ZK.get_zettel_list(path, recursive) function ZK.get_zettel_list(path, recursive)
return ls.get_anchors_and_paths(path, recursive or false) return ls.get_anchors_and_paths(ls.get_all_files(path, recursive or false))
end end
-- Return a valid zettelkasten anchor for the current time, -- Return a valid zettelkasten anchor for the current time,