aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/interface.h
diff options
context:
space:
mode:
authorRobin Haberkorn <rhaberkorn@fmsbw.de>2026-07-25 01:55:28 +0200
committerRobin Haberkorn <rhaberkorn@fmsbw.de>2026-07-25 01:55:28 +0200
commit685507922b0b75da5935076395a5b1ec1ef58356 (patch)
treebebc7c421856a6da363d2f9f7010b1ed211fee6c /src/interface.h
parent378b9da1476cdcac14332b89f6286a450c4a7757 (diff)
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.
Diffstat (limited to 'src/interface.h')
-rw-r--r--src/interface.h64
1 files changed, 58 insertions, 6 deletions
diff --git a/src/interface.h b/src/interface.h
index a7968d1..9c3d31e 100644
--- a/src/interface.h
+++ b/src/interface.h
@@ -211,21 +211,73 @@ teco_interface_get_codepage(void)
}
static inline gssize
-teco_interface_glyphs2bytes(teco_int_t pos)
+teco_interface_glyphs2bytes_rel(teco_int_t pos_glyphs, gsize pos, teco_int_t n)
{
- return teco_view_glyphs2bytes(teco_interface_current_view, pos);
+ return teco_view_glyphs2bytes_rel(teco_interface_current_view,
+ pos_glyphs, pos, n);
}
static inline teco_int_t
-teco_interface_bytes2glyphs(gsize pos)
+teco_interface_bytes2glyphs_rel(teco_int_t pos_glyphs, gsize pos, gssize n)
{
- return teco_view_bytes2glyphs(teco_interface_current_view, pos);
+ return teco_view_bytes2glyphs_rel(teco_interface_current_view,
+ pos_glyphs, pos, n);
}
+/**
+ * Convert absolute glyph position to bytes.
+ *
+ * This allows for scanning around dot and should be used if
+ * pos can be expected to be close to dot.
+ * Dot in glyphs as set by teco_current_doc_set_dot()
+ * \b must be up to date for this to work.
+ */
+static inline gssize
+teco_interface_glyphs2bytes_absdot(teco_int_t pos)
+{
+ teco_int_t dot = teco_current_doc_get_dot();
+ gsize curpos = teco_interface_ssm(SCI_GETCURRENTPOS, 0, 0);
+ return teco_interface_glyphs2bytes_rel(dot, curpos, pos-dot);
+}
+
+/**
+ * Convert absolute byte position to glyphs.
+ *
+ * @see teco_interface_glyphs2bytes_absdot
+ */
+static inline teco_int_t
+teco_interface_bytes2glyphs_absdot(gsize pos)
+{
+ teco_int_t dot = teco_current_doc_get_dot();
+ gsize curpos = teco_interface_ssm(SCI_GETCURRENTPOS, 0, 0);
+ return teco_interface_bytes2glyphs_rel(dot, curpos, pos-curpos);
+}
+
+/**
+ * Convert relative glyph position to bytes.
+ *
+ * Dot in glyphs as set by teco_current_doc_set_dot()
+ * \b must be up to date for this to work.
+ */
static inline gssize
-teco_interface_glyphs2bytes_relative(gsize pos, teco_int_t n)
+teco_interface_glyphs2bytes_reldot(teco_int_t n)
+{
+ teco_int_t dot = teco_current_doc_get_dot();
+ gsize curpos = teco_interface_ssm(SCI_GETCURRENTPOS, 0, 0);
+ return teco_interface_glyphs2bytes_rel(dot, curpos, n);
+}
+
+/**
+ * Convert relative byte position to glyphs.
+ *
+ * @see teco_interface_glyphs2bytes_reldot
+ */
+static inline teco_int_t
+teco_interface_bytes2glyphs_reldot(gssize n)
{
- return teco_view_glyphs2bytes_relative(teco_interface_current_view, pos, n);
+ teco_int_t dot = teco_current_doc_get_dot();
+ gsize curpos = teco_interface_ssm(SCI_GETCURRENTPOS, 0, 0);
+ return teco_interface_bytes2glyphs_rel(dot, curpos, n);
}
static inline teco_int_t