discussion and development of piem
 help / color / mirror / code / Atom feed
From: Kyle Meyer <kyle@kyleam.com>
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	[thread overview]
Message-ID: <20211228022037.206597-7-kyle@kyleam.com> (raw)
In-Reply-To: <20211228022037.206597-1-kyle@kyleam.com>

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)
+
 \f
 ;;;; 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


  parent reply	other threads:[~2021-12-28  2:21 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-12-28  2:20 [PATCH 0/6] lei q: Support displaying multiple threads Kyle Meyer
2021-12-28  2:20 ` [PATCH 1/6] lei q: Allow initial input for query to be customized Kyle Meyer
2021-12-28  2:20 ` [PATCH 2/6] lei q: Extract buffer names to variables Kyle Meyer
2021-12-28  2:20 ` [PATCH 3/6] lei q: Support displaying multiple threads Kyle Meyer
2021-12-28 16:03   ` Xinglu Chen
2021-12-28 17:26     ` Kyle Meyer
2021-12-28  2:20 ` [PATCH 4/6] lei q: Add date placeholder for ghost messages Kyle Meyer
2021-12-28  2:20 ` [PATCH 5/6] lei q: Separate different threads with newline Kyle Meyer
2021-12-28  2:20 ` Kyle Meyer [this message]
2021-12-28 17:24   ` [PATCH 6/6] lei q: Sort threads by date of initial message Kyle Meyer

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://git.kyleam.com/

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

  git send-email \
    --in-reply-to=20211228022037.206597-7-kyle@kyleam.com \
    --to=kyle@kyleam.com \
    --cc=piem@inbox.kyleam.com \
    /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://git.kyleam.com/piem/

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).