2020-10-31 13:17:26 +00:00
|
|
|
local A = {}
|
|
|
|
|
2020-11-02 20:51:14 +00:00
|
|
|
local o = require 'zettelkasten.options'
|
2020-11-04 21:04:31 +00:00
|
|
|
local link = require 'zettelkasten.link'
|
2021-04-30 13:30:43 +00:00
|
|
|
local list = require 'zettelkasten.files'
|
2020-11-02 20:51:14 +00:00
|
|
|
|
2020-11-04 20:29:23 +00:00
|
|
|
local BIGNUMBER = 10000000
|
2020-11-02 20:51:14 +00:00
|
|
|
|
2020-11-04 20:29:23 +00:00
|
|
|
-- Opens the link passed in in the editor's current buffer.
|
|
|
|
-- Requires a link object passed in.
|
2021-04-30 11:26:40 +00:00
|
|
|
function A.open(zlink)
|
|
|
|
if not zlink or not zlink.ref then return end
|
2021-04-30 14:28:22 +00:00
|
|
|
local fname = list.get_zettel_by_anchor(zlink.anchor) or
|
|
|
|
list.get_zettel_by_ref(zlink.ref) or zlink.ref
|
2020-11-06 16:28:05 +00:00
|
|
|
vim.api.nvim_command(string.format("edit %s", fname))
|
2020-10-31 13:17:26 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Gets the input at the current buffer cursor and opens it
|
|
|
|
-- in the current buffer.
|
2020-11-02 20:51:14 +00:00
|
|
|
-- Takes an optional style of link following to use,
|
|
|
|
-- superseding the one set in options.
|
|
|
|
function A.open_selected(style)
|
2021-04-30 11:26:40 +00:00
|
|
|
local st = style or o.link().following
|
2020-11-06 15:42:09 +00:00
|
|
|
|
|
|
|
local curpos = vim.api.nvim_win_get_cursor(0)[2]
|
|
|
|
local links = link.extract_all(vim.api.nvim_get_current_line())
|
|
|
|
|
2021-04-30 11:26:40 +00:00
|
|
|
if st == 'line' then
|
2020-11-06 15:42:09 +00:00
|
|
|
A.open(A.get_next_link_on_line(links, curpos))
|
2021-04-30 11:26:40 +00:00
|
|
|
elseif st == 'cursor' then
|
2020-11-06 15:42:09 +00:00
|
|
|
A.open(A.get_link_under_cursor(links, curpos))
|
2020-11-02 20:51:14 +00:00
|
|
|
end
|
2020-10-31 13:17:26 +00:00
|
|
|
end
|
|
|
|
|
2020-11-04 20:29:23 +00:00
|
|
|
-- Returns the link currently under cursor, roughly the vim equivalent of yiW.
|
|
|
|
-- Works for links containing spaces in their text or reference link.
|
2020-11-06 15:42:09 +00:00
|
|
|
function A.get_link_under_cursor(links, curpos)
|
2021-04-30 11:26:40 +00:00
|
|
|
for _, l in pairs(links) do
|
|
|
|
if l.startpos <= curpos + 1 and l.endpos > curpos then return l end
|
2020-11-04 20:29:23 +00:00
|
|
|
end
|
|
|
|
return nil
|
2020-10-31 13:17:26 +00:00
|
|
|
end
|
2020-11-01 21:18:40 +00:00
|
|
|
|
2020-11-04 21:04:31 +00:00
|
|
|
-- Returns the next link of the current line from the cursor onwards.
|
2020-11-06 15:42:09 +00:00
|
|
|
function A.get_next_link_on_line(links, curpos)
|
2020-11-04 20:29:23 +00:00
|
|
|
local nearestpos = BIGNUMBER
|
|
|
|
local nearestlink
|
2021-04-30 14:10:16 +00:00
|
|
|
for _, ln in pairs(links) do
|
|
|
|
if ln.endpos > curpos and ln.endpos < nearestpos then
|
|
|
|
nearestpos = ln.endpos
|
|
|
|
nearestlink = ln
|
2020-11-04 20:29:23 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
return nearestlink
|
2020-11-02 20:51:14 +00:00
|
|
|
end
|
2020-10-31 13:17:26 +00:00
|
|
|
|
|
|
|
return {open = A.open, open_selected = A.open_selected}
|
2020-11-06 15:01:51 +00:00
|
|
|
|
|
|
|
--- local function get_selection()
|
|
|
|
--- s_start = vim.fn.line("'<") - 1
|
|
|
|
--- s_end = vim.fn.line("'>")
|
|
|
|
--- return vim.api.nvim_buf_get_lines(0, s_start, s_end, true)
|
|
|
|
--- end
|
|
|
|
--
|
|
|
|
--- -- UGLY HACKS ABOUND
|
|
|
|
--- function ZK.create_zettel()
|
|
|
|
--- -- get line and its number
|
|
|
|
--- local selection
|
|
|
|
--- local line = vim.api.nvim_get_current_line()
|
|
|
|
--- local linenr = vim.api.nvim_win_get_cursor(0)[1]
|
|
|
|
--
|
|
|
|
--- -- get words under cursor / selected
|
|
|
|
--- local mode = vim.api.nvim_get_mode()['mode']
|
|
|
|
--- if mode == "n" then
|
|
|
|
--- print(vim.fn.line("'<'") - 1)
|
|
|
|
--- selection = vim.fn.expand("<cWORD>")
|
|
|
|
--- -- NOT WORKING YET
|
|
|
|
--- elseif mode == "v" then
|
|
|
|
--- selection = get_selection()
|
|
|
|
--- else
|
|
|
|
--- return
|
|
|
|
--- end
|
|
|
|
--
|
|
|
|
--- -- get valid link
|
|
|
|
--- local link = l.create(nil, selection)
|
|
|
|
--
|
|
|
|
--- -- create new line with selection replaced in middle
|
|
|
|
--- local st, en = line:find(selection, 0, true)
|
|
|
|
--- local repl_line = line:sub(1, st - 1) .. link .. line:sub(en + 1)
|
|
|
|
--
|
|
|
|
--- -- replace existing line in favor of new one
|
|
|
|
--- vim.api.nvim_buf_set_lines(0, linenr - 1, linenr, true, {repl_line})
|
|
|
|
--- end
|