From 48d6fff419c1fb115dd831b2026bc2cdc17e97fc 2010-09-12 09:50:35 From: Fernando Perez Date: 2010-09-12 09:50:35 Subject: [PATCH] Add init_environment(), %less, %more, %man and %clear/%cls, in zmq shell. These are all things that normally need direct terminal control and users are likely to call them via !, blocking their session because the pager will be invoked and we can't pass keystrokes to it. So for the zmq shell, we implement a few very common things as magics. --- diff --git a/IPython/core/interactiveshell.py b/IPython/core/interactiveshell.py index 82eb15b..00de609 100644 --- a/IPython/core/interactiveshell.py +++ b/IPython/core/interactiveshell.py @@ -227,6 +227,7 @@ class InteractiveShell(Configurable, Magic): # These are relatively independent and stateless self.init_ipython_dir(ipython_dir) self.init_instance_attrs() + self.init_environment() # Create namespaces (user_ns, user_global_ns, etc.) self.init_create_namespaces(user_ns, user_global_ns) @@ -396,6 +397,10 @@ class InteractiveShell(Configurable, Magic): # interactive statements or whole blocks. self.input_splitter = IPythonInputSplitter() + def init_environment(self): + """Any changes we need to make to the user's environment.""" + pass + def init_encoding(self): # Get system encoding at startup time. Certain terminals (like Emacs # under Win32 have it set to None, and we need to have a known valid @@ -1869,7 +1874,7 @@ class InteractiveShell(Configurable, Magic): Parameters ---------- cmd : str - Command to execute (can not end in '&', as bacground processes are + Command to execute (can not end in '&', as background processes are not supported. split : bool, optional diff --git a/IPython/core/magic.py b/IPython/core/magic.py index 2029e26..31ba558 100644 --- a/IPython/core/magic.py +++ b/IPython/core/magic.py @@ -577,7 +577,6 @@ Currently the magic system has the following functions:\n""" """Provide extra detailed information about an object. '%pinfo2 object' is just a synonym for object?? or ??object.""" - print 'pinfo2 par: <%s>' % parameter_s # dbg self.shell._inspect('pinfo', parameter_s, detail_level=1, namespaces=namespaces) @@ -2509,6 +2508,7 @@ Defaulting color scheme to 'NoColor'""" # atab.append(k, v[0]) print "Total number of aliases:", len(aliases) + sys.stdout.flush() return aliases # Now try to define a new one @@ -2917,11 +2917,8 @@ Defaulting color scheme to 'NoColor'""" except ValueError: var,cmd = '','' # If all looks ok, proceed - out = self.shell.getoutput(cmd) - if opts.has_key('l'): - out = SList(out.splitlines()) - else: - out = LSString(out) + split = 'l' in opts + out = self.shell.getoutput(cmd, split=split) if opts.has_key('v'): print '%s ==\n%s' % (var,pformat(out)) if var: @@ -2965,9 +2962,7 @@ Defaulting color scheme to 'NoColor'""" system commands.""" if parameter_s: - out = self.shell.getoutput(parameter_s) - if out is not None: - return SList(out.splitlines()) + return self.shell.getoutput(parameter_s) def magic_r(self, parameter_s=''): """Repeat previous input. diff --git a/IPython/zmq/zmqshell.py b/IPython/zmq/zmqshell.py index 4fdbd5e..f3a7d00 100644 --- a/IPython/zmq/zmqshell.py +++ b/IPython/zmq/zmqshell.py @@ -24,6 +24,7 @@ import re from IPython.core.interactiveshell import ( InteractiveShell, InteractiveShellABC ) +from IPython.core import page from IPython.core.displayhook import DisplayHook from IPython.core.macro import Macro from IPython.core.payloadpage import install_payload_page @@ -78,6 +79,19 @@ class ZMQInteractiveShell(InteractiveShell): displayhook_class = Type(ZMQDisplayHook) + def init_environment(self): + """Configure the user's environment. + + """ + env = os.environ + # These two ensure 'ls' produces nice coloring on BSD-derived systems + env['TERM'] = 'xterm-color' + env['CLICOLOR'] = '1' + # Since normal pagers don't work at all (over pexpect we don't have + # single-key control of the subprocess), try to disable paging in + # subprocesses as much as possible. + env['PAGER'] = 'cat' + env['GIT_PAGER'] = 'cat' def auto_rewrite_input(self, cmd): """Called to show the auto-rewritten input for autocall and friends. @@ -485,5 +499,37 @@ class ZMQInteractiveShell(InteractiveShell): raise NotImplementedError( 'pylab support must be enabled in command line options.') + # A few magics that are adapted to the specifics of using pexpect and a + # remote terminal + + def magic_clear(self, arg_s): + """Clear the terminal.""" + if os.name == 'posix': + self.shell.system("clear") + else: + self.shell.system("cls") + + if os.name == 'nt': + # This is the usual name in windows + magic_cls = magic_clear + + # Terminal pagers won't work over pexpect, but we do have our own pager + + def magic_less(self, arg_s): + """Show a file through the pager. + + Files ending in .py are syntax-highlighted.""" + cont = open(arg_s).read() + if arg_s.endswith('.py'): + cont = self.shell.pycolorize(cont) + page.page(cont) + + magic_more = magic_less + + # Man calls a pager, so we also need to redefine it + if os.name == 'posix': + def magic_man(self, arg_s): + """Find the man page for the given command and display in pager.""" + page.page(self.shell.getoutput('man %s' % arg_s, split=False)) InteractiveShellABC.register(ZMQInteractiveShell)