From 2a7fb0d165aec1b7ab183f3d229d19efdc9e329b Mon Sep 17 00:00:00 2001 From: moculus Date: Sat, 23 Jun 2007 20:12:00 +0000 Subject: [PATCH] --HG-- branch : pmacs2 --- application.py | 5 +-- mode_life.py | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++ mode_perl.py | 2 +- mode_python.py | 2 +- 4 files changed, 91 insertions(+), 4 deletions(-) create mode 100644 mode_life.py diff --git a/application.py b/application.py index 5624dc8..f8628e6 100755 --- a/application.py +++ b/application.py @@ -11,7 +11,7 @@ from point2 import Point #import mode_replace, mode_xml, mode_console, mode_sh, mode_text, mode_which #import mode_mutt, mode_sql, mode_javascript, mode_diff, mode_blame, mode_tt import mode2, mode_mini, mode_python, mode_perl, mode_search, mode_replace -import mode_xml +import mode_xml, mode_life def run(buffers, jump_to_line=None, init_mode=None): # save terminal state so we can restore it when the program exits @@ -94,6 +94,7 @@ class Application(object): # 'text': mode_text.Text, # 'which': mode_which.Which, 'xml': mode_xml.XML, + 'life': mode_life.Life, # 'mutt': mode_mutt.Mutt, # 'sql': mode_sql.Sql, # 'javascript': mode_javascript.Javascript, @@ -745,7 +746,7 @@ if __name__ == "__main__": # we will support using +19 as the first argument to indicate opening the # first file on line 19 (same as -g 19 or --goto 19) - if len(sys.argv) > 1 and args[0].startswith('+'): + if len(args) > 0 and args[0].startswith('+'): opts.goto = int(args[0][1:]) args = args[1:] diff --git a/mode_life.py b/mode_life.py new file mode 100644 index 0000000..43b8152 --- /dev/null +++ b/mode_life.py @@ -0,0 +1,86 @@ +import re, sets, string, sys +import color, commands, default, lex2, method, mode2, regex, tab2 +from point2 import Point +from lex2 import Grammar, ConstantRule, PatternRule, ContextPatternRule, \ + RegionRule, DualRegionRule + +class Life(mode2.Fundamental): + def __init__(self, w): + mode2.Fundamental.__init__(self, w) + self.add_action(LifeShiftLeft()) + self.add_action(LifeShiftRight()) + self.add_action_and_bindings(LifeDoTurn(), ('M-RETURN',)) + for c in string.letters + string.digits + string.punctuation: + self.add_action_and_bindings(LifeInsertString(c), c) + self.token = 'o' + w.buffer.set_lines(self.normalize_board(), force=True) + w.goto_beginning() + + def name(self): + return "Life" + def normalize_board(self): + lines = self.window.buffer.lines + s = ' ' * (self.window.width-1) + newlines = [s] * (self.window.height-1) + for i in range(0, min(len(lines), (self.window.height-1))): + chars = [' '] * (self.window.width-1) + for j in range(0, min(len(lines[i]), (self.window.height-1))): + if lines[i][j] != ' ': + chars[j] = self.token + newlines[i] = ''.join(chars) + return newlines + +class LifeShiftLeft(method.Method): + def _execute(self, w, **vargs): + newlines = list(w.buffer.lines) + for i in range(0, len(newlines)): + newlines[i] = newlines[i][10:] + newlines[i][:10] + w.buffer.set_lines(newlines) + w.goto_beginning() +class LifeShiftRight(method.Method): + def _execute(self, w, **vargs): + newlines = list(w.buffer.lines) + for i in range(0, len(newlines)): + newlines[i] = newlines[i][-10:] + newlines[i][:-10] + w.buffer.set_lines(newlines) + w.goto_beginning() + +# insert text +class LifeInsertString(method.Method): + _is_method = False + def __init__(self, s): + self.name = "life-insert-string-%s" % (s) + self.args = [] + self.help = "Insert %r into the current buffer." % s + self.string = s + def _execute(self, w, **vargs): + if w.cursor_char() == '\n': + return + w.right_delete() + w.insert_string_at_cursor(w.mode.token) + +class LifeDoTurn(method.Method): + '''Run a turn of life on the current buffer''' + def _execute(self, w, **vargs): + lines = w.mode.normalize_board() + newlines = list(lines) + w.buffer.set_lines(lines) + w.goto_beginning() + for y in range(0, (w.height-1)): + line = list(newlines[y]) + for x in range(0, (w.width-1)): + on = lines[y][x] != ' ' + count = 0 + for (i,j) in ((-1, -1), (-1, 0), (-1, 1), (0, 1), + (0, -1), (1, -1), (1, 0), (1, 1)): + y2 = (y + i) % (w.height-2) + x2 = (x + j) % (w.width-2) + if lines[y2][x2] != ' ': + count += 1 + if count == 3 or count == 2 and on: + line[x] = w.mode.token + else: + line[x] = ' ' + newlines[y] = ''.join(line) + w.buffer.set_lines(newlines) + w.goto_beginning() diff --git a/mode_perl.py b/mode_perl.py index 31ed05c..e65fcb1 100644 --- a/mode_perl.py +++ b/mode_perl.py @@ -112,7 +112,7 @@ class PerlTabber(tab2.StackTabber): if not highlighter.tokens[y]: return False t = highlighter.tokens[y][0] - return t.name == 'keyword' and t.string == 'sub': + return t.name == 'keyword' and t.string == 'sub' def _handle_open_token(self, currlvl, y, i): currlvl = tab2.StackTabber._handle_open_token(self, currlvl, y, i) return currlvl diff --git a/mode_python.py b/mode_python.py index fddd534..a66afee 100644 --- a/mode_python.py +++ b/mode_python.py @@ -128,7 +128,7 @@ class PythonTabber(tab2.Tabber): if not highlighter.tokens[y]: return False t = highlighter.tokens[y][0] - return t.name == 'keyword' and t.string == 'def': + return t.name == 'keyword' and t.string == 'def' def __init__(self, m): tab2.Tabber.__init__(self, m)