pmacs3/completer.py

147 lines
4.7 KiB
Python
Raw Normal View History

2008-11-08 10:30:04 -05:00
import glob, os, pwd
2007-03-06 10:05:38 -05:00
import method, util
_completers = {}
def set_completer(name, completer):
global _completers
_completers[name] = completer
def get_completer(*args):
return _completers.get(*args)
2007-03-06 10:05:38 -05:00
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):
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)
#raise Exception(s + '*')
candidates = [util.normal_path(p) for p in glob.glob(s + '*')]
# ignore some suffixes by default, unless the only possible completions
# ALL have ignored-suffixes, in which case just return them all.
candidates2 = []
for c in candidates:
ok = True
2008-11-08 12:44:59 -05:00
for suffix in self.application.config['ignore_suffix']:
if c.endswith(suffix):
ok = False
break
if ok:
candidates2.append(c)
if candidates2:
candidates = candidates2
#raise(s + "* :: " + repr(candidates))
2007-03-06 10:05:38 -05:00
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 __init__(self, application):
Completer.__init__(self, application)
self.fc = FileCompleter(application)
2007-03-06 10:05:38 -05:00
def get_candidates(self, s, w=None):
if s.startswith('~') or s.startswith('/') or s.startswith('./') or s.startswith('../'):
return self.fc.get_candidates(s)
2007-03-06 10:05:38 -05:00
path = os.getenv('PATH')
path_dirs = path.split(':')
2008-11-08 10:30:04 -05:00
candidates = 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):
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:]
candidates = self.fc.get_candidates(last)
2007-03-06 10:05:38 -05:00
return [base + x for x in candidates]
else:
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)]
class ConfigCompleter(Completer):
def get_candidates(self, s, w=None):
return [n for n in w.application.config if n.startswith(s)]
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)]