summaryrefslogtreecommitdiff
path: root/config/essentials/vis
diff options
context:
space:
mode:
authorRaymaekers Luca <raymaekers.luca@gmail.com>2024-06-23 14:12:17 +0200
committerRaymaekers Luca <raymaekers.luca@gmail.com>2024-06-23 14:12:17 +0200
commit8986d060f2f308ba768758ef9c3077f00f3d6b2a (patch)
tree1dcbbd479f498693de0180ecae85bf532d74a8d1 /config/essentials/vis
parentd466dcc80e77463fa035fc85ced0c253e38cbac2 (diff)
checkpoint
Diffstat (limited to 'config/essentials/vis')
-rw-r--r--config/essentials/vis/format.lua63
-rw-r--r--config/essentials/vis/visrc.lua42
2 files changed, 56 insertions, 49 deletions
diff --git a/config/essentials/vis/format.lua b/config/essentials/vis/format.lua
index e39320e..d35db04 100644
--- a/config/essentials/vis/format.lua
+++ b/config/essentials/vis/format.lua
@@ -1,6 +1,8 @@
-local global_options = { check_same = true }
+local M = {}
+M.check_same = true
+M.wrapwidth = 90
-local function stdio_formatter(cmd, options)
+M.stdio_formatter = function(cmd, options)
local function apply(win, range, pos)
local size = win.file.size
local all = { start = 0, finish = size }
@@ -8,7 +10,7 @@ local function stdio_formatter(cmd, options)
range = all
end
local command = type(cmd) == "function" and cmd(win, range, pos) or cmd
- local check_same = (options and options.check_same ~= nil) and options.check_same or global_options.check_same
+ local check_same = (options and options.check_same ~= nil) and options.check_same or M.check_same
local check = check_same == true or (type(check_same) == "number" and check_same >= size)
local status, out, err = vis:pipe(win.file, all, command)
if status ~= 0 then
@@ -28,7 +30,7 @@ local function stdio_formatter(cmd, options)
}
end
-local function with_filename(win, option)
+M.with_filename = function(win, option)
if win.file.path then
return option .. "'" .. win.file.path:gsub("'", "\\'") .. "'"
else
@@ -36,13 +38,12 @@ local function with_filename(win, option)
end
end
-local formatters = {}
-formatters = {
- bash = stdio_formatter(function(win)
- return "shfmt " .. with_filename(win, "--filename ") .. " -"
+M.formatters = {
+ bash = M.stdio_formatter(function(win)
+ return "shfmt " .. M.with_filename(win, "--filename ") .. " -"
end),
- csharp = stdio_formatter("dotnet csharpier"),
- go = stdio_formatter("gofmt"),
+ csharp = M.stdio_formatter("dotnet csharpier"),
+ go = M.stdio_formatter("gofmt"),
lua = {
pick = function(win)
local _, out = vis:pipe(
@@ -50,41 +51,41 @@ formatters = {
{ start = 0, finish = win.file.size },
"test -e .lua-format && echo luaformatter || echo stylua"
)
- return formatters[out:gsub("\n$", "")]
+ return M.formatters[out:gsub("\n$", "")]
end,
},
- luaformatter = stdio_formatter("lua-format"),
- markdown = stdio_formatter(function(win)
- if win.options and win.options.colorcolumn ~= 0 then
+ luaformatter = M.stdio_formatter("lua-format"),
+ markdown = M.stdio_formatter(function(win)
+ if win.options and M.wrapwidth ~= 0 then
return "prettier --parser markdown --prose-wrap always "
- .. ("--print-width " .. (win.options.colorcolumn - 1) .. " ")
- .. with_filename(win, "--stdin-filepath ")
+ .. ("--print-width " .. (M.wrapwidth - 1) .. " ")
+ .. M.with_filename(win, "--stdin-filepath ")
else
- return "prettier --parser markdown " .. with_filename(win, "--stdin-filepath ")
+ return "prettier --parser markdown " .. M.with_filename(win, "--stdin-filepath ")
end
end, { ranged = false }),
- powershell = stdio_formatter([[
+ powershell = M.stdio_formatter([[
"$( (command -v powershell.exe || command -v pwsh) 2>/dev/null )" -c '
Invoke-Formatter -ScriptDefinition `
([IO.StreamReader]::new([Console]::OpenStandardInput()).ReadToEnd())
' | sed -e :a -e '/^[\r\n]*$/{$d;N;};/\n$/ba'
]]),
- rust = stdio_formatter("rustfmt"),
- stylua = stdio_formatter(function(win, range)
+ rust = M.stdio_formatter("rustfmt"),
+ stylua = M.stdio_formatter(function(win, range)
if range and (range.start ~= 0 or range.finish ~= win.file.size) then
return "stylua -s --range-start "
.. range.start
.. " --range-end "
.. range.finish
- .. with_filename(win, " --stdin-filepath ")
+ .. M.with_filename(win, " --stdin-filepath ")
.. " -"
else
- return "stylua -s " .. with_filename(win, "--stdin-filepath ") .. " -"
+ return "stylua -s " .. M.with_filename(win, "--stdin-filepath ") .. " -"
end
end),
- text = stdio_formatter(function(win)
- if win.options and win.options.colorcolumn ~= 0 then
- return "fmt -w " .. (win.options.colorcolumn - 1)
+ text = M.stdio_formatter(function(win)
+ if win.options and M.wrapwidth ~= 0 then
+ return "fmt -w " .. (M.wrapwidth - 1)
else
return "fmt"
end
@@ -99,7 +100,7 @@ local function getwinforfile(file)
end
end
-local function apply(file_or_keys, range, pos)
+M.apply = function(file_or_keys, range, pos)
local win = type(file_or_keys) ~= "string" and getwinforfile(file_or_keys) or vis.win
local ret = type(file_or_keys) ~= "string" and function()
return pos
@@ -107,7 +108,7 @@ local function apply(file_or_keys, range, pos)
return 0
end
pos = pos or win.selection.pos
- local formatter = formatters[win.syntax]
+ local formatter = M.formatters[win.syntax]
if formatter and formatter.pick then
formatter = formatter.pick(win)
end
@@ -125,10 +126,4 @@ local function apply(file_or_keys, range, pos)
return ret()
end
-return {
- formatters = formatters,
- options = global_options,
- apply = apply,
- stdio_formatter = stdio_formatter,
- with_filename = with_filename,
-}
+return M
diff --git a/config/essentials/vis/visrc.lua b/config/essentials/vis/visrc.lua
index a763edc..2a6a706 100644
--- a/config/essentials/vis/visrc.lua
+++ b/config/essentials/vis/visrc.lua
@@ -69,7 +69,7 @@ end
-----------------------------------
vis:command_register("make", function()
- vis:command("!make && head -n 1")
+ vis:command("!make; head -n 1")
end, "make")
vis:command_register("Q", function()
vis:command("qa!")
@@ -100,16 +100,10 @@ map_cmd(m.NORMAL, " q", "q!", "Quit (force)")
map_cmd(m.NORMAL, " s", "!doas vis $vis_filepath", "Edit as superuser")
map_cmd(m.NORMAL, " w", "w", "Write")
map_cmd(m.NORMAL, " x", "!chmod u+x $vis_filepath", "Make active file executable")
-
-vis:map(m.NORMAL, " eh", function()
- vis:command("!lowdown $vis_filepath > ${vis_filepath%.md}.html")
- vis:info("exported.")
-end, "Export markdown to html")
+map_cmd(m.NORMAL, "!", "!bash", "Run bash")
map_keys(m.NORMAL, " nl", ":<seq -f '%0.0f. ' 1 ", "Insert numbered list")
--- select markdown list element: ,x/^(\d+\.|[-*])\s+.+\n(^ .+\n)*/
-
------------------------------------
--- EVENTS
------------------------------------
@@ -131,24 +125,42 @@ vis.events.subscribe(vis.events.WIN_OPEN, function(win) -- luacheck: no unused a
win.options.relativenumbers = true
+ if win.syntax then
+ vis:info(win.syntax)
+ end
+
+ -- FILETYPE OPTIONS
if win.syntax == "bash" then
map_keys(
m.NORMAL,
- ";p",
+ "\\p",
"V:x/^(\\s*)(.+)$/ c/\\1>\\&2 printf '\\2: %s\\\\n' \"$\\2\"/<Enter><Escape>",
"Print variable"
)
- map_keys(m.NORMAL, ";v", 'V:x/^(\\s*)(.+)$/ c/\\1"$(\\2)"/<Enter><Escape>', "Surround in variable")
+ map_keys(m.NORMAL, "\\v", 'V:x/^(\\s*)(.+)$/ c/\\1"$(\\2)"/<Enter><Escape>', "Surround in variable")
map_keys(m.NORMAL, ";|", "V:x/\\| / c/|\n\t/<Enter><Escape>", "Wrap one-line multi pipe command")
map_keys(
m.NORMAL,
- ";e",
+ "\\e",
'V:x/^(\\s*)(.+)$/ c/\\1[ "\\2" ] || exit 1/<Enter><Escape>',
"Condition exit if empty"
)
- map_keys(m.NORMAL, ";sc", ":-/\\<case\\>/,/\\<esac\\>/<Enter>", "Expand to case")
- map_keys(m.NORMAL, ";sw", ":-/\\<while/,/\\<done\\>/<Enter>", "Expand to while")
- map_keys(m.NORMAL, ";sf", ":-/\\<for\\>/,/\\<done\\>/<Enter>", "Expand to for")
- map_keys(m.NORMAL, ";si", ":-/\\<if\\>/,/\\<fi\\>/<Enter>", "Expand to if")
+ map_cmd(m.NORMAL, "\\sc", "-/\\<case\\>/,/\\<esac\\>/", "Expand to case")
+ map_cmd(m.NORMAL, "\\sw", "-/\\<while/,/\\<done\\>/", "Expand to while")
+ map_cmd(m.NORMAL, "\\sf", "-/\\<for\\>/,/\\<done\\>/", "Expand to for")
+ map_cmd(m.NORMAL, "\\si", "-/\\<if\\>/,/\\<fi\\>/", "Expand to if")
end
+
+ if win.syntax == "markdown" then
+ vis:map(m.NORMAL, "\\h", function()
+ vis:command("!lowdown $vis_filepath > ${vis_filepath%.md}.html")
+ vis:info("exported.")
+ end, "Export markdown to html")
+ map_cmd(m.NORMAL, "\\sl", "-+x/(\\d+|[-*])\\s+.+\n/", "Expand to list item")
+ end
+
+ if win.syntax == "ansi_c" then
+ map_keys(m.NORMAL, "\\a", "f,a <Escape>hdw<S-Tab>i<Tab><Escape>", "Align table")
+ end
+
end)