aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlorian Fischer <florian.fischer@muhq.space>2025-12-09 14:40:17 +0100
committerFlorian Fischer <florian.fischer@muhq.space>2025-12-09 14:54:19 +0100
commit36ef33c448efb53b28fa59b474b66faaaf29f008 (patch)
tree0ba751e651adf19fe5037f1c5ec3aff383c77472
parent40c64db15f579e8f119f1850b14b851b1c1bde2b (diff)
downloadvis-spellcheck-36ef33c448efb53b28fa59b474b66faaaf29f008.tar.gz
vis-spellcheck-36ef33c448efb53b28fa59b474b66faaaf29f008.zip
create a file snapshot before modifying the textmaster
-rw-r--r--spellcheck.lua21
1 files changed, 21 insertions, 0 deletions
diff --git a/spellcheck.lua b/spellcheck.lua
index 4e4797c..a01170f 100644
--- a/spellcheck.lua
+++ b/spellcheck.lua
@@ -64,6 +64,24 @@ 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)
+--- Detect if vis supports file snapshots via lua
+-- This is a function to lazily evaluate vis' capacity, because during startup
+-- there is no file available.
+local vis_supports_file_snapshot
+do
+ local _vis_supports_file_snapshot
+
+ vis_supports_file_snapshot = function()
+ if _vis_supports_file_snapshot ~= nil then
+ return _vis_supports_file_snapshot
+ end
+ if vis.win and vis.win.file and _vis_supports_file_snapshot == nil then
+ _vis_supports_file_snapshot = pcall(vis.win.file.snapshot, vis.win.file)
+ end
+ return _vis_supports_file_snapshot
+ end
+end
+
-- 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.
@@ -384,6 +402,9 @@ vis:map(vis.modes.NORMAL, '<C-w>w', function()
if status == 0 then
-- trim correction
correction = correction:match('^%s*(.-)%s*$')
+ if vis_supports_file_snapshot() then
+ win.file:snapshot()
+ end
win.file:delete(range)
win.file:insert(range.start, correction)
end