pmacs3/mode_dir.py

90 lines
3.1 KiB
Python
Raw Normal View History

import method, mode2, os.path
from lex3 import Grammar, PatternRule, RegionRule, PatternGroupRule
from point2 import Point
class PermGrammar(Grammar):
rules = [
PatternRule(r'set', r's'),
PatternRule(r'read', r'r'),
PatternRule(r'write', r'w'),
PatternRule(r'exec', r'x'),
]
class PathGrammar(Grammar):
rules = [
RegionRule(r'perm', r'(?<=^.)', PermGrammar, r' '),
PatternGroupRule(r'fields', r'owner', r'[^ ]+ +', r'group', r'[^ ]+ +',
r'size', r'[^ ]+ +',
r'mtime', r'[A-Za-z]{3} [ 0-9]{2} [0-9]{2}:[0-9]{2} +',
r'name', r'[^\n]*'),
]
class DirGrammar(Grammar):
rules = [
RegionRule(r'file', r'^-', PathGrammar, r'\n'),
RegionRule(r'blk', r'^b', PathGrammar, r'\n'),
RegionRule(r'chr', r'^c', PathGrammar, r'\n'),
RegionRule(r'dir', r'^d', PathGrammar, r'\n'),
RegionRule(r'lnk', r'^l', PathGrammar, r'\n'),
RegionRule(r'fifo', r'^p', PathGrammar, r'\n'),
RegionRule(r'sock', r'^s', PathGrammar, r'\n'),
RegionRule(r'unk', r'^\?', PathGrammar, r'\n'),
]
class Dir(mode2.Fundamental):
grammar = DirGrammar()
colors = {
'blk.start': ('cyan', 'default'),
'blk.name': ('cyan', 'default'),
'chr.start': ('yellow', 'default'),
'chr.name': ('yellow', 'default'),
'dir.start': ('blue', 'default'),
'dir.name': ('blue', 'default'),
'lnk.start': ('green', 'default'),
'lnk.name': ('green', 'default'),
'fifo.start': ('red', 'default'),
'fifo.name': ('red', 'default'),
'sock.start': ('red', 'default'),
'sock.name': ('red', 'default'),
'unk.start': ('magenta', 'default'),
'unk.name': ('magenta', 'default'),
'perm.set': ('red', 'default'),
'perm.read': ('magenta', 'default'),
'perm.write': ('magenta', 'default'),
'perm.exec': ('magenta', 'default'),
'owner': ('blue', 'default'),
'group': ('cyan', 'default'),
'size': ('yellow', 'default'),
'mtime': ('green', 'default'),
}
def __init__(self, w):
mode2.Fundamental.__init__(self, w)
self.add_action(Chmod())
self.add_action(Chown())
self.add_action(Chgrp())
self.add_action_and_bindings(OpenPath(), ('RETURN',))
def name(self):
return "Dir"
class Chmod(method.Method):
def _execute(self, w, **vargs):
w.set_error('chmod not implemented')
class Chown(method.Method):
def _execute(self, w, **vargs):
w.set_error('chown not implemented')
class Chgrp(method.Method):
def _execute(self, w, **vargs):
w.set_error('chgrp not implemented')
class OpenPath(method.Method):
def _execute(self, w, **vargs):
assert w.buffer.btype == 'dir'
c = w.logical_cursor()
p = Point(0, c.y)
t = w.get_next_token_by_type(p, 'name')
path = os.path.join(w.buffer.path, t.string)
w.set_error("opening %r" % path)
w.application.methods['open-file'].execute(w, filename=path)