BigDiver

Dive away, explore the net…

Archive for the ‘UltraEdit’ Category

ActiveState Komodo and Abbreviation Expansion

with 2 comments

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.

Written by bigdiver

January 27, 2007 at 1:06 pm

UltraEdit Tab Expansion Macros

leave a comment »

It is a shame that for the moment UltraEdit and UEStudio do not support TAB keyword expansion. After testing it for a while i found the editor to be really good but lacking, in my view, this extremely important feature.

After messing around with the configuration for a while, trying the auto completes, templates, etc, i found none of these features really didn’t do what i wanted. Expand a keyword as soon as one presses the TAB key, leaving the cursor at the proper position for continuing your edit as fast as possible.

so i started playing around with the macros and after much head banging and trial and error
(mostly error) I wrote the set of macros attached to this post that do what I wanted.
I don’t know the limitations on the number of commands per macro, or if this is the best way to achieve the proposed goal, so if you find a way to improve this please be my guest, just let everybody else know about it.

The perfect solution would be for UltraEdit to support this feature, by reading a text file with keywords and their expansions or something like that, but for the moment you can use this workaround.

The macros assume that you have UltraEdit configured to automatically insert the closing Prentiss, or curly braces (Advanced->Configuration->IDE->InteliTips->Miscellaneous)
And Smart Placement of “}” when placed automatically is also checked.

After loading the macros assign the TabExpand macro to the TAB key. If you do this remember that the autocomplete feature will be disabled. You can always use a different key thought.

The structure of the macros is simple:

create a new macro for each tab keyword expansion you want. call it TE_keyword

write the macro as follows

InsertMode
ColumnModeOff
HexOff
PerlReOn
Find MatchWord Select “your_keyword
Replace SelectText “your_expanded_text
IfFound
PlayMacro 9 “TE_Left”
EndIf

replace the your_keyword with the keyword you like and do the same with your_expanded_text, which is the text that should be placed after the keyword is “expanded”.

After the IfFound command you can place more macro instruction, either to place the cursor properlly or insert more text.

to activate the newly created macro modify the TabExpand macro and add the following code to it:

PlayMacro 1 “TE_keyword”
IfSel
Else
ExitMacro
EndIf

The IFSel is a hack in order for the macro to exit if there was any replacement done. meaning the keyword was expanded in the previous call to PlayMacro. This is not very beautiful but works. If you know of a better way to do this let me know…

Please take a look at all the predefined macros in the file for more details on how to use the TabExpand “system”

Download the macro file from here.

Written by bigdiver

January 25, 2007 at 6:10 am

Follow

Get every new post delivered to your Inbox.