Table/issue 2868

This finishes up @cherti's original PR at #3055 by rebasing on master and updating to work on master.  I also fixed a few bugs and reformatted the code a bit.  After I submit this PR I'm probably going to rebase again to collapse down some of @cherti's original commits, but will still make sure they get credit.
The prev_format-mechanism does only work if there has been a format already set at that point, as testing indicated. If this is the case, the try/except is indeed unnecessary, but for the first setting of the format it is relevant as there is no format set that we can back up. Therefore the try/except to avoid crashing in that case.