unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
From: David Edmondson <dme@dme.org>
To: Mark Walters <markwalters1009@gmail.com>, notmuch@notmuchmail.org
Subject: Re: [PATCH v2 1/3] search: Separately report matching and non-matching	authors.
Date: Mon, 19 Jan 2015 09:14:48 +0000	[thread overview]
Message-ID: <cunfvb7ku7r.fsf@gargravarr.hh.sledj.net> (raw)
In-Reply-To: <87r3usj7fo.fsf@qmul.ac.uk>

On Sun, Jan 18 2015, Mark Walters wrote:
> On Fri, 24 Oct 2014, David Edmondson <dme@dme.org> wrote:
>> In addition to the 'authors' attribute of each search result, include
>> 'authors_matched' and 'authors_non_matched' attributes. Both
>> attributes are always included and are formatted as a list of
>> authors. If there are no matching authors, the 'authors_non_matched'
>> attribute is set to the empty list.
>
> Hi
>
> Sorry to be so slow reviewing this. Would it be possible to do the
> matching/non-matching stuff in lib/thread.cc and just call that from
> notmuch-search.c? I guess you would need to add a matched_authors, and
> unmatched_authors string to the notmuch_thread struct.
>
> Doing this in search.c seems to redo things that the thread code is
> already doing but maybe I don't really know this code.

Is that different to what I did originally? Austin suggested the
approach in this version (id:20141024124016.GG7970@csail.mit.edu),
unless I misunderstood him.

> Best wishes
>
> Mark
>
>> ---
>>  notmuch-search.c | 105 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
>>  1 file changed, 105 insertions(+)
>>
>> diff --git a/notmuch-search.c b/notmuch-search.c
>> index bc9be45..18c3b20 100644
>> --- a/notmuch-search.c
>> +++ b/notmuch-search.c
>> @@ -22,6 +22,8 @@
>>  #include "sprinter.h"
>>  #include "string-util.h"
>>  
>> +#include <glib.h>
>> +
>>  typedef enum {
>>      OUTPUT_SUMMARY,
>>      OUTPUT_THREADS,
>> @@ -69,6 +71,105 @@ get_thread_query (notmuch_thread_t *thread,
>>      return 0;
>>  }
>>  
>> +/* Return a more pleasent rendering of the mail address
>> + * `nasty_author'. */
>> +static const char *
>> +_nice_author (void *ctx, const char *nasty_author)
>> +{
>> +    const char *nice_author = NULL;
>> +
>> +    InternetAddressList *list = internet_address_list_parse_string (nasty_author);
>> +    if (list) {
>> +	InternetAddress *address = internet_address_list_get_address (list, 0);
>> +	if (address) {
>> +	    nice_author = internet_address_get_name (address);
>> +	    if (nice_author == NULL) {
>> +		InternetAddressMailbox *mailbox = INTERNET_ADDRESS_MAILBOX (address);
>> +		nice_author = internet_address_mailbox_get_addr (mailbox);
>> +	    }
>> +	}
>> +	/* Duplicate the string before `g_object_unref' destroys
>> +	 * it. */
>> +	if (nice_author)
>> +	    nice_author = talloc_strdup (ctx, nice_author);
>> +
>> +	g_object_unref (G_OBJECT (list));
>> +    }
>> +
>> +    if (nice_author)
>> +	return nice_author;
>> +    else
>> +	return nasty_author;
>> +}
>> +
>> +static int
>> +_enumerate_authors (sprinter_t *format,
>> +		 notmuch_thread_t *thread)
>> +{
>> +    notmuch_messages_t *messages;
>> +    GHashTable *matched_hash = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, NULL);
>> +    GHashTable *unmatched_hash = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, NULL);
>> +    GPtrArray *matched_array = g_ptr_array_new ();
>> +    GPtrArray *unmatched_array = g_ptr_array_new ();
>> +
>> +    /* Iterate over the messages in the thread collecting matching and
>> +     * non-matching authors. */
>> +    for (messages = notmuch_thread_get_messages (thread);
>> +	 notmuch_messages_valid (messages);
>> +	 notmuch_messages_move_to_next (messages))
>> +    {
>> +	notmuch_message_t *message = notmuch_messages_get (messages);
>> +	const char *author = _nice_author (thread, notmuch_message_get_header (message, "from"));
>> +
>> +	if (author) {
>> +	    GHashTable *hash;
>> +	    GPtrArray *array;
>> +
>> +	    if (notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH)) {
>> +		hash = matched_hash;
>> +		array = matched_array;
>> +	    } else {
>> +		hash = unmatched_hash;
>> +		array = unmatched_array;
>> +	    }
>> +
>> +	    if (!g_hash_table_lookup_extended (hash, author, NULL, NULL)) {
>> +		char *copy = talloc_strdup (thread, author);
>> +		g_hash_table_insert (hash, copy, NULL);
>> +		g_ptr_array_add (array, (char *) copy);
>> +	    }
>> +	}
>> +    }
>> +
>> +    /* Output the matched authors. */
>> +    unsigned int i;
>> +    format->map_key (format, "authors_matched");
>> +    format->begin_list (format);
>> +    for (i = 0; i < matched_array->len; i++)
>> +	format->string (format, (char *) g_ptr_array_index( matched_array, i));
>> +    format->end (format);
>> +
>> +    /* Output the non-matched authors, but not if they were seen
>> +     * already in the matched authors list. */
>> +    format->map_key (format, "authors_non_matched");
>> +    format->begin_list (format);
>> +    for (i = 0; i < unmatched_array->len; i++) {
>> +	char *author = (char *) g_ptr_array_index( unmatched_array, i);
>> +
>> +	if (!g_hash_table_lookup_extended (matched_hash, author, NULL, NULL))
>> +	    format->string (format, author);
>> +    }
>> +    format->end (format);
>> +
>> +    g_hash_table_unref (matched_hash);
>> +    g_hash_table_unref (unmatched_hash);
>> +
>> +    g_ptr_array_free (matched_array, TRUE);
>> +    g_ptr_array_free (unmatched_array, TRUE);
>> +
>> +    return 0;
>> +}
>> +
>>  static int
>>  do_search_threads (sprinter_t *format,
>>  		   notmuch_query_t *query,
>> @@ -152,6 +253,10 @@ do_search_threads (sprinter_t *format,
>>  		format->integer (format, total);
>>  		format->map_key (format, "authors");
>>  		format->string (format, authors);
>> +		if (_enumerate_authors (format, thread) < 0) {
>> +		    fprintf (stderr, "Out of memory\n");
>> +		    return 1;
>> +		}
>>  		format->map_key (format, "subject");
>>  		format->string (format, subject);
>>  		if (notmuch_format_version >= 2) {
>> -- 
>> 2.1.1
>>
>> _______________________________________________
>> notmuch mailing list
>> notmuch@notmuchmail.org
>> http://notmuchmail.org/mailman/listinfo/notmuch

  reply	other threads:[~2015-01-19  9:15 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-10-24 17:44 [PATCH v2 0/3] Improve the display of matching/non-matching authors David Edmondson
2014-10-24 17:44 ` [PATCH v2 1/3] search: Separately report matching and non-matching authors David Edmondson
2015-01-18 14:19   ` David Bremner
2015-01-18 17:59   ` Mark Walters
2015-01-19  9:14     ` David Edmondson [this message]
2014-10-24 17:44 ` [PATCH v2 2/3] emacs: Improved display of matching/non-matching authors David Edmondson
2015-01-18 14:27   ` David Bremner
2015-01-18 18:10   ` Mark Walters
2015-01-19  9:18     ` David Edmondson
2014-10-24 17:44 ` [PATCH v2 3/3] test: Update tests for 'authors_matched' and authors_non_matched' David Edmondson
2015-01-18 18:12   ` Mark Walters

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://notmuchmail.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=cunfvb7ku7r.fsf@gargravarr.hh.sledj.net \
    --to=dme@dme.org \
    --cc=markwalters1009@gmail.com \
    --cc=notmuch@notmuchmail.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://yhetil.org/notmuch.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).