diff options
| author | Albert Cervin <albert@acervin.com> | 2024-08-23 17:07:27 +0200 |
|---|---|---|
| committer | Albert Cervin <albert@acervin.com> | 2024-09-11 16:22:58 +0200 |
| commit | 4ab7e453e26afc6e9f4938c65f89463fbba9e267 (patch) | |
| tree | 4745d99e70d645a8134dafc3814dc68bf678daf4 /src/dged/buffer_view.c | |
| parent | 991283f684c224db46fe68738470921b8c394f13 (diff) | |
| download | dged-4ab7e453e26afc6e9f4938c65f89463fbba9e267.tar.gz dged-4ab7e453e26afc6e9f4938c65f89463fbba9e267.tar.xz dged-4ab7e453e26afc6e9f4938c65f89463fbba9e267.zip | |
Overhaul unicode parsing
It now instead iterates the actual unicode code points. This is better
than what it was previously doing but it is still not entirely correct
w.r.t to unicode sequences.
This handling of unicode code points does however make it slightly
easier to handle UTF-16 if needed in the future.
This also adds some long needed tests for buffer methods.
Diffstat (limited to 'src/dged/buffer_view.c')
| -rw-r--r-- | src/dged/buffer_view.c | 40 |
1 files changed, 17 insertions, 23 deletions
diff --git a/src/dged/buffer_view.c b/src/dged/buffer_view.c index 4e67d78..f3dd2b9 100644 --- a/src/dged/buffer_view.c +++ b/src/dged/buffer_view.c @@ -128,7 +128,7 @@ void buffer_view_backward_nlines(struct buffer_view *view, uint32_t nlines) { } void buffer_view_goto_end_of_line(struct buffer_view *view) { - view->dot.col = buffer_num_chars(view->buffer, view->dot.line); + view->dot.col = buffer_line_length(view->buffer, view->dot.line); } void buffer_view_goto_beginning_of_line(struct buffer_view *view) { @@ -224,15 +224,22 @@ void buffer_view_delete_word(struct buffer_view *view) { } void buffer_view_kill_line(struct buffer_view *view) { - uint32_t nchars = - buffer_num_chars(view->buffer, view->dot.line) - view->dot.col; - if (nchars == 0) { - nchars = 1; + uint32_t ncols = + buffer_line_length(view->buffer, view->dot.line) - view->dot.col; + + uint32_t line = view->dot.line; + uint32_t col = view->dot.col + ncols; + + // kill the newline if we are at the end of the line + if (ncols == 0) { + struct location loc = buffer_next_char(view->buffer, view->dot); + line = loc.line; + col = loc.col; } struct region reg = region_new(view->dot, (struct location){ - .line = view->dot.line, - .col = view->dot.col + nchars, + .line = line, + .col = col, }); buffer_cut(view->buffer, reg); @@ -241,7 +248,8 @@ void buffer_view_kill_line(struct buffer_view *view) { void buffer_view_sort_lines(struct buffer_view *view) { struct region reg = region_new(view->dot, view->mark); if (view->mark_set && region_has_size(reg)) { - if (reg.end.line > 0 && buffer_num_chars(view->buffer, reg.end.line) == 0) { + if (reg.end.line > 0 && + buffer_line_length(view->buffer, reg.end.line) == 0) { reg.end.line -= 1; } @@ -271,21 +279,7 @@ struct location buffer_view_dot_to_relative(struct buffer_view *view) { } struct location buffer_view_dot_to_visual(struct buffer_view *view) { - // calculate visual column index for dot column - struct text_chunk c = buffer_line(view->buffer, view->dot.line); - uint32_t width = visual_string_width(c.text, c.nbytes, 0, view->dot.col); - if (view->scroll.col > 0) { - width -= visual_string_width(c.text, c.nbytes, 0, view->scroll.col); - } - - struct location l = buffer_view_dot_to_relative(view); - l.col = width + view->fringe_width; - - if (c.allocated) { - free(c.text); - } - - return l; + return buffer_view_dot_to_relative(view); } void buffer_view_undo(struct buffer_view *view) { |
