Skip to content

ActiveState Komodo and Abbreviation Expansion

January 27, 2007

Ask any 10 programmers for a favorite editor and you’ll get 30 different answers. I’m not stranger to this reality and I different editors for different tasks or programming languages.

I’ve been working like this for years and it all came to a sudden end when I saw the Ruby for Rails demo videos. To my astonishment the guys were typing a few keystrokes and the text was expanded to full code sequences, that usually, at least for me, take some “finger-work” and tons of typos…

This brought to life the ridiculousness of typing the same things hundreds, even thousands of times, doing the same mistakes over and over again.
Of course I like many others have been using code snippets that I paste when needed, especially for file headers, comments and such. I had never seen, or used, typing abbreviations that got expanded to full code with intelligent cursor positioning, or text block selection.

Conclusion I could no longer work as I’ve had for dozens of years…

The editor in use was TextMate, a MAC editor with no Windows version, so i started reviewing the Editors I’ve been using for a long time, like UltraEdit and others, and none supported this type of functionality.

I wrote a few macros for UltraEdit in a previous post but I soon realized that there are serious limitations in its macro engine that put the functionality at risk so to speak. The “find” macro command as no capability to search only selected text, so if your keyword exists any where in your code if would get expanded so to speak… this is a bad behavior that i have not been able to fix.

So I turned to ActiveState’s Komodo editor. I realized that the macro engine of Komodo is much more advanced and together with Code Snippets gave me the possibility to implement the functionality in an elegant way almost perfectly.

This code is based on excerpts from the JeffG’s Komodo Hack Blog and changed to support what I call Advanced code snippets.

Download the Macro code and install it in Komodo. Unfortunately this version can not be assigned to the TAB key but I found it to work very well with SHIFT+TAB.

Create code snippets in the same ToolBox folder where you placed the macro and the name of each Snippet is your expansion keyword. Remember to check the two check boxes at the bottom of the window and place the cursor, or selected text block where you want it to be after the code insertion.

You do not need to assign a key to the snippet.

In this version of the macro code it is possible to expand the functionality of the basic snippet by using a very simple syntax as follows.


the tag ReplaceMe> (case sensitive) gets replaced by your keyword text, and the <sel>your text here</sel> will be where the cursor will be placed after insertion.
All text between the <Sel> and </Sel> tags will be selected after insertion. In this example the word self will be selected and ready to be replaced if needed.

The way to expand this type of blocks is a little bit different than the basic snippets. Instead of using the snippet’s name as the abbreviation to be expanded an advanced snippet’s name is just the start of the abbreviation’s name. Using the previous example we would write

submy_sub_name

and it would be expanded to


=item my_sub_name( )

=cut

sub my_sub_name {
my ( $self ) = @_;


}

so you would need to type the snippet’s name (sub in this case) followed by the text you would like to place in the ReplaceMe tag that shows in the snippet’s body.

If you want to add more snippets you’ll need to edit the expansion macro and add the snippet’s name to the keywords function.


function keywords(word) {

// Repeat this block for your own advanced snippets
var m = word.match(/^sub(.*)/);
if( m ) {
return ['sub',m[1]];
}
// end block

m = word.match(/^ht(.*)/);
if( m ) {
return ['ht',m[1]];
}
}

the format is simple. To add a Snippet called func you would change you would add


m = word.match(/^func(.*)/);
if( m ) {
return ['func',m[1]];
}

Then add a Snippet called func.

Creating an Advanced Snippet called ht with the following body


<<ReplaceMe>>
<Sel></Sel>
</<ReplaceMe>>

Will allow us to expand any HTML tag without having to define every single Snippet for each tag. Just type:

httable

will get expanded to


<table>

</table>

you get the picture!

Hope you enjoy this macro, and if you make improvements let me know.

Advertisements
2 Comments
  1. david permalink

    Just one micro point: the expansion of the last macro doesn’t show up because it’s unescaped HTML…

  2. poster permalink

    Fixed!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: