0

I have a string that contains Linux style line breaks. Linux style is #13 while Windows style is #13#10.

I would like to show this string in a TMemo. Looks like TMemo accepts only Windows style and does not treat #13 as a new line.

Is the only way for TMemo to format new lines is to insert #10, or can I somehow ask TMemo to act in Linux style?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
vico
  • 17,051
  • 45
  • 159
  • 315

2 Answers2

2

I would like to show this string in Memo. Looks like Memo accepts only Windows style and does not treat #$13 as new line.

That depends on how you give the string to the Memo.

The underlying Win32 EDIT control that TMemo wraps only supports #13#10 style line breaks internally.

If you assign the string to the TMemo.Text property, it will just pass the string as-is to the underlying Win32 control. So, the string will need to use Windows-style line breaks only.

However, if you assign the string to the TMemo.Lines.Text property instead, it will internally adjust all styles of line breaks to Windows-style, and then give the adjusted string to the Win32 control. So, in that regard, you can handle Linux-style and Windows-style line breaks equally.

Alternatively, the TStringList class supports parsing all styles of line breaks (when its LineBreak property matches the sLineBreak constant, which it does by default). So, another option would be to first assign the string to the TStringList.Text property, and then you can assign the resulting list to the TMemo.Lines property.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
0

Actually Linux style is #10, not #13 (#13 is MacOS style, AFAIK). Also, note that it's #10 and not #$10 (which is #16).

The easiest way would be to replace the line ends on load/save, ie. instead of

Memo.Lines.LoadFromFile(FileName)
or
Memo.Lines.Text := STR;

do

uses System.IOUtils;

Memo.Lines.Text := TFile.ReadAllText(FileName,TEncoding.UTF8).Replace(#13#10,#13).Replace(#10,#13).Replace(#13,#13#10)
or
Memo.Lines.Text := STR.Replace(#13#10,#13).Replace(#10,#13).Replace(#13,#13#10)

and instead of

Memo.Lines.SaveToFile(FileName)
or
STR := Memo.Lines.Text

do

uses System.IOUtils;

TFile.WriteAllText(Memo.Lines.Text.Replace(#13#10,#13),TEncoding.UTF8)
or
STR := Memo.Lines.Text.Replace(#13#10,#13)

Of course, you should replace the TEncoding.UTF8 with the appropriate encoding you want to use.

HeartWare
  • 7,464
  • 2
  • 26
  • 30
  • Another possible option is to use the Memo.Lines.LineBreak property. – MarkF Feb 10 '23 at 12:42
  • @MarkF: Unfortunately, that doesn't work in a Memo.Lines TStrings descendant. When you read the text out using Memo.Lines.Text it always returns it with #13#10 line breaks (due to an optimization that reads the entire Memo from Windows instead of reading it line-by-line, I believe). Also, whether or not you set LineBreak when assigning to Memo.Lines.Text, it supports #10 line breaks fine. – HeartWare Feb 10 '23 at 13:08
  • When you assign a string to `TMemo.Lines.Text`, internally it adjusts all styles of line breaks to `#13#10` and then sends a `WM_SETTEXT` message to the underlying window. – Remy Lebeau Feb 10 '23 at 17:45