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 217 additions and 20 deletions

View File

@ -3136,12 +3136,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},
@ -3152,8 +3160,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},
@ -3176,8 +3188,10 @@ def km_console(_params):
{"properties": [("interactive", True)]}),
("console.copy_as_script", {"type": 'C', "value": 'PRESS', "shift": True, "ctrl": True}, None),
("console.copy", {"type": 'C', "value": 'PRESS', "ctrl": True}, None),
("console.copy", {"type": 'X', "value": 'PRESS', "ctrl": True}, {"properties": [("delete", True)]}),
("console.paste", {"type": 'V', "value": 'PRESS', "ctrl": True, "repeat": True}, None),
("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

@ -1894,12 +1894,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},
@ -1910,8 +1918,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},
@ -1934,8 +1946,10 @@ def km_console(params):
{"properties": [("interactive", True)]}),
("console.copy_as_script", {"type": 'C', "value": 'PRESS', "shift": True, "ctrl": True}, None),
("console.copy", {"type": 'C', "value": 'PRESS', "ctrl": True}, None),
("console.copy", {"type": 'X', "value": 'PRESS', "ctrl": True}, {"properties": [("delete", True)]}),
("console.paste", {"type": 'V', "value": 'PRESS', "ctrl": True, "repeat": True}, None),
("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,15 @@ 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 to break selection mid-character. */
int m_pos[2] = {mval[0], mval[1]};
/* Mouse position is initialized with max int. */
if (m_pos[0] != INT_MAX) {
m_pos[0] += tvc.lheight / 4;
}
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(wmOperatorType *ot);
void CONSOLE_OT_copy(wmOperatorType *ot);
void CONSOLE_OT_paste(wmOperatorType *ot);
void CONSOLE_OT_select_set(wmOperatorType *ot);
void CONSOLE_OT_select_all(wmOperatorType *ot);
void CONSOLE_OT_select_word(wmOperatorType *ot);
enum { LINE_BEGIN, LINE_END, PREV_CHAR, NEXT_CHAR, PREV_WORD, NEXT_WORD };

View File

@ -100,6 +100,41 @@ 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;
}
if (sc->sel_start < 0) {
sc->sel_start = 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 */
@ -358,11 +393,20 @@ 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;
}
switch (type) {
case LINE_BEGIN:
@ -400,6 +444,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);
@ -425,6 +482,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
@ -460,6 +520,7 @@ static int console_insert_exec(bContext *C, wmOperator *op)
}
if (len != 0) {
console_delete_editable_selection(sc);
console_line_insert(ci, str, len);
}
@ -692,6 +753,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:
@ -1043,7 +1112,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);
@ -1052,10 +1121,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 */
@ -1064,10 +1145,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)
@ -1095,6 +1182,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);
@ -1135,12 +1223,13 @@ 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*/)
static void console_cursor_set_to_pos(SpaceConsole *sc,
ARegion *region,
SetConsoleCursor *scu,
const wmEvent *event)
{
int pos;
pos = console_char_pick(sc, region, mval);
int pos = console_char_pick(sc, region, event->mval);
bool dragging = event->type == MOUSEMOVE;
if (scu->sel_init == INT_MAX) {
scu->sel_init = pos;
@ -1159,6 +1248,22 @@ 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 (dragging && sc->sel_end > cl->len && pos <= cl->len) {
/* Do not move cursor while dragging into the editable area. */
}
else if (pos <= cl->len) {
console_line_cursor_set(cl, cl->len - pos);
}
else if (pos > cl->len && sc->sel_start < cl->len) {
/* Dragging out of editable area, move cursor to start of selection. */
console_line_cursor_set(cl, cl->len - sc->sel_start);
}
}
}
static void console_modal_select_apply(bContext *C, wmOperator *op, const wmEvent *event)
@ -1166,16 +1271,9 @@ static void console_modal_select_apply(bContext *C, wmOperator *op, const wmEven
SpaceConsole *sc = CTX_wm_space_console(C);
ARegion *region = CTX_wm_region(C);
SetConsoleCursor *scu = static_cast<SetConsoleCursor *>(op->customdata);
int mval[2];
int sel_prev[2];
int sel_prev[2] = {sc->sel_start, sc->sel_end};
mval[0] = event->mval[0];
mval[1] = event->mval[1];
sel_prev[0] = sc->sel_start;
sel_prev[1] = sc->sel_end;
console_cursor_set_to_pos(sc, region, scu, mval, true);
console_cursor_set_to_pos(sc, region, scu, event);
/* only redraw if the selection changed */
if (sel_prev[0] != sc->sel_start || sel_prev[1] != sc->sel_end) {
@ -1196,9 +1294,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);
@ -1216,11 +1322,21 @@ static int console_modal_select_invoke(bContext *C, wmOperator *op, const wmEven
static int console_modal_select(bContext *C, wmOperator *op, const wmEvent *event)
{
/* Move text cursor to the last selection point. */
SpaceConsole *sc = CTX_wm_space_console(C);
ConsoleLine *cl = static_cast<ConsoleLine *>(sc->history.last);
switch (event->type) {
case LEFTMOUSE:
case MIDDLEMOUSE:
case RIGHTMOUSE:
if (event->val == KM_RELEASE) {
if (event->val == KM_PRESS) {
console_modal_select_apply(C, op, event);
break;
}
else if (event->val == KM_RELEASE) {
console_modal_select_apply(C, op, event);
ED_area_tag_redraw(CTX_wm_area(C));
console_cursor_set_exit(C, op);
return OPERATOR_FINISHED;
}
@ -1252,6 +1368,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);
@ -1284,6 +1437,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);
}