1
/* Bluefish HTML Editor
2
* blocksync.c - synchronise blocks of text trough all open documents
4
* Copyright (C) 2006 Olivier Sessink
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 3 of the License, or
9
* (at your option) any later version.
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.
16
* You should have received a copy of the GNU General Public License
17
* along with this program. If not, see <http://www.gnu.org/licenses/>.
26
#include "document.h" /* doc_replace_text() */
27
#include "gtk_easy.h" /* a lot of GUI functions */
49
static gboolean single_occurence(gchar *allcontent, gchar *needle) {
51
/* the string should be only once in allcontent */
52
first = strstr(allcontent, needle);
54
gchar *second = strstr(first+1, needle);
58
DEBUG_MSG("single_occurence, error, needle %s exists twice!, first=%p, second=%p\n",needle,first,second);
63
static gchar *pcre_escape_string(const gchar *orig) {
68
new = g_new(gchar,len*2+1);
70
if (orig[i] == '.' || orig[i] == '?' || orig[i] == '+' || orig[i] == '*'
71
|| orig[i] == '(' || orig[i] == ')' || orig[i] == '[' || orig[i] == ']'
72
|| orig[i] == '\\' || orig[i] == '^' || orig[i] == '$') {
80
DEBUG_MSG("pcre_escape_string, orig=%s, new=%s\n",orig,new);
84
static void bs_page_no_selection(Tbsdialog *bsdialog) {
85
GtkWidget *label, *vbox;
87
const gchar *stext = _("Select the block to sync to other pages. Include the start-of-block and end-of-block markers in your selection.");
89
vbox = gtk_vbox_new(FALSE,6);
90
gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(bsdialog->dialog))), vbox, TRUE,TRUE,5);
91
if (doc_has_selection(bsdialog->bfwin->current_document)) {
92
tmp1 = g_strconcat(stext, " ", _("To proceed with the current selection click forward."), NULL);
94
tmp1 = g_strdup(stext);
97
label = gtk_label_new(tmp1);
100
gtk_label_set_use_markup(GTK_LABEL(label),TRUE);
101
gtk_label_set_line_wrap(GTK_LABEL(label),TRUE);
102
gtk_box_pack_start(GTK_BOX(vbox), label, FALSE,TRUE,5);
104
gtk_widget_show_all(bsdialog->dialog);
105
bsdialog->warnlabel = gtk_label_new("");
106
gtk_box_pack_start(GTK_BOX(vbox), bsdialog->warnlabel, FALSE,TRUE,5);
109
bsdialog->child = vbox;
110
bsdialog->curpage = page_no_selection;
113
static void bs_page_start_marker(Tbsdialog *bsdialog, const gchar *text) {
114
GtkWidget *vbox, *label, *scrolwin;
115
GtkTextBuffer *buffer;
116
GtkTextIter start,end;
118
vbox = gtk_vbox_new(FALSE,6);
119
gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(bsdialog->dialog))), vbox, TRUE,TRUE,5);
121
label = gtk_label_new(_("Below is the block, including start and end marker. Please <b>select the start marker</b> in the text below."));
122
gtk_label_set_use_markup(GTK_LABEL(label),TRUE);
123
gtk_label_set_line_wrap(GTK_LABEL(label),TRUE);
124
gtk_box_pack_start(GTK_BOX(vbox),label,FALSE,TRUE,6);
126
bsdialog->warnlabel = gtk_label_new("");
127
gtk_box_pack_start(GTK_BOX(vbox), bsdialog->warnlabel, FALSE,TRUE,6);
129
scrolwin = textview_buffer_in_scrolwin(&bsdialog->textview, -1, -1, text, GTK_WRAP_NONE);
130
gtk_box_pack_start(GTK_BOX(vbox),scrolwin,TRUE,TRUE,6);
131
gtk_widget_show(bsdialog->textview);
132
gtk_widget_show(label);
133
gtk_widget_show(scrolwin);
134
gtk_widget_show(vbox);
136
buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(bsdialog->textview));
137
gtk_text_buffer_get_start_iter(buffer,&start);
139
gtk_text_iter_forward_to_line_end(&end);
140
gtk_text_buffer_select_range(buffer,&start,&end);
141
mark = gtk_text_buffer_get_selection_bound(buffer);
142
gtk_text_view_scroll_mark_onscreen(GTK_TEXT_VIEW(bsdialog->textview),mark);
144
bsdialog->child = vbox;
145
bsdialog->curpage = page_start_marker;
148
static void bs_page_end_marker(Tbsdialog *bsdialog, const gchar *text) {
149
GtkWidget *vbox, *label, *scrolwin;
150
GtkTextBuffer *buffer;
151
GtkTextIter start,end;
153
vbox = gtk_vbox_new(FALSE,6);
154
gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(bsdialog->dialog))), vbox, TRUE,TRUE,5);
156
label = gtk_label_new(_("Please <b>select the end marker</b> in the text below."));
157
gtk_label_set_use_markup(GTK_LABEL(label),TRUE);
158
gtk_label_set_line_wrap(GTK_LABEL(label),TRUE);
159
gtk_box_pack_start(GTK_BOX(vbox),label,FALSE,TRUE,6);
161
bsdialog->warnlabel = gtk_label_new("");
162
gtk_box_pack_start(GTK_BOX(vbox), bsdialog->warnlabel, FALSE,TRUE,6);
164
scrolwin = textview_buffer_in_scrolwin(&bsdialog->textview, -1, -1, text, GTK_WRAP_NONE);
165
gtk_box_pack_start(GTK_BOX(vbox),scrolwin,TRUE,TRUE,6);
166
gtk_widget_show(bsdialog->textview);
167
gtk_widget_show(label);
168
gtk_widget_show(scrolwin);
169
gtk_widget_show(vbox);
171
buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(bsdialog->textview));
172
gtk_text_buffer_get_end_iter(buffer,&end);
174
gtk_text_iter_set_line_offset(&start,0);
175
if (gtk_text_iter_equal(&start,&end)) {
176
/* if the last line is just a newline, we select the line before */
177
gtk_text_iter_backward_line(&start);
178
gtk_text_iter_set_line_offset(&start,0);
180
gtk_text_buffer_select_range(buffer,&start,&end);
181
mark = gtk_text_buffer_get_selection_bound(buffer);
182
gtk_text_view_scroll_mark_onscreen(GTK_TEXT_VIEW(bsdialog->textview),mark);
184
bsdialog->child = vbox;
185
bsdialog->curpage = page_end_marker;
188
static void bs_page_summary(Tbsdialog *bsdialog) {
189
gchar *tmp, *tmp1, *tmp2;
192
tmp1 = g_markup_escape_text(bsdialog->startmarker,-1);
193
tmp2 = g_markup_escape_text(bsdialog->endmarker,-1);
194
tmp = g_strdup_printf(_("Will replace the text between <i>%s</i> and <i>%s</i> in all documents opened in this window.\n<b>Proceed?</b>"), tmp1,tmp2);
195
label = gtk_label_new(tmp);
199
gtk_label_set_use_markup(GTK_LABEL(label),TRUE);
200
gtk_label_set_line_wrap(GTK_LABEL(label),TRUE);
201
gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(bsdialog->dialog))), label, TRUE,TRUE,5);
202
gtk_widget_show_all(bsdialog->dialog);
204
bsdialog->child = label;
205
bsdialog->curpage = page_summary;
208
static void bsdialog_cleanup(Tbsdialog *bsdialog) {
209
gtk_widget_destroy(bsdialog->dialog);
210
if (bsdialog->startmarker) g_free(bsdialog->startmarker);
211
if (bsdialog->endmarker) g_free(bsdialog->endmarker);
212
if (bsdialog->allblock) g_free(bsdialog->allblock);
216
static void bs_dialog_response_lcb(GtkDialog *dialog, gint response, Tbsdialog *bsdialog) {
217
if (bsdialog->curpage == page_no_selection && response == 1) {
219
if (doc_get_selection(bsdialog->bfwin->current_document,&start,&end)) {
220
/* build the new page with the selection text in there */
221
bsdialog->allblock = doc_get_chars(bsdialog->bfwin->current_document,start,end);
222
gtk_widget_destroy(bsdialog->child);
223
bs_page_start_marker(bsdialog, bsdialog->allblock);
225
gtk_label_set_markup(GTK_LABEL(bsdialog->warnlabel), _("<span foreground=\"red\" weight=\"bold\">Please select the block of text to synchronize.</span>"));
226
gtk_widget_show(bsdialog->warnlabel);
228
} else if (bsdialog->curpage == page_start_marker && response == 1) {
229
GtkTextBuffer *buffer;
230
GtkTextIter start,end;
231
/* check the textview for the selection */
232
buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(bsdialog->textview));
233
if (gtk_text_buffer_get_selection_bounds(buffer,&start,&end)) {
234
bsdialog->startmarker = gtk_text_buffer_get_text(buffer,&start,&end,FALSE);
235
if (single_occurence(bsdialog->allblock, bsdialog->startmarker)) {
236
gtk_widget_destroy(bsdialog->child);
237
bs_page_end_marker(bsdialog, bsdialog->allblock);
239
gtk_label_set_markup(GTK_LABEL(bsdialog->warnlabel), _("<span foreground=\"red\" weight=\"bold\">This marker exists more than once in the block.</span>"));
240
gtk_widget_show(bsdialog->warnlabel);
241
g_free(bsdialog->startmarker);
242
bsdialog->startmarker = NULL;
245
gtk_label_set_markup(GTK_LABEL(bsdialog->warnlabel), _("<span foreground=\"red\" weight=\"bold\">Select the text that marks the start of the block.</span>"));
246
gtk_widget_show(bsdialog->warnlabel);
248
} else if (bsdialog->curpage == page_end_marker && response == 1) {
249
GtkTextBuffer *buffer;
250
GtkTextIter start,end;
251
/* check the textview for the selection */
252
buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(bsdialog->textview));
253
if (gtk_text_buffer_get_selection_bounds(buffer,&start,&end)) {
254
bsdialog->endmarker = gtk_text_buffer_get_text(buffer,&start,&end,FALSE);
255
if (single_occurence(bsdialog->allblock, bsdialog->endmarker)) {
256
gtk_widget_destroy(bsdialog->child);
257
bs_page_summary(bsdialog);
259
gtk_label_set_markup(GTK_LABEL(bsdialog->warnlabel), _("<span foreground=\"red\" weight=\"bold\">This marker exists more than once in the block.</span>"));
260
gtk_widget_show(bsdialog->warnlabel);
261
g_free(bsdialog->endmarker);
262
bsdialog->endmarker = NULL;
265
gtk_label_set_markup(GTK_LABEL(bsdialog->warnlabel), _("<span foreground=\"red\" weight=\"bold\">Select the text that marks the end of the block.</span>"));
266
gtk_widget_show(bsdialog->warnlabel);
268
} else if (bsdialog->curpage == page_summary && response == 1) {
269
gchar *searchpat, *tmp1, *tmp2;
271
tmp1 = pcre_escape_string(bsdialog->startmarker);
272
tmp2 = pcre_escape_string(bsdialog->endmarker);
273
searchpat = g_strconcat(tmp1, ".*?",tmp2 , NULL);
274
DEBUG_MSG("searchpat=%s\n",searchpat);
277
snr3_run_extern_replace(bsdialog->bfwin->current_document, searchpat, snr3scope_alldocs,snr3type_pcre,TRUE, bsdialog->allblock,FALSE);
281
bsdialog_cleanup(bsdialog);
282
} else if (response == -2 || response == -4){
283
bsdialog_cleanup(bsdialog);
285
DEBUG_MSG("flow broken, response=%d, page=%d\n",response,bsdialog->curpage);
289
void blocksync_dialog(Tbfwin *bfwin) {
292
bsdialog = g_new0(Tbsdialog,1);
293
bsdialog->bfwin = bfwin;
294
bsdialog->curpage = page_no_selection;
295
bsdialog->dialog = gtk_dialog_new_with_buttons(_("Synchronize text block"),GTK_WINDOW(bfwin->main_window),
296
GTK_DIALOG_DESTROY_WITH_PARENT,
297
GTK_STOCK_CANCEL,GTK_RESPONSE_REJECT,
298
GTK_STOCK_GO_FORWARD,1,
300
gtk_window_set_default_size(GTK_WINDOW(bsdialog->dialog),400,300);
302
bs_page_no_selection(bsdialog);
304
gtk_widget_show_all(bsdialog->dialog);
305
g_signal_connect(bsdialog->dialog, "response", G_CALLBACK(bs_dialog_response_lcb), bsdialog);