From a382c63805616bd3f2384d3912c6bdac06a46bd0 Mon Sep 17 00:00:00 2001 From: NeilBrown Date: Fri, 25 Aug 2023 13:48:07 +1000 Subject: [PATCH] Get rid of attributes shift_left and shift-left Add more meaning to render:wrap and discard shift-left and shift_left. It was confusing having two ... or three ... attributes for almost the same thing. Signed-off-by: NeilBrown --- DOC/Developer/06-rendering.md | 16 +++++----- DOC/TODO.md | 3 +- DOC/render-control | 8 +++-- core.h | 2 +- lib-askpass.c | 2 +- lib-menu.c | 2 +- lib-messageline.c | 2 +- lib-renderline.c | 2 +- mode-emacs.c | 11 +++++-- render-lines.c | 57 +++++++++++++++++++++-------------- 10 files changed, 65 insertions(+), 40 deletions(-) diff --git a/DOC/Developer/06-rendering.md b/DOC/Developer/06-rendering.md index 657099af..14b6a743 100644 --- a/DOC/Developer/06-rendering.md +++ b/DOC/Developer/06-rendering.md @@ -245,8 +245,12 @@ appearing in the pane stack: perform detailed drawing of the background + "color:COLOUR" - fills the background with the given colour. + "image:IMAGE" - the image is drawn to fill the pane -- "render-wrap" - if set to any value other than "yes", text is not - wrapped. +- "render-wrap" - How to handle lines wider than the pane: + - if "yes" wrap the line onto multiple lines. + - if "NN" shift the line "NN" character spaces (in standard font) to left. + - if "NN auto", update as needed so that cursor is displayed. + - if not set, treat as "yes" + - if any other value, treat as "0 auto" - "render-vmargin" - if set to a number of lines, "render-lines" will attempt to keep the cursor at least this far from the top or bottom of the display. @@ -291,11 +295,9 @@ redraw as needed. The pane's behaviour can be modified by several attributes that can be found in the stack: -- "shift_left" - this should be in integer. If less than zero the line - will be wrapped if it is too wide to display completely in the pane. - If it is non-negative then the display will not be wrapped and the - display will be shifted to the left this far so later parts of the - line can be seen. +- "render:wrap" - this is processed the same way that render-lines + understands it, though no "auto" adjustment is made. It either + requests wrap or sets the shift size. - "prefix" - This marked-up text will be display first. It will not be shifted even when the main text is shifted left. diff --git a/DOC/TODO.md b/DOC/TODO.md index f051ba3a..ecd90022 100644 --- a/DOC/TODO.md +++ b/DOC/TODO.md @@ -37,7 +37,8 @@ the file. replacement string is shorter. If longer, they get added. - [X] path completion in shell command. If cannot find look for '/' following punctuation (=) and try there. -- [ ] resolve shift-left vs shift_left distinction - add a "fixed" suffix? +- [X] resolve shift-left vs shift_left distinction - add a "fixed" + suffix? Also consider render-wrap - [ ] should zoom affect whole window, not just pane? - [X] ditch cached-size for images - store in pane data instead. - [X] use foreach_attr for parsing image details diff --git a/DOC/render-control b/DOC/render-control index 23cdf201..b3bf9a7b 100644 --- a/DOC/render-control +++ b/DOC/render-control @@ -200,7 +200,11 @@ Several pane attributes can affect the pane as a whole: - "scale" - width/height values to aim for - in "points" which are 1/10 for an M e.g. if 800x240, then text is scaled so that at least 80 M's fit across, and 24 M's fit down. - - "render-wrap" - if define but not "yes", don't wrap long lines, else shift left to - fit cursor on pane. + - "render-wrap" - How to handle lines wider than the pane: + - if "yes" wrap the line onto multiple lines. + - if "NN" shift the line "NN" character spaces (in standard font) to left. + - if "NN auto", update as needed so that cursor is displayed. + - if not set, treat as "yes" + - if any other value, treat as "0 auto" - "prefix" - text to place at start of each line. This can be used as a prompt in a dialogue box. diff --git a/core.h b/core.h index 68bc7888..8987d3d4 100644 --- a/core.h +++ b/core.h @@ -495,7 +495,7 @@ static inline int pane_attr_get_int(struct pane *p safe, const char *key safe, if (!c) return dflt; rv = strtol(c, &end, 10); - if (end == c || !end || *end) + if (end == c || !end || (*end && *end != ' ')) return dflt; return rv; } diff --git a/lib-askpass.c b/lib-askpass.c index 6ab786c1..c8805914 100644 --- a/lib-askpass.c +++ b/lib-askpass.c @@ -40,7 +40,7 @@ DEF_CMD(askpass_refresh_view) call("render-line:set", ci->focus, b.len, NULL, buf_final(&b)); for (i = 0; i < 10; i++) { int cw; - attr_set_int(&ci->focus->attrs, "shift_left", shift); + attr_set_int(&ci->focus->attrs, "render-wrap", shift); call("render-line:measure", ci->focus, b.len); cw = pane_attr_get_int(ci->focus, "curs_width", 1); if (ci->home->parent->cx < ci->home->parent->w - cw) diff --git a/lib-menu.c b/lib-menu.c index f21ec370..296286b8 100644 --- a/lib-menu.c +++ b/lib-menu.c @@ -94,7 +94,7 @@ DEF_CMD(menu_attach) return Efail; call("doc:set:autoclose", docp, 1); attr_set_str(&docp->attrs, "render-simple", "format"); - attr_set_str(&docp->attrs, "shift-left", "0"); + attr_set_int(&docp->attrs, "render-wrap", 0); attr_set_str(&docp->attrs, "heading", ""); attr_set_str(&docp->attrs, "line-format", "%name"); attr_set_str(&docp->attrs, "done-key", ci->str2 ?: "menu-done"); diff --git a/lib-messageline.c b/lib-messageline.c index ae6540ec..d6e6ba09 100644 --- a/lib-messageline.c +++ b/lib-messageline.c @@ -260,7 +260,7 @@ static struct pane *do_messageline_attach(struct pane *p safe) return NULL; } /* Support wrapping */ - attr_set_int(&mlp->attrs, "shift_left", -1); + attr_set_str(&mlp->attrs, "render:wrap", "yes"); pane_damaged(ret, DAMAGED_VIEW); mli->line = mlp; pane_focus(ret); diff --git a/lib-renderline.c b/lib-renderline.c index 5cbaa2a2..ef34b8a6 100644 --- a/lib-renderline.c +++ b/lib-renderline.c @@ -517,7 +517,7 @@ static int measure_line(struct pane *p safe, struct pane *focus safe, int offset */ struct rline_data *rd = &p->data; struct render_item *ri, *wraprl; - int shift_left = pane_attr_get_int(focus, "shift_left", 0); + int shift_left = pane_attr_get_int(focus, "render-wrap", -1); bool wrap = shift_left < 0; int wrap_margin; int right_margin; diff --git a/mode-emacs.c b/mode-emacs.c index 389c18bf..eae28935 100644 --- a/mode-emacs.c +++ b/mode-emacs.c @@ -2704,7 +2704,7 @@ DEF_CMD(emacs_shift) int rpt = ci->num; int shift; - shift = pane_attr_get_int(ci->focus, "shift-left", -1); + shift = pane_attr_get_int(ci->focus, "render-wrap", -1); if (strcmp(ci->key, "K:CX->") == 0 || strcmp(ci->key, "K->") == 0) rpt = -rpt; if (rpt == NO_NUMERIC) { @@ -2730,9 +2730,14 @@ DEF_CMD(emacs_shift) shift += rpt; } if (shift < 0) - attr_set_str(&ci->focus->attrs, "shift-left", ""); + attr_set_str(&ci->focus->attrs, "render-wrap", "yes"); + else if (shift > 0) + attr_set_int(&ci->focus->attrs, "render-wrap", shift); + else if (rpt > 0) + attr_set_str(&ci->focus->attrs, "render-wrap", "0 auto"); else - attr_set_int(&ci->focus->attrs, "shift-left", shift); + /* When reducing shift to zero, don't enable auto */ + attr_set_int(&ci->focus->attrs, "render-wrap", 0); call("view:changed", ci->focus); call("Mode:set-num2", ci->focus, N2_shift); call("Message:modal", ci->focus, 0, NULL, "Type < or > to shift again"); diff --git a/render-lines.c b/render-lines.c index 459a3f95..b6e86228 100644 --- a/render-lines.c +++ b/render-lines.c @@ -170,12 +170,22 @@ static int _measure_line(struct pane *p safe, struct pane *focus safe, struct mark *mk safe, short cursor_offset, char **cursor_attr) { + struct rl_data *rl = p->data; struct pane *hp = mk->mdata; struct call_return cr; if (!mark_valid(mk) || !hp) return False; pane_resize(hp, hp->x, hp->y, p->w, p->h); + if (!rl->shift_locked) { + int sl = pane_attr_get_int(focus, "render-wrap", -2); + if (sl != rl->shift_left) { + char *sla = NULL; + asprintf(&sla, "%d auto", rl->shift_left); + attr_set_str(&focus->attrs, "render-wrap", sla); + free(sla); + } + } cr = pane_call_ret(all, hp, "render-line:measure", focus, cursor_offset); if (cursor_attr) @@ -990,21 +1000,6 @@ static int render(struct mark *pm, struct pane *p safe, return y; } -DEF_CMD(render_lines_get_attr) -{ - struct rl_data *rl = ci->home->data; - - if (ci->str && strcmp(ci->str, "shift_left") == 0) { - char ret[10]; - if (rl->do_wrap && !rl->shift_locked) - return comm_call(ci->comm2, "cb", ci->focus, - 0, NULL, "-1"); - snprintf(ret, sizeof(ret), "%d", rl->shift_left); - return comm_call(ci->comm2, "cb", ci->focus, 0, NULL, ret); - } - return Efallthrough; -} - DEF_CMD(render_lines_point_moving) { struct pane *p = ci->home; @@ -1222,27 +1217,46 @@ DEF_CMD(render_lines_revise) struct mark *pm = NULL; struct mark *m1, *m2; bool refresh_all = False; + bool wrap; char *hdr; char *a; int shift; a = pane_attr_get(focus, "render-wrap"); - if (rl->do_wrap != (!a || strcmp(a, "yes") ==0)) { - rl->do_wrap = (!a || strcmp(a, "yes") ==0); + wrap = (!a || strcmp(a, "yes") == 0); + if (rl->do_wrap != wrap) { + rl->do_wrap = wrap; refresh_all = True; + rl->shift_left = 0; + } + if (wrap) + rl->shift_locked = True; + if (!a) + /* avoid any ambiguity */ + attr_set_str(&focus->attrs, "render-wrap", "yes"); + + if (a) { + char *end; + shift = strtol(a, &end, 10); + if (end == a || (end && *end && *end != ' ')) + shift = -1; } + else + shift = -1; - shift = pane_attr_get_int(focus, "shift-left", -1); - if (shift >= 0) { + if (a && shift >= 0) { if (rl->shift_left != shift) refresh_all = True; rl->shift_left = shift; - rl->shift_locked = 1; - } else { + rl->shift_locked = strstr(a, "auto") == NULL; + } else if (!wrap) { + /* unrecognised - no wrap, not locked */ if (rl->shift_locked) refresh_all = True; + rl->shift_left = 0; rl->shift_locked = 0; + attr_set_str(&focus->attrs, "render-wrap", "0 auto"); } if (refresh_all) { struct mark *v; @@ -1966,7 +1980,6 @@ static void render_lines_register_map(void) key_add(rl_map, "Refresh:view", &render_lines_revise); key_add(rl_map, "Refresh:size", &render_lines_resize); key_add(rl_map, "Notify:clip", &render_lines_clip); - key_add(rl_map, "get-attr", &render_lines_get_attr); key_add(rl_map, "mark:moving", &render_lines_point_moving); key_add(rl_map, "doc:replaced", &render_lines_notify_replace); -- 2.39.5