diff --git a/application.py b/application.py index 1aea537..adcf6eb 100755 --- a/application.py +++ b/application.py @@ -3,7 +3,7 @@ import curses, curses.ascii, getpass, os, re, string, sys, termios, time import traceback from subprocess import Popen, PIPE, STDOUT -import buffer, buffer.about, buffer.color +import buffer, buffer.about, buffer.color, buffer.console, buffer.data, buffer.fs import bufferlist, color, completer, keyinput, method, minibuffer, mode import util, window from point import Point @@ -163,10 +163,10 @@ class Application(object): # initialize our buffers # note that the first buffer in buffers will be initially visible buffers.append(buffer.about.AboutBuffer()) - buffers.append(buffer.ConsoleBuffer()) + buffers.append(buffer.console.ConsoleBuffer()) if self.rcerror: - buffers.insert(0, buffer.DataBuffer('*RcError*', self.rcerror)) + buffers.insert(0, buffer.data.DataBuffer('*RcError*', self.rcerror)) # build windows for our buffers for b in buffers: @@ -368,7 +368,7 @@ class Application(object): else: b = buffer.FileBuffer(path, name=name) elif os.path.isdir(path): - b = buffer.DirBuffer(path, name=name) + b = buffer.fs.DirBuffer(path, name=name) mode_name = 'dir' else: raise Exception, "not a file or dir: %r" % path @@ -466,7 +466,7 @@ class Application(object): if self.has_buffer_name(name): b = self.bufferlist.buffer_names[name] self.remove_buffer(b) - b = buffer.DataBuffer(name, data) + b = buffer.data.DataBuffer(name, data) if modename is not None: b.modename = modename window.Window(b, self, height=0, width=0) @@ -892,7 +892,7 @@ def open_plain_file(path, name=None, binary=False): else: return buffer.FileBuffer(path, name) elif os.path.isdir(path): - return buffer.DirBuffer(path, name) + return buffer.fs.DirBuffer(path, name) else: raise Exception, "can't open %r; unsupported file type" % path diff --git a/buffer/__init__.py b/buffer/__init__.py index 167c01f..94182cf 100644 --- a/buffer/__init__.py +++ b/buffer/__init__.py @@ -354,48 +354,6 @@ class Buffer(object): # should not happen raise Exception, "iiiijjjj" -class DataBuffer(Buffer): - btype = 'data' - def __init__(self, name, data): - Buffer.__init__(self) - self._name = name - self.lines = data.split("\n") - def name(self): - return self._name - def close(self): - pass - def readonly(self): - return True - -# console is another singleton -console = None -class ConsoleBuffer(Buffer): - btype = 'console' - modename = 'console' - def __new__(cls, *args, **kwargs): - global console - if console is None: - b = object.__new__(ConsoleBuffer, *args, **kwargs) - console = b - return console - def __init__(self): - Buffer.__init__(self) - self.clear() - def clear(self): - lines = ['Python Console\n', - "Evaluate python expressions in the editor's context (self)\n", - 'Press Control-] to exit\n'] - console.set_data(''.join(lines), force=True) - def name(self): - return '*Console*' - def changed(self): - return False - def close(self): - global console - console = None - def readonly(self): - return True - class InterpreterPipeError(Exception): pass @@ -738,80 +696,3 @@ class Binary32Buffer(FileBuffer): return '\n'.join(lines) def write_filter(self, data): return ''.join(self.rawdata) - -class DirBuffer(Buffer): - btype = 'dir' - def __init__(self, path, name=None): - Buffer.__init__(self) - self.path = os.path.realpath(path) - def changed(self): - return False - def readonly(self): - return True - def name(self): - return self.path - def path_exists(self): - return os.path.exists(self.path) - - def _get_names(self): - if not self.path_exists(): - raise Exception, "directory %r does not exists" % self.path - names = os.listdir(self.path) - if self.path != '/': - names.insert(0, '..') - names.insert(0, '.') - return names - def _make_path(self, name): - return os.path.join(self.path, name) - def _get_lines(self): - names = self._get_names() - - fieldlines = [] - maxlens = [0] * 5 - for name in names: - path = self._make_path(name) - fields = dirutil.path_fields(path, name) - for i in range(0, 5): - try: - maxlens[i] = max(maxlens[i], len(fields[i])) - except: - raise Exception, '%d %r' % (i, fields[i]) - fieldlines.append(fields) - - fieldlines.sort(cmp=dirutil.path_sort) - fmt = '%%%ds %%-%ds %%-%ds %%%ds %%%ds %%s' % tuple(maxlens) - - lines = [] - for fields in fieldlines: - s = fmt % fields - lines.append(s) - return lines - def open(self): - self.lines = self._get_lines() - def reload(self): - lines = self._get_lines() - self.set_lines(lines, force=True) - def save(self, force=False): - raise Exception, "can't save a directory buffer" - def save_as(self, path): - raise Exception, "can't save a directory buffer" - -class PathListBuffer(DirBuffer): - btype = 'pathlist' - def __init__(self, name, paths): - Buffer.__init__(self) - self.paths = list(paths) - self.path = os.getcwd() - self._name = name - def path_exists(self): - raise Exception - def _get_names(self): - cwd = os.getcwd() - return [x.replace(cwd, '.', 1) for x in self.paths] - def _make_path(self, name): - if name.startswith('.'): - return name.replace('.', os.getcwd(), 1) - else: - return name - def name(self): - return self._name diff --git a/buffer/color.py b/buffer/color.py index f3c77d2..04a9070 100644 --- a/buffer/color.py +++ b/buffer/color.py @@ -1,6 +1,7 @@ import re import lex -from buffer import Buffer, DataBuffer +from buffer import Buffer +from buffer.data import DataBuffer from highlight import Highlighter # NOTE: this highlighter will not reprocess the data given. it is intended to diff --git a/buffer/console.py b/buffer/console.py new file mode 100644 index 0000000..5ff6e8a --- /dev/null +++ b/buffer/console.py @@ -0,0 +1,30 @@ +from buffer import Buffer + +# console is another singleton +console = None +class ConsoleBuffer(Buffer): + btype = 'console' + modename = 'console' + def __new__(cls, *args, **kwargs): + global console + if console is None: + b = object.__new__(ConsoleBuffer, *args, **kwargs) + console = b + return console + def __init__(self): + Buffer.__init__(self) + self.clear() + def clear(self): + lines = ['Python Console\n', + "Evaluate python expressions in the editor's context (self)\n", + 'Press Control-] to exit\n'] + console.set_data(''.join(lines), force=True) + def name(self): + return '*Console*' + def changed(self): + return False + def close(self): + global console + console = None + def readonly(self): + return True diff --git a/buffer/data.py b/buffer/data.py new file mode 100644 index 0000000..7ac0d8a --- /dev/null +++ b/buffer/data.py @@ -0,0 +1,14 @@ +from buffer import Buffer + +class DataBuffer(Buffer): + btype = 'data' + def __init__(self, name, data): + Buffer.__init__(self) + self._name = name + self.lines = data.split("\n") + def name(self): + return self._name + def close(self): + pass + def readonly(self): + return True diff --git a/buffer/fs.py b/buffer/fs.py new file mode 100644 index 0000000..3734487 --- /dev/null +++ b/buffer/fs.py @@ -0,0 +1,79 @@ +import dirutil, os +from buffer import Buffer + +class DirBuffer(Buffer): + btype = 'dir' + def __init__(self, path, name=None): + Buffer.__init__(self) + self.path = os.path.realpath(path) + def changed(self): + return False + def readonly(self): + return True + def name(self): + return self.path + def path_exists(self): + return os.path.exists(self.path) + + def _get_names(self): + if not self.path_exists(): + raise Exception, "directory %r does not exists" % self.path + names = os.listdir(self.path) + if self.path != '/': + names.insert(0, '..') + names.insert(0, '.') + return names + def _make_path(self, name): + return os.path.join(self.path, name) + def _get_lines(self): + names = self._get_names() + + fieldlines = [] + maxlens = [0] * 5 + for name in names: + path = self._make_path(name) + fields = dirutil.path_fields(path, name) + for i in range(0, 5): + try: + maxlens[i] = max(maxlens[i], len(fields[i])) + except: + raise Exception, '%d %r' % (i, fields[i]) + fieldlines.append(fields) + + fieldlines.sort(cmp=dirutil.path_sort) + fmt = '%%%ds %%-%ds %%-%ds %%%ds %%%ds %%s' % tuple(maxlens) + + lines = [] + for fields in fieldlines: + s = fmt % fields + lines.append(s) + return lines + def open(self): + self.lines = self._get_lines() + def reload(self): + lines = self._get_lines() + self.set_lines(lines, force=True) + def save(self, force=False): + raise Exception, "can't save a directory buffer" + def save_as(self, path): + raise Exception, "can't save a directory buffer" + +class PathListBuffer(DirBuffer): + btype = 'pathlist' + def __init__(self, name, paths): + Buffer.__init__(self) + self.paths = list(paths) + self.path = os.getcwd() + self._name = name + def path_exists(self): + raise Exception + def _get_names(self): + cwd = os.getcwd() + return [x.replace(cwd, '.', 1) for x in self.paths] + def _make_path(self, name): + if name.startswith('.'): + return name.replace('.', os.getcwd(), 1) + else: + return name + def name(self): + return self._name diff --git a/method/introspect.py b/method/introspect.py index 5763b9b..3408da4 100644 --- a/method/introspect.py +++ b/method/introspect.py @@ -1,7 +1,8 @@ import os, commands, re, sets, tempfile from subprocess import Popen, PIPE, STDOUT -import buffer, completer, default, dirutil, regex, util, window +import buffer, buffer.console +import completer, default, dirutil, regex, util, window import mode.mini from point import Point @@ -144,7 +145,7 @@ class OpenConsole(Method): def execute(self, w, **vargs): a = w.application if not a.has_buffer_name('*Console*'): - b = buffer.ConsoleBuffer() + b = buffer.console.ConsoleBuffer() a.add_buffer(b) window.Window(b, a) b = a.bufferlist.get_buffer_by_name('*Console*') diff --git a/mode/dir.py b/mode/dir.py index d0e758b..9732059 100644 --- a/mode/dir.py +++ b/mode/dir.py @@ -1,5 +1,6 @@ import commands, dirutil, grp, method, mode, os.path, pwd, re -import buffer, window +import buffer, buffer.fs +import window from lex import Grammar, PatternRule, RegionRule, PatternGroupRule from point import Point from method import Method, Argument @@ -56,7 +57,7 @@ class DirGrep(Method): (status, output) = commands.getstatusoutput(cmd) paths = [x for x in output.split('\n') if x] bufname = '*%s*' % self.name.title() - b = buffer.PathListBuffer(bufname, paths) + b = buffer.fs.PathListBuffer(bufname, paths) b.modename = 'dir' b.open() window.Window(b, w.application, height=0, width=0)