Skip to content

Commit

Permalink
Merge branch 'juliantaylor-multiline-history'
Browse files Browse the repository at this point in the history
Restore multiline history (on by default) as it worked in the 0.10.x
series.  A new configuration variable
InteractiveShell.multiline_history can be set to control this behavior
for users who prefer not to have it on.

When True, cells spanning multiple lines will be saved in history as a
single entry instead of one entry per line

Closes ipythongh-571.
  • Loading branch information
fperez committed Oct 18, 2011
2 parents 4332951 + 4794ecb commit bf4b2a0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
13 changes: 10 additions & 3 deletions IPython/core/interactiveshell.py
Expand Up @@ -307,6 +307,9 @@ def _exiter_default(self):
Automatically call the pdb debugger after every exception.
"""
)
multiline_history = CBool(True, config=True,
help="Store multiple line spanning cells as a single entry in history."
)

prompt_in1 = Unicode('In [\\#]: ', config=True)
prompt_in2 = Unicode(' .\\D.: ', config=True)
Expand Down Expand Up @@ -1791,9 +1794,13 @@ def refill_readline_hist(self):
for _, _, cell in self.history_manager.get_tail(1000,
include_latest=True):
if cell.strip(): # Ignore blank lines
for line in cell.splitlines():
self.readline.add_history(py3compat.unicode_to_str(line,
stdin_encoding))
if self.multiline_history:
self.readline.add_history(py3compat.unicode_to_str(cell.rstrip(),
stdin_encoding))
else:
for line in cell.splitlines():
self.readline.add_history(py3compat.unicode_to_str(line,
stdin_encoding))

def set_next_input(self, s):
""" Sets the 'default' input string for the next command line.
Expand Down
15 changes: 14 additions & 1 deletion IPython/frontend/terminal/interactiveshell.py
Expand Up @@ -229,6 +229,14 @@ def mainloop(self, display_banner=None):
# handling seems rather unpredictable...
self.write("\nKeyboardInterrupt in interact()\n")

def _replace_rlhist_multiline(self, source_raw, hlen_before_cell):
"""Store multiple lines as a single entry in history"""
if self.multiline_history and self.has_readline:
hlen = self.readline.get_current_history_length()
for i in range(hlen - hlen_before_cell):
self.readline.remove_history_item(hlen - i - 1)
self.readline.add_history(source_raw.rstrip())

def interact(self, display_banner=None):
"""Closely emulate the interactive Python console."""

Expand All @@ -245,6 +253,7 @@ def interact(self, display_banner=None):
self.show_banner()

more = False
hlen_before_cell = self.readline.get_current_history_length()

# Mark activity in the builtins
__builtin__.__dict__['__IPYTHON__active'] += 1
Expand Down Expand Up @@ -281,7 +290,9 @@ def interact(self, display_banner=None):
#double-guard against keyboardinterrupts during kbdint handling
try:
self.write('\nKeyboardInterrupt\n')
self.input_splitter.reset()
source_raw = self.input_splitter.source_raw_reset()[1]
self._replace_rlhist_multiline(source_raw, hlen_before_cell)
hlen_before_cell = self.readline.get_current_history_length()
more = False
except KeyboardInterrupt:
pass
Expand Down Expand Up @@ -309,6 +320,8 @@ def interact(self, display_banner=None):
self.edit_syntax_error()
if not more:
source_raw = self.input_splitter.source_raw_reset()[1]
self._replace_rlhist_multiline(source_raw, hlen_before_cell)
hlen_before_cell = self.readline.get_current_history_length()
self.run_cell(source_raw, store_history=True)

# We are off again...
Expand Down

0 comments on commit bf4b2a0

Please sign in to comment.