2006-04-29 14:38:28 -05:00
|
|
|
/*
|
2005-10-13 14:08:18 +00:00
|
|
|
* moolang.c
|
|
|
|
*
|
2006-02-23 14:03:17 +00:00
|
|
|
* Copyright (C) 2004-2006 by Yevgen Muntyan <muntyan@math.tamu.edu>
|
2005-10-13 14:08:18 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* See COPYING file that comes with this distribution.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define MOOEDIT_COMPILATION
|
2006-08-26 04:46:29 -05:00
|
|
|
#include "config.h"
|
|
|
|
#include "mooedit/moolang-private.h"
|
|
|
|
#include "mooutils/mooi18n.h"
|
|
|
|
#include "gtksourceview/gtksourcelanguage-private.h"
|
|
|
|
#include <libxml/xmlreader.h>
|
|
|
|
#include <glib/gstdio.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
2005-10-13 14:08:18 +00:00
|
|
|
|
|
|
|
|
2006-08-26 04:46:29 -05:00
|
|
|
struct _MooLangPrivate {
|
|
|
|
char *version; /* not NULL; "" by default */
|
|
|
|
char *author; /* not NULL; "" by default */
|
2005-10-13 14:08:18 +00:00
|
|
|
|
2006-08-26 04:46:29 -05:00
|
|
|
char *brackets;
|
|
|
|
char *line_comment;
|
|
|
|
char *block_comment_start;
|
|
|
|
char *block_comment_end;
|
2005-10-13 14:08:18 +00:00
|
|
|
|
2006-08-26 04:46:29 -05:00
|
|
|
guint hidden : 1;
|
|
|
|
};
|
2005-10-13 14:08:18 +00:00
|
|
|
|
2006-08-26 04:46:29 -05:00
|
|
|
|
|
|
|
G_DEFINE_TYPE (MooLang, moo_lang, GTK_TYPE_SOURCE_LANGUAGE)
|
2005-10-13 14:08:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
static void
|
2006-08-26 04:46:29 -05:00
|
|
|
moo_lang_init (MooLang *lang)
|
2005-10-13 14:08:18 +00:00
|
|
|
{
|
2006-08-26 04:46:29 -05:00
|
|
|
lang->priv = G_TYPE_INSTANCE_GET_PRIVATE (lang, MOO_TYPE_LANG, MooLangPrivate);
|
|
|
|
lang->priv->version = g_strdup ("");
|
|
|
|
lang->priv->author = g_strdup ("");
|
2005-10-13 14:08:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-08-26 04:46:29 -05:00
|
|
|
static void
|
|
|
|
moo_lang_finalize (GObject *object)
|
2005-10-13 14:08:18 +00:00
|
|
|
{
|
2006-08-26 04:46:29 -05:00
|
|
|
MooLang *lang = MOO_LANG (object);
|
2005-10-13 14:08:18 +00:00
|
|
|
|
2006-08-26 04:46:29 -05:00
|
|
|
g_free (lang->priv->version);
|
|
|
|
g_free (lang->priv->author);
|
|
|
|
g_free (lang->priv->brackets);
|
|
|
|
g_free (lang->priv->line_comment);
|
|
|
|
g_free (lang->priv->block_comment_start);
|
|
|
|
g_free (lang->priv->block_comment_end);
|
2005-10-13 14:08:18 +00:00
|
|
|
|
2006-08-26 04:46:29 -05:00
|
|
|
G_OBJECT_CLASS (moo_lang_parent_class)->finalize (object);
|
2005-10-13 14:08:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-08-26 04:46:29 -05:00
|
|
|
static void
|
|
|
|
moo_lang_class_init (MooLangClass *klass)
|
2005-10-13 14:08:18 +00:00
|
|
|
{
|
2006-08-26 04:46:29 -05:00
|
|
|
G_OBJECT_CLASS (klass)->finalize = moo_lang_finalize;
|
|
|
|
g_type_class_add_private (klass, sizeof (MooLangPrivate));
|
2005-10-13 14:08:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-08-26 04:46:29 -05:00
|
|
|
GtkSourceEngine *
|
|
|
|
_moo_lang_get_engine (MooLang *lang)
|
2005-10-13 14:08:18 +00:00
|
|
|
{
|
2006-08-26 04:46:29 -05:00
|
|
|
g_return_val_if_fail (MOO_IS_LANG (lang), NULL);
|
|
|
|
return _gtk_source_language_create_engine (GTK_SOURCE_LANGUAGE (lang));
|
2005-10-13 14:08:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-08-26 04:46:29 -05:00
|
|
|
MooLangMgr *
|
|
|
|
_moo_lang_get_mgr (MooLang *lang)
|
2005-10-17 05:23:40 +00:00
|
|
|
{
|
2006-08-26 04:46:29 -05:00
|
|
|
g_return_val_if_fail (MOO_IS_LANG (lang), NULL);
|
|
|
|
return MOO_LANG_MGR (_gtk_source_language_get_languages_manager (GTK_SOURCE_LANGUAGE (lang)));
|
2005-10-17 05:23:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-08-26 04:46:29 -05:00
|
|
|
void
|
|
|
|
_moo_lang_parse_file (MooLang *lang)
|
2005-10-17 05:23:40 +00:00
|
|
|
{
|
2006-08-26 04:46:29 -05:00
|
|
|
GtkSourceLanguage *slang;
|
|
|
|
xmlTextReaderPtr reader;
|
|
|
|
int ret;
|
|
|
|
int fd;
|
2005-10-17 05:23:40 +00:00
|
|
|
|
2006-08-26 04:46:29 -05:00
|
|
|
g_return_if_fail (MOO_IS_LANG (lang));
|
2005-10-17 05:23:40 +00:00
|
|
|
|
2006-08-26 04:46:29 -05:00
|
|
|
slang = GTK_SOURCE_LANGUAGE (lang);
|
|
|
|
g_return_if_fail (slang->priv->lang_file_name != NULL);
|
2005-10-17 05:23:40 +00:00
|
|
|
|
2006-08-26 04:46:29 -05:00
|
|
|
fd = g_open (slang->priv->lang_file_name, O_RDONLY, 0);
|
|
|
|
g_return_if_fail (fd != -1);
|
2005-10-17 05:23:40 +00:00
|
|
|
|
2006-08-26 04:46:29 -05:00
|
|
|
reader = xmlReaderForFd (fd, slang->priv->lang_file_name, NULL, 0);
|
2005-10-17 05:23:40 +00:00
|
|
|
|
2006-08-26 04:46:29 -05:00
|
|
|
if (!reader)
|
2005-10-17 05:23:40 +00:00
|
|
|
{
|
2006-08-26 04:46:29 -05:00
|
|
|
g_critical ("%s: oops", G_STRLOC);
|
|
|
|
close (fd);
|
|
|
|
return;
|
2005-10-17 05:23:40 +00:00
|
|
|
}
|
|
|
|
|
2006-08-26 04:46:29 -05:00
|
|
|
ret = xmlTextReaderRead (reader);
|
2005-10-13 14:08:18 +00:00
|
|
|
|
2006-08-26 04:46:29 -05:00
|
|
|
while (ret == 1)
|
|
|
|
{
|
|
|
|
if (xmlTextReaderNodeType (reader) == XML_READER_TYPE_ELEMENT)
|
|
|
|
{
|
|
|
|
xmlChar *name = xmlTextReaderName (reader);
|
2005-10-13 14:08:18 +00:00
|
|
|
|
2006-08-26 04:46:29 -05:00
|
|
|
if (xmlStrcmp (name, BAD_CAST "language") == 0)
|
|
|
|
{
|
|
|
|
// read_language_node (lang, reader, slang->priv->lang_file_name);
|
|
|
|
ret = 0;
|
|
|
|
}
|
2005-10-13 14:08:18 +00:00
|
|
|
|
2006-08-26 04:46:29 -05:00
|
|
|
xmlFree (name);
|
|
|
|
}
|
2005-10-13 14:08:18 +00:00
|
|
|
|
2006-08-26 04:46:29 -05:00
|
|
|
if (ret == 1)
|
|
|
|
ret = xmlTextReaderRead (reader);
|
|
|
|
}
|
2005-10-13 14:08:18 +00:00
|
|
|
|
2006-08-26 04:46:29 -05:00
|
|
|
xmlFreeTextReader (reader);
|
|
|
|
close (fd);
|
2006-04-10 02:55:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-04-26 23:04:30 -05:00
|
|
|
const char *
|
2006-08-26 04:46:29 -05:00
|
|
|
_moo_lang_id (MooLang *lang)
|
2005-10-13 14:08:18 +00:00
|
|
|
{
|
2006-08-26 04:46:29 -05:00
|
|
|
g_return_val_if_fail (!lang || MOO_IS_LANG (lang), NULL);
|
2005-10-13 14:08:18 +00:00
|
|
|
|
2006-08-26 04:46:29 -05:00
|
|
|
if (lang)
|
|
|
|
return GTK_SOURCE_LANGUAGE(lang)->priv->id;
|
2005-10-13 14:08:18 +00:00
|
|
|
else
|
2006-08-26 04:46:29 -05:00
|
|
|
return MOO_LANG_NONE;
|
2005-10-13 14:08:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-08-26 04:46:29 -05:00
|
|
|
const char *
|
|
|
|
_moo_lang_display_name (MooLang *lang)
|
2005-10-13 14:08:18 +00:00
|
|
|
{
|
2006-08-26 04:46:29 -05:00
|
|
|
g_return_val_if_fail (!lang || MOO_IS_LANG (lang), NULL);
|
2005-10-13 14:08:18 +00:00
|
|
|
|
2006-08-26 04:46:29 -05:00
|
|
|
if (lang)
|
|
|
|
return GTK_SOURCE_LANGUAGE(lang)->priv->name;
|
|
|
|
else
|
|
|
|
return _(MOO_LANG_NONE_NAME);
|
2005-10-13 14:08:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-08-26 04:46:29 -05:00
|
|
|
const char *
|
|
|
|
_moo_lang_get_line_comment (MooLang *lang)
|
2005-10-13 14:08:18 +00:00
|
|
|
{
|
2006-08-26 04:46:29 -05:00
|
|
|
g_return_val_if_fail (MOO_IS_LANG (lang), NULL);
|
|
|
|
return GTK_SOURCE_LANGUAGE(lang)->priv->line_comment;
|
2005-10-13 14:08:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-08-26 04:46:29 -05:00
|
|
|
const char *
|
|
|
|
_moo_lang_get_block_comment_start (MooLang *lang)
|
2005-10-13 14:08:18 +00:00
|
|
|
{
|
2006-08-26 04:46:29 -05:00
|
|
|
g_return_val_if_fail (MOO_IS_LANG (lang), NULL);
|
|
|
|
return GTK_SOURCE_LANGUAGE(lang)->priv->block_comment_start;
|
2005-10-13 14:08:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-08-26 04:46:29 -05:00
|
|
|
const char *
|
|
|
|
_moo_lang_get_block_comment_end (MooLang *lang)
|
2005-10-13 14:08:18 +00:00
|
|
|
{
|
2006-08-26 04:46:29 -05:00
|
|
|
g_return_val_if_fail (MOO_IS_LANG (lang), NULL);
|
|
|
|
return GTK_SOURCE_LANGUAGE(lang)->priv->block_comment_end;
|
2005-10-13 14:08:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-08-26 04:46:29 -05:00
|
|
|
const char *
|
|
|
|
_moo_lang_get_brackets (MooLang *lang)
|
2005-10-13 14:08:18 +00:00
|
|
|
{
|
2006-08-26 04:46:29 -05:00
|
|
|
g_return_val_if_fail (MOO_IS_LANG (lang), NULL);
|
|
|
|
return GTK_SOURCE_LANGUAGE(lang)->priv->brackets;
|
2005-10-13 14:08:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-08-26 04:46:29 -05:00
|
|
|
const char *
|
|
|
|
_moo_lang_get_section (MooLang *lang)
|
2005-10-13 14:08:18 +00:00
|
|
|
{
|
2006-08-26 04:46:29 -05:00
|
|
|
g_return_val_if_fail (MOO_IS_LANG (lang), NULL);
|
|
|
|
return GTK_SOURCE_LANGUAGE(lang)->priv->section;
|
2005-10-13 14:08:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-08-26 04:46:29 -05:00
|
|
|
gboolean
|
|
|
|
_moo_lang_get_hidden (MooLang *lang)
|
2005-10-13 14:08:18 +00:00
|
|
|
{
|
2006-08-26 04:46:29 -05:00
|
|
|
g_return_val_if_fail (MOO_IS_LANG (lang), TRUE);
|
|
|
|
return GTK_SOURCE_LANGUAGE(lang)->priv->hidden != 0;
|
2005-10-13 14:08:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-08-26 04:46:29 -05:00
|
|
|
char *
|
|
|
|
_moo_lang_id_from_name (const char *whatever)
|
2005-10-13 14:08:18 +00:00
|
|
|
{
|
2006-08-26 04:46:29 -05:00
|
|
|
if (!whatever || !g_ascii_strcasecmp (whatever, MOO_LANG_NONE))
|
|
|
|
return g_strdup (MOO_LANG_NONE);
|
2005-10-13 14:08:18 +00:00
|
|
|
else
|
2006-08-26 04:46:29 -05:00
|
|
|
return g_strstrip (g_ascii_strdown (whatever, -1));
|
2005-10-13 14:08:18 +00:00
|
|
|
}
|