From 96021e8a558ed82100b5c6d1509300804912c35e Mon Sep 17 00:00:00 2001 From: Daniel Prado Velasco Date: Sat, 27 Jan 2024 19:56:18 +0100 Subject: [PATCH] Optimizations in Replace All --- editor/kn_FindReplaceMng.pas | 77 ++++++++++++++++++++---------------- editor/kn_LinksMng.pas | 15 +++++-- 2 files changed, 55 insertions(+), 37 deletions(-) diff --git a/editor/kn_FindReplaceMng.pas b/editor/kn_FindReplaceMng.pas index 25a7068a..1be297c8 100644 --- a/editor/kn_FindReplaceMng.pas +++ b/editor/kn_FindReplaceMng.pas @@ -1339,7 +1339,7 @@ function RunFindNext (Is_ReplacingAll: Boolean= False): boolean; TTreeNote( ActiveNote ).TV.Selected := myTreeNode; end; - SearchCaretPos (myNote, myTreeNode, PatternPos, length( Text_To_Find) + SizeInternalHiddenText, true); + SearchCaretPos (myNote, myTreeNode, PatternPos, length( Text_To_Find) + SizeInternalHiddenText, true, false); end; @@ -1513,6 +1513,38 @@ procedure ReplaceEventProc( ReplaceAll : boolean ); AppliedBeginUpdate: Boolean; + procedure BeginUpdateOnNotes; + var + i: integer; + note: TTabNote; + begin + AppliedBeginUpdate:= True; + for i := 0 to Form_Main.Pages.PageCount - 1 do + begin + note:= TTabNote(Form_Main.Pages.Pages[i].PrimaryObject); + note.Editor.BeginUpdate; + note.Editor.OnSelectionChange := nil; + note.Editor.Visible:= False; + end; + end; + + procedure EndUpdateOnNotes; + var + i: integer; + note: TTabNote; + begin + if not AppliedBeginUpdate then exit; + + for i := 0 to Form_Main.Pages.PageCount - 1 do + begin + note:= TTabNote(Form_Main.Pages.Pages[i].PrimaryObject); + note.Editor.EndUpdate; + note.Editor.OnSelectionChange := Form_Main.RxRTFSelectionChange; + note.Editor.Visible:= True; + end; + AppliedBeginUpdate:= False; + end; + function GetReplacementConfirmation: Boolean; begin Result := false; @@ -1525,6 +1557,8 @@ procedure ReplaceEventProc( ReplaceAll : boolean ); mrAll: begin Result := true; FindOptions.ReplaceConfirm := false; + BeginUpdateOnNotes; + screen.Cursor := crHourGlass; end; mrCancel: begin Result := false; @@ -1558,33 +1592,6 @@ procedure ReplaceEventProc( ReplaceAll : boolean ); end; end; - procedure BeginUpdateOnNotes; - var - i: integer; - note: TTabNote; - begin - AppliedBeginUpdate:= True; - for i := 0 to Form_Main.Pages.PageCount - 1 do - begin - note:= TTabNote(Form_Main.Pages.Pages[i].PrimaryObject); - note.Editor.EndUpdate; - end; - end; - - procedure EndUpdateOnNotes; - var - i: integer; - note: TTabNote; - begin - if not AppliedBeginUpdate then exit; - - for i := 0 to Form_Main.Pages.PageCount - 1 do - begin - note:= TTabNote(Form_Main.Pages.Pages[i].PrimaryObject); - note.Editor.EndUpdate; - end; - AppliedBeginUpdate:= False; - end; begin if assigned( Form_FindReplace ) then begin @@ -1619,7 +1626,13 @@ procedure ReplaceEventProc( ReplaceAll : boolean ); // directamente. SelectedTextToReplace:= False; if not ReplaceAll then - SelectedTextToReplace:= IdentifySelectedTextToReplace; + SelectedTextToReplace:= IdentifySelectedTextToReplace + else + if not FindOptions.ReplaceConfirm then begin + BeginUpdateOnNotes; + screen.Cursor := crHourGlass; + end; + if not SelectedTextToReplace then begin SelectedTextToReplace:= RunFindNext(ReplaceAll); @@ -1631,8 +1644,6 @@ procedure ReplaceEventProc( ReplaceAll : boolean ); if DoReplace then begin - if ReplaceAll and not FindOptions.ReplaceConfirm then - BeginUpdateOnNotes; while SelectedTextToReplace do begin @@ -1645,8 +1656,7 @@ procedure ReplaceEventProc( ReplaceAll : boolean ); if GetReplacementConfirmation then begin inc(ReplaceCnt); - ActiveNote.Editor.SelText := FindOptions.ReplaceWith; - ActiveNote.Editor.SelStart := ActiveNote.Editor.SelStart + length( ActiveNote.Editor.SelText ); + ActiveNote.Editor.AddText(FindOptions.ReplaceWith); end; Application.ProcessMessages; @@ -1667,6 +1677,7 @@ procedure ReplaceEventProc( ReplaceAll : boolean ); end; finally + screen.Cursor := crDefault; EndUpdateOnNotes; Is_Replacing := false; UserBreak := false; diff --git a/editor/kn_LinksMng.pas b/editor/kn_LinksMng.pas index 47dc46cc..9fde7715 100644 --- a/editor/kn_LinksMng.pas +++ b/editor/kn_LinksMng.pas @@ -60,7 +60,9 @@ interface function BuildKNTLocationText( const aLocation : TLocation) : string; procedure JumpToKNTLocation( LocationStr : string ); function JumpToLocation( Location: TLocation; IgnoreOtherFiles: boolean = true): boolean; - function SearchCaretPos (myNote : TTabNote; myTreeNode: TTreeNTNode; CaretPosition: integer; SelectionLength: integer; PlaceCaret: boolean): integer; + function SearchCaretPos (myNote : TTabNote; myTreeNode: TTreeNTNode; + CaretPosition: integer; SelectionLength: integer; PlaceCaret: boolean; + AdjustVisiblePosition: boolean = true): integer; function PositionInImLinkTextPlain (myNote: TTabNote; myTreeNode: TTreeNTNode; CaretPosition: integer; ForceCalc: boolean = false): integer; procedure ClickOnURL(const URLstr: string; chrgURL: TCharRange; myURLAction: TURLAction; EnsureAsk: boolean = false); @@ -991,7 +993,9 @@ function GetPositionOffset (myNote : TTabNote; myTreeNode: TTreeNTNode; Pos_ImLi end; -function SearchCaretPos (myNote : TTabNote; myTreeNode: TTreeNTNode; CaretPosition: integer; SelectionLength: integer; PlaceCaret: boolean): integer; +function SearchCaretPos (myNote : TTabNote; myTreeNode: TTreeNTNode; + CaretPosition: integer; SelectionLength: integer; PlaceCaret: boolean; + AdjustVisiblePosition: boolean = true): integer; var Offset: integer; Pos_ImLinkTextPlain: integer; @@ -1007,8 +1011,11 @@ function SearchCaretPos (myNote : TTabNote; myTreeNode: TTreeNTNode; CaretPositi SelStart := CaretPosition - Offset; if SelectionLength >= 0 then SelLength := SelectionLength; - myNote.Editor.ScrollLinesBy(80); - Perform( EM_SCROLLCARET, 0, 0 ); + if AdjustVisiblePosition then begin + myNote.Editor.ScrollLinesBy(80); + Perform( EM_SCROLLCARET, 0, 0 ); + end; + finally EndUpdate; end;