aboutsummaryrefslogtreecommitdiffstats
path: root/spellcheck.lua
diff options
context:
space:
mode:
authorFlorian Fischer <florian.fl.fischer@fau.de>2017-04-07 13:06:36 +0200
committerFlorian Fischer <florian.fl.fischer@fau.de>2017-04-07 13:06:36 +0200
commit003737c59668b6793d36fef4040318b999c153de (patch)
tree887f38e7b49172ff361a5255b82037f5a10649f3 /spellcheck.lua
downloadvis-spellcheck-003737c59668b6793d36fef4040318b999c153de.tar.gz
add Readme and plugin
Diffstat (limited to 'spellcheck.lua')
-rw-r--r--spellcheck.lua58
1 files changed, 58 insertions, 0 deletions
diff --git a/spellcheck.lua b/spellcheck.lua
new file mode 100644
index 0000000..2765a86
--- /dev/null
+++ b/spellcheck.lua
@@ -0,0 +1,58 @@
+local module = {}
+module.lang = os.getenv("LANG"):sub(0,5) or "en_US"
+-- TODO use more spellcheckers (aspell/hunspell)
+module.cmd = "enchant -d %s -a"
+
+function spellcheck()
+ local win = vis.win
+ local file = win.file
+
+ local cmd = module.cmd:format(module.lang)
+ local ret, so, se = vis:pipe(file, { start = 0, finish = file.size }, cmd)
+
+ if ret ~= 0 then
+ return ret, se
+ end
+
+ local word_corrections = so:gmatch("(.-)\n")
+ -- skip header line
+ word_corrections()
+
+ for i=1,#file.lines do
+ local line = file.lines[i]
+ local new_line = ""
+ local words = line:gmatch("%w+")
+ for w in words do
+ local correction = word_corrections()
+ if correction ~= "*" then
+ local orig, pos, sug = correction:match("& (%w+) %d+ (%d+): (.*)")
+ if orig ~= w then
+ return 1, "Bad things happend!! Correction is not for" .. w
+ end
+ local cmd = 'printf "' .. sug:gsub(", ", "\\n") .. '\\n" | vis-menu'
+ local f = io.popen(cmd)
+ correction = f:read("*all")
+ -- trim correction
+ correction = correction:match("%a+")
+ f:close()
+ if correction then
+ w = correction
+ end
+ end
+ new_line = new_line .. " " .. w
+ end
+ file.lines[i] = new_line:match("^%s*(.-)%s*$")
+ -- skip "end of line" new line
+ word_corrections()
+ end
+end
+
+vis:map(vis.modes.NORMAL, "<C-s>", function(keys)
+ ret, err = spellcheck()
+ if ret then
+ vis:info(err)
+ end
+ return 0
+end, "Spellcheck the whole file")
+
+return module