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 (let ((mark-alist (elmo-msgdb-get-mark-alist (wl-summary-buffer-msgdb)))
374 (if (setq mark (nth 1 (car mark-alist)))
375 (if (or (string= mark wl-summary-unread-uncached-mark)
376 (string= mark wl-summary-unread-cached-mark)
377 (string= mark wl-summary-new-mark)
378 (string= mark wl-summary-important-mark))
379 (wl-thread-entity-force-open (wl-thread-get-entity
380 (nth 0 (car mark-alist))))))
381 (setq mark-alist (cdr mark-alist)))))
383 (defsubst wl-thread-maybe-get-children-num (msg)
384 (let ((entity (wl-thread-get-entity msg)))
385 (if (not (wl-thread-entity-get-opened entity))
386 (wl-thread-entity-get-children-num entity))))
388 (defsubst wl-thread-update-line-on-buffer-sub (entity msg &optional parent-msg)
389 (let* ((entity (or entity (wl-thread-get-entity msg)))
390 (parent-msg (or parent-msg (wl-thread-entity-get-parent entity)))
391 (overview (elmo-msgdb-get-overview (wl-summary-buffer-msgdb)))
392 (mark-alist (elmo-msgdb-get-mark-alist (wl-summary-buffer-msgdb)))
393 (buffer-read-only nil)
394 (inhibit-read-only t)
395 overview-entity temp-mark summary-line invisible-top dest-pair)
396 (if (wl-thread-delete-line-from-buffer msg)
399 ((memq msg wl-summary-buffer-delete-list)
400 (setq temp-mark "D"))
401 ((memq msg wl-summary-buffer-target-mark-list)
402 (setq temp-mark "*"))
403 ((setq dest-pair (assq msg wl-summary-buffer-refile-list))
404 (setq temp-mark "o"))
405 ((setq dest-pair (assq msg wl-summary-buffer-copy-list))
406 (setq temp-mark "O"))
407 (t (setq temp-mark (wl-summary-get-score-mark msg))))
408 (when (setq overview-entity
409 (elmo-msgdb-overview-get-entity
410 msg (wl-summary-buffer-msgdb)))
411 (wl-summary-insert-line
412 (wl-summary-create-line
414 (elmo-msgdb-overview-get-entity
415 parent-msg (wl-summary-buffer-msgdb))
417 (if wl-thread-insert-force-opened
419 (wl-thread-maybe-get-children-num msg))
420 (wl-thread-make-indent-string entity)
421 (wl-thread-entity-get-linked entity)))
423 (wl-summary-print-destination (car dest-pair)
425 ;; insert thread (moving thread)
426 (if (not (setq invisible-top
427 (wl-thread-entity-parent-invisible-p entity)))
428 (wl-summary-update-thread
429 (elmo-msgdb-overview-get-entity msg (wl-summary-buffer-msgdb))
433 (elmo-msgdb-overview-get-entity
434 parent-msg (wl-summary-buffer-msgdb))))
435 ;; currently invisible.. update closed line.
436 (wl-thread-update-children-number invisible-top)))))
438 (defun wl-thread-update-line-on-buffer (&optional msg parent-msg updates)
440 (let ((msgs (list (or msg (wl-summary-message-number))))
441 entity children msgs-stack)
443 (setq msg (wl-pop msgs))
444 (setq updates (and updates (delete msg updates)))
445 (setq entity (wl-thread-get-entity msg))
446 (wl-thread-update-line-on-buffer-sub entity msg parent-msg)
448 (setq children (wl-thread-entity-get-children entity))
451 (when (wl-thread-entity-get-opened entity)
452 (wl-push msgs msgs-stack)
456 (while (and (null msgs) msgs-stack)
457 (setq msgs (wl-pop msgs-stack)))
460 (wl-thread-entity-get-number
461 (wl-thread-entity-get-parent-entity
462 (wl-thread-get-entity (car msgs)))))))))
465 (defun wl-thread-update-line-msgs (msgs &optional no-msg)
466 (wl-delete-all-overlays)
473 ;;; (wl-thread-get-children-msgs (car msgs))))
474 ;;; (setq msgs (cdr msgs)))
475 ;;; (setq updates (elmo-uniq-list updates))
476 (setq len (length updates))
478 (wl-thread-update-line-on-buffer-sub nil (car updates))
479 (setq updates (cdr updates))
480 (when (and (not no-msg)
481 (> len elmo-display-progress-threshold))
483 (if (or (zerop (% i 5)) (= i len))
484 (elmo-display-progress
485 'wl-thread-update-line-msgs "Updating deleted thread..."
486 (/ (* i 100) len)))))))
488 (defun wl-thread-delete-line-from-buffer (msg)
489 "Simply delete msg line."
491 (if (wl-summary-jump-to-msg msg)
495 (delete-region beg (point))
499 (defun wl-thread-cleanup-symbols (msgs)
502 (when (setq entity (wl-thread-get-entity (car msgs)))
504 (setq wl-thread-entities (delq entity wl-thread-entities))
506 (elmo-clear-hash-val (format "#%d" (car msgs))
507 wl-thread-entity-hashtb))
508 (setq msgs (cdr msgs)))))
510 (defun wl-thread-get-exist-children (msg)
511 (let ((msgs (list msg))
515 (setq children (wl-thread-entity-get-children
516 (setq entity (wl-thread-get-entity (car msgs)))))
517 (when (elmo-msgdb-overview-get-entity (car msgs) (wl-summary-buffer-msgdb))
518 (wl-append ret-val (list (car msgs)))
520 (setq msgs (cdr msgs))
522 (while (and (null msgs) msgs-stack)
523 (setq msgs (wl-pop msgs-stack)))
524 (wl-push msgs msgs-stack)
525 (setq msgs children)))
528 (defun wl-thread-delete-message (msg &optional deep update)
529 "Delete MSG from entity and buffer."
531 (let* ((entity (wl-thread-get-entity msg))
532 children older-brothers younger-brothers top-child ;;grandchildren
533 top-entity parent update-msgs beg invisible-top)
535 (setq parent (wl-thread-entity-get-parent-entity entity))
539 ;;; (setq brothers (wl-thread-entity-get-children parent))
540 (setq older-brothers (wl-thread-entity-get-older-brothers
542 (setq younger-brothers (wl-thread-entity-get-younger-brothers
546 (setq children (wl-thread-entity-get-children entity))
547 (wl-thread-reparent-children
548 children (wl-thread-entity-get-number parent))
550 (apply (function nconc)
555 (wl-thread-get-children-msgs message t)))
557 (wl-thread-entity-set-children
558 parent (append older-brothers children younger-brothers))
559 ;; If chidren and younger-brothers not exists,
560 ;; update nearly older brother.
561 (when (and older-brothers
562 (not younger-brothers)
566 (wl-thread-get-children-msgs (car (last older-brothers))))))
568 ;; top...oldest child becomes top.
570 (setq children (wl-thread-entity-get-children entity))
572 (setq top-child (car children)
573 children (cdr children))
574 (setq top-entity (wl-thread-get-entity top-child))
575 (wl-thread-entity-set-parent top-entity nil)
576 (wl-thread-entity-set-linked top-entity nil)
577 (wl-append update-msgs
578 (wl-thread-get-children-msgs top-child t)))
580 (wl-thread-entity-set-children
583 (wl-thread-entity-get-children top-entity)
585 (wl-thread-reparent-children children top-child)
586 (wl-append update-msgs children)))
587 ;; delete myself from top list.
588 (setq wl-summary-buffer-number-list
589 (delq msg wl-summary-buffer-number-list))
590 (setq older-brothers (wl-thread-entity-get-older-brothers
592 (setq younger-brothers (wl-thread-entity-get-younger-brothers
594 (setq wl-thread-entity-list
595 (append (append older-brothers
596 (and top-child (list top-child)))
600 ;; delete thread on buffer
601 (when (wl-summary-jump-to-msg msg)
603 (wl-thread-goto-bottom-of-sub-thread)
604 (delete-region beg (point)))
605 ;; delete myself from buffer.
606 (unless (wl-thread-delete-line-from-buffer msg)
607 ;; jump to suitable point.
608 ;; just upon the oldest younger-brother of my top.
610 (car (wl-thread-entity-parent-invisible-p entity)))
613 (wl-append update-msgs (list invisible-top))
614 (wl-summary-jump-to-msg invisible-top))
615 (goto-char (point-max))))
617 ;; insert children if thread is closed or delete top.
619 (not (wl-thread-entity-get-opened entity)))
620 (let* (next-top insert-msgs ent e grandchildren)
623 (setq insert-msgs (wl-thread-get-exist-children top-child))
624 (setq next-top (car insert-msgs))
625 (setq ent (wl-thread-get-entity next-top))
627 (wl-thread-entity-get-opened entity) ;; open
628 (not (wl-thread-entity-get-opened ent)) ;; close
630 (wl-thread-entity-get-children ent))
631 (wl-summary-jump-to-msg next-top))
633 (setq insert-msgs (append (cdr insert-msgs) grandchildren)))
634 (when top-entity (wl-thread-entity-set-opened top-entity t))
635 (when ent (wl-thread-entity-set-opened ent t)))
636 (when (not invisible-top)
637 (setq insert-msgs (wl-thread-get-exist-children msg))
638 ;; First msg always opened, because first msg maybe becomes top.
639 (if (setq ent (wl-thread-get-entity (car insert-msgs)))
640 (wl-thread-entity-set-opened ent t))))
643 ;; if no exists in summary, insert entity.
644 (when (and (car insert-msgs)
645 (not (wl-summary-jump-to-msg (car insert-msgs))))
646 (setq ent (wl-thread-get-entity (car insert-msgs)))
647 (wl-thread-insert-entity 0 ; no mean now...
649 (setq insert-msgs (cdr insert-msgs))))))
653 (wl-thread-update-line-on-buffer-sub nil (pop update-msgs)))
654 ;; don't update buffer
655 update-msgs)))) ; return value
657 (defun wl-thread-insert-message (overview-entity
659 msg parent-msg &optional update linked)
660 "Insert MSG to the entity.
661 When optional argument UPDATE is non-nil,
662 Message is inserted to the summary buffer."
663 (let ((parent (wl-thread-get-entity parent-msg))
664 child-entity invisible-top)
665 ;;; Update the thread view...not implemented yet.
666 ;;; (when force-insert
668 ;;; (wl-thread-entity-force-open parent))
670 ;; insert as children.
671 (wl-thread-entity-insert-as-children
673 (setq child-entity (wl-thread-create-entity
674 msg (nth 0 parent) nil linked)))
675 ;; insert as top message.
676 (wl-thread-entity-insert-as-top
677 (wl-thread-create-entity msg nil)))
679 (if (not (setq invisible-top
680 (wl-thread-entity-parent-invisible-p child-entity)))
683 (wl-summary-update-thread
687 (elmo-msgdb-overview-get-entity
688 parent-msg (wl-summary-buffer-msgdb)))
690 ;; use thread structure.
691 (wl-thread-entity-get-nearly-older-brother
692 child-entity parent))) ; return value
693 ;;; (wl-thread-entity-get-number
694 ;;; (wl-thread-entity-get-top-entity parent)))) ; return value;
695 ;;; (setq beg (point))
696 ;;; (wl-thread-goto-bottom-of-sub-thread)
697 ;;; (wl-thread-update-indent-string-region beg (point)))
698 ;; currently invisible.. update closed line.
699 (wl-thread-update-children-number invisible-top)
702 (defun wl-thread-get-parent-list (msgs)
706 (setq myself (car msgs2)
708 (while (not (eq myself (car msgs2)))
709 (if (wl-thread-descendant-p myself (car msgs2))
710 (setq msgs (delq (car msgs2) msgs)))
711 (setq msgs2 (or (cdr msgs2) msgs)))
712 (setq msgs2 (cdr msgs2)))
715 (defun wl-thread-update-indent-string-thread (top-list)
716 (let ((top-list (wl-thread-get-parent-list top-list))
720 (wl-summary-jump-to-msg (car top-list))
722 (wl-thread-goto-bottom-of-sub-thread)
723 (wl-thread-update-indent-string-region beg (point)))
724 (setq top-list (cdr top-list)))))
726 (defun wl-thread-update-children-number (entity)
727 "Update the children number."
729 (wl-summary-jump-to-msg (wl-thread-entity-get-number entity))
731 (let ((text-prop (get-text-property (point) 'face))
732 from from-end beg str)
734 ((looking-at (concat "^" wl-summary-buffer-number-regexp
735 "..../..\(.*\)..:.. ["
736 wl-thread-indent-regexp
737 "]*[[<]\\+\\([0-9]+\\):"))
738 (delete-region (match-beginning 1)(match-end 1))
739 (goto-char (match-beginning 1))
740 (setq str (format "%s" (wl-thread-entity-get-children-num entity)))
741 (if wl-summary-highlight
742 (put-text-property 0 (length str) 'face text-prop str))
744 ((looking-at (concat "^" wl-summary-buffer-number-regexp
745 "..../..\(.*\)..:.. ["
746 wl-thread-indent-regexp
748 (goto-char (match-end 0))
749 (setq beg (current-column))
750 (setq from-end (save-excursion
751 (move-to-column (+ 1 beg wl-summary-from-width))
753 (setq from (buffer-substring (match-end 0) from-end))
754 (delete-region (match-end 0) from-end)
755 (setq str (wl-set-string-width
756 (1+ wl-summary-from-width)
759 (wl-thread-entity-get-children-num
762 (if wl-summary-highlight
763 (put-text-property 0 (length str) 'face text-prop str))
765 (condition-case nil ; it's dangerous, so ignore error.
766 (run-hooks 'wl-thread-update-children-number-hook)
769 (message "Error in wl-thread-update-children-number-hook."))))))))
772 ;; Thread oriented commands.
774 (defun wl-thread-call-region-func (func &optional arg)
777 (wl-summary-goto-top-of-current-thread)
780 (wl-thread-goto-bottom-of-sub-thread)
781 (funcall func beg (point)))))
783 (defun wl-thread-prefetch (&optional arg)
785 (wl-thread-call-region-func 'wl-summary-prefetch-region arg))
787 (defun wl-thread-msg-mark-as-important (msg)
788 "Set mark as important for invisible MSG. Modeline is not changed."
789 (let* ((msgdb (wl-summary-buffer-msgdb))
790 (mark-alist (elmo-msgdb-get-mark-alist msgdb))
792 (setq cur-mark (cadr (assq msg mark-alist)))
794 (elmo-msgdb-mark-set mark-alist
796 (if (string= cur-mark wl-summary-important-mark)
798 wl-summary-important-mark)))
799 (elmo-msgdb-set-mark-alist msgdb mark-alist)
800 (wl-summary-set-mark-modified)))
802 (defun wl-thread-mark-as-read (&optional arg)
804 (wl-thread-call-region-func 'wl-summary-mark-as-read-region arg))
806 (defun wl-thread-mark-as-unread (&optional arg)
808 (wl-thread-call-region-func 'wl-summary-mark-as-unread-region arg))
810 (defun wl-thread-mark-as-important (&optional arg)
812 (wl-thread-call-region-func 'wl-summary-mark-as-important-region arg))
814 (defun wl-thread-copy (&optional arg)
816 (wl-thread-call-region-func 'wl-summary-copy-region arg))
818 (defun wl-thread-refile (&optional arg)
822 (wl-thread-call-region-func 'wl-summary-refile-region arg)
824 (wl-summary-goto-top-of-current-thread))
825 (wl-thread-goto-bottom-of-sub-thread))
827 (elmo-display-error err t)
830 (defun wl-thread-delete (&optional arg)
832 (wl-thread-call-region-func 'wl-summary-delete-region arg)
834 (wl-summary-goto-top-of-current-thread))
835 (if (not wl-summary-move-direction-downward)
837 (wl-thread-goto-bottom-of-sub-thread)
838 (if wl-summary-buffer-disp-msg
839 (wl-summary-redisplay))))
841 (defun wl-thread-target-mark (&optional arg)
843 (wl-thread-call-region-func 'wl-summary-target-mark-region arg))
845 (defun wl-thread-unmark (&optional arg)
847 (wl-thread-call-region-func 'wl-summary-unmark-region arg))
849 (defun wl-thread-exec (&optional arg)
851 (wl-thread-call-region-func 'wl-summary-exec-region arg))
853 (defun wl-thread-save (&optional arg)
855 (wl-thread-call-region-func 'wl-summary-save-region arg))
857 (defun wl-thread-force-open (&optional msg-num)
858 "force open current folder"
860 (wl-summary-jump-to-msg msg-num))
861 (let ((wl-thread-insert-force-opened t))
862 (wl-thread-open-close)))
864 (defun wl-thread-entity-force-open (entity)
865 (let ((wl-thread-insert-force-opened t)
867 (if (null (wl-thread-entity-get-parent entity))
869 (if (and (not (wl-thread-entity-get-opened entity))
870 (wl-thread-entity-get-children entity))
871 (wl-thread-force-open (wl-thread-entity-get-number entity)))
872 (if (setq notopen (wl-thread-entity-parent-invisible-p entity))
873 (wl-thread-force-open (wl-thread-entity-get-number notopen))))))
875 (defun wl-thread-insert-top ()
876 (let ((elist wl-thread-entity-list)
877 (len (length wl-thread-entity-list))
879 (wl-delete-all-overlays)
881 (wl-thread-insert-entity
883 (wl-thread-get-entity (car elist))
886 (setq elist (cdr elist))
887 (when (> len elmo-display-progress-threshold)
889 (if (or (zerop (% cur 2)) (= cur len))
890 (elmo-display-progress
891 'wl-thread-insert-top "Inserting thread..."
892 (/ (* cur 100) len)))))))
894 (defsubst wl-thread-insert-entity-sub (indent entity parent-entity all)
895 (let ((mark-alist (elmo-msgdb-get-mark-alist (wl-summary-buffer-msgdb)))
900 (when (setq msg-num (wl-thread-entity-get-number entity))
901 (unless all ; all...means no temp-mark.
902 (cond ((memq msg-num wl-summary-buffer-delete-list)
903 (setq temp-mark "D"))
904 ((memq msg-num wl-summary-buffer-target-mark-list)
905 (setq temp-mark "*"))
906 ((assq msg-num wl-summary-buffer-refile-list)
907 (setq temp-mark "o"))
908 ((assq msg-num wl-summary-buffer-copy-list)
909 (setq temp-mark "O"))))
911 (setq temp-mark (wl-summary-get-score-mark msg-num)))
912 (setq overview-entity
913 (elmo-msgdb-overview-get-entity
914 (nth 0 entity) (wl-summary-buffer-msgdb)))
915 ;;; (wl-delete-all-overlays)
916 (when overview-entity
917 (wl-summary-insert-line
918 (wl-summary-create-line
920 (elmo-msgdb-overview-get-entity
921 (nth 0 parent-entity) (wl-summary-buffer-msgdb))
923 (if wl-thread-insert-force-opened
925 (wl-thread-maybe-get-children-num msg-num))
926 (wl-thread-make-indent-string entity)
927 (wl-thread-entity-get-linked entity)))))))
929 (defun wl-thread-insert-entity (indent entity parent-entity all)
930 "Insert thread entity in current buffer."
931 (let ((msgs (list (car entity)))
934 (wl-thread-insert-entity-sub indent entity parent-entity all)
935 (setq msgs (cdr msgs))
936 (setq children (nth 2 entity))
939 (when (or wl-thread-insert-force-opened
940 (wl-thread-entity-get-opened entity))
941 (wl-thread-entity-set-opened entity t)
942 (wl-push msgs msgs-stack)
945 parent-entity entity)))
947 (while (and (null msgs) msgs-stack)
948 (setq msgs (wl-pop msgs-stack))
949 (setq indent (1- indent)))
951 (setq entity (wl-thread-get-entity (car msgs)))
952 (setq parent-entity (wl-thread-entity-get-parent-entity entity))))
953 (setq entity (wl-thread-get-entity (car msgs))))))
955 (defun wl-thread-descendant-p (mynumber number)
956 (let ((cur (wl-thread-get-entity number))
960 (setq cur (wl-thread-entity-get-parent-entity cur))
961 (if (null (setq num (wl-thread-entity-get-number cur))) ; top!
964 (eq mynumber (wl-thread-entity-get-number cur)))
968 ;; (defun wl-thread-goto-bottom-of-sub-thread ()
970 ;; (let ((depth (wl-thread-get-depth-of-current-line)))
972 ;; (while (and (not (eobp))
973 ;; (> (wl-thread-get-depth-of-current-line)
976 ;; (beginning-of-line)))
978 (defun wl-thread-goto-bottom-of-sub-thread (&optional msg)
980 (let ((mynumber (or msg (wl-summary-message-number))))
982 (while (wl-thread-descendant-p mynumber (wl-summary-message-number))
984 (beginning-of-line)))
986 (defun wl-thread-remove-destination-region (beg end)
989 (narrow-to-region beg end)
990 (goto-char (point-min))
992 (let ((num (wl-summary-message-number)))
993 (if (assq num wl-summary-buffer-refile-list)
994 (wl-summary-remove-destination)))
997 (defun wl-thread-print-destination-region (beg end)
998 (if (or wl-summary-buffer-refile-list
999 wl-summary-buffer-copy-list)
1002 (narrow-to-region beg end)
1003 (goto-char (point-min))
1005 (let ((num (wl-summary-message-number))
1007 (if (or (setq pair (assq num wl-summary-buffer-refile-list))
1008 (setq pair (assq num wl-summary-buffer-copy-list)))
1009 (wl-summary-print-destination (car pair) (cdr pair))))
1010 (forward-line 1))))))
1012 (defsubst wl-thread-get-children-msgs (msg &optional visible-only)
1013 (let ((msgs (list msg))
1017 (wl-append ret-val (list (car msgs)))
1018 (setq children (wl-thread-entity-get-children
1019 (setq entity (wl-thread-get-entity (car msgs)))))
1020 (if (and visible-only
1021 (not (wl-thread-entity-get-opened entity)))
1022 (setq children nil))
1023 (setq msgs (cdr msgs))
1025 (while (and (null msgs) msgs-stack)
1026 (setq msgs (wl-pop msgs-stack)))
1027 (wl-push msgs msgs-stack)
1028 (setq msgs children)))
1031 (defun wl-thread-get-children-msgs-uncached (msg &optional uncached-marks)
1032 (let ((children-msgs (wl-thread-get-children-msgs msg))
1033 (mark-alist (elmo-msgdb-get-mark-alist (wl-summary-buffer-msgdb)))
1034 (number-alist (elmo-msgdb-get-number-alist (wl-summary-buffer-msgdb)))
1037 (while children-msgs
1038 (if (and (not (eq msg (car children-msgs))) ; except itself
1039 (or (and uncached-marks
1040 (setq mark (cadr (assq (car children-msgs)
1042 (member mark uncached-marks))
1043 (and (not uncached-marks)
1044 (null (elmo-file-cache-exists-p
1046 wl-summary-buffer-elmo-folder
1049 (wl-append uncached-list (list (car children-msgs))))
1050 (setq children-msgs (cdr children-msgs)))
1053 (defun wl-thread-get-children-msgs-with-mark (msg mark)
1054 (let ((children-msgs (wl-thread-get-children-msgs msg))
1055 (check-func (cond ((string= mark "o")
1056 'wl-summary-msg-marked-as-refiled)
1058 'wl-summary-msg-marked-as-copied)
1060 'wl-summary-msg-marked-as-deleted)
1062 'wl-summary-msg-marked-as-target)))
1064 (while children-msgs
1065 (if (funcall check-func (car children-msgs))
1066 (wl-append ret-val (list (car children-msgs))))
1067 (setq children-msgs (cdr children-msgs)))
1070 (defun wl-thread-close (entity)
1072 (wl-thread-entity-set-opened entity nil)
1073 (setq depth (wl-thread-get-depth-of-current-line))
1076 (wl-thread-goto-bottom-of-sub-thread)
1077 (wl-thread-remove-destination-region beg
1079 (forward-char -1) ;; needed for mouse-face.
1080 (delete-region beg (point))
1081 (wl-thread-insert-entity (- depth 1)
1083 (wl-thread-get-entity
1086 (delete-char 1) ; delete '\n'
1087 (wl-thread-print-destination-region beg (point))))
1089 (defun wl-thread-open (entity)
1093 (setq depth (wl-thread-get-depth-of-current-line))
1095 (delete-region beg (point))
1096 (wl-thread-entity-set-opened entity t)
1097 (wl-thread-insert-entity depth ;(- depth 1)
1099 (wl-thread-get-entity
1100 (nth 3 entity)) nil)
1101 (delete-char 1) ; delete '\n'
1102 (wl-thread-print-destination-region beg (point))))
1104 (defun wl-thread-open-close (&optional force-open)
1106 (when (eq wl-summary-buffer-view 'thread)
1107 ;;; (if (equal wl-thread-top-entity '(nil t nil nil))
1108 ;;; (error "There's no thread structure"))
1110 (let ((inhibit-read-only t)
1111 (buffer-read-only nil)
1112 (wl-thread-insert-force-opened
1113 (or wl-thread-insert-force-opened
1116 (setq msg (wl-summary-message-number))
1117 (setq entity (wl-thread-get-entity msg))
1118 (if (wl-thread-entity-get-opened entity)
1119 ;; if already opened, close its child!
1120 (if (wl-thread-entity-get-children entity)
1121 (wl-thread-close entity)
1122 ;; opened, but has no children, close its parent!
1123 (when (setq parent (wl-thread-entity-get-parent entity))
1124 (wl-summary-jump-to-msg parent)
1126 (wl-thread-get-entity (wl-summary-message-number)))))
1127 ;; if closed (or it is just a thread bottom message)
1128 ;; has children, open it!
1129 (if (wl-thread-entity-get-children entity)
1130 (wl-thread-open entity)
1131 ;; closed, and has no children, close its parent!
1132 (setq msg (or (wl-thread-entity-get-parent entity)
1133 (wl-thread-entity-get-number entity)))
1135 (wl-summary-jump-to-msg msg)
1137 (wl-thread-get-entity (wl-summary-message-number)))))))
1138 (wl-summary-set-message-modified)
1139 (set-buffer-modified-p nil))))
1141 (defun wl-thread-get-depth-of-current-line ()
1142 (let ((entity (wl-thread-get-entity (wl-summary-message-number)))
1145 (while (setq number (wl-thread-entity-get-parent entity))
1147 (setq entity (wl-thread-get-entity number)))
1150 (defun wl-thread-update-indent-string-region (beg end)
1154 (while (< (point) end)
1155 (wl-thread-update-indent-string)
1158 (defsubst wl-thread-make-indent-string (entity)
1161 (space-str (wl-repeat-string wl-thread-space-str-internal
1162 (- wl-thread-indent-level-internal 1)))
1164 (when (wl-thread-entity-get-number
1165 (setq parent (wl-thread-entity-get-parent-entity cur)))
1166 (if (wl-thread-entity-get-younger-brothers cur)
1167 (setq ret-val wl-thread-have-younger-brother-str-internal)
1168 (setq ret-val wl-thread-youngest-child-str-internal))
1169 (setq ret-val (concat ret-val
1171 wl-thread-horizontal-str-internal
1172 (- wl-thread-indent-level-internal 1))))
1174 (while (wl-thread-entity-get-number
1175 (wl-thread-entity-get-parent-entity cur))
1176 (if (wl-thread-entity-get-younger-brothers cur)
1177 (setq ret-val (concat wl-thread-vertical-str-internal
1180 (setq ret-val (concat wl-thread-space-str-internal
1183 (setq cur (wl-thread-entity-get-parent-entity cur))))
1186 (defun wl-thread-update-indent-string ()
1187 "Update indent string of current line."
1191 (let ((inhibit-read-only t)
1192 (buffer-read-only nil)
1194 (when (looking-at (concat "^ *\\([0-9]+\\)"
1195 "..../..\(.*\)..:.. \\("
1196 wl-highlight-thread-indent-string-regexp
1198 (goto-char (match-beginning 2))
1199 (delete-region (match-beginning 2)
1202 (wl-thread-make-indent-string
1203 (wl-thread-get-entity (string-to-int (wl-match-buffer 1)))))
1204 (if (and wl-summary-indent-length-limit
1205 (< wl-summary-indent-length-limit
1206 (string-width thr-str)))
1207 (setq thr-str (wl-set-string-width
1208 wl-summary-indent-length-limit
1211 (if wl-summary-highlight
1212 (wl-highlight-summary-current-line))))))
1214 (defun wl-thread-set-parent (&optional parent-number)
1215 "Set current message's parent interactively."
1217 (let ((number (wl-summary-message-number))
1218 (dst-parent (if (interactive-p)
1219 (read-from-minibuffer "Parent Message (No.): ")))
1220 entity dst-parent-entity src-parent children
1223 (if (string= dst-parent "")
1224 (setq dst-parent nil)
1226 (setq dst-parent (string-to-int dst-parent))
1227 (setq dst-parent parent-number)))
1229 (memq dst-parent (wl-thread-get-children-msgs number)))
1230 (error "Parent is children or myself"))
1231 (setq entity (wl-thread-get-entity number))
1232 (when (and number entity)
1234 (setq update-msgs (wl-thread-delete-message number 'deep))
1235 ;; insert as child at new parent
1236 (setq dst-parent-entity (wl-thread-get-entity dst-parent))
1237 (if dst-parent-entity
1240 (wl-thread-entity-get-children dst-parent-entity))
1241 (wl-append update-msgs
1242 (wl-thread-get-children-msgs
1243 (car (last children)) t)))
1244 (wl-thread-entity-set-children
1246 (append children (list number)))
1247 (wl-thread-entity-set-linked entity t))
1249 (wl-append wl-thread-entity-list (list number))
1250 (wl-thread-entity-set-linked entity nil))
1253 (wl-append update-msgs (wl-thread-get-children-msgs number t))
1254 (setq update-msgs (elmo-uniq-list update-msgs))
1255 (wl-thread-entity-set-parent entity dst-parent)
1256 ;; update thread on buffer
1257 (wl-thread-update-line-msgs update-msgs t))))
1260 (product-provide (provide 'wl-thread) (require 'wl-version))
1262 ;;; wl-thread.el ends here