]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - console.c
experimental new persistent console history (uses same buffer as console output)...
[xonotic/darkplaces.git] / console.c
index 8abc906afea6bef27b199d9795d688dece935848..28b0bef798d9966abb604d715549c21e7bd8b138 100644 (file)
--- a/console.c
+++ b/console.c
@@ -28,8 +28,8 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
 float con_cursorspeed = 4;
 
-#define                CON_TEXTSIZE    131072
-#define                CON_MAXLINES      4096
+#define                CON_TEXTSIZE    1048576
+#define                CON_MAXLINES      16384
 
 // lines up from bottom to display
 int con_backscroll;
@@ -37,13 +37,10 @@ int con_backscroll;
 // console buffer
 char con_text[CON_TEXTSIZE];
 
-#define CON_MASK_HIDENOTIFY 128
-#define CON_MASK_CHAT 1
-
 typedef struct
 {
        char *start;
-       int len;
+       size_t len;
 
        double addtime;
        int mask;
@@ -56,6 +53,7 @@ con_lineinfo con_lines[CON_MAXLINES];
 int con_lines_first; // cyclic buffer
 int con_lines_count;
 #define CON_LINES_IDX(i) ((con_lines_first + (i)) % CON_MAXLINES)
+#define CON_LINES_UNIDX(i) (((i) - con_lines_first + CON_MAXLINES) % CON_MAXLINES)
 #define CON_LINES_LAST CON_LINES_IDX(con_lines_count - 1)
 #define CON_LINES(i) con_lines[CON_LINES_IDX(i)]
 #define CON_LINES_PRED(i) (((i) + CON_MAXLINES - 1) % CON_MAXLINES)
@@ -96,13 +94,18 @@ cvar_t con_nickcompletion_flags = {CVAR_SAVE, "con_nickcompletion_flags", "11",
 #define NICKS_ALPHANUMERICS_ONLY 8
 #define NICKS_NO_SPACES 16
 
+cvar_t con_completion_playdemo = {CVAR_SAVE, "con_completion_playdemo", "*.dem"};
+cvar_t con_completion_timedemo = {CVAR_SAVE, "con_completion_timedemo", "*.dem"};
+cvar_t con_completion_exec = {CVAR_SAVE, "con_completion_exec", "*.cfg"};
+
 int con_linewidth;
 int con_vislines;
 
 qboolean con_initialized;
 
 // used for server replies to rcon command
-qboolean rcon_redirect = false;
+lhnetsocket_t *rcon_redirect_sock = NULL;
+lhnetaddress_t *rcon_redirect_dest = NULL;
 int rcon_redirect_bufferpos = 0;
 char rcon_redirect_buffer[1400];
 
@@ -119,7 +122,7 @@ cvar_t log_file = {0, "log_file","", "filename to log messages to"};
 cvar_t log_dest_udp = {0, "log_dest_udp","", "UDP address to log messages to (in QW rcon compatible format); multiple destinations can be separated by spaces; DO NOT SPECIFY DNS NAMES HERE"};
 char log_dest_buffer[1400]; // UDP packet
 size_t log_dest_buffer_pos;
-qboolean log_dest_buffer_appending;
+unsigned int log_dest_buffer_appending;
 char crt_log_file [MAX_OSPATH] = "";
 qfile_t* logfile = NULL;
 
@@ -188,13 +191,22 @@ const char* Log_Timestamp (const char *desc)
 {
        static char timestamp [128];
        time_t crt_time;
-       const struct tm *crt_tm;
+#if _MSC_VER >= 1400
+       struct tm crt_tm;
+#else
+       struct tm *crt_tm;
+#endif
        char timestring [64];
 
        // Build the time stamp (ex: "Wed Jun 30 21:49:08 1993");
        time (&crt_time);
+#if _MSC_VER >= 1400
+       localtime_s (&crt_tm, &crt_time);
+       strftime (timestring, sizeof (timestring), "%a %b %d %H:%M:%S %Y", &crt_tm);
+#else
        crt_tm = localtime (&crt_time);
        strftime (timestring, sizeof (timestring), "%a %b %d %H:%M:%S %Y", crt_tm);
+#endif
 
        if (desc != NULL)
                dpsnprintf (timestamp, sizeof (timestamp), "====== %s (%s) ======\n", desc, timestring);
@@ -215,7 +227,7 @@ void Log_Open (void)
        if (logfile != NULL || log_file.string[0] == '\0')
                return;
 
-       logfile = FS_Open (log_file.string, "ab", false, false);
+       logfile = FS_OpenRealFile(log_file.string, "a", false);
        if (logfile != NULL)
        {
                strlcpy (crt_log_file, log_file.string, sizeof (crt_log_file));
@@ -349,7 +361,7 @@ void Log_Printf (const char *logfilename, const char *fmt, ...)
 {
        qfile_t *file;
 
-       file = FS_Open (logfilename, "ab", true, false);
+       file = FS_OpenRealFile(logfilename, "a", true);
        if (file != NULL)
        {
                va_list argptr;
@@ -417,7 +429,7 @@ Con_MessageMode_f
 void Con_MessageMode_f (void)
 {
        key_dest = key_message;
-       chat_team = false;
+       chat_mode = 0; // "say"
 }
 
 
@@ -429,9 +441,24 @@ Con_MessageMode2_f
 void Con_MessageMode2_f (void)
 {
        key_dest = key_message;
-       chat_team = true;
+       chat_mode = 1; // "say_team"
 }
 
+/*
+================
+Con_CommandMode_f
+================
+*/
+void Con_CommandMode_f (void)
+{
+       key_dest = key_message;
+       if(Cmd_Argc() > 1)
+       {
+               dpsnprintf(chat_buffer, sizeof(chat_buffer), "%s ", Cmd_Args());
+               chat_bufferlen = strlen(chat_buffer);
+       }
+       chat_mode = -1; // command
+}
 
 /*
 ================
@@ -487,7 +514,7 @@ void Con_ConDump_f (void)
                Con_Printf("usage: condump <filename>\n");
                return;
        }
-       file = FS_Open(Cmd_Argv(1), "wb", false, false);
+       file = FS_OpenRealFile(Cmd_Argv(1), "w", false);
        if (!file)
        {
                Con_Printf("condump: unable to write file \"%s\"\n", Cmd_Argv(1));
@@ -544,10 +571,15 @@ void Con_Init (void)
        Cvar_RegisterVariable (&con_nickcompletion);
        Cvar_RegisterVariable (&con_nickcompletion_flags);
 
+       Cvar_RegisterVariable (&con_completion_playdemo); // *.dem
+       Cvar_RegisterVariable (&con_completion_timedemo); // *.dem
+       Cvar_RegisterVariable (&con_completion_exec); // *.cfg
+
        // register our commands
        Cmd_AddCommand ("toggleconsole", Con_ToggleConsole_f, "opens or closes the console");
        Cmd_AddCommand ("messagemode", Con_MessageMode_f, "input a chat message to say to everyone");
        Cmd_AddCommand ("messagemode2", Con_MessageMode2_f, "input a chat message to say to only your team");
+       Cmd_AddCommand ("commandmode", Con_CommandMode_f, "input a console command");
        Cmd_AddCommand ("clear", Con_Clear_f, "clear console history");
        Cmd_AddCommand ("maps", Con_Maps_f, "list information about available maps");
        Cmd_AddCommand ("condump", Con_ConDump_f, "output console history to a file (see also log_file)");
@@ -774,6 +806,35 @@ static char qfont_table[256] = {
        'x',  'y',  'z',  '{',  '|',  '}',  '~',  '<'
 };
 
+void Con_Rcon_Redirect_Init(lhnetsocket_t *sock, lhnetaddress_t *dest)
+{
+       rcon_redirect_sock = sock;
+       rcon_redirect_dest = dest;
+       memcpy(rcon_redirect_buffer, "\377\377\377\377n", 5); // QW rcon print
+       rcon_redirect_bufferpos = 5;
+}
+
+void Con_Rcon_Redirect_Flush()
+{
+       rcon_redirect_buffer[rcon_redirect_bufferpos] = 0;
+       NetConn_WriteString(rcon_redirect_sock, rcon_redirect_buffer, rcon_redirect_dest);
+       memcpy(rcon_redirect_buffer, "\377\377\377\377n", 5); // QW rcon print
+       rcon_redirect_bufferpos = 5;
+}
+
+void Con_Rcon_Redirect_End()
+{
+       Con_Rcon_Redirect_Flush();
+       rcon_redirect_dest = NULL;
+       rcon_redirect_sock = NULL;
+}
+
+void Con_Rcon_Redirect_Abort()
+{
+       rcon_redirect_dest = NULL;
+       rcon_redirect_sock = NULL;
+}
+
 /*
 ================
 Con_Rcon_AddChar
@@ -781,7 +842,7 @@ Con_Rcon_AddChar
 Adds a character to the rcon buffer
 ================
 */
-void Con_Rcon_AddChar(char c)
+void Con_Rcon_AddChar(int c)
 {
        if(log_dest_buffer_appending)
                return;
@@ -790,8 +851,12 @@ void Con_Rcon_AddChar(char c)
        // if this print is in response to an rcon command, add the character
        // to the rcon redirect buffer
 
-       if (rcon_redirect && rcon_redirect_bufferpos < (int)sizeof(rcon_redirect_buffer) - 1)
+       if (rcon_redirect_dest)
+       {
                rcon_redirect_buffer[rcon_redirect_bufferpos++] = c;
+               if(rcon_redirect_bufferpos >= (int)sizeof(rcon_redirect_buffer) - 1)
+                       Con_Rcon_Redirect_Flush();
+       }
        else if(*log_dest_udp.string) // don't duplicate rcon command responses here, these are sent another way
        {
                if(log_dest_buffer_pos == 0)
@@ -806,6 +871,69 @@ void Con_Rcon_AddChar(char c)
        --log_dest_buffer_appending;
 }
 
+/**
+ * Convert an RGB color to its nearest quake color.
+ * I'll cheat on this a bit by translating the colors to HSV first,
+ * S and V decide if it's black or white, otherwise, H will decide the
+ * actual color.
+ * @param _r Red (0-255)
+ * @param _g Green (0-255)
+ * @param _b Blue (0-255)
+ * @return A quake color character.
+ */
+static char Sys_Con_NearestColor(const unsigned char _r, const unsigned char _g, const unsigned char _b)
+{
+       float r = ((float)_r)/255.0;
+       float g = ((float)_g)/255.0;
+       float b = ((float)_b)/255.0;
+       float min = min(r, min(g, b));
+       float max = max(r, max(g, b));
+
+       int h; ///< Hue angle [0,360]
+       float s; ///< Saturation [0,1]
+       float v = max; ///< In HSV v == max [0,1]
+
+       if(max == min)
+               s = 0;
+       else
+               s = 1.0 - (min/max);
+
+       // Saturation threshold. We now say 0.2 is the minimum value for a color!
+       if(s < 0.2)
+       {
+               // If the value is less than half, return a black color code.
+               // Otherwise return a white one.
+               if(v < 0.5)
+                       return '0';
+               return '7';
+       }
+
+       // Let's get the hue angle to define some colors:
+       if(max == min)
+               h = 0;
+       else if(max == r)
+               h = (int)(60.0 * (g-b)/(max-min))%360;
+       else if(max == g)
+               h = (int)(60.0 * (b-r)/(max-min) + 120);
+       else // if(max == b) redundant check
+               h = (int)(60.0 * (r-g)/(max-min) + 240);
+
+       if(h < 36) // *red* to orange
+               return '1';
+       else if(h < 80) // orange over *yellow* to evilish-bright-green
+               return '3';
+       else if(h < 150) // evilish-bright-green over *green* to ugly bright blue
+               return '2';
+       else if(h < 200) // ugly bright blue over *bright blue* to darkish blue
+               return '5';
+       else if(h < 270) // darkish blue over *dark blue* to cool purple
+               return '4';
+       else if(h < 330) // cool purple over *purple* to ugly swiny red
+               return '6';
+       else // ugly red to red closes the circly
+               return '1';
+}
+
 /*
 ================
 Con_Print
@@ -816,7 +944,7 @@ Prints to all appropriate console targets, and adds timestamps
 extern cvar_t timestamps;
 extern cvar_t timeformat;
 extern qboolean sys_nostdout;
-void Con_Print(const char *msg)
+static void Con_Print_Internal(const char *msg, qboolean history)
 {
        static int mask = 0;
        static int index = 0;
@@ -877,7 +1005,7 @@ void Con_Print(const char *msg)
                        // send to log file
                        Log_ConPrint(line);
                        // send to scrollable buffer
-                       if (con_initialized && cls.state != ca_dedicated)
+                       if (history && con_initialized && cls.state != ca_dedicated)
                        {
                                Con_PrintToHistory(line, mask);
                                mask = 0;
@@ -900,12 +1028,32 @@ void Con_Print(const char *msg)
                                        int lastcolor = 0;
                                        const char *in;
                                        char *out;
+                                       int color;
                                        for(in = line, out = printline; *in; ++in)
                                        {
                                                switch(*in)
                                                {
                                                        case STRING_COLOR_TAG:
-                                                               switch(in[1])
+                                                               if( in[1] == STRING_COLOR_RGB_TAG_CHAR && isxdigit(in[2]) && isxdigit(in[3]) && isxdigit(in[4]) )
+                                                               {
+                                                                       char r = tolower(in[2]);
+                                                                       char g = tolower(in[3]);
+                                                                       char b = tolower(in[4]);
+                                                                       // it's a hex digit already, so the else part needs no check --blub
+                                                                       if(isdigit(r)) r -= '0';
+                                                                       else r -= 87;
+                                                                       if(isdigit(g)) g -= '0';
+                                                                       else g -= 87;
+                                                                       if(isdigit(b)) b -= '0';
+                                                                       else b -= 87;
+                                                                       
+                                                                       color = Sys_Con_NearestColor(r * 17, g * 17, b * 17);
+                                                                       in += 3; // 3 only, the switch down there does the fourth
+                                                               }
+                                                               else
+                                                                       color = in[1];
+                                                               
+                                                               switch(color)
                                                                {
                                                                        case STRING_COLOR_TAG:
                                                                                ++in;
@@ -1005,6 +1153,16 @@ void Con_Print(const char *msg)
                                                        case STRING_COLOR_TAG:
                                                                switch(in[1])
                                                                {
+                                                                       case STRING_COLOR_RGB_TAG_CHAR:
+                                                                               if ( isxdigit(in[2]) && isxdigit(in[3]) && isxdigit(in[4]) )
+                                                                               {
+                                                                                       in+=4;
+                                                                                       break;
+                                                                               }
+                                                                               *out++ = STRING_COLOR_TAG;
+                                                                               *out++ = STRING_COLOR_RGB_TAG_CHAR;
+                                                                               ++in;
+                                                                               break;
                                                                        case STRING_COLOR_TAG:
                                                                                ++in;
                                                                                *out++ = STRING_COLOR_TAG;
@@ -1040,6 +1198,14 @@ void Con_Print(const char *msg)
                }
        }
 }
+void Con_Print(const char *msg)
+{
+       Con_Print_Internal(msg, true);
+}
+void Con_PrintNotToHistory(const char *msg)
+{
+       Con_Print_Internal(msg, false);
+}
 
 
 /*
@@ -1125,7 +1291,7 @@ void Con_DrawInput (void)
        if (!key_consoleactive)
                return;         // don't draw anything
 
-       strlcpy(editlinecopy, key_lines[edit_line], sizeof(editlinecopy));
+       strlcpy(editlinecopy, key_line, sizeof(editlinecopy));
        text = editlinecopy;
 
        // Advanced Console Editing by Radix radix@planetquake.com
@@ -1153,7 +1319,7 @@ void Con_DrawInput (void)
        DrawQ_String_Font(x, con_vislines - con_textsize.value*2, text, 0, con_textsize.value, con_textsize.value, 1.0, 1.0, 1.0, 1.0, 0, NULL, false, FONT_CONSOLE );
 
        // remove cursor
-//     key_lines[edit_line][key_linepos] = 0;
+//     key_line[key_linepos] = 0;
 }
 
 typedef struct
@@ -1178,7 +1344,7 @@ float Con_WordWidthFunc(void *passthrough, const char *w, size_t *length, float
        if(w == NULL)
        {
                ti->colorindex = -1;
-               return ti->fontsize * ti->font->width_of[0];
+               return ti->fontsize * ti->font->maxwidth;
        }
        if(maxWidth >= 0)
                return DrawQ_TextWidth_Font_UntilWidth(w, length, false, ti->font, maxWidth / ti->fontsize) * ti->fontsize;
@@ -1212,9 +1378,9 @@ int Con_DisplayLineFunc(void *passthrough, const char *line, size_t length, floa
                (void) 0;
        else
        {
-               int x = ti->x + (ti->width - width) * ti->alignment;
+               int x = (int) (ti->x + (ti->width - width) * ti->alignment);
                if(isContinuation && *ti->continuationString)
-                       x += DrawQ_String_Font(x, ti->y, ti->continuationString, strlen(ti->continuationString), ti->fontsize, ti->fontsize, 1.0, 1.0, 1.0, 1.0, 0, NULL, false, ti->font);
+                       x += (int) DrawQ_String_Font(x, ti->y, ti->continuationString, strlen(ti->continuationString), ti->fontsize, ti->fontsize, 1.0, 1.0, 1.0, 1.0, 0, NULL, false, ti->font);
                if(length > 0)
                        DrawQ_String_Font(x, ti->y, line, length, ti->fontsize, ti->fontsize, 1.0, 1.0, 1.0, 1.0, 0, &(ti->colorindex), false, ti->font);
        }
@@ -1223,6 +1389,65 @@ int Con_DisplayLineFunc(void *passthrough, const char *line, size_t length, floa
        return 1;
 }
 
+int Con_FindPrevLine(int mask_must, int mask_mustnot, int start)
+{
+       int i;
+       if(start == -1)
+               start = con_lines_count;
+       for(i = start - 1; i >= 0; --i)
+       {
+               con_lineinfo *l = &CON_LINES(i);
+
+               if((l->mask & mask_must) != mask_must)
+                       continue;
+               if(l->mask & mask_mustnot)
+                       continue;
+
+               return i;
+       }
+
+       return -1;
+}
+
+int Con_FindNextLine(int mask_must, int mask_mustnot, int start)
+{
+       int i;
+       for(i = start + 1; i < con_lines_count; ++i)
+       {
+               con_lineinfo *l = &CON_LINES(i);
+
+               if((l->mask & mask_must) != mask_must)
+                       continue;
+               if(l->mask & mask_mustnot)
+                       continue;
+
+               return i;
+       }
+
+       return -1;
+}
+
+const char *Con_GetLine(int i)
+{
+       static char buf[MAX_INPUTLINE];
+       con_lineinfo *l = &CON_LINES(i);
+       size_t sz = l->len+1 > sizeof(buf) ? sizeof(buf) : l->len+1;
+       strlcpy(buf, l->start, sz);
+       return buf;
+}
+
+int Con_GetLineID(int i)
+{
+       return CON_LINES_IDX(i);
+}
+
+int Con_GetLineByID(int i)
+{
+       i = CON_LINES_UNIDX(i);
+       if(i >= con_lines_count)
+               return -1;
+       return i;
+}
 
 int Con_DrawNotifyRect(int mask_must, int mask_mustnot, float maxage, float x, float y, float width, float height, float fontsize, float alignment_x, float alignment_y, const char *continuationString)
 {
@@ -1247,7 +1472,7 @@ int Con_DrawNotifyRect(int mask_must, int mask_mustnot, float maxage, float x, f
        l = 0;
        Con_WordWidthFunc(&ti, NULL, &l, -1);
        l = strlen(continuationString);
-       continuationWidth = Con_WordWidthFunc(&ti, continuationString, &l, -1);
+       continuationWidth = (int) Con_WordWidthFunc(&ti, continuationString, &l, -1);
 
        // first find the first line to draw by backwards iterating and word wrapping to find their length...
        startidx = con_lines_count;
@@ -1363,13 +1588,13 @@ void Con_DrawNotify (void)
                chatstart = 0; // shut off gcc warning
        }
 
-       v = notifystart + con_notifysize.value * Con_DrawNotifyRect(0, CON_MASK_HIDENOTIFY | (numChatlines ? CON_MASK_CHAT : 0), con_notifytime.value, 0, notifystart, vid_conwidth.value, con_notify.value * con_notifysize.value, con_notifysize.value, align, 0.0, "");
+       v = notifystart + con_notifysize.value * Con_DrawNotifyRect(0, CON_MASK_LOADEDHISTORY | CON_MASK_INPUT | CON_MASK_HIDENOTIFY | (numChatlines ? CON_MASK_CHAT : 0), con_notifytime.value, 0, notifystart, vid_conwidth.value, con_notify.value * con_notifysize.value, con_notifysize.value, align, 0.0, "");
 
        // chat?
        if(numChatlines)
        {
                v = chatstart + numChatlines * con_chatsize.value;
-               Con_DrawNotifyRect(CON_MASK_CHAT, 0, con_chattime.value, 0, chatstart, vid_conwidth.value * con_chatwidth.value, v - chatstart, con_chatsize.value, 0.0, 1.0, "^3\014\014\014 "); // 015 is Â·> character in conchars.tga
+               Con_DrawNotifyRect(CON_MASK_CHAT, CON_MASK_LOADEDHISTORY | CON_MASK_INPUT, con_chattime.value, 0, chatstart, vid_conwidth.value * con_chatwidth.value, v - chatstart, con_chatsize.value, 0.0, 1.0, "^3\014\014\014 "); // 015 is Ã‚·> character in conchars.tga
        }
 
        if (key_dest == key_message)
@@ -1377,10 +1602,12 @@ void Con_DrawNotify (void)
                int colorindex = -1;
 
                // LordHavoc: speedup, and other improvements
-               if (chat_team)
-                       sprintf(temptext, "say_team:%s%c", chat_buffer, (int) 10+((int)(realtime*con_cursorspeed)&1));
+               if (chat_mode < 0)
+                       dpsnprintf(temptext, sizeof(temptext), "]%s%c", chat_buffer, (int) 10+((int)(realtime*con_cursorspeed)&1));
+               else if(chat_mode)
+                       dpsnprintf(temptext, sizeof(temptext), "say_team:%s%c", chat_buffer, (int) 10+((int)(realtime*con_cursorspeed)&1));
                else
-                       sprintf(temptext, "say:%s%c", chat_buffer, (int) 10+((int)(realtime*con_cursorspeed)&1));
+                       dpsnprintf(temptext, sizeof(temptext), "say:%s%c", chat_buffer, (int) 10+((int)(realtime*con_cursorspeed)&1));
 
                // FIXME word wrap
                inputsize = (numChatlines ? con_chatsize : con_notifysize).value;
@@ -1402,6 +1629,10 @@ int Con_MeasureConsoleLine(int lineno)
 {
        float width = vid_conwidth.value;
        con_text_info_t ti;
+
+       if(con_lines[lineno].mask & CON_MASK_LOADEDHISTORY)
+               return 0;
+
        ti.fontsize = con_textsize.value;
        ti.font = FONT_CONSOLE;
 
@@ -1435,8 +1666,11 @@ returned.
 int Con_DrawConsoleLine(float y, int lineno, float ymin, float ymax)
 {
        float width = vid_conwidth.value;
-
        con_text_info_t ti;
+
+       if(con_lines[lineno].mask & CON_MASK_LOADEDHISTORY)
+               return 0;
+
        ti.continuationString = "";
        ti.alignment = 0;
        ti.fontsize = con_textsize.value;
@@ -1553,13 +1787,13 @@ its format (q1/q2/q3/hl) and even its message
 qboolean GetMapList (const char *s, char *completedname, int completednamebufferlength)
 {
        fssearch_t      *t;
-       char            message[64];
+       char            message[1024];
        int                     i, k, max, p, o, min;
        unsigned char *len;
        qfile_t         *f;
        unsigned char buf[1024];
 
-       sprintf(message, "maps/%s*.bsp", s);
+       dpsnprintf(message, sizeof(message), "maps/%s*.bsp", s);
        t = FS_Search(message, 1, true);
        if(!t)
                return false;
@@ -1588,7 +1822,7 @@ qboolean GetMapList (const char *s, char *completedname, int completednamebuffer
                char entfilename[MAX_QPATH];
                strlcpy(message, "^1**ERROR**^7", sizeof(message));
                p = 0;
-               f = FS_Open(t->filenames[i], "rb", true, false);
+               f = FS_OpenVirtualFile(t->filenames[i], true);
                if(f)
                {
                        memset(buf, 0, 1024);
@@ -1642,8 +1876,8 @@ qboolean GetMapList (const char *s, char *completedname, int completednamebuffer
                                        if (com_token[0] == '}')
                                                break;
                                        // skip leading whitespace
-                                       for (k = 0;com_token[k] && com_token[k] <= ' ';k++);
-                                       for (l = 0;l < (int)sizeof(keyname) - 1 && com_token[k+l] && com_token[k+l] > ' ';l++)
+                                       for (k = 0;com_token[k] && ISWHITESPACE(com_token[k]);k++);
+                                       for (l = 0;l < (int)sizeof(keyname) - 1 && com_token[k+l] && !ISWHITESPACE(com_token[k+l]);l++)
                                                keyname[l] = com_token[k+l];
                                        keyname[l] = 0;
                                        if (!COM_ParseToken_Simple(&data, false, false))
@@ -1735,11 +1969,10 @@ void Con_DisplayList(const char **list)
                Con_Print("\n\n");
 }
 
-/* Nicks_CompleteCountPossible
-
-   Count the number of possible nicks to complete
- */
-//qboolean COM_StringDecolorize(const char *in, size_t size_in, char *out, size_t size_out, qboolean escape_carets);
+/*
+       SanitizeString strips color tags from the string in
+       and writes the result on string out
+*/
 void SanitizeString(char *in, char *out)
 {
        while(*in)
@@ -1752,18 +1985,33 @@ void SanitizeString(char *in, char *out)
                                out[0] = STRING_COLOR_TAG;
                                out[1] = 0;
                                return;
-                       } else if(*in >= '0' && *in <= '9')
+                       }
+                       else if (*in >= '0' && *in <= '9') // ^[0-9] found
                        {
                                ++in;
-                               if(!*in) // end
+                               if(!*in)
                                {
                                        *out = 0;
                                        return;
-                               } else if (*in == STRING_COLOR_TAG)
+                               } else if (*in == STRING_COLOR_TAG) // ^[0-9]^ found, don't print ^[0-9]
                                        continue;
-                       } else if (*in != STRING_COLOR_TAG) {
-                               --in;
                        }
+                       else if (*in == STRING_COLOR_RGB_TAG_CHAR) // ^x found
+                       {
+                               if ( isxdigit(in[1]) && isxdigit(in[2]) && isxdigit(in[3]) )
+                               {
+                                       in+=4;
+                                       if (!*in)
+                                       {
+                                               *out = 0;
+                                               return;
+                                       } else if (*in == STRING_COLOR_TAG) // ^xrgb^ found, don't print ^xrgb
+                                               continue;
+                               }
+                               else in--;
+                       }
+                       else if (*in != STRING_COLOR_TAG)
+                               --in;
                }
                *out = qfont_table[*(unsigned char*)in];
                ++in;
@@ -1771,7 +2019,6 @@ void SanitizeString(char *in, char *out)
        }
        *out = 0;
 }
-int Sbar_GetPlayer (int index); // <- safety?
 
 // Now it becomes TRICKY :D --blub
 static char Nicks_list[MAX_SCOREBOARD][MAX_SCOREBOARDNAME];    // contains the nicks with colors and all that
@@ -1816,15 +2063,15 @@ int Nicks_strncasecmp(char *a, char *b, unsigned int a_len)
                        return Nicks_strncasecmp_nospaces(a, b, a_len);
                return strncasecmp(a, b, a_len);
        }
-       
+
        space_char = (con_nickcompletion_flags.integer & NICKS_NO_SPACES) ? 'a' : ' ';
-       
+
        // ignore non alphanumerics of B
        // if A contains a non-alphanumeric, B must contain it as well though!
        while(a_len)
        {
                qboolean alnum_a, alnum_b;
-               
+
                if(tolower(*a) == tolower(*b))
                {
                        if(*a == 0) // end of both strings, they're equal
@@ -1853,6 +2100,11 @@ int Nicks_strncasecmp(char *a, char *b, unsigned int a_len)
        return 0;
 }
 
+
+/* Nicks_CompleteCountPossible
+
+   Count the number of possible nicks to complete
+ */
 int Nicks_CompleteCountPossible(char *line, int pos, char *s, qboolean isCon)
 {
        char name[128];
@@ -1861,34 +2113,31 @@ int Nicks_CompleteCountPossible(char *line, int pos, char *s, qboolean isCon)
        int match;
        int spos;
        int count = 0;
-       
+
        if(!con_nickcompletion.integer)
                return 0;
 
        // changed that to 1
        if(!line[0])// || !line[1]) // we want at least... 2 written characters
                return 0;
-       
+
        for(i = 0; i < cl.maxclients; ++i)
        {
-               /*p = Sbar_GetPlayer(i);
-               if(p < 0)
-               break;*/
                p = i;
                if(!cl.scores[p].name[0])
                        continue;
 
                SanitizeString(cl.scores[p].name, name);
-               //Con_Printf("Sanitized: %s^7 -> %s", cl.scores[p].name, name);
-               
+               //Con_Printf(" ^2Sanitized: ^7%s -> %s", cl.scores[p].name, name);
+
                if(!name[0])
                        continue;
-               
+
                length = strlen(name);
                match = -1;
                spos = pos - 1; // no need for a minimum of characters :)
-               
-               while(spos >= 0 && (spos - pos) < length) // search-string-length < name length
+
+               while(spos >= 0)
                {
                        if(spos > 0 && line[spos-1] != ' ' && line[spos-1] != ';' && line[spos-1] != '\"' && line[spos-1] != '\'')
                        {
@@ -1916,7 +2165,7 @@ int Nicks_CompleteCountPossible(char *line, int pos, char *s, qboolean isCon)
                {
                        Nicks_matchpos = match;
                }
-               
+
                Nicks_offset[count] = s - (&line[match]);
                //Con_Printf("offset for %s: %i\n", name, Nicks_offset[count]);
 
@@ -1943,7 +2192,7 @@ void Nicks_CutMatchesNormal(int count)
                l = strlen(Nicks_sanlist[i]) - 1;
                if(l < c)
                        c = l;
-               
+
                for(l = 0; l <= c; ++l)
                        if(tolower(Nicks_sanlist[0][l]) != tolower(Nicks_sanlist[i][l]))
                        {
@@ -1978,7 +2227,7 @@ void Nicks_CutMatchesAlphaNumeric(int count)
        char tempstr[sizeof(Nicks_sanlist[0])];
        char *a, *b;
        char space_char = (con_nickcompletion_flags.integer & NICKS_NO_SPACES) ? 'a' : ' '; // yes this is correct, we want NO spaces when no spaces
-       
+
        c = strlen(Nicks_sanlist[0]);
        for(i = 0, l = 0; i < (int)c; ++i)
        {
@@ -1990,7 +2239,7 @@ void Nicks_CutMatchesAlphaNumeric(int count)
                }
        }
        tempstr[l] = 0;
-       
+
        for(i = 1; i < count; ++i)
        {
                a = tempstr;
@@ -2036,7 +2285,7 @@ void Nicks_CutMatchesNoSpaces(int count)
        unsigned int c, l;
        char tempstr[sizeof(Nicks_sanlist[0])];
        char *a, *b;
-       
+
        c = strlen(Nicks_sanlist[0]);
        for(i = 0, l = 0; i < (int)c; ++i)
        {
@@ -2046,7 +2295,7 @@ void Nicks_CutMatchesNoSpaces(int count)
                }
        }
        tempstr[l] = 0;
-       
+
        for(i = 1; i < count; ++i)
        {
                a = tempstr;
@@ -2106,40 +2355,73 @@ const char **Nicks_CompleteBuildList(int count)
                buf[bpos] = Nicks_sanlist[bpos] + Nicks_offset[bpos];
 
        Nicks_CutMatches(count);
-       
+
        buf[bpos] = NULL;
        return buf;
 }
 
+/*
+       Nicks_AddLastColor
+       Restores the previous used color, after the autocompleted name.
+*/
 int Nicks_AddLastColor(char *buffer, int pos)
 {
        qboolean quote_added = false;
        int match;
-       char color = '7';
-       
+       int color = STRING_COLOR_DEFAULT + '0';
+       char r = 0, g = 0, b = 0;
+
        if(con_nickcompletion_flags.integer & NICKS_ADD_QUOTE && buffer[Nicks_matchpos-1] == '\"')
        {
                // we'll have to add a quote :)
                buffer[pos++] = '\"';
                quote_added = true;
        }
-       
+
        if((!quote_added && con_nickcompletion_flags.integer & NICKS_ADD_COLOR) || con_nickcompletion_flags.integer & NICKS_FORCE_COLOR)
        {
                // add color when no quote was added, or when flags &4?
                // find last color
                for(match = Nicks_matchpos-1; match >= 0; --match)
                {
-                       if(buffer[match] == STRING_COLOR_TAG && buffer[match+1] >= '0' && buffer[match+1] <= '9')
+                       if(buffer[match] == STRING_COLOR_TAG)
                        {
-                               color = buffer[match+1];
-                               break;
+                               if( isdigit(buffer[match+1]) )
+                               {
+                                       color = buffer[match+1];
+                                       break;
+                               }
+                               else if(buffer[match+1] == STRING_COLOR_RGB_TAG_CHAR)
+                               {
+                                       if ( isxdigit(buffer[match+2]) && isxdigit(buffer[match+3]) && isxdigit(buffer[match+4]) )
+                                       {
+                                               r = buffer[match+2];
+                                               g = buffer[match+3];
+                                               b = buffer[match+4];
+                                               color = -1;
+                                               break;
+                                       }
+                               }
                        }
                }
-               if(!quote_added && buffer[pos-2] == STRING_COLOR_TAG && buffer[pos-1] >= '0' && buffer[pos-1] <= '9') // when thes use &4
-                       pos -= 2;
+               if(!quote_added)
+               {
+                       if( pos >= 2 && buffer[pos-2] == STRING_COLOR_TAG && isdigit(buffer[pos-1]) ) // when thes use &4
+                               pos -= 2;
+                       else if( pos >= 5 && buffer[pos-5] == STRING_COLOR_TAG && buffer[pos-4] == STRING_COLOR_RGB_TAG_CHAR
+                                        && isxdigit(buffer[pos-3]) && isxdigit(buffer[pos-2]) && isxdigit(buffer[pos-1]) )
+                               pos -= 5;
+               }
                buffer[pos++] = STRING_COLOR_TAG;
-               buffer[pos++] = color;
+               if (color == -1)
+               {
+                       buffer[pos++] = STRING_COLOR_RGB_TAG_CHAR;
+                       buffer[pos++] = r;
+                       buffer[pos++] = g;
+                       buffer[pos++] = b;
+               }
+               else
+                       buffer[pos++] = color;
        }
        return pos;
 }
@@ -2154,11 +2436,11 @@ int Nicks_CompleteChatLine(char *buffer, size_t size, unsigned int pos)
        {
                size_t len;
                char *msg;
-               
+
                msg = Nicks_list[0];
                len = min(size - Nicks_matchpos - 3, strlen(msg));
                memcpy(&buffer[Nicks_matchpos], msg, len);
-               if( len < (size - 4) ) // space for color and space and \0
+               if( len < (size - 7) ) // space for color (^[0-9] or ^xrgb) and space and \0
                        len = Nicks_AddLastColor(buffer, Nicks_matchpos+len);
                buffer[len++] = ' ';
                buffer[len] = 0;
@@ -2167,7 +2449,7 @@ int Nicks_CompleteChatLine(char *buffer, size_t size, unsigned int pos)
        {
                int len;
                char *msg;
-               Con_Printf("\n%i possible nick%s\n", n, (n > 1) ? "s: " : ":");
+               Con_Printf("\n%i possible nicks:\n", n);
                Cmd_CompleteNicksPrint(n);
 
                Nicks_CutMatches(n);
@@ -2203,25 +2485,25 @@ void Con_CompleteCommandLine (void)
        int c, v, a, i, cmd_len, pos, k;
        int n; // nicks --blub
        const char *space, *patterns;
-       
+
        //find what we want to complete
        pos = key_linepos;
        while(--pos)
        {
-               k = key_lines[edit_line][pos];
+               k = key_line[pos];
                if(k == '\"' || k == ';' || k == ' ' || k == '\'')
                        break;
        }
        pos++;
 
-       s = key_lines[edit_line] + pos;
-       strlcpy(s2, key_lines[edit_line] + key_linepos, sizeof(s2));    //save chars after cursor
-       key_lines[edit_line][key_linepos] = 0;                                  //hide them
+       s = key_line + pos;
+       strlcpy(s2, key_line + key_linepos, sizeof(s2));        //save chars after cursor
+       key_line[key_linepos] = 0;                                      //hide them
 
-       space = strchr(key_lines[edit_line] + 1, ' ');
-       if(space && pos == (space - key_lines[edit_line]) + 1)
+       space = strchr(key_line + 1, ' ');
+       if(space && pos == (space - key_line) + 1)
        {
-               strlcpy(command, key_lines[edit_line] + 1, min(sizeof(command), (unsigned int)(space - key_lines[edit_line])));
+               strlcpy(command, key_line + 1, min(sizeof(command), (unsigned int)(space - key_line)));
 
                patterns = Cvar_VariableString(va("con_completion_%s", command)); // TODO maybe use a better place for this?
                if(patterns && !*patterns)
@@ -2238,12 +2520,12 @@ void Con_CompleteCommandLine (void)
 
                                // and now do the actual work
                                *s = 0;
-                               strlcat(key_lines[edit_line], t, MAX_INPUTLINE);
-                               strlcat(key_lines[edit_line], s2, MAX_INPUTLINE); //add back chars after cursor
+                               strlcat(key_line, t, MAX_INPUTLINE);
+                               strlcat(key_line, s2, MAX_INPUTLINE); //add back chars after cursor
 
                                // and fix the cursor
-                               if(key_linepos > (int) strlen(key_lines[edit_line]))
-                                       key_linepos = (int) strlen(key_lines[edit_line]);
+                               if(key_linepos > (int) strlen(key_line))
+                                       key_linepos = (int) strlen(key_line);
                        }
                        return;
                }
@@ -2378,12 +2660,12 @@ void Con_CompleteCommandLine (void)
 
                                        // and now do the actual work
                                        *s = 0;
-                                       strlcat(key_lines[edit_line], t, MAX_INPUTLINE);
-                                       strlcat(key_lines[edit_line], s2, MAX_INPUTLINE); //add back chars after cursor
+                                       strlcat(key_line, t, MAX_INPUTLINE);
+                                       strlcat(key_line, s2, MAX_INPUTLINE); //add back chars after cursor
 
                                        // and fix the cursor
-                                       if(key_linepos > (int) strlen(key_lines[edit_line]))
-                                               key_linepos = (int) strlen(key_lines[edit_line]);
+                                       if(key_linepos > (int) strlen(key_line))
+                                               key_linepos = (int) strlen(key_line);
                                }
                                stringlistfreecontents(&resultbuf);
                                stringlistfreecontents(&dirbuf);
@@ -2412,7 +2694,7 @@ void Con_CompleteCommandLine (void)
                Con_Printf("\n%i possible aliases%s\n", a, (a > 1) ? "s: " : ":");
                Cmd_CompleteAliasPrint(s);
        }
-       n = Nicks_CompleteCountPossible(key_lines[edit_line], key_linepos, s, true);
+       n = Nicks_CompleteCountPossible(key_line, key_linepos, s, true);
        if (n)
        {
                Con_Printf("\n%i possible nick%s\n", n, (n > 1) ? "s: " : ":");
@@ -2420,9 +2702,9 @@ void Con_CompleteCommandLine (void)
        }
 
        if (!(c + v + a + n))   // No possible matches
-       {               
+       {
                if(s2[0])
-                       strlcpy(&key_lines[edit_line][key_linepos], s2, sizeof(key_lines[edit_line]) - key_linepos);
+                       strlcpy(&key_line[key_linepos], s2, sizeof(key_line) - key_linepos);
                return;
        }
 
@@ -2434,7 +2716,7 @@ void Con_CompleteCommandLine (void)
                cmd = *(list[2] = Cmd_CompleteAliasBuildList(s));
        if (n)
                cmd = *(list[3] = Nicks_CompleteBuildList(n));
-       
+
        for (cmd_len = (int)strlen(s);;cmd_len++)
        {
                const char **l;
@@ -2454,33 +2736,33 @@ void Con_CompleteCommandLine (void)
 done:
 
        // prevent a buffer overrun by limiting cmd_len according to remaining space
-       cmd_len = min(cmd_len, (int)sizeof(key_lines[edit_line]) - 1 - pos);
+       cmd_len = min(cmd_len, (int)sizeof(key_line) - 1 - pos);
        if (cmd)
        {
                key_linepos = pos;
-               memcpy(&key_lines[edit_line][key_linepos], cmd, cmd_len);
+               memcpy(&key_line[key_linepos], cmd, cmd_len);
                key_linepos += cmd_len;
                // if there is only one match, add a space after it
-               if (c + v + a + n == 1 && key_linepos < (int)sizeof(key_lines[edit_line]) - 1)
+               if (c + v + a + n == 1 && key_linepos < (int)sizeof(key_line) - 1)
                {
                        if(n)
                        { // was a nick, might have an offset, and needs colors ;) --blub
                                key_linepos = pos - Nicks_offset[0];
                                cmd_len = strlen(Nicks_list[0]);
-                               cmd_len = min(cmd_len, (int)sizeof(key_lines[edit_line]) - 3 - pos);
+                               cmd_len = min(cmd_len, (int)sizeof(key_line) - 3 - pos);
 
-                               memcpy(&key_lines[edit_line][key_linepos] , Nicks_list[0], cmd_len);
+                               memcpy(&key_line[key_linepos] , Nicks_list[0], cmd_len);
                                key_linepos += cmd_len;
-                               if(key_linepos < (int)(sizeof(key_lines[edit_line])-4)) // space for ^, X and space and \0
-                                       key_linepos = Nicks_AddLastColor(key_lines[edit_line], key_linepos);
+                               if(key_linepos < (int)(sizeof(key_line)-4)) // space for ^, X and space and \0
+                                       key_linepos = Nicks_AddLastColor(key_line, key_linepos);
                        }
-                       key_lines[edit_line][key_linepos++] = ' ';
+                       key_line[key_linepos++] = ' ';
                }
        }
 
        // use strlcat to avoid a buffer overrun
-       key_lines[edit_line][key_linepos] = 0;
-       strlcat(key_lines[edit_line], s2, sizeof(key_lines[edit_line]));
+       key_line[key_linepos] = 0;
+       strlcat(key_line, s2, sizeof(key_line));
 
        // free the command, cvar, and alias lists
        for (i = 0; i < 4; i++)