On Mon, 08 Nov 2010 17:42:40 +0100%
Frank Lanitz <[email protected]> wrote:

> Am 08.11.2010 10:55, schrieb Eugene Arshinov:
> >
> > If you have any objections about the code or the functionality of
> > the plugin, please write.  I will update the code as needed.
> 
> Didn't digg into the code too much but what about to put it to the
> plugins project?
> 

It's absolutely possible.  I don't know precisely when you should put a
plugin in geany and when in geany-plugins.  I guess new plugins
should go to geany-plugins, but this one is somewhat related to HTML
Chars, which is in geany, and also it's very tiny (for reference I
attach the full source code here).  So I decided to put it into geany
for the present.

Best regards,
Eugene
/*
 *      xmlsnippets.c - this file is part of Geany, a fast and lightweight IDE
 *
 *      Copyright 2010 Eugene Arshinov <earshinov(at)gmail(dot)com>
 *
 *      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.
 *
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 *
 * $Id$
 */

/* XML Snippets plugin (Autocompletes XML/HTML tags using snippets) */

#include <string.h>
#include "geanyplugin.h"
#include "SciLexer.h"


GeanyData		*geany_data;
GeanyFunctions	*geany_functions;


PLUGIN_VERSION_CHECK(GEANY_API_VERSION)

PLUGIN_SET_INFO(_("XML Snippets"), _("Autocompletes XML/HTML tags using snippets."), VERSION,
	_("The Geany developer team"))


static gboolean editor_notify_cb(GObject *object, GeanyEditor *editor,
								SCNotification *nt, gpointer data);

PluginCallback plugin_callbacks[] =
{
	{ "editor-notify", (GCallback) &editor_notify_cb, FALSE, NULL },
	{ NULL, NULL, FALSE, NULL }
};


void plugin_init(GeanyData *data)
{
}


void plugin_cleanup(void)
{
}


static gboolean editor_notify_cb(GObject *object, GeanyEditor *editor,
								SCNotification *nt, gpointer data)
{
	gint lexer, pos, style, min, size;
	gchar sel[512];
	gchar *str_found, *snippet_name, *completion;
	GHashTable *snippets;
	gboolean handled = FALSE;

	if (nt->nmhdr.code == SCN_CHARADDED && nt->ch == '>')
	{
		lexer = sci_get_lexer(editor->sci);
		if (lexer == SCLEX_XML || lexer == SCLEX_HTML)
		{
			pos = sci_get_current_position(editor->sci);
			style = sci_get_style_at(editor->sci, pos);

			if ((style <= SCE_H_XCCOMMENT || sci_is_string_style(lexer, style)) &&
				!sci_is_comment_style(lexer, style))
			{
				/* Grab the last 512 characters or so */
				min = pos - sizeof(sel);
				if (min < 0) min = 0;
				size = pos - min;

				sci_get_text_range(editor->sci, min, pos, sel);

				if (sel[size - 1] != '/')
				{
					str_found = utils_find_open_xml_tag(sel, size);
					if (NZV(str_found))
					{
						snippets = editor_get_snippets_for_file_type(editor->document->file_type->name);
						snippet_name = g_strconcat("<", str_found, ">", NULL);
						completion = g_hash_table_lookup(snippets, snippet_name);
						if (NZV(completion))
						{
							editor_insert_snippet(editor, pos, completion);
							sci_scroll_caret(editor->sci);
							handled = TRUE;
						}
						g_free(str_found);
						g_free(snippet_name);
					}
				}
			}
		}
	}
	return handled;
}





_______________________________________________
Geany mailing list
[email protected]
http://lists.uvena.de/cgi-bin/mailman/listinfo/geany

Reply via email to