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