Make the `C-c C-n' command in Message more sturdy
authorLars Ingebrigtsen <larsi@gnus.org>
Wed, 5 Sep 2012 17:47:27 +0000 (19:47 +0200)
committerLars Ingebrigtsen <larsi@gnus.org>
Wed, 5 Sep 2012 17:47:27 +0000 (19:47 +0200)
* message.el (message-insert-newsgroups): Don't insert newsgroup
duplicates (bug#12275).

lisp/ChangeLog
lisp/message.el

index 5379383..b5b7063 100644 (file)
@@ -1,3 +1,8 @@
+2012-09-05  Lars Ingebrigtsen  <larsi@gnus.org>
+
+       * message.el (message-insert-newsgroups): Don't insert newsgroup
+       duplicates (bug#12275).
+
 2012-09-05  John Wiegley  <johnw@newartisans.com>
 
        * gnus.el (gnus-expand-group-parameters): Allow regexp substitutions in
index 1fea5f9..12002b6 100644 (file)
@@ -3336,11 +3336,33 @@ or in the synonym headers, defined by `message-header-synonyms'."
 (defun message-insert-newsgroups ()
   "Insert the Newsgroups header from the article being replied to."
   (interactive)
-  (when (and (message-position-on-field "Newsgroups")
-            (mail-fetch-field "newsgroups")
-            (not (string-match "\\` *\\'" (mail-fetch-field "newsgroups"))))
-    (insert ","))
-  (insert (or (message-fetch-reply-field "newsgroups") "")))
+  (let ((old-newsgroups (mail-fetch-field "newsgroups"))
+       (new-newsgroups (message-fetch-reply-field "newsgroups"))
+       (first t)
+       insert-newsgroups)
+    (message-position-on-field "Newsgroups")
+    (cond
+     ((not new-newsgroups)
+      (error "No Newsgroups to insert"))
+     ((not old-newsgroups)
+      (insert new-newsgroups))
+     (t
+      (setq new-newsgroups (split-string new-newsgroups "[, ]+")
+           old-newsgroups (split-string old-newsgroups "[, ]+"))
+      (dolist (group new-newsgroups)
+       (unless (member group old-newsgroups)
+         (push group insert-newsgroups)))
+      (if (null insert-newsgroups)
+         (error "Newgroup%s already in the header"
+                (if (> (length new-newsgroups) 1)
+                    "s" ""))
+       (when old-newsgroups
+         (setq first nil))
+       (dolist (group insert-newsgroups)
+         (unless first
+           (insert ","))
+         (setq first nil)
+         (insert group)))))))
 
 \f