]> git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - keys.c
fix errors and warnings in MSVC when compiling as C++
[xonotic/darkplaces.git] / keys.c
diff --git a/keys.c b/keys.c
index bfe77d26a0ced1309d2a7e7e1bfa975a3b9a9a2b..879944f9ef9ba004ad4bd4626c54f68259626e50 100644 (file)
--- a/keys.c
+++ b/keys.c
@@ -41,7 +41,10 @@ conbuffer_t history;
 #define HIST_TEXTSIZE 262144
 #define HIST_MAXLINES 4096
 
-static void Key_History_Init()
+extern cvar_t  con_textsize;
+
+
+static void Key_History_Init(void)
 {
        qfile_t *historyfile;
        ConBuffer_Init(&history, HIST_TEXTSIZE, HIST_MAXLINES, zonemempool);
@@ -81,7 +84,7 @@ static void Key_History_Init()
        history_line = -1;
 }
 
-static void Key_History_Shutdown()
+static void Key_History_Shutdown(void)
 {
        // TODO write history to a file
 
@@ -97,7 +100,7 @@ static void Key_History_Shutdown()
        ConBuffer_Shutdown(&history);
 }
 
-static void Key_History_Push()
+static void Key_History_Push(void)
 {
        if(key_line[1]) // empty?
        if(strcmp(key_line, "]quit")) // putting these into the history just sucks
@@ -107,7 +110,7 @@ static void Key_History_Push()
        history_line = -1;
 }
 
-static void Key_History_Up()
+static void Key_History_Up(void)
 {
        if(history_line == -1) // editing the "new" line
                strlcpy(history_savedline, key_line + 1, sizeof(history_savedline));
@@ -129,7 +132,7 @@ static void Key_History_Up()
        }
 }
 
-static void Key_History_Down()
+static void Key_History_Down(void)
 {
        if(history_line == -1) // editing the "new" line
                return;
@@ -392,7 +395,7 @@ Key_Console (int key, int ascii)
                break;
        }
 
-       if ((toupper(key) == 'V' && keydown[K_CTRL]) || ((key == K_INS || key == K_KP_INS) && keydown[K_SHIFT]))
+       if ((key == 'v' && keydown[K_CTRL]) || ((key == K_INS || key == K_KP_INS) && keydown[K_SHIFT]))
        {
                char *cbd, *p;
                if ((cbd = Sys_GetClipboardData()) != 0)
@@ -402,11 +405,13 @@ Key_Console (int key, int ascii)
                        p = cbd;
                        while (*p)
                        {
-                               if (*p == '\n' || *p == '\r' || *p == '\b')
+                               if (*p == '\r' && *(p+1) == '\n')
                                {
-                                       *p++ = 0;
-                                       break;
+                                       *p++ = ';';
+                                       *p++ = ' ';
                                }
+                               else if (*p == '\n' || *p == '\r' || *p == '\b')
+                                       *p++ = ';';
                                p++;
                        }
 #else
@@ -435,13 +440,29 @@ Key_Console (int key, int ascii)
                return;
        }
 
-       if (key == 'l')
+       if (key == 'l' && keydown[K_CTRL])
        {
-               if (keydown[K_CTRL])
-               {
-                       Cbuf_AddText ("clear\n");
-                       return;
-               }
+               Cbuf_AddText ("clear\n");
+               return;
+       }
+
+       if (key == 'u' && keydown[K_CTRL]) // like vi/readline ^u: delete currently edited line
+       {
+               // clear line
+               key_line[0] = ']';
+               key_line[1] = 0;
+               key_linepos = 1;
+               return;
+       }
+
+       if (key == 'q' && keydown[K_CTRL]) // like zsh ^q: push line to history, don't execute, and clear
+       {
+               // clear line
+               Key_History_Push();
+               key_line[0] = ']';
+               key_line[1] = 0;
+               key_linepos = 1;
+               return;
        }
 
        if (key == K_ENTER || key == K_KP_ENTER)
@@ -460,6 +481,57 @@ Key_Console (int key, int ascii)
 
        if (key == K_TAB)
        {
+               if(keydown[K_CTRL]) // append to the cvar its value
+               {
+                       int             cvar_len, cvar_str_len, chars_to_move;
+                       char    k;
+                       char    cvar[MAX_INPUTLINE];
+                       const char *cvar_str;
+                       
+                       // go to the start of the variable
+                       while(--key_linepos)
+                       {
+                               k = key_line[key_linepos];
+                               if(k == '\"' || k == ';' || k == ' ' || k == '\'')
+                                       break;
+                       }
+                       key_linepos++;
+                       
+                       // save the variable name in cvar
+                       for(cvar_len=0; (k = key_line[key_linepos + cvar_len]) != 0; cvar_len++)
+                       {
+                               if(k == '\"' || k == ';' || k == ' ' || k == '\'')
+                                       break;
+                               cvar[cvar_len] = k;
+                       }
+                       if (cvar_len==0)
+                               return;
+                       cvar[cvar_len] = 0;
+                       
+                       // go to the end of the cvar
+                       key_linepos += cvar_len;
+                       
+                       // save the content of the variable in cvar_str
+                       cvar_str = Cvar_VariableString(cvar);
+                       cvar_str_len = strlen(cvar_str);
+                       if (cvar_str_len==0)
+                               return;
+                       
+                       // insert space and cvar_str in key_line
+                       chars_to_move = strlen(&key_line[key_linepos]);
+                       if (key_linepos + 1 + cvar_str_len + chars_to_move < MAX_INPUTLINE)
+                       {
+                               if (chars_to_move)
+                                       memmove(&key_line[key_linepos + 1 + cvar_str_len], &key_line[key_linepos], chars_to_move);
+                               key_line[key_linepos++] = ' ';
+                               memcpy(&key_line[key_linepos], cvar_str, cvar_str_len);
+                               key_linepos += cvar_str_len;
+                               key_line[key_linepos + chars_to_move] = 0;
+                       }
+                       else
+                               Con_Printf("Couldn't append cvar value, edit line too long.\n");
+                       return;
+               }
                // Enhanced command completion
                // by EvilTypeGuy eviltypeguy@qeradiant.com
                // Thanks to Fett, Taniwha
@@ -482,6 +554,15 @@ Key_Console (int key, int ascii)
                        int             pos;
                        char    k;
                        pos = key_linepos-1;
+
+                       if(pos) // skip all "; ' after the word
+                               while(--pos)
+                               {
+                                       k = key_line[pos];
+                                       if (!(k == '\"' || k == ';' || k == ' ' || k == '\''))
+                                               break;
+                               }
+
                        if(pos)
                                while(--pos)
                                {
@@ -503,6 +584,8 @@ Key_Console (int key, int ascii)
                                        pos-=5;
                                else
                                {
+                                       if(pos-1 > 0 && key_line[pos-1] == STRING_COLOR_TAG && key_line[pos] == STRING_COLOR_TAG) // consider ^^ as a character
+                                               pos--;
                                        pos--;
                                        break;
                                }
@@ -546,12 +629,21 @@ Key_Console (int key, int ascii)
                        char    k;
                        len = (int)strlen(key_line);
                        pos = key_linepos;
+
                        while(++pos < len)
                        {
                                k = key_line[pos];
                                if(k == '\"' || k == ';' || k == ' ' || k == '\'')
                                        break;
                        }
+                       
+                       if (pos < len) // skip all "; ' after the word
+                               while(++pos < len)
+                               {
+                                       k = key_line[pos];
+                                       if (!(k == '\"' || k == ';' || k == ' ' || k == '\''))
+                                               break;
+                               }
                        key_linepos = pos;
                }
                else if(keydown[K_SHIFT]) // move cursor to the next character ignoring colors
@@ -559,14 +651,22 @@ Key_Console (int key, int ascii)
                        int             pos, len;
                        len = (int)strlen(key_line);
                        pos = key_linepos;
-                       // check if there is a color tag right after the cursor
-                       if (key_line[pos] == STRING_COLOR_TAG)
-                       {
-                               if(isdigit(key_line[pos+1]))
-                                       pos+=1;
-                               else if(key_line[pos+1] == STRING_COLOR_RGB_TAG_CHAR && isxdigit(key_line[pos+2]) && isxdigit(key_line[pos+3]) && isxdigit(key_line[pos+4]))
-                                       pos+=4;
-                       }
+                       
+                       // go beyond all initial consecutive color tags, if any
+                       if(pos < len)
+                               while (key_line[pos] == STRING_COLOR_TAG)
+                               {
+                                       if(isdigit(key_line[pos+1]))
+                                               pos+=2;
+                                       else if(key_line[pos+1] == STRING_COLOR_RGB_TAG_CHAR && isxdigit(key_line[pos+2]) && isxdigit(key_line[pos+3]) && isxdigit(key_line[pos+4]))
+                                               pos+=5;
+                                       else
+                                               break;
+                               }
+                       
+                       // skip the char
+                       if (key_line[pos] == STRING_COLOR_TAG && key_line[pos+1] == STRING_COLOR_TAG) // consider ^^ as a character
+                               pos++;
                        pos++;
                        
                        // now go beyond all next consecutive color tags, if any
@@ -606,28 +706,74 @@ Key_Console (int key, int ascii)
                Key_History_Down();
                return;
        }
-
-       if (key == K_PGUP || key == K_KP_PGUP || key == K_MWHEELUP)
+       // ~1.0795 = 82/76  using con_textsize 64 76 is height of the char, 6 is the distance between 2 lines
+       if (key == K_PGUP || key == K_KP_PGUP)
        {
                if(keydown[K_CTRL])
                {
-                       con_backscroll += 3;
+                       con_backscroll += ((vid_conheight.integer >> 2) / con_textsize.integer)-1;
                }
                else
-                       con_backscroll += ((int) vid_conheight.integer >> 5);
+                       con_backscroll += ((vid_conheight.integer >> 1) / con_textsize.integer)-3;
                return;
        }
 
-       if (key == K_PGDN || key == K_KP_PGDN || key == K_MWHEELDOWN)
+       if (key == K_PGDN || key == K_KP_PGDN)
        {
                if(keydown[K_CTRL])
                {
-                       con_backscroll -= 3;
+                       con_backscroll -= ((vid_conheight.integer >> 2) / con_textsize.integer)-1;
                }
                else
-                       con_backscroll -= ((int) vid_conheight.integer >> 5);
+                       con_backscroll -= ((vid_conheight.integer >> 1) / con_textsize.integer)-3;
                return;
        }
+       if (key == K_MWHEELUP)
+       {
+               if(keydown[K_CTRL])
+                       con_backscroll += 1;
+               else if(keydown[K_SHIFT])
+                       con_backscroll += ((vid_conheight.integer >> 2) / con_textsize.integer)-1;
+               else
+                       con_backscroll += 5;
+               return;
+       }
+
+       if (key == K_MWHEELDOWN)
+       {
+               if(keydown[K_CTRL])
+                       con_backscroll -= 1;
+               else if(keydown[K_SHIFT])
+                       con_backscroll -= ((vid_conheight.integer >> 2) / con_textsize.integer)-1;
+               else
+                       con_backscroll -= 5;
+               return;
+       }
+
+       if (keydown[K_CTRL])
+       {
+               // text zoom in
+               if (key == '+' || key == K_KP_PLUS)
+               {
+                       if (con_textsize.integer < 128)
+                               Cvar_SetValueQuick(&con_textsize, con_textsize.integer + 1);
+                       return;
+               }
+               // text zoom out
+               if (key == '-' || key == K_KP_MINUS)
+               {
+                       if (con_textsize.integer > 1)
+                               Cvar_SetValueQuick(&con_textsize, con_textsize.integer - 1);
+                       return;
+               }
+               // text zoom reset
+               if (key == '0' || key == K_KP_INS)
+               {
+                       Cvar_SetValueQuick(&con_textsize, atoi(Cvar_VariableDefString("con_textsize")));
+                       return;
+               }
+       }
 
        if (key == K_HOME || key == K_KP_HOME)
        {