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 | |
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')
-rw-r--r-- | source/blender/blenkernel/BKE_text.h | 1 | ||||
-rw-r--r-- | source/blender/blenkernel/intern/text.c | 52 | ||||
-rw-r--r-- | source/blender/makesrna/intern/rna_text_api.c | 18 |
3 files changed, 71 insertions, 0 deletions
diff --git a/source/blender/blenkernel/BKE_text.h b/source/blender/blenkernel/BKE_text.h index 65aa43ced7c..98a94c5f689 100644 --- a/source/blender/blenkernel/BKE_text.h +++ b/source/blender/blenkernel/BKE_text.h @@ -78,6 +78,7 @@ void txt_delete_selected(struct Text *text); void txt_sel_all(struct Text *text); void txt_sel_clear(struct Text *text); void txt_sel_line(struct Text *text); +void txt_sel_set(struct Text *text, int startl, int startc, int endl, int endc); char *txt_sel_to_buf(struct Text *text, int *r_buf_strlen); void txt_insert_buf(struct Text *text, const char *in_buffer); void txt_split_curline(struct Text *text); 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 * diff --git a/source/blender/makesrna/intern/rna_text_api.c b/source/blender/makesrna/intern/rna_text_api.c index 524dcfa9ad7..bcaa693524c 100644 --- a/source/blender/makesrna/intern/rna_text_api.c +++ b/source/blender/makesrna/intern/rna_text_api.c @@ -46,6 +46,12 @@ static void rna_Text_write(Text *text, const char *str) WM_main_add_notifier(NC_TEXT | NA_EDITED, text); } +static void rna_Text_select_set(Text *text, int startl, int startc, int endl, int endc) +{ + txt_sel_set(text, startl, startc, endl, endc); + WM_main_add_notifier(NC_TEXT | NA_EDITED, text); +} + #else void RNA_api_text(StructRNA *srna) @@ -69,6 +75,18 @@ void RNA_api_text(StructRNA *srna) RNA_def_function_ui_description(func, "Returns True if the editor supports syntax highlighting " "for the current text datablock"); + + func = RNA_def_function(srna, "select_set", "rna_Text_select_set"); + RNA_def_function_ui_description(func, "Set selection range by line and character index"); + parm = RNA_def_int(func, "line_start", 0, INT_MIN, INT_MAX, "Start Line", "", INT_MIN, INT_MAX); + RNA_def_parameter_flags(parm, 0, PARM_REQUIRED); + parm = RNA_def_int( + func, "char_start", 0, INT_MIN, INT_MAX, "Start Character", "", INT_MIN, INT_MAX); + RNA_def_parameter_flags(parm, 0, PARM_REQUIRED); + parm = RNA_def_int(func, "line_end", 0, INT_MIN, INT_MAX, "End Line", "", INT_MIN, INT_MAX); + RNA_def_parameter_flags(parm, 0, PARM_REQUIRED); + parm = RNA_def_int(func, "char_end", 0, INT_MIN, INT_MAX, "End Character", "", INT_MIN, INT_MAX); + RNA_def_parameter_flags(parm, 0, PARM_REQUIRED); } #endif |