diff options
-rw-r--r-- | Readme.md | 22 | ||||
-rw-r--r-- | spellcheck.lua | 58 |
2 files changed, 80 insertions, 0 deletions
diff --git a/Readme.md b/Readme.md new file mode 100644 index 0000000..344a6d9 --- /dev/null +++ b/Readme.md @@ -0,0 +1,22 @@ +# vis-spellcheck + +A spellchecking lua plugin for the [vis editor](https://github.com/martanne/vis). + +## installation + +1. Download 'spellcheck.lua' or clone this repository +2. Load the plugin in your 'visrc.lua' with 'require(path/to/plugin/spellcheck)' + +## usage + ++ To correct the whole file press Ctrl+s in normal mode. + +## configuration + +The module table returned from 'require(...)' has two configuration fields +'lang' and 'cmd'. 'lang' is inserted in the 'cmd' string at '%s'. +The defaults are 'enchant -d %s' and '$LANG or "en_US". + + spell = require(...) + spell.cmd = "aspell -l %s -a" + spell.lang = "en_US" 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 |