]> git.rm.cloudns.org Git - xonotic/darkplaces.git/commitdiff
chat: adding history search as in console, displaying results on console is a bit...
authornyov <nyov@nexnode.net>
Mon, 12 Mar 2012 09:10:12 +0000 (10:10 +0100)
committernyov <nyov@nexnode.net>
Mon, 12 Mar 2012 09:10:12 +0000 (10:10 +0100)
keys.c

diff --git a/keys.c b/keys.c
index 377cd19c590453bc585e920aab74dfef29b7d897..76145e6c79992cc3c6f448d5d1c7653888fa160c 100644 (file)
--- a/keys.c
+++ b/keys.c
@@ -306,6 +306,7 @@ static void MsgKey_History_Last(void)
                chat_bufferpos = strlen(chat_buffer);
        }
 }
+
 static void Key_History_Find_Backwards(void)
 {
        int i;
@@ -341,6 +342,41 @@ static void Key_History_Find_Backwards(void)
                }
 }
 
+static void MsgKey_History_Find_Backwards(void)
+{
+       int i;
+       const char *partial = chat_buffer;
+       char vabuf[1024];
+       size_t digits = strlen(va(vabuf, sizeof(vabuf), "%i", HIST_MAXLINES));
+
+       if (chathistory_line == -1) // editing the "new" line
+               strlcpy(chathistory_savedline, chat_buffer, sizeof(chathistory_savedline));
+
+       if (strcmp(chat_buffer, chathistory_searchstring)) // different string? Start a new search
+       {
+               strlcpy(chathistory_searchstring, chat_buffer, sizeof(chathistory_searchstring));
+               i = CONBUFFER_LINES_COUNT(&chathistory) - 1;
+       }
+       else if (chathistory_line == -1)
+               i = CONBUFFER_LINES_COUNT(&chathistory) - 1;
+       else
+               i = chathistory_line - 1;
+
+       if (!*partial)
+               partial = "*";
+       else if (!( strchr(partial, '*') || strchr(partial, '?') )) // no pattern?
+               partial = va(vabuf, sizeof(vabuf), "*%s*", partial);
+
+       for ( ; i >= 0; i--)
+               if (matchpattern_with_separator(ConBuffer_GetLine(&chathistory, i), partial, true, "", false))
+               {
+                       Con_Printf("^2%*i^7 %s\n", (int)digits, i+1, ConBuffer_GetLine(&chathistory, i));
+                       chathistory_line = i;
+                       chathistory_matchfound = true;
+                       return;
+               }
+}
+
 static void Key_History_Find_Forwards(void)
 {
        int i;
@@ -373,6 +409,38 @@ static void Key_History_Find_Forwards(void)
                }
 }
 
+static void MsgKey_History_Find_Forwards(void)
+{
+       int i;
+       const char *partial = chat_buffer;
+       char vabuf[1024];
+       size_t digits = strlen(va(vabuf, sizeof(vabuf), "%i", HIST_MAXLINES));
+
+       if (chathistory_line == -1) // editing the "new" line
+               return;
+
+       if (strcmp(chat_buffer, chathistory_searchstring)) // different string? Start a new search
+       {
+               strlcpy(chathistory_searchstring, chat_buffer, sizeof(chathistory_searchstring));
+               i = 0;
+       }
+       else i = chathistory_line + 1;
+
+       if (!*partial)
+               partial = "*";
+       else if (!( strchr(partial, '*') || strchr(partial, '?') )) // no pattern?
+               partial = va(vabuf, sizeof(vabuf), "*%s*", partial);
+
+       for ( ; i < CONBUFFER_LINES_COUNT(&chathistory); i++)
+               if (matchpattern_with_separator(ConBuffer_GetLine(&chathistory, i), partial, true, "", false))
+               {
+                       Con_Printf("^2%*i^7 %s\n", (int)digits, i+1, ConBuffer_GetLine(&chathistory, i));
+                       chathistory_line = i;
+                       chathistory_matchfound = true;
+                       return;
+               }
+}
+
 static void Key_History_Find_All(void)
 {
        const char *partial = key_line + 1;
@@ -395,6 +463,28 @@ static void Key_History_Find_All(void)
        Con_Printf("%i result%s\n\n", count, (count != 1) ? "s" : "");
 }
 
+static void MsgKey_History_Find_All(void)
+{
+       const char *partial = chat_buffer;
+       int i, count = 0;
+       char vabuf[1024];
+       size_t digits = strlen(va(vabuf, sizeof(vabuf), "%i", HIST_MAXLINES));
+       Con_Printf("Chathistory containing \"%s\":\n", chat_buffer);
+
+       if (!*partial)
+               partial = "*";
+       else if (!( strchr(partial, '*') || strchr(partial, '?') )) // no pattern?
+               partial = va(vabuf, sizeof(vabuf), "*%s*", partial);
+
+       for (i=0; i<CONBUFFER_LINES_COUNT(&chathistory); i++)
+               if (matchpattern_with_separator(ConBuffer_GetLine(&chathistory, i), partial, true, "", false))
+               {
+                       Con_Printf("%s%*i^7 %s\n", (i == chathistory_line) ? "^2" : "^3", (int)digits, i+1, ConBuffer_GetLine(&chathistory, i));
+                       count++;
+               }
+       Con_Printf("%i result%s\n\n", count, (count != 1) ? "s" : "");
+}
+
 static void Key_History_f(void)
 {
        char *errchar = NULL;
@@ -1616,6 +1706,38 @@ Key_Message (int key, int unicode)
                return;
        }
 
+       if (keydown[K_CTRL]  && keydown[K_ALT])
+       {
+               // prints all the matching commands
+               if (key == 'f')
+               {
+                       MsgKey_History_Find_All();
+                       return;
+               }
+               // Search forwards/backwards, pointing the history's index to the
+               // matching command but without fetching it to let one continue the search.
+               // To fetch it, it suffices to just press UP or DOWN.
+               if (key == 'r')
+               {
+                       if (keydown[K_SHIFT])
+                               MsgKey_History_Find_Forwards();
+                       else
+                               MsgKey_History_Find_Backwards();
+                       return;
+               }
+               // go to the last/first command of the history
+               if (key == ',')
+               {
+                       MsgKey_History_First();
+                       return;
+               }
+               if (key == '.')
+               {
+                       MsgKey_History_Last();
+                       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