From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp10.migadu.com ([2001:41d0:403:4ea1::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms12 with LMTPS id EPqKM5V0ymFHSgAAsNZ9tg (envelope-from ); Tue, 28 Dec 2021 02:21:09 +0000 Received: from out1.migadu.com ([2001:41d0:2:863f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp10.migadu.com with LMTPS id eCqgMJV0ymFbZwEAG6o9tA (envelope-from ); Tue, 28 Dec 2021 03:21:09 +0100 X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kyleam.com; s=key1; t=1640658069; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=hfhExFFVavcCJ1ssZomdFuTaxYpWpkF2A2FFmUcSQIk=; b=up/8mKVc3k6uB0IzxRvN/To97Y60RUly4kWVdwn3AyhdlDE3EEZpaoP9u8D4tzbDpVPo/h GKLsU1vF1/J+q3vNwD/YvEaOWY/G1xpJcpqte7DI+ZomncV+KvkbU14nHW5mmHIUHKrf71 3YdBfZNSoH4apUHy3pG+r0r0te6qE/NpQTUtoJ4/CzNXn+scgvFgQjTg5iNyaLSy3kKpbk D0gYmfmPOXl6hLk/bED/LrEsxQSLiK+qGAy/jUr9QF1hu/KhD3EmJaK4Z6QgvauGW+BJez iBVPt0tCD7OCHRPJWrHfFr9cfZ1W/r0ter/Ew/xMcmrmbRW9+Oeh7vz23aDj+g== From: Kyle Meyer To: piem@inbox.kyleam.com Subject: [PATCH 6/6] lei q: Sort threads by date of initial message Date: Mon, 27 Dec 2021 21:20:37 -0500 Message-Id: <20211228022037.206597-7-kyle@kyleam.com> In-Reply-To: <20211228022037.206597-1-kyle@kyleam.com> References: <20211228022037.206597-1-kyle@kyleam.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: kyleam.com X-TUID: 1oV9jrExh0Gs 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. --- piem-lei.el | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/piem-lei.el b/piem-lei.el index 1c232da2..db922944 100644 --- a/piem-lei.el +++ b/piem-lei.el @@ -43,6 +43,13 @@ (defcustom piem-lei-query-initial-input "d:20.days.ago.. " :type '(choice (const :tag "None" nil) (string :tag "Query"))) +(defcustom piem-lei-query-oldest-thread-first nil + "Whether to display older threads before new 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 @@ (defun piem-lei-query--has-descendant (msg1 msg2) (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 @@ (defun piem-lei-query--thread (records) (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 -- 2.34.0