+extern cvar_t con_textsize;
+
+
+static void Key_History_Init(void)
+{
+ qfile_t *historyfile;
+ ConBuffer_Init(&history, HIST_TEXTSIZE, HIST_MAXLINES, zonemempool);
+
+ historyfile = FS_OpenRealFile("darkplaces_history.txt", "rb", false); // rb to handle unix line endings on windows too
+ if(historyfile)
+ {
+ char buf[MAX_INPUTLINE];
+ int bufpos;
+ int c;
+
+ bufpos = 0;
+ for(;;)
+ {
+ c = FS_Getc(historyfile);
+ if(c < 0 || c == 0 || c == '\r' || c == '\n')
+ {
+ if(bufpos > 0)
+ {
+ buf[bufpos] = 0;
+ ConBuffer_AddLine(&history, buf, bufpos, 0);
+ bufpos = 0;
+ }
+ if(c < 0)
+ break;
+ }
+ else
+ {
+ if(bufpos < MAX_INPUTLINE - 1)
+ buf[bufpos++] = c;
+ }
+ }
+
+ FS_Close(historyfile);
+ }
+
+ history_line = -1;
+}
+
+static void Key_History_Shutdown(void)
+{
+ // TODO write history to a file
+
+ qfile_t *historyfile = FS_OpenRealFile("darkplaces_history.txt", "w", false);
+ if(historyfile)
+ {
+ int i;
+ for(i = 0; i < CONBUFFER_LINES_COUNT(&history); ++i)
+ FS_Printf(historyfile, "%s\n", ConBuffer_GetLine(&history, i));
+ FS_Close(historyfile);
+ }
+
+ ConBuffer_Shutdown(&history);
+}
+
+static void Key_History_Push(void)
+{
+ if(key_line[1]) // empty?
+ if(strcmp(key_line, "]quit")) // putting these into the history just sucks
+ if(strncmp(key_line, "]quit ", 6)) // putting these into the history just sucks
+ ConBuffer_AddLine(&history, key_line + 1, strlen(key_line) - 1, 0);
+ Con_Printf("%s\n", key_line); // don't mark empty lines as history
+ history_line = -1;
+}
+
+static void Key_History_Up(void)
+{
+ if(history_line == -1) // editing the "new" line
+ strlcpy(history_savedline, key_line + 1, sizeof(history_savedline));
+
+ if(history_line == -1)
+ {
+ history_line = CONBUFFER_LINES_COUNT(&history) - 1;
+ if(history_line != -1)
+ {
+ strlcpy(key_line + 1, ConBuffer_GetLine(&history, history_line), sizeof(key_line) - 1);
+ key_linepos = strlen(key_line);
+ }
+ }
+ else if(history_line > 0)
+ {
+ --history_line; // this also does -1 -> 0, so it is good
+ strlcpy(key_line + 1, ConBuffer_GetLine(&history, history_line), sizeof(key_line) - 1);
+ key_linepos = strlen(key_line);
+ }
+}
+
+static void Key_History_Down(void)
+{
+ if(history_line == -1) // editing the "new" line
+ return;
+
+ if(history_line < CONBUFFER_LINES_COUNT(&history) - 1)
+ {
+ ++history_line;
+ strlcpy(key_line + 1, ConBuffer_GetLine(&history, history_line), sizeof(key_line) - 1);
+ }
+ else
+ {
+ history_line = -1;
+ strlcpy(key_line + 1, history_savedline, sizeof(key_line) - 1);
+ }
+
+ key_linepos = strlen(key_line);
+}