Return-Path: X-Original-To: notmuch@notmuchmail.org Delivered-To: notmuch@notmuchmail.org Received: from localhost (localhost [127.0.0.1]) by olra.theworths.org (Postfix) with ESMTP id 36D18431FC4 for ; Tue, 8 Dec 2009 01:41:03 -0800 (PST) X-Virus-Scanned: Debian amavisd-new at olra.theworths.org Received: from olra.theworths.org ([127.0.0.1]) by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id Lmt3eDTja8vI for ; Tue, 8 Dec 2009 01:41:01 -0800 (PST) Received: from flatline.sindominio.net (flatline.sindominio.net [82.144.4.26]) by olra.theworths.org (Postfix) with ESMTP id 75F96431FC3 for ; Tue, 8 Dec 2009 01:41:01 -0800 (PST) Received: from localhost (localhost.localdomain [127.0.0.1]) by flatline.sindominio.net (Postfix) with ESMTP id CAE57262583; Tue, 8 Dec 2009 10:41:00 +0100 (CET) X-Virus-Scanned: Debian amavisd-new at sindominio.net Received: from flatline.sindominio.net ([127.0.0.1]) by localhost (flatline.sindominio.net [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id PXwM++ZdJgHT; Tue, 8 Dec 2009 10:40:57 +0100 (CET) Received: from blackspot (rpollan-laptop.cern.ch [137.138.192.228]) by flatline.sindominio.net (Postfix) with ESMTPA id DD2D726257F; Tue, 8 Dec 2009 10:40:56 +0100 (CET) Received: by blackspot (Postfix, from userid 1000) id A10448BC3B; Tue, 8 Dec 2009 10:43:04 +0100 (CET) From: meskio@sindominio.net To: notmuch@notmuchmail.org Date: Tue, 8 Dec 2009 10:41:32 +0100 Message-Id: <1260265292-12591-3-git-send-email-meskio@sindominio.net> X-Mailer: git-send-email 1.6.5.4 In-Reply-To: <1260265292-12591-2-git-send-email-meskio@sindominio.net> References: <20091126202347.GA16654@blackspot> <1260265292-12591-1-git-send-email-meskio@sindominio.net> <1260265292-12591-2-git-send-email-meskio@sindominio.net> Subject: [notmuch] [PATCH 2/2] Added regress option to messages iterator X-BeenThere: notmuch@notmuchmail.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: "Use and development of the notmuch mail system." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 08 Dec 2009 09:41:03 -0000 From: Ruben Pollan Added the functions notmuch_messages_regress and notmuch_messages_is_first to notmuch library. With them is possible to iterate backwards on messages. * notmuch_messages_regress do the opposite than notmuch_messages_advance, getting the messages iterator one position backwards. * notmuch_messages_is_first return TRUE if the iterator is in the first message. --- lib/messages.c | 27 +++++++++++++++++++++++++++ lib/notmuch-private.h | 7 +++++++ lib/notmuch.h | 8 ++++++++ lib/query.cc | 24 ++++++++++++++++++++++++ 4 files changed, 66 insertions(+), 0 deletions(-) diff --git a/lib/messages.c b/lib/messages.c index 5414f87..2c28738 100644 --- a/lib/messages.c +++ b/lib/messages.c @@ -90,6 +90,7 @@ _notmuch_messages_create (notmuch_message_list_t *list) messages->is_of_list_type = TRUE; messages->iterator = list->head; + messages->previous_node = NULL; return messages; } @@ -121,6 +122,18 @@ notmuch_messages_has_more (notmuch_messages_t *messages) return (messages->iterator != NULL); } +notmuch_bool_t +notmuch_messages_is_first (notmuch_messages_t *messages) +{ + if (messages == NULL) + return TRUE; + + if (! messages->is_of_list_type) + return _notmuch_mset_messages_is_first (messages); + + return (messages->previous_node == NULL); +} + notmuch_message_t * notmuch_messages_get (notmuch_messages_t *messages) { @@ -142,10 +155,24 @@ notmuch_messages_advance (notmuch_messages_t *messages) if (messages->iterator == NULL) return; + messages->previous_node = messages->iterator; messages->iterator = messages->iterator->next; } void +notmuch_messages_regress (notmuch_messages_t *messages) +{ + if (! messages->is_of_list_type) + return _notmuch_mset_messages_regress (messages); + + if (messages->previous_node == NULL) + return; + + messages->iterator = messages->previous_node; + messages->previous_node = messages->iterator->prev; +} + +void notmuch_messages_destroy (notmuch_messages_t *messages) { talloc_free (messages); diff --git a/lib/notmuch-private.h b/lib/notmuch-private.h index 133ed0e..5852c00 100644 --- a/lib/notmuch-private.h +++ b/lib/notmuch-private.h @@ -299,6 +299,7 @@ typedef struct _notmuch_message_list { */ struct _notmuch_messages { notmuch_bool_t is_of_list_type; + notmuch_message_node_t *previous_node; notmuch_message_node_t *iterator; }; @@ -321,12 +322,18 @@ _notmuch_messages_create (notmuch_message_list_t *list); notmuch_bool_t _notmuch_mset_messages_has_more (notmuch_messages_t *messages); +notmuch_bool_t +_notmuch_mset_messages_is_first (notmuch_messages_t *messages); + notmuch_message_t * _notmuch_mset_messages_get (notmuch_messages_t *messages); void _notmuch_mset_messages_advance (notmuch_messages_t *messages); +void +_notmuch_mset_messages_regress (notmuch_messages_t *messages); + /* message.cc */ void diff --git a/lib/notmuch.h b/lib/notmuch.h index 60834fb..69bd98a 100644 --- a/lib/notmuch.h +++ b/lib/notmuch.h @@ -604,6 +604,10 @@ notmuch_thread_destroy (notmuch_thread_t *thread); notmuch_bool_t notmuch_messages_has_more (notmuch_messages_t *messages); +/* Is the given notmuch_messages_t object on the first message */ +notmuch_bool_t +notmuch_messages_is_first (notmuch_messages_t *messages); + /* Get the current message from 'messages' as a notmuch_message_t. * * Note: The returned message belongs to 'messages' and has a lifetime @@ -626,6 +630,10 @@ notmuch_messages_get (notmuch_messages_t *messages); void notmuch_messages_advance (notmuch_messages_t *messages); +/* Regress the 'messages' iterator to the previous result. */ +void +notmuch_messages_regress (notmuch_messages_t *messages); + /* Destroy a notmuch_messages_t object. * * It's not strictly necessary to call this function. All memory from diff --git a/lib/query.cc b/lib/query.cc index 9106b92..94a6860 100644 --- a/lib/query.cc +++ b/lib/query.cc @@ -35,6 +35,7 @@ typedef struct _notmuch_mset_messages { notmuch_messages_t base; notmuch_database_t *notmuch; Xapian::MSetIterator iterator; + Xapian::MSetIterator iterator_begin; Xapian::MSetIterator iterator_end; } notmuch_mset_messages_t; @@ -86,6 +87,7 @@ static int _notmuch_messages_destructor (notmuch_mset_messages_t *messages) { messages->iterator.~MSetIterator (); + messages->iterator_begin.~MSetIterator (); messages->iterator_end.~MSetIterator (); return 0; @@ -108,6 +110,7 @@ notmuch_query_search_messages (notmuch_query_t *query) messages->base.iterator = NULL; messages->notmuch = notmuch; new (&messages->iterator) Xapian::MSetIterator (); + new (&messages->iterator_begin) Xapian::MSetIterator (); new (&messages->iterator_end) Xapian::MSetIterator (); talloc_set_destructor (messages, _notmuch_messages_destructor); @@ -155,6 +158,7 @@ notmuch_query_search_messages (notmuch_query_t *query) mset = enquire.get_mset (0, notmuch->xapian_db->get_doccount ()); messages->iterator = mset.begin (); + messages->iterator_end = mset.begin (); messages->iterator_end = mset.end (); } catch (const Xapian::Error &error) { @@ -177,6 +181,16 @@ _notmuch_mset_messages_has_more (notmuch_messages_t *messages) return (mset_messages->iterator != mset_messages->iterator_end); } +notmuch_bool_t +_notmuch_mset_messages_is_first (notmuch_messages_t *messages) +{ + notmuch_mset_messages_t *mset_messages; + + mset_messages = (notmuch_mset_messages_t *) messages; + + return (mset_messages->iterator == mset_messages->iterator_begin); +} + notmuch_message_t * _notmuch_mset_messages_get (notmuch_messages_t *messages) { @@ -215,6 +229,16 @@ _notmuch_mset_messages_advance (notmuch_messages_t *messages) mset_messages->iterator++; } +void +_notmuch_mset_messages_regress (notmuch_messages_t *messages) +{ + notmuch_mset_messages_t *mset_messages; + + mset_messages = (notmuch_mset_messages_t *) messages; + + mset_messages->iterator--; +} + /* Glib objects force use to use a talloc destructor as well, (but not * nearly as ugly as the for messages due to C++ objects). At * this point, I'd really like to have some talloc-friendly -- 1.6.5.4