[Lazarus] Synedit and search/replace

Mark Morgan Lloyd markMLl.lazarus at telemetry.co.uk
Sat Feb 25 20:46:50 CET 2012


Martin wrote:
> On 25/02/2012 18:40, Mark Morgan Lloyd wrote:
>> Martin wrote:
>>> On 25/02/2012 16:48, Mark Morgan Lloyd wrote:
>>>> Is my understanding correct that in order to get Synedit's replace 
>>>> facility working, one has to write a custom OnReplaceText handler?
>>>>
>>>> What does this in the source do:
>>>>
>>>> CurReplace:=AReplace;
>>>> if ssoRegExpr in AOptions then
>>>>   CurReplace:=fTSearch.RegExprReplace;
>>>>
>>>> Is there (partial) support for regex-based S&R, and if so how does 
>>>> one use it?
>>>>
>>>
>>> What is wrong with:
>>> replace one (first after x/y = 1/1)
>>>   SynEdit1.SearchReplaceEx('a(.)', 'b$1', [ssoReplace, ssoRegExpr], 
>>> Point(1,1));
>>>
>>> or replace all (after x/y = 1/1)
>>>   SynEdit1.SearchReplaceEx('a(.)', 'b$1', [ssoReplaceAll, 
>>> ssoRegExpr], Point(1,1));
>>>
>>> SynEdit has no build-in confirm dialog, if that is what you mean
>>
>> No, I just mean I don't understand how to use it and can't find any 
>> usage notes. What's supposed to happen- does Synedit call the 
>> OnReplaceText handler and expect this to actually do the 
>> search/replace? Or is the handler there to allow a confirmation 
>> dialogue to be displayed, after which code like you've shown above 
>> should be called explicitly?
>>
>> (I think I understand the regex usage in your example, thanks for that).
>>
> 
> Yes I am aware of the total lack of documentation for SynEdit...

But at least Google should pick this thread up.

> Anyway:
> 
> 1) you need to use "SyneditTypes"
> 
> the options are
>   TSynSearchOption =
>     ( ssoMatchCase, ssoWholeWord,
>       ssoBackwards,
>       ssoEntireScope, ssoSelectedOnly,
>       ssoReplace, ssoReplaceAll,
>       ssoPrompt,
>       ssoSearchInReplacement,    // continue search-replace in 
> replacement (with ssoReplaceAll) // replace recursive
>       ssoRegExpr, ssoRegExprMultiLine,
>       ssoFindContinue      // Assume the current selection is the last 
> match, and start search behind selection
>                            // (before if ssoBackward) // Default is to 
> start at caret (Only SearchReplace / SearchReplaceEx has start/end param)
>     );
>   TSynSearchOptions = set of TSynSearchOption;
> 
> 2) The callback
>   bPrompt := (ssoPrompt in AOptions);
>         if bPrompt and Assigned(fOnReplaceText) then begin
>             nAction := DoOnReplaceText(ASearch,CurReplace,
>                                        ptFoundStart.Y,ptFoundStart.X);
> // calling your event
>   TReplaceTextEvent = procedure(Sender: TObject; const ASearch, AReplace:
>     string; Line, Column: integer; var ReplaceAction: TSynReplaceAction) 
> of object;
> 
> 
> NOTE: if you call BeginUpdate, then SynEdit can not paint. So make sure 
> you have not called it.
> 
> The editor will select the found text and move it into the visible area, 
> before it calls the event
> 
> your callback can set TSynReplaceAction to:
>   TSynReplaceAction = (raCancel, raSkip, raReplace, raReplaceAll);
> 
> Cancel, aborts the search
> ReplaceAll, means no further callbacks

Thanks Martin, I think I've got that :-)

It looks as though my basic mistake was setting ssoPrompt, at which 
point since I'd implemented an empty callback (i.e. wasn't setting 
ReplaceAction) nothing was happening.

-- 
Mark Morgan Lloyd
markMLl .AT. telemetry.co .DOT. uk

[Opinions above are the author's, not those of his employers or colleagues]




More information about the Lazarus mailing list