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