+static void Key_History_First(void)
+{
+ if(history_line == -1) // editing the "new" line
+ strlcpy(history_savedline, key_line + 1, sizeof(history_savedline));
+
+ if (CONBUFFER_LINES_COUNT(&history) > 0)
+ {
+ history_line = 0;
+ strlcpy(key_line + 1, ConBuffer_GetLine(&history, history_line), sizeof(key_line) - 1);
+ key_linepos = strlen(key_line);
+ }
+}
+
+static void Key_History_Last(void)
+{
+ if(history_line == -1) // editing the "new" line
+ strlcpy(history_savedline, key_line + 1, sizeof(history_savedline));
+
+ if (CONBUFFER_LINES_COUNT(&history) > 0)
+ {
+ history_line = CONBUFFER_LINES_COUNT(&history) - 1;
+ strlcpy(key_line + 1, ConBuffer_GetLine(&history, history_line), sizeof(key_line) - 1);
+ key_linepos = strlen(key_line);
+ }
+}
+
+static void Key_History_Find_Backwards(void)
+{
+ int i;
+ const char *partial = key_line + 1;
+ size_t digits = strlen(va("%i", HIST_MAXLINES));
+
+ if (history_line == -1) // editing the "new" line
+ strlcpy(history_savedline, key_line + 1, sizeof(history_savedline));
+
+ if (strcmp(key_line + 1, history_searchstring)) // different string? Start a new search
+ {
+ strlcpy(history_searchstring, key_line + 1, sizeof(history_searchstring));
+ i = CONBUFFER_LINES_COUNT(&history) - 1;
+ }
+ else if (history_line == -1)
+ i = CONBUFFER_LINES_COUNT(&history) - 1;
+ else
+ i = history_line - 1;
+
+ if (!*partial)
+ partial = "*";
+ else if (!( strchr(partial, '*') || strchr(partial, '?') )) // no pattern?
+ partial = va("*%s*", partial);
+
+ for ( ; i >= 0; i--)
+ if (matchpattern_with_separator(ConBuffer_GetLine(&history, i), partial, true, "", false))
+ {
+ Con_Printf("^2%*i^7 %s\n", (int)digits, i+1, ConBuffer_GetLine(&history, i));
+ history_line = i;
+ history_matchfound = true;
+ return;
+ }
+}
+
+static void Key_History_Find_Forwards(void)
+{
+ int i;
+ const char *partial = key_line + 1;
+ size_t digits = strlen(va("%i", HIST_MAXLINES));
+
+ if (history_line == -1) // editing the "new" line
+ return;
+
+ if (strcmp(key_line + 1, history_searchstring)) // different string? Start a new search
+ {
+ strlcpy(history_searchstring, key_line + 1, sizeof(history_searchstring));
+ i = 0;
+ }
+ else i = history_line + 1;
+
+ if (!*partial)
+ partial = "*";
+ else if (!( strchr(partial, '*') || strchr(partial, '?') )) // no pattern?
+ partial = va("*%s*", partial);
+
+ for ( ; i < CONBUFFER_LINES_COUNT(&history); i++)
+ if (matchpattern_with_separator(ConBuffer_GetLine(&history, i), partial, true, "", false))
+ {
+ Con_Printf("^2%*i^7 %s\n", (int)digits, i+1, ConBuffer_GetLine(&history, i));
+ history_line = i;
+ history_matchfound = true;
+ return;
+ }
+}
+
+static void Key_History_Find_All(void)
+{
+ const char *partial = key_line + 1;
+ int i, count = 0;
+ size_t digits = strlen(va("%i", HIST_MAXLINES));
+ Con_Printf("History commands containing \"%s\":\n", key_line + 1);
+
+ if (!*partial)
+ partial = "*";
+ else if (!( strchr(partial, '*') || strchr(partial, '?') )) // no pattern?
+ partial = va("*%s*", partial);
+
+ for (i=0; i<CONBUFFER_LINES_COUNT(&history); i++)
+ if (matchpattern_with_separator(ConBuffer_GetLine(&history, i), partial, true, "", false))
+ {
+ Con_Printf("%s%*i^7 %s\n", (i == history_line) ? "^2" : "^3", (int)digits, i+1, ConBuffer_GetLine(&history, i));
+ count++;
+ }
+ Con_Printf("%i result%s\n\n", count, (count != 1) ? "s" : "");
+}
+
+static void Key_History_f(void)
+{
+ char *errchar = NULL;
+ int i = 0;
+ size_t digits = strlen(va("%i", HIST_MAXLINES));
+
+ if (Cmd_Argc () > 1)
+ {
+ if (!strcmp(Cmd_Argv (1), "-c"))
+ {
+ ConBuffer_Clear(&history);
+ return;
+ }
+ i = strtol(Cmd_Argv (1), &errchar, 0);
+ if ((i < 0) || (i > CONBUFFER_LINES_COUNT(&history)) || (errchar && *errchar))
+ i = 0;
+ else
+ i = CONBUFFER_LINES_COUNT(&history) - i;
+ }
+
+ for ( ; i<CONBUFFER_LINES_COUNT(&history); i++)
+ Con_Printf("^3%*i^7 %s\n", (int)digits, i+1, ConBuffer_GetLine(&history, i));
+ Con_Printf("\n");
+}
+