Funktion dafür ist in SciTE4AutoIt bereits enthalten ( Common.lua; Common:ReplaceCharacters(p, r) ).
Jedes Vorkommen des über das Pattern p definierten Ausdrucks wird durch r ersetzt. Position des Carets und Ansicht im Editor werden beibehalten.
Meine Lösung ist vom Funktionsumfang her identisch. Ich habe nur einen anderen Weg gewählt, um die Caretposition und Anzeigezeile zu ermitteln und zu setzen. Dafür brauchte ich auch die Funktion: GenerateSciTEUserUUID()
Lua
--[[
----------------------------------------------------------------------------------------------------
Replaces each occurrence of an expression defined with a pattern with the passed string.
_pattern - the string pattern
_replacement - the replacement string
--------------------------------------------------------------------------------------------------]]
function ReplaceInEditor(_pattern, _replacement)
local pos = editor.CurrentPos
local line = editor:LineFromPosition(pos)
local column = editor.Column[pos]
local fvline = editor.FirstVisibleLine
local shiftline = line - fvline -- store difference from top
local uuid = GenerateSciTEUserUUID()
editor:BeginUndoAction()
editor:InsertText(pos, uuid) -- insert unique value to identify the caret position
local text = editor:GetText()
editor:SetText(text:gsub(_pattern, _replacement)) -- make the replacement(s)
pos = editor:GetText():find(uuid:gsub('%-', '%%-')) -1 -- search for the previous position of the cursor in the new text
local uuidline = editor:LineFromPosition(pos) -- current number from our marked line
local newfirstvisible = uuidline - shiftline -- to have the old distance
if newfirstvisible < 0 then newfirstvisible = 0 end
editor:SetSel(pos, pos + uuid:len())
editor:ReplaceSel('') -- remove the uuid
editor.FirstVisibleLine = newfirstvisible
editor:SetSel(pos, pos)
editor:EndUndoAction()
end -- ReplaceInEditor()
Alles anzeigen