From 277cd82b68e06713e43fcb6efb2ab37acef7ef6a Mon Sep 17 00:00:00 2001 From: moculus Date: Mon, 10 Nov 2008 15:18:08 +0000 Subject: [PATCH] --HG-- branch : pmacs2 --- application.py | 1 + mode/mbox.py | 108 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 109 insertions(+) create mode 100644 mode/mbox.py diff --git a/application.py b/application.py index 13fa7bf..78a24db 100755 --- a/application.py +++ b/application.py @@ -108,6 +108,7 @@ class Application(object): 'latex', 'insertmini', 'conf', 'haskell', 'erlang', 'iperl', 'iperlmini', 'ipython', 'ipythonmini', 'awk', 'shell', 'shellmini', 'fstab', 'yacc', 'pipe', + 'mbox', ) for name in names: exec("import mode.%s; mode.%s.install(self)" % (name, name)) diff --git a/mode/mbox.py b/mode/mbox.py new file mode 100644 index 0000000..3cb1d7e --- /dev/null +++ b/mode/mbox.py @@ -0,0 +1,108 @@ +import commands, dirutil, grp, mailbox, method, mode, os.path, pwd, re +import buffer, default, window +from lex import Grammar, PatternRule, RegionRule, PatternGroupRule +from point import Point +from buffer import Buffer +from method import Method, Argument, arg + +class MboxMsgBuffer(Buffer): + btype = 'mboxmsg' + def __init__(self, path, lineno, msg): + Buffer.__init__(self) + self.path = os.path.realpath(path) + self.base = os.path.basename(self.path) + self.lineno = lineno + self.msg = msg + def changed(self): return False + def readonly(self): return True + def path_exists(self): raise Exception + def _make_path(self, name): raise Exception + def save(self, force=False): raise Exception, "can't save an mbox message" + def save_as(self, path): raise Exception, "can't save an mbox message" + def name(self): return 'mbox:%s:%s' % (self.base, self.lineno) + def _get_lines(self): + return ['to', 'from', 'headers', 'subject', 'body'] + def open(self): + self.lines = self._get_lines() + def reload(self): + lines = self._get_lines() + self.set_lines(lines, force=True) + +class MboxListBuffer(Buffer): + btype = 'mboxlist' + format = '%(pos)4d %(flags)5.5s %(subject)s' + re1 = re.compile('[\n\t ]+') + def __init__(self, path): + Buffer.__init__(self) + self.path = os.path.realpath(path) + self.base = os.path.basename(self.path) + self.size = 0 + def changed(self): return False + def readonly(self): return True + def path_exists(self): raise Exception + def _make_path(self, name): raise Exception + def save(self, force=False): raise Exception, "can't save an mbox" + def save_as(self, path): raise Exception, "can't save an mbox" + def name(self): return 'mbox:%s' % (self.base) + def _get_lines(self): + f = open(self.path, 'r') + self.size = len(f.read()) + f.close() + self.mbox = mailbox.mbox(self.path) + + # + lines = [] + pos = 1 + for msg in self.mbox: + subject = self.re1.sub(' ', msg['subject']) + msize = len(str(msg)) + d = { + 'pos': pos, + 'flags': ''.join(msg.get_flags()), + 'subject': subject, + } + s = self.format % d + lines.append(s) + pos += 1 + # + + return lines + def open(self): + self.lines = self._get_lines() + def reload(self): + lines = self._get_lines() + self.set_lines(lines, force=True) + +class MboxRefresh(Method): + def _execute(self, w, **vargs): + w.buffer.reload() +class MboxOpenMsg(Method): + def _execute(self, w, **vargs): + w.set_error('your implementation here') +class MboxOpenPath(Method): + args = [arg('mbox', dt="path", p="Open Mbox: ", dv=default.path_dirname, + ld=True, h="mbox to open")] + def _execute(self, w, **vargs): + b = MboxListBuffer(vargs['mbox']) + b.open() + window.Window(b, w.application, height=0, width=0, mode_name='mbox') + w.application.add_buffer(b) + w.application.switch_buffer(b) + +class Mbox(mode.Fundamental): + modename = 'Mbox' + colors = {} + actions = [MboxRefresh, MboxOpenPath, MboxOpenMsg] + def __init__(self, w): + mode.Fundamental.__init__(self, w) + self.add_bindings('mbox-refresh', ('C-c r',)) + self.add_bindings('mbox-open-msg', ('RETURN',)) + #self.add_bindings('open-path', ('RETURN',)) + #self.add_bindings('dir-grep', ('C-c G',)) + #self.add_bindings('chmod', ('C-c m',)) + #self.add_bindings('chown', ('C-c o',)) + #self.add_bindings('chgrp', ('C-c g',)) + #self.add_bindings('touch-path', ('C-c t',)) + #self.add_bindings('remove-path', ('DELETE', 'BACKSPACE', 'C-d')) + +install = Mbox.install