How Do You Update SynMemo Undo/RedoList When Deleting and Inserting a Line

1.1k views Asked by At

If you delete a line and then insert a new line into TSynMemo how do you update the UndoList and RedoList so that the change can be undone with SynMemo.Undo?

SynMemo1.BeginUpdate;
iLineIndex := SynMemo1.Lines.IndexOf( SynMemo1.LineText );
SynMemo1.Lines.Delete( iLineIndex );
iStartTag := SourceStyleComboBox1.CurText;
iEndTag := SourceStyleComboBox1.CurText;
System.Insert( '/', iEndTag, 2 );
iHTML := iStartTag + iElement + iEndTag;  
SynMemo1.Lines.Insert( iLineIndex, iHTML );
SynMemo1.EndUpdate;

EDIT I tried this but undo and redo does not work correctly... what I mean by this is after an undo the line is not restored to what it was before the actions.

StartOfBlock.Line := SynMemo1.CaretY;
StartOfBlock.Char := 0;
EndOfBlock.Line := SynMemo1.CaretY;
EndOfBlock.Char := Length( iHTML );
SynMemo1.UndoList.BeginBlock;
SynMemo1.UndoList.AddChange(crInsert, StartOfBlock, EndOfBlock, iHTML, smNormal);
SynMemo1.UndoList.EndBlock;
SynMemo1.RedoList.BeginBlock;
SynMemo1.RedoList.AddChange(crInsert, StartOfBlock, EndOfBlock, iHTML, smNormal);
SynMemo1.RedoList.EndBlock;

I can not find any guidance about setting the StartOfBlock and EdifOfBlock parameters. The two "actions" should be combined so that there is only one undo and redo for the "combined" action - "Insert and Delete" with option eoGroupUndo = True.

1

There are 1 answers

0
Vivian Mills On

While I haven't used TSynMemo, I do use TSynEdit, I think that the processing would be similar.

This is how I do BlockUndo updates:

ActiveEditor.SynEditor.BeginUpdate;
try
  //This tells SynEdit to mark all upcoming changes as a single block
  ActiveEditor.SynEditor.BeginUndoBlock;  
  try

    {Any change made here is recorded for undo purposes}
    {Buffer changes (Adding/Editing/Deletion of lines),  caret pos changes, etc}

  finally
    //This completes the undo block.
    ActiveEditor.SynEditor.EndUndoBlock;
  end;
finally
  ActiveEditor.SynEditor.EndUpdate;
end;

I believe the BeginUndoBlock/EndUndoBlock functionality resides on TSynEdit, but because TSynMemo actually descends from TSynEdit this should still work.