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.
This commit is contained in:
Kai Jægersen 2019-10-04 05:56:41 +10:00 committed by Campbell Barton
parent 2ba8adca4f
commit bf2d92b277
3 changed files with 71 additions and 0 deletions

View File

@ -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);

View File

@ -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
*

View File

@ -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