Subject: [PATCH 0/4] debbugs improvements
From: Morgan Smith <[email-masked]>
To: [email-masked]
Cc: Michael Albinus <[email-masked]>
Date: Wed, 14 Jun 2023 08:15:57 -0400
List-Id: <debbugs-submit.debbugs.gnu.org>

Hello!

These patches enable the use of some more 'tabulated-list' functions
like 'tabulated-list-sort' (bound to "S").

You may or may not want to install the last patch.  It does add
potential network calls to the 'revert-buffer' function.  If there are
legitimate reasons to revert the buffer without fetching new bugs then
people might not like that change.

Morgan Smith (4):
  Call 'tabulated-list-print-entry' in 'debbugs-gnu-print-entry'.
  Don't error out on 'nil' alist value.
  Don't sort bugs in 'debbugs-gnu-show-reports'.
  Add 'debbugs-gnu-rescan' to 'tabulated-list-revert-hook'.

 debbugs-gnu.el | 62 +++++++++-----------------------------------------
 1 file changed, 11 insertions(+), 51 deletions(-)
 
From f64d98ea82cfc999fc6eb644696dc2a0f6f100ec Mon Sep 17 00:00:00 2001
From: Morgan Smith <[email-masked]>
Date: Mon, 12 Jun 2023 20:12:11 -0400
Subject: [PATCH 1/4] Call 'tabulated-list-print-entry' in
 'debbugs-gnu-print-entry'.

Some of the features of 'tabulated-list' (tabulated-list-next-column,
tabulated-list-sort) didn't work properly because 'debbugs-gnu-print-entry'
didn't set the 'tabulated-list-column-name' text property.  Instead of trying
to duplicate code we can simply call 'tabulated-list-print-entry'.

* debbugs-gnu.el (debbugs-gnu-print-entry): Remove all printing logic and
replace with a call to 'tabulated-list-print-entry'.
---
 debbugs-gnu.el | 40 ++--------------------------------------
 1 file changed, 2 insertions(+), 38 deletions(-)

diff --git a/debbugs-gnu.el b/debbugs-gnu.el
index 337c7cbb9a..2c76a3361b 100644
--- a/debbugs-gnu.el
+++ b/debbugs-gnu.el
@@ -1052,18 +1052,7 @@ are taken from the cache instead."
 (defun debbugs-gnu-print-entry (list-id cols)
   "Insert a debbugs entry at point.
 Used instead of `tabulated-list-print-entry'."
-  (let ((beg (point))
-	(pos 0)
-	(case-fold-search t)
-	(id               (aref cols 0))
-	(id-length        (nth 1 (aref tabulated-list-format 0)))
-	(state            (aref cols 1))
-	(state-length     (nth 1 (aref tabulated-list-format 1)))
-	(submitter        (aref cols 2))
-	(submitter-length (nth 1 (aref tabulated-list-format 2)))
-	(title            (aref cols 3))
-	;; (title-length     (nth 1 (aref tabulated-list-format 3)))
-        )
+  (let ((case-fold-search t))
     (when (and
 	   ;; We may have a narrowing in effect.
 	   (or (not debbugs-gnu-limit)
@@ -1090,32 +1079,7 @@ Used instead of `tabulated-list-print-entry'."
 				       (> (cddr check) val)))
 			  (throw :suppress t))))))))
 
-      ;; Insert id.
-      (indent-to (- id-length (length id)))
-      (insert id)
-      ;; Insert state.
-      (indent-to (setq pos (+ pos id-length 1)) 1)
-      (insert (if (> (length state) state-length)
-		  (propertize (substring state 0 state-length)
-			      'help-echo state)
-		state))
-      ;; Insert submitter.
-      (indent-to (setq pos (+ pos state-length 1)) 1)
-      (insert (if (> (length submitter) submitter-length)
-		  (propertize (substring submitter 0 submitter-length)
-			      'help-echo submitter)
-		submitter))
-      (indent-to (+ pos (1- submitter-length)))
-      ;; Insert title.
-      (indent-to (setq pos (+ pos submitter-length 1)) 1)
-      (insert (propertize title 'help-echo title))
-      ;; Add properties.
-      (add-text-properties
-       beg (point)
-       `(tabulated-list-id ,list-id
-	 tabulated-list-entry ,cols
-	 mouse-face highlight))
-      (insert ?\n))))
+      (tabulated-list-print-entry list-id cols))))
 
 (defun debbugs-gnu-menu-map-emacs-enabled ()
   "Whether \"Show Release Blocking Bugs\" is enabled in the menu."
-- 
2.40.1

From 54a3e56379308c05a9ac6646160d136a26044b95 Mon Sep 17 00:00:00 2001
From: Morgan Smith <[email-masked]>
Date: Mon, 12 Jun 2023 19:56:50 -0400
Subject: [PATCH 2/4] Don't error out on 'nil' alist value.

The intention of the DEFAULT argument to alist-get is to return "" instead of
'nil'.  However, this still returns 'nil' if the key exists but the value is
'nil'.

This problem wasn't detected previously since the functions
'debbugs-gnu-sort-submitter' and 'debbugs-gnu-sort-title' weren't actually
used until the previous commit.

* debbugs-gnu.el (debbugs-gnu-print-entry):
(debbugs-gnu-sort-submitter):
(debbugs-gnu-sort-title):
Remove DEFAULT argument of alist-get and instead wrap call with 'or' to return
default.
---
 debbugs-gnu.el | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)

diff --git a/debbugs-gnu.el b/debbugs-gnu.el
index 2c76a3361b..c28a4aa5e4 100644
--- a/debbugs-gnu.el
+++ b/debbugs-gnu.el
@@ -1062,7 +1062,7 @@ Used instead of `tabulated-list-print-entry'."
 	       (not (catch :suppress
 		      (dolist (check debbugs-gnu-default-suppress-bugs)
 			(when (string-match
-			       (cdr check) (alist-get (car check) list-id ""))
+			       (cdr check) (or (alist-get (car check) list-id) ""))
 			  (throw :suppress t))))))
 	   ;; Filter search list.
 	   (not (catch :suppress
@@ -1288,10 +1288,10 @@ Interactively, it is non-nil with the prefix argument."
 (defun debbugs-gnu-sort-submitter (s1 s2)
   (let ((address1
 	 (debbugs-gnu--split-address
-	  (decode-coding-string (alist-get 'originator (car s1) "") 'utf-8)))
+	  (decode-coding-string (or (alist-get 'originator (car s1)) "") 'utf-8)))
 	(address2
 	 (debbugs-gnu--split-address
-	  (decode-coding-string (alist-get 'originator (car s2) "") 'utf-8))))
+	  (decode-coding-string (or (alist-get 'originator (car s2)) "") 'utf-8))))
     (cond
      ;; Bugs I'm the originator of go to the beginning.
      ((and (string-equal user-mail-address (car address1))
@@ -1309,14 +1309,14 @@ Interactively, it is non-nil with the prefix argument."
 (defun debbugs-gnu-sort-title (s1 s2)
   (let ((owner1
 	 (car (debbugs-gnu--split-address
-	       (decode-coding-string (alist-get 'owner (car s1) "") 'utf-8))))
+	       (decode-coding-string (or (alist-get 'owner (car s1)) "") 'utf-8))))
 	(subject1
-	 (decode-coding-string (alist-get 'subject (car s1) "") 'utf-8))
+	 (decode-coding-string (or (alist-get 'subject (car s1)) "") 'utf-8))
 	(owner2
 	 (car (debbugs-gnu--split-address
-	       (decode-coding-string (alist-get 'owner (car s2) "") 'utf-8))))
+	       (decode-coding-string (or (alist-get 'owner (car s2)) "") 'utf-8))))
 	(subject2
-	 (decode-coding-string (alist-get 'subject (car s2) "") 'utf-8)))
+	 (decode-coding-string (or (alist-get 'subject (car s2)) "") 'utf-8)))
     (cond
      ;; Bugs I'm the owner of go to the beginning.
      ((and (string-equal user-mail-address owner1)
-- 
2.40.1

From 550385aeb852ad30bb18d341d475f65ec613fdaf Mon Sep 17 00:00:00 2001
From: Morgan Smith <[email-masked]>
Date: Mon, 12 Jun 2023 20:13:53 -0400
Subject: [PATCH 3/4] Don't sort bugs in 'debbugs-gnu-show-reports'.

Everything is automatically sorted by 'tabulated-list-mode' according to
'tabulated-list-sort-key' which we already set to the id.

* debbugs-gnu.el (debbugs-gnu-show-reports): Don't sort the bugs.
---
 debbugs-gnu.el | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)

diff --git a/debbugs-gnu.el b/debbugs-gnu.el
index c28a4aa5e4..c65ea3e827 100644
--- a/debbugs-gnu.el
+++ b/debbugs-gnu.el
@@ -923,7 +923,6 @@ are taken from the cache instead."
 
     ;; Print bug reports.
     (dolist (status
-	     (sort
 	      (let ((debbugs-cache-expiry (if offline nil debbugs-cache-expiry))
 		    ids)
 		(apply #'debbugs-get-status
@@ -933,11 +932,7 @@ are taken from the cache instead."
 					(push key ids))
 				      debbugs-cache-data)
 			     ids)
-			 (debbugs-gnu-get-bugs debbugs-gnu-local-query))))
-	      ;; Sort so that if a new report gets merged with an old
-	      ;; report, it shows up under the new report.
-	      (lambda (s1 s2)
-		(> (alist-get 'id s1) (alist-get 'id s2)))))
+			 (debbugs-gnu-get-bugs debbugs-gnu-local-query)))))
       (let* ((id (alist-get 'id status))
 	     (words (cons (alist-get 'severity status)
 			  (alist-get 'keywords status)))
-- 
2.40.1

From 5526b57121df84a9db6fd8f14771b01ee2e2cddc Mon Sep 17 00:00:00 2001
From: Morgan Smith <[email-masked]>
Date: Mon, 12 Jun 2023 20:18:38 -0400
Subject: [PATCH 4/4] Add 'debbugs-gnu-rescan' to 'tabulated-list-revert-hook'.

This now fetches new bugs when the buffer is reverted.

* debbugs-gnu.el (debbugs-gnu-mode): Add 'debbugs-gnu-rescan' to
'tabulated-list-revert-hook'.
---
 debbugs-gnu.el | 1 +
 1 file changed, 1 insertion(+)

diff --git a/debbugs-gnu.el b/debbugs-gnu.el
index c65ea3e827..df651f3f2f 100644
--- a/debbugs-gnu.el
+++ b/debbugs-gnu.el
@@ -1221,6 +1221,7 @@ Interactively, it is non-nil with the prefix argument."
 			       ("Title"     10 debbugs-gnu-sort-title)])
   (setq tabulated-list-sort-key (cons "Id" nil))
   (setq tabulated-list-printer #'debbugs-gnu-print-entry)
+  (add-hook 'tabulated-list-revert-hook #'debbugs-gnu-rescan nil t)
   (buffer-disable-undo)
   (setq truncate-lines t)
   (setq buffer-read-only t))
-- 
2.40.1