Subject: [PATCH] Support hiding deleted IMAP messages (v2)

From: Nikolaus Rath <[email-masked]>
Date: Wed, 01 Jul 2015 19:36:57 -0700

Hello,

The previous patch did not take into account that an IMAP server is free
to send the UID, FLAGS and RFC822.SIZE response data items in arbitrary
order. This has been fixed in this version of the patch.

Best,
-Nikolaus

-- 
GPG encrypted emails preferred. Key id: 0xD113FCAC3C4E599F
Fingerprint: ED31 791B 2C5C 1613 AF38 8B8A D113 FCAC 3C4E 599F

             »Time flies like an arrow, fruit flies like a Banana.«
--- nnimap.el.bak3	2015-05-26 19:27:53.797971148 -0700
+++ nnimap.el.bak4	2015-07-01 19:20:21.220598632 -0700
@@ -134,6 +134,10 @@
 likely value would be \"text/\" to automatically fetch all
 textual parts.")
 
+(defvoo nnimap-hide-deleted nil
+  "If non-nil, articles flagged as deleted (using the IMAP
+\\Delete flag) will not be shown in the Summary buffer.")
+
 (defgroup nnimap nil
   "IMAP for Gnus."
   :group 'gnus)
@@ -180,7 +184,7 @@
   (nnimap-find-process-buffer nntp-server-buffer))
 
 (defun nnimap-header-parameters ()
-  (format "(UID RFC822.SIZE BODYSTRUCTURE %s)"
+  (format "(UID FLAGS RFC822.SIZE BODYSTRUCTURE %s)"
 	  (format
 	   (if (nnimap-ver4-p)
 	       "BODY.PEEK[HEADER.FIELDS %s]"
@@ -239,12 +243,19 @@
 	      (and (re-search-forward "UID \\([0-9]+\\)" (line-end-position)
 				      t)
 		   (match-string 1)))
-	(setq lines nil)
+	(beginning-of-line)
+	(setq flags
+	      (and (re-search-forward "FLAGS (\\([^)]+\\))"
+				      (line-end-position)
+				      t)
+		   (split-string (match-string 1))))
+	(beginning-of-line)
 	(setq size
 	      (and (re-search-forward "RFC822.SIZE \\([0-9]+\\)"
 				      (line-end-position)
 				      t)
 		   (match-string 1)))
+	(setq lines nil)
 	(beginning-of-line)
 	(when (search-forward "BODYSTRUCTURE" (line-end-position) t)
 	  (let ((structure (ignore-errors
@@ -258,20 +269,25 @@
 			     (equal (upcase (nth 1 structure)) "RFC822"))
 			    (nth 9 structure)
 			  (nth 7 structure)))))
-	(delete-region (line-beginning-position) (line-end-position))
-	(insert (format "211 %s Article retrieved." article))
-	(forward-line 1)
-	(when size
-	  (insert (format "Chars: %s\n" size)))
-	(when lines
-	  (insert (format "Lines: %s\n" lines)))
-	;; Most servers have a blank line after the headers, but
-	;; Davmail doesn't.
-	(unless (re-search-forward "^\r$\\|^)\r?$" nil t)
-	  (goto-char (point-max)))
-	(delete-region (line-beginning-position) (line-end-position))
-	(insert ".")
-	(forward-line 1)))))
+        (if (and nnimap-hide-deleted
+                 (member "\\Deleted" flags))
+            (delete-region (line-beginning-position)
+                           (or (re-search-forward "^\r$\\|^)\r?$" nil t)
+                               (point-max)))
+          (delete-region (line-beginning-position) (line-end-position))
+          (insert (format "211 %s Article retrieved." article))
+          (forward-line 1)
+          (when size
+            (insert (format "Chars: %s\n" size)))
+          (when lines
+            (insert (format "Lines: %s\n" lines)))
+          ;; Most servers have a blank line after the headers, but
+          ;; Davmail doesn't.
+          (unless (re-search-forward "^\r$\\|^)\r?$" nil t)
+            (goto-char (point-max)))
+          (delete-region (line-beginning-position) (line-end-position))
+          (insert ".")
+          (forward-line 1))))))
 
 (defun nnimap-unfold-quoted-lines ()
   ;; Unfold quoted {number} strings.