Extract more functions
This commit is contained in:
parent
8356ca6a25
commit
d690d7a2a3
67
editing.py
67
editing.py
@ -42,3 +42,70 @@ def cursor_go_up():
|
||||
# TODO: This should be global
|
||||
line_len = 20
|
||||
cursor.col = min(cursor.want_col, line_len)
|
||||
|
||||
def buffer_insert_line_below(buf: Buffer, text):
|
||||
line_num = buffer_line_num(buf)
|
||||
buf.lines.insert(line_num + 1, line_create(text))
|
||||
|
||||
def buffer_newline_at_pos():
|
||||
buf = ctx.current_buffer
|
||||
curr_line = buffer_line_current(buf)
|
||||
cursor = buf.cursor
|
||||
if cursor.col < buffer_line_len(buf):
|
||||
remainder = curr_line.length - cursor.col
|
||||
line = curr_line.piece.string_at(cursor.col, remainder)
|
||||
curr_line.piece.delete(cursor.col, remainder)
|
||||
curr_line.length -= remainder
|
||||
else:
|
||||
line = ""
|
||||
buffer_insert_line_below(buf, line)
|
||||
cursor.line_num += 1
|
||||
# Eventually when we need to be indentation aware
|
||||
cursor.col = 0
|
||||
cursor.want_col = 0
|
||||
|
||||
|
||||
def buffer_delete_character_under_cursor():
|
||||
buf = ctx.current_buffer
|
||||
cursor = buf.cursor
|
||||
if cursor_pos(cursor) != (0,0):
|
||||
if cursor.col == 0:
|
||||
curr_line = buf.lines.pop(cursor.line_num)
|
||||
cursor.line_num -= 1
|
||||
above_row = buf.lines[cursor.line_num]
|
||||
above_row.piece.insert(curr_line.piece.get_text(), above_row.length)
|
||||
cursor.col = above_row.length
|
||||
cursor.want_col = cursor.col
|
||||
above_row.length += curr_line.length
|
||||
else:
|
||||
curr_line = buffer_line_current(buf)
|
||||
curr_line.piece.delete(cursor.col - 1, 1)
|
||||
curr_line.length -= 1
|
||||
cursor.col = max(0, cursor.col - 1)
|
||||
cursor.want_col = cursor.col
|
||||
|
||||
def buffer_delete_next_character():
|
||||
buf = ctx.current_buffer
|
||||
cursor = buf.cursor
|
||||
curr_line = buffer_line_current(buf)
|
||||
if cursor.col >= buffer_line_len(buf):
|
||||
if cursor.line_num < len(buf.lines) - 1:
|
||||
below_row = buf.lines.pop(cursor.line_num+1)
|
||||
if below_row.length > 0:
|
||||
curr_line.piece.insert(below_row.piece.get_text(), curr_line.length)
|
||||
curr_line.length += below_row.length
|
||||
cursor.want_col = cursor.col
|
||||
else:
|
||||
if curr_line.length >= 1:
|
||||
curr_line.piece.delete(cursor.col, 1)
|
||||
curr_line.length -= 1
|
||||
cursor.want_col = cursor.col
|
||||
|
||||
def buffer_insert_text_at_cursor(text):
|
||||
cursor = ctx.current_buffer.cursor
|
||||
curr_line = buffer_line_current(ctx.current_buffer)
|
||||
curr_line.piece.insert(text, cursor.col)
|
||||
text_len = len(text)
|
||||
curr_line.length += text_len
|
||||
cursor.col += text_len
|
||||
cursor.want_col = text_len
|
||||
|
56
fide.py
56
fide.py
@ -61,44 +61,6 @@ def buffer_total_lines(buf: Buffer) -> int:
|
||||
def buffer_line_len(buf: Buffer) -> int:
|
||||
return buffer_line_current(buf).length
|
||||
|
||||
def buffer_insert_line_below(buf: Buffer, text):
|
||||
line_num = buffer_line_num(buf)
|
||||
buf.lines.insert(line_num + 1, line_create(text))
|
||||
|
||||
def buffer_newline_at_pos(buf):
|
||||
curr_line = buffer_line_current(buf)
|
||||
cursor = buf.cursor
|
||||
if cursor.col < buffer_line_len(buf):
|
||||
remainder = curr_line.length - cursor.col
|
||||
line = curr_line.piece.string_at(cursor.col, remainder)
|
||||
curr_line.piece.delete(cursor.col, remainder)
|
||||
curr_line.length -= remainder
|
||||
else:
|
||||
line = ""
|
||||
buffer_insert_line_below(buf, line)
|
||||
cursor.line_num += 1
|
||||
# Eventually when we need to be indentation aware
|
||||
cursor.col = 0
|
||||
cursor.want_col = 0
|
||||
|
||||
|
||||
def buffer_delete_character_at_pos(buf):
|
||||
cursor = buf.cursor
|
||||
if cursor_pos(cursor) != (0,0):
|
||||
if cursor.col == 0:
|
||||
curr_line = buf.lines.pop(cursor.line_num)
|
||||
cursor.line_num -= 1
|
||||
above_row = buf.lines[cursor.line_num]
|
||||
above_row.piece.insert(curr_line.piece.get_text(), above_row.length)
|
||||
cursor.col = above_row.length
|
||||
cursor.want_col = cursor.col
|
||||
above_row.length += curr_line.length
|
||||
else:
|
||||
curr_line = buffer_line_current(buf)
|
||||
curr_line.piece.delete(cursor.col - 1, 1)
|
||||
curr_line.length -= 1
|
||||
cursor.col = max(0, cursor.col - 1)
|
||||
cursor.want_col = cursor.col
|
||||
|
||||
oswinx, oswiny = 1940,50
|
||||
os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (oswinx,oswiny)
|
||||
@ -161,24 +123,12 @@ def main():
|
||||
if event.key == pg.K_RETURN:
|
||||
src = buffer_line_current(current_buffer).piece.get_text()
|
||||
console.runsource(src[3:])
|
||||
# console.runcode()
|
||||
elif event.key == pg.K_RETURN:
|
||||
buffer_newline_at_pos(current_buffer)
|
||||
buffer_newline_at_pos()
|
||||
elif event.key == pg.K_BACKSPACE:
|
||||
buffer_delete_character_at_pos(current_buffer)
|
||||
buffer_delete_character_under_cursor()
|
||||
elif event.key == pg.K_DELETE:
|
||||
if cursor.col >= buffer_line_len(current_buffer):
|
||||
if cursor.line_num < len(current_buffer.lines) - 1:
|
||||
below_row = current_buffer.lines.pop(cursor.line_num+1)
|
||||
if below_row.length > 0:
|
||||
curr_line.piece.insert(below_row.piece.get_text(), curr_line.length)
|
||||
curr_line.length += below_row.length
|
||||
cursor.want_col = cursor.col
|
||||
else:
|
||||
if curr_line.length >= 1:
|
||||
curr_line.piece.delete(cursor.col, 1)
|
||||
curr_line.length -= 1
|
||||
cursor.want_col = cursor.col
|
||||
buffer_delete_next_character()
|
||||
elif event.key == pg.K_LEFT:
|
||||
key_map['left']()
|
||||
elif event.key == pg.K_RIGHT:
|
||||
|
Loading…
x
Reference in New Issue
Block a user