The one where I catch up (er, Visual Studio Macros)

October 29, 2008 – 8:49 pm

Since I’ve been using the newer Visual Studios (starting with VS.Net 2003) an annoying issue has been how it copies code into the clipboard. See, I’m an avid fan of the Dark Side of Visual Studio and have been ever since I came across it in the Borland IDE years ago. However, when using these dark themes there appears to be an issue when copying and pasting from Visual Studio to applications like Outlook. The problem is that it keeps the theme settings and pastes the text as is into the target.

For example, here’s an image of some code from Visual Studio 2005. You’re welcome to hate me over the color settings. Cie la vie. I’ve heard them all, too.

Here’s that same code pasted directly into this blog post:

xmlDocPtr getdoc (char *docname)

{

xmlDocPtr doc;

doc = xmlParseFile(docname);

    
 


if
(doc == NULL )

{

        return NULL;

}

    
 


return
doc;

}

Ugh. Since I regularly email code snippets to people, this requires manually editing the code after it’s pasted into an email message or Word doc or whatever.

Paste Special allows you to choose from RTF or unformatted text or unformatted Unicode text. However, “Paste Special” isn’t really helpful here because it pastes the text with a white foreground, which isn’t particularly useful on a white background.

Here’s where I catch up with the times. Turns out, Visual Studio has these cool things called macros. Never heard of ‘em! Why, in my day, we did everything by hand in our IDE. And it snowed both ways, uphill. And we liked it. Or, something like that.

Anywho, macros. As is usually the case, I’m not the only one bothered by this nuisance. It’s just I’m the only one who didn’t go looking for a code solution. I just kept editing the code by hand. And I liked it. I liked it so much; I did it several times a day. Up hill, both ways.

I don’t know what got into me, but today I’d finally had enough. I went looking for a solution.

This link will take you to Jeff Atwood’s solution. Go ahead, read it now. I’ll wait here. Jeff points out that VS copies text to the clipboard in Rich Text Format (RTF). This is supported by the “Formatted Text (RTF)” in the Paste Special choices. Jeff wanted to copy HTML from VS to paste into web pages or text boxes. I just wanted plain text. So, I expanded his macro a bit to do what I wanted. I added a function, RTFtoText that strips all of the RTF tags from the copied text and leaves the line breaks and tabs intact. There’s probably a more eloquent solution, but this works for me. I didn’t bind the macro to Ctrl-C yet, so I still have one extra key stroke or mouse click. I’ve mapped the CodeToText macro function to Alt+C in the text editor, so my key combination is Ctrl+C and the Alt+C. Then I can go to Outlook or Word or whatever and Ctrl+V. Way easier than Ctrl+C, go to Outlook, Ctrl+V, then highlight the new text remove the background color. Rehighlight the new text and change the font color to black.

So, I figure that one key stroke will save 30-40 seconds per instance. That adds up after a while. It’s worth the time it took me to work on this, which was a couple of hours or so.

Here’s the sample shown pasted into an email message after applying the CodeToText macro:

I was going to show a sample here after running Jeff’s RtfToHtml, but it doesn’t quite get it right for C source. I’ll have to spend some time on that later, but for now this does exactly what I want for emails.

Download the FormatToHtml macro (6kb)

After that success, I started playing with all of the Sample macros provided by Visual Studio. Some of the more useful ones I found were the ones that added comments (AddFunctionDescription, AddRevisionMarks, etc.) and navigated to places in the code (BeginningofFunction). I modified AddFunctionDescription and AddRevisionMarks to add block comments formatted the way I like them. I also copied BeginningofFunction and created EndofFunction from it. Vim has the same browsing features, and I’ve found these quite useful. I’m glad there’s an equivalent in VS. I also added a macro that adds a comment to the end of the selected line. I do this a lot and I figure this macro will shave a couple of seconds from that activity.

I feel sad that I’d not found macros before. Of course, I knew they existed but I’ve never played worked with them. Now, I’m glad I’m did, and that I didn’t just keep doing things like we did in the old days. Up hill, both ways.

Coffee helps me clear the fog. If you've found any of the posts on this blog helpful, I could always use more coffee.

Post a Comment