Skip to content

Conversation

@rayworks
Copy link

  • Implement search_fwd() function in ted_buffer.c

    • Search from current cursor position forward through buffer
    • Move cursor to first match found
  • Add search command to ted_commands.c

    • Prompt user for search keyword
    • Integrate with search_fwd() function
  • Add ctrl+r keybinding for search in ted_input.c

  • Add command hints for search functionality

- Implement search_fwd() function in ted_buffer.c
  * Search from current cursor position forward through buffer
  * Move cursor to first match found

- Add search command to ted_commands.c
  * Prompt user for search keyword
  * Integrate with search_fwd() function

- Add ctrl+r keybinding for search in ted_input.c
- Add command hints for search functionality
@rayworks
Copy link
Author

Hi @arthurbacci, any feedback for this PR?

@arthurbacci
Copy link
Owner

Hi @arthurbacci, any feedback for this PR?

Hi. It seems like a good adition, thank you for contributing. But I believe something is wrong with the way x_width is used but I am not sure. Unfortunately I don't have much time this week so I will probably take some days to review it and merge.

Copy link
Owner

@arthurbacci arthurbacci left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non optimal behaviour with width vs bytes

@arthurbacci
Copy link
Owner

I just don't remember how to recalculate x from x_bytes, it may be a function from libgrapheme, idk. I will take a better look at this later.

@rayworks
Copy link
Author

Thanks for reviewing it. I've updated the part to calculate the cursor position.

@rayworks rayworks requested a review from arthurbacci August 19, 2025 01:09
@arthurbacci
Copy link
Owner

Thanks for reviewing it. I've updated the part to calculate the cursor position.

Thank you. Now you just need to also calculate .x using wi_to_gi with .x_bytes

@rayworks
Copy link
Author

Thanks for reviewing it. I've updated the part to calculate the cursor position.

Thank you. Now you just need to also calculate .x using wi_to_gi with .x_bytes

👍 Updated.

@arthurbacci
Copy link
Owner

Thanks for reviewing it. I've updated the part to calculate the cursor position.

Thank you. Now you just need to also calculate .x using wi_to_gi with .x_bytes

👍 Updated.

Actually, all that code you deleted in your last commit was probably right.

Basically:

  • x is the index of the grapheme cluster the cursor is at
  • x_bytes is the index of the byte the cursor is at
  • x_width is the graphical horizontal position of the x cursor in the screen relative to the start of the line

In 90% of the cases they will all be the same.

I don't remember very well what recalc_cur does and what is does not, but I believe you need to set all those three. Since no functions in ted work with bytes, I don't remember how one would set both from x_bytes, but I think what you did with that while loop was the way. You should keep that, since it calculates x from x_bytes, and then you should only do:

buf->cursor.x_width = wi_to_gi(buf->cursor.x, curr_line);

This finishes setting both three.

@rayworks
Copy link
Author

rayworks commented Aug 20, 2025

typedef struct {
    size_t lx_width;
    size_t x_width;
    size_t x_bytes;
    
    size_t y;
} Cursor;

void truncate_cur(Buffer *buf) {
    Line *ln = &buf->lines[buf->cursor.y];
    char *s = ln->data;
    
    buf->cursor.x_width -= index_by_width(buf->cursor.x_width, &s);
    buf->cursor.x_bytes = s - ln->data;
}

void recalc_cur(Buffer *buf) {
    truncate_cur(buf);
    buf->cursor.lx_width = buf->cursor.x_width;
}

It seems as long as x_width is in place, lx_width will be set as well after calling recalc_cur.

@arthurbacci
Copy link
Owner

What you are doing now seems right, except that you should use gi_to_wi (grapheme index to width index) instead of wi_to_gi

@rayworks
Copy link
Author

What you are doing now seems right, except that you should use gi_to_wi (grapheme index to width index) instead of wi_to_gi

Nice catch.

@arthurbacci
Copy link
Owner

Seems to work well, tested with the unicode-test.txt file and it seems to correctly set the x position for complex grapheme sequences. Thank you a lot for your contribution.

I will just check for more details this weekend so that I can be sure it's doing everything right before merging into main, since I don't remember very well what's the correctly way of dealing with changing the x cursor. Maybe I should create a utility function to set it from width, grapheme index, or byte index so that this proccess isn't needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants