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