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