Influence end of word search pattern in macros

Hello all,

working with chord sheets for songs, I want to automate a editing task of this kind:

C        F7+         Am2  Am
In every heart there is a room 

should transfer to

[C]In every [F7+]heart there [Am2]is a [Am]room 

So I recorded this macro

// Macro recorded on: Sun Aug 23 2015 18:21:27 GMT+0200 (CEST)
komodo.assertMacroVersion(3);
if (komodo.view) { komodo.view.setFocus(); }
ko.commands.doCommand('cmd_endOfWordExtend')
ko.commands.doCommand('cmd_cut')
ko.commands.doCommand('cmd_lineNext')
komodo.view.selection = '[]';
ko.commands.doCommand('cmd_left')
ko.commands.doCommand('cmd_paste')
ko.commands.doCommand('cmd_linePrevious')
ko.commands.doCommand('cmd_wordLeft')

When I place the cursor on the last chord in the line and run this macro it is moved down correctly and the cursor places on the beginning of the previous chord. The only thing is the F7+. The plus sign is considered as a word delimiter, so the “cmd_wordLeft” and “cmd_endOfWordExtend” do not work fine for me.

Is there a possibility to select everything from the cursor until the next space? Can I use a different command than cmd_wordLeft that would bring the cursor from the “Am2” position to the beginning of “F7+”?

Thank you all, folks

Sounds like a cool project. :smile:

I think Komodo’s help file has what you need. Open it with F1 and drill down to “Macros” → “Macro API” → “JavaScript Macros” → “The scimoz Object”. This section explains how to access lower level parts of the editor. There is a lot there, so if you dig in but need some help, feel free to ask here.

Based on the scimoz help, here is a quick-and-dirty example macro that stores all the text from the cursor until the next whitespace character:

ko.views.manager.currentView.setFocus();
const scimoz = ko.views.manager.currentView.scimoz;

//scimoz.currentPos <-- "The location (in character units) of the caret."
//scimoz.getWCharAt(pos) <-- "Get the (Unicode) character at the specified position."

//Store the result here
const result = []

//Stop at any one of these characters
const whitespace = ' \t\r\n'

//Our current reading position
var pos = scimoz.currentPos;

//Make sure we stop at the end of the editor text
while (pos !== scimoz.length) {
    
    //Grab a character and move our position ahead by 1
    var c = scimoz.getWCharAt(pos++);

    if (whitespace.indexOf(c) !== -1) {
        //It's a stopping point so we're done
        break;
    }

    //It was good, so stash it
    result.push(c)
}

//Done!
alert("Found this word: \"" + result.join('') + "\"");

You can use require("ko/editor").scimoz() instead of ko.views.manager.currentView.scimoz

scimoz() is a helper function of ko/editor, it’s not smart to default to it because a lot of its functionality have been abstracted in ko/editor. Have a look at the beta documentation: http://beta.docs.komodoide.com/SDK/api/commonjs/editor