tab completion for console

--HG--
branch : pmacs2
This commit is contained in:
moculus 2008-03-16 22:12:29 +00:00
parent d0a22e4909
commit 6cb96eb661
1 changed files with 70 additions and 49 deletions

View File

@ -1,10 +1,12 @@
import code, string, StringIO, sys, traceback import code, re, string, StringIO, sys, traceback
import color, completer, method, mode import color, completer, lex, method, mode
from lex import Grammar, PatternRule from lex import Grammar, PatternRule
from mode.python import PythonGrammar
from point import Point from point import Point
class ConsoleMini(mode.Fundamental): class ConsoleMini(mode.Fundamental):
modename = 'ConsoleMini' modename = 'ConsoleMini'
grammar = PythonGrammar
def __init__(self, w): def __init__(self, w):
mode.Fundamental.__init__(self, w) mode.Fundamental.__init__(self, w)
self.bindings = {} self.bindings = {}
@ -46,7 +48,7 @@ class ConsoleMini(mode.Fundamental):
self.add_action_and_bindings(ConsoleCancel(), ('C-]',)) self.add_action_and_bindings(ConsoleCancel(), ('C-]',))
self.add_action_and_bindings(ConsoleHistoryPrev(), ('C-p',)) self.add_action_and_bindings(ConsoleHistoryPrev(), ('C-p',))
self.add_action_and_bindings(ConsoleHistoryNext(), ('C-n',)) self.add_action_and_bindings(ConsoleHistoryNext(), ('C-n',))
#self.add_action_and_bindings(ConsoleTab(), ('TAB',)) self.add_action_and_bindings(ConsoleTab(), ('TAB',))
for c in string.letters + string.digits + string.punctuation: for c in string.letters + string.digits + string.punctuation:
self.add_binding('insert-string-%s' % c, c) self.add_binding('insert-string-%s' % c, c)
@ -134,50 +136,69 @@ class ConsoleHistoryNext(method.Method):
w.mode.hindex += 1 w.mode.hindex += 1
w.buffer.set_data(w.mode.history[w.mode.hindex]) w.buffer.set_data(w.mode.history[w.mode.hindex])
#class ConsoleTab(method.Method): class ConsoleTab(method.Method):
# def execute(self, w, **vargs): def execute(self, w, **vargs):
# a = w.application a = w.application
# s = w.buffer.make_string() s = w.buffer.make_string()
# l = lex.Lexer(w.mode, PythonGrammar)
# if '"' in s or "'" in s or "(" in s or ")" in s or "[" in s or "]" in s:
# return tokens = list(l.lex([s]))
# x = w.logical_cursor().x
# parts = s.split(".") #raise Exception, repr(x)
# if len(parts) == 0:
# return curr_t = None
# curr_i = None
# v = a.globals() for i in range(0, len(tokens)):
# v.update(a.locals()) t = tokens[i]
# obj = None if t.x < x and t.end_x() >= x:
# for part in parts[:-1]: curr_i = i
# if obj is None: curr_t = t
# if part in v: if curr_t is None:
# obj = v[part] #raise Exception, 'not found: %r %r' % (x, tokens)
# else: return
# return
# else: first_t = curr_t
# if hasattr(obj, part): j = curr_i
# obj = getattr(obj, part)
# else: names = [curr_t.string]
# return name_re = re.compile('^[a-zA-Z_][a-zA-Z0-9_]*$')
# while j >= 1:
# if obj is None: j -= 1
# pool = v.keys() t = tokens[j]
# else: if name_re.match(t.string):
# pool = dir(obj) names.insert(0, t.string)
# candidates = [x for x in pool if x.startswith(parts[-1])] elif t.string == '.':
# pass
# if len(candidates) == 0: else:
# return break
#
# common = completer.find_common_string(candidates) obj = None
# s2 = '.'.join(parts[:-1]) + '.' + common g = globals()
# i = 0
# w.buffer.set_data(s2) name = None
# while i < len(names):
# if len(candidates) > 1: name = names[i]
# if not a.has_buffer_name('*Console*'): if obj is None:
# a.add_buffer(buffer.ConsoleBuffer()) if name in w.mode.locals:
# b = a.bufferlist.get_buffer_by_name('*Console*') obj = w.mode.locals[name]
# b.insert_string(b.get_buffer_end(), repr(candidates) + '\n', force=True) elif name in w.mode.globals:
obj = w.mode.globals[name]
else:
break
else:
if hasattr(obj, name):
obj = getattr(obj, name)
else:
break
i += 1
if obj is not None and i == len(names) - 1:
newnames = dir(obj)
candidates = [x for x in newnames if x.startswith(name)]
if len(candidates) > 1:
s = completer.find_common_string(candidates)[len(name):]
w.insert_string_at_cursor(s)
elif len(candidates) == 1:
s = candidates[0][len(name):]
w.insert_string_at_cursor(s)
install = ConsoleMini.install install = ConsoleMini.install