3333import cmd
3434import collections
3535from colorama import Fore
36+ import copy
3637import glob
3738import os
3839import platform
@@ -494,13 +495,16 @@ def __init__(self, completekey='tab', stdin=None, stdout=None, persistent_histor
494495 # will be added if there is an unmatched opening quote
495496 self .allow_closing_quote = True
496497
497- # Use this list if you are completing strings that contain a common delimiter and you only want to
498- # display the final portion of the matches as the tab-completion suggestions. The full matches
499- # still must be returned from your completer function. For an example, look at path_complete()
500- # which uses this to show only the basename of paths as the suggestions. delimiter_complete() also
501- # populates this list.
498+ # If the tab-completion suggestions should be displayed in a way that is different than the actual match values,
499+ # then place those results in this list. The full matches still must be returned from your completer function.
500+ # For an example, look at path_complete() which uses this to show only the basename of paths as the
501+ # suggestions. delimiter_complete() also populates this list.
502502 self .display_matches = []
503503
504+ # Used by functions like path_complete() and delimiter_complete() to properly
505+ # quote matches that are completed in a delimited fashion
506+ self .matches_delimited = False
507+
504508 # ----- Methods related to presenting output to the user -----
505509
506510 @property
@@ -658,6 +662,7 @@ def reset_completion_defaults(self):
658662 self .allow_appended_space = True
659663 self .allow_closing_quote = True
660664 self .display_matches = []
665+ self .matches_delimited = False
661666
662667 if rl_type == RlType .GNU :
663668 readline .set_completion_display_matches_hook (self ._display_matches_gnu_readline )
@@ -683,7 +688,6 @@ def tokens_for_completion(self, line, begidx, endidx):
683688 On Failure
684689 Both items are None
685690 """
686- import copy
687691 unclosed_quote = ''
688692 quotes_to_try = copy .copy (constants .QUOTES )
689693
@@ -836,6 +840,8 @@ def delimiter_complete(self, text, line, begidx, endidx, match_against, delimite
836840
837841 # Display only the portion of the match that's being completed based on delimiter
838842 if matches :
843+ # Set this to True for proper quoting of matches with spaces
844+ self .matches_delimited = True
839845
840846 # Get the common beginning for the matches
841847 common_prefix = os .path .commonprefix (matches )
@@ -1037,6 +1043,9 @@ def complete_users():
10371043 search_str = os .path .join (os .getcwd (), search_str )
10381044 cwd_added = True
10391045
1046+ # Set this to True for proper quoting of paths with spaces
1047+ self .matches_delimited = True
1048+
10401049 # Find all matching path completions
10411050 matches = glob .glob (search_str )
10421051
@@ -1414,17 +1423,10 @@ def complete(self, text, state):
14141423 display_matches_set = set (self .display_matches )
14151424 self .display_matches = list (display_matches_set )
14161425
1417- # Check if display_matches has been used. If so, then matches
1418- # on delimited strings like paths was done.
1419- if self .display_matches :
1420- matches_delimited = True
1421- else :
1422- matches_delimited = False
1423-
1426+ if not self .display_matches :
14241427 # Since self.display_matches is empty, set it to self.completion_matches
14251428 # before we alter them. That way the suggestions will reflect how we parsed
14261429 # the token being completed and not how readline did.
1427- import copy
14281430 self .display_matches = copy .copy (self .completion_matches )
14291431
14301432 # Check if we need to add an opening quote
@@ -1435,7 +1437,7 @@ def complete(self, text, state):
14351437 # This is the tab completion text that will appear on the command line.
14361438 common_prefix = os .path .commonprefix (self .completion_matches )
14371439
1438- if matches_delimited :
1440+ if self . matches_delimited :
14391441 # Check if any portion of the display matches appears in the tab completion
14401442 display_prefix = os .path .commonprefix (self .display_matches )
14411443
0 commit comments