* wl-expire.el (wl-summary-archive): Fixed;
[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-folder-msgdb-path fld))))
62     (setq top-list
63           (wl-summary-load-file-object
64            (expand-file-name wl-thread-entity-list-file
65                              (elmo-folder-msgdb-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         (if (wl-thread-entity-get-opened
353              (setq entity (wl-thread-get-entity
354                            (wl-summary-message-number))))
355             (forward-line 1)
356           (wl-thread-force-open)
357           (wl-thread-goto-bottom-of-sub-thread))
358         (when (> len elmo-display-progress-threshold)
359           (setq cur (1+ cur))
360           (elmo-display-progress
361            'wl-thread-open-all "Opening all threads..."
362            (/ (* cur 100) len)))))
363     ;; Make sure to be 100%.
364     (elmo-display-progress
365      'wl-thread-open-all "Opening all threads..."
366      100))
367   (message "Opening all threads...done"))
368
369 (defun wl-thread-open-all-unread ()
370   (interactive)
371   (let ((mark-alist (elmo-msgdb-get-mark-alist (wl-summary-buffer-msgdb)))
372         mark)
373     (while mark-alist
374       (if (setq mark (nth 1 (car mark-alist)))
375           (if (or (string= mark wl-summary-unread-uncached-mark)
376                   (string= mark wl-summary-unread-cached-mark)
377                   (string= mark wl-summary-new-mark)
378                   (string= mark wl-summary-important-mark))
379               (wl-thread-entity-force-open (wl-thread-get-entity
380                                             (nth 0 (car mark-alist))))))
381       (setq mark-alist (cdr mark-alist)))))
382
383 (defsubst wl-thread-maybe-get-children-num (msg)
384   (let ((entity (wl-thread-get-entity msg)))
385     (if (not (wl-thread-entity-get-opened entity))
386         (wl-thread-entity-get-children-num entity))))
387
388 (defsubst wl-thread-update-line-on-buffer-sub (entity msg &optional parent-msg)
389   (let* ((entity (or entity (wl-thread-get-entity msg)))
390          (parent-msg (or parent-msg (wl-thread-entity-get-parent entity)))
391          (overview (elmo-msgdb-get-overview (wl-summary-buffer-msgdb)))
392          (mark-alist (elmo-msgdb-get-mark-alist (wl-summary-buffer-msgdb)))
393          (buffer-read-only nil)
394          (inhibit-read-only t)
395          overview-entity temp-mark summary-line invisible-top dest-pair)
396     (if (wl-thread-delete-line-from-buffer msg)
397         (progn
398           (cond
399            ((memq msg wl-summary-buffer-delete-list)
400             (setq temp-mark "D"))
401            ((memq msg wl-summary-buffer-target-mark-list)
402             (setq temp-mark "*"))
403            ((setq dest-pair (assq msg wl-summary-buffer-refile-list))
404             (setq temp-mark "o"))
405            ((setq dest-pair (assq msg wl-summary-buffer-copy-list))
406             (setq temp-mark "O"))
407            (t (setq temp-mark (wl-summary-get-score-mark msg))))
408           (when (setq overview-entity
409                       (elmo-msgdb-overview-get-entity
410                        msg (wl-summary-buffer-msgdb)))
411             (setq summary-line
412                   (wl-summary-overview-create-summary-line
413                    msg
414                    overview-entity
415                    (elmo-msgdb-overview-get-entity
416                     parent-msg (wl-summary-buffer-msgdb))
417                    nil
418                    mark-alist
419                    (if wl-thread-insert-force-opened
420                        nil
421                      (wl-thread-maybe-get-children-num msg))
422                    temp-mark entity))
423             (save-excursion
424               (wl-summary-insert-line summary-line))
425             (if dest-pair
426                 (wl-summary-print-destination (car dest-pair)
427                                               (cdr dest-pair)))))
428       ;; insert thread (moving thread)
429       (if (not (setq invisible-top
430                      (wl-thread-entity-parent-invisible-p entity)))
431           (wl-summary-update-thread
432            (elmo-msgdb-overview-get-entity msg (wl-summary-buffer-msgdb))
433            overview
434            mark-alist
435            entity
436            (and parent-msg
437                 (elmo-msgdb-overview-get-entity
438                  parent-msg (wl-summary-buffer-msgdb))))
439         ;; currently invisible.. update closed line.
440         (wl-thread-update-children-number invisible-top)))))
441
442 (defun wl-thread-update-line-on-buffer (&optional msg parent-msg updates)
443   (interactive)
444   (let ((msgs (list (or msg (wl-summary-message-number))))
445         entity children msgs-stack)
446    (while msgs
447     (setq msg (wl-pop msgs))
448     (setq updates (and updates (delete msg updates)))
449     (setq entity (wl-thread-get-entity msg))
450     (wl-thread-update-line-on-buffer-sub entity msg parent-msg)
451     ;;
452     (setq children (wl-thread-entity-get-children entity))
453     (if children
454         ;; update children
455         (when (wl-thread-entity-get-opened entity)
456           (wl-push msgs msgs-stack)
457           (setq parent-msg msg
458                 msgs children))
459       (unless msgs
460         (while (and (null msgs) msgs-stack)
461           (setq msgs (wl-pop msgs-stack)))
462         (when msgs
463           (setq parent-msg
464                 (wl-thread-entity-get-number
465                  (wl-thread-entity-get-parent-entity
466                   (wl-thread-get-entity (car msgs)))))))))
467    updates))
468
469 (defun wl-thread-update-line-msgs (msgs &optional no-msg)
470   (wl-delete-all-overlays)
471   (let ((i 0)
472         (updates msgs)
473         len)
474 ;;; (while msgs
475 ;;;   (setq updates
476 ;;;         (append updates
477 ;;;                 (wl-thread-get-children-msgs (car msgs))))
478 ;;;   (setq msgs (cdr msgs)))
479 ;;; (setq updates (elmo-uniq-list updates))
480     (setq len (length updates))
481     (while updates
482       (wl-thread-update-line-on-buffer-sub nil (car updates))
483       (setq updates (cdr updates))
484       (when (and (not no-msg)
485                  (> len elmo-display-progress-threshold))
486         (setq i (1+ i))
487         (if (or (zerop (% i 5)) (= i len))
488             (elmo-display-progress
489              'wl-thread-update-line-msgs "Updating deleted thread..."
490              (/ (* i 100) len)))))))
491
492 (defun wl-thread-delete-line-from-buffer (msg)
493   "Simply delete msg line."
494   (let (beg)
495     (if (wl-summary-jump-to-msg msg)
496         (progn
497           (setq beg (point))
498           (forward-line 1)
499           (delete-region beg (point))
500           t)
501       nil)))
502
503 (defun wl-thread-cleanup-symbols (msgs)
504   (let (entity)
505     (while msgs
506       (when (setq entity (wl-thread-get-entity (car msgs)))
507         ;; delete entity.
508         (setq wl-thread-entities (delq entity wl-thread-entities))
509         ;; free symbol.
510         (elmo-clear-hash-val (format "#%d" (car msgs))
511                              wl-thread-entity-hashtb))
512       (setq msgs (cdr msgs)))))
513
514 (defun wl-thread-get-exist-children (msg)
515   (let ((msgs (list msg))
516         msgs-stack children
517         entity ret-val)
518     (while msgs
519       (setq children (wl-thread-entity-get-children
520                       (setq entity (wl-thread-get-entity (car msgs)))))
521       (when (elmo-msgdb-overview-get-entity (car msgs) (wl-summary-buffer-msgdb))
522         (wl-append ret-val (list (car msgs)))
523         (setq children nil))
524       (setq msgs (cdr msgs))
525       (if (null children)
526           (while (and (null msgs) msgs-stack)
527             (setq msgs (wl-pop msgs-stack)))
528         (wl-push msgs msgs-stack)
529         (setq msgs children)))
530     ret-val))
531
532 (defun wl-thread-delete-message (msg &optional deep update)
533   "Delete MSG from entity and buffer."
534   (save-excursion
535     (let* ((entity (wl-thread-get-entity msg))
536            children older-brothers younger-brothers top-child ;;grandchildren
537            top-entity parent update-msgs beg invisible-top)
538       (when entity
539         (setq parent (wl-thread-entity-get-parent-entity entity))
540         (if parent
541             (progn
542 ;;; has parent.
543 ;;;           (setq brothers (wl-thread-entity-get-children parent))
544               (setq older-brothers (wl-thread-entity-get-older-brothers
545                                     entity parent))
546               (setq younger-brothers (wl-thread-entity-get-younger-brothers
547                                       entity parent))
548               ;;
549               (unless deep
550                 (setq children (wl-thread-entity-get-children entity))
551                 (wl-thread-reparent-children
552                  children (wl-thread-entity-get-number parent))
553                 (setq update-msgs
554                       (apply (function nconc)
555                              update-msgs
556                              (mapcar
557                               (function
558                                (lambda (message)
559                                  (wl-thread-get-children-msgs message t)))
560                               children))))
561               (wl-thread-entity-set-children
562                parent (append older-brothers children younger-brothers))
563               ;; If chidren and younger-brothers not exists,
564               ;; update nearly older brother.
565               (when (and older-brothers
566                          (not younger-brothers)
567                          (not children))
568                 (wl-append
569                  update-msgs
570                  (wl-thread-get-children-msgs (car (last older-brothers))))))
571
572           ;; top...oldest child becomes top.
573           (unless deep
574             (setq children (wl-thread-entity-get-children entity))
575             (when children
576               (setq top-child (car children)
577                     children (cdr children))
578               (setq top-entity (wl-thread-get-entity top-child))
579               (wl-thread-entity-set-parent top-entity nil)
580               (wl-thread-entity-set-linked top-entity nil)
581               (wl-append update-msgs
582                          (wl-thread-get-children-msgs top-child t)))
583             (when children
584               (wl-thread-entity-set-children
585                top-entity
586                (append
587                 (wl-thread-entity-get-children top-entity)
588                 children))
589               (wl-thread-reparent-children children top-child)
590               (wl-append update-msgs children)))
591           ;; delete myself from top list.
592           (setq wl-summary-buffer-number-list
593                 (delq msg wl-summary-buffer-number-list))
594           (setq older-brothers (wl-thread-entity-get-older-brothers
595                                 entity nil))
596           (setq younger-brothers (wl-thread-entity-get-younger-brothers
597                                   entity nil))
598           (setq wl-thread-entity-list
599                 (append (append older-brothers
600                                 (and top-child (list top-child)))
601                         younger-brothers))))
602
603       (if deep
604           ;; delete thread on buffer
605           (when (wl-summary-jump-to-msg msg)
606             (setq beg (point))
607             (wl-thread-goto-bottom-of-sub-thread)
608             (delete-region beg (point)))
609         ;; delete myself from buffer.
610         (unless (wl-thread-delete-line-from-buffer msg)
611           ;; jump to suitable point.
612           ;; just upon the oldest younger-brother of my top.
613           (setq invisible-top
614                 (car (wl-thread-entity-parent-invisible-p entity)))
615           (if invisible-top
616               (progn
617                 (wl-append update-msgs (list invisible-top))
618                 (wl-summary-jump-to-msg invisible-top))
619             (goto-char (point-max))))
620
621         ;; insert children if thread is closed or delete top.
622         (when (or top-child
623                   (not (wl-thread-entity-get-opened entity)))
624           (let* (next-top insert-msgs ent e grandchildren)
625             (if top-child
626                 (progn
627                   (setq insert-msgs (wl-thread-get-exist-children top-child))
628                   (setq next-top (car insert-msgs))
629                   (setq ent (wl-thread-get-entity next-top))
630                   (when (and
631                          (wl-thread-entity-get-opened entity) ;; open
632                          (not (wl-thread-entity-get-opened ent)) ;; close
633                          (setq grandchildren
634                                (wl-thread-entity-get-children ent))
635                          (wl-summary-jump-to-msg next-top))
636                     (forward-line 1)
637                     (setq insert-msgs (append (cdr insert-msgs) grandchildren)))
638                   (when top-entity (wl-thread-entity-set-opened top-entity t))
639                   (when ent (wl-thread-entity-set-opened ent t)))
640               (when (not invisible-top)
641                 (setq insert-msgs (wl-thread-get-exist-children msg))
642                 ;; First msg always opened, because first msg maybe becomes top.
643                 (if (setq ent (wl-thread-get-entity (car insert-msgs)))
644                     (wl-thread-entity-set-opened ent t))))
645             ;; insert children
646             (while insert-msgs
647               ;; if no exists in summary, insert entity.
648               (when (and (car insert-msgs)
649                          (not (wl-summary-jump-to-msg (car insert-msgs))))
650                 (setq ent (wl-thread-get-entity (car insert-msgs)))
651                 (wl-thread-insert-entity 0 ; no mean now...
652                                          ent entity nil))
653               (setq insert-msgs (cdr insert-msgs))))))
654       (if update
655           ;; modify buffer.
656           (while update-msgs
657             (wl-thread-update-line-on-buffer-sub nil (pop update-msgs)))
658         ;; don't update buffer
659         update-msgs)))) ; return value
660
661 (defun wl-thread-insert-message (overview-entity overview mark-alist
662                                  msg parent-msg &optional update linked)
663   "Insert MSG to the entity.
664 When optional argument UPDATE is non-nil,
665 Message is inserted to the summary buffer."
666   (let ((parent (wl-thread-get-entity parent-msg))
667         child-entity invisible-top)
668 ;;; Update the thread view...not implemented yet.
669 ;;;  (when force-insert
670 ;;;    (if parent
671 ;;;       (wl-thread-entity-force-open parent))
672     (if parent
673         ;; insert as children.
674         (wl-thread-entity-insert-as-children
675          parent
676          (setq child-entity (wl-thread-create-entity msg (nth 0 parent) nil linked)))
677       ;; insert as top message.
678       (wl-thread-entity-insert-as-top
679        (wl-thread-create-entity msg nil)))
680     (if update
681         (if (not (setq invisible-top
682                        (wl-thread-entity-parent-invisible-p child-entity)))
683             ;; visible.
684             (progn
685               (wl-summary-update-thread
686                overview-entity
687                overview
688                mark-alist
689                child-entity
690                (elmo-msgdb-overview-get-entity
691                 parent-msg (wl-summary-buffer-msgdb)))
692               (when parent
693                 ;; use thread structure.
694                 (wl-thread-entity-get-nearly-older-brother
695                  child-entity parent))) ; return value
696 ;;;             (wl-thread-entity-get-number
697 ;;;              (wl-thread-entity-get-top-entity parent)))) ; return value;
698 ;;;           (setq beg (point))
699 ;;;           (wl-thread-goto-bottom-of-sub-thread)
700 ;;;           (wl-thread-update-indent-string-region beg (point)))
701           ;; currently invisible.. update closed line.
702           (wl-thread-update-children-number invisible-top)
703           nil))))
704
705 (defun wl-thread-get-parent-list (msgs)
706   (let* ((msgs2 msgs)
707          myself)
708     (while msgs2
709       (setq myself (car msgs2)
710             msgs2 (cdr msgs2))
711       (while (not (eq myself (car msgs2)))
712         (if (wl-thread-descendant-p myself (car msgs2))
713             (setq msgs (delq (car msgs2) msgs)))
714         (setq msgs2 (or (cdr msgs2) msgs)))
715       (setq msgs2 (cdr msgs2)))
716     msgs))
717
718 (defun wl-thread-update-indent-string-thread (top-list)
719   (let ((top-list (wl-thread-get-parent-list top-list))
720         beg)
721     (while top-list
722       (when (car top-list)
723         (wl-summary-jump-to-msg (car top-list))
724         (setq beg (point))
725         (wl-thread-goto-bottom-of-sub-thread)
726         (wl-thread-update-indent-string-region beg (point)))
727       (setq top-list (cdr top-list)))))
728
729 (defun wl-thread-update-children-number (entity)
730   "Update the children number."
731   (save-excursion
732     (wl-summary-jump-to-msg (wl-thread-entity-get-number entity))
733     (beginning-of-line)
734     (let ((text-prop (get-text-property (point) 'face))
735           from from-end beg str)
736       (cond
737        ((looking-at (concat "^" wl-summary-buffer-number-regexp
738                             "..../..\(.*\)..:.. ["
739                             wl-thread-indent-regexp
740                             "]*[[<]\\+\\([0-9]+\\):"))
741         (delete-region (match-beginning 1)(match-end 1))
742         (goto-char (match-beginning 1))
743         (setq str (format "%s" (wl-thread-entity-get-children-num entity)))
744         (if wl-summary-highlight
745             (put-text-property 0 (length str) 'face text-prop str))
746         (insert str))
747        ((looking-at (concat "^" wl-summary-buffer-number-regexp
748                             "..../..\(.*\)..:.. ["
749                             wl-thread-indent-regexp
750                             "]*[[<]"))
751         (goto-char (match-end 0))
752         (setq beg (current-column))
753         (setq from-end (save-excursion
754                          (move-to-column (+ 1 beg wl-from-width))
755                          (point)))
756         (setq from (buffer-substring (match-end 0) from-end))
757         (delete-region (match-end 0) from-end)
758         (setq str (wl-set-string-width
759                    (1+ wl-from-width)
760                    (format
761                     "+%s:%s"
762                     (wl-thread-entity-get-children-num
763                      entity)
764                     from)))
765         (if wl-summary-highlight
766             (put-text-property 0 (length str) 'face text-prop str))
767         (insert str)
768         (condition-case nil ; it's dangerous, so ignore error.
769             (run-hooks 'wl-thread-update-children-number-hook)
770           (error
771            (ding)
772            (message "Error in wl-thread-update-children-number-hook."))))))))
773
774 ;; 
775 ;; Thread oriented commands.
776 ;;
777 (defun wl-thread-call-region-func (func &optional arg)
778   (save-excursion
779     (if arg
780         (wl-summary-goto-top-of-current-thread)
781       (beginning-of-line))
782     (let ((beg (point)))
783       (wl-thread-goto-bottom-of-sub-thread)
784       (funcall func beg (point)))))
785
786 (defun wl-thread-prefetch (&optional arg)
787   (interactive "P")
788   (wl-thread-call-region-func 'wl-summary-prefetch-region arg))
789
790 (defun wl-thread-msg-mark-as-important (msg)
791   "Set mark as important for invisible MSG. Modeline is not changed."
792   (let* ((msgdb (wl-summary-buffer-msgdb))
793          (mark-alist (elmo-msgdb-get-mark-alist msgdb))
794          cur-mark)
795     (setq cur-mark (cadr (assq msg mark-alist)))
796     (setq mark-alist
797           (elmo-msgdb-mark-set mark-alist
798                                msg
799                                (if (string= cur-mark wl-summary-important-mark)
800                                    nil
801                                  wl-summary-important-mark)))
802     (elmo-msgdb-set-mark-alist msgdb mark-alist)
803     (wl-summary-set-mark-modified)))
804
805 (defun wl-thread-mark-as-read (&optional arg)
806   (interactive "P")
807   (wl-thread-call-region-func 'wl-summary-mark-as-read-region arg))
808
809 (defun wl-thread-mark-as-unread (&optional arg)
810   (interactive "P")
811   (wl-thread-call-region-func 'wl-summary-mark-as-unread-region arg))
812
813 (defun wl-thread-mark-as-important (&optional arg)
814   (interactive "P")
815   (wl-thread-call-region-func 'wl-summary-mark-as-important-region arg))
816
817 (defun wl-thread-copy (&optional arg)
818   (interactive "P")
819   (wl-thread-call-region-func 'wl-summary-copy-region arg))
820
821 (defun wl-thread-refile (&optional arg)
822   (interactive "P")
823   (condition-case err
824       (progn
825         (wl-thread-call-region-func 'wl-summary-refile-region arg)
826         (if arg
827             (wl-summary-goto-top-of-current-thread))
828         (wl-thread-goto-bottom-of-sub-thread))
829     (error
830      (elmo-display-error err t)
831      nil)))
832         
833 (defun wl-thread-delete (&optional arg)
834   (interactive "P")
835   (wl-thread-call-region-func 'wl-summary-delete-region arg)
836   (if arg
837       (wl-summary-goto-top-of-current-thread))
838   (if (not wl-summary-move-direction-downward)
839       (wl-summary-prev)
840     (wl-thread-goto-bottom-of-sub-thread)
841     (if wl-summary-buffer-disp-msg
842         (wl-summary-redisplay))))
843
844 (defun wl-thread-target-mark (&optional arg)
845   (interactive "P")
846   (wl-thread-call-region-func 'wl-summary-target-mark-region arg))
847
848 (defun wl-thread-unmark (&optional arg)
849   (interactive "P")
850   (wl-thread-call-region-func 'wl-summary-unmark-region arg))
851
852 (defun wl-thread-exec (&optional arg)
853   (interactive "P")
854   (wl-thread-call-region-func 'wl-summary-exec-region arg))
855
856 (defun wl-thread-save (&optional arg)
857   (interactive "P")
858   (wl-thread-call-region-func 'wl-summary-save-region arg))
859
860 (defun wl-thread-force-open (&optional msg-num)
861   "force open current folder"
862   (if msg-num
863       (wl-summary-jump-to-msg msg-num))
864   (let ((wl-thread-insert-force-opened t))
865     (wl-thread-open-close)))
866
867 (defun wl-thread-entity-force-open (entity)
868   (let ((wl-thread-insert-force-opened t)
869         notopen)
870     (if (null (wl-thread-entity-get-parent entity))
871         ;; top!!
872         (if (and (not (wl-thread-entity-get-opened entity))
873                  (wl-thread-entity-get-children entity))
874             (wl-thread-force-open (wl-thread-entity-get-number entity)))
875       (if (setq notopen (wl-thread-entity-parent-invisible-p entity))
876           (wl-thread-force-open (wl-thread-entity-get-number notopen))))))
877
878 (defun wl-thread-insert-top ()
879   (let ((elist wl-thread-entity-list)
880         (len (length wl-thread-entity-list))
881         (cur 0))
882     (wl-delete-all-overlays)
883     (while elist
884       (wl-thread-insert-entity
885        0
886        (wl-thread-get-entity (car elist))
887        nil
888        len)
889       (setq elist (cdr elist))
890       (when (> len elmo-display-progress-threshold)
891         (setq cur (1+ cur))
892         (if (or (zerop (% cur 2)) (= cur len))
893             (elmo-display-progress
894              'wl-thread-insert-top "Inserting thread..."
895              (/ (* cur 100) len)))))))
896
897 (defsubst wl-thread-insert-entity-sub (indent entity parent-entity all)
898   (let ((mark-alist (elmo-msgdb-get-mark-alist (wl-summary-buffer-msgdb)))
899         msg-num
900         overview-entity
901         temp-mark
902         summary-line)
903     (when (setq msg-num (wl-thread-entity-get-number entity))
904       (unless all ; all...means no temp-mark.
905         (cond ((memq msg-num wl-summary-buffer-delete-list)
906                (setq temp-mark "D"))
907               ((memq msg-num wl-summary-buffer-target-mark-list)
908                (setq temp-mark "*"))
909               ((assq msg-num wl-summary-buffer-refile-list)
910                (setq temp-mark "o"))
911               ((assq msg-num wl-summary-buffer-copy-list)
912                (setq temp-mark "O"))))
913       (unless temp-mark
914         (setq temp-mark (wl-summary-get-score-mark msg-num)))
915       (setq overview-entity
916             (elmo-msgdb-overview-get-entity
917              (nth 0 entity) (wl-summary-buffer-msgdb)))
918 ;;;   (wl-delete-all-overlays)
919       (when overview-entity
920         (setq summary-line
921               (wl-summary-overview-create-summary-line
922                msg-num
923                overview-entity
924                (elmo-msgdb-overview-get-entity
925                 (nth 0 parent-entity) (wl-summary-buffer-msgdb))
926                (1+ indent)
927                mark-alist
928                (if wl-thread-insert-force-opened
929                    nil
930                  (wl-thread-maybe-get-children-num msg-num))
931                temp-mark entity))
932         (wl-summary-insert-line summary-line)))))
933
934 (defun wl-thread-insert-entity (indent entity parent-entity all)
935   "Insert thread entity in current buffer."
936   (let ((msgs (list (car entity)))
937         children msgs-stack)
938     (while msgs
939       (wl-thread-insert-entity-sub indent entity parent-entity all)
940       (setq msgs (cdr msgs))
941       (setq children (nth 2 entity))
942       (if children
943           ;; insert children
944           (when (or wl-thread-insert-force-opened
945                     (wl-thread-entity-get-opened entity))
946             (wl-thread-entity-set-opened entity t)
947             (wl-push msgs msgs-stack)
948             (setq msgs children
949                   indent (1+ indent)
950                   parent-entity entity)))
951       (unless msgs
952         (while (and (null msgs) msgs-stack)
953           (setq msgs (wl-pop msgs-stack))
954           (setq indent (1- indent)))
955         (when msgs
956           (setq entity (wl-thread-get-entity (car msgs)))
957           (setq parent-entity (wl-thread-entity-get-parent-entity entity))))
958       (setq entity (wl-thread-get-entity (car msgs))))))
959
960 (defun wl-thread-descendant-p (mynumber number)
961   (let ((cur (wl-thread-get-entity number))
962         num)
963     (catch 'done
964       (while cur
965         (setq cur (wl-thread-entity-get-parent-entity cur))
966         (if (null (setq num (wl-thread-entity-get-number cur))) ; top!
967             (throw 'done nil))
968         (if (and num
969                  (eq mynumber (wl-thread-entity-get-number cur)))
970             (throw 'done t)))
971       nil)))
972
973 ;; (defun wl-thread-goto-bottom-of-sub-thread ()
974 ;;   (interactive)
975 ;;   (let ((depth (wl-thread-get-depth-of-current-line)))
976 ;;     (forward-line 1)
977 ;;     (while (and (not (eobp))
978 ;;              (> (wl-thread-get-depth-of-current-line)
979 ;;                 depth))
980 ;;       (forward-line 1))
981 ;;     (beginning-of-line)))
982
983 (defun wl-thread-goto-bottom-of-sub-thread (&optional msg)
984   (interactive)
985   (let ((mynumber (or msg (wl-summary-message-number))))
986     (forward-line 1)
987     (while (wl-thread-descendant-p mynumber (wl-summary-message-number))
988       (forward-line 1))
989     (beginning-of-line)))
990
991 (defun wl-thread-remove-destination-region (beg end)
992   (save-excursion
993     (save-restriction
994       (narrow-to-region beg end)
995       (goto-char (point-min))
996       (while (not (eobp))
997         (let ((num (wl-summary-message-number)))
998           (if (assq num wl-summary-buffer-refile-list)
999               (wl-summary-remove-destination)))
1000         (forward-line 1)))))
1001
1002 (defun wl-thread-print-destination-region (beg end)
1003   (if (or wl-summary-buffer-refile-list
1004           wl-summary-buffer-copy-list)
1005       (save-excursion
1006         (save-restriction
1007           (narrow-to-region beg end)
1008           (goto-char (point-min))
1009           (while (not (eobp))
1010             (let ((num (wl-summary-message-number))
1011                   pair)
1012               (if (or (setq pair (assq num wl-summary-buffer-refile-list))
1013                       (setq pair (assq num wl-summary-buffer-copy-list)))
1014                   (wl-summary-print-destination (car pair) (cdr pair))))
1015             (forward-line 1))))))
1016
1017 (defsubst wl-thread-get-children-msgs (msg &optional visible-only)
1018   (let ((msgs (list msg))
1019         msgs-stack children
1020         entity ret-val)
1021     (while msgs
1022       (wl-append ret-val (list (car msgs)))
1023       (setq children (wl-thread-entity-get-children
1024                       (setq entity (wl-thread-get-entity (car msgs)))))
1025       (if (and visible-only
1026                (not (wl-thread-entity-get-opened entity)))
1027           (setq children nil))
1028       (setq msgs (cdr msgs))
1029       (if (null children)
1030           (while (and (null msgs) msgs-stack)
1031             (setq msgs (wl-pop msgs-stack)))
1032         (wl-push msgs msgs-stack)
1033         (setq msgs children)))
1034     ret-val))
1035
1036 (defun wl-thread-get-children-msgs-uncached (msg &optional uncached-marks)
1037   (let ((children-msgs (wl-thread-get-children-msgs msg))
1038         (mark-alist (elmo-msgdb-get-mark-alist (wl-summary-buffer-msgdb)))
1039         (number-alist (elmo-msgdb-get-number-alist (wl-summary-buffer-msgdb)))
1040         mark
1041         uncached-list)
1042     (while children-msgs
1043       (if (and (not (eq msg (car children-msgs))) ; except itself
1044                (or (and uncached-marks
1045                         (setq mark (cadr (assq (car children-msgs)
1046                                                mark-alist)))
1047                         (member mark uncached-marks))
1048                    (and (not uncached-marks)
1049                         (null (elmo-file-cache-exists-p
1050                                (elmo-message-field
1051                                 wl-summary-buffer-elmo-folder
1052                                 (car children-msgs)
1053                                 'message-id))))))
1054           (wl-append uncached-list (list (car children-msgs))))
1055       (setq children-msgs (cdr children-msgs)))
1056     uncached-list))
1057
1058 (defun wl-thread-get-children-msgs-with-mark (msg mark)
1059   (let ((children-msgs (wl-thread-get-children-msgs msg))
1060         (check-func (cond ((string= mark "o")
1061                            'wl-summary-msg-marked-as-refiled)
1062                           ((string= mark "O")
1063                            'wl-summary-msg-marked-as-copied)
1064                           ((string= mark "D")
1065                            'wl-summary-msg-marked-as-deleted)
1066                           ((string= mark "*")
1067                            'wl-summary-msg-marked-as-target)))
1068         ret-val)
1069     (while children-msgs
1070       (if (funcall check-func (car children-msgs))
1071           (wl-append ret-val (list (car children-msgs))))
1072       (setq children-msgs (cdr children-msgs)))
1073     ret-val))
1074
1075 (defun wl-thread-close (entity)
1076   (let (depth beg)
1077     (wl-thread-entity-set-opened entity nil)
1078     (setq depth (wl-thread-get-depth-of-current-line))
1079     (beginning-of-line)
1080     (setq beg (point))
1081     (wl-thread-goto-bottom-of-sub-thread)
1082     (wl-thread-remove-destination-region beg
1083                                          (point))
1084     (forward-char -1)   ;; needed for mouse-face.
1085     (delete-region beg (point))
1086     (wl-thread-insert-entity (- depth 1)
1087                              entity
1088                              (wl-thread-get-entity
1089                               (nth 3 entity))
1090                              nil)
1091     (delete-char 1) ; delete '\n'
1092     (wl-thread-print-destination-region beg (point))))
1093
1094 (defun wl-thread-open (entity)
1095   (let (depth beg)
1096     (beginning-of-line)
1097     (setq beg (point))
1098     (setq depth (wl-thread-get-depth-of-current-line))
1099     (end-of-line)
1100     (delete-region beg (point))
1101     (wl-thread-entity-set-opened entity t)
1102     (wl-thread-insert-entity depth ;(- depth 1)
1103                              entity
1104                              (wl-thread-get-entity
1105                               (nth 3 entity)) nil)
1106     (delete-char 1) ; delete '\n'
1107     (wl-thread-print-destination-region beg (point))))
1108
1109 (defun wl-thread-open-close (&optional force-open)
1110   (interactive "P")
1111   (when (eq wl-summary-buffer-view 'thread)
1112 ;;; (if (equal wl-thread-top-entity '(nil t nil nil))
1113 ;;;     (error "There's no thread structure"))
1114     (save-excursion
1115       (let ((inhibit-read-only t)
1116             (buffer-read-only nil)
1117             (wl-thread-insert-force-opened
1118              (or wl-thread-insert-force-opened
1119                  force-open))
1120             msg entity parent)
1121         (setq msg (wl-summary-message-number))
1122         (setq entity (wl-thread-get-entity msg))
1123         (if (wl-thread-entity-get-opened entity)
1124             ;; if already opened, close its child!
1125           (if (wl-thread-entity-get-children entity)
1126               (wl-thread-close entity)
1127             ;; opened, but has no children, close its parent!
1128             (when (setq parent (wl-thread-entity-get-parent entity))
1129               (wl-summary-jump-to-msg parent)
1130               (wl-thread-close
1131                (wl-thread-get-entity (wl-summary-message-number)))))
1132           ;; if closed (or it is just a thread bottom message)
1133           ;; has children, open it!
1134           (if (wl-thread-entity-get-children entity)
1135               (wl-thread-open entity)
1136             ;; closed, and has no children, close its parent!
1137             (setq msg (or (wl-thread-entity-get-parent entity)
1138                           (wl-thread-entity-get-number entity)))
1139             (when msg
1140               (wl-summary-jump-to-msg msg)
1141               (wl-thread-close
1142                (wl-thread-get-entity (wl-summary-message-number)))))))
1143       (wl-summary-set-message-modified)
1144       (set-buffer-modified-p nil))))
1145   
1146
1147 (defun wl-thread-get-depth-of-current-line ()
1148   (interactive)
1149   (save-excursion
1150     (beginning-of-line)
1151     (let ((depth 0))
1152       (if (re-search-forward (concat "^" wl-summary-buffer-number-regexp
1153                                      "..../..\(.*\)..:.. ")
1154                              nil t)
1155           (while (string-match wl-thread-indent-regexp
1156                                (char-to-string
1157                                 (char-after (point))))
1158             (setq depth (1+ depth))
1159             (forward-char)))
1160       (/ depth wl-thread-indent-level-internal))))
1161
1162 (defun wl-thread-update-indent-string-region (beg end)
1163   (interactive "r")
1164   (save-excursion
1165     (goto-char beg)
1166     (while (< (point) end)
1167       (wl-thread-update-indent-string)
1168       (forward-line 1))))
1169
1170 (defsubst wl-thread-make-indent-string (entity)
1171   (let ((cur entity)
1172         (ret-val "")
1173         (space-str (wl-repeat-string wl-thread-space-str-internal
1174                                      (- wl-thread-indent-level-internal 1)))
1175         parent)
1176     (when (wl-thread-entity-get-number
1177            (setq parent (wl-thread-entity-get-parent-entity cur)))
1178       (if (wl-thread-entity-get-younger-brothers cur)
1179           (setq ret-val wl-thread-have-younger-brother-str-internal)
1180         (setq ret-val wl-thread-youngest-child-str-internal))
1181       (setq ret-val (concat ret-val
1182                             (wl-repeat-string
1183                              wl-thread-horizontal-str-internal
1184                              (- wl-thread-indent-level-internal 1))))
1185       (setq cur parent)
1186       (while (wl-thread-entity-get-number
1187               (wl-thread-entity-get-parent-entity cur))
1188         (if (wl-thread-entity-get-younger-brothers cur)
1189             (setq ret-val (concat wl-thread-vertical-str-internal
1190                                   space-str
1191                                   ret-val))
1192           (setq ret-val (concat wl-thread-space-str-internal
1193                                 space-str
1194                                 ret-val)))
1195         (setq cur (wl-thread-entity-get-parent-entity cur))))
1196     ret-val))
1197
1198 (defun wl-thread-update-indent-string ()
1199   "Update indent string of current line."
1200   (interactive)
1201   (save-excursion
1202     (beginning-of-line)
1203     (let ((inhibit-read-only t)
1204           (buffer-read-only nil)
1205           thr-str)
1206       (when (looking-at (concat "^ *\\([0-9]+\\)"
1207                                 "..../..\(.*\)..:.. \\("
1208                                 wl-highlight-thread-indent-string-regexp
1209                                 "\\)[[<]"))
1210         (goto-char (match-beginning 2))
1211         (delete-region (match-beginning 2)
1212                        (match-end 2))
1213         (setq thr-str
1214               (wl-thread-make-indent-string
1215                (wl-thread-get-entity (string-to-int (wl-match-buffer 1)))))
1216         (if (and wl-summary-width
1217                  wl-summary-indent-length-limit
1218                  (< wl-summary-indent-length-limit
1219                     (string-width thr-str)))
1220             (setq thr-str (wl-set-string-width
1221                            wl-summary-indent-length-limit
1222                            thr-str)))
1223         (insert thr-str)
1224         (if wl-summary-highlight
1225             (wl-highlight-summary-current-line))))))
1226
1227 (defun wl-thread-set-parent (&optional parent-number)
1228   "Set current message's parent interactively."
1229   (interactive)
1230   (let ((number (wl-summary-message-number))
1231         (dst-parent (if (interactive-p)
1232                         (read-from-minibuffer "Parent Message (No.): ")))
1233         entity dst-parent-entity src-parent children
1234         update-msgs
1235         buffer-read-only)
1236     (if (string= dst-parent "")
1237         (setq dst-parent nil)
1238       (if (interactive-p)
1239           (setq dst-parent (string-to-int dst-parent))
1240         (setq dst-parent parent-number)))
1241     (if (and dst-parent
1242              (memq dst-parent (wl-thread-get-children-msgs number)))
1243         (error "Parent is children or myself"))
1244     (setq entity (wl-thread-get-entity number))
1245     (when (and number entity)
1246       ;; delete thread
1247       (setq update-msgs (wl-thread-delete-message number 'deep))
1248       ;; insert as child at new parent
1249       (setq dst-parent-entity (wl-thread-get-entity dst-parent))
1250       (if dst-parent-entity
1251           (progn
1252             (if (setq children
1253                       (wl-thread-entity-get-children dst-parent-entity))
1254                 (wl-append update-msgs
1255                            (wl-thread-get-children-msgs
1256                             (car (last children)) t)))
1257             (wl-thread-entity-set-children
1258              dst-parent-entity
1259              (append children (list number)))
1260             (wl-thread-entity-set-linked entity t))
1261         ;; insert as top
1262         (wl-append wl-thread-entity-list (list number))
1263         (wl-thread-entity-set-linked entity nil))
1264
1265       ;; update my thread
1266       (wl-append update-msgs (wl-thread-get-children-msgs number t))
1267       (setq update-msgs (elmo-uniq-list update-msgs))
1268       (wl-thread-entity-set-parent entity dst-parent)
1269       ;; update thread on buffer
1270       (wl-thread-update-line-msgs update-msgs t))))
1271
1272 (require 'product)
1273 (product-provide (provide 'wl-thread) (require 'wl-version))
1274
1275 ;;; wl-thread.el ends here