[SOLVED]I implemented the undo/redo method,but it doesn't work properly at Linux lazarus / 31-LCL ---------------------------------------------------------------------- tomitomy · 2017-10-19 16:22 Please visit the new version through the link bollow(support undo by word): https://forum.lazarus.freepascal.org/index.php/topic,54069.0.html I solved this problem, see: http://forum.lazarus.freepascal.org/index.php/topic,38714.0.html Thanks for Handoko's help, and thanks for accorp, he fixes TMemo's BUG. Thank you very much both! ---------------------------------------------------------------------- tomitomy · 2017-10-20 00:45 The following code also has the same problem: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button1Click(Sender: TObject);var  i: Integer;begin  Memo1.Text := 'abcdefg';  for i := 1 to 5 do begin    Memo1.SelStart := 1;    Memo1.SelLength := 2;    Memo1.SelText := 'a';  end;end; The result should be ‘aa', which is correct in Windows, but it is 'abcdefgaaaaa' in Linux. ---------------------------------------------------------------------- Handoko · 2017-10-20 02:55 I tested on Lazarus 1.8.0RC5 Linux 64 Gtk2 and yes it is a bug in TMemo.SelText := something. I checked into the source code (WSStdCtrl.pas) and I found this code seems not correct: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i class procedure TWSCustomEdit.SetSelText(const ACustomEdit: TCustomEdit;  const NewSelText: string);var  OldText, NewText: string;  OldPos: Integer;begin  OldPos := ACustomEdit.SelStart;  OldText := ACustomEdit.Text;  NewText := UTF8Copy(OldText, 1, OldPos) +           NewSelText +             UTF8Copy(OldText, OldPos + ACustomEdit.SelLength + 1, MaxInt);  ACustomEdit.Text := NewText;  ACustomEdit.SelStart := OldPos + UTF8Length(NewSelText);end;The SelStart on line #7 does not give the correct value. Do you already have account in the bug report forum? If not, you can create an account there and submit report this bug. Or if you want, I can help you submit this bug too. ---------------------------------------------------------------------- tomitomy · 2017-10-20 05:42 Quote from: Handoko on October 20, 2017, 04:55:19 amDo you already have account in the bug report forum? If not, you can create an account there and submit report this bug. Or if you want, I can help you submit this bug too. No, I have not account in the bug report forum, please help me to submit it, my English is not good, and I don't know how to report bug. Thak you very much! ---------------------------------------------------------------------- Handoko · 2017-10-20 06:48 I have submitted the issue to the bug tracker forum: https://bugs.freepascal.org/view.php?id=32583 ---------------------------------------------------------------------- tomitomy · 2017-10-20 07:28 Quote from: Handoko on October 20, 2017, 08:48:46 amI have submitted the issue to the bug tracker forum: https://bugs.freepascal.org/view.php?id=32583 Thank you very much! ---------------------------------------------------------------------- tomitomy · 2017-10-23 15:25 https://bugs.freepascal.org/view.php?id=32583 This BUG has been repaired, but there is still a problem, and the following code doesn't get the right result: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button1Click(Sender: TObject);begin  Memo1.Text := #10'abc';  Memo1.SelStart := 0;  Memo1.SelLength := 1;  Memo1.SelText := 'a';end; The result should be 'aabc', but it's #10'abca'. @Handoko, Can you help me to submit it again? Thank you! ---------------------------------------------------------------------- Handoko · 2017-10-23 16:08 Have you applied the patch? Or tried the revision r56138 or above? ---------------------------------------------------------------------- tomitomy · 2017-10-23 16:15 Quote from: Handoko on October 23, 2017, 06:08:03 pmHave you applied the patch? Or tried the revision r56138 or above? I have applied the patch. and This code works normally: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button1Click(Sender: TObject);var  i: Integer;begin  Memo1.Text := 'abcdefg';  for i := 1 to 5 do begin    Memo1.SelStart := 1;    Memo1.SelLength := 2;    Memo1.SelText := 'a';  end;  // Result is 'aa'end; But this code is not normal: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button1Click(Sender: TObject);begin  Memo1.Text := 'abc';  Memo1.SelStart := 0;  Memo1.SelLength := 1;  Memo1.SelText := 'd';   // Result should be 'dbc', but is 'abcd'end; ---------------------------------------------------------------------- Handoko · 2017-10-23 16:45 I have submitted the information to the bug tracker forum. ---------------------------------------------------------------------- tomitomy · 2017-10-23 16:51 Quote from: Handoko on October 23, 2017, 06:45:55 pmI have submitted the information to the bug tracker forum. Thank you very much! ---------------------------------------------------------------------- tomitomy · 2017-10-24 06:00 I have applied the attached patch, and there is still a problem: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button1Click(Sender: TObject);begin  Memo1.Text := 'abcd';  Memo1.SelStart := 1;  Memo1.SelLength := 1;  Memo1.SelText := 'x';  Memo1.SelText := 'x'; // should be 'axxcd', but is 'axxd'  Memo1.SetFocus;  // Memo1.SelLegth was not updated after Memo1.SelText:=something.  // Memo1.SelLegth should be zero after Memo1.SelText:=something.  writeln(Memo1.SelStart, ' ', Memo1.SelLength);end; And I have a new problem about SelText: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button1Click(Sender: TObject);begin  Memo1.SelStart := 0;  Memo1.SelLength := 0;  Memo1.SelText := 'xxx';  Memo1.SetFocus;  writeln('Button1: ', Memo1.SelStart, ' ', Memo1.SelLength);end; procedure TForm1.Memo1Change(Sender: TObject);begin  // After Click Button1, I want get Memo1.SelStart here, but the Result is wrong.  // If I type text in Memo, the Result is right.  writeln('OnChange: ', Memo1.SelStart, ' ', Memo1.SelLength);end; The new problem is also in windows system, is it a bug or a feature? If it‘s a feature, how to achieve my needs. ---------------------------------------------------------------------- tomitomy · 2017-10-24 06:42 Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button1Click(Sender: TObject);begin  Memo1.SelStart := 0;  Memo1.SelLength := 3;  Memo1.SelText := 'abc';end; This code does not mean selecting three characters, and then pasting 'abc', They trigger different events. I'm afraid that I can't go through this path to implement Undo and Redo. The demo in the attachment almost works normally, but I still have problems when I use it, and I can't find the problem. The following code has different results in Windows and Linux, when using Ctrl+A, then Ctrl+C, and then Ctrl+V. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Memo1Change(Sender: TObject);begin  Writeln(Memo1.Text);end; ---------------------------------------------------------------------- tomitomy · 2017-10-24 13:48 I registered an account in the bug report forum, And added a note about SelLength in the bug report. thank you, Handoko, you're a kind man! I can't implement the Undo and Redo functions through the OnChange event, because the OnChange event has different performance under Windows and Linux, and I couldn't get the Text and SelStart I needed in the Onchange event, and my ability is limited. I hope the official can implement TMemo's Undo and Redo functions, because it is the most basic function, without this function, the content of TMemo is like losing protection, it is very uneasy to use. Thank you for the efforts made by community members for Lazarus, and hope Lazarus is getting better and better. ---------------------------------------------------------------------- Handoko · 2017-10-24 14:32 Sorry, I'm late. Yesterday I was busy trying to upgrade my Ubuntu 17.04. I saw you already added the note at the bug report. Have you tried TSynEdit? I wrote myself a small programs to help me organize my notes. I got several problems with TMemo, one of the issue was undo/redo. So I used TSynEdit. You can think TSynEdit is a more powerful version of TMemo, the syntax-highlighting can be disabled so it will give the result as a TMemo. Maybe you can try it. http://wiki.freepascal.org/TSynEdit ---------------------------------------------------------------------- tomitomy · 2017-10-24 16:14 Quote from: Handoko on October 24, 2017, 04:32:09 pmSorry, I'm late. Yesterday I was busy trying to upgrade my Ubuntu 17.04. I saw you already added the note at the bug report. Have you tried TSynEdit? I wrote myself a small programs to help me organize my notes. I got several problems with TMemo, one of the issue was undo/redo. So I used TSynEdit. You can think TSynEdit is a more powerful version of TMemo, the syntax-highlighting can be disabled so it will give the result as a TMemo. Maybe you can try it. http://wiki.freepascal.org/TSynEdit Thank you for your help, I have tried TSynEdit, but it can‘t enter Chinese, so I didn't continue to learn it.