mceditor: reimplement syntax name list using GPtrArray.

Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
This commit is contained in:
Andrew Borodin 2014-09-30 14:48:54 +04:00
parent 7f8135d071
commit 4dd577fbe0
4 changed files with 38 additions and 45 deletions

View File

@ -35,7 +35,7 @@
#include <config.h>
#include <stdlib.h>
#include <sys/types.h>
#include <string.h> /* strcmp() */
#include "lib/global.h"
#include "lib/widget.h" /* Listbox */
@ -67,19 +67,23 @@ pstrcmp (const void *p1, const void *p2)
/* --------------------------------------------------------------------------------------------- */
static int
exec_edit_syntax_dialog (const char **names, const char *current_syntax)
exec_edit_syntax_dialog (const GPtrArray * names, const char *current_syntax)
{
size_t i;
Listbox *syntaxlist;
Listbox *syntaxlist = create_listbox_window (LIST_LINES, MAX_ENTRY_LEN,
_("Choose syntax highlighting"), NULL);
syntaxlist = create_listbox_window (LIST_LINES, MAX_ENTRY_LEN,
_("Choose syntax highlighting"), NULL);
LISTBOX_APPEND_TEXT (syntaxlist, 'A', _("< Auto >"), NULL);
LISTBOX_APPEND_TEXT (syntaxlist, 'R', _("< Reload Current Syntax >"), NULL);
for (i = 0; names[i] != NULL; i++)
for (i = 0; i < names->len; i++)
{
LISTBOX_APPEND_TEXT (syntaxlist, 0, names[i], NULL);
if ((current_syntax != NULL) && (strcmp (names[i], current_syntax) == 0))
const char *name;
name = g_ptr_array_index (names, i);
LISTBOX_APPEND_TEXT (syntaxlist, 0, name, NULL);
if (current_syntax != NULL && strcmp (name, current_syntax) == 0)
listbox_select_entry (syntaxlist->list, i + N_DFLT_ENTRIES);
}
@ -93,27 +97,27 @@ exec_edit_syntax_dialog (const char **names, const char *current_syntax)
void
edit_syntax_dialog (WEdit * edit)
{
char *current_syntax;
int old_auto_syntax, syntax;
char **names;
gboolean force_reload = FALSE;
size_t count;
GPtrArray *names;
int syntax;
current_syntax = g_strdup (edit->syntax_type);
old_auto_syntax = option_auto_syntax;
names = g_new0 (char *, 1);
names = g_ptr_array_new ();
/* We fill the list of syntax files every time the editor is invoked.
Instead we could save the list to a file and update it once the syntax
file gets updated (either by testing or by explicit user command). */
edit_load_syntax (NULL, &names, NULL);
count = g_strv_length (names);
qsort (names, count, sizeof (char *), pstrcmp);
edit_load_syntax (NULL, names, NULL);
g_ptr_array_sort (names, pstrcmp);
syntax = exec_edit_syntax_dialog ((const char **) names, current_syntax);
syntax = exec_edit_syntax_dialog (names, edit->syntax_type);
if (syntax >= 0)
{
gboolean force_reload = FALSE;
char *current_syntax;
int old_auto_syntax;
current_syntax = g_strdup (edit->syntax_type);
old_auto_syntax = option_auto_syntax;
switch (syntax)
{
case 0: /* auto syntax */
@ -125,19 +129,20 @@ edit_syntax_dialog (WEdit * edit)
default:
option_auto_syntax = 0;
g_free (edit->syntax_type);
edit->syntax_type = g_strdup (names[syntax - N_DFLT_ENTRIES]);
edit->syntax_type = g_strdup (g_ptr_array_index (names, syntax - N_DFLT_ENTRIES));
}
/* Load or unload syntax rules if the option has changed */
if ((option_auto_syntax && !old_auto_syntax) || old_auto_syntax ||
(current_syntax && edit->syntax_type &&
(strcmp (current_syntax, edit->syntax_type) != 0)) || force_reload)
if (force_reload || (option_auto_syntax && !old_auto_syntax) || old_auto_syntax ||
(current_syntax != NULL && edit->syntax_type != NULL &&
strcmp (current_syntax, edit->syntax_type) != 0))
edit_load_syntax (edit, NULL, edit->syntax_type);
g_free (current_syntax);
}
g_strfreev (names);
g_ptr_array_foreach (names, (GFunc) g_free, NULL);
g_ptr_array_free (names, TRUE);
}
/* --------------------------------------------------------------------------------------------- */

View File

@ -264,7 +264,7 @@ void edit_paste_from_history (WEdit * edit);
void edit_set_filename (WEdit * edit, const vfs_path_t * name_vpath);
void edit_load_syntax (WEdit * edit, char ***pnames, const char *type);
void edit_load_syntax (WEdit * edit, GPtrArray * pnames, const char *type);
void edit_free_syntax_rules (WEdit * edit);
int edit_get_syntax_color (WEdit * edit, off_t byte_index);

View File

@ -1230,18 +1230,15 @@ edit_read_syntax_rules (WEdit * edit, FILE * f, char **args, int args_size)
/* returns -1 on file error, line number on error in file syntax */
static int
edit_read_syntax_file (WEdit * edit, char ***pnames, const char *syntax_file,
edit_read_syntax_file (WEdit * edit, GPtrArray * pnames, const char *syntax_file,
const char *editor_file, const char *first_line, const char *type)
{
#define NENTRIES 30
FILE *f, *g = NULL;
char *args[1024], *l = NULL;
long line = 0;
int result = 0;
int count = 0;
char *lib_file;
gboolean found = FALSE;
char **tmpnames = NULL;
f = fopen (syntax_file, "r");
if (f == NULL)
@ -1290,20 +1287,11 @@ edit_read_syntax_file (WEdit * edit, char ***pnames, const char *syntax_file,
result = line;
break;
}
if (pnames && *pnames)
if (pnames != NULL)
{
/* 1: just collecting a list of names of rule sets */
/* Reallocate the list if required */
if (count % NENTRIES == 0)
{
tmpnames =
(char **) g_try_realloc (*pnames, (count + NENTRIES + 1) * sizeof (char *));
if (tmpnames == NULL)
break;
*pnames = tmpnames;
}
(*pnames)[count++] = g_strdup (args[2]);
(*pnames)[count] = NULL;
g_ptr_array_add (pnames, g_strdup (args[2]));
}
else if (type)
{
@ -1463,7 +1451,7 @@ edit_free_syntax_rules (WEdit * edit)
* type must be edit->syntax_type or NULL
*/
void
edit_load_syntax (WEdit * edit, char ***pnames, const char *type)
edit_load_syntax (WEdit * edit, GPtrArray * pnames, const char *type)
{
int r;
char *f = NULL;
@ -1483,7 +1471,7 @@ edit_load_syntax (WEdit * edit, char ***pnames, const char *type)
if (!tty_use_colors ())
return;
if (!option_syntax_highlighting && (!pnames || !*pnames))
if (!option_syntax_highlighting && (pnames == NULL || pnames->len == 0))
return;
if (edit != NULL && edit->filename_vpath == NULL)

View File

@ -48,7 +48,7 @@ static WEdit *test_edit;
/* --------------------------------------------------------------------------------------------- */
/* @Mock */
void
edit_load_syntax (WEdit * _edit, char ***_pnames, const char *_type)
edit_load_syntax (WEdit * _edit, GPtrArray * _pnames, const char *_type)
{
(void) _edit;
(void) _pnames;