UI: Console Text Operations #108626

Manually merged
Campbell Barton merged 13 commits from Harley/blender:ConsoleSelect into main 2023-09-22 05:42:35 +02:00
7 changed files with 192 additions and 6 deletions
Showing only changes of commit fb4f6e4d2b - Show all commits

View File

@ -3080,12 +3080,20 @@ def km_console(_params):
items.extend([
("console.move", {"type": 'LEFT_ARROW', "value": 'PRESS', "ctrl": True, "repeat": True},
{"properties": [("type", 'PREVIOUS_WORD')]}),
("console.move", {"type": 'LEFT_ARROW', "value": 'PRESS', "ctrl": True, "shift": True, "repeat": True},
{"properties": [("type", 'PREVIOUS_WORD'),("select", True)]}),
("console.move", {"type": 'RIGHT_ARROW', "value": 'PRESS', "ctrl": True, "repeat": True},
{"properties": [("type", 'NEXT_WORD')]}),
("console.move", {"type": 'RIGHT_ARROW', "value": 'PRESS', "ctrl": True, "shift": True, "repeat": True},
{"properties": [("type", 'NEXT_WORD'),("select", True)]}),
("console.move", {"type": 'HOME', "value": 'PRESS'},
{"properties": [("type", 'LINE_BEGIN')]}),
("console.move", {"type": 'HOME', "value": 'PRESS', "shift": True},
{"properties": [("type", 'LINE_BEGIN'),("select", True)]}),
("console.move", {"type": 'END', "value": 'PRESS'},
{"properties": [("type", 'LINE_END')]}),
("console.move", {"type": 'END', "value": 'PRESS', "shift": True},
{"properties": [("type", 'LINE_END'),("select", True)]}),
("wm.context_cycle_int", {"type": 'WHEELUPMOUSE', "value": 'PRESS', "ctrl": True},
{"properties": [("data_path", 'space_data.font_size'), ("reverse", False)]}),
("wm.context_cycle_int", {"type": 'WHEELDOWNMOUSE', "value": 'PRESS', "ctrl": True},
@ -3096,8 +3104,12 @@ def km_console(_params):
{"properties": [("data_path", 'space_data.font_size'), ("reverse", True)]}),
("console.move", {"type": 'LEFT_ARROW', "value": 'PRESS', "repeat": True},
{"properties": [("type", 'PREVIOUS_CHARACTER')]}),
("console.move", {"type": 'LEFT_ARROW', "value": 'PRESS', "repeat": True, "shift": True},
{"properties": [("type", 'PREVIOUS_CHARACTER'),("select", True)]}),
("console.move", {"type": 'RIGHT_ARROW', "value": 'PRESS', "repeat": True},
{"properties": [("type", 'NEXT_CHARACTER')]}),
("console.move", {"type": 'RIGHT_ARROW', "value": 'PRESS', "repeat": True, "shift": True},
{"properties": [("type", 'NEXT_CHARACTER'),("select", True)]}),
("console.history_cycle", {"type": 'UP_ARROW', "value": 'PRESS', "repeat": True},
{"properties": [("reverse", True)]}),
("console.history_cycle", {"type": 'DOWN_ARROW', "value": 'PRESS', "repeat": True},
@ -3121,7 +3133,9 @@ def km_console(_params):
("console.copy_as_script", {"type": 'C', "value": 'PRESS', "shift": True, "ctrl": True}, None),
("console.copy", {"type": 'C', "value": 'PRESS', "ctrl": True}, None),
("console.paste", {"type": 'V', "value": 'PRESS', "ctrl": True, "repeat": True}, None),
("console.copy", {"type": 'DEL', "value": 'PRESS', "shift": True}, {"properties": [("delete", True)]}),
("console.select_set", {"type": 'LEFTMOUSE', "value": 'PRESS'}, None),
("console.select_all", {"type": 'A', "value": 'PRESS', "ctrl": True}, None),
("console.select_word", {"type": 'LEFTMOUSE', "value": 'DOUBLE_CLICK'}, None),
("console.insert", {"type": 'TAB', "value": 'PRESS', "ctrl": True, "repeat": True},
{"properties": [("text", '\t')]}),

View File

@ -1887,12 +1887,20 @@ def km_console(params):
items.extend([
("console.move", {"type": 'LEFT_ARROW', "value": 'PRESS', "ctrl": True, "repeat": True},
{"properties": [("type", 'PREVIOUS_WORD')]}),
("console.move", {"type": 'LEFT_ARROW', "value": 'PRESS', "ctrl": True, "shift": True, "repeat": True},
{"properties": [("type", 'PREVIOUS_WORD'),("select", True)]}),
("console.move", {"type": 'RIGHT_ARROW', "value": 'PRESS', "ctrl": True, "repeat": True},
{"properties": [("type", 'NEXT_WORD')]}),
("console.move", {"type": 'RIGHT_ARROW', "value": 'PRESS', "ctrl": True, "shift": True, "repeat": True},
{"properties": [("type", 'NEXT_WORD'),("select", True)]}),
("console.move", {"type": 'HOME', "value": 'PRESS'},
{"properties": [("type", 'LINE_BEGIN')]}),
("console.move", {"type": 'HOME', "value": 'PRESS', "shift": True},
{"properties": [("type", 'LINE_BEGIN'),("select", True)]}),
("console.move", {"type": 'END', "value": 'PRESS'},
{"properties": [("type", 'LINE_END')]}),
("console.move", {"type": 'END', "value": 'PRESS', "shift": True},
{"properties": [("type", 'LINE_END'),("select", True)]}),
("wm.context_cycle_int", {"type": 'WHEELUPMOUSE', "value": 'PRESS', "ctrl": True},
{"properties": [("data_path", 'space_data.font_size'), ("reverse", False)]}),
("wm.context_cycle_int", {"type": 'WHEELDOWNMOUSE', "value": 'PRESS', "ctrl": True},
@ -1903,8 +1911,12 @@ def km_console(params):
{"properties": [("data_path", 'space_data.font_size'), ("reverse", True)]}),
("console.move", {"type": 'LEFT_ARROW', "value": 'PRESS', "repeat": True},
{"properties": [("type", 'PREVIOUS_CHARACTER')]}),
("console.move", {"type": 'LEFT_ARROW', "value": 'PRESS', "repeat": True, "shift": True},
{"properties": [("type", 'PREVIOUS_CHARACTER'),("select", True)]}),
("console.move", {"type": 'RIGHT_ARROW', "value": 'PRESS', "repeat": True},
{"properties": [("type", 'NEXT_CHARACTER')]}),
("console.move", {"type": 'RIGHT_ARROW', "value": 'PRESS', "repeat": True, "shift": True},
{"properties": [("type", 'NEXT_CHARACTER'),("select", True)]}),
("console.history_cycle", {"type": 'UP_ARROW', "value": 'PRESS', "repeat": True},
{"properties": [("reverse", True)]}),
("console.history_cycle", {"type": 'DOWN_ARROW', "value": 'PRESS', "repeat": True},
@ -1928,7 +1940,9 @@ def km_console(params):
("console.copy_as_script", {"type": 'C', "value": 'PRESS', "shift": True, "ctrl": True}, None),
("console.copy", {"type": 'C', "value": 'PRESS', "ctrl": True}, None),
("console.paste", {"type": 'V', "value": 'PRESS', "ctrl": True, "repeat": True}, None),
("console.copy", {"type": 'DEL', "value": 'PRESS', "shift": True}, {"properties": [("delete", True)]}),
("console.select_set", {"type": 'LEFTMOUSE', "value": 'PRESS'}, None),
("console.select_all", {"type": 'A', "value": 'PRESS', "ctrl": True}, None),
("console.select_word", {"type": 'LEFTMOUSE', "value": 'DOUBLE_CLICK'}, None),
("console.insert", {"type": 'TAB', "value": 'PRESS', "ctrl": True, "repeat": True},
{"properties": [("text", '\t')]}),

View File

@ -92,6 +92,7 @@ class CONSOLE_MT_console(Menu):
layout.separator()
layout.operator("console.copy_as_script", text="Copy as Script")
layout.operator("console.copy", text="Cut").delete = True
layout.operator("console.copy", text="Copy")
layout.operator("console.paste", text="Paste")
@ -124,6 +125,7 @@ class CONSOLE_MT_context_menu(Menu):
layout.separator()
layout.operator("console.copy_as_script", text="Copy as Script")
layout.operator("console.copy", text="Cut").delete = True
layout.operator("console.copy", text="Copy")
layout.operator("console.paste", text="Paste")

View File

@ -217,8 +217,11 @@ static int console_textview_main__internal(SpaceConsole *sc,
console_textview_draw_rect_calc(region, &tvc.draw_rect, &tvc.draw_rect_outer);
/* Nudge right by half a column or so to break selection mid-character. */
const int m_pos[2] = {mval[0] + (int)(sc->lheight * UI_SCALE_FAC * 0.25f), mval[1]};
console_scrollback_prompt_begin(sc, &cl_dummy);
ret = textview_draw(&tvc, do_draw, mval, r_mval_pick_item, r_mval_pick_offset);
ret = textview_draw(&tvc, do_draw, m_pos, r_mval_pick_item, r_mval_pick_offset);
console_scrollback_prompt_end(sc, &cl_dummy);
return ret;

View File

@ -52,6 +52,7 @@ void CONSOLE_OT_history_cycle(struct wmOperatorType *ot);
void CONSOLE_OT_copy(struct wmOperatorType *ot);
void CONSOLE_OT_paste(struct wmOperatorType *ot);
void CONSOLE_OT_select_set(struct wmOperatorType *ot);
void CONSOLE_OT_select_all(struct wmOperatorType *ot);
void CONSOLE_OT_select_word(struct wmOperatorType *ot);
enum { LINE_BEGIN, LINE_END, PREV_CHAR, NEXT_CHAR, PREV_WORD, NEXT_WORD };

View File

@ -101,6 +101,37 @@ static void console_select_update_primary_clipboard(SpaceConsole *sc)
MEM_freeN(buf);
}
/* Delete selected characters in the edit line. */
static int console_delete_editable_selection(SpaceConsole *sc)
{
if (sc->sel_start == sc->sel_end) {
return 0;
}
ConsoleLine *cl = static_cast<ConsoleLine *>(sc->history.last);
if (!cl || sc->sel_start > cl->len) {
sc->sel_start = sc->sel_end;
return 0;
}
int del_start = sc->sel_start;
int del_end = sc->sel_end;
if (del_end > cl->len) {
/* Adjust range to only editable portion. */
del_end = cl->len;
}
const int len = del_end - del_start;
memmove(cl->line + cl->len - del_end, cl->line + cl->len - del_start, del_start);
cl->len -= len;
cl->line[cl->len] = 0;
cl->cursor = cl->len - del_start;
sc->sel_start = sc->sel_end = cl->cursor;
return len;
}
/** \} */
/* so when we type - the view scrolls to the bottom */
@ -361,11 +392,25 @@ static const EnumPropertyItem console_move_type_items[] = {
static int console_move_exec(bContext *C, wmOperator *op)
{
SpaceConsole *sc = CTX_wm_space_console(C);
ConsoleLine *ci = console_history_verify(C);
int type = RNA_enum_get(op->ptr, "type");
bool select = RNA_boolean_get(op->ptr, "select");
bool done = false;
int pos;
int old_pos = ci->cursor;
int pos = 0;
if (!select && sc->sel_start != sc->sel_end) {
/* Clear selection if we are not extending it. */
sc->sel_start = sc->sel_end;
if (sc->sel_end <= ci->len && sc->sel_start <= ci->len) {
/* If selection is in an editable section, do nothing else. */
ED_area_tag_redraw(CTX_wm_area(C));
return OPERATOR_FINISHED;
}
}
switch (type) {
case LINE_BEGIN:
@ -403,6 +448,19 @@ static int console_move_exec(bContext *C, wmOperator *op)
break;
}
if (select) {
if (sc->sel_start == sc->sel_end || sc->sel_start > ci->len || sc->sel_end > ci->len) {
sc->sel_start = ci->len - old_pos;
sc->sel_end = sc->sel_start;
}
if (pos > old_pos) {
sc->sel_start = ci->len - pos;
}
else {
sc->sel_end = ci->len - pos;
}
}
if (done) {
ScrArea *area = CTX_wm_area(C);
ARegion *region = CTX_wm_region(C);
@ -428,6 +486,9 @@ void CONSOLE_OT_move(wmOperatorType *ot)
/* properties */
RNA_def_enum(
ot->srna, "type", console_move_type_items, LINE_BEGIN, "Type", "Where to move cursor to");
PropertyRNA *prop = RNA_def_boolean(
ot->srna, "select", false, "Select", "Whether to select while moving");
RNA_def_property_flag(prop, PROP_SKIP_SAVE);
}
#define TAB_LENGTH 4
@ -463,6 +524,7 @@ static int console_insert_exec(bContext *C, wmOperator *op)
}
if (len != 0) {
console_delete_editable_selection(sc);
console_line_insert(ci, str, len);
}
@ -695,6 +757,14 @@ static int console_delete_exec(bContext *C, wmOperator *op)
return OPERATOR_CANCELLED;
}
/* If there is a selection just delete it and nothing else. */
if (sc->sel_start != sc->sel_end && console_delete_editable_selection(sc) > 0) {
console_textview_update_rect(sc, region);
ED_area_tag_redraw(CTX_wm_area(C));
console_scroll_bottom(region);
return OPERATOR_FINISHED;
}
switch (type) {
case DEL_NEXT_CHAR:
case DEL_NEXT_WORD:
@ -1046,7 +1116,7 @@ void CONSOLE_OT_scrollback_append(wmOperatorType *ot)
"Console output type");
}
static int console_copy_exec(bContext *C, wmOperator * /*op*/)
static int console_copy_exec(bContext *C, wmOperator *op)
{
SpaceConsole *sc = CTX_wm_space_console(C);
char *buf = console_select_to_buffer(sc);
@ -1055,10 +1125,22 @@ static int console_copy_exec(bContext *C, wmOperator * /*op*/)
}
WM_clipboard_text_set(buf, false);
if (RNA_boolean_get(op->ptr, "delete")) {
console_delete_editable_selection(sc);
ED_area_tag_redraw(CTX_wm_area(C));
}
MEM_freeN(buf);
return OPERATOR_FINISHED;
}
static bool console_copy_poll(bContext *C)
{
SpaceConsole *sc = CTX_wm_space_console(C);
return ED_operator_console_active(C) && sc && (sc->sel_start != sc->sel_end);
}
void CONSOLE_OT_copy(wmOperatorType *ot)
{
/* identifiers */
@ -1067,10 +1149,16 @@ void CONSOLE_OT_copy(wmOperatorType *ot)
ot->idname = "CONSOLE_OT_copy";
/* api callbacks */
ot->poll = ED_operator_console_active;
ot->poll = console_copy_poll;
ot->exec = console_copy_exec;
/* properties */
PropertyRNA *prop = RNA_def_boolean(ot->srna,
"delete",
false,
"Delete Selection",
"Whether to delete the selection after copying");
RNA_def_property_flag(prop, PROP_SKIP_SAVE);
}
static int console_paste_exec(bContext *C, wmOperator *op)
@ -1098,6 +1186,7 @@ static int console_paste_exec(bContext *C, wmOperator *op)
WM_operator_name_call(C, "CONSOLE_OT_execute", WM_OP_EXEC_DEFAULT, nullptr, nullptr);
ci = console_history_verify(C);
}
console_delete_editable_selection(sc);
console_line_insert(ci, buf, buf_len);
console_select_offset(sc, buf_len);
} while (*buf_step ? ((void)buf_step++, true) : false);
@ -1138,7 +1227,6 @@ struct SetConsoleCursor {
int sel_init;
};
/* TODO: cursor placement without selection. */
static void console_cursor_set_to_pos(
SpaceConsole *sc, ARegion *region, SetConsoleCursor *scu, const int mval[2], int /*sel*/)
{
@ -1162,6 +1250,19 @@ static void console_cursor_set_to_pos(
else {
sc->sel_start = sc->sel_end = pos;
}
/* Move text cursor to the last selection point. */
ConsoleLine *cl = static_cast<ConsoleLine *>(sc->history.last);
if (cl != NULL) {
if (pos <= cl->len) {
console_line_cursor_set(cl, cl->len - pos);
}
else if (sc->sel_end > cl->len && sc->sel_start < cl->len) {
/* if mixed selection, move cursor to the start. */
console_line_cursor_set(cl, cl->len - sc->sel_start);
}
}
}
static void console_modal_select_apply(bContext *C, wmOperator *op, const wmEvent *event)
@ -1199,9 +1300,17 @@ static void console_cursor_set_exit(bContext *C, wmOperator *op)
static int console_modal_select_invoke(bContext *C, wmOperator *op, const wmEvent *event)
{
SpaceConsole *sc = CTX_wm_space_console(C);
// ARegion *region = CTX_wm_region(C);
SetConsoleCursor *scu;
ConsoleLine *cl = static_cast<ConsoleLine *>(sc->history.last);
if (cl != NULL) {
const int pos = console_char_pick(sc, CTX_wm_region(C), event->mval);
if (pos >= 0 && pos <= cl->len) {
/* Set text cursor immediately. */
console_line_cursor_set(cl, cl->len - pos);
}
}
op->customdata = MEM_callocN(sizeof(SetConsoleCursor), "SetConsoleCursor");
scu = static_cast<SetConsoleCursor *>(op->customdata);
@ -1255,6 +1364,43 @@ void CONSOLE_OT_select_set(wmOperatorType *ot)
ot->poll = ED_operator_console_active;
}
static int console_modal_select_all_invoke(bContext *C,
wmOperator * /* op */,
const wmEvent * /* event */)
{
SpaceConsole *sc = CTX_wm_space_console(C);
int offset = strlen(sc->prompt);
for (ConsoleLine *cl = static_cast<ConsoleLine *>(sc->scrollback.first); cl; cl = cl->next) {
offset += cl->len + 1;
}
ConsoleLine *cl = static_cast<ConsoleLine *>(sc->history.last);
if (cl) {
offset += cl->len + 1;
}
sc->sel_start = 0;
sc->sel_end = offset;
ED_area_tag_redraw(CTX_wm_area(C));
return OPERATOR_FINISHED;
}
void CONSOLE_OT_select_all(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Select All";
ot->idname = "CONSOLE_OT_select_all";
ot->description = "Select all the text";
/* api callbacks */
ot->invoke = console_modal_select_all_invoke;
ot->poll = ED_operator_console_active;
}
static int console_selectword_invoke(bContext *C, wmOperator * /*op*/, const wmEvent *event)
{
SpaceConsole *sc = CTX_wm_space_console(C);
@ -1287,6 +1433,11 @@ static int console_selectword_invoke(bContext *C, wmOperator * /*op*/, const wmE
console_scrollback_prompt_end(sc, &cl_dummy);
ConsoleLine *ci = static_cast<ConsoleLine *>(sc->history.last);
if (ci && sc->sel_start <= ci->len) {
console_line_cursor_set(ci, ci->len - sc->sel_start);
}
if (ret & OPERATOR_FINISHED) {
console_select_update_primary_clipboard(sc);
}

View File

@ -239,6 +239,7 @@ static void console_operatortypes()
WM_operatortype_append(CONSOLE_OT_copy);
WM_operatortype_append(CONSOLE_OT_paste);
WM_operatortype_append(CONSOLE_OT_select_set);
WM_operatortype_append(CONSOLE_OT_select_all);
WM_operatortype_append(CONSOLE_OT_select_word);
}