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