From 80b5dbf3c9e3b1ce012ca93f68cd9a61fe8d5369 Mon Sep 17 00:00:00 2001 From: Sami Samhuri Date: Sat, 5 Nov 2011 16:32:01 -0700 Subject: [PATCH] seed the editing tmp file with the last line of history --- lib/index.js | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/index.js b/lib/index.js index 79aa1e0..d06dc77 100644 --- a/lib/index.js +++ b/lib/index.js @@ -98,7 +98,7 @@ function extendRepl(aRepl) { function edit(cmdFile, editor) { editor = editor || process.env['VISUAL'] || process.env['EDITOR'] - // TODO seed the file with repl.context._ if the file doesn't exist yet + var fds = [process.openStdin(), process.stdout, process.stdout] , args = [cmdFile] @@ -108,6 +108,19 @@ function edit(cmdFile, editor) { args = words.slice(1).concat(args) editor = words[0] } + + // seed the file with repl.context._ if the file doesn't exist yet + try { + fs.statSync(cmdFile) + } + catch (e) { + // skip history[0], it's the .edit command + var lastCmd = theRepl.rli.history[1] + if (lastCmd && lastCmd[0] !== '.') { + fs.writeFileSync(cmdFile, lastCmd) + } + } + pause() spawn(editor, args, {customFds: fds}).on('exit', function(code) { // some editors change the terminal resulting in skewed output, clean up