]> git.neil.brown.name Git - edlib.git/commitdiff
notmuch: act on menu selection to add address to a "from" list
authorNeilBrown <neil@brown.name>
Tue, 29 Aug 2023 22:24:53 +0000 (08:24 +1000)
committerNeilBrown <neil@brown.name>
Tue, 29 Aug 2023 22:25:49 +0000 (08:25 +1000)
The menu selection to add to a known query of from: addresses now works.
The menu selection to compose a new message still doesn't.

Signed-off-by: NeilBrown <neil@brown.name>
DOC/TODO.md
python/module-notmuch.py

index a7b7620c37c5394c6b67a8da44da8363281bf9c3..3c3787ea7a6193075500950c4d65e880892263a2 100644 (file)
@@ -13,7 +13,7 @@ the file.
 
 ### Small
 
-- [ ] notmuch addresses in From: list to have menu to add address to any
+- [X] notmuch addresses in From: list to have menu to add address to any
       from-* query
 - [ ] Disable <hide> if cursor is in the hidden region.
 - [X] fill mode to handle all punctuation at start of this line
index 05a327e27916cb73c0330e3d39e48f932a2f4811..3b55216066394c302b34e8b8143e59d2840df29b 100644 (file)
@@ -38,6 +38,7 @@ import os, fcntl
 import json
 import time
 import mimetypes
+import email.utils
 
 def cvt_size(n):
     if n < 1000:
@@ -771,6 +772,24 @@ class notmuch_main(edlib.Doc):
             comm2("cb", focus, self.searches.slist[str1])
         return 1
 
+    def handle_set_query(self, key, focus, str1, str2, **a):
+        "handle:doc:notmuch:set-query"
+        if not (str1 and str2):
+            return edlib.Enoarg
+        self.searches.slist[str1] = str2
+        p = Popen(["/usr/bin/notmuch", "config", "set",
+                   "query."+str1, str2],
+                  stdout = DEVNULL, stderr=DEVNULL, stdin=DEVNULL)
+        try:
+            p.communicate(timeout=5)
+        except TimeoutExpired:
+            p.kill()
+            p.communicate()
+            return edlib.Efalse
+        if p.returncode != 0:
+            return edlib.Efalse
+        return 1
+
     def tick(self, key, **a):
         if not self.updating:
             self.searches.load(False)
@@ -1899,6 +1918,7 @@ class notmuch_master_view(edlib.Pane):
             return edlib.Efail
         prev = self.recursed
         self.recursed = key
+        # FIXME catch exception to return failure state properly
         ret = self.list_pane.call(key, **a)
         self.recursed = prev
         return ret
@@ -3546,6 +3566,9 @@ class notmuch_message_view(edlib.Pane):
                 addr = focus.call("doc:get-attr", 0, mark, "addr-"+t, ret='str')
         if not addr:
             return 1
+        ad = email.utils.getaddresses([addr])
+        if ad and ad[0] and len(ad[0]) == 2:
+            addr = ad[0][1]
         focus.call("Message", "Menu for address %s" % addr)
         mp = self.call("attach-menu", "", "notmuch-addr-choice", xy, ret='pane')
         mp.call("menu-add", "C", "Compose")
@@ -3554,7 +3577,11 @@ class notmuch_message_view(edlib.Pane):
             for t in q.split():
                 if t.startswith("query:"):
                     t = t[6:]
-                    mp.call("menu-add", t, 'Add to "%s"' % t)
+                    qq = focus.call("doc:notmuch:get-query", t, ret='str')
+                    if qq and ("from:"+addr) in qq:
+                        mp.call("menu-add", "-" + t, 'Already in "%s"' % t)
+                    else:
+                        mp.call("menu-add", t, 'Add to "%s"' % t)
         mp.call("doc:file", -1)
         self.menu = mp
         self.addr = addr
@@ -3572,7 +3599,18 @@ class notmuch_message_view(edlib.Pane):
         "handle:notmuch-addr-choice"
         if not str1 or not self.addr:
             return None
-        edlib.LOG("Addr menu Chose", str1, "for", self.addr)
+        if str1.startswith('-'):
+            # already in this query
+            return None
+        q = focus.call("doc:notmuch:get-query", str1, ret='str')
+        if type(q) == str:
+            q = q + " from:" + self.addr
+            if focus.call("doc:notmuch:set-query", str1, q) > 0:
+                focus.call("Message",
+                           "Updated query.%s with %s" % (str1, self.addr))
+            else:
+                focus.call("Message",
+                           "Update for query.%s failed." % str1)
         return 1
 
     def handle_render_line(self, key, focus, num, mark, mark2, comm2, **a):