This page was last modified 15:26, 27 June 2007.
Codewarrior Scripting
From Forum Nokia Wiki
Contents |
Introduction
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.
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
| Related Discussions | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| On-device debugging, TRK, ARMV5, CodeWarrior | hubbabubba | General Symbian C++ | 1 | 2007-07-05 08:36 |
| CodeWarrior gets stuck at Linking | rock_win | Carbide.c++ and CodeWarrior Tools | 5 | 2006-10-06 07:18 |
| CodeWarrior build issues | petxolobo | Carbide.c++ and CodeWarrior Tools | 3 | 2006-04-19 15:37 |
| Codewarrior 3.1 error | mailkamlesh | Symbian Tools & SDKs | 1 | 2006-09-21 08:39 |
| Which one is better carbide c++ Express or codeWarrior | yogesh.sharma | Carbide.c++ and CodeWarrior Tools | 1 | 2007-03-21 11:57 |
