Fix cursor position logic

At end of line and related to the custom character API.

Calculate cursor position for next line at end of line and keep track of
current cursor position to restore it after custom character get/set.