×
Namespaces

Variants
Actions
Revision as of 14:32, 26 May 2011 by hamishwillee (Talk | contribs)

Archived:Codewarrior Scripting

From Nokia Developer Wiki
Jump to: navigation, search

Archived.pngArchived: This article is archived because it is not considered relevant for third-party developers creating commercial solutions today. If you think this article is still relevant, let us know by adding the template {{ReviewForRemovalFromArchive|user=~~~~|write your reason here}}.

Starting in Jan 2008, CodeWarrior will no longer be available for sale. Users will be able to upgrade their CodeWarrior products to Carbide.c++. In December 2008 Carbide 2.0 has been launched, this version is the first FREE no cost version so upgade is cheap. If you need to continue to use Code Warrior then consider the Bugs which will not be fixed due support ending January 2008. Code Warrior doea not support for phones announced in 2007/2008.


CodeWarrior has a (poor) scripting API that can be used to add nifty little helper features that can make the everyday programming task a bit more easier. This article describes how you can us this scripting interface.

Contents

Installation

All that needs to be done is to store the perl scripts to a folder somewhere and then adding a command binding in CodeWarrior to call the script. For each script I will give the binding that I use, but they can be bound to any key combination.

The steps to install a script are:

    1. From CodeWarrior menu select Edit->Commands and Key Bindings.
    2. Click "New Group" to create a group for the scripts if it does not already exist.
    3. Click "New Command" and give it a good name.
    4. In the "Execute" box browse the path for wperl.exe (C:\perl\bin\wperl.exe in my case)
    5. In the "Argument" box browse the path for the script and add possible command line parameters.
    6. Click "New Binding" and hit they key combination you want. You may need to remove the old binding for it.
    7. Click "Save" and you are done.

Limitations

As I already mentioned, the support that CodeWarrior provides is rather poor. This means that the scripting API is very limited and the scripts do not work in all windows. For example they do not work debugger windows or the error/warning window. The source file must be opened into a new window for CodeWarrior to successfully run the script. Usually it can be done by double-clicking for example the error description.

To Upper/Lower Case

This is a script that implements the conversion of selected text to upper or lowercase. This comes standard in Visual Studio, but is lacking from CodeWarrior.

File: upper_case.pl

use strict;
use warnings;
 
use Win32::OLE;
use POSIX;
 
# Create instance of CodeWarrior
my $CWApp = Win32::OLE->new("CodeWarrior.CodeWarriorApp");
 
# Get active document
my $doc = $CWApp->ActiveDocument() or die "can't get active doc!\n";
my $text_engine = $doc->TextEngine or die "can't get TextEngine!\n";
 
# Check that there is a selection
if( $text_engine->HasSelection() ) {
# Get selected text
my $selected_text = $text_engine->SelectionText();
 
my $param = uc $ARGV[0];
if ( $param eq "L" ) {
$selected_text = lc $selected_text;
} else {
$selected_text = uc $selected_text;
}
 
# Reset the selected line contents
$text_engine->InsertText( $selected_text );
}

Command Binding

To Upper Case:

Arguments:   C:\scripts\upper_case.pl
Key Binding: Ctrl-U

To Lower Case:

Arguments:   C:\scripts\upper_case.pl L
Key Binding: Ctrl-Shift-U

Open Folder

This script opens the file explorer to the folder of the active document. I find it useful when I step into someone else's code while debugging and want to see the header file which is usually located nearby.

File: open_folder.pl

use strict;
 
use Win32::OLE;
use POSIX;
 
# Create instance of CodeWarrior
my $CWApp = Win32::OLE->new("CodeWarrior.CodeWarriorApp");
 
# Get active document
my $doc = $CWApp->ActiveDocument() or die "can't get active doc!\n";
 
# Get the full path of the active document
my $file_spec = $doc->FileSpec();
my $path = $file_spec->FullPath();
 
# Strip the file name off
$path =~ s/(.+\\).+$/$1/e;
 
# Open explorer to containing folder
my $cmd = "explorer ".$path;
system( $cmd );

Command Binding

Arguments:   C:\scripts\open_folder.pl
Key Binding: Ctrl-å


Comment/Uncomment block

This script takes the selected block of code and either adds or removes "//" comments at each line. It is much more convenient than using the C-style /* */ comments because it doesn't break down when you nest them.

File: comment_block.pl

use strict;
use warnings;
 
use Win32::OLE;
 
# Create instance of CodeWarrior
my $CWApp = Win32::OLE->new("CodeWarrior.CodeWarriorApp");
 
# Get active document
my $active_doc = $CWApp->ActiveDocument() or die "can't get active doc!\n";
my $text_engine = $active_doc->TextEngine or die "can't get TextEngine!\n";
 
# Check that there is a selection
if( $text_engine->HasSelection() ) {
my $comment = "//";
 
# Get selected text
$_ = $text_engine->SelectionText();
 
my $param = uc $ARGV[0];
if ( $param eq "U" ) {
# Find comments from beginning of row and strip them off
s/^$comment(.*)/$1/gm;
} else {
# Match a line and add a comment in front of it
s/^(.+)$/$comment$1/gm;
}
 
# Reset the selected line contents
$text_engine->InsertText( $_ );
}

Command Binding

Comment Block:

Arguments:   C:\scripts\comment_block.pl
Key Binding: Ctrl-7

Uncomment Block:

Arguments:   C:\scripts\comment_block.pl U
Key Binding: Ctrl-Shift-7


Comment Parameters

This script has more to do with being bored at work than actual everyday need, but I added it here anyway. It takes a selected block of code and if it is a function definition, comments out each parameter with /* */ comments. Also it can comment and uncomment individual parameters when a single word is selected.

Note. I just recently noticed that it doesn't work if the EXPORT_C macro in the beginning of a function is also selected. It would be simple to fix, but I just haven't gotten around to it and it works if the macro is left out of the selection.

File: comment_params.pl

use strict;
use warnings;
 
use Win32::OLE;
 
# Create instance of CodeWarrior
my $CWApp = Win32::OLE->new("CodeWarrior.CodeWarriorApp");
 
# Get active document
my $doc = $CWApp->ActiveDocument() or die "can't get active doc!\n";
my $textEngine = $doc->TextEngine or die "can't get TextEngine!\n";
 
my $comment_start = "/*"; # /*
my $comment_end = "*/"; # */
 
my $DEBUG = 0;
 
# Check that there is a selection
if( $textEngine->HasSelection() ) {
my $commented_text;
 
# Get selected text
my $selected_text = $textEngine->SelectionText();
 
undef $/;
 
if ( $selected_text =~ / / ) {
# The string contained an empty space, assume it is a function
 
# Flag indicating if we should add or remove comments. 1 = add, 0 = remove
my $add_comment = 1;
 
if ( $selected_text =~ /\/\*/ ) {
$add_comment = 0; # comment already found, remove them
}
 
$commented_text = HandleFunction( $selected_text, $add_comment );
} else {
# No space, assume it is a type name and toggle the comment
$commented_text = ToggleComment( $selected_text );
}
 
# Set the new text content
if ( length( $commented_text ) > 0 ) {
$textEngine->InsertText( $commented_text );
}
}
 
exit 0;
 
sub ToggleComment {
$_ = shift;
 
# Try to remove /* and */ comments from beginning and end
if ( !s/^\/\*(.+)\*\/$/$1/ ) {
# Comments not found, add them
s/(.+)/$comment_start$1$comment_end/;
}
 
return $_;
}
 
# Handles commenting or uncommenting a function parameters
sub HandleFunction {
my $function = shift;
my $add_comment = shift;
my $ret;
 
# We are removing comments, just strip off the start and end tags ( /* and */ )
if ( $add_comment == 0 ) {
$function =~ s/\/\*//g;
$function =~ s/\*\///g;
# $function =~ s/$comment_start//g;
# $function =~ s/$comment_end//g;
return $function;
}
 
if ( $function =~ /^([\w*&]*?\s*?\w+?\s*?::\s*?\w+?\s*?\()([^)]*)(\).*$)/ ) {
my $func_name = $1;
my $params = $2;
my $func_end = $3;
 
# Print debug information
if ( $DEBUG ) {
print "Function name: $func_name\n";
print "Function params: $params\n";
print "Function end: $func_end\n\n";
}
 
my @result;
foreach ( split /,/, $params ) {
if ( /^(\s*)(.+\s+)(\w+)(\s*)$/ ) {
my $before = $1;
my $type = $2;
my $name = $3;
my $after = $4;
 
# Print debug information
if ( $DEBUG ) {
print " Param: $_\n";
print " before: $before\n";
print " type: $type\n";
print " name: $name\n";
print " after: $after\n\n";
}
 
$name = $comment_start.$name.$comment_end;
my $param = $before.$type.$name.$after;
push @result, $param;
} else {
push @result, $_;
}
}
$params = join ",", @result;
 
$ret = $func_name.$params.$func_end;
}
 
return $ret;
}

Command Binding

Arguments:   C:\scripts\comment_params.pl
Key Binding: Ctrl-P

Disabling WIN32 macro in CodeWarrior

The CodeWarrior compiler defines the _WIN32 preprocessor macro by default. This can have unwanted consequences when porting e.g. POSIX code to Symbian. You can undefine this macro by adding the following line to the MMP file:

OPTION CW -u _WIN32

This will only work on command line and not in projects imported to the CodeWarrior IDE.

Code Warrior Bugs

The old CodeWarrior (not longer supported by Nokia) IDE is not bugs free. Anyone who is still using it and is aware of any bug it has is welcomed to edit this page

Bugs


  • Multilingual support

Problem is discussed here (workaround): http://discussion.forum.nokia.com/forum/showthread.php?71712-CodeWarrior-Multi-lingual-app-support


  • Headers are missing in the source tree

Problem is discussed here (unfortunately without any solution): http://discussion.forum.nokia.com/forum/showthread.php?69383-Headers-folder-missing-CW-project-view


  • Compiling error

Problem is discussed here (solution was found): http://discussion.forum.nokia.com/forum/showthread.php?101263-Client-Server-strange-problem-on-3rd-device


  • Image resources are not be created when importing a project into IDE

Problem is described in Nokia Knowledge Base: KIS000304 - Image resources may not be created when importing a project into IDE

94 page views in the last 30 days.