Compare commits

..

21 Commits

Author SHA1 Message Date
7865f9a44b
Renamed README.md -> README.txt
Renamed README to be a plaintext file instead of Markdown due to
Markdown syntax interfering with document layout.
2023-02-04 20:37:12 +00:00
ef257557cf
Updated builtins/init.lua
Added lazy export for modules@hyperlink
2023-02-04 18:41:22 +00:00
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
dd0f25ffa1
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.
2023-02-04 18:16:58 +00:00
dbebdde8c3
Merge branch 'main' into experimental
Lack of branch protection and pre-commit hooks makes for a minor
inconvenience. We make silly little mistakes using git :P
2023-02-04 17:53:54 +00:00
69af7b05d1
Updated utils.lua
M@clean_buffer no longer escapes sequences (caused undesirable
behaviour).

Added M@get_selection which will return four values indicating the
starting row/column and ending row/column of a visual-mode selection. If
no selection has been performed in buffer instance, values may be -1.
Value is "cached" as defined Vim behaviour of '< and '> positions.

Added M@subslice is used to get a substring of a word between two
whitespace characters (i.e., spaces). The `line` is the string to check,
and `col` is a position (e.g., from the cursor) within the word which
will be extracted. Return value is a table of starting and ending
indices of the original string.
2023-01-23 18:21:44 +00:00
b81d6b5acc
Updated init.lua to allow autocomplete of new command builtins@hyperlink 2023-01-23 18:19:56 +00:00
1dec350d04
Updated context_manager/init.lua 2023-01-13 16:48:06 +00:00
6e3e7f9d2d
Moved presser/ -> lua/presser/
Created new director lua/ and moved presser/ into the subdirectory.
This is to align with other standard developments of Neovim extensions.
2023-01-13 16:40:43 +00:00
6b63ea6446
Added streamer.lua
Steamers are akin to that of Telescope's Pickers. A "steamer" is the
class structure used by builtins to create new windows/buffers for the
user to interact with. This code was simply migrated across from
presser@new and refactored to adopt a class-like OOP paradigm.

steamers@new is a wrapper function accessed by builtins to invoke the
class constructor Steamers@new. Allows to greater control and
flexibility of window/buffer options.
2023-01-13 16:29:21 +00:00
e1b2cb5de8
Added presser/builtins
Module is responsible for implementing a lazy-loader for any new tool
which is defined in table@modules. This function makes use of a
lazy-loading strategy (needs citation).
2023-01-13 16:25:56 +00:00
b14fadee45
Added builtins/__modules.lua
Script defines all tools available by presser.nvim, which are
lazy-loaded and executed when called by the user.

Function presser@find_replace has undergone major refactorisation:
	- Keymaps have been updated to respect new codebase structure.
	- A new OOP implementation is being experimented for constructing
	  windows, giving future flexibility. Consult presser/steamers.lua.
	- Function now calls the class@streamers and invokes the
	  streamers@new() method. This constructs the windows/buffers which
	  was originally done via call to presser@new().
	- This function is NOT directly accessed, but via proxy
	  table@builtins which lazy-loads function.

Any new tools which are created for presser are to be defined in this
file and lazy-loaded. Module may be renamed in future for theming
consistency.
2023-01-13 16:18:06 +00:00
028b34bdda
Added new actions module
All functions originally defined in presser/init.lua and part of
table@presser which were related to performing a specific action,
have be migrated to this file.

Function definitions have remained the same
	- presser@start_buffer has been renamed
	  actions@buf_put_cursor_at() to allow for more generic
	  use-cases which the function name did not imply. Call this
	  function to place the cursor into any buffer in the GCM by its
	  label name.
2023-01-13 16:13:21 +00:00
7a126af17a
Refactored main init.lua file
File has undergone refactor to reduce overall expected responsibility
which was temporary for kickstarting the extension.

The presser/init.lua script will now, and only now, be responsible for
handling and setting up configuration of presser itself (extensibility
to be concluded in future). Now, the script defines presser@setup()
which is to be called by a user in their Neovim config script. This
function currently defines one user command which allows for executing
the builtins@find_replace.

Most functions which were defined in table@presser have been migrated to
a new location presser/actions, defined in table@actions.

The function definition presser@find_replace has been migrated to
presser.builtins (in builtins@find_replace).

Consult other commits related to migrations for more info about specific
refactors.
2023-01-13 16:04:04 +00:00
686dbd9ae4
Upated context_manager
Added a getter function to return the current state of the global
context manager.
2023-01-13 16:02:11 +00:00
7908c64c00
Moved to presser subdirectory 2023-01-10 22:34:05 +00:00
d1ab26196a
Updated presser/utils.lua
Updated M@clean_buf. If the type is not string or is nil, return empty string.
2023-01-10 22:33:45 +00:00
4d40f43a73
Updated presser/init.lua
File has been moved to subdirectory.
Note: TBR = To Be Refactored/Restructured.

- changed `ctx` to `gcm` as import for context manager module.
	- gcm/GCM = global context manager
	- all associations with this variable have been updated
	  respectfully.
- func@new is no longer responsible for creating a new context in the
  GCM. This is now handled per built-in.
	- call to gcm@update now respects newly modified GCM structure.
- Added presser@move_next_buffer:
	- temporary function to experiment moving between buffers which
	  are marked as `allowed = true` in the GCM.
	- TBR.
- Added func@read_buffer:
	- reads the contents of all buffers in the GCM.
	- returns a table storing all collected buffer contents.
	- TBR.
- Added presser@execute:
	- temporary function which calls func@read_buffer and performs a
	  vim substitute as per the built-in presser@find_replace.
	 - TBR.
- Added func@start_buffer:
	- place the cursor in an initial buffer by name.
	- TBR. Should be func@put_cursor_in_buf (or similar).
- Updated keymap to store more keys for experimenting.
- Calculate the centre of the current window height (should check for
  split windows in future, or determine terminal size).
- Changed ordering of window creation. Windows created in order they are
  rendered.
2023-01-10 22:17:30 +00:00
a66d9ae62f
Updated context_manager/init.lua
- Added diagram for global context manager (GCM) structure.
- Removed local func@get_ctx_head (unused).
- Updated func@M.update:
	- function takes 2 parameter arguments (see docstring).
	- constructs new record for the given context in the GCM to keep
	  record of a newly constructed window buffer.
	- function updates the GCM with the new record.
- Updated func@M.flush:
	- handles for new structure of records in the GCM.
	- see docstring for info.
2023-01-10 22:02:16 +00:00
8f88af8d69
Renamed modules
Files were moved from top-level space into presser sub-directory.
2023-01-09 19:46:12 +00:00
90b57ed4b1
Added context manager module
Added a context manager for handling and managing windows/buffers
constructed by built-ins. This was handled by the `new()` function in
presser/init.lua but is now a dedicated module.
2023-01-09 19:44:44 +00:00
7 changed files with 243 additions and 10 deletions

View File

@ -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
View 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.

View File

@ -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

View File

@ -1,6 +1,7 @@
local gcm = require'presser.context_manager'
local steamers = require'presser.steamers'
local actions = require'presser.actions'
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
@ -80,4 +81,130 @@ modules.find_replace = function ()
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

View File

@ -12,5 +12,6 @@ end
builtins.find_replace = require_on_exported_call("presser.builtins.__modules").find_replace
builtins.hyperlink = require_on_exported_call("presser.builtins.__modules").hyperlink
return builtins

View File

@ -23,7 +23,7 @@ presser.setup = function ( opts )
{
nargs = 1,
complete = function (ArgLead, CmdLine, CursorPos)
return { 'find_replace' }
return { 'find_replace', 'hyperlink' }
end,
}
)

View File

@ -31,8 +31,32 @@ M.clean_buf = function ( text )
return ""
end
return escape_chars( text )
:match( "^%s*(.-)%s*$" )
return text: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