-
Notifications
You must be signed in to change notification settings - Fork 0
/
core-mark.c
1293 lines (1167 loc) · 31.4 KB
/
core-mark.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 Neil Brown ©2015-2023 <neil@brown.name>
* May be distributed under terms of GPLv2 - see file:COPYING
*
* Marks and Points.
*
* A 'Mark' is a reference to a location in a text. The location
* is between two characters and remains there while characters
* are added or removed before or after.
* All marks are linked together in text order and are assigned
* sparse ordered numbers so that it is easy to determine the
* relative order of two points.
*
* Each mark is optionally in two lists. There is one list that
* contains all marks on a given text, and arbitrary other lists
* that contain only select marks. A given mark can only be in one
* of these extra lists - except for points which are described below.
*
* A mark has a list of attributes and a pointer to a handler
* which can be called in various circumstances to customise
* handling of different sections of text.
*
* A 'point' is a special mark which identifies a place where
* things can happen. Text can be added or removed, marks can
* be created or destroyed, only at a 'point'.
* A 'point' is on all lists. This allows nearby marks on any list to be
* found quickly.
*
* As the 'group' lists can hold either marks or points, and with a
* different anchor in each, we use 'tlist_head'. Pointers in a
* tlist use the bottom 2 bits to store a type. We only have
* two types: mark or point. When we find a point we know which of the
* various lists because we know the index of the list we are walking.
*
* The text buffer can only be modified at a point.
* Prior to deleting text, all marks/points must be moved off the removed
* section. After adding text ... something.
*
* A mark or point can be deleted at any time with impunity.
* A point can be duplicated, or can fork a mark. When this happens
* we might need to re-assign some seq numbers to keep them sparse.
* A point can move forward or backward, or can jump to a mark.
* When that happens, various lists might need to be adjusted and
* seq number might need to be reassigned.
*
* There can only be one (non-borrowed) reference to a mark. So
* a second reference is wanted, the mark must be duplicated.
* How these references are stored is not yet clear. Maybe that is
* up to whatever module uses it.
*
* Each mark has a 'type' identifying which group it is of.
* Type -1 is points. They aren't like other groups.
* Other types(groups) can be defined as needed
* The number is an index into text->groups and point->lists.
*
* This is distinct from the type used by the tlist to differentiate
* points the location of the tlist_head. There 'GRP_MARK' is the 'group'
* in each mark of that group, and 'GRP_LIST' is the 'lists[n]' in each
* point. GRP_HEAD is the head of the list.
*/
#include <unistd.h>
#include <stdlib.h>
#include <memory.h>
#define DOC_DATA_TYPE struct doc
#include "core.h"
#include "core-pane.h"
#include "internal.h"
#include "misc.h"
static MEMPOOL(mark);
static struct mark *vmark_or_point_next(struct mark *m safe, int view);
static struct mark *vmark_or_point_prev(struct mark *m safe, int view);
/* seq numbers added to the end are given a gap of 128.
* seq numbers at other locations are placed at mean of before and after.
* If there is no room, seq add 256 to the 'next' seq, 255 to the one
* after etc until we find a seq already above the target, or reach
* gap size of 64. In later case we continue with fixed gap size.
*/
static void assign_seq(struct mark *m safe)
{
struct mark *p, *n;
int gap = 256;
n = mark_next(m);
p = mark_prev(m);
if (!n && !p) {
/* First mark: start in the middle */
m->seq = INT_MAX/2;
return;
}
while (--gap >= 2) {
if (n && p && n->seq - p->seq >= 2) {
/* There is room here, so insert */
m->seq = p->seq + (n->seq - p->seq)/2;
return;
}
if (!n && p->seq < INT_MAX - 1) {
if (INT_MAX - p->seq < gap)
gap = INT_MAX - p->seq;
m->seq = p->seq + gap / 2;
return;
}
if (!p && n->seq > 1) {
if (n->seq < gap)
gap = n->seq;
m->seq = n->seq - gap / 2;
return;
}
/* We are between two marks with no room for a seq number,
* need to reshuffle and try to find space somewhere.
* Try to spread out the current marks more so there is
* 'gap' space between them, but let 'gap' decrease as we
* go to avoid pushing too hard.
*/
if (p && n && p->seq > INT_MAX / 2) {
while (p && n->seq - p->seq < gap) {
/* step back to 'p' and leave 'm' with a larger
* gap.
*/
if (n->seq > gap)
m->seq = n->seq - gap;
else if (n->seq > 0)
m->seq = n->seq - 1;
else
m->seq = 0; /* Ouch! */
n = m;
m = p;
p = mark_prev(p);
if (gap > 4)
gap -= 1;
}
} else if (p) {
while (n && n->seq - p->seq < gap) {
if (INT_MAX - p->seq > gap)
m->seq = p->seq + gap;
else if (p->seq < INT_MAX)
m->seq = p->seq + 1;
else
m->seq = INT_MAX; /* Ouch! */
p = m;
m = n;
n = mark_next(n);
if (gap > 4)
gap -= 1;
}
}
/* NOTE: p->seq may now exceed n->seq, and
* there might be adjacent seq numbers if we hit 0 or INT_MAX.
* These will be fixed up on next loop through.
*/
}
/* We must have scanned all the way to one end, and all the way to the
* other, and found no space, so 4 billion marks. Seems unlikely.
*/
abort();
}
static void point_free(struct mark *p safe)
{
unsigned int i;
struct point_links *lnk = safe_cast p->mdata;
for (i = 0; i < lnk->size; i++)
tlist_del_init(&lnk->lists[i]);
unalloc_buf(p->mdata, sizeof(*lnk) + lnk->size * sizeof(lnk->lists[0]),
mark);
}
void do_mark_free(struct mark *m)
{
unalloc(m, mark);
}
static void mark_refcnt(struct mark *m safe, int inc)
{
struct doc *d = &m->owner->doc;
if (d->refcnt)
d->refcnt(m, inc);
}
void mark_free(struct mark *m)
{
struct pane *owner;
/* mark might have already been freed by the
* pane getting closed.
*/
if (!mark_valid(m))
return;
if (m->viewnum == MARK_POINT)
point_free(m);
ASSERT(m->mdata == NULL);
hlist_del_init(&m->all);
if (m->viewnum != MARK_UNGROUPED)
tlist_del_init(&m->view);
attr_free(&m->attrs);
mark_refcnt(m, -1);
owner = m->owner;
memset(m, 0xff, sizeof(*m));
m->owner = owner;
owner->marks -= 1;
m->viewnum = MARK_UNGROUPED;
editor_delayed_mark_free(m);
}
static void notify_mark_moving(struct mark *m safe, struct mark *m2)
{
if (m->flags & MARK_FLAG_WATCHED) {
m->flags &= ~MARK_FLAG_WATCHED;
pane_notify("mark:moving", m->owner, 0, m);
}
if (m2 && (m2->flags & MARK_FLAG_WATCHED))
pane_notify("mark:arrived", m->owner, 0, m, NULL, 0, m2);
}
void mark_watch(struct mark *m)
{
if (m)
m->flags |= MARK_FLAG_WATCHED;
}
static void mark_ref_copy(struct mark *to safe, struct mark *from safe)
{
if (!mark_valid(to) || !mark_valid(from))
return;
if ((void*)to->owner && to->owner != from->owner) {
LOG("mark_ref_copy given marks with different owners");
return;
}
if (!(void*)to->owner) {
from->owner->marks += 1;
ASSERT(from->owner->marks < 20000000);
}
to->owner = from->owner;
if (to->ref.p == from->ref.p &&
to->ref.i == from->ref.i)
return;
mark_refcnt(to, -1);
to->ref = from->ref;
mark_refcnt(to, 1);
}
static void dup_mark(struct mark *orig safe, struct mark *new safe)
{
hlist_add_after(&orig->all, &new->all);
INIT_TLIST_HEAD(&new->view, GRP_MARK);
new->attrs = NULL;
new->flags = 0;
assign_seq(new);
mark_ref_copy(new, orig);
}
struct mark *do_mark_at_point(struct mark *pt safe, int view)
{
struct mark *ret;
struct point_links *lnk;
if (pt->viewnum != MARK_POINT)
return NULL;
lnk = safe_cast pt->mdata;
alloc(ret, mark);
dup_mark(pt, ret);
ret->viewnum = view;
if (view >= 0)
tlist_add(&ret->view, GRP_MARK, &lnk->lists[view]);
else
INIT_TLIST_HEAD(&ret->view, GRP_MARK);
notify_mark_moving(ret, pt);
return ret;
}
struct mark *mark_at_point(struct pane *p safe, struct mark *pm, int view)
{
return call_ret(mark, "doc:dup-point", p, 0, pm, NULL, view);
}
struct mark *safe point_dup(struct mark *p safe)
{
unsigned int i;
struct point_links *old = safe_cast p->mdata;
struct mark *ret;
struct point_links *lnk;
alloc(ret, mark);
lnk = alloc_buf(sizeof(*lnk) + old->size * sizeof(lnk->lists[0]),
mark);
dup_mark(p, ret);
ret->viewnum = MARK_POINT;
ret->mdata = lnk;
lnk->size = old->size;
lnk->pt = ret;
tlist_add(&ret->view, GRP_MARK, &p->view);
for (i = 0; lnk && i < lnk->size; i++)
if (tlist_empty(&old->lists[i]))
INIT_TLIST_HEAD(&lnk->lists[i], GRP_LIST);
else
tlist_add(&lnk->lists[i], GRP_LIST, &old->lists[i]);
notify_mark_moving(ret, p);
return ret;
}
void points_resize(struct doc *d safe)
{
struct mark *p;
tlist_for_each_entry(p, &d->points, view) {
unsigned int i;
struct point_links *old = safe_cast p->mdata;
struct point_links *new = alloc_buf(sizeof(*new) +
d->nviews *
sizeof(new->lists[0]),
mark);
new->pt = p;
new->size = d->nviews;
p->mdata = new;
for (i = 0; i < old->size; i++) {
tlist_add(&new->lists[i], GRP_LIST, &old->lists[i]);
tlist_del(&old->lists[i]);
}
for (; i < new->size; i++)
INIT_TLIST_HEAD(&new->lists[i], GRP_HEAD);
free(old);
}
}
void points_attach(struct doc *d safe, int view)
{
struct mark *p;
tlist_for_each_entry(p, &d->points, view) {
struct point_links *lnk = safe_cast p->mdata;
tlist_add_tail(&lnk->lists[view], GRP_LIST,
&d->views[view].head);
}
}
struct mark *safe mark_dup(struct mark *m safe)
{
struct mark *ret;
if (!mark_valid(m))
return m;
alloc(ret, mark);
dup_mark(m, ret);
ret->viewnum = MARK_UNGROUPED;
INIT_TLIST_HEAD(&ret->view, GRP_MARK);
notify_mark_moving(ret, m);
return ret;
}
struct mark *safe mark_dup_view(struct mark *m safe)
{
struct mark *ret;
if (!mark_valid(m))
return m;
if (m->viewnum == MARK_POINT)
return point_dup(m);
alloc(ret, mark);
dup_mark(m, ret);
if (m->viewnum == MARK_POINT) abort();
ret->viewnum = m->viewnum;
if (ret->viewnum == MARK_UNGROUPED)
INIT_TLIST_HEAD(&ret->view, GRP_MARK);
else
tlist_add(&ret->view, GRP_MARK, &m->view);
notify_mark_moving(ret, m);
return ret;
}
/* if 'end', move mark after all other marks, else move before all others */
void mark_to_end(struct pane *p safe, struct mark *m safe, int end)
{
struct doc *d = &p->doc;
unsigned int i;
struct point_links *lnk;
if (!mark_valid(m))
return;
ASSERT(m->owner == p);
notify_mark_moving(m, NULL);
hlist_del(&m->all);
if (end) {
if (hlist_empty(&d->marks))
hlist_add_head(&m->all, &d->marks);
else {
struct mark *last = hlist_first_entry(&d->marks,
struct mark, all);
while (last->all.next)
last = hlist_next_entry(last, all);
hlist_add_after(&last->all, &m->all);
}
} else
hlist_add_head(&m->all, &d->marks);
assign_seq(m);
if (m->viewnum == MARK_UNGROUPED)
return;
if (m->viewnum != MARK_POINT) {
tlist_del(&m->view);
if (end)
tlist_add_tail(&m->view, GRP_MARK,
&d->views[m->viewnum].head);
else
tlist_add(&m->view, GRP_MARK,
&d->views[m->viewnum].head);
return;
}
/* MARK_POINT */
tlist_del(&m->view);
if (end)
tlist_add_tail(&m->view, GRP_MARK, &d->points);
else
tlist_add(&m->view, GRP_MARK, &d->points);
lnk = safe_cast m->mdata;
for (i = 0; d->views && i < lnk->size; i++)
if (d->views[i].owner) {
tlist_del(&lnk->lists[i]);
if (end)
tlist_add_tail(&lnk->lists[i], GRP_LIST,
&d->views[i].head);
else
tlist_add(&lnk->lists[i],
GRP_LIST, &d->views[i].head);
}
}
void mark_reset(struct pane *p safe, struct mark *m safe, int end)
{
ASSERT((void*)m->owner == NULL || m->owner == p);
if (!mark_valid(m))
return;
if (!(void*)m->owner) {
p->marks += 1;
ASSERT(p->marks < 20000000);
}
m->owner = p;
pane_call(p, "doc:set-ref", p, !end, m);
}
struct mark *mark_first(struct doc *d safe)
{
if (!hlist_empty(&d->marks))
return hlist_first_entry(&d->marks, struct mark, all);
return NULL;
}
struct mark *mark_next(struct mark *m safe)
{
if (mark_valid(m) && m->all.next)
return hlist_next_entry(m, all);
return NULL;
}
struct mark *mark_prev(struct mark *m safe)
{
if (mark_valid(m) && !HLIST_IS_HEAD(m->all.pprev))
return hlist_prev_entry(m, all);
return NULL;
}
struct mark *doc_new_mark(struct pane *p safe, int view, struct pane *owner)
{
/* FIXME view is >= -1 */
struct mark *ret;
struct doc *d = &p->doc;
if (view >= d->nviews ||
view < MARK_UNGROUPED ||
(view >= 0 && (!d->views || d->views[view].owner != owner))) {
/* Erroneous call, or race with document closing down */
return NULL;
}
alloc(ret, mark);
INIT_HLIST_NODE(&ret->all);
INIT_TLIST_HEAD(&ret->view, GRP_MARK);
ret->viewnum = view;
hlist_add_head(&ret->all, &d->marks);
if (view == MARK_POINT) {
struct point_links *lnk = alloc_buf(sizeof(*lnk) +
d->nviews *
sizeof(lnk->lists[0]),
mark);
int i;
lnk->size = d->nviews;
lnk->pt = ret;
for (i = 0; i < d->nviews; i++)
INIT_TLIST_HEAD(&lnk->lists[i], GRP_LIST);
ret->mdata = lnk;
}
mark_reset(p, ret, 0);
if (hlist_unhashed(&ret->all)) {
/* Document misbehaved, fail gracefully */
mark_free(ret);
return NULL;
}
return ret;
}
/* Movement of points and marks.
*
* Both points and marks can move.
* Marks can move forward or backward one character. They only
* step over other marks if they have to.
* To move a mark to another mark of the same group, or to a
* point, the mark needs to be deleted and a new one created.
* Points can step fore/back like marks. They can jump to another
* point easily but to move to mark they must walk one mark at a time.
*
* Note that 'm' isn't 'safe' - A NULL mark will be step to point
* by the doc redirector.
*/
wint_t do_doc_step(struct pane *p safe, struct mark *m,
int forward, int move)
{
int ret;
static int dodebug = -1;
static int count;
if (dodebug < 0)
dodebug = atoi(getenv("EDLIB_DEBUG_MARKS")?:"10000");
if (m && dodebug && count++ >= dodebug) {
count = 0;
doc_check_consistent(&m->owner->doc);
}
if (move)
ret = call("doc:char", p, forward ? 1 : -1, m);
else
ret = call("doc:char", p, 0, m, NULL, forward ? 1 : -1);
if (ret <= 0)
return WEOF;
if ((unsigned)ret >= CHAR_RET(WEOF))
return WEOF;
else
return ret & 0x1fffff;
}
/* Move the point so it is at the same location as the mark, both in the
* text.
* Firstly find the point closest to the mark, though that will often
* be the point we already have.
* Then for each mark group we find the last that is before the target,
* and move the point to there.
* Then update 'all' list, text ref and seq number.
*/
static void point_forward_to_mark(struct mark *p safe, struct mark *m safe)
{
struct mark *ptmp, *pnear;
unsigned int i;
struct point_links *plnk = safe_cast p->mdata;
pnear = p;
ptmp = p;
tlist_for_each_entry_continue(ptmp, GRP_HEAD, view) {
if (ptmp->seq <= m->seq)
pnear = ptmp;
else
break;
}
/* pnear is the nearest point to m that is before m. So
* move p after pnear in the point list. */
if (p != pnear) {
tlist_del(&p->view);
tlist_add(&p->view, GRP_MARK, &pnear->view);
}
/* Now move 'p' in the various mark lists */
for (i = 0; i < plnk->size; i++) {
struct mark *mnear = NULL;
struct tlist_head *tl;
struct point_links *pnlnk = safe_cast pnear->mdata;
tl = &pnlnk->lists[i];
if (tlist_empty(tl))
continue;
tlist_for_each_continue(tl, GRP_HEAD) {
struct mark *mtmp;
if (TLIST_TYPE(tl) != GRP_MARK)
break;
mtmp = container_of(tl, struct mark, view);
if (mtmp->seq <= m->seq)
mnear = mtmp;
else
break;
}
if (mnear) {
tlist_del(&plnk->lists[i]);
tlist_add(&plnk->lists[i], GRP_LIST, &mnear->view);
} else if (p != pnear) {
tlist_del(&plnk->lists[i]);
tlist_add(&plnk->lists[i], GRP_LIST, &pnlnk->lists[i]);
}
}
/* finally move in the overall list */
hlist_del(&p->all);
hlist_add_after(&m->all, &p->all);
assign_seq(p);
}
static void point_backward_to_mark(struct mark *p safe, struct mark *m safe)
{
struct mark *ptmp, *pnear;
unsigned int i;
struct point_links *plnk = safe_cast p->mdata;
pnear = p;
ptmp = p;
tlist_for_each_entry_continue_reverse(ptmp, GRP_HEAD, view) {
if (ptmp->seq >= m->seq)
pnear = ptmp;
else
break;
}
/* pnear is the nearest point to m that is after m. So
* move p before pnear in the point list */
if (p != pnear) {
tlist_del(&p->view);
tlist_add_tail(&p->view, GRP_MARK, &pnear->view);
}
/* Now move 'p' in the various mark lists */
for (i = 0; i < plnk->size; i++) {
struct mark *mnear = NULL;
struct tlist_head *tl;
struct point_links *pnlnk = safe_cast pnear->mdata;
tl = &pnlnk->lists[i];
if (tlist_empty(tl))
continue;
tlist_for_each_continue_reverse(tl, GRP_HEAD) {
struct mark *mtmp;
if (TLIST_TYPE(tl) != GRP_MARK)
break;
mtmp = container_of(tl, struct mark, view);
if (mtmp->seq >= m->seq)
mnear = mtmp;
else
break;
}
if (mnear) {
tlist_del(&plnk->lists[i]);
tlist_add_tail(&plnk->lists[i], GRP_LIST, &mnear->view);
} else if (p != pnear) {
tlist_del(&plnk->lists[i]);
tlist_add_tail(&plnk->lists[i], GRP_LIST,
&pnlnk->lists[i]);
}
}
/* finally move in the overall list */
hlist_del(&p->all);
hlist_add_before(&p->all, &m->all);
assign_seq(p);
}
static void _mark_to_mark_noref(struct mark *m safe, struct mark *target safe)
{
/* DEBUG: Make sure they are on same list */
struct mark *a = m, *b = target;
if (!mark_valid(m) || !mark_valid(target))
return;
if (a->owner != b->owner)
LOG("mark_to_mark: marks have different owners");
else {
if (m->seq > target->seq) {
a = target; b = m;
}
while (a && a != b)
a = mark_next(a);
if (a != b)
LOG("mark_to_mark with marks on different list - same owner");
}
if (a != b) {
call("editor:notify:Message:broadcast",
m->owner, 0, NULL,
"WARNING marks inconsistent - see log");
return;
}
/* END DEBUG */
if (m == target)
return;
if (!mark_same(m, target))
notify_mark_moving(m, target);
if (m->viewnum == MARK_POINT) {
/* Lots of linkage to fix up */
if (m->seq < target->seq)
point_forward_to_mark(m, target);
else if (m->seq > target->seq)
point_backward_to_mark(m, target);
return;
}
if (m->viewnum == MARK_UNGROUPED) {
/* Only one linked list to worry about */
if (m->seq < target->seq) {
hlist_del(&m->all);
hlist_add_after(&target->all, &m->all);
assign_seq(m);
} else {
hlist_del(&m->all);
hlist_add_before(&m->all, &target->all);
m->seq = target->seq;
assign_seq(target);
}
return;
}
if (m->viewnum == target->viewnum) {
/* Same view, both on the same 2 lists */
if (m->seq < target->seq) {
hlist_del(&m->all);
hlist_add_after(&target->all, &m->all);
tlist_del(&m->view);
tlist_add(&m->view, GRP_MARK, &target->view);
assign_seq(m);
} else {
hlist_del(&m->all);
hlist_add_before(&m->all, &target->all);
tlist_del(&m->view);
tlist_add_tail(&m->view, GRP_MARK, &target->view);
m->seq = target->seq;
assign_seq(target);
}
return;
}
if (target->viewnum == MARK_POINT) {
/* A vmark and a point, both on the only 2 lists
* that need changing */
struct point_links *lnks = safe_cast target->mdata;
if (m->seq < target->seq) {
hlist_del(&m->all);
hlist_add_after(&target->all, &m->all);
tlist_del(&m->view);
tlist_add(&m->view, GRP_MARK, &lnks->lists[m->viewnum]);
assign_seq(m);
} else {
hlist_del(&m->all);
hlist_add_before(&m->all, &target->all);
tlist_del(&m->view);
tlist_add_tail(&m->view, GRP_MARK,
&lnks->lists[m->viewnum]);
m->seq = target->seq;
assign_seq(target);
}
return;
}
/* Hard case: We have a vmark and a mark which isn't on the same list.
* We need to find a matching vmark 'close' to the destination and link
* after that.
*/
if (m->seq < target->seq) {
struct mark *m1 = m, *n;
while ((n = vmark_or_point_next(m1, m->viewnum)) != NULL &&
n->seq <= target->seq)
m1 = n;
if (m1 != m) {
tlist_del(&m->view);
if (m1->viewnum == MARK_POINT) {
struct point_links *lnks = safe_cast m1->mdata;
tlist_add(&m->view, GRP_MARK,
&lnks->lists[m->viewnum]);
} else
tlist_add(&m->view, GRP_MARK, &m1->view);
}
hlist_del(&m->all);
hlist_add_after(&target->all, &m->all);
assign_seq(m);
} else {
struct mark *m1 = m, *n;
while ((n = vmark_or_point_prev(m1, m->viewnum)) != NULL &&
n->seq >= target->seq)
m1 = n;
if (m1 != m) {
tlist_del(&m->view);
if (m1->viewnum == MARK_POINT) {
struct point_links *lnks = safe_cast m1->mdata;
tlist_add_tail(&m->view, GRP_MARK,
&lnks->lists[m->viewnum]);
} else
tlist_add_tail(&m->view, GRP_MARK, &m1->view);
}
hlist_del(&m->all);
hlist_add_before(&m->all, &target->all);
m->seq = target->seq;
assign_seq(target);
}
}
void mark_to_mark_noref(struct mark *m safe, struct mark *target safe)
{
struct mark *m2;
int err = 0;
static bool warned = False;
_mark_to_mark_noref(m, target);
m2 = mark_prev(m);
if (m2 && !mark_same(m2, m) &&
pane_call(m->owner, "debug:validate-marks", m->owner,
0, m2, NULL, 0, m) < 0)
err = 1;
m2 = mark_next(m);
if (m2 && !mark_same(m, m2) &&
pane_call(m->owner, "debug:validate-marks", m->owner,
0, m, NULL, 0, m2) < 0)
err = 1;
if (err && !warned) {
LOG_BT();
call("editor:notify:Message:broadcast",
m->owner, 0, NULL,
"WARNING: marks inconsistent in mark_to_mark, check log");
warned = True;
}
}
void mark_to_mark(struct mark *m safe, struct mark *target safe)
{
_mark_to_mark_noref(m, target);
mark_ref_copy(m, target);
}
void mark_step(struct mark *m safe, int forward)
{
/* step mark forward, or backward, over all marks with same
* ref.
*/
struct mark *m2, *target = m;
/* This is called after .ref has been updated, so we can
* assume the point really is moving.
*/
notify_mark_moving(m, NULL);
if (forward) {
for (m2 = mark_next(m);
m2 && mark_same(m, m2);
m2 = mark_next(m2))
target = m2;
} else {
for (m2 = mark_prev(m);
m2 && mark_same(m, m2);
m2 = mark_prev(m2))
target = m2;
}
mark_to_mark_noref(m, target);
}
void mark_step_sharesref(struct mark *m safe, int forward)
{
/* step mark forward, or backward, over all marks with same
* ref, ignoring the .i, and sets '.i' to a safe value wrt the
* last mark stepped over.
*/
struct mark *m2, *target = m;
/* This is called after .ref has been updated, so we can
* assume the point really is moving.
*/
notify_mark_moving(m, NULL);
if (forward) {
for (m2 = mark_next(m);
m2 && m->ref.p == m2->ref.p;
m2 = mark_next(m2))
target = m2;
} else {
for (m2 = mark_prev(m);
m2 && m->ref.p == m2->ref.p;
m2 = mark_prev(m2))
target = m2;
}
m->ref.i = target->ref.i;
mark_to_mark_noref(m, target);
}
/* A 'vmark' is a mark in a particular view. We can walk around those
* silently skipping over the points.
*/
static struct mark *_vmark_next(struct tlist_head *tl safe)
{
while (TLIST_TYPE(tl) != GRP_HEAD) {
if (TLIST_TYPE(tl) == GRP_LIST) {
tl = TLIST_PTR(tl->next);
continue;
}
return container_of(tl, struct mark, view);
}
return NULL;
}
struct mark *vmark_next(struct mark *m safe)
{
struct tlist_head *tl;
if (!mark_valid(m))
return NULL;
tl = TLIST_PTR(m->view.next);
return _vmark_next(tl);
}
static struct mark *vmark_or_point_next(struct mark *m safe, int view)
{
struct tlist_head *tl;
struct point_links *lnk;
if (m->viewnum == view)
tl = TLIST_PTR(m->view.next);
else if (m->viewnum == MARK_POINT) {
lnk = safe_cast m->mdata;
tl = TLIST_PTR(lnk->lists[view].next);
} else
return NULL;
switch(TLIST_TYPE(tl)) {
default:
case GRP_HEAD:
return NULL;
case GRP_MARK:
return container_of(tl, struct mark, view);
case GRP_LIST:
lnk = container_of_array(tl, struct point_links, lists, view);
return lnk->pt;
}
}
static struct mark *_vmark_prev(struct tlist_head *tl safe)
{
while (TLIST_TYPE(tl) != GRP_HEAD) {
if (TLIST_TYPE(tl) == GRP_LIST) {
tl = TLIST_PTR(tl->prev);
continue;
}
return container_of(tl, struct mark, view);
}
return NULL;
}
struct mark *vmark_prev(struct mark *m safe)
{
struct tlist_head *tl;
if (!mark_valid(m))
return NULL;
tl = TLIST_PTR(m->view.prev);
return _vmark_prev(tl);
}
static struct mark *vmark_or_point_prev(struct mark *m safe, int view)
{
struct tlist_head *tl;
struct point_links *lnk;
if (m->viewnum == view)
tl = TLIST_PTR(m->view.prev);
else if (m->viewnum == MARK_POINT) {
lnk = safe_cast m->mdata;
tl = TLIST_PTR(lnk->lists[view].prev);
} else
return NULL;
switch(TLIST_TYPE(tl)) {
default:
case GRP_HEAD:
return NULL;
case GRP_MARK:
return container_of(tl, struct mark, view);
case GRP_LIST:
lnk = container_of_array(tl, struct point_links, lists, view);
return lnk->pt;
}
}
struct mark *do_vmark_first(struct doc *d safe, int view,
struct pane *owner safe)
{
struct tlist_head *tl;