Here is the English version of the explanation and the corrected script, ready
for you to share with the community or the original author.
Why the Original Script Failed (Infinite Loop)
Scintilla indicators work with values. When there is no search hit at a text
position, the indicator value there is 0. If you call SCI_INDICATORSTART at an
unmarked position, Scintilla looks for the start of the continuous range that
shares that same value (which is 0).
So, if you are at position n2 = 10 and there is no highlight,
SCI_INDICATORSTART will jump all the way back to 0 (the beginning of the file
where the text is also unmarked). This resets your n1 to 0, making n2 become 1
in the next iteration, trapping the script in an infinite loop.
The Solution: Using SCI_INDICATOREND (2509)
To reliably step through the search results, you need a combination of
SCI_INDICATORVALUEAT (2507) (to check if a highlight exists at the current
position) and SCI_INDICATOREND (2509) (to jump to the next state transition).
When you call SCI_INDICATOREND while sitting on an unmarked region, Scintilla
automatically fast-forwards to the start of the next actual highlight.
Here is the corrected and working Lua script:
Lua
```
local pos = 0
local doc_length = geany.scintilla(2006, 0, 0) -- SCI_GETLENGTH: Total document
length
while pos < doc_length do
-- 2507 = SCI_INDICATORVALUEAT: Check if the search indicator (8) is active
at 'pos' (> 0)
if geany.scintilla(2507, 8, pos) > 0 then
-- 2166 = SCI_LINEFROMPOSITION: Get the line number from the character
position
local line = geany.scintilla(2166, pos, 0)
-- 2043 = SCI_MARKERADD: Add a bookmark to this line
-- Note: Geany uses Scintilla marker 0 for its standard native bookmarks
geany.scintilla(2043, line, 0)
-- Jump to the end of this highlighted text block
local next_pos = geany.scintilla(2509, 8, pos) -- SCI_INDICATOREND
if next_pos <= pos then break end -- Safety brake against infinite loops
pos = next_pos
else
-- If there is no highlight here, jump to the end of the unhighlighted block
-- (= the exact starting point of the next search hit!)
local next_pos = geany.scintilla(2509, 8, pos) -- SCI_INDICATOREND
if next_pos <= pos then break end
pos = next_pos
end
end
```
Try this hope will help
--
Reply to this email directly or view it on GitHub:
https://github.com/geany/geany/discussions/4602#discussioncomment-17408983
You are receiving this because you are subscribed to this thread.
Message ID: <geany/geany/repo-discussions/4602/comments/[email protected]>