diff --git a/application.py b/application.py index 04b7c25..dd279f4 100755 --- a/application.py +++ b/application.py @@ -109,17 +109,6 @@ class Application(object): self.mode_extensions = {} self.mode_detection = {} - # ok, now let's load all the "standard" modes - mode.install(self) - names = ('about', 'blame', 'c', 'console', 'consolemini', 'css', 'diff', - 'dir', 'elisp', 'hex', 'html', 'java', 'javascript', 'lisp', - 'make', 'mini', 'mutt', 'nasm', 'ocaml', 'perl', 'python', - 'replace', 'rst', 'scheme', 'search', 'sh', 'sql', 'tt', - 'text', 'text2', 'which', 'xml', 'cheetah', 'colortext', - 'latex', 'insertmini', 'conf') - for name in names: - exec("import mode.%s; mode.%s.install(self)" % (name, name)) - # initialize our methods self.methods = {} names = ('method', 'method.svn', 'method.cvs', 'method.search', @@ -133,6 +122,17 @@ class Application(object): if hasattr(cls, '_is_method') and cls._is_method: self.methods[cls._name()] = cls() + # ok, now let's load all the "standard" modes + mode.install(self) + names = ('about', 'blame', 'c', 'console', 'consolemini', 'css', 'diff', + 'dir', 'elisp', 'hex', 'html', 'java', 'javascript', 'lisp', + 'make', 'mini', 'mutt', 'nasm', 'ocaml', 'perl', 'python', + 'replace', 'rst', 'scheme', 'search', 'sh', 'sql', 'tt', + 'text', 'text2', 'which', 'xml', 'cheetah', 'colortext', + 'latex', 'insertmini', 'conf') + for name in names: + exec("import mode.%s; mode.%s.install(self)" % (name, name)) + # create all the insert methods for the character ranges we like for c in string.letters + string.digits + string.punctuation: obj = method.InsertString(c) diff --git a/mode/python.py b/mode/python.py index 3dbeaf6..12327c9 100644 --- a/mode/python.py +++ b/mode/python.py @@ -179,83 +179,6 @@ class PythonTabber(tab.StackTabber): self._append(token.string, currlvl + w) return currlvl -class Python(mode.Fundamental): - modename = 'Python' - extensions = ['.py'] - detection = ['python'] - tabbercls = PythonTabber - grammar = PythonGrammar - opentokens = ('delimiter',) - opentags = {'(': ')', '[': ']', '{': '}'} - closetokens = ('delimiter',) - closetags = {')': '(', ']': '[', '}': '{'} - colors = { - 'python_keyword': ('cyan', 'default'), - 'python_reserved': ('magenta', 'default'), - 'python_builtin': ('cyan', 'default'), - 'functionname': ('blue', 'default'), - 'classname': ('green', 'default'), - 'rawstring.start': ('green', 'default'), - 'rawstring.null': ('green', 'default'), - 'rawstring.escaped': ('green', 'default'), - 'rawstring.end': ('green', 'default'), - 'system_identifier': ('cyan', 'default'), - } - config = { - 'python.lib': '.', - } - def __init__(self, w): - mode.Fundamental.__init__(self, w) - # tag matching - self.add_bindings('close-paren', (')',)) - self.add_bindings('close-brace', ('}',)) - self.add_bindings('close-bracket', (']',)) - # add python-specific methods - self.add_action(PythonInitFunctions()) - self.add_action(PythonListFunctions()) - self.add_action_and_bindings(PythonGotoFunction(), ('C-c M-g',)) - self.add_action_and_bindings(PythonCheckSyntax(), ('C-c s',)) - self.add_action_and_bindings(PythonDictCleanup(), ('C-c h',)) - self.add_action_and_bindings(PythonInsertTripleSquotes(), ('C-c M-\'',)) - self.add_action_and_bindings(PythonInsertTripleDquotes(), ('C-c M-"',)) - # other python - self.functions = None - def build_function_map(self): - b = self.window.buffer - scope_stack = [] - self.functions = {} - for i in range(0, len(b.lines)): - if regex.whitespace.match(b.lines[i]): - continue - m = regex.python_indent.match(b.lines[i]) - assert m - lvl = len(m.group(1)) - while scope_stack: - if lvl <= scope_stack[-1][0]: - scope_stack.pop(-1) - else: - break - m = regex.python_scope.match(b.lines[i]) - if m: - (ws, typ, name) = m.groups() - lvl = len(ws) - if scope_stack: - prefix = '.'.join([x[1] for x in scope_stack]) - self.functions['%s.%s' % (prefix, name)] = i - else: - self.functions[name] = i - scope_stack.append((len(ws), name)) - def get_functions(self): - if self.functions is None: - self.build_function_map() - return self.functions - def get_function_names(self): - functions = self.get_functions() - pairs = [[functions[key], key] for key in functions] - pairs.sort() - names = [x[1] for x in pairs] - return names - class PythonInitFunctions(method.Method): '''Jump to a function defined in this module''' def _execute(self, w, **vargs): @@ -390,4 +313,82 @@ class PythonInsertTripleDquotes(method.Method): for i in range(0, 3): w.backward() +class Python(mode.Fundamental): + modename = 'Python' + extensions = ['.py'] + detection = ['python'] + tabbercls = PythonTabber + grammar = PythonGrammar + opentokens = ('delimiter',) + opentags = {'(': ')', '[': ']', '{': '}'} + closetokens = ('delimiter',) + closetags = {')': '(', ']': '[', '}': '{'} + colors = { + 'python_keyword': ('cyan', 'default'), + 'python_reserved': ('magenta', 'default'), + 'python_builtin': ('cyan', 'default'), + 'functionname': ('blue', 'default'), + 'classname': ('green', 'default'), + 'rawstring.start': ('green', 'default'), + 'rawstring.null': ('green', 'default'), + 'rawstring.escaped': ('green', 'default'), + 'rawstring.end': ('green', 'default'), + 'system_identifier': ('cyan', 'default'), + } + config = { + 'python.lib': '.', + } + methods = [PythonInitFunctions, PythonListFunctions, PythonGotoFunction, + PythonCheckSyntax, PythonDictCleanup, PythonInsertTripleSquotes, + PythonInsertTripleDquotes] + def __init__(self, w): + mode.Fundamental.__init__(self, w) + # tag matching + self.add_bindings('close-paren', (')',)) + self.add_bindings('close-brace', ('}',)) + self.add_bindings('close-bracket', (']',)) + # add python-specific methods + self.add_bindings('python-goto-function', ('C-c M-g',)) + self.add_bindings('python-check-syntax', ('C-c s',)) + self.add_bindings('python-dict-cleanup', ('C-c h',)) + self.add_bindings('python-insert-triple-squotes', ('C-c M-\'',)) + self.add_bindings('python-insert-triple-dquotes', ('C-c M-"',)) + # other python + self.functions = None + def build_function_map(self): + b = self.window.buffer + scope_stack = [] + self.functions = {} + for i in range(0, len(b.lines)): + if regex.whitespace.match(b.lines[i]): + continue + m = regex.python_indent.match(b.lines[i]) + assert m + lvl = len(m.group(1)) + while scope_stack: + if lvl <= scope_stack[-1][0]: + scope_stack.pop(-1) + else: + break + m = regex.python_scope.match(b.lines[i]) + if m: + (ws, typ, name) = m.groups() + lvl = len(ws) + if scope_stack: + prefix = '.'.join([x[1] for x in scope_stack]) + self.functions['%s.%s' % (prefix, name)] = i + else: + self.functions[name] = i + scope_stack.append((len(ws), name)) + def get_functions(self): + if self.functions is None: + self.build_function_map() + return self.functions + def get_function_names(self): + functions = self.get_functions() + pairs = [[functions[key], key] for key in functions] + pairs.sort() + names = [x[1] for x in pairs] + return names + install = Python.install