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