Welcome, guest | Sign In | My Account | Store | Cart

Komodo doesn't automatically wrap lines when they reach a certain length, which is a good trait to have in a code editor. However, for those times where you're editing non-code, it can be helpful to have newlines inserted automatically. Troy Topnik addressed this problem a couple of years ago, in his post http://www.activestate.com/blog/2008/11/revenge-auto-wrap-type-type-type-ding . As Komodo 6 heads out the door, I've been writing blog posts, and found this feature useful. But his solution used two macros. I put my coding hat back on, and reimplemented it as a "toggle macro".

JavaScript, 53 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
if (!('extensions' in ko)) ko.extensions = {};
if (!('toggers' in ko.extensions)) ko.extensions.togglers = {};
var methodName = "*ext*autoWrap";
var methodObject;
if (!(methodName in view)) {
    var view = ko.views.manager.currentView;
    var scimoz = view.scimoz;
    methodObject = ko.extensions.togglers[methodName] = function(event) {
        if (event.keyCode === 0 && event.charCode == event.DOM_VK_SPACE) {
            if (scimoz.getColumn(scimoz.currentPos) > 72) {
                komodo.doCommand('cmd_newline');
                event.preventDefault();
                event.stopPropagation();   
            }
        } else if (event.keyCode == event.DOM_VK_ESCAPE) {
            methodObject.toggleOff();
        }
    };
    methodObject.active = false;
    var macro = ko.macros.current;
    methodObject.macro = macro;
    methodObject.orig_iconurl = macro.iconurl;

    methodObject.toggleOn = function() {
        view.addEventListener('keypress', methodObject, true);
        methodObject.active = true;
        if (komodo.view) {
            komodo.view.setFocus();
            if (komodo.view.scintilla) { komodo.view.scintilla.focus(); }
        }
        methodObject.columnPos = scimoz.getColumn(scimoz.currentPos);
        methodObject.macro.iconurl = 'chrome://famfamfamsilk/skin/icons/bell.png';
        methodObject.macro.save();
        // Watching view.blur won't fire when the view is closed.
        window.addEventListener('current_view_changed', methodObject.toggleOff, false);
    }
    
    methodObject.toggleOff = function() {
        view.removeEventListener('keypress', methodObject, true);
        window.removeEventListener('current_view_changed', methodObject.toggleOff, false);
        methodObject.active = false;
        methodObject.macro.iconurl = methodObject.orig_iconurl;
        methodObject.macro.save();
    }
} else {
    methodObject = ko.extensions.togglers[methodName];
}

if (!methodObject.active) {
    methodObject.toggleOn();
} else {
    methodObject.toggleOff();
}

There sure is a lot of common code between this recipe and my vertical editing toggle macro, at http://code.activestate.com/recipes/577406-implementing-vertical-editing-in-komodo-6/?in=user-4166930 . Yes, a framework for toggle macros isn't far away.

1 comment

Eric Promislow (author) 13 years, 7 months ago  # | flag

It turns out there's an ugly bug in these toggle macros: when I add the function onto the view object, Komodo crashes at shutdown time. Strictly speaking, this won't affect your work, but we never like it when Komodo crashes.

The solution is to store all the objects in our own extension space, ko.extensions.toggers["ext<name>"], and not off the view object.

I'll go update this recipe accordingly.

Created by Eric Promislow on Mon, 4 Oct 2010 (MIT)
JavaScript recipes (69)
Eric Promislow's recipes (11)

Required Modules

  • (none specified)

Other Information and Tasks