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-folder-msgdb-path fld))))
63 (wl-summary-load-file-object
64 (expand-file-name wl-thread-entity-list-file
65 (elmo-folder-msgdb-path fld))))
66 (message "Resuming thread structure...")
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 (if (wl-thread-entity-get-opened
353 (setq entity (wl-thread-get-entity
354 (wl-summary-message-number))))
356 (wl-thread-force-open)
357 (wl-thread-goto-bottom-of-sub-thread))
358 (when (> len elmo-display-progress-threshold)
360 (elmo-display-progress
361 'wl-thread-open-all "Opening all threads..."
362 (/ (* cur 100) len)))))
363 ;; Make sure to be 100%.
364 (elmo-display-progress
365 'wl-thread-open-all "Opening all threads..."
367 (message "Opening all threads...done"))
369 (defun wl-thread-open-all-unread ()
371 (dolist (number (elmo-folder-list-messages-mark-match
372 wl-summary-buffer-elmo-folder
373 (wl-regexp-opt (list wl-summary-unread-uncached-mark
374 wl-summary-unread-cached-mark
376 wl-summary-important-mark))))
377 (wl-thread-entity-force-open (wl-thread-get-entity number))))
379 (defsubst wl-thread-maybe-get-children-num (msg)
380 (let ((entity (wl-thread-get-entity msg)))
381 (if (not (wl-thread-entity-get-opened entity))
382 (wl-thread-entity-get-children-num entity))))
384 (defsubst wl-thread-update-line-on-buffer-sub (entity msg &optional parent-msg)
385 (let* ((entity (or entity (wl-thread-get-entity msg)))
386 (parent-msg (or parent-msg (wl-thread-entity-get-parent entity)))
387 (overview (elmo-msgdb-get-overview (wl-summary-buffer-msgdb)))
388 (buffer-read-only nil)
389 (inhibit-read-only t)
390 overview-entity temp-mark summary-line invisible-top dest-pair)
391 (if (wl-thread-delete-line-from-buffer msg)
394 ((memq msg wl-summary-buffer-delete-list)
395 (setq temp-mark "D"))
396 ((memq msg wl-summary-buffer-target-mark-list)
397 (setq temp-mark "*"))
398 ((setq dest-pair (assq msg wl-summary-buffer-refile-list))
399 (setq temp-mark "o"))
400 ((setq dest-pair (assq msg wl-summary-buffer-copy-list))
401 (setq temp-mark "O"))
402 (t (setq temp-mark (wl-summary-get-score-mark msg))))
403 (when (setq overview-entity
404 (elmo-msgdb-overview-get-entity
405 msg (wl-summary-buffer-msgdb)))
406 (wl-summary-insert-line
407 (wl-summary-create-line
409 (elmo-msgdb-overview-get-entity
410 parent-msg (wl-summary-buffer-msgdb))
412 (if wl-thread-insert-force-opened
414 (wl-thread-maybe-get-children-num msg))
415 (wl-thread-make-indent-string entity)
416 (wl-thread-entity-get-linked entity)))
418 (wl-summary-print-destination (car dest-pair)
420 ;; insert thread (moving thread)
421 (if (not (setq invisible-top
422 (wl-thread-entity-parent-invisible-p entity)))
423 (wl-summary-update-thread
424 (elmo-msgdb-overview-get-entity msg (wl-summary-buffer-msgdb))
427 (elmo-msgdb-overview-get-entity
428 parent-msg (wl-summary-buffer-msgdb))))
429 ;; currently invisible.. update closed line.
430 (wl-thread-update-children-number invisible-top)))))
432 (defun wl-thread-update-line-on-buffer (&optional msg parent-msg updates)
434 (let ((msgs (list (or msg (wl-summary-message-number))))
435 entity children msgs-stack)
437 (setq msg (wl-pop msgs))
438 (setq updates (and updates (delete msg updates)))
439 (setq entity (wl-thread-get-entity msg))
440 (wl-thread-update-line-on-buffer-sub entity msg parent-msg)
442 (setq children (wl-thread-entity-get-children entity))
445 (when (wl-thread-entity-get-opened entity)
446 (wl-push msgs msgs-stack)
450 (while (and (null msgs) msgs-stack)
451 (setq msgs (wl-pop msgs-stack)))
454 (wl-thread-entity-get-number
455 (wl-thread-entity-get-parent-entity
456 (wl-thread-get-entity (car msgs)))))))))
459 (defun wl-thread-update-line-msgs (msgs &optional no-msg)
460 (wl-delete-all-overlays)
467 ;;; (wl-thread-get-children-msgs (car msgs))))
468 ;;; (setq msgs (cdr msgs)))
469 ;;; (setq updates (elmo-uniq-list updates))
470 (setq len (length updates))
472 (wl-thread-update-line-on-buffer-sub nil (car updates))
473 (setq updates (cdr updates))
474 (when (and (not no-msg)
475 (> len elmo-display-progress-threshold))
477 (if (or (zerop (% i 5)) (= i len))
478 (elmo-display-progress
479 'wl-thread-update-line-msgs "Updating deleted thread..."
480 (/ (* i 100) len)))))))
482 (defun wl-thread-delete-line-from-buffer (msg)
483 "Simply delete msg line."
485 (if (wl-summary-jump-to-msg msg)
489 (delete-region beg (point))
493 (defun wl-thread-cleanup-symbols (msgs)
496 (when (setq entity (wl-thread-get-entity (car msgs)))
498 (setq wl-thread-entities (delq entity wl-thread-entities))
500 (elmo-clear-hash-val (format "#%d" (car msgs))
501 wl-thread-entity-hashtb))
502 (setq msgs (cdr msgs)))))
504 (defun wl-thread-get-exist-children (msg &optional include-self)
505 (let ((msgs (list msg))
509 (setq children (wl-thread-entity-get-children
510 (setq entity (wl-thread-get-entity (car msgs)))))
511 (when (elmo-msgdb-overview-get-entity (car msgs)
512 (wl-summary-buffer-msgdb))
513 (wl-append ret-val (list (car msgs)))
515 (setq msgs (cdr msgs))
517 (while (and (null msgs) msgs-stack)
518 (setq msgs (wl-pop msgs-stack)))
519 (wl-push msgs msgs-stack)
520 (setq msgs children)))
521 (unless include-self (setq ret-val (delq msg ret-val)))
524 (defun wl-thread-delete-message (msg &optional deep update)
525 "Delete MSG from entity and buffer."
527 (let* ((entity (wl-thread-get-entity msg))
528 children older-brothers younger-brothers top-child ;;grandchildren
529 top-entity parent update-msgs beg invisible-top)
531 (setq parent (wl-thread-entity-get-parent-entity entity))
535 ;;; (setq brothers (wl-thread-entity-get-children parent))
536 (setq older-brothers (wl-thread-entity-get-older-brothers
538 (setq younger-brothers (wl-thread-entity-get-younger-brothers
542 (setq children (wl-thread-entity-get-children entity))
543 (wl-thread-reparent-children
544 children (wl-thread-entity-get-number parent))
546 (apply (function nconc)
551 (wl-thread-get-children-msgs message t)))
553 (wl-thread-entity-set-children
554 parent (append older-brothers children younger-brothers))
555 ;; If chidren and younger-brothers not exists,
556 ;; update nearly older brother.
557 (when (and older-brothers
558 (not younger-brothers)
562 (wl-thread-get-children-msgs (car (last older-brothers))))))
564 ;; top...oldest child becomes top.
566 (setq children (wl-thread-entity-get-children entity))
568 (setq top-child (car children)
569 children (cdr children))
570 (setq top-entity (wl-thread-get-entity top-child))
571 (wl-thread-entity-set-parent top-entity nil)
572 (wl-thread-entity-set-linked top-entity nil)
573 (wl-append update-msgs
574 (wl-thread-get-children-msgs top-child t)))
576 (wl-thread-entity-set-children
579 (wl-thread-entity-get-children top-entity)
581 (wl-thread-reparent-children children top-child)
582 (wl-append update-msgs children)))
583 ;; delete myself from top list.
584 (setq wl-summary-buffer-number-list
585 (delq msg wl-summary-buffer-number-list))
586 (setq older-brothers (wl-thread-entity-get-older-brothers
588 (setq younger-brothers (wl-thread-entity-get-younger-brothers
590 (setq wl-thread-entity-list
591 (append (append older-brothers
592 (and top-child (list top-child)))
596 ;; delete thread on buffer
597 (when (wl-summary-jump-to-msg msg)
599 (wl-thread-goto-bottom-of-sub-thread)
600 (delete-region beg (point)))
601 ;; delete myself from buffer.
602 (unless (wl-thread-delete-line-from-buffer msg)
603 ;; jump to suitable point.
604 ;; just upon the oldest younger-brother of my top.
606 (car (wl-thread-entity-parent-invisible-p entity)))
609 (wl-append update-msgs (list invisible-top))
610 (wl-summary-jump-to-msg invisible-top))
611 (goto-char (point-max))))
613 ;; insert children if thread is closed or delete top.
615 (not (wl-thread-entity-get-opened entity)))
616 (let* (next-top insert-msgs ent e grandchildren)
619 (setq insert-msgs (wl-thread-get-exist-children
620 top-child 'include-self))
621 (setq next-top (car insert-msgs))
622 (setq ent (wl-thread-get-entity next-top))
624 (wl-thread-entity-get-opened entity) ;; open
625 (not (wl-thread-entity-get-opened ent)) ;; close
627 (wl-thread-entity-get-children ent))
628 (wl-summary-jump-to-msg next-top))
630 (setq insert-msgs (append (cdr insert-msgs) grandchildren)))
631 (when top-entity (wl-thread-entity-set-opened top-entity t))
632 (when ent (wl-thread-entity-set-opened ent t)))
633 (when (not invisible-top)
634 (setq insert-msgs (wl-thread-get-exist-children msg))
635 ;; First msg always opened, because first msg maybe becomes top.
636 (if (setq ent (wl-thread-get-entity (car insert-msgs)))
637 (wl-thread-entity-set-opened ent t))))
640 ;; if no exists in summary, insert entity.
641 (when (and (car insert-msgs)
642 (not (wl-summary-jump-to-msg (car insert-msgs))))
643 (setq ent (wl-thread-get-entity (car insert-msgs)))
644 (wl-thread-insert-entity 0 ; no mean now...
646 (setq insert-msgs (cdr insert-msgs))))))
650 (wl-thread-update-line-on-buffer-sub nil (pop update-msgs)))
651 ;; don't update buffer
652 update-msgs)))) ; return value
654 (defun wl-thread-insert-message (overview-entity
655 msg parent-msg &optional update linked)
656 "Insert MSG to the entity.
657 When optional argument UPDATE is non-nil,
658 Message is inserted to the summary buffer."
659 (let ((parent (wl-thread-get-entity parent-msg))
660 child-entity invisible-top)
661 ;;; Update the thread view...not implemented yet.
662 ;;; (when force-insert
664 ;;; (wl-thread-entity-force-open parent))
666 ;; insert as children.
667 (wl-thread-entity-insert-as-children
669 (setq child-entity (wl-thread-create-entity
670 msg (nth 0 parent) nil linked)))
671 ;; insert as top message.
672 (wl-thread-entity-insert-as-top
673 (wl-thread-create-entity msg nil)))
675 (if (not (setq invisible-top
676 (wl-thread-entity-parent-invisible-p child-entity)))
679 (wl-summary-update-thread
682 (elmo-msgdb-overview-get-entity
683 parent-msg (wl-summary-buffer-msgdb)))
685 ;; use thread structure.
686 (wl-thread-entity-get-nearly-older-brother
687 child-entity parent))) ; return value
688 ;;; (wl-thread-entity-get-number
689 ;;; (wl-thread-entity-get-top-entity parent)))) ; return value;
690 ;;; (setq beg (point))
691 ;;; (wl-thread-goto-bottom-of-sub-thread)
692 ;;; (wl-thread-update-indent-string-region beg (point)))
693 ;; currently invisible.. update closed line.
694 (wl-thread-update-children-number invisible-top)
697 (defun wl-thread-get-parent-list (msgs)
701 (setq myself (car msgs2)
703 (while (not (eq myself (car msgs2)))
704 (if (wl-thread-descendant-p myself (car msgs2))
705 (setq msgs (delq (car msgs2) msgs)))
706 (setq msgs2 (or (cdr msgs2) msgs)))
707 (setq msgs2 (cdr msgs2)))
710 (defun wl-thread-update-indent-string-thread (top-list)
711 (let ((top-list (wl-thread-get-parent-list top-list))
715 (wl-summary-jump-to-msg (car top-list))
717 (wl-thread-goto-bottom-of-sub-thread)
718 (wl-thread-update-indent-string-region beg (point)))
719 (setq top-list (cdr top-list)))))
721 (defun wl-thread-update-children-number (entity)
722 "Update the children number."
723 (wl-thread-update-line-on-buffer (wl-thread-entity-get-number entity)))
726 ;; Thread oriented commands.
728 (defun wl-thread-call-region-func (func &optional arg)
731 (wl-summary-goto-top-of-current-thread)
734 (wl-thread-goto-bottom-of-sub-thread)
735 (funcall func beg (point)))))
737 (defun wl-thread-prefetch (&optional arg)
739 (wl-thread-call-region-func 'wl-summary-prefetch-region arg))
741 (defun wl-thread-msg-mark-as-important (msg)
742 "Set mark as important for invisible MSG. Modeline is not changed."
743 (let ((msgdb (wl-summary-buffer-msgdb))
745 (setq cur-mark (elmo-msgdb-get-mark msgdb msg))
746 (elmo-msgdb-set-mark msgdb
748 (if (string= cur-mark wl-summary-important-mark)
750 wl-summary-important-mark))
751 (wl-summary-set-mark-modified)))
753 (defun wl-thread-mark-as-read (&optional arg)
755 (wl-thread-call-region-func 'wl-summary-mark-as-read-region arg))
757 (defun wl-thread-mark-as-unread (&optional arg)
759 (wl-thread-call-region-func 'wl-summary-mark-as-unread-region arg))
761 (defun wl-thread-mark-as-important (&optional arg)
763 (wl-thread-call-region-func 'wl-summary-mark-as-important-region arg))
765 (defun wl-thread-copy (&optional arg)
767 (wl-thread-call-region-func 'wl-summary-copy-region arg))
769 (defun wl-thread-refile (&optional arg)
773 (wl-thread-call-region-func 'wl-summary-refile-region arg)
775 (wl-summary-goto-top-of-current-thread))
776 (wl-thread-goto-bottom-of-sub-thread))
778 (elmo-display-error err t)
781 (defun wl-thread-delete (&optional arg)
783 (wl-thread-call-region-func 'wl-summary-delete-region arg)
785 (wl-summary-goto-top-of-current-thread))
786 (if (not wl-summary-move-direction-downward)
788 (wl-thread-goto-bottom-of-sub-thread)
789 (if wl-summary-buffer-disp-msg
790 (wl-summary-redisplay))))
792 (defun wl-thread-target-mark (&optional arg)
794 (wl-thread-call-region-func 'wl-summary-target-mark-region arg))
796 (defun wl-thread-unmark (&optional arg)
798 (wl-thread-call-region-func 'wl-summary-unmark-region arg))
800 (defun wl-thread-exec (&optional arg)
802 (wl-thread-call-region-func 'wl-summary-exec-region arg))
804 (defun wl-thread-save (&optional arg)
806 (wl-thread-call-region-func 'wl-summary-save-region arg))
808 (defun wl-thread-force-open (&optional msg-num)
809 "force open current folder"
811 (wl-summary-jump-to-msg msg-num))
812 (let ((wl-thread-insert-force-opened t))
813 (wl-thread-open-close)))
815 (defun wl-thread-entity-force-open (entity)
816 (let ((wl-thread-insert-force-opened t)
818 (if (null (wl-thread-entity-get-parent entity))
820 (if (and (not (wl-thread-entity-get-opened entity))
821 (wl-thread-entity-get-children entity))
822 (wl-thread-force-open (wl-thread-entity-get-number entity)))
823 (if (setq notopen (wl-thread-entity-parent-invisible-p entity))
824 (wl-thread-force-open (wl-thread-entity-get-number notopen))))))
826 (defun wl-thread-insert-top ()
827 (let ((elist wl-thread-entity-list)
828 (len (length wl-thread-entity-list))
830 (wl-delete-all-overlays)
832 (wl-thread-insert-entity
834 (wl-thread-get-entity (car elist))
837 (setq elist (cdr elist))
838 (when (> len elmo-display-progress-threshold)
840 (if (or (zerop (% cur 2)) (= cur len))
841 (elmo-display-progress
842 'wl-thread-insert-top "Inserting thread..."
843 (/ (* cur 100) len)))))))
845 (defsubst wl-thread-insert-entity-sub (indent entity parent-entity all)
850 (when (setq msg-num (wl-thread-entity-get-number entity))
851 (unless all ; all...means no temp-mark.
852 (cond ((memq msg-num wl-summary-buffer-delete-list)
853 (setq temp-mark "D"))
854 ((memq msg-num wl-summary-buffer-target-mark-list)
855 (setq temp-mark "*"))
856 ((assq msg-num wl-summary-buffer-refile-list)
857 (setq temp-mark "o"))
858 ((assq msg-num wl-summary-buffer-copy-list)
859 (setq temp-mark "O"))))
861 (setq temp-mark (wl-summary-get-score-mark msg-num)))
862 (setq overview-entity
863 (elmo-msgdb-overview-get-entity
864 (nth 0 entity) (wl-summary-buffer-msgdb)))
865 ;;; (wl-delete-all-overlays)
866 (when overview-entity
867 (wl-summary-insert-line
868 (wl-summary-create-line
870 (elmo-msgdb-overview-get-entity
871 (nth 0 parent-entity) (wl-summary-buffer-msgdb))
873 (if wl-thread-insert-force-opened
875 (wl-thread-maybe-get-children-num msg-num))
876 (wl-thread-make-indent-string entity)
877 (wl-thread-entity-get-linked entity)))))))
879 (defun wl-thread-insert-entity (indent entity parent-entity all)
880 "Insert thread entity in current buffer."
881 (let ((msgs (list (car entity)))
884 (wl-thread-insert-entity-sub indent entity parent-entity all)
885 (setq msgs (cdr msgs))
886 (setq children (nth 2 entity))
889 (when (or wl-thread-insert-force-opened
890 (wl-thread-entity-get-opened entity))
891 (wl-thread-entity-set-opened entity t)
892 (wl-push msgs msgs-stack)
895 parent-entity entity)))
897 (while (and (null msgs) msgs-stack)
898 (setq msgs (wl-pop msgs-stack))
899 (setq indent (1- indent)))
901 (setq entity (wl-thread-get-entity (car msgs)))
902 (setq parent-entity (wl-thread-entity-get-parent-entity entity))))
903 (setq entity (wl-thread-get-entity (car msgs))))))
905 (defun wl-thread-descendant-p (mynumber number)
906 (let ((cur (wl-thread-get-entity number))
910 (setq cur (wl-thread-entity-get-parent-entity cur))
911 (if (null (setq num (wl-thread-entity-get-number cur))) ; top!
914 (eq mynumber (wl-thread-entity-get-number cur)))
918 ;; (defun wl-thread-goto-bottom-of-sub-thread ()
920 ;; (let ((depth (wl-thread-get-depth-of-current-line)))
922 ;; (while (and (not (eobp))
923 ;; (> (wl-thread-get-depth-of-current-line)
926 ;; (beginning-of-line)))
928 (defun wl-thread-goto-bottom-of-sub-thread (&optional msg)
930 (let ((mynumber (or msg (wl-summary-message-number))))
932 (while (wl-thread-descendant-p mynumber (wl-summary-message-number))
934 (beginning-of-line)))
936 (defun wl-thread-remove-destination-region (beg end)
939 (narrow-to-region beg end)
940 (goto-char (point-min))
942 (let ((num (wl-summary-message-number)))
943 (if (assq num wl-summary-buffer-refile-list)
944 (wl-summary-remove-destination)))
947 (defun wl-thread-print-destination-region (beg end)
948 (if (or wl-summary-buffer-refile-list
949 wl-summary-buffer-copy-list)
952 (narrow-to-region beg end)
953 (goto-char (point-min))
955 (let ((num (wl-summary-message-number))
957 (if (or (setq pair (assq num wl-summary-buffer-refile-list))
958 (setq pair (assq num wl-summary-buffer-copy-list)))
959 (wl-summary-print-destination (car pair) (cdr pair))))
960 (forward-line 1))))))
962 (defsubst wl-thread-get-children-msgs (msg &optional visible-only)
963 (let ((msgs (list msg))
967 (wl-append ret-val (list (car msgs)))
968 (setq children (wl-thread-entity-get-children
969 (setq entity (wl-thread-get-entity (car msgs)))))
970 (if (and visible-only
971 (not (wl-thread-entity-get-opened entity)))
973 (setq msgs (cdr msgs))
975 (while (and (null msgs) msgs-stack)
976 (setq msgs (wl-pop msgs-stack)))
977 (wl-push msgs msgs-stack)
978 (setq msgs children)))
981 (defun wl-thread-get-children-msgs-uncached (msg &optional uncached-marks)
982 (let ((children-msgs (wl-thread-get-children-msgs msg))
983 (number-alist (elmo-msgdb-get-number-alist (wl-summary-buffer-msgdb)))
987 (if (and (not (eq msg (car children-msgs))) ; except itself
988 (or (and uncached-marks
989 (setq mark (elmo-msgdb-get-mark
990 (wl-summary-buffer-msgdb)
991 (car children-msgs)))
992 (member mark uncached-marks))
993 (and (not uncached-marks)
994 (null (elmo-file-cache-exists-p
996 wl-summary-buffer-elmo-folder
999 (wl-append uncached-list (list (car children-msgs))))
1000 (setq children-msgs (cdr children-msgs)))
1003 (defun wl-thread-get-children-msgs-with-mark (msg mark)
1004 (let ((children-msgs (wl-thread-get-children-msgs msg))
1005 (check-func (cond ((string= mark "o")
1006 'wl-summary-msg-marked-as-refiled)
1008 'wl-summary-msg-marked-as-copied)
1010 'wl-summary-msg-marked-as-deleted)
1012 'wl-summary-msg-marked-as-target)))
1014 (while children-msgs
1015 (if (funcall check-func (car children-msgs))
1016 (wl-append ret-val (list (car children-msgs))))
1017 (setq children-msgs (cdr children-msgs)))
1020 (defun wl-thread-close (entity)
1022 (wl-thread-entity-set-opened entity nil)
1023 (setq depth (wl-thread-get-depth-of-current-line))
1026 (wl-thread-goto-bottom-of-sub-thread)
1027 (wl-thread-remove-destination-region beg
1029 (forward-char -1) ;; needed for mouse-face.
1030 (delete-region beg (point))
1031 (wl-thread-insert-entity (- depth 1)
1033 (wl-thread-get-entity
1036 (delete-char 1) ; delete '\n'
1037 (wl-thread-print-destination-region beg (point))))
1039 (defun wl-thread-open (entity)
1043 (setq depth (wl-thread-get-depth-of-current-line))
1045 (delete-region beg (point))
1046 (wl-thread-entity-set-opened entity t)
1047 (wl-thread-insert-entity depth ;(- depth 1)
1049 (wl-thread-get-entity
1050 (nth 3 entity)) nil)
1051 (delete-char 1) ; delete '\n'
1052 (wl-thread-print-destination-region beg (point))))
1054 (defun wl-thread-open-close (&optional force-open)
1056 (when (eq wl-summary-buffer-view 'thread)
1057 ;;; (if (equal wl-thread-top-entity '(nil t nil nil))
1058 ;;; (error "There's no thread structure"))
1060 (let ((inhibit-read-only t)
1061 (buffer-read-only nil)
1062 (wl-thread-insert-force-opened
1063 (or wl-thread-insert-force-opened
1066 (setq msg (wl-summary-message-number))
1067 (setq entity (wl-thread-get-entity msg))
1068 (if (wl-thread-entity-get-opened entity)
1069 ;; if already opened, close its child!
1070 (if (wl-thread-entity-get-children entity)
1071 (wl-thread-close entity)
1072 ;; opened, but has no children, close its parent!
1073 (when (setq parent (wl-thread-entity-get-parent entity))
1074 (wl-summary-jump-to-msg parent)
1076 (wl-thread-get-entity (wl-summary-message-number)))))
1077 ;; if closed (or it is just a thread bottom message)
1078 ;; has children, open it!
1079 (if (wl-thread-entity-get-children entity)
1080 (wl-thread-open entity)
1081 ;; closed, and has no children, close its parent!
1082 (setq msg (or (wl-thread-entity-get-parent entity)
1083 (wl-thread-entity-get-number entity)))
1085 (wl-summary-jump-to-msg msg)
1087 (wl-thread-get-entity (wl-summary-message-number)))))))
1088 (wl-summary-set-message-modified)
1089 (set-buffer-modified-p nil))))
1091 (defun wl-thread-get-depth-of-current-line ()
1092 (let ((entity (wl-thread-get-entity (wl-summary-message-number)))
1095 (while (setq number (wl-thread-entity-get-parent entity))
1097 (setq entity (wl-thread-get-entity number)))
1100 (defun wl-thread-update-indent-string-region (beg end)
1104 (while (< (point) end)
1105 ;(wl-thread-update-indent-string)
1106 (wl-thread-update-line-on-buffer)
1109 (defsubst wl-thread-make-indent-string (entity)
1112 (space-str (wl-repeat-string wl-thread-space-str-internal
1113 (- wl-thread-indent-level-internal 1)))
1115 (when (wl-thread-entity-get-number
1116 (setq parent (wl-thread-entity-get-parent-entity cur)))
1117 (if (wl-thread-entity-get-younger-brothers cur)
1118 (setq ret-val wl-thread-have-younger-brother-str-internal)
1119 (setq ret-val wl-thread-youngest-child-str-internal))
1120 (setq ret-val (concat ret-val
1122 wl-thread-horizontal-str-internal
1123 (- wl-thread-indent-level-internal 1))))
1125 (while (wl-thread-entity-get-number
1126 (wl-thread-entity-get-parent-entity cur))
1127 (if (wl-thread-entity-get-younger-brothers cur)
1128 (setq ret-val (concat wl-thread-vertical-str-internal
1131 (setq ret-val (concat wl-thread-space-str-internal
1134 (setq cur (wl-thread-entity-get-parent-entity cur))))
1137 (defun wl-thread-set-parent (&optional parent-number)
1138 "Set current message's parent interactively."
1140 (let ((number (wl-summary-message-number))
1141 (dst-parent (if (interactive-p)
1142 (read-from-minibuffer "Parent Message (No.): ")))
1143 entity dst-parent-entity src-parent children
1146 (if (string= dst-parent "")
1147 (setq dst-parent nil)
1149 (setq dst-parent (string-to-int dst-parent))
1150 (setq dst-parent parent-number)))
1152 (memq dst-parent (wl-thread-get-children-msgs number)))
1153 (error "Parent is children or myself"))
1154 (setq entity (wl-thread-get-entity number))
1155 (when (and number entity)
1157 (setq update-msgs (wl-thread-delete-message number 'deep))
1158 ;; insert as child at new parent
1159 (setq dst-parent-entity (wl-thread-get-entity dst-parent))
1160 (if dst-parent-entity
1163 (wl-thread-entity-get-children dst-parent-entity))
1164 (wl-append update-msgs
1165 (wl-thread-get-children-msgs
1166 (car (last children)) t)))
1167 (wl-thread-entity-set-children
1169 (append children (list number)))
1170 (wl-thread-entity-set-linked entity t))
1172 (wl-append wl-thread-entity-list (list number))
1173 (wl-thread-entity-set-linked entity nil))
1176 (wl-append update-msgs (wl-thread-get-children-msgs number t))
1177 (setq update-msgs (elmo-uniq-list update-msgs))
1178 (wl-thread-entity-set-parent entity dst-parent)
1179 ;; update thread on buffer
1180 (wl-thread-update-line-msgs update-msgs t))))
1183 (product-provide (provide 'wl-thread) (require 'wl-version))
1185 ;;; wl-thread.el ends here