Tag Archives: plugins

Why KDE bindings simply rock (or: Creating our first Ruby plugin for Kate)

Akademy really speeds up development and communication between developers:
Yesterday I was poked by Milian Wolff and he told me how many people want to write Kate plugins in languages other than C++ and ECMAScript and how he always told them that’s not possible and probably a huge amount of work to get working. Well, that wasn’t entirely true:
If the target application has a SMOKE lib that wraps its API and an according bindings extensions, it works pretty much out of the box, thanks to the gorgeous KPluginFactory API and of course thanks to our gorgeous bindings.
Kate was a bit trickier to get working though, because it used old and deprecated API to load plugins. Well, I filed a merge request on gitorious about that, fixed a bug in KRubyPluginFactory and now I’m very proud to present

The absolutely gorgeous, simple, straight-forward, packed with all the Ruby goodness, very first Ruby Hello-World Kate plugin:

require 'kate'

# The module name has to be the capitalized name of the containing directory.
# In this case, the directory name is kate_ruby_test, so the module name _has_
# to be KateRubyTest.
module KateRubyTest

  # The main class name is the capitalized name of the main script. In this case it's 'test.rb',
  # so the main class name is 'Test'.
  class Test < Kate::Plugin

    # initializer
    def initialize parent, args = nil
      # call Kate::Plugin's constructor
      super parent, 'kate-ruby-hello-world'
    end

    def createView mainWindow
      # @componentData is automatically set to the plugin's component data after the constructor has run
      return KateRubyHelloWorldView.new mainWindow, @componentData
    end

  end

  class KateRubyHelloWorldView < Kate::PluginView
    slots 'slotInsertHello()'

    def initialize mainWindow, componentData
      super mainWindow

      # create the action defined in the ui.rc file
      @guiClient = Kate::XMLGUIClient.new componentData
      action = @guiClient.actionCollection.addAction('edit_insert_helloworld')
      action.text = KDE::i18n('Insert Hello World')
      connect action, SIGNAL('triggered(bool)'), self, SLOT('slotInsertHello()')

      # and add our client to the gui factory
      mainWindow.guiFactory.addClient @guiClient
    end

    def slotInsertHello
      return if self.mainWindow.nil?

      kv = self.mainWindow.activeView

      kv.insertText 'Hello World!' if !kv.nil?
    end

    def readSessionConfig config, groupPrefix
      # do something useful here
    end

    def writeSessionConfig config, groupPrefix
      # do something useful here
    end

  end

end

You’ll also need an ui.rc file that defines the actions to be merged into the Kate main window and a .desktop file which describes your plugin. Actually I’m too lazy to post them all here, so I made a nice package containing everything you need, together with a Makefile which can ‘make install’ the plugin into your home directory.

Since all SMOKE based bindings wrap the C++ API nearly 1:1 and only add syntactic language specific sugar on top, you can create nearly any plugin you like in any language, without modifying the host application, as long as the API is wrapped in a SMOKE lib and a bindings extension. In C# you can even create KIO slaves (a monodoc KIO slave example is shipped with the kdebindings tarball). That feature hasn’t been added to Ruby yet, but is on my ToDo list.

So praise the bindings, praise the KDE plugin infrastructure and start working on Ruby Kate plugins! 🙂

You can find the Ruby Kate binding and the SMOKE kate lib in current kdebindings trunk.