diff options
author | Florian Fischer <florian.fl.fischer@fau.de> | 2017-04-09 19:22:59 +0200 |
---|---|---|
committer | Florian Fischer <florian.fl.fischer@fau.de> | 2017-04-09 19:22:59 +0200 |
commit | a50798008c512503e804823ecd0f71d44c854674 (patch) | |
tree | d26fa80098e250f20c7fbb97841b4a13a15f7967 /spellcheck.lua | |
parent | 003737c59668b6793d36fef4040318b999c153de (diff) | |
download | vis-spellcheck-a50798008c512503e804823ecd0f71d44c854674.tar.gz |
generalize spellcheck to work on ranges
Diffstat (limited to 'spellcheck.lua')
-rw-r--r-- | spellcheck.lua | 62 |
1 files changed, 31 insertions, 31 deletions
diff --git a/spellcheck.lua b/spellcheck.lua index 2765a86..65d337b 100644 --- a/spellcheck.lua +++ b/spellcheck.lua @@ -3,12 +3,9 @@ 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 - +function spellcheck(file, range) local cmd = module.cmd:format(module.lang) - local ret, so, se = vis:pipe(file, { start = 0, finish = file.size }, cmd) + local ret, so, se = vis:pipe(file, range, cmd) if ret ~= 0 then return ret, se @@ -18,37 +15,40 @@ function spellcheck() -- 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 + + local orig = file:content(range) + local new = orig:gsub("%S+", function(w) + local correction = word_corrections() + -- empty correction means a new line in range + if correction == "" then + correction = word_corrections() + end + if correction ~= "*" then + -- get corrections + local orig, pos, sug = correction:match("& (%w+) %d+ (%d+): (.*)") + if orig ~= w then + return 1, "Bad things happend!! Correction is not for" .. w + end + -- select a correction + local cmd = 'printf "' .. sug:gsub(", ", "\\n") .. '\\n" | vis-menu' + local f = io.popen(cmd) + correction = f:read("*all") + -- trim correction + correction = correction:match("%S+") + f:close() + if correction then + return correction 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) + + file:delete(range) + file:insert(range.start, new) end vis:map(vis.modes.NORMAL, "<C-s>", function(keys) - ret, err = spellcheck() + local file = vis.win.file + ret, err = spellcheck(file, { start=1, finish=file.size }) if ret then vis:info(err) end |