2008-10-29 11:43:15 -04:00
|
|
|
import glob, os, pwd, sets
|
2007-03-06 10:05:38 -05:00
|
|
|
import method, util
|
|
|
|
|
|
|
|
def find_common_string(candidates):
|
|
|
|
if len(candidates) == 0:
|
|
|
|
return ""
|
|
|
|
elif len(candidates) == 1:
|
|
|
|
return candidates[0]
|
|
|
|
else:
|
|
|
|
done = False
|
|
|
|
index = 0
|
|
|
|
test = candidates[0]
|
|
|
|
while True:
|
|
|
|
for c in candidates:
|
|
|
|
if len(c) <= index or c[index] != test[index]:
|
|
|
|
return test[:index]
|
|
|
|
index += 1
|
|
|
|
return test
|
|
|
|
|
2008-03-14 17:17:04 -04:00
|
|
|
class Completer(object):
|
2008-10-15 17:17:16 -04:00
|
|
|
def __init__(self, application):
|
|
|
|
self.application = application
|
2007-03-06 10:05:38 -05:00
|
|
|
def get_candidates(self, s):
|
|
|
|
assert "Not implemented"
|
|
|
|
def tab_string(self, s, w=None):
|
|
|
|
'''returns a tuple of three things:
|
|
|
|
1. the new string
|
|
|
|
2. whether the string "exists"
|
|
|
|
3. whether the string is "complete"'''
|
|
|
|
candidates = self.get_candidates(s, w)
|
|
|
|
if len(candidates) == 0:
|
|
|
|
return (s, False, True)
|
|
|
|
elif len(candidates) == 1:
|
|
|
|
return (candidates[0], True, True)
|
|
|
|
else:
|
|
|
|
s2 = find_common_string(candidates)
|
|
|
|
if s2 in candidates:
|
|
|
|
return (s2, True, False)
|
|
|
|
else:
|
|
|
|
return (s2, False, False)
|
|
|
|
|
|
|
|
class FileCompleter(Completer):
|
|
|
|
def get_candidates(self, s, w=None):
|
|
|
|
s = util.expand_tilde(s)
|
|
|
|
if s.startswith('~'):
|
|
|
|
users = ['~%s' % (x[0]) for x in pwd.getpwall()]
|
|
|
|
candidates = [util.expand_tilde(user) for user in users if user.startswith(s)]
|
|
|
|
else:
|
|
|
|
candidates = glob.glob(s + '*')
|
2008-10-15 17:17:16 -04:00
|
|
|
|
|
|
|
# ignore some suffixes by default, unless the only possible completions
|
|
|
|
# ALL have ignored-suffixes, in which case just return them all.
|
|
|
|
cand2 = []
|
|
|
|
for c in candidates:
|
|
|
|
ok = True
|
|
|
|
for suffix in self.application.config['ignore-suffix']:
|
|
|
|
if c.endswith(suffix):
|
|
|
|
ok = False
|
|
|
|
break
|
|
|
|
if ok:
|
|
|
|
cand2.append(c)
|
|
|
|
if cand2:
|
|
|
|
candidates = cand2
|
|
|
|
|
2007-03-06 10:05:38 -05:00
|
|
|
for i in range(0, len(candidates)):
|
|
|
|
c = candidates[i]
|
|
|
|
if os.path.isdir(os.path.realpath(c)):
|
|
|
|
candidates[i] = c + '/'
|
|
|
|
return candidates
|
|
|
|
|
|
|
|
class BufferCompleter(Completer):
|
|
|
|
def get_candidates(self, s, w=None):
|
|
|
|
bl = self.application.bufferlist
|
|
|
|
candidates = [b.name() for b in bl.buffers if b.name().startswith(s)]
|
|
|
|
return candidates
|
|
|
|
|
|
|
|
class CommandCompleter(Completer):
|
|
|
|
def get_candidates(self, s, w=None):
|
|
|
|
path = os.getenv('PATH')
|
|
|
|
path_dirs = path.split(':')
|
2008-10-29 11:43:15 -04:00
|
|
|
candidates = sets.Set()
|
2007-03-06 10:05:38 -05:00
|
|
|
for d in path_dirs:
|
|
|
|
if (not os.path.isdir(d) or not os.access(d, os.R_OK)):
|
|
|
|
continue
|
|
|
|
for p in os.listdir(d):
|
|
|
|
if not os.path.isfile(os.path.join(d, p)):
|
|
|
|
continue
|
|
|
|
elif not p.startswith(s):
|
|
|
|
continue
|
|
|
|
else:
|
2008-10-29 11:43:15 -04:00
|
|
|
candidates.add(p)
|
|
|
|
return sorted(candidates)
|
2007-03-06 10:05:38 -05:00
|
|
|
|
|
|
|
class ShellCompleter(Completer):
|
2008-10-15 17:17:16 -04:00
|
|
|
def __init__(self, application):
|
|
|
|
Completer.__init__(self, application)
|
|
|
|
self.fc = FileCompleter(application)
|
|
|
|
self.cc = CommandCompleter(application)
|
2007-03-06 10:05:38 -05:00
|
|
|
def get_candidates(self, s, w=None):
|
|
|
|
if ' ' in s:
|
|
|
|
i = s.rindex(' ') + 1
|
|
|
|
base = s[:i]
|
|
|
|
last = s[i:]
|
2008-10-15 17:17:16 -04:00
|
|
|
candidates = self.fc.get_candidates(last)
|
2007-03-06 10:05:38 -05:00
|
|
|
return [base + x for x in candidates]
|
|
|
|
else:
|
2008-10-15 17:17:16 -04:00
|
|
|
return self.cc.get_candidates(s)
|
2007-03-06 10:05:38 -05:00
|
|
|
|
2008-05-30 14:13:26 -04:00
|
|
|
class TokenCompleter(Completer):
|
|
|
|
def get_candidates(self, s, w=None):
|
|
|
|
w2 = w.buffer.method.old_window
|
|
|
|
t = w2.get_token2()
|
|
|
|
h = w2.get_highlighter()
|
|
|
|
candidates = {}
|
|
|
|
for line in h.tokens:
|
|
|
|
for t2 in line:
|
|
|
|
if t2 is t:
|
|
|
|
continue
|
|
|
|
elif t2.string.startswith(s):
|
|
|
|
candidates[t2.string] = 1
|
|
|
|
return candidates.keys()
|
|
|
|
|
2007-03-06 10:05:38 -05:00
|
|
|
class MethodCompleter(Completer):
|
|
|
|
def get_candidates(self, s, w=None):
|
|
|
|
return [n for n in w.application.methods if n.startswith(s)]
|
|
|
|
|
2008-03-20 21:58:30 -04:00
|
|
|
class ConfigCompleter(Completer):
|
|
|
|
def get_candidates(self, s, w=None):
|
|
|
|
return [n for n in w.application.config if n.startswith(s)]
|
|
|
|
|
2008-03-18 10:51:47 -04:00
|
|
|
class RegisterCompleter(Completer):
|
|
|
|
def get_candidates(self, s, w=None):
|
|
|
|
return [n for n in w.application.registers if n.startswith(s)]
|
|
|
|
|
2007-03-06 10:05:38 -05:00
|
|
|
class ModeCompleter(Completer):
|
|
|
|
def get_candidates(self, s, w=None):
|
|
|
|
return [n for n in w.application.modes if n.startswith(s)]
|