Capture undo and re-edit text groups in NSTextView

I am writing a plugin for an existing application and I need to capture the changed text and range affected by the undo and redo action. I can access the NSUndoManager and NSTextView that the application has created, and I can register for notifications. Is there a way to use these elements to capture a group of text that has been undone / redone?

+4
source share
1 answer

I did not do this, so I only communicate with documents / knowledge.

Since you have access to the text view, you can become a textview delegate. Then you will receive helpful messages ...

Before changing the text:

  • TextView: shouldChangeTextInRange: replacementString:
  • TextView: shouldChangeTextInRanges: replacementStrings:
  • TextView: shouldChangeTypingAttributes: toAttributes:

After changing the text:

  • textViewDidChangeTypingAttributes:

I don’t know if you will receive these changes (UndoManager bypasses this stuff?), But you can. In any case, you can request selections when processing previous messages.

Before changing a selection:

  • TextView: willChangeSelectionFromCharacterRange: toCharacterRange:
  • TextView: willChangeSelectionFromCharacterRanges: toCharacterRanges:

After:

  • textViewDidChangeSelection:

UndoManager should tell you that it is in the process of executing Undo, which means that you can distinguish between regular changes and undo-based changes.

It seems to be enough to continue, I hope this helps.

+3
source

All Articles