1 /*
2  * plugin.c - this file is part of XMLSnippets, a Geany plugin
3  *
4  * Copyright 2010 Eugene Arshinov <earshinov(at)gmail(dot)com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19  * MA 02110-1301, USA.
20  */
21 
22 #ifndef TEST
23 
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27 
28 #include "plugin.h"
29 #include "xmlsnippets.h"
30 #include <SciLexer.h>
31 
32 
33 static gboolean editor_notify_cb(GObject *object, GeanyEditor *editor,
34 	SCNotification *nt, gpointer data);
35 
36 
37 GeanyData *geany_data;
38 GeanyPlugin *geany_plugin;
39 
40 PLUGIN_VERSION_CHECK(224)
41 PLUGIN_SET_TRANSLATABLE_INFO(
42   LOCALEDIR,
43   GETTEXT_PACKAGE,
44   _("XML Snippets"),
45   _("Autocompletes XML/HTML tags using snippets."),
46   VERSION,
47   "Eugene Arshinov")
48 
49 PluginCallback plugin_callbacks[] =
50 {
51 	{ "editor-notify", (GCallback) &editor_notify_cb, FALSE, NULL },
52 	{ NULL, NULL, FALSE, NULL }
53 };
54 
plugin_init(GeanyData * data)55 void plugin_init(GeanyData *data)
56 {
57 }
58 
plugin_cleanup(void)59 void plugin_cleanup(void)
60 {
61 }
62 
63 
editor_notify_cb(GObject * object,GeanyEditor * editor,SCNotification * nt,gpointer data)64 static gboolean editor_notify_cb(GObject *object, GeanyEditor *editor,
65 								SCNotification *nt, gpointer data)
66 {
67 	gint lexer, pos, style, min, size;
68 	gboolean handled = FALSE;
69 
70 	if (nt->nmhdr.code == SCN_CHARADDED && nt->ch == '>')
71 	{
72 		lexer = sci_get_lexer(editor->sci);
73 		if (lexer == SCLEX_XML || lexer == SCLEX_HTML)
74 		{
75 			pos = sci_get_current_position(editor->sci);
76 			style = sci_get_style_at(editor->sci, pos);
77 
78 			if ((style <= SCE_H_XCCOMMENT || highlighting_is_string_style(lexer, style)) &&
79 				!highlighting_is_comment_style(lexer, style))
80 			{
81 				CompletionInfo c;
82 				InputInfo i;
83 				gchar *sel;
84 
85 				/* Grab the last 512 characters or so */
86 				min = pos - 512;
87 				if (min < 0) min = 0;
88 				size = pos - min;
89 
90 				sel = sci_get_contents_range(editor->sci, min, pos);
91 
92 				if (get_completion(editor, sel, size, &c, &i))
93 				{
94 					/* Remove typed opening tag */
95 					sci_set_selection_start(editor->sci, min + i.tag_start);
96 					sci_set_selection_end(editor->sci, pos);
97 					sci_replace_sel(editor->sci, "");
98 					pos -= (size - i.tag_start); /* pos has changed while deleting */
99 
100 					/* Insert the completion */
101 					editor_insert_snippet(editor, pos, c.completion);
102 					sci_scroll_caret(editor->sci);
103 
104 					g_free((gchar *)c.completion);
105 					handled = TRUE;
106 				}
107 
108 				g_free(sel);
109 			}
110 		}
111 	}
112 	return handled;
113 }
114 
115 #endif
116