# This patch file was generated by NetBeans IDE
# This patch can be applied using context Tools: Apply Diff Patch action on respective folder.
# It uses platform neutral UTF-8 encoding.
# Above lines and this line are ignored by the patching process.
Index: drupal/modules/taxonomy/taxonomy.pages.inc
--- drupal/modules/taxonomy/taxonomy.pages.inc Base (1.23)
+++ drupal/modules/taxonomy/taxonomy.pages.inc Locally Modified (Based On 1.23)
@@ -117,29 +117,40 @@
  * Helper function for autocompletion
  */
 function taxonomy_autocomplete($vid, $string = '') {
+    /*
+     * Automatically add a closing quote to the end of string if the last term
+     * contains an opening quote.  If this is not added, the last term will be
+     * discarded by drupal_explode_tags
+     */
+    if( preg_match('/,\s*\"(\"{2}|[^\"])*$/', $string) ) $string .= '"';
+
   // The user enters a comma-separated list of tags. We only autocomplete the last tag.
   $array = drupal_explode_tags($string);
 
+    $matches = array();
+
   // Fetch last tag
   $last_string = trim(array_pop($array));
-  $matches = array();
-  if ($last_string != '') {
-    $result = db_query_range(db_rewrite_sql("SELECT t.tid, t.name FROM {taxonomy_term_data} t WHERE t.vid = :vid AND LOWER(t.name) LIKE LOWER(:last_string)", 't', 'tid'), array(
-      ':vid' => $vid,
-      ':last_string' => '%'. $last_string .'%',
-    ), 0, 10);
 
-    $prefix = count($array) ? implode(', ', $array) . ', ' : '';
+    /*
+     * Dont bother doing anything if the string passed ends in a comma followed
+     * by zero or more whitespace or quote (") characters
+     */
+    if( !preg_match('/,[\s\"]*$/', $string) ) {
 
+
+        $result = db_query_range(db_rewrite_sql("SELECT t.tid, t.name FROM {term_data} t WHERE t.vid = %d AND LOWER(t.name) LIKE LOWER('%%%s%%')", 't', 'tid'), $vid, $last_string, 0, 10);
+
     while ($tag = db_fetch_object($result)) {
-      $n = $tag->name;
-      // Commas and quotes in terms are special cases, so encode 'em.
-      if (strpos($tag->name, ',') !== FALSE || strpos($tag->name, '"') !== FALSE) {
-        $n = '"' . str_replace('"', '""', $tag->name) . '"';
+
+            // Don't list if it would result in a duplicate
+            if( array_search($tag->name, $array) !== FALSE ) continue;
+
+            // Add tags->name to existing tags and implode
+            $replacement = drupal_implode_tags( array_merge($array, array($tag->name) ) );
+            $matches[$replacement] = check_plain($tag->name);
       }
-      $matches[$prefix . $n] = check_plain($tag->name);
     }
-  }
 
   drupal_json($matches);
 }
