+(defsubst elmo-folder-search-fast (folder condition numbers)
+ (when (and numbers
+ (vectorp condition)
+ (member (elmo-filter-key condition) '("first" "last")))
+ (let ((len (length numbers))
+ (lastp (string= (elmo-filter-key condition) "last"))
+ (value (string-to-number (elmo-filter-value condition))))
+ (when (eq (elmo-filter-type condition) 'unmatch)
+ (setq lastp (not lastp)
+ value (- len value)))
+ (if lastp
+ (nthcdr (max (- len value) 0) numbers)
+ (when (> value 0)
+ (let ((last (nthcdr (1- value) numbers)))
+ (when last
+ (setcdr last nil))
+ numbers))))))
+
+(luna-define-method elmo-folder-search ((folder elmo-folder)
+ condition
+ &optional numbers)
+ (let ((numbers (or numbers (elmo-folder-list-messages folder))))
+ (or (elmo-folder-search-fast folder condition numbers)
+ (let ((msgdb (elmo-folder-msgdb folder))
+ (len (length numbers))
+ matched)
+ (when (> len elmo-display-progress-threshold)
+ (elmo-progress-set 'elmo-folder-search len "Searching..."))
+ (unwind-protect
+ (dolist (number numbers)
+ (let ((entity (elmo-msgdb-overview-get-entity number msgdb))
+ result)
+ (if entity
+ (setq result (elmo-msgdb-match-condition
+ condition
+ entity
+ numbers))
+ (setq result condition))
+ (when (elmo-filter-condition-p result)
+ (setq result (elmo-message-match-condition
+ folder
+ number
+ condition
+ numbers)))
+ (when result
+ (setq matched (cons number matched))))
+ (elmo-progress-notify 'elmo-folder-search))
+ (elmo-progress-clear 'elmo-folder-search))
+ (nreverse matched)))))
+
+(luna-define-method elmo-message-match-condition ((folder elmo-folder)
+ number condition
+ numbers)
+ (let ((filename (cond
+ ((elmo-message-file-name folder number))
+ ((let* ((cache (elmo-file-cache-get
+ (elmo-message-field folder number
+ 'message-id)))
+ (cache-path (elmo-file-cache-path cache)))
+ (when (and cache-path
+ (not (elmo-cache-path-section-p cache-path)))
+ cache-path))))))
+ (when (and filename
+ (file-readable-p filename))
+ (with-temp-buffer
+ (insert-file-contents-as-binary filename)
+ (elmo-set-buffer-multibyte default-enable-multibyte-characters)
+ ;; Should consider charset?
+ (decode-mime-charset-region (point-min) (point-max) elmo-mime-charset)
+ (elmo-buffer-field-condition-match condition number numbers)))))
+