diff --git a/lua/zettelkasten/action.lua b/lua/zettelkasten/action.lua index 3e7a1ef..59a956e 100644 --- a/lua/zettelkasten/action.lua +++ b/lua/zettelkasten/action.lua @@ -33,7 +33,60 @@ function A.open_selected(style) A.open(ln) 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. -- 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 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() --- s_start = vim.fn.line("'<") - 1 diff --git a/lua/zettelkasten/action_spec.lua b/lua/zettelkasten/action_spec.lua index 95b3c2f..f87ca55 100644 --- a/lua/zettelkasten/action_spec.lua +++ b/lua/zettelkasten/action_spec.lua @@ -105,3 +105,14 @@ describe("open_selected", function() 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) diff --git a/lua/zettelkasten/init.lua b/lua/zettelkasten/init.lua index c12a727..292d103 100644 --- a/lua/zettelkasten/init.lua +++ b/lua/zettelkasten/init.lua @@ -20,8 +20,12 @@ function ZK.get_anchor() return anchor.create() end -- Open link under cursor, or next on line function ZK.open_link() return action.open_selected() end +-- Create a new link under cursor +function ZK.create_link() return action.link() end + return { get_zettel_list = ZK.get_zettel_list, get_anchor = ZK.get_anchor, - open_link = ZK.open_link + open_link = ZK.open_link, + create_link = ZK.create_link } diff --git a/plugin/zettelkasten.vim b/plugin/zettelkasten.vim index 8476b75..4b77862 100644 --- a/plugin/zettelkasten.vim +++ b/plugin/zettelkasten.vim @@ -13,6 +13,10 @@ augroup Zettelkasten autocmd! augroup END +command! ZKOpen lua require('zettelkasten').open_link() + +command! -range ZKCreate lua require('zettelkasten').create_link() + " example plug mappings " nnoremap Zettel_Link :call zettelkasten#zettel_link() nnoremap zettel_link_open :lua require 'zettelkasten'.open_link()