~ubuntu-branches/ubuntu/saucy/bluefish/saucy

« back to all changes in this revision

Viewing changes to .pc/fix_segfault_blocksync.patch/src/blocksync.c

  • Committer: Package Import Robot
  • Author(s): Daniel Leidert
  • Date: 2013-05-09 14:36:58 UTC
  • mfrom: (17.1.1 experimental)
  • Revision ID: package-import@ubuntu.com-20130509143658-kgqxj5c23oi0lk20
Tags: 2.2.4-2
* debian/control (Standards-Version): Bumped to 3.9.4.
* debian/copyright: Updated.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Bluefish HTML Editor
2
 
 * blocksync.c - synchronise blocks of text trough all open documents
3
 
 *
4
 
 * Copyright (C) 2006 Olivier Sessink
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 3 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, see <http://www.gnu.org/licenses/>.
18
 
 */
19
 
/*#define DEBUG*/
20
 
 
21
 
#include <gtk/gtk.h>
22
 
#include <string.h>
23
 
#include "config.h"
24
 
 
25
 
#include "bluefish.h"
26
 
#include "document.h"        /* doc_replace_text() */
27
 
#include "gtk_easy.h"        /* a lot of GUI functions */
28
 
#include "snr3.h"
29
 
 
30
 
typedef enum {
31
 
        page_no_selection,
32
 
        page_start_marker,
33
 
        page_end_marker,
34
 
        page_summary
35
 
} Tbspage;
36
 
 
37
 
typedef struct {
38
 
        GtkWidget *dialog;
39
 
        GtkWidget *warnlabel;
40
 
        Tbspage curpage;
41
 
        Tbfwin *bfwin;
42
 
        GtkWidget *child;
43
 
        GtkWidget *textview;
44
 
        gchar *allblock;
45
 
        gchar *startmarker;
46
 
        gchar *endmarker;
47
 
} Tbsdialog;
48
 
 
49
 
static gboolean single_occurence(gchar *allcontent, gchar *needle) {
50
 
        gchar *first;
51
 
        /* the string should be only once in allcontent */
52
 
        first = strstr(allcontent, needle);
53
 
        if (first) {
54
 
                gchar *second = strstr(first+1, needle);
55
 
                if (second == NULL) {
56
 
                        return TRUE;
57
 
                }
58
 
                DEBUG_MSG("single_occurence, error, needle %s exists twice!, first=%p, second=%p\n",needle,first,second);
59
 
        }
60
 
        return FALSE;
61
 
}
62
 
 
63
 
static gchar *pcre_escape_string(const gchar *orig) {
64
 
        gint len,i=0,o=0;
65
 
        gchar *new;
66
 
        
67
 
        len = strlen(orig);
68
 
        new = g_new(gchar,len*2+1);
69
 
        while (i<=len) {
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] == '$') {
73
 
                        new[o] = '\\';
74
 
                        o++;
75
 
                }
76
 
                new[o] = orig[i];
77
 
                i++;
78
 
                o++;
79
 
        }
80
 
        DEBUG_MSG("pcre_escape_string, orig=%s, new=%s\n",orig,new);
81
 
        return new;
82
 
}
83
 
 
84
 
static void bs_page_no_selection(Tbsdialog *bsdialog) {
85
 
        GtkWidget *label, *vbox;
86
 
        gchar *tmp1;
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.");      
88
 
        
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);
93
 
        } else {
94
 
                tmp1 = g_strdup(stext);
95
 
        }
96
 
        
97
 
        label = gtk_label_new(tmp1);
98
 
        g_free(tmp1);
99
 
        
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);
103
 
        
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);
107
 
        
108
 
        
109
 
        bsdialog->child = vbox;
110
 
        bsdialog->curpage = page_no_selection;
111
 
}
112
 
 
113
 
static void bs_page_start_marker(Tbsdialog *bsdialog, const gchar *text) {
114
 
        GtkWidget *vbox, *label, *scrolwin;
115
 
        GtkTextBuffer *buffer;
116
 
        GtkTextIter start,end;
117
 
        GtkTextMark *mark;
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);
120
 
        
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);
125
 
        
126
 
        bsdialog->warnlabel = gtk_label_new("");        
127
 
        gtk_box_pack_start(GTK_BOX(vbox), bsdialog->warnlabel, FALSE,TRUE,6);   
128
 
        
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);
135
 
        
136
 
        buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(bsdialog->textview));
137
 
        gtk_text_buffer_get_start_iter(buffer,&start);
138
 
        end = 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);
143
 
        
144
 
        bsdialog->child = vbox;
145
 
        bsdialog->curpage = page_start_marker;
146
 
}
147
 
 
148
 
static void bs_page_end_marker(Tbsdialog *bsdialog, const gchar *text) {
149
 
        GtkWidget *vbox, *label, *scrolwin;
150
 
        GtkTextBuffer *buffer;
151
 
        GtkTextIter start,end;
152
 
        GtkTextMark *mark;
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);
155
 
        
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);
160
 
        
161
 
        bsdialog->warnlabel = gtk_label_new("");        
162
 
        gtk_box_pack_start(GTK_BOX(vbox), bsdialog->warnlabel, FALSE,TRUE,6);
163
 
        
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);
170
 
        
171
 
        buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(bsdialog->textview));
172
 
        gtk_text_buffer_get_end_iter(buffer,&end);
173
 
        start = 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);
179
 
        }
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);
183
 
        
184
 
        bsdialog->child = vbox;
185
 
        bsdialog->curpage = page_end_marker;
186
 
}
187
 
 
188
 
static void bs_page_summary(Tbsdialog *bsdialog) {
189
 
        gchar *tmp, *tmp1, *tmp2;
190
 
        GtkWidget *label;
191
 
 
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);
196
 
        g_free(tmp);
197
 
        g_free(tmp1);
198
 
        g_free(tmp2);
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);
203
 
 
204
 
        bsdialog->child = label;
205
 
        bsdialog->curpage = page_summary;
206
 
}
207
 
 
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);
213
 
        g_free(bsdialog);
214
 
}
215
 
 
216
 
static void bs_dialog_response_lcb(GtkDialog *dialog, gint response, Tbsdialog *bsdialog) {
217
 
        if (bsdialog->curpage == page_no_selection && response == 1) {
218
 
                gint start,end;
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);
224
 
                } else {
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);
227
 
                }
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);
238
 
                        } else {
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;
243
 
                        }
244
 
                } else {
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);
247
 
                }
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);
258
 
                        } else {
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;
263
 
                        }
264
 
                }  else {
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);
267
 
                }
268
 
        } else if (bsdialog->curpage == page_summary && response == 1) {
269
 
                gchar *searchpat, *tmp1, *tmp2;
270
 
                /* GO */
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);
275
 
                g_free(tmp1);
276
 
                g_free(tmp2);
277
 
                snr3_run_extern_replace(bsdialog->bfwin->current_document, searchpat, snr3scope_alldocs,snr3type_pcre,TRUE, bsdialog->allblock,FALSE);
278
 
                g_free(searchpat);
279
 
                
280
 
                /* cleanup */
281
 
                bsdialog_cleanup(bsdialog);
282
 
        } else if (response == -2 || response == -4){
283
 
                bsdialog_cleanup(bsdialog);
284
 
        } else{
285
 
                DEBUG_MSG("flow broken, response=%d, page=%d\n",response,bsdialog->curpage);
286
 
        }
287
 
}
288
 
 
289
 
void blocksync_dialog(Tbfwin *bfwin) {
290
 
        Tbsdialog *bsdialog;
291
 
        
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,
299
 
                                        NULL);
300
 
        gtk_window_set_default_size(GTK_WINDOW(bsdialog->dialog),400,300);
301
 
 
302
 
        bs_page_no_selection(bsdialog);
303
 
        
304
 
        gtk_widget_show_all(bsdialog->dialog);
305
 
        g_signal_connect(bsdialog->dialog, "response", G_CALLBACK(bs_dialog_response_lcb), bsdialog);
306
 
}