From eb40c78da8217b22681f5c42d74d9bc64eff1e77 Mon Sep 17 00:00:00 2001 From: Kyle Meyer Date: Sat, 5 Jun 2021 17:13:50 -0400 Subject: lei: Add command for viewing a thread piem-lei-query presents a message-based overview. In many cases the caller will want to use that search result as a seed for finding the associated thread. Add a command that construct thread for a given message. The threading algorithm is based on public-inbox's. Some details may have been lost in translation, but I haven't spotted any differences yet when doing side-by-side comparisons of output from piem-lei-query-thread and public-inbox's web interface. And testing with a few ~100-message threads, the performance seems to be okay. The appearance also follows public-inbox's, which I like. Message-Id: <20210605211402.20304-7-kyle@kyleam.com> --- tests/piem-lei-tests.el | 74 +++++++++++++++++++++++++++++++++++++++++++++++++ tests/piem-tests.el | 1 + 2 files changed, 75 insertions(+) create mode 100644 tests/piem-lei-tests.el (limited to 'tests') diff --git a/tests/piem-lei-tests.el b/tests/piem-lei-tests.el new file mode 100644 index 0000000..e20c62f --- /dev/null +++ b/tests/piem-lei-tests.el @@ -0,0 +1,74 @@ +;;; piem-lei-tests.el --- tests for piem-lei -*- lexical-binding: t; -*- + +;; Copyright (C) 2021 all contributors + +;; Author: Kyle Meyer + +;; This program is free software; you can redistribute it and/or modify +;; it under the terms of the GNU General Public License as published by +;; the Free Software Foundation, either version 3 of the License, or +;; (at your option) any later version. + +;; This program is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. + +;; You should have received a copy of the GNU General Public License +;; along with this program. If not, see . + +;;; Code: + +(require 'ert) +(require 'piem-lei) + +(ert-deftest piem-lei-query--add-child () + (should-error + (piem-lei-query--add-child + (make-piem-lei-msg :mid "m1") + (make-piem-lei-msg :mid "m1"))) + (let ((m1 (make-piem-lei-msg :mid "m1")) + (m2 (make-piem-lei-msg :mid "m2"))) + (piem-lei-query--add-child m1 m2) + (should (equal (piem-lei-msg-parent m2) m1)) + (should (equal (piem-lei-msg-children m1) (list m2)))) + (let ((m1 (make-piem-lei-msg :mid "m1")) + (m2 (make-piem-lei-msg :mid "m2")) + (m3 (make-piem-lei-msg :mid "m3")) + (m4 (make-piem-lei-msg :mid "m4"))) + (piem-lei-query--add-child m1 m2) + (piem-lei-query--add-child m1 m4) + (piem-lei-query--add-child m3 m2) + (should (equal (piem-lei-msg-parent m2) m3)) + (should (equal (piem-lei-msg-children m1) (list m4))) + (should (equal (piem-lei-msg-children m3) (list m2))))) + +(ert-deftest piem-lei-query--has-descendant () + (let ((m1 (make-piem-lei-msg :mid "m1")) + (m2 (make-piem-lei-msg :mid "m2"))) + (should-not + (piem-lei-query--has-descendant m1 m2)) + (should-not + (piem-lei-query--has-descendant m2 m1))) + (let ((m1 (make-piem-lei-msg :mid "m1"))) + (should (piem-lei-query--has-descendant m1 m1))) + (let ((m1 (make-piem-lei-msg :mid "m1")) + (m2 (make-piem-lei-msg :mid "m2"))) + (piem-lei-query--add-child m1 m2) + (should (piem-lei-query--has-descendant m1 m2)) + (should-not + (piem-lei-query--has-descendant m2 m1))) + (let ((m1 (make-piem-lei-msg :mid "m1")) + (m2 (make-piem-lei-msg :mid "m2")) + (m3 (make-piem-lei-msg :mid "m3"))) + (piem-lei-query--add-child m1 m2) + (piem-lei-query--add-child m2 m3) + (should (piem-lei-query--has-descendant m1 m2)) + (should (piem-lei-query--has-descendant m1 m3)) + (should (piem-lei-query--has-descendant m2 m3)) + (should-not (piem-lei-query--has-descendant m2 m1)) + (should-not (piem-lei-query--has-descendant m3 m2)) + (should-not (piem-lei-query--has-descendant m3 m1)))) + +(provide 'piem-lei-tests) +;;; piem-lei-tests.el ends here diff --git a/tests/piem-tests.el b/tests/piem-tests.el index 5f01a5e..91beb9a 100644 --- a/tests/piem-tests.el +++ b/tests/piem-tests.el @@ -21,6 +21,7 @@ (require 'ert) (require 'piem) +(require 'piem-lei-tests) (require 'piem-rmail-tests) (ert-deftest piem-message-link-re () -- cgit v1.2.3 From df741cd4254c274903dac9390d2b158558e6efa6 Mon Sep 17 00:00:00 2001 From: Kyle Meyer Date: Sat, 5 Jun 2021 17:13:55 -0400 Subject: piem-lei-query-thread: Omit main part of subject if shared MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In addition to suppressing identical subjects (after stripping "re:"), public-inbox's web interface will compare the current line's subject with the previous line's, and cut off the shared tail: [PATCH] Add basic integration for Rmail ` ` [PATCH v2] " <-- here ` I think the above is helpful. However, in some cases, I find the presentation more confusing than helpful: [PATCH 0/3] notmuch: Improve handling of attached patches ` [PATCH 1/3] piem-notmuch--with-current-message: Declare debug and indent specs ` [PATCH 2/3] piem-notmuch-am-ready-mbox: Improve handling of attachments ` ` [PATCH v2 0/3] notmuch: Improve handling of attached patches ` [PATCH v2 1/3] piem-notmuch--with-current-message: Declare debug and indent specs ` [PATCH v2 2/3] piem-notmuch-am-ready-mbox: Improve handling of attachments ` [PATCH v2 3/3] gnus, notmuch: Absorb now-shared bits into patch attachment helper ` [PATCH " It takes me a second to figure out what the omitted bits in the last line's subject are. I'm not sure, but I think the subject truncation that I find clear is where the omitted text is the main subject after a bracketed tag (i.e. "[tag] main"), not more or less. Teach piem-lei-query-thread to split the subject into a "prefix" (some number of "[tag]" items) and a "main" part (everything else), and elide a line's main part if it matches the previous line's. In the above example, the last line would be ` [PATCH 3/3] … Message-Id: <20210605211402.20304-12-kyle@kyleam.com> --- piem-lei.el | 30 +++++++++++++++++++++++++++++- tests/piem-lei-tests.el | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 1 deletion(-) (limited to 'tests') diff --git a/piem-lei.el b/piem-lei.el index cf19195..f7ccc6e 100644 --- a/piem-lei.el +++ b/piem-lei.el @@ -341,6 +341,33 @@ Return a list with a `piem-lei-msg' object for each root." thread) (nreverse roots)))) +(defvar piem-lei-query--subject-split-re + (rx string-start + ;; Prefix. + (group (zero-or-more space) + (one-or-more "[" (one-or-more (not (any "]" "\n"))) "]" + (one-or-more space))) + ;; Main subject. A match consists of at least two islands of + ;; non-space characters because there's not much point in + ;; eliding one word. + (group (one-or-more (not space)) + (one-or-more space) + (not space) + (one-or-more anychar)))) + +(defun piem-lei-query--split-subject (s) + (if (string-match piem-lei-query--subject-split-re s) + (cons (match-string 1 s) (match-string 2 s)) + (cons nil s))) + +(defun piem-lei-query--elide-subject (s1 s2) + (pcase-let ((`(,head2 . ,tail2) (piem-lei-query--split-subject s2))) + (if (and s1 head2 + (let ((tail1 (cdr (piem-lei-query--split-subject s1)))) + (equal tail1 tail2))) + (concat head2 (if (char-displayable-p ?…) "…" "...")) + s2))) + (defun piem-lei-query--format-thread-marker (level) (if (= level 0) "" @@ -400,7 +427,8 @@ Return a list with a `piem-lei-msg' object for each root." (if (equal subject subject-prev) "" (concat " " - (propertize subject + (propertize (piem-lei-query--elide-subject + subject-prev subject) 'font-lock-face 'piem-lei-query-subject)))) (add-text-properties (line-beginning-position) diff --git a/tests/piem-lei-tests.el b/tests/piem-lei-tests.el index e20c62f..71dc109 100644 --- a/tests/piem-lei-tests.el +++ b/tests/piem-lei-tests.el @@ -70,5 +70,47 @@ (should-not (piem-lei-query--has-descendant m3 m2)) (should-not (piem-lei-query--has-descendant m3 m1)))) +(ert-deftest piem-lei-query--elide-subject:keep-original () + (should (equal "ghi jlk" + (piem-lei-query--elide-subject + nil + "ghi jlk"))) + (should (equal "ghi jlk" + (piem-lei-query--elide-subject + "abc def" + "ghi jlk"))) + (should (equal "abc def" + (piem-lei-query--elide-subject + "[PATCH] abc def" + "abc def"))) + (should (equal "abc def" + (piem-lei-query--elide-subject + "[bug#00000] [PATCH] abc def" + "abc def"))) + (should (equal "abc def" + (piem-lei-query--elide-subject + "[PATCH] abc def" + "abc def"))) + (should (equal "[bug#00000] [PATCH v2] abc" + (piem-lei-query--elide-subject + "[bug#00000] [PATCH] abc" + "[bug#00000] [PATCH v2] abc"))) + (should (equal "[bug#00000] [PATCH v2] ghi jlk mno" + (piem-lei-query--elide-subject + "[bug#00000] [PATCH] abc def" + "[bug#00000] [PATCH v2] ghi jlk mno")))) + +(defvar piem-lei-tests-elide-string (if (char-displayable-p ?…) "…" "...")) + +(ert-deftest piem-lei-query--elide-subject:elide () + (should (equal (concat "[PATCH v2] " piem-lei-tests-elide-string) + (piem-lei-query--elide-subject + "[PATCH] abc def" + "[PATCH v2] abc def"))) + (should (equal (concat "[bug#00000] [PATCH v2] " piem-lei-tests-elide-string) + (piem-lei-query--elide-subject + "[bug#00000] [PATCH] abc def" + "[bug#00000] [PATCH v2] abc def")))) + (provide 'piem-lei-tests) ;;; piem-lei-tests.el ends here -- cgit v1.2.3 From 653326e51503ba0bddcf9dc365003886527edbe2 Mon Sep 17 00:00:00 2001 From: Kyle Meyer Date: Sat, 5 Jun 2021 17:13:56 -0400 Subject: piem-lei-query-thread: Add bug#NNN special case when eliding subject In debbugs threads, it's not uncommon for a leading "[bug#NNN]" in the subject to be converted to "bug#NNN:" [*]. I'm not sure what the source of this is, but it prevents the suppression of an otherwise identical subject. It's probably not worth normalizing before the comparison to get full suppression, but it'd be nice to at least elide the main part of the subject so it's more obvious that it didn't change. Add a special case so that "bug#NNN:" prefix is treated the same as a bracketed prefix. [*] example: https://yhetil.org/guix-patches/20201128051435.30580-1-kyle@kyleam.com Message-Id: <20210605211402.20304-13-kyle@kyleam.com> --- piem-lei.el | 14 ++++++++++++-- tests/piem-lei-tests.el | 6 +++++- 2 files changed, 17 insertions(+), 3 deletions(-) (limited to 'tests') diff --git a/piem-lei.el b/piem-lei.el index f7ccc6e..3760176 100644 --- a/piem-lei.el +++ b/piem-lei.el @@ -345,8 +345,18 @@ Return a list with a `piem-lei-msg' object for each root." (rx string-start ;; Prefix. (group (zero-or-more space) - (one-or-more "[" (one-or-more (not (any "]" "\n"))) "]" - (one-or-more space))) + (or (and (one-or-more (and "bug#" (one-or-more digit) ":")) + (one-or-more space) + (zero-or-more + ;; This pattern... + "[" (one-or-more (not (any "]" "\n"))) "]" + (one-or-more space))) + (one-or-more + ;; ... is repeated here. Extract it to an rx-let + ;; binding once minimum Emacs version is at least + ;; 27. + "[" (one-or-more (not (any "]" "\n"))) "]" + (one-or-more space)))) ;; Main subject. A match consists of at least two islands of ;; non-space characters because there's not much point in ;; eliding one word. diff --git a/tests/piem-lei-tests.el b/tests/piem-lei-tests.el index 71dc109..dd58360 100644 --- a/tests/piem-lei-tests.el +++ b/tests/piem-lei-tests.el @@ -110,7 +110,11 @@ (should (equal (concat "[bug#00000] [PATCH v2] " piem-lei-tests-elide-string) (piem-lei-query--elide-subject "[bug#00000] [PATCH] abc def" - "[bug#00000] [PATCH v2] abc def")))) + "[bug#00000] [PATCH v2] abc def"))) + (should (equal (concat "bug#00000: [PATCH v2] " piem-lei-tests-elide-string) + (piem-lei-query--elide-subject + "[bug#00000] [PATCH] abc def" + "bug#00000: [PATCH v2] abc def")))) (provide 'piem-lei-tests) ;;; piem-lei-tests.el ends here -- cgit v1.2.3