Whitespace cleanups.
[platform/upstream/evolution-data-server.git] / camel / camel-folder-search.c
index c8a6f4e..c72acc8 100644 (file)
@@ -20,8 +20,8 @@
  */
 
 /* This is a helper class for folders to implement the search function.
  It implements enough to do basic searches on folders that can provide
  an in-memory summary and a body index. */
* It implements enough to do basic searches on folders that can provide
* an in-memory summary and a body index. */
 
 #ifdef HAVE_CONFIG_H
 #include <config.h>
        ((obj), CAMEL_TYPE_FOLDER_SEARCH, CamelFolderSearchPrivate))
 
 struct _CamelFolderSearchPrivate {
+       GCancellable *cancellable;
        GError **error;
 
        CamelFolderThread *threads;
        GHashTable *threads_hash;
 };
 
-static ESExpResult *search_not (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search);
-
-static ESExpResult *search_header_contains (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search);
-static ESExpResult *search_header_matches (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search);
-static ESExpResult *search_header_starts_with (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search);
-static ESExpResult *search_header_ends_with (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search);
-static ESExpResult *search_header_exists (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search);
-static ESExpResult *search_header_soundex (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search);
-static ESExpResult *search_header_regex (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search);
-static ESExpResult *search_header_full_regex (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search);
-static ESExpResult *search_match_all (struct _ESExp *f, gint argc, struct _ESExpTerm **argv, CamelFolderSearch *search);
-static ESExpResult *search_match_threads (struct _ESExp *f, gint argc, struct _ESExpTerm **argv, CamelFolderSearch *s);
-static ESExpResult *search_body_contains (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search);
-static ESExpResult *search_body_regex (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search);
-static ESExpResult *search_user_flag (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s);
-static ESExpResult *search_user_tag (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s);
-static ESExpResult *search_system_flag (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s);
-static ESExpResult *search_get_sent_date (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s);
-static ESExpResult *search_get_received_date (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s);
-static ESExpResult *search_get_current_date (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s);
-static ESExpResult *search_get_size (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s);
-static ESExpResult *search_uid (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s);
-static ESExpResult *search_message_location (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s);
-
-static ESExpResult *search_dummy (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search);
+static CamelSExpResult *search_not (struct _CamelSExp *f, gint argc, struct _CamelSExpResult **argv, CamelFolderSearch *search);
+
+static CamelSExpResult *search_header_contains (struct _CamelSExp *f, gint argc, struct _CamelSExpResult **argv, CamelFolderSearch *search);
+static CamelSExpResult *search_header_matches (struct _CamelSExp *f, gint argc, struct _CamelSExpResult **argv, CamelFolderSearch *search);
+static CamelSExpResult *search_header_starts_with (struct _CamelSExp *f, gint argc, struct _CamelSExpResult **argv, CamelFolderSearch *search);
+static CamelSExpResult *search_header_ends_with (struct _CamelSExp *f, gint argc, struct _CamelSExpResult **argv, CamelFolderSearch *search);
+static CamelSExpResult *search_header_exists (struct _CamelSExp *f, gint argc, struct _CamelSExpResult **argv, CamelFolderSearch *search);
+static CamelSExpResult *search_header_soundex (struct _CamelSExp *f, gint argc, struct _CamelSExpResult **argv, CamelFolderSearch *search);
+static CamelSExpResult *search_header_regex (struct _CamelSExp *f, gint argc, struct _CamelSExpResult **argv, CamelFolderSearch *search);
+static CamelSExpResult *search_header_full_regex (struct _CamelSExp *f, gint argc, struct _CamelSExpResult **argv, CamelFolderSearch *search);
+static CamelSExpResult *search_match_all (struct _CamelSExp *f, gint argc, struct _CamelSExpTerm **argv, CamelFolderSearch *search);
+static CamelSExpResult *search_match_threads (struct _CamelSExp *f, gint argc, struct _CamelSExpTerm **argv, CamelFolderSearch *s);
+static CamelSExpResult *search_body_contains (struct _CamelSExp *f, gint argc, struct _CamelSExpResult **argv, CamelFolderSearch *search);
+static CamelSExpResult *search_body_regex (struct _CamelSExp *f, gint argc, struct _CamelSExpResult **argv, CamelFolderSearch *search);
+static CamelSExpResult *search_user_flag (struct _CamelSExp *f, gint argc, struct _CamelSExpResult **argv, CamelFolderSearch *s);
+static CamelSExpResult *search_user_tag (struct _CamelSExp *f, gint argc, struct _CamelSExpResult **argv, CamelFolderSearch *s);
+static CamelSExpResult *search_system_flag (struct _CamelSExp *f, gint argc, struct _CamelSExpResult **argv, CamelFolderSearch *s);
+static CamelSExpResult *search_get_sent_date (struct _CamelSExp *f, gint argc, struct _CamelSExpResult **argv, CamelFolderSearch *s);
+static CamelSExpResult *search_get_received_date (struct _CamelSExp *f, gint argc, struct _CamelSExpResult **argv, CamelFolderSearch *s);
+static CamelSExpResult *search_get_current_date (struct _CamelSExp *f, gint argc, struct _CamelSExpResult **argv, CamelFolderSearch *s);
+static CamelSExpResult *search_get_relative_months (struct _CamelSExp *f, gint argc, struct _CamelSExpResult **argv, CamelFolderSearch *s);
+static CamelSExpResult *search_get_size (struct _CamelSExp *f, gint argc, struct _CamelSExpResult **argv, CamelFolderSearch *s);
+static CamelSExpResult *search_uid (struct _CamelSExp *f, gint argc, struct _CamelSExpResult **argv, CamelFolderSearch *s);
+static CamelSExpResult *search_message_location (struct _CamelSExp *f, gint argc, struct _CamelSExpResult **argv, CamelFolderSearch *s);
+
+static CamelSExpResult *search_dummy (struct _CamelSExp *f, gint argc, struct _CamelSExpResult **argv, CamelFolderSearch *search);
 
 static gint read_uid_callback (gpointer  ref, gint ncol, gchar ** cols, gchar **name);
 
@@ -104,7 +106,7 @@ folder_search_dispose (GObject *object)
        CamelFolderSearch *search = CAMEL_FOLDER_SEARCH (object);
 
        if (search->sexp != NULL) {
-               e_sexp_unref (search->sexp);
+               g_object_unref (search->sexp);
                search->sexp = NULL;
        }
 
@@ -153,6 +155,7 @@ camel_folder_search_class_init (CamelFolderSearchClass *class)
        class->get_sent_date = search_get_sent_date;
        class->get_received_date = search_get_received_date;
        class->get_current_date = search_get_current_date;
+       class->get_relative_months = search_get_relative_months;
        class->get_size = search_get_size;
        class->uid = search_uid;
        class->message_location = search_message_location;
@@ -162,7 +165,7 @@ static void
 camel_folder_search_init (CamelFolderSearch *search)
 {
        search->priv = CAMEL_FOLDER_SEARCH_GET_PRIVATE (search);
-       search->sexp = e_sexp_new ();
+       search->sexp = camel_sexp_new ();
 }
 
 static struct {
@@ -199,6 +202,7 @@ static struct {
        { "get-sent-date", G_STRUCT_OFFSET(CamelFolderSearchClass, get_sent_date), 1 },
        { "get-received-date", G_STRUCT_OFFSET(CamelFolderSearchClass, get_received_date), 1 },
        { "get-current-date", G_STRUCT_OFFSET(CamelFolderSearchClass, get_current_date), 1 },
+       { "get-relative-months", G_STRUCT_OFFSET(CamelFolderSearchClass, get_relative_months), 1 },
        { "get-size", G_STRUCT_OFFSET(CamelFolderSearchClass, get_size), 1 },
        { "uid", G_STRUCT_OFFSET(CamelFolderSearchClass, uid), 1 },
        { "message-location", G_STRUCT_OFFSET(CamelFolderSearchClass, message_location), 1 },
@@ -215,16 +219,16 @@ camel_folder_search_construct (CamelFolderSearch *search)
        for (i = 0; i < G_N_ELEMENTS (builtins); i++) {
                gpointer func;
                /* c is sure messy sometimes */
-               func = *((gpointer *)(((gchar *)class)+builtins[i].offset));
+               func = *((gpointer *)(((gchar *) class) + builtins[i].offset));
                if (func == NULL && builtins[i].flags&1) {
                        g_warning("Search class doesn't implement '%s' method: %s", builtins[i].name, G_OBJECT_TYPE_NAME (search));
-                       func = (gpointer)search_dummy;
+                       func = (gpointer) search_dummy;
                }
                if (func != NULL) {
                        if (builtins[i].flags&2) {
-                               e_sexp_add_ifunction (search->sexp, 0, builtins[i].name, (ESExpIFunc *)func, search);
+                               camel_sexp_add_ifunction (search->sexp, 0, builtins[i].name, (CamelSExpIFunc) func, search);
                        } else {
-                               e_sexp_add_function (search->sexp, 0, builtins[i].name, (ESExpFunc *)func, search);
+                               camel_sexp_add_function (search->sexp, 0, builtins[i].name, (CamelSExpFunc) func, search);
                        }
                }
        }
@@ -263,8 +267,12 @@ camel_folder_search_new (void)
  * available.  Or for use by subclasses.
  **/
 void
-camel_folder_search_set_folder (CamelFolderSearch *search, CamelFolder *folder)
+camel_folder_search_set_folder (CamelFolderSearch *search,
+                                CamelFolder *folder)
 {
+       g_return_if_fail (CAMEL_IS_FOLDER_SEARCH (search));
+       g_return_if_fail (CAMEL_IS_FOLDER (folder));
+
        search->folder = folder;
 }
 
@@ -279,15 +287,18 @@ camel_folder_search_set_folder (CamelFolderSearch *search, CamelFolder *folder)
  * for searching headers and for the match-all operator.
  **/
 void
-camel_folder_search_set_summary (CamelFolderSearch *search, GPtrArray *summary)
+camel_folder_search_set_summary (CamelFolderSearch *search,
+                                 GPtrArray *summary)
 {
+       g_return_if_fail (CAMEL_IS_FOLDER_SEARCH (search));
+
        search->summary = summary;
 }
 
 /**
  * camel_folder_search_set_body_index:
  * @search:
- * @index:
+ * @body_index:
  *
  * Set the index representing the contents of all messages
  * in this folder.  If this is not set, then the folder implementation
@@ -295,121 +306,27 @@ camel_folder_search_set_summary (CamelFolderSearch *search, GPtrArray *summary)
  * body-contains function.
  **/
 void
-camel_folder_search_set_body_index (CamelFolderSearch *search, CamelIndex *index)
-{
-       if (search->body_index)
-               g_object_unref (search->body_index);
-       search->body_index = index;
-       if (index)
-               g_object_ref (index);
-}
-
-/**
- * camel_folder_search_execute_expression:
- * @search:
- * @expr:
- * @error: return location for a #GError, or %NULL
- *
- * Execute the search expression @expr, returning an array of
- * all matches as a GPtrArray of uid's of matching messages.
- *
- * Note that any settings such as set_body_index(), set_folder(),
- * and so on are reset to #NULL once the search has completed.
- *
- * TODO: The interface should probably return summary items instead
- * (since they are much more useful to any client).
- *
- * Returns: A GPtrArray of strings of all matching messages.
- * This must only be freed by camel_folder_search_free_result.
- **/
-GPtrArray *
-camel_folder_search_execute_expression (CamelFolderSearch *search,
-                                        const gchar *expr,
-                                        GError **error)
+camel_folder_search_set_body_index (CamelFolderSearch *search,
+                                    CamelIndex *body_index)
 {
-       ESExpResult *r;
-       GPtrArray *matches;
-       gint i;
-       GHashTable *results;
-       CamelFolderSearchPrivate *p = search->priv;
-
-       p->error = error;
-
-       /* only re-parse if the search has changed */
-       if (search->last_search == NULL
-           || strcmp (search->last_search, expr)) {
-               e_sexp_input_text (search->sexp, expr, strlen (expr));
-               if (e_sexp_parse (search->sexp) == -1) {
-                       g_set_error (
-                               error, CAMEL_ERROR, CAMEL_ERROR_GENERIC,
-                               _("Cannot parse search expression: %s:\n%s"),
-                               e_sexp_error (search->sexp), expr);
-                       return NULL;
-               }
+       g_return_if_fail (CAMEL_IS_FOLDER_SEARCH (search));
 
-               g_free (search->last_search);
-               search->last_search = g_strdup (expr);
+       if (body_index != NULL) {
+               g_return_if_fail (CAMEL_IS_INDEX (body_index));
+               g_object_ref (body_index);
        }
-       r = e_sexp_eval (search->sexp);
-       if (r == NULL) {
-               g_set_error (
-                       error, CAMEL_ERROR, CAMEL_ERROR_GENERIC,
-                       _("Error executing search expression: %s:\n%s"),
-                       e_sexp_error (search->sexp), expr);
-               return NULL;
-       }
-
-       matches = g_ptr_array_new ();
-
-       /* now create a folder summary to return?? */
-       if (r->type == ESEXP_RES_ARRAY_PTR) {
-               d(printf("got result ...\n"));
-               if (search->summary) {
-                       /* reorder result in summary order */
-                       results = g_hash_table_new (g_str_hash, g_str_equal);
-                       for (i=0;i<r->value.ptrarray->len;i++) {
-                               d(printf("adding match: %s\n", (gchar *)g_ptr_array_index(r->value.ptrarray, i)));
-                               g_hash_table_insert (results, g_ptr_array_index (r->value.ptrarray, i), GINT_TO_POINTER (1));
-                       }
-                       for (i=0;i<search->summary->len;i++) {
-                               gchar *uid = g_ptr_array_index (search->summary, i);
-                               if (g_hash_table_lookup (results, uid)) {
-                                       g_ptr_array_add (matches, (gpointer) camel_pstring_strdup (uid));
-                               }
-                       }
-                       g_hash_table_destroy (results);
-               } else {
-                       for (i=0;i<r->value.ptrarray->len;i++) {
-                               d(printf("adding match: %s\n", (gchar *)g_ptr_array_index(r->value.ptrarray, i)));
-                               g_ptr_array_add (matches, (gpointer) camel_pstring_strdup (g_ptr_array_index (r->value.ptrarray, i)));
-                       }
-               }
-       } else {
-               g_warning("Search returned an invalid result type");
-       }
-
-       e_sexp_result_free (search->sexp, r);
 
-       if (p->threads)
-               camel_folder_thread_messages_unref (p->threads);
-       if (p->threads_hash)
-               g_hash_table_destroy (p->threads_hash);
-
-       p->threads = NULL;
-       p->threads_hash = NULL;
-       search->folder = NULL;
-       search->summary = NULL;
-       search->current = NULL;
-       search->body_index = NULL;
+       if (search->body_index != NULL)
+               g_object_unref (search->body_index);
 
-       return matches;
+       search->body_index = body_index;
 }
 
 /**
  * camel_folder_search_count:
  * @search:
  * @expr:
- * @uids: to search against, NULL for all uid's.
+ * @cancellable: a #GCancellable
  * @error: return location for a #GError, or %NULL
  *
  * Run a search.  Search must have had Folder already set on it, and
@@ -423,9 +340,10 @@ camel_folder_search_execute_expression (CamelFolderSearch *search,
 guint32
 camel_folder_search_count (CamelFolderSearch *search,
                            const gchar *expr,
+                           GCancellable *cancellable,
                            GError **error)
 {
-       ESExpResult *r;
+       CamelSExpResult *r;
        GPtrArray *summary_set;
        gint i;
        CamelDB *cdb;
@@ -433,10 +351,15 @@ camel_folder_search_count (CamelFolderSearch *search,
        GHashTable *results;
        guint32 count = 0;
 
-       CamelFolderSearchPrivate *p = search->priv;
+       CamelFolderSearchPrivate *p;
+
+       g_return_val_if_fail (search != NULL, 0);
+
+       p = search->priv;
 
        g_assert (search->folder);
 
+       p->cancellable = cancellable;
        p->error = error;
 
        /* We route body-contains search and thread based search through memory and not via db. */
@@ -449,39 +372,39 @@ camel_folder_search_count (CamelFolderSearch *search,
                /* only re-parse if the search has changed */
                if (search->last_search == NULL
                    || strcmp (search->last_search, expr)) {
-                       e_sexp_input_text (search->sexp, expr, strlen (expr));
-                       if (e_sexp_parse (search->sexp) == -1) {
+                       camel_sexp_input_text (search->sexp, expr, strlen (expr));
+                       if (camel_sexp_parse (search->sexp) == -1) {
                                g_set_error (
                                        error, CAMEL_ERROR, CAMEL_ERROR_GENERIC,
                                        _("Cannot parse search expression: %s:\n%s"),
-                                       e_sexp_error (search->sexp), expr);
+                                       camel_sexp_error (search->sexp), expr);
                                goto fail;
                        }
 
                        g_free (search->last_search);
                        search->last_search = g_strdup (expr);
                }
-               r = e_sexp_eval (search->sexp);
+               r = camel_sexp_eval (search->sexp);
                if (r == NULL) {
                        g_set_error (
                                error, CAMEL_ERROR, CAMEL_ERROR_GENERIC,
                                _("Error executing search expression: %s:\n%s"),
-                               e_sexp_error (search->sexp), expr);
+                               camel_sexp_error (search->sexp), expr);
                        goto fail;
                }
 
                /* now create a folder summary to return?? */
-               if (r->type == ESEXP_RES_ARRAY_PTR) {
+               if (r->type == CAMEL_SEXP_RES_ARRAY_PTR) {
                        d(printf("got result\n"));
 
                        /* reorder result in summary order */
                        results = g_hash_table_new (g_str_hash, g_str_equal);
-                       for (i=0;i<r->value.ptrarray->len;i++) {
+                       for (i = 0; i < r->value.ptrarray->len; i++) {
                                d(printf("adding match: %s\n", (gchar *)g_ptr_array_index(r->value.ptrarray, i)));
                                g_hash_table_insert (results, g_ptr_array_index (r->value.ptrarray, i), GINT_TO_POINTER (1));
                        }
 
-                       for (i=0;i<summary_set->len;i++) {
+                       for (i = 0; i < summary_set->len; i++) {
                                gchar *uid = g_ptr_array_index (summary_set, i);
                                if (g_hash_table_lookup (results, uid))
                                        count++;
@@ -489,7 +412,7 @@ camel_folder_search_count (CamelFolderSearch *search,
                        g_hash_table_destroy (results);
                }
 
-               e_sexp_result_free (search->sexp, r);
+               camel_sexp_result_free (search->sexp, r);
 
        } else {
                CamelStore *parent_store;
@@ -538,6 +461,8 @@ fail:
        if (search->summary)
                camel_folder_free_summary (search->folder, search->summary);
 
+       p->cancellable = NULL;
+       p->error = NULL;
        p->threads = NULL;
        p->threads_hash = NULL;
        search->folder = NULL;
@@ -553,7 +478,17 @@ static gboolean
 do_search_in_memory (const gchar *expr)
 {
        /* if the expression contains any of these tokens, then perform a memory search, instead of the SQL one */
-       const gchar *in_memory_tokens[] = { "body-contains", "body-regex", "match-threads", "message-location", "header-soundex", "header-regex", "header-full-regex", "header-contains", NULL };
+       const gchar *in_memory_tokens[] = {
+               "body-contains",
+               "body-regex",
+               "match-threads",
+               "message-location",
+               "header-soundex",
+               "header-regex",
+               "header-full-regex",
+               "header-contains",
+               "header-has-words",
+               NULL };
        gint i;
 
        if (!expr)
@@ -572,6 +507,7 @@ do_search_in_memory (const gchar *expr)
  * @search:
  * @expr:
  * @uids: to search against, NULL for all uid's.
+ * @cancellable: a #GCancellable
  * @error: return location for a #GError, or %NULL
  *
  * Run a search.  Search must have had Folder already set on it, and
@@ -583,19 +519,25 @@ GPtrArray *
 camel_folder_search_search (CamelFolderSearch *search,
                             const gchar *expr,
                             GPtrArray *uids,
+                            GCancellable *cancellable,
                             GError **error)
 {
-       ESExpResult *r;
+       CamelSExpResult *r;
        GPtrArray *matches = NULL, *summary_set;
        gint i;
        CamelDB *cdb;
        gchar *sql_query, *tmp, *tmp1;
        GHashTable *results;
 
-       CamelFolderSearchPrivate *p = search->priv;
+       CamelFolderSearchPrivate *p;
+
+       g_return_val_if_fail (search != NULL, NULL);
+
+       p = search->priv;
 
        g_assert (search->folder);
 
+       p->cancellable = cancellable;
        p->error = error;
 
        /* We route body-contains / thread based search and uid search through memory and not via db. */
@@ -607,9 +549,9 @@ camel_folder_search_search (CamelFolderSearch *search,
                        GHashTable *uids_hash = g_hash_table_new (g_str_hash, g_str_equal);
 
                        summary_set = search->summary_set = g_ptr_array_new ();
-                       for (i=0;i<uids->len;i++)
+                       for (i = 0; i < uids->len; i++)
                                g_hash_table_insert (uids_hash, uids->pdata[i], uids->pdata[i]);
-                       for (i=0;i<search->summary->len;i++)
+                       for (i = 0; i < search->summary->len; i++)
                                if (g_hash_table_lookup (uids_hash, search->summary->pdata[i]))
                                        g_ptr_array_add (search->summary_set, search->summary->pdata[i]);
                        g_hash_table_destroy (uids_hash);
@@ -620,41 +562,41 @@ camel_folder_search_search (CamelFolderSearch *search,
                /* only re-parse if the search has changed */
                if (search->last_search == NULL
                    || strcmp (search->last_search, expr)) {
-                       e_sexp_input_text (search->sexp, expr, strlen (expr));
-                       if (e_sexp_parse (search->sexp) == -1) {
+                       camel_sexp_input_text (search->sexp, expr, strlen (expr));
+                       if (camel_sexp_parse (search->sexp) == -1) {
                                g_set_error (
                                        error, CAMEL_ERROR, CAMEL_ERROR_GENERIC,
                                        _("Cannot parse search expression: %s:\n%s"),
-                                       e_sexp_error (search->sexp), expr);
+                                       camel_sexp_error (search->sexp), expr);
                                goto fail;
                        }
 
                        g_free (search->last_search);
                        search->last_search = g_strdup (expr);
                }
-               r = e_sexp_eval (search->sexp);
+               r = camel_sexp_eval (search->sexp);
                if (r == NULL) {
                        g_set_error (
                                error, CAMEL_ERROR, CAMEL_ERROR_GENERIC,
                                _("Error executing search expression: %s:\n%s"),
-                               e_sexp_error (search->sexp), expr);
+                               camel_sexp_error (search->sexp), expr);
                        goto fail;
                }
 
                matches = g_ptr_array_new ();
 
                /* now create a folder summary to return?? */
-               if (r->type == ESEXP_RES_ARRAY_PTR) {
+               if (r->type == CAMEL_SEXP_RES_ARRAY_PTR) {
                        d(printf("got result\n"));
 
                        /* reorder result in summary order */
                        results = g_hash_table_new (g_str_hash, g_str_equal);
-                       for (i=0;i<r->value.ptrarray->len;i++) {
+                       for (i = 0; i < r->value.ptrarray->len; i++) {
                                d(printf("adding match: %s\n", (gchar *)g_ptr_array_index(r->value.ptrarray, i)));
                                g_hash_table_insert (results, g_ptr_array_index (r->value.ptrarray, i), GINT_TO_POINTER (1));
                        }
 
-                       for (i=0;i<summary_set->len;i++) {
+                       for (i = 0; i < summary_set->len; i++) {
                                gchar *uid = g_ptr_array_index (summary_set, i);
                                if (g_hash_table_lookup (results, uid))
                                        g_ptr_array_add (matches, (gpointer) camel_pstring_strdup (uid));
@@ -662,7 +604,7 @@ camel_folder_search_search (CamelFolderSearch *search,
                        g_hash_table_destroy (results);
                }
 
-               e_sexp_result_free (search->sexp, r);
+               camel_sexp_result_free (search->sexp, r);
 
        } else {
                CamelStore *parent_store;
@@ -715,6 +657,8 @@ fail:
        if (search->summary)
                camel_folder_free_summary (search->folder, search->summary);
 
+       p->cancellable = NULL;
+       p->error = NULL;
        p->threads = NULL;
        p->threads_hash = NULL;
        search->folder = NULL;
@@ -733,16 +677,19 @@ void camel_folder_search_free_result (CamelFolderSearch *search, GPtrArray *resu
 }
 
 /* dummy function, returns false always, or an empty match array */
-static ESExpResult *
-search_dummy (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
+static CamelSExpResult *
+search_dummy (struct _CamelSExp *f,
+              gint argc,
+              struct _CamelSExpResult **argv,
+              CamelFolderSearch *search)
 {
-       ESExpResult *r;
+       CamelSExpResult *r;
 
        if (search->current == NULL) {
-               r = e_sexp_result_new (f, ESEXP_RES_BOOL);
+               r = camel_sexp_result_new (f, CAMEL_SEXP_RES_BOOL);
                r->value.boolean = FALSE;
        } else {
-               r = e_sexp_result_new (f, ESEXP_RES_ARRAY_PTR);
+               r = camel_sexp_result_new (f, CAMEL_SEXP_RES_ARRAY_PTR);
                r->value.ptrarray = g_ptr_array_new ();
        }
 
@@ -750,18 +697,21 @@ search_dummy (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFold
 }
 
 /* impelemnt an 'array not', i.e. everything in the summary, not in the supplied array */
-static ESExpResult *
-search_not (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
+static CamelSExpResult *
+search_not (struct _CamelSExp *f,
+            gint argc,
+            struct _CamelSExpResult **argv,
+            CamelFolderSearch *search)
 {
-       ESExpResult *r;
+       CamelSExpResult *r;
        gint i;
 
-       if (argc>0) {
-               if (argv[0]->type == ESEXP_RES_ARRAY_PTR) {
+       if (argc > 0) {
+               if (argv[0]->type == CAMEL_SEXP_RES_ARRAY_PTR) {
                        GPtrArray *v = argv[0]->value.ptrarray;
                        const gchar *uid;
 
-                       r = e_sexp_result_new (f, ESEXP_RES_ARRAY_PTR);
+                       r = camel_sexp_result_new (f, CAMEL_SEXP_RES_ARRAY_PTR);
                        r->value.ptrarray = g_ptr_array_new ();
 
                        /* not against a single message?*/
@@ -769,13 +719,13 @@ search_not (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolder
                                gint found = FALSE;
 
                                uid = camel_message_info_uid (search->current);
-                               for (i=0;!found && i<v->len;i++) {
+                               for (i = 0; !found && i < v->len; i++) {
                                        if (strcmp (uid, v->pdata[i]) == 0)
                                                found = TRUE;
                                }
 
                                if (!found)
-                                       g_ptr_array_add (r->value.ptrarray, (gchar *)uid);
+                                       g_ptr_array_add (r->value.ptrarray, (gchar *) uid);
                        } else if (search->summary == NULL) {
                                g_warning("No summary set, 'not' against an array requires a summary");
                        } else {
@@ -784,13 +734,13 @@ search_not (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolder
                                gchar **s;
                                gchar **m;
 
-                               s = (gchar **)v->pdata;
-                               for (i=0;i<v->len;i++)
+                               s = (gchar **) v->pdata;
+                               for (i = 0; i < v->len; i++)
                                        g_hash_table_insert (have, s[i], s[i]);
 
-                               v = search->summary_set?search->summary_set:search->summary;
-                               m = (gchar **)v->pdata;
-                               for (i=0;i<v->len;i++) {
+                               v = search->summary_set ? search->summary_set : search->summary;
+                               m = (gchar **) v->pdata;
+                               for (i = 0; i < v->len; i++) {
                                        gchar *uid = m[i];
 
                                        if (g_hash_table_lookup (have, uid) == NULL)
@@ -801,29 +751,32 @@ search_not (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolder
                } else {
                        gint res = TRUE;
 
-                       if (argv[0]->type == ESEXP_RES_BOOL)
+                       if (argv[0]->type == CAMEL_SEXP_RES_BOOL)
                                res = !argv[0]->value.boolean;
 
-                       r = e_sexp_result_new (f, ESEXP_RES_BOOL);
+                       r = camel_sexp_result_new (f, CAMEL_SEXP_RES_BOOL);
                        r->value.boolean = res;
                }
        } else {
-               r = e_sexp_result_new (f, ESEXP_RES_BOOL);
+               r = camel_sexp_result_new (f, CAMEL_SEXP_RES_BOOL);
                r->value.boolean = TRUE;
        }
 
        return r;
 }
 
-static ESExpResult *
-search_match_all (struct _ESExp *f, gint argc, struct _ESExpTerm **argv, CamelFolderSearch *search)
+static CamelSExpResult *
+search_match_all (struct _CamelSExp *f,
+                  gint argc,
+                  struct _CamelSExpTerm **argv,
+                  CamelFolderSearch *search)
 {
        gint i;
-       ESExpResult *r, *r1;
+       CamelSExpResult *r, *r1;
        gchar *error_msg;
        GPtrArray *v;
 
-       if (argc>1) {
+       if (argc > 1) {
                g_warning("match-all only takes a single argument, other arguments ignored");
        }
 
@@ -831,27 +784,28 @@ search_match_all (struct _ESExp *f, gint argc, struct _ESExpTerm **argv, CamelFo
        if (search->current) {
                d(printf("matching against 1 message: %s\n", camel_message_info_subject(search->current)));
 
-               r = e_sexp_result_new (f, ESEXP_RES_BOOL);
+               r = camel_sexp_result_new (f, CAMEL_SEXP_RES_BOOL);
                r->value.boolean = FALSE;
 
-               if (argc>0) {
-                       r1 = e_sexp_term_eval (f, argv[0]);
-                       if (r1->type == ESEXP_RES_BOOL) {
+               if (argc > 0) {
+                       r1 = camel_sexp_term_eval (f, argv[0]);
+                       if (r1->type == CAMEL_SEXP_RES_BOOL) {
                                r->value.boolean = r1->value.boolean;
                        } else {
                                g_warning("invalid syntax, matches require a single bool result");
+                               /* Translators: The '%s' is an element type name, part of an expressing language */
                                error_msg = g_strdup_printf(_("(%s) requires a single bool result"), "match-all");
-                               e_sexp_fatal_error (f, error_msg);
+                               camel_sexp_fatal_error (f, error_msg);
                                g_free (error_msg);
                        }
-                       e_sexp_result_free (f, r1);
+                       camel_sexp_result_free (f, r1);
                } else {
                        r->value.boolean = TRUE;
                }
                return r;
        }
 
-       r = e_sexp_result_new (f, ESEXP_RES_ARRAY_PTR);
+       r = camel_sexp_result_new (f, CAMEL_SEXP_RES_ARRAY_PTR);
        r->value.ptrarray = g_ptr_array_new ();
 
        if (search->summary == NULL) {
@@ -861,34 +815,35 @@ search_match_all (struct _ESExp *f, gint argc, struct _ESExpTerm **argv, CamelFo
                return r;
        }
 
-       v = search->summary_set?search->summary_set:search->summary;
+       v = search->summary_set ? search->summary_set : search->summary;
 
        if (!CAMEL_IS_VEE_FOLDER (search->folder)) {
                camel_folder_summary_prepare_fetch_all (search->folder->summary, search->priv->error);
        }
 
-       for (i=0;i<v->len;i++) {
+       for (i = 0; i < v->len && !g_cancellable_is_cancelled (search->priv->cancellable); i++) {
                const gchar *uid;
 
-               search->current = camel_folder_summary_uid (search->folder->summary, v->pdata[i]);
+               search->current = camel_folder_summary_get (search->folder->summary, v->pdata[i]);
                if (!search->current)
                        continue;
                uid = camel_message_info_uid (search->current);
 
-               if (argc>0) {
-                       r1 = e_sexp_term_eval (f, argv[0]);
-                       if (r1->type == ESEXP_RES_BOOL) {
+               if (argc > 0) {
+                       r1 = camel_sexp_term_eval (f, argv[0]);
+                       if (r1->type == CAMEL_SEXP_RES_BOOL) {
                                if (r1->value.boolean)
-                                       g_ptr_array_add (r->value.ptrarray, (gchar *)uid);
+                                       g_ptr_array_add (r->value.ptrarray, (gchar *) uid);
                        } else {
                                g_warning("invalid syntax, matches require a single bool result");
+                               /* Translators: The '%s' is an element type name, part of an expressing language */
                                error_msg = g_strdup_printf(_("(%s) requires a single bool result"), "match-all");
-                               e_sexp_fatal_error (f, error_msg);
+                               camel_sexp_fatal_error (f, error_msg);
                                g_free (error_msg);
                        }
-                       e_sexp_result_free (f, r1);
+                       camel_sexp_result_free (f, r1);
                } else {
-                       g_ptr_array_add (r->value.ptrarray, (gchar *)uid);
+                       g_ptr_array_add (r->value.ptrarray, (gchar *) uid);
                }
                camel_message_info_free (search->current);
        }
@@ -897,10 +852,11 @@ search_match_all (struct _ESExp *f, gint argc, struct _ESExpTerm **argv, CamelFo
 }
 
 static void
-fill_thread_table (struct _CamelFolderThreadNode *root, GHashTable *id_hash)
+fill_thread_table (struct _CamelFolderThreadNode *root,
+                   GHashTable *id_hash)
 {
        while (root) {
-               g_hash_table_insert (id_hash, (gchar *)camel_message_info_uid (root->message), root);
+               g_hash_table_insert (id_hash, (gchar *) camel_message_info_uid (root->message), root);
                if (root->child)
                        fill_thread_table (root->child, id_hash);
                root = root->next;
@@ -908,10 +864,11 @@ fill_thread_table (struct _CamelFolderThreadNode *root, GHashTable *id_hash)
 }
 
 static void
-add_thread_results (struct _CamelFolderThreadNode *root, GHashTable *result_hash)
+add_thread_results (struct _CamelFolderThreadNode *root,
+                    GHashTable *result_hash)
 {
        while (root) {
-               g_hash_table_insert (result_hash, (gchar *)camel_message_info_uid (root->message), GINT_TO_POINTER (1));
+               g_hash_table_insert (result_hash, (gchar *) camel_message_info_uid (root->message), GINT_TO_POINTER (1));
                if (root->child)
                        add_thread_results (root->child, result_hash);
                root = root->next;
@@ -919,15 +876,20 @@ add_thread_results (struct _CamelFolderThreadNode *root, GHashTable *result_hash
 }
 
 static void
-add_results (gchar *uid, gpointer dummy, GPtrArray *result)
+add_results (gchar *uid,
+             gpointer dummy,
+             GPtrArray *result)
 {
        g_ptr_array_add (result, uid);
 }
 
-static ESExpResult *
-search_match_threads (struct _ESExp *f, gint argc, struct _ESExpTerm **argv, CamelFolderSearch *search)
+static CamelSExpResult *
+search_match_threads (struct _CamelSExp *f,
+                      gint argc,
+                      struct _CamelSExpTerm **argv,
+                      CamelFolderSearch *search)
 {
-       ESExpResult *r;
+       CamelSExpResult *r;
        CamelFolderSearchPrivate *p = search->priv;
        gint i, type;
        GHashTable *results;
@@ -935,21 +897,24 @@ search_match_threads (struct _ESExp *f, gint argc, struct _ESExpTerm **argv, Cam
 
        /* not supported in match-all */
        if (search->current) {
+               /* Translators: Each '%s' is an element type name, part of an expressing language */
                error_msg = g_strdup_printf(_("(%s) not allowed inside %s"), "match-threads", "match-all");
-               e_sexp_fatal_error (f, error_msg);
+               camel_sexp_fatal_error (f, error_msg);
                g_free (error_msg);
        }
 
        if (argc == 0) {
+               /* Translators: The '%s' is an element type name, part of an expressing language */
                error_msg = g_strdup_printf(_("(%s) requires a match type string"), "match-threads");
-               e_sexp_fatal_error (f, error_msg);
+               camel_sexp_fatal_error (f, error_msg);
                g_free (error_msg);
        }
 
-       r = e_sexp_term_eval (f, argv[0]);
-       if (r->type != ESEXP_RES_STRING) {
+       r = camel_sexp_term_eval (f, argv[0]);
+       if (r->type != CAMEL_SEXP_RES_STRING) {
+               /* Translators: The '%s' is an element type name, part of an expressing language */
                error_msg = g_strdup_printf(_("(%s) requires a match type string"), "match-threads");
-               e_sexp_fatal_error (f, error_msg);
+               camel_sexp_fatal_error (f, error_msg);
                g_free (error_msg);
        }
 
@@ -964,19 +929,20 @@ search_match_threads (struct _ESExp *f, gint argc, struct _ESExpTerm **argv, Cam
                type = 3;
        else if (!strcmp(r->value.string, "single"))
                type = 4;
-       e_sexp_result_free (f, r);
+       camel_sexp_result_free (f, r);
 
        /* behave as (begin does */
        r = NULL;
-       for (i=1;i<argc;i++) {
+       for (i = 1; i < argc; i++) {
                if (r)
-                       e_sexp_result_free (f, r);
-               r = e_sexp_term_eval (f, argv[i]);
+                       camel_sexp_result_free (f, r);
+               r = camel_sexp_term_eval (f, argv[i]);
        }
 
-       if (r == NULL || r->type != ESEXP_RES_ARRAY_PTR) {
+       if (r == NULL || r->type != CAMEL_SEXP_RES_ARRAY_PTR) {
+               /* Translators: The '%s' is an element type name, part of an expressing language */
                error_msg = g_strdup_printf(_("(%s) expects an array result"), "match-threads");
-               e_sexp_fatal_error (f, error_msg);
+               camel_sexp_fatal_error (f, error_msg);
                g_free (error_msg);
        }
 
@@ -984,8 +950,9 @@ search_match_threads (struct _ESExp *f, gint argc, struct _ESExpTerm **argv, Cam
                return r;
 
        if (search->folder == NULL) {
+               /* Translators: The '%s' is an element type name, part of an expressing language */
                error_msg = g_strdup_printf(_("(%s) requires the folder set"), "match-threads");
-               e_sexp_fatal_error (f, error_msg);
+               camel_sexp_fatal_error (f, error_msg);
                g_free (error_msg);
        }
 
@@ -998,42 +965,42 @@ search_match_threads (struct _ESExp *f, gint argc, struct _ESExpTerm **argv, Cam
        }
 
        results = g_hash_table_new (g_str_hash, g_str_equal);
-       for (i=0;i<r->value.ptrarray->len;i++) {
+       for (i = 0; i < r->value.ptrarray->len; i++) {
                struct _CamelFolderThreadNode *node, *scan;
 
                if (type != 4)
                        g_hash_table_insert (results, g_ptr_array_index (r->value.ptrarray, i), GINT_TO_POINTER (1));
 
-               node = g_hash_table_lookup (p->threads_hash, (gchar *)g_ptr_array_index (r->value.ptrarray, i));
+               node = g_hash_table_lookup (p->threads_hash, (gchar *) g_ptr_array_index (r->value.ptrarray, i));
                if (node == NULL) /* this shouldn't happen but why cry over spilt milk */
                        continue;
 
                /* select messages in thread according to search criteria */
                if (type == 4) {
                        if (node->child == NULL && node->parent == NULL)
-                               g_hash_table_insert (results, (gchar *)camel_message_info_uid (node->message), GINT_TO_POINTER (1));
+                               g_hash_table_insert (results, (gchar *) camel_message_info_uid (node->message), GINT_TO_POINTER (1));
                } else {
                        if (type == 3) {
                                scan = node;
                                while (scan && scan->parent) {
                                        scan = scan->parent;
-                                       g_hash_table_insert (results, (gchar *)camel_message_info_uid (scan->message), GINT_TO_POINTER (1));
+                                       g_hash_table_insert (results, (gchar *) camel_message_info_uid (scan->message), GINT_TO_POINTER (1));
                                }
                        } else if (type == 1) {
                                while (node && node->parent)
                                        node = node->parent;
                        }
-                       g_hash_table_insert (results, (gchar *)camel_message_info_uid (node->message), GINT_TO_POINTER (1));
+                       g_hash_table_insert (results, (gchar *) camel_message_info_uid (node->message), GINT_TO_POINTER (1));
                        if (node->child)
                                add_thread_results (node->child, results);
                }
        }
-       e_sexp_result_free (f, r);
+       camel_sexp_result_free (f, r);
 
-       r = e_sexp_result_new (f, ESEXP_RES_ARRAY_PTR);
+       r = camel_sexp_result_new (f, CAMEL_SEXP_RES_ARRAY_PTR);
        r->value.ptrarray = g_ptr_array_new ();
 
-       g_hash_table_foreach (results, (GHFunc)add_results, r->value.ptrarray);
+       g_hash_table_foreach (results, (GHFunc) add_results, r->value.ptrarray);
        g_hash_table_destroy (results);
 
        return r;
@@ -1045,22 +1012,25 @@ get_current_message (CamelFolderSearch *search)
        if (!search || !search->folder || !search->current)
                return NULL;
 
-       /* FIXME Pass a GCancellable */
        return camel_folder_get_message_sync (
-               search->folder, search->current->uid, NULL, NULL);
+               search->folder, search->current->uid, search->priv->cancellable, NULL);
 }
 
-static ESExpResult *
-check_header (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search, camel_search_match_t how)
+static CamelSExpResult *
+check_header (struct _CamelSExp *f,
+              gint argc,
+              struct _CamelSExpResult **argv,
+              CamelFolderSearch *search,
+              camel_search_match_t how)
 {
-       ESExpResult *r;
+       CamelSExpResult *r;
        gint truth = FALSE;
 
        r(printf("executing check-header %d\n", how));
 
        /* are we inside a match-all? */
-       if (search->current && argc>1
-           && argv[0]->type == ESEXP_RES_STRING) {
+       if (search->current && argc > 1
+           && argv[0]->type == CAMEL_SEXP_RES_STRING) {
                gchar *headername;
                const gchar *header = NULL, *charset = NULL;
                gchar strbuf[32];
@@ -1106,17 +1076,17 @@ check_header (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFold
                        header = "";
 
                /* performs an OR of all words */
-               for (i=1;i<argc && !truth;i++) {
-                       if (argv[i]->type == ESEXP_RES_STRING) {
+               for (i = 1; i < argc && !truth; i++) {
+                       if (argv[i]->type == CAMEL_SEXP_RES_STRING) {
                                if (argv[i]->value.string[0] == 0) {
                                        truth = TRUE;
                                } else if (how == CAMEL_SEARCH_MATCH_CONTAINS) {
                                        /* doesn't make sense to split words on anything but contains i.e. we can't have an ending match different words */
                                        words = camel_search_words_split ((const guchar *) argv[i]->value.string);
                                        truth = TRUE;
-                                       for (j=0;j<words->len && truth;j++) {
+                                       for (j = 0; j < words->len && truth; j++) {
                                                if (message) {
-                                                       for (raw_header = ((CamelMimePart *)message)->headers; raw_header; raw_header = raw_header->next) {
+                                                       for (raw_header = ((CamelMimePart *) message)->headers; raw_header; raw_header = raw_header->next) {
                                                                if (!g_ascii_strcasecmp (raw_header->name, headername)) {
                                                                        if (camel_search_header_match (raw_header->value, words->words[j]->word, how, type, charset))
                                                                                break;;
@@ -1130,7 +1100,7 @@ check_header (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFold
                                        camel_search_words_free (words);
                                } else {
                                        if (message) {
-                                               for (raw_header = ((CamelMimePart *)message)->headers; raw_header && !truth; raw_header = raw_header->next) {
+                                               for (raw_header = ((CamelMimePart *) message)->headers; raw_header && !truth; raw_header = raw_header->next) {
                                                        if (!g_ascii_strcasecmp (raw_header->name, headername)) {
                                                                truth = camel_search_header_match (raw_header->value, argv[i]->value.string, how, type, charset);
                                                        }
@@ -1146,7 +1116,7 @@ check_header (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFold
        }
        /* TODO: else, find all matches */
 
-       r = e_sexp_result_new (f, ESEXP_RES_BOOL);
+       r = camel_sexp_result_new (f, CAMEL_SEXP_RES_BOOL);
        r->value.boolean = truth;
 
        return r;
@@ -1160,60 +1130,81 @@ printf("%s\t", node);
 }
 */
 
-static ESExpResult *
-search_header_contains (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
+static CamelSExpResult *
+search_header_contains (struct _CamelSExp *f,
+                        gint argc,
+                        struct _CamelSExpResult **argv,
+                        CamelFolderSearch *search)
 {
        return check_header (f, argc, argv, search, CAMEL_SEARCH_MATCH_CONTAINS);
 }
 
-static ESExpResult *
-search_header_matches (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
+static CamelSExpResult *
+search_header_matches (struct _CamelSExp *f,
+                       gint argc,
+                       struct _CamelSExpResult **argv,
+                       CamelFolderSearch *search)
 {
        return check_header (f, argc, argv, search, CAMEL_SEARCH_MATCH_EXACT);
 }
 
-static ESExpResult *
-search_header_starts_with (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
+static CamelSExpResult *
+search_header_starts_with (struct _CamelSExp *f,
+                           gint argc,
+                           struct _CamelSExpResult **argv,
+                           CamelFolderSearch *search)
 {
        return check_header (f, argc, argv, search, CAMEL_SEARCH_MATCH_STARTS);
 }
 
-static ESExpResult *
-search_header_ends_with (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
+static CamelSExpResult *
+search_header_ends_with (struct _CamelSExp *f,
+                         gint argc,
+                         struct _CamelSExpResult **argv,
+                         CamelFolderSearch *search)
 {
        return check_header (f, argc, argv, search, CAMEL_SEARCH_MATCH_ENDS);
 }
 
-static ESExpResult *
-search_header_exists (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
+static CamelSExpResult *
+search_header_exists (struct _CamelSExp *f,
+                      gint argc,
+                      struct _CamelSExpResult **argv,
+                      CamelFolderSearch *search)
 {
-       ESExpResult *r;
+       CamelSExpResult *r;
 
        r(printf ("executing header-exists\n"));
 
        if (search->current) {
-               r = e_sexp_result_new (f, ESEXP_RES_BOOL);
-               if (argc == 1 && argv[0]->type == ESEXP_RES_STRING)
+               r = camel_sexp_result_new (f, CAMEL_SEXP_RES_BOOL);
+               if (argc == 1 && argv[0]->type == CAMEL_SEXP_RES_STRING)
                        r->value.boolean = camel_medium_get_header (CAMEL_MEDIUM (search->current), argv[0]->value.string) != NULL;
 
        } else {
-               r = e_sexp_result_new (f, ESEXP_RES_ARRAY_PTR);
+               r = camel_sexp_result_new (f, CAMEL_SEXP_RES_ARRAY_PTR);
                r->value.ptrarray = g_ptr_array_new ();
        }
 
        return r;
 }
 
-static ESExpResult *
-search_header_soundex (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
+static CamelSExpResult *
+search_header_soundex (struct _CamelSExp *f,
+                       gint argc,
+                       struct _CamelSExpResult **argv,
+                       CamelFolderSearch *search)
 {
        return check_header (f, argc, argv, search, CAMEL_SEARCH_MATCH_SOUNDEX);
 }
 
-static ESExpResult *
-search_header_regex (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
+static CamelSExpResult *
+search_header_regex (struct _CamelSExp *f,
+                     gint argc,
+                     struct _CamelSExpResult **argv,
+                     CamelFolderSearch *search)
 {
-       ESExpResult *r;
+       CamelSExpResult *r;
        CamelMimeMessage *msg;
 
        msg = get_current_message (search);
@@ -1222,11 +1213,11 @@ search_header_regex (struct _ESExp *f, gint argc, struct _ESExpResult **argv, Ca
                regex_t pattern;
                const gchar *contents;
 
-               r = e_sexp_result_new (f, ESEXP_RES_BOOL);
+               r = camel_sexp_result_new (f, CAMEL_SEXP_RES_BOOL);
 
-               if (argc > 1 && argv[0]->type == ESEXP_RES_STRING
+               if (argc > 1 && argv[0]->type == CAMEL_SEXP_RES_STRING
                    && (contents = camel_medium_get_header (CAMEL_MEDIUM (msg), argv[0]->value.string))
-                   && camel_search_build_match_regex (&pattern, CAMEL_SEARCH_MATCH_REGEX|CAMEL_SEARCH_MATCH_ICASE, argc-1, argv+1, search->priv->error) == 0) {
+                   && camel_search_build_match_regex (&pattern, CAMEL_SEARCH_MATCH_REGEX | CAMEL_SEARCH_MATCH_ICASE, argc - 1, argv + 1, search->priv->error) == 0) {
                        r->value.boolean = regexec (&pattern, contents, 0, NULL, 0) == 0;
                        regfree (&pattern);
                } else
@@ -1234,7 +1225,7 @@ search_header_regex (struct _ESExp *f, gint argc, struct _ESExpResult **argv, Ca
 
                g_object_unref (msg);
        } else {
-               r = e_sexp_result_new (f, ESEXP_RES_ARRAY_PTR);
+               r = camel_sexp_result_new (f, CAMEL_SEXP_RES_ARRAY_PTR);
                r->value.ptrarray = g_ptr_array_new ();
        }
 
@@ -1263,10 +1254,13 @@ get_full_header (CamelMimeMessage *message)
        return g_string_free (str, FALSE);
 }
 
-static ESExpResult *
-search_header_full_regex (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
+static CamelSExpResult *
+search_header_full_regex (struct _CamelSExp *f,
+                          gint argc,
+                          struct _CamelSExpResult **argv,
+                          CamelFolderSearch *search)
 {
-       ESExpResult *r;
+       CamelSExpResult *r;
        CamelMimeMessage *msg;
 
        msg = get_current_message (search);
@@ -1274,9 +1268,9 @@ search_header_full_regex (struct _ESExp *f, gint argc, struct _ESExpResult **arg
        if (msg) {
                regex_t pattern;
 
-               r = e_sexp_result_new (f, ESEXP_RES_BOOL);
+               r = camel_sexp_result_new (f, CAMEL_SEXP_RES_BOOL);
 
-               if (camel_search_build_match_regex (&pattern, CAMEL_SEARCH_MATCH_REGEX|CAMEL_SEARCH_MATCH_ICASE|CAMEL_SEARCH_MATCH_NEWLINE, argc, argv, search->priv->error) == 0) {
+               if (camel_search_build_match_regex (&pattern, CAMEL_SEARCH_MATCH_REGEX | CAMEL_SEARCH_MATCH_ICASE | CAMEL_SEARCH_MATCH_NEWLINE, argc, argv, search->priv->error) == 0) {
                        gchar *contents;
 
                        contents = get_full_header (msg);
@@ -1289,7 +1283,7 @@ search_header_full_regex (struct _ESExp *f, gint argc, struct _ESExpResult **arg
 
                g_object_unref (msg);
        } else {
-               r = e_sexp_result_new (f, ESEXP_RES_ARRAY_PTR);
+               r = camel_sexp_result_new (f, CAMEL_SEXP_RES_ARRAY_PTR);
                r->value.ptrarray = g_ptr_array_new ();
        }
 
@@ -1304,14 +1298,18 @@ struct IterData {
 
 /* or, store all unique values */
 static void
-htor (gchar *key, gint value, struct IterData *iter_data)
+htor (gchar *key,
+      gint value,
+      struct IterData *iter_data)
 {
        g_ptr_array_add (iter_data->uids, key);
 }
 
 /* and, only store duplicates */
 static void
-htand (gchar *key, gint value, struct IterData *iter_data)
+htand (gchar *key,
+       gint value,
+       struct IterData *iter_data)
 {
        if (value == iter_data->count)
                g_ptr_array_add (iter_data->uids, key);
@@ -1348,13 +1346,13 @@ match_message_index (CamelIndex *idx,
 
 /*
  "one two" "three" "four five"
-
-  one and two
-or
-  three
-or
-  four and five
-*/
+ *
* one and two
+ * or
* three
+ * or
* four and five
+ */
 
 /* returns messages which contain all words listed in words */
 static GPtrArray *
@@ -1374,7 +1372,7 @@ match_words_index (CamelFolderSearch *search,
        wc = camel_index_words (search->body_index);
        if (wc) {
                while ((word = camel_index_cursor_next (wc))) {
-                       for (i=0;i<words->len;i++) {
+                       for (i = 0; i < words->len; i++) {
                                if (camel_ustrstrcase (word, words->words[i]->word) != NULL) {
                                        /* perf: could have the wc cursor return the name cursor */
                                        nc = camel_index_find (search->body_index, word);
@@ -1394,7 +1392,7 @@ match_words_index (CamelFolderSearch *search,
 
                lambdafoo.uids = result;
                lambdafoo.count = (1 << words->len) - 1;
-               g_hash_table_foreach (ht, (GHFunc)htand, &lambdafoo);
+               g_hash_table_foreach (ht, (GHFunc) htand, &lambdafoo);
                g_hash_table_destroy (ht);
        }
 
@@ -1402,7 +1400,10 @@ match_words_index (CamelFolderSearch *search,
 }
 
 static gboolean
-match_words_1message (CamelDataWrapper *object, struct _camel_search_words *words, guint32 *mask)
+match_words_1message (CamelDataWrapper *object,
+                      struct _camel_search_words *words,
+                      guint32 *mask,
+                      GCancellable *cancellable)
 {
        CamelDataWrapper *containee;
        gint truth = FALSE;
@@ -1417,13 +1418,13 @@ match_words_1message (CamelDataWrapper *object, struct _camel_search_words *word
        if (CAMEL_IS_MULTIPART (containee)) {
                parts = camel_multipart_get_number (CAMEL_MULTIPART (containee));
                for (i = 0; i < parts && truth == FALSE; i++) {
-                       CamelDataWrapper *part = (CamelDataWrapper *)camel_multipart_get_part (CAMEL_MULTIPART (containee), i);
+                       CamelDataWrapper *part = (CamelDataWrapper *) camel_multipart_get_part (CAMEL_MULTIPART (containee), i);
                        if (part)
-                               truth = match_words_1message (part, words, mask);
+                               truth = match_words_1message (part, words, mask, cancellable);
                }
        } else if (CAMEL_IS_MIME_MESSAGE (containee)) {
                /* for messages we only look at its contents */
-               truth = match_words_1message ((CamelDataWrapper *)containee, words, mask);
+               truth = match_words_1message ((CamelDataWrapper *) containee, words, mask, cancellable);
        } else if (camel_content_type_is(CAMEL_DATA_WRAPPER (containee)->mime_type, "text", "*")) {
                /* for all other text parts, we look inside, otherwise we dont care */
                CamelStream *stream;
@@ -1433,16 +1434,15 @@ match_words_1message (CamelDataWrapper *object, struct _camel_search_words *word
                stream = camel_stream_mem_new_with_byte_array (byte_array);
 
                /* FIXME The match should be part of a stream op */
-               /* FIXME Pass a GCancellable and GError here. */
                camel_data_wrapper_decode_to_stream_sync (
-                       containee, stream, NULL, NULL);
+                       containee, stream, cancellable, NULL);
                camel_stream_write (stream, "", 1, NULL, NULL);
-               for (i=0;i<words->len;i++) {
+               for (i = 0; i < words->len; i++) {
                        /* FIXME: This is horridly slow, and should use a real search algorithm */
                        if (camel_ustrstrcase ((const gchar *) byte_array->data, words->words[i]->word) != NULL) {
                                *mask |= (1 << i);
                                /* shortcut a match */
-                               if (*mask == (1 << (words->len))-1)
+                               if (*mask == (1 << (words->len)) - 1)
                                        return TRUE;
                        }
                }
@@ -1467,7 +1467,7 @@ match_words_message (CamelFolder *folder,
        msg = camel_folder_get_message_sync (folder, uid, cancellable, error);
        if (msg) {
                mask = 0;
-               truth = match_words_1message ((CamelDataWrapper *)msg, words, &mask);
+               truth = match_words_1message ((CamelDataWrapper *) msg, words, &mask, cancellable);
                g_object_unref (msg);
        }
 
@@ -1491,39 +1491,42 @@ match_words_messages (CamelFolderSearch *search,
                indexed = match_words_index (search, simple, error);
                camel_search_words_free (simple);
 
-               for (i=0;i<indexed->len;i++) {
+               for (i = 0; i < indexed->len; i++) {
                        const gchar *uid = g_ptr_array_index (indexed, i);
 
                        if (match_words_message (
                                        search->folder, uid, words,
                                        cancellable, error))
-                               g_ptr_array_add (matches, (gchar *)uid);
+                               g_ptr_array_add (matches, (gchar *) uid);
                }
 
                g_ptr_array_free (indexed, TRUE);
        } else {
-               GPtrArray *v = search->summary_set?search->summary_set:search->summary;
+               GPtrArray *v = search->summary_set ? search->summary_set : search->summary;
 
-               for (i=0;i<v->len;i++) {
+               for (i = 0; i < v->len; i++) {
                        gchar *uid  = g_ptr_array_index (v, i);
 
                        if (match_words_message (
                                search->folder, uid, words,
                                cancellable, error))
-                               g_ptr_array_add (matches, (gchar *)uid);
+                               g_ptr_array_add (matches, (gchar *) uid);
                }
        }
 
        return matches;
 }
 
-static ESExpResult *
-search_body_contains (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
+static CamelSExpResult *
+search_body_contains (struct _CamelSExp *f,
+                      gint argc,
+                      struct _CamelSExpResult **argv,
+                      CamelFolderSearch *search)
 {
        gint i, j;
        GError **error = search->priv->error;
        struct _camel_search_words *words;
-       ESExpResult *r;
+       CamelSExpResult *r;
        struct IterData lambdafoo;
 
        if (search->current) {
@@ -1532,32 +1535,31 @@ search_body_contains (struct _ESExp *f, gint argc, struct _ESExpResult **argv, C
                if (argc == 1 && argv[0]->value.string[0] == 0) {
                        truth = TRUE;
                } else {
-                       for (i=0;i<argc && !truth;i++) {
-                               if (argv[i]->type == ESEXP_RES_STRING) {
+                       for (i = 0; i < argc && !truth; i++) {
+                               if (argv[i]->type == CAMEL_SEXP_RES_STRING) {
                                        words = camel_search_words_split ((const guchar *) argv[i]->value.string);
                                        truth = TRUE;
                                        if ((words->type & CAMEL_SEARCH_WORD_COMPLEX) == 0 && search->body_index) {
-                                               for (j=0;j<words->len && truth;j++)
+                                               for (j = 0; j < words->len && truth; j++)
                                                        truth = match_message_index (search->body_index, camel_message_info_uid (search->current), words->words[j]->word, error);
                                        } else {
                                                /* TODO: cache current message incase of multiple body search terms */
-                                               /* FIXME Pass a GCancellable */
-                                               truth = match_words_message (search->folder, camel_message_info_uid (search->current), words, NULL, error);
+                                               truth = match_words_message (search->folder, camel_message_info_uid (search->current), words, search->priv->cancellable, error);
                                        }
                                        camel_search_words_free (words);
                                }
                        }
                }
-               r = e_sexp_result_new (f, ESEXP_RES_BOOL);
+               r = camel_sexp_result_new (f, CAMEL_SEXP_RES_BOOL);
                r->value.boolean = truth;
        } else {
-               r = e_sexp_result_new (f, ESEXP_RES_ARRAY_PTR);
+               r = camel_sexp_result_new (f, CAMEL_SEXP_RES_ARRAY_PTR);
                r->value.ptrarray = g_ptr_array_new ();
 
                if (argc == 1 && argv[0]->value.string[0] == 0) {
-                       GPtrArray *v = search->summary_set?search->summary_set:search->summary;
+                       GPtrArray *v = search->summary_set ? search->summary_set : search->summary;
 
-                       for (i=0;i<v->len;i++) {
+                       for (i = 0; i < v->len; i++) {
                                gchar *uid = g_ptr_array_index (v, i);
 
                                g_ptr_array_add (r->value.ptrarray, uid);
@@ -1566,16 +1568,15 @@ search_body_contains (struct _ESExp *f, gint argc, struct _ESExpResult **argv, C
                        GHashTable *ht = g_hash_table_new (g_str_hash, g_str_equal);
                        GPtrArray *matches;
 
-                       for (i=0;i<argc;i++) {
-                               if (argv[i]->type == ESEXP_RES_STRING) {
+                       for (i = 0; i < argc; i++) {
+                               if (argv[i]->type == CAMEL_SEXP_RES_STRING) {
                                        words = camel_search_words_split ((const guchar *) argv[i]->value.string);
                                        if ((words->type & CAMEL_SEARCH_WORD_COMPLEX) == 0 && search->body_index) {
                                                matches = match_words_index (search, words, error);
                                        } else {
-                                               /* FIXME Pass a GCancellable */
-                                               matches = match_words_messages (search, words, NULL, error);
+                                               matches = match_words_messages (search, words, search->priv->cancellable, error);
                                        }
-                                       for (j=0;j<matches->len;j++) {
+                                       for (j = 0; j < matches->len; j++) {
                                                g_hash_table_insert (ht, matches->pdata[j], matches->pdata[j]);
                                        }
                                        g_ptr_array_free (matches, TRUE);
@@ -1583,7 +1584,7 @@ search_body_contains (struct _ESExp *f, gint argc, struct _ESExpResult **argv, C
                                }
                        }
                        lambdafoo.uids = r->value.ptrarray;
-                       g_hash_table_foreach (ht, (GHFunc)htor, &lambdafoo);
+                       g_hash_table_foreach (ht, (GHFunc) htor, &lambdafoo);
                        g_hash_table_destroy (ht);
                }
        }
@@ -1591,18 +1592,21 @@ search_body_contains (struct _ESExp *f, gint argc, struct _ESExpResult **argv, C
        return r;
 }
 
-static ESExpResult *
-search_body_regex (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
+static CamelSExpResult *
+search_body_regex (struct _CamelSExp *f,
+                   gint argc,
+                   struct _CamelSExpResult **argv,
+                   CamelFolderSearch *search)
 {
-       ESExpResult *r;
+       CamelSExpResult *r;
        CamelMimeMessage *msg = get_current_message (search);
 
        if (msg) {
                regex_t pattern;
 
-               r = e_sexp_result_new (f, ESEXP_RES_BOOL);
+               r = camel_sexp_result_new (f, CAMEL_SEXP_RES_BOOL);
 
-               if (camel_search_build_match_regex (&pattern, CAMEL_SEARCH_MATCH_ICASE|CAMEL_SEARCH_MATCH_REGEX|CAMEL_SEARCH_MATCH_NEWLINE, argc, argv, search->priv->error) == 0) {
+               if (camel_search_build_match_regex (&pattern, CAMEL_SEARCH_MATCH_ICASE | CAMEL_SEARCH_MATCH_REGEX | CAMEL_SEARCH_MATCH_NEWLINE, argc, argv, search->priv->error) == 0) {
                        r->value.boolean = camel_search_message_body_contains ((CamelDataWrapper *) msg, &pattern);
                        regfree (&pattern);
                } else
@@ -1612,20 +1616,19 @@ search_body_regex (struct _ESExp *f, gint argc, struct _ESExpResult **argv, Came
        } else {
                regex_t pattern;
 
-               r = e_sexp_result_new (f, ESEXP_RES_ARRAY_PTR);
+               r = camel_sexp_result_new (f, CAMEL_SEXP_RES_ARRAY_PTR);
                r->value.ptrarray = g_ptr_array_new ();
 
-               if (camel_search_build_match_regex (&pattern, CAMEL_SEARCH_MATCH_ICASE|CAMEL_SEARCH_MATCH_REGEX|CAMEL_SEARCH_MATCH_NEWLINE, argc, argv, search->priv->error) == 0) {
+               if (camel_search_build_match_regex (&pattern, CAMEL_SEARCH_MATCH_ICASE | CAMEL_SEARCH_MATCH_REGEX | CAMEL_SEARCH_MATCH_NEWLINE, argc, argv, search->priv->error) == 0) {
                        gint i;
-                       GPtrArray *v = search->summary_set?search->summary_set:search->summary;
+                       GPtrArray *v = search->summary_set ? search->summary_set : search->summary;
                        CamelMimeMessage *message;
 
-                       for (i = 0; i < v->len; i++) {
+                       for (i = 0; i < v->len && !g_cancellable_is_cancelled (search->priv->cancellable); i++) {
                                gchar *uid = g_ptr_array_index (v, i);
 
-                               /* FIXME Pass a GCancellable */
                                message = camel_folder_get_message_sync (
-                                       search->folder, uid, NULL, NULL);
+                                       search->folder, uid, search->priv->cancellable, NULL);
                                if (message) {
                                        if (camel_search_message_body_contains ((CamelDataWrapper *) message, &pattern)) {
                                                g_ptr_array_add (r->value.ptrarray, uid);
@@ -1642,10 +1645,13 @@ search_body_regex (struct _ESExp *f, gint argc, struct _ESExpResult **argv, Came
        return r;
 }
 
-static ESExpResult *
-search_user_flag (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
+static CamelSExpResult *
+search_user_flag (struct _CamelSExp *f,
+                  gint argc,
+                  struct _CamelSExpResult **argv,
+                  CamelFolderSearch *search)
 {
-       ESExpResult *r;
+       CamelSExpResult *r;
        gint i;
 
        r(printf("executing user-flag\n"));
@@ -1654,27 +1660,30 @@ search_user_flag (struct _ESExp *f, gint argc, struct _ESExpResult **argv, Camel
        if (search->current) {
                gint truth = FALSE;
                /* performs an OR of all words */
-               for (i=0;i<argc && !truth;i++) {
-                       if (argv[i]->type == ESEXP_RES_STRING
+               for (i = 0; i < argc && !truth; i++) {
+                       if (argv[i]->type == CAMEL_SEXP_RES_STRING
                            && camel_message_info_user_flag (search->current, argv[i]->value.string)) {
                                truth = TRUE;
                                break;
                        }
                }
-               r = e_sexp_result_new (f, ESEXP_RES_BOOL);
+               r = camel_sexp_result_new (f, CAMEL_SEXP_RES_BOOL);
                r->value.boolean = truth;
        } else {
-               r = e_sexp_result_new (f, ESEXP_RES_ARRAY_PTR);
+               r = camel_sexp_result_new (f, CAMEL_SEXP_RES_ARRAY_PTR);
                r->value.ptrarray = g_ptr_array_new ();
        }
 
        return r;
 }
 
-static ESExpResult *
-search_system_flag (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
+static CamelSExpResult *
+search_system_flag (struct _CamelSExp *f,
+                    gint argc,
+                    struct _CamelSExpResult **argv,
+                    CamelFolderSearch *search)
 {
-       ESExpResult *r;
+       CamelSExpResult *r;
 
        r(printf ("executing system-flag\n"));
 
@@ -1684,108 +1693,149 @@ search_system_flag (struct _ESExp *f, gint argc, struct _ESExpResult **argv, Cam
                if (argc == 1)
                        truth = camel_system_flag_get (camel_message_info_flags (search->current), argv[0]->value.string);
 
-               r = e_sexp_result_new (f, ESEXP_RES_BOOL);
+               r = camel_sexp_result_new (f, CAMEL_SEXP_RES_BOOL);
                r->value.boolean = truth;
        } else {
-               r = e_sexp_result_new (f, ESEXP_RES_ARRAY_PTR);
+               r = camel_sexp_result_new (f, CAMEL_SEXP_RES_ARRAY_PTR);
                r->value.ptrarray = g_ptr_array_new ();
        }
 
        return r;
 }
 
-static ESExpResult *
-search_user_tag (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
+static CamelSExpResult *
+search_user_tag (struct _CamelSExp *f,
+                 gint argc,
+                 struct _CamelSExpResult **argv,
+                 CamelFolderSearch *search)
 {
        const gchar *value = NULL;
-       ESExpResult *r;
+       CamelSExpResult *r;
 
        r(printf("executing user-tag\n"));
 
        if (search->current && argc == 1)
                value = camel_message_info_user_tag (search->current, argv[0]->value.string);
 
-       r = e_sexp_result_new (f, ESEXP_RES_STRING);
+       r = camel_sexp_result_new (f, CAMEL_SEXP_RES_STRING);
        r->value.string = g_strdup (value ? value : "");
 
        return r;
 }
 
-static ESExpResult *
-search_get_sent_date (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s)
+static CamelSExpResult *
+search_get_sent_date (struct _CamelSExp *f,
+                      gint argc,
+                      struct _CamelSExpResult **argv,
+                      CamelFolderSearch *s)
 {
-       ESExpResult *r;
+       CamelSExpResult *r;
 
        r(printf("executing get-sent-date\n"));
 
        /* are we inside a match-all? */
        if (s->current) {
-               r = e_sexp_result_new (f, ESEXP_RES_INT);
+               r = camel_sexp_result_new (f, CAMEL_SEXP_RES_INT);
 
                r->value.number = camel_message_info_date_sent (s->current);
        } else {
-               r = e_sexp_result_new (f, ESEXP_RES_ARRAY_PTR);
+               r = camel_sexp_result_new (f, CAMEL_SEXP_RES_ARRAY_PTR);
                r->value.ptrarray = g_ptr_array_new ();
        }
 
        return r;
 }
 
-static ESExpResult *
-search_get_received_date (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s)
+static CamelSExpResult *
+search_get_received_date (struct _CamelSExp *f,
+                          gint argc,
+                          struct _CamelSExpResult **argv,
+                          CamelFolderSearch *s)
 {
-       ESExpResult *r;
+       CamelSExpResult *r;
 
        r(printf("executing get-received-date\n"));
 
        /* are we inside a match-all? */
        if (s->current) {
-               r = e_sexp_result_new (f, ESEXP_RES_INT);
+               r = camel_sexp_result_new (f, CAMEL_SEXP_RES_INT);
 
                r->value.number = camel_message_info_date_received (s->current);
        } else {
-               r = e_sexp_result_new (f, ESEXP_RES_ARRAY_PTR);
+               r = camel_sexp_result_new (f, CAMEL_SEXP_RES_ARRAY_PTR);
                r->value.ptrarray = g_ptr_array_new ();
        }
 
        return r;
 }
 
-static ESExpResult *
-search_get_current_date (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s)
+static CamelSExpResult *
+search_get_current_date (struct _CamelSExp *f,
+                         gint argc,
+                         struct _CamelSExpResult **argv,
+                         CamelFolderSearch *s)
 {
-       ESExpResult *r;
+       CamelSExpResult *r;
 
        r(printf("executing get-current-date\n"));
 
-       r = e_sexp_result_new (f, ESEXP_RES_INT);
+       r = camel_sexp_result_new (f, CAMEL_SEXP_RES_INT);
        r->value.number = time (NULL);
        return r;
 }
 
-static ESExpResult *
-search_get_size (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *s)
+static CamelSExpResult *
+search_get_relative_months (struct _CamelSExp *f,
+                            gint argc,
+                            struct _CamelSExpResult **argv,
+                            CamelFolderSearch *s)
 {
-       ESExpResult *r;
+       CamelSExpResult *r;
+
+       r(printf("executing get-relative-months\n"));
+
+       if (argc != 1 || argv[0]->type != CAMEL_SEXP_RES_INT) {
+               r = camel_sexp_result_new (f, CAMEL_SEXP_RES_BOOL);
+               r->value.boolean = FALSE;
+
+               g_debug ("%s: Expecting 1 argument, an integer, but got %d arguments", G_STRFUNC, argc);
+       } else {
+               r = camel_sexp_result_new (f, CAMEL_SEXP_RES_INT);
+               r->value.number = camel_folder_search_util_add_months (time (NULL), argv[0]->value.number);
+       }
+
+       return r;
+}
+
+static CamelSExpResult *
+search_get_size (struct _CamelSExp *f,
+                 gint argc,
+                 struct _CamelSExpResult **argv,
+                 CamelFolderSearch *s)
+{
+       CamelSExpResult *r;
 
        r(printf("executing get-size\n"));
 
        /* are we inside a match-all? */
        if (s->current) {
-               r = e_sexp_result_new (f, ESEXP_RES_INT);
+               r = camel_sexp_result_new (f, CAMEL_SEXP_RES_INT);
                r->value.number = camel_message_info_size (s->current) / 1024;
        } else {
-               r = e_sexp_result_new (f, ESEXP_RES_ARRAY_PTR);
+               r = camel_sexp_result_new (f, CAMEL_SEXP_RES_ARRAY_PTR);
                r->value.ptrarray = g_ptr_array_new ();
        }
 
        return r;
 }
 
-static ESExpResult *
-search_uid (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
+static CamelSExpResult *
+search_uid (struct _CamelSExp *f,
+            gint argc,
+            struct _CamelSExpResult **argv,
+            CamelFolderSearch *search)
 {
-       ESExpResult *r;
+       CamelSExpResult *r;
        gint i;
 
        r(printf("executing uid\n"));
@@ -1796,20 +1846,20 @@ search_uid (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolder
                const gchar *uid = camel_message_info_uid (search->current);
 
                /* performs an OR of all words */
-               for (i=0;i<argc && !truth;i++) {
-                       if (argv[i]->type == ESEXP_RES_STRING
+               for (i = 0; i < argc && !truth; i++) {
+                       if (argv[i]->type == CAMEL_SEXP_RES_STRING
                            && !strcmp (uid, argv[i]->value.string)) {
                                truth = TRUE;
                                break;
                        }
                }
-               r = e_sexp_result_new (f, ESEXP_RES_BOOL);
+               r = camel_sexp_result_new (f, CAMEL_SEXP_RES_BOOL);
                r->value.boolean = truth;
        } else {
-               r = e_sexp_result_new (f, ESEXP_RES_ARRAY_PTR);
+               r = camel_sexp_result_new (f, CAMEL_SEXP_RES_ARRAY_PTR);
                r->value.ptrarray = g_ptr_array_new ();
-               for (i=0;i<argc;i++) {
-                       if (argv[i]->type == ESEXP_RES_STRING)
+               for (i = 0; i < argc; i++) {
+                       if (argv[i]->type == CAMEL_SEXP_RES_STRING)
                                g_ptr_array_add (r->value.ptrarray, argv[i]->value.string);
                }
        }
@@ -1818,7 +1868,10 @@ search_uid (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolder
 }
 
 static gint
-read_uid_callback (gpointer  ref, gint ncol, gchar ** cols, gchar **name)
+read_uid_callback (gpointer ref,
+                   gint ncol,
+                   gchar **cols,
+                   gchar **name)
 {
        GPtrArray *matches;
 
@@ -1828,32 +1881,39 @@ read_uid_callback (gpointer  ref, gint ncol, gchar ** cols, gchar **name)
        return 0;
 }
 
-static ESExpResult *
-search_message_location (struct _ESExp *f, gint argc, struct _ESExpResult **argv, CamelFolderSearch *search)
+static CamelSExpResult *
+search_message_location (struct _CamelSExp *f,
+                         gint argc,
+                         struct _CamelSExpResult **argv,
+                         CamelFolderSearch *search)
 {
-       CamelStore *parent_store;
-       ESExpResult *r;
+       CamelSExpResult *r;
        gboolean same = FALSE;
 
-       parent_store = camel_folder_get_parent_store (search->folder);
+       if (argc == 1 && argv[0]->type == CAMEL_SEXP_RES_STRING) {
+               if (argv[0]->value.string && search->folder) {
+                       CamelStore *store;
+                       const gchar *name;
+                       const gchar *uid;
+                       gchar *uri;
 
-       if (argc == 1 && argv[0]->type == ESEXP_RES_STRING) {
-               if (argv[0]->value.string && search->folder && parent_store && camel_folder_get_full_name (search->folder)) {
-                       /* FIXME Pass a GCancellable */
-                       CamelFolderInfo *fi = camel_store_get_folder_info_sync (parent_store, camel_folder_get_full_name (search->folder), 0, NULL, NULL);
-                       if (fi) {
-                               same = g_str_equal (fi->uri ? fi->uri : "", argv[0]->value.string);
+                       /* FIXME Folder URI formats are Evolution-specific
+                        *       knowledge and doesn't belong here! */
+                       store = camel_folder_get_parent_store (search->folder);
+                       name = camel_folder_get_full_name (search->folder);
+                       uid = camel_service_get_uid (CAMEL_SERVICE (store));
 
-                               camel_store_free_folder_info (parent_store, fi);
-                       }
+                       uri = g_strdup_printf ("folder://%s/%s", uid, name);
+                       same = g_str_equal (uri, argv[0]->value.string);
+                       g_free (uri);
                }
        }
 
        if (search->current) {
-               r = e_sexp_result_new (f, ESEXP_RES_BOOL);
+               r = camel_sexp_result_new (f, CAMEL_SEXP_RES_BOOL);
                r->value.boolean = same ? TRUE : FALSE;
        } else {
-               r = e_sexp_result_new (f, ESEXP_RES_ARRAY_PTR);
+               r = camel_sexp_result_new (f, CAMEL_SEXP_RES_ARRAY_PTR);
                r->value.ptrarray = g_ptr_array_new ();
 
                if (same) {
@@ -1871,3 +1931,42 @@ search_message_location (struct _ESExp *f, gint argc, struct _ESExpResult **argv
 
        return r;
 }
+
+/**
+ * camel_folder_search_util_add_months:
+ * @t: Initial time
+ * @months: number of months to add or subtract
+ *
+ * Increases time @t by the given number of months (or decreases, if
+ * @months is negative).
+ *
+ * Returns: a new #time_t value
+ *
+ * Since: 3.2
+ **/
+time_t
+camel_folder_search_util_add_months (time_t t,
+                                     gint months)
+{
+       GDateTime *dt, *dt2;
+       time_t res;
+
+       if (!months)
+               return t;
+
+       dt = g_date_time_new_from_unix_utc (t);
+
+       /* just for issues, to return something inaccurate, but sane */
+       res = t + (60 * 60 * 24 * 30 * months);
+
+       g_return_val_if_fail (dt != NULL, res);
+
+       dt2 = g_date_time_add_months (dt, months);
+       g_date_time_unref (dt);
+       g_return_val_if_fail (dt2 != NULL, res);
+
+       res = g_date_time_to_unix (dt2);
+       g_date_time_unref (dt2);
+
+       return res;
+}