2015-02-22 12:07:08 -08:00
|
|
|
#ifdef HAVE_CONFIG
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "Elm_Code.h"
|
|
|
|
|
|
|
|
#include "elm_code_private.h"
|
|
|
|
|
2015-02-26 15:19:09 -08:00
|
|
|
EAPI const char *
|
2015-03-08 03:14:02 -07:00
|
|
|
elm_code_line_text_get(Elm_Code_Line *line, unsigned int *length)
|
2015-02-22 12:07:08 -08:00
|
|
|
{
|
|
|
|
if (!line)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (length)
|
|
|
|
*length = line->length;
|
|
|
|
|
|
|
|
if (line->modified)
|
|
|
|
return line->modified;
|
|
|
|
return line->content;
|
|
|
|
}
|
|
|
|
|
2015-03-08 03:33:07 -07:00
|
|
|
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;
|
2015-03-14 09:42:56 -07:00
|
|
|
line->unicode_length = elm_code_text_unicode_strlen(line->modified, line->length);
|
2015-03-08 03:33:07 -07:00
|
|
|
|
|
|
|
file = line->file;
|
|
|
|
elm_code_callback_fire(file->parent, &ELM_CODE_EVENT_LINE_LOAD_DONE, line);
|
|
|
|
}
|
|
|
|
|
2015-03-22 15:36:04 -07:00
|
|
|
EAPI int
|
|
|
|
elm_code_line_text_strpos(Elm_Code_Line *line, const char *search, int offset)
|
|
|
|
{
|
|
|
|
unsigned int length, searchlen, c;
|
|
|
|
const char *content;
|
|
|
|
char *ptr;
|
|
|
|
|
|
|
|
searchlen = strlen(search);
|
|
|
|
content = elm_code_line_text_get(line, &length);
|
|
|
|
ptr = (char *) content;
|
|
|
|
|
|
|
|
if (searchlen > length)
|
|
|
|
return ELM_CODE_TEXT_NOT_FOUND;
|
|
|
|
|
|
|
|
ptr += offset;
|
|
|
|
for (c = offset; c < length - strlen(search); c++)
|
|
|
|
{
|
|
|
|
if (!strncmp(ptr, search, searchlen))
|
|
|
|
return c;
|
|
|
|
|
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ELM_CODE_TEXT_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
|
|
|
EAPI Eina_Bool
|
|
|
|
elm_code_line_text_contains(Elm_Code_Line *line, const char *search)
|
|
|
|
{
|
|
|
|
return elm_code_line_text_strpos(line, search, 0) != ELM_CODE_TEXT_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
2015-03-25 14:55:00 -07:00
|
|
|
EAPI char *
|
|
|
|
elm_code_line_text_substr(Elm_Code_Line *line, unsigned int position, int length)
|
|
|
|
{
|
|
|
|
const char *content;
|
|
|
|
|
|
|
|
if (!line || length < 1)
|
|
|
|
return strdup("");
|
|
|
|
|
|
|
|
if (position + length > line->length)
|
|
|
|
length = line->length - position;
|
|
|
|
|
|
|
|
content = elm_code_line_text_get(line, NULL);
|
|
|
|
return strndup(content + position, length);
|
|
|
|
}
|
|
|
|
|
2015-02-28 14:50:18 -08:00
|
|
|
static void
|
|
|
|
_elm_code_line_tokens_move_right(Elm_Code_Line *line, int position, int move)
|
|
|
|
{
|
|
|
|
Eina_List *item;
|
|
|
|
Elm_Code_Token *token;
|
|
|
|
|
|
|
|
EINA_LIST_FOREACH(line->tokens, item, token)
|
|
|
|
{
|
|
|
|
if (token->end >= position)
|
|
|
|
token->end += move;
|
|
|
|
|
|
|
|
if (token->start > position)
|
|
|
|
token->start += move;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-08 00:44:26 -08:00
|
|
|
static void
|
|
|
|
_elm_code_line_tokens_move_left(Elm_Code_Line *line, int position, int move)
|
|
|
|
{
|
|
|
|
Eina_List *item, *next;
|
|
|
|
Elm_Code_Token *token;
|
|
|
|
|
|
|
|
EINA_LIST_FOREACH_SAFE(line->tokens, item, next, token)
|
|
|
|
{
|
|
|
|
if (token->end >= position)
|
|
|
|
token->end -= move;
|
|
|
|
|
|
|
|
if (token->start > position)
|
|
|
|
token->start -= move;
|
|
|
|
|
|
|
|
if (token->end < token->start)
|
|
|
|
line->tokens = eina_list_remove_list(line->tokens, item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-22 12:07:08 -08:00
|
|
|
EAPI void
|
2015-03-07 11:48:02 -08:00
|
|
|
elm_code_line_text_insert(Elm_Code_Line *line, unsigned int position, const char *string, int length)
|
2015-02-22 12:07:08 -08:00
|
|
|
{
|
|
|
|
Elm_Code_File *file;
|
|
|
|
char *inserted;
|
|
|
|
|
|
|
|
if (!line)
|
|
|
|
return;
|
|
|
|
|
|
|
|
inserted = malloc(sizeof(char) * line->length + length + 1);
|
2015-03-07 11:48:02 -08:00
|
|
|
if (position > 0)
|
|
|
|
position--;
|
2015-02-22 12:07:08 -08:00
|
|
|
if (position > line->length)
|
|
|
|
position = line->length;
|
|
|
|
|
2015-02-28 14:50:18 -08:00
|
|
|
_elm_code_line_tokens_move_right(line, position + 1, length);
|
|
|
|
|
2015-02-22 12:07:08 -08:00
|
|
|
if (line->modified)
|
|
|
|
{
|
|
|
|
strncpy(inserted, line->modified, position);
|
|
|
|
strncpy(inserted + position, string, length);
|
|
|
|
strncpy(inserted + position + length, line->modified + position, line->length - position);
|
|
|
|
|
|
|
|
free(line->modified);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
strncpy(inserted, line->content, position);
|
|
|
|
strncpy(inserted + position, string, length);
|
|
|
|
strncpy(inserted + position + length, line->content + position, line->length - position);
|
|
|
|
}
|
|
|
|
|
|
|
|
line->modified = inserted;
|
2015-03-07 13:35:59 -08:00
|
|
|
line->length += length;
|
2015-03-14 09:42:56 -07:00
|
|
|
line->unicode_length = elm_code_text_unicode_strlen(line->modified, line->length);
|
2015-02-22 12:07:08 -08:00
|
|
|
|
|
|
|
file = line->file;
|
|
|
|
elm_code_callback_fire(file->parent, &ELM_CODE_EVENT_LINE_LOAD_DONE, line);
|
|
|
|
}
|
2015-03-08 00:44:26 -08:00
|
|
|
|
|
|
|
EAPI void
|
|
|
|
elm_code_line_text_remove(Elm_Code_Line *line, unsigned int position, int length)
|
|
|
|
{
|
|
|
|
Elm_Code_File *file;
|
|
|
|
char *removed;
|
|
|
|
|
|
|
|
if (!line)
|
|
|
|
return;
|
|
|
|
|
|
|
|
removed = malloc(sizeof(char) * line->length - length + 1);
|
|
|
|
if (position > 0)
|
|
|
|
position--;
|
|
|
|
if (position > line->length)
|
|
|
|
position = line->length;
|
|
|
|
|
|
|
|
_elm_code_line_tokens_move_left(line, position + 1, length);
|
|
|
|
|
|
|
|
if (line->modified)
|
|
|
|
{
|
|
|
|
strncpy(removed, line->modified, position);
|
|
|
|
strncpy(removed + position, line->modified + position + length, line->length - position - length);
|
|
|
|
|
|
|
|
free(line->modified);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
strncpy(removed, line->content, position);
|
|
|
|
strncpy(removed + position, line->content + position + length, line->length - position - length);
|
|
|
|
}
|
|
|
|
|
|
|
|
line->modified = removed;
|
|
|
|
line->length -= length;
|
2015-03-14 09:42:56 -07:00
|
|
|
line->unicode_length = elm_code_text_unicode_strlen(line->modified, line->length);
|
2015-03-08 00:44:26 -08:00
|
|
|
|
|
|
|
file = line->file;
|
|
|
|
elm_code_callback_fire(file->parent, &ELM_CODE_EVENT_LINE_LOAD_DONE, line);
|
|
|
|
}
|
2015-03-14 09:42:56 -07:00
|
|
|
|
|
|
|
/* generic text functions */
|
|
|
|
|
|
|
|
EAPI unsigned int
|
|
|
|
elm_code_text_unicode_strlen(const char *chars, unsigned int length)
|
|
|
|
{
|
|
|
|
Eina_Unicode unicode;
|
|
|
|
unsigned int count = 0;
|
|
|
|
int index = 0;
|
|
|
|
|
|
|
|
if (chars == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
while ((unsigned int) index < length)
|
|
|
|
{
|
|
|
|
unicode = eina_unicode_utf8_next_get(chars, &index);
|
|
|
|
if (unicode == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|