diff --git a/source/blender/editors/space_console/console_ops.cc b/source/blender/editors/space_console/console_ops.cc index ab4dd0c9b84..6c53fa22f40 100644 --- a/source/blender/editors/space_console/console_ops.cc +++ b/source/blender/editors/space_console/console_ops.cc @@ -183,21 +183,6 @@ static void console_scrollback_limit(SpaceConsole *sc) } } -static ConsoleLine *console_history_find(SpaceConsole *sc, const char *str, ConsoleLine *cl_ignore) -{ - LISTBASE_FOREACH_BACKWARD (ConsoleLine *, cl, &sc->history) { - if (cl == cl_ignore) { - continue; - } - - if (STREQ(str, cl->line)) { - return cl; - } - } - - return nullptr; -} - /* return 0 if no change made, clamps the range */ static bool console_line_cursor_set(ConsoleLine *cl, int cursor) { @@ -934,37 +919,54 @@ static int console_history_cycle_exec(bContext *C, wmOperator *op) const bool reverse = RNA_boolean_get(op->ptr, "reverse"); /* assumes down, reverse is up */ int prev_len = ci->len; - /* keep a copy of the line above so when history is cycled - * this is the only function that needs to know about the double-up */ - if (ci->prev) { - ConsoleLine *ci_prev = (ConsoleLine *)ci->prev; - - if (STREQ(ci->line, ci_prev->line)) { - console_history_free(sc, ci_prev); + int old_index = sc->history_index; + int new_index; + if (reverse) { + if (old_index <= 0) { + new_index = 1; + } + else { + new_index = old_index + 1; } - } - - if (reverse) { /* last item in history */ - ci = static_cast(sc->history.last); - BLI_remlink(&sc->history, ci); - BLI_addhead(&sc->history, ci); } else { - ci = static_cast(sc->history.first); - BLI_remlink(&sc->history, ci); - BLI_addtail(&sc->history, ci); - } - - { /* add a duplicate of the new arg and remove all other instances */ - ConsoleLine *cl; - while ((cl = console_history_find(sc, ci->line, ci))) { - console_history_free(sc, cl); + if (old_index <= 0) { /* Down-arrow after exec. */ + new_index = -old_index; + } + else { + new_index = old_index - 1; } - - console_history_add(sc, (ConsoleLine *)sc->history.last); } - ci = static_cast(sc->history.last); + /* Find the history item. */ + ConsoleLine *ci_prev = ci; + if (old_index > 0) { + /* Skip a previous copy of history item. */ + if (ci_prev->prev) { + ci_prev = ci_prev->prev; + } + else { /* Just in case the duplicate item got deleted. */ + old_index = 0; + } + } + for (int i = 0; i < new_index; i++) { + if (!ci_prev->prev) { + new_index = i; + break; + } + ci_prev = ci_prev->prev; + } + + sc->history_index = new_index; + + if (old_index > 0) { /* Remove old copy. */ + console_history_free(sc, ci); + ci = ci_prev; + } + if (new_index > 0) { /* Copy history item to the end. */ + ci = console_history_add(sc, ci_prev); + } + console_select_offset(sc, ci->len - prev_len); /* could be wrapped so update scroll rect */ @@ -1005,13 +1007,23 @@ static int console_history_append_exec(bContext *C, wmOperator *op) const bool rem_dupes = RNA_boolean_get(op->ptr, "remove_duplicates"); int prev_len = ci->len; - if (rem_dupes) { - ConsoleLine *cl; - - while ((cl = console_history_find(sc, ci->line, ci))) { + if (sc->history_index > 0) { + /* Keep the copy of history item, remove the saved "history 0". */ + ConsoleLine *cl = ci->prev; + if (cl) { console_history_free(sc, cl); } + /* Negative number makes down-arrow go to same item as before. */ + sc->history_index = -sc->history_index; + } + if (rem_dupes) { + /* Remove a repeated command. */ + ConsoleLine *cl = ci->prev; + if (cl && STREQ(cl->line, ci->line)) { + console_history_free(sc, cl); + } + /* Remove blank command. */ if (STREQ(str, ci->line)) { MEM_freeN(str); return OPERATOR_FINISHED; diff --git a/source/blender/makesdna/DNA_space_types.h b/source/blender/makesdna/DNA_space_types.h index 2f9b3b1d01e..8c93194c352 100644 --- a/source/blender/makesdna/DNA_space_types.h +++ b/source/blender/makesdna/DNA_space_types.h @@ -1697,9 +1697,7 @@ typedef struct SpaceConsole { char _pad0[6]; /* End 'SpaceLink' header. */ - /* space vars */ - int lheight; - char _pad[4]; + /* Space variables. */ /** ConsoleLine; output. */ ListBase scrollback; @@ -1709,6 +1707,11 @@ typedef struct SpaceConsole { /** Multiple consoles are possible, not just python. */ char language[32]; + int lheight; + + /** Index into history of most recent up/down arrow keys. */ + int history_index; + /** Selection offset in bytes. */ int sel_start; int sel_end;