diff options
author | Florian Fischer <florian.fl.fischer@fau.de> | 2020-08-26 14:30:51 +0200 |
---|---|---|
committer | Florian Fischer <florian.fl.fischer@fau.de> | 2020-08-26 14:30:51 +0200 |
commit | 6619dd4570be1b00f5611138ce32fda414e69ba7 (patch) | |
tree | c17cc4fcc5e39ebbd0f0ab7a73b216d5eaa4e040 | |
parent | b90ae91b24a214fb603f90855a3247138e8c9422 (diff) | |
download | vis-spellcheck-6619dd4570be1b00f5611138ce32fda414e69ba7.tar.gz |
extent get_typo() to handle string inputs
This allows us to be independent of a vis.window and only use
the text passed to lexer.lex for our spellchecking.
This is helpfull when the active window is for example the command
prompt but the original source file still gets lexed.
-rw-r--r-- | spellcheck.lua | 42 |
1 files changed, 34 insertions, 8 deletions
diff --git a/spellcheck.lua b/spellcheck.lua index 6ecaec0..52a2e54 100644 --- a/spellcheck.lua +++ b/spellcheck.lua @@ -31,23 +31,49 @@ spellcheck.check_tokens = { } --- Return nil or a string of misspelled word in a specific file range +-- 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 get_typos(range) +local function get_typos(range_or_text) local cmd = spellcheck.list_cmd:format(spellcheck.lang) - local ret, so, se = vis:pipe(vis.win.file, range, cmd) - if ret ~= 0 then - vis:info("calling " .. cmd .. " failed ("..ret..")") - return nil + local typos = nil + if type(range_or_text) == "string" then + local text = range_or_text + local tmp_name = os.tmpname() + local full_cmd = cmd .. "> " .. tmp_name .. supress_stderr + local proc = assert(io.popen(full_cmd, "w")) + proc:write(text) + -- this error detection may need lua5.2 + local success, reason, exit_code = proc:close() + if not success then + vis:info("calling " .. cmd .. " failed ("..exit_code..")") + return nil + end + + local tmp_file = io.open(tmp_name, "r") + typos = tmp_file:read("*a") + tmp_file:close() + os.remove(tmp_name) + else + local range = range_or_text + local ret, so, se = vis:pipe(vis.win.file, range, cmd) + + if ret ~= 0 then + vis:info("calling " .. cmd .. " failed ("..ret..")") + return nil + end + typos = so end - return so + + return typos end -- plugin global list of ignored typos local ignored = {} - -- Return an iterator over all not ignored typos and their positions in text. -- The returned iterator is a seelf contained statefull iterator function closure. -- Which will return the next typo and ist start and finish in the text, starting by 1. |