2

我想从 node.js REPL 启动像 vi 或 emacs 这样的编辑器。

到目前为止,我已经尝试了两种方法:

  1. 节点插件

    这是我的editor.cc样子:

    const char *tempFile = "TEMP_FILE"; // File to be opened with the editor
    
    Handle<Value> launchEditor (const Arguments& args) {
        const char *editor = "vi";
        Local<String> buffer;
    
        pid_t pid = fork();
        if (pid == 0) {
            execlp(editor, editor, tempFile, NULL);
    
            // Exit with "command-not-found" if above fails.
            exit(127);
        } else {
            waitpid(pid, 0, 0);
            char *fileContent = readTempFile(); // Simple file IO code to read file.
            buffer = String::New(fileContent);
            free(fileContent);
        }
        return buffer;
    }
    
    
    // MAKE IT A NODE MODULE
    
    void Init(Handle<Object> target) {
        target->Set(String::NewSymbol("editor"), FunctionTemplate::New(launchEditor)->GetFunction());
    }
    
    NODE_MODULE(editor, Init)
    

    这在我有节点 v0.6.12(用 编译node-waf)时有效,但是当我将节点更新到 v0.8.1 时,此代码停止工作(用 编译node-gyp)。编辑器根本没有出现,文件内容被读取并返回(使用 emacs)或编辑器作为后台进程运行(使用 vi)!有什么我需要改变它才能与 0.8.1 一起工作吗?

    即使编辑器作为后台进程启动,我可以将它从代码本身带到前台吗?

  2. Child_process 模块

    spawn = require('child_process').spawn;
    editor = spawn('emacs', ['TEMP_FILE']);
    

    但这不能正常工作。使用 emacs,它会显示错误 input is not a tty,而 vi 会给出一个奇怪的界面。

有人可以为上述任何解决方案提供帮助,或建议其他一些可行的解决方案吗?

4

1 回答 1

0

大约一周前我偶然发现了一个,你应该试一试:node-editor

于 2012-07-20T02:52:47.747 回答