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