Spotlight: Kate Scripting

Hey ho everyone.

Dominik asked me to blog about a feature in Kate that is still (sadly!) pretty unknown and seldom used: Kate Scripting. As you should know you can script KatePart completely via JavaScript. As those articles explain, it’s rather simple to write functions and put them into a file to have them reusable. But what for those write-use-throwaway kind of cases, where you simply need to get a job done quickly and don’t want to go through the overhead of writing some full fledged, documented, action-binded, localized script?

Utility Functions and why JavaScript rocks

Note: Neither map nor filter will be shipped with 4.5 to my knowledge, sorry about that. But you can still use the each helper (see below) to achieve the same with a bit more typing…

Take a look at utils.js on current git master: http://gitorious.org/kate/kate/blobs/master/part/script/data/utils.js

Put a special note on the helper functions map, filter and each and how they are used to implement e.g. rmblank, [rl]trim and the other functions. Cool eh? And the best part, you can reuse them directly from inside KatePart to get a job done:

mail-style quoting

Lets assume you write an email or use something like Markdown or Textile and want to quote. You’ll have to prepend a few lines with the two chars ‘> ‘. Instead of copy’n’pasting like a maniac do this instead and save yourself some breath:

  1. press F7 to open the Kate command line
  2. write e.g. map "function(l) { return '> ' + l; }"
  3. execute

Note: When you don’t have anything selected, the whole document will get “quoted”.

remove lines that match a pattern

This is something everyone needs to do sooner or later, happened quite a few times to me already. I bet vim has some esoteric command I cannot remember and emacs has something like C-x M-c M-butterfly. But with Kate most users only see search & replace and forfeit to the command line. Well, now it’s again a good time to use the command line:

  1. press F7 again
  2. write e.g. filter "function(l) { return l.indexOf('myNeedle') == -1; }"
  3. execute

Now all lines that contain ‘myNeedle’ will get removed. Sure, this is “verbose” but assuming you know JavaScript it’s actually quite easy, expendable and - best of all - good to remember. At least for me, YMMV.

shortcuts

For simple cases I’ve now introduced a shortcut way of doing the above, that saves you even more typing, but is limited to simple evaluations like the ones above. If you need something fancy, you’ll have to stick to the type-intensive way. Aynhow, here’s the shortcut version of the two scripts:

  1. map "'> ' + line"
  2. filter "line.indexOf('myNeedle') == -1"
the guts: each (interesting for users of KDE 4.x, x < 6)

Both of the above are implemented using the each helper I introduced even before KDE 4.4 afair. If you are using KDE 4.5 and want to do one of the above, a bit more typing is required:

  1. for map, you write something like this:
    each "function(lines) { return lines.map(function(l){ /** actual map code **/ }); }"
  2. for filter you do the same but replace map with filter:
    each "function(lines) { return lines.filter(function(l){ /** actual filter code **/ }); }"
Conclusion

You see, it’s quite simple and powerful. I really love map-reduce and how easy it is to use with JavaScript. Hope you like it as well.

PS: I actually think about making it yet even easier, by allowing some syntax like this: map '> ' + line or filter line.indexOf('myNeedle') == -1, must take a look on how hard it would be (beside the need for extensive documentation, but hey we have the help command in the Kate CLI, who can complain now? :) Implemented

Bye