From 685507922b0b75da5935076395a5b1ec1ef58356 Mon Sep 17 00:00:00 2001 From: Robin Haberkorn Date: Sat, 25 Jul 2026 01:55:28 +0200 Subject: revised and improved the Unicode glyph-to-byte conversion heuristics Previously almost all glyph-to-byte offset conversions consulted Scintilla's line index and counted characters on the resulting line. For instance a simple expression like `.+1J` would scan the same line twice completely, which would be very slow on pathologically long lines. Even insertions did that due to having to update the ^Y ranges. If you repeat such an operation over all characters as in `<.+1:J;>` you would have complexity O(n^2) for n = line length. Only commands with an explicit relative nature like `C` and `A` would use teco_view_glyph2bytes_relative() which scans beginning at dot as long as the relative movement is less than 1024 glyphs. Wit the new heuristics almost all glyph-to-byte and byte-to-glyph conversions can make use of that optimization. This requires that dot must at all times be known in glyphs as well - the byte position is managed by Scintilla (SCI_GETCURRENTPOS). We therefore introduced teco_current_doc_set_dot() and teco_current_doc_get_dot() to update dot in the current buffer or Q-Register -- it cannot be stored along with the view since Q-Registers share a single view. A number of auxiliary functions have been introduced for converting relative to a known (glyphs,bytes) offset pair and for converting absolute and relative positions with regard to the current doc and SCI_GETCURRENTPOS position. Of course this is error-prone since the glyph and dot positions are interdependant - they must always be kept in sync. With these new optimizations even pathologically long lines can (usually) be managed even in UTF-8 documents. It does not address slow-downs in Scintilla's line layout, yet. grosciteco.tes for instance runs twice as fast now. --- src/qreg-commands.c | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) (limited to 'src/qreg-commands.c') diff --git a/src/qreg-commands.c b/src/qreg-commands.c index 4ede403..a31fe4c 100644 --- a/src/qreg-commands.c +++ b/src/qreg-commands.c @@ -572,9 +572,11 @@ teco_state_getqregstring_got_register(teco_machine_main_t *ctx, teco_qreg_t *qre undo__teco_interface_ssm(SCI_UNDO, 0, 0); } - teco_undo_int(teco_ranges[0].from) = teco_interface_bytes2glyphs(pos); - teco_undo_int(teco_ranges[0].to) = teco_interface_bytes2glyphs(pos + str.len); + teco_undo_int(teco_ranges[0].from) = teco_current_doc_get_dot(); + teco_int_t to = teco_interface_bytes2glyphs_rel(teco_ranges[0].from, pos, str.len); + teco_undo_int(teco_ranges[0].to) = to; teco_undo_guint(teco_ranges_count) = 1; + teco_current_doc_set_dot(to); return &teco_state_start; } @@ -798,9 +800,10 @@ teco_state_copytoqreg_got_register(teco_machine_main_t *ctx, teco_qreg_t *qreg, if (ctx->flags.mode > TECO_MODE_NORMAL) return &teco_state_start; + teco_int_t from_glyphs, len_glyphs; gsize from, len; - if (!teco_get_range_args("X", &from, &len, error)) + if (!teco_get_range_args("X", &from_glyphs, &from, &len_glyphs, &len, error)) return NULL; /* @@ -829,11 +832,12 @@ teco_state_copytoqreg_got_register(teco_machine_main_t *ctx, teco_qreg_t *qreg, if (!modifier_at || len == 0) return &teco_state_start; + sptr_t pos = teco_interface_ssm(SCI_GETCURRENTPOS, 0, 0); + /* * If @-modified, cut into the register */ if (teco_current_doc_must_undo()) { - sptr_t pos = teco_interface_ssm(SCI_GETCURRENTPOS, 0, 0); undo__teco_interface_ssm(SCI_GOTOPOS, pos, 0); undo__teco_interface_ssm(SCI_UNDO, 0, 0); } @@ -846,6 +850,13 @@ teco_state_copytoqreg_got_register(teco_machine_main_t *ctx, teco_qreg_t *qreg, teco_interface_ssm(SCI_ENDUNDOACTION, 0, 0); teco_ring_dirtify(); + if (from+len < pos) + /* dot after deleted range */ + teco_current_doc_set_dot(teco_current_doc_get_dot() - len_glyphs); + else if (from <= pos) + /* dot falls on deleted range */ + teco_current_doc_set_dot(from_glyphs); + return &teco_state_start; } -- cgit v1.2.3