Synedit with word wrap? lazarus / 31-LCL ---------------------------------------------------------------------- cookie22 · 2014-10-23 14:10 I need synedit with word wrap. Does any one know about a version or someone who implemented word wrap to synedit? I don't understand why such a basic feature is missing in such a mighty component. Thanks, Cookie ---------------------------------------------------------------------- BigChimp · 2014-10-23 14:44 http://wiki.lazarus.freepascal.org/Bounties#SynEdit_word-wrap ---------------------------------------------------------------------- typo · 2014-10-23 14:46 I am trying to write a SynEdit descendant which would make SynEdit more friendly  to ordinary edit usage. But I cannot promise anything. Perhaps for now you could try this wordwrap routine, since FPC WordWrap funtion does not work fine. Code: [Select]function WrapTextUTF8(S :string; MaxCol: integer): string; var   P :PChar;   CharLen :integer;   RightSpace : Integer = 0;   N :integer = 0;   i : Integer;   j : Integer;   Len :integer = 0;   ResultLen :Integer;   LineEndingLen :Integer; begin   Result := '';   P := PChar(S);   while P^ <> #0 do   begin     CharLen := UTF8CharacterLength(P);     // Result := Result + UTF8Copy(P, 1, 1);     i := 1;     j := 0;     ResultLen := Length(Result);     while i <= CharLen do     begin       /// Result := Result + (P + j)^;       SetLength(Result, ResultLen + 1);       Result[ResultLen + 1] := (P + j)^;       ///       Inc(i);       Inc(j);       Inc(ResultLen);     end;     //     Inc(N);     if P^ = #10 then       N := 0;     if N > MaxCol then     begin       Len := Length(Result);       RightSpace := Len - RPos(' ', Result);       if RightSpace > 0 then       begin         Dec(P, RightSpace);         Dec(Len, RightSpace);         SetLength(Result, Len {- RightSpace});       end;       // Add lineending chars to Result       Result := Result + LineEnding;       {---       LineEndingLen := Length(LineEnding);       SetLength(Result, Len + LineEndingLen);       if LineEndingLen = 1 then         Result[Len + 1] := LineEnding[1]       else       begin         Result[Len + 1] := LineEnding[1];         Result[Len + 2] := LineEnding[2]       end;       ---}       N := 0;     end;     Inc(P, CharLen);   end; end;          Maybe you could even improve it. http://bugs.freepascal.org/view.php?id=26677 ---------------------------------------------------------------------- Edson · 2014-10-23 17:29 Quote from: typo on October 23, 2014, 04:46:35 pmI am trying to write a SynEdit descendant which would make SynEdit more friendly  to ordinary edit usage. But I cannot promise anything. Are you thinking on include some other features, apart from Word wrap? ---------------------------------------------------------------------- typo · 2014-10-23 21:51 Formatted text. I don't know if I could actually "implement" wordwrapping on SynEdit. I will try. For my needs probably it will be enough. ---------------------------------------------------------------------- Edson · 2014-10-24 03:34 I think Word wrap it's a lot of work on SynEdit. Specially if you want to control correctly the line number. Anyway, Good luck. ---------------------------------------------------------------------- cookie22 · 2014-10-24 06:51 That's true, but I don't get why it was removed in the port. The original version had it already implemeted.  It is realy a very basic feature. ---------------------------------------------------------------------- BigChimp · 2014-10-24 07:27 You're very welcome to write a patch to put it back in based on the original code... ---------------------------------------------------------------------- typo · 2014-10-29 20:32 I think wordwrap was implemented after the forking that has originated our SynEdit. http://sourceforge.net/p/synedit/wiki/Home/ On this SynEdit, word wrap is made by a plugin. Here is the whole SynEditWordWrap.pas file: Code: [Select]{------------------------------------------------------- ------------------------ The contents of this file are subject to the Mozilla Public License Version 1.1 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.mozilla.org/MPL/ Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the specific language governing rights and limitations under the License. The Original Code is SynEditWordWrap.pas by Flávio Etrusco, released 2003-12-11. Unicode translation by Maël Hörz. All Rights Reserved. Contributors to the SynEdit and mwEdit projects are listed in the Contributors.txt file. Alternatively, the contents of this file may be used under the terms of the GNU General Public License Version 2 or later (the "GPL"), in which case the provisions of the GPL are applicable instead of those above. If you wish to allow use of your version of this file only under the terms of the GPL and not to allow others to use your version of this file under the MPL, indicate your decision by deleting the provisions above and replace them with the notice and other provisions required by the GPL. If you do not delete the provisions above, a recipient may use your version of this file under either the MPL or the GPL. $Id: SynEditWordWrap.pas,v 1.8.2.6 2008/09/14 16:24:59 maelh Exp $ You may retrieve the latest version of this file at the SynEdit home page, located at http://SynEdit.SourceForge.net Known Issues: ---------------------------------------------------------------------- ---------} //todo: Use a single implementation of ReWrapLines that takes starting line and number of lines to rewrap //todo: Tweak code to try finding better wrapping points. Some support by the highlighters will be needed, probably. //todo: Document the code //todo: The length of the last Row of a Line could be calculated from the Line length instead of being stored. This would be only useful when most of the lines aren't wrapped. {$IFNDEF QSYNEDITWORDWRAP} unit SynEditWordWrap; {$ENDIF} {$I SynEdit.inc} interface uses {$IFDEF SYN_CLX}   QSynEditTypes,   QSynEditTextBuffer,   QSynEdit, {$ELSE}   SynEditTypes,   SynEditTextBuffer,   SynEdit, {$ENDIF}   SysUtils,   Classes; var   // Accumulate/hide whitespace at EOL (at end of wrapped rows, actually)   OldWhitespaceBehaviour: Boolean = False; const   MaxIndex = MaxInt div 16; type   TLineIndex = 0..MaxIndex;   TRowIndex = 0..MaxIndex;   TRowLength = word;   TRowIndexArray = array [TLineIndex] of TRowIndex;   PRowIndexArray = ^TRowIndexArray;   TRowLengthArray = array [TRowIndex] of TRowLength;   PRowLengthArray = ^TRowLengthArray;   {$IFNDEF SYN_COMPILER_4_UP}   TSysCharSet = set of Char;   {$ENDIF}   // For clarity, I'll refer to buffer coordinates as 'Line' and   // 'Char' and to display (wrapped) coordinates as 'Row' and 'Column'.   // fLineOffsets[n] is the index of the first row of the [n+1]th line.   // e.g. Starting row of first line (0) is 0. Starting row of second line (1)   // is fLineOffsets[0]. Clear?   TSynWordWrapPlugin = class(TInterfacedObject, ISynEditBufferPlugin)   private     fLineOffsets: PRowIndexArray;     fRowLengths: PRowLengthArray;     fLineCapacity: integer;     fRowCapacity: integer;     fLineCount: integer;     fEditor: TCustomSynEdit;     fMinRowLength: TRowLength;     fMaxRowLength: TRowLength;     procedure GrowLines(aMinSize: integer);     procedure MoveLines(aStart: TLineIndex; aMoveBy: integer);     procedure GrowRows(aMinSize: integer);     procedure MoveRows(aStart: TRowIndex; aMoveBy: integer);     procedure SetEmpty;   protected     procedure WrapLines;     function ReWrapLine(aIndex: TLineIndex): integer;     procedure TrimArrays;     property LineOffsets: PRowIndexArray read fLineOffsets;     property RowLengths: PRowLengthArray read fRowLengths;     property Editor: TCustomSynEdit read fEditor;   public     constructor Create(aOwner: TCustomSynEdit);     destructor Destroy; override;     property LineCount: integer read fLineCount;     { ISynEditBufferPlugin }     function BufferToDisplayPos(const aPos: TBufferCoord): TDisplayCoord;     function DisplayToBufferPos(const aPos: TDisplayCoord): TBufferCoord;     function RowCount: integer;     function GetRowLength(aRow: integer): integer;     function LinesInserted(aIndex: integer; aCount: integer): integer;     function LinesDeleted(aIndex: integer; aCount: integer): integer;     function LinesPutted(aIndex: integer; aCount: integer): integer;     procedure Reset;     procedure DisplayChanged;   end; implementation uses {$IFDEF SYN_CLX}   QSynUnicode, {$ELSE}   SynUnicode, {$ENDIF} {$IFDEF SYN_COMPILER_6_UP}   RTLConsts, {$ELSE}   {$IFDEF SYN_CLX}     QConsts,   {$ELSE}     Consts,   {$ENDIF} {$ENDIF} {$IFNDEF SYN_COMPILER_4_UP}   SynEditMiscProcs, {$ENDIF}   Math; { TSynWordWrapPlugin } function TSynWordWrapPlugin.BufferToDisplayPos(   const aPos: TBufferCoord): TDisplayCoord; var   vStartRow: integer; // first row of the line   cRow: integer;   vRowLen: integer; begin   Assert(aPos.Char > 0);   Assert(aPos.Line > 0);   if LineCount < aPos.Line then   begin     // beyond EOF     Result.Column := aPos.Char;     Result.Row := RowCount + (aPos.Line - LineCount);     Exit;   end;   if aPos.Line = 1 then     vStartRow := 0   else     vStartRow := fLineOffsets[aPos.Line - 2];   vRowLen := 0;   for cRow := vStartRow to fLineOffsets[aPos.Line - 1] - 1 do   begin     Inc(vRowLen, fRowLengths[cRow]);     if aPos.Char <= vRowLen then     begin       Result.Column := aPos.Char - vRowLen + fRowLengths[cRow];       Result.Row := cRow + 1;       Exit;     end;   end;   // beyond EOL   Result.Column := aPos.Char - vRowLen + fRowLengths[fLineOffsets[aPos.Line - 1] - 1];   Result.Row := fLineOffsets[aPos.Line - 1]; end; constructor TSynWordWrapPlugin.Create(aOwner: TCustomSynEdit); begin   inherited Create; // just to work as reminder in case I revert it to a TComponent...   if aOwner = nil then     raise Exception.Create( 'Owner of TSynWordWrapPlugin must be a TCustomSynEdit' );   fEditor := aOwner;   Reset; end; destructor TSynWordWrapPlugin.Destroy; begin   inherited;   FreeMem(fLineOffsets);   FreeMem(fRowLengths); end; procedure TSynWordWrapPlugin.DisplayChanged; begin   if Editor.CharsInWindow <> fMaxRowLength then     Reset; end; function TSynWordWrapPlugin.DisplayToBufferPos(   const aPos: TDisplayCoord): TBufferCoord; var   cLine: integer;   cRow: integer; begin   Assert(aPos.Column > 0);   Assert(aPos.Row > 0);   if aPos.Row > RowCount then   begin     // beyond EOF     Result.Char := aPos.Column;     Result.Line := aPos.Row - RowCount + LineCount;     Exit;   end;   //todo: use a binary search or something smarter   for cLine := LineCount - 2 downto 0 do     if aPos.Row > fLineOffsets[cLine] then     begin       Result.Line := cLine + 2;       if aPos.Row = fLineOffsets[cLine + 1] then //last row of line         Result.Char := Min(aPos.Column, fMaxRowLength + 1)       else         Result.Char := Min(aPos.Column, fRowLengths[aPos.Row - 1] + 1);       for cRow := fLineOffsets[cLine] to aPos.Row - 2 do         Inc(Result.Char, fRowLengths[cRow]);       Exit;     end;   // first line   Result.Line := 1;   if aPos.Row = fLineOffsets[0] then //last row of line     Result.Char := Min(aPos.Column, fMaxRowLength + 1)   else     Result.Char := Min(aPos.Column, fRowLengths[aPos.Row - 1] + 1);   for cRow := 0 to aPos.Row - 2 do     Inc(Result.Char, fRowLengths[cRow]); end; function TSynWordWrapPlugin.GetRowLength(aRow: integer): integer; // aRow is 1-based... begin   if (aRow <= 0) or (aRow > RowCount) then     TList.Error(SListIndexError, aRow);   Result := fRowLengths[aRow - 1]; end; procedure TSynWordWrapPlugin.GrowLines(aMinSize: integer); const   vStepSize = 256; begin   Assert(aMinSize > 0);   if aMinSize > fLineCapacity then   begin     aMinSize := aMinSize + vStepSize - (aMinSize mod vStepSize);     ReallocMem(fLineOffsets, aMinSize * SizeOf(TRowIndex));     fLineCapacity := aMinSize;   end; end; procedure TSynWordWrapPlugin.GrowRows(aMinSize: integer); const   vStepSize = 512; begin   Assert(aMinSize > 0);   if aMinSize > fRowCapacity then   begin     aMinSize := aMinSize + vStepSize - (aMinSize mod vStepSize);     ReallocMem(fRowLengths, aMinSize * SizeOf(TRowLength));     fRowCapacity := aMinSize;   end; end; function TSynWordWrapPlugin.LinesDeleted(aIndex: integer; aCount: integer): integer; var   vStartRow: integer;   vEndRow: integer;   cLine: integer; begin   if fMaxRowLength = 0 then   begin     Result := 0;     Exit;   end;   Assert(aIndex >= 0);   Assert(aCount >= 1);   Assert(aIndex + aCount <= LineCount);   if aIndex = 0 then     vStartRow := 0   else     vStartRow := fLineOffsets[aIndex - 1];   vEndRow := fLineOffsets[aIndex + aCount - 1];   Result := vEndRow - vStartRow;   // resize fRowLengths   if vEndRow < RowCount then     MoveRows(vEndRow, -Result);   // resize fLineOffsets   MoveLines(aIndex + aCount, -aCount);   Dec(fLineCount, aCount);   // update offsets   for cLine := aIndex to LineCount - 1 do     Dec(fLineOffsets[cLine], Result); end; function TSynWordWrapPlugin.LinesInserted(aIndex: integer; aCount: integer): integer; var   vPrevOffset: TRowIndex;   cLine: integer; begin   if fMaxRowLength = 0 then   begin     Result := 0;     Exit;   end;   Assert(aIndex >= 0);   Assert(aCount >= 1);   Assert(aIndex <= LineCount);   // resize fLineOffsets   GrowLines(LineCount + aCount);   if aIndex < LineCount then // no need for MoveLines if inserting at LineCount (TSynEditStringList.Add)   begin     Inc(fLineCount, aCount); // fLineCount must be updated before calling MoveLines()     MoveLines(aIndex, aCount);   end   else     Inc(fLineCount, aCount);   // set offset to same as previous line (i.e. the line has 0 rows)   if aIndex = 0 then     vPrevOffset := 0   else     vPrevOffset := fLineOffsets[aIndex - 1];   for cLine := aIndex to aIndex + aCount - 1 do     fLineOffsets[cLine] := vPrevOffset;   // Rewrap   Result := 0;   for cLine := aIndex to aIndex + aCount - 1 do     Inc(Result, ReWrapLine(cLine)); end; function TSynWordWrapPlugin.LinesPutted(aIndex: integer; aCount: integer): integer; var   cLine: integer; begin   if fMaxRowLength = 0 then   begin     Result := 0;     Exit;   end;   Assert(aIndex >= 0);   Assert(aCount >= 1);   Assert(aIndex + aCount <= LineCount);   // Rewrap   Result := 0;   for cLine := aIndex to aIndex + aCount - 1 do     Inc(Result, ReWrapLine(cLine)); end; procedure TSynWordWrapPlugin.MoveLines(aStart: TLineIndex; aMoveBy: integer); var   vMoveCount: integer; begin   Assert(aMoveBy <> 0);   Assert(aStart + aMoveBy >= 0);   Assert(aStart + aMoveBy < LineCount);   vMoveCount := LineCount - aStart;   if aMoveBy > 0 then     Dec(vMoveCount, aMoveBy);   Move(fLineOffsets[aStart], fLineOffsets[aStart + aMoveBy],     vMoveCount * SizeOf(TRowIndex)); end; procedure TSynWordWrapPlugin.MoveRows(aStart: TRowIndex; aMoveBy: integer); var   vMoveCount: integer; begin   Assert(aMoveBy <> 0);   Assert(aStart + aMoveBy >= 0);   Assert(aStart + aMoveBy < RowCount);   vMoveCount := RowCount - aStart;   if aMoveBy > 0 then     Dec(vMoveCount, aMoveBy);   Move(fRowLengths[aStart], fRowLengths[aStart + aMoveBy],     vMoveCount * SizeOf(TRowLength)); end; procedure TSynWordWrapPlugin.Reset; begin   Assert(Editor.CharsInWindow >= 0);   fMaxRowLength := Editor.CharsInWindow;   fMinRowLength := Editor.CharsInWindow - (Editor.CharsInWindow div 3);   if fMinRowLength <= 0 then     fMinRowLength := 1;   WrapLines; end; function TSynWordWrapPlugin.ReWrapLine(aIndex: TLineIndex): integer; // Returns RowCount delta (how many wrapped lines were added or removed by this change). var   vMaxNewRows: Cardinal;   vLine: UnicodeString;   vLineRowCount: Integer; //numbers of rows parsed in this line   vTempRowLengths: PRowLengthArray;   vRowBegin: PWideChar;   vLineEnd: PWideChar;   vRowEnd: PWideChar;   vRunner: PWideChar;   vRowMinEnd: PWideChar;   vLastVisibleChar: PWideChar;   vStartRow: Integer; // first row of the line   vOldNextRow: Integer; // first row of the next line, before the change   cLine: Integer;   p : PRowIndexArray; begin   // ****** First parse the new string using an auxiliar array *****   vLine := TSynEditStringList(Editor.Lines).ExpandedStrings[aIndex];   vLine := Editor.ExpandAtWideGlyphs(vLine);   // Pre-allocate a buffer for rowlengths   vMaxNewRows := ((Length(vLine) - 1) div fMinRowLength) + 1;   vTempRowLengths := AllocMem(vMaxNewRows * SizeOf(TRowLength));   try     vLineRowCount := 0;     vRowBegin := PWideChar(vLine);     vRowEnd := vRowBegin + fMaxRowLength;     vLineEnd := vRowBegin + Length(vLine);     while vRowEnd < vLineEnd do     begin       if OldWhitespaceBehaviour and CharInSet(vRowEnd^, [#32, #9]) then       begin         repeat           Inc(vRowEnd);         until not CharInSet(vRowEnd^, [#32, #9]);       end       else       begin         vRowMinEnd := vRowBegin + fMinRowLength;         vRunner := vRowEnd;         while vRunner > vRowMinEnd do         begin           if Editor.IsWordBreakChar(vRunner^) then           begin             vRowEnd := vRunner;             break;           end;           Dec(vRunner);         end;       end;       // Check TRowLength overflow       if OldWhitespaceBehaviour and (vRowEnd - vRowBegin > High(TRowLength)) then       begin         vRowEnd := vRowBegin + High(TRowLength);         vRowMinEnd := vRowEnd - (High(TRowLength) mod Editor.TabWidth);         while (vRowEnd^ = #9) and (vRowEnd > vRowMinEnd) do           Dec(vRowEnd);       end;       // do not cut wide glyphs in half       if vRowEnd > vRowBegin then       begin         vLastVisibleChar := vRowEnd - 1;         while (vLastVisibleChar^ = FillerChar) and (vLastVisibleChar > vRowBegin) do           dec(vLastVisibleChar);         vRowEnd := vLastVisibleChar + 1;       end;       // Finally store the rowlength       vTempRowLengths[vLineRowCount] := vRowEnd - vRowBegin;       Inc(vLineRowCount);       vRowBegin := vRowEnd;       Inc(vRowEnd, fMaxRowLength);     end; //endwhile vRowEnd < vLineEnd     if (vLineEnd > vRowBegin) or (Length(vLine) = 0) then     begin       vTempRowLengths[vLineRowCount] := vLineEnd - vRowBegin;       Inc(vLineRowCount);     end;     // ****** Then updates the main arrays ******     if aIndex = 0 then       vStartRow := 0     else       vStartRow := fLineOffsets[aIndex - 1];     vOldNextRow := fLineOffsets[aIndex];     Result := vLineRowCount - (vOldNextRow - vStartRow);     if Result <> 0 then     begin       // MoveRows depends on RowCount, so we need some special processing...       if Result > 0 then       begin         // ...if growing, update offsets (and thus RowCount) before rowlengths         GrowRows(RowCount + Result);         if Result = 1 then begin           // EG: this makes Schlemiel run twice as fast, but doesn't solve           // the algorithmic issue if someone can spend some time looking           // at the big picture... there are huge speedups to be made by           // eliminating this loop           p:=fLineOffsets;           for cLine := aIndex to LineCount - 1 do              Inc(p[cLine])         end else begin           p:=fLineOffsets;           for cLine := aIndex to LineCount - 1 do             Inc(p[cLine], Result);         end;         if vOldNextRow < RowCount - Result then           MoveRows(vOldNextRow, Result);       end       else       begin         // ...if shrinking, update offsets after rowlengths         if vOldNextRow < RowCount then           MoveRows(vOldNextRow, Result);         for cLine := aIndex to LineCount - 1 do           Inc(fLineOffsets[cLine], Result);       end;     end;     Move(vTempRowLengths[0], fRowLengths[vStartRow], vLineRowCount * SizeOf(TRowLength));   finally     FreeMem(vTempRowLengths);   end; end; procedure TSynWordWrapPlugin.WrapLines; var   cRow: Integer;   cLine: Integer;   vLine: UnicodeString;   vMaxNewRows: Integer;   vRowBegin: PWideChar;   vLineEnd: PWideChar;   vRowEnd: PWideChar;   vRunner: PWideChar;   vRowMinEnd: PWideChar;   vLastVisibleChar: PWideChar; begin   if (Editor.Lines.Count = 0) or (fMaxRowLength <= 0) then   begin     SetEmpty;     Exit;   end;   GrowLines(Editor.Lines.Count);   GrowRows(Editor.Lines.Count);   cRow := 0;   for cLine := 0 to Editor.Lines.Count - 1 do   begin     vLine := TSynEditStringList(Editor.Lines).ExpandedStrings[cLine];     vLine := Editor.ExpandAtWideGlyphs(vLine);     vMaxNewRows := ((Length(vLine) - 1) div fMinRowLength) + 1;     GrowRows(cRow + vMaxNewRows);     vRowBegin := PWideChar(vLine);     vRowEnd := vRowBegin + fMaxRowLength;     vLineEnd := vRowBegin + Length(vLine);     while vRowEnd < vLineEnd do     begin       if OldWhitespaceBehaviour and CharInSet(vRowEnd^, [#32, #9]) then       begin         repeat           Inc(vRowEnd);         until not CharInSet(vRowEnd^, [#32, #9]);       end       else       begin         vRowMinEnd := vRowBegin + fMinRowLength;         vRunner := vRowEnd;         while vRunner > vRowMinEnd do         begin           if Editor.IsWordBreakChar(vRunner^) then           begin             vRowEnd := vRunner;             break;           end;           Dec(vRunner);         end;       end;       if OldWhitespaceBehaviour and (vRowEnd - vRowBegin > High(TRowLength)) then       begin         vRowEnd := vRowBegin + High(TRowLength);         vRowMinEnd := vRowEnd - (High(TRowLength) mod Editor.TabWidth);         while (vRowEnd^ = #9) and (vRowEnd > vRowMinEnd) do           Dec(vRowEnd);       end;       // do not cut wide glyphs in half       if vRowEnd > vRowBegin then       begin         vLastVisibleChar := vRowEnd - 1;         while (vLastVisibleChar^ = FillerChar) and (vLastVisibleChar > vRowBegin) do           dec(vLastVisibleChar);         vRowEnd := vLastVisibleChar + 1;       end;       fRowLengths[cRow] := vRowEnd - vRowBegin;       Inc(cRow);       vRowBegin := vRowEnd;       Inc(vRowEnd, fMaxRowLength);     end;     if (vLineEnd > vRowBegin) or (Length(vLine) = 0) then     begin       fRowLengths[cRow] := vLineEnd - vRowBegin;       Inc(cRow);     end;     fLineOffsets[cLine] := cRow;   end;   fLineCount := Editor.Lines.Count; end; function TSynWordWrapPlugin.RowCount: integer; begin   if LineCount > 0 then     Result := fLineOffsets[LineCount - 1]   else     Result := 0; end; procedure TSynWordWrapPlugin.SetEmpty; begin   fLineCount := 0;   // free unsused memory   TrimArrays; end; procedure TSynWordWrapPlugin.TrimArrays; begin   ReallocMem(fLineOffsets, LineCount * SizeOf(TRowIndex));   fLineCapacity := LineCount;   ReallocMem(fRowLengths, RowCount * SizeOf(TRowLength));   fRowCapacity := RowCount; end; end. ---------------------------------------------------------------------- cookie22 · 2014-10-31 21:52 The problem is, you can't just copy it because the gutter works different in both forks. ---------------------------------------------------------------------- typo · 2014-10-31 22:05 Yes, of course, and not only the Gutter. Indeed this component is not helping me so much, since it seems to deliver results to SynEdit itself, which is different from ours. With our SynEdit probably I will need to handle the text by myself. If I could simply deliver an array of text information like this component seems to do, the things would be easier. ---------------------------------------------------------------------- Martin_fr · 2014-10-31 23:03 If you refer to the one you attached in http://forum.lazarus.freepascal.org/index.php/topic,26320.0 On a very surface inspection: It stores a mapping of real line, and display line in fLineOffsets (e.g if line 1 wraps, then line 2 becomes display line 3 ...) That is how folding works (just reverse) The fold module does that. There is a concept of "views" wrapping around the text buffer, based on TSynEditStrings. Only folding is not fully based on that yet, but it is meant to someday be. So there is a place to hook. (unfinished...) --- Problem: fLineOffsets: array ... works for small text. But if you have 100,000 lines or more then it becomes *very CPU intensive. Because if you jump to a bookmark at line 50000 you need to sum up all the lines above (required to set the scrollbar right) So you need a better storage. Some kind of index. A tree with pages of line mappings. -------------- And yes, all that "view" stuff, wass added after the fork. So the old edit does not have it. ---------------------------------------------------------------------- typo · 2014-11-01 10:16 AFAICS, text processors handle only visible lines or paragraphs, letting lines/pages count calculation to a different thread. It is not so fast, anyway. Text processors pages are not so easily resizable and word wrap process can be "hiden" from the user in a thread. ---------------------------------------------------------------------- Martin_fr · 2014-11-01 11:51 Yes, wordwrap (re-wrap) for none visible lines, are bes done in a thread (or in idle) But that is not what I wrote about: In order to set the scrollbar (which must correspondent to the amount of visible lines (lines after wrapping) you must often calculate between real and visible numbers. In the array, you store info per line. that is if a line wraps once, you store "2" (meaning: this line takes 2 display lines). Then to get the line mapping, for line 50000 you must sum-up 50000 entries. If you store the final map (that is for line 5 you store 10, because all lines wrap once, so this is the 10th visible line) then it gets worse. Edit the 1st line of text, so it rewraps, and you  have to update all entries. Even in a thread it uses up your laptops battery.... ---------------------------------------------------------------------- typo · 2014-11-01 13:01 Could not one re-wrap only until the end of the paragraph? And store paragraph information instead of lines? Paragraphs are not as often modified in number of lines when edited. And resizing could be made not so easy. I see that lines in SynEdit are paragraphs and why store information relativelly to the first line? Reset procedure in this component should be used only when resized, not for every edition one does. So why not to limit resizability? ---------------------------------------------------------------------- Martin_fr · 2014-11-02 12:26 Not sure what you mean. But if you delay, then during this delay the display is incorrect. Example: For the vertical scrollbar, you need: - the total of visible lines (aka after wrapping) - the amount of visible lines before the first line on screen. Doing the plain array is a start. On a modern, decent fast computer it will likely work (unless you have millions of lines). And if you are not on a laptop, you can keep the CPU as busy as you like. ------------- Here is another bit of SynEdit that needs to be improved. Create a text(copy and paste) with 100,000 lines (average 20 to 60 char long. 1) You may notice, that pasting 20000 lines, takes a moment 2) Select all 100000 lines, and indent them (tab if configured, or ctrl-i). That takes a moment too. Especially on the 1.2 fixes branch or earlier. Trunk was improved. ---------------------------------------------------------------------- Edson · 2014-11-02 17:06 I think that @typo refers to treat the lines as paragraphs and storage just the number of lines in every paragraph, not the beginning line. So it isn't not necessary to update all the paragraphs, every time a line is inserted. Anyway, it still will be necessary to calculate the total lines of the text in some time, but it won't be so heavy as it's just accumulate a counter. The critical situation is in horizontal-resizing. It would force to process all the paragraphs. Of course, a word-wrap editor use more CPU. That's a reason it wouldn't be activated   if it's not necessary. ---------------------------------------------------------------------- Martin_fr · 2014-11-02 17:44 Quote from: Edson on November 02, 2014, 06:06:16 pmI think that @typo refers to treat the lines as paragraphs and storage just the number of lines in every paragraph, not the beginning line. So it isn't not necessary to update all the paragraphs, every time a line is inserted. Anyway, it still will be necessary to calculate the total lines of the text in some time, but it won't be so heavy as it's just accumulate a counter. The total amount of lines is needed for the scrollbar, so it is needed all the time. Yet you can keep a separate counter, that is fine. The above works well, except if scrolling, or "jump to line", or bookmark. If you jump to a bookmark at real line 50000 you need to sum up all the lines before. (or you can not show the scrollbar correctly) Of course 50,000 is not an issue for a modern PC. Then make it 500,000 lines.... (FPC used to have a unit with 300,000 lines, so it is not a theoretical issue) With normal scrolling, you can avoid recalculating, by calculating the offset to the last known line. But that means you need to add optimizations like that. Because every time you scroll one line, you need to set the scrollbar. Avoiding this would mean to change lots of synedit code ---------------------------------------------------------------------- Edson · 2014-11-02 23:05 Quote from: Martin_fr on November 02, 2014, 06:44:04 pmThe total amount of lines is needed for the scrollbar, so it is needed all the time. Yet you can keep a separate counter, that is fine. You can easy calculate it exactly, if you have just a few thousands of lines. If there are 50000 lines or more, I would use an aproximate value, using just the paragraphs index for have the scrollbar adjusted. It that case, it could be calculated exactly, just in idle CPU time. Quote from: Martin_fr on November 02, 2014, 06:44:04 pmThe above works well, except if scrolling, or "jump to line", or bookmark. If you jump to a bookmark at real line 50000 you need to sum up all the lines before. (or you can not show the scrollbar correctly) Bookmarks (and "jump to line") must be referred to paragraphs, not to physical lines. Then, it would be easy to jump to any "line". Even when resizing the control, we could use just an approximate value (using paragraphs, if there are too many lines) for to show the scrollbar. It would be very fast, even breaking the visible lines, on real-time. ---------------------------------------------------------------------- Martin_fr · 2014-11-02 23:24 On resize, yes it would adjust over time. So apporximate to start with. Code: [Select]Bookmarks (and "jump to line") must be referred to paragraphs, not to physical lines. Then, it would be easy to jump to any "line". You mean the visible line? then you must calculate the real line from it, or else you can not fetch the text. ---------------------------------------------------------------------- Edson · 2014-11-02 23:51 I'm assuming that on a hypothetical word-wrap mode, we show on gutter just a line number for all the paragraphs (like Notepad++ do). And we maintain the same storage for lines, using a TStrings list like SynEdit currently do. The difference would be now, that "one line" (one entry of Lines[] ), can be visualized on more than one visible "lines". That means that we really storage "paragraphs". Then when we set a Bookmark, it must save something like Line=5000, Column=200. So, when jumping, we jump quickly to paragraph 5000 (in fact Lines[5000]), and then we must find the real "visible line" that match the column 200 (just breaking the paragraph 5000) [1]. What I propose is just using an approximate value of lines, based on paragraphs, for the scrollbar if we manage many lines, leaving the exactly calculation for a thread or for idle time. It would avoid to break all the lines for the real calculation of line number. After all, it's only necessary for the scrollbar. [1], we could need to break some previous or next paragraphs, that we need for to show an entire screen. ---------------------------------------------------------------------- Martin_fr · 2014-11-03 01:47 Ok, I see what you mean. Would probably work. Just not the way I would go. Also: While approximation is almost unavoidable during resize of window, it should be avaided whenever possible. When approximation is used, and the user grabs the scrollbar slider with the mouse, then it will behave unexpected.... In case of a bookmark it can be avoided. ---------------------------------------------------------------------- typo · 2014-11-03 12:39 If SynEdit could store non-wrapped text and draw it wrapped or justified internally (and also manage the caret around larger spaces which the text would have), external component could calculate the words and lines positons like TSynWordWrap does, instead of writing an already written text and fighting against the caret like it would need to do now. AFAICS, the text in SynEdit is actually written in TheTextDrawer TextOut, ExtTextOut and NewTextOut methods from SynTextDrawer unit. ---------------------------------------------------------------------- typo · 2014-11-03 19:32 It could work similarly to a highlighter, with SynEdit asking it on where to write the words/characters. ---------------------------------------------------------------------- Martin_fr · 2014-11-03 20:11 Look at he fold module. IT's the same logic. ---------------------------------------------------------------------- typo · 2014-11-03 22:45 The thread which counts lines could increase a counter progressively, at each step while running, avoiding that SynEdit be waiting for the final result. At each step it has a parcial result, which could be used to set scrollbar as properly as possible. But if after all one enables the user to resize horizontally the control at any time with the mouse, the work is lost. WordPad itself does not allow that. Anyway, it could be possible if the resize action interrupts the previous resetting and restart it with a new value. ---------------------------------------------------------------------- Martin_fr · 2014-11-03 23:31 I am starting to get deja vue, and also am not sure what we try to archive with the discussion from this point on. There is more than one solution, they differ in cpu usage, battery life time, and how long which actions will take. But we went through all that. I pointed out which approach I would take (if I would be doing it). But if you offer a different solution, then I wont stop you. As for how to integrate it with SynEdit., I pointed to the "views". That is the place to go. All you said can be done there. At least for line mapping. For caret X mapping some extra work will be needed. ---------------------------------------------------------------------- typo · 2014-11-03 23:45 Do not worry, I am not contesting you. My statements have more the character of a question than an answer. You have already answer what I wanted to know and I am satisfied. Thank you. ---------------------------------------------------------------------- Martin_fr · 2014-11-04 00:21 Never thought as it as a contest, but open questioning. Just thought it started to repeat questions. 1) Determining where to wrap a line, is easy (should be) 2) storing, is important, as it affects speed, but in a good design, can be changed later 3) mapping for synedit Code: [Select]  TLazSynDisplayView = class   private     FNextView: TLazSynDisplayView;   public     constructor Create;     property NextView: TLazSynDisplayView read FNextView write FNextView;   public     procedure InitHighlighterTokens(AHighlighter: TSynCustomHighlighter); virtual;     procedure SetHighlighterTokensLine(ALine: TLineIdx; out ARealLine: TLineIdx); virtual;     procedure FinishHighlighterTokens; virtual;     function  GetNextHighlighterToken(out ATokenInfo: TLazSynDisplayTokenInfo): Boolean; virtual;     function GetLinesCount: Integer; virtual;     function GetDrawDividerInfo: TSynDividerDrawConfigSetting; virtual;     function TextToViewIndex(AIndex: TLineIdx): TLineRange; virtual;     function ViewToTextIndex(AIndex: TLineIdx): TLineIdx; virtual;     //function ViewToTextIndexEx(AIndex: TLineIdx; out AScreenRange: TLineRange): TLineIdx;     // todo: gutter info   end; /////////////   TLazSynDisplayViewEx = class(TLazSynDisplayView) ///////////   TLazSynDisplayFold = class(TLazSynDisplayViewEx) This is used by the views, to provide lines for painting. It is placed between the text buffer and the painter. "SetHighlighterTokensLine" takes input in visible lines. May need extensions for the gutter. ---------------------------------------------------------------------- typo · 2014-11-04 20:45 I don't intend to contest you, I want only to understand which modifications you could want for SynEdit and in what direction. Probably SynEdit needs none modifications to remain the IDE's edit window. But it needs a few more modifications to be an edit control available for the community. Some developers use SynEdit for text editors, maybe because they can see what happens inside it and will not have any suprise about its behavior. They need an edit control for ordinary text edition, with a caret, selection, etc. Maybe SynEdit can be this control. ---------------------------------------------------------------------- Martin_fr · 2014-11-04 21:40 If you questions was, what features I think are missing. / If not ask again: http://wiki.freepascal.org/User:Martin#SynEdit List is not exclusive, ideas welcome. Currently not going to happen: Multi-font. Currently not going to happen: see through background, eg.g for image in background. (unless TWincontrol gets that.) At the moment I am stretched for time.... But when I get back to it. As for the code design. I have been trying to make things more modular. But that is still in progress. ---------------------------------------------------------------------- typo · 2014-11-04 22:09 What do you mean by "currently not going to happen"? This means that you are "against" these? Well, I can see WordWrap on your list. Maybe center paragraph, align at left and right and justified could be added. Maybe multi-font too. ---------------------------------------------------------------------- Martin_fr · 2014-11-04 23:31 "Currently not ..." Different font (sizes) would be a fundamental change to SynEdit. SynEdit is meant as an editor, not word processor. It is optimized for this. Adding those feature is likely (not necessarily, but likely) having huge performance impact. In that case it may be that the decision goes for 2 branches, keeping the editor, and creating a text processor. But I may be wrong, that is to be seen if an actual implementation surfaces. left right center.... Without line wrapping, I only see a point to have the entire text left or right. That would also be good for RTL text) With wrap, the wrapper can influence how the text is shown. ---------------------------------------------------------------------- typo · 2014-11-05 12:42 The open source RichEdit does not exist yet? ---------------------------------------------------------------------- BigChimp · 2014-11-05 12:50 AFAIK not one that is cross platform and suitable for inclusion into Lazarus. There are - IIRC - various richedit controls available that do work with Lazarus. Edit: see e.g. http://wiki.lazarus.freepascal.org/RichMemo ---------------------------------------------------------------------- typo · 2014-11-05 14:30 Yes, RichMemo seems to be good. I use it. I use SynEdit only for an algorithmic editor, indeed.