diff options
author | Kai Jægersen <kaio> | 2019-10-03 22:56:41 +0300 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2019-10-03 23:00:08 +0300 |
commit | bf2d92b277b9be383b486f5c169a77399f69b6f0 (patch) | |
tree | 056ea8e6ca782a92cf6c94bcfdb02f4d52527012 /source/blender/blenkernel/intern/text.c | |
parent | 2ba8adca4ffd0df0c537fcdf991c56bd6d5b7def (diff) |
Text: add Text.select_set(...)
Support setting the selection for a text buffer
with support for negative indices, select_set(1, 1, -1, -1)
selects the entire buffer.
Diffstat (limited to 'source/blender/blenkernel/intern/text.c')
-rw-r--r-- | source/blender/blenkernel/intern/text.c | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/source/blender/blenkernel/intern/text.c b/source/blender/blenkernel/intern/text.c index 6b8e33e3a01..4b01b6467dd 100644 --- a/source/blender/blenkernel/intern/text.c +++ b/source/blender/blenkernel/intern/text.c @@ -1218,6 +1218,58 @@ void txt_sel_line(Text *text) text->selc = text->sell->len; } +void txt_sel_set(Text *text, int startl, int startc, int endl, int endc) +{ + TextLine *froml, *tol; + int fromllen, tollen; + + /* Support negative indices. */ + if (startl < 0 || endl < 0) { + int end = BLI_listbase_count(&text->lines) - 1; + if (startl < 0) { + startl = end + startl + 1; + } + if (endl < 0) { + endl = end + endl + 1; + } + } + CLAMP_MIN(startl, 0); + CLAMP_MIN(endl, 0); + + froml = BLI_findlink(&text->lines, startl); + if (froml == NULL) { + froml = text->lines.last; + } + if (startl == endl) { + tol = froml; + } + else { + tol = BLI_findlink(&text->lines, endl); + if (tol == NULL) { + tol = text->lines.last; + } + } + + fromllen = BLI_strlen_utf8(froml->line); + tollen = BLI_strlen_utf8(tol->line); + + /* Support negative indices. */ + if (startc < 0) { + startc = fromllen + startc + 1; + } + if (endc < 0) { + endc = tollen + endc + 1; + } + + CLAMP(startc, 0, fromllen); + CLAMP(endc, 0, tollen); + + text->curl = froml; + text->curc = BLI_str_utf8_offset_from_index(froml->line, startc); + text->sell = tol; + text->selc = BLI_str_utf8_offset_from_index(tol->line, endc); +} + /* -------------------------------------------------------------------- */ /** \name Buffer Conversion for Undo/Redo * |