forked from gwsw/less
-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.c
2432 lines (2252 loc) · 55.8 KB
/
search.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
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
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 1984-2024 Mark Nudelman
*
* You may distribute under the terms of either the GNU General Public
* License or the Less License, as specified in the README file.
*
* For more information, see the README file.
*/
/*
* Routines to search a file for a pattern.
*/
#include "less.h"
#include "position.h"
#include "charset.h"
#define MINPOS(a,b) (((a) < (b)) ? (a) : (b))
#define MAXPOS(a,b) (((a) > (b)) ? (a) : (b))
extern int sigs;
extern int how_search;
extern int caseless;
extern int linenums;
extern int jump_sline;
extern int bs_mode;
extern int proc_backspace;
extern int proc_return;
extern int ctldisp;
extern int status_col;
extern void *ml_search;
extern POSITION start_attnpos;
extern POSITION end_attnpos;
extern int utf_mode;
extern int sc_width;
extern int sc_height;
extern int hshift;
extern int match_shift;
extern int nosearch_header_lines;
extern int nosearch_header_cols;
extern int header_lines;
extern int header_cols;
extern LWCHAR rscroll_char;
#if HILITE_SEARCH
extern int hilite_search;
extern size_t size_linebuf;
extern lbool squished;
extern int can_goto_line;
extern lbool no_eof_bell;
static lbool hide_hilite;
static POSITION prep_startpos;
static POSITION prep_endpos;
public POSITION header_start_pos = NULL_POSITION;
static POSITION header_end_pos;
public lbool search_wrapped = FALSE;
#if OSC8_LINK
public POSITION osc8_linepos = NULL_POSITION;
public POSITION osc8_match_start = NULL_POSITION;
public POSITION osc8_match_end = NULL_POSITION;
public POSITION osc8_params_start = NULL_POSITION;
public POSITION osc8_params_end = NULL_POSITION;
public POSITION osc8_uri_start = NULL_POSITION;
public POSITION osc8_uri_end = NULL_POSITION;
public POSITION osc8_text_start = NULL_POSITION;
public POSITION osc8_text_end = NULL_POSITION;
char *osc8_path = NULL;
char *osc8_uri = NULL;
constant char *osc8_search_param = NULL;
#endif
/*
* Structures for maintaining a set of ranges for hilites and filtered-out
* lines. Each range is stored as a node within a red-black tree, and we
* try to extend existing ranges (without creating overlaps) rather than
* create new nodes if possible. We remember the last node found by a
* search for constant-time lookup if the next search is near enough to
* the previous. To aid that, we overlay a secondary doubly-linked list
* on top of the red-black tree so we can find the preceding/succeeding
* nodes also in constant time.
*
* Each node is allocated from a series of pools, each pool double the size
* of the previous (for amortised constant time allocation). Since our only
* tree operations are clear and node insertion, not node removal, we don't
* need to maintain a usage bitmap or freelist and can just return nodes
* from the pool in-order until capacity is reached.
*/
struct hilite
{
POSITION hl_startpos;
POSITION hl_endpos;
int hl_attr;
};
struct hilite_node
{
struct hilite_node *parent;
struct hilite_node *left;
struct hilite_node *right;
struct hilite_node *prev;
struct hilite_node *next;
int red;
struct hilite r;
};
struct hilite_storage
{
size_t capacity;
size_t used;
struct hilite_storage *next;
struct hilite_node *nodes;
};
struct hilite_tree
{
struct hilite_storage *first;
struct hilite_storage *current;
struct hilite_node *root;
struct hilite_node *lookaside;
};
#define HILITE_INITIALIZER() { NULL, NULL, NULL, NULL }
#define HILITE_LOOKASIDE_STEPS 2
static struct hilite_tree hilite_anchor = HILITE_INITIALIZER();
static struct hilite_tree filter_anchor = HILITE_INITIALIZER();
static struct pattern_info *filter_infos = NULL;
#endif
/*
* These are the static variables that represent the "remembered"
* search pattern and filter pattern.
*/
struct pattern_info {
PATTERN_TYPE compiled;
char* text;
int search_type;
lbool is_ucase_pattern;
struct pattern_info *next;
};
#if NO_REGEX
#define info_compiled(info) ((void*)0)
#else
#define info_compiled(info) ((info)->compiled)
#endif
static struct pattern_info search_info;
public int is_caseless;
/*
* Are there any uppercase letters in this string?
*/
static lbool is_ucase(constant char *str)
{
constant char *str_end = str + strlen(str);
LWCHAR ch;
while (str < str_end)
{
ch = step_charc(&str, +1, str_end);
if (IS_UPPER(ch))
return (TRUE);
}
return (FALSE);
}
/*
* Discard a saved pattern.
*/
static void clear_pattern(struct pattern_info *info)
{
if (info->text != NULL)
free(info->text);
info->text = NULL;
#if !NO_REGEX
uncompile_pattern(&info->compiled);
#endif
}
/*
* Compile and save a search pattern.
*/
static int set_pattern(struct pattern_info *info, constant char *pattern, int search_type, int show_error)
{
/*
* Ignore case if -I is set OR
* -i is set AND the pattern is all lowercase.
*/
info->is_ucase_pattern = (pattern == NULL) ? FALSE : is_ucase(pattern);
is_caseless = (info->is_ucase_pattern && caseless != OPT_ONPLUS) ? 0 : caseless;
#if !NO_REGEX
if (pattern == NULL)
SET_NULL_PATTERN(info->compiled);
else if (compile_pattern(pattern, search_type, show_error, &info->compiled) < 0)
return -1;
#endif
/* Pattern compiled successfully; save the text too. */
if (info->text != NULL)
free(info->text);
info->text = NULL;
if (pattern != NULL)
{
info->text = (char *) ecalloc(1, strlen(pattern)+1);
strcpy(info->text, pattern);
}
info->search_type = search_type;
return 0;
}
/*
* Initialize saved pattern to nothing.
*/
static void init_pattern(struct pattern_info *info)
{
SET_NULL_PATTERN(info->compiled);
info->text = NULL;
info->search_type = 0;
info->next = NULL;
}
/*
* Initialize search variables.
*/
public void init_search(void)
{
init_pattern(&search_info);
}
/*
* Determine which text conversions to perform before pattern matching.
*/
public int get_cvt_ops(int search_type)
{
int ops = 0;
if (is_caseless && (!re_handles_caseless || (search_type & SRCH_NO_REGEX)))
ops |= CVT_TO_LC;
if (proc_backspace == OPT_ON || (bs_mode == BS_SPECIAL && proc_backspace == OPT_OFF))
ops |= CVT_BS;
if (proc_return == OPT_ON || (bs_mode != BS_CONTROL && proc_backspace == OPT_OFF))
ops |= CVT_CRLF;
if (ctldisp == OPT_ONPLUS)
ops |= CVT_ANSI;
return (ops);
}
/*
* Is there a previous (remembered) search pattern?
*/
static int prev_pattern(struct pattern_info *info)
{
#if !NO_REGEX
if ((info->search_type & SRCH_NO_REGEX) == 0)
return (!is_null_pattern(info->compiled));
#endif
return (info->text != NULL);
}
#if HILITE_SEARCH
/*
* Repaint the hilites currently displayed on the screen.
* Repaint each line which contains highlighted text.
* If on==0, force all hilites off.
*/
public void repaint_hilite(lbool on)
{
int sindex;
POSITION pos;
lbool save_hide_hilite;
if (squished)
repaint();
save_hide_hilite = hide_hilite;
if (!on)
{
if (hide_hilite)
return;
hide_hilite = TRUE;
}
if (!can_goto_line)
{
repaint();
hide_hilite = save_hide_hilite;
return;
}
for (sindex = TOP; sindex < TOP + sc_height-1; sindex++)
{
pos = position(sindex);
if (pos == NULL_POSITION)
continue;
(void) forw_line(pos);
goto_line(sindex);
clear_eol();
put_line();
}
overlay_header();
lower_left();
hide_hilite = save_hide_hilite;
}
#endif
/*
* Clear the attn hilite.
*/
public void clear_attn(void)
{
#if HILITE_SEARCH
int sindex;
POSITION old_start_attnpos;
POSITION old_end_attnpos;
POSITION pos;
POSITION epos;
int moved = 0;
if (start_attnpos == NULL_POSITION)
return;
old_start_attnpos = start_attnpos;
old_end_attnpos = end_attnpos;
start_attnpos = end_attnpos = NULL_POSITION;
if (!can_goto_line)
{
repaint();
return;
}
if (squished)
repaint();
for (sindex = TOP; sindex < TOP + sc_height-1; sindex++)
{
pos = position(sindex);
if (pos == NULL_POSITION)
continue;
epos = position(sindex+1);
if (pos <= old_end_attnpos &&
(epos == NULL_POSITION || epos > old_start_attnpos))
{
(void) forw_line(pos);
goto_line(sindex);
clear_eol();
put_line();
moved = 1;
}
}
if (overlay_header())
moved = 1;
if (moved)
lower_left();
#endif
}
/*
* Toggle or clear search string highlighting.
*/
public void undo_search(lbool clear)
{
clear_pattern(&search_info);
#if OSC8_LINK
osc8_linepos = NULL_POSITION;
#endif
#if HILITE_SEARCH
if (clear)
{
clr_hilite();
} else
{
if (hilite_anchor.first == NULL)
{
error("No previous regular expression", NULL_PARG);
return;
}
hide_hilite = !hide_hilite;
}
repaint_hilite(TRUE);
#endif
}
#if HILITE_SEARCH
/*
* Clear the hilite list.
*/
public void clr_hlist(struct hilite_tree *anchor)
{
struct hilite_storage *hls;
struct hilite_storage *nexthls;
for (hls = anchor->first; hls != NULL; hls = nexthls)
{
nexthls = hls->next;
free((void*)hls->nodes);
free((void*)hls);
}
anchor->first = NULL;
anchor->current = NULL;
anchor->root = NULL;
anchor->lookaside = NULL;
prep_startpos = prep_endpos = NULL_POSITION;
}
public void clr_hilite(void)
{
clr_hlist(&hilite_anchor);
}
public void clr_filter(void)
{
clr_hlist(&filter_anchor);
}
/*
* Find the node covering pos, or the node after it if no node covers it,
* or return NULL if pos is after the last range. Remember the found node,
* to speed up subsequent searches for the same or similar positions (if
* we return NULL, remember the last node.)
*/
static struct hilite_node* hlist_find(struct hilite_tree *anchor, POSITION pos)
{
struct hilite_node *n, *m;
if (anchor->lookaside)
{
int steps = 0;
int hit = 0;
n = anchor->lookaside;
for (;;)
{
if (pos < n->r.hl_endpos)
{
if (n->prev == NULL || pos >= n->prev->r.hl_endpos)
{
hit = 1;
break;
}
} else if (n->next == NULL)
{
n = NULL;
hit = 1;
break;
}
/*
* If we don't find the right node within a small
* distance, don't keep doing a linear search!
*/
if (steps >= HILITE_LOOKASIDE_STEPS)
break;
steps++;
if (pos < n->r.hl_endpos)
anchor->lookaside = n = n->prev;
else
anchor->lookaside = n = n->next;
}
if (hit)
return n;
}
n = anchor->root;
m = NULL;
while (n != NULL)
{
if (pos < n->r.hl_startpos)
{
if (n->left != NULL)
{
m = n;
n = n->left;
continue;
}
break;
}
if (pos >= n->r.hl_endpos)
{
if (n->right != NULL)
{
n = n->right;
continue;
}
if (m != NULL)
{
n = m;
} else
{
m = n;
n = NULL;
}
}
break;
}
if (n != NULL)
anchor->lookaside = n;
else if (m != NULL)
anchor->lookaside = m;
return n;
}
/*
* Should any characters in a specified range be highlighted?
*/
static int hilited_range_attr(POSITION pos, POSITION epos)
{
struct hilite_node *n = hlist_find(&hilite_anchor, pos);
if (n == NULL)
return 0;
if (epos != NULL_POSITION && epos <= n->r.hl_startpos)
return 0;
return n->r.hl_attr;
}
/*
* Set header parameters.
*/
public void set_header(POSITION pos)
{
header_start_pos = (header_lines == 0) ? NULL_POSITION : pos;
if (header_start_pos != NULL_POSITION)
{
int ln;
for (ln = 0; ln < header_lines; ++ln)
{
pos = forw_raw_line(pos, NULL, NULL);
if (pos == NULL_POSITION) break;
}
header_end_pos = pos;
}
}
/*
* Is a position within the header lines?
*/
static int pos_in_header(POSITION pos)
{
return (header_start_pos != NULL_POSITION &&
pos >= header_start_pos && pos < header_end_pos);
}
/*
* Is a line "filtered" -- that is, should it be hidden?
*/
public lbool is_filtered(POSITION pos)
{
struct hilite_node *n;
if (ch_getflags() & CH_HELPFILE)
return (FALSE);
if (pos_in_header(pos))
return (FALSE);
n = hlist_find(&filter_anchor, pos);
return (n != NULL && pos >= n->r.hl_startpos);
}
/*
* If pos is hidden, return the next position which isn't, otherwise
* just return pos.
*/
public POSITION next_unfiltered(POSITION pos)
{
struct hilite_node *n;
if (ch_getflags() & CH_HELPFILE)
return (pos);
if (pos_in_header(pos))
return (pos);
n = hlist_find(&filter_anchor, pos);
while (n != NULL && pos >= n->r.hl_startpos)
{
pos = n->r.hl_endpos;
n = n->next;
}
return (pos);
}
/*
* If pos is hidden, return the previous position which isn't or 0 if
* we're filtered right to the beginning, otherwise just return pos.
*/
public POSITION prev_unfiltered(POSITION pos)
{
struct hilite_node *n;
if (ch_getflags() & CH_HELPFILE)
return (pos);
if (pos_in_header(pos))
return (pos);
n = hlist_find(&filter_anchor, pos);
while (n != NULL && pos >= n->r.hl_startpos)
{
pos = n->r.hl_startpos;
if (pos == 0)
break;
pos--;
n = n->prev;
}
return (pos);
}
/*
* Set the hshift for the line starting at line_pos so that the string
* between start_off and end_off is visible on the screen.
*/
static void shift_visible(POSITION line_pos, size_t start_off, size_t end_off)
{
POSITION start_pos = line_pos + start_off;
POSITION end_pos = line_pos + end_off;
int start_col = col_from_pos(line_pos, start_pos, NULL_POSITION, -1);
int end_col = col_from_pos(line_pos, end_pos, start_pos, start_col);
int swidth = sc_width - line_pfx_width() - (rscroll_char ? 1 : 0);
int new_hshift;
if (start_col < 0 || end_col < 0)
return;
if (end_col < swidth) /* whole string is in first screen */
new_hshift = 0;
else if (start_col > hshift && end_col < hshift + swidth)
new_hshift = hshift; /* already visible; leave hshift unchanged */
else
{
int eol_col = col_from_pos(line_pos, NULL_POSITION, end_pos, end_col) - swidth;
if (start_col >= eol_col) /* whole string is in last screen */
new_hshift = eol_col;
else /* shift it to column match_shift */
new_hshift = (start_col < match_shift) ? 0 : start_col - match_shift;
}
if (new_hshift != hshift)
{
hshift = new_hshift;
screen_trashed();
}
}
/*
* Should any characters in a specified range be highlighted?
* If nohide is nonzero, don't consider hide_hilite.
*/
public int is_hilited_attr(POSITION pos, POSITION epos, int nohide, int *p_matches)
{
int attr;
if (p_matches != NULL)
*p_matches = 0;
if (!status_col &&
start_attnpos != NULL_POSITION &&
pos <= end_attnpos &&
(epos == NULL_POSITION || epos > start_attnpos))
/*
* The attn line overlaps this range.
*/
return (AT_HILITE|AT_COLOR_ATTN);
#if OSC8_LINK
if (osc8_linepos != NULL_POSITION &&
pos <= osc8_text_end && (epos == NULL_POSITION || epos > osc8_text_start))
return (AT_HILITE|AT_COLOR_SEARCH);
#endif
attr = hilited_range_attr(pos, epos);
if (attr == 0)
return (0);
if (p_matches == NULL)
/*
* Kinda kludgy way to recognize that caller is checking for
* hilite in status column. In this case we want to return
* hilite status even if hiliting is disabled or hidden.
*/
return (attr);
/*
* Report matches, even if we're hiding highlights.
*/
*p_matches = 1;
if (hilite_search == 0)
/*
* Not doing highlighting.
*/
return (0);
if (!nohide && hide_hilite)
/*
* Highlighting is hidden.
*/
return (0);
return (attr);
}
/*
* Tree node storage: get the current block of nodes if it has spare
* capacity, or create a new one if not.
*/
static struct hilite_storage * hlist_getstorage(struct hilite_tree *anchor)
{
size_t capacity = 1;
struct hilite_storage *s;
if (anchor->current)
{
if (anchor->current->used < anchor->current->capacity)
return anchor->current;
capacity = anchor->current->capacity * 2;
}
s = (struct hilite_storage *) ecalloc(1, sizeof(struct hilite_storage));
s->nodes = (struct hilite_node *) ecalloc(capacity, sizeof(struct hilite_node));
s->capacity = capacity;
s->used = 0;
s->next = NULL;
if (anchor->current)
anchor->current->next = s;
else
anchor->first = s;
anchor->current = s;
return s;
}
/*
* Tree node storage: retrieve a new empty node to be inserted into the
* tree.
*/
static struct hilite_node * hlist_getnode(struct hilite_tree *anchor)
{
struct hilite_storage *s = hlist_getstorage(anchor);
return &s->nodes[s->used++];
}
/*
* Rotate the tree left around a pivot node.
*/
static void hlist_rotate_left(struct hilite_tree *anchor, struct hilite_node *n)
{
struct hilite_node *np = n->parent;
struct hilite_node *nr = n->right;
struct hilite_node *nrl = n->right->left;
if (np != NULL)
{
if (n == np->left)
np->left = nr;
else
np->right = nr;
} else
{
anchor->root = nr;
}
nr->left = n;
n->right = nrl;
nr->parent = np;
n->parent = nr;
if (nrl != NULL)
nrl->parent = n;
}
/*
* Rotate the tree right around a pivot node.
*/
static void hlist_rotate_right(struct hilite_tree *anchor, struct hilite_node *n)
{
struct hilite_node *np = n->parent;
struct hilite_node *nl = n->left;
struct hilite_node *nlr = n->left->right;
if (np != NULL)
{
if (n == np->right)
np->right = nl;
else
np->left = nl;
} else
{
anchor->root = nl;
}
nl->right = n;
n->left = nlr;
nl->parent = np;
n->parent = nl;
if (nlr != NULL)
nlr->parent = n;
}
/*
* Add a new hilite to a hilite list.
*/
static void add_hilite(struct hilite_tree *anchor, struct hilite *hl)
{
struct hilite_node *p, *n, *u;
/* Ignore empty ranges. */
if (hl->hl_startpos >= hl->hl_endpos)
return;
p = anchor->root;
/* Inserting the very first node is trivial. */
if (p == NULL)
{
n = hlist_getnode(anchor);
n->r = *hl;
anchor->root = n;
anchor->lookaside = n;
return;
}
/*
* Find our insertion point. If we come across any overlapping
* or adjoining existing ranges, shrink our range and discard
* if it become empty.
*/
for (;;)
{
if (hl->hl_startpos < p->r.hl_startpos)
{
if (hl->hl_endpos > p->r.hl_startpos && hl->hl_attr == p->r.hl_attr)
hl->hl_endpos = p->r.hl_startpos;
if (p->left != NULL)
{
p = p->left;
continue;
}
break;
}
if (hl->hl_startpos < p->r.hl_endpos && hl->hl_attr == p->r.hl_attr) {
hl->hl_startpos = p->r.hl_endpos;
if (hl->hl_startpos >= hl->hl_endpos)
return;
}
if (p->right != NULL)
{
p = p->right;
continue;
}
break;
}
/*
* Now we're at the right leaf, again check for contiguous ranges
* and extend the existing node if possible to avoid the
* insertion. Otherwise insert a new node at the leaf.
*/
if (hl->hl_startpos < p->r.hl_startpos) {
if (hl->hl_attr == p->r.hl_attr)
{
if (hl->hl_endpos == p->r.hl_startpos)
{
p->r.hl_startpos = hl->hl_startpos;
return;
}
if (p->prev != NULL && p->prev->r.hl_endpos == hl->hl_startpos)
{
p->prev->r.hl_endpos = hl->hl_endpos;
return;
}
}
p->left = n = hlist_getnode(anchor);
n->next = p;
if (p->prev != NULL)
{
n->prev = p->prev;
p->prev->next = n;
}
p->prev = n;
} else {
if (hl->hl_attr == p->r.hl_attr)
{
if (p->r.hl_endpos == hl->hl_startpos)
{
p->r.hl_endpos = hl->hl_endpos;
return;
}
if (p->next != NULL && hl->hl_endpos == p->next->r.hl_startpos) {
p->next->r.hl_startpos = hl->hl_startpos;
return;
}
}
p->right = n = hlist_getnode(anchor);
n->prev = p;
if (p->next != NULL)
{
n->next = p->next;
p->next->prev = n;
}
p->next = n;
}
n->parent = p;
n->red = 1;
n->r = *hl;
/*
* The tree is in the correct order and covers the right ranges
* now, but may have become unbalanced. Rebalance it using the
* standard red-black tree constraints and operations.
*/
for (;;)
{
/* case 1 - current is root, root is always black */
if (n->parent == NULL)
{
n->red = 0;
break;
}
/* case 2 - parent is black, we can always be red */
if (!n->parent->red)
break;
/*
* constraint: because the root must be black, if our
* parent is red it cannot be the root therefore we must
* have a grandparent
*/
/*
* case 3 - parent and uncle are red, repaint them black,
* the grandparent red, and start again at the grandparent.
*/
u = n->parent->parent->left;
if (n->parent == u)
u = n->parent->parent->right;
if (u != NULL && u->red)
{
n->parent->red = 0;
u->red = 0;
n = n->parent->parent;
n->red = 1;
continue;
}
/*
* case 4 - parent is red but uncle is black, parent and
* grandparent on opposite sides. We need to start
* changing the structure now. This and case 5 will shorten
* our branch and lengthen the sibling, between them
* restoring balance.
*/
if (n == n->parent->right &&
n->parent == n->parent->parent->left)
{
hlist_rotate_left(anchor, n->parent);
n = n->left;
} else if (n == n->parent->left &&
n->parent == n->parent->parent->right)
{
hlist_rotate_right(anchor, n->parent);
n = n->right;
}
/*
* case 5 - parent is red but uncle is black, parent and
* grandparent on same side
*/
n->parent->red = 0;
n->parent->parent->red = 1;
if (n == n->parent->left)
hlist_rotate_right(anchor, n->parent->parent);
else
hlist_rotate_left(anchor, n->parent->parent);
break;
}
}
/*
* Highlight every character in a range of displayed characters.
*/
static void create_hilites(POSITION linepos, constant char *line, constant char *sp, constant char *ep, int attr, int *chpos)
{
size_t start_index = ptr_diff(sp, line); /*{{type-issue}}*/
size_t end_index = ptr_diff(ep, line);
struct hilite hl;
size_t i;
/* Start the first hilite. */
hl.hl_startpos = linepos + chpos[start_index];
hl.hl_attr = attr;
/*
* Step through the displayed chars.
* If the source position (before cvt) of the char is one more
* than the source pos of the previous char (the usual case),
* just increase the size of the current hilite by one.
* Otherwise (there are backspaces or something involved),
* finish the current hilite and start a new one.
*/
for (i = start_index+1; i <= end_index; i++)
{
if (chpos[i] != chpos[i-1] + 1 || i == end_index)