Scripting in Kate

Since several releases, Kate Part has scripting support through javascript. So far, it seems it is not much used by users. Still, I stumbled over two extensions:

If you have more user defined scripts, it would be nice if you let us know! For KDE5, we plan to extend this, so applications like Kile or KDevelop can reuse Kate’s internal code.

On another note, here is a nice blog about the upcoming KDE 4.8 release :-)

21 thoughts on “Scripting in Kate”

  1. I could need the scripting functionality if it was possible to invoke an external script, push some text (e.g. the current selection) via STDIN into it and do something with the STDOUT output of the script, e.g. replace the current selection. AFAIK this is not possible with kate scripting. So I am using medit for such things.

  2. No just KDevelop and Kile, every text field / label / lineedit / textedit / textbrower / webkit. KDE need an equivalent to Android Linkify subsystem. This would be great. There was a GSOC for that a while ago, but (i) had to give up because of internal conflict between using QtQuick/JS or KROSS. Then once it was sorted out (the final decision was that we would need two complete, competing system!?!?, I was against Kross), I had no time left and had to give up and find a real job. But that was some years ago. Having regex based text trigger and other script to kaction goodies would still be awesome for mobile and powerusers.

  3. I’m very tempted to learn the API for this, but I’d like to first ask if this is possible/allowed:

    I want auto-brackets/quotes that, upon typing the second quote/bracket/parenthesis, will simply move the cursor outside of the set.

    Right now, if I try to type ‘(‘, it will make ‘(|)’ (where the | is the position of the cursor). I want this, but I sometimes, without realizing it, type both ‘()’. But it instead does ‘()|)’.

    I realize there’s an extension for this, but I also like the non-extension (builtin) features of selecting text and hitting the parenthesis, and it’ll wrap the selection in the parenthesis. So, I figured I wanted to script/extend/modify the built-in feature instead.

  4. I have ported the latest zen-coding JScrpt plugin for Kate. I’ve been using it for a few weeks and it works well. I did this because the Insane (not ZEN) HTML coding (light edition) extension lacked some very useful features. If you’re interested in a copy, let me know. One awesome addition for the kate js api would be creating user dialogs to ask for input. I am currently doing this by asking for input on the command line, but this has its own problems.

      1. Here’s the script. All you have to do is copy it to /home/YOUR-USERNAME/.kde4/share/apps/katepart/script or /usr/share/apps/katepart/script but this may vary depending on your system but ultimately it needs to be in $KDEHOME/share/apps/katepart/script. Then just start kate and under Tools/Scripts you should see all the functionality of zen I’m exposing. You can change the shortcuts at Settings/Configure Shortcuts/Kate Part or you can hardcode new defaults in the zen.js file itself under function action(cmd).

        I hope this works for you. For the record, I’m using this with Kate 4.8.3 on Arch Linux. I have not tested it on anything else so YMMV.

        Enjoy

        1. One last thing. “Wrap With Abbreviation” requires you to select some text, trigger the action (Default Shortcut Ctrl+Shift+w) and then input the abbreviation that will wrap the selection. Since Kate’s JavaScript API has no way of displaying an input box, the action, when triggered, will open the kate command line and place itself there. Simply enter the abbreviation you want after the action name (delimited by space) and press enter. Zen will then expand the abbreviation and wrap your selection with it.

          I’ve included all this in the script itself as a comment.

  5. So is there any chance it will be possible to invoke external programs in future versions of Kate scripting? I’ve looked into this multiple times, but always found that the APIs provided are simply not sufficient to do what I want. This would not be such a problem if it was possible to invoke external programs to do the actual work (e.g., python scripts).

    Additionally, I have some external programs which do non-trivial stuff (e.g., code reformatting). I seriously do not want to replicate them in javascript, especially not since I already *have* working programs and all I would need would be a way to get the text into them, and out of them using the scripts.

    1. For a lot of stuff you can use the text filter plugin and pipe selected text through external programs, and with that you can e.g. reformat the text.

      But right, a js function to invoke external programs does not exist, yet. How would it look like? system.exec(“app with parameters”) ?

  6. I still have 3.4.5 version of Kate with KDE 4.4.5 (Debian stable). I’ve tried to copy script files in the appropriate directories without any effect in Kate (scripts do not appear in the plugins conf of Kate.
    Is there any chance I can make it work ?
    Tia.

  7. Excellent script Ivan- good work and thank you for a great effort.

    Kate 3.8.4 under Kubuntu 12 did not have the ‘katepart/script’ path, which had to be created. Once done, the script works perfectly.

Leave a Reply