presser.nvim/lua/presser/builtins/__modules.lua
TheOnePath b59715ae67
Updated builtins/__modules.lua
Added modules@hyperlink. A new presser tool which will convert text to a
hyperlink in markdown format.
	- Firstly the function identifies the mode. If visual-mode, the text
	  selected is used. If normal-mode, the text used is whatever is
	  identified under the cursor.
	- Secondly, if the text is identified to be a valid URI schema, then
	  hyperlink will automatically make the conversion.
	- If the text isn't a URI schema, then a new steamer is created to
	  allow for the user to enter a URI.
2023-02-04 18:30:29 +00:00

211 lines
6.0 KiB
Lua

local gcm = require 'presser.context_manager'
local steamers = require 'presser.steamers'
local actions = require 'presser.actions'
local utils = require 'presser.utils'
local a = vim.api
local modules = {}
-- @Description: Find and replace words within the current buffer.
-- @Params:
-- @Returns: nil.
--
-- @Dev: function is responsible to creating all required buffers to allow full user interaction.
--
-- @Future: implementation may allow for greater user customisation similar to what's found with
-- extensions such as Telescope. For now, it should provide a concrete UI for purpose of design.
modules.find_replace = function ()
-- define the context which these windows will belong to in the context manager.
local ctx = "find_replace"
gcm.create( ctx )
local keymap = {
n = {
["<esc>"] = "<cmd>lua require'presser.actions'.close()<CR>",
["<A-a>"] = "<cmd>lua require'presser.actions'.execute()<CR>",
},
i = {
["<down>"] = "<cmd>lua require'presser.actions'.move_next_buffer()<CR>",
["<A-a>"] = "<cmd>lua require'presser.actions'.execute()<CR>",
}
}
-- get the centre of the current buffer
local c_cols = math.floor( a.nvim_win_get_width(0) / 2 )
local c_lines = math.floor( a.nvim_win_get_height(0) / 2 )
-- create a title buffer for the steamer
steamers
.new( ctx, {
label = "presser_title",
steamer = {
placeholder = "Find & Replace",
allowed = false,
window = {
line = c_lines - 3,
border = false,
minwidth = 82,
padding = { 0, 1, 1, 1 }
}
}
} )
-- create a new buffer for find pattern
steamers
.new ( ctx, {
label = "find_buf",
steamer = {
window = {
line = c_lines,
title = "Find",
},
keybinds = keymap,
}
} )
-- create a new buffer for replace pattern
steamers
.new( ctx, {
label = "replace_buf",
steamer = {
window = {
line = c_lines + 3,
title = "Replace",
},
keybinds = keymap,
}
} )
local result = actions.buf_put_curser_at( "find_buf" )
a.nvim_feedkeys('A', 'n', false)
end
modules.hyperlink = function ()
local check_url = function ( text )
local text = string.lower( text )
-- string list of all possible URI schemes which have RFC specification and identified with permanent status
local patterns = "aaa|aaas|about|acap|acct|afs|cap|cid|coap|coap+tcp|coap+ws|coaps|coaps+tcp|coaps+ws|crid|data" ..
"|dav|dict|dns|dtn|example|fax|file|ftp|geo|go|gopher|h323|ham|http|https|iax|icap|im|imap|info|ipn|ipp|ipps" ..
"|iris|iris.beep|iris.lwz|iris.xpc|iris.xpcs|jms|ldap|leaptofrogans|mailserver|mailto|mid|modem|msrp|msrps" ..
"|mtqp|mupdate|news|nfs|ni|nih|nntp|opaquelocktoken|payto|pkcs11|pop|pres|prospero|reload|rsync|rtmfp|rtsp" ..
"|rtsps|rtspu|secret-token|service|session|sieve|sip|sips|sms|snews|snmp|soap.beep|soap.beeps|stun|stuns|tag" ..
"|tel|telnet|tftp|thismessage|tip|tn3270|turn|turns|tv|urn|vemmi|videotex|vnc|wais|ws|wss|xcon|xcon-userid" ..
"|xmlrpc.beep|xmlrpc.beeps|xmpp|z39.50|z39.50r|z39.50s|"
-- check if text is a type of URI
-- @Dev: TODO, consider option of logarithmic matching (patterns already in alphabetical order)
for proto in string.gmatch( patterns, "([^|]+)" ) do
-- if so, text can be converted directly to hyperlink (return true)
if string.find( text, proto ) then
return true
end
end
return false
end
local link_txt = nil
local row, col = unpack(a.nvim_win_get_cursor(0))
local h,j,_,l = utils.get_selection()
local line = a.nvim_get_current_line()
if vim.api.nvim_get_mode()["mode"] == "n" then
if (row-1) == h and (col == j or col == l) then
print("visual mode selection")
link_txt = string.sub(line, j, l)
else
link_txt = string.sub(line, unpack(utils.subslice(line, col)))
end
else
link_txt = vim.fn.expand "<cexpr>"
end
link_txt = utils.clean_buf(link_txt)
if link_txt == "" then
print("Presser.hyperlink: No text selected to convert.")
return
end
if check_url( link_txt ) then
print("Identified URI scheme under cursor, generating hyperlink for: " .. link_txt)
if not ( col == j or col == l ) then
j, l = unpack(utils.subslice( line .. " ", col ))
end
print(j,l)
actions.suburl( line, row, link_txt, j, l )
return nil
end
print("Cursor is not over an identified URI scheme: " .. link_txt .. ". Constructing UI...")
-- define the context which these windows will belong to in the context manager.
local ctx = "hyperlink"
gcm.create( ctx )
local keymap = {
n = {
["<esc>"] = "<cmd>lua require'presser.actions'.close()<CR>",
["<A-a>"] = "<cmd>lua require'presser.actions'.hyperlink_exec()<CR>",
},
i = {
["<A-a>"] = "<cmd>lua require'presser.actions'.hyperlink_exec()<CR>",
}
}
-- get the centre of the current buffer
local c_lines = math.floor( a.nvim_win_get_height(0) / 2 )
-- create a title buffer for the steamer
steamers
.new( ctx, {
label = "presser_title",
steamer = {
placeholder = "Hyperlink",
allowed = false,
window = {
line = c_lines - 2,
border = false,
minwidth = 82,
padding = { 0, 1, 1, 1 }
}
}
} )
steamers
.new( ctx, {
label = "selected_text",
steamer = {
placeholder = "Selected text: " .. link_txt,
allowed = false,
window = {
line = c_lines,
border = false,
minwidth = 82,
padding = { 0, 1, 1, 1 }
}
}
} )
-- create a new buffer for replace pattern
steamers
.new( ctx, {
label = "hyperlink_buf",
steamer = {
window = {
line = c_lines + 3,
title = "Hyperlink",
},
keybinds = keymap,
}
} )
local result = actions.buf_put_curser_at( "hyperlink_buf" )
a.nvim_feedkeys('A', 'n', false)
end
return modules