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

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

The Mission of KDE’s Wikis

KDE has three wikis: TechBase, Community and UserBase. The separation has the following meaning according to http://wiki.kde.org:

  • TechBase: The primary place for high quality technical information about KDE targeted at 3rd party developers, ISVs and system administrators.
  • Community: The working area for the KDE community. It provides a place for sharing information within the community and coordinating community teams.
  • UserBase: The home for KDE users and enthusiasts. It provides high quality information for end users on how to use KDE applications.

So TechBase is a source of mostly technical information. This includes step-by-step howtos for all sorts of KDE development as well as the feature plans and schedules for KDE releases and so forth. It’s mainly static content. Think of a howto for a Plasma Widget or a howto for building KDE. The content usually is valid for a long time, mostly even for years. For those of you longer in the KDE project, TechBase is the same as our good old developer.kde.org page (and we’ve never put arbitrary content there). The only difference is, that it’s now maintained as wiki.

UserBase is the same thing for users. It provides information about tips and tricks for KDE application, links to further pages an so on. It’s also rather static content. This is also why TechBase and UserBase both feature the “high quality” term.

This leaves us with the youngest of our wikis: the Community wiki. The purpose of the Community wiki is to give the KDE community a place where to coordinate. Think of leaving notes for thoughts. Or a list of contributors attending a conference. The current todo-list of a KDE project. In other words: The community wiki is a scratch pad for all sorts of content that does not really fit into TechBase or UserBase. If you are unsure, the community wiki is most likely the right place.

Currently, there is a lot of content on TechBase that never was supposed to be put there. This is because the Community wiki did not exist earlier. So what should be moved from TechBase to the Community wiki? All the articles in the Projects page without exception belong in the Community wiki. Just read through the list and you’ll get the impression that it’s more a dumping ground for some project related content. This is exactly what we NOT want on TechBase. However, people keep putting content there. So this is an appeal to the KDE community: Please use the community wiki! You are free to do there whatever you want :) We have to take care to keep TechBase maintainable (this is also why techbase uses the subpages to structure the content). Sometimes less is more, so please take the time to remove Projects pages no longer needed, or move them to the community wiki. And especially put new content to the Community wiki instead of TechBase. Thanks!!!

Join Kate Development!

The Wheel of Time turns… meaning that the Kate Project has quite along history by now. The Kate Project was started back in December 2000, so it’s almost 10 years old. Development sometimes continues with a fast pace; and at other times there is almost no progress for weeks. But all in all, looking back at those 10 years, we can proudly tell you that the project is very much alive. Let’s take a look at the traffic of our mailing list:

The traffic itself does not say much about the development. It probably tells us that there are quite a lot of people using Kate and reporting wishes and bugs. This is a good thing :) The commit statistics for Kate, KWrite and the KTextEditor interfaces look like this:

Before 2000, Kate did not exist yet. Instead, so these commits come from the KWrite days. The commit rate is quite constant according to this statistic. In fact, there usually are always several developers working on Kate. Some only for a short period to implement single features or fix some bugs, and some long-term contributors. A very nice trend is given by the fact that the core team of Kate grew from about 4 to 7 developers over the last two years. So the Kate Project is very healthy – nice! And of course we are happy for every single contribution. So if you want to get involved in Kate development, join us!