* fix searching for strings with spaces (e.g. maildir:"sent items")

This commit is contained in:
djcb 2011-11-24 00:12:29 +02:00
parent cc6738c195
commit c9f72ba3c7
5 changed files with 118 additions and 69 deletions

View File

@ -294,6 +294,35 @@ resolve_bookmark (MuConfig *opts, GError **err)
}
gchar*
str_quoted_from_strv (const gchar **params)
{
GString *str;
int i;
g_return_val_if_fail (params, NULL);
if (!params[0])
return g_strdup ("");
str = g_string_sized_new (64); /* just a guess */
for (i = 0; params[i]; ++i) {
if (i > 0)
g_string_append_c (str, ' ');
g_string_append_c (str, '"');
g_string_append (str, params[i]);
g_string_append_c (str, '"');
}
return g_string_free (str, FALSE);
}
static gchar*
get_query (MuConfig *opts, GError **err)
{
@ -313,7 +342,7 @@ get_query (MuConfig *opts, GError **err)
return NULL;
}
query = mu_util_str_from_strv ((const gchar**)&opts->params[1]);
query = str_quoted_from_strv ((const gchar**)&opts->params[1]);
if (bookmarkval) {
gchar *tmp;
tmp = g_strdup_printf ("%s %s", bookmarkval, query);

View File

@ -203,7 +203,8 @@ get_query (MuQuery *mqx, const char* searchexpr, GError **err)
Xapian::QueryParser::FLAG_PURE_NOT |
Xapian::QueryParser::FLAG_WILDCARD |
Xapian::QueryParser::FLAG_AUTO_SYNONYMS |
Xapian::QueryParser::FLAG_BOOLEAN_ANY_CASE);
Xapian::QueryParser::FLAG_BOOLEAN_ANY_CASE
);
g_free (preprocessed);
return query;
@ -275,22 +276,32 @@ mu_query_destroy (MuQuery *self)
}
/* preprocess a query to make them a bit more permissive */
/* preprocess a query to make them a bit more promiscuous */
char*
mu_query_preprocess (const char *query)
{
gchar *my_query;
GSList *parts, *cur;
gchar *myquery;
g_return_val_if_fail (query, NULL);
my_query = g_strdup (query);
/* remove accents and turn to lower-case */
mu_str_normalize_in_place (my_query, TRUE);
/* escape '@', single '_' and ':' if it's not following a
* xapian-pfx with '_' */
mu_str_ascii_xapian_escape_in_place (my_query);
/* convert the query to a list of query terms, and escape them
* separately */
parts = mu_str_esc_to_list (query);
return my_query;
for (cur = parts; cur; cur = g_slist_next(cur)) {
/* remove accents and turn to lower-case */
cur->data = mu_str_normalize_in_place ((gchar*)cur->data, TRUE);
/* escape '@', single '_' and ':' if it's not following a
* xapian-pfx with '_' */
cur->data = mu_str_ascii_xapian_escape_in_place
((gchar*)cur->data, TRUE /*escape spaces too*/);
}
myquery = mu_str_from_list (parts, ' ');
mu_str_free_list (parts);
return myquery;
}

View File

@ -432,7 +432,7 @@ mu_str_subject_normalize (const gchar* str)
* specially; function below is an ugly hack to make it DWIM in most
* cases...*/
char*
mu_str_ascii_xapian_escape_in_place (char *query)
mu_str_ascii_xapian_escape_in_place (char *query, gboolean esc_space)
{
gchar *cur;
const char escchar = '_';
@ -444,10 +444,14 @@ mu_str_ascii_xapian_escape_in_place (char *query)
*cur = tolower(*cur);
switch (*cur) {
case ' ':
if (!esc_space)
break;
case '@':
case '-':
case '/':
case '.': {
case '.':
{
/* don't replace a final special char */
if (cur[1]== ' ' || cur[1]=='\t' || cur[1]== '.')
++cur;
@ -472,11 +476,11 @@ mu_str_ascii_xapian_escape_in_place (char *query)
}
char*
mu_str_ascii_xapian_escape (const char *query)
mu_str_ascii_xapian_escape (const char *query, gboolean esc_space)
{
g_return_val_if_fail (query, NULL);
return mu_str_ascii_xapian_escape_in_place (g_strdup(query));
return mu_str_ascii_xapian_escape_in_place (g_strdup(query), esc_space);
}

View File

@ -141,10 +141,11 @@ char* mu_str_normalize_in_place (char *str, gboolean downcase);
* works for ascii strings, like e-mail addresses and message-id.
*
* @param query a query string
* @param esc_space escape space characters as well
*
* @return the escaped string or NULL in case of error
*/
char* mu_str_ascii_xapian_escape_in_place (char *query);
char* mu_str_ascii_xapian_escape_in_place (char *query, gboolean esc_space);
/**
* escape the string for use with xapian matching. in practice, if the
@ -155,10 +156,11 @@ char* mu_str_ascii_xapian_escape_in_place (char *query);
* works for ascii strings, like e-mail addresses and message-id.
*
* @param query a query string
* @param esc_space escape space characters as well
*
* @return the escaped string (free with g_free) or NULL in case of error
*/
char* mu_str_ascii_xapian_escape (const char *query)
char* mu_str_ascii_xapian_escape (const char *query, gboolean esc_space)
G_GNUC_WARN_UNUSED_RESULT;

View File

@ -284,7 +284,7 @@ mu_util_str_from_strv (const gchar **params)
for (i = 0; params[i]; ++i) {
if (i>0)
if (i > 0)
g_string_append_c (str, ' ');
g_string_append (str, params[i]);
@ -293,6 +293,11 @@ mu_util_str_from_strv (const gchar **params)
return g_string_free (str, FALSE);
}
int
mu_util_create_writeable_fd (const char* path, mode_t mode,
gboolean overwrite)
@ -482,5 +487,3 @@ mu_util_printerr_encoded (const char *frm, ...)
return rv;
}