]> git.neil.brown.name Git - edlib.git/blob - render-lines.c
render-complete: fix render_complete_line again.
[edlib.git] / render-lines.c
1 /*
2  * Copyright Neil Brown ©2015-2021 <neil@brown.name>
3  * May be distributed under terms of GPLv2 - see file:COPYING
4  *
5  * Rendering for any document which presents as a sequence of lines.
6  *
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
19  * be NO_NUMERIC
20  *
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.
24  *
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.
30  *
31  * Other control characters should be rendered as
32  * e.g. <fg:red>^X</> - in particular, nul must not appear in the line.
33  *
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
45  * line.
46  * The last mark may also be mid-line, and it must never have an
47  * attached rendering.
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.
53  *
54  * To render the pane we:
55  * 1/ call 'render-line-prev' on a mark at the point and look for that mark
56  *    in the view.
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
62  *    height between.
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.
71  *
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.
75  *
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.
79  */
80
81 #define MARK_DATA_PTR struct pane
82 #define _GNU_SOURCE /*  for asprintf */
83 #include "core.h"
84 #include "misc.h"
85 #include <stdio.h> /* snprintf */
86
87 /*
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
95  * in one region.
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'.
99  */
100
101 struct rl_data {
102         int             top_sol; /* true when first mark is at a start-of-line */
103         int             ignore_point;
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
108                                       * on this line */
109         short           target_x, target_y;
110         short           i_moved;        /* I moved cursor, so don't clear
111                                          * target
112                                          */
113         int             do_wrap;
114         short           shift_left;
115         short           shift_left_last_refresh;
116         struct mark     *header;
117         int             typenum;
118         int             repositioned; /* send "render:reposition" when we know
119                                        * full position again.
120                                        */
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;
126
127         /* If cursor not visible, we add this pane in bottom-right and place
128          * cursor there.
129          */
130         struct pane     *cursor_pane;
131 };
132
133 static void vmark_clear(struct mark *m safe)
134 {
135         if (m->mdata) {
136                 pane_close(m->mdata);
137                 m->mdata = NULL;
138         }
139 }
140
141 static void vmark_free(struct mark *m safe)
142 {
143         vmark_clear(m);
144         mark_free(m);
145 }
146
147 static void vmark_set(struct pane *p safe, struct mark *m safe, char *line safe)
148 {
149         if (!m->mdata)
150                 m->mdata = call_ret(pane, "attach-renderline", p);
151         if (m->mdata)
152                 pane_call(m->mdata, "render-line:set", p, 0, NULL, line);
153 }
154
155 static void vmark_invalidate(struct mark *m safe)
156 {
157         if (m->mdata)
158                 pane_damaged(m->mdata, DAMAGED_VIEW);
159 }
160
161 static bool vmark_is_valid(struct mark *m safe)
162 {
163         return mark_valid(m) && m->mdata && !(m->mdata->damaged & DAMAGED_VIEW);
164 }
165
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)
169 {
170         struct pane *hp = mk->mdata;
171         int ret = 0;
172
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);
177         }
178         /* end-of-page flag */
179         return ret == 2;
180 }
181
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)
185 {
186         struct pane *hp = mk->mdata;
187         int ret = 0;
188
189         if (hp) {
190                 ret = pane_call(hp,
191                                 "render-line:findxy",
192                                 focus,
193                                 -1, NULL, NULL,
194                                 0, NULL, NULL,
195                                 posx - hp->x, posy - hp->y);
196         }
197         /* xypos */
198         return ret > 0 ? (ret - 1) : -1;
199 }
200
201 static void draw_line(struct pane *p safe, struct pane *focus safe,
202                       struct mark *mk safe, short offset, bool refresh_all)
203 {
204         struct pane *hp = mk->mdata;
205
206         if (hp &&
207             (refresh_all || hp->damaged & DAMAGED_REFRESH)) {
208                 hp->damaged &= ~DAMAGED_REFRESH;
209                 pane_call(hp, "render-line:draw", focus, offset);
210         }
211 }
212
213 static struct mark *call_render_line_prev(struct pane *p safe,
214                                           struct mark *m safe,
215                                           int n, int *found)
216 {
217         int ret;
218         struct mark *m2;
219
220         if (m->viewnum < 0)
221                 return NULL;
222         ret = call("doc:render-line-prev", p, n, m);
223         if (ret <= 0) {
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.
226                  */
227                 if (found)
228                         *found = (ret == Efail);
229                 mark_free(m);
230                 return NULL;
231         }
232         if (ret < 0) {
233                 /* current line is start-of-file */
234                 mark_free(m);
235                 return NULL;
236         }
237
238         m2 = vmark_matching(m);
239         if (m2)
240                 mark_free(m);
241         else
242                 m2 = m;
243         return m2;
244 }
245
246 static void call_render_line(struct pane *home safe, struct pane *p safe,
247                              struct mark *start safe, struct mark **end)
248 {
249         struct mark *m, *m2;
250         char *s;
251
252         if (vmark_is_valid(start))
253                 return;
254
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.
259                  */
260                 call("doc:render-line-prev", p, 0, m);
261                 if (!mark_same(m, start)) {
262                         mark_free(m);
263                         vmark_clear(start);
264                         return;
265                 }
266                 s = "";
267         } else
268                 s = call_ret(strsave, "doc:render-line", p, NO_NUMERIC, m);
269
270         if (!mark_valid(start)) {
271                 mark_free(m);
272                 return;
273         }
274         if (s)
275                 vmark_set(home, start, s);
276
277         m2 = vmark_matching(m);
278         if (m2)
279                 mark_free(m);
280         else
281                 m2 = m;
282         /*FIXME shouldn't be needed */
283         m2 = safe_cast m2;
284
285         /* Any mark between start and m2 must be discarded,
286          */
287         while ((m = vmark_next(start)) != NULL &&
288                m->seq < m2->seq) {
289                 if (end && m == *end)
290                         *end = m2;
291                 vmark_free(m);
292         }
293         /* Any mark at same location as m2 must go too. */
294         while ((m = vmark_next(m2)) != NULL &&
295                mark_same(m, m2)) {
296                 if (end && m == *end)
297                         *end = m2;
298                 vmark_free(m);
299         }
300         /* Any mark at same location as start must go too. */
301         while ((m = vmark_prev(start)) != NULL &&
302                mark_same(m, start)) {
303                 vmark_free(m);
304         }
305 }
306
307 DEF_CMD(no_save)
308 {
309         return 1;
310 }
311
312 static struct mark *call_render_line_offset(struct pane *p safe,
313                                             struct mark *start safe, int offset)
314 {
315         struct mark *m;
316
317         m = mark_dup_view(start);
318         if (call_comm("doc:render-line", p, &no_save, offset, m) <= 0) {
319                 mark_free(m);
320                 return NULL;
321         }
322         return m;
323 }
324
325 DEF_CMD(get_len)
326 {
327         if (ci->str) {
328                 int l = strlen(ci->str);
329                 while (l >=4 && strncmp(ci->str+l-3, "</>", 3) == 0 &&
330                 ci->str[l-4] != '<')
331                         l -= 3;
332                 return l + 1;
333         } else
334                 return 1;
335 }
336
337 static int call_render_line_to_point(struct pane *p safe, struct mark *pm safe,
338                                      struct mark *start safe)
339 {
340         int len;
341         struct mark *m = mark_dup_view(start);
342
343         len = call_comm("doc:render-line", p, &get_len, -1, m, NULL, 0, pm);
344         mark_free(m);
345         if (len <= 0)
346                 return 0;
347
348         return len - 1;
349 }
350
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.
360  */
361
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)
365 {
366         /* step backwards moving start */
367         struct rl_data *rl = p->data;
368         struct mark *m;
369         bool found_start = False;
370         struct mark *start = *startp;
371
372         if (!start)
373                 return True;
374         m = call_render_line_prev(focus, mark_dup_view(start),
375                                   1, &rl->top_sol);
376         if (!m) {
377                 /* no text before 'start' */
378                 found_start = True;
379         } else {
380                 short h = 0;
381                 start = m;
382                 call_render_line(p, focus, start, endp);
383                 measure_line(p, focus, start, -1);
384                 h = start->mdata ? start->mdata->h : 0;
385                 if (h) {
386                         *y_pre = h;
387                         *line_height_pre =
388                                 attr_find_int(start->mdata->attrs,
389                                               "line-height");
390                 } else
391                         found_start = True;
392         }
393         *startp = start;
394         return found_start;
395 }
396
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)
400 {
401         struct mark *end = *endp;
402         bool found_end = False;
403
404         if (!end)
405                 return True;
406         call_render_line(p, focus, end, startp);
407         found_end = measure_line(p, focus, end, -1);
408         if (end->mdata)
409                 *y_post = end->mdata->h;
410         if (*y_post > 0 && end->mdata)
411                 *line_height_post =
412                         attr_find_int(end->mdata->attrs,
413                                       "line-height");
414         if (!end->mdata || !end->mdata->h)
415                 end = NULL;
416         else
417                 end = vmark_next(end);
418         if (!end) {
419                 found_end = 1;
420                 if (p->h >= *line_height_post *2)
421                         *y_post = p->h / 10;
422         }
423
424         *endp = end;
425         return found_end;
426 }
427
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)
433 {
434         int y_pre = *y_prep;
435         int y_post = *y_postp;
436
437         if (y_pre > 0 && y_post > 0) {
438                 int consume = (y_post < y_pre
439                                ? y_post : y_pre) * 2;
440                 int above, below;
441                 if (consume > p->h - y)
442                         consume = p->h - y;
443                 if (y_pre > y_post) {
444                         above = consume - (consume/2);
445                         below = consume/2;
446                 } else {
447                         below = consume - (consume/2);
448                         above = consume/2;
449                 }
450                 y += above + below;
451                 y_pre -= above;
452                 *lines_above += above / (line_height_pre?:1);
453                 y_post -= below;
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
457                  * both > 0
458                  */
459         }
460         if (found_end && y_pre) {
461                 int consume = p->h - y;
462                 if (consume > y_pre)
463                         consume = y_pre;
464                 y_pre -= consume;
465                 y += consume;
466                 *lines_above += consume / (line_height_pre?:1);
467         }
468         if (found_start && y_post) {
469                 int consume = p->h - y;
470                 if (consume > y_post)
471                         consume = y_post;
472                 y_post -= consume;
473                 y += consume;
474                 *lines_below += consume / (line_height_post?:1);
475         }
476         *y_prep = y_pre;
477         *y_postp = y_post;
478         return y;
479 }
480
481 /*
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.
490  *
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
497  *    moving upward.
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
500  *    moving downward
501  *
502  * If we decide to stop moving in both directions, but have not
503  * reached EOF or full height of display, keep moving downwards.
504  */
505 static void find_lines(struct mark *pm safe, struct pane *p safe,
506                        struct pane *focus safe,
507                        int vline)
508 {
509         struct rl_data *rl = p->data;
510         struct mark *orig_top, *orig_bot;
511         struct mark *top, *bot;  // boundary of previous display
512         struct mark *m;
513         struct mark *start, *end; // current estimate for new display
514         short y = 0;
515         short lines_above = 0, lines_below = 0; /* distance from start/end
516                                                  * to pm.
517                                                  */
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.
522          */
523         short y_pre = 0, y_post = 0;
524         short line_height_pre = 1, line_height_post = 1;
525
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 */
529         if (orig_top)
530                 orig_top = mark_dup(orig_top);
531         if (orig_bot)
532                 orig_bot = mark_dup(orig_bot);
533
534         start = vmark_new(focus, rl->typenum, p);
535         if (!start)
536                 goto abort;
537         rl->repositioned = 1;
538         mark_to_mark(start, pm);
539         start = call_render_line_prev(focus, start, 0, &rl->top_sol);
540         if (!start)
541                 goto abort;
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.
547          */
548
549         rl->shift_left = 0;
550
551         /* ->cy is top of cursor, we want to measure from bottom */
552         if (start->mdata) {
553                 struct pane *hp = start->mdata;
554                 int curs_width;
555                 found_end = measure_line(p, focus, start, offset);
556
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;
562                         if (shift > hp->cx)
563                                 shift = hp->cx;
564                         rl->shift_left += shift;
565                         measure_line(p, focus, start, offset);
566                 }
567                 line_height_pre = attr_find_int(start->mdata->attrs, "line-height");
568                 if (line_height_pre < 1)
569                         line_height_pre = 1;
570                 y_pre = start->mdata->cy + line_height_pre;
571                 y_post = start->mdata->h - y_pre;
572         } else {
573                 /* Should never happen */
574                 y_pre = 0;
575                 y_post = 0;
576         }
577         if (!end) {
578                 found_end = True;
579                 if (p->h > line_height_pre * 2)
580                         y_post += p->h / 10;
581                 else
582                         /* Small display, no space at EOF */
583                         y_post = 0;
584         }
585         y = 0;
586         if (rl->header && rl->header->mdata)
587                 y = rl->header->mdata->h;
588
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
591          * vertical space.
592          */
593
594         if (vline != NO_NUMERIC) {
595                 /* ignore current position - top/bot irrelevant */
596                 top = NULL;
597                 bot = NULL;
598         } else {
599                 top = orig_top;
600                 bot = orig_bot;
601         }
602
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)
607                                 found_start = True;
608                         if (!found_end && vline < 0 &&
609                             lines_below >= -vline-1)
610                                 found_end = True;
611                 }
612                 if (!found_start && y_pre <= 0)
613                         found_start = step_back(p, focus, &start, &end,
614                                                 &y_pre, &line_height_pre);
615
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.
621                          */
622                         y_post = 0;
623
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.
630                              */
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.
635                                  */
636                                 found_end = True;
637
638                 if (!found_end && y_post <= 0)
639                         /* step forwards */
640                         found_end = step_fore(p, focus, &start, &end,
641                                               &y_post, &line_height_post);
642
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))
649                                 found_start = True;
650
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);
655         }
656         /* We might need to continue downwards even after found_end
657          * if there is more space.
658          */
659         found_end = end == NULL;
660         while (!found_end && y < p->h) {
661                 if (y_post <= 0)
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);
668         }
669
670         if (start->mdata && start->mdata->h <= y_pre) {
671                 y_pre = 0;
672                 m = vmark_next(start);
673                 vmark_free(start);
674                 start = m;
675         }
676         if (!start)
677                 goto abort;
678
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
685                  * freeing it.
686                  */
687                 end = start;
688         while ((m = vmark_prev(start)) != NULL)
689                 vmark_free(m);
690
691         if (end) {
692                 while ((m = vmark_next(end)) != NULL)
693                         vmark_free(m);
694
695                 vmark_clear(end);
696         }
697
698         y = 0;
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);
709                 y += hp->h;
710         }
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;
718
719 abort:
720         mark_free(orig_top);
721         mark_free(orig_bot);
722 }
723
724 DEF_CMD(cursor_handle)
725 {
726         return 0;
727 }
728
729 static int render(struct mark *pm, struct pane *p safe,
730                   struct pane *focus safe)
731 {
732         struct rl_data *rl = p->data;
733         short y = 0;
734         struct mark *m, *m2;
735         struct xy scale = pane_scale(focus);
736         char *s;
737         int hide_cursor = 0;
738         int cursor_drawn = 0;
739         bool refresh_all = rl->shift_left != rl->shift_left_last_refresh;
740
741         rl->shift_left_last_refresh = rl->shift_left;
742         s = pane_attr_get(focus, "hide-cursor");
743         if (s && strcmp(s, "yes") == 0)
744                 hide_cursor = 1;
745
746         rl->cols = 0;
747         m = vmark_first(focus, rl->typenum, p);
748         if (!rl->background_drawn) {
749                 refresh_all = True;
750                 rl->background_uniform = True;
751         }
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);
756                 refresh_all = True;
757                 rl->background_uniform = False;
758         } else if (rl->background_drawn)
759                 ;
760         else if (!s)
761                 home_call(focus, "Draw:clear", p, 0, NULL, "");
762         else if (strncmp(s, "color:", 6) == 0) {
763                 char *a = strdup(s);
764                 strcpy(a, "bg:");
765                 strcpy(a+3, s+6);
766                 home_call(focus, "Draw:clear", p, 0, NULL, a);
767                 free(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;
772         } else
773                 home_call(focus, "Draw:clear", p, 0, NULL, "");
774         rl->background_drawn = True;
775
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);
779                 y = hp->h;
780                 rl->cols = hp->x + hp->w;
781         }
782         y -= rl->skip_height;
783
784         p->cx = p->cy = -1;
785         rl->cursor_line = 0;
786
787         while (m && m->mdata) {
788                 m2 = vmark_next(m);
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))) {
793                         struct xy curs;
794                         struct pane *hp = m->mdata;
795                         short len = call_render_line_to_point(focus, pm,
796                                                               m);
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);
800                         if (hp->cx < 0) {
801                                 p->cx = -1;
802                                 p->cy = -1;
803                         } else {
804                                 p->cx = curs.x;
805                                 p->cy = curs.y;
806                         }
807                         cursor_drawn = 1;
808                 } else {
809                         draw_line(p, focus, m, -1, refresh_all);
810                 }
811                 if (m->mdata) {
812                         int cols = m->mdata->x + m->mdata->w;
813                         if (cols > rl->cols)
814                                 rl->cols = cols;
815                         y = m->mdata->y + m->mdata->h;
816                 }
817                 m = m2;
818         }
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;
824                 short mwidth = -1;
825                 short lineheight;
826
827                 if (!cp) {
828                         cp = pane_register(p, -1, &cursor_handle);
829                         rl->cursor_pane = cp;
830                 }
831                 if (m)
832                         m2 = vmark_prev(m);
833                 else
834                         m2 = vmark_last(focus, rl->typenum, p);
835
836                 while (m2 && mwidth <= 0) {
837                         if (m2->mdata) {
838                                 mwidth = pane_attr_get_int(
839                                         m2->mdata, "curs_width", -1);
840                                 lineheight = pane_attr_get_int(
841                                         m2->mdata, "line-height", -1);
842                         }
843                         m2 = vmark_prev(m2);
844                 }
845
846                 if (mwidth <= 0) {
847                         mwidth = 1;
848                         lineheight = 1;
849                 }
850                 if (cp) {
851                         pane_resize(cp,
852                                     p->w - mwidth,
853                                     p->h - lineheight,
854                                     mwidth, lineheight);
855
856                         home_call(focus, "Draw:clear", cp);
857                         home_call(focus, "Draw:text", cp, 0, NULL, " ",
858                                   scale.x, NULL, "",
859                                   0, lineheight-1);
860                 }
861         } else if (rl->cursor_pane) {
862                 pane_close(rl->cursor_pane);
863                 rl->cursor_pane = NULL;
864         }
865         return y;
866 }
867
868 DEF_CMD(render_lines_get_attr)
869 {
870         struct rl_data *rl = ci->home->data;
871
872         if (ci->str && strcmp(ci->str, "shift_left") == 0) {
873                 char ret[10];
874                 if (rl->do_wrap)
875                         return comm_call(ci->comm2, "cb", ci->focus,
876                                          0, NULL, "-1");
877                 snprintf(ret, sizeof(ret), "%d", rl->shift_left);
878                 return comm_call(ci->comm2, "cb", ci->focus, 0, NULL, ret);
879         }
880         return Efallthrough;
881 }
882
883 DEF_CMD(render_lines_point_moving)
884 {
885         struct pane *p = ci->home;
886         struct rl_data *rl = p->data;
887         struct mark *pt = call_ret(mark, "doc:point", ci->home);
888         struct mark *m;
889
890         if (!pt || ci->mark != pt)
891                 return 1;
892         /* Stop igoring point, because it is probably relevant now */
893         rl->ignore_point = 0;
894         if (!rl->i_moved)
895                 /* Someone else moved the point, so reset target column */
896                 rl->target_x = -1;
897         m = vmark_at_or_before(ci->focus, pt, rl->typenum, p);
898         if (m && m->mdata) {
899                 pane_damaged(m->mdata, DAMAGED_REFRESH);
900                 pane_damaged(m->mdata->parent, DAMAGED_REFRESH);
901         }
902         return 1;
903 }
904
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,
908                             bool refresh_all)
909 {
910         int y;
911         bool on_screen = False;
912         struct mark *m, *m2;
913         bool found_end = False;
914         bool start_of_file;
915         int shifts = 0;
916
917         /* This loop is fragile and sometimes spins.  So ensure we
918          * never loop more than 1000 times.
919          */
920         if (pm && !rl->do_wrap && shifts++ < 1000) {
921                 int prefix_len;
922                 int curs_width;
923                 /* Need to check if side-shift is needed on cursor line */
924                 m2 = mark_dup(pm);
925                 call("doc:render-line-prev", focus, 0, m2);
926
927                 m = vmark_at_or_before(focus, m2, rl->typenum, p);
928                 mark_free(m2);
929
930                 if (m && refresh_all)
931                         vmark_invalidate(m);
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);
936                 }
937                 if (m && m->mdata) {
938                         struct pane *hp = m->mdata;
939                         int offset = call_render_line_to_point(focus,
940                                                                pm, m);
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);
946
947                         while (hp->cx + curs_width >= p->w && shifts++ < 1000) {
948                                 int shift = 8 * curs_width;
949                                 if (shift > hp->cx)
950                                         shift = hp->cx;
951                                 rl->shift_left += shift;
952                                 measure_line(p, focus, m, offset);
953                                 refresh_all = 1;
954                         }
955                         while (hp->cx < prefix_len &&
956                                rl->shift_left > 0 &&
957                                shifts++ < 1000 &&
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);
964                                 refresh_all = 1;
965                         }
966                 }
967         }
968         y = 0;
969         if (rl->header) {
970                 struct pane *hp = rl->header->mdata;
971                 if (refresh_all) {
972                         measure_line(p, focus, rl->header, -1);
973                         if (hp)
974                                 pane_resize(hp, hp->x, y, hp->w, hp->h);
975                 }
976                 if (hp)
977                         y = hp->h;
978         }
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)) {
982                 struct pane *hp;
983                 if (refresh_all)
984                         vmark_invalidate(m);
985                 call_render_line(p, focus, m, NULL);
986                 found_end = measure_line(p, focus, m, -1);
987                 hp = m->mdata;
988                 if (!mark_valid(m) || !hp)
989                         break;
990
991                 if (y != hp->y) {
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);
997                 }
998                 y += hp->h;
999                 m2 = vmark_next(m);
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
1003                          * of the screen
1004                          */
1005                         int offset = call_render_line_to_point(focus,
1006                                                                pm, m);
1007                         if (offset >= 0) {
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.
1012                                          */
1013                                         on_screen = True;
1014                                 else if (start_of_file && rl->skip_height == 0)
1015                                         /* Cannot make more margin space */
1016                                         on_screen = True;
1017                         }
1018                 } else if (pm && y >= p->h && m->seq < pm->seq) {
1019                         /* point might be in this line, but off end
1020                          * of the screen
1021                          */
1022                         int offset = call_render_line_to_point(focus,
1023                                                                pm, m);
1024                         if (offset > 0) {
1025                                 int lh;
1026                                 measure_line(p, focus, m, offset);
1027                                 lh = attr_find_int(hp->attrs,
1028                                                    "line-height");
1029                                 if (lh <= 0)
1030                                         lh = 1;
1031                                 if (y - hp->h + hp->cy <= p->h - lh - rl->margin) {
1032                                         /* Cursor is on screen */
1033                                         on_screen = True;
1034                                 }
1035                         }
1036                 } else if (pm && mark_ordered_or_same(m, pm) && m2 &&
1037                            mark_ordered_or_same(pm, m2)) {
1038                         if (rl->margin == 0)
1039                                 on_screen = True;
1040                         else {
1041                                 int offset = call_render_line_to_point(
1042                                         focus, pm, m);
1043                                 if (offset > 0) {
1044                                         int lh;
1045                                         int cy;
1046                                         measure_line(p, focus, m, offset);
1047                                         lh = attr_find_int(hp->attrs,
1048                                                            "line-height");
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 */
1053                                                 on_screen = True;
1054                                 }
1055                         }
1056                 }
1057         }
1058         if (y >= p->h)
1059                 rl->tail_height = p->h - y;
1060         else
1061                 rl->tail_height = 0;
1062         if (mark_valid(m)) {
1063                 vmark_clear(m);
1064                 while (mark_valid(m2 = vmark_next(m)) && m2) {
1065                         /* end of view has clearly changed */
1066                         rl->repositioned = 1;
1067                         vmark_free(m2);
1068                 }
1069         }
1070         if (!pm || on_screen) {
1071                 if (rl->repositioned) {
1072                         rl->repositioned = 0;
1073                         call("render:reposition", focus,
1074                              rl->lines, vmark_first(focus,
1075                                                     rl->typenum,
1076                                                     p), NULL,
1077                              rl->cols, vmark_last(focus,
1078                                                   rl->typenum,
1079                                                   p), NULL,
1080                              p->cx, p->cy);
1081                 }
1082                 return 1;
1083         }
1084         return 0;
1085 }
1086
1087 DEF_CMD(render_lines_revise)
1088 {
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;
1095         char *hdr;
1096         char *a;
1097
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);
1101                 refresh_all = True;
1102         }
1103
1104         rl->margin = pane_attr_get_int(focus, "render-vmargin", 0);
1105         if (rl->margin >= p->h/2)
1106                 rl->margin = p->h/2;
1107
1108         hdr = pane_attr_get(focus, "heading");
1109         if (hdr && !*hdr)
1110                 hdr = NULL;
1111
1112         if (hdr) {
1113                 if (!rl->header)
1114                         rl->header = vmark_new(focus, MARK_UNGROUPED, NULL);
1115                 if (rl->header) {
1116                         vmark_set(p, rl->header, hdr);
1117                         measure_line(p, focus, rl->header, -1);
1118                 }
1119         } else if (rl->header) {
1120                 vmark_free(rl->header);
1121                 rl->header = NULL;
1122         }
1123
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);
1128
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...
1133
1134         if (m1 && m2 &&
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
1138                  * positioned.
1139                  */
1140                 if (revalidate_start(rl, p, focus, m1, pm, refresh_all))
1141                         return 1;
1142         }
1143         /* Need to find a new top-of-display */
1144         if (!pm)
1145                 pm = call_ret(mark, "doc:point", focus);
1146         if (!pm)
1147                 /* Don't know what to do here... */
1148                 return 1;
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,
1154              p->cx, p->cy);
1155         return 1;
1156 }
1157
1158 DEF_CMD(render_lines_refresh)
1159 {
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;
1164
1165         //pane_damaged(p, DAMAGED_VIEW);
1166
1167         pm = call_ret(mark, "doc:point", focus);
1168
1169         m = vmark_first(focus, rl->typenum, p);
1170
1171         if (!m)
1172                 return 1;
1173
1174         rl->lines = render(pm, p, focus);
1175
1176         return 1;
1177 }
1178
1179 DEF_CMD(render_lines_close)
1180 {
1181         struct rl_data *rl = ci->home->data;
1182
1183         if (rl->header)
1184                 vmark_free(rl->header);
1185         rl->header = NULL;
1186
1187         return 1;
1188 }
1189
1190 DEF_CMD(render_lines_close_mark)
1191 {
1192         struct mark *m = ci->mark;
1193
1194         if (m)
1195                 vmark_clear(m);
1196         return 1;
1197 }
1198
1199 DEF_CMD(render_lines_abort)
1200 {
1201         struct pane *p = ci->home;
1202         struct rl_data *rl = p->data;
1203
1204         rl->ignore_point = 0;
1205         rl->target_x = -1;
1206
1207         pane_damaged(p, DAMAGED_VIEW);
1208
1209         /* Allow other handlers to complete the Abort */
1210         return Efallthrough;
1211 }
1212
1213 DEF_CMD(render_lines_move_view)
1214 {
1215         /*
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.
1224          */
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;
1230
1231         top = vmark_first(focus, rl->typenum, p);
1232         if (!top)
1233                 return Efallthrough;
1234
1235         old_top = mark_dup(top);
1236         rpt *= p->h ?: 1;
1237         rpt /= 1000;
1238
1239         rl->ignore_point = 1;
1240
1241         if (rl->skip_line_height <= 0)
1242                 rl->skip_line_height = 1;
1243
1244         if (rpt < 0) {
1245                 /* Need to add new lines at the top and remove
1246                  * at the bottom.
1247                  */
1248                 while (rpt < 0) {
1249                         short y = 0;
1250                         struct mark *m;
1251                         struct mark *prevtop = top;
1252
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;
1258                                 if (rpt > 0)
1259                                         rpt = 0;
1260                                 continue;
1261                         }
1262
1263                         m = mark_dup_view(top);
1264                         top = call_render_line_prev(focus, m,
1265                                                     1, &rl->top_sol);
1266                         if (!top && doc_prior(focus, prevtop) != WEOF) {
1267                                 /* Double check - maybe a soft top-of-file - Ctrl-L*/
1268                                 m = mark_dup(prevtop);
1269                                 doc_prev(focus, m);
1270                                 top = call_render_line_prev(focus, m,
1271                                                             1, &rl->top_sol);
1272                         }
1273                         if (!top)
1274                                 break;
1275                         m = top;
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) {
1280                                         rpt = 0;
1281                                         break;
1282                                 }
1283                                 measure_line(p, focus, m, -1);
1284                                 y += m->mdata->h;
1285                                 m = vmark_next(m);
1286                         }
1287                         /* FIXME remove extra lines, maybe add */
1288                         rl->skip_height = y;
1289                 }
1290         } else {
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) {
1295                         short y = 0;
1296
1297                         y = top->mdata->h;
1298                         if (rpt < y - rl->skip_height) {
1299                                 rl->skip_height += rpt;
1300                                 break;
1301                         }
1302                         rpt -= y - rl->skip_height;
1303                         rl->skip_height = 0;
1304                         top = vmark_next(top);
1305                         if (!top)
1306                                 break;
1307                         call_render_line(p, focus, top, NULL);
1308                         measure_line(p, focus, top, -1);
1309                 }
1310                 if (top && top->mdata) {
1311                         /* We didn't fall off the end, so it is OK to remove
1312                          * everything before 'top'
1313                          */
1314                         struct mark *old;
1315                         while ((old = vmark_first(focus, rl->typenum, p)) != NULL &&
1316                                old != top)
1317                                 vmark_free(old);
1318                 }
1319         }
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)) {
1324                 mark_free(old_top);
1325                 return 2;
1326         }
1327         mark_free(old_top);
1328         return 1;
1329 }
1330
1331 static char *get_active_tag(const char *a)
1332 {
1333         char *t;
1334         char *c;
1335
1336         if (!a)
1337                 return NULL;
1338         t = strstr(a, ",active-tag:");
1339         if (!t)
1340                 return NULL;
1341         t += 12;
1342         c = strchr(t, ',');
1343         return strndup(t, c?c-t: (int)strlen(t));
1344 }
1345
1346 DEF_CMD(render_lines_set_cursor)
1347 {
1348         /* ->num is
1349          * 1 if this resulted from a click
1350          * 2 if from a release
1351          * 3 if from motion
1352          * 0 any other reason.
1353          */
1354         struct pane *p = ci->home;
1355         struct pane *focus = ci->focus;
1356         struct rl_data *rl = p->data;
1357         struct mark *m;
1358         struct mark *m2 = NULL;
1359         struct xy cih;
1360         int xypos;
1361
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,
1365                          False);
1366
1367         m = vmark_first(p, rl->typenum, p);
1368
1369         while (m && m->mdata && m->mdata->y + m->mdata->h <= cih.y &&
1370                vmark_next(m))
1371                 m = vmark_next(m);
1372
1373         if (!m)
1374                 /* There is nothing rendered? */
1375                 return 1;
1376         if (!m->mdata) {
1377                 /* chi is after the last visible content, and m is the end
1378                  * of that content (possible EOF) so move there
1379                  */
1380         } else {
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);
1384                 if (xypos >= 0)
1385                         m2 = call_render_line_offset(focus, m, xypos);
1386         }
1387         if (m2) {
1388                 char *tag, *xyattr;
1389
1390                 if (ci->num == 2) { /* Mouse release */
1391                         xyattr = pane_attr_get(m->mdata, "xyattr");
1392                         tag = get_active_tag(xyattr);
1393                         if (tag) {
1394                                 char *c = NULL;
1395                                 asprintf(&c, "Mouse-Activate:%s", tag);
1396                                 if (c)
1397                                         call(c, focus, 0, m2, tag,
1398                                              0, ci->mark, xyattr);
1399                                 free(c);
1400                         }
1401                 }
1402                 m = m2;
1403         } else {
1404                 /* m is the closest we'll get */
1405         }
1406
1407         if (ci->mark)
1408                 mark_to_mark(ci->mark, m);
1409         else
1410                 call("Move-to", focus, 0, m);
1411         mark_free(m2);
1412
1413         return 1;
1414 }
1415
1416 DEF_CMD(render_lines_move_pos)
1417 {
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;
1423
1424         if (!pm)
1425                 return Enoarg;
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
1431                  * not sufficient */
1432                 top = vmark_next(top);
1433         if (bot)
1434                 /* last line might not be fully displayed, so don't assume */
1435                 bot = vmark_prev(bot);
1436         if (!top || !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);
1442         return 1;
1443 }
1444
1445 DEF_CMD(render_lines_view_line)
1446 {
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;
1451         int line = ci->num;
1452
1453         if (!pm)
1454                 return Enoarg;
1455         if (line == NO_NUMERIC)
1456                 return Einval;
1457
1458         rl->ignore_point = 1;
1459         find_lines(pm, p, focus, line);
1460         pane_damaged(p, DAMAGED_REFRESH);
1461         return 1;
1462 }
1463
1464 DEF_CMD(render_lines_move_line)
1465 {
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
1473          */
1474         struct pane *p = ci->home;
1475         struct pane *focus = ci->focus;
1476         struct rl_data *rl = p->data;
1477         int num;
1478         int xypos = -1;
1479         struct mark *m = ci->mark;
1480         struct mark *start;
1481
1482         if (!m)
1483                 m = call_ret(mark, "doc:point", focus);
1484         if (!m)
1485                 return Efail;
1486
1487         if (rl->target_x < 0) {
1488                 rl->target_x = p->cx;
1489                 rl->target_y = p->cy - rl->cursor_line;
1490         }
1491         if (rl->target_x < 0)
1492                 /* maybe not displayed yet */
1493                 rl->target_x = rl->target_y = 0;
1494
1495         rl->i_moved = 1;
1496         num = RPT_NUM(ci);
1497         if (call("doc:EOL", ci->focus, num, m, NULL, 1) <= 0) {
1498                 rl->i_moved = 0;
1499                 return Efail;
1500         }
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) {
1504                         rl->i_moved = 0;
1505                         return Efail;
1506                 }
1507         }
1508
1509         start = vmark_new(focus, rl->typenum, p);
1510
1511         if (start) {
1512                 mark_to_mark(start, m);
1513                 start = call_render_line_prev(focus, start, 0, NULL);
1514         }
1515
1516         if (!start) {
1517                 pane_damaged(p, DAMAGED_VIEW);
1518                 rl->i_moved = 0;
1519                 return 1;
1520         }
1521         if (vmark_first(focus, rl->typenum, p) == start) {
1522                 /* New first mark, so view will have changed */
1523                 rl->repositioned = 1;
1524         }
1525
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.
1530                  */
1531                 rl->i_moved = 0;
1532                 return 1;
1533         }
1534         /* FIXME only do this if point is active/volatile, or
1535          * if start->mdata is NULL
1536          */
1537         vmark_invalidate(start);
1538         call_render_line(p, focus, start, NULL);
1539         if (start->mdata)
1540                 xypos = find_xy_line(p, focus, start, rl->target_x,
1541                                      rl->target_y + start->mdata->y);
1542
1543         /* xypos is the distance from start-of-line to the target */
1544         if (xypos >= 0) {
1545                 struct mark *m2 = call_render_line_offset(
1546                         focus, start, xypos);
1547                 if (m2)
1548                         mark_to_mark(m, m2);
1549                 mark_free(m2);
1550         }
1551         rl->i_moved = 0;
1552         return 1;
1553 }
1554
1555 DEF_CMD(render_lines_notify_replace)
1556 {
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;
1561         struct mark *first;
1562
1563         if (strcmp(ci->key, "doc:replaced") == 0) {
1564                 struct mark *pt = call_ret(mark, "doc:point", ci->home);
1565
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.
1569                  */
1570                 rl->target_x = -1;
1571
1572                 /* If the replacement happened at 'point', then stop
1573                  * ignoring it, and handle the fact that point moved.
1574                  */
1575                 if (ci->mark2 == pt)
1576                         pane_call(p, "point:moving", ci->focus, 0, pt);
1577         }
1578
1579         if (strcmp(ci->key, "view:changed") == 0)
1580                 /* Cursor possibly moved, so need to refresh */
1581                 pane_damaged(ci->home, DAMAGED_REFRESH);
1582
1583         if (!start && !end) {
1584                 /* No marks given - assume everything changed */
1585                 struct mark *m;
1586                 for (m = vmark_first(p, rl->typenum, p);
1587                      m;
1588                      m = vmark_next(m))
1589                         vmark_invalidate(m);
1590
1591                 pane_damaged(p, DAMAGED_VIEW);
1592                 return Efallthrough;
1593         }
1594
1595         if (start && end && start->seq > end->seq) {
1596                 start = ci->mark2;
1597                 end = ci->mark;
1598         }
1599
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);
1605         }
1606
1607         if (start) {
1608                 start = vmark_at_or_before(ci->home, start, rl->typenum, p);
1609                 if (!start)
1610                         start = vmark_first(ci->home, rl->typenum, p);
1611         } else {
1612                 start = vmark_at_or_before(ci->home, end, rl->typenum, p);
1613                 if (!start)
1614                         /* change is before visible region */
1615                         return Efallthrough;
1616                 /* FIXME check 'start' is at least 'num' before end */
1617         }
1618         if (end) {
1619                 end = vmark_at_or_before(ci->home, end, rl->typenum, p);
1620                 if (!end)
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);
1624                 if (!end)
1625                         end = vmark_first(ci->home, rl->typenum, p);
1626                 if (!end)
1627                         return Efallthrough;
1628                 if (vmark_next(end))
1629                         end = vmark_next(end);
1630                 /* FIXME check that 'end' is at least 'num' after start */
1631         }
1632
1633         if (!end || !start)
1634                 /* Change outside visible region */
1635                 return Efallthrough;
1636
1637         while (end && mark_ordered_or_same(start, end)) {
1638                 vmark_invalidate(end);
1639                 end = vmark_prev(end);
1640         }
1641         /* Must be sure to invalidate the line *before* the change */
1642         if (end)
1643                 vmark_invalidate(end);
1644
1645         pane_damaged(p, DAMAGED_VIEW);
1646
1647         return Efallthrough;
1648 }
1649
1650 DEF_CMD(render_lines_clip)
1651 {
1652         struct rl_data *rl = ci->home->data;
1653
1654         marks_clip(ci->home, ci->mark, ci->mark2, rl->typenum, ci->home,
1655                    !!ci->num);
1656         if (rl->header)
1657                 mark_clip(rl->header, ci->mark, ci->mark2, !!ci->num);
1658         return Efallthrough;
1659 }
1660
1661 DEF_CMD(render_lines_attach);
1662 DEF_CMD(render_lines_clone)
1663 {
1664         struct pane *parent = ci->focus;
1665
1666         render_lines_attach.func(ci);
1667         pane_clone_children(ci->home, parent->focus);
1668         return 1;
1669 }
1670
1671 DEF_CMD(render_lines_resize)
1672 {
1673         struct pane *p = ci->home;
1674         struct rl_data *rl = p->data;
1675         struct mark *m;
1676
1677         for (m = vmark_first(p, rl->typenum, p);
1678              m;
1679              m = vmark_next(m)) {
1680                 vmark_invalidate(m);
1681                 pane_damaged(m->mdata, DAMAGED_REFRESH);
1682         }
1683         rl->background_drawn = False;
1684         pane_damaged(p, DAMAGED_VIEW | DAMAGED_REFRESH);
1685
1686         /* Allow propagation to children */
1687         return 0;
1688 }
1689
1690 DEF_CMD(render_send_reposition)
1691 {
1692         /* Some (probably new) pane wants to know the extend of the
1693          * view, so resent render:resposition.
1694          */
1695         struct pane *p = ci->home;
1696         struct rl_data *rl = p->data;
1697
1698         rl->repositioned = 1;
1699         return Efallthrough;
1700 }
1701
1702 static struct map *rl_map;
1703
1704 DEF_LOOKUP_CMD(render_lines_handle, rl_map);
1705
1706 static void render_lines_register_map(void)
1707 {
1708         rl_map = key_alloc();
1709
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);
1715
1716         /* Make it easy to stop ignoring point */
1717         key_add(rl_map, "Abort", &render_lines_abort);
1718
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);
1729
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);
1736 }
1737
1738 REDEF_CMD(render_lines_attach)
1739 {
1740         struct rl_data *rl;
1741         struct pane *p;
1742
1743         if (!rl_map)
1744                 render_lines_register_map();
1745
1746         alloc(rl, pane);
1747         rl->target_x = -1;
1748         rl->target_y = -1;
1749         rl->do_wrap = 1;
1750         p = ci->focus;
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);
1754         if (!p) {
1755                 free(rl);
1756                 return Efail;
1757         }
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);
1762
1763         return comm_call(ci->comm2, "callback:attach", p);
1764 }
1765
1766 void edlib_init(struct pane *ed safe)
1767 {
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");
1772 }