From 8689741dd5a2dbb22ff848fa7993727d834064f0 Mon Sep 17 00:00:00 2001 From: Kyle Meyer Date: Mon, 27 Dec 2021 21:20:37 -0500 Subject: lei q: Sort threads by date of initial message Now that there is a command for displaying multiple threads, the order that piem-lei-query--thread returns the threads matters. When lei-q is given the --threads option, the threads don't have a clear order, and --sort and --reverse don't have an effect, so piem-lei-query--thread must handle the sorting. Teach piem-lei-query--thread to sort by the date of the root message in a thread, adding a option to control the direction of this sorting. It may be better to sort by the latest date within the entire thread. However, doing so is more expensive, so don't bother until a clear case is made that it's the better option. Message-Id: <20211228022037.206597-7-kyle@kyleam.com> --- piem-lei.el | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/piem-lei.el b/piem-lei.el index 1c232da..6c53733 100644 --- a/piem-lei.el +++ b/piem-lei.el @@ -43,6 +43,13 @@ :type '(choice (const :tag "None" nil) (string :tag "Query"))) +(defcustom piem-lei-query-oldest-thread-first nil + "Whether to display older threads before newer ones. +The date and time of the initial message is taken as the age of +the thread." + :package-version '(piem . "0.4.0") + :type 'boolean) + ;;;; Helpers @@ -567,6 +574,20 @@ external (via `lei add-external')." (setq msg2 (piem-lei-msg-parent msg2))) nil))) +(defun piem-lei--msg-time-with-fallback (msg) + (or (piem-lei-msg-time msg) + ;; The initial message is a ghost. Use the time from the first + ;; child encountered, without making any effort to ensure that + ;; it's the sibling with the earliest time. + (catch 'stop + (let ((children (piem-lei-msg-children msg))) + (while children + (let ((child (pop children))) + (if-let ((time (piem-lei-msg-time child))) + (throw 'stop time) + (setq children + (append children (piem-lei-msg-children child)))))))))) + (defun piem-lei-query--thread (records) "Thread messages in RECORDS. @@ -612,7 +633,13 @@ Return a list with a `piem-lei-msg' object for each root." (unless (piem-lei-msg-parent v) (push v roots))) thread) - (nreverse roots)))) + (let* ((fn (if piem-lei-query-oldest-thread-first #'not #'identity)) + (sort-fn + (lambda (a b) + (funcall fn + (time-less-p (piem-lei--msg-time-with-fallback b) + (piem-lei--msg-time-with-fallback a)))))) + (sort roots sort-fn))))) (defvar piem-lei-query--subject-split-re (rx string-start -- cgit v1.2.3