7 class QuoteError(Exception):
11 def _quotesplit(line):
16 for i in range(len(line)):
19 if inquote == q and c != q:
20 word += '\\' # single-q backslashes can only quote single-q
27 # this is un-sh-like, but do it for sanity when autocompleting
28 yield (wordstart, word)
31 elif not inquote and not word and (c == q or c == qq):
32 # the 'not word' constraint on this is un-sh-like, but do it
33 # for sanity when autocompleting
36 elif not inquote and c in [' ', '\n', '\r', '\t']:
38 yield (wordstart, word)
44 yield (wordstart, word)
45 if inquote or inescape or word:
52 for i in _quotesplit(line):
59 def unfinished_word(line):
61 for (wordstart,word) in _quotesplit(line):
64 firstchar = line[wordstart]
65 if firstchar in [q, qq]:
66 return (firstchar, word)
73 def quotify(qtype, word, terminate):
75 return qq + word.replace(qq, '\\"') + (terminate and qq or '')
77 return q + word.replace(q, "\\'") + (terminate and q or '')
79 return re.sub(r'([\"\' \t\n\r])', r'\\\1', word)
82 def what_to_add(qtype, origword, newword, terminate):
83 if not newword.startswith(origword):
86 qold = quotify(qtype, origword, terminate=False)
87 return quotify(qtype, newword, terminate=terminate)[len(qold):]