1 ;;; wl-thread.el -- Thread display modules for Wanderlust.
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>
6 ;; Author: Yuuichi Teranishi <teranisi@gohome.org>
7 ;; Masahiro MURATA <muse@ba2.so-net.ne.jp>
8 ;; Keywords: mail, net news
10 ;; This file is part of Wanderlust (Yet Another Message Interface on Emacsen).
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)
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.
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.
35 (require 'wl-highlight)
36 (eval-when-compile (require 'cl))
38 ;; buffer local variables.
39 ;;(defvar wl-thread-top-entity '(nil t nil nil)) ; top entity
40 (defvar wl-thread-tops nil) ; top number list (number)
41 (defvar wl-thread-entities nil)
42 (defvar wl-thread-entity-list nil) ; entity list
43 (defvar wl-thread-entity-hashtb nil) ; obarray
44 (defvar wl-thread-indent-regexp nil)
46 (make-variable-buffer-local 'wl-thread-entity-hashtb)
47 (make-variable-buffer-local 'wl-thread-entities) ; ".wl-thread-entity"
48 (make-variable-buffer-local 'wl-thread-entity-list) ; ".wl-thread-entity-list"
49 (make-variable-buffer-local 'wl-thread-entity-cur)
50 (make-variable-buffer-local 'wl-thread-indent-regexp)
53 (defvar wl-thread-insert-force-opened nil)
55 ;;;;;; each entity is (number opened-or-not children parent) ;;;;;;;
57 (defun wl-thread-resume-entity (fld)
58 (let (entities top-list)
59 (setq entities (wl-summary-load-file-object
60 (expand-file-name wl-thread-entity-file
61 (elmo-msgdb-expand-path fld))))
63 (wl-summary-load-file-object
64 (expand-file-name wl-thread-entity-list-file
65 (elmo-msgdb-expand-path fld))))
66 (message "Resuming thread structure...")
68 (setq wl-thread-entity-hashtb (elmo-make-hash (* (length entities) 2)))
69 ;; set buffer local variables.
70 (setq wl-thread-entities entities)
71 (setq wl-thread-entity-list top-list)
73 (elmo-set-hash-val (format "#%d" (car (car entities))) (car entities)
74 wl-thread-entity-hashtb)
75 (setq entities (cdr entities)))
76 (wl-thread-make-number-list)
77 (message "Resuming thread structure...done")))
79 (defun wl-thread-make-number-list ()
80 "Make `wl-summary-buffer-number-list', a list of message numbers."
81 (let* ((node (wl-thread-get-entity (car wl-thread-entity-list)))
82 (children (wl-thread-entity-get-children node))
84 (setq wl-summary-buffer-number-list (list (car wl-thread-entity-list)))
86 (wl-thread-entity-make-number-list-from-children
87 (wl-thread-get-entity (car children)))
88 (setq children (cdr children)))
90 (setq parent (wl-thread-entity-get-parent-entity node)
91 sibling (wl-thread-entity-get-younger-brothers
94 (wl-thread-entity-make-number-list-from-children
95 (wl-thread-get-entity (car sibling)))
96 (setq sibling (cdr sibling)))
98 (setq wl-summary-buffer-number-list (nreverse
99 wl-summary-buffer-number-list))))
101 (defun wl-thread-entity-make-number-list-from-children (entity)
102 (let ((msgs (list (car entity)))
105 (setq wl-summary-buffer-number-list (cons (car entity)
106 wl-summary-buffer-number-list))
107 (setq msgs (cdr msgs))
108 (setq children (wl-thread-entity-get-children entity))
111 (wl-push msgs msgs-stack)
112 (setq msgs children))
114 (while (and (null msgs) msgs-stack)
115 (setq msgs (wl-pop msgs-stack)))))
116 (setq entity (wl-thread-get-entity (car msgs))))))
118 (defun wl-thread-save-entity (dir)
119 (wl-thread-save-entities dir)
120 (wl-thread-save-top-list dir))
122 (defun wl-thread-save-top-list (dir)
123 (let ((top-file (expand-file-name wl-thread-entity-list-file dir))
124 (entity wl-thread-entity-list)
125 (tmp-buffer (get-buffer-create " *wl-thread-save-top-list*")))
127 (set-buffer tmp-buffer)
129 (when (file-writable-p top-file)
130 (prin1 entity tmp-buffer)
131 (princ "\n" tmp-buffer)
132 (write-region (point-min) (point-max) top-file nil 'no-msg)
133 (kill-buffer tmp-buffer)))))
135 (defun wl-thread-save-entities (dir)
136 (let ((top-file (expand-file-name wl-thread-entity-file dir))
137 (entities wl-thread-entities)
138 (tmp-buffer (get-buffer-create " *wl-thread-save-entities*")))
140 (set-buffer tmp-buffer)
142 (when (file-writable-p top-file)
143 (prin1 entities tmp-buffer)
144 (princ "\n" tmp-buffer)
145 (write-region (point-min) (point-max) top-file nil 'no-msg)
146 (kill-buffer tmp-buffer)))))
148 (defsubst wl-thread-entity-get-number (entity)
150 (defsubst wl-thread-entity-get-opened (entity)
152 (defsubst wl-thread-entity-get-children (entity)
154 (defsubst wl-thread-entity-get-parent (entity)
156 (defsubst wl-thread-entity-get-linked (entity)
159 (defsubst wl-thread-create-entity (num parent &optional opened linked)
160 (list num (or opened wl-thread-insert-opened) nil parent linked))
162 (defsubst wl-thread-get-entity (num)
164 (elmo-get-hash-val (format "#%d" num) wl-thread-entity-hashtb)))
166 (defsubst wl-thread-entity-set-parent (entity parent)
167 (setcar (cdddr entity) parent)
170 (defsubst wl-thread-entity-set-children (entity children)
171 (setcar (cddr entity) children))
173 (defsubst wl-thread-entity-set-linked (entity linked)
175 (setcar (cddddr entity) linked)
176 (nconc entity (list linked)))
179 (defsubst wl-thread-reparent-children (children parent)
181 (wl-thread-entity-set-parent
182 (wl-thread-get-entity (car children)) parent)
183 (wl-thread-entity-set-linked
184 (wl-thread-get-entity (car children)) t)
185 (setq children (cdr children))))
187 (defsubst wl-thread-entity-insert-as-top (entity)
190 (wl-append wl-thread-entity-list (list (car entity)))
191 (setq wl-thread-entities (cons entity wl-thread-entities))
192 (setq wl-summary-buffer-number-list
193 (nconc wl-summary-buffer-number-list (list (car entity))))
194 (elmo-set-hash-val (format "#%d" (car entity)) entity
195 wl-thread-entity-hashtb)))
197 (defsubst wl-thread-entity-insert-as-children (to entity)
198 (let ((children (wl-thread-entity-get-children to))
201 (elmo-list-insert wl-summary-buffer-number-list
202 (wl-thread-entity-get-number entity)
205 (wl-thread-entity-get-children curp))
206 (setq curp (wl-thread-get-entity
207 (nth (- (length curc) 1)
209 (wl-thread-entity-get-number curp)))
210 (setcar (cddr to) (wl-append children
211 (list (car entity))))
212 (setq wl-thread-entities (cons entity wl-thread-entities))
213 (elmo-set-hash-val (format "#%d" (car entity)) entity
214 wl-thread-entity-hashtb)))
216 (defsubst wl-thread-entity-set-opened (entity opened)
217 (setcar (cdr entity) opened))
219 (defsubst wl-thread-entity-get-children-num (entity)
222 (msgs (list (car entity))))
224 (setq msgs (cdr msgs))
225 (setq children (wl-thread-entity-get-children entity))
227 (while (and (null msgs) msgs-stack)
228 (setq msgs (wl-pop msgs-stack)))
229 (setq ret-val (+ (or ret-val 0) (length children)))
230 (wl-push msgs msgs-stack)
231 (setq msgs children))
232 (setq entity (wl-thread-get-entity (car msgs))))
235 (defsubst wl-thread-entity-get-descendant (entity)
238 (msgs (list (car entity))))
240 (setq msgs (cdr msgs))
241 (setq children (wl-thread-entity-get-children entity))
243 (while (and (null msgs) msgs-stack)
244 (setq msgs (wl-pop msgs-stack)))
245 (setq ret-val (append ret-val (copy-sequence children)))
246 (wl-push msgs msgs-stack)
247 (setq msgs children))
248 (setq entity (wl-thread-get-entity (car msgs))))
251 (defsubst wl-thread-entity-get-parent-entity (entity)
252 (wl-thread-get-entity (wl-thread-entity-get-parent entity)))
254 (defun wl-thread-entity-get-top-entity (entity)
255 (let ((cur-entity entity)
257 (while (setq p-num (wl-thread-entity-get-parent cur-entity))
258 (setq cur-entity (wl-thread-get-entity p-num)))
261 (defun wl-thread-entity-parent-invisible-p (entity)
262 "If parent of ENTITY is invisible, the top invisible ancestor entity of
264 (let ((cur-entity entity)
267 (while (setq cur-entity (wl-thread-entity-get-parent-entity
269 (if (null (wl-thread-entity-get-number cur-entity))
274 (when (not (wl-thread-entity-get-opened cur-entity))
276 (setq ret-val cur-entity)))))
277 ;; top of closed entity in the path.
280 (defun wl-thread-entity-get-nearly-older-brother (entity &optional parent)
281 (let ((brothers (wl-thread-entity-get-older-brothers entity parent)))
283 (car (last brothers)))))
285 (defun wl-thread-entity-get-older-brothers (entity &optional parent)
286 (let* ((parent (or parent
287 (wl-thread-entity-get-parent-entity entity)))
288 (brothers (wl-thread-entity-get-children parent))
292 (setq brothers wl-thread-entity-list))
294 (not (eq (wl-thread-entity-get-number entity)
296 (wl-append ret-val (list (car brothers)))
297 (setq brothers (cdr brothers)))
300 (defun wl-thread-entity-get-younger-brothers (entity &optional parent)
301 (let* ((parent (or parent
302 (wl-thread-entity-get-parent-entity entity)))
303 (brothers (wl-thread-entity-get-children parent)))
305 (cdr (memq (wl-thread-entity-get-number entity)
308 (cdr (memq (car entity) wl-thread-entity-list)))))
310 (defun wl-thread-jump-to-msg (&optional number)
312 (let ((num (or number
314 (read-from-minibuffer "Jump to Message(No.): ")))))
315 (wl-thread-entity-force-open (wl-thread-get-entity num))
316 (wl-summary-jump-to-msg num)))
318 (defun wl-thread-close-all ()
319 "Close all top threads."
321 (message "Closing all threads...")
323 (let ((entities wl-thread-entity-list)
325 (len (length wl-thread-entity-list)))
327 (when (and (wl-thread-entity-get-opened (wl-thread-get-entity
329 (wl-thread-entity-get-children (wl-thread-get-entity
331 (wl-summary-jump-to-msg (car entities))
332 (wl-thread-open-close))
333 (when (> len elmo-display-progress-threshold)
335 (if (or (zerop (% cur 5)) (= cur len))
336 (elmo-display-progress
337 'wl-thread-close-all "Closing all threads..."
338 (/ (* cur 100) len))))
339 (setq entities (cdr entities)))))
340 (message "Closing all threads...done"))
342 (defun wl-thread-open-all ()
345 (message "Opening all threads...")
347 (goto-char (point-min))
348 (let ((len (count-lines (point-min) (point-max)))
352 (unless (wl-thread-entity-get-opened
353 (setq entity (wl-thread-get-entity
354 (wl-summary-message-number))))
355 (wl-thread-entity-force-open entity))
356 (wl-thread-goto-bottom-of-sub-thread)
357 (when (> len elmo-display-progress-threshold)
359 (elmo-display-progress
360 'wl-thread-open-all "Opening all threads..."
361 (/ (* cur 100) len)))))
362 ;; Make sure to be 100%.
363 (elmo-display-progress
364 'wl-thread-open-all "Opening all threads..."
366 (message "Opening all threads...done"))
368 (defun wl-thread-open-all-unread ()
370 (let ((mark-alist (elmo-msgdb-get-mark-alist wl-summary-buffer-msgdb))
373 (if (setq mark (nth 1 (car mark-alist)))
374 (if (or (string= mark wl-summary-unread-uncached-mark)
375 (string= mark wl-summary-unread-cached-mark)
376 (string= mark wl-summary-new-mark)
377 (string= mark wl-summary-important-mark))
378 (wl-thread-entity-force-open (wl-thread-get-entity
379 (nth 0 (car mark-alist))))))
380 (setq mark-alist (cdr mark-alist)))))
382 (defsubst wl-thread-maybe-get-children-num (msg)
383 (let ((entity (wl-thread-get-entity msg)))
384 (if (not (wl-thread-entity-get-opened entity))
385 (wl-thread-entity-get-children-num entity))))
387 (defsubst wl-thread-update-line-on-buffer-sub (entity msg &optional parent-msg)
388 (let* ((entity (or entity (wl-thread-get-entity msg)))
389 (parent-msg (or parent-msg (wl-thread-entity-get-parent entity)))
390 (overview (elmo-msgdb-get-overview wl-summary-buffer-msgdb))
391 (mark-alist (elmo-msgdb-get-mark-alist wl-summary-buffer-msgdb))
392 (buffer-read-only nil)
393 (inhibit-read-only t)
394 overview-entity temp-mark summary-line invisible-top dest-pair)
395 (if (wl-thread-delete-line-from-buffer msg)
398 ((memq msg wl-summary-buffer-delete-list)
399 (setq temp-mark "D"))
400 ((memq msg wl-summary-buffer-target-mark-list)
401 (setq temp-mark "*"))
402 ((setq dest-pair (assq msg wl-summary-buffer-refile-list))
403 (setq temp-mark "o"))
404 ((setq dest-pair (assq msg wl-summary-buffer-copy-list))
405 (setq temp-mark "O"))
406 (t (setq temp-mark (wl-summary-get-score-mark msg))))
407 (when (setq overview-entity
408 (elmo-msgdb-overview-get-entity
409 msg wl-summary-buffer-msgdb))
411 (wl-summary-overview-create-summary-line
414 (elmo-msgdb-overview-get-entity
415 parent-msg wl-summary-buffer-msgdb)
418 (if wl-thread-insert-force-opened
420 (wl-thread-maybe-get-children-num msg))
423 (wl-summary-insert-line summary-line))
425 (wl-summary-print-destination (car dest-pair)
427 ;; insert thread (moving thread)
428 (if (not (setq invisible-top
429 (wl-thread-entity-parent-invisible-p entity)))
430 (wl-summary-update-thread
431 (elmo-msgdb-overview-get-entity msg wl-summary-buffer-msgdb)
436 (elmo-msgdb-overview-get-entity
437 parent-msg wl-summary-buffer-msgdb)))
438 ;; currently invisible.. update closed line.
439 (wl-thread-update-children-number invisible-top)))))
441 (defun wl-thread-update-line-on-buffer (&optional msg parent-msg updates)
443 (let ((msgs (list (or msg (wl-summary-message-number))))
444 entity children msgs-stack)
446 (setq msg (wl-pop msgs))
447 (setq updates (and updates (delete msg updates)))
448 (setq entity (wl-thread-get-entity msg))
449 (wl-thread-update-line-on-buffer-sub entity msg parent-msg)
451 (setq children (wl-thread-entity-get-children entity))
454 (when (wl-thread-entity-get-opened entity)
455 (wl-push msgs msgs-stack)
459 (while (and (null msgs) msgs-stack)
460 (setq msgs (wl-pop msgs-stack)))
463 (wl-thread-entity-get-number
464 (wl-thread-entity-get-parent-entity
465 (wl-thread-get-entity (car msgs)))))))))
468 (defun wl-thread-update-line-msgs (msgs &optional no-msg)
469 (wl-delete-all-overlays)
476 ;;; (wl-thread-get-children-msgs (car msgs))))
477 ;;; (setq msgs (cdr msgs)))
478 ;;; (setq updates (elmo-uniq-list updates))
479 (setq len (length updates))
481 (wl-thread-update-line-on-buffer-sub nil (car updates))
482 (setq updates (cdr updates))
483 (when (and (not no-msg)
484 (> len elmo-display-progress-threshold))
486 (if (or (zerop (% i 5)) (= i len))
487 (elmo-display-progress
488 'wl-thread-update-line-msgs "Updating deleted thread..."
489 (/ (* i 100) len)))))))
491 (defun wl-thread-delete-line-from-buffer (msg)
492 "Simply delete msg line."
494 (if (wl-summary-jump-to-msg msg)
498 (delete-region beg (point))
502 (defun wl-thread-cleanup-symbols (msgs)
505 (when (setq entity (wl-thread-get-entity (car msgs)))
507 (setq wl-thread-entities (delq entity wl-thread-entities))
509 (elmo-clear-hash-val (format "#%d" (car msgs))
510 wl-thread-entity-hashtb))
511 (setq msgs (cdr msgs)))))
513 (defun wl-thread-get-exist-children (msg)
514 (let ((msgs (list msg))
518 (setq children (wl-thread-entity-get-children
519 (setq entity (wl-thread-get-entity (car msgs)))))
520 (when (elmo-msgdb-overview-get-entity (car msgs) wl-summary-buffer-msgdb)
521 (wl-append ret-val (list (car msgs)))
523 (setq msgs (cdr msgs))
525 (while (and (null msgs) msgs-stack)
526 (setq msgs (wl-pop msgs-stack)))
527 (wl-push msgs msgs-stack)
528 (setq msgs children)))
531 (defun wl-thread-delete-message (msg &optional deep update)
532 "Delete MSG from entity and buffer."
534 (let* ((entity (wl-thread-get-entity msg))
535 children older-brothers younger-brothers top-child ;;grandchildren
536 top-entity parent update-msgs beg invisible-top)
538 (setq parent (wl-thread-entity-get-parent-entity entity))
542 ;;; (setq brothers (wl-thread-entity-get-children parent))
543 (setq older-brothers (wl-thread-entity-get-older-brothers
545 (setq younger-brothers (wl-thread-entity-get-younger-brothers
549 (setq children (wl-thread-entity-get-children entity))
550 (wl-thread-reparent-children
551 children (wl-thread-entity-get-number parent))
553 (apply (function nconc)
558 (wl-thread-get-children-msgs message t)))
560 (wl-thread-entity-set-children
561 parent (append older-brothers children younger-brothers))
562 ;; If chidren and younger-brothers not exists,
563 ;; update nearly older brother.
564 (when (and older-brothers
565 (not younger-brothers)
569 (wl-thread-get-children-msgs (car (last older-brothers))))))
571 ;; top...oldest child becomes top.
573 (setq children (wl-thread-entity-get-children entity))
575 (setq top-child (car children)
576 children (cdr children))
577 (setq top-entity (wl-thread-get-entity top-child))
578 (wl-thread-entity-set-parent top-entity nil)
579 (wl-thread-entity-set-linked top-entity nil)
580 (wl-append update-msgs
581 (wl-thread-get-children-msgs top-child t)))
583 (wl-thread-entity-set-children
586 (wl-thread-entity-get-children top-entity)
588 (wl-thread-reparent-children children top-child)
589 (wl-append update-msgs children)))
590 ;; delete myself from top list.
591 (setq wl-summary-buffer-number-list
592 (delq msg wl-summary-buffer-number-list))
593 (setq older-brothers (wl-thread-entity-get-older-brothers
595 (setq younger-brothers (wl-thread-entity-get-younger-brothers
597 (setq wl-thread-entity-list
598 (append (append older-brothers
599 (and top-child (list top-child)))
603 ;; delete thread on buffer
604 (when (wl-summary-jump-to-msg msg)
606 (wl-thread-goto-bottom-of-sub-thread)
607 (delete-region beg (point)))
608 ;; delete myself from buffer.
609 (unless (wl-thread-delete-line-from-buffer msg)
610 ;; jump to suitable point.
611 ;; just upon the oldest younger-brother of my top.
613 (car (wl-thread-entity-parent-invisible-p entity)))
616 (wl-append update-msgs (list invisible-top))
617 (wl-summary-jump-to-msg invisible-top))
618 (goto-char (point-max))))
620 ;; insert children if thread is closed or delete top.
622 (not (wl-thread-entity-get-opened entity)))
623 (let* (next-top insert-msgs ent e grandchildren)
626 (setq insert-msgs (wl-thread-get-exist-children top-child))
627 (setq next-top (car insert-msgs))
628 (setq ent (wl-thread-get-entity next-top))
630 (wl-thread-entity-get-opened entity) ;; open
631 (not (wl-thread-entity-get-opened ent)) ;; close
633 (wl-thread-entity-get-children ent))
634 (wl-summary-jump-to-msg next-top))
636 (setq insert-msgs (append (cdr insert-msgs) grandchildren)))
637 (when top-entity (wl-thread-entity-set-opened top-entity t))
638 (when ent (wl-thread-entity-set-opened ent t)))
639 (when (not invisible-top)
640 (setq insert-msgs (wl-thread-get-exist-children msg))
641 ;; First msg always opened, because first msg maybe becomes top.
642 (if (setq ent (wl-thread-get-entity (car insert-msgs)))
643 (wl-thread-entity-set-opened ent t))))
646 ;; if no exists in summary, insert entity.
647 (when (and (car insert-msgs)
648 (not (wl-summary-jump-to-msg (car insert-msgs))))
649 (setq ent (wl-thread-get-entity (car insert-msgs)))
650 (wl-thread-insert-entity 0 ; no mean now...
652 (setq insert-msgs (cdr insert-msgs))))))
656 (wl-thread-update-line-on-buffer-sub nil (pop update-msgs)))
657 ;; don't update buffer
658 update-msgs)))) ; return value
660 (defun wl-thread-insert-message (overview-entity overview mark-alist
661 msg parent-msg &optional update linked)
662 "Insert MSG to the entity.
663 When optional argument UPDATE is non-nil,
664 Message is inserted to the summary buffer."
665 (let ((parent (wl-thread-get-entity parent-msg))
666 child-entity invisible-top)
667 ;;; Update the thread view...not implemented yet.
668 ;;; (when force-insert
670 ;;; (wl-thread-entity-force-open parent))
672 ;; insert as children.
673 (wl-thread-entity-insert-as-children
675 (setq child-entity (wl-thread-create-entity msg (nth 0 parent) nil linked)))
676 ;; insert as top message.
677 (wl-thread-entity-insert-as-top
678 (wl-thread-create-entity msg nil)))
680 (if (not (setq invisible-top
681 (wl-thread-entity-parent-invisible-p child-entity)))
684 (wl-summary-update-thread
689 (elmo-msgdb-overview-get-entity
690 parent-msg wl-summary-buffer-msgdb))
692 ;; use thread structure.
693 (wl-thread-entity-get-nearly-older-brother
694 child-entity parent))) ; return value
695 ;;; (wl-thread-entity-get-number
696 ;;; (wl-thread-entity-get-top-entity parent)))) ; return value;
697 ;;; (setq beg (point))
698 ;;; (wl-thread-goto-bottom-of-sub-thread)
699 ;;; (wl-thread-update-indent-string-region beg (point)))
700 ;; currently invisible.. update closed line.
701 (wl-thread-update-children-number invisible-top)
704 (defun wl-thread-get-parent-list (msgs)
708 (setq myself (car msgs2)
710 (while (not (eq myself (car msgs2)))
711 (if (wl-thread-descendant-p myself (car msgs2))
712 (setq msgs (delq (car msgs2) msgs)))
713 (setq msgs2 (or (cdr msgs2) msgs)))
714 (setq msgs2 (cdr msgs2)))
717 (defun wl-thread-update-indent-string-thread (top-list)
718 (let ((top-list (wl-thread-get-parent-list top-list))
722 (wl-summary-jump-to-msg (car top-list))
724 (wl-thread-goto-bottom-of-sub-thread)
725 (wl-thread-update-indent-string-region beg (point)))
726 (setq top-list (cdr top-list)))))
728 (defun wl-thread-update-children-number (entity)
729 "Update the children number."
731 (wl-summary-jump-to-msg (wl-thread-entity-get-number entity))
733 (let ((text-prop (get-text-property (point) 'face))
734 from from-end beg str)
736 ((looking-at (concat "^" wl-summary-buffer-number-regexp
737 "..../..\(.*\)..:.. ["
738 wl-thread-indent-regexp
739 "]*[[<]\\+\\([0-9]+\\):"))
740 (delete-region (match-beginning 1)(match-end 1))
741 (goto-char (match-beginning 1))
742 (setq str (format "%s" (wl-thread-entity-get-children-num entity)))
743 (if wl-summary-highlight
744 (put-text-property 0 (length str) 'face text-prop str))
746 ((looking-at (concat "^" wl-summary-buffer-number-regexp
747 "..../..\(.*\)..:.. ["
748 wl-thread-indent-regexp
750 (goto-char (match-end 0))
751 (setq beg (current-column))
752 (setq from-end (save-excursion
753 (move-to-column (+ 1 beg wl-from-width))
755 (setq from (buffer-substring (match-end 0) from-end))
756 (delete-region (match-end 0) from-end)
757 (setq str (wl-set-string-width
761 (wl-thread-entity-get-children-num
764 (if wl-summary-highlight
765 (put-text-property 0 (length str) 'face text-prop str))
767 (condition-case nil ; it's dangerous, so ignore error.
768 (run-hooks 'wl-thread-update-children-number-hook)
771 (message "Error in wl-thread-update-children-number-hook."))))))))
774 ;; Thread oriented commands.
776 (defun wl-thread-call-region-func (func &optional arg)
779 (wl-summary-goto-top-of-current-thread)
782 (wl-thread-goto-bottom-of-sub-thread)
783 (funcall func beg (point)))))
785 (defun wl-thread-prefetch (&optional arg)
787 (wl-thread-call-region-func 'wl-summary-prefetch-region arg))
789 (defun wl-thread-msg-mark-as-important (msg)
790 "Set mark as important for invisible MSG. Modeline is not changed."
791 (let* ((msgdb wl-summary-buffer-msgdb)
792 (mark-alist (elmo-msgdb-get-mark-alist msgdb))
794 (setq cur-mark (cadr (assq msg mark-alist)))
796 (elmo-msgdb-mark-set mark-alist
798 (if (string= cur-mark wl-summary-important-mark)
800 wl-summary-important-mark)))
801 (elmo-msgdb-set-mark-alist msgdb mark-alist)
802 (wl-summary-set-mark-modified)))
804 (defun wl-thread-mark-as-read (&optional arg)
806 (wl-thread-call-region-func 'wl-summary-mark-as-read-region arg))
808 (defun wl-thread-mark-as-unread (&optional arg)
810 (wl-thread-call-region-func 'wl-summary-mark-as-unread-region arg))
812 (defun wl-thread-mark-as-important (&optional arg)
814 (wl-thread-call-region-func 'wl-summary-mark-as-important-region arg))
816 (defun wl-thread-copy (&optional arg)
818 (wl-thread-call-region-func 'wl-summary-copy-region arg))
820 (defun wl-thread-refile (&optional arg)
824 (wl-thread-call-region-func 'wl-summary-refile-region arg)
826 (wl-summary-goto-top-of-current-thread))
827 (wl-thread-goto-bottom-of-sub-thread))
829 (elmo-display-error err t)
832 (defun wl-thread-delete (&optional arg)
834 (wl-thread-call-region-func 'wl-summary-delete-region arg)
836 (wl-summary-goto-top-of-current-thread))
837 (if (not wl-summary-move-direction-downward)
839 (wl-thread-goto-bottom-of-sub-thread)
840 (if wl-summary-buffer-disp-msg
841 (wl-summary-redisplay))))
843 (defun wl-thread-target-mark (&optional arg)
845 (wl-thread-call-region-func 'wl-summary-target-mark-region arg))
847 (defun wl-thread-unmark (&optional arg)
849 (wl-thread-call-region-func 'wl-summary-unmark-region arg))
851 (defun wl-thread-exec (&optional arg)
853 (wl-thread-call-region-func 'wl-summary-exec-region arg))
855 (defun wl-thread-save (&optional arg)
857 (wl-thread-call-region-func 'wl-summary-save-region arg))
859 (defun wl-thread-force-open (&optional msg-num)
860 "force open current folder"
862 (wl-summary-jump-to-msg msg-num))
863 (let ((wl-thread-insert-force-opened t))
864 (wl-thread-open-close)))
866 (defun wl-thread-entity-force-open (entity)
867 (let ((wl-thread-insert-force-opened t)
869 (if (null (wl-thread-entity-get-parent entity))
871 (if (and (not (wl-thread-entity-get-opened entity))
872 (wl-thread-entity-get-children entity))
873 (wl-thread-force-open (wl-thread-entity-get-number entity)))
874 (if (setq notopen (wl-thread-entity-parent-invisible-p entity))
875 (wl-thread-force-open (wl-thread-entity-get-number notopen))))))
877 (defun wl-thread-insert-top ()
878 (let ((elist wl-thread-entity-list)
879 (len (length wl-thread-entity-list))
881 (wl-delete-all-overlays)
883 (wl-thread-insert-entity
885 (wl-thread-get-entity (car elist))
888 (setq elist (cdr elist))
889 (when (> len elmo-display-progress-threshold)
891 (if (or (zerop (% cur 2)) (= cur len))
892 (elmo-display-progress
893 'wl-thread-insert-top "Inserting thread..."
894 (/ (* cur 100) len)))))))
896 (defsubst wl-thread-insert-entity-sub (indent entity parent-entity all)
897 (let ((mark-alist (elmo-msgdb-get-mark-alist wl-summary-buffer-msgdb))
902 (when (setq msg-num (wl-thread-entity-get-number entity))
903 (unless all ; all...means no temp-mark.
904 (cond ((memq msg-num wl-summary-buffer-delete-list)
905 (setq temp-mark "D"))
906 ((memq msg-num wl-summary-buffer-target-mark-list)
907 (setq temp-mark "*"))
908 ((assq msg-num wl-summary-buffer-refile-list)
909 (setq temp-mark "o"))
910 ((assq msg-num wl-summary-buffer-copy-list)
911 (setq temp-mark "O"))))
913 (setq temp-mark (wl-summary-get-score-mark msg-num)))
914 (setq overview-entity
915 (elmo-msgdb-overview-get-entity
916 (nth 0 entity) wl-summary-buffer-msgdb))
917 ;;; (wl-delete-all-overlays)
918 (when overview-entity
920 (wl-summary-overview-create-summary-line
923 (elmo-msgdb-overview-get-entity
924 (nth 0 parent-entity) wl-summary-buffer-msgdb)
927 (if wl-thread-insert-force-opened
929 (wl-thread-maybe-get-children-num msg-num))
931 (wl-summary-insert-line summary-line)))))
933 (defun wl-thread-insert-entity (indent entity parent-entity all)
934 "Insert thread entity in current buffer."
935 (let ((msgs (list (car entity)))
938 (wl-thread-insert-entity-sub indent entity parent-entity all)
939 (setq msgs (cdr msgs))
940 (setq children (nth 2 entity))
943 (when (or wl-thread-insert-force-opened
944 (wl-thread-entity-get-opened entity))
945 (wl-thread-entity-set-opened entity t)
946 (wl-push msgs msgs-stack)
949 parent-entity entity)))
951 (while (and (null msgs) msgs-stack)
952 (setq msgs (wl-pop msgs-stack))
953 (setq indent (1- indent)))
955 (setq entity (wl-thread-get-entity (car msgs)))
956 (setq parent-entity (wl-thread-entity-get-parent-entity entity))))
957 (setq entity (wl-thread-get-entity (car msgs))))))
959 (defun wl-thread-descendant-p (mynumber number)
960 (let ((cur (wl-thread-get-entity number))
964 (setq cur (wl-thread-entity-get-parent-entity cur))
965 (if (null (setq num (wl-thread-entity-get-number cur))) ; top!
968 (eq mynumber (wl-thread-entity-get-number cur)))
972 ;; (defun wl-thread-goto-bottom-of-sub-thread ()
974 ;; (let ((depth (wl-thread-get-depth-of-current-line)))
976 ;; (while (and (not (eobp))
977 ;; (> (wl-thread-get-depth-of-current-line)
980 ;; (beginning-of-line)))
982 (defun wl-thread-goto-bottom-of-sub-thread (&optional msg)
984 (let ((mynumber (or msg (wl-summary-message-number))))
986 (while (wl-thread-descendant-p mynumber (wl-summary-message-number))
988 (beginning-of-line)))
990 (defun wl-thread-remove-destination-region (beg end)
993 (narrow-to-region beg end)
994 (goto-char (point-min))
996 (let ((num (wl-summary-message-number)))
997 (if (assq num wl-summary-buffer-refile-list)
998 (wl-summary-remove-destination)))
1001 (defun wl-thread-print-destination-region (beg end)
1002 (if (or wl-summary-buffer-refile-list
1003 wl-summary-buffer-copy-list)
1006 (narrow-to-region beg end)
1007 (goto-char (point-min))
1009 (let ((num (wl-summary-message-number))
1011 (if (or (setq pair (assq num wl-summary-buffer-refile-list))
1012 (setq pair (assq num wl-summary-buffer-copy-list)))
1013 (wl-summary-print-destination (car pair) (cdr pair))))
1014 (forward-line 1))))))
1016 (defsubst wl-thread-get-children-msgs (msg &optional visible-only)
1017 (let ((msgs (list msg))
1021 (wl-append ret-val (list (car msgs)))
1022 (setq children (wl-thread-entity-get-children
1023 (setq entity (wl-thread-get-entity (car msgs)))))
1024 (if (and visible-only
1025 (not (wl-thread-entity-get-opened entity)))
1026 (setq children nil))
1027 (setq msgs (cdr msgs))
1029 (while (and (null msgs) msgs-stack)
1030 (setq msgs (wl-pop msgs-stack)))
1031 (wl-push msgs msgs-stack)
1032 (setq msgs children)))
1035 (defun wl-thread-get-children-msgs-uncached (msg &optional uncached-marks)
1036 (let ((children-msgs (wl-thread-get-children-msgs msg))
1037 (mark-alist (elmo-msgdb-get-mark-alist wl-summary-buffer-msgdb))
1038 (number-alist (elmo-msgdb-get-number-alist wl-summary-buffer-msgdb))
1041 (while children-msgs
1042 (if (and (not (eq msg (car children-msgs))) ; except itself
1043 (or (and uncached-marks
1044 (setq mark (cadr (assq (car children-msgs)
1046 (member mark uncached-marks))
1047 (and (not uncached-marks)
1048 (null (elmo-cache-exists-p
1049 (cdr (assq (car children-msgs)
1051 (wl-append uncached-list (list (car children-msgs))))
1052 (setq children-msgs (cdr children-msgs)))
1055 (defun wl-thread-get-children-msgs-with-mark (msg mark)
1056 (let ((children-msgs (wl-thread-get-children-msgs msg))
1057 (check-func (cond ((string= mark "o")
1058 'wl-summary-msg-marked-as-refiled)
1060 'wl-summary-msg-marked-as-copied)
1062 'wl-summary-msg-marked-as-deleted)
1064 'wl-summary-msg-marked-as-target)))
1066 (while children-msgs
1067 (if (funcall check-func (car children-msgs))
1068 (wl-append ret-val (list (car children-msgs))))
1069 (setq children-msgs (cdr children-msgs)))
1072 (defun wl-thread-close (entity)
1074 (wl-thread-entity-set-opened entity nil)
1075 (setq depth (wl-thread-get-depth-of-current-line))
1078 (wl-thread-goto-bottom-of-sub-thread)
1079 (wl-thread-remove-destination-region beg
1081 (forward-char -1) ;; needed for mouse-face.
1082 (delete-region beg (point))
1083 (wl-thread-insert-entity (- depth 1)
1085 (wl-thread-get-entity
1088 (delete-char 1) ; delete '\n'
1089 (wl-thread-print-destination-region beg (point))))
1091 (defun wl-thread-open (entity)
1095 (setq depth (wl-thread-get-depth-of-current-line))
1097 (delete-region beg (point))
1098 (wl-thread-entity-set-opened entity t)
1099 (wl-thread-insert-entity depth ;(- depth 1)
1101 (wl-thread-get-entity
1102 (nth 3 entity)) nil)
1103 (delete-char 1) ; delete '\n'
1104 (wl-thread-print-destination-region beg (point))))
1106 (defun wl-thread-open-close (&optional force-open)
1108 (when (eq wl-summary-buffer-view 'thread)
1109 ;;; (if (equal wl-thread-top-entity '(nil t nil nil))
1110 ;;; (error "There's no thread structure"))
1112 (let ((inhibit-read-only t)
1113 (buffer-read-only nil)
1114 (wl-thread-insert-force-opened
1115 (or wl-thread-insert-force-opened
1118 (setq msg (wl-summary-message-number))
1119 (setq entity (wl-thread-get-entity msg))
1120 (if (wl-thread-entity-get-opened entity)
1121 ;; if already opened, close its child!
1122 (if (wl-thread-entity-get-children entity)
1123 (wl-thread-close entity)
1124 ;; opened, but has no children, close its parent!
1125 (when (setq parent (wl-thread-entity-get-parent entity))
1126 (wl-summary-jump-to-msg parent)
1128 (wl-thread-get-entity (wl-summary-message-number)))))
1129 ;; if closed (or it is just a thread bottom message)
1130 ;; has children, open it!
1131 (if (wl-thread-entity-get-children entity)
1132 (wl-thread-open entity)
1133 ;; closed, and has no children, close its parent!
1134 (setq msg (or (wl-thread-entity-get-parent entity)
1135 (wl-thread-entity-get-number entity)))
1137 (wl-summary-jump-to-msg msg)
1139 (wl-thread-get-entity (wl-summary-message-number)))))))
1140 (wl-summary-set-message-modified)
1141 (set-buffer-modified-p nil))))
1144 (defun wl-thread-get-depth-of-current-line ()
1149 (if (re-search-forward (concat "^" wl-summary-buffer-number-regexp
1150 "..../..\(.*\)..:.. ")
1152 (while (string-match wl-thread-indent-regexp
1154 (char-after (point))))
1155 (setq depth (1+ depth))
1157 (/ depth wl-thread-indent-level-internal))))
1159 (defun wl-thread-update-indent-string-region (beg end)
1163 (while (< (point) end)
1164 (wl-thread-update-indent-string)
1167 (defsubst wl-thread-make-indent-string (entity)
1170 (space-str (wl-repeat-string wl-thread-space-str-internal
1171 (- wl-thread-indent-level-internal 1)))
1173 (when (wl-thread-entity-get-number
1174 (setq parent (wl-thread-entity-get-parent-entity cur)))
1175 (if (wl-thread-entity-get-younger-brothers cur)
1176 (setq ret-val wl-thread-have-younger-brother-str-internal)
1177 (setq ret-val wl-thread-youngest-child-str-internal))
1178 (setq ret-val (concat ret-val
1180 wl-thread-horizontal-str-internal
1181 (- wl-thread-indent-level-internal 1))))
1183 (while (wl-thread-entity-get-number
1184 (wl-thread-entity-get-parent-entity cur))
1185 (if (wl-thread-entity-get-younger-brothers cur)
1186 (setq ret-val (concat wl-thread-vertical-str-internal
1189 (setq ret-val (concat wl-thread-space-str-internal
1192 (setq cur (wl-thread-entity-get-parent-entity cur))))
1195 (defun wl-thread-update-indent-string ()
1196 "Update indent string of current line."
1200 (let ((inhibit-read-only t)
1201 (buffer-read-only nil)
1203 (when (looking-at (concat "^ *\\([0-9]+\\)"
1204 "..../..\(.*\)..:.. \\("
1205 wl-highlight-thread-indent-string-regexp
1207 (goto-char (match-beginning 2))
1208 (delete-region (match-beginning 2)
1211 (wl-thread-make-indent-string
1212 (wl-thread-get-entity (string-to-int (wl-match-buffer 1)))))
1213 (if (and wl-summary-width
1214 wl-summary-indent-length-limit
1215 (< wl-summary-indent-length-limit
1216 (string-width thr-str)))
1217 (setq thr-str (wl-set-string-width
1218 wl-summary-indent-length-limit
1221 (if wl-summary-highlight
1222 (wl-highlight-summary-current-line))))))
1224 (defun wl-thread-set-parent (&optional parent-number)
1225 "Set current message's parent interactively."
1227 (let ((number (wl-summary-message-number))
1228 (dst-parent (if (interactive-p)
1229 (read-from-minibuffer "Parent Message (No.): ")))
1230 entity dst-parent-entity src-parent children
1233 (if (string= dst-parent "")
1234 (setq dst-parent nil)
1236 (setq dst-parent (string-to-int dst-parent))
1237 (setq dst-parent parent-number)))
1239 (memq dst-parent (wl-thread-get-children-msgs number)))
1240 (error "Parent is children or myself"))
1241 (setq entity (wl-thread-get-entity number))
1242 (when (and number entity)
1244 (setq update-msgs (wl-thread-delete-message number 'deep))
1245 ;; insert as child at new parent
1246 (setq dst-parent-entity (wl-thread-get-entity dst-parent))
1247 (if dst-parent-entity
1250 (wl-thread-entity-get-children dst-parent-entity))
1251 (wl-append update-msgs
1252 (wl-thread-get-children-msgs
1253 (car (last children)) t)))
1254 (wl-thread-entity-set-children
1256 (append children (list number)))
1257 (wl-thread-entity-set-linked entity t))
1259 (wl-append wl-thread-entity-list (list number))
1260 (wl-thread-entity-set-linked entity nil))
1263 (wl-append update-msgs (wl-thread-get-children-msgs number t))
1264 (setq update-msgs (elmo-uniq-list update-msgs))
1265 (wl-thread-entity-set-parent entity dst-parent)
1266 ;; update thread on buffer
1267 (wl-thread-update-line-msgs update-msgs t))))
1270 (product-provide (provide 'wl-thread) (require 'wl-version))
1272 ;;; wl-thread.el ends here