CRichEditCtrl as syntax-highlighting programming language editor: SetSelectionCharFormat, undo

845 Views Asked by At

I'm using a CRichEditCtrl to edit a computer language, and on every change to it I'm changing the CharFormat of the text to highlight the syntax. (EG: comments in green, section headings in a bigger font, compilation errors in a different font, etc.)

It all looks like its working fine.

However there are some weird issues. One is, the undo buffer of the CRichEditCtrl contains not only the user events but also my calls to SetSelectionCharFormat. For instance, lets say the user types "#" at the beginning of the line, making that line a comment. The software then automatically turns it green. Now when the user tries ctrl-z, the line turns back to black. Another ctrl-z removes his "#". How can I avoid SetSelectionCharFormat() changes from going into the undo buffer?

1

There are 1 best solutions below

0
On

I think it all depends on when you are processing the user input messages. If you are processing it in the handler of ON_EN_CHANGE event then your actions will be considered as a separate step and will be added to the Undo buffer.

However, if you put your code in ON_EN_UPDATE handler, your actions will be added to the user's actions and be placed into the undo buffer as a single step.

Read more about notifications from edit controls here (CRichEditCtrl supports almost all of the operations and notification messages used with multiline edit controls, therefore I have provided the link for CEdit control)

BTW, have a look here for an alternative approach