Respect the text selection in FormatAction

Implements and closes #986.