Factor out caretPosition and caretPositionInternal code into sub-package.

This will make it easier to read and improve cursor API.