Update.
[elisp/wanderlust.git] / wl / wl-thread.el
1 ;;; wl-thread.el -- Thread display modules for Wanderlust.
2
3 ;; Copyright (C) 1998,1999,2000 Yuuichi Teranishi <teranisi@gohome.org>
4 ;; Copyright (C) 1998,1999,2000 Masahiro MURATA  <muse@ba2.so-net.ne.jp>
5
6 ;; Author: Yuuichi Teranishi <teranisi@gohome.org>
7 ;;      Masahiro MURATA  <muse@ba2.so-net.ne.jp>
8 ;; Keywords: mail, net news
9
10 ;; This file is part of Wanderlust (Yet Another Message Interface on Emacsen).
11
12 ;; This program is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
16 ;;
17 ;; This program is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 ;; GNU General Public License for more details.
21 ;;
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
26 ;;
27
28 ;;; Commentary:
29 ;; 
30
31 ;;; Code:
32 ;; 
33
34 (require 'wl-summary)
35 (require 'wl-highlight)
36 (eval-when-compile (require 'cl))
37
38 ;; buffer local variables.
39 ;;(defvar wl-thread-top-entity '(nil t nil nil)) ; top entity
40 (defvar wl-thread-tops nil)           ; top number list (number)
41 (defvar wl-thread-entities nil)
42 (defvar wl-thread-entity-list nil)    ; entity list
43 (defvar wl-thread-entity-hashtb nil)  ; obarray
44 (defvar wl-thread-indent-regexp nil)
45
46 (make-variable-buffer-local 'wl-thread-entity-hashtb)
47 (make-variable-buffer-local 'wl-thread-entities)     ; ".wl-thread-entity"
48 (make-variable-buffer-local 'wl-thread-entity-list)  ; ".wl-thread-entity-list"
49 (make-variable-buffer-local 'wl-thread-entity-cur)
50 (make-variable-buffer-local 'wl-thread-indent-regexp)
51
52 ;;; global flag
53 (defvar wl-thread-insert-force-opened nil)
54
55 ;;;;;; each entity is (number opened-or-not children parent) ;;;;;;;
56
57 (defun wl-thread-resume-entity (fld)
58   (let (entities top-list)
59     (setq entities (wl-summary-load-file-object
60                     (expand-file-name wl-thread-entity-file
61                                       (elmo-msgdb-expand-path fld))))
62     (setq top-list
63           (wl-summary-load-file-object
64            (expand-file-name wl-thread-entity-list-file
65                              (elmo-msgdb-expand-path fld))))
66     (message "Resuming thread structure...")
67     ;; set obarray value.
68     (setq wl-thread-entity-hashtb (elmo-make-hash (* (length entities) 2)))
69     ;; set buffer local variables.
70     (setq wl-thread-entities entities)
71     (setq wl-thread-entity-list top-list)
72     (while entities
73       (elmo-set-hash-val (format "#%d" (car (car entities))) (car entities)
74                          wl-thread-entity-hashtb)
75       (setq entities (cdr entities)))
76     (wl-thread-make-number-list)
77     (message "Resuming thread structure...done")))
78
79 (defun wl-thread-make-number-list ()
80   "Make `wl-summary-buffer-number-list', a list of message numbers."
81   (let* ((node (wl-thread-get-entity (car wl-thread-entity-list)))
82          (children (wl-thread-entity-get-children node))
83          parent sibling)
84     (setq wl-summary-buffer-number-list (list (car wl-thread-entity-list)))
85     (while children
86       (wl-thread-entity-make-number-list-from-children
87        (wl-thread-get-entity (car children)))
88       (setq children (cdr children)))
89     (while node
90       (setq parent (wl-thread-entity-get-parent-entity node)
91             sibling (wl-thread-entity-get-younger-brothers
92                      node parent))
93       (while sibling
94         (wl-thread-entity-make-number-list-from-children
95          (wl-thread-get-entity (car sibling)))
96         (setq sibling (cdr sibling)))
97       (setq node parent))
98     (setq wl-summary-buffer-number-list (nreverse
99                                          wl-summary-buffer-number-list))))
100
101 (defun wl-thread-entity-make-number-list-from-children (entity)
102   (let ((msgs (list (car entity)))
103         msgs-stack children)
104     (while msgs
105       (setq wl-summary-buffer-number-list (cons (car entity)
106                                         wl-summary-buffer-number-list))
107       (setq msgs (cdr msgs))
108       (setq children (wl-thread-entity-get-children entity))
109       (if children
110           (progn
111             (wl-push msgs msgs-stack)
112             (setq msgs children))
113         (unless msgs
114           (while (and (null msgs) msgs-stack)
115             (setq msgs (wl-pop msgs-stack)))))
116       (setq entity (wl-thread-get-entity (car msgs))))))
117
118 (defun wl-thread-save-entity (dir)
119   (wl-thread-save-entities dir)
120   (wl-thread-save-top-list dir))
121
122 (defun wl-thread-save-top-list (dir)
123   (let ((top-file (expand-file-name wl-thread-entity-list-file dir))
124         (entity wl-thread-entity-list)
125         (tmp-buffer (get-buffer-create " *wl-thread-save-top-list*")))
126     (save-excursion
127       (set-buffer tmp-buffer)
128       (erase-buffer)
129       (when (file-writable-p top-file)
130         (prin1 entity tmp-buffer)
131         (princ "\n" tmp-buffer)
132         (write-region (point-min) (point-max) top-file nil 'no-msg)
133         (kill-buffer tmp-buffer)))))
134
135 (defun wl-thread-save-entities (dir)
136   (let ((top-file (expand-file-name wl-thread-entity-file dir))
137         (entities wl-thread-entities)
138         (tmp-buffer (get-buffer-create " *wl-thread-save-entities*")))
139     (save-excursion
140       (set-buffer tmp-buffer)
141       (erase-buffer)
142       (when (file-writable-p top-file)
143         (prin1 entities tmp-buffer)
144         (princ "\n" tmp-buffer)
145         (write-region (point-min) (point-max) top-file nil 'no-msg)
146         (kill-buffer tmp-buffer)))))
147
148 (defsubst wl-thread-entity-get-number (entity)
149   (nth 0 entity))
150 (defsubst wl-thread-entity-get-opened (entity)
151   (nth 1 entity))
152 (defsubst wl-thread-entity-get-children (entity)
153   (nth 2 entity))
154 (defsubst wl-thread-entity-get-parent (entity)
155   (nth 3 entity))
156 (defsubst wl-thread-entity-get-linked (entity)
157   (nth 4 entity))
158
159 (defsubst wl-thread-create-entity (num parent &optional opened linked)
160   (list num (or opened wl-thread-insert-opened) nil parent linked))
161
162 (defsubst wl-thread-get-entity (num)
163   (and num
164        (elmo-get-hash-val (format "#%d" num) wl-thread-entity-hashtb)))
165
166 (defsubst wl-thread-entity-set-parent (entity parent)
167   (setcar (cdddr entity) parent)
168   entity)
169
170 (defsubst wl-thread-entity-set-children (entity children)
171   (setcar (cddr entity) children))
172
173 (defsubst wl-thread-entity-set-linked (entity linked)
174   (if (cddddr entity)
175       (setcar (cddddr entity) linked)
176     (nconc entity (list linked)))
177   entity)
178
179 (defsubst wl-thread-reparent-children (children parent)
180   (while children
181     (wl-thread-entity-set-parent
182      (wl-thread-get-entity (car children)) parent)
183     (wl-thread-entity-set-linked
184      (wl-thread-get-entity (car children)) t)
185     (setq children (cdr children))))
186
187 (defsubst wl-thread-entity-insert-as-top (entity)
188   (when (and entity
189              (car entity))
190     (wl-append wl-thread-entity-list (list (car entity)))
191     (setq wl-thread-entities (cons entity wl-thread-entities))
192     (setq wl-summary-buffer-number-list
193           (nconc wl-summary-buffer-number-list (list (car entity))))
194     (elmo-set-hash-val (format "#%d" (car entity)) entity
195                        wl-thread-entity-hashtb)))
196
197 (defsubst wl-thread-entity-insert-as-children (to entity)
198   (let ((children (wl-thread-entity-get-children to))
199         curp curc)
200     (setq curp to)
201     (elmo-list-insert wl-summary-buffer-number-list
202                       (wl-thread-entity-get-number entity)
203                       (progn
204                         (while (setq curc 
205                                      (wl-thread-entity-get-children curp))
206                           (setq curp (wl-thread-get-entity 
207                                       (nth (- (length curc) 1) 
208                                            curc))))
209                         (wl-thread-entity-get-number curp)))
210     (setcar (cddr to) (wl-append children
211                                  (list (car entity))))
212     (setq wl-thread-entities (cons entity wl-thread-entities))
213     (elmo-set-hash-val (format "#%d" (car entity)) entity
214                        wl-thread-entity-hashtb)))
215
216 (defsubst wl-thread-entity-set-opened (entity opened)
217   (setcar (cdr entity) opened))
218
219 (defsubst wl-thread-entity-get-children-num (entity)
220   (let (children
221         ret-val msgs-stack
222         (msgs (list (car entity))))
223    (while msgs
224      (setq msgs (cdr msgs))
225      (setq children (wl-thread-entity-get-children entity))
226      (if (null children)
227          (while (and (null msgs) msgs-stack)
228            (setq msgs (wl-pop msgs-stack)))
229        (setq ret-val (+ (or ret-val 0) (length children)))
230        (wl-push msgs msgs-stack)
231        (setq msgs children))
232      (setq entity (wl-thread-get-entity (car msgs))))
233    ret-val))
234
235 (defsubst wl-thread-entity-get-descendant (entity)
236   (let (children
237         ret-val msgs-stack
238         (msgs (list (car entity))))
239    (while msgs
240      (setq msgs (cdr msgs))
241      (setq children (wl-thread-entity-get-children entity))
242      (if (null children)
243          (while (and (null msgs) msgs-stack)
244            (setq msgs (wl-pop msgs-stack)))
245        (setq ret-val (append ret-val (copy-sequence children)))
246        (wl-push msgs msgs-stack)
247        (setq msgs children))
248      (setq entity (wl-thread-get-entity (car msgs))))
249    ret-val))
250
251 (defsubst wl-thread-entity-get-parent-entity (entity)
252   (wl-thread-get-entity (wl-thread-entity-get-parent entity)))
253
254 (defun wl-thread-entity-get-top-entity (entity)
255   (let ((cur-entity entity)
256         p-num)
257     (while (setq p-num (wl-thread-entity-get-parent cur-entity))
258       (setq cur-entity (wl-thread-get-entity p-num)))
259     cur-entity))
260
261 (defun wl-thread-entity-parent-invisible-p (entity)
262   "If parent of ENTITY is invisible, the top invisible ancestor entity of
263 ENTITY is returned."
264   (let ((cur-entity entity)
265         ret-val)
266     (catch 'done
267       (while (setq cur-entity (wl-thread-entity-get-parent-entity
268                                cur-entity))
269         (if (null (wl-thread-entity-get-number cur-entity))
270             ;; top!!
271             (progn
272               ;;(setq ret-val nil)
273               (throw 'done nil))
274           (when (not (wl-thread-entity-get-opened cur-entity))
275             ;; not opened!!
276             (setq ret-val cur-entity)))))
277     ;; top of closed entity in the path.
278     ret-val))
279
280 (defun wl-thread-entity-get-nearly-older-brother (entity &optional parent)
281   (let ((brothers (wl-thread-entity-get-older-brothers entity parent)))
282     (when brothers
283       (car (last brothers)))))
284
285 (defun wl-thread-entity-get-older-brothers (entity &optional parent)
286   (let* ((parent (or parent
287                      (wl-thread-entity-get-parent-entity entity)))
288          (brothers (wl-thread-entity-get-children parent))
289          ret-val)
290     (if parent
291         brothers
292       (setq brothers wl-thread-entity-list))
293     (while (and brothers
294                 (not (eq (wl-thread-entity-get-number entity)
295                          (car brothers))))
296       (wl-append ret-val (list (car brothers)))
297       (setq brothers (cdr brothers)))
298     ret-val))
299
300 (defun wl-thread-entity-get-younger-brothers (entity &optional parent)
301   (let* ((parent (or parent
302                      (wl-thread-entity-get-parent-entity entity)))
303          (brothers (wl-thread-entity-get-children parent)))
304     (if parent
305         (cdr (memq (wl-thread-entity-get-number entity)
306                    brothers))
307       ;; top!!
308       (cdr (memq (car entity) wl-thread-entity-list)))))
309
310 (defun wl-thread-jump-to-msg (&optional number)
311   (interactive)
312   (let ((num (or number
313                  (string-to-int
314                   (read-from-minibuffer "Jump to Message(No.): ")))))
315     (wl-thread-entity-force-open (wl-thread-get-entity num))
316     (wl-summary-jump-to-msg num)))
317
318 (defun wl-thread-close-all ()
319   "Close all top threads."
320   (interactive)
321   (message "Closing all threads...")
322   (save-excursion
323     (let ((entities wl-thread-entity-list)
324           (cur 0)
325           (len (length wl-thread-entity-list)))
326       (while entities
327         (when (and (wl-thread-entity-get-opened (wl-thread-get-entity
328                                                  (car entities)))
329                    (wl-thread-entity-get-children (wl-thread-get-entity
330                                                    (car entities))))
331           (wl-summary-jump-to-msg (car entities))
332           (wl-thread-open-close))
333         (when (> len elmo-display-progress-threshold)
334           (setq cur (1+ cur))
335           (if (or (zerop (% cur 5)) (= cur len))
336               (elmo-display-progress
337                'wl-thread-close-all "Closing all threads..."
338                (/ (* cur 100) len))))
339         (setq entities (cdr entities)))))
340   (message "Closing all threads...done"))
341
342 (defun wl-thread-open-all ()
343   "Open all threads."
344   (interactive)
345   (message "Opening all threads...")
346   (save-excursion
347     (goto-char (point-min))
348     (let ((len (count-lines (point-min) (point-max)))
349           (cur 0)
350           entity)
351       (while (not (eobp))
352         (unless (wl-thread-entity-get-opened
353                  (setq entity (wl-thread-get-entity
354                                (wl-summary-message-number))))
355           (wl-thread-entity-force-open entity))
356         (wl-thread-goto-bottom-of-sub-thread)
357         (when (> len elmo-display-progress-threshold)
358           (setq cur (1+ cur))
359           (elmo-display-progress
360            'wl-thread-open-all "Opening all threads..."
361            (/ (* cur 100) len)))))
362     ;; Make sure to be 100%.
363     (elmo-display-progress
364      'wl-thread-open-all "Opening all threads..."
365      100))
366   (message "Opening all threads...done"))
367
368 (defun wl-thread-open-all-unread ()
369   (interactive)
370   (let ((mark-alist (elmo-msgdb-get-mark-alist wl-summary-buffer-msgdb))
371         mark)
372     (while mark-alist
373       (if (setq mark (nth 1 (car mark-alist)))
374           (if (or (string= mark wl-summary-unread-uncached-mark)
375                   (string= mark wl-summary-unread-cached-mark)
376                   (string= mark wl-summary-new-mark)
377                   (string= mark wl-summary-important-mark))
378               (wl-thread-entity-force-open (wl-thread-get-entity
379                                             (nth 0 (car mark-alist))))))
380       (setq mark-alist (cdr mark-alist)))))
381
382 (defsubst wl-thread-maybe-get-children-num (msg)
383   (let ((entity (wl-thread-get-entity msg)))
384     (if (not (wl-thread-entity-get-opened entity))
385         (wl-thread-entity-get-children-num entity))))
386
387 (defsubst wl-thread-update-line-on-buffer-sub (entity msg &optional parent-msg)
388   (let* ((entity (or entity (wl-thread-get-entity msg)))
389          (parent-msg (or parent-msg (wl-thread-entity-get-parent entity)))
390          (overview (elmo-msgdb-get-overview wl-summary-buffer-msgdb))
391          (mark-alist (elmo-msgdb-get-mark-alist wl-summary-buffer-msgdb))
392          (buffer-read-only nil)
393          (inhibit-read-only t)
394          overview-entity temp-mark summary-line invisible-top dest-pair)
395     (if (wl-thread-delete-line-from-buffer msg)
396         (progn
397           (cond
398            ((memq msg wl-summary-buffer-delete-list)
399             (setq temp-mark "D"))
400            ((memq msg wl-summary-buffer-target-mark-list)
401             (setq temp-mark "*"))
402            ((setq dest-pair (assq msg wl-summary-buffer-refile-list))
403             (setq temp-mark "o"))
404            ((setq dest-pair (assq msg wl-summary-buffer-copy-list))
405             (setq temp-mark "O"))
406            (t (setq temp-mark (wl-summary-get-score-mark msg))))
407           (when (setq overview-entity
408                       (elmo-msgdb-overview-get-entity
409                        msg wl-summary-buffer-msgdb))
410             (setq summary-line
411                   (wl-summary-overview-create-summary-line
412                    msg
413                    overview-entity
414                    (elmo-msgdb-overview-get-entity
415                     parent-msg wl-summary-buffer-msgdb)
416                    nil
417                    mark-alist
418                    (if wl-thread-insert-force-opened
419                        nil
420                      (wl-thread-maybe-get-children-num msg))
421                    temp-mark entity))
422             (save-excursion
423               (wl-summary-insert-line summary-line))
424             (if dest-pair
425                 (wl-summary-print-destination (car dest-pair)
426                                               (cdr dest-pair)))))
427       ;; insert thread (moving thread)
428       (if (not (setq invisible-top
429                      (wl-thread-entity-parent-invisible-p entity)))
430           (wl-summary-update-thread
431            (elmo-msgdb-overview-get-entity msg wl-summary-buffer-msgdb)
432            overview
433            mark-alist
434            entity
435            (and parent-msg
436                 (elmo-msgdb-overview-get-entity
437                  parent-msg wl-summary-buffer-msgdb)))
438         ;; currently invisible.. update closed line.
439         (wl-thread-update-children-number invisible-top)))))
440
441 (defun wl-thread-update-line-on-buffer (&optional msg parent-msg updates)
442   (interactive)
443   (let ((msgs (list (or msg (wl-summary-message-number))))
444         entity children msgs-stack)
445    (while msgs
446     (setq msg (wl-pop msgs))
447     (setq updates (and updates (delete msg updates)))
448     (setq entity (wl-thread-get-entity msg))
449     (wl-thread-update-line-on-buffer-sub entity msg parent-msg)
450     ;;
451     (setq children (wl-thread-entity-get-children entity))
452     (if children
453         ;; update children
454         (when (wl-thread-entity-get-opened entity)
455           (wl-push msgs msgs-stack)
456           (setq parent-msg msg
457                 msgs children))
458       (unless msgs
459         (while (and (null msgs) msgs-stack)
460           (setq msgs (wl-pop msgs-stack)))
461         (when msgs
462           (setq parent-msg
463                 (wl-thread-entity-get-number
464                  (wl-thread-entity-get-parent-entity
465                   (wl-thread-get-entity (car msgs)))))))))
466    updates))
467
468 (defun wl-thread-update-line-msgs (msgs &optional no-msg)
469   (wl-delete-all-overlays)
470   (let ((i 0)
471         (updates msgs)
472         len)
473 ;;; (while msgs
474 ;;;   (setq updates
475 ;;;         (append updates
476 ;;;                 (wl-thread-get-children-msgs (car msgs))))
477 ;;;   (setq msgs (cdr msgs)))
478 ;;; (setq updates (elmo-uniq-list updates))
479     (setq len (length updates))
480     (while updates
481       (wl-thread-update-line-on-buffer-sub nil (car updates))
482       (setq updates (cdr updates))
483       (when (and (not no-msg)
484                  (> len elmo-display-progress-threshold))
485         (setq i (1+ i))
486         (if (or (zerop (% i 5)) (= i len))
487             (elmo-display-progress
488              'wl-thread-update-line-msgs "Updating deleted thread..."
489              (/ (* i 100) len)))))))
490
491 (defun wl-thread-delete-line-from-buffer (msg)
492   "Simply delete msg line."
493   (let (beg)
494     (if (wl-summary-jump-to-msg msg)
495         (progn
496           (setq beg (point))
497           (forward-line 1)
498           (delete-region beg (point))
499           t)
500       nil)))
501
502 (defun wl-thread-cleanup-symbols (msgs)
503   (let (entity)
504     (while msgs
505       (when (setq entity (wl-thread-get-entity (car msgs)))
506         ;; delete entity.
507         (setq wl-thread-entities (delq entity wl-thread-entities))
508         ;; free symbol.
509         (elmo-clear-hash-val (format "#%d" (car msgs))
510                              wl-thread-entity-hashtb))
511       (setq msgs (cdr msgs)))))
512
513 (defun wl-thread-get-exist-children (msg)
514   (let ((msgs (list msg))
515         msgs-stack children
516         entity ret-val)
517     (while msgs
518       (setq children (wl-thread-entity-get-children
519                       (setq entity (wl-thread-get-entity (car msgs)))))
520       (when (elmo-msgdb-overview-get-entity (car msgs) wl-summary-buffer-msgdb)
521         (wl-append ret-val (list (car msgs)))
522         (setq children nil))
523       (setq msgs (cdr msgs))
524       (if (null children)
525           (while (and (null msgs) msgs-stack)
526             (setq msgs (wl-pop msgs-stack)))
527         (wl-push msgs msgs-stack)
528         (setq msgs children)))
529     ret-val))
530
531 (defun wl-thread-delete-message (msg &optional deep update)
532   "Delete MSG from entity and buffer."
533   (save-excursion
534     (let* ((entity (wl-thread-get-entity msg))
535            children older-brothers younger-brothers top-child ;;grandchildren
536            top-entity parent update-msgs beg invisible-top)
537       (when entity
538         (setq parent (wl-thread-entity-get-parent-entity entity))
539         (if parent
540             (progn
541 ;;; has parent.
542 ;;;           (setq brothers (wl-thread-entity-get-children parent))
543               (setq older-brothers (wl-thread-entity-get-older-brothers
544                                     entity parent))
545               (setq younger-brothers (wl-thread-entity-get-younger-brothers
546                                       entity parent))
547               ;;
548               (unless deep
549                 (setq children (wl-thread-entity-get-children entity))
550                 (wl-thread-reparent-children
551                  children (wl-thread-entity-get-number parent))
552                 (setq update-msgs
553                       (apply (function nconc)
554                              update-msgs
555                              (mapcar
556                               (function
557                                (lambda (message)
558                                  (wl-thread-get-children-msgs message t)))
559                               children))))
560               (wl-thread-entity-set-children
561                parent (append older-brothers children younger-brothers))
562               ;; If chidren and younger-brothers not exists,
563               ;; update nearly older brother.
564               (when (and older-brothers
565                          (not younger-brothers)
566                          (not children))
567                 (wl-append
568                  update-msgs
569                  (wl-thread-get-children-msgs (car (last older-brothers))))))
570
571           ;; top...oldest child becomes top.
572           (unless deep
573             (setq children (wl-thread-entity-get-children entity))
574             (when children
575               (setq top-child (car children)
576                     children (cdr children))
577               (setq top-entity (wl-thread-get-entity top-child))
578               (wl-thread-entity-set-parent top-entity nil)
579               (wl-thread-entity-set-linked top-entity nil)
580               (wl-append update-msgs
581                          (wl-thread-get-children-msgs top-child t)))
582             (when children
583               (wl-thread-entity-set-children
584                top-entity
585                (append
586                 (wl-thread-entity-get-children top-entity)
587                 children))
588               (wl-thread-reparent-children children top-child)
589               (wl-append update-msgs children)))
590           ;; delete myself from top list.
591           (setq wl-summary-buffer-number-list
592                 (delq msg wl-summary-buffer-number-list))
593           (setq older-brothers (wl-thread-entity-get-older-brothers
594                                 entity nil))
595           (setq younger-brothers (wl-thread-entity-get-younger-brothers
596                                   entity nil))
597           (setq wl-thread-entity-list
598                 (append (append older-brothers
599                                 (and top-child (list top-child)))
600                         younger-brothers))))
601
602       (if deep
603           ;; delete thread on buffer
604           (when (wl-summary-jump-to-msg msg)
605             (setq beg (point))
606             (wl-thread-goto-bottom-of-sub-thread)
607             (delete-region beg (point)))
608         ;; delete myself from buffer.
609         (unless (wl-thread-delete-line-from-buffer msg)
610           ;; jump to suitable point.
611           ;; just upon the oldest younger-brother of my top.
612           (setq invisible-top
613                 (car (wl-thread-entity-parent-invisible-p entity)))
614           (if invisible-top
615               (progn
616                 (wl-append update-msgs (list invisible-top))
617                 (wl-summary-jump-to-msg invisible-top))
618             (goto-char (point-max))))
619
620         ;; insert children if thread is closed or delete top.
621         (when (or top-child
622                   (not (wl-thread-entity-get-opened entity)))
623           (let* (next-top insert-msgs ent e grandchildren)
624             (if top-child
625                 (progn
626                   (setq insert-msgs (wl-thread-get-exist-children top-child))
627                   (setq next-top (car insert-msgs))
628                   (setq ent (wl-thread-get-entity next-top))
629                   (when (and
630                          (wl-thread-entity-get-opened entity) ;; open
631                          (not (wl-thread-entity-get-opened ent)) ;; close
632                          (setq grandchildren
633                                (wl-thread-entity-get-children ent))
634                          (wl-summary-jump-to-msg next-top))
635                     (forward-line 1)
636                     (setq insert-msgs (append (cdr insert-msgs) grandchildren)))
637                   (when top-entity (wl-thread-entity-set-opened top-entity t))
638                   (when ent (wl-thread-entity-set-opened ent t)))
639               (when (not invisible-top)
640                 (setq insert-msgs (wl-thread-get-exist-children msg))
641                 ;; First msg always opened, because first msg maybe becomes top.
642                 (if (setq ent (wl-thread-get-entity (car insert-msgs)))
643                     (wl-thread-entity-set-opened ent t))))
644             ;; insert children
645             (while insert-msgs
646               ;; if no exists in summary, insert entity.
647               (when (and (car insert-msgs)
648                          (not (wl-summary-jump-to-msg (car insert-msgs))))
649                 (setq ent (wl-thread-get-entity (car insert-msgs)))
650                 (wl-thread-insert-entity 0 ; no mean now...
651                                          ent entity nil))
652               (setq insert-msgs (cdr insert-msgs))))))
653       (if update
654           ;; modify buffer.
655           (while update-msgs
656             (wl-thread-update-line-on-buffer-sub nil (pop update-msgs)))
657         ;; don't update buffer
658         update-msgs)))) ; return value
659
660 (defun wl-thread-insert-message (overview-entity overview mark-alist
661                                  msg parent-msg &optional update linked)
662   "Insert MSG to the entity.
663 When optional argument UPDATE is non-nil,
664 Message is inserted to the summary buffer."
665   (let ((parent (wl-thread-get-entity parent-msg))
666         child-entity invisible-top)
667 ;;; Update the thread view...not implemented yet.
668 ;;;  (when force-insert
669 ;;;    (if parent
670 ;;;       (wl-thread-entity-force-open parent))
671     (if parent
672         ;; insert as children.
673         (wl-thread-entity-insert-as-children
674          parent
675          (setq child-entity (wl-thread-create-entity msg (nth 0 parent) nil linked)))
676       ;; insert as top message.
677       (wl-thread-entity-insert-as-top
678        (wl-thread-create-entity msg nil)))
679     (if update
680         (if (not (setq invisible-top
681                        (wl-thread-entity-parent-invisible-p child-entity)))
682             ;; visible.
683             (progn
684               (wl-summary-update-thread
685                overview-entity
686                overview
687                mark-alist
688                child-entity
689                (elmo-msgdb-overview-get-entity
690                 parent-msg wl-summary-buffer-msgdb))
691               (when parent
692                 ;; use thread structure.
693                 (wl-thread-entity-get-nearly-older-brother
694                  child-entity parent))) ; return value
695 ;;;             (wl-thread-entity-get-number
696 ;;;              (wl-thread-entity-get-top-entity parent)))) ; return value;
697 ;;;           (setq beg (point))
698 ;;;           (wl-thread-goto-bottom-of-sub-thread)
699 ;;;           (wl-thread-update-indent-string-region beg (point)))
700           ;; currently invisible.. update closed line.
701           (wl-thread-update-children-number invisible-top)
702           nil))))
703
704 (defun wl-thread-get-parent-list (msgs)
705   (let* ((msgs2 msgs)
706          myself)
707     (while msgs2
708       (setq myself (car msgs2)
709             msgs2 (cdr msgs2))
710       (while (not (eq myself (car msgs2)))
711         (if (wl-thread-descendant-p myself (car msgs2))
712             (setq msgs (delq (car msgs2) msgs)))
713         (setq msgs2 (or (cdr msgs2) msgs)))
714       (setq msgs2 (cdr msgs2)))
715     msgs))
716
717 (defun wl-thread-update-indent-string-thread (top-list)
718   (let ((top-list (wl-thread-get-parent-list top-list))
719         beg)
720     (while top-list
721       (when (car top-list)
722         (wl-summary-jump-to-msg (car top-list))
723         (setq beg (point))
724         (wl-thread-goto-bottom-of-sub-thread)
725         (wl-thread-update-indent-string-region beg (point)))
726       (setq top-list (cdr top-list)))))
727
728 (defun wl-thread-update-children-number (entity)
729   "Update the children number."
730   (save-excursion
731     (wl-summary-jump-to-msg (wl-thread-entity-get-number entity))
732     (beginning-of-line)
733     (let ((text-prop (get-text-property (point) 'face))
734           from from-end beg str)
735       (cond
736        ((looking-at (concat "^" wl-summary-buffer-number-regexp
737                             "..../..\(.*\)..:.. ["
738                             wl-thread-indent-regexp
739                             "]*[[<]\\+\\([0-9]+\\):"))
740         (delete-region (match-beginning 1)(match-end 1))
741         (goto-char (match-beginning 1))
742         (setq str (format "%s" (wl-thread-entity-get-children-num entity)))
743         (if wl-summary-highlight
744             (put-text-property 0 (length str) 'face text-prop str))
745         (insert str))
746        ((looking-at (concat "^" wl-summary-buffer-number-regexp
747                             "..../..\(.*\)..:.. ["
748                             wl-thread-indent-regexp
749                             "]*[[<]"))
750         (goto-char (match-end 0))
751         (setq beg (current-column))
752         (setq from-end (save-excursion
753                          (move-to-column (+ 1 beg wl-from-width))
754                          (point)))
755         (setq from (buffer-substring (match-end 0) from-end))
756         (delete-region (match-end 0) from-end)
757         (setq str (wl-set-string-width
758                    (1+ wl-from-width)
759                    (format
760                     "+%s:%s"
761                     (wl-thread-entity-get-children-num
762                      entity)
763                     from)))
764         (if wl-summary-highlight
765             (put-text-property 0 (length str) 'face text-prop str))
766         (insert str)
767         (condition-case nil ; it's dangerous, so ignore error.
768             (run-hooks 'wl-thread-update-children-number-hook)
769           (error
770            (ding)
771            (message "Error in wl-thread-update-children-number-hook."))))))))
772
773 ;; 
774 ;; Thread oriented commands.
775 ;;
776 (defun wl-thread-call-region-func (func &optional arg)
777   (save-excursion
778     (if arg
779         (wl-summary-goto-top-of-current-thread)
780       (beginning-of-line))
781     (let ((beg (point)))
782       (wl-thread-goto-bottom-of-sub-thread)
783       (funcall func beg (point)))))
784
785 (defun wl-thread-prefetch (&optional arg)
786   (interactive "P")
787   (wl-thread-call-region-func 'wl-summary-prefetch-region arg))
788
789 (defun wl-thread-msg-mark-as-important (msg)
790   "Set mark as important for invisible MSG. Modeline is not changed."
791   (let* ((msgdb wl-summary-buffer-msgdb)
792          (mark-alist (elmo-msgdb-get-mark-alist msgdb))
793          cur-mark)
794     (setq cur-mark (cadr (assq msg mark-alist)))
795     (setq mark-alist
796           (elmo-msgdb-mark-set mark-alist
797                                msg
798                                (if (string= cur-mark wl-summary-important-mark)
799                                    nil
800                                  wl-summary-important-mark)))
801     (elmo-msgdb-set-mark-alist msgdb mark-alist)
802     (wl-summary-set-mark-modified)))
803
804 (defun wl-thread-mark-as-read (&optional arg)
805   (interactive "P")
806   (wl-thread-call-region-func 'wl-summary-mark-as-read-region arg))
807
808 (defun wl-thread-mark-as-unread (&optional arg)
809   (interactive "P")
810   (wl-thread-call-region-func 'wl-summary-mark-as-unread-region arg))
811
812 (defun wl-thread-mark-as-important (&optional arg)
813   (interactive "P")
814   (wl-thread-call-region-func 'wl-summary-mark-as-important-region arg))
815
816 (defun wl-thread-copy (&optional arg)
817   (interactive "P")
818   (wl-thread-call-region-func 'wl-summary-copy-region arg))
819
820 (defun wl-thread-refile (&optional arg)
821   (interactive "P")
822   (condition-case err
823       (progn
824         (wl-thread-call-region-func 'wl-summary-refile-region arg)
825         (if arg
826             (wl-summary-goto-top-of-current-thread))
827         (wl-thread-goto-bottom-of-sub-thread))
828     (error
829      (elmo-display-error err t)
830      nil)))
831         
832 (defun wl-thread-delete (&optional arg)
833   (interactive "P")
834   (wl-thread-call-region-func 'wl-summary-delete-region arg)
835   (if arg
836       (wl-summary-goto-top-of-current-thread))
837   (if (not wl-summary-move-direction-downward)
838       (wl-summary-prev)
839     (wl-thread-goto-bottom-of-sub-thread)
840     (if wl-summary-buffer-disp-msg
841         (wl-summary-redisplay))))
842
843 (defun wl-thread-target-mark (&optional arg)
844   (interactive "P")
845   (wl-thread-call-region-func 'wl-summary-target-mark-region arg))
846
847 (defun wl-thread-unmark (&optional arg)
848   (interactive "P")
849   (wl-thread-call-region-func 'wl-summary-unmark-region arg))
850
851 (defun wl-thread-exec (&optional arg)
852   (interactive "P")
853   (wl-thread-call-region-func 'wl-summary-exec-region arg))
854
855 (defun wl-thread-save (&optional arg)
856   (interactive "P")
857   (wl-thread-call-region-func 'wl-summary-save-region arg))
858
859 (defun wl-thread-force-open (&optional msg-num)
860   "force open current folder"
861   (if msg-num
862       (wl-summary-jump-to-msg msg-num))
863   (let ((wl-thread-insert-force-opened t))
864     (wl-thread-open-close)))
865
866 (defun wl-thread-entity-force-open (entity)
867   (let ((wl-thread-insert-force-opened t)
868         notopen)
869     (if (null (wl-thread-entity-get-parent entity))
870         ;; top!!
871         (if (and (not (wl-thread-entity-get-opened entity))
872                  (wl-thread-entity-get-children entity))
873             (wl-thread-force-open (wl-thread-entity-get-number entity)))
874       (if (setq notopen (wl-thread-entity-parent-invisible-p entity))
875           (wl-thread-force-open (wl-thread-entity-get-number notopen))))))
876
877 (defun wl-thread-insert-top ()
878   (let ((elist wl-thread-entity-list)
879         (len (length wl-thread-entity-list))
880         (cur 0))
881     (wl-delete-all-overlays)
882     (while elist
883       (wl-thread-insert-entity
884        0
885        (wl-thread-get-entity (car elist))
886        nil
887        len)
888       (setq elist (cdr elist))
889       (when (> len elmo-display-progress-threshold)
890         (setq cur (1+ cur))
891         (if (or (zerop (% cur 2)) (= cur len))
892             (elmo-display-progress
893              'wl-thread-insert-top "Inserting thread..."
894              (/ (* cur 100) len)))))))
895
896 (defsubst wl-thread-insert-entity-sub (indent entity parent-entity all)
897   (let ((mark-alist (elmo-msgdb-get-mark-alist wl-summary-buffer-msgdb))
898         msg-num
899         overview-entity
900         temp-mark
901         summary-line)
902     (when (setq msg-num (wl-thread-entity-get-number entity))
903       (unless all ; all...means no temp-mark.
904         (cond ((memq msg-num wl-summary-buffer-delete-list)
905                (setq temp-mark "D"))
906               ((memq msg-num wl-summary-buffer-target-mark-list)
907                (setq temp-mark "*"))
908               ((assq msg-num wl-summary-buffer-refile-list)
909                (setq temp-mark "o"))
910               ((assq msg-num wl-summary-buffer-copy-list)
911                (setq temp-mark "O"))))
912       (unless temp-mark
913         (setq temp-mark (wl-summary-get-score-mark msg-num)))
914       (setq overview-entity
915             (elmo-msgdb-overview-get-entity
916              (nth 0 entity) wl-summary-buffer-msgdb))
917 ;;;   (wl-delete-all-overlays)
918       (when overview-entity
919         (setq summary-line
920               (wl-summary-overview-create-summary-line
921                msg-num
922                overview-entity
923                (elmo-msgdb-overview-get-entity
924                 (nth 0 parent-entity) wl-summary-buffer-msgdb)
925                (1+ indent)
926                mark-alist
927                (if wl-thread-insert-force-opened
928                    nil
929                  (wl-thread-maybe-get-children-num msg-num))
930                temp-mark entity))
931         (wl-summary-insert-line summary-line)))))
932
933 (defun wl-thread-insert-entity (indent entity parent-entity all)
934   "Insert thread entity in current buffer."
935   (let ((msgs (list (car entity)))
936         children msgs-stack)
937     (while msgs
938       (wl-thread-insert-entity-sub indent entity parent-entity all)
939       (setq msgs (cdr msgs))
940       (setq children (nth 2 entity))
941       (if children
942           ;; insert children
943           (when (or wl-thread-insert-force-opened
944                     (wl-thread-entity-get-opened entity))
945             (wl-thread-entity-set-opened entity t)
946             (wl-push msgs msgs-stack)
947             (setq msgs children
948                   indent (1+ indent)
949                   parent-entity entity)))
950       (unless msgs
951         (while (and (null msgs) msgs-stack)
952           (setq msgs (wl-pop msgs-stack))
953           (setq indent (1- indent)))
954         (when msgs
955           (setq entity (wl-thread-get-entity (car msgs)))
956           (setq parent-entity (wl-thread-entity-get-parent-entity entity))))
957       (setq entity (wl-thread-get-entity (car msgs))))))
958
959 (defun wl-thread-descendant-p (mynumber number)
960   (let ((cur (wl-thread-get-entity number))
961         num)
962     (catch 'done
963       (while cur
964         (setq cur (wl-thread-entity-get-parent-entity cur))
965         (if (null (setq num (wl-thread-entity-get-number cur))) ; top!
966             (throw 'done nil))
967         (if (and num
968                  (eq mynumber (wl-thread-entity-get-number cur)))
969             (throw 'done t)))
970       nil)))
971
972 ;; (defun wl-thread-goto-bottom-of-sub-thread ()
973 ;;   (interactive)
974 ;;   (let ((depth (wl-thread-get-depth-of-current-line)))
975 ;;     (forward-line 1)
976 ;;     (while (and (not (eobp))
977 ;;              (> (wl-thread-get-depth-of-current-line)
978 ;;                 depth))
979 ;;       (forward-line 1))
980 ;;     (beginning-of-line)))
981
982 (defun wl-thread-goto-bottom-of-sub-thread (&optional msg)
983   (interactive)
984   (let ((mynumber (or msg (wl-summary-message-number))))
985     (forward-line 1)
986     (while (wl-thread-descendant-p mynumber (wl-summary-message-number))
987       (forward-line 1))
988     (beginning-of-line)))
989
990 (defun wl-thread-remove-destination-region (beg end)
991   (save-excursion
992     (save-restriction
993       (narrow-to-region beg end)
994       (goto-char (point-min))
995       (while (not (eobp))
996         (let ((num (wl-summary-message-number)))
997           (if (assq num wl-summary-buffer-refile-list)
998               (wl-summary-remove-destination)))
999         (forward-line 1)))))
1000
1001 (defun wl-thread-print-destination-region (beg end)
1002   (if (or wl-summary-buffer-refile-list
1003           wl-summary-buffer-copy-list)
1004       (save-excursion
1005         (save-restriction
1006           (narrow-to-region beg end)
1007           (goto-char (point-min))
1008           (while (not (eobp))
1009             (let ((num (wl-summary-message-number))
1010                   pair)
1011               (if (or (setq pair (assq num wl-summary-buffer-refile-list))
1012                       (setq pair (assq num wl-summary-buffer-copy-list)))
1013                   (wl-summary-print-destination (car pair) (cdr pair))))
1014             (forward-line 1))))))
1015
1016 (defsubst wl-thread-get-children-msgs (msg &optional visible-only)
1017   (let ((msgs (list msg))
1018         msgs-stack children
1019         entity ret-val)
1020     (while msgs
1021       (wl-append ret-val (list (car msgs)))
1022       (setq children (wl-thread-entity-get-children
1023                       (setq entity (wl-thread-get-entity (car msgs)))))
1024       (if (and visible-only
1025                (not (wl-thread-entity-get-opened entity)))
1026           (setq children nil))
1027       (setq msgs (cdr msgs))
1028       (if (null children)
1029           (while (and (null msgs) msgs-stack)
1030             (setq msgs (wl-pop msgs-stack)))
1031         (wl-push msgs msgs-stack)
1032         (setq msgs children)))
1033     ret-val))
1034
1035 (defun wl-thread-get-children-msgs-uncached (msg &optional uncached-marks)
1036   (let ((children-msgs (wl-thread-get-children-msgs msg))
1037         (mark-alist (elmo-msgdb-get-mark-alist wl-summary-buffer-msgdb))
1038         (number-alist (elmo-msgdb-get-number-alist wl-summary-buffer-msgdb))
1039         mark
1040         uncached-list)
1041     (while children-msgs
1042       (if (and (not (eq msg (car children-msgs))) ; except itself
1043                (or (and uncached-marks
1044                         (setq mark (cadr (assq (car children-msgs)
1045                                                mark-alist)))
1046                         (member mark uncached-marks))
1047                    (and (not uncached-marks)
1048                         (null (elmo-cache-exists-p
1049                                (cdr (assq (car children-msgs)
1050                                           number-alist)))))))
1051           (wl-append uncached-list (list (car children-msgs))))
1052       (setq children-msgs (cdr children-msgs)))
1053     uncached-list))
1054
1055 (defun wl-thread-get-children-msgs-with-mark (msg mark)
1056   (let ((children-msgs (wl-thread-get-children-msgs msg))
1057         (check-func (cond ((string= mark "o")
1058                            'wl-summary-msg-marked-as-refiled)
1059                           ((string= mark "O")
1060                            'wl-summary-msg-marked-as-copied)
1061                           ((string= mark "D")
1062                            'wl-summary-msg-marked-as-deleted)
1063                           ((string= mark "*")
1064                            'wl-summary-msg-marked-as-target)))
1065         ret-val)
1066     (while children-msgs
1067       (if (funcall check-func (car children-msgs))
1068           (wl-append ret-val (list (car children-msgs))))
1069       (setq children-msgs (cdr children-msgs)))
1070     ret-val))
1071
1072 (defun wl-thread-close (entity)
1073   (let (depth beg)
1074     (wl-thread-entity-set-opened entity nil)
1075     (setq depth (wl-thread-get-depth-of-current-line))
1076     (beginning-of-line)
1077     (setq beg (point))
1078     (wl-thread-goto-bottom-of-sub-thread)
1079     (wl-thread-remove-destination-region beg
1080                                          (point))
1081     (forward-char -1)   ;; needed for mouse-face.
1082     (delete-region beg (point))
1083     (wl-thread-insert-entity (- depth 1)
1084                              entity
1085                              (wl-thread-get-entity
1086                               (nth 3 entity))
1087                              nil)
1088     (delete-char 1) ; delete '\n'
1089     (wl-thread-print-destination-region beg (point))))
1090
1091 (defun wl-thread-open (entity)
1092   (let (depth beg)
1093     (beginning-of-line)
1094     (setq beg (point))
1095     (setq depth (wl-thread-get-depth-of-current-line))
1096     (end-of-line)
1097     (delete-region beg (point))
1098     (wl-thread-entity-set-opened entity t)
1099     (wl-thread-insert-entity depth ;(- depth 1)
1100                              entity
1101                              (wl-thread-get-entity
1102                               (nth 3 entity)) nil)
1103     (delete-char 1) ; delete '\n'
1104     (wl-thread-print-destination-region beg (point))))
1105
1106 (defun wl-thread-open-close (&optional force-open)
1107   (interactive "P")
1108   (when (eq wl-summary-buffer-view 'thread)
1109 ;;; (if (equal wl-thread-top-entity '(nil t nil nil))
1110 ;;;     (error "There's no thread structure"))
1111     (save-excursion
1112       (let ((inhibit-read-only t)
1113             (buffer-read-only nil)
1114             (wl-thread-insert-force-opened
1115              (or wl-thread-insert-force-opened
1116                  force-open))
1117             msg entity parent)
1118         (setq msg (wl-summary-message-number))
1119         (setq entity (wl-thread-get-entity msg))
1120         (if (wl-thread-entity-get-opened entity)
1121             ;; if already opened, close its child!
1122           (if (wl-thread-entity-get-children entity)
1123               (wl-thread-close entity)
1124             ;; opened, but has no children, close its parent!
1125             (when (setq parent (wl-thread-entity-get-parent entity))
1126               (wl-summary-jump-to-msg parent)
1127               (wl-thread-close
1128                (wl-thread-get-entity (wl-summary-message-number)))))
1129           ;; if closed (or it is just a thread bottom message)
1130           ;; has children, open it!
1131           (if (wl-thread-entity-get-children entity)
1132               (wl-thread-open entity)
1133             ;; closed, and has no children, close its parent!
1134             (setq msg (or (wl-thread-entity-get-parent entity)
1135                           (wl-thread-entity-get-number entity)))
1136             (when msg
1137               (wl-summary-jump-to-msg msg)
1138               (wl-thread-close
1139                (wl-thread-get-entity (wl-summary-message-number)))))))
1140       (wl-summary-set-message-modified)
1141       (set-buffer-modified-p nil))))
1142   
1143
1144 (defun wl-thread-get-depth-of-current-line ()
1145   (interactive)
1146   (save-excursion
1147     (beginning-of-line)
1148     (let ((depth 0))
1149       (if (re-search-forward (concat "^" wl-summary-buffer-number-regexp
1150                                      "..../..\(.*\)..:.. ")
1151                              nil t)
1152           (while (string-match wl-thread-indent-regexp
1153                                (char-to-string
1154                                 (char-after (point))))
1155             (setq depth (1+ depth))
1156             (forward-char)))
1157       (/ depth wl-thread-indent-level-internal))))
1158
1159 (defun wl-thread-update-indent-string-region (beg end)
1160   (interactive "r")
1161   (save-excursion
1162     (goto-char beg)
1163     (while (< (point) end)
1164       (wl-thread-update-indent-string)
1165       (forward-line 1))))
1166
1167 (defsubst wl-thread-make-indent-string (entity)
1168   (let ((cur entity)
1169         (ret-val "")
1170         (space-str (wl-repeat-string wl-thread-space-str-internal
1171                                      (- wl-thread-indent-level-internal 1)))
1172         parent)
1173     (when (wl-thread-entity-get-number
1174            (setq parent (wl-thread-entity-get-parent-entity cur)))
1175       (if (wl-thread-entity-get-younger-brothers cur)
1176           (setq ret-val wl-thread-have-younger-brother-str-internal)
1177         (setq ret-val wl-thread-youngest-child-str-internal))
1178       (setq ret-val (concat ret-val
1179                             (wl-repeat-string
1180                              wl-thread-horizontal-str-internal
1181                              (- wl-thread-indent-level-internal 1))))
1182       (setq cur parent)
1183       (while (wl-thread-entity-get-number
1184               (wl-thread-entity-get-parent-entity cur))
1185         (if (wl-thread-entity-get-younger-brothers cur)
1186             (setq ret-val (concat wl-thread-vertical-str-internal
1187                                   space-str
1188                                   ret-val))
1189           (setq ret-val (concat wl-thread-space-str-internal
1190                                 space-str
1191                                 ret-val)))
1192         (setq cur (wl-thread-entity-get-parent-entity cur))))
1193     ret-val))
1194
1195 (defun wl-thread-update-indent-string ()
1196   "Update indent string of current line."
1197   (interactive)
1198   (save-excursion
1199     (beginning-of-line)
1200     (let ((inhibit-read-only t)
1201           (buffer-read-only nil)
1202           thr-str)
1203       (when (looking-at (concat "^ *\\([0-9]+\\)"
1204                                 "..../..\(.*\)..:.. \\("
1205                                 wl-highlight-thread-indent-string-regexp
1206                                 "\\)[[<]"))
1207         (goto-char (match-beginning 2))
1208         (delete-region (match-beginning 2)
1209                        (match-end 2))
1210         (setq thr-str
1211               (wl-thread-make-indent-string
1212                (wl-thread-get-entity (string-to-int (wl-match-buffer 1)))))
1213         (if (and wl-summary-width
1214                  wl-summary-indent-length-limit
1215                  (< wl-summary-indent-length-limit
1216                     (string-width thr-str)))
1217             (setq thr-str (wl-set-string-width
1218                            wl-summary-indent-length-limit
1219                            thr-str)))
1220         (insert thr-str)
1221         (if wl-summary-highlight
1222             (wl-highlight-summary-current-line))))))
1223
1224 (defun wl-thread-set-parent (&optional parent-number)
1225   "Set current message's parent interactively."
1226   (interactive)
1227   (let ((number (wl-summary-message-number))
1228         (dst-parent (if (interactive-p)
1229                         (read-from-minibuffer "Parent Message (No.): ")))
1230         entity dst-parent-entity src-parent children
1231         update-msgs
1232         buffer-read-only)
1233     (if (string= dst-parent "")
1234         (setq dst-parent nil)
1235       (if (interactive-p)
1236           (setq dst-parent (string-to-int dst-parent))
1237         (setq dst-parent parent-number)))
1238     (if (and dst-parent
1239              (memq dst-parent (wl-thread-get-children-msgs number)))
1240         (error "Parent is children or myself"))
1241     (setq entity (wl-thread-get-entity number))
1242     (when (and number entity)
1243       ;; delete thread
1244       (setq update-msgs (wl-thread-delete-message number 'deep))
1245       ;; insert as child at new parent
1246       (setq dst-parent-entity (wl-thread-get-entity dst-parent))
1247       (if dst-parent-entity
1248           (progn
1249             (if (setq children
1250                       (wl-thread-entity-get-children dst-parent-entity))
1251                 (wl-append update-msgs
1252                            (wl-thread-get-children-msgs
1253                             (car (last children)) t)))
1254             (wl-thread-entity-set-children
1255              dst-parent-entity
1256              (append children (list number)))
1257             (wl-thread-entity-set-linked entity t))
1258         ;; insert as top
1259         (wl-append wl-thread-entity-list (list number))
1260         (wl-thread-entity-set-linked entity nil))
1261
1262       ;; update my thread
1263       (wl-append update-msgs (wl-thread-get-children-msgs number t))
1264       (setq update-msgs (elmo-uniq-list update-msgs))
1265       (wl-thread-entity-set-parent entity dst-parent)
1266       ;; update thread on buffer
1267       (wl-thread-update-line-msgs update-msgs t))))
1268
1269 (require 'product)
1270 (product-provide (provide 'wl-thread) (require 'wl-version))
1271
1272 ;;; wl-thread.el ends here