diff --git a/src/classes.c b/src/classes.c index a5e21ea..4552ce4 100644 --- a/src/classes.c +++ b/src/classes.c @@ -43,13 +43,13 @@ void text_render(struct TeX_Node const * node, struct editContext * context, int if (context != NULL && context->elementIsText && context->cursorText == node) { // Set the cursor position // Temporarily cap the length of the string to the cursor position - char old = node->text[context->offset + 1]; - node->text[context->offset + 1] = '\0'; + char old = node->text[context->offset]; + node->text[context->offset] = '\0'; // Measure the width of the string int w, h; TeX_size((char const *)node->text, &w, &h); // Restore the string - node->text[context->offset + 1] = old; + node->text[context->offset] = old; // Update the cursor position to inside the text context->cursorX = x + w; context->cursorY = y; diff --git a/src/flow.c b/src/flow.c index 6f873ba..70b2976 100644 --- a/src/flow.c +++ b/src/flow.c @@ -405,18 +405,32 @@ enum cursorMoveResult TeX_flow_cursor_action(struct TeX_Flow * flow, struct edit } else if (returnCode == CURSOR_PAST_END) { + bool wasText = context->elementIsText; // Put the cursor in this flow after the node context->elementIsText = false; context->cursorFlow = flow; context->offset = offset + 1; + // If we just left a text node, do the cursor action again + // This is because if we just put the cursor after the text + // node then it will look like it's just at the end of it + // and also it will be immedately put back in again + if (wasText) + { + return TeX_flow_cursor_action(flow, context, action); + } return SUCCESS; } else if (returnCode == CURSOR_PAST_START) { + bool wasText = context->elementIsText; // Put the cursor in this flow before the node context->elementIsText = false; context->cursorFlow = flow; context->offset = offset; + if (wasText) + { + return TeX_flow_cursor_action(flow, context, action); + } return SUCCESS; } diff --git a/src/node.c b/src/node.c index 0bb2395..533b406 100644 --- a/src/node.c +++ b/src/node.c @@ -22,7 +22,7 @@ enum cursorMoveResult TeX_node_cursor_action(struct TeX_Node *node, struct editC { // Text // TODO: Store the length of the text in the node - // THat way we won't have to call strlen every time + // That way we won't have to call strlen every time if (node->type == 0) { // If this isn't where the cursor is, continue the recursive search @@ -38,14 +38,7 @@ enum cursorMoveResult TeX_node_cursor_action(struct TeX_Node *node, struct editC return SUCCESS; } else if (action == CURSOR_MOVE_RIGHT) { - // We take away 2 because offset 0 is actually one character into the text, - // and the last possible offset is one character before the end of the text, - // because if the cursor is at the start or end it goes in the parent flow. - - // TODO: It would make more sense to still have offset 0 be the first character, - // but just make sure that offset is never actually set to 0 before the cursor - // returns to the parent flow. - if (context->offset >= strlen(node->text) - 2) + if (context->offset >= strlen(node->text)) return CURSOR_PAST_END; context->offset++; @@ -108,10 +101,9 @@ enum cursorMoveResult TeX_node_cursor_enter(struct TeX_Node *node, struct editCo if (node->type == 0) { int len = strlen(node->text); - if (len == 1) return skipPast; context->elementIsText = true; context->cursorText = node; - context->offset = action == CURSOR_MOVE_RIGHT ? 0 : len - 2; + context->offset = action == CURSOR_MOVE_RIGHT ? 0 : len; return SUCCESS; } // Regular nodes