Improve the citation tidying by being more aggressive about the
changes made:
- collapse all citation leaders to the canonical form (stripping out
  intervening spaces, for example),
- ensure that a blank line exists before and after every block of
  cited text.
---
 emacs/notmuch-wash.el | 42 ++++++++++++++++++++++++++++++++++--------
 1 file changed, 34 insertions(+), 8 deletions(-)

diff --git a/emacs/notmuch-wash.el b/emacs/notmuch-wash.el
index 5f8b9267..c449163f 100644
--- a/emacs/notmuch-wash.el
+++ b/emacs/notmuch-wash.el
@@ -300,29 +300,55 @@ that PREFIX should not include a newline."
 
 Perform several transformations on the message body:
 
+- Remove any leading space from lines with citation leaders,
+- Collapse citation leaders to their minimal form (repeated >
+  followed by a space),
 - Remove lines of repeated citation leaders with no other
   content,
-- Remove citation leaders standing alone before a block of cited
-  text,
-- Remove citation trailers standing alone after a block of cited
-  text."
+- Remove citation leaders standing alone before and after a block
+  of cited text,
+- Ensure that a blank line separates a block of cited text from
+  non-cited text."
+
+  ;; Remove any leading spaces on citation lines.
+  (goto-char (point-min))
+  (while (re-search-forward "^ +>" nil t)
+    (replace-match ">"))
+
+  ;; Collapse all citation leaders to the minimal form.
+  (goto-char (point-min))
+  (while (re-search-forward "^>[> ]*>" nil t)
+    (replace-match (remove ?  (match-string 0))))
 
   ;; Remove lines of repeated citation leaders with no other content.
   (goto-char (point-min))
-  (while (re-search-forward "\\(^>[> ]*\n\\)\\{2,\\}" nil t)
+  (while (re-search-forward "\\(^>+ ?\n\\)\\{2,\\}" nil t)
     (replace-match "\\1"))
 
   ;; Remove citation leaders standing alone before a block of cited
   ;; text.
   (goto-char (point-min))
-  (while (re-search-forward "\\(\n\\|^[^>].*\\)\n\\(^>[> ]*\n\\)" nil t)
+  (while (re-search-forward "\\(\n\\|^[^>\n].*\\)\n\\(^>+ ?\n\\)" nil t)
     (replace-match "\\1\n"))
 
   ;; Remove citation trailers standing alone after a block of cited
   ;; text.
   (goto-char (point-min))
-  (while (re-search-forward "\\(^>[> ]*\n\\)\\(^$\\|^[^>].*\\)" nil t)
-    (replace-match "\\2")))
+  (while (re-search-forward "\\(^>+ ?\n\\)\\(^$\\|^[^>].*\\)" nil t)
+    (replace-match "\\2"))
+
+  ;; Ensure that a blank line separates a citation and any following
+  ;; text.
+  (goto-char (point-min))
+  (while (re-search-forward "\\(^>+ .*?\n\\)\\([^>\n]\\)" nil t)
+    (replace-match "\\1\n\\2"))
+
+  ;; Ensure that a blank line separates any text and a following
+  ;; citation.
+  (goto-char (point-min))
+  (while (re-search-forward "\\(^[^>\n].+?\n\\)>" nil t)
+    (replace-match "\\1\n>" ""))
+  )
 
 ;;
 
-- 
2.11.0

_______________________________________________
notmuch mailing list
notmuch@notmuchmail.org
https://notmuchmail.org/mailman/listinfo/notmuch

Reply via email to