0

I have just been introduced to the LUA language, and I am embarking on my first project. However, the biggest challenge I am facing now is how to implement or make an Undo and Redo.

However, to make issues clear, the project is a Custom Text Editor, and as a result, the Undo/Redo here is required for editing any input text. I have manage to handle issues like Cut,Copy, Clear, Find Word, as well as Changing Font, Text Colour, inserting tables and images among others, and all these were handled in the lua language. Obviously, there are several of the custom text editors, i believe the effort to cater for many will pave the way for future advancements or improvements. But the Undo/Redo actions are tearing me apart, which from my research is even the lack by most of the existing custom text editors.

I have searched several forums where they all seem to give the tip of using an associative kind of table to load the information, and retrieve them from there. Unbelievably, i think some of these sites are just sharing their knowledge acquired from other sites without any technical view point or whatsoever. This is because, most of the suggestions i come across seem to look alike and the same in all aspect. For about tens of sites visited, there is non where a user has tried to post an example, but all i see is the same complain about the majority of lua users. Undoubtedly, this will seem a bit easy to some respected gurus in this forum.

I don't seem to get the true picture of the suggestions. Can someone provide me with an example?

Tadeusz A. Kadłubowski
  • 8,047
  • 1
  • 30
  • 37

1 Answers1

2

Undo/redo is a perfect fit for command pattern.

First you need to write some of the text manipulation functionality per se. Just the do part, without worrying about un- or re-. That will be lots of work in itself.

You will then have a bunch of functions to manipulate your document. Things like insertText(), setFont(), insertJpgImage() and such. The trick is that now you need to wrap each of this functions in a so called command object. Each command class must have a method to do() itself, and to undo() itself.

Now that all your text manipulation operations are represented by command objects, you execute each operation (e.g. bold some text) by something like:

boldCommand = setTextPropertyCommand:new(document, selectedArea, textProperties.bold)
boldCommand:do() --actually modify text
table.insert(commandUndoStack, boldCommand) --keep the command for possible undoing later.

When you want to undo the bolding of some text you can then call:

command = table.remove(commandUndoStack)
command:undo()

NB, if your are using some GUI framework binding in Lua, then it might be the case that this framework has its own readymade undo/redo functionality. For example Qt (with qtlua bindings) offers QUndoStack class.

Tadeusz A. Kadłubowski
  • 8,047
  • 1
  • 30
  • 37
  • Ok Tadeusz, that is the best of responds and a very quick one. But i'm using an application that gives me access to creat the RichText Object, so what i am actually asking is how to implement a redo in this RichText Object, because eventhough your responds is a quick one, i actually want to know as a biggingner, how i can create the undo redo using the personal edition of media studio. Thanks, but still counting on you for a reply that will serve a newbie so simple –  Jun 26 '12 at 16:04
  • Ok Tadeusz, that is the best of responds and a very quick one. But i'm using an application that gives me access to creat the RichText Object, so what i am actually asking is how to implement a redo in this RichText Object, because eventhough your responds is a quick one, i actually want to know as a beginner, how i can create the undo redo using for instance the personal edition of media studio. Thanks, but still counting on you for a reply that will serve a newbie so simple –  Jun 26 '12 at 16:15
  • @Derick: Did you finish the first part of my plan? You start by writing some of the text manipulation functionalityt, without worrying about undo or redo. – Tadeusz A. Kadłubowski Jun 26 '12 at 18:24
  • @Kadlubowski: Hi friend, that's very good of you, keep it up. You are such a reliable and a dependable programmer that one can not do without. I never thought you were going to be that quick. All the best of friend i would remember! –  Jun 27 '12 at 15:37
  • @Kadlubowski: Hi my friend, it appears like the comment is shortened. I said yes i've noticed your post, so let say now i have things like; RichText.GetText, RichText.GetSelection, RichText.SetText among others. So in this case how can i do something like RichText.Undo and RichText.Redo? Please i'm now learning the lua, so please just come down to the level of an absolute beginner. All the same, i'm also requesting for friendship for your generosity and reliability! –  Jun 27 '12 at 18:53
  • @derick: Now you're in the third paragraph of my answer. Make sure you read and understand documents linked in the answer. Also, read about object-oriented programming in Lua. – Tadeusz A. Kadłubowski Jun 28 '12 at 08:40