diff --git a/editing.py b/editing.py index f320f9c..a3b52c1 100644 --- a/editing.py +++ b/editing.py @@ -3,8 +3,7 @@ from fide import * global ctx ctx = GlobalContext() -# def cursor_go_left(): -def go_left(): +def cursor_go_left(): buf = ctx.current_buffer cursor = ctx.current_buffer.cursor if cursor.col == 0: @@ -15,3 +14,31 @@ def go_left(): else: cursor.col = max(0, cursor.col - 1) cursor.want_col = cursor.col + +def cursor_go_right(): + buf = ctx.current_buffer + cursor = ctx.current_buffer.cursor + if cursor.col == buffer_line_len(buf): + if cursor.line_num < buffer_total_lines(buf) - 1: + cursor.col = 0 + cursor.want_col = cursor.col + cursor.line_num += 1 + else: + cursor.col = min(buffer_line_len(buf), cursor.col + 1) + cursor.want_col = cursor.col + +def cursor_go_down(): + buf = ctx.current_buffer + cursor = ctx.current_buffer.cursor + line_len = buffer_line_len(buf) + cursor.line_num = min(len(buf.lines) - 1, cursor.line_num + 1) + cursor.col = min(cursor.want_col, line_len) + +def cursor_go_up(): + buf = ctx.current_buffer + cursor = ctx.current_buffer.cursor + cursor.line_num = max(0, cursor.line_num - 1) + line_len = buffer_line_len(buf) + # TODO: This should be global + line_len = 20 + cursor.col = min(cursor.want_col, line_len) diff --git a/fide.py b/fide.py index f972d1e..a98c219 100755 --- a/fide.py +++ b/fide.py @@ -134,6 +134,12 @@ def main(): current_buffer = ctx.current_buffer console = InteractiveInterpreter(locals()) console.runsource('from editing import *') + key_map = { + 'left': cursor_go_left, + 'right': cursor_go_right, + 'up': cursor_go_up, + 'down': cursor_go_down + } while running: dt = clock.tick(120) / 1000.0 screen.fill('white') @@ -174,24 +180,13 @@ def main(): curr_line.length -= 1 cursor.want_col = cursor.col elif event.key == pg.K_LEFT: - cursor_go_left(current_buffer) + key_map['left']() elif event.key == pg.K_RIGHT: - if cursor.col == buffer_line_len(current_buffer): - if cursor.line_num < buffer_total_lines(current_buffer) - 1: - cursor.col = 0 - cursor.want_col = cursor.col - cursor.line_num += 1 - else: - cursor.col = min(buffer_line_len(current_buffer), cursor.col + 1) - cursor.want_col = cursor.col + key_map['right']() elif event.key == pg.K_UP: - cursor.line_num = max(0, cursor.line_num - 1) - line_len = buffer_line_len(current_buffer) - cursor.col = min(cursor.want_col, line_len) + key_map['up']() elif event.key == pg.K_DOWN: - line_len = buffer_line_len(current_buffer) - cursor.line_num = min(len(current_buffer.lines) - 1, cursor.line_num + 1) - cursor.col = min(cursor.want_col, line_len) + key_map['down']() elif event.key == pg.K_SPACE: curr_line.piece.insert(u' ', cursor.col) curr_line.length += 1