#include "line-ed.h" #include #include void alloc_empty_history_entry(struct line_ed *ed) { fx_value *str_v = fx_array_get_ref( ed->l_history, fx_array_get_size(ed->l_history) - 1); fx_string *str = NULL; fx_value_get_object(str_v, &str); if (!str || fx_string_get_size(str, FX_STRLEN_NORMAL) > 0) { str = fx_string_create(); fx_array_push_back(ed->l_history, FX_VALUE_OBJECT(str)); fx_string_unref(str); } ed->l_history_pos = fx_array_get_size(ed->l_history) - 1; } void save_buf_to_history(struct line_ed *ed) { fx_value *cur_v = fx_array_get_ref(ed->l_history, ed->l_history_pos); fx_string *cur = NULL; fx_value_get_object(cur_v, &cur); fx_string_clear(cur); fx_string_append_wstr(cur, ed->l_buf); } void append_buf_to_history(struct line_ed *ed) { fx_value *cur_v = fx_array_get_ref(ed->l_history, ed->l_history_pos); fx_string *cur = NULL; fx_value_get_object(cur_v, &cur); char s[] = {'\n', 0}; fx_string_append_cstr(cur, s); fx_string_append_wstr(cur, ed->l_buf); } void load_buf_from_history(struct line_ed *ed) { fx_value *cur_v = fx_array_get_ref(ed->l_history, ed->l_history_pos); fx_string *cur = NULL; fx_value_get_object(cur_v, &cur); size_t len = fx_min( size_t, (size_t)(ed->l_buf_end - ed->l_buf - 1), fx_string_get_size(cur, FX_STRLEN_CODEPOINTS)); const char *src = fx_string_get_cstr(cur); for (size_t i = 0; i < len; i++) { fx_wchar ch_src = fx_wchar_utf8_codepoint_decode(src); ed->l_buf[i] = ch_src; src += fx_wchar_utf8_codepoint_stride(src); } ed->l_buf[len] = '\0'; unsigned int x = 0, y = 0; for (size_t i = 0; ed->l_buf[i]; i++) { if (ed->l_buf[i] == '\n') { x = 0; y++; } else { x++; } } ed->l_buf_ptr = ed->l_buf + len; ed->l_line_end = ed->l_buf_ptr; ed->l_cursor_x = x; ed->l_cursor_y = y; } const char *last_history_line(struct line_ed *ed) { size_t nlines = fx_array_get_size(ed->l_history); if (nlines < 2) { return NULL; } fx_value *last_v = fx_array_get_ref(ed->l_history, nlines - 2); fx_string *last = NULL; fx_value_get_object(last_v, &last); return fx_string_get_cstr(last); }