]> git.neil.brown.name Git - edlib.git/commitdiff
introduce doc:append:...
authorNeilBrown <neil@brown.name>
Fri, 26 May 2023 03:38:12 +0000 (13:38 +1000)
committerNeilBrown <neil@brown.name>
Fri, 26 May 2023 22:18:43 +0000 (08:18 +1000)
doc:append: is similar to doc:set:, but the given value is appended to
the attr if it exists.
The value should start with a separator character, and this is removed
if it is the first item to be appended.

Use this to simplify setting of "view-default"

Signed-off-by: NeilBrown <neil@brown.name>
DOC/Developer/03-documents.md
core-doc.c
lib-whitespace.c
python/config.py
python/lib-autospell.py
python/lib-diff.py
python/lib-mergeview.py
python/lib-textfill.py
python/render-calc.py

index 7567275d38a8fcc2114df550d9a8d07ef73dffae..034ad8f123771dbfdb5762329109a9655542ca8d 100644 (file)
@@ -154,6 +154,7 @@ the objects to present
                       &doc_request_notify);
        key_add_prefix(doc_default_cmd, "doc:notify:", &doc_notify);
        key_add_prefix(doc_default_cmd, "doc:set:", &doc_set);
+       key_add_prefix(doc_default_cmd, "doc:append:", &doc_append);
 
 ## The document link pane
 
index e4db60f7cc37abae1698bcfab5c290882d00d026..5100b02df284f45af36f637e77d8ad5b73e1eb8c 100644 (file)
@@ -497,6 +497,34 @@ DEF_CMD(doc_set)
        return 1;
 }
 
+DEF_CMD(doc_append)
+{
+       struct pane *p = ci->home;
+       const char *attr = ksuffix(ci, "doc:append:");
+       const char *val = ci->str;
+       const char *old;
+
+       if (!val || !val[0])
+               return Enoarg;
+       /* Append the string to the attr.  It attr doesn't
+        * exists, string first char of val and use that.
+        */
+       old = attr_find(p->attrs, attr);
+       if (!old) {
+               attr_set_str(&p->attrs, attr, val+1);
+       } else {
+               const char *pos = strstr(old, val+1);
+               int len = strlen(val+1);
+               if (pos &&
+                   (pos == old || pos[-1] == val[0]) &&
+                   (pos[len] == 0 || pos[len] == val[0]))
+                       ; /* val already present */
+               else
+                       attr_set_str(&p->attrs, attr, strconcat(p, old, val));
+       }
+       return 1;
+}
+
 DEF_CMD(doc_get_attr)
 {
        struct doc *d = ci->home->data;
@@ -1284,6 +1312,7 @@ static void init_doc_cmds(void)
                       &doc_request_notify);
        key_add_prefix(doc_default_cmd, "doc:notify:", &doc_notify);
        key_add_prefix(doc_default_cmd, "doc:set:", &doc_set);
+       key_add_prefix(doc_default_cmd, "doc:append:", &doc_append);
 }
 
 static void do_doc_assign(struct pane *p safe, struct pane *doc safe)
index fed67e41af275712ec582a717ae4ccc7b2436fcd..9f9c899e7c24527c9bff7fc3b30a0343b3b0487a 100644 (file)
@@ -314,17 +314,11 @@ DEF_CMD(whitespace_attach)
 DEF_CMD(whitespace_activate)
 {
        struct pane *p;
-       char *v, *vn = NULL;
 
        p = call_ret(pane, "attach-whitespace", ci->focus);
        if (!p)
                return Efail;
-       v = pane_attr_get(p, "view-default");
-       asprintf(&vn, "%s%swhitespace", v?:"", v?",":"");
-       if (vn) {
-               call("doc:set:view-default", p, 0, NULL, vn);
-               free(vn);
-       }
+       call("doc:append:view-default", p, 0, NULL, ",whitespace");
        return 1;
 }
 
index de0e723f63386403851abb7cf7fb6baeb24a38ae..60afba9bf2e7456296ba843b5887e71818397515 100644 (file)
@@ -14,7 +14,7 @@ def config_appeared(key, focus, **a):
     p = focus['filename']
 
     if p and ("COMMIT_EDITMSG" in p or "/.stgit" in p):
-        focus.call("doc:set:view-default", "textfill,whitespace,autospell")
+        focus.call("doc:append:view-default", ",textfill,whitespace,autospell")
         focus.call("doc:set:fill-width", "72")
         if "/git/lustre-release/" in p:
             # looks like a lustre commit, need to limit line width
@@ -24,9 +24,9 @@ def config_appeared(key, focus, **a):
     if p and p[-3:] == ".md":
         # Until I have a real markdown module, I need this at least.
         if os.getenv("EDLIB_TESTING"):
-            focus.call("doc:set:view-default", "textfill,whitespace")
+            focus.call("doc:append:view-default", ",textfill,whitespace")
         else:
-            focus.call("doc:set:view-default", "textfill,whitespace,autospell")
+            focus.call("doc:append:view-default", ",textfill,whitespace,autospell")
         focus["fill-width"] = "72"
         focus["fill:start-re"] = ("^("
                                   "[^a-zA-Z0-9\\n]*$|" # empty/puctuation line
index 085d36ec12cad46fd0353d7cc7bf2f4038037ac4..37cae7eefc36d881f534ba0976dcbd83a2562061 100644 (file)
@@ -312,12 +312,7 @@ def autospell_attach_helper(key, focus, **a):
 def autospell_activate(key, focus, comm2, **a):
     autospell_view(focus)
 
-    v = focus['view-default']
-    if v:
-        v = v + ',autospell'
-    else:
-        v = 'autospell'
-    focus.call("doc:set:view-default", v)
+    focus.call("doc:append:view-default", ",autospell")
 
     return 1
 
index 7eb5df3e21d782bb4f02b13a0f1b88c5d5b01b93..b88d3826bcf065b34e1587addfabc2cb9e6c0ade 100644 (file)
@@ -329,7 +329,7 @@ def add_diff(key, focus, **a):
     p = DiffPane(focus)
     if p:
         p.call("view:changed")
-    focus.call("doc:set:view-default", "diff")
+    focus.call("doc:append:view-default", ",diff")
     return 1
 
 editor.call("global-set-command", "attach-diff", diff_view_attach)
index a8b6944a51ece660513902fbe4f409a74c980c66..97991bc8f634d6fd165267708374119ed4af21c5 100644 (file)
@@ -192,12 +192,7 @@ def add_merge(key, focus, mark, **a):
     if p:
         p.call("view:changed")
 
-    v = focus['view-default']
-    if v:
-        v = v + ',merge'
-    else:
-        v = 'merge'
-    focus.call("doc:set:view-default", v)
+    focus.call("doc:append:view-default", ",merge")
     if mark:
         p.call("K:A-m", focus, mark)
     return 1
index 41bfdcec8f646422ec0627cd092da190cdf767eb..d9da65f9d1788694009f9875d9a7c152929f1f73 100644 (file)
@@ -279,17 +279,10 @@ class FillMode(edlib.Pane):
 
     def enable_fill(self, key, focus, num, **a):
         "handle:interactive-cmd-fill-mode"
-        v = focus['view-default']
         if not self.cols:
             self.cols = 72
             self.call("doc:set:fill-width", "72")
-        if v and 'textfill'in v:
-            return 1
-        elif v:
-            v = v + ',textfill'
-        else:
-            v = 'textfill'
-        focus.call("doc:set:view-default", v)
+        focus.call("doc:append:view-default", ",textfill")
         return 1
 
     def handle_space(self, key, focus, mark, **a):
@@ -357,12 +350,7 @@ def fill_mode_activate(key, focus, comm2, **a):
     # enable fill-paragraph and auto-fill at col 72
     FillMode(focus, 72)
 
-    v = focus['view-default']
-    if v:
-        v = v + ',textfill'
-    else:
-        v = 'textfill'
-    focus.call("doc:set:view-default", v)
+    focus.call("doc:append:view-default", ",textfill")
     return 1
 
 editor.call("global-set-command", "attach-textfill", fill_mode_attach)
index 92d1e0c9f0ea488ca5637278797da95c03af0247..cd70f9b0f984273571c73c6b0b13b54a94dfe13c 100644 (file)
@@ -160,12 +160,7 @@ def add_calc(key, focus, mark, **a):
     if p:
         p.call("view:changed")
 
-    v = focus['view-default']
-    if v:
-        v = v + ',view-calc'
-    else:
-        v = 'view-calc'
-    focus.call("doc:set:view-default", v)
+    focus.call("doc:append:view-default", ",view-calc")
     return 1
 
 def calc_appeared(key, focus, **a):