* elmo.el (elmo-folder-search): Defined. Fixed docstring.
[elisp/wanderlust.git] / elmo / elmo-msgdb.el
index 595a435..87c63d2 100644 (file)
@@ -1,10 +1,11 @@
-;;; elmo-msgdb.el -- Message Database for Elmo.
+;;; elmo-msgdb.el --- Message Database for ELMO.
 
-;; Copyright 1998,1999,2000 Yuuichi Teranishi <teranisi@gohome.org>
+;; Copyright (C) 1998,1999,2000 Yuuichi Teranishi <teranisi@gohome.org>
+;; Copyright (C) 2000           Masahiro MURATA <muse@ba2.so-net.ne.jp>
 
 ;; Author: Yuuichi Teranishi <teranisi@gohome.org>
+;;     Masahiro MURATA <muse@ba2.so-net.ne.jp>
 ;; Keywords: mail, net news
-;; Time-stamp: <00/03/06 13:24:13 teranisi>
 
 ;; This file is part of ELMO (Elisp Library for Message Orchestration).
 
 ;;
 
 ;;; Commentary:
-;; 
+;;
 
 ;;; Code:
-;; 
+;;
 
 (eval-when-compile (require 'cl))
 (require 'elmo-vars)
 (require 'elmo-util)
 (require 'emu)
 (require 'std11)
-(require 'elmo-cache)
-
-(defun elmo-msgdb-expand-path (folder &optional spec)
-  (convert-standard-filename
-   (let* ((spec (or spec (elmo-folder-get-spec folder)))
-         (type (car spec))
-         fld)
-     (cond 
-      ((eq type 'imap4)
-       (setq fld (elmo-imap4-spec-mailbox spec))
-       (if (string= "inbox" (downcase fld))
-          (setq fld "inbox"))
-       (if (eq (string-to-char fld) ?/)
-          (setq fld (substring fld 1 (length fld))))
-       (expand-file-name 
-       fld
-       (expand-file-name (or (elmo-imap4-spec-username spec) "nobody")
-                         (expand-file-name (or 
-                                            (elmo-imap4-spec-hostname spec)
-                                            "nowhere")
-                                           (expand-file-name 
-                                            "imap"
-                                            elmo-msgdb-dir)))))
-      ((eq type 'nntp)
-       (expand-file-name 
-       (elmo-nntp-spec-group spec)
-       (expand-file-name (or (elmo-nntp-spec-hostname spec) "nowhere")
-                         (expand-file-name "nntp"
-                                           elmo-msgdb-dir))))
-      ((eq type 'maildir)
-       (expand-file-name (elmo-safe-filename (nth 1 spec))
-                        (expand-file-name "maildir"
-                                          elmo-msgdb-dir)))
-      ((eq type 'folder)
-       (expand-file-name (elmo-safe-filename (nth 1 spec))
-                        (expand-file-name "folder"
-                                          elmo-msgdb-dir)))
-      ((eq type 'multi)
-       (expand-file-name (elmo-safe-filename folder)
-                        (expand-file-name "multi"
-                                          elmo-msgdb-dir)))
-      ((eq type 'filter)
-       (expand-file-name
-       (elmo-safe-filename folder)
-       (expand-file-name "filter"
-                         elmo-msgdb-dir)))
-      ((eq type 'archive)
-       (expand-file-name 
-       (directory-file-name
-        (concat
-         (elmo-replace-in-string 
-          (elmo-replace-in-string
-           (elmo-replace-in-string 
-            (nth 1 spec)
-            "/" "_")
-           ":" "__")
-          "~" "___")
-         "/" (nth 3 spec)))
-       (expand-file-name (concat (symbol-name type) "/" 
-                                 (symbol-name (nth 2 spec)))
-                         elmo-msgdb-dir)))
-      ((eq type 'pop3)
-       (expand-file-name 
-       (elmo-safe-filename (elmo-pop3-spec-username spec))
-       (expand-file-name (elmo-pop3-spec-hostname spec)
-                         (expand-file-name
-                          "pop"
-                          elmo-msgdb-dir))))
-      ((eq type 'localnews)
-       (expand-file-name
-       (elmo-replace-in-string (nth 1 spec) "/" ".")
-       (expand-file-name "localnews"
-                         elmo-msgdb-dir)))
-      ((eq type 'internal)
-       (expand-file-name (elmo-safe-filename (concat (symbol-name (nth 1 spec))
-                                                    (nth 2 spec)))
-                        (expand-file-name "internal"
-                                          elmo-msgdb-dir)))
-      ((eq type 'cache)
-       (expand-file-name (elmo-safe-filename (nth 1 spec))
-                        (expand-file-name "internal/cache"
-                                          elmo-msgdb-dir)))
-      (t ; local dir or undefined type
-       ;; absolute path
-       (setq fld (nth 1 spec))
-       (if (file-name-absolute-p fld)
-          (setq fld (elmo-safe-filename fld)))
-       (expand-file-name fld
-                        (expand-file-name (symbol-name type)
-                                          elmo-msgdb-dir)))))))
 
 (defsubst elmo-msgdb-append-element (list element)
   (if list
-      ;(append list (list element))
+;;;   (append list (list element))
       (nconc list (list element))
     ;; list is nil
     (list element)))
   (cadr msgdb))
 (defsubst elmo-msgdb-get-mark-alist (msgdb)
   (caddr msgdb))
-(defsubst elmo-msgdb-get-location (msgdb)
-  (cadddr msgdb))
+;(defsubst elmo-msgdb-get-location (msgdb)
+;  (cadddr msgdb))
+(defsubst elmo-msgdb-get-overviewht (msgdb)
+  (nth 3 msgdb))
 
 ;;
 ;; number <-> Message-ID handling
 ;;
 (defsubst elmo-msgdb-number-add (alist number id)
   (let ((ret-val alist))
-    (setq ret-val 
+    (setq ret-val
          (elmo-msgdb-append-element ret-val (cons number id)))
     ret-val))
 
 (defun elmo-msgdb-global-mark-delete (msgid)
   (let* ((path (expand-file-name
                elmo-msgdb-global-mark-filename
-               elmo-msgdb-dir))
-        (malist (or elmo-msgdb-global-mark-alist 
-                    (setq elmo-msgdb-global-mark-alist 
+               elmo-msgdb-directory))
+        (malist (or elmo-msgdb-global-mark-alist
+                    (setq elmo-msgdb-global-mark-alist
                           (elmo-object-load path))))
         match)
     (when (setq match (assoc msgid malist))
 (defun elmo-msgdb-global-mark-set (msgid mark)
   (let* ((path (expand-file-name
                elmo-msgdb-global-mark-filename
-               elmo-msgdb-dir))
-        (malist (or elmo-msgdb-global-mark-alist 
-                    (setq elmo-msgdb-global-mark-alist 
+               elmo-msgdb-directory))
+        (malist (or elmo-msgdb-global-mark-alist
+                    (setq elmo-msgdb-global-mark-alist
                           (elmo-object-load path))))
         match)
     (if (setq match (assoc msgid malist))
     (elmo-object-save path elmo-msgdb-global-mark-alist)))
 
 (defun elmo-msgdb-global-mark-get (msgid)
-  (cdr (assoc msgid (or elmo-msgdb-global-mark-alist 
-                       (setq elmo-msgdb-global-mark-alist 
-                             (elmo-object-load 
+  (cdr (assoc msgid (or elmo-msgdb-global-mark-alist
+                       (setq elmo-msgdb-global-mark-alist
+                             (elmo-object-load
                               (expand-file-name
                                elmo-msgdb-global-mark-filename
-                               elmo-msgdb-dir)))))))
-
-;;
-;; number <-> location handling
-;;
-(defsubst elmo-msgdb-location-load (dir)
-  (elmo-object-load
-   (expand-file-name
-    elmo-msgdb-location-filename
-    dir)))
-                         
-(defsubst elmo-msgdb-location-add (alist number location)
-  (let ((ret-val alist))
-    (setq ret-val 
-         (elmo-msgdb-append-element ret-val (cons number location)))
-    ret-val))
-
-(defsubst elmo-msgdb-location-save (dir alist)
-  (elmo-object-save 
-   (expand-file-name
-    elmo-msgdb-location-filename
-    dir) alist))
-
-(defun elmo-list-folder-by-location (spec locations &optional msgdb)
-  (let* ((path (elmo-msgdb-expand-path nil spec))
-        (location-alist (if msgdb
-                            (elmo-msgdb-get-location msgdb)
-                          (elmo-msgdb-location-load path)))
-        (locations-in-db (mapcar 'cdr location-alist))
-        result new-locs new-alist deleted-locs i
-        modified)
-    (setq new-locs
-         (elmo-delete-if (function
-                          (lambda (x) (member x locations-in-db)))
-                         locations))
-    (setq deleted-locs
-         (elmo-delete-if (function
-                          (lambda (x) (member x locations)))
-                         locations-in-db))
-    (setq modified new-locs)
-    (setq i (or (elmo-max-of-list (mapcar 'car location-alist)) 0))
-    (mapcar 
-     (function 
-      (lambda (x)
-       (setq location-alist
-             (delq (rassoc x location-alist) location-alist))))
-     deleted-locs)
-    (while new-locs
-      (setq i (1+ i))
-      (setq new-alist (cons (cons i (car new-locs)) new-alist))
-      (setq new-locs (cdr new-locs)))
-    (setq result (nconc location-alist new-alist))
-    (setq result (sort result (lambda (x y) (< (car x)(car y)))))
-    (if modified (elmo-msgdb-location-save path result))
-    (mapcar 'car result)))
+                               elmo-msgdb-directory)))))))
 
 ;;;
 ;; persistent mark handling
            (setq ret-val (delq entity alist))
          ;; set mark
          (setcar (cdr entity) mark))
-      (if mark 
+      (if mark
          (setq ret-val (elmo-msgdb-append-element ret-val
                                                   (list id mark)))))
     ret-val))
 
 (defun elmo-msgdb-mark-append (alist id mark)
-  "Append mark"
+  "Append mark."
   (setq alist (elmo-msgdb-append-element alist
                                         (list id mark))))
 
 (defun elmo-msgdb-mark-alist-to-seen-list (number-alist mark-alist seen-marks)
-  "Make seen-list from mark-alist"
+  "Make seen-list from MARK-ALIST."
   (let ((seen-mark-list (string-to-char-list seen-marks))
        ret-val ent)
     (while number-alist
     ret-val))
 
 ;;
+;; mime decode cache
+
+(defvar elmo-msgdb-decoded-cache-hashtb nil)
+(make-variable-buffer-local 'elmo-msgdb-decoded-cache-hashtb)
+
+(defsubst elmo-msgdb-get-decoded-cache (string)
+  (if elmo-use-decoded-cache
+      (let ((hashtb (or elmo-msgdb-decoded-cache-hashtb
+                       (setq elmo-msgdb-decoded-cache-hashtb
+                             (elmo-make-hash 2048))))
+           decoded)
+       (or (elmo-get-hash-val string hashtb)
+           (progn
+             (elmo-set-hash-val
+              string
+              (setq decoded
+                    (decode-mime-charset-string string elmo-mime-charset))
+              hashtb)
+             decoded)))
+    (decode-mime-charset-string string elmo-mime-charset)))
+
+;;
 ;; overview handling
 ;;
 
        (while (re-search-forward (concat "^" name ":[ \t]*") nil t)
          (setq field-body
                (nconc field-body
-                      (list (buffer-substring-no-properties 
+                      (list (buffer-substring-no-properties
                              (match-end 0) (std11-field-end))))))
        field-body))))
 
@@ -347,15 +228,15 @@ header separator."
                  (buffer-substring beg (point)) "\n[ \t]*" ""))))))))
 
 (defun elmo-msgdb-number-load (dir)
-  (elmo-object-load 
+  (elmo-object-load
    (expand-file-name elmo-msgdb-number-filename dir)))
 
 (defun elmo-msgdb-overview-load (dir)
-  (elmo-object-load 
+  (elmo-object-load
    (expand-file-name elmo-msgdb-overview-filename dir)))
 
 (defun elmo-msgdb-mark-load (dir)
-  (elmo-object-load 
+  (elmo-object-load
    (expand-file-name elmo-msgdb-mark-filename dir)))
 
 (defsubst elmo-msgdb-seen-load (dir)
@@ -364,58 +245,163 @@ header separator."
                     dir)))
 
 (defun elmo-msgdb-number-save (dir obj)
-  (elmo-object-save 
+  (elmo-object-save
    (expand-file-name elmo-msgdb-number-filename dir)
    obj))
 
 (defun elmo-msgdb-mark-save (dir obj)
-  (elmo-object-save 
+  (elmo-object-save
    (expand-file-name elmo-msgdb-mark-filename dir)
    obj))
 
+(defun elmo-msgdb-change-mark (msgdb before after)
+  "Set the BEFORE marks to AFTER."
+  (let ((mark-alist (elmo-msgdb-get-mark-alist msgdb))
+       entity)
+    (while mark-alist
+      (setq entity (car mark-alist))
+      (when (string= (cadr entity) before)
+       (setcar (cdr entity) after))
+      (setq mark-alist (cdr mark-alist)))))
+
 (defsubst elmo-msgdb-seen-save (dir obj)
-  (elmo-object-save 
+  (elmo-object-save
    (expand-file-name elmo-msgdb-seen-filename dir)
    obj))
 
 (defsubst elmo-msgdb-overview-save (dir overview)
-  (elmo-object-save 
+  (elmo-object-save
    (expand-file-name elmo-msgdb-overview-filename dir)
    overview))
 
-(defun elmo-msgdb-delete-msgs (folder msgs msgdb &optional reserve-cache)
-  "Delete MSGS from FOLDER in MSGDB. 
+(defun elmo-msgdb-match-condition-primitive (condition entity numbers)
+  (catch 'unresolved
+    (let ((key (elmo-filter-key condition))
+         (case-fold-search t)
+         result)
+      (cond
+       ((string= key "last")
+       (setq result (<= (length (memq
+                                 (elmo-msgdb-overview-entity-get-number
+                                  entity)
+                                 numbers))
+                        (string-to-int (elmo-filter-value condition)))))
+       ((string= key "first")
+       (setq result (< (-
+                        (length numbers)
+                        (length (memq
+                                 (elmo-msgdb-overview-entity-get-number
+                                  entity)
+                                 numbers)))
+                       (string-to-int (elmo-filter-value condition)))))
+       ((string= key "from")
+       (setq result (string-match
+                     (elmo-filter-value condition)
+                     (elmo-msgdb-overview-entity-get-from entity))))
+       ((string= key "subject")
+       (setq result (string-match
+                     (elmo-filter-value condition)
+                     (elmo-msgdb-overview-entity-get-subject entity))))
+       ((string= key "to")
+       (setq result (string-match
+                     (elmo-filter-value condition)
+                     (elmo-msgdb-overview-entity-get-to entity))))
+       ((string= key "cc")
+       (setq result (string-match
+                     (elmo-filter-value condition)
+                     (elmo-msgdb-overview-entity-get-cc entity))))
+       ((or (string= key "since")
+           (string= key "before"))
+       (let ((field-date (elmo-date-make-sortable-string
+                          (timezone-fix-time
+                           (elmo-msgdb-overview-entity-get-date entity)
+                           (current-time-zone) nil)))
+             (specified-date
+              (elmo-date-make-sortable-string
+               (elmo-date-get-datevec
+                (elmo-filter-value condition)))))
+         (setq result (if (string= key "since")
+                          (or (string= specified-date field-date)
+                              (string< specified-date field-date))
+                        (string< field-date specified-date)))))
+       ((member key elmo-msgdb-extra-fields)
+       (let ((extval (elmo-msgdb-overview-entity-get-extra-field entity key)))
+         (when (stringp extval)
+           (setq result (string-match
+                         (elmo-filter-value condition)
+                         extval)))))
+       (t
+       (throw 'unresolved condition)))
+      (if (eq (elmo-filter-type condition) 'unmatch)
+         (not result)
+       result))))
+
+(defun elmo-msgdb-match-condition (condition entity numbers)
+  (cond
+   ((vectorp condition)
+    (elmo-msgdb-match-condition-primitive condition entity numbers))
+   ((eq (car condition) 'and)
+    (let ((lhs (elmo-msgdb-match-condition (nth 1 condition)
+                                          entity numbers)))
+      (cond
+       ((elmo-filter-condition-p lhs)
+       (let ((rhs (elmo-msgdb-match-condition (nth 2 condition)
+                                              entity numbers)))
+         (cond ((elmo-filter-condition-p rhs)
+                (list 'and lhs rhs))
+               (rhs
+                lhs))))
+       (lhs
+       (elmo-msgdb-match-condition (nth 2 condition)
+                                   entity numbers)))))
+   ((eq (car condition) 'or)
+    (let ((lhs (elmo-msgdb-match-condition (nth 1 condition)
+                                          entity numbers)))
+      (cond
+       ((elmo-filter-condition-p lhs)
+       (let ((rhs (elmo-msgdb-match-condition (nth 2 condition)
+                                              entity numbers)))
+         (cond ((elmo-filter-condition-p rhs)
+                (list 'or lhs rhs))
+               (rhs
+                t)
+               (t
+                lhs))))
+       (lhs
+       t)
+       (t
+       (elmo-msgdb-match-condition (nth 2 condition)
+                                   entity numbers)))))))
+
+(defun elmo-msgdb-delete-msgs (msgdb msgs)
+  "Delete MSGS from MSGDB
 content of MSGDB is changed."
   (save-excursion
-    (let* ((msg-list msgs)
-          (dir (elmo-msgdb-expand-path folder))
-          (overview (or (car msgdb)
-                        (elmo-msgdb-overview-load dir)))
-          (number-alist (or (cadr msgdb)
-                            (elmo-msgdb-number-load dir)))
-          (mark-alist (or (caddr msgdb)
-                          (elmo-msgdb-mark-load dir)))
-          message-id)
+    (let* (;(msgdb (elmo-folder-msgdb folder))
+          (overview (car msgdb))
+          (number-alist (cadr msgdb))
+          (mark-alist (caddr msgdb))
+          (hashtb (elmo-msgdb-get-overviewht msgdb))
+          (newmsgdb (list overview number-alist mark-alist hashtb))
+          ov-entity)
       ;; remove from current database.
-      (while msg-list
-       (setq message-id (cdr (assq (car msg-list) number-alist)))
-       (if (and (not reserve-cache) message-id) 
-           (elmo-cache-delete message-id
-                              folder (car msg-list)))
-       ;; This is no good!!!!
-       ;(setq overview (delete (assoc message-id overview) overview))
-       (setq overview 
+      (while msgs
+       (setq overview
              (delq
-              (elmo-msgdb-overview-get-entity-by-number overview 
-                                                        (car msg-list))
+              (setq ov-entity
+                    (elmo-msgdb-overview-get-entity (car msgs) newmsgdb))
               overview))
+       (when (and elmo-use-overview-hashtb hashtb)
+         (elmo-msgdb-clear-overview-hashtb ov-entity hashtb))
        (setq number-alist
-             (delq (assq (car msg-list) number-alist) number-alist))
-       (setq mark-alist (delq (assq (car msg-list) mark-alist) mark-alist))
-       (setq msg-list (cdr msg-list)))
+             (delq (assq (car msgs) number-alist) number-alist))
+       (setq mark-alist (delq (assq (car msgs) mark-alist) mark-alist))
+       (setq msgs (cdr msgs)))
+      ;(elmo-folder-set-message-modified-internal folder t)
       (setcar msgdb overview)
       (setcar (cdr msgdb) number-alist)
-      (setcar (cddr msgdb) mark-alist))
+      (setcar (cddr msgdb) mark-alist)
+      (setcar (nthcdr 3 msgdb) hashtb))
     t)) ;return value
 
 (defsubst elmo-msgdb-set-overview (msgdb overview)
@@ -430,12 +416,16 @@ content of MSGDB is changed."
 (defsubst elmo-msgdb-overview-entity-get-references (entity)
   (and entity (aref (cdr entity) 1)))
 
+(defsubst elmo-msgdb-overview-entity-set-references (entity references)
+  (and entity (aset (cdr entity) 1 references))
+  entity)
+
 ;; entity -> parent-entity
 (defsubst elmo-msgdb-overview-get-parent-entity (entity database)
   (setq entity (elmo-msgdb-overview-entity-get-references entity))
   ;; entity is parent-id.
   (and entity (assoc entity database)))
-  
+
 (defsubst elmo-msgdb-overview-entity-get-number (entity)
   (and entity (aref (cdr entity) 0)))
 
@@ -445,13 +435,12 @@ content of MSGDB is changed."
 (defsubst elmo-msgdb-overview-entity-get-from (entity)
   (and entity
        (aref (cdr entity) 2)
-       (decode-mime-charset-string (aref (cdr entity) 2) 
-                                  elmo-mime-charset)))
+       (elmo-msgdb-get-decoded-cache (aref (cdr entity) 2))))
 
 (defsubst elmo-msgdb-overview-entity-set-number (entity number)
   (and entity (aset (cdr entity) 0 number))
   entity)
-  ;(setcar (cadr entity) number) entity)
+;;;(setcar (cadr entity) number) entity)
 
 (defsubst elmo-msgdb-overview-entity-set-from (entity from)
   (and entity (aset (cdr entity) 2 from))
@@ -460,8 +449,7 @@ content of MSGDB is changed."
 (defsubst elmo-msgdb-overview-entity-get-subject (entity)
   (and entity
        (aref (cdr entity) 3)
-       (decode-mime-charset-string (aref (cdr entity) 3)
-                                  elmo-mime-charset)))
+       (elmo-msgdb-get-decoded-cache (aref (cdr entity) 3))))
 
 (defsubst elmo-msgdb-overview-entity-get-subject-no-decode (entity)
   (and entity (aref (cdr entity) 3)))
@@ -473,6 +461,10 @@ content of MSGDB is changed."
 (defsubst elmo-msgdb-overview-entity-get-date (entity)
   (and entity (aref (cdr entity) 4)))
 
+(defsubst elmo-msgdb-overview-entity-set-date (entity date)
+  (and entity (aset (cdr entity) 4 date))
+  entity)
+
 (defsubst elmo-msgdb-overview-entity-get-to (entity)
   (and entity (aref (cdr entity) 5)))
 
@@ -482,6 +474,10 @@ content of MSGDB is changed."
 (defsubst elmo-msgdb-overview-entity-get-size (entity)
   (and entity (aref (cdr entity) 7)))
 
+(defsubst elmo-msgdb-overview-entity-set-size (entity size)
+  (and entity (aset (cdr entity) 7 size))
+  entity)
+
 (defsubst elmo-msgdb-overview-entity-get-id (entity)
   (and entity (car entity)))
 
@@ -490,17 +486,50 @@ content of MSGDB is changed."
     (and extra
         (cdr (assoc field-name extra)))))
 
+(defsubst elmo-msgdb-overview-entity-set-extra-field (entity field-name value)
+  (let ((extras (and entity (aref (cdr entity) 8)))
+       extra)
+    (if (setq extra (assoc field-name extras))
+       (setcdr extra value)
+      (elmo-msgdb-overview-entity-set-extra
+       entity
+       (cons (cons field-name value) extras)))))
+
+(defsubst elmo-msgdb-overview-entity-get-extra (entity)
+  (and entity (aref (cdr entity) 8)))
+
+(defsubst elmo-msgdb-overview-entity-set-extra (entity extra)
+  (and entity (aset (cdr entity) 8 extra))
+  entity)
+
 (defun elmo-msgdb-overview-get-entity-by-number (database number)
-  (let ((db database)
-       entity)
-    (catch 'loop
+  (when number
+    (let ((db database)
+         entity)
       (while db
        (if (eq (elmo-msgdb-overview-entity-get-number (car db)) number)
-           (progn
-             (setq entity (car db))
-             (throw 'loop nil))
-         (setq db (cdr db)))))
-    entity))
+           (setq entity (car db)
+                 db nil) ; exit loop
+         (setq db (cdr db))))
+      entity)))
+
+(defun elmo-msgdb-overview-get-entity (id msgdb)
+  (when id
+    (let ((ovht (elmo-msgdb-get-overviewht msgdb)))
+      (if ovht ; use overview hash
+         (if (stringp id) ;; ID is message-id
+             (elmo-get-hash-val id ovht)
+           (elmo-get-hash-val (format "#%d" id) ovht))
+       (let* ((overview (elmo-msgdb-get-overview msgdb))
+              (number-alist (elmo-msgdb-get-number-alist msgdb))
+              (message-id (if (stringp id)
+                              id ;; ID is message-id
+                            (cdr (assq id number-alist))))
+              entity)
+         (if message-id
+             (assoc message-id overview)
+           ;; ID is number. message-id is nil or no exists in number-alist.
+           (elmo-msgdb-overview-get-entity-by-number overview id)))))))
 
 ;;
 ;; deleted message handling
@@ -516,126 +545,124 @@ content of MSGDB is changed."
    killed-list))
 
 (defun elmo-msgdb-killed-message-p (killed-list msg)
-  (and killed-list
-       (not (listp
-            (catch 'found
-              (mapcar 
-               (function 
-                (lambda (entity)
-                  (cond 
-                   ((integerp entity)
-                    (if (eq entity msg)
-                        (throw 'found t)))
-                   ((consp entity)
-                    (if (and (<= (car entity) msg)
-                             (<= msg (cdr entity)))
-                        (throw 'found t)))))
-                killed-list)))))))
+  (elmo-number-set-member msg killed-list))
 
 (defun elmo-msgdb-set-as-killed (killed-list msg)
-  "if cons cell, set car-cdr messages as killed.
-if integer, set number th message as killed."
-  (let ((dlist killed-list)
-       (ret-val killed-list)
-       entity found)
-    (cond
-     ((integerp msg)
-      (while (and dlist (not found))
-       (setq entity (car dlist))
-       (if (or (and (integerp entity) (eq entity msg))
-               (and (consp entity) 
-                    (<= (car entity) msg)
-                    (<= msg (cdr entity))))
-           (setq found t))
-       (setq dlist (cdr dlist))
-       )
-      (if (not found)
-         (setq ret-val (elmo-msgdb-append-element killed-list msg)))
-      )
-     ((consp msg)
-      (while (and dlist (not found))
-       (setq entity (car dlist))
-       (if (integerp entity)
-           (cond 
-            ((and (<= (car msg) entity)(<= entity (cdr msg)))
-             (setcar dlist msg)
-             (setq found t)
-             )
-            ((= (1- (car msg)) entity)
-             (setcar dlist (cons entity (cdr msg)))
-             (setq found t)
-             )
-            ((= (1+ (cdr msg)) entity)
-             (setcar dlist (cons (car msg) entity))
-             (setq found t)
-             ))
-         ;; entity is consp
-         (cond  ; there are four patterns
-          ((and (<= (car msg) (car entity))
-                (<= (cdr entity) (cdr msg)))
-           (setcar dlist msg)
-           (setq found t))
-          ((and (< (car entity)(car msg))
-                (< (cdr msg) (cdr entity)))
-           (setq found t))
-          ((and (<= (car msg) (car entity))
-                (<= (cdr msg) (cdr entity)))
-           (setcar dlist (cons (car msg) (cdr entity)))
-           (setq found t))
-          ((and (<= (car entity) (car msg))
-                (<= (cdr entity) (cdr msg)))
-           (setcar dlist (cons (car entity) (cdr msg)))
-           (setq found t))))
-       (setq dlist (cdr dlist)))
-      (if (not found)
-         (setq ret-val (elmo-msgdb-append-element killed-list msg)))))
+  (elmo-number-set-append killed-list msg))
+
+(defun elmo-msgdb-append-to-killed-list (folder msgs)
+  (elmo-folder-set-killed-list-internal
+   folder
+   (elmo-number-set-append-list
+    (elmo-folder-killed-list-internal folder)
+    msgs)))
+
+(defun elmo-msgdb-killed-list-length (killed-list)
+  (let ((killed killed-list)
+       (ret-val 0))
+    (while (car killed)
+      (if (consp (car killed))
+         (setq ret-val (+ ret-val 1 (- (cdar killed) (caar killed))))
+       (setq ret-val (+ ret-val 1)))
+      (setq killed (cdr killed)))
     ret-val))
 
+(defun elmo-msgdb-max-of-killed (killed-list)
+  (let ((klist killed-list)
+       (max 0)
+       k)
+    (while (car klist)
+      (if (< max
+            (setq k
+                  (if (consp (car klist))
+                      (cdar klist)
+                    (car klist))))
+         (setq max k))
+      (setq klist (cdr klist)))
+    max))
+
+(defun elmo-living-messages (messages killed-list)
+  (if killed-list
+      (delq nil
+           (mapcar (lambda (number)
+                     (unless (elmo-number-set-member number killed-list)
+                       number))
+                   messages))
+    messages))
+
 (defun elmo-msgdb-finfo-load ()
-  (elmo-object-load (expand-file-name 
+  (elmo-object-load (expand-file-name
                     elmo-msgdb-finfo-filename
-                    elmo-msgdb-dir)
+                    elmo-msgdb-directory)
                    elmo-mime-charset t))
 
 (defun elmo-msgdb-finfo-save (finfo)
   (elmo-object-save (expand-file-name
                     elmo-msgdb-finfo-filename
-                    elmo-msgdb-dir)
+                    elmo-msgdb-directory)
                    finfo elmo-mime-charset))
 
-(defun elmo-msgdb-flist-load (folder)
+(defun elmo-msgdb-flist-load (fname)
   (let ((flist-file (expand-file-name
                     elmo-msgdb-flist-filename
-                    (elmo-msgdb-expand-path folder (list 'folder folder)))))
-    (elmo-object-load flist-file nil t)))
+                    (expand-file-name
+                     (elmo-safe-filename fname)
+                     (expand-file-name "folder" elmo-msgdb-directory)))))
+    (elmo-object-load flist-file elmo-mime-charset t)))
 
-(defun elmo-msgdb-flist-save (folder flist)
+(defun elmo-msgdb-flist-save (fname flist)
   (let ((flist-file (expand-file-name
                     elmo-msgdb-flist-filename
-                    (elmo-msgdb-expand-path folder (list 'folder folder)))))
-    (elmo-object-save flist-file flist)))
+                    (expand-file-name
+                     (elmo-safe-filename fname)
+                     (expand-file-name "folder" elmo-msgdb-directory)))))
+    (elmo-object-save flist-file flist elmo-mime-charset)))
 
 (defun elmo-crosspost-alist-load ()
   (elmo-object-load (expand-file-name
                     elmo-crosspost-alist-filename
-                    elmo-msgdb-dir)
+                    elmo-msgdb-directory)
                    nil t))
 
 (defun elmo-crosspost-alist-save (alist)
   (elmo-object-save (expand-file-name
                     elmo-crosspost-alist-filename
-                    elmo-msgdb-dir)
+                    elmo-msgdb-directory)
                    alist))
 
+(defun elmo-msgdb-add-msgs-to-seen-list (msgs msgdb unread-marks seen-list)
+  ;; Add to seen list.
+  (let* ((number-alist (elmo-msgdb-get-number-alist msgdb))
+        (mark-alist   (elmo-msgdb-get-mark-alist msgdb))
+        ent)
+    (while msgs
+      (if (setq ent (assq (car msgs) mark-alist))
+         (unless (member (cadr ent) unread-marks) ;; not unread mark
+           (setq seen-list
+                 (cons (cdr (assq (car msgs) number-alist)) seen-list)))
+       ;; no mark ... seen...
+       (setq seen-list
+             (cons (cdr (assq (car msgs) number-alist)) seen-list)))
+      (setq msgs (cdr msgs)))
+    seen-list))
+
+(defun elmo-msgdb-get-message-id-from-buffer ()
+  (or (elmo-field-body "message-id")
+      ;; no message-id, so put dummy msgid.
+      (concat (timezone-make-date-sortable
+              (elmo-field-body "date"))
+             (nth 1 (eword-extract-address-components
+                     (or (elmo-field-body "from") "nobody"))))))
+
 (defsubst elmo-msgdb-create-overview-from-buffer (number &optional size time)
-  "Create overview entity from current buffer. 
+  "Create overview entity from current buffer.
 Header region is supposed to be narrowed."
   (save-excursion
     (let ((extras elmo-msgdb-extra-fields)
          message-id references from subject to cc date
          extra field-body)
       (elmo-set-buffer-multibyte default-enable-multibyte-characters)
-      (setq message-id (elmo-field-body "message-id"))
+      (setq message-id (elmo-msgdb-get-message-id-from-buffer))
       (setq references
            (or (elmo-msgdb-get-last-message-id
                 (elmo-field-body "in-reply-to"))
@@ -643,7 +670,7 @@ Header region is supposed to be narrowed."
                 (elmo-field-body "references"))))
       (setq from (elmo-mime-string (elmo-delete-char
                                    ?\"
-                                   (or 
+                                   (or
                                     (elmo-field-body "from")
                                     elmo-no-from))))
       (setq subject (elmo-mime-string (or (elmo-field-body "subject")
@@ -660,20 +687,64 @@ Header region is supposed to be narrowed."
            (setq extra (cons (cons (downcase (car extras))
                                    field-body) extra)))
        (setq extras (cdr extras)))
-      (cons message-id (vector number references 
+      (cons message-id (vector number references
                               from subject date to cc
                               size extra))
       )))
-  
+
+(defun elmo-msgdb-copy-overview-entity (entity)
+  (cons (car entity)
+       (copy-sequence (cdr entity))))
+
+(defsubst elmo-msgdb-insert-file-header (file)
+  "Insert the header of the article."
+  (let ((beg 0)
+       insert-file-contents-pre-hook   ; To avoid autoconv-xmas...
+       insert-file-contents-post-hook
+       format-alist)
+    (when (file-exists-p file)
+      ;; Read until header separator is found.
+      (while (and (eq elmo-msgdb-file-header-chop-length
+                     (nth 1
+                          (insert-file-contents-as-binary
+                           file nil beg
+                           (incf beg elmo-msgdb-file-header-chop-length))))
+                 (prog1 (not (search-forward "\n\n" nil t))
+                   (goto-char (point-max))))))))
+
+(defsubst elmo-msgdb-create-overview-entity-from-file (number file)
+  (let (insert-file-contents-pre-hook   ; To avoid autoconv-xmas...
+       insert-file-contents-post-hook header-end
+       (attrib (file-attributes file))
+       ret-val size mtime)
+    (with-temp-buffer
+      (if (not (file-exists-p file))
+         ()
+       (setq size (nth 7 attrib))
+       (setq mtime (timezone-make-date-arpa-standard
+                    (current-time-string (nth 5 attrib)) (current-time-zone)))
+       ;; insert header from file.
+       (catch 'done
+         (condition-case nil
+             (elmo-msgdb-insert-file-header file)
+           (error (throw 'done nil)))
+         (goto-char (point-min))
+         (setq header-end
+               (if (re-search-forward "\\(^--.*$\\)\\|\\(\n\n\\)" nil t)
+                   (point)
+                 (point-max)))
+         (narrow-to-region (point-min) header-end)
+         (elmo-msgdb-create-overview-from-buffer number size mtime))))))
+
 (defun elmo-msgdb-overview-sort-by-date (overview)
   (sort overview
        (function
-        (lambda (x y) 
+        (lambda (x y)
           (condition-case nil
               (string<
-               (timezone-make-date-sortable 
+               (timezone-make-date-sortable
                 (elmo-msgdb-overview-entity-get-date x))
-               (timezone-make-date-sortable 
+               (timezone-make-date-sortable
                 (elmo-msgdb-overview-entity-get-date y)))
             (error))))))
 
@@ -681,40 +752,121 @@ Header region is supposed to be narrowed."
   (message "Sorting...")
   (let ((overview (elmo-msgdb-get-overview msgdb)))
     (setq overview (elmo-msgdb-overview-sort-by-date overview))
-    (message "Sorting...done.")
+    (message "Sorting...done")
     (list overview (nth 1 msgdb)(nth 2 msgdb))))
 
-(defsubst elmo-msgdb-search-overview-entity (number number-alist overview)
-  (let ((message-id (cdr (assq number number-alist)))
-       ovs)
-    (if message-id
-       (assoc message-id overview)
-      (elmo-msgdb-overview-get-entity-by-number overview number))))
-
-(defsubst elmo-msgdb-append (msgdb msgdb-append)
-  (list 
+(defun elmo-msgdb-clear-overview-hashtb (entity hashtb)
+  (let (number)
+    (when (and entity
+              elmo-use-overview-hashtb
+              hashtb)
+      (and (setq number (elmo-msgdb-overview-entity-get-number entity))
+          (elmo-clear-hash-val (format "#%d" number) hashtb))
+      (and (car entity) ;; message-id
+          (elmo-clear-hash-val (car entity) hashtb)))))
+
+(defun elmo-msgdb-make-overview-hashtb (overview &optional hashtb)
+  (if (and elmo-use-overview-hashtb
+          overview)
+      (let ((hashtb (or hashtb ;; append
+                       (elmo-make-hash (length overview)))))
+       (while overview
+         ;; key is message-id
+         (if (caar overview)
+             (elmo-set-hash-val (caar overview) (car overview) hashtb))
+         ;; key is number
+         (elmo-set-hash-val
+          (format "#%d" (elmo-msgdb-overview-entity-get-number (car overview)))
+          (car overview) hashtb)
+         (setq overview (cdr overview)))
+       hashtb)
+    nil))
+
+(defsubst elmo-msgdb-append (msgdb msgdb-append &optional set-hash)
+  (list
    (nconc (car msgdb) (car msgdb-append))
    (nconc (cadr msgdb) (cadr msgdb-append))
    (nconc (caddr msgdb) (caddr msgdb-append))
-   (nconc (cadddr msgdb) (cadddr msgdb-append))))
-
-(defun elmo-msgdb-delete-path (folder &optional spec)
-  (let ((path (elmo-msgdb-expand-path folder spec)))
-    (if (file-directory-p path)
-       (elmo-delete-directory path t))))
-
-(defun elmo-msgdb-rename-path (old-folder new-folder &optional old-spec new-spec)
-  (let* ((old (directory-file-name (elmo-msgdb-expand-path old-folder old-spec)))
-        (new (directory-file-name (elmo-msgdb-expand-path new-folder new-spec)))
-        (new-dir (directory-file-name (file-name-directory new))))
-    (if (not (file-directory-p old))
-       ()
-      (if (file-exists-p new)
-         (error "already exists directory: %s" new)
-       (if (not (file-exists-p new-dir))
-           (elmo-make-directory new-dir))
-       (rename-file old new)))))
-
-(provide 'elmo-msgdb)
+   (and set-hash
+       (elmo-msgdb-make-overview-hashtb (car msgdb-append) (nth 3 msgdb)))))
+
+(defsubst elmo-msgdb-clear (&optional msgdb)
+  (if msgdb
+      (list
+       (setcar msgdb nil)
+       (setcar (cdr msgdb) nil)
+       (setcar (cddr msgdb) nil)
+       (setcar (nthcdr 3 msgdb) (elmo-msgdb-make-overview-hashtb nil)))
+    (list nil nil nil (elmo-msgdb-make-overview-hashtb nil))))
+
+(defsubst elmo-folder-get-info (folder &optional hashtb)
+  (elmo-get-hash-val folder
+                    (or hashtb elmo-folder-info-hashtb)))
+
+(defun elmo-folder-set-info-hashtb (folder max numbers &optional new unread)
+  (let ((info (elmo-folder-get-info folder)))
+    (when info
+      (or new     (setq new     (nth 0 info)))
+      (or unread  (setq unread  (nth 1 info)))
+      (or numbers (setq numbers (nth 2 info)))
+      (or max     (setq max     (nth 3 info))))
+    (elmo-set-hash-val folder
+                      (list new unread numbers max)
+                      elmo-folder-info-hashtb)))
+
+(defun elmo-folder-set-info-max-by-numdb (folder msgdb-number)
+  (let ((num-db (sort (mapcar 'car msgdb-number) '<)))
+    (elmo-folder-set-info-hashtb
+     folder
+     (or (nth (max 0 (1- (length num-db))) num-db) 0)
+     nil ;;(length num-db)
+     )))
+
+(defun elmo-folder-get-info-max (folder)
+  "Get folder info from cache."
+  (nth 3 (elmo-folder-get-info folder)))
+
+(defun elmo-folder-get-info-length (folder)
+  (nth 2 (elmo-folder-get-info folder)))
+
+(defun elmo-folder-get-info-unread (folder)
+  (nth 1 (elmo-folder-get-info folder)))
+
+(defun elmo-folder-info-make-hashtb (info-alist hashtb)
+  (let* ((hashtb (or hashtb
+                    (elmo-make-hash (length info-alist)))))
+    (mapcar
+     '(lambda (x)
+       (let ((info (cadr x)))
+         (and (intern-soft (car x) hashtb)
+              (elmo-set-hash-val (car x)
+                                 (list (nth 2 info)   ;; new
+                                       (nth 3 info)   ;; unread
+                                       (nth 1 info)   ;; length
+                                       (nth 0 info))  ;; max
+                                 hashtb))))
+     info-alist)
+    (setq elmo-folder-info-hashtb hashtb)))
+
+(defsubst elmo-msgdb-location-load (dir)
+  (elmo-object-load
+   (expand-file-name
+    elmo-msgdb-location-filename
+    dir)))
+
+(defsubst elmo-msgdb-location-add (alist number location)
+  (let ((ret-val alist))
+    (setq ret-val
+         (elmo-msgdb-append-element ret-val (cons number location)))
+    ret-val))
+
+(defsubst elmo-msgdb-location-save (dir alist)
+  (elmo-object-save
+   (expand-file-name
+    elmo-msgdb-location-filename
+    dir) alist))
+
+(require 'product)
+(product-provide (provide 'elmo-msgdb) (require 'elmo-version))
 
 ;;; elmo-msgdb.el ends here