]> git.rm.cloudns.org Git - xonotic/darkplaces.git/commitdiff
chat: UPARROW and DOWNARROW to navigate chat history
authornyov <nyov@nexnode.net>
Fri, 16 Dec 2011 01:44:38 +0000 (02:44 +0100)
committernyov <nyov@nexnode.net>
Fri, 16 Dec 2011 01:50:38 +0000 (02:50 +0100)
keys.c
quakedef.h

diff --git a/keys.c b/keys.c
index fa77ce9e1eadfccd5e5ceefabc562d3e6711a074..a045d3ab8a2392b41fdcca13f9cd65ebf4652214 100644 (file)
--- a/keys.c
+++ b/keys.c
@@ -44,6 +44,12 @@ char         history_searchstring[MAX_INPUTLINE];
 qboolean       history_matchfound = false;
 conbuffer_t history;
 
+int            msghistory_line;
+char           msghistory_savedline[MAX_INPUTLINE];
+char           msghistory_searchstring[MAX_INPUTLINE];
+qboolean       msghistory_matchfound = false;
+conbuffer_t    msghistory;
+
 extern cvar_t  con_textsize;
 
 
@@ -313,6 +319,112 @@ static void Key_History_f(void)
        Con_Printf("\n");
 }
 
+// nyov: chat buffer history
+// (almost copy of history buffer functions, should merge them)
+static void MsgKey_History_Init(void)
+{
+       ConBuffer_Init(&msghistory, CHATHIST_TEXTSIZE, CHATHIST_MAXLINES, zonemempool);
+       msghistory_line = -1;
+}
+
+static void MsgKey_History_Shutdown(void)
+{
+       ConBuffer_Shutdown(&msghistory);
+}
+
+static void MsgKey_History_Push(void)
+{
+       if(chat_buffer[0]) // empty?
+               ConBuffer_AddLine(&msghistory, chat_buffer, strlen(chat_buffer), 0);
+       msghistory_line = -1;
+       if (msghistory_matchfound)
+               msghistory_matchfound = false;
+}
+
+static qboolean MsgKey_History_Get_foundCommand(void)
+{
+       if (!msghistory_matchfound)
+               return false;
+       strlcpy(chat_buffer, ConBuffer_GetLine(&msghistory, msghistory_line), sizeof(chat_buffer));
+       chat_bufferpos = strlen(chat_buffer);
+       msghistory_matchfound = false;
+       return true;
+}
+
+static void MsgKey_History_Up(void)
+{
+       if(msghistory_line == -1) // editing the "new" line
+               strlcpy(msghistory_savedline, chat_buffer, sizeof(msghistory_savedline));
+
+       if (MsgKey_History_Get_foundCommand())
+               return;
+
+       if(msghistory_line == -1)
+       {
+               msghistory_line = CONBUFFER_LINES_COUNT(&msghistory) - 1;
+               if(msghistory_line != -1)
+               {
+                       strlcpy(chat_buffer, ConBuffer_GetLine(&msghistory, msghistory_line), sizeof(chat_buffer));
+                       chat_bufferpos = strlen(chat_buffer);
+               }
+       }
+       else if(msghistory_line > 0)
+       {
+               --msghistory_line; // this also does -1 -> 0, so it is good
+               strlcpy(chat_buffer, ConBuffer_GetLine(&msghistory, msghistory_line), sizeof(chat_buffer));
+               chat_bufferpos = strlen(chat_buffer);
+       }
+}
+
+static void MsgKey_History_Down(void)
+{
+       if(msghistory_line == -1) // editing the "new" line
+               return;
+
+       if (MsgKey_History_Get_foundCommand())
+               return;
+
+       if(msghistory_line < CONBUFFER_LINES_COUNT(&msghistory) - 1)
+       {
+               ++msghistory_line;
+               strlcpy(chat_buffer, ConBuffer_GetLine(&msghistory, msghistory_line), sizeof(chat_buffer));
+       }
+       else
+       {
+               msghistory_line = -1;
+               strlcpy(chat_buffer, msghistory_savedline, sizeof(chat_buffer));
+       }
+
+       chat_bufferpos = strlen(chat_buffer);
+}
+
+static void MsgKey_History_First(void)
+{
+       if(msghistory_line == -1) // editing the "new" line
+               strlcpy(msghistory_savedline, chat_buffer, sizeof(msghistory_savedline));
+
+       if (CONBUFFER_LINES_COUNT(&msghistory) > 0)
+       {
+               msghistory_line = 0;
+               strlcpy(chat_buffer, ConBuffer_GetLine(&msghistory, msghistory_line), sizeof(chat_buffer));
+               chat_bufferpos = strlen(chat_buffer);
+       }
+}
+
+static void MsgKey_History_Last(void)
+{
+       if(msghistory_line == -1) // editing the "new" line
+               strlcpy(msghistory_savedline, chat_buffer, sizeof(msghistory_savedline));
+
+       if (CONBUFFER_LINES_COUNT(&msghistory) > 0)
+       {
+               msghistory_line = CONBUFFER_LINES_COUNT(&msghistory) - 1;
+               strlcpy(chat_buffer, ConBuffer_GetLine(&msghistory, msghistory_line), sizeof(chat_buffer));
+               chat_bufferpos = strlen(chat_buffer);
+       }
+}
+// end chat buffer history
+
 static int     key_bmap, key_bmap2;
 static unsigned char keydown[MAX_KEYS];        // 0 = up, 1 = down, 2 = repeating
 
@@ -1263,7 +1375,10 @@ Key_Message (int key, int unicode)
                if(chat_mode < 0)
                        Cmd_ExecuteString(chat_buffer, src_command, true); // not Cbuf_AddText to allow semiclons in args; however, this allows no variables then. Use aliases!
                else
+               {
                        Cmd_ForwardStringToServer(va(vabuf, sizeof(vabuf), "%s %s", chat_mode ? "say_team" : "say ", chat_buffer));
+                       MsgKey_History_Push();
+               }
 
                key_dest = key_game;
                chat_bufferpos = 0;
@@ -1488,6 +1603,18 @@ Key_Message (int key, int unicode)
 
        // End Advanced Console Editing
 
+       if (key == K_UPARROW || key == K_KP_UPARROW || (key == 'p' && keydown[K_CTRL] && keydown[K_ALT]))
+       {
+               MsgKey_History_Up();
+               return;
+       }
+
+       if (key == K_DOWNARROW || key == K_KP_DOWNARROW || (key == 'n' && keydown[K_CTRL] && keydown[K_ALT]))
+       {
+               MsgKey_History_Down();
+               return;
+       }
+
        if (key == K_HOME /*|| key == K_KP_HOME*/ || (key == 'a' && keydown[K_CTRL] && keydown[K_ALT]))
        {
                // TODO +CTRL for MsgKey_History_Top() or something
@@ -1904,6 +2031,8 @@ Key_Init (void)
        key_line[1] = 0;
        key_linepos = 1;
 
+       MsgKey_History_Init();
+
 //
 // register our functions
 //
@@ -1926,6 +2055,7 @@ void
 Key_Shutdown (void)
 {
        Key_History_Shutdown();
+       MsgKey_History_Shutdown();
 }
 
 const char *Key_GetBind (int key, int bindmap)
index 65f241590335bffd34d37b7bbe89ae4193a9872d..c6c686147fa054dca581b5bc77c9de245e720708 100644 (file)
@@ -55,6 +55,8 @@ extern char engineversion[128];
 #define        CON_MAXLINES                    256
 #define        HIST_TEXTSIZE                   2048
 #define        HIST_MAXLINES                   16
+#define        CHATHIST_TEXTSIZE               1024
+#define        CHATHIST_MAXLINES               16
 #define        MAX_ALIAS_NAME                  32
 #define        CMDBUFSIZE                              131072
 #define        MAX_ARGS                                80
@@ -124,6 +126,8 @@ extern char engineversion[128];
 #define        CON_MAXLINES                    16384 ///< max scrollback buffer lines in console
 #define        HIST_TEXTSIZE                   262144 ///< max command history buffer characters in console
 #define        HIST_MAXLINES                   4096 ///< max command history buffer lines in console
+#define        CHATHIST_TEXTSIZE               16384 ///< max history buffer characters in chat typing buffer
+#define        CHATHIST_MAXLINES               256 ///< max history buffer lines in chat typing buffer
 #define        MAX_ALIAS_NAME                  32
 #define        CMDBUFSIZE                              655360 ///< maximum script size that can be loaded by the exec command (8192 in Quake)
 #define        MAX_ARGS                                80 ///< maximum number of parameters to a console command or alias