From 0d224104468c332b3057c92ff297dfb90d8e35e2 Mon Sep 17 00:00:00 2001 From: Florian Fischer Date: Wed, 26 Aug 2020 16:30:49 +0200 Subject: change typo_iter to not find typos in correct spelled words When naivingly searching for the typo in our text we might find it before its actual appearense in a correct word containing the typo (e.g. "broken ok" would find the typo "ok" in the middle of the word "broken"). To prevent this we now only find a typo if its enclosed in non-letter characters, this prevents typos from being found in regular words. Typos starting with '"' for example are still found correctly. --- spellcheck.lua | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/spellcheck.lua b/spellcheck.lua index 59c5630..fe7d8a3 100644 --- a/spellcheck.lua +++ b/spellcheck.lua @@ -86,9 +86,14 @@ local function typo_iter(text, typos, ignored) until(not typo or not ignored[typo]) if typo then - local start, finish = text:find(typo, index, true) + -- to prevent typos from being found in correct words before them + -- ("stuff stuf", "broken ok", ...) + -- we match typos only when they are enclosed in non-letter characters. + local start, finish = text:find("[%A]" .. typo .. "[%A]", index) index = finish - return typo, start, finish + + -- ignore the first and last non letter character + return typo, start + 1, finish - 1 end end end -- cgit v1.2.3