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
8 ;; This file is part of Wanderlust (Yet Another Message Interface on Emacsen).
10 ;; This program is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; This program is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
33 (require 'wl-highlight)
35 ;; buffer local variables.
36 ;(defvar wl-thread-top-entity '(nil t nil nil)) ; top entity
37 (defvar wl-thread-tops nil) ; top number list (number)
38 (defvar wl-thread-entities nil)
39 (defvar wl-thread-entity-list nil) ; entity list
40 (defvar wl-thread-entity-hashtb nil) ; obarray
41 (defvar wl-thread-indent-regexp nil)
44 (function make-variable-buffer-local)
45 (list 'wl-thread-entity-hashtb
46 'wl-thread-entities ; -> ".wl-thread-entity"
47 'wl-thread-entity-list ; -> ".wl-thread-entity-list"
49 'wl-thread-indent-regexp))
52 (defvar wl-thread-insert-force-opened nil)
54 ;;;;;; each entity is (number opened-or-not children parent) ;;;;;;;
56 (defun wl-meaning-of-mark (mark)
57 (if (not (elmo-folder-plugged-p wl-summary-buffer-folder-name))
59 ((string= mark wl-summary-unread-cached-mark)
61 ((string= mark wl-summary-important-mark)
64 ((string= mark wl-summary-new-mark)
66 ((or (string= mark wl-summary-unread-uncached-mark)
67 (string= mark wl-summary-unread-cached-mark))
69 ((string= mark wl-summary-important-mark)
72 (defun wl-thread-next-mark-p (mark next)
73 (cond ((not (elmo-folder-plugged-p wl-summary-buffer-folder-name))
74 (or (string= mark wl-summary-unread-cached-mark)
75 (string= mark wl-summary-important-mark)))
77 (string= mark wl-summary-new-mark))
79 (or (string= mark wl-summary-unread-uncached-mark)
80 (string= mark wl-summary-unread-cached-mark)
81 (string= mark wl-summary-new-mark)))
83 (or (string= mark wl-summary-unread-uncached-mark)
84 (string= mark wl-summary-unread-cached-mark)
85 (string= mark wl-summary-new-mark)
86 (string= mark wl-summary-important-mark)))))
88 (defun wl-thread-next-failure-mark-p (mark next)
89 (cond ((not (elmo-folder-plugged-p wl-summary-buffer-folder-name))
90 (string= mark wl-summary-unread-cached-mark))
93 (or (string= mark wl-summary-unread-uncached-mark)
94 (string= mark wl-summary-unread-cached-mark)
95 (string= mark wl-summary-new-mark)
96 (string= mark wl-summary-important-mark)))
99 (defun wl-thread-resume-entity (fld)
100 (let (entities top-list)
101 (setq entities (wl-summary-load-file-object
102 (expand-file-name wl-thread-entity-file
103 (elmo-msgdb-expand-path fld))))
105 (wl-summary-load-file-object
106 (expand-file-name wl-thread-entity-list-file
107 (elmo-msgdb-expand-path fld))))
109 (message "Resuming thread structure...")
110 ;; set obarray value.
111 (setq wl-thread-entity-hashtb (elmo-make-hash (* (length entities) 2)))
114 (elmo-set-hash-val (format "#%d" (car x))
116 wl-thread-entity-hashtb))
118 ;; set buffer local variables.
119 (setq wl-thread-entities entities)
120 (setq wl-thread-entity-list top-list)
121 (message "Resuming thread structure...done.")))
123 (defun wl-thread-save-entity (dir)
124 (wl-thread-save-entities dir)
125 (wl-thread-save-top-list dir))
127 (defun wl-thread-save-top-list (dir)
128 (let ((top-file (expand-file-name wl-thread-entity-list-file dir))
129 (entity wl-thread-entity-list)
130 (tmp-buffer (get-buffer-create " *wl-thread-save-top-list*")))
132 (set-buffer tmp-buffer)
134 (when (file-writable-p top-file)
135 (prin1 entity tmp-buffer)
136 (princ "\n" tmp-buffer)
137 (write-region (point-min) (point-max) top-file nil 'no-msg)
138 (kill-buffer tmp-buffer)))))
140 (defun wl-thread-save-entities (dir)
141 (let ((top-file (expand-file-name wl-thread-entity-file dir))
142 (entities wl-thread-entities)
143 (tmp-buffer (get-buffer-create " *wl-thread-save-entities*")))
145 (set-buffer tmp-buffer)
147 (when (file-writable-p top-file)
148 (prin1 entities tmp-buffer)
149 (princ "\n" tmp-buffer)
150 (write-region (point-min) (point-max) top-file nil 'no-msg)
151 (kill-buffer tmp-buffer)))))
153 (defsubst wl-thread-entity-get-number (entity)
155 (defsubst wl-thread-entity-get-opened (entity)
157 (defsubst wl-thread-entity-get-children (entity)
159 (defsubst wl-thread-entity-get-parent (entity)
162 (defsubst wl-thread-create-entity (num parent &optional opened)
163 (list num (or opened wl-thread-insert-opened) nil parent))
165 (defsubst wl-thread-get-entity (num)
167 (boundp (intern (format "#%d" num) wl-thread-entity-hashtb))
168 (elmo-get-hash-val (format "#%d" num) wl-thread-entity-hashtb)))
170 (defsubst wl-thread-entity-set-parent (entity parent)
171 (setcar (cdddr entity) parent)
174 (defsubst wl-thread-entity-set-children (entity children)
175 (setcar (cddr entity) children))
177 (defsubst wl-thread-entity-insert-as-top (entity)
180 (setq wl-thread-entity-list (append wl-thread-entity-list
181 (list (car entity))))
182 (setq wl-thread-entities (cons entity wl-thread-entities))
183 (elmo-set-hash-val (format "#%d" (car entity)) entity
184 wl-thread-entity-hashtb)))
186 (defsubst wl-thread-entity-insert-as-children (to entity)
187 (let ((children (nth 2 to)))
188 (setcar (cddr to) (wl-append children
189 (list (car entity))))
190 (setq wl-thread-entities (cons entity wl-thread-entities))
191 (elmo-set-hash-val (format "#%d" (car entity)) entity
192 wl-thread-entity-hashtb)))
194 (defsubst wl-thread-entity-set-opened (entity opened)
195 (setcar (cdr entity) opened))
197 (defsubst wl-thread-entity-get-children-num (entity)
200 (msgs (list (car entity))))
202 (setq msgs (cdr msgs))
203 (setq children (wl-thread-entity-get-children entity))
205 (while (and (null msgs) msgs-stack)
206 (setq msgs (wl-pop msgs-stack)))
207 (setq ret-val (+ (or ret-val 0) (length children)))
208 (wl-push msgs msgs-stack)
209 (setq msgs children))
210 (setq entity (wl-thread-get-entity (car msgs))))
213 (defsubst wl-thread-entity-get-descendant (entity)
216 (msgs (list (car entity))))
218 (setq msgs (cdr msgs))
219 (setq children (wl-thread-entity-get-children entity))
221 (while (and (null msgs) msgs-stack)
222 (setq msgs (wl-pop msgs-stack)))
223 (setq ret-val (append ret-val (copy-sequence children)))
224 (wl-push msgs msgs-stack)
225 (setq msgs children))
226 (setq entity (wl-thread-get-entity (car msgs))))
229 (defsubst wl-thread-entity-get-parent-entity (entity)
230 (wl-thread-get-entity (wl-thread-entity-get-parent entity)))
232 (defun wl-thread-entity-get-top-entity (entity)
233 (let ((cur-entity entity)
235 (while (setq p-num (wl-thread-entity-get-parent cur-entity))
236 (setq cur-entity (wl-thread-get-entity p-num)))
239 (defun wl-thread-entity-parent-invisible-p (entity)
240 "If parent of ENTITY is invisible, the top invisible ancestor entity of
242 (let ((cur-entity entity)
245 (while (setq cur-entity (wl-thread-entity-get-parent-entity
247 (if (null (wl-thread-entity-get-number cur-entity))
252 (when (not (wl-thread-entity-get-opened cur-entity))
254 (setq ret-val cur-entity)))))
255 ;; top of closed entity in the path.
258 (defun wl-thread-entity-get-mark (number)
259 (let ((mark-alist (elmo-msgdb-get-mark-alist wl-summary-buffer-msgdb))
261 (setq mark (cadr (assq number mark-alist)))
262 (if (string= mark wl-summary-read-uncached-mark)
266 (defun wl-thread-meaning-alist-get-result (meaning-alist)
267 (let ((malist meaning-alist)
271 (if (setq ret-val (cdr (car malist)))
272 (throw 'done ret-val))
273 (setq malist (cdr malist))))))
275 (defun wl-thread-entity-check-prev-mark (entity prev-marks)
276 "Check prev mark. Result is stored in PREV-MARK."
277 (let ((msgs (list (car entity)))
278 (succeed-list (car prev-marks))
279 (failure-list (cdr prev-marks))
281 mark meaning success failure parents)
284 (if (and (not (memq (car msgs) parents))
285 (setq children (reverse (wl-thread-entity-get-children entity))))
287 (wl-append parents (list (car msgs)))
288 (wl-push msgs msgs-stack)
289 (setq msgs children))
290 (if (setq mark (wl-thread-entity-get-mark (car entity)))
291 (if (setq meaning (wl-meaning-of-mark mark))
292 (if (setq success (assq meaning succeed-list))
294 (setcdr success entity)
296 (setq failure (assq meaning failure-list))
297 (unless (cdr failure)
298 (setcdr (assq meaning failure-list) entity)))))
299 (setq msgs (cdr msgs)))
301 (while (and (null msgs) msgs-stack)
302 (setq msgs (wl-pop msgs-stack))))
303 (setq entity (wl-thread-get-entity (car msgs)))))))
305 (defun wl-thread-entity-check-next-mark (entity next-marks)
306 "Check next mark. Result is stored in NEXT-MARK."
307 (let ((msgs (list (car entity)))
308 (succeed-list (car next-marks))
309 (failure-list (cdr next-marks))
311 mark meaning success failure)
314 (if (setq mark (wl-thread-entity-get-mark (car entity)))
315 (if (setq meaning (wl-meaning-of-mark mark))
316 (if (setq success (assq meaning succeed-list))
318 (setcdr success entity)
320 (setq failure (assq meaning failure-list))
321 (unless (cdr failure)
322 (setcdr (assq meaning failure-list) entity)))))
323 (setq msgs (cdr msgs))
324 (setq children (wl-thread-entity-get-children entity))
327 (wl-push msgs msgs-stack)
328 (setq msgs children))
330 (while (and (null msgs) msgs-stack)
331 (setq msgs (wl-pop msgs-stack)))))
332 (setq entity (wl-thread-get-entity (car msgs)))))))
334 (defun wl-thread-entity-get-older-brothers (entity &optional parent)
335 (let* ((parent (or parent
336 (wl-thread-entity-get-parent-entity entity)))
337 (brothers (wl-thread-entity-get-children parent))
341 (setq brothers wl-thread-entity-list))
344 (if (not (eq (wl-thread-entity-get-number entity)
346 (wl-append ret-val (list (car brothers)))
347 (throw 'done ret-val))
348 (setq brothers (cdr brothers))))))
350 (defun wl-thread-entity-get-younger-brothers (entity &optional parent)
351 (let* ((parent (or parent
352 (wl-thread-entity-get-parent-entity entity)))
353 (brothers (wl-thread-entity-get-children parent)))
355 (cdr (memq (wl-thread-entity-get-number entity)
358 (cdr (memq (car entity) wl-thread-entity-list)))))
360 (defun wl-thread-entity-check-prev-mark-from-older-brother (entity prev-marks)
361 (let* (older-brother parent)
365 (reverse (wl-thread-entity-get-older-brothers entity)))
367 (let ((succeed-list (car prev-marks))
368 (failure-list (cdr prev-marks))
369 mark meaning success failure)
370 (if (setq mark (wl-thread-entity-get-mark (car entity)))
371 (if (setq meaning (wl-meaning-of-mark mark))
372 (if (setq success (assq meaning succeed-list))
374 (setcdr success entity)
376 (setq failure (assq meaning failure-list))
377 (unless (cdr failure)
378 (setcdr (assq meaning failure-list) entity))))))
379 ;; check older brothers
381 (wl-thread-entity-check-prev-mark (wl-thread-get-entity
384 (if (wl-thread-meaning-alist-get-result
387 (setq older-brother (cdr older-brother)))
388 (setq entity (wl-thread-entity-get-parent-entity entity))))))
390 (defun wl-thread-entity-get-prev-marked-entity (entity prev-marks)
391 (let ((older-brothers (reverse
392 (wl-thread-entity-get-older-brothers entity)))
395 (while older-brothers
396 (wl-thread-entity-check-prev-mark
397 (wl-thread-get-entity (car older-brothers)) prev-marks)
399 (wl-thread-meaning-alist-get-result
401 (throw 'done marked))
402 (setq older-brothers (cdr older-brothers))))
403 (wl-thread-entity-check-prev-mark-from-older-brother
404 (wl-thread-entity-get-parent-entity entity) prev-marks)
406 (wl-thread-meaning-alist-get-result
410 (wl-thread-meaning-alist-get-result
414 (defun wl-thread-get-prev-unread (msg &optional hereto)
415 (let ((cur-entity (wl-thread-get-entity msg))
416 (prev-marks (cond ((eq wl-summary-move-order 'new)
417 (cons (list (cons 'new nil))
418 (list (cons 'unread nil)
419 (cons 'important nil))))
420 ((eq wl-summary-move-order 'unread)
421 (cons (list (cons 'unread nil)
423 (list (cons 'important nil))))
425 (cons (list (cons 'unread nil)
427 (cons 'important nil))
431 (when (wl-thread-next-mark-p (setq mark
432 (wl-thread-entity-get-mark
435 ;;(setq mark (cons cur-entity
436 ;;(wl-thread-entity-get-mark cur-entity)))
438 (when (and (not ret-val)
440 (wl-thread-entity-get-prev-marked-entity
441 cur-entity prev-marks))
445 (let ((success-list (car prev-marks)))
447 (if (cdr (car success-list))
449 (setq success-list (cdr success-list)))
451 (wl-thread-next-failure-mark-p mark (caaar prev-marks)))
454 (setq ret-val (car cur-entity)))))
457 (defun wl-thread-jump-to-prev-unread (&optional hereto)
458 "If prev unread is a children of a closed message,
459 the closed parent will be opened."
461 (let ((msg (wl-thread-get-prev-unread
462 (wl-summary-message-number) hereto)))
464 (wl-thread-entity-force-open (wl-thread-get-entity msg))
465 (wl-summary-jump-to-msg msg)
468 (defun wl-thread-jump-to-msg (&optional number)
470 (let ((num (or number
472 (read-from-minibuffer "Jump to Message(No.): ")))))
473 (wl-thread-entity-force-open (wl-thread-get-entity num))
474 (wl-summary-jump-to-msg num)))
476 (defun wl-thread-get-next-unread (msg &optional hereto)
477 (let ((cur-entity (wl-thread-get-entity msg))
478 (next-marks (cond ((not (elmo-folder-plugged-p
479 wl-summary-buffer-folder-name))
480 (cons (list (cons 'unread nil))
481 (list (cons 'important nil))))
482 ((eq wl-summary-move-order 'new)
483 (cons (list (cons 'new nil))
484 (list (cons 'unread nil)
485 (cons 'important nil))))
486 ((eq wl-summary-move-order 'unread)
487 (cons (list (cons 'unread nil)
489 (list (cons 'important nil))))
491 (cons (list (cons 'unread nil)
493 (cons 'important nil))
497 (when (wl-thread-next-mark-p (setq mark
498 (wl-thread-entity-get-mark
502 (when (and (not ret-val)
504 (wl-thread-entity-get-next-marked-entity
505 cur-entity next-marks))
508 ;; all success-list is nil
510 (let ((success-list (car next-marks)))
512 (if (cdr (car success-list))
514 (setq success-list (cdr success-list)))
516 (wl-thread-next-failure-mark-p mark (caaar next-marks)))
519 (setq ret-val (car cur-entity)))))
522 (defun wl-thread-jump-to-next-unread (&optional hereto)
523 "If next unread is a children of a closed message,
524 the closed parent will be opened."
526 (let ((msg (wl-thread-get-next-unread
527 (wl-summary-message-number) hereto)))
529 (wl-thread-entity-force-open (wl-thread-get-entity msg))
530 (wl-summary-jump-to-msg msg)
533 (defun wl-thread-close-all ()
534 "Close all top threads."
536 (message "Closing all threads...")
537 (let ((entities wl-thread-entity-list)
539 (len (length wl-thread-entity-list)))
541 (when (and (wl-thread-entity-get-opened (wl-thread-get-entity
543 (wl-thread-entity-get-children (wl-thread-get-entity
545 (wl-summary-jump-to-msg (car entities))
546 (wl-thread-open-close)
548 (elmo-display-progress
549 'wl-thread-close-all "Closing all threads..."
550 (/ (* cur 100) len)))
551 (setq entities (cdr entities))))
552 (elmo-display-progress 'wl-thread-close-all
553 "Closing all threads..."
555 (message "Closing all threads...done.")
556 (goto-char (point-max)))
558 (defun wl-thread-open-all ()
561 (message "Opening all threads...")
562 (let ((entities wl-thread-entity-list)
564 (len (length wl-thread-entity-list)))
566 (if (not (wl-thread-entity-get-opened (wl-thread-get-entity
568 (wl-thread-entity-force-open (wl-thread-get-entity
571 (elmo-display-progress
572 'wl-thread-open-all "Opening all threads..."
574 (setq entities (cdr entities))))
575 (message "Opening all threads...done.")
576 (goto-char (point-max)))
578 (defun wl-thread-open-all-unread ()
580 (let ((mark-alist (elmo-msgdb-get-mark-alist wl-summary-buffer-msgdb))
583 (if (setq mark (nth 1 (car mark-alist)))
584 (if (or (string= mark wl-summary-unread-uncached-mark)
585 (string= mark wl-summary-unread-cached-mark)
586 (string= mark wl-summary-new-mark)
587 (string= mark wl-summary-important-mark))
588 (wl-thread-entity-force-open (wl-thread-get-entity
589 (nth 0 (car mark-alist))))))
590 (setq mark-alist (cdr mark-alist)))))
592 ;;; a subroutine for wl-thread-entity-get-next-marked-entity.
593 (defun wl-thread-entity-check-next-mark-from-younger-brother
595 (let* (parent younger-brother)
598 (setq parent (wl-thread-entity-get-parent-entity entity)
600 (wl-thread-entity-get-younger-brothers entity parent))
602 (while younger-brother
603 (wl-thread-entity-check-next-mark
604 (wl-thread-get-entity (car younger-brother))
606 (if (wl-thread-meaning-alist-get-result
609 (setq younger-brother (cdr younger-brother)))
610 (setq entity parent)))))
612 (defun wl-thread-entity-get-next-marked-entity (entity next-marks)
613 (let ((children (wl-thread-entity-get-children entity))
617 (wl-thread-entity-check-next-mark
618 (wl-thread-get-entity (car children)) next-marks)
620 (wl-thread-meaning-alist-get-result
622 (throw 'done marked))
623 (setq children (cdr children))))
624 ;; check younger brother
625 (wl-thread-entity-check-next-mark-from-younger-brother
628 (wl-thread-meaning-alist-get-result
632 (wl-thread-meaning-alist-get-result
636 (defun wl-thread-update-line-msgs (msgs)
637 (wl-delete-all-overlays)
640 (wl-thread-update-line-on-buffer (car msgs) nil msgs))))
642 (defsubst wl-thread-update-line-on-buffer-sub (entity &optional msg parent-msg)
643 (let ((number-alist (elmo-msgdb-get-number-alist wl-summary-buffer-msgdb))
644 (overview (elmo-msgdb-get-overview wl-summary-buffer-msgdb))
645 (mark-alist (elmo-msgdb-get-mark-alist wl-summary-buffer-msgdb))
646 (buffer-read-only nil)
647 (inhibit-read-only t)
648 ;;(parent-msg parent-msg)
653 (if (memq msg wl-summary-buffer-delete-list)
654 (setq temp-mark "D"))
655 (if (memq msg wl-summary-buffer-target-mark-list)
656 (setq temp-mark "*"))
657 (if (assq msg wl-summary-buffer-refile-list)
658 (setq temp-mark "o"))
659 (if (assq msg wl-summary-buffer-copy-list)
660 (setq temp-mark "O"))
662 (setq temp-mark (wl-summary-get-score-mark msg)))
663 ;(setq parent-entity (wl-thread-entity-get-parent-entity entity))
665 (setq parent-msg (wl-thread-entity-get-parent entity)))
666 ;;(setq children (wl-thread-entity-get-children entity))
667 (setq children-num (wl-thread-entity-get-children-num entity))
668 (setq overview-entity
669 (elmo-msgdb-search-overview-entity msg
670 number-alist overview))
671 ;;(wl-delete-all-overlays)
672 (when overview-entity
674 (wl-summary-overview-create-summary-line
677 (assoc ; parent-entity
678 (cdr (assq parent-msg
679 number-alist)) overview)
682 (if wl-thread-insert-force-opened
684 (if (not (wl-thread-entity-get-opened entity))
687 (wl-summary-insert-line summary-line))))
689 (defun wl-thread-update-line-on-buffer (&optional msg parent-msg updates)
691 (let ((msgs (list (or msg (wl-summary-message-number))))
692 entity children msgs-stack)
694 (setq msg (wl-pop msgs))
695 (setq updates (and updates (delete msg updates)))
696 (when (wl-thread-delete-line-from-buffer msg)
697 (setq entity (wl-thread-get-entity msg))
698 (wl-thread-update-line-on-buffer-sub entity msg parent-msg)
700 (setq children (wl-thread-entity-get-children entity))
703 (when (wl-thread-entity-get-opened entity)
704 (wl-push msgs msgs-stack)
708 (while (and (null msgs) msgs-stack)
709 (setq msgs (wl-pop msgs-stack)))
712 (wl-thread-entity-get-number
713 (wl-thread-entity-get-parent-entity
714 (wl-thread-get-entity (car msgs))))))))))
717 (defun wl-thread-delete-line-from-buffer (msg)
718 "Simply delete msg line."
720 (if (wl-summary-jump-to-msg msg)
724 (delete-region beg (point))
728 (defun wl-thread-cleanup-symbols (msgs)
732 (when (boundp (setq sym (intern (format "#%d" (car msgs))
733 wl-thread-entity-hashtb)))
735 (setq wl-thread-entities
736 (delq (wl-thread-get-entity (car msgs))
739 (setq msgs (cdr msgs)))))
741 (defun wl-thread-delete-message (msg &optional update)
742 "Delete MSG from entity and buffer."
744 (let* ((entity (wl-thread-get-entity msg))
746 older-brothers younger-brothers ;;brothers
749 (setq parent (wl-thread-entity-get-parent-entity entity))
753 ;;(setq brothers (wl-thread-entity-get-children parent))
754 (setq older-brothers (wl-thread-entity-get-older-brothers
756 (setq younger-brothers (wl-thread-entity-get-younger-brothers
759 (setq children (wl-thread-entity-get-children entity))
761 (wl-thread-entity-set-parent
762 (wl-thread-get-entity x)
763 (wl-thread-entity-get-number parent)))
765 (wl-thread-entity-set-children
772 ;; top...children becomes top.
774 (wl-thread-entity-set-parent (wl-thread-get-entity x)
776 (setq children (wl-thread-entity-get-children entity)))
777 ;; delete myself from top list.
778 (setq older-brothers (wl-thread-entity-get-older-brothers
780 (setq younger-brothers (wl-thread-entity-get-younger-brothers
782 (setq wl-thread-entity-list
783 (append (append older-brothers children)
786 ;; delete myself from buffer.
787 (unless (wl-thread-delete-line-from-buffer msg)
788 ;; jump to suitable point.
789 ;; just upon the oldest younger-brother of my top.
790 (let ((younger-bros (wl-thread-entity-get-younger-brothers
791 (wl-thread-entity-get-top-entity entity)
794 (wl-summary-jump-to-msg (car younger-bros))
795 (goto-char (point-max)))) ; no younger brothers.
797 ;; insert children if thread is closed.
798 (when (not (wl-thread-entity-get-opened entity))
799 (setq children2 children)
801 (wl-thread-insert-entity 0 ; no mean now...
802 (wl-thread-get-entity
805 (setq children2 (cdr children2))))
810 ;; update parent on buffer.
812 (setq num (wl-thread-entity-get-number parent))
814 (wl-thread-update-line-on-buffer num)))
815 ;; update children lines on buffer.
817 (wl-thread-update-line-on-buffer
819 (wl-thread-entity-get-number parent)))
821 ;; don't update buffer
823 ;; return parent number
824 (list (wl-thread-entity-get-number parent))
826 ;; update the indent string
827 ; (wl-summary-goto-top-of-current-thread)
829 ; (wl-thread-goto-bottom-of-sub-thread)
830 ; (wl-thread-update-indent-string-region beg (point)))
834 (defun wl-thread-insert-message (overview-entity overview mark-alist
835 msg parent-msg &optional update)
836 "Insert MSG to the entity.
837 When optional argument UPDATE is non-nil,
838 Message is inserted to the summary buffer."
839 (let ((parent (wl-thread-get-entity parent-msg))
840 child-entity invisible-top)
841 ;; Update the thread view...not implemented yet.
844 ; (wl-thread-entity-force-open parent))
846 ;; insert as children.
847 (wl-thread-entity-insert-as-children
849 (setq child-entity (wl-thread-create-entity msg (nth 0 parent))))
850 ;; insert as top message.
851 (wl-thread-entity-insert-as-top
852 (wl-thread-create-entity msg nil)))
854 (if (not (setq invisible-top
855 (wl-thread-entity-parent-invisible-p child-entity)))
858 (wl-summary-update-thread
863 (elmo-msgdb-overview-get-entity-by-number overview parent-msg))
865 ;; use thread structure.
866 (wl-thread-entity-get-number
867 (wl-thread-entity-get-top-entity parent)))); return value;
868 ;; (setq beg (point))
869 ;; (wl-thread-goto-bottom-of-sub-thread)
870 ;; (wl-thread-update-indent-string-region beg (point)))
871 ;; currently invisible.. update closed line.
872 (wl-thread-update-children-number invisible-top)
875 (defun wl-thread-update-indent-string-thread (top-list)
878 (wl-summary-jump-to-msg (car top-list))
880 (wl-thread-goto-bottom-of-sub-thread)
881 (wl-thread-update-indent-string-region beg (point))
882 (setq top-list (cdr top-list)))))
884 (defun wl-thread-update-children-number (entity)
885 "Update the children number."
887 (wl-summary-jump-to-msg (wl-thread-entity-get-number entity))
889 (let ((text-prop (get-text-property (point) 'face))
890 from from-end beg str)
892 ((looking-at (concat "^" wl-summary-buffer-number-regexp
893 "..../..\(.*\)..:.. ["
894 wl-thread-indent-regexp
895 "]*\\[\\+\\([0-9]+\\):"))
896 (delete-region (match-beginning 1)(match-end 1))
897 (goto-char (match-beginning 1))
898 (setq str (format "%s" (wl-thread-entity-get-children-num entity)))
899 (if wl-summary-highlight
900 (put-text-property 0 (length str) 'face text-prop str))
902 ((looking-at (concat "^" wl-summary-buffer-number-regexp
903 "..../..\(.*\)..:.. ["
904 wl-thread-indent-regexp
906 (goto-char (match-end 0))
907 (setq beg (current-column))
908 (setq from-end (save-excursion
909 (move-to-column (+ 1 beg wl-from-width))
911 (setq from (buffer-substring (match-end 0) from-end))
912 (delete-region (match-end 0) from-end)
913 (setq str (wl-set-string-width
917 (wl-thread-entity-get-children-num
920 (if wl-summary-highlight
921 (put-text-property 0 (length str) 'face text-prop str))
923 (condition-case nil ; it's dangerous, so ignore error.
924 (run-hooks 'wl-thread-update-children-number-hook)
927 (message "Error in wl-thread-update-children-number-hook."))))))))
930 ;; Thread oriented commands.
932 (defun wl-thread-call-region-func (func &optional arg)
935 (wl-summary-goto-top-of-current-thread)
938 (wl-thread-goto-bottom-of-sub-thread)
939 (funcall func beg (point)))))
941 (defun wl-thread-prefetch (&optional arg)
943 (wl-thread-call-region-func 'wl-summary-prefetch-region arg))
945 (defun wl-thread-msg-mark-as-read (msg)
946 "Set mark as read for invisible MSG. Modeline is not changed."
947 (let* ((msgdb wl-summary-buffer-msgdb)
948 (mark-alist (elmo-msgdb-get-mark-alist msgdb))
950 (setq cur-mark (cadr (assq msg mark-alist)))
951 (cond ((or (string= cur-mark wl-summary-new-mark)
952 (string= cur-mark wl-summary-unread-uncached-mark))
955 (elmo-msgdb-mark-set mark-alist
957 (if (elmo-use-cache-p
958 wl-summary-buffer-folder-name
960 wl-summary-read-uncached-mark)))
961 (elmo-msgdb-set-mark-alist msgdb mark-alist)
962 (wl-summary-set-mark-modified))
963 ((string= cur-mark wl-summary-unread-cached-mark)
965 (setq mark-alist (elmo-msgdb-mark-set mark-alist msg nil))
966 (elmo-msgdb-set-mark-alist msgdb mark-alist)
967 (wl-summary-set-mark-modified)))))
969 (defun wl-thread-msg-mark-as-unread (msg)
970 "Set mark as unread for invisible MSG. Modeline is not changed."
971 (let* ((msgdb wl-summary-buffer-msgdb)
972 (mark-alist (elmo-msgdb-get-mark-alist msgdb))
974 (setq cur-mark (cadr (assq msg mark-alist)))
975 (cond ((string= cur-mark wl-summary-read-uncached-mark)
978 (elmo-msgdb-mark-set mark-alist
980 wl-summary-unread-uncached-mark))
981 (elmo-msgdb-set-mark-alist msgdb mark-alist)
982 (wl-summary-set-mark-modified))
985 (setq mark-alist (elmo-msgdb-mark-set mark-alist msg
986 wl-summary-unread-cached-mark))
987 (elmo-msgdb-set-mark-alist msgdb mark-alist)
988 (wl-summary-set-mark-modified)))))
990 (defun wl-thread-msg-mark-as-important (msg)
991 "Set mark as important for invisible MSG. Modeline is not changed."
992 (let* ((msgdb wl-summary-buffer-msgdb)
993 (mark-alist (elmo-msgdb-get-mark-alist msgdb))
995 (setq cur-mark (cadr (assq msg mark-alist)))
997 (elmo-msgdb-mark-set mark-alist
999 (if (string= cur-mark wl-summary-important-mark)
1001 wl-summary-important-mark)))
1002 (elmo-msgdb-set-mark-alist msgdb mark-alist)
1003 (wl-summary-set-mark-modified)))
1005 (defun wl-thread-mark-as-read (&optional arg)
1007 (wl-thread-call-region-func 'wl-summary-mark-as-read-region arg))
1009 (defun wl-thread-mark-as-unread (&optional arg)
1011 (wl-thread-call-region-func 'wl-summary-mark-as-unread-region arg))
1013 (defun wl-thread-mark-as-important (&optional arg)
1015 (wl-thread-call-region-func 'wl-summary-mark-as-important-region arg))
1017 (defun wl-thread-copy (&optional arg)
1019 (wl-thread-call-region-func 'wl-summary-copy-region arg))
1021 (defun wl-thread-refile (&optional arg)
1025 (wl-thread-call-region-func 'wl-summary-refile-region arg)
1027 (wl-summary-goto-top-of-current-thread))
1028 (wl-thread-goto-bottom-of-sub-thread))
1030 (elmo-display-error err t)
1033 (defun wl-thread-delete (&optional arg)
1035 (wl-thread-call-region-func 'wl-summary-delete-region arg)
1037 (wl-summary-goto-top-of-current-thread))
1038 (if (not wl-summary-move-direction-downward)
1040 (wl-thread-goto-bottom-of-sub-thread)
1041 (if wl-summary-buffer-disp-msg
1042 (wl-summary-redisplay))))
1044 (defun wl-thread-target-mark (&optional arg)
1046 (wl-thread-call-region-func 'wl-summary-target-mark-region arg))
1048 (defun wl-thread-unmark (&optional arg)
1050 (wl-thread-call-region-func 'wl-summary-unmark-region arg))
1052 (defun wl-thread-exec (&optional arg)
1054 (wl-thread-call-region-func 'wl-summary-exec-region arg))
1056 (defun wl-thread-save (&optional arg)
1058 (wl-thread-call-region-func 'wl-summary-save-region arg))
1060 (defun wl-thread-force-open (&optional msg-num)
1061 "force open current folder"
1063 (wl-summary-jump-to-msg msg-num))
1064 (let ((wl-thread-insert-force-opened t))
1065 (wl-thread-open-close)))
1067 (defun wl-thread-entity-force-open (entity)
1068 (let ((wl-thread-insert-force-opened t)
1070 (if (null (wl-thread-entity-get-parent entity))
1072 (if (and (not (wl-thread-entity-get-opened entity))
1073 (wl-thread-entity-get-children entity))
1074 (wl-thread-force-open (wl-thread-entity-get-number entity)))
1075 (if (setq notopen (wl-thread-entity-parent-invisible-p entity))
1076 (wl-thread-force-open (wl-thread-entity-get-number notopen))))))
1078 (defun wl-thread-insert-top ()
1079 (let ((elist wl-thread-entity-list)
1080 (len (length wl-thread-entity-list))
1082 (wl-delete-all-overlays)
1084 (wl-thread-insert-entity
1086 (wl-thread-get-entity (car elist))
1090 (elmo-display-progress
1091 'wl-thread-insert-top "Inserting thread..."
1092 (/ (* cur 100) len))
1093 (setq elist (cdr elist)))))
1095 (defsubst wl-thread-insert-entity-sub (indent entity parent-entity all)
1096 (let ((number-alist (elmo-msgdb-get-number-alist wl-summary-buffer-msgdb))
1097 (overview (elmo-msgdb-get-overview wl-summary-buffer-msgdb))
1098 (mark-alist (elmo-msgdb-get-mark-alist wl-summary-buffer-msgdb))
1105 (when (setq msg-num (wl-thread-entity-get-number entity))
1106 (unless all ; all...means no temp-mark.
1107 (cond ((memq msg-num wl-summary-buffer-delete-list)
1108 (setq temp-mark "D"))
1109 ((memq msg-num wl-summary-buffer-target-mark-list)
1110 (setq temp-mark "*"))
1111 ((assq msg-num wl-summary-buffer-refile-list)
1112 (setq temp-mark "o"))
1113 ((assq msg-num wl-summary-buffer-copy-list)
1114 (setq temp-mark "O"))))
1116 (setq temp-mark (wl-summary-get-score-mark msg-num)))
1117 (setq children-num (wl-thread-entity-get-children-num entity))
1118 (setq overview-entity
1119 (elmo-msgdb-search-overview-entity
1120 (nth 0 entity) number-alist overview))
1121 ;;(wl-delete-all-overlays)
1122 (when overview-entity
1124 (wl-summary-overview-create-summary-line
1127 (assoc ; parent-entity
1128 (cdr (assq (nth 0 parent-entity)
1129 number-alist)) overview)
1132 (if wl-thread-insert-force-opened
1134 (if (not (wl-thread-entity-get-opened entity))
1137 (wl-summary-insert-line summary-line)))))
1139 (defun wl-thread-insert-entity (indent entity parent-entity all)
1140 "Insert thread entity in current buffer."
1141 (let ((msgs (list (car entity)))
1142 children msgs-stack)
1144 (wl-thread-insert-entity-sub indent entity parent-entity all)
1145 (setq msgs (cdr msgs))
1146 (setq children (nth 2 entity))
1149 (when (or wl-thread-insert-force-opened
1150 (wl-thread-entity-get-opened entity))
1151 (wl-thread-entity-set-opened entity t)
1152 (wl-push msgs msgs-stack)
1155 parent-entity entity)))
1157 (while (and (null msgs) msgs-stack)
1158 (setq msgs (wl-pop msgs-stack))
1159 (setq indent (1- indent)))
1161 (setq entity (wl-thread-get-entity (car msgs)))
1162 (setq parent-entity (wl-thread-entity-get-parent-entity entity))))
1163 (setq entity (wl-thread-get-entity (car msgs))))))
1165 (defun wl-thread-descendant-p (mynumber number)
1166 (let ((cur (wl-thread-get-entity number))
1170 (setq cur (wl-thread-entity-get-parent-entity cur))
1171 (if (null (setq num (wl-thread-entity-get-number cur))) ; top!
1174 (eq mynumber (wl-thread-entity-get-number cur)))
1178 ; (defun wl-thread-goto-bottom-of-sub-thread ()
1180 ; (let ((depth (wl-thread-get-depth-of-current-line)))
1182 ; (while (and (not (eobp))
1183 ; (> (wl-thread-get-depth-of-current-line)
1186 ; (beginning-of-line)))
1188 (defun wl-thread-goto-bottom-of-sub-thread (&optional msg)
1190 (let ((mynumber (or msg (wl-summary-message-number))))
1192 (while (wl-thread-descendant-p mynumber (wl-summary-message-number))
1194 (beginning-of-line)))
1196 (defun wl-thread-remove-destination-region (beg end)
1199 (narrow-to-region beg end)
1200 (goto-char (point-min))
1202 (let ((num (wl-summary-message-number)))
1203 (if (assq num wl-summary-buffer-refile-list)
1204 (wl-summary-remove-destination)))
1205 (forward-line 1)))))
1207 (defun wl-thread-print-destination-region (beg end)
1208 (if (or wl-summary-buffer-refile-list
1209 wl-summary-buffer-copy-list)
1212 (narrow-to-region beg end)
1213 (goto-char (point-min))
1215 (let ((num (wl-summary-message-number))
1217 (if (or (setq pair (assq num wl-summary-buffer-refile-list))
1218 (setq pair (assq num wl-summary-buffer-copy-list)))
1219 (wl-summary-print-destination (car pair) (cdr pair))))
1220 (forward-line 1))))))
1222 (defsubst wl-thread-get-children-msgs (msg)
1223 (let ((msgs (list msg))
1227 (wl-append ret-val (list (car msgs)))
1228 (setq children (wl-thread-entity-get-children
1229 (wl-thread-get-entity (car msgs))))
1230 (setq msgs (cdr msgs))
1232 (while (and (null msgs) msgs-stack)
1233 (setq msgs (wl-pop msgs-stack)))
1234 (wl-push msgs msgs-stack)
1235 (setq msgs children)))
1238 (defun wl-thread-get-children-msgs-uncached (msg &optional uncached-marks)
1239 (let ((children-msgs (wl-thread-get-children-msgs msg))
1240 (mark-alist (elmo-msgdb-get-mark-alist wl-summary-buffer-msgdb))
1241 (number-alist (elmo-msgdb-get-number-alist wl-summary-buffer-msgdb))
1244 (while children-msgs
1245 (if (and (not (eq msg (car children-msgs))) ; except itself
1246 (or (and uncached-marks
1247 (setq mark (cadr (assq (car children-msgs)
1249 (member mark uncached-marks))
1250 (and (not uncached-marks)
1251 (null (elmo-cache-exists-p
1252 (cdr (assq (car children-msgs)
1254 (wl-append uncached-list (list (car children-msgs))))
1255 (setq children-msgs (cdr children-msgs)))
1258 (defun wl-thread-get-children-msgs-with-mark (msg mark)
1259 (let ((children-msgs (wl-thread-get-children-msgs msg))
1260 (check-func (cond ((string= mark "o")
1261 'wl-summary-msg-marked-as-refiled)
1263 'wl-summary-msg-marked-as-copied)
1265 'wl-summary-msg-marked-as-deleted)
1267 'wl-summary-msg-marked-as-target)))
1269 (while children-msgs
1270 (if (funcall check-func (car children-msgs))
1271 (wl-append ret-val (list (car children-msgs))))
1272 (setq children-msgs (cdr children-msgs)))
1275 (defun wl-thread-close (entity)
1277 (wl-thread-entity-set-opened entity nil)
1278 (setq depth (wl-thread-get-depth-of-current-line))
1281 (wl-thread-goto-bottom-of-sub-thread)
1282 (wl-thread-remove-destination-region beg
1284 (forward-char -1) ;; needed for mouse-face.
1285 (delete-region beg (point))
1286 (wl-thread-insert-entity (- depth 1)
1288 (wl-thread-get-entity
1291 (delete-char 1) ; delete '\n'
1292 (wl-thread-print-destination-region beg (point))))
1294 (defun wl-thread-open (entity)
1298 (setq depth (wl-thread-get-depth-of-current-line))
1300 (delete-region beg (point))
1301 (wl-thread-entity-set-opened entity t)
1302 (wl-thread-insert-entity depth ;(- depth 1)
1304 (wl-thread-get-entity
1305 (nth 3 entity)) nil)
1306 (delete-char 1) ; delete '\n'
1307 (wl-thread-print-destination-region beg (point))))
1309 (defun wl-thread-open-close (&optional force-open)
1311 (when (eq wl-summary-buffer-view 'thread)
1312 ;(if (equal wl-thread-top-entity '(nil t nil nil))
1313 ;(error "There's no thread structure."))
1315 (let ((inhibit-read-only t)
1316 (buffer-read-only nil)
1317 (wl-thread-insert-force-opened
1318 (or wl-thread-insert-force-opened
1320 msg entity beg depth parent)
1321 (setq msg (wl-summary-message-number))
1322 (setq entity (wl-thread-get-entity msg))
1323 (if (wl-thread-entity-get-opened entity)
1324 ;; if already opened, close its child!
1325 (if (wl-thread-entity-get-children entity)
1326 (wl-thread-close entity)
1327 ;; opened, but has no children, close its parent!
1328 (when (setq parent (wl-thread-entity-get-parent entity))
1329 (wl-summary-jump-to-msg parent)
1331 (wl-thread-get-entity (wl-summary-message-number)))))
1332 ;; if closed (or it is just a thread bottom message)
1333 ;; has children, open it!
1334 (if (wl-thread-entity-get-children entity)
1335 (wl-thread-open entity)
1336 ;; closed, and has no children, close its parent!
1337 (setq msg (or (wl-thread-entity-get-parent entity)
1338 (wl-thread-entity-get-number entity)))
1340 (wl-summary-jump-to-msg msg)
1342 (wl-thread-get-entity (wl-summary-message-number)))))))
1343 (wl-summary-set-message-modified)
1344 (set-buffer-modified-p nil))))
1347 (defun wl-thread-get-depth-of-current-line ()
1352 (if (re-search-forward (concat "^" wl-summary-buffer-number-regexp
1353 "..../..\(.*\)..:.. ")
1355 (while (string-match wl-thread-indent-regexp
1357 (char-after (point))))
1358 (setq depth (1+ depth))
1360 (/ depth wl-thread-indent-level-internal))))
1362 (defun wl-thread-update-indent-string-region (beg end)
1366 (while (< (point) end)
1367 (wl-thread-update-indent-string)
1370 (defsubst wl-thread-make-indent-string (entity)
1373 (space-str (wl-repeat-string wl-thread-space-str-internal
1374 (- wl-thread-indent-level-internal 1)))
1376 (when (wl-thread-entity-get-number
1377 (setq parent (wl-thread-entity-get-parent-entity cur)))
1378 (if (wl-thread-entity-get-younger-brothers cur)
1379 (setq ret-val wl-thread-have-younger-brother-str-internal)
1380 (setq ret-val wl-thread-youngest-child-str-internal))
1381 (setq ret-val (concat ret-val
1383 wl-thread-horizontal-str-internal
1384 (- wl-thread-indent-level-internal 1))))
1386 (while (wl-thread-entity-get-number
1387 (wl-thread-entity-get-parent-entity cur))
1388 (if (wl-thread-entity-get-younger-brothers cur)
1389 (setq ret-val (concat wl-thread-vertical-str-internal
1392 (setq ret-val (concat wl-thread-space-str-internal
1395 (setq cur (wl-thread-entity-get-parent-entity cur))))
1398 (defun wl-thread-update-indent-string ()
1399 "Update indent string of current line."
1403 (let ((inhibit-read-only t)
1404 (buffer-read-only nil)
1406 (when (looking-at (concat "^ *\\([0-9]+\\)"
1407 "..../..\(.*\)..:.. \\("
1408 wl-highlight-thread-indent-string-regexp
1410 (goto-char (match-beginning 2))
1411 (delete-region (match-beginning 2)
1414 (wl-thread-make-indent-string
1415 (wl-thread-get-entity (string-to-int (wl-match-buffer 1)))))
1416 (if (and wl-summary-width
1417 wl-summary-indent-length-limit
1418 (< wl-summary-indent-length-limit
1419 (string-width thr-str)))
1420 (setq thr-str (wl-set-string-width
1421 wl-summary-indent-length-limit
1424 (if wl-summary-highlight
1425 (wl-highlight-summary-current-line))))))
1427 (provide 'wl-thread)
1429 ;;; wl-thread.el ends here