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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646 | /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
*
* lapiztextregion.h - CtkTextMark based region utility functions
*
* This file is part of the CtkSourceView widget
*
* Copyright (C) 2002 Gustavo Giráldez <gustavo.giraldez@gmx.net>
*
* 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 St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <glib.h>
#include "lapiztextregion.h"
#undef ENABLE_DEBUG
/*
#define ENABLE_DEBUG
*/
#ifdef ENABLE_DEBUG
#define DEBUG(x) (x)
#else
#define DEBUG(x)
#endif
typedef struct _Subregion {
CtkTextMark *start;
CtkTextMark *end;
} Subregion;
struct _LapizTextRegion {
CtkTextBuffer *buffer;
GList *subregions;
guint32 time_stamp;
};
typedef struct _LapizTextRegionIteratorReal LapizTextRegionIteratorReal;
struct _LapizTextRegionIteratorReal {
LapizTextRegion *region;
guint32 region_time_stamp;
GList *subregions;
};
/* ----------------------------------------------------------------------
Private interface
---------------------------------------------------------------------- */
/* Find and return a subregion node which contains the given text
iter. If left_side is TRUE, return the subregion which contains
the text iter or which is the leftmost; else return the rightmost
subregion */
static GList *
find_nearest_subregion (LapizTextRegion *region,
const CtkTextIter *iter,
GList *begin,
gboolean leftmost,
gboolean include_edges)
{
GList *l, *retval;
g_return_val_if_fail (region != NULL && iter != NULL, NULL);
if (!begin)
begin = region->subregions;
if (begin)
retval = begin->prev;
else
retval = NULL;
for (l = begin; l; l = l->next) {
CtkTextIter sr_iter;
Subregion *sr = l->data;
gint cmp;
if (!leftmost) {
ctk_text_buffer_get_iter_at_mark (region->buffer, &sr_iter, sr->end);
cmp = ctk_text_iter_compare (iter, &sr_iter);
if (cmp < 0 || (cmp == 0 && include_edges)) {
retval = l;
break;
}
} else {
ctk_text_buffer_get_iter_at_mark (region->buffer, &sr_iter, sr->start);
cmp = ctk_text_iter_compare (iter, &sr_iter);
if (cmp > 0 || (cmp == 0 && include_edges))
retval = l;
else
break;
}
}
return retval;
}
/* ----------------------------------------------------------------------
Public interface
---------------------------------------------------------------------- */
LapizTextRegion *
lapiz_text_region_new (CtkTextBuffer *buffer)
{
LapizTextRegion *region;
g_return_val_if_fail (buffer != NULL, NULL);
region = g_new (LapizTextRegion, 1);
region->buffer = buffer;
region->subregions = NULL;
region->time_stamp = 0;
return region;
}
void
lapiz_text_region_destroy (LapizTextRegion *region, gboolean delete_marks)
{
g_return_if_fail (region != NULL);
while (region->subregions) {
Subregion *sr = region->subregions->data;
if (delete_marks) {
ctk_text_buffer_delete_mark (region->buffer, sr->start);
ctk_text_buffer_delete_mark (region->buffer, sr->end);
}
g_free (sr);
region->subregions = g_list_delete_link (region->subregions,
region->subregions);
}
region->buffer = NULL;
region->time_stamp = 0;
g_free (region);
}
CtkTextBuffer *
lapiz_text_region_get_buffer (LapizTextRegion *region)<--- Parameter 'region' can be declared as pointer to const
{
g_return_val_if_fail (region != NULL, NULL);
return region->buffer;
}
static void
lapiz_text_region_clear_zero_length_subregions (LapizTextRegion *region)
{
CtkTextIter start, end;
GList *node;
g_return_if_fail (region != NULL);
for (node = region->subregions; node; ) {
Subregion *sr = node->data;
ctk_text_buffer_get_iter_at_mark (region->buffer, &start, sr->start);
ctk_text_buffer_get_iter_at_mark (region->buffer, &end, sr->end);
if (ctk_text_iter_equal (&start, &end)) {
ctk_text_buffer_delete_mark (region->buffer, sr->start);
ctk_text_buffer_delete_mark (region->buffer, sr->end);
g_free (sr);
if (node == region->subregions)
region->subregions = node = g_list_delete_link (node, node);
else
node = g_list_delete_link (node, node);
++region->time_stamp;
} else {
node = node->next;
}
}
}
void
lapiz_text_region_add (LapizTextRegion *region,
const CtkTextIter *_start,
const CtkTextIter *_end)
{
GList *start_node, *end_node;<--- Variable 'end_node' can be declared as pointer to const
CtkTextIter start, end;
g_return_if_fail (region != NULL && _start != NULL && _end != NULL);
start = *_start;
end = *_end;
DEBUG (g_print ("---\n"));
DEBUG (lapiz_text_region_debug_print (region));
DEBUG (g_message ("region_add (%d, %d)",
ctk_text_iter_get_offset (&start),
ctk_text_iter_get_offset (&end)));
ctk_text_iter_order (&start, &end);
/* don't add zero-length regions */
if (ctk_text_iter_equal (&start, &end))
return;
/* find bounding subregions */
start_node = find_nearest_subregion (region, &start, NULL, FALSE, TRUE);
end_node = find_nearest_subregion (region, &end, start_node, TRUE, TRUE);
if (start_node == NULL || end_node == NULL || end_node == start_node->prev) {
/* create the new subregion */
Subregion *sr = g_new0 (Subregion, 1);
sr->start = ctk_text_buffer_create_mark (region->buffer, NULL, &start, TRUE);
sr->end = ctk_text_buffer_create_mark (region->buffer, NULL, &end, FALSE);
if (start_node == NULL) {
/* append the new region */
region->subregions = g_list_append (region->subregions, sr);
} else if (end_node == NULL) {
/* prepend the new region */
region->subregions = g_list_prepend (region->subregions, sr);
} else {
/* we are in the middle of two subregions */
region->subregions = g_list_insert_before (region->subregions,
start_node, sr);
}
}
else {
CtkTextIter iter;
Subregion *sr = start_node->data;
if (start_node != end_node) {
/* we need to merge some subregions */
GList *l = start_node->next;
Subregion *q;
ctk_text_buffer_delete_mark (region->buffer, sr->end);
while (l != end_node) {
q = l->data;
ctk_text_buffer_delete_mark (region->buffer, q->start);
ctk_text_buffer_delete_mark (region->buffer, q->end);
g_free (q);
l = g_list_delete_link (l, l);
}
q = l->data;
ctk_text_buffer_delete_mark (region->buffer, q->start);
sr->end = q->end;
g_free (q);
l = g_list_delete_link (l, l);
}
/* now move marks if that action expands the region */
ctk_text_buffer_get_iter_at_mark (region->buffer, &iter, sr->start);
if (ctk_text_iter_compare (&iter, &start) > 0)
ctk_text_buffer_move_mark (region->buffer, sr->start, &start);
ctk_text_buffer_get_iter_at_mark (region->buffer, &iter, sr->end);
if (ctk_text_iter_compare (&iter, &end) < 0)
ctk_text_buffer_move_mark (region->buffer, sr->end, &end);
}
++region->time_stamp;
DEBUG (lapiz_text_region_debug_print (region));
}
void
lapiz_text_region_subtract (LapizTextRegion *region,
const CtkTextIter *_start,
const CtkTextIter *_end)
{
GList *start_node, *end_node, *node;
CtkTextIter sr_start_iter, sr_end_iter;
gboolean done;
gboolean start_is_outside, end_is_outside;
Subregion *sr;
CtkTextIter start, end;
g_return_if_fail (region != NULL && _start != NULL && _end != NULL);
start = *_start;
end = *_end;
DEBUG (g_print ("---\n"));
DEBUG (lapiz_text_region_debug_print (region));
DEBUG (g_message ("region_substract (%d, %d)",
ctk_text_iter_get_offset (&start),
ctk_text_iter_get_offset (&end)));
ctk_text_iter_order (&start, &end);
/* find bounding subregions */
start_node = find_nearest_subregion (region, &start, NULL, FALSE, FALSE);
end_node = find_nearest_subregion (region, &end, start_node, TRUE, FALSE);
/* easy case first */
if (start_node == NULL || end_node == NULL || end_node == start_node->prev)
return;
/* deal with the start point */
start_is_outside = end_is_outside = FALSE;
sr = start_node->data;
ctk_text_buffer_get_iter_at_mark (region->buffer, &sr_start_iter, sr->start);
ctk_text_buffer_get_iter_at_mark (region->buffer, &sr_end_iter, sr->end);
if (ctk_text_iter_in_range (&start, &sr_start_iter, &sr_end_iter) &&
!ctk_text_iter_equal (&start, &sr_start_iter)) {
/* the starting point is inside the first subregion */
if (ctk_text_iter_in_range (&end, &sr_start_iter, &sr_end_iter) &&
!ctk_text_iter_equal (&end, &sr_end_iter)) {
/* the ending point is also inside the first
subregion: we need to split */
Subregion *new_sr = g_new0 (Subregion, 1);
new_sr->end = sr->end;
new_sr->start = ctk_text_buffer_create_mark (region->buffer,
NULL, &end, TRUE);
start_node = g_list_insert_before (start_node, start_node->next, new_sr);<--- Variable 'start_node' is assigned a value that is never used.
sr->end = ctk_text_buffer_create_mark (region->buffer,
NULL, &start, FALSE);
/* no further processing needed */
DEBUG (g_message ("subregion splitted"));
return;
} else {
/* the ending point is outside, so just move
the end of the subregion to the starting point */
ctk_text_buffer_move_mark (region->buffer, sr->end, &start);
}
} else {
/* the starting point is outside (and so to the left)
of the first subregion */
DEBUG (g_message ("start is outside"));
start_is_outside = TRUE;
}
/* deal with the end point */
if (start_node != end_node) {
sr = end_node->data;
ctk_text_buffer_get_iter_at_mark (region->buffer, &sr_start_iter, sr->start);
ctk_text_buffer_get_iter_at_mark (region->buffer, &sr_end_iter, sr->end);
}
if (ctk_text_iter_in_range (&end, &sr_start_iter, &sr_end_iter) &&
!ctk_text_iter_equal (&end, &sr_end_iter)) {
/* ending point is inside, move the start mark */
ctk_text_buffer_move_mark (region->buffer, sr->start, &end);
} else {
end_is_outside = TRUE;
DEBUG (g_message ("end is outside"));
}
/* finally remove any intermediate subregions */
done = FALSE;
node = start_node;
while (!done) {
if (node == end_node)
/* we are done, exit in the next iteration */
done = TRUE;
if ((node == start_node && !start_is_outside) ||
(node == end_node && !end_is_outside)) {
/* skip starting or ending node */
node = node->next;
} else {
GList *l = node->next;
sr = node->data;
ctk_text_buffer_delete_mark (region->buffer, sr->start);
ctk_text_buffer_delete_mark (region->buffer, sr->end);
g_free (sr);
region->subregions = g_list_delete_link (region->subregions,
node);
node = l;
}
}
++region->time_stamp;
DEBUG (lapiz_text_region_debug_print (region));
/* now get rid of empty subregions */
lapiz_text_region_clear_zero_length_subregions (region);
DEBUG (lapiz_text_region_debug_print (region));
}
gint
lapiz_text_region_subregions (LapizTextRegion *region)
{
g_return_val_if_fail (region != NULL, 0);
return g_list_length (region->subregions);
}
gboolean
lapiz_text_region_nth_subregion (LapizTextRegion *region,
guint subregion,
CtkTextIter *start,
CtkTextIter *end)
{
Subregion *sr;
g_return_val_if_fail (region != NULL, FALSE);
sr = g_list_nth_data (region->subregions, subregion);
if (sr == NULL)
return FALSE;
if (start)
ctk_text_buffer_get_iter_at_mark (region->buffer, start, sr->start);
if (end)
ctk_text_buffer_get_iter_at_mark (region->buffer, end, sr->end);
return TRUE;
}
LapizTextRegion *
lapiz_text_region_intersect (LapizTextRegion *region,
const CtkTextIter *_start,
const CtkTextIter *_end)
{
GList *start_node, *end_node, *node;<--- Variable 'end_node' can be declared as pointer to const
CtkTextIter sr_start_iter, sr_end_iter;
Subregion *sr, *new_sr;
gboolean done;
LapizTextRegion *new_region;
CtkTextIter start, end;
g_return_val_if_fail (region != NULL && _start != NULL && _end != NULL, NULL);
start = *_start;
end = *_end;
ctk_text_iter_order (&start, &end);
/* find bounding subregions */
start_node = find_nearest_subregion (region, &start, NULL, FALSE, FALSE);
end_node = find_nearest_subregion (region, &end, start_node, TRUE, FALSE);
/* easy case first */
if (start_node == NULL || end_node == NULL || end_node == start_node->prev)
return NULL;
new_region = lapiz_text_region_new (region->buffer);
done = FALSE;
sr = start_node->data;
ctk_text_buffer_get_iter_at_mark (region->buffer, &sr_start_iter, sr->start);
ctk_text_buffer_get_iter_at_mark (region->buffer, &sr_end_iter, sr->end);
/* starting node */
if (ctk_text_iter_in_range (&start, &sr_start_iter, &sr_end_iter)) {
new_sr = g_new0 (Subregion, 1);
new_region->subregions = g_list_prepend (new_region->subregions, new_sr);
new_sr->start = ctk_text_buffer_create_mark (new_region->buffer, NULL,
&start, TRUE);
if (start_node == end_node) {
/* things will finish shortly */
done = TRUE;
if (ctk_text_iter_in_range (&end, &sr_start_iter, &sr_end_iter))
new_sr->end = ctk_text_buffer_create_mark (new_region->buffer,
NULL, &end, FALSE);
else
new_sr->end = ctk_text_buffer_create_mark (new_region->buffer,
NULL, &sr_end_iter,
FALSE);
} else {
new_sr->end = ctk_text_buffer_create_mark (new_region->buffer, NULL,
&sr_end_iter, FALSE);
}
node = start_node->next;
} else {
/* start should be the same as the subregion, so copy it in the loop */
node = start_node;
}
if (!done) {
while (node != end_node) {
/* copy intermediate subregions verbatim */
sr = node->data;
ctk_text_buffer_get_iter_at_mark (region->buffer, &sr_start_iter,
sr->start);
ctk_text_buffer_get_iter_at_mark (region->buffer, &sr_end_iter, sr->end);
new_sr = g_new0 (Subregion, 1);
new_region->subregions = g_list_prepend (new_region->subregions, new_sr);
new_sr->start = ctk_text_buffer_create_mark (new_region->buffer, NULL,
&sr_start_iter, TRUE);
new_sr->end = ctk_text_buffer_create_mark (new_region->buffer, NULL,
&sr_end_iter, FALSE);
/* next node */
node = node->next;
}
/* ending node */
sr = node->data;
ctk_text_buffer_get_iter_at_mark (region->buffer, &sr_start_iter, sr->start);
ctk_text_buffer_get_iter_at_mark (region->buffer, &sr_end_iter, sr->end);
new_sr = g_new0 (Subregion, 1);
new_region->subregions = g_list_prepend (new_region->subregions, new_sr);
new_sr->start = ctk_text_buffer_create_mark (new_region->buffer, NULL,
&sr_start_iter, TRUE);
if (ctk_text_iter_in_range (&end, &sr_start_iter, &sr_end_iter))
new_sr->end = ctk_text_buffer_create_mark (new_region->buffer, NULL,
&end, FALSE);
else
new_sr->end = ctk_text_buffer_create_mark (new_region->buffer, NULL,
&sr_end_iter, FALSE);
}
new_region->subregions = g_list_reverse (new_region->subregions);
return new_region;
}
static gboolean
check_iterator (LapizTextRegionIteratorReal *real)<--- Parameter 'real' can be declared as pointer to const
{
if ((real->region == NULL) ||
(real->region_time_stamp != real->region->time_stamp))
{
g_warning("Invalid iterator: either the iterator "
"is uninitialized, or the region "
"has been modified since the iterator "
"was created.");
return FALSE;
}
return TRUE;
}
void
lapiz_text_region_get_iterator (LapizTextRegion *region,
LapizTextRegionIterator *iter,
guint start)
{
LapizTextRegionIteratorReal *real;
g_return_if_fail (region != NULL);
g_return_if_fail (iter != NULL);
real = (LapizTextRegionIteratorReal *)iter;
/* region->subregions may be NULL, -> end iter */
real->region = region;
real->subregions = g_list_nth (region->subregions, start);
real->region_time_stamp = region->time_stamp;
}
gboolean
lapiz_text_region_iterator_is_end (LapizTextRegionIterator *iter)
{
LapizTextRegionIteratorReal *real;
g_return_val_if_fail (iter != NULL, FALSE);
real = (LapizTextRegionIteratorReal *)iter;
g_return_val_if_fail (check_iterator (real), FALSE);
return (real->subregions == NULL);
}
gboolean
lapiz_text_region_iterator_next (LapizTextRegionIterator *iter)
{
LapizTextRegionIteratorReal *real;
g_return_val_if_fail (iter != NULL, FALSE);
real = (LapizTextRegionIteratorReal *)iter;
g_return_val_if_fail (check_iterator (real), FALSE);
if (real->subregions != NULL) {
real->subregions = g_list_next (real->subregions);
return TRUE;
}
else
return FALSE;
}
void
lapiz_text_region_iterator_get_subregion (LapizTextRegionIterator *iter,
CtkTextIter *start,
CtkTextIter *end)
{
LapizTextRegionIteratorReal *real;
Subregion *sr;
g_return_if_fail (iter != NULL);
real = (LapizTextRegionIteratorReal *)iter;
g_return_if_fail (check_iterator (real));
g_return_if_fail (real->subregions != NULL);
sr = (Subregion*)real->subregions->data;
g_return_if_fail (sr != NULL);
if (start)
ctk_text_buffer_get_iter_at_mark (real->region->buffer, start, sr->start);
if (end)
ctk_text_buffer_get_iter_at_mark (real->region->buffer, end, sr->end);
}
void
lapiz_text_region_debug_print (LapizTextRegion *region)
{
GList *l;
g_return_if_fail (region != NULL);
g_print ("Subregions: ");
l = region->subregions;
while (l) {
Subregion *sr = l->data;
CtkTextIter iter1, iter2;
ctk_text_buffer_get_iter_at_mark (region->buffer, &iter1, sr->start);
ctk_text_buffer_get_iter_at_mark (region->buffer, &iter2, sr->end);
g_print ("%d-%d ", ctk_text_iter_get_offset (&iter1),
ctk_text_iter_get_offset (&iter2));
l = l->next;
}
g_print ("\n");
}
|