Fix tabs expansion

This is the fixed tabs expansion together with test in a standalone branch, as extracted from the smart_text branch.