Unfortunately it is not so easy but test this macro. It is only for one selection, no multiselection via Ctrl.
Sub findReplaceInSelection
on local error goto bug
dim oDoc as object, oSel as object, oDesc as object, p(), oFound as object, s$, i&, a&, undoMgr as object, pp()
const cUndo="F&R in Selection"
const special="¿" 'special character, it is needful also for test at the end vith oVCur !!!
'arrays with replacements: array(Find, Replace)
p=array( array("([.!?])$", "$1" & special), array("$", " "), array(special, " " & chr(13)) ) '!!! use only $1, $2 etc. for regex replacement in Replace part !!!
oDoc=ThisComponent
undoMgr=oDoc.UndoManager 'undo manager
undoMgr.enterUndoContext(cUndo) 'only one step in Undo
oDesc=oDoc.createReplaceDescriptor()
with oDesc
.SearchCaseSensitive=true
.SearchRegularExpression=true
end with
oSel=oDoc.CurrentController.Selection.getByIndex(0) 'current selection (only for one selection, no multiselection via holding by Ctrl)
rem Find&Replace
for i=lbound(p) to ubound(p)
with oDesc
.SearchString=p(i)(0)
.ReplaceString=p(i)(1)
end with
oFound=oDoc.findNext(oSel.Start, oDesc) 'find 1st occurence in selection
do while not isNull(oFound)
a=oDoc.Text.compareRegionEnds(oFound, oSel) 'compare the Ends
if a<>-1 then 'the occurence is in selection
pp=getSubreg(oFound.String, p(i)(0))
if isNull(pp) then 'static replacing
oFound.string=p(i)(1)
else 'replace via regular sub-expressions
oFound.String=replRegString( pp(), p(i)(1) )
end if
else
exit do
end if
oFound=oDoc.findNext(oFound.End, oDesc)
loop
next i
rem delete special character if it is left at the end of selection
dim oStart as object, oVCur as object
oVCur=oDoc.CurrentController.ViewCursor
oStart=oVCur.Start 'remember the start position of visible cursor
oVCur.collapseToEnd
oVCur.goLeft(Len(special), true)
if oVCur.String=special then oVCur.String="" else oVCur.goRight(1, false)
oVCur.goToRange(oStart, true)
undoMgr.leaveUndoContext(cUndo)
exit sub
bug:
msgbox(sErr & ": " & sError & chr(13) & "Line: " & sErl & chr(13), 16, "findReplaceInSelection")
End Sub
Function replRegString(p(), sreg$) as string 'replace the $1 in string for found characters; p() is array from function getSubreg
dim i%
for i=lbound(p) to ubound(p)
sreg=replace(sreg, "$" & i+1, p(i))
next i
replRegString=sreg
End Function
Function getSubreg(optional s$, optional sreg$) as array 'get all sub-expressions from brackets (aa)(bb)
dim pole(2), oHledani, oHledaniParam, oNalez, oStart, oEnd, pocet%, i%, a()
oHledani=CreateUnoService("com.sun.star.util.TextSearch")
oHledaniParam=CreateUnoStruct("com.sun.star.util.SearchOptions")
With oHledaniParam
.algorithmType=com.sun.star.util.SearchAlgorithms.REGEXP
.searchString=sreg 'regular expression
End With
oHledani.setOptions(oHledaniParam)
oNalez=oHledani.searchForward(s,0,len(s)) 'search string from start to end
oStart=oNalez.startOffset() 'array with starts of sub-expressions
oEnd=oNalez.endOffset() 'array with ends of sub-expressions
rem get sub-expressions
pocet=oNalez.subRegExpressions()-1
if pocet<1 then exit function '-1= no found; 0=no subexpressions
redim a(pocet-1) 'output array
for i=1 to pocet
a(i-1)=Mid(s,oStart(i)+1,oEnd(i)-oStart(i)) 'the characters from sub-expression
next i
getSubreg=a()
End Function
The details of problems: There isn’t command for regexp F&R only in Selection :-(. It is not problem to find all occurences in Selection with regex searching, but the problem is to replace these occurences with regex replacing. It need the method com.sun.star.util.TextSearch to get sub-expressions from brackets like (aa)(bb)
and put ones to replacements instead $1,$2
etc. And this procedure could be complicated, but it seems it is functional.
Safe solution is copy the selected part to new document, use replaceAll and paste it back, but it will be slower.