6

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.

Bill
  • 2,993
  • 5
  • 37
  • 71
  • look at 'Editor.UndoList.AddChange()' –  Jan 18 '12 at 21:13
  • @Dorin: I already tried AddChange without sucess. see my edit... – Bill Jan 18 '12 at 21:50
  • ouch, then I suggest looking at the way it handles the undo/redo internally, I'm pretty sure you can get somewhere from there –  Jan 19 '12 at 02:32

1 Answers1

0

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.

Vivian Mills
  • 2,552
  • 14
  • 19