2 * Copyright Neil Brown ©2016-2023 <neil@brown.name>
3 * May be distributed under terms of GPLv2 - see file:COPYING
5 * This module provides render-line and render-line-prev, making use of
6 * the chars returned by doc:char.
7 * A line is normally text ending with a newline. However if no newline
8 * is found in a long distance, we drop a mark and use that as the start
19 #define PANE_DATA_TYPE struct mu_info
26 #include "core-pane.h"
28 static struct map *mu_map safe;
30 #define LARGE_LINE 5000
32 static int is_render_eol(wint_t ch, struct pane *p safe, struct mark *m safe)
37 attr = pane_mark_attr(p, m, "markup:not_eol");
45 /* In the process of rendering a line we need to find the
46 * start of line. We use a mark to create an artificial
47 * start-of-line where none can be found.
48 * Search backwards until a newline or start-of-file or the
49 * mark is found. Move backwards at most LARGE_LINE characters
50 * and if nothing else is found, put a mark there and treat as s-o-l.
52 * If RPT_NUM == 1, step back at least one character so we get
53 * the previous line and not the line we are on.
54 * If we hit start-of-file without finding newline, return Efail;
56 struct mark *m = ci->mark;
57 struct pane *f = ci->focus;
58 struct mu_info *mu = ci->home->data;
59 struct mark *boundary = NULL;
60 struct mark *doc_boundary = NULL;
62 int rpt = RPT_NUM(ci);
69 boundary = vmark_at_or_before(f, m, mu->view, ci->home);
70 doc_boundary = call_ret(mark, "doc:get-boundary", f, -1, m);
72 while ((ch = doc_prev(f, m)) != WEOF &&
73 (!is_render_eol(ch, f, m) || rpt > 0) &&
75 (!boundary || mark_ordered_not_same(boundary, m)) &&
76 (!doc_boundary || mark_ordered_not_same(doc_boundary, m))) {
78 boundary = vmark_at_or_before(f, m, mu->view, ci->home);
79 doc_boundary = call_ret(mark, "doc:get-boundary", f, -1, m);
84 if (ch != WEOF && !is_render_eol(ch, f, m) &&
85 (!doc_boundary || !mark_same(doc_boundary, m))) {
86 /* Just crossed the boundary, or the max count.
87 * Need to step back, and ensure there is a stable boundary
90 mark_free(doc_boundary);
92 if (!boundary || !mark_same(boundary, m)) {
93 boundary = vmark_new(f, mu->view, ci->home);
95 mark_to_mark(boundary, m);
99 mark_free(doc_boundary);
100 if (ch == WEOF && rpt)
102 /* Found a '\n', so step forward over it for start-of-line. */
103 if (is_render_eol(ch, f, m))
108 /* 'ast' is a stack is all the attributes that should be applied "here".
109 * They are sorted by priority with the highest first.
110 * 'end' is an offset in chars-since-start-of-line where the attribute
111 * should stop applying. The current chars-since-start-of-line is 'chars'.
112 * The stack structure reflects the nesting of <attr> and </>.
113 * To change an attribute (normally add or delete) we pop it and any attributes
114 * above it in the stack and push them onto tmpst, which is then in
115 * reverse priority order. As we do that, we count them in 'popped'.
116 * Changes can be made in the secondary stack.
117 * When all change have been made, we add 'popped' ETX marked to the output,
118 * then process everything in 'tmpst', either discarding it if end<=chars, or
119 * outputting the attributes and pushing back on 'ast'.
125 struct attr_stack *next;
128 unsigned short priority;
136 /* Find which attibutes should be finished by 'pos'. The depth of
137 * to deepest such is returned, and the next time to endpoint is
139 * Everything higher than that returned depth will need to be closed,
140 * so that the deepest one can be closed.
141 * Then some of the higher ones might get re-opened.
143 static int find_finished(struct attr_stack *st, int pos, int *nextp safe)
149 for (; st ; st = st->next, depth++) {
152 else if (next < 0 || next > st->end)
159 /* Move the top 'depth' attributes from 'ast' to 'tmpst', updating 'popped' */
160 static void as_pop(struct attr_return *ar safe, int depth)
162 struct attr_stack *from = ar->ast;
163 struct attr_stack *to = ar->tmpst;
165 while (from && depth > 0) {
166 struct attr_stack *t;
178 /* re-push any attriubtes that are still valid, freeing those that aren't */
179 static void as_repush(struct attr_return *ar safe, struct buf *b safe)
181 struct attr_stack *from = ar->tmpst;
182 struct attr_stack *to = ar->ast;
184 while (ar->popped > 0) {
190 struct attr_stack *t = from->next;
191 if (from->end <= ar->chars) {
196 buf_concat(b, from->attr);
200 if (from->end < ar->min_end)
201 ar->min_end = from->end;
209 static void as_add(struct attr_return *ar safe,
210 int end, int prio, const char *attr safe)
212 struct attr_stack *new, **here;
214 while (ar->ast && ar->ast->priority > prio)
218 while (*here && (*here)->priority <= prio)
219 here = &(*here)->next;
220 new = calloc(1, sizeof(*new));
222 new->attr = strdup(attr);
223 if (end == 0 || INT_MAX - end <= ar->chars)
224 end = INT_MAX - 1 - ar->chars;
225 new->end = ar->chars + end;
226 new->priority = prio;
230 static void as_clear(struct attr_return *ar safe,
231 int prio, const char *attr)
233 struct attr_stack *st;
235 while (ar->ast && ar->ast->priority >= prio)
238 for (st = ar->tmpst; st && st->priority <= prio; st = st->next)
239 if (st->priority == prio &&
240 (attr == NULL || strcmp(st->attr, attr) == 0))
244 DEF_CB(text_attr_forward)
246 struct attr_return *ar = container_of(ci->comm, struct attr_return, fwd);
247 if (!ci->str || !ci->str2)
249 return call_comm("map-attr", ci->focus, &ar->rtn, 0, ci->mark, ci->str2,
253 DEF_CB(text_attr_callback)
255 struct attr_return *ar = container_of(ci->comm, struct attr_return,
264 as_add(ar, ci->num, prio, ci->str);
266 as_clear(ar, prio, ci->str);
268 const char *c = ci->str2;
270 while ((wch = get_utf8(&c, NULL)) != WEOF)
271 buf_append(&ar->insert, wch);
276 static void call_map_mark(struct pane *f safe, struct mark *m safe,
277 struct attr_return *ar safe)
279 const char *key = "render:";
282 while ((key = attr_get_next_key(m->attrs, key, -1, &val)) != NULL &&
283 strstarts(key, "render:"))
284 call_comm("map-attr", f, &ar->rtn, 0, m, key, 0, NULL, val);
287 static int want_vis_newline(struct attr_stack *as)
289 while (as && strstr(as->attr, "vis-nl") == NULL)
296 /* Render the line from 'mark' to the first '\n' or until
300 struct pane *focus = ci->focus;
301 struct mu_info *mu = ci->home->data;
302 struct mark *m = ci->mark;
303 struct mark *pm = ci->mark2; /* The location to render as cursor */
304 struct mark *boundary, *start_boundary = NULL;
305 struct mark *doc_boundary;
311 struct attr_return ar;
317 ar.rtn = text_attr_callback;
318 ar.fwd = text_attr_forward;
319 ar.ast = ar.tmpst = NULL;
322 buf_init(&ar.insert);
328 oneline = pane_attr_get(focus, "render-one-line");
329 if (oneline && strcmp(oneline, "yes") != 0)
331 noret = pane_attr_get(focus, "render-hide-CR");
332 if (noret && strcmp(noret, "yes") != 0)
335 ch = doc_following(focus, m);
338 if ((attr = pane_mark_attr(focus, m, "markup:func")) != NULL) {
339 /* An alternate function handles this line */
340 ret = call_comm(attr, focus, ci->comm2, o, m, NULL, 0, pm);
344 boundary = vmark_at_or_before(focus, m, mu->view, ci->home);
346 if (mark_same(m, boundary))
347 start_boundary = boundary;
348 boundary = vmark_next(boundary);
350 doc_boundary = call_ret(mark, "doc:get-boundary", focus, 1, m);
353 /* Assert that '<' are not quoted */
355 call_comm("map-attr", focus, &ar.rtn, 0, m, "start-of-line");
357 buf_concat(&b, buf_final(&ar.insert));
358 buf_reinit(&ar.insert);
364 if (o >= 0 && b.len >= o)
367 if (boundary && mark_ordered_or_same(boundary, m))
369 if (doc_boundary && mark_ordered_or_same(doc_boundary, m))
372 if (pm && mark_same(m, pm) && pm_offset < 0)
375 if (ar.ast && ar.min_end <= chars) {
376 int depth = find_finished(ar.ast, chars, &ar.min_end);
381 call_comm("doc:get-attr", focus, &ar.fwd, 0, m, "render:", 1);
383 /* find all marks "here". They might get moved when we call map_mark,
384 * so move 'm' among them
387 while ((m2 = mark_next(m)) != NULL &&
389 mark_to_mark_noref(m, m2);
390 call_map_mark(focus, m2, &ar);
395 if (o >= 0 && b.len >= o)
399 buf_concat(&b, buf_final(&ar.insert));
400 buf_reinit(&ar.insert);
403 ch = doc_next(focus, m);
407 if (!oneline && is_eol(ch)) {
409 is_true_eol = is_render_eol(ch, focus, m);
417 if (ch == '\r' && noret) {
419 } else if (ch < ' ' && ch != '\t') {
420 buf_concat(&b, SOH "fg:red" STX "^");
421 buf_append(&b, '@' + ch);
423 } else if (ch == 0x7f) {
424 buf_concat(&b, SOH "fg:red" STX "^?" ETX);
425 } else if (ch >= 0x80 && iswcntrl(ch)) {
426 /* Extra unicode control */
427 buf_concat(&b, SOH "fg:magenta" STX "^");
428 buf_append(&b, 96 + (ch & 0x1f));
433 if (add_newline && want_vis_newline(ar.ast))
440 if (o >= 0 && b.len >= o)
441 /* skip the newline */
444 buf_append(&b, '\n');
447 if (start_boundary && chars < LARGE_LINE - 5)
448 /* This boundary is no longer well-placed. */
449 mark_free(start_boundary);
451 mark_free(doc_boundary);
453 if (pm && pm_offset < 0)
456 ret = comm_call(ci->comm2, "callback:render", focus, pm_offset, NULL,
463 DEF_LOOKUP_CMD(markup_handle, mu_map);
465 static struct pane *do_markup_attach(struct pane *p safe)
470 ret = pane_register(p, 0, &markup_handle.c);
474 mu->view = home_call(p, "doc:add-view", ret) - 1;
479 DEF_CMD(markup_attach)
483 ret = do_markup_attach(ci->focus);
486 return comm_call(ci->comm2, "callback:attach", ret);
491 struct pane *parent = ci->focus;
492 struct pane *child = do_markup_attach(parent);
493 pane_clone_children(ci->home, child);
499 struct mu_info *mu = ci->home->data;
501 marks_clip(ci->home, ci->mark, ci->mark2,
502 mu->view, ci->home, !!ci->num);
506 void edlib_init(struct pane *ed safe)
508 mu_map = key_alloc();
510 key_add(mu_map, "doc:render-line", &render_line);
511 key_add(mu_map, "doc:render-line-prev", &render_prev);
512 key_add(mu_map, "Clone", &mu_clone);
513 key_add(mu_map, "Notify:clip", &mu_clip);
515 call_comm("global-set-command", ed, &markup_attach,
516 0, NULL, "attach-markup");