Tutorial: Copying the entire console buffer to clipboard

Post tutorials on how to do certain tasks within game or engine code here.
Post Reply
Baker
Posts: 3666
Joined: Tue Mar 14, 2006 5:15 am

Tutorial: Copying the entire console buffer to clipboard

Post by Baker »

I think Inside3d should serve as the central depot for engine code now that Quakesrc.org is dead and it is clear it won't ever be back. Here is a tutorial I made a while back @ QuakeOne.com (original thread) and I think this is needs to be here:

Adding "copy" command to Quake

Type "copy" in the console and all that text is now on your clipboard and you can paste it somewhere, instead of the various hassle methods of using condump (if you use an engine with that) or using -condebug + qconsole.log.

In console.c (adapted from FitzQuake's condump command)

Code: Select all

/*
================
Con_Copy_f -- Baker -- adapted from Con_Dump
================
*/
void Con_Copy_f (void)
{
	char	outstring[CON_TEXTSIZE]="";
	int		l, x;
	char	*line;
	char	buffer[1024];

	// skip initial empty lines
	for (l = con_current - con_totallines + 1 ; l <= con_current ; l++)
	{
		line = con_text + (l%con_totallines)*con_linewidth;
		for (x=0 ; x<con_linewidth ; x++)
			if (line[x] != ' ')
				break;
		if (x != con_linewidth)
			break;
	}

	// write the remaining lines
	buffer[con_linewidth] = 0;
	for ( ; l <= con_current ; l++)
	{
		line = con_text + (l%con_totallines)*con_linewidth;
		strncpy (buffer, line, con_linewidth);
		for (x=con_linewidth-1 ; x>=0 ; x--)
		{
			if (buffer[x] == ' ')
				buffer[x] = 0;
			else
				break;
		}
		for (x=0; buffer[x]; x++)
			buffer[x] &= 0x7f;

		strcat(outstring, va("%s\r\n", buffer));

	}

	Sys_CopyToClipboard(outstring);
	Con_Printf ("Copied console to clipboard\n");
}
In Con_Init in console.c

Code: Select all

Cmd_AddCommand ("copy", Con_Copy_f); // Baker - copy console to clipboard
In sys_win.c (straight out of FuhQuake)

Code: Select all

// copies given text to clipboard
void Sys_CopyToClipboard(char *text) {
	char *clipText;
	HGLOBAL hglbCopy;

	if (!OpenClipboard(NULL))
		return;

	if (!EmptyClipboard()) {
		CloseClipboard();
		return;
	}

	if (!(hglbCopy = GlobalAlloc(GMEM_DDESHARE, strlen(text) + 1))) {
		CloseClipboard();
		return;
	}

	if (!(clipText = GlobalLock(hglbCopy))) {
		CloseClipboard();
		return;
	}

	strcpy((char *) clipText, text);
	GlobalUnlock(hglbCopy);
	SetClipboardData(CF_TEXT, hglbCopy);

	CloseClipboard();
}
In sys.h

Code: Select all

void Sys_CopyToClipboard(char *);
The End. Compile and startup Quake and type "copy" in the console and the entire console will be copied to the clipboard.
Post Reply