* elmo-msgdb.el (elmo-msgdb-get-message-id-from-buffer): Added
[elisp/wanderlust.git] / elmo / elmo-msgdb.el
index ab844ab..286741a 100644 (file)
@@ -120,24 +120,30 @@ if MARK is nil, mark is removed."
     ;; return value.
     t))
 
-(defun elmo-msgdb-set-cached (msgdb number cached)
+(defun elmo-msgdb-get-cached (msgdb number)
+  "Return non-nil if message is cached."
+  (not (member (elmo-msgdb-get-mark msgdb number)
+              (elmo-msgdb-uncached-marks))))
+
+(defun elmo-msgdb-set-cached (msgdb number cached use-cache)
   "Set message cache status.
 If mark is changed, return non-nil."
   (let* ((cur-mark (elmo-msgdb-get-mark msgdb number))
-        (cur-status (cond
+        (cur-flag (cond
                      ((string= cur-mark elmo-msgdb-important-mark)
                       'important)
                      ((member cur-mark (elmo-msgdb-answered-marks))
                       'answered)
                      ((not (member cur-mark (elmo-msgdb-unread-marks)))
                       'read)))
-        (cur-cached (not (member cur-mark (elmo-msgdb-uncached-marks)))))
+        (cur-cached (elmo-file-cache-exists-p
+                     (elmo-msgdb-get-field msgdb number 'message-id))))
     (unless (eq cached cur-cached)
-      (case cur-status
+      (case cur-flag
        (read
         (elmo-msgdb-set-mark msgdb number
-                             (unless cached
-                               elmo-msgdb-read-uncached-mark)))
+                             (if (and use-cache (not cached))
+                                 elmo-msgdb-read-uncached-mark)))
        (important nil)
        (answered
         (elmo-msgdb-set-mark msgdb number
@@ -150,85 +156,85 @@ If mark is changed, return non-nil."
                                  elmo-msgdb-unread-cached-mark
                                elmo-msgdb-unread-uncached-mark)))))))
 
-(defun elmo-msgdb-set-status (msgdb folder number status)
-  "Set message status.
+(defun elmo-msgdb-set-flag (msgdb folder number flag)
+  "Set message flag.
 MSGDB is the ELMO msgdb.
 FOLDER is a ELMO folder structure.
-NUMBER is a message number to be set status.
-STATUS is a symbol which is one of the following:
+NUMBER is a message number to set flag.
+FLAG is a symbol which is one of the following:
 `read'      ... Messages which are already read.
 `important' ... Messages which are marked as important.
 `answered'  ... Messages which are marked as answered."
   (let* ((cur-mark (elmo-msgdb-get-mark msgdb number))
         (use-cache (elmo-message-use-cache-p folder number))
-        (cur-status (cond
-                     ((string= cur-mark elmo-msgdb-important-mark)
-                      'important)
-                     ((member cur-mark (elmo-msgdb-answered-marks))
-                      'answered)
-                     ((not (member cur-mark (elmo-msgdb-unread-marks)))
-                      'read)))
-        (cur-cached (not (member cur-mark (elmo-msgdb-uncached-marks))))
+        (cur-flag (cond
+                   ((string= cur-mark elmo-msgdb-important-mark)
+                    'important)
+                   ((member cur-mark (elmo-msgdb-answered-marks))
+                    'answered)
+                   ((not (member cur-mark (elmo-msgdb-unread-marks)))
+                    'read)))
+        (cur-cached (elmo-file-cache-exists-p
+                     (elmo-msgdb-get-field msgdb number 'message-id)))
         mark-modified)
-    (case status
+    (case flag
       (read
-       (case cur-status
+       (case cur-flag
         ((read important answered))
         (t (elmo-msgdb-set-mark msgdb number
                                 (if (and use-cache (not cur-cached))
                                     elmo-msgdb-read-uncached-mark))
            (setq mark-modified t))))
       (important
-       (unless (eq cur-status 'important)
+       (unless (eq cur-flag 'important)
         (elmo-msgdb-set-mark msgdb number elmo-msgdb-important-mark)
         (setq mark-modified t)))
       (answered
-       (unless (or (eq cur-status 'answered) (eq cur-status 'important))
+       (unless (or (eq cur-flag 'answered) (eq cur-flag 'important))
         (elmo-msgdb-set-mark msgdb number
                              (if cur-cached
-                                 (if use-cache
-                                     elmo-msgdb-answered-cached-mark
-                                   elmo-msgdb-answered-uncached-mark)
+                                 elmo-msgdb-answered-cached-mark
                                elmo-msgdb-answered-uncached-mark)))
        (setq mark-modified t)))
     (if mark-modified (elmo-folder-set-mark-modified-internal folder t))))
 
-(defun elmo-msgdb-unset-status (msgdb folder number status)
-  "Unset message status.
+(defun elmo-msgdb-unset-flag (msgdb folder number flag)
+  "Unset message flag.
 MSGDB is the ELMO msgdb.
 FOLDER is a ELMO folder structure.
-NUMBER is a message number to be set status.
-STATUS is a symbol which is one of the following:
+NUMBER is a message number to be set flag.
+FLAG is a symbol which is one of the following:
 `read'      ... Messages which are already read.
 `important' ... Messages which are marked as important.
 `answered'  ... Messages which are marked as answered."
   (let* ((cur-mark (elmo-msgdb-get-mark msgdb number))
         (use-cache (elmo-message-use-cache-p folder number))
-        (cur-status (cond
-                     ((string= cur-mark elmo-msgdb-important-mark)
-                      'important)
-                     ((member cur-mark (elmo-msgdb-answered-marks))
-                      'answered)
-                     ((not (member cur-mark (elmo-msgdb-unread-marks)))
-                      'read)))
-        (cur-cached (not (member cur-mark (elmo-msgdb-uncached-marks))))
+        (cur-flag (cond
+                   ((string= cur-mark elmo-msgdb-important-mark)
+                    'important)
+                   ((member cur-mark (elmo-msgdb-answered-marks))
+                    'answered)
+                   ((not (member cur-mark (elmo-msgdb-unread-marks)))
+                    'read)))
+        (cur-cached (elmo-file-cache-exists-p
+                     (elmo-msgdb-get-field msgdb number 'message-id)))
         mark-modified)
-    (case status
+    (case flag
       (read
-       (when (eq cur-status 'read)
+       (when (eq cur-flag 'read)
         (elmo-msgdb-set-mark msgdb number
-                             (if (and cur-cached use-cache)
+                             (if cur-cached
                                  elmo-msgdb-unread-cached-mark
                                elmo-msgdb-unread-uncached-mark))
         (setq mark-modified t)))
       (important
-       (when (eq cur-status 'important)
+       (when (eq cur-flag 'important)
         (elmo-msgdb-set-mark msgdb number nil)
         (setq mark-modified t)))
       (answered
-       (when (eq cur-status 'answered)
+       (when (eq cur-flag 'answered)
         (elmo-msgdb-set-mark msgdb number
-                             (if (and cur-cached (not use-cache))
+                             (if (and use-cache (not cur-cached))
                                  elmo-msgdb-read-uncached-mark))
         (setq mark-modified t))))
     (if mark-modified (elmo-folder-set-mark-modified-internal folder t))))
@@ -593,8 +599,8 @@ header separator."
        (setcar (cdr entity) after))
       (setq mark-alist (cdr mark-alist)))))
 
-(defsubst elmo-msgdb-mark (status cached)
-  (case status
+(defsubst elmo-msgdb-mark (flag cached)
+  (case flag
     (unread
      (if cached
         elmo-msgdb-unread-cached-mark
@@ -636,12 +642,17 @@ header separator."
                                   entity)
                                  numbers)))
                        (string-to-int (elmo-filter-value condition)))))
-       ((string= key "mark")
+       ((string= key "flag")
        (setq result
              (cond
               ((string= (elmo-filter-value condition) "any")
                (not (or (null mark)
                         (string= mark elmo-msgdb-read-uncached-mark))))
+              ((string= (elmo-filter-value condition) "digest")
+               (not (or (null mark)
+                        (string= mark elmo-msgdb-read-uncached-mark)
+                        (string= mark elmo-msgdb-answered-cached-mark)
+                        (string= mark elmo-msgdb-answered-uncached-mark))))
 ;;       (member mark (append (elmo-msgdb-answered-marks)
 ;;                            (list elmo-msgdb-important-mark)
 ;;                            (elmo-msgdb-unread-marks))))
@@ -987,12 +998,16 @@ Return CONDITION itself if no entity exists in msgdb."
     seen-list))
 
 (defun elmo-msgdb-get-message-id-from-buffer ()
-  (or (elmo-field-body "message-id")
+  (let ((msgid (elmo-field-body "message-id")))
+    (if msgid
+       (if (string-match "<\\(.+\\)>$" msgid)
+           msgid
+         (concat "<" msgid ">")) ; Invaild 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"))) ">")))
+                     (or (elmo-field-body "from") "nobody"))) ">"))))
 
 (defsubst elmo-msgdb-create-overview-from-buffer (number &optional size time)
   "Create overview entity from current buffer.
@@ -1170,6 +1185,41 @@ Return the updated INDEX."
     elmo-msgdb-location-filename
     dir) alist))
 
+(defun elmo-msgdb-list-flagged (msgdb flag)
+  (let ((case-fold-search nil)
+       mark-regexp matched)
+    (case flag
+      (new
+       (setq mark-regexp (regexp-quote elmo-msgdb-new-mark)))
+      (unread
+       (setq mark-regexp (elmo-regexp-opt (elmo-msgdb-unread-marks))))
+      (answered
+       (setq mark-regexp (elmo-regexp-opt (elmo-msgdb-unread-marks))))
+      (important
+       (setq mark-regexp (regexp-quote elmo-msgdb-important-mark)))
+      (read
+       (setq mark-regexp (elmo-regexp-opt (elmo-msgdb-unread-marks))))
+      (digest
+       (setq mark-regexp (elmo-regexp-opt
+                         (append (elmo-msgdb-unread-marks)
+                                 (list elmo-msgdb-important-mark)))))
+      (any
+       (setq mark-regexp (elmo-regexp-opt
+                         (append
+                          (elmo-msgdb-unread-marks)
+                          (elmo-msgdb-answered-marks)
+                          (list elmo-msgdb-important-mark))))))
+    (when mark-regexp
+      (if (eq flag 'read)
+         (dolist (number (elmo-msgdb-get-number-alist msgdb))
+           (unless (string-match mark-regexp (elmo-msgdb-get-mark
+                                              msgdb number))
+             (setq matched (cons number matched))))
+       (dolist (elem (elmo-msgdb-get-mark-alist msgdb))
+         (if (string-match mark-regexp (cadr elem))
+             (setq matched (cons (car elem) matched))))))
+    matched))
+
 (put 'elmo-msgdb-do-each-entity 'lisp-indent-function '1)
 (def-edebug-spec elmo-msgdb-do-each-entity
   ((symbolp form &rest form) &rest form))