Commits

Ali Afshar committed 8dafd0e

Movement

  • Participants
  • Parent commits 58f874c

Comments (0)

Files changed (4)

File er/buffer.py

     :license: GPL, see LICENSE_FILE for more details.
 """
 
+import gtk
 import gtksourceview2
 
 
 
     def __init__(self):
         self._source_buf = gtksourceview2.Buffer()
+        self._source_buf.connect('notify::cursor-position',
+                                  self._on_cursor_moved)
+        self.last_column = None
+        self.last_line = None
 
     @property
     def _start_iter(self):
         return self._source_buf.get_iter_at_mark(
             self._source_buf.get_insert())
 
+    def _move_cursor(self, iter):
+        self._source_buf.place_cursor(iter)
+
+    def _move_cursor_vertically(self, count=1):
+        new_pos = self._cursor_iter.copy()
+        if count:
+            new_pos.backward_lines(count)
+        else:
+            raise ValueError
+        new_pos.set_line_offset(self.cursor_column)
+        self._move_cursor(new_pos)
+
     def set_text(self, s):
         self._source_buf.set_text(s)
 
     def cursor_line(self):
         return self._cursor_iter.get_line()
 
+    @property
+    def cursor_column(self):
+        # XXX broken for hidden things
+        return self._cursor_iter.get_line_offset()
 
+    def cursor_up(self, count=1):
+        new_pos = self._cursor_iter.copy()
+        if count:
+            new_pos.backward_lines(count)
+        else:
+            raise ValueError
+        new_pos.set_line_offset(self.cursor_column)
+        self._move_cursor(new_pos)
+
+    def cursor_down(self, count=1):
+        new_pos = self._cursor_iter.copy()
+        if count:
+            new_pos.forward_lines(count)
+        else:
+            raise ValueError
+        new_pos.set_line_offset(self.cursor_column)
+        print new_pos.ends_line()
+        self._move_cursor(new_pos)
+
+    def _on_cursor_moved(self, buf, prop):
+        print 'moved'
+        print buf.get_property('cursor-position')
+        print self.cursor_line, self.cursor_column
+
+    def _debug(self):
+        # XXX will probably go away
+        vb = gtk.HBox()
+        v = gtksourceview2.View(self._source_buf)
+        sw = gtk.ScrolledWindow()
+        sw.add(v)
+        vb.pack_start(sw)
+        v = gtksourceview2.View(self._source_buf)
+        sw = gtk.ScrolledWindow()
+        sw.add(v)
+        vb.pack_start(sw)
+        w = gtk.Window()
+        w.resize(600, 300)
+        w.add(vb)
+        w.show_all()
+        gtk.main()
+
+
+if __name__ == '__main__':
+    b = Buffer()
+    b.set_text(open(__file__).read())
+    b._debug()
     :license: GPLv3, see LICENSE.txt for more details.
 """
 
+
 _er_registry = {}
 
 
-
 class Command(object):
     """
     A command instance, to be used as a decorator.
     return _er_registry.get(name)
 
 
+# movement commands
+
+def go_up(ctx):
+    pass
+

File tests/test_buffer.py

 def test_cursor_lines():
     b = Buffer()
     b.set_text(u'Hello\nHello')
-    print b.cursor_line
     assert b.cursor_line == 1
 
+
+def test_cursor_columns():
+    b = Buffer()
+    b.set_text(u'Hello\nHello')
+    assert b.cursor_column == 5
+
+
+def test_move_cursor_up():
+    b = Buffer()
+    b.set_text(u'Hello\nHello')
+    assert b.cursor_line == 1
+    b.cursor_up()
+    refresh_gui()
+    assert b.cursor_column == 5
+    assert b.cursor_line == 0
+
+
+def test_move_cursor_up_empty():
+    b = Buffer()
+    b.set_text(u'H\nHello')
+    refresh_gui()
+    assert b.cursor_line == 1
+    assert b.cursor_column == 5
+    b.cursor_up()
+    refresh_gui()
+    assert b.cursor_column == 0
+    print b.cursor_line
+    assert b.cursor_line == 0
+
+
+def test_move_cursor_up_many():
+    b = Buffer()
+    b.set_text(u'H\nHello')
+    refresh_gui()
+    assert b.cursor_line == 1
+    assert b.cursor_column == 5
+    b.cursor_up(19)
+    refresh_gui()
+    print b.cursor_column
+    assert b.cursor_column == 0
+    assert b.cursor_line == 0
+
+def test_move_cursor_down():
+    b = Buffer()
+    b.set_text(u'Hello\nHello')
+    b._move_cursor(b._start_iter)
+    assert b.cursor_line == 0
+    b.cursor_down()
+    refresh_gui()
+    assert b.cursor_column == 0
+    assert b.cursor_line == 1
+
+
+def test_move_cursor_down_empty():
+    b = Buffer()
+    b.set_text(u'H\nHello')
+    b._move_cursor(b._start_iter)
+    refresh_gui()
+    assert b.cursor_line == 0
+    assert b.cursor_column == 0
+    b.cursor_down()
+    refresh_gui()
+    assert b.cursor_column == 0
+    assert b.cursor_line == 1
+
+
+def test_move_cursor_down_many():
+    b = Buffer()
+    b.set_text(u'H\nHello')
+    b._move_cursor(b._start_iter)
+    refresh_gui()
+    assert b.cursor_line == 0
+    assert b.cursor_column == 0
+    b.cursor_down(19)
+    refresh_gui()
+    assert b.cursor_column == 0
+    assert b.cursor_line == 1
+
+
+def test_move_cursor_down_many_empty():
+    b = Buffer()
+    b.set_text(u'Hello\nH')
+    b._move_cursor(b._start_iter)
+    end = b._cursor_iter.copy()
+    end.forward_to_line_end()
+    b._move_cursor(end)
+    refresh_gui()
+    assert b.cursor_line == 0
+    assert b.cursor_column == 5
+    b.cursor_down(19)
+    refresh_gui()
+    print b.cursor_line, b.cursor_column
+    assert b.cursor_line == 1
+    assert b.cursor_column == 1

File tests/test_ctx.py

 
 def test_context():
     ctx = Context(None, None, None, None)
-    assert 0