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
* [ ] 'rename' anchor (goes against stability?)
* [ ] recognize duplicate anchors (in directory, when listing, etc)
* [ ] potentially warn user
* [ ] provide option to rename and automatically change backlinks
* [ ] 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

View file

@ -2,16 +2,14 @@ local A = {}
local o = require 'zettelkasten.options'
local link = require 'zettelkasten.link'
local list = require 'zettelkasten.files'
local BIGNUMBER = 10000000
local files = require 'zettelkasten.files'
-- Opens the link passed in in the editor's current buffer.
-- 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
list.get_zettel_by_ref(zlink.ref) or zlink.ref
local fname = files.get_zettel_by_anchor(zlink.anchor) or
files.get_zettel_by_ref(zlink.ref) or zlink.ref
vim.api.nvim_command(string.format("edit %s", fname))
end
@ -25,13 +23,18 @@ function A.open_selected(style)
local curpos = vim.api.nvim_win_get_cursor(0)[2]
local links = link.extract_all(vim.api.nvim_get_current_line())
local ln
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
A.open(A.get_link_under_cursor(links, curpos))
ln = A.get_link_under_cursor(links, curpos)
end
A.open(ln)
end
function A.create_link() return end
-- Returns the link currently under cursor, roughly the vim equivalent of yiW.
-- Works for links containing spaces in their text or reference link.
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.
function A.get_next_link_on_line(links, curpos)
local nearestpos = BIGNUMBER
local nearestpos = math.huge
local nearestlink
for _, ln in pairs(links) do
if ln.endpos > curpos and ln.endpos < nearestpos then

View file

@ -10,7 +10,7 @@ local action = require 'zettelkasten.action'
-- table.
-- Recurses into subdirectories if recursive argument is true.
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
-- Return a valid zettelkasten anchor for the current time,