mu4e-server: use updated handling for tempfile handling

Update for the server updates, to handle temp-file / normal args uniformly
This commit is contained in:
Dirk-Jan C. Binnema 2023-08-15 19:26:22 +03:00
parent 1a1eb1f906
commit 5dcdb88806
1 changed files with 38 additions and 32 deletions

View File

@ -73,17 +73,22 @@ better with e.g. offlineimap."
:group 'mu4e :group 'mu4e
:safe 'booleanp) :safe 'booleanp)
(defcustom mu4e-mu-allow-temp-file t (defcustom mu4e-mu-allow-temp-file nil
"Allow using temp-files for optimizing mu <-> mu4e communication. "Allow using temp-files for optimizing mu <-> mu4e communication.
Some commands - in particular \"find\" and \"contacts\" - return Some commands - in particular \"find\" and \"contacts\" - return
big s-expressions and it can make mu4e noticeably snappier by big s-expressions; and it turns out that reading those is faster
using temporary files (esp. when those live on an in-memory by passing them through a temp file rather than through normal
filesystem); so we pass the information that way. stdin/stdout channel - esp. on the (common case) where the
file-system for temp-files is in-memory.
This is an experimental feature, so at least for now it can be To see if the helps, you can benchmark the rendering with
turned off, as we gather experience. For a change to this variable (setq mu4e-headers-report-render-time t)
to take effect, you need to stop/start mu4e."
and compare the results with `mu4e-mu-allow-temp' set and unset.
Note: for a change to this variable to take effect, you need to
stop/start mu4e."
:type 'boolean :type 'boolean
:group 'mu4e :group 'mu4e
:safe 'booleanp) :safe 'booleanp)
@ -253,13 +258,28 @@ removed."
(setq mu4e--server-buf (substring mu4e--server-buf sexp-len)) (setq mu4e--server-buf (substring mu4e--server-buf sexp-len))
(car objcons))))))) (car objcons)))))))
(defun mu4e--server-file-data-and-remove (file) (defun mu4e--server-plist-get (plist key)
"Return Elisp object from FILE and remove FILE." "Like `plist-get' but load data from file if it is a string.
(with-temp-buffer
(insert-file-contents file) I.e. (mu4e--server-plist-get (:foo bar) :foo)
(goto-char (point-min)) => bar
(delete-file file) but
(read (current-buffer)))) (mu4e--server-plist-get (:foo \"/tmp/data.eld\") :foo)
=> evaluates the contents of /tmp/data.eld
(and deletes the file afterward).
This for the few sexps we get from the mu server that support this
(headers, contacts, maildirs)."
;; XXX: perhaps re-use the same buffer?
(let ((val (plist-get plist key)))
(if (stringp val)
(with-temp-buffer
(insert-file-contents val)
(goto-char (point-min))
(delete-file val)
(read (current-buffer)))
val)))
(defun mu4e--server-filter (_proc str) (defun mu4e--server-filter (_proc str)
"Filter string STR from PROC. "Filter string STR from PROC.
@ -330,13 +350,8 @@ The server output is as follows:
(cond (cond
;; a list of messages (after a find command) ;; a list of messages (after a find command)
((plist-get sexp :headers) ((plist-get sexp :headers)
(funcall mu4e-headers-append-func (plist-get sexp :headers)))
;; similar to :headers, but instead of reading the headers from the
;; process, we can read it from a tempfile the server created for us.
((plist-get sexp :headers-temp-file)
(funcall mu4e-headers-append-func (funcall mu4e-headers-append-func
(mu4e--server-file-data-and-remove (mu4e--server-plist-get sexp :headers)))
(plist-get sexp :headers-temp-file))))
;; the found sexp, we receive after getting all the headers ;; the found sexp, we receive after getting all the headers
((plist-get sexp :found) ((plist-get sexp :found)
@ -370,14 +385,7 @@ The server output is as follows:
;; note: we use 'member', to match (:contacts nil) ;; note: we use 'member', to match (:contacts nil)
((plist-member sexp :contacts) ((plist-member sexp :contacts)
(funcall mu4e-contacts-func (funcall mu4e-contacts-func
(plist-get sexp :contacts) (mu4e--server-plist-get sexp :contacts)
(plist-get sexp :tstamp)))
;; similar to :contacts, but instead of reading the headers from the
;; process, we can read it from a tempfile the server created for us.
((plist-get sexp :contacts-temp-file)
(funcall mu4e-contacts-func
(mu4e--server-file-data-and-remove
(plist-get sexp :contacts-temp-file))
(plist-get sexp :tstamp))) (plist-get sexp :tstamp)))
;; something got moved/flags changed ;; something got moved/flags changed
@ -403,10 +411,8 @@ The server output is as follows:
(funcall mu4e-info-func sexp)) (funcall mu4e-info-func sexp))
;; get some data ;; get some data
((plist-get sexp :data) ((plist-get sexp :maildirs)
(pcase (plist-get sexp :data) (setq mu4e-maildir-list (mu4e--server-plist-get sexp :maildirs)))
('maildirs (setq mu4e-maildir-list (plist-get sexp :value)))
(_ (mu4e-warn "unknown data kind"))))
;; receive an error ;; receive an error
((plist-get sexp :error) ((plist-get sexp :error)