An easier way to manage language-specific indent levels

Posted by ericp on 2007-03-09 17:03
OS: All / Any | Product: Komodo | tags: editor indentation macros
Question: 

How do I get Komodo to use language-specific indentation levels? We use 2-space indentation for Ruby and HTML, 1-space for XML, and 4-space for everything else.

Answer: 

Time for another one of those tips to play with this weekend...

There are various internal reasons why Komodo doesn't have language-specific
indentation settings, but that hasn't stopped many people from asking for them.

However, this is a snap to implement with Komodo's trigger macros. Create
a new macro, and do the following:

1. Press the Triggers tab

2. Check "Macro should trigger on a Komodo event"

3. Select the "After file open" radio button.

You can have more than one trigger macro on a given event. The lower
the assigned rank, the later it fires. Your macros can even define
variables and functions into Komodo's global namespace, so an early-firing
macro could set a value that a later-firing one could use.

4. Finally, this code will control the indentations on load:

var language = komodo.document.languageObj.name;
// alert("Doc language = " + language );
var indentWidth = null;
switch(language) {
    case 'XML':
        indentWidth = 1;
        break;
    case 'Ruby':
    case 'RHTML':
    case 'HTML':
        indentWidth = 2;
        break;
}
if (indentWidth) {
    komodo.document.indentWidth = indentWidth;
}

The only problem with this trigger is that if you choose a
different indentation for a particular buffer, it will be
reset to the language default every time you load it.
I'll have to figure that one out.

cgchoffman
ActiveState Staff
Thu, 2011-12-08 16:10

Don't forget to replace "komodo.document.*" with "komodo.koDoc.*". Above you'll need to do it twice, once in line 1 and again in line 15.

EDIT:
This is due to the deprecation of komodo.document in Komodo 6 and it's subsequent removal from Komodo 7.

If you want to be backward compatible with Komodo 5- there is a method mentions in the Macro API:
http://docs.activestate.com/komodo/7.0/macroapi.html#macroapi_koDoc