Sunday, November 01, 2009

Scripting Kate

In my last blog I explained Kate's scripting features in KDE 4.4. To better understand how scripting can be used let's look at some use cases.

  • join lines: This feature request wants the action "join lines" to not join different paragraphs, i.e. not remove empty lines. We have not implemented this wish, as there are probably users who prefer the current behaviour. This request can be fixed by writing a small script that joins the lines according to the user's wishes.

  • reformat paragraph: An intelligent reformatter for paragraphs. Should be rather straight forward to implement.

  • XML tools: In KDE3, Kate once had a xmltools plugin. Unfortunately noone ported it to KDE4. The plugin provided lots of very useful features for xml editing. For example, you could select text and then wrap it with xml elements, e.g. "text" would become "<para>text</para>". This is a perfect example for a command line script as well. Any volunteers? :)

Scripting also brings us closer to fixing the following reports:

  • macro system: Kate still does not have a macro system. A macro can be interpreted as a group of scripts, executed in a sequence (more or less). The vi input mode already supports pretty complex commands, and the code for scripting is all there. It's just a matter of putting this together so it's usable for users.

  • word count: maybe the word count features can be implemented by a script (too slow?). Problem is, that a script cannot show dialogs etc.

To make scripting an even better experience, we still need to implement binding shortcuts to scripts. Again: any volunteers? :)


Anonymous said...

To really be useful (at least for me) it must be possible not only to define hotkeys for scripts, but also having the possibility to call an external programm, pipe something to STDIN, read its output from STDOUT and continue scripting with this new content.

I am waiting for such functionality since many years now and it is really great to see the first step implemented. But as long as these two features are missing I still have to use nedit or medit. I keep the hope up.

dhaumann said...

@yellow snow: can you give examples?
Piping text through stdin and replace the selection with stdout works already through external tools.

Anonymous said...

Hello dhaumann,
we are implementing web-applications using perl and a perl-embedded-HTML or HTML-embedded-perl macro language called CIPP (can be found on cpan).

Now if you type something like "sql" and press a hotkey behind this word, the editor is automatically selecting defined characters around the cursor (in this case "sql"), pipes this to a perl script that knows it and returns the complete tag that is replacing the "sql" string:
<?SQL SQL=""
MY VAR="">

I you instead type "sql+" and press the hotkey some more optional attributes are inserted in the sql-tag.

kate does have something like snippets, but it is not so comfortable AFAIK.

Another example is automatically opening other files: E.g. if you have an CIPP-Include it looks like:
<INCLUDE NAME="foo.|bar">
(the pipe shows an example for the cursor position)

If you now press a hotkey the editor automatically selects the characters around the cursor (where [a-zA-Z._-] are allowed. This selection is given to an external script that does know about the logic of include names (and also module names, etc.), finds the regarding filename and opens this file for you in the editor. (Alternativally it could return the full filename and the editor could open it itself).

I did implement such features in nedit and medit, but could not find a way to do this in kate, because writing a complete plugin in c++ looks too complicated for me.