Index: trac/ticket/templates/query.html
===================================================================
--- trac/ticket/templates/query.html	(revision 8664)
+++ trac/ticket/templates/query.html	(working copy)
@@ -122,7 +122,7 @@
                     </tr>
                   </tbody>
 
-                  <tbody py:with="last_clause = clause_num == len(clauses) - 1">
+                  <tbody py:with="last_clause = clause_num == (len(clauses) or 1) - 1">
                     <tr class="actions">
                       <td class="and" colspan="2">
                         &nbsp;<label for="add_filter_${clause_num}">And</label>&nbsp;
Index: trac/htdocs/js/query.js
===================================================================
--- trac/htdocs/js/query.js	(revision 8664)
+++ trac/htdocs/js/query.js	(working copy)
@@ -33,6 +33,9 @@
         var ctbody = table.closest("tbody");
         if (table.children().length > 2 || !ctbody.siblings().length) {
           tbody.remove();
+          if (!ctbody.siblings().length && table.children().length == 1) {
+            $("#add_clause").attr("disabled", true);
+          }
         } else {
           var add_clause = $("#add_clause", ctbody);
           if (add_clause.length)
@@ -219,6 +222,9 @@
         this.options[this.selectedIndex].disabled = true;
       
       this.selectedIndex = 0;
+
+      // Enable the Or... button if it's been disabled
+      $("#add_clause").attr("disabled", false);
     }).next("div.inlinebuttons").remove();
     
     // Add a new empty clause at the end by cloning the current last clause
@@ -229,13 +235,14 @@
       tbody = tbody.parents("tbody").eq(0);
       var copy = tbody.clone(true);
       $(button).closest("td").next().attr("colSpan", 4).end().remove();
-      $("td.trac-clause-sep", copy).parent().removeAttr("style");
+      $("tr:first", copy).removeAttr("style");
       $("tr tbody:not(:last)", copy).remove();
       var newId = "add_filter_" + clauseNum;
       $("select", copy).attr("id", newId).attr("name", newId)
         .children().enable().end()
         .prev().attr("for", newId);
       tbody.after(copy);
+      $("#add_clause").attr("disabled", true);
     }
     
     // Make the button for adding a clause a client-side trigger
@@ -246,6 +253,10 @@
       addClause(this);
       return false;
     }));
+    if (!add_clause.closest("tbody").siblings().length) {
+      // That is, if there are no filters added to this clause
+      add_clause.attr("disabled", true);
+    }
   }
 
 })(jQuery);

