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