aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatěj Cepl <mcepl@cepl.eu>2022-07-04 23:41:33 +0200
committerMatěj Cepl <mcepl@cepl.eu>2022-07-05 09:46:29 +0200
commit6caeb15c66e61b3d4c26b09e67ff2a50cddc2ab5 (patch)
tree3b10dd77502de45a271155bbb42bef7e5df17246
parent5dd63f3ffc4244375cecaa2edc58f8f1514f77d4 (diff)
downloadvis-par-6caeb15c66e61b3d4c26b09e67ff2a50cddc2ab5.tar.gz
Allow multi-segment formatting.
Fixes #1.
-rw-r--r--init.lua57
1 files changed, 38 insertions, 19 deletions
diff --git a/init.lua b/init.lua
index 25486c0..4f23eac 100644
--- a/init.lua
+++ b/init.lua
@@ -1,5 +1,10 @@
require("vis")
par_prg = "par"
+par_tw = 65
+ex_args = "RET4bhgqw" .. par_tw
+-- ex_args = "RET4bhgqB=.,?_A_aQ=_s>|w" .. par_tw
+-- export PARINIT='Tbp2hgqR B=.,?_A_a Q=_s>|'
+
function print(...)
_print_ = _print_ or io.open("/tmp/visprint", "w")
@@ -52,7 +57,7 @@ local function run_shell_cmd(cmd, params, inp)
print("cmd = " .. cmd .. ", params = " .. params .. ", inp = " .. inp .. ", (" .. type(inp) .. ")")
if type(inp) == "string" then
- local full_cmd = cmd .. " " .. params
+ local full_cmd = cmd .. " '" .. params .. "'"
local tmp_name = os.tmpname()
print("tmp_name = " .. tmp_name)
full_cmd = full_cmd .. " > " .. tmp_name
@@ -69,8 +74,7 @@ local function run_shell_cmd(cmd, params, inp)
out = tmp_file:read("*a")
tmp_file:close()
print("out = " .. out)
- os.remove(tmp_name)
- -- out = vis:pipe(vis.win.file, inp, cmd .. params)
+ -- os.remove(tmp_name)
if not exit_code then
print("calling " .. full_cmd .. " failed (" .. out .. ")")
@@ -120,14 +124,25 @@ function hanging_symbol(line, nspc)
return nspc -- no hanging idennt
end
+function new_segment(segment, p_arg)
+ if next(segment) then
+ local new_text = run_shell_cmd(par_prg, ex_args .. p_arg,
+ table.concat(segment, "\n"))
+ print("new_text = " .. new_text)
+ return new_text, {}, 0 -- add-to-out, segment, p_arg
+ else
+ return "", {}, 0 -- add-to-out, segment, p_arg
+ end
+end
+
vis:operator_new(
"gq",
function(file, range, pos)
local out = ""
+ local hang_sym = ""
local segment = {}
local p_arg = 0
local range_lines = file:content(range):lines()
- local ex_args = "ET4w65"
for _, line in ipairs(range_lines) do
local beg, beg_spc_len = line:find("^%s*")
@@ -135,22 +150,26 @@ vis:operator_new(
local non_spc_idx = line:len() >= beg_spc_len + 1 and beg_spc_len + 1 or 0
print("non_spc_idx = " .. non_spc_idx)
- -- FIXME This is wrong, consider multi item list
- if p_arg == 0 then
- p_arg = "p" .. hanging_symbol(line, non_spc_idx)
- end
-
- if non_spc_idx ~= 0 then
- print("line = " .. line)
- print("segment = " .. dump(segment) .. ", p_arg = " .. p_arg)
- table.insert(segment, line)
+ hang_sym = hanging_symbol(line, non_spc_idx)
+ print("hang_sym = " .. hang_sym .. ", non_spc_idx = " .. non_spc_idx)
+
+ if non_spc_idx ~= 0 then -- non-empty line
+ -- item: start new segment
+ if hang_sym ~= non_spc_idx then
+ seg_pared, segment, p_arg = new_segment(segment, p_arg)
+ if seg_pared then
+ out = out .. seg_pared
+ p_arg = "p" .. hang_sym
+ end
+ table.insert(segment, line)
+ else
+ -- continuous line in a segment, just collect it
+ table.insert(segment, line)
+ end
else -- this is an empty line, so do whole circus FIXME
- if next(segment) then
- local new_text = run_shell_cmd(par_prg, ex_args .. p_arg,
- table.concat(segment, "\n"))
- segment = {}
- p_arg = 0
- out = out .. new_text
+ seg_pared, segment, p_arg = new_segment(segment, p_arg)
+ if seg_pared then
+ out = out .. seg_pared
end
end
end