1 /*
2  *	  ao_xmltagging.c
3  *
4  *	  Copyright 2010 Frank Lanitz <frank(at)frank(dot)uvena(dot)de>
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 #ifdef HAVE_CONFIG_H
23 	#include "config.h"
24 #endif
25 #include <geanyplugin.h>
26 
27 #include "addons.h"
28 #include "ao_xmltagging.h"
29 
30 
enter_key_pressed_in_entry(G_GNUC_UNUSED GtkWidget * widget,gpointer dialog)31 static void enter_key_pressed_in_entry(G_GNUC_UNUSED GtkWidget *widget, gpointer dialog)
32 {
33 	gtk_dialog_response(GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT);
34 }
35 
36 
ao_xmltagging(void)37 void ao_xmltagging(void)
38 {
39 	GeanyDocument *doc = NULL;
40 
41 	doc = document_get_current();
42 
43 	g_return_if_fail(doc != NULL);
44 
45 	if (sci_has_selection(doc->editor->sci) == TRUE)
46 	{
47 		GtkWidget *dialog = NULL;
48 		GtkWidget *vbox = NULL;
49 		GtkWidget *hbox = NULL;
50 		GtkWidget *label = NULL;
51 		GtkWidget *textbox = NULL;
52 		GtkWidget *textline = NULL;
53 
54 		dialog = gtk_dialog_new_with_buttons(_("XML tagging"),
55 							 GTK_WINDOW(geany->main_widgets->window),
56 							 GTK_DIALOG_DESTROY_WITH_PARENT, GTK_STOCK_CANCEL,
57 							 GTK_RESPONSE_CANCEL, GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
58 							 NULL);
59 		vbox = ui_dialog_vbox_new(GTK_DIALOG(dialog));
60 		gtk_widget_set_name(dialog, "GeanyDialog");
61 		gtk_box_set_spacing(GTK_BOX(vbox), 10);
62 
63 		hbox = gtk_hbox_new(FALSE, 10);
64 
65 		label = gtk_label_new(_("Tag name to be inserted:"));
66 		textbox = gtk_entry_new();
67 
68 		textline = gtk_label_new(
69 			_("%s will be replaced with your current selection. Please keep care on your selection"));
70 
71 		gtk_container_add(GTK_CONTAINER(hbox), label);
72 		gtk_container_add(GTK_CONTAINER(hbox), textbox);
73 		gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
74 
75 		gtk_container_add(GTK_CONTAINER(vbox), hbox);
76 		gtk_container_add(GTK_CONTAINER(vbox), textline);
77 
78 		g_signal_connect(G_OBJECT(textbox), "activate",
79 			G_CALLBACK(enter_key_pressed_in_entry), dialog);
80 
81 		gtk_widget_show_all(vbox);
82 
83 		if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT)
84 		{
85 			GString *tag = NULL;
86 			gchar *selection  = NULL;
87 			gchar *replacement = NULL;
88 
89 			/* Getting the selection and setting the undo flag */
90 			selection = sci_get_selection_contents(doc->editor->sci);
91 			sci_start_undo_action(doc->editor->sci);
92 
93 			/* Getting the tag */
94 			tag = g_string_new(gtk_entry_get_text(GTK_ENTRY(textbox)));
95 
96 			if (tag->len > 0)
97 			{
98 				gsize end = 0;
99 				gchar *end_tag;
100 
101 				/* First we check for %s and replace it with selection*/
102 				utils_string_replace_all(tag, "%s", selection);
103 
104 				/* We try to find a space inside the inserted tag as we
105 				 * only need to close the tag with part until first space.
106 				 * */
107 				while (end < tag->len && !g_ascii_isspace(tag->str[end]) )
108 					end++;
109 
110 				if (end > 0)
111 				{
112 					end_tag = g_strndup(tag->str, end);
113 				}
114 				else
115 				{
116 					end_tag = tag->str;
117 				}
118 				replacement = g_strconcat("<", tag->str, ">",
119 								selection, "</", end_tag, ">", NULL);
120 				g_free(end_tag);
121 			}
122 
123 			sci_replace_sel(doc->editor->sci, replacement);
124 			sci_end_undo_action(doc->editor->sci);
125 			g_free(selection);
126 			g_free(replacement);
127 			g_string_free(tag, TRUE);
128 		}
129 		gtk_widget_destroy(dialog);
130 	}
131 }
132