Tag Archives:

komodo edit 中运行选定的 php 代码

komodo edit 是个挺好用的跨平台的编辑器。而且有很强大的扩展性。不仅有常规的运行外部程序功能,还能用javascript、python编写宏和扩展。

写程序的时候,常常需要测试一小段代码的结果。但是如果要先保存下来,再运行就有些麻烦了。就写了下面这个宏,只需要选定要运行的 php 代码,然后按一下快捷键,运行结果就会输出到 Command Output 面板中了。加了监听以后,还能中断运行。

// Macro recorded on: Thu Mar 24 2011 13:10:18 GMT+0800
komodo.assertMacroVersion(3);
var s = ko.views.manager.currentView.scimoz.selText;
var runsvc = Components.classes["@activestate.com/koRunService;1"].getService(Components.interfaces.koIRunService);
var cmd = "php";
var cwd = "";
var env = "";
var input = s.match(/^\s*<\?/) ? s : '<?php '+s;
input = input.match(/;\s*$/) ? input : input + ';';
ko.run.output.show(window, false);
var name = 'run selected PHP code';
ko.run.output.startSession(name, false, null, '', '', true);

function _terminationListener() { }

_terminationListener.prototype = {
    init: function (editor, command) {
        this._editor = editor;
        this._command = command;
    },
    onTerminate: function (retval) {
        this._editor.ko.run.output.endSession(retval);
        var msg = "'" + this._command + "' returned " + retval + ".";
        this._editor.ko.statusBar.AddMessage(msg, "run_command", 3000, retval ? 1 : 0);
    },
    QueryInterface: function (iid) {
        if (!iid.equals(Components.interfaces.koIRunTerminationListener) &&
            !iid.equals(Components.interfaces.nsISupports)) {
            throw Components.results.NS_ERROR_NO_INTERFACE;
        }
        return this;
    }
}

termListener = new _terminationListener();
termListener.init(window, name);
var process = null;
try {
    process = runsvc.RunInTerminal(cmd, cwd, env, ko.run.output.getTerminal(), termListener, input);
} catch (e) {
    ko.run.output.endSession(-1);
    return false;
}
ko.run.registerProcess(name, process);
ko.run.output.setProcessHandle(process);