Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions IPython/core/inputsplitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@
ESC_QUOTE2 = ';' # Quote all args as a single string, call
ESC_PAREN = '/' # Call first argument with rest of line as arguments

ESC_SEQUENCES = [ESC_SHELL, ESC_SH_CAP, ESC_HELP ,\
ESC_HELP2, ESC_MAGIC, ESC_MAGIC2,\
ESC_QUOTE, ESC_QUOTE2, ESC_PAREN ]

#-----------------------------------------------------------------------------
# Utilities
#-----------------------------------------------------------------------------
Expand Down
29 changes: 28 additions & 1 deletion IPython/frontend/qt/console/console_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#-----------------------------------------------------------------------------

# Standard library imports
from os.path import commonprefix
import os.path
import re
import sys
from textwrap import dedent
Expand All @@ -16,6 +16,7 @@

# Local imports
from IPython.config.configurable import LoggingConfigurable
from IPython.core.inputsplitter import ESC_SEQUENCES
from IPython.frontend.qt.rich_text import HtmlExporter
from IPython.frontend.qt.util import MetaQObjectHasTraits, get_font
from IPython.utils.text import columnize
Expand All @@ -26,10 +27,36 @@
from completion_plain import CompletionPlain
from kill_ring import QtKillRing


#-----------------------------------------------------------------------------
# Functions
#-----------------------------------------------------------------------------

ESCAPE_CHARS = ''.join(ESC_SEQUENCES)
ESCAPE_RE = re.compile("^["+ESCAPE_CHARS+"]+")

def commonprefix(items):
"""Get common prefix for completions

Return the longest common prefix of a list of strings, but with special
treatment of escape characters that might precede commands in IPython,
such as %magic functions. Used in tab completion.

For a more general function, see os.path.commonprefix
"""
# the last item will always have the least leading % symbol
# min / max are first/last in alphabetical order
first_match = ESCAPE_RE.match(min(items))
last_match = ESCAPE_RE.match(max(items))
# common suffix is (common prefix of reversed items) reversed
if first_match and last_match:
prefix = os.path.commonprefix((first_match.group(0)[::-1], last_match.group(0)[::-1]))[::-1]
else:
prefix = ''

items = [s.lstrip(ESCAPE_CHARS) for s in items]
return prefix+os.path.commonprefix(items)

def is_letter_or_number(char):
""" Returns whether the specified unicode character is a letter or a number.
"""
Expand Down