2

node.js REPL から vi や emacs などのエディタを起動したい。

私は今まで2つのアプローチを試してきました:

  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

ちょうど 1 週間ほど前に 1 つ見つけたので、試してみてください: node-editor

于 2012-07-20T02:52:47.747 に答える