working to editor of Turbo C/C++

soodsandeep
soodsandeep used Ask the Experts™
on
hello,
please, i want to little about working of TC/TCC editor of C++.
to undo we can use Alt+Backspace, and it carries cursor back to previous position by advancing one position at a time and  brings back whatever character we removed.  i can undo any number of changes made since i opened the file.

How does it happen.
i mean internally, what does the editor do for this.  what data structure does it use for this ?


Thanks.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2009

Commented:
I don't know how THEY did it, but the idea is to find a representation for all types of modifications you want to be able to undo (with enough information to actually be able to undo it and re-do it when needed).
Once you have found an appropriate representation for an undo-able action, you can then push it onto an "undo list" each time the user performs one of those actions. When the user requests undo-ing an action, you can walk back through that list for as long as the user wants, and undo the actions recorded in the "undo list". When the user wants to re-do a previously un-done action, you simple walk forward again through the list, and re-do the actions.
Top Expert 2009

Commented:
So, say the user performs 5 consecutive actions, then the undo list will have become something like :

        action1 -> action2 -> action3 -> action4 -> action5
                                                                                ^

with the current pointer pointing to the last action.

When the user then requests to undo the last action, you call the undo method on the item in the undo list currently pointed to, so in this case action5.undo(), and the current pointer moves one item back :

        action1 -> action2 -> action3 -> action4 -> action5
                                                               ^

Notice that action5 is not removed from the list. It is still there.

Imagine now that the user requests to undo two more actions. You'd do action4.undo() and action3.undo() and end up with this undo list :

        action1 -> action2 -> action3 -> action4 -> action5
                             ^

If the user then requests to re-do the previously undone action, then the current pointer moves forward again to action3, and that action is re-done using action3.redo(), and you end up with :

        action1 -> action2 -> action3 -> action4 -> action5
                                               ^

Now, suppose the user continues performing new actions. What happens now, is that the part of the undo list after the current item is removed :

        action1 -> action2 -> action3
                                               ^

and the new user actions are added to the undo list as he performs them :

        action1 -> action2 -> action3 -> action6
                                                               ^

etc.
Just as addition - here you can find a very simple project implementing undo/redo which can be used as a base for own implementations: http://www.codeproject.com/KB/cpp/undoredo_cpp.aspx

ZOPPO
OWASP: Threats Fundamentals

Learn the top ten threats that are present in modern web-application development and how to protect your business from them.

Author

Commented:
thanks infinity08 for your reply.
but it was very detailed but very basic / fundamental explanation.

Thanks Zoppo for your reply. i wil try the link you have sent.

thanks.
Top Expert 2009

Commented:
>> but it was very detailed but very basic / fundamental explanation.

That's what your question was, right ? To explain how a program is able to have an undo-redo feature.

If anything is unclear, or you need more information about something, don't hesitate to ask.
There are two possible approaches for realising undo-redo functionality. The first is as Infinity told to have the reverse action for each action, e. g. for removing a part of text there is inserting the removed text as reverse action and so on. Another approach is similar to that of versioning systems. An undo or redo was made by subtracting or adding the saved *delta* between the two states. That approach only operates on data structures and doesn't care for actions. It has the advantage that it easily could reverse actions made by complex macros which theirselves combine a lot of actions. The disadvantage is that the delta between states may exceed a full save of data and that deltas between dynamically sized structures could not easily be stored efficiently.

I don't know of Turbo C/C++. But in MS Visual Studio they probably have changed from first method to the second one with Version 7. Before, undoing an update made by a macro reversed each single step made in the macro. Later, all actions made by the macro were reversed at one go and it was much faster even for complex macros.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial