From dd0f25ffa1397d815b7b2d21eff2c5231ceeb3b2 Mon Sep 17 00:00:00 2001 From: TheOnePath Date: Sat, 4 Feb 2023 18:16:58 +0000 Subject: [PATCH] Updated actions/init.lua Added actions@hyperlink_exec. Responsible for acting upon an execution call from keybinding. Function gets the contents of the buffer and makes call to actions@suburl. Added actions@suburl. This function specifically performs a substitution of text in a buffer line and replaces it with a hyperlink, formatted as markdown. --- lua/presser/actions/init.lua | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/lua/presser/actions/init.lua b/lua/presser/actions/init.lua index f2e09db..21484db 100644 --- a/lua/presser/actions/init.lua +++ b/lua/presser/actions/init.lua @@ -1,4 +1,5 @@ local gcm = require'presser.context_manager' +--local actions = require'presser.actions' local utils = require'presser.utils' local a = vim.api @@ -59,6 +60,34 @@ actions.execute = function () a.nvim_exec( cmd, false ) end +actions.hyperlink_exec = function () + local url = utils.clean_buf(read_buffer()[1]) + actions.close() + + if url == "" then return nil end + + local _, cur_start, _, cur_end = utils.get_selection() + local line = a.nvim_get_current_line() + if line == "" then + return + end + + local row, col = unpack(a.nvim_win_get_cursor(0)) + if cur_start == -1 then + cur_start, cur_end = unpack(utils.subslice(line .. " ", col)) + end + + actions.suburl( line, row, url, cur_start, cur_end ) +end + +actions.suburl = function (line, row, url, cur_start, cur_end) + local prefix = string.sub(line, 1, cur_start) -- the first part up to selection + local suffix = string.sub(line, cur_end + 1, line:len()) -- the last part after selection + local infix = "[" .. utils.clean_buf(string.sub(line, cur_start, cur_end)) .. "]" .. "(" .. url .. ")" + + a.nvim_buf_set_lines( 0, row-1, row, false, { prefix .. infix .. suffix } ) +end + actions.buf_put_curser_at = function ( label ) for _, ctx_obj in pairs( vim.g.presser_buf_ctx ) do for _, record in pairs( ctx_obj ) do