From 48c3114710976ee0c632f3024100c0efc5249529 2006-01-20 09:03:57 From: fperez Date: 2006-01-20 09:03:57 Subject: [PATCH] _tentative_ fixes to pasting of multiline code with autoindent on. Needs more testing... --- diff --git a/IPython/genutils.py b/IPython/genutils.py index 25328ea..0bf0954 100644 --- a/IPython/genutils.py +++ b/IPython/genutils.py @@ -5,7 +5,7 @@ General purpose utilities. This is a grab-bag of stuff I find useful in most programs I write. Some of these things are also convenient when working at the command line. -$Id: genutils.py 1028 2006-01-16 21:42:33Z vivainio $""" +$Id: genutils.py 1032 2006-01-20 09:03:57Z fperez $""" #***************************************************************************** # Copyright (C) 2001-2006 Fernando Perez. @@ -209,7 +209,7 @@ def fatal(msg,exit_val=1): # useful for debugging -def debugp(expr): +def debugp(expr,pre_msg=''): """Print the value of an expression from the caller's frame. Takes an expression, evaluates it in the caller's frame and prints both @@ -217,7 +217,11 @@ def debugp(expr): suitable for eval().""" cf = sys._getframe(1) - print '[DBG] %s -> %r' % (expr,eval(expr,cf.f_globals,cf.f_locals)) + print '[DBG] %s %s -> %r' % (pre_msg,expr, + eval(expr,cf.f_globals,cf.f_locals)) + +# deactivate it from here: +def debugp(expr,pre_msg=''): pass #---------------------------------------------------------------------------- StringTypes = types.StringTypes diff --git a/IPython/iplib.py b/IPython/iplib.py index 5be999b..cfab762 100644 --- a/IPython/iplib.py +++ b/IPython/iplib.py @@ -6,7 +6,7 @@ Requires Python 2.3 or newer. This file contains all the classes and helper functions specific to IPython. -$Id: iplib.py 1031 2006-01-18 20:20:39Z vivainio $ +$Id: iplib.py 1032 2006-01-20 09:03:57Z fperez $ """ #***************************************************************************** @@ -560,7 +560,6 @@ class InteractiveShell(object,Magic): # indentation management self.autoindent = False self.indent_current_nsp = 0 - self.indent_current = '' # actual indent string # Make some aliases automatically # Prepare list of shell aliases to auto-define @@ -1149,7 +1148,8 @@ want to merge them back into the new files.""" % locals() Currently it handles auto-indent only.""" - self.readline.insert_text(self.indent_current) + #debugp('self.indent_current_nsp','pre_readline:') + self.readline.insert_text(self.indent_current_str()) def init_readline(self): """Command history completion/saving/reloading.""" @@ -1469,7 +1469,6 @@ want to merge them back into the new files.""" % locals() if self.autoindent: self.indent_current_nsp = 0 - self.indent_current = ' '* self.indent_current_nsp except bdb.BdbQuit: warn("The Python debugger has exited with a BdbQuit exception.\n" @@ -1537,12 +1536,20 @@ want to merge them back into the new files.""" % locals() except: self.showtraceback() + def indent_current_str(self): + """return the current level of indentation as a string""" + return self.indent_current_nsp * ' ' + def autoindent_update(self,line): """Keep track of the indent level.""" + debugp('line','autoindent_update:') + debugp('self.indent_current_nsp') if self.autoindent: if line: - self.indent_current_nsp = num_ini_spaces(line) + inisp = num_ini_spaces(line) + if inisp < self.indent_current_nsp: + self.indent_current_nsp = inisp if line[-1] == ':': self.indent_current_nsp += 4 @@ -1551,10 +1558,6 @@ want to merge them back into the new files.""" % locals() else: self.indent_current_nsp = 0 - # indent_current is the actual string to be inserted - # by the readline hooks for indentation - self.indent_current = ' '* self.indent_current_nsp - def runlines(self,lines): """Run a string of one or more lines of source. @@ -1721,7 +1724,7 @@ want to merge them back into the new files.""" % locals() def resetbuffer(self): """Reset the input buffer.""" self.buffer[:] = [] - + def raw_input(self,prompt='',continue_prompt=False): """Write a prompt and read a line. @@ -1741,25 +1744,16 @@ want to merge them back into the new files.""" % locals() # than necessary. We do this by trimming out the auto-indent initial # spaces, if the user's actual input started itself with whitespace. #debugp('self.buffer[-1]') -## if self.autoindent: -## try: -## prev_line = self.buffer[-1] -## except IndexError: -## prev_line = '' -## prev_indent = num_ini_spaces(prev_line) -## debugp('prev_indent') -## # Split the user's input -## line1 = line[:self.indent_current_nsp] -## line2 = line[self.indent_current_nsp:] -## if line1.isspace() and line2 and \ -## num_ini_spaces(line2)==prev_indent: -## line = line2 - #debugp('line') - #debugp('line1') - #debugp('line2') -## if line1.isspace() and line2 and line2[0:1] in (' ','\t'): -## line = line2 -## debugp('line') + + debugp('line') + debugp('self.indent_current_nsp') + if self.autoindent: + if num_ini_spaces(line) > self.indent_current_nsp: + line = line[self.indent_current_nsp:] + self.indent_current_nsp = 0 + debugp('self.indent_current_nsp') + + debugp('line') return self.prefilter(line,continue_prompt) def split_user_input(self,line): @@ -1937,7 +1931,8 @@ want to merge them back into the new files.""" % locals() # of a size different to the indent level, will exit the input loop. if (continue_prompt and self.autoindent and line.isspace() and - (line != self.indent_current or (self.buffer[-1]).isspace() )): + (line != self.indent_current_str() or + (self.buffer[-1]).isspace() )): line = '' self.log(line,continue_prompt) diff --git a/doc/ChangeLog b/doc/ChangeLog index f92917a..d54c4c0 100644 --- a/doc/ChangeLog +++ b/doc/ChangeLog @@ -1,3 +1,15 @@ +2006-01-20 Fernando Perez + + * IPython/iplib.py (raw_input): I _think_ I got the pasting of + multiline code with autoindent on working. But I am really not + sure, so this needs more testing. Will commit a debug-enabled + version for now, while I test it some more, so that Ville and + others may also catch any problems. Also made + self.indent_current_str() a method, to ensure that there's no + chance of the indent space count and the corresponding string + falling out of sync. All code needing the string should just call + the method. + 2006-01-18 Fernando Perez * IPython/Magic.py (magic_edit): fix check for when users don't diff --git a/doc/manual_base.lyx b/doc/manual_base.lyx index 42d6f19..0b90abe 100644 --- a/doc/manual_base.lyx +++ b/doc/manual_base.lyx @@ -2604,7 +2604,10 @@ I recently happened on a nifty way to keep tidy per-project log files. include ipythonrc \layout LyX-Code -logfile '' # cancel earlier logfile invocation +# cancel earlier logfile invocation: +\layout LyX-Code + +logfile '' \layout LyX-Code execute import time