From deb978ab88691716dbfd5ce7b75f2ae20fc38056 Mon Sep 17 00:00:00 2001 From: ketmar <> Date: Tue, 31 Oct 2023 14:38:28 +0000 Subject: [PATCH] word up/locaser script cosmetix FossilOrigin-Name: 4d79df8bacb84157fee02744e39b2ab0e7ea349799f39bb10738d6e450d0abce --- config/sex/modules/locaseword/main.sex | 72 +++++++++++++--------------------- 1 file changed, 28 insertions(+), 44 deletions(-) diff --git a/config/sex/modules/locaseword/main.sex b/config/sex/modules/locaseword/main.sex index c279b1a..5ad5710 100644 --- a/config/sex/modules/locaseword/main.sex +++ b/config/sex/modules/locaseword/main.sex @@ -27,25 +27,10 @@ func is_word_char (ch) { ch == `_`; } - -func is_up_char (ch) { - return (ch >= `A` && ch <= `Z`); -} - - -func is_lo_char (ch) { - return (ch >= `a` && ch <= `z`); -} - - -func locase_char (ch) { - return (ch >= `A` && ch <= `Z` ? ch-`A`+`a` : ch); -} - - -func upcase_char (ch) { - return (ch >= `a` && ch <= `z` ? ch-`a`+`A` : ch); -} +func is_up_char (ch) { return (ch >= `A` && ch <= `Z`); } +func is_lo_char (ch) { return (ch >= `a` && ch <= `z`); } +func locase_char (ch) { return (ch >= `A` && ch <= `Z` ? ch-`A`+`a` : ch); } +func upcase_char (ch) { return (ch >= `a` && ch <= `z` ? ch-`a`+`A` : ch); } // returns position or -1 @@ -53,17 +38,15 @@ func find_word_start (frm) { var cpos = frm.cursor_byteofs; var buf = frm.buffer; - // not in a word? if (!is_word_char(buf[cpos])) { // check right while (cpos < buf.length && !is_word_char(buf[cpos])) cpos += 1; if (cpos >= buf.length) cpos = -1; - return cpos; + } else { + // find the beginning of the word + while (cpos > 0 && is_word_char(buf[cpos - 1])) cpos -= 1; } - // find the beginning of the word - while (cpos > 0 && is_word_char(buf[cpos - 1])) cpos -= 1; - return cpos; } @@ -71,41 +54,42 @@ func find_word_start (frm) { module.loupcaseword = func (frm, upcase) { var wstart = find_word_start(frm); if (wstart == -1) return; + // find end, and check if we have anything to do - var wend = wstart; + var buf = frm.buffer; + var wend = wstart; // do not skip the first char, we need it fo `seenUp` var seenUp = false; var ch = nil; - for (;;) { - ch = frm.buffer[wend]; - if (!is_word_char(ch)) break; + ch = buf[wend]; + while (is_word_char(ch)) { if (!seenUp) seenUp = (upcase ? is_lo_char(ch) : is_up_char(ch)); - ++wend; + wend += 1; + ch = buf[wend]; } if (!seenUp) { // just move the cursor frm.cursor_byteofs = wend; - return; - } + } else { + // convert + var news = ""; - // convert - var news = ""; + for (var f = wstart; f < wend; f += 1) { + ch = (upcase ? upcase_char(buf[f]) : locase_char(buf[f])); + news ~= chr(ch); + } - for (var f = wstart; f < wend; ++f) { - ch = (upcase ? upcase_char(frm.buffer[f]) : locase_char(frm.buffer[f])); - news ~= chr(ch); - } - - var ugid = frm.buffer.undo_start_group(); - frm.write_mutation_undo(); + var ugid = buf.undo_start_group(); + frm.write_mutation_undo(); - frm.cursor_byteofs = wend; + frm.cursor_byteofs = wend; - frm.buffer.delete(wstart, wend-wstart); - frm.insert_raw(news); + buf.delete(wstart, wend - wstart); + frm.insert_raw(news); - frm.buffer.undo_end_group(ugid); + buf.undo_end_group(ugid); + } } -- 2.11.4.GIT