Restore cumulative undo functionality
The functionality has been lost due to a merge conflict in 2015(!). This patch restores it.
WARNING: This patch is based on my colorsmudge branch to avoid conflicts, hence the patch is marked as WIP until colorsmudge is merged.
Edited by Dmitry Kazakov