2016-06-02 17:19:08 -07:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "elementary_config.h"
|
2014-10-26 15:47:27 -07:00
|
|
|
#endif
|
|
|
|
|
2016-06-02 17:19:08 -07:00
|
|
|
#include "Elementary.h"
|
2014-10-26 15:47:27 -07:00
|
|
|
|
|
|
|
#include "elm_code_private.h"
|
|
|
|
|
2015-02-21 10:24:17 -08:00
|
|
|
static Elm_Code_Line *_elm_code_file_line_blank_create(Elm_Code_File *file, int line, void *data)
|
2014-10-26 15:47:27 -07:00
|
|
|
{
|
|
|
|
Elm_Code_Line *ecl;
|
|
|
|
|
|
|
|
ecl = calloc(1, sizeof(Elm_Code_Line));
|
|
|
|
if (!ecl) return NULL;
|
|
|
|
|
2015-02-21 10:24:17 -08:00
|
|
|
ecl->file = file;
|
2014-10-26 15:47:27 -07:00
|
|
|
ecl->number = line;
|
2014-11-04 16:01:28 -08:00
|
|
|
ecl->status = ELM_CODE_STATUS_TYPE_DEFAULT;
|
2015-02-14 15:14:06 -08:00
|
|
|
ecl->data = data;
|
|
|
|
|
2014-10-26 15:47:27 -07:00
|
|
|
return ecl;
|
|
|
|
}
|
|
|
|
|
2015-03-15 16:07:31 -07:00
|
|
|
static Elm_Code_File_Line_Ending _elm_code_line_ending_get(const char *ending)
|
|
|
|
{
|
|
|
|
switch (*ending)
|
|
|
|
{
|
|
|
|
case '\r':
|
|
|
|
return ELM_CODE_FILE_LINE_ENDING_WINDOWS;
|
|
|
|
default:
|
|
|
|
return ELM_CODE_FILE_LINE_ENDING_UNIX;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-07 11:48:02 -08:00
|
|
|
static void _elm_code_file_line_insert_data(Elm_Code_File *file, const char *content, unsigned int length,
|
2015-02-28 06:20:32 -08:00
|
|
|
unsigned int row, Eina_Bool mapped, void *data)
|
2014-11-04 13:05:29 -08:00
|
|
|
{
|
2015-02-28 06:20:32 -08:00
|
|
|
Elm_Code_Line *line, *after;
|
2014-11-04 13:05:29 -08:00
|
|
|
|
2015-02-21 10:24:17 -08:00
|
|
|
line = _elm_code_file_line_blank_create(file, row, data);
|
2014-11-04 13:05:29 -08:00
|
|
|
if (!line) return;
|
|
|
|
|
2014-11-19 14:39:00 -08:00
|
|
|
if (mapped)
|
|
|
|
{
|
|
|
|
line->content = content;
|
|
|
|
line->length = length;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
line->modified = malloc(sizeof(char)*(length+1));
|
|
|
|
strncpy(line->modified, content, length);
|
|
|
|
line->modified[length] = 0;
|
|
|
|
line->length = length;
|
|
|
|
}
|
2014-11-04 13:05:29 -08:00
|
|
|
|
2015-02-28 06:20:32 -08:00
|
|
|
if (row == 1)
|
|
|
|
file->lines = eina_list_prepend(file->lines, line);
|
|
|
|
else if (row == eina_list_count(file->lines) + 1)
|
|
|
|
file->lines = eina_list_append(file->lines, line);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
after = eina_list_nth(file->lines, row - 2);
|
|
|
|
file->lines = eina_list_append_relative(file->lines, line, after);
|
|
|
|
}
|
2014-11-06 15:43:56 -08:00
|
|
|
|
|
|
|
if (file->parent)
|
2014-11-16 15:52:41 -08:00
|
|
|
{
|
2015-02-26 05:54:46 -08:00
|
|
|
_elm_code_parse_line(file->parent, line);
|
2015-02-14 15:14:06 -08:00
|
|
|
elm_code_callback_fire(file->parent, &ELM_CODE_EVENT_LINE_LOAD_DONE, line);
|
2014-11-16 15:52:41 -08:00
|
|
|
}
|
2014-11-04 13:05:29 -08:00
|
|
|
}
|
|
|
|
|
2015-03-21 12:25:25 -07:00
|
|
|
EAPI const char *elm_code_file_filename_get(Elm_Code_File *file)
|
|
|
|
{
|
2016-12-27 13:12:49 -08:00
|
|
|
if (!file->file)
|
|
|
|
return NULL;
|
|
|
|
|
2015-03-21 12:25:25 -07:00
|
|
|
return basename((char *)eina_file_filename_get(file->file));
|
|
|
|
}
|
|
|
|
|
|
|
|
EAPI const char *elm_code_file_path_get(Elm_Code_File *file)
|
|
|
|
{
|
2016-12-27 13:12:49 -08:00
|
|
|
if (!file->file)
|
|
|
|
return NULL;
|
|
|
|
|
2015-03-21 12:25:25 -07:00
|
|
|
return eina_file_filename_get(file->file);
|
|
|
|
}
|
|
|
|
|
|
|
|
EAPI char *_elm_code_file_tmp_path_get(Elm_Code_File *file)
|
|
|
|
{
|
|
|
|
const char *name, *path;
|
|
|
|
char *tmp;
|
|
|
|
size_t dirlen;
|
|
|
|
|
|
|
|
path = elm_code_file_path_get(file);
|
|
|
|
name = elm_code_file_filename_get(file);
|
|
|
|
dirlen = strlen(path) - strlen(name);
|
|
|
|
|
|
|
|
tmp = malloc(sizeof(char) * (strlen(path) + 6));
|
|
|
|
snprintf(tmp, dirlen + 1, "%s", path);
|
|
|
|
snprintf(tmp + dirlen, strlen(name) + 6, ".%s.tmp", name);
|
|
|
|
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
2014-11-16 15:52:41 -08:00
|
|
|
EAPI Elm_Code_File *elm_code_file_new(Elm_Code *code)
|
2014-11-04 13:05:29 -08:00
|
|
|
{
|
|
|
|
Elm_Code_File *ret;
|
|
|
|
|
2014-11-28 00:55:42 -08:00
|
|
|
if (code->file)
|
|
|
|
elm_code_file_free(code->file);
|
|
|
|
|
2014-11-04 13:05:29 -08:00
|
|
|
ret = calloc(1, sizeof(Elm_Code_File));
|
2014-11-16 15:52:41 -08:00
|
|
|
code->file = ret;
|
|
|
|
ret->parent = code;
|
2014-11-04 13:05:29 -08:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-11-16 15:52:41 -08:00
|
|
|
EAPI Elm_Code_File *elm_code_file_open(Elm_Code *code, const char *path)
|
2014-10-26 15:47:27 -07:00
|
|
|
{
|
|
|
|
Elm_Code_File *ret;
|
|
|
|
Eina_File *file;
|
|
|
|
Eina_File_Line *line;
|
|
|
|
Eina_Iterator *it;
|
|
|
|
unsigned int lastindex;
|
|
|
|
|
2014-11-16 15:52:41 -08:00
|
|
|
ret = elm_code_file_new(code);
|
2014-10-26 15:47:27 -07:00
|
|
|
file = eina_file_open(path, EINA_FALSE);
|
|
|
|
ret->file = file;
|
|
|
|
lastindex = 1;
|
|
|
|
|
2014-11-19 14:39:00 -08:00
|
|
|
ret->map = eina_file_map_all(file, EINA_FILE_POPULATE);
|
2014-10-26 15:47:27 -07:00
|
|
|
it = eina_file_map_lines(file);
|
|
|
|
EINA_ITERATOR_FOREACH(it, line)
|
|
|
|
{
|
|
|
|
Elm_Code_Line *ecl;
|
|
|
|
|
2015-03-15 16:07:31 -07:00
|
|
|
if (lastindex == 1)
|
|
|
|
ret->line_ending = _elm_code_line_ending_get(line->start + line->length);
|
|
|
|
|
2014-10-26 15:47:27 -07:00
|
|
|
/* Working around the issue that eina_file_map_lines does not trigger an item for empty lines */
|
2015-03-27 16:45:23 -07:00
|
|
|
/* This was fixed in 1.13.99 so once we depend on 1.14 minimum this can go */
|
2014-10-26 15:47:27 -07:00
|
|
|
while (lastindex < line->index - 1)
|
|
|
|
{
|
2015-02-21 10:24:17 -08:00
|
|
|
ecl = _elm_code_file_line_blank_create(ret, ++lastindex, NULL);
|
2014-10-26 15:47:27 -07:00
|
|
|
if (!ecl) continue;
|
|
|
|
|
|
|
|
ret->lines = eina_list_append(ret->lines, ecl);
|
|
|
|
}
|
|
|
|
|
2015-02-28 06:20:32 -08:00
|
|
|
_elm_code_file_line_insert_data(ret, line->start, line->length, lastindex = line->index, EINA_TRUE, NULL);
|
2014-10-26 15:47:27 -07:00
|
|
|
}
|
|
|
|
eina_iterator_free(it);
|
|
|
|
|
2014-11-12 14:59:05 -08:00
|
|
|
if (ret->parent)
|
2014-11-16 15:52:41 -08:00
|
|
|
{
|
2015-02-26 05:54:46 -08:00
|
|
|
_elm_code_parse_file(ret->parent, ret);
|
2014-11-16 15:52:41 -08:00
|
|
|
elm_code_callback_fire(ret->parent, &ELM_CODE_EVENT_FILE_LOAD_DONE, ret);
|
|
|
|
}
|
2014-10-26 15:47:27 -07:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-03-21 12:25:25 -07:00
|
|
|
EAPI void elm_code_file_save(Elm_Code_File *file)
|
|
|
|
{
|
|
|
|
Eina_List *item;
|
2015-08-29 06:46:18 -07:00
|
|
|
Elm_Code *code;
|
2015-03-21 12:25:25 -07:00
|
|
|
Elm_Code_Line *line_item;
|
|
|
|
const char *path, *content, *crchars;
|
|
|
|
char *tmp;
|
|
|
|
unsigned int length;
|
|
|
|
short crlength;
|
|
|
|
FILE *out;
|
|
|
|
|
2015-08-29 06:46:18 -07:00
|
|
|
code = file->parent;
|
2015-03-21 12:25:25 -07:00
|
|
|
path = elm_code_file_path_get(file);
|
|
|
|
tmp = _elm_code_file_tmp_path_get(file);
|
|
|
|
crchars = elm_code_file_line_ending_chars_get(file, &crlength);
|
|
|
|
|
|
|
|
out = fopen(tmp, "w");
|
|
|
|
if (out == NULL)
|
|
|
|
{
|
|
|
|
free(tmp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
EINA_LIST_FOREACH(file->lines, item, line_item)
|
|
|
|
{
|
2015-08-29 06:46:18 -07:00
|
|
|
if (code && code->config.trim_whitespace &&
|
|
|
|
!elm_code_line_contains_widget_cursor(line_item))
|
2015-08-29 06:11:40 -07:00
|
|
|
elm_code_line_text_trailing_whitespace_strip(line_item);
|
2015-03-21 12:25:25 -07:00
|
|
|
content = elm_code_line_text_get(line_item, &length);
|
2015-08-29 04:05:56 -07:00
|
|
|
|
2015-03-21 12:25:25 -07:00
|
|
|
fwrite(content, sizeof(char), length, out);
|
|
|
|
fwrite(crchars, sizeof(char), crlength, out);
|
|
|
|
}
|
|
|
|
fclose(out);
|
|
|
|
|
|
|
|
ecore_file_mv(tmp, path);
|
|
|
|
free(tmp);
|
2015-04-19 17:21:03 -07:00
|
|
|
|
|
|
|
if (file->parent)
|
|
|
|
{
|
|
|
|
_elm_code_parse_reset_file(file->parent, file);
|
|
|
|
_elm_code_parse_file(file->parent, file);
|
|
|
|
elm_code_callback_fire(file->parent, &ELM_CODE_EVENT_FILE_LOAD_DONE, file);
|
|
|
|
}
|
2015-03-21 12:25:25 -07:00
|
|
|
}
|
|
|
|
|
2014-11-04 13:05:29 -08:00
|
|
|
EAPI void elm_code_file_free(Elm_Code_File *file)
|
2014-10-26 15:47:27 -07:00
|
|
|
{
|
|
|
|
Elm_Code_Line *l;
|
|
|
|
|
|
|
|
EINA_LIST_FREE(file->lines, l)
|
|
|
|
{
|
2015-03-08 03:15:31 -07:00
|
|
|
elm_code_line_free(l);
|
2014-10-26 15:47:27 -07:00
|
|
|
}
|
|
|
|
|
2016-11-11 07:00:46 -08:00
|
|
|
elm_code_file_close(file);
|
2014-10-26 15:47:27 -07:00
|
|
|
free(file);
|
|
|
|
}
|
|
|
|
|
2014-11-04 13:05:29 -08:00
|
|
|
EAPI void elm_code_file_close(Elm_Code_File *file)
|
|
|
|
{
|
2016-11-11 07:00:46 -08:00
|
|
|
if (!file->file)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (file->map)
|
|
|
|
eina_file_map_free(file->file, file->map);
|
|
|
|
|
2014-11-04 13:05:29 -08:00
|
|
|
eina_file_close(file->file);
|
2016-11-11 07:00:46 -08:00
|
|
|
file->file = NULL;
|
2014-11-04 13:05:29 -08:00
|
|
|
}
|
|
|
|
|
2015-03-15 16:07:31 -07:00
|
|
|
EAPI Elm_Code_File_Line_Ending elm_code_file_line_ending_get(Elm_Code_File *file)
|
|
|
|
{
|
|
|
|
return file->line_ending;
|
|
|
|
}
|
|
|
|
|
2015-03-21 06:58:30 -07:00
|
|
|
EAPI const char *elm_code_file_line_ending_chars_get(Elm_Code_File *file, short *length)
|
|
|
|
{
|
|
|
|
if (length)
|
|
|
|
{
|
|
|
|
if (elm_code_file_line_ending_get(file) == ELM_CODE_FILE_LINE_ENDING_WINDOWS)
|
|
|
|
*length = 2;
|
|
|
|
else
|
|
|
|
*length = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (elm_code_file_line_ending_get(file) == ELM_CODE_FILE_LINE_ENDING_WINDOWS)
|
|
|
|
return "\r\n";
|
|
|
|
return "\n";
|
|
|
|
}
|
|
|
|
|
2014-11-18 13:11:10 -08:00
|
|
|
EAPI void elm_code_file_clear(Elm_Code_File *file)
|
|
|
|
{
|
|
|
|
Elm_Code_Line *l;
|
|
|
|
|
|
|
|
EINA_LIST_FREE(file->lines, l)
|
|
|
|
{
|
2015-03-08 03:15:31 -07:00
|
|
|
elm_code_line_free(l);
|
2014-11-18 13:11:10 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (file->parent)
|
|
|
|
elm_code_callback_fire(file->parent, &ELM_CODE_EVENT_FILE_LOAD_DONE, file);
|
|
|
|
}
|
|
|
|
|
2014-10-26 15:47:27 -07:00
|
|
|
EAPI unsigned int elm_code_file_lines_get(Elm_Code_File *file)
|
|
|
|
{
|
|
|
|
return eina_list_count(file->lines);
|
|
|
|
}
|
|
|
|
|
2014-11-04 13:05:29 -08:00
|
|
|
|
2015-02-14 15:14:06 -08:00
|
|
|
EAPI void elm_code_file_line_append(Elm_Code_File *file, const char *line, int length, void *data)
|
2014-11-04 13:05:29 -08:00
|
|
|
{
|
|
|
|
int row;
|
|
|
|
|
2015-02-28 06:20:32 -08:00
|
|
|
row = elm_code_file_lines_get(file) + 1;
|
|
|
|
_elm_code_file_line_insert_data(file, line, length, row, EINA_FALSE, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
EAPI void elm_code_file_line_insert(Elm_Code_File *file, unsigned int row, const char *line, int length, void *data)
|
|
|
|
{
|
|
|
|
Eina_List *item;
|
|
|
|
Elm_Code_Line *line_item;
|
|
|
|
unsigned int r;
|
|
|
|
|
|
|
|
_elm_code_file_line_insert_data(file, line, length, row, EINA_FALSE, data);
|
|
|
|
|
|
|
|
r = row;
|
|
|
|
EINA_LIST_FOREACH(file->lines, item, line_item)
|
|
|
|
{
|
|
|
|
if (line_item->number < row)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
line_item->number = r++;
|
|
|
|
}
|
2014-11-04 13:05:29 -08:00
|
|
|
}
|
|
|
|
|
2015-03-08 03:33:07 -07:00
|
|
|
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 (tofree)
|
|
|
|
elm_code_line_free(tofree);
|
|
|
|
}
|
|
|
|
|
2014-11-04 16:01:28 -08:00
|
|
|
EAPI Elm_Code_Line *elm_code_file_line_get(Elm_Code_File *file, unsigned int number)
|
|
|
|
{
|
|
|
|
return eina_list_nth(file->lines, number - 1);
|
|
|
|
}
|