correct line/offset calculations

- there is always at least one line
- handle special case where cursor is at end of file
fix editor layout