-
Notifications
You must be signed in to change notification settings - Fork 0
/
render-lines.c
2030 lines (1843 loc) · 55 KB
/
render-lines.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 <[email protected]>
* May be distributed under terms of GPLv2 - see file:COPYING
*
* Rendering for any document which presents as a sequence of lines.
*
* The underlying document, or an intervening filter, must return lines of
* text in response to the "doc:render-line" command.
* This takes a mark and moves it to the end of the rendered line
* so that another call will produce another line.
* "doc:render-line" must always return a full line including '\n'
* unless the result would be bigger than the 'max' passed in ->num or
* ->num < 0. In these cases it can stop before a '\n'. In each case,
* the mark is moved to the end of the region that was rendered;
* This allows a mark to be found for a given character position.
* If mark2 is given, the offset in the rendering when mark2 is reached
* is reported as ->num in the callback.
* For the standard 'render the whole line' functionality, ->num should
* be negative.
*
* The document or filter must also provide "doc:render-line-prev" which
* moves mark to a start-of-line. If num is 0, then don't skip over any
* newlines. If it is '1', then skip one newline.
*
* The returned line can contain attribute markings as <attr,attr>. </>
* is used to pop most recent attributes. << is used to include a
* literal '<'. Lines generally contain UTF-8. Control character '\n'
* is end of line and '\t' tabs 1-8 spaces. '\f' marks end of page -
* nothing after this will be displayed.
*
* Other control characters should be rendered as
* e.g. <fg:red>^X</> - in particular, nul must not appear in the line.
*
* We store all start-of-line the marks found while rendering a pane in
* a 'view' on the document. The line returned for a given mark is
* attached to extra space allocated for that mark. When a change
* notification is received for a mark we discard that string. So the
* string associated with a mark is certainly the string that would be
* rendered after that mark (though it may be truncated). The set of
* marks in a view should always identify exactly the set of lines to be
* displayed. Each mark should be at a start-of-line except possibly
* for the first and last. The first may be internal to a long line,
* but the line rendering attached will always continue to the
* end-of-line. We record the number of display lines in that first
* line.
* The last mark may also be mid-line, and it must never have an
* attached rendering.
* In the worst case of there being no newlines in the document, there
* will be precisely two marks: one contains a partial line and one that
* marks the end of that line. When point moves outside that range a
* new start will be chosen before point using "doc:render-line-prev"
* and the old start is discarded.
*
* To render the pane we:
* 1/ call 'render-line-prev' on a mark at the point and look for that mark
* in the view.
* 2/ If the mark matches and has a string, we have a starting point,
* else we call "doc:render-line" and store the result, thus
* producing a starting point. We determine how many display lines
* are needed to display this text-line and set 'y' accordingly.
* At this point we have two marks: start and end, with known text of known
* height between.
* 3/ Then we move outwards, back from the first mark and forward from
* the last mark. If we find a mark already in the view in the
* desired direction with text attached it is correct and we use
* that. Otherwise we find start (when going backwards) and render a
* new line. Any old mark that is in the range is discarded.
* 4/ When we have a full set of marks and the full height of the pane,
* we discard marks outside the range and start rendering from the
* top. ARG how is cursor drawn.
*
* If we already have correct marks on one side and not the other, we prefer
* to advance on that first side to maximize the amount of text that was common
* with the previous rendering of the page.
*
* Sometimes we need to render without a point. In this case we start
* at the first mark in the view and move forward. If we can we do this
* anyway, and only try the slow way if the target point wasn't found.
*/
#define MARK_DATA_PTR struct pane
#define _GNU_SOURCE /* for asprintf */
#define PANE_DATA_TYPE struct rl_data
#include "core.h"
#include "misc.h"
#include <stdio.h> /* snprintf */
/*
* All functions involved in sending Draw and size requests
* to the display are given two panes: p and focus.
* 'p' is the pane where the drawing happens. 'focus' is the
* leaf on the current stack.
* These are different when the drawing is segmented into regions
* of the target pane, with light-weight panes being used to avoid
* having to refresh the whole target pane when the only change is
* in one region.
* The calls to the display are home_calls with 'focus' as the home
* pane, and 'p' as the focus. The x,y co-ords are, as always,
* relative to the focus pane 'p'.
*/
struct rl_data {
int top_sol; /* true when first mark is at a start-of-line */
int ignore_point;
int skip_height; /* Skip display-lines for first "line" */
int skip_line_height; /* height of lines in skip_height */
int tail_height; /* display lines at eop not display */
int cursor_line; /* line that contains the cursor starts
* on this line */
short target_x, target_y;
short i_moved; /* I moved cursor, so don't clear
* target
*/
short do_wrap;
short shift_locked;
short shift_left;
short shift_left_last_refresh;
struct mark *header;
int typenum;
int repositioned; /* send "render:reposition" when we know
* full position again.
*/
short lines; /* lines drawn before we hit eof */
short cols; /* columns used for longest line */
short margin; /* distance from top/bottom required for cursor */
bool background_drawn;
bool background_uniform;
/* If cursor not visible, we add this pane in bottom-right and place
* cursor there.
*/
struct pane *cursor_pane;
};
#include "core-pane.h"
static void vmark_clear(struct mark *m safe)
{
if (m->mdata) {
pane_close(m->mdata);
m->mdata = NULL;
}
}
static void vmark_free(struct mark *m safe)
{
vmark_clear(m);
mark_free(m);
}
static void vmark_set(struct pane *p safe, struct pane *focus safe,
struct mark *m safe, char *line safe)
{
if (!m->mdata)
m->mdata = call_ret(pane, "attach-renderline", p, -1);
if (m->mdata)
pane_call(m->mdata, "render-line:set", focus, -1, NULL, line);
}
static void vmark_invalidate(struct mark *m safe)
{
if (m->mdata)
pane_damaged(m->mdata, DAMAGED_VIEW);
}
static bool vmark_is_valid(struct mark *m safe)
{
return mark_valid(m) && m->mdata && !(m->mdata->damaged & DAMAGED_VIEW);
}
/* Returns 'true' at end-of-page */
static int _measure_line(struct pane *p safe, struct pane *focus safe,
struct mark *mk safe, short cursor_offset,
char **cursor_attr)
{
struct rl_data *rl = p->data;
struct pane *hp = mk->mdata;
struct call_return cr;
if (!mark_valid(mk) || !hp)
return False;
pane_resize(hp, hp->x, hp->y, p->w, p->h);
if (!rl->shift_locked) {
int sl = pane_attr_get_int(focus, "render-wrap", -2);
if (sl != rl->shift_left) {
char *sla = NULL;
asprintf(&sla, "%d auto", rl->shift_left);
attr_set_str(&focus->attrs, "render-wrap", sla);
free(sla);
}
}
cr = pane_call_ret(all, hp, "render-line:measure",
focus, cursor_offset);
if (cursor_attr)
*cursor_attr = cr.s;
/* end-of-page flag */
return cr.ret & 3;
}
#define measure_line(...) VFUNC(measure_line, __VA_ARGS__)
static inline int measure_line3(struct pane *p safe, struct pane *focus safe,
struct mark *mk safe)
{
return _measure_line(p, focus, mk, -1, NULL);
}
static inline int measure_line4(struct pane *p safe, struct pane *focus safe,
struct mark *mk safe, short cursor_offset)
{
return _measure_line(p, focus, mk, cursor_offset, NULL);
}
static inline int measure_line5(struct pane *p safe, struct pane *focus safe,
struct mark *mk safe, short cursor_offset,
char **cursor_attr)
{
return _measure_line(p, focus, mk, cursor_offset, cursor_attr);
}
/* Returns offset of posx,posy */
static int find_xy_line(struct pane *p safe, struct pane *focus safe,
struct mark *mk safe, short posx, short posy,
const char **xyattr)
{
struct pane *hp = mk->mdata;
struct call_return cr;
if (!hp)
return -1;
cr = pane_call_ret(all, hp,
"render-line:findxy",
focus,
-1, NULL, NULL,
0, NULL, NULL,
posx - hp->x, posy - hp->y);
if (xyattr)
*xyattr = cr.s;
/* xypos */
return cr.ret > 0 ? (cr.ret - 1) : -1;
}
static void draw_line(struct pane *p safe, struct pane *focus safe,
struct mark *mk safe, short offset, bool refresh_all)
{
struct pane *hp = mk->mdata;
if (hp &&
(refresh_all || hp->damaged & DAMAGED_REFRESH)) {
hp->damaged &= ~DAMAGED_REFRESH;
pane_call(hp, "render-line:draw", focus, offset);
}
}
static struct mark *call_render_line_prev(struct pane *p safe,
struct mark *m safe,
int n, int *found)
{
int ret;
struct mark *m2;
if (m->viewnum < 0) {
mark_free(m);
return NULL;
}
ret = call("doc:render-line-prev", p, n, m);
if (ret <= 0) {
/* if n>0 we can fail because start-of-file was found before
* any newline. In that case ret == Efail, and we return NULL.
*/
if (found)
*found = (ret == Efail);
mark_free(m);
return NULL;
}
if (ret < 0) {
/* current line is start-of-file */
mark_free(m);
return NULL;
}
m2 = vmark_matching(m);
if (m2)
mark_free(m);
else
m2 = m;
return m2;
}
static void call_render_line(struct pane *home safe, struct pane *p safe,
struct mark *start safe, struct mark **end)
{
struct mark *m, *m2;
char *s;
if (vmark_is_valid(start))
return;
m = mark_dup_view(start);
if (doc_following(p, m) == WEOF) {
/* We only create a subpane for EOF when it is at start
* of line, else it is included in the preceding line.
*/
call("doc:render-line-prev", p, 0, m);
if (!mark_same(m, start)) {
mark_free(m);
vmark_clear(start);
return;
}
s = "";
} else
s = call_ret(strsave, "doc:render-line", p, -1, m);
if (!mark_valid(start)) {
mark_free(m);
return;
}
if (s)
vmark_set(home, p, start, s);
m2 = vmark_matching(m);
if (m2)
mark_free(m);
else
m2 = m;
/*FIXME shouldn't be needed */
m2 = safe_cast m2;
/* Any mark between start and m2 must be discarded,
*/
while ((m = vmark_next(start)) != NULL &&
m->seq < m2->seq) {
if (end && m == *end)
*end = m2;
vmark_free(m);
}
/* Any mark at same location as m2 must go too. */
while ((m = vmark_next(m2)) != NULL &&
mark_same(m, m2)) {
if (end && m == *end)
*end = m2;
vmark_free(m);
}
/* Any mark at same location as start must go too. */
while ((m = vmark_prev(start)) != NULL &&
mark_same(m, start)) {
vmark_free(m);
}
}
DEF_CMD(no_save)
{
return 1;
}
static struct mark *call_render_line_offset(struct pane *p safe,
struct mark *start safe, int offset)
{
struct mark *m;
m = mark_dup_view(start);
if (call_comm("doc:render-line", p, &no_save, offset, m) <= 0) {
mark_free(m);
return NULL;
}
return m;
}
DEF_CMD(get_offset)
{
if (ci->num < 0)
return 1;
else
return ci->num + 1;
}
static int call_render_line_to_point(struct pane *p safe, struct mark *pm safe,
struct mark *start safe)
{
int len;
struct mark *m = mark_dup_view(start);
len = call_comm("doc:render-line", p, &get_offset, -1, m, NULL, 0, pm);
mark_free(m);
if (len <= 0)
return 0;
return len - 1;
}
/* Choose a new set of lines to display, and mark each one with a line marker.
* We start at pm and move both backwards and forwards one line at a time.
* We stop moving in one of the directions when
* - we hit start/end of file
* - when the edge in the *other* direction enters the previously visible
* area (if there was one). This increases stability of display when
* we move off a line or 2.
* - when we reach the given line count (vline). A positive count restricts
* backward movement, a negative restricts forwards movement.
*/
static bool step_back(struct pane *p safe, struct pane *focus safe,
struct mark **startp safe, struct mark **endp,
short *y_pre safe, short *line_height_pre safe)
{
/* step backwards moving start */
struct rl_data *rl = p->data;
struct mark *m;
bool found_start = False;
struct mark *start = *startp;
if (!start)
return True;
m = call_render_line_prev(focus, mark_dup_view(start),
1, &rl->top_sol);
if (!m) {
/* no text before 'start' */
found_start = True;
} else {
short h = 0;
start = m;
call_render_line(p, focus, start, endp);
measure_line(p, focus, start);
h = start->mdata ? start->mdata->h : 0;
if (h) {
*y_pre = h;
*line_height_pre =
attr_find_int(start->mdata->attrs,
"line-height");
} else
found_start = True;
}
*startp = start;
return found_start;
}
static bool step_fore(struct pane *p safe, struct pane *focus safe,
struct mark **startp safe, struct mark **endp safe,
short *y_post safe, short *line_height_post safe)
{
struct mark *end = *endp;
if (!end)
return True;
call_render_line(p, focus, end, startp);
measure_line(p, focus, end);
if (end->mdata)
*y_post = end->mdata->h;
if (*y_post > 0 && end->mdata)
*line_height_post =
attr_find_int(end->mdata->attrs,
"line-height");
if (!end->mdata || !end->mdata->h)
end = NULL;
else
end = vmark_next(end);
if (!end) {
if (p->h >= *line_height_post *2)
*y_post = p->h / 10;
}
*endp = end;
return False;
}
static int consume_space(struct pane *p safe, int y,
short *y_prep safe, short *y_postp safe,
short *lines_above safe, short *lines_below safe,
int found_start, int found_end,
int line_height_pre, int line_height_post,
bool line_at_a_time)
{
int y_pre = *y_prep;
int y_post = *y_postp;
if (y_pre > 0 && y_post > 0 && !found_start && !found_end) {
int consume = (y_post < y_pre
? y_post : y_pre) * 2;
int above, below;
if (consume > p->h - y)
consume = p->h - y;
if (line_at_a_time && consume > 2*line_height_pre &&
line_height_pre > 0)
consume = 2*line_height_pre;
if (line_at_a_time && consume > 2*line_height_post &&
line_height_post > 0)
consume = 2*line_height_post;
if (y_pre > y_post) {
above = consume - (consume/2);
below = consume/2;
} else {
below = consume - (consume/2);
above = consume/2;
}
y += above + below;
y_pre -= above;
*lines_above += above / (line_height_pre?:1);
y_post -= below;
*lines_below += below / (line_height_post?:1);
/* We have just consumed all of one of
* lines_{above,below} so they are no longer
* both > 0
*/
}
if (found_end && y_pre && !found_start) {
int consume = p->h - y;
if (consume > y_pre)
consume = y_pre;
if (line_at_a_time && consume > line_height_pre &&
line_height_pre > 0)
consume = line_height_pre;
y_pre -= consume;
y += consume;
*lines_above += consume / (line_height_pre?:1);
}
if (found_start && y_post && !found_end) {
int consume = p->h - y;
if (consume > y_post)
consume = y_post;
if (line_at_a_time && consume > line_height_post &&
line_height_post > 0)
consume = line_height_post;
y_post -= consume;
y += consume;
*lines_below += consume / (line_height_post?:1);
}
*y_prep = y_pre;
*y_postp = y_post;
return y;
}
/*
* Choose new start/end to be displayed in the given pane.
* 'pm' must be displayed, and if vline is not NO_NUMERIC,
* pm should be displayed on that line of the display, where
* negative numbers count from the bottom of the page.
* Otherwise pm should be at least rl->margin from top and bottom,
* but in no case should start-of-file be *after* top of display.
* If there is an existing display, move the display as little as
* possible while complying with the above.
*
* We start at 'pm' and move both forward and backward one line at a
* time measuring each line and assessing space used.
* - If the space above pm reaches positive vline, that will be top.
* - If the space below reaches negative vline, that will likely be bottom
* - If pm was before old top and we reach the old top going down,
* and if space measured before pm has reached ->margin, we stop
* moving upward.
* - If pm was after old bottom and we reach the old bottom going up
* and if space measured after pm has reached ->margin, we stop
* moving downward
*
* If we decide to stop moving in both directions, but have not
* reached EOF or full height of display, keep moving downwards.
*
* "start" is a mark at the start of the first line we currently
* intend to display, and y_pre is the number of pixel from the top
* of the display of that line, to the top pixel that will be displayed.
* We only move 'start' backward when y_pre is zero, and initially y_pre
* is the full height of that line.
*
* Similarly "end" is the start of the last line we currently intend
* to display, and y_post is the number of pixel from the bottom of that display
* up to the point we currently intend to display. We only move "end" forward
* when y_post is zero, and when we do we set y_post to the full height of the
* line.
*
* Until we decide on the start or end (found_start, found_end), we
* repeatedly add equal parts of y_pre and y_post into the total to
* be display - consume_space() does this. The space removed from y_pre
* and y_post is added to 'y' - the total height.
* It is also included into lines_above and lines_below which count text lines,
* rather than pixels, using line_height_pre and line_height_post as scale
* factors. These are used to determine when vline or rl->margin requirements
* have been met.
*/
static void find_lines(struct mark *pm safe, struct pane *p safe,
struct pane *focus safe,
int vline)
{
struct rl_data *rl = p->data;
/* orig_top/bot bound what is currently displayed and
* are used to determine if the display has been repositioned.
* orig_bot is *after* the last displayed line. Its ->mdata
* will be NULL.
*/
struct mark *orig_top, *orig_bot;
/* top and bot are used to enhance stability. They are NULL
* if vline is given, else they match orig_top/bot.
*/
struct mark *top, *bot;
struct mark *m;
/* Current estimate of new display. From y_pre pixels down
* from the top of line at 'start', to y_post pixels up
* from the end of the line before 'end' there are 'y'
* pixel lines that we have committed to display.
*/
struct mark *start, *end; // current estimate for new display
short y_pre = 0, y_post = 0;
short y = 0;
/* Number of text-lines in the committed region above or below
* the baseline of the line containing pm. These lines might not
* all be the same height. line_height_pre/post are the heights of
* start and end-1 so changes in y_pre/y_post can be merged into these
* counts.
*/
short lines_above = 0, lines_below = 0; /* distance from start/end
* to pm.
*/
short line_height_pre = 1, line_height_post = 1;
short offset; // pos of pm while measureing the line holding the cursor.
/* We set found_start we we don't want to consider anything above the
* top that we currently intend to display. Once it is set,
* 'start', y_pre, lines_above are all frozen.
* Similarly once found_end is set we freeze end, y_pos, lines_below,
* but we mught unfreeze those if there is room for more text at end of
* display.
* found_start is set:
* - when y_pre is zero and start is at top of file
* - when lines_above reaches positive vline
* - when intended display has grown down into the previous
* display. This means we have added enough lines above and
* don't want to scroll the display more than we need.
* - When we hit unexpected errors moving backwards
* found_end is set:
* - when we hit end-of-file
* - when lines_below reached -vline
* - when the top of the intended display overlaps the
* previous display.
*/
bool found_start = False, found_end = False;
orig_top = vmark_first(focus, rl->typenum, p);
orig_bot = vmark_last(focus, rl->typenum, p);
/* Protect top/bot from being freed by call_render_line */
if (orig_top)
orig_top = mark_dup(orig_top);
if (orig_bot)
orig_bot = mark_dup(orig_bot);
start = vmark_new(focus, rl->typenum, p);
if (!start)
goto abort;
/* FIXME why is this here. We set ->repositioned at the end
* if the marks move. Maybe we need to check if y_pre moves too.
*/
rl->repositioned = 1;
mark_to_mark(start, pm);
start = call_render_line_prev(focus, start, 0, &rl->top_sol);
if (!start)
goto abort;
/* Render the cursor line, and find where the cursor is. */
offset = call_render_line_to_point(focus, pm, start);
call_render_line(p, focus, start, NULL);
end = vmark_next(start);
/* Note: 'end' might be NULL if 'start' is end-of-file, otherwise
* call_render_line() will have created 'end' if it didn't exist.
*/
if (!rl->shift_locked)
rl->shift_left = 0;
if (start->mdata) {
struct pane *hp = start->mdata;
int curs_width;
int shifts = 0;
found_end = measure_line(p, focus, start, offset) & 2;
curs_width = pane_attr_get_int(
start->mdata, "curs_width", 1);
if (curs_width <= 0)
curs_width = 1;
// FIXME this loops indefinitely if cursor after
// right-justified text.
while (!rl->do_wrap && !rl->shift_locked &&
hp->cx + curs_width >= p->w) {
int shift = 8 * curs_width;
if (shift > hp->cx)
shift = hp->cx;
rl->shift_left += shift;
measure_line(p, focus, start, offset);
if (shifts++ > 100)
break;
}
/* ->cy is top of cursor, we want to measure from bottom */
line_height_pre = attr_find_int(start->mdata->attrs, "line-height");
if (line_height_pre < 1)
line_height_pre = 1;
/* We now have a better estimate than '1' */
line_height_post = line_height_pre;
y_pre = start->mdata->cy + line_height_pre;
y_post = start->mdata->h - y_pre;
} else {
/* Should never happen */
y_pre = 0;
y_post = 0;
}
if (!end) {
/* When cursor at EOF, leave 10% height of display
* blank at bottom to make this more obvious - unless
* the display is so small that might push the last line partly
* off display at the top.
*/
if (p->h > line_height_pre * 2)
y_post += p->h / 10;
else {
/* Small display, no space at EOF */
y_post = 0;
found_end = True;
}
}
y = 0;
if (rl->header && rl->header->mdata)
y = rl->header->mdata->h;
/* We have start/end of the focus line. When rendered this,
* plus header and eof-footer would use y_pre + y + y_post
* vertical space.
*/
if (vline != NO_NUMERIC) {
/* ignore current position - top/bot irrelevant */
top = NULL;
bot = NULL;
} else {
top = orig_top;
bot = orig_bot;
}
while ((!found_start || !found_end) && y < p->h) {
if (vline != NO_NUMERIC) {
/* As lines_above/below measure from the baseline
* of the cursor line, and as we want to see the top
* of he cursor line as well, these two cases are
* asymmetric.
*/
if (!found_start && vline > 0 &&
lines_above >= vline)
found_start = True;
if (!found_end && vline < 0 &&
lines_below >= -vline-1)
found_end = True;
}
if (!found_start && y_pre <= 0)
found_start = step_back(p, focus, &start, &end,
&y_pre, &line_height_pre);
if (found_end && y_post && bot &&
mark_ordered_or_same(start, bot))
/* Extra vertical space gets inserted after EOF when
* there is a long jump to get there, but if we hit 'bot'
* soon when searching back, we discard any unused space.
*/
y_post = 0;
if (!found_end && bot &&
(!end || mark_ordered_or_same(bot, end)) &&
lines_below >= rl->margin)
if (mark_ordered_not_same(start, bot) ||
/* Overlap original from below, so prefer to
* maximize that overlap.
*/
(mark_same(start, bot) &&
y_pre - rl->skip_height >= y_post))
/* No overlap in marks yet, but over-lap in
* space, so same result as above.
*/
found_end = True;
if (!found_end && y_post <= 0)
/* step forwards */
found_end = step_fore(p, focus, &start, &end,
&y_post, &line_height_post);
/* This test has "> rl->margin" while found_end test has
* ">= rl->margin" due to the asymmetry of measuring from the
* baseline, not the centreling, of the target text.
*/
if (!found_start && top && end &&
mark_ordered_or_same(start, top) &&
lines_above > rl->margin)
if (mark_ordered_not_same(top, end) ||
(mark_same(top, end) &&
y_post - rl->tail_height >= y_pre))
found_start = True;
y = consume_space(p, y, &y_pre, &y_post,
&lines_above, &lines_below,
found_start, found_end,
line_height_pre, line_height_post,
vline && vline != NO_NUMERIC);
}
/* We might need to continue downwards even after found_end
* if there is more space.
*/
found_end = end == NULL;
while (!found_end && y < p->h) {
if (y_post <= 0)
found_end = step_fore(p, focus, &start, &end,
&y_post, &line_height_post);
y = consume_space(p, y, &y_pre, &y_post,
&lines_above, &lines_below,
found_start, found_end,
line_height_pre, line_height_post,
vline && vline != NO_NUMERIC);
}
if (start->mdata && start->mdata->h <= y_pre) {
y_pre = 0;
m = vmark_next(start);
vmark_free(start);
start = m;
}
if (!start)
goto abort;
rl->skip_height = y_pre;
rl->skip_line_height = line_height_pre;
rl->tail_height = y_post;
/* Now discard any marks outside start-end */
if (end && end->seq < start->seq)
/* something confused, make sure we don't try to use 'end' after
* freeing it.
*/
end = start;
while ((m = vmark_prev(start)) != NULL)
vmark_free(m);
if (end) {
while ((m = vmark_next(end)) != NULL)
vmark_free(m);
vmark_clear(end);
}
y = 0;
rl->cols = 0;
if (rl->header && rl->header->mdata) {
y = rl->header->mdata->h;
rl->cols = pane_attr_get_int(rl->header->mdata, "width", 0);
}
y -= rl->skip_height;
for (m = vmark_first(focus, rl->typenum, p);
m && m->mdata ; m = vmark_next(m)) {
struct pane *hp = m->mdata;
int cols;
if (pane_resize(hp, hp->x, y, hp->w, hp->h) &&
!rl->background_uniform)
pane_damaged(hp, DAMAGED_REFRESH);
y += hp->h;
cols = pane_attr_get_int(hp, "width", 0);
if (cols > rl->cols)
rl->cols = cols;
}
rl->lines = y;
pane_damaged(p, DAMAGED_REFRESH);
m = vmark_first(focus, rl->typenum, p);
if (!m || !orig_top || !mark_same(m, orig_top))
rl->repositioned = 1;
m = vmark_last(focus, rl->typenum, p);
if (!m || !orig_bot || !mark_same(m, orig_bot))
rl->repositioned = 1;
abort:
mark_free(orig_top);
mark_free(orig_bot);
}
DEF_CMD(cursor_handle)
{
return 0;
}
static int render(struct mark *pm, struct pane *p safe,
struct pane *focus safe)
{
struct rl_data *rl = p->data;
short y = 0;
struct mark *m, *m2;
struct xy scale = pane_scale(focus);
char *s;
bool hide_cursor = False;
bool cursor_drawn = False;
bool refresh_all = rl->shift_left != rl->shift_left_last_refresh;
rl->shift_left_last_refresh = rl->shift_left;
s = pane_attr_get(focus, "hide-cursor");
if (s && strcmp(s, "yes") == 0)
hide_cursor = True;
rl->cols = 0;
m = vmark_first(focus, rl->typenum, p);
if (!rl->background_drawn) {
refresh_all = True;
rl->background_uniform = True;
}
s = pane_attr_get(focus, "background");
if (s && strstarts(s, "call:")) {
home_call(focus, "Draw:clear", p, 0, NULL, "");
home_call(focus, s+5, p, 0, m);
refresh_all = True;
rl->background_uniform = False;
} else if (rl->background_drawn)
;
else if (!s)
home_call(focus, "Draw:clear", p, 0, NULL, "");
else if (strstarts(s, "color:")) {
char *a = strdup(s);
strcpy(a, "bg:");
strcpy(a+3, s+6);
home_call(focus, "Draw:clear", p, 0, NULL, a);
free(a);
} else if (strstarts(s, "image:")) {
home_call(focus, "Draw:clear", p);
home_call(focus, "Draw:image", p, 0, NULL, s+6, 0, NULL, "S");
rl->background_uniform = False;
} else
home_call(focus, "Draw:clear", p, 0, NULL, "");
rl->background_drawn = True;
if (rl->header && vmark_is_valid(rl->header)) {
struct pane *hp = rl->header->mdata;
draw_line(p, focus, rl->header, -1, refresh_all);
y = hp->h;
rl->cols = pane_attr_get_int(hp, "width", 0);
}
y -= rl->skip_height;
p->cx = p->cy = -1;
rl->cursor_line = 0;
while (m && m->mdata) {
m2 = vmark_next(m);
if (!hide_cursor && p->cx <= 0 && pm &&
mark_ordered_or_same(m, pm) &&
(!(m2 && doc_following(focus, m2) != WEOF) ||
mark_ordered_not_same(pm, m2))) {
struct xy curs;
struct pane *hp = m->mdata;
short len = call_render_line_to_point(focus, pm,
m);
draw_line(p, focus, m, len, True);
rl->cursor_line = hp->y + hp->cy;
curs = pane_mapxy(hp, p, hp->cx, hp->cy, False);
if (hp->cx < 0 || hp->cx >= hp->w) {
p->cx = -1;
p->cy = -1;
} else {
p->cx = curs.x;
p->cy = curs.y;
cursor_drawn = True;
}
} else {
draw_line(p, focus, m, -1, refresh_all);
}
if (m->mdata) {
int cols = pane_attr_get_int(m->mdata, "width", 0);
if (cols > rl->cols)
rl->cols = cols;
y = m->mdata->y + m->mdata->h;
}
m = m2;
}
if (m && !m->mdata && vmark_next(m))
LOG("render-lines: break in vmark sequence");
if (!cursor_drawn && !hide_cursor) {
/* Place cursor in bottom right */
struct pane *cp = rl->cursor_pane;
short mwidth = -1;
short lineheight;
if (!cp) {
/* Note: this pane will container rl_data
* which isn't used.
*/
cp = pane_register(p, -1, &cursor_handle);
rl->cursor_pane = cp;
}
if (m)
m2 = vmark_prev(m);
else
m2 = vmark_last(focus, rl->typenum, p);
while (m2 && mwidth <= 0) {
if (m2->mdata) {
mwidth = pane_attr_get_int(
m2->mdata, "curs_width", -1);
lineheight = pane_attr_get_int(
m2->mdata, "line-height", -1);
}
m2 = vmark_prev(m2);
}
if (mwidth <= 0) {
mwidth = 1;
lineheight = 1;
}
if (cp) {
pane_resize(cp,
p->w - mwidth,
p->h - lineheight,
mwidth, lineheight);