undo.inc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. {
  2. "SHEdit" - Text editor with syntax highlighting
  3. Copyright (C) 1999-2000 by Sebastian Guenther ([email protected])
  4. See the file COPYING.FPC, included in this distribution,
  5. for details about the copyright.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9. }
  10. // TSHTextEdit: Undo/Redo support
  11. function TUndoInfo.Merge(AEdit: TSHTextEdit; AInfo: TUndoInfo): Boolean;
  12. begin
  13. Result := False;
  14. end;
  15. constructor TUndoEdit.Create;
  16. begin
  17. inherited Create;
  18. NumOfChars := 1;
  19. end;
  20. constructor TUndoEdit.Create(ANumOfChars: Integer);
  21. begin
  22. inherited Create;
  23. NumOfChars := ANumOfChars;
  24. end;
  25. function TUndoEdit.Merge(AEdit: TSHTextEdit; AInfo: TUndoInfo): Boolean;
  26. begin
  27. // if (CursorX <> AEdit.CursorX - TUndoEdit(AInfo).NumOfChars) or
  28. // (CursorY <> AEdit.CursorY) then exit(False);
  29. Inc(NumOfChars, TUndoEdit(AInfo).NumOfChars);
  30. if AEdit.CursorY = CursorY + 1 then begin
  31. CursorX := 0;
  32. Inc(CursorY);
  33. end else
  34. Inc(CursorX, TUndoEdit(AInfo).NumOfChars);
  35. Result := True;
  36. end;
  37. procedure TUndoEdit.DoUndo(AEdit: TSHTextEdit);
  38. begin
  39. AEdit.FCursorX := CursorX;
  40. AEdit.FCursorY := CursorY;
  41. AEdit.MultiDelLeft(NumOfChars);
  42. end;
  43. constructor TUndoDelLeft.Create(const ADeletedString: String);
  44. begin
  45. inherited Create;
  46. DeletedString := ADeletedString;
  47. end;
  48. function TUndoDelLeft.Merge(AEdit: TSHTextEdit; AInfo: TUndoInfo): Boolean;
  49. var
  50. l: Integer;
  51. begin
  52. if TUndoDelLeft(AInfo).
  53. DeletedString[Length(TUndoDelLeft(AInfo).DeletedString)] = #13 then
  54. exit(False);
  55. l := Length(TUndoDelLeft(AInfo).DeletedString);
  56. if CursorY <> AEdit.CursorY then exit(False);
  57. if CursorX = AEdit.CursorX + l then begin
  58. DeletedString := TUndoDelLeft(AInfo).DeletedString + DeletedString;
  59. Dec(CursorX, l);
  60. end else if CursorX = AEdit.CursorX then
  61. DeletedString := DeletedString + TUndoDelLeft(AInfo).DeletedString
  62. else
  63. exit(False);
  64. Result := True;
  65. end;
  66. procedure TUndoDelLeft.DoUndo(AEdit: TSHTextEdit);
  67. begin
  68. AEdit.FCursorX := CursorX;
  69. AEdit.FCursorY := CursorY;
  70. AEdit.ExecKeys(DeletedString, False);
  71. end;
  72. procedure TUndoDelRight.DoUndo(AEdit: TSHTextEdit);
  73. var
  74. OldX, OldY: Integer;
  75. begin
  76. OldX := CursorX;
  77. OldY := CursorY;
  78. AEdit.FCursorX := CursorX;
  79. AEdit.FCursorY := CursorY;
  80. AEdit.ExecKeys(DeletedString, False);
  81. AEdit.FCursorX := OldX;
  82. AEdit.FCursorY := OldY;
  83. end;
  84. procedure TSHTextEdit.AddUndoInfo(AInfo: TUndoInfo; CanMerge: Boolean);
  85. var
  86. ok: Boolean;
  87. info: TUndoInfo;
  88. begin
  89. ok := False;
  90. info := LastUndoInfo;
  91. if CanMerge and Assigned(info) and (info.ClassType = AInfo.ClassType) then begin
  92. if info.Merge(Self, AInfo) then begin
  93. AInfo.Free;
  94. AInfo := info;
  95. ok := True;
  96. end;
  97. end;
  98. if not ok then begin
  99. if LastUndoInfo = nil then
  100. LastUndoInfo := AInfo
  101. else begin
  102. AInfo.Prev := LastUndoInfo;
  103. LastUndoInfo.Next := AInfo;
  104. LastUndoInfo := AInfo;
  105. end;
  106. AInfo.CursorX := FCursorX;
  107. AInfo.CursorY := FCursorY;
  108. end;
  109. end;