summaryrefslogtreecommitdiff
path: root/cmd2/cmd2.py
diff options
context:
space:
mode:
authorTodd Leonhardt <todd.leonhardt@gmail.com>2018-04-28 01:32:19 -0400
committerGitHub <noreply@github.com>2018-04-28 01:32:19 -0400
commite54ea631d81d2e94633f4f37b5c8ced899b3c354 (patch)
tree625aa782165a77f9bec147386f40c647c3b20243 /cmd2/cmd2.py
parent8c69e0e75454a5665ed2d428aba3c052b97b68b5 (diff)
parent01e4e0296adf57c920c1e12c3c92f45f615894b1 (diff)
downloadcmd2-git-e54ea631d81d2e94633f4f37b5c8ced899b3c354.tar.gz
Merge branch 'master' into ply
Diffstat (limited to 'cmd2/cmd2.py')
-rwxr-xr-xcmd2/cmd2.py70
1 files changed, 36 insertions, 34 deletions
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py
index 2c004c85..40c024d0 100755
--- a/cmd2/cmd2.py
+++ b/cmd2/cmd2.py
@@ -72,10 +72,6 @@ elif rl_type == RlType.GNU:
import ctypes
from .rl_utils import readline_lib
- # Save address that rl_basic_quote_characters is pointing to since we need to override and restore it
- rl_basic_quote_characters = ctypes.c_char_p.in_dll(readline_lib, "rl_basic_quote_characters")
- orig_rl_basic_quote_characters_addr = ctypes.cast(rl_basic_quote_characters, ctypes.c_void_p).value
-
# Newer versions of pyperclip are released as a single file, but older versions had a more complicated structure
try:
from pyperclip.exceptions import PyperclipException
@@ -524,11 +520,22 @@ class AddSubmenu(object):
try:
# copy over any shared attributes
self._copy_in_shared_attrs(_self)
+
+ # Reset the submenu's tab completion parameters
+ submenu.allow_appended_space = True
+ submenu.allow_closing_quote = True
+ submenu.display_matches = []
+
return _complete_from_cmd(submenu, text, line, begidx, endidx)
finally:
# copy back original attributes
self._copy_out_shared_attrs(_self, original_attributes)
+ # Pass the submenu's tab completion parameters back up to the menu that called complete()
+ _self.allow_appended_space = submenu.allow_appended_space
+ _self.allow_closing_quote = submenu.allow_closing_quote
+ _self.display_matches = copy.copy(submenu.display_matches)
+
original_do_help = cmd_obj.do_help
original_complete_help = cmd_obj.complete_help
@@ -913,6 +920,11 @@ class Cmd(cmd.Cmd):
self.allow_closing_quote = True
self.display_matches = []
+ if rl_type == RlType.GNU:
+ readline.set_completion_display_matches_hook(self._display_matches_gnu_readline)
+ elif rl_type == RlType.PYREADLINE:
+ readline.rl.mode._display_completions = self._display_matches_pyreadline
+
def tokens_for_completion(self, line, begidx, endidx):
"""
Used by tab completion functions to get all tokens through the one being completed
@@ -2315,38 +2327,33 @@ class Cmd(cmd.Cmd):
"""
# An almost perfect copy from Cmd; however, the pseudo_raw_input portion
# has been split out so that it can be called separately
- if self.use_rawinput and self.completekey:
+ if self.use_rawinput and self.completekey and rl_type != RlType.NONE:
# Set up readline for our tab completion needs
if rl_type == RlType.GNU:
- readline.set_completion_display_matches_hook(self._display_matches_gnu_readline)
-
# Set GNU readline's rl_basic_quote_characters to NULL so it won't automatically add a closing quote
# We don't need to worry about setting rl_completion_suppress_quote since we never declared
# rl_completer_quote_characters.
- rl_basic_quote_characters.value = None
+ basic_quote_characters = ctypes.c_char_p.in_dll(readline_lib, "rl_basic_quote_characters")
+ old_basic_quote_characters = ctypes.cast(basic_quote_characters, ctypes.c_void_p).value
+ basic_quote_characters.value = None
- elif rl_type == RlType.PYREADLINE:
- readline.rl.mode._display_completions = self._display_matches_pyreadline
+ old_completer = readline.get_completer()
+ old_delims = readline.get_completer_delims()
+ readline.set_completer(self.complete)
- try:
- self.old_completer = readline.get_completer()
- self.old_delims = readline.get_completer_delims()
- readline.set_completer(self.complete)
+ # Break words on whitespace and quotes when tab completing
+ completer_delims = " \t\n" + ''.join(constants.QUOTES)
- # Break words on whitespace and quotes when tab completing
- completer_delims = " \t\n" + ''.join(constants.QUOTES)
+ if self.allow_redirection:
+ # If redirection is allowed, then break words on those characters too
+ completer_delims += ''.join(constants.REDIRECTION_CHARS)
- if self.allow_redirection:
- # If redirection is allowed, then break words on those characters too
- completer_delims += ''.join(constants.REDIRECTION_CHARS)
+ readline.set_completer_delims(completer_delims)
- readline.set_completer_delims(completer_delims)
+ # Enable tab completion
+ readline.parse_and_bind(self.completekey + ": complete")
- # Enable tab completion
- readline.parse_and_bind(self.completekey + ": complete")
- except NameError:
- pass
stop = None
try:
while not stop:
@@ -2370,19 +2377,15 @@ class Cmd(cmd.Cmd):
# Run the command along with all associated pre and post hooks
stop = self.onecmd_plus_hooks(line)
finally:
- if self.use_rawinput and self.completekey:
+ if self.use_rawinput and self.completekey and rl_type != RlType.NONE:
# Restore what we changed in readline
- try:
- readline.set_completer(self.old_completer)
- readline.set_completer_delims(self.old_delims)
- except NameError:
- pass
+ readline.set_completer(old_completer)
+ readline.set_completer_delims(old_delims)
if rl_type == RlType.GNU:
readline.set_completion_display_matches_hook(None)
- rl_basic_quote_characters.value = orig_rl_basic_quote_characters_addr
-
+ basic_quote_characters.value = old_basic_quote_characters
elif rl_type == RlType.PYREADLINE:
readline.rl.mode._display_completions = orig_pyreadline_display
@@ -2723,7 +2726,7 @@ Usage: Usage: unalias [-a] name [name ...]
set_parser = ACArgumentParser(formatter_class=argparse.RawTextHelpFormatter)
set_parser.add_argument('-a', '--all', action='store_true', help='display read-only settings as well')
set_parser.add_argument('-l', '--long', action='store_true', help='describe function of parameter')
- set_parser.add_argument('settable', nargs=(0,2), help='[param_name] [value]')
+ set_parser.add_argument('settable', nargs=(0, 2), help='[param_name] [value]')
@with_argparser(set_parser)
def do_set(self, args):
@@ -2804,7 +2807,6 @@ Usage: Usage: unalias [-a] name [name ...]
index_dict = {1: self.shell_cmd_complete}
return self.index_based_complete(text, line, begidx, endidx, index_dict, self.path_complete)
-
# noinspection PyBroadException
def do_py(self, arg):
"""