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