Sync with wl-2_8 branch:
[elisp/wanderlust.git] / wl / wl-folder.el
1 ;;; wl-folder.el --- Folder mode for Wanderlust.
2
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>
5
6 ;; Author: Yuuichi Teranishi <teranisi@gohome.org>
7 ;;      Masahiro MURATA <muse@ba2.so-net.ne.jp>
8 ;; Keywords: mail, net news
9
10 ;; This file is part of Wanderlust (Yet Another Message Interface on Emacsen).
11
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)
15 ;; any later version.
16 ;;
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.
21 ;;
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.
26 ;;
27
28 ;;; Commentary:
29 ;;
30
31 ;;; Code:
32 ;;
33
34 (require 'elmo-vars)
35 (require 'elmo-util)
36 (require 'elmo)
37 (require 'wl-vars)
38 (condition-case ()
39     (require 'easymenu) ; needed here.
40   (error))
41
42 (eval-when-compile
43   (require 'cl)
44   (require 'wl-util)
45   (provide 'wl-folder)
46   (require 'wl)
47   (require 'elmo-nntp))
48
49 (defcustom wl-folder-init-hook nil
50   "A hook called after folder initialization is finished."
51   :type 'hook
52   :group 'wl)
53
54 (defvar wl-folder-buffer-name "Folder")
55 (defvar wl-folder-entity nil)           ; desktop entity.
56 (defvar wl-folder-group-alist nil)      ; opened or closed
57 (defvar wl-folder-entity-id nil) ; id
58 (defvar wl-folder-entity-hashtb nil)
59 (defvar wl-folder-entity-id-name-hashtb nil)
60 (defvar wl-folder-elmo-folder-hashtb nil)  ; name => elmo folder structure
61
62 (defvar wl-folder-newsgroups-hashtb nil)
63 (defvar wl-folder-info-alist-modified nil)
64 (defvar wl-folder-completion-function nil)
65
66 (defvar wl-folder-mode-map nil)
67
68 (defvar wl-folder-buffer-disp-summary nil)
69 (defvar wl-folder-buffer-cur-entity-id nil)
70 (defvar wl-folder-buffer-cur-path nil)
71 (defvar wl-folder-buffer-cur-point nil)
72
73 (make-variable-buffer-local 'wl-folder-buffer-disp-summary)
74 (make-variable-buffer-local 'wl-folder-buffer-cur-entity-id)
75 (make-variable-buffer-local 'wl-folder-buffer-cur-path)
76 (make-variable-buffer-local 'wl-folder-buffer-cur-point)
77
78 (defconst wl-folder-entity-regexp "^\\([ ]*\\)\\(\\[[\\+-]\\]\\)?\\([^\\[].+\\):[-*0-9]+/[-*0-9]+/[-*0-9]+")
79 (defconst wl-folder-group-regexp  "^\\([ ]*\\)\\[\\([\\+-]\\)\\]\\(.+\\):[-0-9-]+/[0-9-]+/[0-9-]+\n")
80 ;;                              1:indent 2:opened 3:group-name
81 (defconst wl-folder-unsync-regexp ":[^0\\*][0-9]*/[0-9\\*-]+/[0-9\\*-]+$")
82
83 (defvar wl-folder-mode-menu-spec
84   '("Folder"
85     ["Enter Current Folder" wl-folder-jump-to-current-entity t]
86     ["Prev Folder"          wl-folder-prev-entity t]
87     ["Next Folder"          wl-folder-next-entity t]
88     ["Check Current Folder" wl-folder-check-current-entity t]
89     ["Sync Current Folder"  wl-folder-sync-current-entity t]
90 ;    ["Drop Current Folder" wl-folder-drop-unsync-current-entity t]
91     ["Prefetch Current Folder" wl-folder-prefetch-current-entity t]
92     ["Mark as Read all Current Folder" wl-folder-mark-as-read-all-current-entity t]
93     ["Expire Current Folder" wl-folder-expire-current-entity t]
94     ["Empty trash" wl-folder-empty-trash t]
95     ["Flush queue" wl-folder-flush-queue t]
96     ["Open All" wl-folder-open-all t]
97     ["Open All Unread folder" wl-folder-open-all-unread-folder t]
98     ["Close All" wl-folder-close-all t]
99     ("Folder Manager"
100      ["Add folder" wl-fldmgr-add t]
101      ["Add group" wl-fldmgr-make-group t]
102      ["Copy" wl-fldmgr-copy t]
103      ["Cut" wl-fldmgr-cut t]
104      ["Paste" wl-fldmgr-yank t]
105      ["Set petname" wl-fldmgr-set-petname t]
106      ["Rename" wl-fldmgr-rename t]
107      ["Save" wl-fldmgr-save-folders t]
108      "----"
109      ["Unsubscribe" wl-fldmgr-unsubscribe t]
110      ["Display all" wl-fldmgr-access-display-all t])
111     "----"
112     ["Write a message" wl-draft t]
113     "----"
114     ["Toggle Plug Status" wl-toggle-plugged t]
115     ["Change Plug Status" wl-plugged-change t]
116     "----"
117     ["Save Current Status"  wl-save t]
118     ["Update Satus"         wl-status-update t]
119     ["Exit"                 wl-exit t]
120     ))
121
122 (if wl-on-xemacs
123     (defun wl-folder-setup-mouse ()
124       (define-key wl-folder-mode-map 'button2 'wl-folder-click)
125       (define-key wl-folder-mode-map 'button4 'wl-folder-prev-entity)
126       (define-key wl-folder-mode-map 'button5 'wl-folder-next-entity)
127       (define-key wl-folder-mode-map [(shift button4)]
128         'wl-folder-prev-unread)
129       (define-key wl-folder-mode-map [(shift button5)]
130         'wl-folder-next-unread))
131   (defun wl-folder-setup-mouse ()
132     (define-key wl-folder-mode-map [mouse-2] 'wl-folder-click)
133     (define-key wl-folder-mode-map [mouse-4] 'wl-folder-prev-entity)
134     (define-key wl-folder-mode-map [mouse-5] 'wl-folder-next-entity)
135     (define-key wl-folder-mode-map [S-mouse-4] 'wl-folder-prev-unread)
136     (define-key wl-folder-mode-map [S-mouse-5] 'wl-folder-next-unread)))
137
138 (if wl-folder-mode-map
139     nil
140   (setq wl-folder-mode-map (make-sparse-keymap))
141   (define-key wl-folder-mode-map " "    'wl-folder-jump-to-current-entity)
142 ;  (define-key wl-folder-mode-map "\M- " 'wl-folder-open-close)
143   (define-key wl-folder-mode-map "/"    'wl-folder-open-close)
144   (define-key wl-folder-mode-map "\C-m" 'wl-folder-jump-to-current-entity)
145   (define-key wl-folder-mode-map "\M-\C-m" 'wl-folder-update-recursive-current-entity)
146   (define-key wl-folder-mode-map "rc"    'wl-folder-mark-as-read-all-region)
147   (define-key wl-folder-mode-map "c"    'wl-folder-mark-as-read-all-current-entity)
148   (define-key wl-folder-mode-map "g"    'wl-folder-goto-folder)
149   (define-key wl-folder-mode-map "j"    'wl-folder-jump-to-current-entity)
150   (define-key wl-folder-mode-map "w"    'wl-draft)
151   (define-key wl-folder-mode-map "W"    'wl-folder-write-current-folder)
152   (define-key wl-folder-mode-map "\C-c\C-o" 'wl-jump-to-draft-buffer)
153   (define-key wl-folder-mode-map "\C-c\C-a" 'wl-addrmgr)
154   (define-key wl-folder-mode-map "rS"   'wl-folder-sync-region)
155   (define-key wl-folder-mode-map "S"    'wl-folder-sync-current-entity)
156   (define-key wl-folder-mode-map "rs"   'wl-folder-check-region)
157   (define-key wl-folder-mode-map "s"    'wl-folder-check-current-entity)
158   (define-key wl-folder-mode-map "I"    'wl-folder-prefetch-current-entity)
159 ;  (define-key wl-folder-mode-map "D"    'wl-folder-drop-unsync-current-entity)
160   (define-key wl-folder-mode-map "p"    'wl-folder-prev-entity)
161   (define-key wl-folder-mode-map "n"    'wl-folder-next-entity)
162   (define-key wl-folder-mode-map "v"    'wl-folder-toggle-disp-summary)
163   (define-key wl-folder-mode-map "P"    'wl-folder-prev-unread)
164   (define-key wl-folder-mode-map "N"    'wl-folder-next-unread)
165   (define-key wl-folder-mode-map "J"    'wl-folder-jump-folder)
166   (define-key wl-folder-mode-map "f"    'wl-folder-goto-first-unread-folder)
167   (define-key wl-folder-mode-map "o"    'wl-folder-open-all-unread-folder)
168   (define-key wl-folder-mode-map "["    'wl-folder-open-all)
169   (define-key wl-folder-mode-map "]"    'wl-folder-close-all)
170   (define-key wl-folder-mode-map "e"    'wl-folder-expire-current-entity)
171   (define-key wl-folder-mode-map "E"    'wl-folder-empty-trash)
172   (define-key wl-folder-mode-map "F"    'wl-folder-flush-queue)
173   (define-key wl-folder-mode-map "q"    'wl-exit)
174   (define-key wl-folder-mode-map "z"    'wl-folder-suspend)
175   (define-key wl-folder-mode-map "\M-t" 'wl-toggle-plugged)
176   (define-key wl-folder-mode-map "\C-t" 'wl-plugged-change)
177   (define-key wl-folder-mode-map "<"    'beginning-of-buffer)
178   (define-key wl-folder-mode-map ">"    'end-of-buffer)
179   ;; wl-fldmgr
180   (define-key wl-folder-mode-map "m"    'wl-fldmgr-mode-map)
181   (define-key wl-folder-mode-map "*"    'wl-fldmgr-make-multi)
182   (define-key wl-folder-mode-map "+"    'wl-fldmgr-make-group)
183   (define-key wl-folder-mode-map "|"    'wl-fldmgr-make-filter)
184   (define-key wl-folder-mode-map "\M-c" 'wl-fldmgr-copy)
185   (define-key wl-folder-mode-map "\M-w" 'wl-fldmgr-copy-region)
186   (define-key wl-folder-mode-map "\C-k" 'wl-fldmgr-cut)
187   (define-key wl-folder-mode-map "\C-w" 'wl-fldmgr-cut-region)
188   (define-key wl-folder-mode-map "\C-y" 'wl-fldmgr-yank)
189   (define-key wl-folder-mode-map "R"    'wl-fldmgr-rename)
190   (define-key wl-folder-mode-map "u"    'wl-fldmgr-unsubscribe)
191   (define-key wl-folder-mode-map "ru"   'wl-fldmgr-unsubscribe-region)
192   (define-key wl-folder-mode-map "U"    'wl-fldmgr-unsubscribe-region)
193   (define-key wl-folder-mode-map "l"    'wl-fldmgr-access-display-normal)
194   (define-key wl-folder-mode-map "L"    'wl-fldmgr-access-display-all)
195   (define-key wl-folder-mode-map "Z"    'wl-status-update)
196   (define-key wl-folder-mode-map "\C-x\C-s" 'wl-save)
197   (define-key wl-folder-mode-map "\M-s"     'wl-save)
198   (define-key wl-folder-mode-map "\C-xk"    'wl-folder-mimic-kill-buffer)
199   (define-key wl-folder-mode-map "\M-\C-a"
200     'wl-folder-goto-top-of-current-folder)
201   (define-key wl-folder-mode-map "\M-\C-e"
202     'wl-folder-goto-bottom-of-current-folder)
203
204   (wl-folder-setup-mouse)
205   (easy-menu-define
206    wl-folder-mode-menu
207    wl-folder-mode-map
208    "Menu used in Folder mode."
209    wl-folder-mode-menu-spec))
210
211 (defmacro wl-folder-unread-regex (group)
212   (` (concat "^[ ]*.+:[0-9\\*-]+/[^0\\*][0-9]*/[0-9\\*-]+$"
213              (if (, group)
214                  "\\|^[ ]*\\[[+-]\\]"
215                ""))))
216
217 (defmacro wl-folder-buffer-group-p ()
218   (` (save-excursion (beginning-of-line)
219                      (looking-at wl-folder-group-regexp))))
220
221 (defmacro wl-folder-folder-name ()
222   (` (save-excursion
223        (beginning-of-line)
224        (if (or (looking-at "^[ ]*\\[[\\+-]\\]\\(.+\\):[-0-9\\*-]+/[0-9\\*-]+/[0-9\\*-]+\n")
225                (looking-at "^[ ]*\\([^\\[].+\\):.*\n"))
226            (wl-match-buffer 1)))))
227
228 (defmacro wl-folder-entity-name ()
229   (` (save-excursion
230        (beginning-of-line)
231        (if (looking-at "^[ ]*\\([^\\[].+\\):.*\n")
232            (wl-match-buffer 1)))))
233
234 (defun wl-folder-buffer-search-group (group)
235   (re-search-forward
236    (concat
237     "^\\([ \t]*\\)\\[[\\+-]\\]"
238     (regexp-quote group) ":[-0-9-]+/[0-9-]+/[0-9-]+") nil t))
239
240 (defun wl-folder-buffer-search-entity (folder &optional searchname)
241   (let ((search (or searchname (wl-folder-get-petname folder))))
242     (re-search-forward
243      (concat
244       "^[ \t]*"
245       (regexp-quote search) ":[-0-9\\*-]+/[0-9\\*-]+/[0-9\\*-]+") nil t)))
246
247 (defsubst wl-folder-get-folder-name-by-id (entity-id &optional hashtb)
248   (and (numberp entity-id)
249        (elmo-get-hash-val (format "#%d" entity-id)
250                           (or hashtb wl-folder-entity-id-name-hashtb))))
251
252 (defsubst wl-folder-set-id-name (entity-id entity &optional hashtb)
253   (and (numberp entity-id)
254        (elmo-set-hash-val (format "#%d" entity-id)
255                           entity (or hashtb wl-folder-entity-id-name-hashtb))))
256
257 (defmacro wl-folder-get-entity-id (entity)
258   `(get-text-property 0 'wl-folder-entity-id ,entity))
259
260 (defmacro wl-folder-get-entity-from-buffer (&optional getid)
261   `(let ((id (get-text-property (point)
262                                 'wl-folder-entity-id)))
263      (if ,getid
264          id
265        (wl-folder-get-folder-name-by-id id))))
266
267 (defmacro wl-folder-entity-exists-p (entity &optional hashtb)
268   (` (let ((sym (intern-soft (, entity)
269                              (or (, hashtb) wl-folder-entity-hashtb))))
270        (and sym (boundp sym)))))
271
272 (defmacro wl-folder-clear-entity-info (entity &optional hashtb)
273   (` (let ((sym (intern-soft (, entity)
274                              (or (, hashtb) wl-folder-entity-hashtb))))
275        (if (boundp sym)
276            (makunbound sym)))))
277
278 (defmacro wl-folder-get-entity-info (entity &optional hashtb)
279   (` (elmo-get-hash-val (, entity) (or (, hashtb) wl-folder-entity-hashtb))))
280
281 (defmacro wl-folder-set-entity-info (entity value &optional hashtb)
282   (` (let* ((hashtb (or (, hashtb) wl-folder-entity-hashtb))
283             (info (wl-folder-get-entity-info (, entity) hashtb)))
284        (elmo-set-hash-val (, entity)
285                           (if (< (length (, value)) 4)
286                               (append (, value) (list (nth 3 info)))
287                             (, value))
288                           hashtb))))
289
290 (defun wl-folder-persistent-p (folder)
291   (or (and (wl-folder-search-entity-by-name folder wl-folder-entity
292                                             'folder)
293            t) ; on Folder mode.
294       (catch 'found
295         (let ((li wl-save-folder-list))
296           (while li
297             (if (string-match (car li) folder)
298                 (throw 'found t))
299             (setq li (cdr li)))))
300       (not (catch 'found
301              (let ((li wl-no-save-folder-list))
302                (while li
303                  (if (string-match (car li) folder)
304                      (throw 'found t))
305                  (setq li (cdr li))))))))
306
307 ;;; ELMO folder structure with cache.
308 (defmacro wl-folder-get-elmo-folder (entity &optional no-cache)
309   "Get elmo folder structure from entity."
310   (` (if (, no-cache)
311          (elmo-make-folder (elmo-string (, entity)))
312        (or (wl-folder-elmo-folder-cache-get (, entity))
313            (let* ((name (elmo-string (, entity)))
314                   (folder (elmo-make-folder name)))
315              (wl-folder-elmo-folder-cache-put name folder)
316              folder)))))
317
318 (defmacro wl-folder-elmo-folder-cache-get (name &optional hashtb)
319   "Returns a elmo folder structure associated with NAME from HASHTB.
320 Default HASHTB is `wl-folder-elmo-folder-hashtb'."
321   (` (elmo-get-hash-val (, name)
322                         (or (, hashtb) wl-folder-elmo-folder-hashtb))))
323
324 (defmacro wl-folder-elmo-folder-cache-put (name folder &optional hashtb)
325   "Get folder elmo folder structure on HASHTB for folder with NAME.
326 Default HASHTB is `wl-folder-elmo-folder-hashtb'."
327   (` (elmo-set-hash-val (, name) (, folder)
328                         (or (, hashtb) wl-folder-elmo-folder-hashtb))))
329
330 (defun wl-folder-prev-entity ()
331   (interactive)
332   (forward-line -1))
333
334 (defun wl-folder-next-entity ()
335   (interactive)
336   (forward-line 1))
337
338 (defun wl-folder-prev-entity-skip-invalid (&optional hereto)
339   "move to previous entity. skip unsubscribed or removed entity."
340   (interactive)
341   (if hereto
342       (end-of-line))
343   (if (re-search-backward wl-folder-entity-regexp nil t)
344       (beginning-of-line)
345     (goto-char (point-min))))
346
347 (defun wl-folder-next-entity-skip-invalid (&optional hereto)
348   "Move to next entity. skip unsubscribed or removed entity."
349   (interactive)
350   (beginning-of-line)
351   (if (not hereto)
352       (forward-line 1))
353   (if (re-search-forward wl-folder-entity-regexp nil t)
354       (beginning-of-line)
355     (goto-char (point-max))))
356
357 (defun wl-folder-search-group-entity-by-name (name entity)
358   (wl-folder-search-entity-by-name name entity 'group))
359
360 (defun wl-folder-search-entity-by-name (name entity &optional type)
361   (let ((entities (list entity))
362         entity-stack)
363     (catch 'done
364       (while entities
365         (setq entity (wl-pop entities))
366         (cond
367          ((consp entity)
368           (if (and (not (eq type 'folder))
369                    (string= name (car entity)))
370               (throw 'done entity))
371           (and entities
372                (wl-push entities entity-stack))
373           (setq entities (nth 2 entity)))
374          ((and (not (eq type 'group))
375                (stringp entity))
376           (if (string= name entity)
377               (throw 'done entity))))
378         (unless entities
379           (setq entities (wl-pop entity-stack)))))))
380
381 (defun wl-folder-search-entity-list-by-name (name entity &optional get-id)
382   (let ((entities (list entity))
383         entity-stack ret-val)
384     (while entities
385       (setq entity (wl-pop entities))
386       (cond
387        ((consp entity)
388         (and entities
389              (wl-push entities entity-stack))
390         (setq entities (nth 2 entity)))
391        ((stringp entity)
392         (if (string= name entity)
393             (wl-append ret-val (if get-id
394                                    (list (wl-folder-get-entity-id entity))
395                                  (list entity))))))
396       (unless entities
397         (setq entities (wl-pop entity-stack))))
398     ret-val))
399
400 (defun wl-folder-get-prev-folder (id &optional unread)
401   (let ((name (if (stringp id)
402                   id
403                 (wl-folder-get-folder-name-by-id id)))
404         entity entity-stack last-entity finfo
405         (entities (list wl-folder-entity)))
406     (catch 'done
407       (while entities
408         (setq entity (wl-pop entities))
409         (cond
410          ((consp entity)
411 ;;        (if (and (string= name (car entity))
412 ;;                 (eq id (wl-folder-get-entity-id (car entity))))
413 ;;            (throw 'done last-entity))
414           (and entities
415                (wl-push entities entity-stack))
416           (setq entities (nth 2 entity)))
417          ((stringp entity)
418           (if (and (string= name entity)
419                    ;; don't use eq, `id' is string on Nemacs.
420                    (equal id (wl-folder-get-entity-id entity)))
421               (throw 'done last-entity))
422           (if (or (not unread)
423                   (and (setq finfo (wl-folder-get-entity-info entity))
424                        (and (nth 0 finfo)(nth 1 finfo))
425                        (> (+ (nth 0 finfo)(nth 1 finfo)) 0)))
426               (setq last-entity entity))))
427         (unless entities
428           (setq entities (wl-pop entity-stack)))))))
429
430 (defun wl-folder-get-next-folder (id &optional unread)
431   (let ((name (if (stringp id)
432                   id
433                 (wl-folder-get-folder-name-by-id id)))
434         entity entity-stack found finfo
435         (entities (list wl-folder-entity)))
436     (catch 'done
437       (while entities
438         (setq entity (wl-pop entities))
439         (cond
440          ((consp entity)
441 ;;;       (if (and (string= name (car entity))
442 ;;;                (eq id (wl-folder-get-entity-id (car entity))))
443 ;;;           (setq found t))
444           (and entities
445                (wl-push entities entity-stack))
446           (setq entities (nth 2 entity)))
447          ((stringp entity)
448           (if found
449               (when (or (not unread)
450                         (and (setq finfo (wl-folder-get-entity-info entity))
451                              (and (nth 0 finfo)(nth 1 finfo))
452                              (> (+ (nth 0 finfo)(nth 1 finfo)) 0)))
453                 (throw 'done entity))
454             (if (and (string= name entity)
455                      ;; don't use eq, `id' is string on Nemacs.
456                      (equal id (wl-folder-get-entity-id entity)))
457                 (setq found t)))))
458         (unless entities
459           (setq entities (wl-pop entity-stack)))))))
460
461 (defun wl-folder-flush-queue ()
462   "Flush queue."
463   (interactive)
464   (let ((cur-buf (current-buffer))
465         (wl-auto-select-first nil)
466         (wl-plugged t)
467         emptied)
468     (if elmo-enable-disconnected-operation
469         (elmo-dop-queue-flush 'force)) ; Try flushing all queue.
470     (if (not (elmo-folder-list-messages
471               (wl-folder-get-elmo-folder wl-queue-folder)))
472         (message "No sending queue exists.")
473       (if wl-stay-folder-window
474           (wl-folder-select-buffer
475            (wl-summary-get-buffer-create wl-queue-folder)))
476       (wl-summary-goto-folder-subr wl-queue-folder 'force-update nil)
477       (unwind-protect
478           (wl-draft-queue-flush)
479         (if (get-buffer-window cur-buf)
480             (select-window (get-buffer-window cur-buf)))
481         (set-buffer cur-buf)
482         (if wl-stay-folder-window
483             (wl-folder-toggle-disp-summary 'off wl-queue-folder)
484           (switch-to-buffer cur-buf))))))
485
486 (defun wl-folder-empty-trash ()
487   "Empty trash."
488   (interactive)
489   (let ((cur-buf (current-buffer))
490         (wl-auto-select-first nil)
491         trash-buf emptied)
492     (if wl-stay-folder-window
493         (wl-folder-select-buffer
494          (wl-summary-get-buffer-create wl-trash-folder)))
495     (wl-summary-goto-folder-subr wl-trash-folder 'force-update nil nil t)
496     (setq trash-buf (current-buffer))
497     (unwind-protect
498         (setq emptied (wl-summary-delete-all-msgs))
499       (when emptied
500         (setq wl-thread-entities nil
501               wl-thread-entity-list nil)
502         (if wl-summary-cache-use (wl-summary-save-view-cache))
503         (elmo-folder-commit wl-summary-buffer-elmo-folder))
504       (if (get-buffer-window cur-buf)
505           (select-window (get-buffer-window cur-buf)))
506       (set-buffer cur-buf)
507       (if emptied
508           (wl-folder-set-folder-updated wl-trash-folder '(0 0 0)))
509       (if wl-stay-folder-window
510           (wl-folder-toggle-disp-summary 'off wl-trash-folder)
511         (switch-to-buffer cur-buf))
512       (and trash-buf
513            (kill-buffer trash-buf)))))
514
515 (defun wl-folder-goto-top-of-current-folder (&optional arg)
516   "Move backward to the top of the current folder group.
517 Optional argument ARG is repeart count."
518   (interactive "P")
519   (if (re-search-backward
520        "^ *\\[[\\+-]\\]" nil t (if arg (prefix-numeric-value arg)))
521       (beginning-of-line)
522     (goto-char (point-min))))
523
524 (defun wl-folder-goto-bottom-of-current-folder (indent)
525   "Move forward to the bottom of the current folder group."
526   (interactive
527    (let ((indent
528           (save-excursion
529             (beginning-of-line)
530             (if (looking-at "^ *")
531                 (buffer-substring (match-beginning 0)(1- (match-end 0)))
532               ""))))
533      (list indent)))
534   (if (catch 'done
535         (while (re-search-forward "^ *" nil t)
536           (if (<= (length (match-string 0))
537                   (length indent))
538               (throw 'done nil)))
539         (throw 'done t))
540       (goto-char (point-max))))
541
542 (defsubst wl-folder-update-group (entity diffs &optional is-group)
543   (save-excursion
544     (let ((path (wl-folder-get-path
545                  wl-folder-entity
546                  (wl-folder-get-entity-id entity)
547                  entity)))
548       (if (not is-group)
549           ;; delete itself from path
550           (setq path (delete (nth (- (length path) 1) path) path)))
551       (goto-char (point-min))
552       (catch 'done
553         (while path
554           ;; goto the path line.
555           (if (or (eq (car path) 0) ; update desktop
556                   (wl-folder-buffer-search-group
557                    (wl-folder-get-petname
558                     (if (stringp (car path))
559                         (car path)
560                       (wl-folder-get-folder-name-by-id
561                        (car path))))))
562               ;; update it.
563               (wl-folder-update-diff-line diffs)
564             (throw 'done t))
565           (setq path (cdr path)))))))
566
567 (defun wl-folder-maybe-load-folder-list (entity)
568   (when (null (caddr entity))
569     (setcdr (cdr entity)
570             (elmo-msgdb-flist-load (car entity)))
571     (when (cddr entity)
572       (let (diffs)
573         (save-excursion
574           (wl-folder-entity-assign-id entity
575                                       wl-folder-entity-id-name-hashtb
576                                       t)
577           (setq diffs (wl-fldmgr-add-entity-hashtb (list entity)))
578           (unless (equal diffs '(0 0 0))
579             (wl-folder-update-group (car entity) diffs t)))))))
580
581 (defsubst wl-folder-force-fetch-p (entity)
582   (cond
583    ((consp wl-force-fetch-folders)
584     (wl-string-match-member entity wl-force-fetch-folders))
585    (t
586     wl-force-fetch-folders)))
587
588 (defun wl-folder-jump-to-current-entity (&optional arg)
589   "Enter the current folder.  If optional ARG exists, update folder list."
590   (interactive "P")
591   (beginning-of-line)
592   (let (entity beg end indent opened fname err fld-name)
593     (cond
594      ((looking-at wl-folder-group-regexp)
595       (save-excursion
596         (setq fname (wl-folder-get-realname (wl-match-buffer 3)))
597         (setq indent (wl-match-buffer 1))
598         (setq opened (wl-match-buffer 2))
599         (if (string= opened "+")
600             (progn
601               (setq entity (wl-folder-search-group-entity-by-name
602                             fname
603                             wl-folder-entity))
604               (setq beg (point))
605               (if arg
606                   (wl-folder-update-recursive-current-entity entity)
607                 ;; insert as opened
608                 (setcdr (assoc (car entity) wl-folder-group-alist) t)
609                 (if (eq 'access (cadr entity))
610                     (wl-folder-maybe-load-folder-list entity))
611                 ;(condition-case errobj
612                     (progn
613                       (if (or (wl-folder-force-fetch-p (car entity))
614                               (and
615                                (eq 'access (cadr entity))
616                                (null (caddr entity))))
617                           (wl-folder-update-newest indent entity)
618                         (wl-folder-insert-entity indent entity))
619                       (wl-highlight-folder-path wl-folder-buffer-cur-path))
620                  ; (quit
621                  ;  (setq err t)
622                  ;  (setcdr (assoc fname wl-folder-group-alist) nil))
623                  ; (error
624                  ;  (elmo-display-error errobj t)
625                  ;  (ding)
626                  ;  (setq err t)
627                  ;  (setcdr (assoc fname wl-folder-group-alist) nil)))
628                 (if (not err)
629                     (let ((buffer-read-only nil))
630                       (delete-region (save-excursion (beginning-of-line)
631                                                      (point))
632                                      (save-excursion (end-of-line)
633                                                      (+ 1 (point))))))))
634           (setq beg (point))
635           (end-of-line)
636           (save-match-data
637             (setq end
638                   (progn (wl-folder-goto-bottom-of-current-folder indent)
639                          (beginning-of-line)
640                          (point))))
641           (setq entity (wl-folder-search-group-entity-by-name
642                         fname
643                         wl-folder-entity))
644           (let ((buffer-read-only nil))
645             (delete-region beg end))
646           (setcdr (assoc (car entity) wl-folder-group-alist) nil)
647           (wl-folder-insert-entity indent entity) ; insert entity
648           (forward-line -1)
649           (wl-highlight-folder-path wl-folder-buffer-cur-path)
650 ;         (wl-delete-all-overlays)
651 ;         (wl-highlight-folder-current-line)
652           )))
653      ((setq fld-name (wl-folder-entity-name))
654       (wl-folder-set-current-entity-id
655        (get-text-property (point) 'wl-folder-entity-id))
656       (setq fld-name (wl-folder-get-folder-name-by-id
657                       wl-folder-buffer-cur-entity-id))
658       (let ((summary-buf (wl-summary-get-buffer-create fld-name arg))
659             error-selecting)
660         (if (or wl-stay-folder-window wl-summary-use-frame)
661             (wl-folder-select-buffer summary-buf)
662           (if (and summary-buf
663                    (get-buffer-window summary-buf))
664               (delete-window)))
665         (wl-summary-goto-folder-subr fld-name
666                                      (wl-summary-get-sync-range
667                                       (wl-folder-get-elmo-folder fld-name))
668                                      nil arg t)))))
669   (set-buffer-modified-p nil))
670
671 (defun wl-folder-close-entity (entity)
672   (let ((entities (list entity))
673         entity-stack)
674     (while entities
675       (setq entity (wl-pop entities))
676       (cond
677        ((consp entity)
678         (setcdr (assoc (car entity) wl-folder-group-alist) nil)
679         (and entities
680              (wl-push entities entity-stack))
681         (setq entities (nth 2 entity))))
682       (unless entities
683         (setq entities (wl-pop entity-stack))))))
684
685 (defun wl-folder-update-recursive-current-entity (&optional entity)
686   (interactive)
687   (when (wl-folder-buffer-group-p)
688     (cond
689      ((string= (wl-match-buffer 2) "+")
690       (save-excursion
691         (if entity ()
692           (setq entity
693                 (wl-folder-search-group-entity-by-name
694                  (wl-folder-get-realname (wl-match-buffer 3))
695                  wl-folder-entity)))
696         (let ((inhibit-read-only t)
697               (entities (list entity))
698               entity-stack err indent)
699           (while (and entities (not err))
700             (setq entity (wl-pop entities))
701             (cond
702              ((consp entity)
703               (wl-folder-close-entity entity)
704               (setcdr (assoc (car entity) wl-folder-group-alist) t)
705               (unless (wl-folder-buffer-search-group
706                        (wl-folder-get-petname (car entity)))
707                 (error "%s: not found group" (car entity)))
708               (setq indent (wl-match-buffer 1))
709               (if (eq 'access (cadr entity))
710                   (wl-folder-maybe-load-folder-list entity))
711               (beginning-of-line)
712               (setq err nil)
713               (save-excursion
714                 (condition-case errobj
715                     (wl-folder-update-newest indent entity)
716                   (quit
717                    (setq err t)
718                    (setcdr (assoc (car entity) wl-folder-group-alist) nil))
719                   (error
720                    (elmo-display-error errobj t)
721                    (ding)
722                    (setq err t)
723                    (setcdr (assoc (car entity) wl-folder-group-alist) nil)))
724                 (if (not err)
725                     (delete-region (save-excursion (beginning-of-line)
726                                                    (point))
727                                    (save-excursion (end-of-line)
728                                                    (+ 1 (point))))))
729               ;;
730               (and entities
731                    (wl-push entities entity-stack))
732               (setq entities (nth 2 entity))))
733             (unless entities
734               (setq entities (wl-pop entity-stack)))))
735         (set-buffer-modified-p nil)))
736      (t
737       (wl-folder-jump-to-current-entity)))))
738
739 (defun wl-folder-no-auto-check-folder-p (folder)
740   (if (stringp folder)
741       (if (catch 'found
742             (let ((li wl-auto-check-folder-list))
743               (while li
744                 (if (string-match (car li) folder)
745                     (throw 'found t))
746                 (setq li (cdr li)))))
747           nil
748         (catch 'found
749           (let ((li wl-auto-uncheck-folder-list))
750             (while li
751               (if (string-match (car li) folder)
752                   (throw 'found t))     ; no check!
753               (setq li (cdr li))))))))
754
755 (defsubst wl-folder-add-folder-info (pre-value value)
756   (list
757    (+ (or (nth 0 pre-value) 0) (or (nth 0 value) 0))
758    (+ (or (nth 1 pre-value) 0) (or (nth 1 value) 0))
759    (+ (or (nth 2 pre-value) 0) (or (nth 2 value) 0))))
760
761 (defun wl-folder-check-entity (entity &optional auto)
762   "Check unsync message number."
763   (let ((start-pos (point))
764         ret-val)
765     (run-hooks 'wl-folder-check-entity-pre-hook)
766     (if (and (consp entity)             ;; group entity
767              wl-folder-check-async)     ;; very fast
768         (setq ret-val (wl-folder-check-entity-async entity auto))
769       (save-excursion
770         (cond
771          ((consp entity)
772           (let ((flist (if auto
773                            (elmo-delete-if
774                             'wl-folder-no-auto-check-folder-p
775                             (nth 2 entity))
776                          (nth 2 entity)))
777                 (wl-folder-check-entity-pre-hook nil)
778                 (wl-folder-check-entity-hook nil)
779                 new unread all)
780             (while flist
781               (setq ret-val
782                     (wl-folder-add-folder-info
783                      ret-val
784                      (wl-folder-check-entity (car flist))))
785               (setq flist (cdr flist)))
786             ;(wl-folder-buffer-search-entity (car entity))
787             ;(wl-folder-update-line ret-val)
788             ))
789          ((stringp entity)
790           (message "Checking \"%s\"" entity)
791           (setq ret-val (wl-folder-check-one-entity
792                          entity))
793           (goto-char start-pos)
794           (sit-for 0))
795          (t
796           (message "Uncheck(unplugged) \"%s\"" entity)))))
797     (if ret-val
798         (message "Checking \"%s\" is done."
799                  (if (consp entity) (car entity) entity)))
800     (run-hooks 'wl-folder-check-entity-hook)
801     ret-val))
802
803 (defun wl-folder-check-one-entity (entity &optional biff)
804   (let* ((folder (wl-folder-get-elmo-folder entity biff))
805          (nums (condition-case err
806                    (progn
807                      (if biff (elmo-folder-set-biff-internal folder t))
808                      (if (wl-string-match-member entity wl-strict-diff-folders)
809                          (elmo-strict-folder-diff folder)
810                        (elmo-folder-diff folder)))
811                  (error
812                   ;; maybe not exist folder.
813                   (if (and (not (memq 'elmo-open-error
814                                       (get (car err) 'error-conditions)))
815                            (not (elmo-folder-exists-p folder)))
816                       (wl-folder-create-subr folder)
817                     (signal (car err) (cdr err))))))
818          (new (elmo-diff-new nums))
819          (nums (cons (elmo-diff-unread nums) (elmo-diff-all nums)))
820          unread unsync nomif)
821     (if (and (eq wl-folder-notify-deleted 'sync)
822              (car nums)
823              (or (> 0 (car nums)) (> 0 (cdr nums))))
824         (progn
825           (wl-folder-sync-entity entity)
826           (setq nums (elmo-folder-diff folder)))
827       (unless wl-folder-notify-deleted
828         (setq unsync (if (car nums)
829                          (max 0 (car nums))
830                        nil))
831         (setq nomif (if (cdr nums)
832                         (max 0 (cdr nums))
833                       nil))
834         (setq nums (cons unsync nomif)))
835       (setq unread (or ;; If server diff, All unreads are
836                         ; treated as unsync.
837                     (if (elmo-folder-use-flag-p folder)
838                         (car nums))
839                     (elmo-folder-get-info-unread folder)
840                     (wl-summary-count-unread (elmo-msgdb-mark-load
841                                               (elmo-folder-msgdb-path
842                                                folder)))))
843       (when new (setq unread (- unread new)))
844       (wl-folder-entity-hashtb-set wl-folder-entity-hashtb entity
845                                    (list (or new (car nums))
846                                          unread
847                                          (cdr nums))
848                                    (get-buffer wl-folder-buffer-name)))
849     (setq wl-folder-info-alist-modified t)
850     (sit-for 0)
851     (list (if wl-folder-notify-deleted
852               (or new (car nums) 0)
853             (max 0 (or new (car nums) 0)))
854           unread
855           (cdr nums))))
856
857 (defun wl-folder-check-entity-async (entity &optional auto)
858   (let ((elmo-nntp-groups-async t)
859         (elist (if auto
860                    (elmo-delete-if
861                     'wl-folder-no-auto-check-folder-p
862                     (wl-folder-get-entity-list entity))
863                  (wl-folder-get-entity-list entity)))
864         (nntp-connection-keys nil)
865         name folder folder-list
866         sync-folder-list
867         async-folder-list
868         server
869         ret-val)
870     (while elist
871       (setq folder (wl-folder-get-elmo-folder (car elist)))
872       (if (not (elmo-folder-plugged-p folder))
873           (message "Uncheck \"%s\"" (car elist))
874         (setq folder-list
875               (elmo-folder-get-primitive-list folder))
876         (cond ((elmo-folder-contains-type folder 'nntp)
877                (wl-append async-folder-list (list folder))
878                (while folder-list
879                  (when (eq (elmo-folder-type-internal (car folder-list))
880                            'nntp)
881                    (when (not (string=
882                                server
883                                (elmo-net-folder-server-internal
884                                 (car folder-list))))
885                      (setq server (elmo-net-folder-server-internal
886                                    (car folder-list)))
887                      (message "Checking on \"%s\"" server))
888                    (setq nntp-connection-keys
889                          (elmo-nntp-get-folders-info-prepare
890                           (car folder-list)
891                           nntp-connection-keys)))
892                  (setq folder-list (cdr folder-list))))
893               (t
894                (wl-append sync-folder-list (list folder)))))
895       (setq elist (cdr elist)))
896     ;; check local entity at first
897     (while (setq folder (pop sync-folder-list))
898       (if (not (elmo-folder-plugged-p folder))
899           (message "Uncheck \"%s\"" (elmo-folder-name-internal folder))
900         (message "Checking \"%s\"" (elmo-folder-name-internal folder))
901         (setq ret-val
902               (wl-folder-add-folder-info
903                ret-val
904                (wl-folder-check-one-entity (elmo-folder-name-internal
905                                             folder))))
906         ;;(sit-for 0)
907         ))
908     ;; check network entity at last
909     (when async-folder-list
910       (elmo-nntp-get-folders-info nntp-connection-keys)
911       (while (setq folder (pop async-folder-list))
912         (if (not (elmo-folder-plugged-p folder))
913             (message "Uncheck \"%s\"" (elmo-folder-name-internal folder))
914           (message "Checking \"%s\"" (elmo-folder-name-internal folder))
915           (setq ret-val
916                 (wl-folder-add-folder-info
917                  ret-val
918                  (wl-folder-check-one-entity (elmo-folder-name-internal
919                                               folder))))
920           ;;(sit-for 0)
921           )))
922     ret-val))
923
924 ;;
925 (defun wl-folder-resume-entity-hashtb-by-finfo (entity-hashtb info-alist)
926   "Resume unread info for entity alist."
927   (let (info)
928     (while info-alist
929       (setq info (nth 1 (car info-alist)))
930       (wl-folder-set-entity-info (caar info-alist)
931                                  (list (nth 2 info)(nth 3 info)(nth 1 info))
932                                  entity-hashtb)
933       (setq info-alist (cdr info-alist)))))
934
935 (defun wl-folder-move-path (path)
936   (let ((fp (if (consp path)
937                 path
938               ;; path is entity-id
939               (wl-folder-get-path wl-folder-entity path))))
940     (goto-char (point-min))
941     (while (and fp
942                 (not (eobp)))
943       (when (equal (car fp)
944                    (wl-folder-get-entity-from-buffer t))
945         (setq fp (cdr fp))
946         (setq wl-folder-buffer-cur-point (point)))
947       (forward-line 1))
948     (and wl-folder-buffer-cur-point
949          (goto-char wl-folder-buffer-cur-point))))
950
951 (defun wl-folder-set-current-entity-id (entity-id)
952   (let ((buf (get-buffer wl-folder-buffer-name)))
953     (if buf
954         (save-excursion
955           (set-buffer buf)
956           (setq wl-folder-buffer-cur-entity-id entity-id)
957           (setq wl-folder-buffer-cur-path (wl-folder-get-path wl-folder-entity
958                                                               entity-id))
959           (wl-highlight-folder-path wl-folder-buffer-cur-path)
960           (and wl-folder-move-cur-folder
961                wl-folder-buffer-cur-point
962                (goto-char wl-folder-buffer-cur-point))))
963     (if (eq (current-buffer) buf)
964         (and wl-folder-move-cur-folder
965              wl-folder-buffer-cur-point
966              (goto-char wl-folder-buffer-cur-point)))))
967
968 (defun wl-folder-check-current-entity ()
969   "Check folder at position.
970 If current line is group folder, check all sub entries."
971   (interactive)
972   (let* ((entity-name (wl-folder-get-entity-from-buffer))
973          (group (wl-folder-buffer-group-p))
974          (desktop (string= entity-name wl-folder-desktop-name)))
975     (when entity-name
976       (wl-folder-check-entity
977        (if group
978            (wl-folder-search-group-entity-by-name entity-name
979                                                   wl-folder-entity)
980          entity-name)
981        desktop))))
982
983 (defun wl-folder-sync-entity (entity &optional unread-only)
984   "Synchronize the msgdb of ENTITY."
985   (cond
986    ((consp entity)
987     (let ((flist (nth 2 entity)))
988       (while flist
989         (wl-folder-sync-entity (car flist) unread-only)
990         (setq flist (cdr flist)))))
991    ((stringp entity)
992     (let* ((folder (wl-folder-get-elmo-folder entity))
993            (nums (wl-folder-get-entity-info entity))
994            (wl-summary-highlight (if (or (wl-summary-sticky-p folder)
995                                          (wl-summary-always-sticky-folder-p
996                                           folder))
997                                      wl-summary-highlight))
998            wl-auto-select-first new unread)
999       (setq new (or (car nums) 0))
1000       (setq unread (or (cadr nums) 0))
1001       (if (or (not unread-only)
1002               (or (< 0 new) (< 0 unread)))
1003           (let ((wl-summary-buffer-name (concat
1004                                          wl-summary-buffer-name
1005                                          (symbol-name this-command)))
1006                 (wl-summary-use-frame nil)
1007                 (wl-summary-always-sticky-folder-list nil))
1008             (save-window-excursion
1009               (save-excursion
1010                 (wl-summary-goto-folder-subr entity
1011                                              (wl-summary-get-sync-range
1012                                               folder)
1013                                              nil nil nil t)
1014                 (wl-summary-exit)))))))))
1015
1016 (defun wl-folder-sync-current-entity (&optional unread-only)
1017   "Synchronize the folder at position.
1018 If current line is group folder, check all subfolders."
1019   (interactive "P")
1020   (save-excursion
1021     (let ((entity-name (wl-folder-get-entity-from-buffer))
1022           (group (wl-folder-buffer-group-p)))
1023       (when (and entity-name
1024                  (y-or-n-p (format "Sync %s? " entity-name)))
1025         (wl-folder-sync-entity
1026          (if group
1027              (wl-folder-search-group-entity-by-name entity-name
1028                                                     wl-folder-entity)
1029            entity-name)
1030          unread-only)
1031         (message "Syncing %s is done!" entity-name)))))
1032
1033 (defun wl-folder-mark-as-read-all-entity (entity)
1034   "Mark as read all messages in the ENTITY."
1035   (cond
1036    ((consp entity)
1037     (let ((flist (nth 2 entity)))
1038       (while flist
1039         (wl-folder-mark-as-read-all-entity (car flist))
1040         (setq flist (cdr flist)))))
1041    ((stringp entity)
1042     (let* ((nums (wl-folder-get-entity-info entity))
1043            (folder (wl-folder-get-elmo-folder entity))
1044            (wl-summary-highlight (if (or (wl-summary-sticky-p folder)
1045                                          (wl-summary-always-sticky-folder-p
1046                                           folder))
1047                                      wl-summary-highlight))
1048            wl-auto-select-first new unread)
1049       (setq new (or (car nums) 0))
1050       (setq unread (or (cadr nums) 0))
1051       (if (or (< 0 new) (< 0 unread))
1052           (save-window-excursion
1053             (save-excursion
1054               (let ((wl-summary-buffer-name (concat
1055                                              wl-summary-buffer-name
1056                                              (symbol-name this-command)))
1057                     (wl-summary-use-frame nil)
1058                     (wl-summary-always-sticky-folder-list nil))
1059                 (wl-summary-goto-folder-subr entity
1060                                              (wl-summary-get-sync-range folder)
1061                                              nil)
1062                 (wl-summary-mark-as-read-all)
1063                 (wl-summary-exit))))
1064         (sit-for 0))))))
1065
1066 (defun wl-folder-mark-as-read-all-current-entity ()
1067   "Mark as read all messages in the folder at position.
1068 If current line is group folder, all subfolders are marked."
1069   (interactive)
1070   (save-excursion
1071     (let ((entity-name (wl-folder-get-entity-from-buffer))
1072           (group (wl-folder-buffer-group-p))
1073           summary-buf)
1074       (when (and entity-name
1075                  (y-or-n-p (format "Mark all messages in %s as read? " entity-name)))
1076         (wl-folder-mark-as-read-all-entity
1077          (if group
1078              (wl-folder-search-group-entity-by-name entity-name
1079                                                     wl-folder-entity)
1080            entity-name))
1081         (message "All messages in %s are marked!" entity-name)))))
1082
1083 (defun wl-folder-check-region (beg end)
1084   (interactive "r")
1085   (goto-char beg)
1086   (beginning-of-line)
1087   (setq beg (point))
1088   (goto-char end)
1089   (beginning-of-line)
1090   (setq end (point))
1091   (goto-char beg)
1092   (let ((inhibit-read-only t)
1093         entity)
1094     (while (< (point) end)
1095       ;; normal folder entity
1096       (if (looking-at "^[\t ]*\\([^\\[]+\\):\\(.*\\)\n")
1097           (save-excursion
1098             (setq entity (wl-folder-get-entity-from-buffer))
1099             (if (not (elmo-folder-plugged-p (wl-folder-get-elmo-folder
1100                                              entity)))
1101                 (message "Uncheck %s" entity)
1102               (message "Checking %s" entity)
1103               (wl-folder-check-one-entity entity)
1104               (sit-for 0))))
1105       (forward-line 1)))
1106   (message ""))
1107
1108 (defun wl-folder-sync-region (beg end)
1109   (interactive "r")
1110   (goto-char beg)
1111   (beginning-of-line)
1112   (setq beg (point))
1113   (goto-char end)
1114   (end-of-line)
1115   (setq end (point))
1116   (goto-char beg)
1117   (while (< (point) end)
1118     ;; normal folder entity
1119     (if (looking-at "^[\t ]*\\([^\\[]+\\):\\(.*\\)\n")
1120         (save-excursion
1121           (let ((inhibit-read-only t)
1122                 entity)
1123             (setq entity (wl-folder-get-entity-from-buffer))
1124             (wl-folder-sync-entity entity)
1125             (message "Syncing %s is done!" entity)
1126             (sit-for 0))))
1127     (forward-line 1))
1128   (message ""))
1129
1130 (defun wl-folder-mark-as-read-all-region (beg end)
1131   (interactive "r")
1132   (goto-char beg)
1133   (beginning-of-line)
1134   (setq beg (point))
1135   (goto-char end)
1136   (end-of-line)
1137   (setq end (point))
1138   (goto-char beg)
1139   (while (< (point) end)
1140     ;; normal folder entity
1141     (if (looking-at "^[\t ]*\\([^\\[]+\\):\\(.*\\)\n")
1142         (save-excursion
1143           (let ((inhibit-read-only t)
1144                 entity)
1145             (setq entity (wl-folder-get-entity-from-buffer))
1146             (wl-folder-mark-as-read-all-entity entity)
1147             (message "All messages in %s are marked!" entity)
1148             (sit-for 0))))
1149     (forward-line 1))
1150   (message ""))
1151
1152 (defsubst wl-create-access-init-load-p (folder)
1153   (let ((no-load-regexp (when (and
1154                                (not wl-folder-init-load-access-folders)
1155                                wl-folder-init-no-load-access-folders)
1156                           (mapconcat 'identity
1157                                      wl-folder-init-no-load-access-folders
1158                                      "\\|")))
1159         (load-regexp (and wl-folder-init-load-access-folders
1160                           (mapconcat 'identity
1161                                      wl-folder-init-load-access-folders
1162                                      "\\|"))))
1163     (cond (load-regexp (string-match load-regexp folder))
1164           (t (not (and no-load-regexp
1165                        (string-match no-load-regexp folder)))))))
1166
1167 (defun wl-create-access-folder-entity (name)
1168   (let (flists flist)
1169     (when (wl-create-access-init-load-p name)
1170       (setq flists (elmo-msgdb-flist-load name)) ; load flist.
1171       (setq flist (car flists))
1172       (while flist
1173         (when (consp (car flist))
1174           (setcdr (cdar flist)
1175                   (wl-create-access-folder-entity (caar flist))))
1176         (setq flist (cdr flist)))
1177       flists)))
1178
1179 (defun wl-create-folder-entity-from-buffer ()
1180   "Create folder entity recursively."
1181   (cond
1182    ((looking-at "^[ \t]*$")             ; blank line
1183     (goto-char (+ 1(match-end 0)))
1184     'ignore)
1185    ((looking-at "^#.*$")                ; comment
1186     (goto-char (+ 1 (match-end 0)))
1187     'ignore)
1188    ((looking-at "^[\t ]*\\(.+\\)[\t ]*{[\t ]*$") ; group definition
1189     (let (name entity flist)
1190       (setq name (wl-match-buffer 1))
1191       (goto-char (+ 1 (match-end 0)))
1192       (while (setq entity (wl-create-folder-entity-from-buffer))
1193         (unless (eq entity 'ignore)
1194           (wl-append flist (list entity))))
1195       (if (looking-at "^[\t ]*}[\t ]*$") ; end of group
1196           (progn
1197             (goto-char (+ 1 (match-end 0)))
1198             (if (wl-string-assoc name wl-folder-petname-alist)
1199                 (error "%s already defined as petname" name))
1200             (list name 'group flist))
1201         (error "Syntax error in folder definition"))))
1202    ((looking-at "^[\t ]*\\([^\t \n]+\\)[\t ]*/$") ; access it!
1203     (let (name)
1204       (setq name (wl-match-buffer 1))
1205       (goto-char (+ 1 (match-end 0)))
1206 ;      (condition-case ()
1207 ;         (unwind-protect
1208 ;             (setq flist (elmo-list-folders name)))
1209 ;       (error (message "Access to folder %s failed." name)))
1210 ;;       (setq flist (elmo-msgdb-flist-load name)) ; load flist.
1211 ;;       (setq unsublist (nth 1 flist))
1212 ;;       (setq flist (car flist))
1213 ;;       (list name 'access flist unsublist)))
1214       (append (list name 'access) (wl-create-access-folder-entity name))))
1215    ;((looking-at "^[\t ]*\\([^\t \n}]+\\)[\t ]*\\(\"[^\"]*\"\\)?[\t ]*$") ; normal folder entity
1216    ((looking-at "^[\t ]*=[ \t]+\\([^\n]+\\)$"); petname definition
1217     (goto-char (+ 1 (match-end 0)))
1218     (let ((rest (elmo-match-buffer 1))
1219           petname)
1220       (when (string-match "\\(\"[^\"]*\"\\)[\t ]*$" rest)
1221         (setq petname (elmo-delete-char ?\" (elmo-match-string 1 rest)))
1222         (setq rest (substring rest 0 (match-beginning 0))))
1223       (when (string-match "^[\t ]*\\(.*[^\t ]+\\)[\t ]+$" rest)
1224         (wl-folder-append-petname (elmo-match-string 1 rest)
1225                                   petname))
1226       'ignore))
1227    ((looking-at "^[ \t]*}[ \t]*$") ; end of group
1228     nil)
1229    ((looking-at "^.*$") ; normal folder entity
1230     (goto-char (+ 1 (match-end 0)))
1231     (let ((rest (elmo-match-buffer 0))
1232           realname petname)
1233       (if (string-match "\\(\"[^\"]*\"\\)[\t ]*$" rest)
1234           (progn
1235             (setq petname (elmo-delete-char ?\" (elmo-match-string 1 rest)))
1236             (setq rest (substring rest 0 (match-beginning 0)))
1237             (when (string-match "^[\t ]*\\(.*[^\t ]+\\)[\t ]+$" rest)
1238               (wl-folder-append-petname
1239                (setq realname (elmo-match-string 1 rest))
1240                petname)
1241               realname))
1242         (if (string-match "^[\t ]*\\(.+\\)$" rest)
1243             (elmo-match-string 1 rest)
1244           rest))))))
1245
1246 (defun wl-folder-create-folder-entity ()
1247   "Create folder entries."
1248   (let ((tmp-buf (get-buffer-create " *wl-folder-tmp*"))
1249         entity ret-val)
1250     (condition-case ()
1251         (progn
1252           (with-current-buffer tmp-buf
1253             (erase-buffer)
1254             (insert-file-contents wl-folders-file)
1255             (goto-char (point-min))
1256             (while (and (not (eobp))
1257                         (setq entity (wl-create-folder-entity-from-buffer)))
1258               (unless (eq entity 'ignore)
1259                 (wl-append ret-val (list entity)))))
1260           (kill-buffer tmp-buf))
1261       (file-error nil))
1262     (setq ret-val (list wl-folder-desktop-name 'group ret-val))))
1263
1264 (defun wl-folder-entity-assign-id (entity &optional hashtb on-noid)
1265   (let ((hashtb (or hashtb
1266                     (setq wl-folder-entity-id-name-hashtb
1267                           (elmo-make-hash wl-folder-entity-id))))
1268         (entities (list entity))
1269         entity-stack)
1270     (while entities
1271       (setq entity (wl-pop entities))
1272       (cond
1273        ((consp entity)
1274         (when (not (and on-noid
1275                         (get-text-property 0
1276                                            'wl-folder-entity-id
1277                                            (car entity))))
1278           (put-text-property 0 (length (car entity))
1279                              'wl-folder-entity-id
1280                              wl-folder-entity-id
1281                              (car entity))
1282           (wl-folder-set-id-name wl-folder-entity-id
1283                                  (car entity) hashtb))
1284         (and entities
1285              (wl-push entities entity-stack))
1286         (setq entities (nth 2 entity)))
1287        ((stringp entity)
1288         (when (not (and on-noid
1289                         (get-text-property 0
1290                                            'wl-folder-entity-id
1291                                            entity)))
1292           (put-text-property 0 (length entity)
1293                              'wl-folder-entity-id
1294                              wl-folder-entity-id
1295                              entity)
1296           (wl-folder-set-id-name wl-folder-entity-id
1297                                  entity hashtb))))
1298       (setq wl-folder-entity-id (+ 1 wl-folder-entity-id))
1299       (unless entities
1300         (setq entities (wl-pop entity-stack))))))
1301
1302 (defun wl-folder-click (e)
1303   (interactive "e")
1304   (mouse-set-point e)
1305   (beginning-of-line)
1306   (save-excursion
1307     (wl-folder-jump-to-current-entity)))
1308
1309 (defun wl-folder-select-buffer (buffer)
1310   (let ((gbw (get-buffer-window buffer))
1311         exists)
1312     (if gbw
1313         (progn (select-window gbw)
1314                (setq exists t))
1315       (unless wl-summary-use-frame
1316         (condition-case ()
1317             (unwind-protect
1318                 (split-window-horizontally wl-folder-window-width)
1319               (other-window 1))
1320           (error nil))))
1321     (set-buffer buffer)
1322     (if wl-summary-use-frame
1323         (switch-to-buffer-other-frame buffer)
1324       (switch-to-buffer buffer))
1325     exists))
1326
1327 (defun wl-folder-toggle-disp-summary (&optional arg folder)
1328   (interactive)
1329   (if (or (and folder (assoc folder wl-folder-group-alist))
1330           (and (interactive-p) (wl-folder-buffer-group-p)))
1331       (error "This command is not available on Group"))
1332   (beginning-of-line)
1333   (let (wl-auto-select-first
1334         (wl-stay-folder-window t))
1335     (cond
1336      ((eq arg 'on)
1337       (setq wl-folder-buffer-disp-summary t))
1338      ((eq arg 'off)
1339       (setq wl-folder-buffer-disp-summary nil)
1340       ;; hide wl-summary window.
1341       (let ((cur-buf (current-buffer))
1342             (summary-buffer (wl-summary-get-buffer folder)))
1343         (wl-folder-select-buffer summary-buffer)
1344         (delete-window)
1345         (select-window (get-buffer-window cur-buf))))
1346      (t
1347       (setq wl-folder-buffer-disp-summary
1348             (not wl-folder-buffer-disp-summary))
1349       (let ((cur-buf (current-buffer))
1350             folder-name)
1351         (when (looking-at "^[ ]*\\([^\\[].+\\):.*\n")
1352           (setq folder-name (wl-folder-get-entity-from-buffer))
1353           (if wl-folder-buffer-disp-summary
1354               (progn
1355                 (wl-folder-select-buffer
1356                  (wl-summary-get-buffer-create folder-name))
1357                 (unwind-protect
1358                     (wl-summary-goto-folder-subr folder-name 'no-sync nil)
1359                   (select-window (get-buffer-window cur-buf))))
1360             (wl-folder-select-buffer (wl-summary-get-buffer folder-name))
1361             (delete-window)
1362             (select-window (get-buffer-window cur-buf)))))))))
1363
1364 (defun wl-folder-prev-unsync ()
1365   "Move cursor to the previous unsync folder."
1366   (interactive)
1367   (let (start-point)
1368     (setq start-point (point))
1369     (beginning-of-line)
1370     (if (re-search-backward wl-folder-unsync-regexp nil t)
1371         (beginning-of-line)
1372       (goto-char start-point)
1373       (message "No more unsync folder"))))
1374
1375 (defun wl-folder-next-unsync (&optional plugged)
1376   "Move cursor to the next unsync."
1377   (interactive)
1378   (let (start-point entity)
1379     (setq start-point (point))
1380     (end-of-line)
1381     (if (catch 'found
1382           (while (re-search-forward wl-folder-unsync-regexp nil t)
1383             (if (or (wl-folder-buffer-group-p)
1384                     (not plugged)
1385                     (setq entity
1386                           (wl-folder-get-realname
1387                            (wl-folder-folder-name)))
1388                     (elmo-folder-plugged-p entity))
1389                 (throw 'found t))))
1390         (beginning-of-line)
1391       (goto-char start-point)
1392       (message "No more unsync folder"))))
1393
1394 (defun wl-folder-prev-unread (&optional group)
1395   "Move cursor to the previous unread folder."
1396   (interactive "P")
1397   (let (start-point)
1398     (setq start-point (point))
1399     (beginning-of-line)
1400     (if (re-search-backward (wl-folder-unread-regex group) nil t)
1401         (progn
1402           (beginning-of-line)
1403           (wl-folder-folder-name))
1404       (goto-char start-point)
1405       (message "No more unread folder")
1406       nil)))
1407
1408 (defun wl-folder-next-unread (&optional group)
1409   "Move cursor to the next unread folder."
1410   (interactive "P")
1411   (let (start-point)
1412     (setq start-point (point))
1413     (end-of-line)
1414     (if (re-search-forward (wl-folder-unread-regex group) nil t)
1415         (progn
1416           (beginning-of-line)
1417           (wl-folder-folder-name))
1418       (goto-char start-point)
1419       (message "No more unread folder")
1420       nil)))
1421
1422 (defun wl-folder-mode ()
1423   "Major mode for Wanderlust Folder.
1424 See Info under Wanderlust for full documentation.
1425
1426 Special commands:
1427 \\{wl-folder-mode-map}
1428
1429 Entering Folder mode calls the value of `wl-folder-mode-hook'."
1430   (interactive)
1431   (setq major-mode 'wl-folder-mode)
1432   (setq mode-name "Folder")
1433   (use-local-map wl-folder-mode-map)
1434   (setq buffer-read-only t)
1435   (setq inhibit-read-only nil)
1436   (setq truncate-lines t)
1437   (setq wl-folder-buffer-cur-entity-id nil
1438         wl-folder-buffer-cur-path nil
1439         wl-folder-buffer-cur-point nil)
1440   (wl-mode-line-buffer-identification)
1441   (easy-menu-add wl-folder-mode-menu)
1442   ;; This hook may contain the functions `wl-folder-init-icons' and
1443   ;; `wl-setup-folder' for reasons of system internal to accord
1444   ;; facilities for the Emacs variants.
1445   (run-hooks 'wl-folder-mode-hook))
1446
1447 (defun wl-folder-append-petname (realname petname)
1448   (let (pentry)
1449     ;; check group name.
1450     (if (wl-folder-search-group-entity-by-name petname wl-folder-entity)
1451         (error "%s already defined as group name" petname))
1452     (when (setq pentry (wl-string-assoc realname wl-folder-petname-alist))
1453       (setq wl-folder-petname-alist
1454             (delete pentry wl-folder-petname-alist)))
1455     (wl-append wl-folder-petname-alist
1456                (list (cons realname petname)))))
1457
1458 (defun wl-folder (&optional arg)
1459   (interactive "P")
1460   (let (initialize folder-buf)
1461     (if (setq folder-buf (get-buffer wl-folder-buffer-name))
1462         (if wl-folder-use-frame
1463             (let (select-frame)
1464               (save-selected-window
1465                 (dolist (frame (visible-frame-list))
1466                   (select-frame frame)
1467                   (if (get-buffer-window folder-buf)
1468                       (setq select-frame frame))))
1469               (if select-frame
1470                   (select-frame select-frame)
1471                 (switch-to-buffer folder-buf)))
1472           (switch-to-buffer folder-buf))
1473       (if wl-folder-use-frame
1474           (switch-to-buffer-other-frame
1475            (get-buffer-create wl-folder-buffer-name))
1476         (switch-to-buffer (get-buffer-create wl-folder-buffer-name)))
1477       (set-buffer wl-folder-buffer-name)
1478       (wl-folder-mode)
1479       ;; Initialization.
1480       (setq wl-folder-entity-id 0)
1481       (wl-folder-entity-assign-id wl-folder-entity)
1482       (setq wl-folder-entity-hashtb
1483             (wl-folder-create-entity-hashtb wl-folder-entity))
1484       (setq wl-folder-elmo-folder-hashtb (elmo-make-hash wl-folder-entity-id))
1485       (setq wl-folder-group-alist
1486             (wl-folder-create-group-alist wl-folder-entity))
1487       (setq wl-folder-newsgroups-hashtb
1488             (wl-folder-create-newsgroups-hashtb wl-folder-entity))
1489       (wl-folder-init-info-hashtb)
1490       (let ((inhibit-read-only t)
1491             (buffer-read-only nil))
1492         (erase-buffer)
1493         (setcdr (assoc (car wl-folder-entity) wl-folder-group-alist) t)
1494         (save-excursion
1495           (wl-folder-insert-entity " " wl-folder-entity)))
1496       (sit-for 0)
1497       (set-buffer-modified-p nil)
1498       (setq initialize t))
1499     initialize))
1500
1501 (defun wl-folder-auto-check ()
1502   "Check and update folders in `wl-auto-check-folder-name'."
1503   (interactive)
1504   (when (get-buffer wl-folder-buffer-name)
1505     (switch-to-buffer  wl-folder-buffer-name)
1506     (cond
1507      ((eq wl-auto-check-folder-name 'none))
1508      ((or (consp wl-auto-check-folder-name)
1509           (stringp wl-auto-check-folder-name))
1510       (let ((folder-list (if (consp wl-auto-check-folder-name)
1511                              wl-auto-check-folder-name
1512                            (list wl-auto-check-folder-name)))
1513             entity)
1514         (while folder-list
1515           (if (setq entity (wl-folder-search-entity-by-name
1516                             (car folder-list)
1517                             wl-folder-entity))
1518               (wl-folder-check-entity entity 'auto))
1519           (setq folder-list (cdr folder-list)))))
1520      (t
1521       (wl-folder-check-entity wl-folder-entity 'auto)))))
1522
1523 (defun wl-folder-set-folder-updated (name value)
1524   (save-excursion
1525     (let (buf)
1526       (if (setq buf (get-buffer wl-folder-buffer-name))
1527           (wl-folder-entity-hashtb-set
1528            wl-folder-entity-hashtb name value buf))
1529       (setq wl-folder-info-alist-modified t))))
1530
1531 (defun wl-folder-calc-finfo (entity)
1532   ;; calcurate finfo without inserting.
1533   (let ((entities (list entity))
1534         entity-stack
1535         new unread all nums)
1536     (while entities
1537       (setq entity (wl-pop entities))
1538       (cond
1539        ((consp entity)
1540         (and entities
1541              (wl-push entities entity-stack))
1542         (setq entities (nth 2 entity)))
1543        ((stringp entity)
1544         (setq nums (wl-folder-get-entity-info entity))
1545         (setq new    (+ (or new 0) (or (nth 0 nums) 0)))
1546         (setq unread (+ (or unread 0)
1547                         (or (and (nth 0 nums)(nth 1 nums)
1548                                  (+ (nth 0 nums)(nth 1 nums))) 0)))
1549         (setq all    (+ (or all 0) (or (nth 2 nums) 0)))))
1550       (unless entities
1551         (setq entities (wl-pop entity-stack))))
1552     (list new unread all)))
1553
1554 (defsubst wl-folder-make-save-access-list (list)
1555   (mapcar '(lambda (x)
1556              (cond
1557               ((consp x)
1558                (list (elmo-string (car x)) 'access))
1559               (t
1560                (elmo-string x))))
1561           list))
1562
1563 (defun wl-folder-update-newest (indent entity)
1564   (let (ret-val new unread all)
1565     (cond
1566      ((consp entity)
1567       (let ((inhibit-read-only t)
1568             (buffer-read-only nil)
1569             (flist (nth 2 entity))
1570             (as-opened (cdr (assoc (car entity) wl-folder-group-alist)))
1571             beg
1572             )
1573         (setq beg (point))
1574         (if as-opened
1575             (let (update-flist flist-unsub new-flist removed group-name-end)
1576               (when (and (eq (cadr entity) 'access)
1577                          (elmo-folder-plugged-p
1578                           (wl-folder-get-elmo-folder (car entity))))
1579                 (message "Fetching folder entries...")
1580                 (when (setq new-flist
1581                             (elmo-folder-list-subfolders
1582                              (wl-folder-get-elmo-folder (car entity))
1583                              (wl-string-member
1584                               (car entity)
1585                               wl-folder-hierarchy-access-folders)))
1586                   (setq update-flist
1587                         (wl-folder-update-access-group entity new-flist))
1588                   (setq flist (nth 1 update-flist))
1589                   (when (car update-flist) ;; diff
1590                     (setq flist-unsub (nth 2 update-flist))
1591                     (setq removed (nth 3 update-flist))
1592                     (elmo-msgdb-flist-save
1593                      (car entity)
1594                      (list
1595                       (wl-folder-make-save-access-list flist)
1596                       (wl-folder-make-save-access-list flist-unsub)))
1597                     (wl-folder-entity-assign-id
1598                      entity
1599                      wl-folder-entity-id-name-hashtb
1600                      t)
1601                     (setq wl-folder-entity-hashtb
1602                           (wl-folder-create-entity-hashtb
1603                            entity
1604                            wl-folder-entity-hashtb
1605                            t))
1606                     (setq wl-folder-newsgroups-hashtb
1607                           (or
1608                            (wl-folder-create-newsgroups-hashtb
1609                             entity nil)
1610                            wl-folder-newsgroups-hashtb))))
1611                 (message "Fetching folder entries...done"))
1612               (wl-folder-insert-entity indent entity))))))))
1613
1614 (defun wl-folder-insert-entity (indent entity &optional onlygroup)
1615   (let (ret-val new unread all)
1616     (cond
1617      ((consp entity)
1618       (let ((inhibit-read-only t)
1619             (buffer-read-only nil)
1620             (flist (nth 2 entity))
1621             (as-opened (cdr (assoc (car entity) wl-folder-group-alist)))
1622             beg
1623             )
1624 ;;;     (insert indent "[" (if as-opened "-" "+") "]" (car entity) "\n")
1625 ;;;     (save-excursion (forward-line -1)
1626 ;;;                     (wl-highlight-folder-current-line))
1627         (setq beg (point))
1628         (if (and as-opened
1629                  (not onlygroup))
1630             (let (update-flist flist-unsub new-flist removed group-name-end)
1631 ;;;           (when (and (eq (cadr entity) 'access)
1632 ;;;                      newest)
1633 ;;;             (message "fetching folder entries...")
1634 ;;;             (when (setq new-flist
1635 ;;;                         (elmo-list-folders
1636 ;;;                          (elmo-string (car entity))
1637 ;;;                          (wl-string-member
1638 ;;;                           (car entity)
1639 ;;;                           wl-folder-hierarchy-access-folders)
1640 ;;;                          ))
1641 ;;;               (setq update-flist
1642 ;;;                     (wl-folder-update-access-group entity new-flist))
1643 ;;;               (setq flist (nth 1 update-flist))
1644 ;;;               (when (car update-flist) ;; diff
1645 ;;;                 (setq flist-unsub (nth 2 update-flist))
1646 ;;;                 (setq removed (nth 3 update-flist))
1647 ;;;                 (elmo-msgdb-flist-save
1648 ;;;                  (car entity)
1649 ;;;                  (list
1650 ;;;                   (wl-folder-make-save-access-list flist)
1651 ;;;                   (wl-folder-make-save-access-list flist-unsub)))
1652 ;;;                 ;;
1653 ;;;                 ;; reconstruct wl-folder-entity-id-name-hashtb and
1654 ;;;                 ;;           wl-folder-entity-hashtb
1655 ;;;                 ;;
1656 ;;;                 (wl-folder-entity-assign-id
1657 ;;;                  entity
1658 ;;;                  wl-folder-entity-id-name-hashtb
1659 ;;;                  t)
1660 ;;;                 (setq wl-folder-entity-hashtb
1661 ;;;                       (wl-folder-create-entity-hashtb
1662 ;;;                        entity
1663 ;;;                        wl-folder-entity-hashtb
1664 ;;;                        t))
1665 ;;;                 (setq wl-folder-newsgroups-hashtb
1666 ;;;                       (or
1667 ;;;                        (wl-folder-create-newsgroups-hashtb
1668 ;;;                         entity nil)
1669 ;;;                        wl-folder-newsgroups-hashtb))))
1670 ;;;             (message "fetching folder entries...done"))
1671               (insert indent "[" (if as-opened "-" "+") "]"
1672                       (wl-folder-get-petname (car entity)))
1673               (setq group-name-end (point))
1674               (insert ":0/0/0\n")
1675               (put-text-property beg (point) 'wl-folder-entity-id
1676                                  (get-text-property 0 'wl-folder-entity-id
1677                                                     (car entity)))
1678               (when removed
1679                 (setq beg (point))
1680                 (while removed
1681                   (insert indent "  "
1682                           wl-folder-removed-mark
1683                           (if (listp (car removed))
1684                               (concat "[+]" (caar removed))
1685                             (car removed))
1686                           "\n")
1687                   (save-excursion (forward-line -1)
1688                                   (wl-highlight-folder-current-line))
1689                   (setq removed (cdr removed)))
1690                 (remove-text-properties beg (point) '(wl-folder-entity-id)))
1691               (let* ((len (length flist))
1692                      (mes (> len 100))
1693                      (i 0))
1694                 (while flist
1695                   (setq ret-val
1696                         (wl-folder-insert-entity
1697                          (concat indent "  ") (car flist)))
1698                   (setq new    (+ (or new 0) (or (nth 0 ret-val) 0)))
1699                   (setq unread (+ (or unread 0) (or (nth 1 ret-val) 0)))
1700                   (setq all    (+ (or all 0) (or (nth 2 ret-val) 0)))
1701                   (when (and mes
1702                              (> len elmo-display-progress-threshold))
1703                     (setq i (1+ i))
1704                     (elmo-display-progress
1705                      'wl-folder-insert-entity "Inserting group %s..."
1706                      (/ (* i 100) len) (car entity)))
1707                   (setq flist (cdr flist))))
1708               (save-excursion
1709                 (goto-char group-name-end)
1710                 (delete-region (point) (save-excursion (end-of-line)
1711                                                        (point)))
1712                 (insert (format ":%d/%d/%d" (or new 0)
1713                                 (or unread 0) (or all 0)))
1714                 (setq ret-val (list new unread all))
1715                 (wl-highlight-folder-current-line ret-val)))
1716           (setq ret-val (wl-folder-calc-finfo entity))
1717           (insert indent "[" (if as-opened "-" "+") "]"
1718                   (wl-folder-get-petname (car entity))
1719                   (format ":%d/%d/%d"
1720                           (or (nth 0 ret-val) 0)
1721                           (or (nth 1 ret-val) 0)
1722                           (or (nth 2 ret-val) 0))
1723                   "\n")
1724           (put-text-property beg (point) 'wl-folder-entity-id
1725                              (get-text-property 0 'wl-folder-entity-id
1726                                                 (car entity)))
1727           (save-excursion (forward-line -1)
1728                           (wl-highlight-folder-current-line ret-val)))))
1729      ((stringp entity)
1730       (let* ((inhibit-read-only t)
1731              (buffer-read-only nil)
1732              (nums (wl-folder-get-entity-info entity))
1733              beg)
1734         (setq beg (point))
1735         (insert indent (wl-folder-get-petname entity)
1736                 (format ":%s/%s/%s\n"
1737                         (or (setq new (nth 0 nums)) "*")
1738                         (or (setq unread (and (nth 0 nums)(nth 1 nums)
1739                                               (+ (nth 0 nums)(nth 1 nums))))
1740                             "*")
1741                         (or (setq all (nth 2 nums)) "*")))
1742         (put-text-property beg (point) 'wl-folder-entity-id
1743                            (get-text-property 0 'wl-folder-entity-id entity))
1744         (save-excursion (forward-line -1)
1745                         (wl-highlight-folder-current-line nums))
1746         (setq ret-val (list new unread all)))))
1747     (set-buffer-modified-p nil)
1748     ret-val))
1749
1750 (defun wl-folder-check-all ()
1751   (interactive)
1752   (wl-folder-check-entity wl-folder-entity))
1753
1754 (defun wl-folder-entity-hashtb-set (entity-hashtb name value buffer)
1755   (let (cur-val
1756         (new-diff 0)
1757         (unread-diff 0)
1758         (all-diff 0)
1759         diffs
1760         entity-list)
1761     (setq cur-val (wl-folder-get-entity-info name entity-hashtb))
1762     (setq new-diff    (- (or (nth 0 value) 0) (or (nth 0 cur-val) 0)))
1763     (setq unread-diff
1764           (+ new-diff
1765              (- (or (nth 1 value) 0) (or (nth 1 cur-val) 0))))
1766     (setq all-diff    (- (or (nth 2 value) 0) (or (nth 2 cur-val) 0)))
1767     (setq diffs (list new-diff unread-diff all-diff))
1768     (unless (and (nth 0 cur-val)
1769                  (equal diffs '(0 0 0)))
1770       (wl-folder-set-entity-info name value entity-hashtb)
1771       (save-match-data
1772         (with-current-buffer buffer
1773           (save-excursion
1774             (setq entity-list (wl-folder-search-entity-list-by-name
1775                                name wl-folder-entity))
1776             (while entity-list
1777               (wl-folder-update-group (car entity-list) diffs)
1778               (setq entity-list (cdr entity-list)))
1779             (goto-char (point-min))
1780             (while (wl-folder-buffer-search-entity name)
1781               (wl-folder-update-line value))))))))
1782
1783 (defun wl-folder-update-unread (folder unread)
1784 ;  (save-window-excursion
1785     (let ((buf (get-buffer wl-folder-buffer-name))
1786           cur-unread
1787           (unread-diff 0)
1788           ;;(fld (elmo-string folder))
1789           value newvalue entity-list)
1790 ;;; Update folder-info
1791 ;;;    (elmo-folder-set-info-hashtb fld nil nil nil unread)
1792       (setq cur-unread (or (nth 1 (wl-folder-get-entity-info folder)) 0))
1793       (setq unread-diff (- (or unread 0) cur-unread))
1794       (setq value (wl-folder-get-entity-info folder))
1795       (setq newvalue (list (nth 0 value)
1796                            unread
1797                            (nth 2 value)))
1798       (wl-folder-set-entity-info folder newvalue)
1799       (setq wl-folder-info-alist-modified t)
1800       (when (and buf
1801                  (not (eq unread-diff 0)))
1802         (save-match-data
1803           (with-current-buffer buf
1804             (save-excursion
1805               (setq entity-list (wl-folder-search-entity-list-by-name
1806                                  folder wl-folder-entity))
1807               (while entity-list
1808                 (wl-folder-update-group (car entity-list) (list 0
1809                                                                 unread-diff
1810                                                                 0))
1811                 (setq entity-list (cdr entity-list)))
1812               (goto-char (point-min))
1813               (while (wl-folder-buffer-search-entity folder)
1814                 (wl-folder-update-line newvalue))))))));)
1815
1816 (defun wl-folder-create-entity-hashtb (entity &optional hashtb reconst)
1817   (let ((hashtb (or hashtb (elmo-make-hash wl-folder-entity-id)))
1818         (entities (list entity))
1819         entity-stack)
1820     (while entities
1821       (setq entity (wl-pop entities))
1822       (cond
1823        ((consp entity)
1824         (and entities
1825              (wl-push entities entity-stack))
1826         (setq entities (nth 2 entity)))
1827        ((stringp entity)
1828         (when (not (and reconst
1829                         (wl-folder-get-entity-info entity)))
1830           (wl-folder-set-entity-info entity
1831                                      nil
1832                                      hashtb))))
1833       (unless entities
1834         (setq entities (wl-pop entity-stack))))
1835     hashtb))
1836
1837 ;; Unsync number is reserved.
1838 ;;(defun wl-folder-reconstruct-entity-hashtb (entity &optional hashtb id-name)
1839 ;;  (let* ((hashtb (or hashtb (elmo-make-hash wl-folder-entity-id)))
1840 ;;       (entities (list entity))
1841 ;;       entity-stack)
1842 ;;    (while entities
1843 ;;      (setq entity (wl-pop entities))
1844 ;;      (cond
1845 ;;       ((consp entity)
1846 ;;      (if id-name
1847 ;;          (wl-folder-set-id-name (wl-folder-get-entity-id (car entity))
1848 ;;                                 (car entity)))
1849 ;;      (and entities
1850 ;;           (wl-push entities entity-stack))
1851 ;;      (setq entities (nth 2 entity))
1852 ;;      )
1853 ;;       ((stringp entity)
1854 ;;      (wl-folder-set-entity-info entity
1855 ;;                                 (wl-folder-get-entity-info entity)
1856 ;;                                 hashtb)
1857 ;;      (if id-name
1858 ;;          (wl-folder-set-id-name (wl-folder-get-entity-id entity)
1859 ;;                                 entity))))
1860 ;;      (unless entities
1861 ;;      (setq entities (wl-pop entity-stack))))
1862 ;;    hashtb))
1863
1864 (defun wl-folder-create-newsgroups-from-nntp-access (entity)
1865   (let ((flist (nth 2 entity))
1866         folders)
1867     (while flist
1868       (wl-append folders
1869                  (cond
1870                   ((consp (car flist))
1871                    (wl-folder-create-newsgroups-from-nntp-access (car flist)))
1872                   (t
1873                    (list
1874                     (elmo-nntp-folder-group-internal
1875                      (wl-folder-get-elmo-folder (car flist)))))))
1876       (setq flist (cdr flist)))
1877     folders))
1878
1879 (defun wl-folder-create-newsgroups-hashtb (entity &optional is-list info)
1880   "Create NNTP group hashtable for ENTITY."
1881   (let ((entities (if is-list entity (list entity)))
1882         entity-stack folder-list newsgroups newsgroup make-hashtb)
1883     (and info (message "Creating newsgroups..."))
1884     (while entities
1885       (setq entity (wl-pop entities))
1886       (cond
1887        ((consp entity)
1888         (if (eq (nth 1 entity) 'access)
1889             (when (eq (elmo-folder-type-internal
1890                        (elmo-make-folder (car entity))) 'nntp)
1891               (wl-append newsgroups
1892                          (wl-folder-create-newsgroups-from-nntp-access entity))
1893               (setq make-hashtb t))
1894           (and entities
1895                (wl-push entities entity-stack))
1896           (setq entities (nth 2 entity))))
1897        ((stringp entity)
1898         (setq folder-list (elmo-folder-get-primitive-list
1899                            (elmo-make-folder entity)))
1900         (while folder-list
1901           (when (and (eq (elmo-folder-type-internal (car folder-list))
1902                          'nntp)
1903                      (setq newsgroup (elmo-nntp-folder-group-internal
1904                                       (car folder-list))))
1905             (wl-append newsgroups (list (elmo-string newsgroup))))
1906           (setq folder-list (cdr folder-list)))))
1907       (unless entities
1908         (setq entities (wl-pop entity-stack))))
1909     (and info (message "Creating newsgroups...done"))
1910     (if (or newsgroups make-hashtb)
1911         (elmo-setup-subscribed-newsgroups newsgroups))))
1912
1913 (defun wl-folder-get-path (entity target-id &optional string)
1914   (let ((entities (list entity))
1915         entity-stack result-path)
1916     (reverse
1917      (catch 'done
1918        (while entities
1919          (setq entity (wl-pop entities))
1920          (cond
1921           ((consp entity)
1922            (if (and (or (not string) (string= string (car entity)))
1923                     ;; don't use eq, `id' is string on Nemacs.
1924                     (equal target-id (wl-folder-get-entity-id (car entity))))
1925                (throw 'done
1926                       (wl-push target-id result-path))
1927              (wl-push (wl-folder-get-entity-id (car entity)) result-path))
1928            (wl-push entities entity-stack)
1929            (setq entities (nth 2 entity)))
1930           ((stringp entity)
1931            (if (and (or (not string) (string= string entity))
1932                     ;; don't use eq, `id' is string on Nemacs.
1933                     (equal target-id (wl-folder-get-entity-id entity)))
1934                (throw 'done
1935                       (wl-push target-id result-path)))))
1936          (unless entities
1937            (while (and entity-stack
1938                        (not entities))
1939              (setq result-path (cdr result-path))
1940              (setq entities (wl-pop entity-stack)))))))))
1941
1942 (defun wl-folder-create-group-alist (entity)
1943   (if (consp entity)
1944       (let ((flist (nth 2 entity))
1945             (cur-alist (list (cons (car entity) nil)))
1946              append-alist)
1947         (while flist
1948           (if (consp (car flist))
1949               (wl-append append-alist
1950                          (wl-folder-create-group-alist (car flist))))
1951           (setq flist (cdr flist)))
1952         (append cur-alist append-alist))))
1953
1954 (defun wl-folder-init-info-hashtb ()
1955   (let ((info-alist (and wl-folder-info-save
1956                          (elmo-msgdb-finfo-load))))
1957     (elmo-folder-info-make-hashtb
1958      info-alist
1959      wl-folder-entity-hashtb)))
1960 ;;; (wl-folder-resume-entity-hashtb-by-finfo
1961 ;;;  wl-folder-entity-hashtb
1962 ;;;  info-alist)))
1963
1964 (defun wl-folder-cleanup-variables ()
1965   (setq wl-folder-entity nil
1966         wl-folder-entity-hashtb nil
1967         wl-folder-entity-id-name-hashtb nil
1968         wl-folder-group-alist nil
1969         wl-folder-petname-alist nil
1970         wl-folder-newsgroups-hashtb nil
1971         wl-fldmgr-cut-entity-list nil
1972         wl-fldmgr-modified nil
1973         wl-fldmgr-modified-access-list nil
1974         wl-score-cache nil
1975         ))
1976
1977 (defun wl-make-plugged-alist ()
1978   (let ((entity-list (wl-folder-get-entity-list wl-folder-entity))
1979         (add (not wl-reset-plugged-alist)))
1980     (while entity-list
1981       (elmo-folder-set-plugged
1982        (wl-folder-get-elmo-folder (car entity-list)) wl-plugged add)
1983       (setq entity-list (cdr entity-list)))
1984     ;; smtp posting server
1985     (when wl-smtp-posting-server
1986       (elmo-set-plugged wl-plugged
1987                         wl-smtp-posting-server  ; server
1988                         (or (and (boundp 'smtp-service) smtp-service)
1989                             "smtp")     ; port
1990                         wl-smtp-connection-type
1991                         nil nil "smtp" add))
1992     ;; nntp posting server
1993     (when wl-nntp-posting-server
1994       (elmo-set-plugged wl-plugged
1995                         wl-nntp-posting-server
1996                         wl-nntp-posting-stream-type
1997                         wl-nntp-posting-port
1998                         nil nil "nntp" add))
1999     (run-hooks 'wl-make-plugged-hook)))
2000
2001 (defvar wl-folder-init-function 'wl-local-folder-init)
2002
2003 (defun wl-folder-init ()
2004   "Return top-level folder entity."
2005   (interactive)
2006   (if wl-use-acap
2007       (wl-acap-init)
2008     (funcall wl-folder-init-function))
2009   (run-hooks 'wl-folder-init-hook))
2010
2011 (defun wl-local-folder-init ()
2012   "Initialize local folder."
2013   (message "Initializing folder...")
2014   (setq wl-folder-entity (wl-folder-create-folder-entity))
2015   (message "Initializing folder...done"))
2016
2017 (defun wl-folder-get-realname (petname)
2018   (or (car
2019        (wl-string-rassoc
2020         petname
2021         wl-folder-petname-alist))
2022       petname))
2023
2024 (defun wl-folder-get-petname (name)
2025   (or (cdr
2026        (wl-string-assoc
2027         name
2028         wl-folder-petname-alist))
2029       name))
2030
2031 (defun wl-folder-get-entity-with-petname ()
2032   (let ((alist wl-folder-petname-alist)
2033         (hashtb (copy-sequence wl-folder-entity-hashtb)))
2034     (while alist
2035       (wl-folder-set-entity-info (cdar alist) nil hashtb)
2036       (setq alist (cdr alist)))
2037     hashtb))
2038
2039 (defun wl-folder-get-newsgroups (folder)
2040   "Return Newsgroups field value string for FOLDER newsgroup.
2041 If FOLDER is multi, return comma separated string (cross post)."
2042   (let ((nlist (elmo-folder-newsgroups
2043                         (wl-folder-get-elmo-folder folder))))
2044     (if nlist
2045         (list nil nil (mapconcat 'identity nlist ","))
2046       nil)))
2047
2048 (defun wl-folder-guess-mailing-list-by-refile-rule (entity)
2049   "Return ML address guess by FOLDER.
2050 Use `wl-subscribed-mailing-list' and `wl-refile-rule-alist'."
2051   (let ((flist
2052          (elmo-folder-get-primitive-list
2053           (wl-folder-get-elmo-folder entity)))
2054         fld mladdr to)
2055     (while (setq fld (car flist))
2056       (setq mladdr (wl-folder-guess-mailing-list-by-refile-rule-subr
2057                     (elmo-folder-name-internal fld)))
2058       (when mladdr
2059         (setq to (if (stringp to)
2060                      (concat to ", " mladdr)
2061                    mladdr)))
2062       (setq flist (cdr flist)))
2063     (if (stringp to)
2064         (list to nil nil)
2065       nil)))
2066
2067 (defun wl-folder-guess-mailing-list-by-refile-rule-subr (entity)
2068   (unless (memq (elmo-folder-type entity)
2069                 '(localnews nntp))
2070     (let ((rules wl-refile-rule-alist)
2071           tokey toalist)
2072       (while rules
2073         (if (or (and (stringp (car (car rules)))
2074                      (string-match "[Tt]o" (car (car rules))))
2075                 (and (listp (car (car rules)))
2076                      (elmo-string-matched-member "to" (car (car rules))
2077                                                  'case-ignore)))
2078             (setq toalist (append toalist (cdr (car rules)))))
2079         (setq rules (cdr rules)))
2080       (setq tokey (car (rassoc entity toalist)))
2081 ;;;     (setq histkey (car (rassoc folder wl-refile-alist)))
2082       ;; case-ignore search `wl-subscribed-mailing-list'
2083       (if (stringp tokey)
2084           (elmo-string-matched-member tokey wl-subscribed-mailing-list t)))))
2085
2086 (defun wl-folder-guess-mailing-list-by-folder-name (entity)
2087   "Return ML address guess by FOLDER name's last hierarchy.
2088 Use `wl-subscribed-mailing-list'."
2089   (let ((flist
2090          (elmo-folder-get-primitive-list
2091           (wl-folder-get-elmo-folder entity)))
2092         fld mladdr to)
2093     (while (setq fld (car flist))
2094       (setq mladdr (wl-folder-guess-mailing-list-by-folder-name-subr
2095                     (elmo-folder-name-internal fld)))
2096       (when mladdr
2097         (setq to (if (stringp to)
2098                      (concat to ", " mladdr)
2099                    mladdr)))
2100       (setq flist (cdr flist)))
2101     (if (stringp to)
2102         (list to nil nil)
2103       nil)))
2104
2105 (defun wl-folder-guess-mailing-list-by-folder-name-subr (entity)
2106   (when (memq (elmo-folder-type entity)
2107               '(localdir imap4 maildir))
2108     (let (key foldername)
2109       ;; Get foldername and Remove folder type symbol.
2110       (setq foldername (substring entity 1))
2111       (if (string-match "@" foldername)
2112           (setq foldername (substring foldername 0 (match-beginning 0))))
2113       (when (string-match "[^\\./]+$" foldername)
2114         (setq key (regexp-quote
2115                    (concat (substring foldername (match-beginning 0)) "@")))
2116         (elmo-string-matched-member
2117          key wl-subscribed-mailing-list 'case-ignore)))))
2118
2119 (defun wl-folder-update-diff-line (diffs)
2120   (let ((inhibit-read-only t)
2121         (buffer-read-only nil)
2122         cur-new new-new
2123         cur-unread new-unread
2124         cur-all new-all
2125         id)
2126     (save-excursion
2127       (beginning-of-line)
2128       (setq id (get-text-property (point) 'wl-folder-entity-id))
2129       (when (looking-at "^[ ]*\\(.*\\):\\([0-9\\*-]*\\)/\\([0-9\\*-]*\\)/\\([0-9\\*]*\\)")
2130         ;;(looking-at "^[ ]*\\([^\\[].+\\):\\([0-9\\*-]*/[0-9\\*-]*/[0-9\\*]*\\)")
2131         (setq cur-new (string-to-int
2132                        (wl-match-buffer 2)))
2133         (setq cur-unread (string-to-int
2134                           (wl-match-buffer 3)))
2135         (setq cur-all (string-to-int
2136                        (wl-match-buffer 4)))
2137         (delete-region (match-beginning 2)
2138                        (match-end 4))
2139         (goto-char (match-beginning 2))
2140         (insert (format "%s/%s/%s"
2141                         (setq new-new (+ cur-new (nth 0 diffs)))
2142                         (setq new-unread (+ cur-unread (nth 1 diffs)))
2143                         (setq new-all (+ cur-all (nth 2 diffs)))))
2144         (put-text-property (match-beginning 2) (point)
2145                            'wl-folder-entity-id id)
2146         (if wl-use-highlight-mouse-line
2147             (put-text-property (match-beginning 2) (point)
2148                                'mouse-face 'highlight))
2149         (wl-highlight-folder-group-line (list new-new new-unread new-all))
2150         (setq buffer-read-only t)
2151         (set-buffer-modified-p nil)))))
2152
2153 (defun wl-folder-update-line (nums &optional is-group)
2154   (let ((inhibit-read-only t)
2155         (buffer-read-only nil)
2156         id)
2157     (save-excursion
2158       (beginning-of-line)
2159       (setq id (get-text-property (point) 'wl-folder-entity-id))
2160       (if (looking-at "^[ ]*\\(.*\\):\\([0-9\\*-]*/[0-9\\*-]*/[0-9\\*]*\\)")
2161 ;;;       (looking-at "^[ ]*\\([^\\[].+\\):\\([0-9\\*-]*/[0-9\\*-]*/[0-9\\*]*\\)")
2162           (progn
2163             (delete-region (match-beginning 2)
2164                            (match-end 2))
2165             (goto-char (match-beginning 2))
2166             (insert (format "%s/%s/%s"
2167                             (or (nth 0 nums) "*")
2168                             (or (and (nth 0 nums)(nth 1 nums)
2169                                      (+ (nth 0 nums)(nth 1 nums)))
2170                                 "*")
2171                             (or (nth 2 nums) "*")))
2172             (put-text-property (match-beginning 2) (point)
2173                                'wl-folder-entity-id id)
2174             (if is-group
2175                 ;; update only colors
2176                 (wl-highlight-folder-group-line nums)
2177               (wl-highlight-folder-current-line nums))
2178             (beginning-of-line)
2179             (set-buffer-modified-p nil))))))
2180
2181 (defun wl-folder-goto-folder (&optional arg)
2182   (interactive "P")
2183   (wl-folder-goto-folder-subr nil arg))
2184
2185 (defun wl-folder-goto-folder-subr (&optional folder sticky)
2186   (beginning-of-line)
2187   (let (summary-buf fld-name entity id error-selecting)
2188 ;;; (setq fld-name (wl-folder-get-entity-from-buffer))
2189 ;;; (if (or (null fld-name)
2190 ;;;         (assoc fld-name wl-folder-group-alist))
2191     (setq fld-name wl-default-folder)
2192     (setq fld-name (or folder
2193                        (wl-summary-read-folder fld-name)))
2194     (if (and (setq entity
2195                    (wl-folder-search-entity-by-name fld-name
2196                                                     wl-folder-entity
2197                                                     'folder))
2198              (setq id (wl-folder-get-entity-id entity)))
2199         (wl-folder-set-current-entity-id id))
2200     (setq summary-buf (wl-summary-get-buffer-create fld-name sticky))
2201     (if (or wl-stay-folder-window wl-summary-use-frame)
2202         (wl-folder-select-buffer summary-buf)
2203       (if (and summary-buf
2204                (get-buffer-window summary-buf))
2205           (delete-window)))
2206     (wl-summary-goto-folder-subr fld-name
2207                                  (wl-summary-get-sync-range
2208                                   (wl-folder-get-elmo-folder fld-name))
2209                                  nil sticky t)))
2210
2211 (defun wl-folder-suspend ()
2212   (interactive)
2213   (run-hooks 'wl-folder-suspend-hook)
2214   (wl-folder-info-save)
2215   (elmo-crosspost-message-alist-save)
2216   (elmo-quit)
2217   ;(if (fboundp 'mmelmo-cleanup-entity-buffers)
2218   ;(mmelmo-cleanup-entity-buffers))
2219   (bury-buffer wl-folder-buffer-name)
2220   (delete-windows-on wl-folder-buffer-name t))
2221
2222 (defun wl-folder-info-save ()
2223   (when (and wl-folder-info-save
2224              wl-folder-info-alist-modified)
2225     (let ((entities (list wl-folder-entity))
2226           entity entity-stack info-alist info)
2227       (while entities
2228         (setq entity (wl-pop entities))
2229         (cond
2230          ((consp entity)
2231           (and entities
2232                (wl-push entities entity-stack))
2233           (setq entities (nth 2 entity)))
2234          ((stringp entity)
2235           (when (and (setq info (elmo-folder-get-info
2236                                  (wl-folder-get-elmo-folder entity)))
2237                      (not (equal info '(nil))))
2238             (if (listp info)
2239                 (wl-append info-alist (list (list (elmo-string entity)
2240                                                   (list (nth 3 info)  ;; max
2241                                                         (nth 2 info)  ;; length
2242                                                         (nth 0 info)  ;; new
2243                                                         (nth 1 info)) ;; unread
2244                                                   )))))))
2245         (unless entities
2246           (setq entities (wl-pop entity-stack))))
2247       (elmo-msgdb-finfo-save info-alist)
2248       (setq wl-folder-info-alist-modified nil))))
2249
2250 (defun wl-folder-goto-first-unread-folder (&optional arg)
2251   (interactive "P")
2252   (let ((entities (list wl-folder-entity))
2253         entity entity-stack ret-val
2254         first-entity finfo)
2255     (setq first-entity
2256           (catch 'done
2257             (while entities
2258               (setq entity (wl-pop entities))
2259               (cond
2260                ((consp entity)
2261                 (and entities
2262                      (wl-push entities entity-stack))
2263                 (setq entities (nth 2 entity)))
2264                ((stringp entity)
2265                 (if (and (setq finfo (wl-folder-get-entity-info entity))
2266                          (and (nth 0 finfo)(nth 1 finfo))
2267                          (> (+ (nth 0 finfo)(nth 1 finfo)) 0))
2268                     (throw 'done entity))
2269                 (wl-append ret-val (list entity))))
2270               (unless entities
2271                 (setq entities (wl-pop entity-stack))))))
2272     (if first-entity
2273         (progn
2274           (when arg
2275             (wl-folder-jump-folder first-entity)
2276             (sit-for 0))
2277           (wl-folder-goto-folder-subr first-entity))
2278       (message "No unread folder"))))
2279
2280 (defun wl-folder-jump-folder (&optional fld-name noopen)
2281   (interactive)
2282   (if (not fld-name)
2283       (setq fld-name (wl-summary-read-folder wl-default-folder)))
2284   (goto-char (point-min))
2285   (if (not noopen)
2286       (wl-folder-open-folder fld-name))
2287   (and (wl-folder-buffer-search-entity fld-name)
2288        (beginning-of-line)))
2289
2290 (defun wl-folder-get-entity-list (entity)
2291   (let ((entities (list entity))
2292         entity-stack ret-val)
2293     (while entities
2294       (setq entity (wl-pop entities))
2295       (cond
2296        ((consp entity)
2297         (and entities
2298              (wl-push entities entity-stack))
2299         (setq entities (nth 2 entity)))
2300        ((stringp entity)
2301         (wl-append ret-val (list entity))))
2302       (unless entities
2303         (setq entities (wl-pop entity-stack))))
2304     ret-val))
2305
2306 (defun wl-folder-open-unread-folder (entity)
2307   (save-excursion
2308     (let ((alist (wl-folder-get-entity-list entity))
2309           (unread 0)
2310           finfo path-list path id)
2311       (while alist
2312         (when (and (setq finfo (wl-folder-get-entity-info (car alist)))
2313                    (nth 0 finfo) (nth 1 finfo)
2314                    (> (+ (nth 0 finfo)(nth 1 finfo)) 0))
2315           (setq unread (+ unread (+ (nth 0 finfo)(nth 1 finfo))))
2316           (setq id (wl-folder-get-entity-id (car alist)))
2317           (setq path (delete id (wl-folder-get-path
2318                                  wl-folder-entity
2319                                  id
2320                                  (car alist))))
2321           (if (not (member path path-list))
2322               (wl-append path-list (list path))))
2323         (setq alist (cdr alist)))
2324       (while path-list
2325         (wl-folder-open-folder-sub (car path-list))
2326         (setq path-list (cdr path-list)))
2327       (message "%s unread folder"
2328                (if (> unread 0) unread "No")))))
2329
2330 (defun wl-folder-open-unread-current-entity ()
2331   (interactive)
2332   (let ((entity-name (wl-folder-get-entity-from-buffer))
2333         (group (wl-folder-buffer-group-p)))
2334     (when entity-name
2335       (wl-folder-open-unread-folder
2336        (if group
2337            (wl-folder-search-group-entity-by-name entity-name
2338                                                   wl-folder-entity)
2339          entity-name)))))
2340
2341 (defun wl-folder-open-only-unread-folder ()
2342   (interactive)
2343   (let ((id (progn
2344               (wl-folder-prev-entity-skip-invalid t)
2345               (wl-folder-get-entity-from-buffer t))))
2346     (wl-folder-open-all-unread-folder)
2347     (save-excursion
2348       (goto-char (point-max))
2349       (while (and (re-search-backward
2350                    "^[ ]*\\[[-]\\].+:0/0/[0-9-]+" nil t)
2351                   (not (bobp)))
2352         (wl-folder-jump-to-current-entity) ;; close it
2353         ))
2354     (wl-folder-move-path id)
2355     (recenter)))
2356
2357 (defun wl-folder-open-all-unread-folder (&optional arg)
2358   (interactive "P")
2359   (let ((id (progn
2360               (wl-folder-prev-entity-skip-invalid t)
2361               (wl-folder-get-entity-from-buffer t))))
2362     (wl-folder-open-unread-folder wl-folder-entity)
2363     (if (not arg)
2364         (wl-folder-move-path id)
2365       (goto-char (point-min))
2366       (wl-folder-next-unread t))))
2367
2368 (defun wl-folder-open-folder (&optional fld-name)
2369   (interactive)
2370   (if (not fld-name)
2371       (setq fld-name (wl-summary-read-folder wl-default-folder)))
2372   (let* ((id (wl-folder-get-entity-id
2373               (wl-folder-search-entity-by-name fld-name wl-folder-entity
2374                                                'folder)))
2375          (path (and id (wl-folder-get-path wl-folder-entity id))))
2376       (if path
2377           (wl-folder-open-folder-sub path))))
2378
2379 (defun wl-folder-open-folder-sub (path)
2380   (let ((inhibit-read-only t)
2381         (buffer-read-only nil)
2382         indent name entity
2383         err)
2384     (save-excursion
2385       (goto-char (point-min))
2386       (while (and path
2387                   (wl-folder-buffer-search-group
2388                    (wl-folder-get-petname
2389                     (if (stringp (car path))
2390                         (car path)
2391                       (wl-folder-get-folder-name-by-id
2392                        (car path))))))
2393         (beginning-of-line)
2394         (setq path (cdr path))
2395         (if (and (looking-at wl-folder-group-regexp)
2396                  (string= "+" (wl-match-buffer 2)));; closed group
2397             (save-excursion
2398               (setq indent (wl-match-buffer 1))
2399               (setq name (wl-folder-get-realname (wl-match-buffer 3)))
2400               (setq entity (wl-folder-search-group-entity-by-name
2401                             name
2402                             wl-folder-entity))
2403               ;; insert as opened
2404               (setcdr (assoc (car entity) wl-folder-group-alist) t)
2405               (if (eq 'access (cadr entity))
2406                   (wl-folder-maybe-load-folder-list entity))
2407               (wl-folder-insert-entity indent entity)
2408               (delete-region (save-excursion (beginning-of-line)
2409                                              (point))
2410                              (save-excursion (end-of-line)
2411                                              (+ 1 (point)))))))
2412       (set-buffer-modified-p nil))))
2413
2414 (defun wl-folder-open-all-pre ()
2415   (let ((entities (list wl-folder-entity))
2416         entity entity-stack group-entry)
2417     (while entities
2418       (setq entity (wl-pop entities))
2419       (cond
2420        ((consp entity)
2421         (unless (or (not (setq group-entry
2422                                (assoc (car entity) wl-folder-group-alist)))
2423                     (cdr group-entry))
2424           (setcdr group-entry t)
2425           (when (eq 'access (cadr entity))
2426             (wl-folder-maybe-load-folder-list entity)))
2427         (and entities
2428              (wl-push entities entity-stack))
2429         (setq entities (nth 2 entity))))
2430       (unless entities
2431         (setq entities (wl-pop entity-stack))))))
2432
2433 (defun wl-folder-open-all (&optional refresh)
2434   (interactive "P")
2435   (let* ((inhibit-read-only t)
2436          (buffer-read-only nil)
2437          (len (length wl-folder-group-alist))
2438          (i 0)
2439          indent name entity)
2440     (if refresh
2441         (let ((id (progn
2442                     (wl-folder-prev-entity-skip-invalid t)
2443                     (wl-folder-get-entity-from-buffer t)))
2444               (alist wl-folder-group-alist))
2445           (while alist
2446             (setcdr (pop alist) t))
2447           (erase-buffer)
2448           (wl-folder-insert-entity " " wl-folder-entity)
2449           (wl-folder-move-path id))
2450       (message "Opening all folders...")
2451       (wl-folder-open-all-pre)
2452       (save-excursion
2453         (goto-char (point-min))
2454         (while (re-search-forward
2455                 "^\\([ ]*\\)\\[\\([+]\\)\\]\\(.+\\):[-0-9-]+/[0-9-]+/[0-9-]+\n"
2456                 nil t)
2457           (setq indent (wl-match-buffer 1))
2458           (setq name (wl-folder-get-realname (wl-match-buffer 3)))
2459           (setq entity (wl-folder-search-group-entity-by-name
2460                         name
2461                         wl-folder-entity))
2462           ;; insert as opened
2463           (setcdr (assoc (car entity) wl-folder-group-alist) t)
2464           (forward-line -1)
2465           (wl-folder-insert-entity indent entity)
2466           (delete-region (save-excursion (beginning-of-line)
2467                                          (point))
2468                          (save-excursion (end-of-line)
2469                                          (+ 1 (point))))
2470           (when (> len elmo-display-progress-threshold)
2471             (setq i (1+ i))
2472             (if (or (zerop (% i 5)) (= i len))
2473                 (elmo-display-progress
2474                  'wl-folder-open-all "Opening all folders..."
2475                  (/ (* i 100) len)))))
2476         (when (> len elmo-display-progress-threshold)
2477           (elmo-display-progress
2478            'wl-folder-open-all "Opening all folders..." 100))))
2479     (message "Opening all folders...done")
2480     (set-buffer-modified-p nil)))
2481
2482 (defun wl-folder-close-all ()
2483   (interactive)
2484   (let ((inhibit-read-only t)
2485         (buffer-read-only nil)
2486         (alist wl-folder-group-alist)
2487         (id (progn
2488               (wl-folder-prev-entity-skip-invalid t)
2489               (wl-folder-get-entity-from-buffer t))))
2490     (while alist
2491       (setcdr (car alist) nil)
2492       (setq alist (cdr alist)))
2493     (setcdr (assoc (car wl-folder-entity) wl-folder-group-alist) t)
2494     (erase-buffer)
2495     (wl-folder-insert-entity " " wl-folder-entity)
2496     (wl-folder-move-path id)
2497     (recenter)
2498     (set-buffer-modified-p nil)))
2499
2500 (defun wl-folder-open-close ()
2501   "Open or close parent entity."
2502   (interactive)
2503   (save-excursion
2504     (beginning-of-line)
2505     (if (wl-folder-buffer-group-p)
2506         ;; if group (whether opend or closed.)
2507         (wl-folder-jump-to-current-entity)
2508       ;; if folder
2509       (let (indent)
2510         (setq indent (save-excursion
2511                        (re-search-forward "\\([ ]*\\)." nil t)
2512                        (wl-match-buffer 1)))
2513         (while (looking-at indent)
2514           (forward-line -1)))
2515       (wl-folder-jump-to-current-entity))))
2516
2517 (defsubst wl-folder-access-subscribe-p (group folder)
2518   (let (subscr regexp match)
2519     (if (setq subscr (wl-get-assoc-list-value
2520                       wl-folder-access-subscribe-alist
2521                       group))
2522         (progn
2523           (setq regexp (mapconcat 'identity (cdr subscr) "\\|"))
2524           (setq match (string-match regexp folder))
2525           (if (car subscr)
2526               match
2527             (not match)))
2528       t)))
2529
2530 (defun wl-folder-update-access-group (entity new-flist)
2531   (let* ((flist (nth 2 entity))
2532          (unsubscribes (nth 3 entity))
2533          (len (+ (length flist) (length unsubscribes)))
2534          (i 0)
2535          diff new-unsubscribes removes
2536          subscribed-list folder group entry)
2537     ;; check subscribed groups
2538     (while flist
2539       (cond
2540        ((listp (car flist))     ;; group
2541         (setq group (elmo-string (caar flist)))
2542         (cond
2543          ((assoc group new-flist)       ;; found in new-flist
2544           (setq new-flist (delete (assoc group new-flist)
2545                                   new-flist))
2546           (if (wl-folder-access-subscribe-p (car entity) group)
2547               (wl-append subscribed-list (list (car flist)))
2548             (wl-append new-unsubscribes (list (car flist)))
2549             (setq diff t)))
2550          (t
2551           (setq wl-folder-group-alist
2552                 (delete (wl-string-assoc group wl-folder-group-alist)
2553                         wl-folder-group-alist))
2554           (wl-append removes (list (list group))))))
2555        (t                       ;; folder
2556         (setq folder (elmo-string (car flist)))
2557         (cond
2558          ((member folder new-flist)     ;; found in new-flist
2559           (setq new-flist (delete folder new-flist))
2560           (if (wl-folder-access-subscribe-p (car entity) folder)
2561               (wl-append subscribed-list (list (car flist)))
2562             (wl-append new-unsubscribes (list folder))
2563             (setq diff t)))
2564          (t
2565           (wl-append removes (list folder))))))
2566       (when (> len elmo-display-progress-threshold)
2567         (setq i (1+ i))
2568         (if (or (zerop (% i 10)) (= i len))
2569             (elmo-display-progress
2570              'wl-folder-update-access-group "Updating access group..."
2571              (/ (* i 100) len))))
2572       (setq flist (cdr flist)))
2573     ;; check unsubscribed groups
2574     (while unsubscribes
2575       (cond
2576        ((listp (car unsubscribes))
2577         (when (setq entry (assoc (caar unsubscribes) new-flist))
2578           (setq new-flist (delete entry new-flist))
2579           (wl-append new-unsubscribes (list (car unsubscribes)))))
2580        (t
2581         (when (member (car unsubscribes) new-flist)
2582           (setq new-flist (delete (car unsubscribes) new-flist))
2583           (wl-append new-unsubscribes (list (car unsubscribes))))))
2584       (when (> len elmo-display-progress-threshold)
2585         (setq i (1+ i))
2586         (if (or (zerop (% i 10)) (= i len))
2587             (elmo-display-progress
2588              'wl-folder-update-access-group "Updating access group..."
2589              (/ (* i 100) len))))
2590       (setq unsubscribes (cdr unsubscribes)))
2591     ;;
2592     (if (or new-flist removes)
2593         (setq diff t))
2594     (setq new-flist
2595           (mapcar '(lambda (x)
2596                      (cond ((consp x) (list (car x) 'access))
2597                            (t x)))
2598                   new-flist))
2599     ;; check new groups
2600     (let ((new-list new-flist))
2601       (while new-list
2602         (if (not (wl-folder-access-subscribe-p
2603                   (car entity)
2604                   (if (listp (car new-list))
2605                       (caar new-list)
2606                     (car new-list))))
2607             ;; auto unsubscribe
2608             (progn
2609               (wl-append new-unsubscribes (list (car new-list)))
2610               (setq new-flist (delete (car new-list) new-flist)))
2611           (cond
2612            ((listp (car new-list))
2613             ;; check group exists
2614             (if (wl-string-assoc (caar new-list) wl-folder-group-alist)
2615                 (progn
2616                   (message "%s: group already exists." (caar new-list))
2617                   (sit-for 1)
2618                   (wl-append new-unsubscribes (list (car new-list)))
2619                   (setq new-flist (delete (car new-list) new-flist)))
2620               (wl-append wl-folder-group-alist
2621                          (list (cons (caar new-list) nil)))))))
2622         (setq new-list (cdr new-list))))
2623     (if new-flist
2624         (message "%d new folder(s)." (length new-flist))
2625       (message "Updating access group...done"))
2626     (wl-append new-flist subscribed-list)       ;; new is first
2627     (run-hooks 'wl-folder-update-access-group-hook)
2628     (setcdr (cdr entity) (list new-flist new-unsubscribes))
2629     (list diff new-flist new-unsubscribes removes)))
2630
2631 (defun wl-folder-prefetch-entity (entity)
2632   "Prefetch all new messages in the ENTITY."
2633   (cond
2634    ((consp entity)
2635     (let ((flist (nth 2 entity))
2636           (sum-done 0)
2637           (sum-all 0)
2638           result)
2639       (while flist
2640         (setq result (wl-folder-prefetch-entity (car flist)))
2641         (setq sum-done (+ sum-done (car result)))
2642         (setq sum-all (+ sum-all (cdr result)))
2643         (setq flist (cdr flist)))
2644       (message "Prefetched %d/%d message(s) in \"%s\"."
2645                sum-done sum-all
2646                (wl-folder-get-petname (car entity)))
2647       (cons sum-done sum-all)))
2648    ((stringp entity)
2649     (let* ((folder (wl-folder-get-elmo-folder entity))
2650            (nums (wl-folder-get-entity-info entity))
2651            (wl-summary-highlight (if (or (wl-summary-sticky-p folder)
2652                                          (wl-summary-always-sticky-folder-p
2653                                           folder))
2654                                      wl-summary-highlight))
2655            wl-summary-exit-next-move
2656            wl-auto-select-first ret-val
2657            count)
2658       (setq count (or (car nums) 0))
2659       (setq count (+ count (wl-folder-count-incorporates folder)))
2660       (if (or (null (car nums)) ; unknown
2661               (< 0 count))
2662           (save-window-excursion
2663             (save-excursion
2664               (let ((wl-summary-buffer-name (concat
2665                                              wl-summary-buffer-name
2666                                              (symbol-name this-command)))
2667                     (wl-summary-use-frame nil)
2668                     (wl-summary-always-sticky-folder-list nil))
2669                 (wl-summary-goto-folder-subr entity
2670                                              (wl-summary-get-sync-range
2671                                               folder)
2672                                              nil)
2673                 (setq ret-val (wl-summary-incorporate))
2674                 (wl-summary-exit)
2675                 ret-val)))
2676         (cons 0 0))))))
2677
2678 (defun wl-folder-count-incorporates (folder)
2679   (let ((marks (elmo-msgdb-mark-load
2680                 (elmo-folder-msgdb-path folder)))
2681         (sum 0))
2682     (while marks
2683       (if (member (cadr (car marks))
2684                   wl-summary-incorporate-marks)
2685           (incf sum))
2686       (setq marks (cdr marks)))
2687     sum))
2688
2689 (defun wl-folder-prefetch-current-entity (&optional no-check)
2690   "Prefetch all uncached messages in the folder at position.
2691 If current line is group folder, all subfolders are prefetched."
2692   (interactive "P")
2693   (save-excursion
2694     (let ((entity-name (wl-folder-get-entity-from-buffer))
2695           (group (wl-folder-buffer-group-p))
2696           wl-folder-check-entity-hook
2697           summary-buf entity)
2698       (when entity-name
2699         (setq entity
2700               (if group
2701                   (wl-folder-search-group-entity-by-name entity-name
2702                                                          wl-folder-entity)
2703                 entity-name))
2704         (if (not no-check)
2705             (wl-folder-check-entity entity))
2706         (wl-folder-prefetch-entity entity)))))
2707
2708 ;(defun wl-folder-drop-unsync-entity (entity)
2709 ;  "Drop all unsync messages in the ENTITY."
2710 ;  (cond
2711 ;   ((consp entity)
2712 ;    (let ((flist (nth 2 entity)))
2713 ;      (while flist
2714 ;       (wl-folder-drop-unsync-entity (car flist))
2715 ;       (setq flist (cdr flist)))))
2716 ;   ((stringp entity)
2717 ;    (let ((nums (wl-folder-get-entity-info entity))
2718 ;         wl-summary-highlight wl-auto-select-first new)
2719 ;      (setq new (or (car nums) 0))
2720 ;      (if (< 0 new)
2721 ;         (save-window-excursion
2722 ;           (save-excursion
2723 ;             (let ((wl-summary-buffer-name (concat
2724 ;                                            wl-summary-buffer-name
2725 ;                                            (symbol-name this-command))))
2726 ;               (wl-summary-goto-folder-subr entity 'no-sync nil)
2727 ;               (wl-summary-drop-unsync)
2728 ;               (wl-summary-exit)))))))))
2729
2730 ;(defun wl-folder-drop-unsync-current-entity (&optional force-check)
2731 ;  "Drop all unsync messages in the folder at position.
2732 ;If current line is group folder, all subfolders are dropped.
2733 ;If optional arg exists, don't check any folders."
2734 ;  (interactive "P")
2735 ;  (save-excursion
2736 ;    (let ((entity-name (wl-folder-get-entity-from-buffer))
2737 ;         (group (wl-folder-buffer-group-p))
2738 ;         wl-folder-check-entity-hook
2739 ;         summary-buf entity)
2740 ;      (when (and entity-name
2741 ;                (y-or-n-p (format
2742 ;                           "Drop all unsync messages in %s? " entity-name)))
2743 ;       (setq entity
2744 ;             (if group
2745 ;                 (wl-folder-search-group-entity-by-name entity-name
2746 ;                                                        wl-folder-entity)
2747 ;               entity-name))
2748 ;       (if (null force-check)
2749 ;           (wl-folder-check-entity entity))
2750 ;       (wl-folder-drop-unsync-entity entity)
2751 ;       (message "All unsync messages in %s are dropped!" entity-name)))))
2752
2753 (defun wl-folder-write-current-folder ()
2754   "Write message to current folder's newsgroup or mailing-list.
2755 Call `wl-summary-write-current-folder' with current folder name."
2756   (interactive)
2757   (unless (wl-folder-buffer-group-p)
2758     (wl-summary-write-current-folder
2759      (wl-folder-get-realname (wl-folder-entity-name)))))
2760
2761 (defun wl-folder-mimic-kill-buffer ()
2762   "Kill the current (Folder) buffer with query."
2763   (interactive)
2764   (let ((bufname (read-buffer (format "Kill buffer: (default %s) "
2765                                       (buffer-name))))
2766         wl-interactive-exit)
2767     (if (or (not bufname)
2768             (string-equal bufname "")
2769             (string-equal bufname (buffer-name)))
2770         (wl-exit)
2771       (kill-buffer bufname))))
2772
2773 (defun wl-folder-create-subr (folder)
2774   (if (y-or-n-p (format "Folder %s does not exist, create it? "
2775                         (elmo-folder-name-internal folder)))
2776       (progn
2777         (message "")
2778         (setq wl-folder-entity-hashtb
2779               (wl-folder-create-entity-hashtb
2780                (elmo-folder-name-internal folder)
2781                wl-folder-entity-hashtb))
2782         (unless (elmo-folder-create folder)
2783           (error "Create folder failed")))
2784     (error "Folder %s is not created" (elmo-folder-name-internal folder))))
2785
2786 (defun wl-folder-confirm-existence (folder &optional force)
2787   (if force
2788       (unless (elmo-folder-exists-p folder)
2789         (wl-folder-create-subr folder))
2790     (unless (or (wl-folder-entity-exists-p (elmo-folder-name-internal folder))
2791                 (file-exists-p (elmo-folder-msgdb-path folder))
2792                 (elmo-folder-exists-p folder))
2793       (wl-folder-create-subr folder))))
2794
2795 (require 'product)
2796 (product-provide (provide 'wl-folder) (require 'wl-version))
2797
2798 ;;; wl-folder.el ends here