app improvement and small python stuff

--HG--
branch : pmacs2
This commit is contained in:
moculus 2009-04-11 04:39:34 +00:00
parent e800135795
commit 6ea87b87e4
2 changed files with 33 additions and 49 deletions

View File

@ -703,19 +703,29 @@ class Application(object):
self.try_manual_resize() self.try_manual_resize()
# NOTE: this is totally broken # NOTE: this is taken from the cursor code
def map_point(self, w, p): def map_point(self, slot, p):
count = 0 w = slot.window
x, y = w.first.xy() swidth = slot.width - w.mode.lmargin - w.mode.rmargin
blen = len(w.buffer.lines)
count = w.mode.header
(x, y) = w.first.xy()
(vy, vx) = (None, None)
while count < slot.height: while count < slot.height:
if p1.y == y and p1.x >= x and p1.x - x < slot.width: if p.y == y and p.x >= x and p.x <= x + swidth:
return (count, x) vy, vx = slot.y_offset + count, p.x - x + w.mode.lmargin
if x + slot.width > len(w.buffer.lines[y]): if vx == swidth and p.x < len(w.buffer.lines[y]):
vx = 0
vy += 1
break
if y >= blen or x + swidth >= len(w.buffer.lines[y]):
x = 0 x = 0
y += 1 y += 1
else: else:
x += slot.width x += swidth
count += 1 count += 1
return vx, vy
def draw_cursor(self): def draw_cursor(self):
if self.mini_active: if self.mini_active:
@ -741,24 +751,7 @@ class Application(object):
else: else:
p = w.logical_cursor() p = w.logical_cursor()
blen = len(w.buffer.lines) vx, vy = self.map_point(slot, p)
count = w.mode.header
(x, y) = w.first.xy()
(vy, vx) = (None, None)
while count < slot.height:
if p.y == y and p.x >= x and p.x <= x + swidth:
vy, vx = slot.y_offset + count, p.x - x + w.mode.lmargin
if vx == swidth and p.x < len(w.buffer.lines[y]):
vx = 0
vy += 1
break
if y >= blen or x + swidth >= len(w.buffer.lines[y]):
x = 0
y += 1
else:
x += swidth
count += 1
if vy is None or vx is None: if vy is None or vx is None:
return return
try: try:

View File

@ -41,9 +41,9 @@ class PythonGrammar(Grammar):
rules = [ rules = [
PatternRule(r'functionname', r'(?<=def )[a-zA-Z_][a-zA-Z0-9_]*'), PatternRule(r'functionname', r'(?<=def )[a-zA-Z_][a-zA-Z0-9_]*'),
PatternRule(r'classname', r'(?<=class )[a-zA-Z_][a-zA-Z0-9_]*'), PatternRule(r'classname', r'(?<=class )[a-zA-Z_][a-zA-Z0-9_]*'),
PatternRule(r'python_reserved', r'(?:True|None|False|Exception|self)(?![a-zA-Z0-9_])'), PatternRule(r'python.reserved', r'(?:True|None|False|Exception|self)(?![a-zA-Z0-9_])'),
PatternRule(r'python_keyword', r'(?:yield|while|try|return|raise|print|pass|or|not|lambda|is|in|import|if|global|from|for|finally|exec|except|else|elif|del|def|continue|class|break|assert|as|and)(?![a-zA-Z0-9_])'), PatternRule(r'python.keyword', r'(?:yield|while|try|return|raise|print|pass|or|not|lambda|is|in|import|if|global|from|for|finally|exec|except|else|elif|del|def|continue|class|break|assert|as|and)(?![a-zA-Z0-9_])'),
PatternRule(r"python_builtin", r'(?<!\.)(?:zip|xrange|vars|unicode|unichr|type|tuple|super|sum|str|staticmethod|sorted|slice|setattr|set|round|repr|reduce|raw_input|range|property|pow|ord|open|oct|object|max|min|map|long|locals|list|len|iter|issubclass|isinstance|int|input|id|hex|hash|hasattr|globals|getattr|frozenset|float|filter|file|execfile|eval|enumerate|divmod|dir|dict|delattr|complex|compile|coerce|cmp|classmethod|chr|callable|bool)(?![a-zA-Z0-9_])'), PatternRule(r"python.builtin", r'(?<!\.)(?:zip|xrange|vars|unicode|unichr|type|tuple|super|sum|str|staticmethod|sorted|slice|setattr|set|round|repr|reduce|raw_input|range|property|pow|ord|open|oct|object|max|min|map|long|locals|list|len|iter|issubclass|isinstance|int|input|id|hex|hash|hasattr|globals|getattr|frozenset|float|filter|file|execfile|eval|enumerate|divmod|dir|dict|delattr|complex|compile|coerce|cmp|classmethod|chr|callable|bool)(?![a-zA-Z0-9_])'),
PatternRule(r'methodcall', r'(?<=\. )[a-zA-Z_][a-zA-Z0-9_]*(?= *\()'), PatternRule(r'methodcall', r'(?<=\. )[a-zA-Z_][a-zA-Z0-9_]*(?= *\()'),
PatternRule(r'functioncall', r'[a-zA-Z_][a-zA-Z0-9_]*(?= *\()'), PatternRule(r'functioncall', r'[a-zA-Z_][a-zA-Z0-9_]*(?= *\()'),
PatternRule(r'system_identifier', r'__[a-zA-Z0-9_]+__'), PatternRule(r'system_identifier', r'__[a-zA-Z0-9_]+__'),
@ -76,7 +76,7 @@ class PythonGrammar(Grammar):
class PythonTabber(tab.StackTabber): class PythonTabber(tab.StackTabber):
# NOTE: yield might initially seem like an endlevel name, but it's not one. # NOTE: yield might initially seem like an endlevel name, but it's not one.
# NOTE: return should be an endlevel name but for now it can't b # NOTE: return should be an endlevel name but for now it can't be one.
endlevel_names = ('pass', 'raise', 'break', 'continue') endlevel_names = ('pass', 'raise', 'break', 'continue')
startlevel_names = ('if', 'try', 'class', 'def', 'for', 'while', 'try') startlevel_names = ('if', 'try', 'class', 'def', 'for', 'while', 'try')
def __init__(self, m): def __init__(self, m):
@ -88,9 +88,8 @@ class PythonTabber(tab.StackTabber):
# we always know that line 0 is indented at the 0 level # we always know that line 0 is indented at the 0 level
return True return True
tokens = self.get_tokens(y) tokens = self.get_tokens(y)
t0 = tokens[0] if tokens[0].matchs('python.keyword', self.startlevel_names):
if t0.name == 'python_keyword' and t0.string in self.startlevel_names: # if a line has no whitespace and begins with something like
# if a line has no whitespace and beings with something like
# 'while','class','def','if',etc. then we can start at it # 'while','class','def','if',etc. then we can start at it
return True return True
else: else:
@ -175,7 +174,7 @@ class PythonTabber(tab.StackTabber):
pass pass
else: else:
self._pop() self._pop()
elif fqname == 'python_keyword': elif fqname == 'python.keyword':
s = token.string s = token.string
if s in self.endlevel_names and self.is_leftmost_token(y, i): if s in self.endlevel_names and self.is_leftmost_token(y, i):
# we know we'll unindent at least once # we know we'll unindent at least once
@ -435,22 +434,14 @@ class PythonClassCompleter(completer.Completer):
def _get_dict(self, w): def _get_dict(self, w):
return w.buffer.method.old_window.mode.context.get_classes() return w.buffer.method.old_window.mode.context.get_classes()
CLASS_MATCH = And(Optional(Name('spaces')),
Matchs('python_keyword', ('public', 'protected', 'private')),
Name('spaces'),
Match('keyword', 'class'),
Name('spaces'),
Name('identifier'))
CLASS_OFFSET = 1
class PythonContext(context.Context): class PythonContext(context.Context):
empty_match = And(Optional(Name('spaces')), Name('eol')) empty_match = And(Optional(Name('spaces')), Name('eol'))
class_match = And(Optional(Name('spaces')), class_match = And(Optional(Name('spaces')),
Match('python_keyword', 'class'), Match('python.keyword', 'class'),
Name('spaces'), Name('spaces'),
Name('classname')) Name('classname'))
func_match = And(Optional(Name('spaces')), func_match = And(Optional(Name('spaces')),
Match('python_keyword', 'def'), Match('python.keyword', 'def'),
Name('spaces'), Name('spaces'),
Name('functionname')) Name('functionname'))
def __init__(self, mode): def __init__(self, mode):
@ -519,9 +510,9 @@ class PythonContext(context.Context):
if len(g[j:]) > 3: if len(g[j:]) > 3:
d, found = None, False d, found = None, False
if g[j].name == 'python_keyword' and g[j].string == 'class': if g[j].name == 'python.keyword' and g[j].string == 'class':
d, found = self.classes, True d, found = self.classes, True
elif g[j].name == 'python_keyword' and g[j].string == 'def': elif g[j].name == 'python.keyword' and g[j].string == 'def':
d, found = self.functions, True d, found = self.functions, True
if found: if found:
stack.append([lvl, g[j+2].string]) stack.append([lvl, g[j+2].string])
@ -565,9 +556,9 @@ class Python(mode.Fundamental):
closetags = {')': '(', ']': '[', '}': '{'} closetags = {')': '(', ']': '[', '}': '{'}
commentc = '#' commentc = '#'
colors = { colors = {
'python_keyword': ('cyan', 'default', 'bold'), 'python.keyword': ('cyan', 'default', 'bold'),
'python_reserved': ('magenta', 'default', 'bold'), 'python.reserved': ('magenta', 'default', 'bold'),
'python_builtin': ('cyan', 'default', 'bold'), 'python.builtin': ('cyan', 'default', 'bold'),
'functionname': ('blue', 'default', 'bold'), 'functionname': ('blue', 'default', 'bold'),
'classname': ('green', 'default', 'bold'), 'classname': ('green', 'default', 'bold'),
'rawstring.start': ('green', 'default', 'bold'), 'rawstring.start': ('green', 'default', 'bold'),