From 0ff6aaf472552f094d01ec92f299339e7872e72a Mon Sep 17 00:00:00 2001 From: Bernard Pratz Date: Sat, 7 Jan 2017 18:52:55 +0100 Subject: [PATCH] Fix of the sidebar item highlight when matched by current search Because searches done manually (or shifted with a window) have an absolute path to the current Maildir within the URI, whereas the one declared with the virtual-mailboxes command usually have implicit Maildir path specification, this patch parses the given URL of a mailbox and re-builds it to contain the full path. For that purpose has been added: - nm_normalize_uri() function Has been modified: - get_query_string (in mutt_notmuch): earned an argument to disable adding of a window in the context of the nm_normalize_uri function. - mutt_parse_virtual_mailboxes: the path value is sent through the nm_normalize_uri() function to be normalized. Signed-off-by: Guyzmo --- buffy.c | 2 +- mutt_notmuch.c | 85 +++++++++++++++++++++++++++++++++++++++++--------- mutt_notmuch.h | 1 + 3 files changed, 72 insertions(+), 16 deletions(-) diff --git a/buffy.c b/buffy.c index 25c68d72a..9442cd7bf 100644 --- a/buffy.c +++ b/buffy.c @@ -485,7 +485,7 @@ int mutt_parse_virtual_mailboxes (BUFFER *path, BUFFER *s, unsigned long data, B continue; mutt_extract_token (path, s, 0); - strfcpy (buf, path->data, sizeof (buf)); + nm_normalize_uri(buf, path->data, sizeof(buf)); /* Skip empty tokens. */ if(!*buf) { diff --git a/mutt_notmuch.c b/mutt_notmuch.c index e60d68eeb..77de782fb 100644 --- a/mutt_notmuch.c +++ b/mutt_notmuch.c @@ -446,9 +446,9 @@ static void query_window_reset(void) NotmuchQueryWindowCurrentPosition = 0; } -static int windowed_query_from_query(char *query, char *buf, size_t bufsz) +static int windowed_query_from_query(const char *query, char *buf, size_t bufsz) { - dprint(2, (debugfile, "windowed_query_from_query (%s)\n", query)); + dprint(2, (debugfile, "nm: windowed_query_from_query (%s)\n", query)); int beg = NotmuchQueryWindowDuration * (NotmuchQueryWindowCurrentPosition + 1); int end = NotmuchQueryWindowDuration * NotmuchQueryWindowCurrentPosition; @@ -480,15 +480,16 @@ static int windowed_query_from_query(char *query, char *buf, size_t bufsz) snprintf(buf, bufsz, "date:%d%s..%d%s and %s", beg, NotmuchQueryWindowTimebase, end, NotmuchQueryWindowTimebase, NotmuchQueryWindowCurrentSearch); + dprint(2, (debugfile, "nm: windowed_query_from_query (%s) -> %s\n", query, buf)); + return 1; } -static char *get_query_string(struct nm_ctxdata *data) +static char *get_query_string(struct nm_ctxdata *data, int window) { - dprint(2, (debugfile, "nm: get_query_string()\n")); + dprint(2, (debugfile, "nm: get_query_string(%d)\n", window)); struct uri_tag *item; - char buf[LONG_STRING]; if (!data) return NULL; @@ -515,16 +516,22 @@ static char *get_query_string(struct nm_ctxdata *data) if (!data->query_type) data->query_type = string_to_query_type(NULL); - mutt_str_replace(&NotmuchQueryWindowCurrentSearch, data->db_query); + if (window) + { + char buf[LONG_STRING]; + mutt_str_replace(&NotmuchQueryWindowCurrentSearch, data->db_query); - // if a date part is defined, do not apply windows (to avoid the risk of - // having a non-intersected date frame). A good improvement would be to - // accept if they intersect - if (!strstr(data->db_query, "date:") && - windowed_query_from_query(data->db_query, buf, sizeof(buf))) - data->db_query = safe_strdup(buf); + // if a date part is defined, do not apply windows (to avoid the risk of + // having a non-intersected date frame). A good improvement would be to + // accept if they intersect + if (!strstr(data->db_query, "date:") && + windowed_query_from_query(data->db_query, buf, sizeof(buf))) + data->db_query = safe_strdup(buf); - dprint(2, (debugfile, "nm: query '%s'\n", data->db_query)); + dprint(2, (debugfile, "nm: query (windowed) '%s'\n", data->db_query)); + } + else + dprint(2, (debugfile, "nm: query '%s'\n", data->db_query)); return data->db_query; } @@ -731,7 +738,7 @@ static notmuch_query_t *get_query(struct nm_ctxdata *data, int writable) return NULL; db = get_db(data, writable); - str = get_query_string(data); + str = get_query_string(data, true); if (!db || !str) goto err; @@ -742,7 +749,7 @@ static notmuch_query_t *get_query(struct nm_ctxdata *data, int writable) apply_exclude_tags(q); notmuch_query_set_sort(q, NOTMUCH_SORT_NEWEST_FIRST); - dprint(2, (debugfile, "nm: query successfully initialized\n")); + dprint(2, (debugfile, "nm: query successfully initialized (%s)\n", str)); return q; err: if (!is_longrun(data)) @@ -1673,6 +1680,54 @@ char *nm_uri_from_query(CONTEXT *ctx, char *buf, size_t bufsz) return buf; } +/* + * takes a notmuch URI, parses it and reformat it in a canonical way + */ +int nm_normalize_uri(char *new_url, char* url, size_t new_url_sz) +{ + dprint(2, (debugfile, "nm_normalize_uri (%s)\n", url)); + char buf[LONG_STRING]; + + CONTEXT tmp_ctx; + struct nm_ctxdata tmp_ctxdata; + + tmp_ctx.magic = MUTT_NOTMUCH; + tmp_ctx.data = &tmp_ctxdata; + tmp_ctxdata.db_query = NULL; + + if (url_parse_query(url, &tmp_ctxdata.db_filename, &tmp_ctxdata.query_items)) + { + mutt_error(_("failed to parse #1 notmuch uri: %s"), url); + dprint(2, (debugfile, "nm_normalize_uri () -> error #1\n")); + return 0; + } + + dprint(2, (debugfile, "nm_normalize_uri #1 () -> db_query: %s\n", tmp_ctxdata.db_query)); + + if (get_query_string(&tmp_ctxdata, false) == NULL) + { + mutt_error(_("failed to parse #2 notmuch uri: %s"), url); + dprint(2, (debugfile, "nm_normalize_uri () -> error #2\n")); + return 0; + } + + dprint(2, (debugfile, "nm_normalize_uri #2 () -> db_query: %s\n", tmp_ctxdata.db_query)); + + strncpy(buf, tmp_ctxdata.db_query, sizeof(buf)); + + if (nm_uri_from_query(&tmp_ctx, buf, sizeof(buf)) == NULL) + { + mutt_error(_("failed to parse #3 notmuch uri: %s"), url); + dprint(2, (debugfile, "nm_normalize_uri () -> error #3\n")); + return 0; + } + + strncpy(new_url, buf, new_url_sz); + + dprint(2, (debugfile, "nm_normalize_uri #3 (%s) -> %s\n", url, new_url)); + return 1; +} + void nm_query_window_forward(void) { if (NotmuchQueryWindowCurrentPosition != 0) diff --git a/mutt_notmuch.h b/mutt_notmuch.h index 4d7f2a868..8b08aeef0 100644 --- a/mutt_notmuch.h +++ b/mutt_notmuch.h @@ -23,6 +23,7 @@ int nm_read_entire_thread(CONTEXT *ctx, HEADER *h); char *nm_header_get_folder(HEADER *h); int nm_update_filename(CONTEXT *ctx, const char *old, const char *new, HEADER *h); +int nm_normalize_uri(char *new_url, char *url, size_t new_url_sz); char *nm_uri_from_query(CONTEXT *ctx, char *buf, size_t bufsz); int nm_modify_message_tags(CONTEXT *ctx, HEADER *hdr, char *buf); -- 2.40.0