Compare commits
21 Commits
main
...
experiment
| Author | SHA1 | Date | |
|---|---|---|---|
| 7865f9a44b | |||
| ef257557cf | |||
| b59715ae67 | |||
| dd0f25ffa1 | |||
| dbebdde8c3 | |||
| 69af7b05d1 | |||
| b81d6b5acc | |||
| 1dec350d04 | |||
| 6e3e7f9d2d | |||
| 6b63ea6446 | |||
| e1b2cb5de8 | |||
| b14fadee45 | |||
| 028b34bdda | |||
| 7a126af17a | |||
| 686dbd9ae4 | |||
| 7908c64c00 | |||
| d1ab26196a | |||
| 4d40f43a73 | |||
| a66d9ae62f | |||
| 8f88af8d69 | |||
| 90b57ed4b1 |
|
|
@ -1,4 +0,0 @@
|
||||||
# presser.nvim
|
|
||||||
A collection of word processing tools to press over your files easier with UIs.
|
|
||||||
|
|
||||||
For unstable and latest features, see the [experimental](https://git.closedless.xyz/TheOnePath/presser.nvim/src/branch/experimental) branch.
|
|
||||||
56
README.txt
Normal file
56
README.txt
Normal file
|
|
@ -0,0 +1,56 @@
|
||||||
|
# presser.nvim
|
||||||
|
A collection of word processing tools to press over your files easier with UIs.
|
||||||
|
|
||||||
|
For unstable and latest features, see the [experimental](https://git.closedless.xyz/TheOnePath/presser.nvim/src/branch/experimental) branch.
|
||||||
|
|
||||||
|
## Architecture
|
||||||
|
The diagram below outlines the overall architecture of presser.nvim and how each component interacts with each other.
|
||||||
|
|
||||||
|
|
||||||
|
+++++++++++++++++++++++++++++++++ +++++++++++++++++++++++++++++++++ +++++++++++++++++++++++++++++++++
|
||||||
|
+ + + + + +
|
||||||
|
+ + + + + +
|
||||||
|
+ + + + + +
|
||||||
|
+ Context Manager + + Steamers + + Actions +
|
||||||
|
+ + + + + +
|
||||||
|
+ + + + + +
|
||||||
|
+ + + + + +
|
||||||
|
+++++++++++++++++++++++++++++++++ +++++++++++++++++++++++++++++++++ +++++++++++++++++++++++++++++++++
|
||||||
|
| | |
|
||||||
|
[5] +---------------------------------------------+-----+ [4] |
|
||||||
|
v |
|
||||||
|
+++++++++++++++++++++++++++++++++ +++++++++++++++++++++++++++++++++ |
|
||||||
|
+ + + + |
|
||||||
|
+ + + + |
|
||||||
|
+ + [2] + + [3] |
|
||||||
|
+ Builtins + <--------------- + Pressers + <-----------------+
|
||||||
|
+ + + + |
|
||||||
|
+ + + + |
|
||||||
|
+ + + + |
|
||||||
|
+++++++++++++++++++++++++++++++++ +++++++++++++++++++++++++++++++++ |
|
||||||
|
I |
|
||||||
|
| [1] | [6]
|
||||||
|
| I
|
||||||
|
+++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||||
|
+ + + +
|
||||||
|
+ User + + Window buffer +
|
||||||
|
+ + + +
|
||||||
|
+++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||||
|
|
||||||
|
[1] - The User interacts with Builtins to use a Presser.
|
||||||
|
[2] - Builtins inherit Pressers and lazy loads them to be accessed by Users.
|
||||||
|
[3] - Pressers make use of Actions to do the work.
|
||||||
|
[4] - Pressers inherit Steamers, giving them the ability to generate UIs for the user.
|
||||||
|
[5] - Pressers make use of the Context Manager to organise and track Steamers.
|
||||||
|
[6] - Actions will directly modify the contents of the Window buffer.
|
||||||
|
|
||||||
|
### List of Components
|
||||||
|
|
||||||
|
* User + Window buffer - refers to Neovim, which has the User who does something within the Window buffer.
|
||||||
|
* Builtins - Lazy loaded interface responsible for acting as a proxy to Pressers.
|
||||||
|
* Pressers - Tools which allow the User to perform different word processing tasks. Comprised of Steamers, and perform
|
||||||
|
Actions to modify the Window buffer.
|
||||||
|
* Steamers - Class module which is responsible for constructing buffers, creating a UI for the User.
|
||||||
|
* Context Manager - A data structure which is used to store Steamers for a given context (the Presser). Also known as,
|
||||||
|
"GCM" due to being scoped to vim.g.
|
||||||
|
* Actions - Perform an action defined for a Presser, which will modify the Window buffer in some way.
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
local gcm = require'presser.context_manager'
|
local gcm = require'presser.context_manager'
|
||||||
|
--local actions = require'presser.actions'
|
||||||
local utils = require'presser.utils'
|
local utils = require'presser.utils'
|
||||||
|
|
||||||
local a = vim.api
|
local a = vim.api
|
||||||
|
|
@ -59,6 +60,34 @@ actions.execute = function ()
|
||||||
a.nvim_exec( cmd, false )
|
a.nvim_exec( cmd, false )
|
||||||
end
|
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 )
|
actions.buf_put_curser_at = function ( label )
|
||||||
for _, ctx_obj in pairs( vim.g.presser_buf_ctx ) do
|
for _, ctx_obj in pairs( vim.g.presser_buf_ctx ) do
|
||||||
for _, record in pairs( ctx_obj ) do
|
for _, record in pairs( ctx_obj ) do
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
local gcm = require 'presser.context_manager'
|
local gcm = require 'presser.context_manager'
|
||||||
local steamers = require 'presser.steamers'
|
local steamers = require 'presser.steamers'
|
||||||
local actions = require 'presser.actions'
|
local actions = require 'presser.actions'
|
||||||
|
local utils = require 'presser.utils'
|
||||||
|
|
||||||
local a = vim.api
|
local a = vim.api
|
||||||
|
|
||||||
|
|
@ -80,4 +81,130 @@ modules.find_replace = function ()
|
||||||
a.nvim_feedkeys('A', 'n', false)
|
a.nvim_feedkeys('A', 'n', false)
|
||||||
end
|
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
|
return modules
|
||||||
|
|
|
||||||
|
|
@ -12,5 +12,6 @@ end
|
||||||
|
|
||||||
|
|
||||||
builtins.find_replace = require_on_exported_call("presser.builtins.__modules").find_replace
|
builtins.find_replace = require_on_exported_call("presser.builtins.__modules").find_replace
|
||||||
|
builtins.hyperlink = require_on_exported_call("presser.builtins.__modules").hyperlink
|
||||||
|
|
||||||
return builtins
|
return builtins
|
||||||
|
|
|
||||||
|
|
@ -23,7 +23,7 @@ presser.setup = function ( opts )
|
||||||
{
|
{
|
||||||
nargs = 1,
|
nargs = 1,
|
||||||
complete = function (ArgLead, CmdLine, CursorPos)
|
complete = function (ArgLead, CmdLine, CursorPos)
|
||||||
return { 'find_replace' }
|
return { 'find_replace', 'hyperlink' }
|
||||||
end,
|
end,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -31,8 +31,32 @@ M.clean_buf = function ( text )
|
||||||
return ""
|
return ""
|
||||||
end
|
end
|
||||||
|
|
||||||
return escape_chars( text )
|
return text:match( "^%s*(.-)%s*$" )
|
||||||
:match( "^%s*(.-)%s*$" )
|
end
|
||||||
|
|
||||||
|
|
||||||
|
M.get_selection = function()
|
||||||
|
local _, csrow, cscol, _ = unpack(vim.fn.getpos("'<"))
|
||||||
|
local _, cerow, cecol, _ = unpack(vim.fn.getpos("'>"))
|
||||||
|
if csrow < cerow or (csrow == cerow and cscol <= cecol) then
|
||||||
|
return csrow - 1, cscol - 1, cerow - 1, cecol
|
||||||
|
else
|
||||||
|
return cerow - 1, cecol - 1, csrow - 1, cscol
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
M.subslice = function(line, col)
|
||||||
|
local i = 0
|
||||||
|
local j = 0
|
||||||
|
while true do
|
||||||
|
j = string.find(line, " ", j+1) -- find 'next' newline
|
||||||
|
if j == nil then break end
|
||||||
|
if j > col then break end
|
||||||
|
i = j
|
||||||
|
end
|
||||||
|
|
||||||
|
return { i, j }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user