Posts Tagged ‘kate’

Editors, Conventions and Evdenevenaklederiz.biz

Wednesday, January 7th, 2009

I went editor hunting again today. Lame, but I can’t help doing it time to time. My main editor is Kate for some time now. I use Pâté´s source browser when I am editing Python files. This is all fine for me. I love Kate. A multi-document editor and simple source inspection to jump through within the current file is all I need. But it gets hung. Depending on uptime it freezes up to ten seconds. This is sad.

A few days back I have installed SPE. All in all, it is a nice Python oriented editor1. I have had a good experience with it working on my main project. But when I tried to switch to my side project today, it did something amazingly stupid. When you change the workspace it fails to close currently opened files, and then adds them to the other workspace. Well, maybe it was me being amazingly stupid failing to figure out how to disable this feature. But this is unacceptable behaviour nevertheless.

I tried (again) eric after that. This time I didn’t panic when I saw an IDE style GUI on my screen. Eric should have done the job as well. I could have forgiven it not letting me customize the toolbars2… But it doesn’t show a static word wrap marker3! How can an editor feature dynamic word wrapping but could be unable to show a static word wrap marker?

When I was searching for something else, I had stumbled upon4 an e-mail message about KDevelop´s Class Browser. Did I ever mention that my searching skill is pathetic? It is. Anyway, I decided to try it out. So far so good. The class browser is not designed with Python in mind. But it is better than nothing. And the best news is the editor component is a KatePart. I’ll use KDevelop for programming and continue using Kate for other text editing, such as this one. We’ll see how it goes.

Conventions: A Necessary Evil?

OK, conventions are not evil at all. I have a bad habit of doing the opposite of what is dictated. It would be good thing if I had stopped and made an assessment. I should change from reactive to forethoughtful. Regardless, conventions from smart people are usually the right stuff.

For a long time I have been thinking; “why should I manually break lines, in this modern age of dynamic word wrapping” until I edited a configuration file on my server with Nano and spent two hours after, trying to find out why it wasn’t working. PEP8 says “Limit all lines to a maximum of 79 characters.“, do it. Because you can find yourself in a situation where you have to do without dynamic word wrapping and get your line breaks all messed up. Also, if you haven’t already done that; I would suggest you to take some time to read PEP8 and PEP257.

Another set of conventions I am interested in are Django conventions. I am in the process of splitting apps in my main project. I wasn’t planning a monolytic project in the beginning. But those two apps just got bigger and bigger and uglier… Now, as you can imagine; it is a painful process making them several smaller applications.

Why Am I Neglecting My Blog Lately

This main project I have been talking about from the beginning of this post is online now. You can visit here; Evdenevenaklederiz.biz (in Turkish). It is a simple tool for helping you find a mover. Critics and comments are very much appreciated.

I will be blogging regularly again soon. 3~5 posts per month is acceptable for me. Especially if at least one of them has some valuable information in it. I am not very ambitious about blogging.

Since this turned out to be more like a status update I should mention one last thing; my diet is finished last week. Currently I am 77 kgs, less than 15% fat. I am very happy with the results. For now my goal is to stay below 80 kgs, and focus on work. :D


1: Read: pretty much useless for anything other than Python.

2: Appereantly a newer version allows you to hide the toolbars.

3: You know that vertical line marking the 79th (or 80th) column.

4: No, not that stumbling upon.

Bookmark and Share

Extending Kate With Pâté

Wednesday, November 12th, 2008

Pâté is a plugin for Kate (of KDE Desktop) that exposes editor’s functionality to Python. In short; with Pâté, you can write Kate plugins in Python.

I use Kate for (almost) all my text editing. I think it suits my needs perfectly. It is both as simple as I would be comfortable with and has as many features (such as multi document interface, regex search and replace, etc.) as I need to be productive. I am not an IDE person. Nothing against IDE’s, I have just never been comfortable with them. On the other side I have never taken the time to learn the classic (read antique) editors such as Emacs and Vim. I am sure learning them would be worth my time. But I doubt I will ever take the time for that. At present, I just fire up my Kate and it works pretty well.

Meanwhile, I keep hearing about these neat hacks with Emacs that when you do C-t, C-M-w and then C-k your active buffer is translated to Chinese and then automatically sent to your grandmother’s cell phone as SMS. Wow! And you can write your own macros (in Lisp, elisp) to extend the editor’s capabilities. There is virtually no limit to what you can do and it is not rare that these extensions exceed the borders of an editor. Of course you have to use (and learn) Emacs to take advantage.

This was true, before I discovered Pâté. It was always possible to write plugins for Kate, but Kate was not scriptable. Now using Pâté, you can extend Kate with ease (at least much much easier than writing C). The only thing that bugs me is I couldn’t figure out a way to reload my plugins without restarting Kate.

Creating Simple Pâté Plugins

The first plugin we write will turn the selection into a Django password hash. I use this when I want to create initial_data fixtures for User’s quickly.

Since we do not want to instantiate a complete Django environment we won’t be able to import anything from django.contrib.auth.models. Instead let us copy a dumbed down version of get_hexdigest into our own module.

import random


def get_hexdigest(algorithm, salt, raw_password):
    try:
        import hashlib
    except ImportError:
        import sha
        return sha.new(salt + raw_password).hexdigest()
    else:
        return hashlib.sha1(salt + raw_password).hexdigest()

It should be clear enough, it returns a hash of the given password using the given salt[1]. Now we simply add kate to our imports (remember kate and not pate):

import kate

And add our own callback code:

@kate.onAction('Django Password', 'Shift+Alt+P')
def setPassword():
    v = kate.view()
    raw_password = v.selection.text
    v.selection.removeSelectedText()
    algo = 'sha1'
    salt = get_hexdigest(algo, str(random.random()), \
        str(random.random()))[:5]
    hsh = get_hexdigest(algo, salt, raw_password)
    v.insertText('%s$%s$%s' % (algo, salt, hsh))

The first line makes our function kate-aware. 'Django Password' will be the label for our menu item (it will be listed under Tools) and Shift-Alt-P will be the keyboard shortcut. The rest of the code should be self-explanatory.[2]

Now we copy our module into ~/.kde/share/apps/kate/pyplugins/ and restart Kate. It should show up in the menu and work now.

Second example is a JSON prettifier. I use JSON format for my fixtures, but valid JSON is not very readable. So I have this small plugin to convert a document between JSON and Python literals:

import sys, pprint
import kate
from django.utils import simplejson


HEADER = '# Pretty Printed\\n'

@kate.onAction('Django Pretty Json', 'Shift+Alt+J')
def togglePrettyJsonFormat():
    d = kate.document()
    source = d.text
    if source.startswith(HEADER):
        target = simplejson.dumps(eval(source))
    else:
        pp = pprint.PrettyPrinter(indent=2)
        target = HEADER + pp.pformat(simplejson.loads(source))
    d.text = target

I need HEADER to distinguish between two states. It can actually be anything, but it would be a good idea to make it a quote to have valid Python just in case.

Pâté Is Fun

I have enjoyed experimenting with Pâté. I hope it gets more attention and therefore ends up a much better plugin. If you ask me it should already ship with Kate. Kate is a nice editor, and empowering the users would only make it nicer and more popular.

If you have any ideas for pâté plugins, especially stuff that is useful in Django context, please add it to the comments. I would love to play a little more with pâté.


1: If it is not clear, take a look at django.contrib.auth.models.

2: To learn API, you can fire up Interactive Console under View and type help(kate).

Bookmark and Share