Extracting whole string(paragraph) from TMemo and TRichmemo lazarus / 31-LCL ---------------------------------------------------------------------- egsuh · 2019-01-13 01:05 Hi, I tried this on TRichMemo, but I think TMemo will operate in the same way. When a text string is splitted into two lines in TMemo as the string length is longer than the width of TMemo, it is counted as two (memo1.lines.count). Let's assume that a string is shown as follows in TMemo. This is an example. Then if I execute   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 showmessage(memo1.lines[memo1.lines.count-1]); This will show 'example'. But when I expand the memo1 width (by setting full window, etc.), then the lines are shown as a single line, and treated as such. What I want to know is whether there are any way that I can extract the full string from TMemo. For example, a whole string within which the cursor is positioned. Regards, ---------------------------------------------------------------------- lucamar · 2019-01-13 02:14 I tried with an actual TMemo and can't reproduce. See the attached images: the first one is the memo in full-width, the second is the memo resized (to make it word-wrap) and a call made to:   ShowDialog(Memo.Lines[Memo.Lines.Count-1]); I'm using vanilla Lazarus 1.8.4 - i386 Linux GTK2. ---------------------------------------------------------------------- jamie · 2019-01-13 16:28 The Tmemo is not a very friendly edit box, it steams from the old days and is very limited... The line numbers are those of what will fit in a memo box views width with out scrollbars. what you are trying to do involves a little more work.. P :TPoint; P := Memo1.ScreenTOClient(Mouse.CursorPos); SomeInteger := SendMessage(Memo1.Handle, EM_CHARFROMPOOS,0,MakeLong(P.X,P.Y)); SomeInteger now reports the position within the text buffer the location of the spot you clicked on for example from this, you need to traverse left to right until you find the start of a string and the end of the string which are separated by CR and 0 (nul). There could already be a method in the MEMO for this...  If you don't understand that I can maybe I can whip up an example  Seems like I've done this before but I can't fine the code for it.. Hmm ---------------------------------------------------------------------- lucamar · 2019-01-13 16:34 @jamie: Did you read my post? You don't need so much complexity: TMemo.Lines already has the full lines, whether the view is word- wraped or not. As for the friendliness of TMemo and its limits .. well, it's all a question of what yo intend to use it for. For small, agile simple-text editors? Almost ideal. One such editor in the attached image. ---------------------------------------------------------------------- jamie · 2019-01-13 17:50 I ran a test with Delphi and I can only assume Laz should be the same.. If you Compress the Client window so that line wrapping occurs the end of line 1 ends up being line2 Using the Lines Property will actually generate the end of line one if you reference line 2.. Have you tried indexing the lines by compressing the client window with no scrollbars? I'll need to test this in Lazarus instead of Delphi, maybe something changed but if that is the case then there is a lot of software that will break when doing the transition. yes, verified.. if you have a first line and it wraps around to the next line in the client (View port). the wrapped around portion becomes Line[1]. Line[0] is the first line but if it gets wrapped out due to no scrollbars the Line[1] becomes the tail end of line[0] You need to force a line wrap to see this effect. if this is behaving differently in other widgets then its not compatible in behavior... Now do you understand what I am talking about ? ---------------------------------------------------------------------- jamie · 2019-01-13 19:45 Here is a sample test that will correctly extract the line when lines are wrapping around.. This test uses the Mouse click as a location to play with.. This may be needed only windows since other widgets may not be suffering from this but this has been around since I can remember for windows. 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.Memo1Click(Sender: TObject);var  P: Tpoint;  Index, StartIndex, EndIndex: longint;  S: string;begin  with Sender as Tmemo do  begin  P := ScreenToClient(Mouse.CursorPos);//Locate Cursor.    Index := (SendMessage(handle, EM_CharFromPos, 0, MakeLong(P.X, P.Y)) and $FFFF) + 1;    //Use only the low part.    if Index > Length(Text) then      Exit;    if (Text[Index] < #14) and (Index > 1) then      Dec(Index);//Make sure we are not on a end marker.    StartIndex := Index;    while (StartIndex > 1) and (Text[StartIndex] > #13) do      Dec(StartIndex);    if Text[StartIndex] <= #13 then      Inc(StartIndex);    EndIndex := Index;    while (EndInDex < Length(Text)) and (Text[EndIndex] > #13) and      (EndIndex < Length(Text)) do      Inc(EndIndex);    Setlength(S, EndIndex - StartIndex);    Index := 1;    while StartIndex <= EndIndex do    begin      S[Index] := Text[StartIndex];      Inc(Index);      Inc(StartIndex);    end;  end;  Caption := S;end;    Drop a memo on the form, give it some lines and make sure they wrap around in the display. click in the memo area and take note of the forms Caption. ---------------------------------------------------------------------- lucamar · 2019-01-13 20:35 Quote from: jamie on January 13, 2019, 06:50:07 pmIf you Compress the Client window so that line wrapping occurs the end of line 1 ends up being line2 [...] Have you tried indexing the lines by compressing the client window with no scrollbars? [...] You need to force a line wrap to see this effect. if this is behaving differently in other widgets then its not compatible in behavior... Now do you understand what I am talking about ? I understood from the first. What I'm telling you, as shown in my first post and attached images, is that it doesn't behave that way--at least not in Linux-i386-GTK2 and, AFAIR, neither have I seen this behaviour in Windows (XP). But I can't test ATM on the Windows machines so maybe it's widgetset-specific behaviour. ---------------------------------------------------------------------- egsuh · 2019-01-14 05:57 @jamie, Thank you for your advice and code. I have given some thought to this issue, and found out that the thing to notice is that Memo.Text <> Memo.Lines.Text. So your codes try to get the position of cursor within Memo.Text, and extract the whole string from it. As I do not want to use SendMessage, etc. (just because I do not have enough knowledge on them), I rewrote the procedure as follows. The SelStart seems to return the position of Cursor if no text is selected. The point is finding out which line the "SelStart" is located at within Memo.Text.  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.Button12Click(Sender: TObject);var   i, Index, N : Integer;   MemoContent : TStringList;begin   Index := memo1.SelStart;   if Index > Length(memo1.Text) then Exit;    N := 0;   for i := 0 to index do if memo1.Text[i] = #13 then N += 1;    MemoContent := TStringList.Create;   try     MemoContent.Text := Memo1.Lines.Text;   Showmessage(MemoContent[N]);    finally     MemoContent.Free;   end;end;                 This works fine as long as I use single byte characters. Now I'd like to use Korean characters.  It looks like.. 가나다 I'm not sure how this will be seen on  your Windows (or Linix).  This is three-character string so SelStart positions would be 0, 1, or 2. But function Length('가나다') returns 9. Is there any way that I can find the positions like, 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   for position := 0 to maxposition (memo1.text) do     if memo1.text[position] = #13 then N+=1; Hope that you understand what I mean. BTW, what is the consistent character to line break? #10 or #13? ---------------------------------------------------------------------- egsuh · 2019-01-14 07:54 My final solution is following codes: 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.Button12Click(Sender: TObject);var   i, Index, N : Integer;   MemoContent : TStringList;   s: string;begin   Index := memo1.SelStart;   memo1.SelStart := 0;   memo1.SelLength := Index;   s := memo1.SelText;    N := 0;   for i := 0 to Length(s)-1 do if s[i] = #13 then N += 1;    MemoContent := TStringList.Create;   try      MemoContent.Text := Memo1.Lines.Text;      if N < MemoContent.Count then Showmessage(MemoContent[N]);   finally      MemoContent.Free;   end;end; ---------------------------------------------------------------------- jamie · 2019-01-14 23:17 yes that will work too, since TEXT is one long contiguous string with CR between lines.. Happy camping.