• Kate: Scripted Actions

    by  • July 9, 2010 • Developers • 6 Comments

    Finally, I came around to implement scripted actions for Kate in KDE SC 4.6. Let’s take a look at how this works. When Kate starts, it searches for $KDEDIRS/share/apps/katepart/script/ for *.js files. As example, let’s take a look at utils.js there:

    /* kate-script
     * author: Dominik Haumann
     * license: LGPL
     * revision: 3
     * kate-version: 3.4
     * type: commands
     * functions: moveLinesDown
     */
    
    function moveLinesDown()
    {
      var fromLine = -1;
      var toLine = -1;
    
      var selectionRange = view.selection();
      if (selectionRange.isValid() &&
          selectionRange.end.line < document.lines() - 1)
      {
        toLine = selectionRange.start.line;
        fromLine = selectionRange.end.line + 1;
      } else if (view.cursorPosition().line < document.lines() - 1) {
        toLine = view.cursorPosition().line;
        fromLine = toLine + 1;
      }
    
      if (fromLine != -1 && toLine != -1) {
        var text = document.line(fromLine);
    
        document.editBegin();
        document.removeLine(fromLine);
        document.insertLine(toLine, text);
        document.editEnd();
      }
    }
    
    function action(cmd)
    {
      var a = new Object();
      if (cmd == "moveLinesDown") {
        a.text = i18n("Move Lines Down");
        a.icon = "";
        a.category = "";
        a.interactive = false;
        a.shortcut = "";
      }
    
      return a;
    }
    
    function help(cmd)
    {
      if (cmd == "moveLinesDown") {
        return i18n("Move selected lines down.");
      }
    }

    What happens is the following:

    1. the header tells kate that there is an exported function “moveLinesDown”
    2. so when Kate Part is loaded, it calls “action(moveLinesDown)” to check whether this function should be exposed in the GUI. Here, we return the action info that includes the displayed text, an icon, a category, whether the script needs user input (interactive) and a default shortcut. Of course, you can change the shortcuts, and also configure the toolbars to show the actions.

    With this, every user is able to script arbitrary editing functions for Kate Part. We don’t have to implement all those helpers in C++ anymore. The result looks like this:
    You can have this already now, you just have to use the development version of Kate :)

    About

    Dominik is a PhD student at the Control Theory and Robotics Lab, TU Darmstadt, as part of the Research Training Group GKMM (GRK1362). My research focuses on state estimation in distributed systems. As hobby, I contribute to the KDE project and work on the Kate application and editor component.

    http://www.kate-editor.org

    6 Responses to Kate: Scripted Actions

    1. July 11, 2010 at 02:04

      in action() function, does a has to be an array? It looks like it can just be an object

      • haumann
        July 17, 2010 at 10:08

        Yep, I now changed it to a Object. Thanks for the pointer :)

    2. Kevin Colyer
      December 29, 2010 at 21:21

      Yes!!!! Finally! the featue I have waited for – I even coded a JS script a year ago but then realised there were no key bindings!!! Thanks for this.

    3. Anonymous
      January 1, 2011 at 13:12

      JS only? What about Kross?

    4. April 2, 2011 at 14:28

      Idea: How to code a Script that shows links to all functions and attributes at Kate’s sidebar. (enables browsing code particles at sidebar and one-click link to line of a function or attribute on source code. If user has clicked twice the script would go to next similar line that has that attribute or function =)

    5. Pingback: Move Cursor to the Next/Previous Paragraph in Kate « kucrut