aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatěj Cepl <mcepl@cepl.eu>2022-07-04 00:52:21 +0200
committerMatěj Cepl <mcepl@cepl.eu>2022-07-04 00:52:21 +0200
commitc8073088422aff88781c38455ae32afaced10459 (patch)
tree201f9b579487e23e169710d63bdd5d3ff5b00bc5
parent7080a540246de28f214165d4a6ec13e74573a364 (diff)
downloadvis-par-c8073088422aff88781c38455ae32afaced10459.tar.gz
Attempt to rewrite.
Broken on running subprocesses in Lua/vis and broken par(1).
-rw-r--r--init.lua203
1 files changed, 175 insertions, 28 deletions
diff --git a/init.lua b/init.lua
index f4653a5..42cb0a3 100644
--- a/init.lua
+++ b/init.lua
@@ -1,32 +1,179 @@
-require('vis')
+require("vis")
par_prg = "par"
-vis:operator_new("gq", function(file, range, pos)
- local cur_line = file.lines[vis.win.selection.line]
- local beg, fin = string.find(cur_line, "^%s+")
- local exec = par_prg .. " ET4w65"
- if beg ~= nil then
- local ind = fin
- if string.find(cur_line, "[-+*]", fin) then
- exec = exec .. "h"
- ind = ind + 2
+function print(...)
+ _print_ = _print_ or io.open("/tmp/visprint", "w")
+ _print_:write(..., "\n")
+end
+
+--- Dump value of a variable in a formatted string
+--
+--- @param o table Dumpable object
+--- @param tbs string|nil Tabulation string, ' ' by default
+--- @param tb number|nil Initial tabulation level, 0 by default
+--- @return string
+local function dump(o, tbs, tb)
+ tb = tb or 0
+ tbs = tbs or " "
+ if type(o) == "table" then
+ local s = "{"
+ if (next(o)) then
+ s = s .. "\n"
+ else
+ return s .. "}"
+ end
+ tb = tb + 1
+ for k, v in pairs(o) do
+ if type(k) ~= "number" then
+ k = '"' .. k .. '"'
end
- exec = exec .. "p" .. ind
- vis:info("beg = " .. beg .. ", fin = " .. fin .. ", exec = " .. exec)
+ s = s .. tbs:rep(tb) .. "[" .. k .. "] = " .. dump(v, tbs, tb)
+ s = s .. ",\n"
+ end
+ tb = tb - 1
+ return s .. tbs:rep(tb) .. "}"
+ else
+ return tostring(o)
+ end
+end
+
+-- fp = io.popen("foo >/tmp/unique", "w")
+-- fp:write(anything)
+-- fp:close()
+-- fp = io.open("/tmp/unique")
+-- x = read("*a")
+-- fp:close()
+
+local supress_stdout = " >/dev/null"
+local supress_stderr = " 2>/dev/null"
+local supress_output = supress_stdout .. supress_stderr
+
+-- Return nil or a string of misspelled word in a specific file range or text
+-- by calling the spellchecker's list command.
+-- If given a range we will use vis:pipe to get our typos from the spellchecker.
+-- If a string was passed we call the spellchecker ourself and redirect its stdout
+-- to a temporary file. See http://lua-users.org/lists/lua-l/2007-10/msg00189.html.
+-- The returned string consists of each misspell followed by a newline.
+local function run_par(cmd, params, inp)
+ local out
+ print("cmd = " .. cmd .. ", params = " .. params .. ", inp = " .. inp .. ", (" .. type(inp) .. ")")
+
+ if type(inp) == "string" then
+ -- local full_cmd = cmd .. " " .. params .. " > " .. tmp_name
+ -- print("full_cmd = " .. full_cmd)
+ -- local tmp_name = os.tmpname()
+ -- local proc = assert(io.popen(full_cmd, "w"))
+ -- print("proc = " .. type(proc))
+ -- proc:write(inp)
+ -- -- this error detection may need lua5.2
+ -- proc:flush()
+ -- local exit_code = proc:close()
+ --
+ -- local tmp_file = assert(io.open(tmp_name, "r"))
+ -- out = tmp_file:read("*a")
+ -- tmp_file:close()
+ -- print("out = " .. out)
+ -- -- os.remove(tmp_name)
+ out = vis:pipe(vis.win.file, inp, cmd .. params)
+
+ if exit_code then
+ print(tostring(exit_code))
+ print("calling " .. full_cmd .. " failed (" .. out .. ")")
+ return nil
+ end
+ else
+ local range = inp
+ local ret, so, _ = vis:pipe(vis.win.file, range, cmd)
+
+ if ret ~= 0 then
+ print("calling " .. cmd .. " failed (" .. ret .. ")")
+ return nil
+ end
+ out = so
+ end
+
+ return out
+end
+
+function lines(str)
+ local t = {}
+ local function helper(line)
+ table.insert(t, line)
+ return ""
+ end
+ helper((str:gsub("(.-)\r?\n", helper)))
+ return t
+end
+
+getmetatable(_VERSION).__index.lines = lines
+
+function hanging_symbol(line, nspc)
+ -- itemized list
+ if line:find("^[-+*] ", nspc) then
+ return nspc + 1 -- one for space
+ end
+
+ local beg, fin = line:find("^%d+%. ", nspc)
+ if beg then
+ return fin + 1
+ end
+
+ return nspc -- no hanging idennt
+end
+
+vis:operator_new(
+ "gq",
+ function(file, range, pos)
+ local out = ""
+ 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*")
+ assert(beg == 1, "There is no way this would not be 1.")
+ local non_spc_idx = line:len() >= beg_spc_len + 1 and beg_spc_len + 1 or 0
+ print("non_spc_idx = " .. non_spc_idx)
+
+ if non_spc_idx ~= 0 then -- FIXME This is wrong, no two cycles
+ if p_arg == 0 then
+ p_arg = "p" .. hanging_symbol(line, non_spc_idx)
+ end
+ print("segment = " .. dump(segment) .. ", p_arg = " .. p_arg)
+ table.insert(segment, line)
+ else -- this is an empty line, so do whole circus FIXME
+ if next(segment) then
+ local new_text = run_par(par_prg, ex_args .. p_arg,
+ table.concat(segment, "\n"))
+ segment = {}
+ p_arg = 0
+ out = out .. new_text .. "\n\n"
+ end
+ end
+ end
+
+ if out:len() == 0 then
+ print("Cannot process paragraph.")
+ else
+ file:delete(range)
+ file:insert(range.start, out)
+ end
+ return range.start -- new cursor location
+ end,
+ "Formating operator, filter range through formatting filter"
+)
+
+vis:option_register(
+ "autoformat",
+ "bool",
+ function(value, toogle)
+ if not vis.win then
+ return false
end
- local status, out, err = vis:pipe(file, range, exec)
- if not status then
- vis:info(err)
- else
- file:delete(range)
- file:insert(range.start, out)
- end
- return range.start -- new cursor location
-end, "Formating operator, filter range through formatting filter")
-
-vis:option_register("autoformat", "bool", function(value, toogle)
- if not vis.win then return false end
- vis.win.autoformat = toogle and not vis.win.autoformat or value
- vis:info("Option autoformat = " .. tostring(vis.win.autoformat))
- return true
-end, "Automatically format current paragraph.")
+ vis.win.autoformat = toogle and not vis.win.autoformat or value
+ print("Option autoformat = " .. tostring(vis.win.autoformat))
+ return true
+ end,
+ "Automatically format current paragraph."
+)