Elod P Csirmaz’s Blog: October 2010

## 29 October 2010

### Macros in Kate editor version 2.5.5

Update: For macros in Kate version 3.8.4, please see this post.
It turns out that despite appearances, it is possible to write macros in Kate, as it supports an ECMAScript (JavaScript) -based scripting language. What's more, these macros, if set up correctly, can be invoked with a couple of keystrokes only.

Although the official Kate handbook describes these scripts in detail, the documentation is for Kate version 3.4, whereas I'm using 2.5.5. Many of the functions listed in the handbook are not available, so I decided to post about the ones I found under 2.5.5.

My main aim was to create simple macros that insert some text at the cursor.

Location of the scripts

The scripts should be located in a share/apps/katepart/script folder. It might be necessary to search a bit, as the exact location might be different on each system. Scripts under Kate 2.5.5 do not have functions in them, so you'll have to name the files the way you'll invoke the macros. For example, if you want to run a macro by typing s or st, then the filename will have to be s.js or st.js. More on how to run a macro later.

Writing the script

This is a short script that can be used to insert some text at the point where the cursor currently is. The following should be the entire contents of the file.
text = "TextToBeInserted";
document.insertText(view.cursorLine(),view.cursorColumnReal(),text);

And that's it. You can use the usual things in the string, like \n or \".

If you want other functions or objects, you can look at the scripts already there in the directory, or look into the Kate source code (Google will do that for you), and use try and error.

Running the script / macro

You'll have to restart Kate after creating or modifying the script files. I don't think there's a way to tell Kate to reload them. After that's done, you can run a macro by typing its name in the command line. Press F7 to get to the comand line, type, for example, s or st, and hit ENTER. I know it's 3 (or more) keystrokes, and not one, but it's better than having no macros at all.

## 27 October 2010

### Testing for bless-edness in Perl

Using ref($o) to test whether$o is an object is obviously not enough, as it might be a simple unblessed reference. The following solution is almost perfect:

UNIVERSAL::can($o,'isa') This tests if$o is a descendant of UNIVERSAL, where the isa method is defined.

However, the above is also true of classes! That is, in the following case, the condition is true:

package mypackage;

package clientcode;
if( UNIVERSAL::can('mypackage','isa') ){ ... }
As an aside, let me note that isa can also be called in a class context, which means that the following is also true:

package mypackage;

package clientcode;
$o = 'mypackage'; if($o->isa('mypackage') ){ ... }
All in all, it seems the only reliable way of testing whether something is an object is to test whether it can 'isa' and to test whether it is a reference as well.