Instruction set reference
HIPE_OP_GET_CARAT_POSITION
Gets the current position of the text entry cursor in an element, or the range of text selected in it.
The display server responds with a HIPE_OP_CARAT_POSITION instruction. Its arg[0] is the character offset of the selection's anchor (where the selection starts) and its arg[1] is the offset of the selection's focus (where it ends, which is where the cursor is). They are equal when no text is selected, and arg[1] is less than arg[0] when the text was selected backwards. Offsets are counted as described on the HIPE_OP_CARAT_POSITION page.
- location: The element for which you require the current cursor position or selection range.
Notes
A cursor with no text selected is still a position: it is reported with both arguments equal, e.g. "3" and "3".
If the element contains neither the cursor nor selected text, arg[0] and arg[1] of the reply are both empty strings. A document has only one cursor or selection at a time, so this is the case when it is somewhere else: for example, the user has clicked into another editable element or on ordinary text elsewhere in the document, or nothing has been clicked or focused yet. It is also the case when a selection extends beyond the element, starting inside it and ending outside (this can't happen within a contenteditable element, whose editing selection stays inside it). input and textarea elements keep their own cursor position even when they don't have focus, so they always report one.
An empty string is distinct from "0", which is a cursor before the first character, so check the argument's length before converting it to a number (for example, atoi would return 0 for an empty string).
This works for the cursor in editable content and also for text the user has selected in non-editable content inside the element. To get the selected text itself, use HIPE_OP_GET_SELECTION.
