;;; mew-thread.el

;; Author:  Kazu Yamamoto <Kazu@Mew.org>
;; Created: Feb  1, 1999

;;; Code:

(require 'mew)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Customizable variables
;;;

(defvar mew-use-sorted-thread t)

(defcustom mew-use-complete-thread t
  "If non-nil, threads are made using two passes.

First pass -  Repeat the following procedure in numerical order:
	(1.0) Pick one message from the message list.
	(1.1) Register the current message-id: to DB.
	(1.2) Find its parent message-id: in DB.
	(1.3) If found, register the current message as a child of
	      the parent.
	(1.4) Otherwise, register the current message to the top
	      node list.

Here we have pretty good threads.  However, if the messages are not
sorted by Date:, it is possible that some top nodes can be
connected to other threads.  If 'mew-use-complete-thread' is non-nil,
the second pass is carried out.

Second pass - Repeat the following procedure for top nodes linearly:
	(2.0) Pick one message from the top node list.
	(2.1) Find its parent message-id: in DB.
	(2.2) If found, register the current message as a child of
	      the parent.
	(2.3) Otherwise, register the current message to the new top
	      node list.

If you have bogus messages and the second pass is carried out, thread
structure MAY loop. This results in an infinite loop of visualizing 
threads (not making threads). 

Mew does not provide any loop detection/avoidance mechanism. So, you
should understand this risk."
  :group 'mew-summary
  :type 'boolean)

(defcustom mew-thread-indent-strings [" +" " +" " |" "  "]
  "*Vector of strings to be used for indentation of thread.

This consists of four members; 1st member for prefixing to a child
message that is not the last one, 2nd member is for prefixing to the 
last child, 3rd and 4th members are for prefixing to grand-child thread trees, 
4th member is for the child tree of the last child message.

Example1: [\" +\" \" +\" \" |\" \"  \"] makes thread view below.

    Message 1
     +Message 2
     | +Message 3
     +Message 4
       +Message 5

Example2: [\"  \" \"  \" \"  \" \"  \"] makes thread view below.

    Message 1
      Message 2
        Message 3
      Message 4
        Message 5

All members must have the same length."
  :group 'mew-summary
  :type 'sexp)

(defcustom mew-use-thread-cursor nil
  "*If non-nil, move cursor after the indentation of thread."
  :group 'mew-summary
  :type 'boolean)

(defvar mew-use-thread-separator nil
  "*If non-nil, the specified string is inserted between threads.")
(defvar mew-thread-separator "--")

(defsubst mew-thread-insert-separator ()
  (if (and mew-use-thread-separator
	   (/= (save-excursion (beginning-of-line) (point)) 1))
      (insert mew-thread-separator "\n")))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Thread info macro
;;;

(defun mew-thread-make-entry ()
  (make-vector 5 nil))

(defsubst mew-thread-get-myid (entry)
  (aref entry 0))

(defsubst mew-thread-get-prntid (entry)
  (aref entry 1))

(defsubst mew-thread-get-child (entry)
  (aref entry 2))

(defsubst mew-thread-get-msg (entry)
  (aref entry 3))

(defsubst mew-thread-get-line (entry)
  (aref entry 4))

(defsubst mew-thread-set-myid (entry myid)
  (aset entry 0 myid))

(defsubst mew-thread-set-prntid (entry prntid)
  (aset entry 1 prntid))

(defsubst mew-thread-set-child (entry child)
  (aset entry 2 child))

(defsubst mew-thread-set-msg (entry msg)
  (aset entry 3 msg))

(defsubst mew-thread-set-line (entry line)
  (aset entry 4 line))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Thread setup
;;;

(defvar mew-thread-indent-length nil)
(defvar mew-thread-indent-width nil)

(defun mew-thread-setup ()
  (let ((idt1 (aref mew-thread-indent-strings 0))
	(idt2 (aref mew-thread-indent-strings 1))
	(idt3 (aref mew-thread-indent-strings 2))
	(idt4 (aref mew-thread-indent-strings 3)))
    (unless (and (= (string-width idt1) (string-width idt2))
		 (= (string-width idt2) (string-width idt3))
		 (= (string-width idt3) (string-width idt4)))
      (error
       "All members of mew-thread-indent-strings must have the same length"))
    (setq mew-thread-indent-width  (string-width idt1))
    (setq mew-thread-indent-length (length idt1))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Making thread
;;;

(defun mew-summary-mark-make-thread ()
  "Make threads for messages marked with '*'."
  (interactive)
  (mew-summary-thread-region (point-min) (point-max) 'mark))

(defsubst mew-thread-cache-valid-p (vfolder)
  (let ((cfolder (mew-summary-folder-name 'ext))
	ofld)
    (save-excursion
      (when (get-buffer vfolder)
	(set-buffer vfolder)
	(setq ofld (mew-vinfo-get-parent-folder))
	(and (equal ofld cfolder)
	     (get-buffer ofld)
	     (equal (mew-sinfo-get-cache-time)
		    (progn (set-buffer ofld) (mew-sinfo-get-cache-time))))))))

(defun mew-summary-make-thread (&optional arg)
  "If called in Summary mode or in Virtual mode made of a single
physical folder, this command makes threads for the Summary mode
as Virtual mode, then the cursor jump onto the current message in
the Virtual mode. If a corresponding Virtual mode exists, this
command just visits the Virtual mode.

If called with '\\[universal-argument]' in Summary mode, make
threads for messages in the region.

If called in Thread (Virtual) mode, switch back to the
corresponding Summary mode and move to the current message."
  (interactive "P")
  (if (mew-mark-active-p) (setq arg t))
  (if arg
      (let ((begend (mew-summary-get-region)))
	(mew-summary-thread-region (car begend) (cdr begend)))
    (mew-summary-goto-message)
    (mew-decode-syntax-delete)
    (let* ((msg (mew-summary-message-number))
	   (disp (mew-sinfo-get-disp-msg))
	   (folder (mew-summary-folder-name 'ext)) ;; xxx
	   fld vfolder)
      (cond
       ((mew-virtual-p)
	(cond
	 ((mew-thread-p)
	  (setq fld (mew-vinfo-get-parent-folder))
	  (if (not (and fld (get-buffer fld)))
	      (message "No original folder")
	    (mew-summary-visit-folder fld nil 'no-ls)
	    (mew-summary-toggle-disp-msg (if disp 'on 'off))
	    (if (not msg)
		(goto-char (point-max))
	      (mew-summary-move-and-display msg))))
	 ((mew-pickable-p)
	  (mew-summary-thread-region (point-min) (point-max) nil msg))
	 (t
	  (message "No physical folder"))))
       ((and (setq vfolder (mew-folder-to-thread folder))
	     (mew-thread-cache-valid-p vfolder))
	(mew-summary-visit-folder vfolder)
	(mew-summary-toggle-disp-msg (if disp 'on 'off))
	(when msg
	  (mew-summary-move-and-display msg)
	  (mew-thread-move-cursor)))
       (t
	(mew-summary-thread-region (point-min) (point-max) nil msg))))))

(defun mew-summary-regexp-make-thread (&optional args)
  "Make threads for messages matched to a specified regular expression."
  (interactive "P")
  (mew-decode-syntax-delete)
  (let ((regex "") iter)
    (while (string= regex "")
      (setq regex (read-string "Regexp: ")))
    (if args
	(setq iter (lambda () (re-search-forward regex nil t)))
      (setq iter (lambda () (mew-summary-search-regexp-visible regex))))
    (mew-summary-thread-region (point-min) (point-max) 'mark nil iter)))

(defun mew-summary-thread-region (beg end &optional mark disp-msg iter)
  "Make threads for messages in a region.  If you want to know how
threads are created, see 'mew-use-complete-thread'."
  (interactive "r")
  (mew-pickable
   (when (mew-summary-exclusive-p)
     (let* ((ofolder (mew-summary-folder-name 'ext))
	    (vfolder (mew-folder-to-thread (mew-physical-folder ofolder)))
	    (folder (mew-summary-physical-folder))
	    (disp (mew-sinfo-get-disp-msg))
	    (ctime (mew-sinfo-get-cache-time))
	    (case (mew-sinfo-get-case))
	    (column (mew-get-summary-form folder 'column))
	    db size top start me prnt prnt-cld my-id prnt-id func msg line
	    tm1 tm2 tm3 tm4 tm5 tm6)
       (message "Making thread (first pass)...")
       (save-restriction
	 (narrow-to-region beg end)
	 (setq size (count-lines beg end))
	 (cond
	  ((<= size 211)
	   (setq size 211))
	  ((<= size 1511)
	   (setq size 1511))
	  ((<= size 7211)
	   (setq size 7211))
	  (t
	   (setq size 18211)))
	 (setq db (make-vector size 0)) ;; hash
	 (save-excursion
	   (goto-char (point-min))
	   (if mark
	       (setq func (lambda ()
			    (re-search-forward mew-regex-msg-review nil t)))
	     (setq func (lambda () (not (eobp)))))
           (if iter
               (setq func iter))
	   (setq tm1 (current-time))
	   (while (funcall func)
	     (beginning-of-line)
	     (setq start (point))
	     (if (not (mew-sumsyn-match mew-regex-sumsyn-long))
		 (forward-line)
	       (setq msg     (mew-sumsyn-message-number))
	       (setq my-id   (mew-sumsyn-my-id))
	       (setq prnt-id (mew-sumsyn-parent-id))
	       (forward-line)
	       ;; Throw away properties here and give properties later.
	       ;; This is faster than inheriting properties.
	       (setq line (mew-buffer-substring start (point)))
	       (setq me (mew-thread-make-entry))
	       (mew-thread-set-msg me msg)
	       (mew-thread-set-line me line)
	       (if (string= my-id "")
		   (setq top (cons me top))
		 (mew-thread-set-myid me my-id)
		 (set (intern my-id db) me)
		 (if (string= prnt-id "")
		     (setq top (cons me top))
		   (mew-thread-set-prntid me prnt-id)
		   (setq prnt (symbol-value (intern-soft prnt-id db)))
		   (if (null prnt)
		       (setq top (cons me top))
		     (setq prnt-cld (mew-thread-get-child prnt))
		     (if prnt-cld
			 (nconc prnt-cld (list me))
		       (mew-thread-set-child prnt (list me))))))))))
       ;;
       (if (null me)
 	   (message "No target messages")
	 (setq tm2 (current-time))
	 (mew-summary-switch-to-folder vfolder ofolder)
	 (mew-erase-buffer)
	 (mew-hscroll)
	 (mew-summary-toggle-disp-msg (if disp 'on 'off))
	 (mew-sinfo-set-cache-time ctime)
	 (mew-sinfo-set-case case)
	 (setq mew-summary-buffer-raw t)
	 (mew-vinfo-set-top nil)
	 (mew-vinfo-set-db db)
 	 (mew-vinfo-set-column column)
	 ;;
	 (message "Making thread (second pass)...")
	 (setq tm3 (current-time))
	 (if (null mew-use-complete-thread)
	     (mew-vinfo-set-top (nreverse top))
	   ;; This may create looped thread.
	   ;; See mew-use-complete-thread for more information.
	   (while top
	     (setq me (car top))
	     (if (not (and (mew-thread-get-myid me)
			   (setq prnt-id (mew-thread-get-prntid me))))
		 (mew-vinfo-set-top (cons me (mew-vinfo-get-top)))
	       (setq prnt (symbol-value (intern-soft prnt-id db)))
	       (if (null prnt)
		   (mew-vinfo-set-top (cons me (mew-vinfo-get-top)))
		 (setq prnt-cld (mew-thread-get-child prnt))
		 (if prnt-cld
		     (nconc prnt-cld (list me))
		   (mew-thread-set-child prnt (list me)))))
	     (setq top (cdr top))))
	 ;;
	 (setq tm4 (current-time))
	 (message "Displaying thread...")
	 (setq tm5 (current-time))
	 (mew-summary-thread-print-top
	  (mew-vinfo-get-top) folder column)
	 ;;
	 ;; Unmarking in both Summary and Thread
	 (when mark (mew-mark-undo-mark mew-mark-review))
	 (setq tm6 (current-time))
	 (when mew-gemacs-p
	   (jit-lock-register 'mew-summary-cook-region))
	 (mew-summary-set-count-line)
	 (set-buffer-modified-p nil)
	 (if disp-msg
	     (mew-summary-move-and-display disp-msg)
	   (goto-char (point-max)))
	 (mew-thread-move-cursor)
	 (message "Displaying thread...done")
	 (run-hooks 'mew-thread-display-hook)
	 (when (mew-debug 'thread)
	   (let* ((t1 (mew-time-calc tm2 tm1))
		  (t2 (mew-time-calc tm4 tm3))
		  (t3 (mew-time-calc tm6 tm5)))
	     (message "pass1 %f, pass2 %f, visual %f" t1 t2 t3))))))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Subfunctions
;;;

(defun mew-thread-put-property (beg end level)
  (put-text-property beg end 'mew-thread-indent level))

(defun mew-thread-get-property (beg)
  (get-text-property beg 'mew-thread-indent))

(defun mew-thread-previous-property (beg)
  (previous-single-property-change beg 'mew-thread-indent))

(defun mew-thread-next-property (beg)
  (next-single-property-change beg 'mew-thread-indent))

(defun mew-thread-next-property2 (beg end level)
  (text-property-any beg end 'mew-thread-indent level))

(defun mew-thread-adjust-body (level)
  (when (mew-summary-goto-body)
    (mew-elet
     (let ((end (point))
	   (width (* level mew-thread-indent-width))
	   (sum 0))
       (while (< sum width)
	 (setq sum (+ (char-width (char-before)) sum))
	 (forward-char -1))
       (delete-region (point) end)
       (when (/= sum width)
	 (insert (make-string (- sum width) mew-sp)))))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Visualizing thread
;;;

(defun mew-summary-thread-print-top (top folder column)
  (while top
    (let* ((me (car top))
	   (cld (mew-thread-get-child me)))
      (mew-elet
       (mew-thread-insert-separator)
       (insert (mew-thread-get-line me))
       (forward-line -1)
       (move-to-column column)
       (mew-thread-put-property (point) (1+ (point)) 0)
       (forward-line))
      (if cld (mew-summary-thread-print-tree cld folder column))
      (setq top (cdr top)))))

(defun mew-summary-thread-print-tree (tree folder column)
  (let ((tree-stack nil)
	(prefix "")
	(level 1) pos)
    (while tree
      (let* ((me (car tree))
	     (next (cdr tree))
	     (cld (mew-thread-get-child me)))
	(mew-elet
	 (insert (mew-thread-get-line me))
	 (forward-line -1)
	 (move-to-column column)
 	 (setq pos (point))
	 (if next
	     (insert prefix (aref mew-thread-indent-strings 0))
	   (insert prefix (aref mew-thread-indent-strings 1)))
	 (mew-thread-put-property pos (point) level)
	 (mew-thread-adjust-body level)
	 (forward-line))
	;;
	(setq tree next)
	(cond
	 (cld
	  (if next
	      (setq prefix
		    (concat prefix (aref mew-thread-indent-strings 2)))
	    (setq prefix
		  (concat prefix (aref mew-thread-indent-strings 3))))
	  (setq tree-stack (cons tree tree-stack))
	  (setq tree cld)
	  (setq level (1+ level)))
	 (t
	  (while (and (null tree) tree-stack)
	    (setq prefix (substring prefix 0 (- mew-thread-indent-length)))
	    (setq tree (car tree-stack))
	    (setq tree-stack (cdr tree-stack))
	    (setq level (1- level)))))))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Marking thread
;;;

(defun mew-thread-mark-review ()
  "Put the '*' mark on all messages of the current sub-thread."
  (interactive)
  (mew-thread-mark mew-mark-review))

(defun mew-thread-mark-delete ()
  "Put the 'D' mark on all messages of the current sub-thread."
  (interactive)
  (mew-summary-not-in-nntp
   (mew-thread-mark mew-mark-delete 'valid-only)))

(defun mew-thread-mark-unlink ()
  "Put the 'X' mark on all messages of the current sub-thread."
  (interactive)
  (mew-thread-mark mew-mark-unlink 'valid-only))

(defun mew-thread-mark-escape ()
  "Put the '$' mark on all messages of the current sub-thread."
  (interactive)
  (mew-thread-mark mew-mark-escape))

(defun mew-thread-mark-refile ()
  "Put the 'o' mark on all messages of the current sub-thread."
  (interactive)
  (mew-thread-only
   (let* ((fld (mew-physical-folder (mew-summary-folder-name 'ext)))
	  ;; mew-vinfo-get-parent-folder cannot be used here
	  (folders (mew-summary-refile-body nil nil nil 'no-mark))
	  (folders-str (mew-join "," folders))
	  (func (lambda ()
		  (mew-summary-refile-override-body folders-str)
		  (unless (mew-virtual-p)
		    (mew-summary-refile-log fld folders-str))))
	  alist)
     (when folders
       (setq alist (mew-thread-mark mew-mark-refile 'valid-only func))
       (mew-refile-set-from-alist alist folders)))))

(defun mew-thread-mark-copy ()
  "Put the 'o' mark on all messages of the current sub-thread
with the current folder as a candidate in addition to guessed folders."
  (interactive)
  (mew-thread-only
   (let* ((folders (mew-summary-refile-body
		    nil nil nil 'no-mark (mew-summary-folder-name)))
	  (folders-str (mew-join "," folders))
	  (func (lambda () (mew-summary-refile-override-body folders-str)))
	  alist)
     (when folders
       (setq alist (mew-thread-mark mew-mark-refile 'valid-only func))
       (mew-refile-set-from-alist alist folders)))))

(defun mew-refile-set-from-alist (alist folders)
  (let (ent fld msg msgs)
    (while alist
      (setq ent (car alist))
      (setq alist (cdr alist))
      (setq fld (car ent))
      (setq msgs (sort (copy-sequence (cdr ent)) '<)) ;; sort has side effect
      (while msgs
	(setq msg (int-to-string (car msgs)))
	(setq msgs (cdr msgs))
	(when (get-buffer fld)
	  (save-excursion
	    (set-buffer fld)
	    (mew-refile-reset msg)
	    (mew-refile-set msg folders)))))))

(defun mew-thread-mark (mark &optional valid-only func)
  (mew-thread-only
   (mew-summary-msg-or-part
    (let ((regex (if valid-only mew-regex-sumsyn-valid mew-regex-sumsyn-short))
	  (column (mew-vinfo-get-column))
	  indent cindent fld msg alist bottom pruned)
      (mew-summary-goto-message)
      (mew-decode-syntax-delete)
      (save-excursion
	(beginning-of-line)
	(when (looking-at
	       (concat "^." (regexp-quote (char-to-string mew-mark-thread-root))))
	  (setq pruned (point))
	  (mew-thread-graft 'nomsg))
	(move-to-column column)
	(setq indent (mew-thread-get-property (point)))
	(when (mew-sumsyn-match regex)
	  (setq fld (mew-sumsyn-folder-name))
	  (setq msg (mew-sumsyn-message-number))
	  (if func (funcall func))
	  (mew-mark-put mark)
	  (mew-mark-alist-set alist fld msg))
	(forward-line)
	(catch 'loop
	  (while (not (eobp))
	    (move-to-column column)
	    (when (setq cindent	(mew-thread-get-property (point)))
	      (if (>= indent cindent)
		  (throw 'loop nil)
		(when (mew-sumsyn-match regex)
		  (setq fld (mew-sumsyn-folder-name))
		  (setq msg (mew-sumsyn-message-number))
		  (if func (funcall func))
		  (mew-mark-put mark)
		  (mew-mark-alist-set alist fld msg))))
	    (forward-line)))
	(beginning-of-line)
	(setq bottom (point))
	(mew-summary-mark-in-physical-alist alist mark func)
	(when pruned
	  (goto-char pruned)
	  (mew-thread-prune 'nomsg)))
      (mew-push-mark)
      (let ((mew-summary-down-function (lambda () (goto-char bottom))))
	(mew-summary-display-after mew-summary-mark-direction))
      alist))))

(defun mew-thread-undo (fld msg)
  (let* ((mark (mew-summary-get-mark))
	 (func (mew-markdb-func-undo mark)))
    (and func (fboundp func) (funcall func fld msg))))

(defun mew-thread-unmark ()
  "Unmark messages under this sub-thread."
  (interactive)
  (mew-thread-only
   (mew-summary-msg-or-part
    (let ((column (mew-vinfo-get-column))
	  fld msg alist indent cindent pruned)
      (mew-summary-goto-message)
      (mew-thread-move-cursor)
      (mew-decode-syntax-delete)
      (save-excursion
	(beginning-of-line)
	(when (looking-at
	       (concat "^." (regexp-quote (char-to-string mew-mark-thread-root))))
	  (setq pruned (point))
	  (mew-thread-graft 'nomsg))
	(move-to-column column)
 	(setq indent (mew-thread-get-property (point)))
	(setq fld (mew-summary-folder-name))
	(setq msg (mew-summary-message-number))
	(mew-mark-alist-set alist fld msg)
	(mew-thread-undo fld msg)
	(mew-mark-unmark)
	(forward-line)
	(catch 'loop
 	  (while (not (eobp))
	    (move-to-column column)
	    (when (setq cindent (mew-thread-get-property (point)))
	      (if (>= indent cindent)
		  (throw 'loop nil)
		(setq fld (mew-summary-folder-name))
		(setq msg (mew-summary-message-number))
		(mew-mark-alist-set alist fld msg)
		(mew-thread-undo fld msg)
		(mew-mark-unmark)))
	    (forward-line)))
	(when pruned
	  (goto-char pruned)
	  (mew-thread-prune 'nomsg))
	(mew-thread-unmark-physical-from-alist alist))))))

(defun mew-thread-unmark-physical-from-alist (alist)
  (let (ent fld msg msgs)
    (while alist
      (setq ent (car alist))
      (setq alist (cdr alist))
      (setq fld (car ent))
      (setq msgs (sort (copy-sequence (cdr ent)) '<)) ;; sort has side effect
      (when (get-buffer fld)
	(set-buffer fld)
	(save-excursion
	  (goto-char (point-min))
	  (while msgs
	    (setq msg (int-to-string (car msgs)))
	    (setq msgs (cdr msgs))
	    (when (re-search-forward (mew-regex-sumsyn-msg msg) nil t)
	      (mew-thread-undo fld msg)
	      (mew-mark-unmark))))))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Thread utilities
;;;

(defun mew-thread-up ()
  "Move onto the top of the current thread. If the current message is
a top node, move onto the top of the previous thread."
  (interactive)
  (mew-thread-only
   (let (here pos)
     (mew-summary-goto-message)
     (save-excursion
       (mew-decode-syntax-delete)
       (beginning-of-line)
       (setq pos (point))
       (catch 'loop
 	 (while (and (not (bobp))
		     (setq pos (mew-thread-previous-property pos)))
	   (when (and pos (eq (mew-thread-get-property pos) 0))
 	     (throw 'loop (setq here pos))))))
     (if (not here)
	 (message "No more threads")
       (goto-char here)
       (mew-thread-move-cursor)
       (mew-summary-display nil)))))

(defun mew-thread-down ()
  "Move onto the top of the next thread."
  (interactive)
  (mew-thread-only
   (let (here)
     (mew-summary-goto-message)
     (save-excursion
       (mew-decode-syntax-delete)
       (forward-line)
       (setq here (mew-thread-next-property2 (point) (point-max) 0)))
     (if (not here)
	 (message "No more threads")
       (goto-char here)
       (unless (mew-summary-message-number) (forward-line))
       (mew-thread-move-cursor)
       (mew-summary-display nil)))))

(defun mew-summary-thread-parent ()
  "Move onto the parent message of the current message."
  (interactive)
  (mew-summary-goto-message)
  (mew-decode-syntax-delete)
  (let ((pos (point))
	(par-id (mew-summary-parent-id))
	key)
    (if (or (null par-id) (string= par-id ""))
	(message "No required info")
      (setq key (mew-regex-sumsyn-my-id par-id))
      (if (or (re-search-backward key nil t)
	      (progn
		(goto-char (point-max))
		(re-search-backward key nil t)))
	  (progn
	    (mew-thread-move-cursor)
	    (mew-summary-display nil)
	    (message "Parent found"))
	(goto-char pos)
	(message "No parent")))))

(defun mew-summary-thread-child ()
  "Move onto the child message of the current message."
  (interactive)
  (mew-summary-goto-message)
  (mew-decode-syntax-delete)
  (let ((pos (point))
	(my-id (mew-summary-my-id))
	key)
    (if (or (null my-id) (string= my-id ""))
	(message "No required info")
      (setq key (mew-regex-sumsyn-par-id my-id))
      (if (or (re-search-forward key nil t)
	      (progn
		(goto-char (point-min))
		(re-search-forward key nil t)))
	  (progn
	    (mew-thread-move-cursor)
	    (mew-summary-display nil)
	    (message "Child found"))
	(goto-char pos)
	(message "No child")))))

(defun mew-summary-thread-sibling-up ()
  "Search backward by one sibling message of the current message."
  (interactive)
  (let ((pos (point)) 
	(par-id (mew-summary-parent-id))
	key)
    (if (or (null par-id) (string= par-id ""))
	(message "No required info")
      (setq key (mew-regex-sumsyn-par-id par-id))
      (if (re-search-backward key nil t)
	  (progn
	    (mew-thread-move-cursor)
	    (mew-summary-display nil)
	    (message "Sibling found"))
	(goto-char pos)
	(message "No sibling")))))

(defun mew-summary-thread-sibling-down ()
  "Search forward by one sibling message of the current message."
  (interactive)
  (let ((pos (point))
	(par-id (mew-summary-parent-id))
	key)
    (if (or (null par-id) (string= par-id ""))
	(message "No required info")
      (setq key (mew-regex-sumsyn-par-id par-id))
      (forward-line)
      (if (re-search-forward key nil t)
	  (progn
	    (mew-thread-move-cursor)
	    (mew-summary-display nil)
	    (message "Sibling found"))
	(goto-char pos)
	(message "No sibling")))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Thread sub-functions
;;;

(defun mew-thread-move-cursor ()
  "Move cursor after indentation of thread."
  (if (and mew-use-thread-cursor
	   (mew-thread-p)
	   (mew-summary-message-number))
      (let (indent)
 	(move-to-column (mew-vinfo-get-column))
 	(if (setq indent (mew-thread-get-property (point)))
 	    (unless (= indent 0)
 	      (goto-char (mew-thread-next-property (point))))
 	  (beginning-of-line)))
    (beginning-of-line)))

(defun mew-summary-thread-get-msglst (tree &optional add-separator)
  "Get a list of message in the thread order specified by TREE."
  (let ((tree-stack nil) (level 0) msgs me cld)
    (while tree
      (setq me (car tree))
      (setq cld (mew-thread-get-child me))
      (if (and mew-use-thread-separator add-separator (= level 0))
	  (setq msgs (cons "s" msgs))) ;; "s" thread-separator line
      (setq msgs (cons (mew-thread-get-msg me) msgs))
      (setq tree (cdr tree))
      (if (null cld)
	  (while (and (null tree) tree-stack)
	    (setq tree (car tree-stack))
	    (setq tree-stack (cdr tree-stack))
	    (setq level (1- level)))
	(setq tree-stack (cons tree tree-stack))
	(setq tree cld)
	(setq level (1+ level))))
    (if (and mew-use-thread-separator add-separator)
	;; discard first "s"
	(cdr (nreverse msgs))
      (nreverse msgs))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Hide/disclose children
;;;

(defvar mew-mark-thread-root ?+)

(defun mew-thread-toggle ()
  "Hide children and leave the parent only. If its children are hidden,
\"+\" is displayed on the parent. Also, if this command is executed on a
mail whose children are hidden, they appear."
  (interactive)
  (mew-thread-only
   (mew-summary-goto-message)
   (mew-decode-syntax-delete)
   (if (looking-at (concat "^." (regexp-quote (char-to-string mew-mark-thread-root))))
       (mew-thread-graft)
     (mew-thread-prune))
   (mew-thread-move-cursor)
   (set-buffer-modified-p nil)))

(defun mew-thread-all-prune ()
  "Hide all children."
  (interactive)
  (mew-thread-only
   (mew-summary-goto-message)
   (mew-decode-syntax-delete)
   (save-excursion
     (goto-char (point-min))
     (let (pos)
       (while (setq pos (mew-thread-next-property2 (point) (point-max) 0))
	 (goto-char pos)
	 (mew-thread-prune 'nomsg)
	 (forward-line))))
   (when (eq (get-text-property (point) 'invisible) t)
     (mew-re-search-backward-visible mew-regex-msg-or-part))
   (mew-thread-move-cursor)
   (set-buffer-modified-p nil)))

(defun mew-thread-all-graft ()
  "Display all children."
  (interactive)
  (mew-thread-only
   (mew-summary-goto-message)
   (mew-decode-syntax-delete)
   (save-excursion
     (goto-char (point-min))
     (let ((regex (concat "^." (regexp-quote (char-to-string mew-mark-thread-root)))))
       (while (re-search-forward regex nil t)
	 (mew-thread-graft 'nomsg)
	 (forward-line))))
   (mew-thread-move-cursor)
   (set-buffer-modified-p nil)))

(defun mew-thread-prune (&optional nomsg)
  (beginning-of-line)
  (let ((pos (mew-thread-next-property (point))))
    (unless (and pos (eq (mew-thread-get-property pos) 0)) ;; root
      (catch 'loop
	(while (setq pos (mew-thread-previous-property pos))
	  (when (and pos (eq (mew-thread-get-property pos) 0))
	    (throw 'loop (goto-char pos)))))))
  (beginning-of-line)
  (save-excursion
    (forward-line)
    (let ((beg (point))
	  (next (mew-thread-next-property2 (point) (point-max) 0)))
      (goto-char (or next (point-max)))
      (forward-line -1)
      (if (mew-summary-message-number) (forward-line))
      (if (= beg (point))
	  (or nomsg (message "No children to be pruned"))
	(mew-elet
	 (put-text-property beg (point) 'invisible t)
	 (goto-char beg)
	 (forward-line -1)
	 (forward-char)
	 (put-text-property (point) (1+ (point)) 'invisible t)
	 (insert mew-mark-thread-root))))))

(defun mew-thread-graft (&optional nomsg)
  (save-excursion
    (forward-line)
    (let ((start (point))
	  (next (mew-thread-next-property2 (point) (point-max) 0))
	  beg end)
      (goto-char (or next (point-max)))
      (forward-line -1)
      (if (mew-summary-message-number) (forward-line))
      (setq end (point))
      (if (= start end)
	  (or nomsg (message "No children to be leaned"))
	(mew-elet
	 (goto-char start)
	 (setq beg start)
	 (while (search-forward "\r" end t)
	   (put-text-property beg (1- (point)) 'invisible nil)
	   (forward-line)
	   (put-text-property (1- (point)) (point) 'invisible nil)
	   (setq beg (point)))
	 (goto-char start)
	 (forward-line -1)
	 (forward-char)
	 (delete-char 1)
	 (put-text-property (point) (1+ (point)) 'invisible nil))))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Thread editing
;;;

(defun mew-thread-glue ()
  "Glue a thread/message to the current message as its child(ren).
The thread/message is specified with the mark(\\[set-mark-command])."
  (interactive)
  (mew-summary-msg
   (let* ((zmacs-regions nil)
	  (pos (marker-position (mark-marker))))
     (cond
      ((null pos)
       (message "No marker"))
      ((mew-thread-p)
       (mew-thread-glue-it))
      (t ;; summary or virtual
       (mew-summary-glue-it))))))

(defun mew-summary-glue-it ()
  (save-excursion
    (mew-summary-goto-message)
    (beginning-of-line)
    ;; parent    
    (let ((id (mew-summary-my-id))
	  fld msg)
      (goto-char (marker-position (mark-marker)));; user's mark
      ;; children
      (when (mew-sumsyn-match mew-regex-sumsyn-long)
	(setq fld (mew-sumsyn-folder-name))
	(setq msg (mew-sumsyn-message-number)))
      (when (and fld msg id)
	(mew-thread-change-parent-id id)
	(mew-thread-save-xref fld msg id)
	(if (mew-virtual-p)
	    (mew-summary-change-parent-id fld msg id))
	(message "Glued")))))

(defun mew-thread-glue-it ()
  (let ((column (mew-vinfo-get-column))
	(m (make-marker))
	(width 0) (wd 0) (adjust 0)
	(prefix "")
	fld msg id beg end tree indent idt pbeg pindent has-child)
    (save-excursion
      ;; parent
      (mew-summary-goto-message)
      (beginning-of-line)
      (setq id (mew-summary-my-id))
      (move-to-column column)
      (setq beg (point))
      (setq pindent (mew-thread-get-property (point)))
      (goto-char (mew-thread-next-property (point)))
      (setq end (point))
      (unless (= pindent 0)
	(while (< width mew-thread-indent-width)
	  (forward-char -1)
	  (setq width (+ width (char-width (char-after)))))
	(setq prefix (mew-buffer-substring beg (point)))
	(if (string= (mew-buffer-substring (point) end)
		     (aref mew-thread-indent-strings 0))
	    (setq prefix (concat prefix (aref mew-thread-indent-strings 2)))
	  (setq prefix (concat prefix (aref mew-thread-indent-strings 3)))))
      (when (mew-summary-goto-body)
	(while (> (point) beg)
	  (setq wd (+ wd (char-width (char-before))))
	  (forward-char -1)))
      (setq wd (/ wd mew-thread-indent-width))
      (if (> pindent wd) (setq adjust (- pindent wd)))
      (setq pindent (1+ pindent))
      (forward-line)
      ;; the next line of parent
      (set-marker m (point))
      (unless (looking-at mew-regex-thread-separator)
	(move-to-column column)
	(if (and (mew-thread-get-property (point))
		 (= (mew-thread-get-property (point)) pindent))
	    (setq has-child t)))
      (move-to-column column)
      ;; children
      (goto-char (marker-position (mark-marker)));; user's mark
      (when (mew-sumsyn-match mew-regex-sumsyn-long)
	(setq fld (mew-sumsyn-folder-name))
	(setq msg (mew-sumsyn-message-number)))
      (when (and fld msg id)
	(mew-elet
	 (mew-syntax-change-parent-id id)
	 (beginning-of-line)
	 (setq beg (point))
	 (move-to-column column)
	 (setq pbeg (point))
	 (setq indent (mew-thread-get-property (point)))
	 (insert prefix)
	 (if has-child
	     (insert (aref mew-thread-indent-strings 0))
	   (insert (aref mew-thread-indent-strings 1)))
	 (goto-char (mew-thread-next-property (point)))
	 (mew-thread-put-property pbeg (point) (+ indent pindent))
	 (mew-thread-adjust-body (- pindent adjust))
	 (catch 'loop
	   (while t
	     (forward-line)
	     (move-to-column column)
	     (setq pbeg (point))
	     (setq idt (mew-thread-get-property (point)))
	     (if (or (null idt) (<= idt indent))
		 (throw 'loop nil))
	     (insert prefix)
	     (if has-child
		 (insert (aref mew-thread-indent-strings 2))
	       (insert (aref mew-thread-indent-strings 3)))
	     (goto-char (mew-thread-next-property (point)))
	     (mew-thread-put-property pbeg (point) (+ idt pindent))
	     (mew-thread-adjust-body (- pindent adjust))))
	 (beginning-of-line)
	 (setq end (point))
	 (when (looking-at mew-regex-thread-separator)
	   (forward-line)
	   (delete-region end (point)))
	 ;; This must be "buffer-substring".
	 (setq tree (buffer-substring beg end))
	 (delete-region beg end)
	 ;; the next line of parent
	 (goto-char (marker-position m))
	 (insert tree)
	 (set-buffer-modified-p nil))
	(mew-summary-change-parent-id fld msg id)
	(mew-thread-save-xref fld msg id)))))

(defun mew-summary-change-parent-id (fld msg id)
  (set-buffer fld)
  (save-excursion
    (goto-char (point-min))
    (when (re-search-forward (mew-regex-sumsyn-msg msg) nil t)
      (mew-thread-change-parent-id id))))

(defun mew-thread-change-parent-id (id)
  (mew-elet
   (mew-syntax-change-parent-id id))
  (unless (mew-virtual-p)
    (mew-summary-folder-cache-save))
  (set-buffer-modified-p nil))

(defun mew-thread-save-xref (fld msg id)
  (with-temp-buffer
    (let ((file (mew-expand-folder fld msg)))
      (mew-insert-file-contents2 file)
      (mew-header-delete-lines (list mew-x-mew-ref:))
      (goto-char (point-min))
      (mew-header-insert mew-x-mew-ref: id)
      (write-region (point-min) (point-max) file nil 'no-msg))))

(provide 'mew-thread)

;;; Copyright Notice:

;; Copyright (C) 2000-2005 Mew developing team.
;; All rights reserved.

;; Redistribution and use in source and binary forms, with or without
;; modification, are permitted provided that the following conditions
;; are met:
;; 
;; 1. Redistributions of source code must retain the above copyright
;;    notice, this list of conditions and the following disclaimer.
;; 2. Redistributions in binary form must reproduce the above copyright
;;    notice, this list of conditions and the following disclaimer in the
;;    documentation and/or other materials provided with the distribution.
;; 3. Neither the name of the team nor the names of its contributors
;;    may be used to endorse or promote products derived from this software
;;    without specific prior written permission.
;; 
;; THIS SOFTWARE IS PROVIDED BY THE TEAM AND CONTRIBUTORS ``AS IS'' AND
;; ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
;; IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
;; PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE TEAM OR CONTRIBUTORS BE
;; LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
;; CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
;; SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
;; BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
;; OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
;; IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

;;; mew-thread.el ends here
