From 52ece47dd7a490e9f30d9e4d85a0bddce44eb35e Mon Sep 17 00:00:00 2001 From: Kyle Meyer Date: Sat, 5 Jun 2021 17:14:01 -0400 Subject: lei: Wire up piem.el hooks piem-lei-show-mode and piem-lei-query-mode now have enough functionality to implement all piem.el hooks except for piem-am-ready-mbox-functions. Message-Id: <20210605211402.20304-18-kyle@kyleam.com> --- piem-lei.el | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/piem-lei.el b/piem-lei.el index 71f548c..bcc2589 100644 --- a/piem-lei.el +++ b/piem-lei.el @@ -27,6 +27,7 @@ (require 'message) (require 'piem) (require 'seq) +(require 'subr-x) (defgroup piem-lei nil "lei integration for piem." @@ -554,5 +555,58 @@ Return a list with a `piem-lei-msg' object for each root." (piem-lei-query-mode) (pop-to-buffer-same-window (current-buffer))))) + +;;;; piem integration + +(defun piem-lei-get-mid () + "Return the message ID of a lei buffer." + (cond ((derived-mode-p 'piem-lei-show-mode) + piem-lei-show-mid) + ((derived-mode-p 'piem-lei-query-mode) + (piem-lei-query-get-mid)))) + +(defun piem-lei-get-inbox () + "Return inbox name from a lei buffer." + (when-let ((mid (piem-lei-get-mid))) + (with-temp-buffer + (call-process "lei" nil '(t nil) nil + "q" "--format=mboxrd" (concat "m:" mid)) + (goto-char (point-min)) + (piem-inbox-by-header-match)))) + +(defun piem-lei-known-mid-p (mid) + "Return non-nil if MID is known to lei. +The message ID should not include have surrounding brackets." + (not (string-empty-p + (with-temp-buffer + (call-process "lei" nil '(t nil) nil + "q" "--format=ldjson" (concat "m:" mid)) + (buffer-string))))) + +(defun piem-lei-mid-to-thread (mid) + "Return a function that inserts an mbox for MID's thread." + (when (piem-lei-known-mid-p mid) + (lambda () + (call-process "lei" nil '(t nil) nil + "q" "--format=mboxrd" "--threads" + (concat "m:" mid))))) + +;;;###autoload +(define-minor-mode piem-lei-mode + "Toggle lei support for piem. +With a prefix argument ARG, enable piem-lei mode if ARG is +positive, and disable it otherwise. If called from Lisp, enable +the mode if ARG is omitted or nil." + :global t + :init-value nil + (if piem-lei-mode + (progn + (add-hook 'piem-get-inbox-functions #'piem-lei-get-inbox) + (add-hook 'piem-get-mid-functions #'piem-lei-get-mid) + (add-hook 'piem-mid-to-thread-functions #'piem-lei-mid-to-thread)) + (remove-hook 'piem-get-inbox-functions #'piem-lei-get-inbox) + (remove-hook 'piem-get-mid-functions #'piem-lei-get-mid) + (remove-hook 'piem-mid-to-thread-functions #'piem-lei-mid-to-thread))) + ;;; piem-lei.el ends here (provide 'piem-lei) -- cgit v1.2.3