2 * Copyright Neil Brown ©2015-2021 <neil@brown.name>
3 * May be distributed under terms of GPLv2 - see file:COPYING
5 * Rendering for any document which presents as a sequence of lines.
7 * The underlying document, or an intervening filter, must return lines of
8 * text in response to the "doc:render-line" command.
9 * This takes a mark and moves it to the end of the rendered line
10 * so that another call will produce another line.
11 * "doc:render-line" must always return a full line including '\n'
12 * unless the result would be bigger than the 'max' passed in ->num or,
13 * when ->num==-1, unless the rendering would go beyond the location in
14 * ->mark2. In these cases it can stop before a '\n'. In each case,
15 * the mark is moved to the end of the region that was rendered;
16 * This allows a mark to be found for a given character position, or a display
17 * position found for a given mark.
18 * For the standard 'render the whole line' functionality, ->num should
21 * The document or filter must also provide "doc:render-line-prev" which
22 * moves mark to a start-of-line. If num is 0, then don't skip over any
23 * newlines. If it is '1', then skip one newline.
25 * The returned line can contain attribute markings as <attr,attr>. </>
26 * is used to pop most recent attributes. << is used to include a
27 * literal '<'. Lines generally contain UTF-8. Control character '\n'
28 * is end of line and '\t' tabs 1-8 spaces. '\f' marks end of page -
29 * nothing after this will be displayed.
31 * Other control characters should be rendered as
32 * e.g. <fg:red>^X</> - in particular, nul must not appear in the line.
34 * We store all start-of-line the marks found while rendering a pane in
35 * a 'view' on the document. The line returned for a given mark is
36 * attached to extra space allocated for that mark. When a change
37 * notification is received for a mark we discard that string. So the
38 * string associated with a mark is certainly the string that would be
39 * rendered after that mark (though it may be truncated). The set of
40 * marks in a view should always identify exactly the set of lines to be
41 * displayed. Each mark should be at a start-of-line except possibly
42 * for the first and last. The first may be internal to a long line,
43 * but the line rendering attached will always continue to the
44 * end-of-line. We record the number of display lines in that first
46 * The last mark may also be mid-line, and it must never have an
48 * In the worst case of there being no newlines in the document, there
49 * will be precisely two marks: one contains a partial line and one that
50 * marks the end of that line. When point moves outside that range a
51 * new start will be chosen before point using "doc:render-line-prev"
52 * and the old start is discarded.
54 * To render the pane we:
55 * 1/ call 'render-line-prev' on a mark at the point and look for that mark
57 * 2/ If the mark matches and has a string, we have a starting point,
58 * else we call "doc:render-line" and store the result, thus
59 * producing a starting point. We determine how many display lines
60 * are needed to display this text-line and set 'y' accordingly.
61 * At this point we have two marks: start and end, with known text of known
63 * 3/ Then we move outwards, back from the first mark and forward from
64 * the last mark. If we find a mark already in the view in the
65 * desired direction with text attached it is correct and we use
66 * that. Otherwise we find start (when going backwards) and render a
67 * new line. Any old mark that is in the range is discarded.
68 * 4/ When we have a full set of marks and the full height of the pane,
69 * we discard marks outside the range and start rendering from the
70 * top. ARG how is cursor drawn.
72 * If we already have correct marks on one side and not the other, we prefer
73 * to advance on that first side to maximize the amount of text that was common
74 * with the previous rendering of the page.
76 * Sometimes we need to render without a point. In this case we start
77 * at the first mark in the view and move forward. If we can we do this
78 * anyway, and only try the slow way if the target point wasn't found.
81 #define MARK_DATA_PTR struct pane
82 #define _GNU_SOURCE /* for asprintf */
85 #include <stdio.h> /* snprintf */
88 * All functions involved in sending Draw and size requests
89 * to the display are given two panes: p and focus.
90 * 'p' is the pane where the drawing happens. 'focus' is the
91 * leaf on the current stack.
92 * These are different when the drawing is segmented into regions
93 * of the target pane, with light-weight panes being used to avoid
94 * having to refresh the whole target pane when the only change is
96 * The calls to the display are home_calls with 'focus' as the home
97 * pane, and 'p' as the focus. The x,y co-ords are, as always,
98 * relative to the focus pane 'p'.
102 int top_sol; /* true when first mark is at a start-of-line */
104 int skip_height; /* Skip display-lines for first "line" */
105 int skip_line_height; /* height of lines in skip_height */
106 int tail_height; /* display lines at eop not display */
107 int cursor_line; /* line that contains the cursor starts
109 short target_x, target_y;
110 short i_moved; /* I moved cursor, so don't clear
115 short shift_left_last_refresh;
118 int repositioned; /* send "render:reposition" when we know
119 * full position again.
121 short lines; /* lines drawn before we hit eof */
122 short cols; /* columns used for longest line */
123 short margin; /* distance from top/bottom required for cursor */
124 bool background_drawn;
125 bool background_uniform;
127 /* If cursor not visible, we add this pane in bottom-right and place
130 struct pane *cursor_pane;
133 static void vmark_clear(struct mark *m safe)
136 pane_close(m->mdata);
141 static void vmark_free(struct mark *m safe)
147 static void vmark_set(struct pane *p safe, struct mark *m safe, char *line safe)
150 m->mdata = call_ret(pane, "attach-renderline", p);
152 pane_call(m->mdata, "render-line:set", p, 0, NULL, line);
155 static void vmark_invalidate(struct mark *m safe)
158 pane_damaged(m->mdata, DAMAGED_VIEW);
161 static bool vmark_is_valid(struct mark *m safe)
163 return mark_valid(m) && m->mdata && !(m->mdata->damaged & DAMAGED_VIEW);
166 /* Returns 'true' at end-of-page */
167 static bool measure_line(struct pane *p safe, struct pane *focus safe,
168 struct mark *mk safe, short cursor_offset)
170 struct pane *hp = mk->mdata;
173 if (mark_valid(mk) && hp) {
174 pane_resize(hp, hp->x, hp->y, p->w, p->h);
175 ret = pane_call(hp, "render-line:measure",
176 focus, cursor_offset);
178 /* end-of-page flag */
182 /* Returns offset of posx,posy */
183 static int find_xy_line(struct pane *p safe, struct pane *focus safe,
184 struct mark *mk safe, short posx, short posy)
186 struct pane *hp = mk->mdata;
191 "render-line:findxy",
195 posx - hp->x, posy - hp->y);
198 return ret > 0 ? (ret - 1) : -1;
201 static void draw_line(struct pane *p safe, struct pane *focus safe,
202 struct mark *mk safe, short offset, bool refresh_all)
204 struct pane *hp = mk->mdata;
207 (refresh_all || hp->damaged & DAMAGED_REFRESH)) {
208 hp->damaged &= ~DAMAGED_REFRESH;
209 pane_call(hp, "render-line:draw", focus, offset);
213 static struct mark *call_render_line_prev(struct pane *p safe,
222 ret = call("doc:render-line-prev", p, n, m);
224 /* if n>0 we can fail because start-of-file was found before
225 * any newline. In that case ret == Efail, and we return NULL.
228 *found = (ret == Efail);
233 /* current line is start-of-file */
238 m2 = vmark_matching(m);
246 static void call_render_line(struct pane *home safe, struct pane *p safe,
247 struct mark *start safe, struct mark **end)
252 if (vmark_is_valid(start))
255 m = mark_dup_view(start);
256 if (doc_following(p, m) == WEOF) {
257 /* We only create a subpane for EOF when it is at start
258 * of line, else it is included in the preceding line.
260 call("doc:render-line-prev", p, 0, m);
261 if (!mark_same(m, start)) {
268 s = call_ret(strsave, "doc:render-line", p, NO_NUMERIC, m);
270 if (!mark_valid(start)) {
275 vmark_set(home, start, s);
277 m2 = vmark_matching(m);
282 /*FIXME shouldn't be needed */
285 /* Any mark between start and m2 must be discarded,
287 while ((m = vmark_next(start)) != NULL &&
289 if (end && m == *end)
293 /* Any mark at same location as m2 must go too. */
294 while ((m = vmark_next(m2)) != NULL &&
296 if (end && m == *end)
300 /* Any mark at same location as start must go too. */
301 while ((m = vmark_prev(start)) != NULL &&
302 mark_same(m, start)) {
312 static struct mark *call_render_line_offset(struct pane *p safe,
313 struct mark *start safe, int offset)
317 m = mark_dup_view(start);
318 if (call_comm("doc:render-line", p, &no_save, offset, m) <= 0) {
328 int l = strlen(ci->str);
329 while (l >=4 && strncmp(ci->str+l-3, "</>", 3) == 0 &&
337 static int call_render_line_to_point(struct pane *p safe, struct mark *pm safe,
338 struct mark *start safe)
341 struct mark *m = mark_dup_view(start);
343 len = call_comm("doc:render-line", p, &get_len, -1, m, NULL, 0, pm);
351 /* Choose a new set of lines to display, and mark each one with a line marker.
352 * We start at pm and move both backwards and forwards one line at a time.
353 * We stop moving in one of the directions when
354 * - we hit start/end of file
355 * - when the edge in the *other* direction enters the previously visible
356 * area (if there was one). This increases stability of display when
357 * we move off a line or 2.
358 * - when we reach the given line count (vline). A positive count restricts
359 * backward movement, a negative restricts forwards movement.
362 static bool step_back(struct pane *p safe, struct pane *focus safe,
363 struct mark **startp safe, struct mark **endp,
364 short *y_pre safe, short *line_height_pre safe)
366 /* step backwards moving start */
367 struct rl_data *rl = p->data;
369 bool found_start = False;
370 struct mark *start = *startp;
374 m = call_render_line_prev(focus, mark_dup_view(start),
377 /* no text before 'start' */
382 call_render_line(p, focus, start, endp);
383 measure_line(p, focus, start, -1);
384 h = start->mdata ? start->mdata->h : 0;
388 attr_find_int(start->mdata->attrs,
397 static bool step_fore(struct pane *p safe, struct pane *focus safe,
398 struct mark **startp safe, struct mark **endp safe,
399 short *y_post safe, short *line_height_post safe)
401 struct mark *end = *endp;
402 bool found_end = False;
406 call_render_line(p, focus, end, startp);
407 found_end = measure_line(p, focus, end, -1);
409 *y_post = end->mdata->h;
410 if (*y_post > 0 && end->mdata)
412 attr_find_int(end->mdata->attrs,
414 if (!end->mdata || !end->mdata->h)
417 end = vmark_next(end);
420 if (p->h >= *line_height_post *2)
428 static int consume_space(struct pane *p safe, int y,
429 short *y_prep safe, short *y_postp safe,
430 short *lines_above safe, short *lines_below safe,
431 int found_start, int found_end,
432 int line_height_pre, int line_height_post)
435 int y_post = *y_postp;
437 if (y_pre > 0 && y_post > 0) {
438 int consume = (y_post < y_pre
439 ? y_post : y_pre) * 2;
441 if (consume > p->h - y)
443 if (y_pre > y_post) {
444 above = consume - (consume/2);
447 below = consume - (consume/2);
452 *lines_above += above / (line_height_pre?:1);
454 *lines_below += below / (line_height_post?:1);
455 /* We have just consumed all of one of
456 * lines_{above,below} so they are no longer
460 if (found_end && y_pre) {
461 int consume = p->h - y;
466 *lines_above += consume / (line_height_pre?:1);
468 if (found_start && y_post) {
469 int consume = p->h - y;
470 if (consume > y_post)
474 *lines_below += consume / (line_height_post?:1);
482 * Choose new start/end to be displayed in the given pane.
483 * 'pm' must be displayed, and if vline is not NO_NUMERIC,
484 * pm should be displayed on that line of the display, where
485 * negative numbers count from the bottom of the page.
486 * Otherwise pm should be at least rl->margin from top and bottom,
487 * but in no case should start-of-file be *after* top of display.
488 * If there is an existing display, move the display as little as
489 * possible while complying with the above.
491 * We start at 'pm' and move both forward and backward one line at a
492 * time measuring each line and assessing space used.
493 * - If the space above pm reaches positive vline, that will be top.
494 * - If the space below reaches negative vline, that will likely be bottom
495 * - If pm was before old top and we reach the old top going down,
496 * and if space measured before pm has reached ->margin, we stop
498 * - If pm was after old bottom and we reach the old bottom going up
499 * and if space measured after pm has reached ->margin, we stop
502 * If we decide to stop moving in both directions, but have not
503 * reached EOF or full height of display, keep moving downwards.
505 static void find_lines(struct mark *pm safe, struct pane *p safe,
506 struct pane *focus safe,
509 struct rl_data *rl = p->data;
510 struct mark *orig_top, *orig_bot;
511 struct mark *top, *bot; // boundary of previous display
513 struct mark *start, *end; // current estimate for new display
515 short lines_above = 0, lines_below = 0; /* distance from start/end
518 short offset; // pos of pm in rendering of that line
519 bool found_start = False, found_end = False;
520 /* y_pre and y_post are measurement from start/end that
521 * haven't yet been included into lines_above/lines_below.
523 short y_pre = 0, y_post = 0;
524 short line_height_pre = 1, line_height_post = 1;
526 orig_top = vmark_first(focus, rl->typenum, p);
527 orig_bot = vmark_last(focus, rl->typenum, p);
528 /* Protect top/bot from being freed by call_render_line */
530 orig_top = mark_dup(orig_top);
532 orig_bot = mark_dup(orig_bot);
534 start = vmark_new(focus, rl->typenum, p);
537 rl->repositioned = 1;
538 mark_to_mark(start, pm);
539 start = call_render_line_prev(focus, start, 0, &rl->top_sol);
542 offset = call_render_line_to_point(focus, pm, start);
543 call_render_line(p, focus, start, NULL);
544 end = vmark_next(start);
545 /* Note: 'end' might be NULL if 'start' is end-of-file, otherwise
546 * call_render_line() will have created 'end' if it didn't exist.
551 /* ->cy is top of cursor, we want to measure from bottom */
553 struct pane *hp = start->mdata;
555 found_end = measure_line(p, focus, start, offset);
557 curs_width = pane_attr_get_int(
558 start->mdata, "curs_width", 1);
559 while (!rl->do_wrap && curs_width > 0 &&
560 hp->cx + curs_width >= p->w) {
561 int shift = 8 * curs_width;
564 rl->shift_left += shift;
565 measure_line(p, focus, start, offset);
567 line_height_pre = attr_find_int(start->mdata->attrs, "line-height");
568 if (line_height_pre < 1)
570 y_pre = start->mdata->cy + line_height_pre;
571 y_post = start->mdata->h - y_pre;
573 /* Should never happen */
579 if (p->h > line_height_pre * 2)
582 /* Small display, no space at EOF */
586 if (rl->header && rl->header->mdata)
587 y = rl->header->mdata->h;
589 /* We have start/end of the focus line. When rendered this,
590 * plus header and eof-footed would use y_pre + y + y_post
594 if (vline != NO_NUMERIC) {
595 /* ignore current position - top/bot irrelevant */
603 while ((!found_start || !found_end) && y < p->h) {
604 if (vline != NO_NUMERIC) {
605 if (!found_start && vline > 0 &&
606 lines_above >= vline-1)
608 if (!found_end && vline < 0 &&
609 lines_below >= -vline-1)
612 if (!found_start && y_pre <= 0)
613 found_start = step_back(p, focus, &start, &end,
614 &y_pre, &line_height_pre);
616 if (found_end && y_post && bot &&
617 mark_ordered_or_same(start, bot))
618 /* Extra vertical space gets inserted after EOF when
619 * there is a long jump to get there, but if we hit 'bot'
620 * soon when searching back, we discard any unused space.
624 if (!found_end && bot &&
625 (!end || mark_ordered_or_same(bot, end)) &&
626 lines_below >= rl->margin)
627 if (mark_ordered_not_same(start, bot) ||
628 /* Overlap original from below, so prefer to
629 * maximize that overlap.
631 (mark_same(start, bot) &&
632 y_pre - rl->skip_height >= y_post))
633 /* No overlap in marks yet, but over-lap in
634 * space, so same result as above.
638 if (!found_end && y_post <= 0)
640 found_end = step_fore(p, focus, &start, &end,
641 &y_post, &line_height_post);
643 if (!found_start && top && end &&
644 mark_ordered_or_same(start, top) &&
645 lines_above >= rl->margin)
646 if (mark_ordered_not_same(top, end) ||
647 (mark_same(top, end) &&
648 y_post - rl->tail_height >= y_pre))
651 y = consume_space(p, y, &y_pre, &y_post,
652 &lines_above, &lines_below,
653 found_start, found_end,
654 line_height_pre, line_height_post);
656 /* We might need to continue downwards even after found_end
657 * if there is more space.
659 found_end = end == NULL;
660 while (!found_end && y < p->h) {
662 found_end = step_fore(p, focus, &start, &end,
663 &y_post, &line_height_post);
664 y = consume_space(p, y, &y_pre, &y_post,
665 &lines_above, &lines_below,
666 found_start, found_end,
667 line_height_pre, line_height_post);
670 if (start->mdata && start->mdata->h <= y_pre) {
672 m = vmark_next(start);
679 rl->skip_height = y_pre;
680 rl->skip_line_height = line_height_pre;
681 rl->tail_height = y_post;
682 /* Now discard any marks outside start-end */
683 if (end && end->seq < start->seq)
684 /* something confused, make sure we don't try to use 'end' after
688 while ((m = vmark_prev(start)) != NULL)
692 while ((m = vmark_next(end)) != NULL)
699 if (rl->header && rl->header->mdata)
700 y = rl->header->mdata->h;
701 y -= rl->skip_height;
702 for (m = vmark_first(focus, rl->typenum, p);
703 m && m->mdata ; m = vmark_next(m)) {
704 struct pane *hp = m->mdata;
705 hp->damaged &= ~DAMAGED_SIZE;
706 pane_resize(hp, hp->x, y, hp->w, hp->h);
707 if (hp->damaged & DAMAGED_SIZE && !rl->background_uniform)
708 pane_damaged(hp, DAMAGED_REFRESH);
711 pane_damaged(p, DAMAGED_REFRESH);
712 m = vmark_first(focus, rl->typenum, p);
713 if (!m || !orig_top || !mark_same(m, orig_top))
714 rl->repositioned = 1;
715 m = vmark_last(focus, rl->typenum, p);
716 if (!m || !orig_bot || !mark_same(m, orig_bot))
717 rl->repositioned = 1;
724 DEF_CMD(cursor_handle)
729 static int render(struct mark *pm, struct pane *p safe,
730 struct pane *focus safe)
732 struct rl_data *rl = p->data;
735 struct xy scale = pane_scale(focus);
738 int cursor_drawn = 0;
739 bool refresh_all = rl->shift_left != rl->shift_left_last_refresh;
741 rl->shift_left_last_refresh = rl->shift_left;
742 s = pane_attr_get(focus, "hide-cursor");
743 if (s && strcmp(s, "yes") == 0)
747 m = vmark_first(focus, rl->typenum, p);
748 if (!rl->background_drawn) {
750 rl->background_uniform = True;
752 s = pane_attr_get(focus, "background");
753 if (s && strncmp(s, "call:", 5) == 0) {
754 home_call(focus, "Draw:clear", p, 0, NULL, "");
755 home_call(focus, s+5, p, 0, m);
757 rl->background_uniform = False;
758 } else if (rl->background_drawn)
761 home_call(focus, "Draw:clear", p, 0, NULL, "");
762 else if (strncmp(s, "color:", 6) == 0) {
766 home_call(focus, "Draw:clear", p, 0, NULL, a);
768 } else if (strncmp(s, "image:", 6) == 0) {
769 home_call(focus, "Draw:clear", p);
770 home_call(focus, "Draw:image", p, 1, NULL, s+6);
771 rl->background_uniform = False;
773 home_call(focus, "Draw:clear", p, 0, NULL, "");
774 rl->background_drawn = True;
776 if (rl->header && vmark_is_valid(rl->header)) {
777 struct pane *hp = rl->header->mdata;
778 draw_line(p, focus, rl->header, -1, refresh_all);
780 rl->cols = hp->x + hp->w;
782 y -= rl->skip_height;
787 while (m && m->mdata) {
789 if (!hide_cursor && p->cx <= 0 && pm &&
790 mark_ordered_or_same(m, pm) &&
791 (!(m2 && doc_following(focus, m2) != WEOF) ||
792 mark_ordered_not_same(pm, m2))) {
794 struct pane *hp = m->mdata;
795 short len = call_render_line_to_point(focus, pm,
797 draw_line(p, focus, m, len, True);
798 rl->cursor_line = hp->y + hp->cy;
799 curs = pane_mapxy(hp, p, hp->cx, hp->cy, False);
809 draw_line(p, focus, m, -1, refresh_all);
812 int cols = m->mdata->x + m->mdata->w;
815 y = m->mdata->y + m->mdata->h;
819 if (m && !m->mdata && vmark_next(m))
820 LOG("render-lines: break in vmark sequence");
821 if (!cursor_drawn && !hide_cursor) {
822 /* Place cursor in bottom right */
823 struct pane *cp = rl->cursor_pane;
828 cp = pane_register(p, -1, &cursor_handle);
829 rl->cursor_pane = cp;
834 m2 = vmark_last(focus, rl->typenum, p);
836 while (m2 && mwidth <= 0) {
838 mwidth = pane_attr_get_int(
839 m2->mdata, "curs_width", -1);
840 lineheight = pane_attr_get_int(
841 m2->mdata, "line-height", -1);
856 home_call(focus, "Draw:clear", cp);
857 home_call(focus, "Draw:text", cp, 0, NULL, " ",
861 } else if (rl->cursor_pane) {
862 pane_close(rl->cursor_pane);
863 rl->cursor_pane = NULL;
868 DEF_CMD(render_lines_get_attr)
870 struct rl_data *rl = ci->home->data;
872 if (ci->str && strcmp(ci->str, "shift_left") == 0) {
875 return comm_call(ci->comm2, "cb", ci->focus,
877 snprintf(ret, sizeof(ret), "%d", rl->shift_left);
878 return comm_call(ci->comm2, "cb", ci->focus, 0, NULL, ret);
883 DEF_CMD(render_lines_point_moving)
885 struct pane *p = ci->home;
886 struct rl_data *rl = p->data;
887 struct mark *pt = call_ret(mark, "doc:point", ci->home);
890 if (!pt || ci->mark != pt)
892 /* Stop igoring point, because it is probably relevant now */
893 rl->ignore_point = 0;
895 /* Someone else moved the point, so reset target column */
897 m = vmark_at_or_before(ci->focus, pt, rl->typenum, p);
899 pane_damaged(m->mdata, DAMAGED_REFRESH);
900 pane_damaged(m->mdata->parent, DAMAGED_REFRESH);
905 static int revalidate_start(struct rl_data *rl safe,
906 struct pane *p safe, struct pane *focus safe,
907 struct mark *start safe, struct mark *pm,
911 bool on_screen = False;
913 bool found_end = False;
917 /* This loop is fragile and sometimes spins. So ensure we
918 * never loop more than 1000 times.
920 if (pm && !rl->do_wrap && shifts++ < 1000) {
923 /* Need to check if side-shift is needed on cursor line */
925 call("doc:render-line-prev", focus, 0, m2);
927 m = vmark_at_or_before(focus, m2, rl->typenum, p);
930 if (m && refresh_all)
932 if (m && m->mdata && !vmark_is_valid(m)) {
933 pane_damaged(p, DAMAGED_REFRESH);
934 call("doc:render-line-prev", focus, 0, m);
935 call_render_line(p, focus, m, &start);
938 struct pane *hp = m->mdata;
939 int offset = call_render_line_to_point(focus,
941 measure_line(p, focus, m, offset);
942 prefix_len = pane_attr_get_int(
943 m->mdata, "prefix_len", -1);
944 curs_width = pane_attr_get_int(
945 m->mdata, "curs_width", 1);
947 while (hp->cx + curs_width >= p->w && shifts++ < 1000) {
948 int shift = 8 * curs_width;
951 rl->shift_left += shift;
952 measure_line(p, focus, m, offset);
955 while (hp->cx < prefix_len &&
956 rl->shift_left > 0 &&
958 hp->cx + curs_width * 8*curs_width < p->w) {
959 int shift = 8 * curs_width;
960 if (shift > rl->shift_left)
961 shift = rl->shift_left;
962 rl->shift_left -= shift;
963 measure_line(p, focus, m, offset);
970 struct pane *hp = rl->header->mdata;
972 measure_line(p, focus, rl->header, -1);
974 pane_resize(hp, hp->x, y, hp->w, hp->h);
979 y -= rl->skip_height;
980 start_of_file = doc_prior(focus, start) == WEOF;
981 for (m = start; m && !found_end && y < p->h; m = vmark_next(m)) {
985 call_render_line(p, focus, m, NULL);
986 found_end = measure_line(p, focus, m, -1);
988 if (!mark_valid(m) || !hp)
992 pane_damaged(p, DAMAGED_REFRESH);
993 hp->damaged &= ~DAMAGED_SIZE;
994 pane_resize(hp, hp->x, y, hp->w, hp->h);
995 if (hp->damaged & DAMAGED_SIZE && !rl->background_uniform)
996 pane_damaged(hp, DAMAGED_REFRESH);
1000 if (pm && m == start && rl->skip_height > 0 && m2 &&
1001 mark_ordered_not_same(pm, m2)) {
1002 /* Point might be in this line, but off top
1005 int offset = call_render_line_to_point(focus,
1008 measure_line(p, focus, m, offset);
1009 if (hp->cy >= rl->skip_height + rl->margin)
1010 /* Cursor is visible on this line
1011 * and after margin from top.
1014 else if (start_of_file && rl->skip_height == 0)
1015 /* Cannot make more margin space */
1018 } else if (pm && y >= p->h && m->seq < pm->seq) {
1019 /* point might be in this line, but off end
1022 int offset = call_render_line_to_point(focus,
1026 measure_line(p, focus, m, offset);
1027 lh = attr_find_int(hp->attrs,
1031 if (y - hp->h + hp->cy <= p->h - lh - rl->margin) {
1032 /* Cursor is on screen */
1036 } else if (pm && mark_ordered_or_same(m, pm) && m2 &&
1037 mark_ordered_or_same(pm, m2)) {
1038 if (rl->margin == 0)
1041 int offset = call_render_line_to_point(
1046 measure_line(p, focus, m, offset);
1047 lh = attr_find_int(hp->attrs,
1049 cy = y - hp->h + hp->cy;
1050 if (cy >= rl->margin &&
1051 cy <= p->h - rl->margin - lh)
1052 /* Cursor at least margin from edge */
1059 rl->tail_height = p->h - y;
1061 rl->tail_height = 0;
1062 if (mark_valid(m)) {
1064 while (mark_valid(m2 = vmark_next(m)) && m2) {
1065 /* end of view has clearly changed */
1066 rl->repositioned = 1;
1070 if (!pm || on_screen) {
1071 if (rl->repositioned) {
1072 rl->repositioned = 0;
1073 call("render:reposition", focus,
1074 rl->lines, vmark_first(focus,
1077 rl->cols, vmark_last(focus,
1087 DEF_CMD(render_lines_revise)
1089 struct pane *p = ci->home;
1090 struct pane *focus = ci->focus;
1091 struct rl_data *rl = p->data;
1092 struct mark *pm = NULL;
1093 struct mark *m1, *m2;
1094 bool refresh_all = False;
1098 a = pane_attr_get(focus, "render-wrap");
1099 if (rl->do_wrap != (!a || strcmp(a, "yes") ==0)) {
1100 rl->do_wrap = (!a || strcmp(a, "yes") ==0);
1104 rl->margin = pane_attr_get_int(focus, "render-vmargin", 0);
1105 if (rl->margin >= p->h/2)
1106 rl->margin = p->h/2;
1108 hdr = pane_attr_get(focus, "heading");
1114 rl->header = vmark_new(focus, MARK_UNGROUPED, NULL);
1116 vmark_set(p, rl->header, hdr);
1117 measure_line(p, focus, rl->header, -1);
1119 } else if (rl->header) {
1120 vmark_free(rl->header);
1124 if (!rl->ignore_point)
1125 pm = call_ret(mark, "doc:point", focus);
1126 m1 = vmark_first(focus, rl->typenum, p);
1127 m2 = vmark_last(focus, rl->typenum, p);
1129 if (m1 && !vmark_is_valid(m1))
1130 /* newline before might have been deleted, better check */
1131 call("doc:render-line-prev", focus, 0, m1);
1132 // FIXME double check that we invalidate line before any change...
1135 (!pm || (mark_ordered_or_same(m1,pm)))) {
1136 /* We maybe be able to keep m1 as start, if things work out.
1137 * So check all sub-panes are still valid and properly
1140 if (revalidate_start(rl, p, focus, m1, pm, refresh_all))
1143 /* Need to find a new top-of-display */
1145 pm = call_ret(mark, "doc:point", focus);
1147 /* Don't know what to do here... */
1149 find_lines(pm, p, focus, NO_NUMERIC);
1150 rl->repositioned = 0;
1151 call("render:reposition", focus,
1152 rl->lines, vmark_first(focus, rl->typenum, p), NULL,
1153 rl->cols, vmark_last(focus, rl->typenum, p), NULL,
1158 DEF_CMD(render_lines_refresh)
1160 struct pane *p = ci->home;
1161 struct pane *focus = ci->focus;
1162 struct rl_data *rl = p->data;
1163 struct mark *m, *pm = NULL;
1165 //pane_damaged(p, DAMAGED_VIEW);
1167 pm = call_ret(mark, "doc:point", focus);
1169 m = vmark_first(focus, rl->typenum, p);
1174 rl->lines = render(pm, p, focus);
1179 DEF_CMD(render_lines_close)
1181 struct rl_data *rl = ci->home->data;
1184 vmark_free(rl->header);
1190 DEF_CMD(render_lines_close_mark)
1192 struct mark *m = ci->mark;
1199 DEF_CMD(render_lines_abort)
1201 struct pane *p = ci->home;
1202 struct rl_data *rl = p->data;
1204 rl->ignore_point = 0;
1207 pane_damaged(p, DAMAGED_VIEW);
1209 /* Allow other handlers to complete the Abort */
1210 return Efallthrough;
1213 DEF_CMD(render_lines_move_view)
1216 * Find a new 'top' for the displayed region so that render()
1217 * will draw from there.
1218 * When moving backwards we move back a line and render it.
1219 * When moving forwards we render and then step forward
1220 * At each point we count the number of display lines that result.
1221 * When we choose a new start, we delete all earlier marks.
1222 * We also delete marks before current top when moving forward
1223 * where there are more than a page full.
1225 struct pane *p = ci->home;
1226 struct pane *focus = ci->focus;
1227 int rpt = RPT_NUM(ci);
1228 struct rl_data *rl = p->data;
1229 struct mark *top, *old_top;
1231 top = vmark_first(focus, rl->typenum, p);
1233 return Efallthrough;
1235 old_top = mark_dup(top);
1239 rl->ignore_point = 1;
1241 if (rl->skip_line_height <= 0)
1242 rl->skip_line_height = 1;
1245 /* Need to add new lines at the top and remove
1251 struct mark *prevtop = top;
1253 if (rl->skip_height) {
1254 rl->skip_height -= rl->skip_line_height;
1255 if (rl->skip_height < rl->skip_line_height/2)
1256 rl->skip_height = 0;
1257 rpt += rl->skip_line_height;
1263 m = mark_dup_view(top);
1264 top = call_render_line_prev(focus, m,
1266 if (!top && doc_prior(focus, prevtop) != WEOF) {
1267 /* Double check - maybe a soft top-of-file - Ctrl-L*/
1268 m = mark_dup(prevtop);
1270 top = call_render_line_prev(focus, m,
1276 while (m && m->seq < prevtop->seq &&
1277 !mark_same(m, prevtop)) {
1278 call_render_line(p, focus, m, NULL);
1279 if (m->mdata == NULL) {
1283 measure_line(p, focus, m, -1);
1287 /* FIXME remove extra lines, maybe add */
1288 rl->skip_height = y;
1291 /* Need to remove lines from top */
1292 call_render_line(p, focus, top, NULL);
1293 measure_line(p, focus, top, -1);
1294 while (top && top->mdata && rpt > 0) {
1298 if (rpt < y - rl->skip_height) {
1299 rl->skip_height += rpt;
1302 rpt -= y - rl->skip_height;
1303 rl->skip_height = 0;
1304 top = vmark_next(top);
1307 call_render_line(p, focus, top, NULL);
1308 measure_line(p, focus, top, -1);
1310 if (top && top->mdata) {
1311 /* We didn't fall off the end, so it is OK to remove
1312 * everything before 'top'
1315 while ((old = vmark_first(focus, rl->typenum, p)) != NULL &&
1320 rl->repositioned = 1;
1321 pane_damaged(ci->home, DAMAGED_VIEW);
1322 top = vmark_first(focus, rl->typenum, p);
1323 if (top && mark_same(top, old_top)) {
1331 static char *get_active_tag(const char *a)
1338 t = strstr(a, ",active-tag:");
1343 return strndup(t, c?c-t: (int)strlen(t));
1346 DEF_CMD(render_lines_set_cursor)
1349 * 1 if this resulted from a click
1350 * 2 if from a release
1352 * 0 any other reason.
1354 struct pane *p = ci->home;
1355 struct pane *focus = ci->focus;
1356 struct rl_data *rl = p->data;
1358 struct mark *m2 = NULL;
1362 cih = pane_mapxy(ci->focus, ci->home,
1363 ci->x >= 0 ? ci->x : p->cx >= 0 ? p->cx : 0,
1364 ci->y >= 0 ? ci->y : p->cy >= 0 ? p->cy : 0,
1367 m = vmark_first(p, rl->typenum, p);
1369 while (m && m->mdata && m->mdata->y + m->mdata->h <= cih.y &&
1374 /* There is nothing rendered? */
1377 /* chi is after the last visible content, and m is the end
1378 * of that content (possible EOF) so move there
1381 if (cih.y < m->mdata->y)
1382 cih.y = m->mdata->y;
1383 xypos = find_xy_line(p, focus, m, cih.x, cih.y);
1385 m2 = call_render_line_offset(focus, m, xypos);
1390 if (ci->num == 2) { /* Mouse release */
1391 xyattr = pane_attr_get(m->mdata, "xyattr");
1392 tag = get_active_tag(xyattr);
1395 asprintf(&c, "Mouse-Activate:%s", tag);
1397 call(c, focus, 0, m2, tag,
1398 0, ci->mark, xyattr);
1404 /* m is the closest we'll get */
1408 mark_to_mark(ci->mark, m);
1410 call("Move-to", focus, 0, m);
1416 DEF_CMD(render_lines_move_pos)
1418 struct pane *p = ci->home;
1419 struct pane *focus = ci->focus;
1420 struct rl_data *rl = p->data;
1421 struct mark *pm = ci->mark;
1422 struct mark *top, *bot;
1426 rl->ignore_point = 1;
1427 top = vmark_first(focus, rl->typenum, p);
1428 bot = vmark_last(focus, rl->typenum, p);
1429 if (top && rl->skip_height)
1430 /* top line not fully displayed, being in that line is
1432 top = vmark_next(top);
1434 /* last line might not be fully displayed, so don't assume */
1435 bot = vmark_prev(bot);
1437 !mark_ordered_or_same(top, pm) ||
1438 !mark_ordered_not_same(pm, bot))
1439 /* pos not displayed */
1440 find_lines(pm, p, focus, NO_NUMERIC);
1441 pane_damaged(p, DAMAGED_REFRESH);
1445 DEF_CMD(render_lines_view_line)
1447 struct pane *p = ci->home;
1448 struct pane *focus = ci->focus;
1449 struct rl_data *rl = p->data;
1450 struct mark *pm = ci->mark;
1455 if (line == NO_NUMERIC)
1458 rl->ignore_point = 1;
1459 find_lines(pm, p, focus, line);
1460 pane_damaged(p, DAMAGED_REFRESH);
1464 DEF_CMD(render_lines_move_line)
1466 /* FIXME should be able to select between display lines
1467 * and content lines - different when a line wraps.
1468 * For now just content lines.
1469 * target_x and target_y are the target location in a line
1470 * relative to the start of line.
1471 * We use doc:EOL to find a suitable start of line, then
1472 * render that line and find the last location not after x,y
1474 struct pane *p = ci->home;
1475 struct pane *focus = ci->focus;
1476 struct rl_data *rl = p->data;
1479 struct mark *m = ci->mark;
1483 m = call_ret(mark, "doc:point", focus);
1487 if (rl->target_x < 0) {
1488 rl->target_x = p->cx;
1489 rl->target_y = p->cy - rl->cursor_line;
1491 if (rl->target_x < 0)
1492 /* maybe not displayed yet */
1493 rl->target_x = rl->target_y = 0;
1497 if (call("doc:EOL", ci->focus, num, m, NULL, 1) <= 0) {
1501 if (RPT_NUM(ci) < 0) {
1502 /* at end of target line, move to start */
1503 if (call("doc:EOL", ci->focus, -1, m) <= 0) {
1509 start = vmark_new(focus, rl->typenum, p);
1512 mark_to_mark(start, m);
1513 start = call_render_line_prev(focus, start, 0, NULL);
1517 pane_damaged(p, DAMAGED_VIEW);
1521 if (vmark_first(focus, rl->typenum, p) == start) {
1522 /* New first mark, so view will have changed */
1523 rl->repositioned = 1;
1526 if (rl->target_x == 0 && rl->target_y == 0) {
1527 /* No need to move to target column - already there.
1528 * This simplifies life for render-complete which is
1529 * always at col 0, and messes with markup a bit.
1534 /* FIXME only do this if point is active/volatile, or
1535 * if start->mdata is NULL
1537 vmark_invalidate(start);
1538 call_render_line(p, focus, start, NULL);
1540 xypos = find_xy_line(p, focus, start, rl->target_x,
1541 rl->target_y + start->mdata->y);
1543 /* xypos is the distance from start-of-line to the target */
1545 struct mark *m2 = call_render_line_offset(
1546 focus, start, xypos);
1548 mark_to_mark(m, m2);
1555 DEF_CMD(render_lines_notify_replace)
1557 struct pane *p = ci->home;
1558 struct rl_data *rl = p->data;
1559 struct mark *start = ci->mark;
1560 struct mark *end = ci->mark2;
1563 if (strcmp(ci->key, "doc:replaced") == 0) {
1564 struct mark *pt = call_ret(mark, "doc:point", ci->home);
1566 /* If anyone changes the doc, reset the target. This might
1567 * be too harsh, but I mainly want target tracking for
1568 * close-in-time movement, so it probably doesn't matter.
1572 /* If the replacement happened at 'point', then stop
1573 * ignoring it, and handle the fact that point moved.
1575 if (ci->mark2 == pt)
1576 pane_call(p, "point:moving", ci->focus, 0, pt);
1579 if (strcmp(ci->key, "view:changed") == 0)
1580 /* Cursor possibly moved, so need to refresh */
1581 pane_damaged(ci->home, DAMAGED_REFRESH);
1583 if (!start && !end) {
1584 /* No marks given - assume everything changed */
1586 for (m = vmark_first(p, rl->typenum, p);
1589 vmark_invalidate(m);
1591 pane_damaged(p, DAMAGED_VIEW);
1592 return Efallthrough;
1595 if (start && end && start->seq > end->seq) {
1600 if (strcmp(ci->key, "doc:replaced") == 0) {
1601 first = vmark_first(ci->home, rl->typenum, p);
1602 if (first && start && end && mark_same(first, end))
1603 /* Insert just before visible region */
1604 mark_to_mark(first, start);
1608 start = vmark_at_or_before(ci->home, start, rl->typenum, p);
1610 start = vmark_first(ci->home, rl->typenum, p);
1612 start = vmark_at_or_before(ci->home, end, rl->typenum, p);
1614 /* change is before visible region */
1615 return Efallthrough;
1616 /* FIXME check 'start' is at least 'num' before end */
1619 end = vmark_at_or_before(ci->home, end, rl->typenum, p);
1621 end = vmark_last(ci->home, rl->typenum, p);
1622 } else if (start) { /* smatch needs to know start in not NULL */
1623 end = vmark_at_or_before(ci->home, start, rl->typenum, p);
1625 end = vmark_first(ci->home, rl->typenum, p);
1627 return Efallthrough;
1628 if (vmark_next(end))
1629 end = vmark_next(end);
1630 /* FIXME check that 'end' is at least 'num' after start */
1634 /* Change outside visible region */
1635 return Efallthrough;
1637 while (end && mark_ordered_or_same(start, end)) {
1638 vmark_invalidate(end);
1639 end = vmark_prev(end);
1641 /* Must be sure to invalidate the line *before* the change */
1643 vmark_invalidate(end);
1645 pane_damaged(p, DAMAGED_VIEW);
1647 return Efallthrough;
1650 DEF_CMD(render_lines_clip)
1652 struct rl_data *rl = ci->home->data;
1654 marks_clip(ci->home, ci->mark, ci->mark2, rl->typenum, ci->home,
1657 mark_clip(rl->header, ci->mark, ci->mark2, !!ci->num);
1658 return Efallthrough;
1661 DEF_CMD(render_lines_attach);
1662 DEF_CMD(render_lines_clone)
1664 struct pane *parent = ci->focus;
1666 render_lines_attach.func(ci);
1667 pane_clone_children(ci->home, parent->focus);
1671 DEF_CMD(render_lines_resize)
1673 struct pane *p = ci->home;
1674 struct rl_data *rl = p->data;
1677 for (m = vmark_first(p, rl->typenum, p);
1679 m = vmark_next(m)) {
1680 vmark_invalidate(m);
1681 pane_damaged(m->mdata, DAMAGED_REFRESH);
1683 rl->background_drawn = False;
1684 pane_damaged(p, DAMAGED_VIEW | DAMAGED_REFRESH);
1686 /* Allow propagation to children */
1690 DEF_CMD(render_send_reposition)
1692 /* Some (probably new) pane wants to know the extend of the
1693 * view, so resent render:resposition.
1695 struct pane *p = ci->home;
1696 struct rl_data *rl = p->data;
1698 rl->repositioned = 1;
1699 return Efallthrough;
1702 static struct map *rl_map;
1704 DEF_LOOKUP_CMD(render_lines_handle, rl_map);
1706 static void render_lines_register_map(void)
1708 rl_map = key_alloc();
1710 key_add(rl_map, "Move-View", &render_lines_move_view);
1711 key_add(rl_map, "Move-View-Pos", &render_lines_move_pos);
1712 key_add(rl_map, "Move-View-Line", &render_lines_view_line);
1713 key_add(rl_map, "Move-CursorXY", &render_lines_set_cursor);
1714 key_add(rl_map, "Move-Line", &render_lines_move_line);
1716 /* Make it easy to stop ignoring point */
1717 key_add(rl_map, "Abort", &render_lines_abort);
1719 key_add(rl_map, "Close", &render_lines_close);
1720 key_add(rl_map, "Close:mark", &render_lines_close_mark);
1721 key_add(rl_map, "Free", &edlib_do_free);
1722 key_add(rl_map, "Clone", &render_lines_clone);
1723 key_add(rl_map, "Refresh", &render_lines_refresh);
1724 key_add(rl_map, "Refresh:view", &render_lines_revise);
1725 key_add(rl_map, "Refresh:size", &render_lines_resize);
1726 key_add(rl_map, "Notify:clip", &render_lines_clip);
1727 key_add(rl_map, "get-attr", &render_lines_get_attr);
1728 key_add(rl_map, "point:moving", &render_lines_point_moving);
1730 key_add(rl_map, "doc:replaced", &render_lines_notify_replace);
1731 key_add(rl_map, "doc:replaced-attr", &render_lines_notify_replace);
1732 /* view:changed is sent to a tile when the display might need
1733 * to change, even though the doc may not have*/
1734 key_add(rl_map, "view:changed", &render_lines_notify_replace);
1735 key_add(rl_map, "render:request:reposition", &render_send_reposition);
1738 REDEF_CMD(render_lines_attach)
1744 render_lines_register_map();
1751 if (strcmp(ci->key, "attach-render-text") == 0)
1752 p = call_ret(pane, "attach-markup", p);
1753 p = pane_register(p, 0, &render_lines_handle.c, rl);
1758 rl->typenum = home_call(ci->focus, "doc:add-view", p) - 1;
1759 call("doc:request:doc:replaced", p);
1760 call("doc:request:doc:replaced-attr", p);
1761 call("doc:request:point:moving", p);
1763 return comm_call(ci->comm2, "callback:attach", p);
1766 void edlib_init(struct pane *ed safe)
1768 call_comm("global-set-command", ed, &render_lines_attach, 0, NULL,
1769 "attach-render-lines");
1770 call_comm("global-set-command", ed, &render_lines_attach, 0, NULL,
1771 "attach-render-text");