Implement initial selection link creation

This commit is contained in:
Marty Oehme 2021-05-03 17:31:28 +02:00
parent 7c6882ac16
commit 2310f4a2d0
Signed by: Marty
GPG key ID: B7538B8F50A1C800
4 changed files with 75 additions and 3 deletions

View file

@ -33,7 +33,60 @@ function A.open_selected(style)
A.open(ln) A.open(ln)
end end
function A.create_link() return end -- Returns visually selected text and line, start and end cursor position.
-- Works with selections over multiple lines, but will only return the
-- starting line, as well as the starting line's text.
local function get_current_selection()
local line, start_col, end_col = vim.fn.getpos("'<")[2],
vim.fn.getpos("'<")[3],
vim.fn.getpos("'>")[3]
local selection = vim.fn.getline(line, line)[1]:sub(start_col, end_col)
return selection, {line, start_col, end_col}
end
-- Returns (big) word currently under cursor, a list of current line,
-- start and end cursor position.
local function get_current_word_big()
local curpos = vim.api.nvim_win_get_cursor()
local ln = vim.api.nvim_get_current_line()
print(ln, curpos)
-- return selection, {line, start_col, end_col}
end
-- Replaces the input text on the current line with a zettel link.
-- Takes an optional initial column on which the text to be replaced starts,
-- which can prevent falsely substituting the wrong text fragment if an
-- identical one exists earlier on the line.
function A.substitute_text(newtext, start_col)
local link = l.new(newtext)
local line_full = vim.api.nvim_get_current_line()
local line_edited
if start_col then
line_edited = line_full:sub(1, start_col - 1) ..
line_full:sub(start_col):gsub(newtext, link, 1)
else
line_edited = line_full:gsub(newtext, link, 1)
end
return line_edited
end
-- Replaces the current text context with a link to a new zettel.
-- The current context is the visual selection (if called from visual mode)
-- or the (big) word under the cursor if called from any other mode.
function A.link()
local selection, position
-- if vim.api.nvim_get_mode()['mode'] == "v" then
-- selection, position = get_current_selection()
-- else
-- print(vim.api.nvim_get_mode()['mode'])
-- end
selection, position = get_current_word_big()
vim.api.nvim_set_current_line(A.substitute_text(selection, position[2]))
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.
@ -59,7 +112,7 @@ function A.get_next_link_on_line(links, curpos)
return nearestlink return nearestlink
end end
return {open = A.open, open_selected = A.open_selected} return {open = A.open, open_selected = A.open_selected, link = A.link}
--- local function get_selection() --- local function get_selection()
--- s_start = vim.fn.line("'<") - 1 --- s_start = vim.fn.line("'<") - 1

View file

@ -105,3 +105,14 @@ describe("open_selected", function()
end) end)
end) end)
end) end)
describe("create_link", function()
it("substitutes the argument text with a link", function()
-- vim.fn = {
-- getpos = function() return {0, 0, 0, 0} end,
-- getline = function() return "testline" end
-- }
vim.cmd = function() end
action.create_link("my selection", 1, 1, 37)
end)
end)

View file

@ -20,8 +20,12 @@ function ZK.get_anchor() return anchor.create() end
-- Open link under cursor, or next on line -- Open link under cursor, or next on line
function ZK.open_link() return action.open_selected() end function ZK.open_link() return action.open_selected() end
-- Create a new link under cursor
function ZK.create_link() return action.link() end
return { return {
get_zettel_list = ZK.get_zettel_list, get_zettel_list = ZK.get_zettel_list,
get_anchor = ZK.get_anchor, get_anchor = ZK.get_anchor,
open_link = ZK.open_link open_link = ZK.open_link,
create_link = ZK.create_link
} }

View file

@ -13,6 +13,10 @@ augroup Zettelkasten
autocmd! autocmd!
augroup END augroup END
command! ZKOpen lua require('zettelkasten').open_link()
command! -range ZKCreate lua require('zettelkasten').create_link()
" example plug mappings " example plug mappings
" nnoremap <Plug>Zettel_Link :call zettelkasten#zettel_link()<cr> " nnoremap <Plug>Zettel_Link :call zettelkasten#zettel_link()<cr>
nnoremap <Plug>zettel_link_open :lua require 'zettelkasten'.open_link()<cr> nnoremap <Plug>zettel_link_open :lua require 'zettelkasten'.open_link()<cr>