mirror of
https://github.com/MidnightCommander/mc
synced 2025-01-03 10:04:32 +03:00
mceditor: syntax: reimplement keyword list using GPtrArray.
Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
This commit is contained in:
parent
32cfbfaef1
commit
8307c12bd4
@ -95,9 +95,11 @@ int option_auto_syntax = 1;
|
|||||||
#define check_a {if(!*a){result=line;break;}}
|
#define check_a {if(!*a){result=line;break;}}
|
||||||
#define check_not_a {if(*a){result=line;break;}}
|
#define check_not_a {if(*a){result=line;break;}}
|
||||||
|
|
||||||
|
#define SYNTAX_KEYWORD(x) ((syntax_keyword_t *) (x))
|
||||||
|
|
||||||
/*** file scope type declarations ****************************************************************/
|
/*** file scope type declarations ****************************************************************/
|
||||||
|
|
||||||
struct key_word
|
typedef struct
|
||||||
{
|
{
|
||||||
char *keyword;
|
char *keyword;
|
||||||
unsigned char first;
|
unsigned char first;
|
||||||
@ -105,7 +107,7 @@ struct key_word
|
|||||||
char *whole_word_chars_right;
|
char *whole_word_chars_right;
|
||||||
long line_start;
|
long line_start;
|
||||||
int color;
|
int color;
|
||||||
};
|
} syntax_keyword_t;
|
||||||
|
|
||||||
struct context_rule
|
struct context_rule
|
||||||
{
|
{
|
||||||
@ -121,7 +123,7 @@ struct context_rule
|
|||||||
char *keyword_first_chars;
|
char *keyword_first_chars;
|
||||||
gboolean spelling;
|
gboolean spelling;
|
||||||
/* first word is word[1] */
|
/* first word is word[1] */
|
||||||
struct key_word **keyword;
|
GPtrArray *keyword;
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef struct
|
typedef struct
|
||||||
@ -134,9 +136,23 @@ typedef struct
|
|||||||
|
|
||||||
static char *error_file_name = NULL;
|
static char *error_file_name = NULL;
|
||||||
|
|
||||||
|
/* --------------------------------------------------------------------------------------------- */
|
||||||
/*** file scope functions ************************************************************************/
|
/*** file scope functions ************************************************************************/
|
||||||
/* --------------------------------------------------------------------------------------------- */
|
/* --------------------------------------------------------------------------------------------- */
|
||||||
|
|
||||||
|
static void
|
||||||
|
syntax_keyword_free (gpointer keyword)
|
||||||
|
{
|
||||||
|
syntax_keyword_t *k = SYNTAX_KEYWORD (keyword);
|
||||||
|
|
||||||
|
g_free (k->keyword);
|
||||||
|
g_free (k->whole_word_chars_left);
|
||||||
|
g_free (k->whole_word_chars_right);
|
||||||
|
g_free (k);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* --------------------------------------------------------------------------------------------- */
|
||||||
|
|
||||||
static gint
|
static gint
|
||||||
mc_defines_destroy (gpointer key, gpointer value, gpointer data)
|
mc_defines_destroy (gpointer key, gpointer value, gpointer data)
|
||||||
{
|
{
|
||||||
@ -407,12 +423,12 @@ apply_rules_going_right (WEdit * edit, off_t i)
|
|||||||
if (p != NULL)
|
if (p != NULL)
|
||||||
while (*(p = xx_strchr (edit, (unsigned char *) p + 1, c)) != '\0')
|
while (*(p = xx_strchr (edit, (unsigned char *) p + 1, c)) != '\0')
|
||||||
{
|
{
|
||||||
struct key_word *k;
|
syntax_keyword_t *k;
|
||||||
int count;
|
int count;
|
||||||
off_t e;
|
off_t e;
|
||||||
|
|
||||||
count = p - r->keyword_first_chars;
|
count = p - r->keyword_first_chars;
|
||||||
k = r->keyword[count];
|
k = SYNTAX_KEYWORD (g_ptr_array_index (r->keyword, count));
|
||||||
e = compare_word_to_right (edit, i, k->keyword, k->whole_word_chars_left,
|
e = compare_word_to_right (edit, i, k->keyword, k->whole_word_chars_left,
|
||||||
k->whole_word_chars_right, k->line_start);
|
k->whole_word_chars_right, k->line_start);
|
||||||
if (e > 0)
|
if (e > 0)
|
||||||
@ -509,12 +525,12 @@ apply_rules_going_right (WEdit * edit, off_t i)
|
|||||||
|
|
||||||
while (*(p = xx_strchr (edit, (unsigned char *) p + 1, c)) != '\0')
|
while (*(p = xx_strchr (edit, (unsigned char *) p + 1, c)) != '\0')
|
||||||
{
|
{
|
||||||
struct key_word *k;
|
syntax_keyword_t *k;
|
||||||
int count;
|
int count;
|
||||||
off_t e;
|
off_t e;
|
||||||
|
|
||||||
count = p - r->keyword_first_chars;
|
count = p - r->keyword_first_chars;
|
||||||
k = r->keyword[count];
|
k = SYNTAX_KEYWORD (g_ptr_array_index (r->keyword, count));
|
||||||
e = compare_word_to_right (edit, i, k->keyword, k->whole_word_chars_left,
|
e = compare_word_to_right (edit, i, k->keyword, k->whole_word_chars_left,
|
||||||
k->whole_word_chars_right, k->line_start);
|
k->whole_word_chars_right, k->line_start);
|
||||||
if (e > 0)
|
if (e > 0)
|
||||||
@ -594,7 +610,11 @@ edit_get_rule (WEdit * edit, off_t byte_index)
|
|||||||
static inline int
|
static inline int
|
||||||
translate_rule_to_color (const WEdit * edit, const edit_syntax_rule_t * rule)
|
translate_rule_to_color (const WEdit * edit, const edit_syntax_rule_t * rule)
|
||||||
{
|
{
|
||||||
return edit->rules[rule->context]->keyword[rule->keyword]->color;
|
syntax_keyword_t *k;
|
||||||
|
|
||||||
|
k = SYNTAX_KEYWORD (g_ptr_array_index (edit->rules[rule->context]->keyword, rule->keyword));
|
||||||
|
|
||||||
|
return k->color;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* --------------------------------------------------------------------------------------------- */
|
/* --------------------------------------------------------------------------------------------- */
|
||||||
@ -885,11 +905,10 @@ edit_read_syntax_rules (WEdit * edit, FILE * f, char **args, int args_size)
|
|||||||
char *l = 0;
|
char *l = 0;
|
||||||
int save_line = 0, line = 0;
|
int save_line = 0, line = 0;
|
||||||
struct context_rule **r, *c = NULL;
|
struct context_rule **r, *c = NULL;
|
||||||
int num_words = -1, num_contexts = -1;
|
gboolean no_words = TRUE;
|
||||||
|
int num_contexts = -1;
|
||||||
int result = 0;
|
int result = 0;
|
||||||
int alloc_contexts = MAX_CONTEXTS,
|
int alloc_contexts = MAX_CONTEXTS;
|
||||||
alloc_words_per_context = MAX_WORDS_PER_CONTEXT,
|
|
||||||
max_alloc_words_per_context = MAX_WORDS_PER_CONTEXT;
|
|
||||||
|
|
||||||
args[0] = NULL;
|
args[0] = NULL;
|
||||||
edit->is_case_insensitive = FALSE;
|
edit->is_case_insensitive = FALSE;
|
||||||
@ -982,6 +1001,8 @@ edit_read_syntax_rules (WEdit * edit, FILE * f, char **args, int args_size)
|
|||||||
}
|
}
|
||||||
else if (strcmp (args[0], "context") == 0)
|
else if (strcmp (args[0], "context") == 0)
|
||||||
{
|
{
|
||||||
|
syntax_keyword_t *k;
|
||||||
|
|
||||||
check_a;
|
check_a;
|
||||||
if (num_contexts == -1)
|
if (num_contexts == -1)
|
||||||
{
|
{
|
||||||
@ -998,7 +1019,6 @@ edit_read_syntax_rules (WEdit * edit, FILE * f, char **args, int args_size)
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
/* Terminate previous context. */
|
/* Terminate previous context. */
|
||||||
r[num_contexts - 1]->keyword[num_words] = NULL;
|
|
||||||
c = r[num_contexts] = g_malloc0 (sizeof (struct context_rule));
|
c = r[num_contexts] = g_malloc0 (sizeof (struct context_rule));
|
||||||
if (strcmp (*a, "exclusive") == 0)
|
if (strcmp (*a, "exclusive") == 0)
|
||||||
{
|
{
|
||||||
@ -1041,9 +1061,10 @@ edit_read_syntax_rules (WEdit * edit, FILE * f, char **args, int args_size)
|
|||||||
c->first_left = *c->left;
|
c->first_left = *c->left;
|
||||||
c->first_right = *c->right;
|
c->first_right = *c->right;
|
||||||
}
|
}
|
||||||
c->keyword = g_malloc (alloc_words_per_context * sizeof (struct key_word *));
|
c->keyword = g_ptr_array_new ();
|
||||||
num_words = 1;
|
k = g_new0 (syntax_keyword_t, 1);
|
||||||
c->keyword[0] = g_malloc0 (sizeof (struct key_word));
|
g_ptr_array_add (c->keyword, k);
|
||||||
|
no_words = FALSE;
|
||||||
subst_defines (edit->defines, a, &args[1024]);
|
subst_defines (edit->defines, a, &args[1024]);
|
||||||
fg = *a;
|
fg = *a;
|
||||||
if (*a != NULL)
|
if (*a != NULL)
|
||||||
@ -1057,11 +1078,10 @@ edit_read_syntax_rules (WEdit * edit, FILE * f, char **args, int args_size)
|
|||||||
g_strlcpy (last_fg, fg != NULL ? fg : "", sizeof (last_fg));
|
g_strlcpy (last_fg, fg != NULL ? fg : "", sizeof (last_fg));
|
||||||
g_strlcpy (last_bg, bg != NULL ? bg : "", sizeof (last_bg));
|
g_strlcpy (last_bg, bg != NULL ? bg : "", sizeof (last_bg));
|
||||||
g_strlcpy (last_attrs, attrs != NULL ? attrs : "", sizeof (last_attrs));
|
g_strlcpy (last_attrs, attrs != NULL ? attrs : "", sizeof (last_attrs));
|
||||||
c->keyword[0]->color = this_try_alloc_color_pair (fg, bg, attrs);
|
k->color = this_try_alloc_color_pair (fg, bg, attrs);
|
||||||
c->keyword[0]->keyword = g_strdup (" ");
|
k->keyword = g_strdup (" ");
|
||||||
check_not_a;
|
check_not_a;
|
||||||
|
|
||||||
alloc_words_per_context = MAX_WORDS_PER_CONTEXT;
|
|
||||||
if (++num_contexts >= alloc_contexts)
|
if (++num_contexts >= alloc_contexts)
|
||||||
{
|
{
|
||||||
struct context_rule **tmp;
|
struct context_rule **tmp;
|
||||||
@ -1082,12 +1102,13 @@ edit_read_syntax_rules (WEdit * edit, FILE * f, char **args, int args_size)
|
|||||||
}
|
}
|
||||||
else if (strcmp (args[0], "keyword") == 0)
|
else if (strcmp (args[0], "keyword") == 0)
|
||||||
{
|
{
|
||||||
struct key_word *k;
|
syntax_keyword_t *k;
|
||||||
|
|
||||||
if (num_words == -1)
|
if (no_words)
|
||||||
break_a;
|
break_a;
|
||||||
check_a;
|
check_a;
|
||||||
k = r[num_contexts - 1]->keyword[num_words] = g_malloc0 (sizeof (struct key_word));
|
k = g_new0 (syntax_keyword_t, 1);
|
||||||
|
g_ptr_array_add (r[num_contexts - 1]->keyword, k);
|
||||||
if (strcmp (*a, "whole") == 0)
|
if (strcmp (*a, "whole") == 0)
|
||||||
{
|
{
|
||||||
a++;
|
a++;
|
||||||
@ -1135,19 +1156,6 @@ edit_read_syntax_rules (WEdit * edit, FILE * f, char **args, int args_size)
|
|||||||
attrs = last_attrs;
|
attrs = last_attrs;
|
||||||
k->color = this_try_alloc_color_pair (fg, bg, attrs);
|
k->color = this_try_alloc_color_pair (fg, bg, attrs);
|
||||||
check_not_a;
|
check_not_a;
|
||||||
|
|
||||||
if (++num_words >= alloc_words_per_context)
|
|
||||||
{
|
|
||||||
struct key_word **tmp;
|
|
||||||
|
|
||||||
alloc_words_per_context += 1024;
|
|
||||||
|
|
||||||
if (alloc_words_per_context > max_alloc_words_per_context)
|
|
||||||
max_alloc_words_per_context = alloc_words_per_context;
|
|
||||||
|
|
||||||
tmp = g_realloc (c->keyword, alloc_words_per_context * sizeof (struct key_word *));
|
|
||||||
c->keyword = tmp;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else if (*(args[0]) == '#')
|
else if (*(args[0]) == '#')
|
||||||
{
|
{
|
||||||
@ -1188,10 +1196,7 @@ edit_read_syntax_rules (WEdit * edit, FILE * f, char **args, int args_size)
|
|||||||
|
|
||||||
/* Terminate context array. */
|
/* Terminate context array. */
|
||||||
if (num_contexts > 0)
|
if (num_contexts > 0)
|
||||||
{
|
|
||||||
r[num_contexts - 1]->keyword[num_words] = NULL;
|
|
||||||
r[num_contexts] = NULL;
|
r[num_contexts] = NULL;
|
||||||
}
|
|
||||||
|
|
||||||
if (edit->rules[0] == NULL)
|
if (edit->rules[0] == NULL)
|
||||||
MC_PTR_FREE (edit->rules);
|
MC_PTR_FREE (edit->rules);
|
||||||
@ -1204,18 +1209,23 @@ edit_read_syntax_rules (WEdit * edit, FILE * f, char **args, int args_size)
|
|||||||
if (num_contexts == -1)
|
if (num_contexts == -1)
|
||||||
return line;
|
return line;
|
||||||
|
|
||||||
first_chars = g_string_sized_new (max_alloc_words_per_context + 2);
|
first_chars = g_string_sized_new (32);
|
||||||
|
|
||||||
for (i = 0; edit->rules[i] != NULL; i++)
|
for (i = 0; edit->rules[i] != NULL; i++)
|
||||||
{
|
{
|
||||||
int j;
|
size_t j;
|
||||||
|
|
||||||
g_string_set_size (first_chars, 0);
|
g_string_set_size (first_chars, 0);
|
||||||
c = edit->rules[i];
|
c = edit->rules[i];
|
||||||
|
|
||||||
g_string_append_c (first_chars, (char) 1);
|
g_string_append_c (first_chars, (char) 1);
|
||||||
for (j = 1; c->keyword[j] != NULL; j++)
|
for (j = 1; j < c->keyword->len; j++)
|
||||||
g_string_append_c (first_chars, c->keyword[j]->first);
|
{
|
||||||
|
syntax_keyword_t *k;
|
||||||
|
|
||||||
|
k = SYNTAX_KEYWORD (g_ptr_array_index (c->keyword, j));
|
||||||
|
g_string_append_c (first_chars, k->first);
|
||||||
|
}
|
||||||
|
|
||||||
c->keyword_first_chars = g_strndup (first_chars->str, first_chars->len);
|
c->keyword_first_chars = g_strndup (first_chars->str, first_chars->len);
|
||||||
}
|
}
|
||||||
@ -1330,8 +1340,8 @@ edit_read_syntax_file (WEdit * edit, GPtrArray * pnames, const char *syntax_file
|
|||||||
g_free (edit->syntax_type);
|
g_free (edit->syntax_type);
|
||||||
edit->syntax_type = g_strdup (syntax_type);
|
edit->syntax_type = g_strdup (syntax_type);
|
||||||
/* if there are no rules then turn off syntax highlighting for speed */
|
/* if there are no rules then turn off syntax highlighting for speed */
|
||||||
if (!g && !edit->rules[1])
|
if (g == NULL && edit->rules[1] == NULL)
|
||||||
if (!edit->rules[0]->keyword[1] && !edit->rules[0]->spelling)
|
if (edit->rules[0]->keyword->len == 1 && !edit->rules[0]->spelling)
|
||||||
{
|
{
|
||||||
edit_free_syntax_rules (edit);
|
edit_free_syntax_rules (edit);
|
||||||
break;
|
break;
|
||||||
@ -1404,7 +1414,7 @@ edit_get_syntax_color (WEdit * edit, off_t byte_index)
|
|||||||
void
|
void
|
||||||
edit_free_syntax_rules (WEdit * edit)
|
edit_free_syntax_rules (WEdit * edit)
|
||||||
{
|
{
|
||||||
size_t i, j;
|
size_t i;
|
||||||
|
|
||||||
if (!edit)
|
if (!edit)
|
||||||
return;
|
return;
|
||||||
@ -1418,21 +1428,16 @@ edit_free_syntax_rules (WEdit * edit)
|
|||||||
|
|
||||||
for (i = 0; edit->rules[i]; i++)
|
for (i = 0; edit->rules[i]; i++)
|
||||||
{
|
{
|
||||||
if (edit->rules[i]->keyword)
|
if (edit->rules[i]->keyword != NULL)
|
||||||
{
|
{
|
||||||
for (j = 0; edit->rules[i]->keyword[j]; j++)
|
g_ptr_array_foreach (edit->rules[i]->keyword, (GFunc) syntax_keyword_free, NULL);
|
||||||
{
|
g_ptr_array_free (edit->rules[i]->keyword, TRUE);
|
||||||
MC_PTR_FREE (edit->rules[i]->keyword[j]->keyword);
|
|
||||||
MC_PTR_FREE (edit->rules[i]->keyword[j]->whole_word_chars_left);
|
|
||||||
MC_PTR_FREE (edit->rules[i]->keyword[j]->whole_word_chars_right);
|
|
||||||
MC_PTR_FREE (edit->rules[i]->keyword[j]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
MC_PTR_FREE (edit->rules[i]->left);
|
MC_PTR_FREE (edit->rules[i]->left);
|
||||||
MC_PTR_FREE (edit->rules[i]->right);
|
MC_PTR_FREE (edit->rules[i]->right);
|
||||||
MC_PTR_FREE (edit->rules[i]->whole_word_chars_left);
|
MC_PTR_FREE (edit->rules[i]->whole_word_chars_left);
|
||||||
MC_PTR_FREE (edit->rules[i]->whole_word_chars_right);
|
MC_PTR_FREE (edit->rules[i]->whole_word_chars_right);
|
||||||
MC_PTR_FREE (edit->rules[i]->keyword);
|
|
||||||
MC_PTR_FREE (edit->rules[i]->keyword_first_chars);
|
MC_PTR_FREE (edit->rules[i]->keyword_first_chars);
|
||||||
MC_PTR_FREE (edit->rules[i]);
|
MC_PTR_FREE (edit->rules[i]);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user