Komodo: Open current selection as file

When switching to Komodo Edit, I missed the ability to open file named by the current selection. Luckily, because of the macro support, getting this feature is fairly easy. This macro will re-enable that feature. It opens the current URL in the editor. If the URL appears to be relative, it lets Komodo resolve the URL, but it also supports absolute URIs and for absolute URL’s it has support for the file, http, ftp and scp protocols. If there is no current selection, you will get prompted to type a URL (this is a deliberate choice, because if I want to browse for a document, I can use the regular Open dialog).
So, how do you get this? Create a JavaScript macro with the following contents, and bind it to your favorite key (mine is Ctrl-Alt-O):
bc.. try {
komodo.assertMacroVersion(2);
if (komodo.view) { komodo.view.setFocus() };
var sel = komodo.editor.selText;
if (!sel.length)
{
sel = prompt(“Choose filename”);
}
// resolve path if not absolute
var re = new RegExp(“^(\/|(file|http|ftp|scp):\/\/)”);
if (!re.test(sel))
{
var dir = komodo.document.displayPath.replace(komodo.document.file.baseName,”);
sel = dir+sel;
}
else
{
var re = /^((http|ftp|scp):\/\/[^\/]+)/;
if ((sel[0] == “/”) && (re.test(komodo.document.displayPath) ))
{
var matches = re.exec(komodo.document.displayPath);
if (matches[1])
{
sel = matches[1]+sel;
}
}
}
komodo.openURI(sel);
}
catch(e)
{
alert(e)
}
p. *Edit:* Small fix to make relative URI’s work properly on remote files.

Previous Post

1 Comment

  1. Asbjørn Ulsberg

     /  2007-03-23

    Nice one! 🙂 I’ve never even thought about the possibility, but this can be really handy.