Skif-off left a comment (geany/geany-plugins#1590)
Added check that the document is not empty:
```lua
-- Replace-by-list.lua (cross-platform)
-- 2026.07.23
--[[
Find & replace by list. See the "aFR" table (the third parameter is the
parameters of the "geany.find" function, see
https://plugins.geany.org/geanylua/geanylua-ref.html).
Idea: https://github.com/geany/geany-plugins/issues/1590
P.S. If the found string is located at the beginning of the selection,
then for some reason after the replacement the selection starts AFTER
the replacement string. So, we will "restore" the selection.
]]
local aFR = {
{"find1", "replace1", {"matchcase"}},
{"find2", "replace2", {}},
{"find3", "replace3", {"matchcase", "wholeword"}}
}
local function IsRegExp(iN)
if #aFR[iN][3] == 0 then return false end
for i = 1, #aFR[iN][3] do
if aFR[iN][3][i] == "regexp" then return true end
end
return false
end
local function ReplaceFull()
local iSciM, iPS, iPE, iS, iE, iLR, iLRR
-- SCI_BEGINUNDOACTION
geany.scintilla(2078, 0, 0)
for i = 1, #aFR do
-- SCI_REPLACETARGET (2194) or SCI_REPLACETARGETRE (2195)
if IsRegExp(i) == false then iSciM = 2194 else iSciM = 2195 end
iPS = 0
iPE = geany.length()
iLR = string.len(aFR[i][2])
while true do
iS, iE = geany.find(aFR[i][1], iPS, iPE, aFR[i][3])
if iS == nil then break end
-- SCI_SETTARGETSTART
geany.scintilla(2190, iS, 0)
-- SCI_SETTARGETEND
geany.scintilla(2192, iE, 0)
-- SCI_REPLACETARGET or SCI_REPLACETARGETRE
iLRR = geany.scintilla(iSciM, iLR, aFR[i][2])
iPS = iS + iLRR
iPE = geany.length()
end
end
-- SCI_ENDUNDOACTION
geany.scintilla(2079, 0, 0)
end
local function ReplaceInSel(iN1, iN2)
local iSciM, iPS, iPE, iS, iE, iLR, iLRR
-- SCI_BEGINUNDOACTION
geany.scintilla(2078, 0, 0)
iPE = iN2
for i = 1, #aFR do
-- SCI_REPLACETARGET (2194) or SCI_REPLACETARGETRE (2195)
if IsRegExp(i) == false then iSciM = 2194 else iSciM = 2195 end
iPS = iN1
iLR = string.len(aFR[i][2])
while true do
iS, iE = geany.find(aFR[i][1], iPS, iPE, aFR[i][3])
if iS == nil then break end
-- SCI_SETTARGETSTART
geany.scintilla(2190, iS, 0)
-- SCI_SETTARGETEND
geany.scintilla(2192, iE, 0)
-- SCI_REPLACETARGET or SCI_REPLACETARGETRE
iLRR = geany.scintilla(iSciM, iLR, aFR[i][2])
iPS = iS + iLRR
iPE = iPE - (iE - iS) + iLRR
end
end
-- SCI_ENDUNDOACTION
geany.scintilla(2079, 0, 0)
return iN1, iPE
end
local sSel = geany.selection()
if sSel == nil then
geany.message("There is no open document!")
return
end
-- SCI_GETLENGTH
local iL = geany.scintilla(2006, 0, 0)
if iL == 0 then
geany.message("Is the document empty?")
return
end
local iStart, iStop = geany.select()
if iStart == iStop then
ReplaceFull()
else
local iStartN, iStopN
if iStart < iStop then
iStartN, iStopN = ReplaceInSel(iStart, iStop)
geany.select(iStartN, iStopN)
elseif iStart > iStop then
iStopN, iStartN = ReplaceInSel(iStop, iStart)
geany.select(iStopN, iStartN)
end
end
geany.status(geany.basename(geany.script) .. ": Done.")
```
--
Reply to this email directly or view it on GitHub:
https://github.com/geany/geany-plugins/issues/1590#issuecomment-5178463026
You are receiving this because you are subscribed to this thread.
Message ID: <geany/geany-plugins/issues/1590/[email protected]>