JohnLM 27 Posted Sunday at 10:04 AM Specs: Delphi XE7, VCL, Windows 7 So, while working in a new (isolated) project to figure out a better function() to use in one of my projects (SimpleBrowser), I stumbled upon a problem. And the subject says it all. I need to pull a custom function that I wrote from the SimpleBrowser project and work on it as a separate project. So, I select and copy it to the clipboard and then paste it into a new delphi project. The code snippet is pasted correctly in the IDE. However, when I selected and copied that code again from that new project and pasted it into notepad (because for every new function I create, I added to my "delphi code snippet" file) all the source code are on one line. If I follow that same process but copy from the original project, I get the same issues, all the source codes are on one line--there is no carriage returns, no formating, etc.. I tried to figure out why this is happening, and I can only assume that something unique was set in the IDE of this demo project, "SimpleBrowser" but I can't figure it out. This SimpleBrowser is a demo from the "CEF4Delphi" project. The folder is: "..\CEF4Delphi\WebView4Delphi\demos\Delphi_VCL\SimpleBrowser" Does anyone have any clues as to what is going on ? Share this post Link to post
dummzeuch 1622 Posted Sunday at 10:12 AM You probably copied source code that only contained line feeds (#10) or only carriage return (#13) rather than the Windows (DOS) convention with carriage return followed by line feed (#13#10). Notepad cannot handle this, other editors (e.g. Notepad++) can. The latter can also convert between the different styles too. The IDE in the latest versions can automatically correct that for you, if this option is enabled. Share this post Link to post
JohnLM 27 Posted Wednesday at 10:00 PM (edited) When a new project is created, the default pattern is #13#10 (or, #$D#$A) and has been this way for ages. Unless there is a way to by-pass this by setting delphi's environment code to handle #10, I don't know how it could be done. However, I have one theory and that was already explained in my previous post. The developer of CEF4Delphi had probably been using notepad++ or some other editor that uses on #10, and at some point was pushing all the source code into delphi projects, that is, was saving them this way, as #10. And, since Delphi can handle the #10, it went on this way up to this point for the CEF4Delphi project. ill regardless of which editor was used, and the method to transport the source codes, the saved state was using #10 and that is what was pushed into these CEF4Delphi demos. So all the demo's pas files have #10 in them, and Delphi opens them without any issues. If a person goes into the Delphi IDE and select/copy's it to the clipboard to paste into Windows standard notepad, or proceeds to open any of the demos in notepad, notepad will open it as one line because of the missing #13 char for every linefeed before wrapping to the next line, as shown below. For now, I will have to deal with this in my own way. And I have figure out one way to deal with this issue in order to continue using the demo projects as I add or write my own custom functions in them and/or push them into my notepad file of codes snippets. function fix13and10str(sStr: string): string; var s: string; begin s := StringReplace(sStr, #$A, #$D#$A, [rfReplaceAll]); result:= StringReplace(s, #$D#$D#$A, #$D#$A, [rfReplaceAll]); end; // example usage procedure TForm1.btnProcessClick(Sender: TObject); var s: string; begin s := Fix13And10Str(clipboard.AsText); m1.Lines.Add(s); end; I will figure out a way to fix this the code (above) to handle a source file copied to the clipboard to process when copying whole source codes from the Delphi IDE that could potentially be hundreds of lines. For now, I am on the right track. For the record, I do not use notepad++ and I am not saying it is bad or anything. I just never found uses for it up to this point. Edited Wednesday at 10:07 PM by JohnLM fixed typos Share this post Link to post
Remy Lebeau 1571 Posted Thursday at 01:01 AM (edited) 3 hours ago, JohnLM said: I will figure out a way to fix this the code (above) to handle a source file copied to the clipboard to process when copying whole source codes from the Delphi IDE that could potentially be hundreds of lines. First off, you don't need your fix13and10str() function, as the RTL has its own AdjustLineBreaks() function: Quote AdjustLineBreaks adjusts all line breaks in the given string to the indicated style. When Style is tlbsCRLF, the function changes all CR characters not followed by LF and all LF characters not preceded by CR into CR/LF pairs. When Style is tlbsLF, the function changes all CR/LF pairs and CR characters not followed by LF to LF characters. For example: uses ..., System.SysUtils; procedure TForm1.btnProcessClick(Sender: TObject); var s: string; begin s := AdjustLineBreaks(Clipboard.AsText, tlbsCRLF); // or, just let it use the platform default style... // s := AdjustLineBreaks(Clipboard.AsText); m1.Lines.Add(s); end; Alternatively, you can assign the clipboard text as-is to a TStrings.Text property and let it parse the line breaks for you: https://docwiki.embarcadero.com/Libraries/en/System.Classes.TStrings.Text Quote When setting Text, the value will be parsed and separated into substrings whenever the LineBreak value is encountered. For backward compatibility, on POSIX, if the LineBreak separator is LF, then LF, CR, or CRLF are treated as separators. On Windows if the LineBreak separator is CRLF, then LF, CR, or CRLF are treated as separators. For example: procedure TForm1.btnProcessClick(Sender: TObject); var sl: TStringList; begin sl := TStringList.Create; try // sl.LineBreak is set to System.sLineBreak by default... // sl.LineBreak := sLineBreak; sl.Text := Clipboard.AsText; m1.Lines.AddStrings(sl); finally sl.Free; end; end; Or simpler (if you don't mind the entire TMemo content being replaced): procedure TForm1.btnProcessClick(Sender: TObject); begin m1.Lines.Text := Clipboard.AsText; end; 3 hours ago, JohnLM said: For the record, I do not use notepad++ and I am not saying it is bad or anything. I just never found uses for it up to this point. One handy use-case is changing line breaks in text. You can copy the code from the IDE, paste it into Notepad++ (or just open the original file directly in Notepad++), specify a new type of line feed (bare-CR, bare-LF, and CRLF are supported), copy the new text back to the clipboard, and paste it into your app. Edited Thursday at 01:07 AM by Remy Lebeau 1 Share this post Link to post
JohnLM 27 Posted Thursday at 09:52 PM Ah, yes.. AdjustLineBreaks() thank you for that. I did not know about that function. I will save the code snippets you provided for future projects. Although I have not tested it, I believe that the later versions of Delphi provide the feature build-in under: \Options\Editor\Line Endings\Inconsistent line endings: [Convert all files to CRLF] The above is in D12.2 on my win10 tablet. So, I would not have to go through the steps you outlined in your previous repy, Remy. The select/copy/paste into win10's notepad would probably work without issues, though I have not tested it. I will update true/false once I get around to testing this. Thanks again for your help, Remy. Share this post Link to post