;; -*- emacs-lisp -*-
;;; gnus-mst-manipulate-threads.el --- reconstruct broken threads

;; Description: This code provides a set of functions which allow threads to be
;; broken and reconstructed

;; Author: Mark Triggs <mst@dishevelled.net>
;; Keywords: news
;; $Id: gnus-mst-manipulate-threads.el,v 1.14 2003/04/16 07:06:07 mrbump Exp $

;; This file 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 2, or (at your option)
;; any later version.

;; This file 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 GNU Emacs; see the file COPYING.  If not, write to
;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.

;;; Code:

(defun gnus-mst-summary-break-thread ()
  "Remove In-Reply-To and References headers from the current article"
  (interactive)
  (save-window-excursion
    (gnus-summary-show-article)
    (gnus-summary-edit-article)
    (switch-to-buffer "*Article*")
    (goto-char (point-min))
    (delete-matching-lines "^\\(References\\|In-Reply-To\\):.*"
                           (point-min) (search-forward-regexp "^$"))
    (gnus-article-edit-done)))


(defun gnus-mst-summary-make-child ()
  "Make the current article the child of a marked article"
  (interactive)
  (let (parent-article parent-headers parent-id child-id)
    (setq parent-article (gnus-summary-work-articles nil))

    (when (and (boundp 'parent-article) (= (length parent-article) 1))
      (setq parent-headers
            (gnus-summary-article-header (car parent-article)))
      (setq parent-id (mail-header-message-id parent-headers))

      (gnus-mst-summary-break-thread) ;; Remove any exisiting stuff

      (save-window-excursion
        (gnus-summary-show-article)
        (setq child-id (mail-header-message-id gnus-current-headers))

        (when (not (string-match child-id parent-id))
          (gnus-summary-edit-article)
          (switch-to-buffer "*Article*")
          (goto-char (point-min))
          (goto-char (search-forward-regexp "^From"))
          (beginning-of-line)
          (newline)
          (previous-line 1)
          (insert (concat "References: " parent-id))
          (gnus-article-edit-done)))

      (gnus-summary-remove-process-mark (car parent-article))
      (gnus-summary-rescan-group nil))))

(provide 'gnus-mst-manipulate-threads)
;;; gnus-mst-manipulate-threads.el ends here
