--- /dev/null
+Return-Path: <markwalters1009@gmail.com>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+ by olra.theworths.org (Postfix) with ESMTP id 95B2B431FC7\r
+ for <notmuch@notmuchmail.org>; Wed, 21 May 2014 02:59:09 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: 0.201\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=0.201 tagged_above=-999 required=5\r
+ tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1,\r
+ FREEMAIL_ENVFROM_END_DIGIT=1, FREEMAIL_FROM=0.001,\r
+ RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled\r
+Received: from olra.theworths.org ([127.0.0.1])\r
+ by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
+ with ESMTP id YYHDEm9qyWvN for <notmuch@notmuchmail.org>;\r
+ Wed, 21 May 2014 02:59:04 -0700 (PDT)\r
+Received: from mail-wi0-f176.google.com (mail-wi0-f176.google.com\r
+ [209.85.212.176]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
+ (No client certificate requested)\r
+ by olra.theworths.org (Postfix) with ESMTPS id E469F431FC4\r
+ for <notmuch@notmuchmail.org>; Wed, 21 May 2014 02:59:03 -0700 (PDT)\r
+Received: by mail-wi0-f176.google.com with SMTP id n15so7360239wiw.3\r
+ for <notmuch@notmuchmail.org>; Wed, 21 May 2014 02:59:01 -0700 (PDT)\r
+DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113;\r
+ h=from:to:cc:subject:date:message-id:in-reply-to:references;\r
+ bh=UNWEXWTz/XV+fy3Enf5s+AntmoVmdr0PRCeLNzbJz08=;\r
+ b=pLrOHbAHE1p9gWirIFTrSwY63SSP4pT7CXrCTmDOTaW29XAzPR3Zwugf22merJBltm\r
+ BezFgST38/AlvLUwRv1mGoGYXobpk3yCUs97UFycHoAsB4t9KZ1pWYdldU+MNqOCh1mv\r
+ GL3JVfXY6mszNKzY3ayaEdiA34ea7RjHBshX2dEwu5gf5m6ZgDs5JDeZ3N93gDuPRNpN\r
+ ss3hNcVFbtHtUOymYpuHfdxhlyOAQGmbouEjWLecvnhTgT5PnhYSRCXkas+//yviW3rh\r
+ VlYtxI9SXx9ONe2a0QFpx7XB6icbb4/GkA0XNLC4A0LYEJjGbA33TM5wwfGpBOgW3wF1\r
+ 7ayA==\r
+X-Received: by 10.194.221.69 with SMTP id qc5mr800900wjc.91.1400666341522;\r
+ Wed, 21 May 2014 02:59:01 -0700 (PDT)\r
+Received: from localhost (5751dfa2.skybroadband.com. [87.81.223.162])\r
+ by mx.google.com with ESMTPSA id\r
+ kp5sm21930823wjb.30.2014.05.21.02.59.00 for <multiple recipients>\r
+ (version=TLSv1.2 cipher=RC4-SHA bits=128/128);\r
+ Wed, 21 May 2014 02:59:00 -0700 (PDT)\r
+From: Mark Walters <markwalters1009@gmail.com>\r
+To: notmuch@notmuchmail.org,\r
+ David Edmondson <dme@dme.org>\r
+Subject: [PATCH] emacs: make sure tagging on an empty query is harmless\r
+Date: Wed, 21 May 2014 10:58:50 +0100\r
+Message-Id: <1400666330-4363-1-git-send-email-markwalters1009@gmail.com>\r
+X-Mailer: git-send-email 1.7.10.4\r
+In-Reply-To: <87iop0ynl2.fsf@qmul.ac.uk>\r
+References: <87iop0ynl2.fsf@qmul.ac.uk>\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.13\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+ <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
+List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
+List-Post: <mailto:notmuch@notmuchmail.org>\r
+List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
+List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Wed, 21 May 2014 09:59:09 -0000\r
+\r
+Currently notmuch-tag throws a "wrong-type-argument stringp nil" if\r
+passed a nil query-string. Catch this and provide a more useful error\r
+message. This fixes a case in notmuch-tree (if you try to tag when at\r
+the end of the buffer).\r
+\r
+Secondly, as pointed out by David (dme)\r
+`notmuch-search-find-stable-query-region' can return the query string\r
+() if there are no messages in the region. This gets passed to notmuch\r
+tag, and due to interactions in the optimize_query code in\r
+notmuch-tag.c becomes, in the case tag-change is -inbox, "( () ) and\r
+(tag:inbox)". This query matches some strange collection of messages\r
+which then get archived. This should probably be fixed, but in any\r
+case make `notmuch-search-find-stable-query-region' return a nil\r
+query-string in this case.\r
+\r
+This avoids data-loss (random tag removal) in this case.\r
+---\r
+\r
+This is my attempt to solve the same problem as the parent. I prefer\r
+not throwing an error in n.s.f.s.q.r as it is difficult for the caller\r
+to catch cleanly. Throwing it in notmuch-tag is fine as the caller can\r
+trivially check for query-string being nil before calling notmuch-tag\r
+if it wants to deal with it gracefully.\r
+\r
+If people do prefer an error in n.s.f.s.q.r as in the parent patch\r
+then I think we should update the error message. The first hunk of\r
+this should also be applied to catch nil queries to notmuch-tag gracefully.\r
+\r
+Although this has been present for a while I think it is a dataloss\r
+issue so a fix should go in for 0.18.1\r
+\r
+Best wishes\r
+\r
+Mark\r
+\r
+\r
+\r
+\r
+ emacs/notmuch-tag.el | 2 ++\r
+ emacs/notmuch.el | 6 ++++--\r
+ 2 files changed, 6 insertions(+), 2 deletions(-)\r
+\r
+diff --git a/emacs/notmuch-tag.el b/emacs/notmuch-tag.el\r
+index 07c260e..f54aa9d 100644\r
+--- a/emacs/notmuch-tag.el\r
++++ b/emacs/notmuch-tag.el\r
+@@ -387,6 +387,8 @@ (defun notmuch-tag (query tag-changes)\r
+ (unless (string-match-p "^[-+]\\S-+$" tag-change)\r
+ (error "Tag must be of the form `+this_tag' or `-that_tag'")))\r
+ tag-changes)\r
++ (unless query\r
++ (error "Nothing to tag!"))\r
+ (unless (null tag-changes)\r
+ (run-hooks 'notmuch-before-tag-hook)\r
+ (if (<= (length query) notmuch-tag-argument-limit)\r
+diff --git a/emacs/notmuch.el b/emacs/notmuch.el\r
+index 6c0bc1b..1adea9c 100644\r
+--- a/emacs/notmuch.el\r
++++ b/emacs/notmuch.el\r
+@@ -428,14 +428,16 @@ (defun notmuch-search-find-stable-query-region (beg end &optional only-matched)\r
+ "Return the stable query for the current region.\r
+ \r
+ If ONLY-MATCHED is non-nil, include only matched messages. If it\r
+-is nil, include both matched and unmatched messages."\r
++is nil, include both matched and unmatched messages. If there are\r
++no messages in the region then return nil."\r
+ (let ((query-list nil) (all (not only-matched)))\r
+ (dolist (queries (notmuch-search-properties-in-region :query beg end))\r
+ (when (first queries)\r
+ (push (first queries) query-list))\r
+ (when (and all (second queries))\r
+ (push (second queries) query-list)))\r
+- (concat "(" (mapconcat 'identity query-list ") or (") ")")))\r
++ (when query-list\r
++ (concat "(" (mapconcat 'identity query-list ") or (") ")"))))\r
+ \r
+ (defun notmuch-search-find-authors ()\r
+ "Return the authors for the current thread"\r
+-- \r
+1.7.10.4\r
+\r