diff --git a/IPython/core/displayhook.py b/IPython/core/displayhook.py index f60a764..55accc5 100644 --- a/IPython/core/displayhook.py +++ b/IPython/core/displayhook.py @@ -27,7 +27,7 @@ from IPython.config.configurable import Configurable from IPython.core import prompts import IPython.utils.generics import IPython.utils.io -from IPython.utils.traitlets import Instance +from IPython.utils.traitlets import Instance, Int from IPython.utils.warn import warn #----------------------------------------------------------------------------- @@ -54,6 +54,8 @@ class DisplayHook(Configurable): """ shell = Instance('IPython.core.interactiveshell.InteractiveShellABC') + # Each call to the In[] prompt raises it by 1, even the first. + prompt_count = Int(0) def __init__(self, shell=None, cache_size=1000, colors='NoColor', input_sep='\n', @@ -100,9 +102,6 @@ class DisplayHook(Configurable): pad_left=pad_left) self.set_colors(colors) - # other more normal stuff - # b/c each call to the In[] prompt raises it by 1, even the first. - self.prompt_count = 0 # Store the last prompt string each time, we need it for aligning # continuation and auto-rewrite prompts self.last_prompt = '' diff --git a/IPython/core/prompts.py b/IPython/core/prompts.py index 3cfba55..753659c 100644 --- a/IPython/core/prompts.py +++ b/IPython/core/prompts.py @@ -371,7 +371,14 @@ class Prompt1(BasePrompt): # auto-call prompts used in the auto_rewrite() method. self.col_p_ni = self.col_p.replace('\001','').replace('\002','') self.col_norm_ni = Colors.normal - + + def peek_next_prompt(self): + """Get the next prompt, but don't increment the counter.""" + self.cache.prompt_count += 1 + next_prompt = str_safe(self.p_str) + self.cache.prompt_count -= 1 + return next_prompt + def __str__(self): self.cache.prompt_count += 1 self.cache.last_prompt = str_safe(self.p_str_nocolor).split('\n')[-1] diff --git a/IPython/frontend/qt/console/frontend_widget.py b/IPython/frontend/qt/console/frontend_widget.py index 873a1f0..a228782 100644 --- a/IPython/frontend/qt/console/frontend_widget.py +++ b/IPython/frontend/qt/console/frontend_widget.py @@ -352,6 +352,7 @@ class FrontendWidget(HistoryConsoleWidget): # before writing a new prompt. self.kernel_manager.sub_channel.flush() + # TODO: The reply now has the next_prompt and prompt_number keys. status = reply['content']['status'] if status == 'error': self._handle_execute_error(reply) diff --git a/IPython/zmq/ipkernel.py b/IPython/zmq/ipkernel.py index 3b1c9cd..d9f9afc 100755 --- a/IPython/zmq/ipkernel.py +++ b/IPython/zmq/ipkernel.py @@ -16,7 +16,6 @@ Things to do: # Standard library imports. import __builtin__ -from code import CommandCompiler import os import sys import time @@ -31,9 +30,7 @@ from IPython.zmq.zmqshell import ZMQInteractiveShell from IPython.external.argparse import ArgumentParser from IPython.utils.traitlets import Instance from IPython.zmq.session import Session, Message -from completer import KernelCompleter from iostream import OutStream -from displayhook import DisplayHook from exitpoller import ExitPollerUnix, ExitPollerWindows #----------------------------------------------------------------------------- @@ -58,7 +55,7 @@ class Kernel(Configurable): # Build dict of handlers for message types msg_types = [ 'execute_request', 'complete_request', - 'object_info_request' ] + 'object_info_request', 'prompt_request' ] self.handlers = {} for msg_type in msg_types: self.handlers[msg_type] = getattr(self, msg_type) @@ -120,7 +117,15 @@ class Kernel(Configurable): reply_content = exc_content else: reply_content = {'status' : 'ok'} - + + # Compute the prompt information + prompt_number = self.shell.displayhook.prompt_count + reply_content['prompt_number'] = prompt_number + prompt_string = self.shell.displayhook.prompt1.peek_next_prompt() + next_prompt = {'prompt_string' : prompt_string, + 'prompt_number' : prompt_number+1} + reply_content['next_prompt'] = next_prompt + # Flush output before sending the reply. sys.stderr.flush() sys.stdout.flush() @@ -200,6 +205,15 @@ class Kernel(Configurable): return symbol, [] + def prompt_request(self, ident, parent): + prompt_number = self.shell.displayhook.prompt_count + prompt_string = self.shell.displayhook.prompt1.peek_next_prompt() + content = {'prompt_string' : prompt_string, + 'prompt_number' : prompt_number+1} + msg = self.session.send(self.reply_socket, 'prompt_reply', + content, parent, ident) + print >> sys.__stdout__, msg + def start(self): while True: ident = self.reply_socket.recv() diff --git a/IPython/zmq/zmqshell.py b/IPython/zmq/zmqshell.py index 720a0ea..fb7b88f 100644 --- a/IPython/zmq/zmqshell.py +++ b/IPython/zmq/zmqshell.py @@ -9,7 +9,7 @@ from IPython.utils.traitlets import Instance, Type, Dict from IPython.zmq.session import extract_header -class ZMQDisplayTrap(DisplayHook): +class ZMQDisplayHook(DisplayHook): session = Instance('IPython.zmq.session.Session') pub_socket = Instance('zmq.Socket') @@ -42,7 +42,7 @@ class ZMQDisplayTrap(DisplayHook): class ZMQInteractiveShell(InteractiveShell): """A subclass of InteractiveShell for ZMQ.""" - displayhook_class = Type(ZMQDisplayTrap) + displayhook_class = Type(ZMQDisplayHook) def system(self, cmd): cmd = self.var_expand(cmd, depth=2)