forked from enlightenment/edi
elm_code widget: backspace or delete merges lines
This is text only for now but serves as editor foundation.
This commit is contained in:
parent
ecb35beb30
commit
2f25503f8f
|
@ -212,6 +212,34 @@ EAPI void elm_code_file_line_insert(Elm_Code_File *file, unsigned int row, const
|
|||
}
|
||||
}
|
||||
|
||||
EAPI void elm_code_file_line_remove(Elm_Code_File *file, unsigned int row)
|
||||
{
|
||||
Eina_List *item, *next;
|
||||
Elm_Code_Line *line_item, *tofree = NULL;
|
||||
unsigned int r;
|
||||
|
||||
r = row;
|
||||
EINA_LIST_FOREACH_SAFE(file->lines, item, next, line_item)
|
||||
{
|
||||
if (line_item->number < row)
|
||||
continue;
|
||||
else if (line_item->number == row)
|
||||
{
|
||||
tofree = line_item;
|
||||
file->lines = eina_list_remove_list(file->lines, item);
|
||||
continue;
|
||||
}
|
||||
|
||||
line_item->number = r++;
|
||||
|
||||
if (file->parent)
|
||||
elm_code_callback_fire(file->parent, &ELM_CODE_EVENT_LINE_LOAD_DONE, line_item);
|
||||
}
|
||||
|
||||
if (tofree)
|
||||
elm_code_line_free(tofree);
|
||||
}
|
||||
|
||||
EAPI Elm_Code_Line *elm_code_file_line_get(Elm_Code_File *file, unsigned int number)
|
||||
{
|
||||
return eina_list_nth(file->lines, number - 1);
|
||||
|
|
|
@ -62,6 +62,8 @@ EAPI void elm_code_file_line_append(Elm_Code_File *file, const char *line, int l
|
|||
|
||||
EAPI void elm_code_file_line_insert(Elm_Code_File *file, unsigned int row, const char *line, int length, void *data);
|
||||
|
||||
EAPI void elm_code_file_line_remove(Elm_Code_File *file, unsigned int row);
|
||||
|
||||
EAPI Elm_Code_Line *elm_code_file_line_get(Elm_Code_File *file, unsigned int line);
|
||||
|
||||
/**
|
||||
|
|
|
@ -20,6 +20,29 @@ elm_code_line_text_get(Elm_Code_Line *line, unsigned int *length)
|
|||
return line->content;
|
||||
}
|
||||
|
||||
EAPI void
|
||||
elm_code_line_text_set(Elm_Code_Line *line, const char *chars, unsigned int length)
|
||||
{
|
||||
Elm_Code_File *file;
|
||||
char *newtext;
|
||||
|
||||
if (!line)
|
||||
return;
|
||||
|
||||
if (line->modified)
|
||||
free(line->modified);
|
||||
|
||||
newtext = malloc(sizeof(char) * length + 1);
|
||||
strncpy(newtext, chars, length);
|
||||
line->modified = newtext;
|
||||
line->length = length;
|
||||
// TODO update calculation
|
||||
line->unicode_length = length;
|
||||
|
||||
file = line->file;
|
||||
elm_code_callback_fire(file->parent, &ELM_CODE_EVENT_LINE_LOAD_DONE, line);
|
||||
}
|
||||
|
||||
static void
|
||||
_elm_code_line_tokens_move_right(Elm_Code_Line *line, int position, int move)
|
||||
{
|
||||
|
|
|
@ -22,6 +22,8 @@ extern "C" {
|
|||
|
||||
EAPI const char *elm_code_line_text_get(Elm_Code_Line *line, unsigned int *length);
|
||||
|
||||
EAPI void elm_code_line_text_set(Elm_Code_Line *line, const char *chars, unsigned int length);
|
||||
|
||||
EAPI void elm_code_line_text_insert(Elm_Code_Line *line, unsigned int position, const char *string, int length);
|
||||
|
||||
EAPI void elm_code_line_text_remove(Elm_Code_Line *line, unsigned int position, int length);
|
||||
|
|
|
@ -648,6 +648,49 @@ _elm_code_widget_newline(Elm_Code_Widget *widget)
|
|||
elm_code_widget_cursor_position_set(1, row + 1));
|
||||
}
|
||||
|
||||
static void
|
||||
_elm_code_widget_backspaceline(Elm_Code_Widget *widget, Eina_Bool nextline)
|
||||
{
|
||||
Elm_Code *code;
|
||||
Elm_Code_Line *line, *otherline;
|
||||
unsigned int row, col;
|
||||
|
||||
const char *text1, *text2;
|
||||
char *newtext;
|
||||
unsigned int length1, length2;
|
||||
|
||||
eo_do(widget,
|
||||
code = elm_code_widget_code_get(),
|
||||
elm_code_widget_cursor_position_get(&col, &row));
|
||||
line = elm_code_file_line_get(code->file, row);
|
||||
|
||||
if (nextline)
|
||||
{
|
||||
otherline = elm_code_file_line_get(code->file, row + 1);
|
||||
text1 = elm_code_line_text_get(line, &length1);
|
||||
text2 = elm_code_line_text_get(otherline, &length2);
|
||||
}
|
||||
else
|
||||
{
|
||||
otherline = elm_code_file_line_get(code->file, row - 1);
|
||||
text1 = elm_code_line_text_get(otherline, &length1);
|
||||
text2 = elm_code_line_text_get(line, &length2);
|
||||
}
|
||||
|
||||
newtext = malloc(sizeof(char) * (length1 + length2 + 1));
|
||||
snprintf(newtext, length1 + 1, "%s", text1);
|
||||
snprintf(newtext + length1, length2 + 1, "%s", text2);
|
||||
|
||||
// TODO we need to merge tokens from these lines (move this to elm_code_text)
|
||||
elm_code_file_line_remove(code->file, otherline->number);
|
||||
elm_code_line_text_set(line, newtext, length1 + length2);
|
||||
|
||||
free(newtext);
|
||||
if (!nextline)
|
||||
eo_do(widget,
|
||||
elm_code_widget_cursor_position_set(length1 + 1, row - 1));
|
||||
}
|
||||
|
||||
static void
|
||||
_elm_code_widget_backspace(Elm_Code_Widget *widget)
|
||||
{
|
||||
|
@ -660,7 +703,16 @@ _elm_code_widget_backspace(Elm_Code_Widget *widget)
|
|||
elm_code_widget_cursor_position_get(&col, &row));
|
||||
|
||||
if (col <= 1)
|
||||
return;
|
||||
{
|
||||
if (row == 1)
|
||||
return;
|
||||
|
||||
_elm_code_widget_backspaceline(widget, EINA_FALSE);
|
||||
line = elm_code_file_line_get(code->file, row - 1);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
line = elm_code_file_line_get(code->file, row);
|
||||
|
||||
elm_code_line_text_remove(line, col - 1, 1);
|
||||
|
@ -680,7 +732,13 @@ _elm_code_widget_delete(Elm_Code_Widget *widget)
|
|||
elm_code_widget_cursor_position_get(&col, &row));
|
||||
line = elm_code_file_line_get(code->file, row);
|
||||
if (col > line->unicode_length)
|
||||
return;
|
||||
{
|
||||
if (row == elm_code_file_lines_get(code->file))
|
||||
return;
|
||||
|
||||
_elm_code_widget_backspaceline(widget, EINA_TRUE);
|
||||
return;
|
||||
}
|
||||
|
||||
elm_code_line_text_remove(line, col, 1);
|
||||
eo_do(widget,
|
||||
|
|
Loading…
Reference in New Issue