122 lines
4.0 KiB
Python
122 lines
4.0 KiB
Python
from fide import *
|
|
|
|
def cursor_go_left():
|
|
buf = ctx.current_buffer
|
|
cursor = ctx.current_buffer.cursor
|
|
if cursor.col == 0:
|
|
if cursor.line_num > 0:
|
|
cursor.col = buf.lines[cursor.line_num-1].length
|
|
cursor.want_col = cursor.col
|
|
cursor.line_num -= 1
|
|
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
|
|
if cursor.line_num == len(buf.lines):
|
|
return
|
|
cursor.line_num = min(len(buf.lines) - 1, cursor.line_num + 1)
|
|
line_len = buf.lines[cursor.line_num].length
|
|
cursor.col = min(cursor.want_col, line_len)
|
|
|
|
def cursor_go_up():
|
|
buf = ctx.current_buffer
|
|
cursor = ctx.current_buffer.cursor
|
|
if cursor.line_num == 0:
|
|
return
|
|
cursor.line_num = max(0, cursor.line_num - 1)
|
|
line_len = buf.lines[cursor.line_num].length
|
|
# TODO: This should be global
|
|
cursor.col = min(cursor.want_col, line_len)
|
|
|
|
def cursor_go_beginning_line():
|
|
buf = ctx.current_buffer
|
|
buf.cursor.col = 0
|
|
buf.cursor.want_col = buf.cursor.col
|
|
|
|
def cursor_go_end_line():
|
|
buf = ctx.current_buffer
|
|
buf.cursor.col = buffer_line_len(buf)
|
|
buf.cursor.want_col = buf.cursor.col
|
|
|
|
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
|