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 3C6CE431FAF for ; Wed, 25 Jan 2012 22:36:08 -0800 (PST) X-Virus-Scanned: Debian amavisd-new at olra.theworths.org X-Spam-Flag: NO X-Spam-Score: -0.7 X-Spam-Level: X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5 tests=[RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled 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 NGJiIbzZGf2K for ; Wed, 25 Jan 2012 22:36:07 -0800 (PST) Received: from dmz-mailsec-scanner-7.mit.edu (DMZ-MAILSEC-SCANNER-7.MIT.EDU [18.7.68.36]) by olra.theworths.org (Postfix) with ESMTP id DAC07431FAE for ; Wed, 25 Jan 2012 22:36:06 -0800 (PST) X-AuditID: 12074424-b7fae6d000000906-f1-4f20f4562e8f Received: from mailhub-auth-4.mit.edu ( [18.7.62.39]) by dmz-mailsec-scanner-7.mit.edu (Symantec Messaging Gateway) with SMTP id 8D.0C.02310.654F02F4; Thu, 26 Jan 2012 01:36:06 -0500 (EST) Received: from outgoing.mit.edu (OUTGOING-AUTH.MIT.EDU [18.7.22.103]) by mailhub-auth-4.mit.edu (8.13.8/8.9.2) with ESMTP id q0Q6a5bg015070; Thu, 26 Jan 2012 01:36:06 -0500 Received: from awakening.csail.mit.edu (awakening.csail.mit.edu [18.26.4.91]) (authenticated bits=0) (User authenticated as amdragon@ATHENA.MIT.EDU) by outgoing.mit.edu (8.13.6/8.12.4) with ESMTP id q0Q6a3nx002933 (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NOT); Thu, 26 Jan 2012 01:36:04 -0500 (EST) Received: from amthrax by awakening.csail.mit.edu with local (Exim 4.77) (envelope-from ) id 1RqIve-0000mM-Uj; Thu, 26 Jan 2012 01:35:27 -0500 Date: Thu, 26 Jan 2012 01:35:26 -0500 From: Austin Clements To: Dmitry Kurochkin Subject: Re: [RFC PATCH 5/5] show: Simplify new text formatter code Message-ID: <20120126063526.GB1940@mit.edu> References: <1326332973-30225-1-git-send-email-amdragon@mit.edu> <1326332973-30225-6-git-send-email-amdragon@mit.edu> <87lioxna2n.fsf@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <87lioxna2n.fsf@gmail.com> User-Agent: Mutt/1.5.21 (2010-09-15) X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFmphleLIzCtJLcpLzFFi42IRYrdT1w37ouBvcPCIsMXVrf3sFtdvzmR2 YPLYOesuu8ezVbeYA5iiuGxSUnMyy1KL9O0SuDJWXd3PVLAxuaJ50mP2Bsajvl2MnBwSAiYS s04sZYewxSQu3FvP1sXIxSEksI9RYsK/R4wQzgZGifOzdkI5J5kkzhx+ywThLGGU2HLiBFg/ i4CqxOKps5lAbDYBDYlt+5czgtgiAoYSty6+YgaxmQWkJb79bgarERZwlnh67gDQPg4OXgFt iRtXHCBmTmGUuP7gNthMXgFBiZMzn7BA9GpJ3Pj3kgmkHmTO8n8cIGFOAXWJJetbwFaJCqhI TDm5jW0Co9AsJN2zkHTPQuhewMi8ilE2JbdKNzcxM6c4NVm3ODkxLy+1SNdcLzezRC81pXQT IziwXVR2MDYfUjrEKMDBqMTDu8NIwV+INbGsuDL3EKMkB5OSKO+2T0AhvqT8lMqMxOKM+KLS nNTiQ4wSHMxKIrxi9kA53pTEyqrUonyYlDQHi5I4r4bWOz8hgfTEktTs1NSC1CKYrAwHh5IE b/5noEbBotT01Iq0zJwShDQTByfIcB6g4YogNbzFBYm5xZnpEPlTjIpS4rxbQRICIImM0jy4 XljiecUoDvSKMO9ckCoeYNKC634FNJgJaPBSXXmQwSWJCCmpBsbKH13Xz7zwfH340bGISPGw +8/F33z/zuGd8qRm1a6li67e6f0+c7PhnG6htVcXi7Quedvw6ULty0MKL+p6tNjLbkzzTZp2 tZbZa6v8tuOifknp/np5Hzu6kj6vEii557H9SaD48YIndzS1YplO33Jz//Zhim5EpdVL3aly G1cyHTx52HVW7I6JSizFGYmGWsxFxYkAb+K+dhcDAAA= Cc: notmuch@notmuchmail.org X-BeenThere: notmuch@notmuchmail.org X-Mailman-Version: 2.1.13 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: Thu, 26 Jan 2012 06:36:08 -0000 Quoth Dmitry Kurochkin on Jan 24 at 5:49 am: > On Wed, 11 Jan 2012 20:49:33 -0500, Austin Clements wrote: > > This makes the text formatter take advantage of the new code > > structure. The previously duplicated header logic is now unified, > > several things that we used to compute repeatedly across different > > callbacks are now computed once, and the code is generally simplified. > > > > Unifying the header logic causes this to format some dates slightly > > differently, so the two affected test cases are updated. > > Thanks for these patches, Austin. They are a definite improvement for > notmuch show code. I hope it would soon get pushed to master. And I > hope more patches would follow :) > > Few minor comments below. > > > --- > > notmuch-show.c | 84 ++++++++++++---------------------------------------- > > test/crypto | 2 +- > > test/thread-naming | 16 +++++----- > > 3 files changed, 28 insertions(+), 74 deletions(-) > > > > diff --git a/notmuch-show.c b/notmuch-show.c > > index 3241965..1689222 100644 > > --- a/notmuch-show.c > > +++ b/notmuch-show.c > > @@ -175,67 +175,42 @@ format_part_text (const void *ctx, mime_node_t *node, > > GMimeObject *meta = node->envelope_part ? > > GMIME_OBJECT (node->envelope_part) : node->part; > > GMimeContentType *content_type = g_mime_object_get_content_type (meta); > > + GMimeContentDisposition *disposition = > > + g_mime_object_get_content_disposition (meta); > > + notmuch_bool_t attachment = disposition && > > + strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0; > > If I did not miss anything, attachment is used only as following: > > attachment ? "attachment" : "part" > > Please make it const char[] and set to "attachment" or "part". Ooh, good catch. But I'll do you one better. I introduced a const char *part_type that's set to "attachment", "part", *or* "message" in the if structure below. That eliminates the attachment variable, moves the disposition variable back down into one of the early if bodies, and eliminates the entire conditional the figures out what to print to close the block. > > + notmuch_bool_t leaf = GMIME_IS_PART (node->part); > > Please add const where possible to local variables (e.g. attachment, leaf). Generally I consider these overkill on local non-pointer variables, but okay. leaf wound up being the only one I had to add it to. > > int i; > > > > if (node->envelope_file) { > > notmuch_message_t *message = node->envelope_file; > > - const char *headers[] = { > > - "Subject", "From", "To", "Cc", "Bcc", "Date" > > - }; > > - const char *name, *value; > > - unsigned int i; > > > > - printf ("\fmessage{ "); > > - printf ("id:%s depth:%d match:%d filename:%s\n", > > + printf ("\fmessage{ id:%s depth:%d match:%d filename:%s\n", > > notmuch_message_get_message_id (message), > > indent, > > notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH), > > notmuch_message_get_filename (message)); > > - > > - printf ("\fheader{\n"); > > - > > - printf ("%s\n", _get_one_line_summary (ctx, message)); > > - > > - for (i = 0; i < ARRAY_SIZE (headers); i++) { > > - name = headers[i]; > > - value = notmuch_message_get_header (message, name); > > - if (value && strlen (value)) > > - printf ("%s: %s\n", name, value); > > - } > > - printf ("\fheader}\n"); > > Yay! Only one header-printing code left :) > > > } else { > > - GMimeContentDisposition *disposition = g_mime_object_get_content_disposition (meta); > > const char *cid = g_mime_object_get_content_id (meta); > > + const char *filename = leaf ? > > + g_mime_part_get_filename (GMIME_PART (node->part)) : NULL; > > > > - if (disposition && > > - strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0) > > - { > > - printf ("\fattachment{ ID: %d", node->part_num); > > - > > - } else { > > - > > - printf ("\fpart{ ID: %d", node->part_num); > > - } > > - > > - if (GMIME_IS_PART (node->part)) > > - { > > - const char *filename = g_mime_part_get_filename (GMIME_PART (node->part)); > > - if (filename) > > - printf (", Filename: %s", filename); > > - } > > - > > + printf ("\f%s{ ID: %d", attachment ? "attachment" : "part", node->part_num); > > + if (filename) > > I always forget about it, can we declare variables inside if condition > like in C++? I.e.: > > if (const char *filename = leaf ? g_mime_part_get_filename (GMIME_PART (node->part)) : NULL) > > If yes, I would prefer to use this style where possible. You can do that in C++? You definitely can't do that in C. > > + printf (", Filename: %s", filename); > > if (cid) > > printf (", Content-id: %s", cid); > > - > > I would revert blank line changes. But I do not insist :) Eh. I'm generally opposed to drive-by fixes, but this code inherited a lot of funny formatting that just interrupts the whole flow when the code is all in one place. > > printf (", Content-type: %s\n", g_mime_content_type_to_string (content_type)); > > } > > > > - if (node->envelope_part) { > > + if (GMIME_IS_MESSAGE (node->part)) { > > This condition is repeated at least twice. Please consider moving the > message variable below to the top level and using it in the conditions. If the downcast had to be done in both places I would, but since the conditional itself is the only repetition, I prefer to minimize the scope of the message variable. Thanks for the review! I'll send a new version shortly. > Regards, > Dmitry > > > GMimeMessage *message = GMIME_MESSAGE (node->part); > > InternetAddressList *recipients; > > const char *recipients_string; > > > > printf ("\fheader{\n"); > > + if (node->envelope_file) > > + printf ("%s\n", _get_one_line_summary (ctx, node->envelope_file)); > > printf ("Subject: %s\n", g_mime_message_get_subject (message)); > > printf ("From: %s\n", g_mime_message_get_sender (message)); > > recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_TO); > > @@ -248,9 +223,11 @@ format_part_text (const void *ctx, mime_node_t *node, > > printf ("Cc: %s\n", recipients_string); > > printf ("Date: %s\n", g_mime_message_get_date_as_string (message)); > > printf ("\fheader}\n"); > > + > > + printf ("\fbody{\n"); > > } > > > > - if (!node->envelope_file) { > > + if (leaf) { > > if (g_mime_content_type_is_type (content_type, "text", "*") && > > !g_mime_content_type_is_type (content_type, "text", "html")) > > { > > @@ -258,24 +235,12 @@ format_part_text (const void *ctx, mime_node_t *node, > > g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE); > > show_text_part_content (node->part, stream_stdout); > > g_object_unref(stream_stdout); > > - } > > - else if (g_mime_content_type_is_type (content_type, "multipart", "*") || > > - g_mime_content_type_is_type (content_type, "message", "rfc822")) > > - { > > - /* Do nothing for multipart since its content will be printed > > - * when recursing. */ > > - } > > - else > > - { > > + } else { > > printf ("Non-text part: %s\n", > > g_mime_content_type_to_string (content_type)); > > } > > } > > > > - if (GMIME_IS_MESSAGE (node->part)) { > > - printf ("\fbody{\n"); > > - } > > - > > for (i = 0; i < node->nchildren; i++) > > format_part_text (ctx, mime_node_child (node, i), indent, params); > > > > @@ -286,18 +251,7 @@ format_part_text (const void *ctx, mime_node_t *node, > > if (node->envelope_file) { > > printf ("\fmessage}\n"); > > } else { > > - GMimeContentDisposition *disposition; > > - > > - disposition = g_mime_object_get_content_disposition (meta); > > - if (disposition && > > - strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0) > > - { > > - printf ("\fattachment}\n"); > > - } > > - else > > - { > > - printf ("\fpart}\n"); > > - } > > + printf ("\f%s}\n", attachment ? "attachment" : "part"); > > } > > } > > > > diff --git a/test/crypto b/test/crypto > > index 0af4aa8..6723ef8 100755 > > --- a/test/crypto > > +++ b/test/crypto > > @@ -157,7 +157,7 @@ Notmuch Test Suite (2000-01-01) (encrypted inbox) > > Subject: test encrypted message 001 > > From: Notmuch Test Suite > > To: test_suite@notmuchmail.org > > -Date: 01 Jan 2000 12:00:00 -0000 > > +Date: Sat, 01 Jan 2000 12:00:00 +0000 > > header} > > body{ > > part{ ID: 1, Content-type: multipart/encrypted > > diff --git a/test/thread-naming b/test/thread-naming > > index 41b97d9..b7c96f2 100755 > > --- a/test/thread-naming > > +++ b/test/thread-naming > > @@ -71,7 +71,7 @@ Notmuch Test Suite (2001-01-05) (unread) > > Subject: thread-naming: Initial thread subject > > From: Notmuch Test Suite > > To: Notmuch Test Suite > > -Date: Fri, 05 Jan 2001 15:43:56 -0000 > > +Date: Fri, 05 Jan 2001 15:43:56 +0000 > > header} > > body{ > > part{ ID: 1, Content-type: text/plain > > @@ -85,7 +85,7 @@ Notmuch Test Suite (2001-01-06) (inbox unread) > > Subject: thread-naming: Older changed subject > > From: Notmuch Test Suite > > To: Notmuch Test Suite > > -Date: Sat, 06 Jan 2001 15:43:56 -0000 > > +Date: Sat, 06 Jan 2001 15:43:56 +0000 > > header} > > body{ > > part{ ID: 1, Content-type: text/plain > > @@ -99,7 +99,7 @@ Notmuch Test Suite (2001-01-07) (inbox unread) > > Subject: thread-naming: Newer changed subject > > From: Notmuch Test Suite > > To: Notmuch Test Suite > > -Date: Sun, 07 Jan 2001 15:43:56 -0000 > > +Date: Sun, 07 Jan 2001 15:43:56 +0000 > > header} > > body{ > > part{ ID: 1, Content-type: text/plain > > @@ -113,7 +113,7 @@ Notmuch Test Suite (2001-01-08) (unread) > > Subject: thread-naming: Final thread subject > > From: Notmuch Test Suite > > To: Notmuch Test Suite > > -Date: Mon, 08 Jan 2001 15:43:56 -0000 > > +Date: Mon, 08 Jan 2001 15:43:56 +0000 > > header} > > body{ > > part{ ID: 1, Content-type: text/plain > > @@ -127,7 +127,7 @@ Notmuch Test Suite (2001-01-09) (inbox unread) > > Subject: Re: thread-naming: Initial thread subject > > From: Notmuch Test Suite > > To: Notmuch Test Suite > > -Date: Tue, 09 Jan 2001 15:43:45 -0000 > > +Date: Tue, 09 Jan 2001 15:43:45 +0000 > > header} > > body{ > > part{ ID: 1, Content-type: text/plain > > @@ -141,7 +141,7 @@ Notmuch Test Suite (2001-01-10) (inbox unread) > > Subject: Aw: thread-naming: Initial thread subject > > From: Notmuch Test Suite > > To: Notmuch Test Suite > > -Date: Wed, 10 Jan 2001 15:43:45 -0000 > > +Date: Wed, 10 Jan 2001 15:43:45 +0000 > > header} > > body{ > > part{ ID: 1, Content-type: text/plain > > @@ -155,7 +155,7 @@ Notmuch Test Suite (2001-01-11) (inbox unread) > > Subject: Vs: thread-naming: Initial thread subject > > From: Notmuch Test Suite > > To: Notmuch Test Suite > > -Date: Thu, 11 Jan 2001 15:43:45 -0000 > > +Date: Thu, 11 Jan 2001 15:43:45 +0000 > > header} > > body{ > > part{ ID: 1, Content-type: text/plain > > @@ -169,7 +169,7 @@ Notmuch Test Suite (2001-01-12) (inbox unread) > > Subject: Sv: thread-naming: Initial thread subject > > From: Notmuch Test Suite > > To: Notmuch Test Suite > > -Date: Fri, 12 Jan 2001 15:43:45 -0000 > > +Date: Fri, 12 Jan 2001 15:43:45 +0000 > > header} > > body{ > > part{ ID: 1, Content-type: text/plain > -- Austin Clements MIT/'06/PhD/CSAIL amdragon@mit.edu http://web.mit.edu/amdragon Somewhere in the dream we call reality you will find me, searching for the reality we call dreams.