diff options
| author | Florian Fischer <florian.fischer@muhq.space> | 2025-10-22 19:21:17 +0200 |
|---|---|---|
| committer | Florian Fischer <florian.fischer@muhq.space> | 2025-12-09 14:54:19 +0100 |
| commit | 40c64db15f579e8f119f1850b14b851b1c1bde2b (patch) | |
| tree | 2a8e1301830353e3d7a36ea577cdee64e1eb3454 | |
| parent | 47c68382bdcd864efb098b309fcf113a51b79a21 (diff) | |
| download | vis-spellcheck-40c64db15f579e8f119f1850b14b851b1c1bde2b.tar.gz vis-spellcheck-40c64db15f579e8f119f1850b14b851b1c1bde2b.zip | |
use vis' pipe buffer support to check a string for typos
Since lua does not support bidirectional popen, we used to write to
a temporary file each time we checked some string for typos.
A string is always checked when using the syntax aware mode.
Using the buffered pipe offered by vis removes potential overhead
introduced by the write/read cycle and preserve SSD writes.
| -rw-r--r-- | spellcheck.lua | 14 |
1 files changed, 11 insertions, 3 deletions
diff --git a/spellcheck.lua b/spellcheck.lua index 3ef5e04..4e4797c 100644 --- a/spellcheck.lua +++ b/spellcheck.lua @@ -61,6 +61,9 @@ end local supress_stderr = ' 2>/dev/null' +-- Detect if vis supports piping a buffer to an external command. +local vis_supports_pipe_buf = pcall(vis.pipe, vis, 'foo', 'true', false) + -- Return nil or a sequence of misspelled words 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. @@ -70,7 +73,8 @@ local supress_stderr = ' 2>/dev/null' local function get_typos(range_or_text) local cmd = spellcheck.list_cmd:format(spellcheck.get_lang()) local typos - if type(range_or_text) == 'string' then + -- Legacy fallback using a temporary file as output + if type(range_or_text) == 'string' and not vis_supports_pipe_buf then local text = range_or_text local tmp_name = os.tmpname() local full_cmd = cmd .. '> ' .. tmp_name .. supress_stderr @@ -88,8 +92,12 @@ local function get_typos(range_or_text) tmp_file:close() os.remove(tmp_name) else - local range = range_or_text - local ret, so, _ = vis:pipe(vis.win.file, range, cmd) + local ret, so + if type(range_or_text) == 'string' then + ret, so = vis:pipe(range_or_text, cmd) + else + ret, so = vis:pipe(vis.win.file, range_or_text, cmd) + end if ret ~= 0 then vis:info('calling ' .. cmd .. ' failed (' .. ret .. ')') |
