【问题标题】:How to launch editor like vi or emacs from node.js REPL?如何从 node.js REPL 启动 vi 或 emacs 等编辑器?
【发布时间】:2012-07-15 06:28:52
【问题描述】:

我想从 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. 子进程模块

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

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

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

【问题讨论】:

    标签: node.js editor read-eval-print-loop


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2015-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-09
      • 2014-04-29
      • 2012-11-20
      • 2011-04-12
      相关资源
      最近更新 更多