aboutsummaryrefslogtreecommitdiffstats
path: root/spellcheck.lua
blob: 2765a86efa75df1e4f02b6c28c82b7ed03d26ec0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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