From e1e7f3edc8a4096240b4e4f34c807f4e746f6049 Mon Sep 17 00:00:00 2001 From: NeilBrown Date: Sat, 3 Jun 2023 11:32:55 +1000 Subject: [PATCH] linecount: intro option of sync/async counting. lib-view doesn't need a completely up-to-date number. Other callers do. So add a flag to support this. CountLines will always be async when no mark is given. When a mark is given, only async if num==1 CountLinesAsync is always async. Signed-off-by: NeilBrown --- lib-linecount.c | 20 +++++++++++++------- lib-view.c | 6 +++--- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/lib-linecount.c b/lib-linecount.c index 110255a1..99bc212a 100644 --- a/lib-linecount.c +++ b/lib-linecount.c @@ -117,7 +117,8 @@ static int need_recalc(struct pane *p safe, struct mark *m) static void count_calculate(struct pane *p safe, struct mark *end, - struct pane *owner safe, int type) + struct pane *owner safe, int type, + bool sync) { int lines, words, chars, l, w, c; struct mark *m, *m2; @@ -222,7 +223,9 @@ DEF_CMD(linecount_notify_count) struct pane *d = ci->focus; struct count_info *cli = ci->home->data; /* Option mark is "mark2" as "mark" gets the "point" */ - count_calculate(d, ci->mark2, ci->home, cli->view_num); + /* num==1 means we don't want to wait for precision */ + count_calculate(d, ci->mark2, ci->home, cli->view_num, + ci->mark2 && ci->num != 1); return 1; } @@ -238,7 +241,7 @@ DEF_CMD(linecount_notify_goto) return 1; /* Ensure counts are up-to-date */ - count_calculate(d, NULL, ci->home, cli->view_num); + count_calculate(d, NULL, ci->home, cli->view_num, True); m = vmark_first(d, cli->view_num, ci->home); if (!m) return 1; @@ -266,11 +269,13 @@ DEF_CMD(linecount_notify_goto) DEF_CMD(count_lines) { char *view = pane_attr_get(ci->focus, "view-default"); + int async = strcmp(ci->key, "CountLinesAsync") == 0; + /* FIXME this type-check is a HACK */ if (view && strcmp(view, "make-viewer") == 0) return 1; /* FIXME optimise this away most of the time */ - if (call("doc:notify:doc:CountLines", ci->focus) == 0) { + if (call("doc:notify:doc:CountLines", ci->focus, 1) == 0) { /* No counter in place, add one */ struct count_info *cli; struct pane *p; @@ -284,7 +289,7 @@ DEF_CMD(count_lines) home_call(ci->focus, "doc:request:doc:CountLines", p); home_call(ci->focus, "doc:request:doc:GotoLine", p); home_call(ci->focus, "doc:request:Notify:Close", p); - call("doc:notify:doc:CountLines", ci->focus); + call("doc:notify:doc:CountLines", ci->focus, 1); } if (ci->mark) { if (ci->str && strcmp(ci->str, "goto:line") == 0 && @@ -292,11 +297,11 @@ DEF_CMD(count_lines) call("doc:notify:doc:GotoLine", ci->focus, ci->num, NULL, NULL, 0, ci->mark); } - call("doc:notify:doc:CountLines", ci->focus, 0, NULL, NULL, + call("doc:notify:doc:CountLines", ci->focus, async, NULL, NULL, 0, ci->mark); } if (ci->mark2) - call("doc:notify:doc:CountLines", ci->focus, 0, NULL, NULL, + call("doc:notify:doc:CountLines", ci->focus, async, NULL, NULL, 0, ci->mark2); return 1; } @@ -304,6 +309,7 @@ DEF_CMD(count_lines) void edlib_init(struct pane *ed safe) { call_comm("global-set-command", ed, &count_lines, 0, NULL, "CountLines"); + call_comm("global-set-command", ed, &count_lines, 0, NULL, "CountLinesAsync"); if (linecount_map) return; diff --git a/lib-view.c b/lib-view.c index 8202b7d2..321263e5 100644 --- a/lib-view.c +++ b/lib-view.c @@ -45,7 +45,7 @@ static struct pane *do_view_attach(struct pane *par, int border); static int calc_border(struct pane *p safe); static char default_status[] = - "{!CountLines}M:{doc-modified?,*,-}{doc-readonly?,%%, } D:{doc-file-changed?,CHANGED:,}{doc-name%-15} L{^line}/{lines} {display-context}{render-default}/{view-default} {doc-status}"; + "{!CountLinesAsync}M:{doc-modified?,*,-}{doc-readonly?,%%, } D:{doc-file-changed?,CHANGED:,}{doc-name%-15} L{^line}/{lines} {display-context}{render-default}/{view-default} {doc-status}"; static char default_title[] = "{doc-name}"; @@ -195,11 +195,11 @@ DEF_CMD(view_refresh) int mid; if (vd->viewpoint) { - call("CountLines", ci->focus, 0, vd->viewpoint); + call("CountLinesAsync", ci->focus, 0, vd->viewpoint); vpln = attr_find_int(*mark_attr(vd->viewpoint), "line"); } else if (pm) { - call("CountLines", ci->focus, 0, pm); + call("CountLinesAsync", ci->focus, 0, pm); vpln = attr_find_int(*mark_attr(pm), "line"); } -- 2.39.5