Code Colorizer

Maintainer:
pitonyak
Rating:
4.25
Average: 4.3 (8 votes)
Application:
OpenOffice.org
Tags:
format, macro, code, highlight
Source code:
http://www.pitonyak.org/oo.php
Post date:
Saturday, 10 November, 2007 - 18:49
Statistics
Week: 61 - Month: 264 - Year: 2,385 - Timeline
Download extension
System Independent version - All releases
Compatible with OpenOffice 4: Unknown
User feedback:
Compatible with OpenOffice 4.x?

Note that I changed the name from Code Colorizer from Code Formatter to remove the impression that code is actually reformatted with respect to whitespace.

8085 Assembler, X86 Assembler, StarBasic, C++, C#, Java, PERL, Python, R, and XML can be color coded for a better appearance.

StarBasic is formatted like the OOo IDE. Java colors mimic those in Eclipse.

A block of code is assumed to be formatted based on the following paragraph styles: _OOoComputerCode, _OOoComputerCodeLastLine, _code, _code_first_line, _code_last_line, or _code_one_line.

Numerous color code examples are shown in AndrewMacro.odt, along with a complete description of the add-in.

I received a special request for Python support. I spent about an hour adding support and I have only tested this on two samples.
Python, however, I added by special request, I have only spent about an hour working on the code. Also, I have not used Python, so I cannot easily test the code.

This extension provides the ability to colorize code segments. There are two modes of operation. The first mode (the least used mode for me) is to select the code that you want to colorize, and then use the menu options under: Tools > Add-Ons > MacroFormatterADP

I use a specific paragraph style for my code. I usually use _OOoComputerCodeLastLine for the last line of code in a segment, and _OOoComputerCode for the rest. So, for four lines of code, the first three use _OOoComputerCode and then the last line uses _OOoComputerCodeLastLine. This group then defines an entire code snippet.

Character styles are used for each portion of the code; for example, there is a character style for keywords, and another for literals. This is how color is assigned.

The code that identifies the specific portions is certainly not complete. In other words, I did not write a complete grammar interpreter for each supported language. My intention is to provide something that is "close enough" for regular use. That said, if you find glaring issues, be certain to let me know so that I can fix them. Also, if you fix errors, please share your solution. This latest version supports PERL. I use variable names such as "shall_print", and I noticed that "print" was colorized as a keyword. I corrected this for PERL. If you find something similar in another language, let me know and I will fix it.

When I say that I am only looking for "good enough" support, this means that I may fail under strange situations. An example of an untested, but expected failure is as follows:

Create a single line of code such as "a = 1; // line comment".

If you select the code inside the double quotes, I expect that the single line comment will continue to the end of the paragraph without verifying that this runs past the ends of the selected text. If this is tested and found to be in error, I will fix the problem.

Final note: although a later version of OOo is specified as required, this is only because of the method that the extension uses to set menus. If you must use an older version of OOo, feel free to download the extension, use "unzip" to extract the libraries, and then manually import the libraries; they should work just fine on most any version of OOo.

If a language that you desire to color code is not supported, feel free to contact me directly and ask. Some languages are easy to add.

Code Colorizer

Version Operating system Compatibility Release date
3.0.1 System Independent 3.1, 3.2, 3.3, 3.4 01/06/2012 - 23:59 More information Download
2.4.7 System Independent 3.1, 3.2, 3.3, 3.4 21/04/2011 - 16:40 More information Download
2.4.6 System Independent 3.1, 3.2, 3.3, 3.4 01/04/2011 - 23:44 More information Download
2.4.4 System Independent 3.1, 3.2, 3.3, 3.4 24/06/2010 - 11:07 More information Download
2.4.2 System Independent 3.1, 3.2, 3.3, 3.4 14/12/2009 - 17:51 More information Download
2.4.1 System Independent 3.1, 3.2, 3.3, 3.4 28/10/2009 - 19:05 More information Download
2.4.0 System Independent 3.1, 3.2, 3.3, 3.4 20/08/2009 - 11:50 More information Download
2.3.7 System Independent 3.1, 3.2, 3.3, 3.4 18/08/2009 - 12:49 More information Download
2.3.6 System Independent 3.1, 3.2, 3.3, 3.4 10/08/2009 - 21:05 More information Download
2.3.0 System Independent 3.1, 3.2, 3.3, 3.4 20/06/2009 - 11:14 More information Download
2.2.2 System Independent 3.1, 3.2, 3.3, 3.4 15/06/2009 - 10:56 More information Download
2.2.1 System Independent 3.1, 3.2, 3.3, 3.4 14/06/2009 - 22:44 More information Download
2.1.1 System Independent 3.1, 3.2, 3.3, 3.4 06/06/2009 - 21:38 More information Download
2.1.0 System Independent 3.1, 3.2, 3.3, 3.4 03/06/2009 - 14:28 More information Download
2.0.0 System Independent 3.1, 3.2, 3.3, 3.4 03/06/2009 - 09:47 More information Download
1.1.0 System Independent 3.4 07/05/2008 - 16:50 More information Download

Comments

Version 2.1.0 is only 22 KB against 236 KB of the 2.0.0 version.

Bernard Marcelly's extension compiler is awesome, and I use it. To create the extension, you place your extension creation document in the directory with the files to be distributed. I accidentally included a few extra unused files in this build directory. If you have a copy of both, change the file extension to zip and then use your favorite zip program to take a look at the extra contained files.

When I realized my error, I decided to push 2.1.0 earlier than planned. Version 2.0.0 color codes entire paragraphs. Version 2.1.0 allows for color coding selected text, so you can select a statement embedded in regular text and color code it. I also added a few other minor items such as a "proper" add-on description in English, French, German, and Spanish; at least as well as my limited language abilities allow.

I am able and willing to provide the full creation documents and source to create the full extension, you need but ask! Also, if you find any problems or desire specific improvements, drop me a line...

Andrew Pitonyak

Thank you. It works great, but supports only the English locale? To make it work I had to replace "Default" with the name of this style used in my locale. In French it is named "Standard", in Spanish "Predeterminado", in Russian it is called "Базовый" etc. All the instances of "Default" have to be replaced(?) Please, can you add something that automatically checks the locale and uses the correct style name for this locale?

My usual usage is as follows:

[text paragraphs formatted with a text style]
[code formatted with a code paragraph style]
[code formatted with a code paragraph style]
[code formatted with a code paragraph style]
[code formatted with a code paragraph style]
[text paragraphs formatted with a text style]

Place the text cursor anywhere in the code paragraph style and tell the macro to format the code. The macro looks both before and after the cursor to find all of the code paragraph styles and then adds color to the text using character styles. For this to work, you must use specific paragraph styles. If the default paragraph style is set to be a "code" style, then the macro will attempt to colorize the regular text.

If you select the text and choose to only format the selected text, however, the paragraph style does not matter.

Does this make sense?

If you have locale specific names for code styles, however, I am more than happy to incorporate those names into the macro, but the default paragraph style is not a good choice I think.

This line:
oProps() = Array(CreateProperty("ParentStyle", "Default")
uses the style named "Default".

It can't create code styles because of this, because different languages use diferent names for default style. However, "Colorize Selected" works without the code styles, indeed.

OK, now this has long since been corrected. We also have the ability now to recognize XML as well as raw strings in the C++0x standard.

There are two similar fonts used: Courier and Courier New. But OOo is slowing down when there is a font that is missing (especially on Linux). In my case "Courier" is missing in the list of fonts (Ubuntu), but I have Courier 10 Pitch and Courier New. When scrolling pages it is a bit slowing down as if there was a font that is missing and Writer trying to find a suitable replacement. Courier was automatically replaced by Courier 10 Pitch, however it was somehow "Italic" (with a smaller inclination than a true Italic) and at the same time it was just "Bold" according to all dialogs (?). Windows XP virtual machine shows the "Courier" font as if it were a Times New Roman. So I tried to change the "Courier" to "Courier 10 Pitch" or "Courier New" in the Styles module of this extension and then OOo scrolled the document fast and without small delays (and the font is not Italic anyhow). Please, take a look at this.

Please, use this to refer to the default style: oStyles(0).DisplayName

Rather than throw together a "dirty" solution for the style names, I have been working on a configuration system. I am trying to keep things simple for my first shot at this, with a schema to hold the configured values. So, you will be able to run a configuration dialog and then choose the paragraph style and the font. These values will be stored in a schema so that they will be remembered the next time that the macro is run.

Unknown argument "enableasync" error here:
oConfigProvider = GetProcessServiceManager().createInstanceWithArguments(_
"com.sun.star.configuration.ConfigurationProvider",_
Array( CreateProperty( "enableasync", bEnableSync ) ) )

Good catch.... The configuration stuff broke from this:

http://qa.openoffice.org/issues/show_bug.cgi?id=101955

Exchanged email with Stephan and I opened this

http://qa.openoffice.org/issues/show_bug.cgi?id=111970

Sign "_" should be green, and the digit following that sign should be green. For example, "A_1" is green, "A_a" is green. In CodeFormatter 2.4.2 in "A_1" the symbol "_" is blue, the symbol "1" is red.

I need to color my Tcl/Tk code, which i don't find your extension. thanks

I can confirm that this extension does work in OOo 4.0.1 on my Mac and Windows machines. The basic functionality works, which is all I require. Some issues regarding Paragraph Type does not work, but formatting the selected text does.