From bdae533027e12f2b25adfe75ef30f801d2a7a9a3 2014-12-05 18:58:58
From: Thomas Kluyver <takowl@gmail.com>
Date: 2014-12-05 18:58:58
Subject: [PATCH] Machinery to replace the current cell instead of adding a new one

---

diff --git a/IPython/core/interactiveshell.py b/IPython/core/interactiveshell.py
index a736d32..48374fe 100644
--- a/IPython/core/interactiveshell.py
+++ b/IPython/core/interactiveshell.py
@@ -2003,7 +2003,7 @@ class InteractiveShell(SingletonConfigurable):
                     continue
 
     @skip_doctest
-    def set_next_input(self, s):
+    def set_next_input(self, s, replace=False):
         """ Sets the 'default' input string for the next command line.
 
         Requires readline.
diff --git a/IPython/html/static/notebook/js/codecell.js b/IPython/html/static/notebook/js/codecell.js
index 497266f..d4a9e22 100644
--- a/IPython/html/static/notebook/js/codecell.js
+++ b/IPython/html/static/notebook/js/codecell.js
@@ -389,7 +389,7 @@ define([
      * @private
      */
     CodeCell.prototype._handle_set_next_input = function (payload) {
-        var data = {'cell': this, 'text': payload.text};
+        var data = {'cell': this, 'text': payload.text, replace: payload.replace};
         this.events.trigger('set_next_input.Notebook', data);
     };
 
diff --git a/IPython/html/static/notebook/js/notebook.js b/IPython/html/static/notebook/js/notebook.js
index 42c765e..ee0dfda 100644
--- a/IPython/html/static/notebook/js/notebook.js
+++ b/IPython/html/static/notebook/js/notebook.js
@@ -209,9 +209,13 @@ define([
         var that = this;
 
         this.events.on('set_next_input.Notebook', function (event, data) {
-            var index = that.find_cell_index(data.cell);
-            var new_cell = that.insert_cell_below('code',index);
-            new_cell.set_text(data.text);
+            if (data.replace) {
+                data.cell.set_text(data.text);
+            } else {
+                var index = that.find_cell_index(data.cell);
+                var new_cell = that.insert_cell_below('code',index);
+                new_cell.set_text(data.text);
+            }
             that.dirty = true;
         });
 
diff --git a/IPython/kernel/zmq/zmqshell.py b/IPython/kernel/zmq/zmqshell.py
index ee8038a..870773c 100644
--- a/IPython/kernel/zmq/zmqshell.py
+++ b/IPython/kernel/zmq/zmqshell.py
@@ -447,12 +447,13 @@ class ZMQInteractiveShell(InteractiveShell):
 
         return exc_content
 
-    def set_next_input(self, text):
+    def set_next_input(self, text, replace=False):
         """Send the specified text to the frontend to be presented at the next
         input cell."""
         payload = dict(
             source='set_next_input',
-            text=text
+            text=text,
+            replace=replace,
         )
         self.payload_manager.write_payload(payload)