unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
From: "Antoine Beaupré" <anarcat@orangeseeds.org>
To: David Edmondson <dme@dme.org>, notmuch@notmuchmail.org
Subject: PATCH: fetch missing keys in the background
Date: Mon, 03 Sep 2018 13:47:09 -0400	[thread overview]
Message-ID: <87sh2qv5gi.fsf@curie.anarc.at> (raw)
In-Reply-To: <cunzhwyxzs3.fsf@disaster-area.hh.sledj.net>

[-- Attachment #1: Type: text/plain, Size: 246 bytes --]

On 2018-09-03 18:21:32, David Edmondson wrote:
> How about this patch?
>
> You'll need to set “notmuch-crypto-get-keys-asynchronously” to “t” to
> see any benefit.

This is great!

As you said on IRC, the patch is more likely:


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: notmuch-async-fetch.patch --]
[-- Type: text/x-diff, Size: 2696 bytes --]

diff --git i/emacs/notmuch-crypto.el w/emacs/notmuch-crypto.el
index fc2b5301..175dc5f2 100644
--- i/emacs/notmuch-crypto.el
+++ w/emacs/notmuch-crypto.el
@@ -43,6 +43,18 @@ mode."
   :package-version '(notmuch . "0.25")
   :group 'notmuch-crypto)
 
+(defcustom notmuch-crypto-get-keys-asynchronously nil
+  "Retrieve gpg keys asynchronously.
+
+If this variable is non-nil, hitting the crypto button will
+trigger network operations in the background. This will also have
+the effect of disabling the automatic refresh on completion. Keep
+this set to nil to make sure the button display is up to date,
+but this will freeze everything until the crypto operation is
+completed, which can take a long time for larger keyrings."
+  :type 'boolean
+  :group 'notmuch-crypto)
+
 (defface notmuch-crypto-part-header
   '((((class color)
       (background dark))
@@ -145,19 +157,35 @@ mode."
 	(call-process epg-gpg-program nil t t "--list-keys" fingerprint))
       (recenter -1))))
 
+(defun notmuch-crypto--async-key-sentinel (process event)
+  (let ((status (process-status process))
+	(exit-status (process-exit-status process)))
+    (when (memq status '(exit signal))
+      (message "Asynchronous GPG key retrieval %s."
+	       (if (= exit-status 0)
+		   "completed"
+		 "failed")))))
+
 (defun notmuch-crypto-sigstatus-error-callback (button)
   (let* ((sigstatus (button-get button :notmuch-sigstatus))
 	 (keyid (concat "0x" (plist-get sigstatus :keyid)))
-	 (buffer (get-buffer-create "*notmuch-crypto-gpg-out*"))
-	 (window (display-buffer buffer t nil)))
-    (with-selected-window window
-      (with-current-buffer buffer
-	(goto-char (point-max))
-	(call-process epg-gpg-program nil t t "--recv-keys" keyid)
-	(insert "\n")
-	(call-process epg-gpg-program nil t t "--list-keys" keyid))
-      (recenter -1))
-    (notmuch-show-refresh-view)))
+	 (buffer (get-buffer-create "*notmuch-crypto-gpg-out*")))
+    (if notmuch-crypto-get-keys-asynchronously
+	(progn
+	  (message "Getting the GPG key %s asynchronously..." keyid)
+	  (make-process :name "notmuch GPG key retrieval"
+		       :buffer buffer
+		       :command (list epg-gpg-program "--recv-keys" keyid)
+		       :sentinel #'notmuch-crypto--async-key-sentinel))
+      (let ((window (display-buffer buffer t nil)))
+	(with-selected-window window
+	  (with-current-buffer buffer
+	    (goto-char (point-max))
+	    (call-process epg-gpg-program nil t t "--recv-keys" keyid)
+	    (insert "\n")
+	    (call-process epg-gpg-program nil t t "--list-keys" keyid))
+	  (recenter -1))
+	(notmuch-show-refresh-view)))))
 
 (defun notmuch-crypto-insert-encstatus-button (encstatus)
   (let* ((status (plist-get encstatus :status))

[-- Attachment #3: Type: text/plain, Size: 904 bytes --]


One thing that's missing is to refresh the view automatically. I
understand this might be difficult to implement however. For example, I
naively tried to add this at the end of the sentinel:

      (when (= exit-status 0)
	(notmuch-show-refresh-view))

Then switch to another buffer (back to the search view, FWIW). The
sentinel then fails with:

error in process sentinel: notmuch-escape-boolean-term: Wrong type argument: stringp, nil
error in process sentinel: Wrong type argument: stringp, nil

Not sure why exactly. But I would certainly take the async update over
automatic refresh any time. It would also be useful to indicate that
will be the effect of the variable in the customize help as well. I gave
that a try in the above patch.

Thanks for the ultra-fast response!

A.

-- 
The United States is a nation of laws:
badly written and randomly enforced.
                        - Frank Zappa

  parent reply	other threads:[~2018-09-03 17:47 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-09-03 15:49 feature request: fetch missing keys in the background Antoine Beaupré
2018-09-03 16:46 ` David Edmondson
2018-09-03 16:50   ` David Edmondson
2018-09-03 17:10   ` Antoine Beaupré
2018-09-03 17:21     ` David Edmondson
2018-09-03 17:23       ` David Edmondson
2018-09-03 17:47       ` Antoine Beaupré [this message]
2018-09-03 18:19         ` PATCH: " David Edmondson
2018-09-03 19:49       ` feature request: " Antoine Beaupré
2018-09-04 13:32         ` David Edmondson
2018-09-04 13:34           ` Antoine Beaupré
2018-09-04 13:39             ` David Edmondson
2018-09-04 14:12               ` Antoine Beaupré
2018-09-04 14:39                 ` David Bremner
2018-09-07 11:31                 ` David Edmondson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://notmuchmail.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87sh2qv5gi.fsf@curie.anarc.at \
    --to=anarcat@orangeseeds.org \
    --cc=dme@dme.org \
    --cc=notmuch@notmuchmail.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://yhetil.org/notmuch.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).