Skif-off left a comment (geany/geany-plugins#1590)
Try to test this variant
```lua
-- Replace-text.lua (cross-platform)
-- 2026.07.13
--[[
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
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
end
local function ReplaceInSel(iN1, iN2)
local iSciM, iPS, iPE, iS, iE, iLR, iLRR
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
return iN1, iPE
end
local sSel = geany.selection()
if sSel == nil then
geany.message("There is no open document!")
return
end
if sSel == "" then
ReplaceFull()
else
sSel = nil
local iStart, iStop, iStartN, iStopN
iStart, iStop = geany.select()
if iStart < iStop then
iStartN, iStopN = ReplaceInSel(iStart, iStop)
geany.select(iStartN, iStopN)
else
iStopN, iStartN = ReplaceInSel(iStop, iStart)
geany.select(iStopN, iStartN)
end
end
geany.status(geany.basename(geany.script) .. ": Done.")
```
I tried to use `geany.find` and `SCI_REPLACETARGET` instead of built-in Lua
functions, it was assumed that this would require less attention to restoring
the cursor position and/or selection :) Also this allows to use more familiar
regular expressions (instead of Lua patterns) and doesn't require UTF-8 support
(case sensitivity and so on) in Lua.
--
Reply to this email directly or view it on GitHub:
https://github.com/geany/geany-plugins/issues/1590#issuecomment-5005041694
You are receiving this because you are subscribed to this thread.
Message ID: <geany/geany-plugins/issues/1590/[email protected]>