From 734eb0174a457798d94aea1e467d01e9b3fbf3b1 Mon Sep 17 00:00:00 2001 From: TnS-hun Date: Fri, 27 Dec 2024 10:48:04 +0100 Subject: [PATCH 1/2] xtext: getSelectedWords returns with indices Rename getSelectedWords to getSelectedWordIndices, and return the word boundary indices instead of the words. This is needed for highlighting. The words themselves can obtained by passing the indices to the getText function. Warning! Breaking change. --- xtext.cpp | 34 ++++++++++------------------------ 1 file changed, 10 insertions(+), 24 deletions(-) diff --git a/xtext.cpp b/xtext.cpp index c2d3d402e..1f99d6ff1 100644 --- a/xtext.cpp +++ b/xtext.cpp @@ -2063,9 +2063,8 @@ class XText { free(s_utf8); } - // Get (as a single UTF-8 string) the segment of m_text, extended to include - // the full words that may be cut at boundaries (start, end). - void getSelectedWords(int start, int end, int context) { + // Get start and end indices of an extended segment of m_text that includes the full words that may be cut at boundaries (start, end). + void getSelectedWordIndices(int start, int end, int context) { // Not much documentation about libunibreak word breaking, // some insight in: // http://www.unicode.org/reports/tr29/tr29-25.html @@ -2125,20 +2124,8 @@ class XText { wend++; } - // FriBiDi provides a unicode to UTF-8 conversion function, so use it. - // Let's be cheap and not count the nb of bytes really needed to store the - // UTF-8 encoding of each Unicode codepoint: go allocate for the max (4). - // (As we're called to return user selected text, we shouldn't waste - // too much - and we'll free it just below.) - len = wend - wstart; - char * s_utf8 = (char *)malloc(len * 4*sizeof(char) + 1); - fribidi_unicode_to_charset(FRIBIDI_CHAR_SET_UTF8, m_text+wstart, len, s_utf8); - lua_pushstring(m_L, s_utf8); - #if 0 - printf("getWords: %d>%d #%s#\n", wstart, wend, s_utf8); - lua_pushstring(m_L, ""); // prevent a dict lookup - #endif - free(s_utf8); + lua_pushinteger(m_L, wstart + 1); // Lua indices start at 1 + lua_pushinteger(m_L, wend + 1); free(breaks); } }; @@ -2496,9 +2483,8 @@ static int XText_getText(lua_State *L) { return 1; } -// Get (as a single UTF-8 string) the segment of m_text, extended to include -// the full words that may be cut at boundaries (start, end). -static int XText_getSelectedWords(lua_State *L) { +// Get start and end indices of an extended segment of m_text that includes the full words that may be cut at boundaries (start, end). +static int XText_getSelectedWordIndices(lua_State *L) { XText * xt = check_XText(L, 1); int start = luaL_checkint(L, 2); luaL_argcheck(L, start >= 1 && start <= xt->m_length, 2, "index out of range"); @@ -2511,9 +2497,9 @@ static int XText_getSelectedWords(lua_State *L) { // to inspect to find cut words' start/end. int context = luaL_checkint(L, 4); luaL_argcheck(L, context > 0, 3, "context must be strictly positive"); - xt->getSelectedWords(start, end, context); - // getSelectedWords() will have pushed a Lua string onto the stack - return 1; + xt->getSelectedWordIndices(start, end, context); + // getSelectedWordIndices() will have pushed two Lua integers onto the stack + return 2; } @@ -2538,7 +2524,7 @@ static const struct luaL_Reg xtext_meth[] = { {"getParaDirection", XText_getParaDirection}, {"getSegmentFromEnd", XText_getSegmentFromEnd}, {"getText", XText_getText}, - {"getSelectedWords", XText_getSelectedWords}, + {"getSelectedWordIndices", XText_getSelectedWordIndices}, { "free", XText_free }, { "__gc", XText_destroy }, {NULL, NULL} From 537b35db72c4a1754470c32f201d7701a06b9847 Mon Sep 17 00:00:00 2001 From: TnS-hun Date: Sat, 28 Dec 2024 19:09:21 +0100 Subject: [PATCH 2/2] Fix off by one error --- xtext.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xtext.cpp b/xtext.cpp index 1f99d6ff1..3989ae66b 100644 --- a/xtext.cpp +++ b/xtext.cpp @@ -2125,7 +2125,7 @@ class XText { } lua_pushinteger(m_L, wstart + 1); // Lua indices start at 1 - lua_pushinteger(m_L, wend + 1); + lua_pushinteger(m_L, wend); free(breaks); } };