1 ;;; wl-thread.el -- Thread display modules for Wanderlust.
3 ;; Copyright 1998,1999,2000 Yuuichi Teranishi <teranisi@gohome.org>
5 ;; Author: Yuuichi Teranishi <teranisi@gohome.org>
6 ;; Keywords: mail, net news
7 ;; Time-stamp: <00/04/28 09:49:12 teranisi>
9 ;; This file is part of Wanderlust (Yet Another Message Interface on Emacsen).
11 ;; This program is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; This program is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
34 (require 'wl-highlight)
36 ;; buffer local variables.
37 ;(defvar wl-thread-top-entity '(nil t nil nil)) ; top entity
38 (defvar wl-thread-tops nil) ; top number list (number)
39 (defvar wl-thread-entities nil)
40 (defvar wl-thread-entity-list nil) ; entity list
41 (defvar wl-thread-entity-hashtb nil) ; obarray
42 (defvar wl-thread-indent-regexp nil)
45 (function make-variable-buffer-local)
46 (list 'wl-thread-entity-hashtb
47 'wl-thread-entities ; -> ".wl-thread-entity"
48 'wl-thread-entity-list ; -> ".wl-thread-entity-list"
50 'wl-thread-indent-regexp))
53 (defvar wl-thread-insert-force-opened nil)
55 ;;;;;; each entity is (number opened-or-not children parent) ;;;;;;;
57 (defun wl-meaning-of-mark (mark)
58 (if (not (elmo-folder-plugged-p wl-summary-buffer-folder-name))
60 ((string= mark wl-summary-unread-cached-mark)
62 ((string= mark wl-summary-important-mark)
65 ((string= mark wl-summary-new-mark)
67 ((or (string= mark wl-summary-unread-uncached-mark)
68 (string= mark wl-summary-unread-cached-mark))
70 ((string= mark wl-summary-important-mark)
73 (defun wl-thread-next-mark-p (mark next)
74 (cond ((not (elmo-folder-plugged-p wl-summary-buffer-folder-name))
75 (or (string= mark wl-summary-unread-cached-mark)
76 (string= mark wl-summary-important-mark)))
78 (string= mark wl-summary-new-mark))
80 (or (string= mark wl-summary-unread-uncached-mark)
81 (string= mark wl-summary-unread-cached-mark)
82 (string= mark wl-summary-new-mark)))
84 (or (string= mark wl-summary-unread-uncached-mark)
85 (string= mark wl-summary-unread-cached-mark)
86 (string= mark wl-summary-new-mark)
87 (string= mark wl-summary-important-mark)))))
89 (defun wl-thread-next-failure-mark-p (mark next)
90 (cond ((not (elmo-folder-plugged-p wl-summary-buffer-folder-name))
91 (string= mark wl-summary-unread-cached-mark))
94 (or (string= mark wl-summary-unread-uncached-mark)
95 (string= mark wl-summary-unread-cached-mark)
96 (string= mark wl-summary-new-mark)
97 (string= mark wl-summary-important-mark)))
100 (defun wl-thread-resume-entity (fld)
101 (let (entities top-list)
102 (setq entities (wl-summary-load-file-object
103 (expand-file-name wl-thread-entity-file
104 (elmo-msgdb-expand-path fld))))
106 (wl-summary-load-file-object
107 (expand-file-name wl-thread-entity-list-file
108 (elmo-msgdb-expand-path fld))))
110 (message "Resuming thread structure...")
111 ;; set obarray value.
112 (setq wl-thread-entity-hashtb (elmo-make-hash (* (length entities) 2)))
115 (elmo-set-hash-val (format "#%d" (car x))
117 wl-thread-entity-hashtb))
119 ;; set buffer local variables.
120 (setq wl-thread-entities entities)
121 (setq wl-thread-entity-list top-list)
122 (message "Resuming thread structure...done.")))
124 (defun wl-thread-save-entity (dir)
125 (wl-thread-save-entities dir)
126 (wl-thread-save-top-list dir))
128 (defun wl-thread-save-top-list (dir)
129 (let ((top-file (expand-file-name wl-thread-entity-list-file dir))
130 (entity wl-thread-entity-list)
131 (tmp-buffer (get-buffer-create " *wl-thread-save-top-list*")))
133 (set-buffer tmp-buffer)
135 (when (file-writable-p top-file)
136 (prin1 entity tmp-buffer)
137 (princ "\n" tmp-buffer)
138 (write-region (point-min) (point-max) top-file nil 'no-msg)
139 (kill-buffer tmp-buffer)))))
141 (defun wl-thread-save-entities (dir)
142 (let ((top-file (expand-file-name wl-thread-entity-file dir))
143 (entities wl-thread-entities)
144 (tmp-buffer (get-buffer-create " *wl-thread-save-entities*")))
146 (set-buffer tmp-buffer)
148 (when (file-writable-p top-file)
149 (prin1 entities tmp-buffer)
150 (princ "\n" tmp-buffer)
151 (write-region (point-min) (point-max) top-file nil 'no-msg)
152 (kill-buffer tmp-buffer)))))
154 (defsubst wl-thread-entity-get-number (entity)
156 (defsubst wl-thread-entity-get-opened (entity)
158 (defsubst wl-thread-entity-get-children (entity)
160 (defsubst wl-thread-entity-get-parent (entity)
163 (defsubst wl-thread-create-entity (num parent &optional opened)
164 (list num (or opened wl-thread-insert-opened) nil parent))
166 (defsubst wl-thread-get-entity (num)
168 (boundp (intern (format "#%d" num) wl-thread-entity-hashtb))
169 (elmo-get-hash-val (format "#%d" num) wl-thread-entity-hashtb)))
171 (defsubst wl-thread-entity-set-parent (entity parent)
172 (setcar (cdddr entity) parent)
175 (defsubst wl-thread-entity-set-children (entity children)
176 (setcar (cddr entity) children))
178 (defsubst wl-thread-entity-insert-as-top (entity)
181 (setq wl-thread-entity-list (append wl-thread-entity-list
182 (list (car entity))))
183 (setq wl-thread-entities (cons entity wl-thread-entities))
184 (elmo-set-hash-val (format "#%d" (car entity)) entity
185 wl-thread-entity-hashtb)))
187 (defsubst wl-thread-entity-insert-as-children (to entity)
188 (let ((children (nth 2 to)))
189 (setcar (cddr to) (wl-append children
190 (list (car entity))))
191 (setq wl-thread-entities (cons entity wl-thread-entities))
192 (elmo-set-hash-val (format "#%d" (car entity)) entity
193 wl-thread-entity-hashtb)))
195 (defsubst wl-thread-entity-set-opened (entity opened)
196 (setcar (cdr entity) opened))
198 (defsubst wl-thread-entity-get-children-num (entity)
201 (msgs (list (car entity))))
203 (setq msgs (cdr msgs))
204 (setq children (wl-thread-entity-get-children entity))
206 (while (and (null msgs) msgs-stack)
207 (setq msgs (wl-pop msgs-stack)))
208 (setq ret-val (+ (or ret-val 0) (length children)))
209 (wl-push msgs msgs-stack)
210 (setq msgs children))
211 (setq entity (wl-thread-get-entity (car msgs))))
214 (defsubst wl-thread-entity-get-descendant (entity)
217 (msgs (list (car entity))))
219 (setq msgs (cdr msgs))
220 (setq children (wl-thread-entity-get-children entity))
222 (while (and (null msgs) msgs-stack)
223 (setq msgs (wl-pop msgs-stack)))
224 (setq ret-val (append ret-val (copy-sequence children)))
225 (wl-push msgs msgs-stack)
226 (setq msgs children))
227 (setq entity (wl-thread-get-entity (car msgs))))
230 (defsubst wl-thread-entity-get-parent-entity (entity)
231 (wl-thread-get-entity (wl-thread-entity-get-parent entity)))
233 (defun wl-thread-entity-get-top-entity (entity)
234 (let ((cur-entity entity)
236 (while (setq p-num (wl-thread-entity-get-parent cur-entity))
237 (setq cur-entity (wl-thread-get-entity p-num)))
240 (defun wl-thread-entity-parent-invisible-p (entity)
241 "If parent of ENTITY is invisible, the top invisible ancestor entity of
243 (let ((cur-entity entity)
246 (while (setq cur-entity (wl-thread-entity-get-parent-entity
248 (if (null (wl-thread-entity-get-number cur-entity))
253 (when (not (wl-thread-entity-get-opened cur-entity))
255 (setq ret-val cur-entity)))))
256 ;; top of closed entity in the path.
259 (defun wl-thread-entity-get-mark (number)
260 (let ((mark-alist (elmo-msgdb-get-mark-alist wl-summary-buffer-msgdb))
262 (setq mark (cadr (assq number mark-alist)))
263 (if (string= mark wl-summary-read-uncached-mark)
267 (defun wl-thread-meaning-alist-get-result (meaning-alist)
268 (let ((malist meaning-alist)
272 (if (setq ret-val (cdr (car malist)))
273 (throw 'done ret-val))
274 (setq malist (cdr malist))))))
276 (defun wl-thread-entity-check-prev-mark (entity prev-marks)
277 "Check prev mark. Result is stored in PREV-MARK."
278 (let ((msgs (list (car entity)))
279 (succeed-list (car prev-marks))
280 (failure-list (cdr prev-marks))
282 mark meaning success failure parents)
285 (if (and (not (memq (car msgs) parents))
286 (setq children (reverse (wl-thread-entity-get-children entity))))
288 (wl-append parents (list (car msgs)))
289 (wl-push msgs msgs-stack)
290 (setq msgs children))
291 (if (setq mark (wl-thread-entity-get-mark (car entity)))
292 (if (setq meaning (wl-meaning-of-mark mark))
293 (if (setq success (assq meaning succeed-list))
295 (setcdr success entity)
297 (setq failure (assq meaning failure-list))
298 (unless (cdr failure)
299 (setcdr (assq meaning failure-list) entity)))))
300 (setq msgs (cdr msgs)))
302 (while (and (null msgs) msgs-stack)
303 (setq msgs (wl-pop msgs-stack))))
304 (setq entity (wl-thread-get-entity (car msgs)))))))
306 (defun wl-thread-entity-check-next-mark (entity next-marks)
307 "Check next mark. Result is stored in NEXT-MARK."
308 (let ((msgs (list (car entity)))
309 (succeed-list (car next-marks))
310 (failure-list (cdr next-marks))
312 mark meaning success failure)
315 (if (setq mark (wl-thread-entity-get-mark (car entity)))
316 (if (setq meaning (wl-meaning-of-mark mark))
317 (if (setq success (assq meaning succeed-list))
319 (setcdr success entity)
321 (setq failure (assq meaning failure-list))
322 (unless (cdr failure)
323 (setcdr (assq meaning failure-list) entity)))))
324 (setq msgs (cdr msgs))
325 (setq children (wl-thread-entity-get-children entity))
328 (wl-push msgs msgs-stack)
329 (setq msgs children))
331 (while (and (null msgs) msgs-stack)
332 (setq msgs (wl-pop msgs-stack)))))
333 (setq entity (wl-thread-get-entity (car msgs)))))))
335 (defun wl-thread-entity-get-older-brothers (entity &optional parent)
336 (let* ((parent (or parent
337 (wl-thread-entity-get-parent-entity entity)))
338 (brothers (wl-thread-entity-get-children parent))
342 (setq brothers wl-thread-entity-list))
345 (if (not (eq (wl-thread-entity-get-number entity)
347 (wl-append ret-val (list (car brothers)))
348 (throw 'done ret-val))
349 (setq brothers (cdr brothers))))))
351 (defun wl-thread-entity-get-younger-brothers (entity &optional parent)
352 (let* ((parent (or parent
353 (wl-thread-entity-get-parent-entity entity)))
354 (brothers (wl-thread-entity-get-children parent)))
356 (cdr (memq (wl-thread-entity-get-number entity)
359 (cdr (memq (car entity) wl-thread-entity-list)))))
361 (defun wl-thread-entity-check-prev-mark-from-older-brother (entity prev-marks)
362 (let* (older-brother parent)
366 (reverse (wl-thread-entity-get-older-brothers entity)))
368 (let ((succeed-list (car prev-marks))
369 (failure-list (cdr prev-marks))
370 mark meaning success failure)
371 (if (setq mark (wl-thread-entity-get-mark (car entity)))
372 (if (setq meaning (wl-meaning-of-mark mark))
373 (if (setq success (assq meaning succeed-list))
375 (setcdr success entity)
377 (setq failure (assq meaning failure-list))
378 (unless (cdr failure)
379 (setcdr (assq meaning failure-list) entity))))))
380 ;; check older brothers
382 (wl-thread-entity-check-prev-mark (wl-thread-get-entity
385 (if (wl-thread-meaning-alist-get-result
388 (setq older-brother (cdr older-brother)))
389 (setq entity (wl-thread-entity-get-parent-entity entity))))))
391 (defun wl-thread-entity-get-prev-marked-entity (entity prev-marks)
392 (let ((older-brothers (reverse
393 (wl-thread-entity-get-older-brothers entity)))
396 (while older-brothers
397 (wl-thread-entity-check-prev-mark
398 (wl-thread-get-entity (car older-brothers)) prev-marks)
400 (wl-thread-meaning-alist-get-result
402 (throw 'done marked))
403 (setq older-brothers (cdr older-brothers))))
404 (wl-thread-entity-check-prev-mark-from-older-brother
405 (wl-thread-entity-get-parent-entity entity) prev-marks)
407 (wl-thread-meaning-alist-get-result
411 (wl-thread-meaning-alist-get-result
415 (defun wl-thread-get-prev-unread (msg &optional hereto)
416 (let ((cur-entity (wl-thread-get-entity msg))
417 (prev-marks (cond ((eq wl-summary-move-order 'new)
418 (cons (list (cons 'new nil))
419 (list (cons 'unread nil)
420 (cons 'important nil))))
421 ((eq wl-summary-move-order 'unread)
422 (cons (list (cons 'unread nil)
424 (list (cons 'important nil))))
426 (cons (list (cons 'unread nil)
428 (cons 'important nil))
432 (when (wl-thread-next-mark-p (setq mark
433 (wl-thread-entity-get-mark
436 ;;(setq mark (cons cur-entity
437 ;;(wl-thread-entity-get-mark cur-entity)))
439 (when (and (not ret-val)
441 (wl-thread-entity-get-prev-marked-entity
442 cur-entity prev-marks))
446 (let ((success-list (car prev-marks)))
448 (if (cdr (car success-list))
450 (setq success-list (cdr success-list)))
452 (wl-thread-next-failure-mark-p mark (caaar prev-marks)))
455 (setq ret-val (car cur-entity)))))
458 (defun wl-thread-jump-to-prev-unread (&optional hereto)
459 "If prev unread is a children of a closed message,
460 the closed parent will be opened."
462 (let ((msg (wl-thread-get-prev-unread
463 (wl-summary-message-number) hereto)))
465 (wl-thread-entity-force-open (wl-thread-get-entity msg))
466 (wl-summary-jump-to-msg msg)
469 (defun wl-thread-jump-to-msg (&optional number)
471 (let ((num (or number
473 (read-from-minibuffer "Jump to Message(No.): ")))))
474 (wl-thread-entity-force-open (wl-thread-get-entity num))
475 (wl-summary-jump-to-msg num)))
477 (defun wl-thread-get-next-unread (msg &optional hereto)
478 (let ((cur-entity (wl-thread-get-entity msg))
479 (next-marks (cond ((not (elmo-folder-plugged-p
480 wl-summary-buffer-folder-name))
481 (cons (list (cons 'unread nil))
482 (list (cons 'important nil))))
483 ((eq wl-summary-move-order 'new)
484 (cons (list (cons 'new nil))
485 (list (cons 'unread nil)
486 (cons 'important nil))))
487 ((eq wl-summary-move-order 'unread)
488 (cons (list (cons 'unread nil)
490 (list (cons 'important nil))))
492 (cons (list (cons 'unread nil)
494 (cons 'important nil))
498 (when (wl-thread-next-mark-p (setq mark
499 (wl-thread-entity-get-mark
503 (when (and (not ret-val)
505 (wl-thread-entity-get-next-marked-entity
506 cur-entity next-marks))
509 ;; all success-list is nil
511 (let ((success-list (car next-marks)))
513 (if (cdr (car success-list))
515 (setq success-list (cdr success-list)))
517 (wl-thread-next-failure-mark-p mark (caaar next-marks)))
520 (setq ret-val (car cur-entity)))))
523 (defun wl-thread-jump-to-next-unread (&optional hereto)
524 "If next unread is a children of a closed message,
525 the closed parent will be opened."
527 (let ((msg (wl-thread-get-next-unread
528 (wl-summary-message-number) hereto)))
530 (wl-thread-entity-force-open (wl-thread-get-entity msg))
531 (wl-summary-jump-to-msg msg)
534 (defun wl-thread-close-all ()
535 "Close all top threads."
537 (message "Closing all threads...")
538 (let ((entities wl-thread-entity-list)
540 (len (length wl-thread-entity-list)))
542 (when (and (wl-thread-entity-get-opened (wl-thread-get-entity
544 (wl-thread-entity-get-children (wl-thread-get-entity
546 (wl-summary-jump-to-msg (car entities))
547 (wl-thread-open-close)
549 (elmo-display-progress
550 'wl-thread-close-all "Closing all threads..."
551 (/ (* cur 100) len)))
552 (setq entities (cdr entities))))
553 (elmo-display-progress 'wl-thread-close-all
556 (message "Closing all threads...done.")
557 (goto-char (point-max)))
559 (defun wl-thread-open-all ()
562 (message "Opening all threads...")
563 (let ((entities wl-thread-entity-list)
565 (len (length wl-thread-entity-list)))
567 (if (not (wl-thread-entity-get-opened (wl-thread-get-entity
569 (wl-thread-entity-force-open (wl-thread-get-entity
572 (elmo-display-progress
573 'wl-thread-open-all "Opening all threads..."
575 (setq entities (cdr entities))))
576 (message "Opening all threads...done.")
577 (goto-char (point-max)))
579 (defun wl-thread-open-all-unread ()
581 (let ((mark-alist (elmo-msgdb-get-mark-alist wl-summary-buffer-msgdb))
584 (if (setq mark (nth 1 (car mark-alist)))
585 (if (or (string= mark wl-summary-unread-uncached-mark)
586 (string= mark wl-summary-unread-cached-mark)
587 (string= mark wl-summary-new-mark)
588 (string= mark wl-summary-important-mark))
589 (wl-thread-entity-force-open (wl-thread-get-entity
590 (nth 0 (car mark-alist))))))
591 (setq mark-alist (cdr mark-alist)))))
593 ;;; a subroutine for wl-thread-entity-get-next-marked-entity.
594 (defun wl-thread-entity-check-next-mark-from-younger-brother
596 (let* (parent younger-brother)
599 (setq parent (wl-thread-entity-get-parent-entity entity)
601 (wl-thread-entity-get-younger-brothers entity parent))
603 (while younger-brother
604 (wl-thread-entity-check-next-mark
605 (wl-thread-get-entity (car younger-brother))
607 (if (wl-thread-meaning-alist-get-result
610 (setq younger-brother (cdr younger-brother)))
611 (setq entity parent)))))
613 (defun wl-thread-entity-get-next-marked-entity (entity next-marks)
614 (let ((children (wl-thread-entity-get-children entity))
618 (wl-thread-entity-check-next-mark
619 (wl-thread-get-entity (car children)) next-marks)
621 (wl-thread-meaning-alist-get-result
623 (throw 'done marked))
624 (setq children (cdr children))))
625 ;; check younger brother
626 (wl-thread-entity-check-next-mark-from-younger-brother
629 (wl-thread-meaning-alist-get-result
633 (wl-thread-meaning-alist-get-result
637 (defun wl-thread-update-line-msgs (msgs)
638 (wl-delete-all-overlays)
641 (wl-thread-update-line-on-buffer (car msgs) nil msgs))))
643 (defsubst wl-thread-update-line-on-buffer-sub (entity &optional msg parent-msg)
644 (let ((number-alist (elmo-msgdb-get-number-alist wl-summary-buffer-msgdb))
645 (overview (elmo-msgdb-get-overview wl-summary-buffer-msgdb))
646 (mark-alist (elmo-msgdb-get-mark-alist wl-summary-buffer-msgdb))
647 (buffer-read-only nil)
648 (inhibit-read-only t)
649 ;;(parent-msg parent-msg)
654 (if (memq msg wl-summary-buffer-delete-list)
655 (setq temp-mark "D"))
656 (if (memq msg wl-summary-buffer-target-mark-list)
657 (setq temp-mark "*"))
658 (if (assq msg wl-summary-buffer-refile-list)
659 (setq temp-mark "o"))
660 (if (assq msg wl-summary-buffer-copy-list)
661 (setq temp-mark "O"))
663 (setq temp-mark (wl-summary-get-score-mark msg)))
664 ;(setq parent-entity (wl-thread-entity-get-parent-entity entity))
666 (setq parent-msg (wl-thread-entity-get-parent entity)))
667 ;;(setq children (wl-thread-entity-get-children entity))
668 (setq children-num (wl-thread-entity-get-children-num entity))
669 (setq overview-entity
670 (elmo-msgdb-search-overview-entity msg
671 number-alist overview))
672 ;;(wl-delete-all-overlays)
673 (when overview-entity
675 (wl-summary-overview-create-summary-line
678 (assoc ; parent-entity
679 (cdr (assq parent-msg
680 number-alist)) overview)
683 (if wl-thread-insert-force-opened
685 (if (not (wl-thread-entity-get-opened entity))
688 (wl-summary-insert-line summary-line))))
690 (defun wl-thread-update-line-on-buffer (&optional msg parent-msg updates)
692 (let ((msgs (list (or msg (wl-summary-message-number))))
693 entity children msgs-stack)
695 (setq msg (wl-pop msgs))
696 (setq updates (and updates (delete msg updates)))
697 (when (wl-thread-delete-line-from-buffer msg)
698 (setq entity (wl-thread-get-entity msg))
699 (wl-thread-update-line-on-buffer-sub entity msg parent-msg)
701 (setq children (wl-thread-entity-get-children entity))
704 (when (wl-thread-entity-get-opened entity)
705 (wl-push msgs msgs-stack)
709 (while (and (null msgs) msgs-stack)
710 (setq msgs (wl-pop msgs-stack)))
713 (wl-thread-entity-get-number
714 (wl-thread-entity-get-parent-entity
715 (wl-thread-get-entity (car msgs))))))))))
718 (defun wl-thread-delete-line-from-buffer (msg)
719 "Simply delete msg line."
721 (if (wl-summary-jump-to-msg msg)
725 (delete-region beg (point))
729 (defun wl-thread-cleanup-symbols (msgs)
733 (when (boundp (setq sym (intern (format "#%d" (car msgs))
734 wl-thread-entity-hashtb)))
736 (setq wl-thread-entities
737 (delq (wl-thread-get-entity (car msgs))
740 (setq msgs (cdr msgs)))))
742 (defun wl-thread-delete-message (msg &optional update)
743 "Delete MSG from entity and buffer."
745 (let* ((entity (wl-thread-get-entity msg))
747 older-brothers younger-brothers ;;brothers
750 (setq parent (wl-thread-entity-get-parent-entity entity))
754 ;;(setq brothers (wl-thread-entity-get-children parent))
755 (setq older-brothers (wl-thread-entity-get-older-brothers
757 (setq younger-brothers (wl-thread-entity-get-younger-brothers
760 (setq children (wl-thread-entity-get-children entity))
762 (wl-thread-entity-set-parent
763 (wl-thread-get-entity x)
764 (wl-thread-entity-get-number parent)))
766 (wl-thread-entity-set-children
773 ;; top...children becomes top.
775 (wl-thread-entity-set-parent (wl-thread-get-entity x)
777 (setq children (wl-thread-entity-get-children entity)))
778 ;; delete myself from top list.
779 (setq older-brothers (wl-thread-entity-get-older-brothers
781 (setq younger-brothers (wl-thread-entity-get-younger-brothers
783 (setq wl-thread-entity-list
784 (append (append older-brothers children)
787 ;; delete myself from buffer.
788 (unless (wl-thread-delete-line-from-buffer msg)
789 ;; jump to suitable point.
790 ;; just upon the oldest younger-brother of my top.
791 (let ((younger-bros (wl-thread-entity-get-younger-brothers
792 (wl-thread-entity-get-top-entity entity)
795 (wl-summary-jump-to-msg (car younger-bros))
796 (goto-char (point-max)))) ; no younger brothers.
798 ;; insert children if thread is closed.
799 (when (not (wl-thread-entity-get-opened entity))
800 (setq children2 children)
802 (wl-thread-insert-entity 0 ; no mean now...
803 (wl-thread-get-entity
806 (setq children2 (cdr children2))))
811 ;; update parent on buffer.
813 (setq num (wl-thread-entity-get-number parent))
815 (wl-thread-update-line-on-buffer num)))
816 ;; update children lines on buffer.
818 (wl-thread-update-line-on-buffer
820 (wl-thread-entity-get-number parent)))
822 ;; don't update buffer
824 ;; return parent number
825 (list (wl-thread-entity-get-number parent))
827 ;; update the indent string
828 ; (wl-summary-goto-top-of-current-thread)
830 ; (wl-thread-goto-bottom-of-sub-thread)
831 ; (wl-thread-update-indent-string-region beg (point)))
835 (defun wl-thread-insert-message (overview-entity overview mark-alist
836 msg parent-msg &optional update)
837 "Insert MSG to the entity.
838 When optional argument UPDATE is non-nil,
839 Message is inserted to the summary buffer."
840 (let ((parent (wl-thread-get-entity parent-msg))
841 child-entity invisible-top)
842 ;; Update the thread view...not implemented yet.
845 ; (wl-thread-entity-force-open parent))
847 ;; insert as children.
848 (wl-thread-entity-insert-as-children
850 (setq child-entity (wl-thread-create-entity msg (nth 0 parent))))
851 ;; insert as top message.
852 (wl-thread-entity-insert-as-top
853 (wl-thread-create-entity msg nil)))
855 (if (not (setq invisible-top
856 (wl-thread-entity-parent-invisible-p child-entity)))
859 (wl-summary-update-thread
864 (elmo-msgdb-overview-get-entity-by-number overview parent-msg))
866 ;; use thread structure.
867 (wl-thread-entity-get-number
868 (wl-thread-entity-get-top-entity parent)))); return value;
869 ;; (setq beg (point))
870 ;; (wl-thread-goto-bottom-of-sub-thread)
871 ;; (wl-thread-update-indent-string-region beg (point)))
872 ;; currently invisible.. update closed line.
873 (wl-thread-update-children-number invisible-top)
876 (defun wl-thread-update-indent-string-thread (top-list)
879 (wl-summary-jump-to-msg (car top-list))
881 (wl-thread-goto-bottom-of-sub-thread)
882 (wl-thread-update-indent-string-region beg (point))
883 (setq top-list (cdr top-list)))))
885 (defun wl-thread-update-children-number (entity)
886 "Update the children number."
888 (wl-summary-jump-to-msg (wl-thread-entity-get-number entity))
890 (let ((text-prop (get-text-property (point) 'face))
891 from from-end beg str)
893 ((looking-at (concat "^" wl-summary-buffer-number-regexp
894 "..../..\(.*\)..:.. ["
895 wl-thread-indent-regexp
896 "]*\\[\\+\\([0-9]+\\):"))
897 (delete-region (match-beginning 1)(match-end 1))
898 (goto-char (match-beginning 1))
899 (setq str (format "%s" (wl-thread-entity-get-children-num entity)))
900 (if wl-summary-highlight
901 (put-text-property 0 (length str) 'face text-prop str))
903 ((looking-at (concat "^" wl-summary-buffer-number-regexp
904 "..../..\(.*\)..:.. ["
905 wl-thread-indent-regexp
907 (goto-char (match-end 0))
908 (setq beg (current-column))
909 (setq from-end (save-excursion
910 (move-to-column (+ 1 beg wl-from-width))
912 (setq from (buffer-substring (match-end 0) from-end))
913 (delete-region (match-end 0) from-end)
914 (setq str (wl-set-string-width
918 (wl-thread-entity-get-children-num
921 (if wl-summary-highlight
922 (put-text-property 0 (length str) 'face text-prop str))
924 (condition-case nil ; it's dangerous, so ignore error.
925 (run-hooks 'wl-thread-update-children-number-hook)
928 (message "Error in wl-thread-update-children-number-hook."))))))))
931 ;; Thread oriented commands.
933 (defun wl-thread-call-region-func (func &optional arg)
936 (wl-summary-goto-top-of-current-thread)
939 (wl-thread-goto-bottom-of-sub-thread)
940 (funcall func beg (point)))))
942 (defun wl-thread-prefetch (&optional arg)
944 (wl-thread-call-region-func 'wl-summary-prefetch-region arg))
946 (defun wl-thread-msg-mark-as-read (msg)
947 "Set mark as read for invisible MSG. Modeline is not changed."
948 (let* ((msgdb wl-summary-buffer-msgdb)
949 (mark-alist (elmo-msgdb-get-mark-alist msgdb))
951 (setq cur-mark (cadr (assq msg mark-alist)))
952 (cond ((or (string= cur-mark wl-summary-new-mark)
953 (string= cur-mark wl-summary-unread-uncached-mark))
956 (elmo-msgdb-mark-set mark-alist
958 (if (elmo-use-cache-p
959 wl-summary-buffer-folder-name
961 wl-summary-read-uncached-mark)))
962 (elmo-msgdb-set-mark-alist msgdb mark-alist)
963 (wl-summary-set-mark-modified))
964 ((string= cur-mark wl-summary-unread-cached-mark)
966 (setq mark-alist (elmo-msgdb-mark-set mark-alist msg nil))
967 (elmo-msgdb-set-mark-alist msgdb mark-alist)
968 (wl-summary-set-mark-modified)))))
970 (defun wl-thread-msg-mark-as-unread (msg)
971 "Set mark as unread for invisible MSG. Modeline is not changed."
972 (let* ((msgdb wl-summary-buffer-msgdb)
973 (mark-alist (elmo-msgdb-get-mark-alist msgdb))
975 (setq cur-mark (cadr (assq msg mark-alist)))
976 (cond ((string= cur-mark wl-summary-read-uncached-mark)
979 (elmo-msgdb-mark-set mark-alist
981 wl-summary-unread-uncached-mark))
982 (elmo-msgdb-set-mark-alist msgdb mark-alist)
983 (wl-summary-set-mark-modified))
986 (setq mark-alist (elmo-msgdb-mark-set mark-alist msg
987 wl-summary-unread-cached-mark))
988 (elmo-msgdb-set-mark-alist msgdb mark-alist)
989 (wl-summary-set-mark-modified)))))
991 (defun wl-thread-msg-mark-as-important (msg)
992 "Set mark as important for invisible MSG. Modeline is not changed."
993 (let* ((msgdb wl-summary-buffer-msgdb)
994 (mark-alist (elmo-msgdb-get-mark-alist msgdb))
996 (setq cur-mark (cadr (assq msg mark-alist)))
998 (elmo-msgdb-mark-set mark-alist
1000 (if (string= cur-mark wl-summary-important-mark)
1002 wl-summary-important-mark)))
1003 (elmo-msgdb-set-mark-alist msgdb mark-alist)
1004 (wl-summary-set-mark-modified)))
1006 (defun wl-thread-mark-as-read (&optional arg)
1008 (wl-thread-call-region-func 'wl-summary-mark-as-read-region arg))
1010 (defun wl-thread-mark-as-unread (&optional arg)
1012 (wl-thread-call-region-func 'wl-summary-mark-as-unread-region arg))
1014 (defun wl-thread-mark-as-important (&optional arg)
1016 (wl-thread-call-region-func 'wl-summary-mark-as-important-region arg))
1018 (defun wl-thread-copy (&optional arg)
1020 (wl-thread-call-region-func 'wl-summary-copy-region arg))
1022 (defun wl-thread-refile (&optional arg)
1026 (wl-thread-call-region-func 'wl-summary-refile-region arg)
1028 (wl-summary-goto-top-of-current-thread))
1029 (wl-thread-goto-bottom-of-sub-thread))
1031 (elmo-display-error err t)
1034 (defun wl-thread-delete (&optional arg)
1036 (wl-thread-call-region-func 'wl-summary-delete-region arg)
1038 (wl-summary-goto-top-of-current-thread))
1039 (if (not wl-summary-move-direction-downward)
1041 (wl-thread-goto-bottom-of-sub-thread)
1042 (if wl-summary-buffer-disp-msg
1043 (wl-summary-redisplay))))
1045 (defun wl-thread-target-mark (&optional arg)
1047 (wl-thread-call-region-func 'wl-summary-target-mark-region arg))
1049 (defun wl-thread-unmark (&optional arg)
1051 (wl-thread-call-region-func 'wl-summary-unmark-region arg))
1053 (defun wl-thread-exec (&optional arg)
1055 (wl-thread-call-region-func 'wl-summary-exec-region arg))
1057 (defun wl-thread-save (&optional arg)
1059 (wl-thread-call-region-func 'wl-summary-save-region arg))
1061 (defun wl-thread-force-open (&optional msg-num)
1062 "force open current folder"
1064 (wl-summary-jump-to-msg msg-num))
1065 (let ((wl-thread-insert-force-opened t))
1066 (wl-thread-open-close)))
1068 (defun wl-thread-entity-force-open (entity)
1069 (let ((wl-thread-insert-force-opened t)
1071 (if (null (wl-thread-entity-get-parent entity))
1073 (if (and (not (wl-thread-entity-get-opened entity))
1074 (wl-thread-entity-get-children entity))
1075 (wl-thread-force-open (wl-thread-entity-get-number entity)))
1076 (if (setq notopen (wl-thread-entity-parent-invisible-p entity))
1077 (wl-thread-force-open (wl-thread-entity-get-number notopen))))))
1079 (defun wl-thread-insert-top ()
1080 (let ((elist wl-thread-entity-list)
1081 (len (length wl-thread-entity-list))
1083 (wl-delete-all-overlays)
1085 (wl-thread-insert-entity
1087 (wl-thread-get-entity (car elist))
1091 (elmo-display-progress
1092 'wl-thread-insert-top "Inserting thread..."
1093 (/ (* cur 100) len))
1094 (setq elist (cdr elist)))))
1096 (defsubst wl-thread-insert-entity-sub (indent entity parent-entity all)
1097 (let ((number-alist (elmo-msgdb-get-number-alist wl-summary-buffer-msgdb))
1098 (overview (elmo-msgdb-get-overview wl-summary-buffer-msgdb))
1099 (mark-alist (elmo-msgdb-get-mark-alist wl-summary-buffer-msgdb))
1106 (when (setq msg-num (wl-thread-entity-get-number entity))
1107 (unless all ; all...means no temp-mark.
1108 (cond ((memq msg-num wl-summary-buffer-delete-list)
1109 (setq temp-mark "D"))
1110 ((memq msg-num wl-summary-buffer-target-mark-list)
1111 (setq temp-mark "*"))
1112 ((assq msg-num wl-summary-buffer-refile-list)
1113 (setq temp-mark "o"))
1114 ((assq msg-num wl-summary-buffer-copy-list)
1115 (setq temp-mark "O"))))
1117 (setq temp-mark (wl-summary-get-score-mark msg-num)))
1118 (setq children-num (wl-thread-entity-get-children-num entity))
1119 (setq overview-entity
1120 (elmo-msgdb-search-overview-entity
1121 (nth 0 entity) number-alist overview))
1122 ;;(wl-delete-all-overlays)
1123 (when overview-entity
1125 (wl-summary-overview-create-summary-line
1128 (assoc ; parent-entity
1129 (cdr (assq (nth 0 parent-entity)
1130 number-alist)) overview)
1133 (if wl-thread-insert-force-opened
1135 (if (not (wl-thread-entity-get-opened entity))
1138 (wl-summary-insert-line summary-line)))))
1140 (defun wl-thread-insert-entity (indent entity parent-entity all)
1141 "Insert thread entity in current buffer."
1142 (let ((msgs (list (car entity)))
1143 children msgs-stack)
1145 (wl-thread-insert-entity-sub indent entity parent-entity all)
1146 (setq msgs (cdr msgs))
1147 (setq children (nth 2 entity))
1150 (when (or wl-thread-insert-force-opened
1151 (wl-thread-entity-get-opened entity))
1152 (wl-thread-entity-set-opened entity t)
1153 (wl-push msgs msgs-stack)
1156 parent-entity entity)))
1158 (while (and (null msgs) msgs-stack)
1159 (setq msgs (wl-pop msgs-stack))
1160 (setq indent (1- indent)))
1162 (setq entity (wl-thread-get-entity (car msgs)))
1163 (setq parent-entity (wl-thread-entity-get-parent-entity entity))))
1164 (setq entity (wl-thread-get-entity (car msgs))))))
1166 (defun wl-thread-descendant-p (mynumber number)
1167 (let ((cur (wl-thread-get-entity number))
1171 (setq cur (wl-thread-entity-get-parent-entity cur))
1172 (if (null (setq num (wl-thread-entity-get-number cur))) ; top!
1175 (eq mynumber (wl-thread-entity-get-number cur)))
1179 ; (defun wl-thread-goto-bottom-of-sub-thread ()
1181 ; (let ((depth (wl-thread-get-depth-of-current-line)))
1183 ; (while (and (not (eobp))
1184 ; (> (wl-thread-get-depth-of-current-line)
1187 ; (beginning-of-line)))
1189 (defun wl-thread-goto-bottom-of-sub-thread (&optional msg)
1191 (let ((mynumber (or msg (wl-summary-message-number))))
1193 (while (wl-thread-descendant-p mynumber (wl-summary-message-number))
1195 (beginning-of-line)))
1197 (defun wl-thread-remove-destination-region (beg end)
1200 (narrow-to-region beg end)
1201 (goto-char (point-min))
1203 (let ((num (wl-summary-message-number)))
1204 (if (assq num wl-summary-buffer-refile-list)
1205 (wl-summary-remove-destination)))
1206 (forward-line 1)))))
1208 (defun wl-thread-print-destination-region (beg end)
1209 (if (or wl-summary-buffer-refile-list
1210 wl-summary-buffer-copy-list)
1213 (narrow-to-region beg end)
1214 (goto-char (point-min))
1216 (let ((num (wl-summary-message-number))
1218 (if (or (setq pair (assq num wl-summary-buffer-refile-list))
1219 (setq pair (assq num wl-summary-buffer-copy-list)))
1220 (wl-summary-print-destination (car pair) (cdr pair))))
1221 (forward-line 1))))))
1223 (defsubst wl-thread-get-children-msgs (msg)
1224 (let ((msgs (list msg))
1228 (wl-append ret-val (list (car msgs)))
1229 (setq children (wl-thread-entity-get-children
1230 (wl-thread-get-entity (car msgs))))
1231 (setq msgs (cdr msgs))
1233 (while (and (null msgs) msgs-stack)
1234 (setq msgs (wl-pop msgs-stack)))
1235 (wl-push msgs msgs-stack)
1236 (setq msgs children)))
1239 (defun wl-thread-get-children-msgs-uncached (msg &optional uncached-marks)
1240 (let ((children-msgs (wl-thread-get-children-msgs msg))
1241 (mark-alist (elmo-msgdb-get-mark-alist wl-summary-buffer-msgdb))
1242 (number-alist (elmo-msgdb-get-number-alist wl-summary-buffer-msgdb))
1245 (while children-msgs
1246 (if (and (not (eq msg (car children-msgs))) ; except itself
1247 (or (and uncached-marks
1248 (setq mark (cadr (assq (car children-msgs)
1250 (member mark uncached-marks))
1251 (and (not uncached-marks)
1252 (null (elmo-cache-exists-p
1253 (cdr (assq (car children-msgs)
1255 (wl-append uncached-list (list (car children-msgs))))
1256 (setq children-msgs (cdr children-msgs)))
1259 (defun wl-thread-get-children-msgs-with-mark (msg mark)
1260 (let ((children-msgs (wl-thread-get-children-msgs msg))
1261 (check-func (cond ((string= mark "o")
1262 'wl-summary-msg-marked-as-refiled)
1264 'wl-summary-msg-marked-as-copied)
1266 'wl-summary-msg-marked-as-deleted)
1268 'wl-summary-msg-marked-as-target)))
1270 (while children-msgs
1271 (if (funcall check-func (car children-msgs))
1272 (wl-append ret-val (list (car children-msgs))))
1273 (setq children-msgs (cdr children-msgs)))
1276 (defun wl-thread-close (entity)
1278 (wl-thread-entity-set-opened entity nil)
1279 (setq depth (wl-thread-get-depth-of-current-line))
1282 (wl-thread-goto-bottom-of-sub-thread)
1283 (wl-thread-remove-destination-region beg
1285 (forward-char -1) ;; needed for mouse-face.
1286 (delete-region beg (point))
1287 (wl-thread-insert-entity (- depth 1)
1289 (wl-thread-get-entity
1292 (delete-char 1) ; delete '\n'
1293 (wl-thread-print-destination-region beg (point))))
1295 (defun wl-thread-open (entity)
1299 (setq depth (wl-thread-get-depth-of-current-line))
1301 (delete-region beg (point))
1302 (wl-thread-entity-set-opened entity t)
1303 (wl-thread-insert-entity depth ;(- depth 1)
1305 (wl-thread-get-entity
1306 (nth 3 entity)) nil)
1307 (delete-char 1) ; delete '\n'
1308 (wl-thread-print-destination-region beg (point))))
1310 (defun wl-thread-open-close (&optional force-open)
1312 (when (eq wl-summary-buffer-view 'thread)
1313 ;(if (equal wl-thread-top-entity '(nil t nil nil))
1314 ;(error "There's no thread structure."))
1316 (let ((inhibit-read-only t)
1317 (buffer-read-only nil)
1318 (wl-thread-insert-force-opened
1319 (or wl-thread-insert-force-opened
1321 msg entity beg depth parent)
1322 (setq msg (wl-summary-message-number))
1323 (setq entity (wl-thread-get-entity msg))
1324 (if (wl-thread-entity-get-opened entity)
1325 ;; if already opened, close its child!
1326 (if (wl-thread-entity-get-children entity)
1327 (wl-thread-close entity)
1328 ;; opened, but has no children, close its parent!
1329 (when (setq parent (wl-thread-entity-get-parent entity))
1330 (wl-summary-jump-to-msg parent)
1332 (wl-thread-get-entity (wl-summary-message-number)))))
1333 ;; if closed (or it is just a thread bottom message)
1334 ;; has children, open it!
1335 (if (wl-thread-entity-get-children entity)
1336 (wl-thread-open entity)
1337 ;; closed, and has no children, close its parent!
1338 (setq msg (or (wl-thread-entity-get-parent entity)
1339 (wl-thread-entity-get-number entity)))
1341 (wl-summary-jump-to-msg msg)
1343 (wl-thread-get-entity (wl-summary-message-number)))))))
1344 (wl-summary-set-message-modified)
1345 (set-buffer-modified-p nil))))
1348 (defun wl-thread-get-depth-of-current-line ()
1353 (if (re-search-forward (concat "^" wl-summary-buffer-number-regexp
1354 "..../..\(.*\)..:.. ")
1356 (while (string-match wl-thread-indent-regexp
1358 (char-after (point))))
1359 (setq depth (1+ depth))
1361 (/ depth wl-thread-indent-level-internal))))
1363 (defun wl-thread-update-indent-string-region (beg end)
1367 (while (< (point) end)
1368 (wl-thread-update-indent-string)
1371 (defsubst wl-thread-make-indent-string (entity)
1374 (space-str (wl-repeat-string wl-thread-space-str-internal
1375 (- wl-thread-indent-level-internal 1)))
1377 (when (wl-thread-entity-get-number
1378 (setq parent (wl-thread-entity-get-parent-entity cur)))
1379 (if (wl-thread-entity-get-younger-brothers cur)
1380 (setq ret-val wl-thread-have-younger-brother-str-internal)
1381 (setq ret-val wl-thread-youngest-child-str-internal))
1382 (setq ret-val (concat ret-val
1384 wl-thread-horizontal-str-internal
1385 (- wl-thread-indent-level-internal 1))))
1387 (while (wl-thread-entity-get-number
1388 (wl-thread-entity-get-parent-entity cur))
1389 (if (wl-thread-entity-get-younger-brothers cur)
1390 (setq ret-val (concat wl-thread-vertical-str-internal
1393 (setq ret-val (concat wl-thread-space-str-internal
1396 (setq cur (wl-thread-entity-get-parent-entity cur))))
1399 (defun wl-thread-update-indent-string ()
1400 "Update indent string of current line."
1404 (let ((inhibit-read-only t)
1405 (buffer-read-only nil)
1407 (when (looking-at (concat "^ *\\([0-9]+\\)"
1408 "..../..\(.*\)..:.. \\("
1409 wl-highlight-thread-indent-string-regexp
1411 (goto-char (match-beginning 2))
1412 (delete-region (match-beginning 2)
1415 (wl-thread-make-indent-string
1416 (wl-thread-get-entity (string-to-int (wl-match-buffer 1)))))
1417 (if (and wl-summary-width
1418 wl-summary-indent-length-limit
1419 (< wl-summary-indent-length-limit
1420 (string-width thr-str)))
1421 (setq thr-str (wl-set-string-width
1422 wl-summary-indent-length-limit
1425 (if wl-summary-highlight
1426 (wl-highlight-summary-current-line))))))
1428 (provide 'wl-thread)
1430 ;;; wl-thread.el ends here