Dear Cocoa Dev List,

I am attempting to add live editing to a NSTextView. This means that when the user types some specific text, the text view notices and converts it into a different substring, with possibly a different length. The idea is similar to an auto-correcting spell checker. When a new character completes a special character sequence, replaceCharactersInRange:withString: is called to make the change, and I adjust the selection range to compensate for the change in length.

I have this working almost perfectly by listening for the NSTextDidChangeNotification and searching the text for the special sequence. I've found this to be the best place to handle it, so that cut and paste operations containing the special sequence are converted properly as well. The only problem with this is that it completely screws up the Undo / Redo of the text view. It doesn't seem to be possible to use the standard shouldChangeTextInRange: and textDidChange pair, because my parser acts in response to an edit, and this would set up an infinite loop.

I found I could manually add my changes to the undoManager, and that would allow undos to function as expected, but redos won't work. ('As expected' means restoring the modified substring with the originally typed sequence minus the final character that triggered the change. Redo should restore the modified string.) I don't have much experience with the NSUndoManager, but I understand I could possibly put the reciprocal action in a NSTextView subclass for replaceCharactersInRange:, but as this is a special case, I don't see any way to tell that the particular undo operation is in response to my modification, and not something more generic.

I'm hoping someone has done something similar, and has an elegant solution, or at least a different direction I could take.

Thanks,
Arved von Brasch
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to