Color Matching Parenthesis Sets in Word?

Posted by: Tim

Color Matching Parenthesis Sets in Word? - 21/05/2013 10:05

Ok, my google-fu is especially weak this morning.

I've had other editors that color matched sets of parenthesis to keep track of what set you were in (vim, eclipse, etc). Does anybody know how to set up a macro, addin, or anything to get this functionality in Word?

The basis for this question is a long, complicated, and sordid story about using the incorrect tool for the job, but I'm stuck with what we have.

Thanks for any help!
Posted by: tfabris

Re: Color Matching Parenthesis Sets in Word? - 21/05/2013 22:21


I have extensive experience writing macros in Word's VBA language, and I'm convinced that you could write something which matched parenthesis sets and then color coded them, based on some behavioral parameters you'd have to code in there. (Such as: are indentations/spacing important or ignored? Do you want it to be just parens, or all types of brace-like characters? Do you want it to ignore comments? If so, what computer language is it, so that you can also put in comment ignoring and comment color-coding. Do you want it to ignore braces found inside string literals? If so, what are your string literal delimiters in your language, and if you're doing that well then you might as well color-code your string literals... it goes on and on.).

As you can see, though I know it's do-able, it would be a complex task as you'd essentially be writing a code language parser, and as such it would need to be language-specific. Word's VBA macro language would be up to the task I think.

If, however, Google isn't finding anyone who's already done it, then you might be the first person to need to do it in Word. In that case, have fun, you're on your own, there's no WAY I'm tackling this one for you. :-)

Note: I'm pretty sure it's going to be much easier to do it as a macro that you run, as opposed to something that you do on-the-fly while typing. In other words, run a macro to color-code your entire document, rather than expect it to color-code it on the fly while you type. Though VBA could do it, I don't expect it could do it fast enough for it to be realtime.

Now you've got my curiosity piqued. I could imagine someone who wrote coding books would want something like this. A section of code, in a book, if it were correctly color-coded, would be so much easier to understand than just plan black text. I wonder if someone's written such a thing.
Posted by: Tim

Re: Color Matching Parenthesis Sets in Word? - 22/05/2013 10:04

The only thing I've managed to find was specifically for authors, it only did one paragraph at a time and ignored all the other brace types other than parenthesis.

I've been piecing together how to sling something like this together. I also came to the conclusion that it would have to be run as a check and not in the background.

It might be a decent project for my upcoming vacation to keep me out of (too much) trouble.
Posted by: tfabris

Re: Color Matching Parenthesis Sets in Word? - 22/05/2013 15:47

Aha. The google term you're looking for is SYNTAX HIGHLIGHTER.


http://stackoverflow.com/questions/2556862/syntax-highlighting-in-ms-word-document

The general consensus out there seems to be:
- Paste your code into a code editor which already syntax-highlights your code the way you want,
- Then either copy/pastespecial that code from the editor into Word, where it will preserve the color formatting, or, use the code editor's "export as RTF/HTML" function, which will also preserve the color formatting.

The thing is, I don't know if there are any existing code editor tools out there which will bracket-match the way you want them to. You could experiment with some of them and find out.

If not, then, there are some after-the-fact syntax highlighters available such as:

http://www.planetb.ca/2008/11/syntax-highlight-code-in-word-documents/
http://alexgorbatchev.com/SyntaxHighlighter/
http://www.gnu.org/software/src-highlite/
etc...

And some of those are open source. If you really wanted to dig in, you could take an existing syntax highlighter and modify it to include the bracket-matching that you want.
Posted by: tfabris

Re: Color Matching Parenthesis Sets in Word? - 22/05/2013 16:12

So far, it's looking like Notepad++ is the one with the most advanced controls over syntax highlighting. Although I haven't yet found a way to get it to color code all braces differently, there is a setting to color code *bad* braces in a special way, which is a step in the right direction. Also, they've made Notepad++ to be very customizable and extensible, so it seems like there SHOULD be a way to color-code all braces I'll bet.

Once we've figured out how to do that, you just go up to the Plugins menu in Notepad++ and select "NPPExport->Copy RTF to Clipboard" and paste the pretty formatted text into Word.
Posted by: tfabris

Re: Color Matching Parenthesis Sets in Word? - 22/05/2013 16:19

Hm, it seems that the bad brace highlighting only occurs when you place the insertion cursor on the bad brace. At all other times the bad braces are not highlighted. That's suboptimal and doesn't help you. Hm.
Posted by: Tim

Re: Color Matching Parenthesis Sets in Word? - 22/05/2013 19:25

You are having a lot more luck than I am finding the stuff smile

I haven't tried Notepad++ (even though it is installed on this machine). What I have been doing is copying the section of code I was interested in into a vim window. That allowed me to find mismatched pairs, but I would rather have them highlighted when the cursor wasn't on the character.

At least now I have better starting points than the poorly cobbled together idea in my head.

Thanks for the help.
Posted by: larry818

Re: Color Matching Parenthesis Sets in Word? - 23/05/2013 01:50

I once coded on an HP 9845 which could change the color of the text you were typing by means of colored keys across the top of the keyboard. I used these colors for exactly this purpose.

I know it's not automated the way you want, but it became kind of a motor skill to change the colors as I went along. I didn't even think about it after a while.

You could do this easily in Word using "character style" keyboard shortcuts.

Even easier is to add an Xkeys pad so you don't have to remember where the colors are on the keyboard.

Posted by: Tim

Re: Color Matching Parenthesis Sets in Word? - 23/05/2013 09:35

That isn't a bad idea, and a lot simpler.