Regular expressions in IDE lazarus / 9-Editor ---------------------------------------------------------------------- cd · 2009-03-26 21:03 hi all tried to replace "; " into ";\n" (i mean newline) and could not find how to do this... is this feature missing? ---------------------------------------------------------------------- Leledumbo · 2009-03-27 09:55 IDE regular expressions ---------------------------------------------------------------------- cd · 2009-03-27 13:15 yes i've seen this it tells about how to search for , but nothing about how to replace TO ---------------------------------------------------------------------- cd · 2009-03-28 00:50 the problem is solved now building lazarus from lazarus rulez ---------------------------------------------------------------------- Vincent Snijders · 2009-03-28 08:42 How was the problem solved? ---------------------------------------------------------------------- cd · 2009-03-28 11:43 in TSourceEditor.DoFindAndReplace function Code: [Select]    AReplace:=LazFindReplaceDialog.ReplaceText;     if(ssoRegExpr in LazFindReplaceDialog.Options)then       AReplace:=StringReplace(AReplace,'\n',#13#10,[rfReplaceAll]);     Result:=EditorComponent.SearchReplace(       LazFindReplaceDialog.FindText,AReplace,       LazFindReplaceDialog.Options); instead of Code: [Select]    Result:=EditorComponent.SearchReplace(       LazFindReplaceDialog.FindText, LazFindReplaceDialog.ReplaceText,       LazFindReplaceDialog.Options); later i've added a checkbox SlashNForNewlineCheckBox to findreplacedialog - to make this feature optional, and added checking status of this chekcbox in replace function Code: [Select]    AReplace:=LazFindReplaceDialog.ReplaceText;     if(ssoRegExpr in LazFindReplaceDialog.Options)and(LazFindReplaceDi alog.SlashNForNewlineCheckBox.Checked)then       AReplace:=StringReplace(AReplace,'\n',#13#10,[rfReplaceAll]);     Result:=EditorComponent.SearchReplace(       LazFindReplaceDialog.FindText,AReplace,       LazFindReplaceDialog.Options); it is a quick and not very correct decision, because in this case ReplaceText='\\n' will be changed to '\'#13#10 but not '\n' as expected. this is also a reason i made this feature optional ---------------------------------------------------------------------- cd · 2009-03-28 21:43 Here is the new solution. A variation of function TSourceEditor.DoFindAndReplace that enables \n=System.LineEnding to use with RegExp search&replace in the replace- to string. It deals correctly with "\\n". One can easily upgrade this function by adding a new escaped string and its "meaning" in the arrays in call Code: [Select]AReplace:=ReplaceArray(AReplace,['\\','\n'],['\\',System .LineEnding]);for instance \t=#9 => Code: [Select]AReplace:=ReplaceArray(AReplace,['\\','\n','\t'],['\\',S ystem.LineEnding,#9]); Function code: Code: [Select]function TSourceEditor.DoFindAndReplace: integer; var   OldCaretXY: TPoint;   AText, ACaption: String;   NewTopLine: integer;   AReplace: String;   function ReplaceArray(source : string; strings_from : array of string; strings_to : array of string) : string;   var poss : array of integer;       minpos : integer; //index in array       result1 : string;     procedure calcPos;     var i : integer;     begin       for i:=0 to length(strings_from)-1 do       begin         poss[i]:=System.Pos(strings_from[i],result);         if(poss[i]=0)then           poss[i]:=Length(result)+1;         if(i=0)then           minpos:=i         else           if(poss[i]Length(strings_from))then       raise Exception.Create('from and to arrays must be of the same length');     SetLength(poss,length(strings_from));     result1:='';     result:=source;     calcPos;     while(poss[minpos]nil then     SourceNotebook.AddJumpPointClicked(Self);   if (ssoReplace in LazFindReplaceDialog.Options)   and ReadOnly then begin     DebugLn(['TSourceEditor.DoFindAndReplace Read only']);     exit;   end;   OldCaretXY:=EditorComponent.CaretXY;   if EditorComponent.SelAvail then begin     if ssoBackwards in LazFindReplaceDialog.Options then       EditorComponent.LogicalCaretXY:=EditorComponent.BlockBegin     else       EditorComponent.LogicalCaretXY:=EditorComponent.BlockEnd   end;   //debugln('TSourceEditor.DoFindAndReplace A LazFindReplaceDialog.Fin dText="',dbgstr(LazFindReplaceDialog.FindText),'" ssoEntireScope=',dbgs(ssoEntireScope in LazFindReplaceDialog.Options),' ssoBackwards=',dbgs(ssoBackwards in LazFindReplaceDialog.Options));   try     if (ssoReplace in LazFindReplaceDialog.Options)then     begin       AReplace:=LazFindReplaceDialog.ReplaceText;       if(ssoRegExpr in LazFindReplaceDialog.Options)then         AReplace:=ReplaceArray(AReplace,['\\','\n'],['\\',System.LineEnding]);     end     else       AReplace:='';     Result:=EditorComponent.SearchReplace(       LazFindReplaceDialog.FindText,AReplace,       LazFindReplaceDialog.Options);   except     on E: ERegExpr do begin       MessageDlg(lisUEErrorInRegularExpression,         E.Message,mtError,[mbCancel],0);       exit;     end;   end;   if (OldCaretXY.X = EditorComponent.CaretX) and      (OldCaretXY.Y = EditorComponent.CaretY) and      not (ssoReplaceAll in LazFindReplaceDialog.Options) then   begin     ACaption := lisUENotFound;     AText := Format(lisUESearchStringNotFound, [ValidUTF8String(LazFindReplaceDialog.FindText)]);     MessageDlg(ACaption, AText, mtInformation, [mbOk], 0);     TSourceNotebook(Owner).DeleteLastJumpPointClicked(Self);   end else   if (EditorComponent.CaretY <= EditorComponent.TopLine + 1) or      (EditorComponent.CaretY >= EditorComponent.TopLine + EditorComponent.LinesInWindow - 1) then   begin     NewTopLine := EditorComponent.CaretY - (EditorComponent.LinesInWindow div 2);     if NewTopLine < 1 then       NewTopLine := 1;     EditorComponent.TopLine := NewTopLine;   end; end;