]> git.neil.brown.name Git - edlib.git/commitdiff
linecount: intro option of sync/async counting.
authorNeilBrown <neil@brown.name>
Sat, 3 Jun 2023 01:32:55 +0000 (11:32 +1000)
committerNeilBrown <neil@brown.name>
Wed, 28 Jun 2023 05:40:21 +0000 (15:40 +1000)
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 <neil@brown.name>
lib-linecount.c
lib-view.c

index 110255a1949e5d6a0ac19aeb10faef293088e2fa..99bc212acaf7d7ffb818239e5821efe2c7e9145f 100644 (file)
@@ -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;
index 8202b7d28d60758b872443204ae30c57e4f26d00..321263e5060ce43e721f20d26057a0b973310a0d 100644 (file)
@@ -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");
                        }