Index: libyaml/trunk/src/emitter.c
===================================================================
--- libyaml/trunk/src/emitter.c	(revision 215)
+++ libyaml/trunk/src/emitter.c	(revision 216)
@@ -291,5 +291,5 @@
         if (!yaml_emitter_state_machine(emitter, emitter->events.head))
             return 0;
-        DEQUEUE(emitter, emitter->events);
+        yaml_event_delete(&DEQUEUE(emitter, emitter->events));
     }
 
@@ -688,4 +688,11 @@
         emitter->state = YAML_EMIT_DOCUMENT_START_STATE;
 
+        while (!STACK_EMPTY(emitter, emitter->tag_directives)) {
+            yaml_tag_directive_t tag_directive = POP(emitter,
+                    emitter->tag_directives);
+            yaml_free(tag_directive.handle);
+            yaml_free(tag_directive.prefix);
+        }
+
         return 1;
     }
@@ -696,4 +703,5 @@
 
 /*
+ * 
  * Expect a flow item node.
  */
@@ -729,9 +737,14 @@
     }
 
+    if (!first) {
+        if (!yaml_emitter_write_indicator(emitter, ",", 0, 0, 0))
+            return 0;
+    }
+
     if (emitter->canonical || emitter->column > emitter->best_width) {
         if (!yaml_emitter_write_indent(emitter))
             return 0;
     }
-    if (PUSH(emitter, emitter->states, YAML_EMIT_FLOW_SEQUENCE_ITEM_STATE))
+    if (!PUSH(emitter, emitter->states, YAML_EMIT_FLOW_SEQUENCE_ITEM_STATE))
         return 0;
 
@@ -1179,5 +1192,6 @@
     if (style == YAML_LITERAL_SCALAR_STYLE || style == YAML_FOLDED_SCALAR_STYLE)
     {
-        if (!emitter->scalar_data.block_allowed)
+        if (!emitter->scalar_data.block_allowed
+                || emitter->flow_level || emitter->simple_key_context)
             style = YAML_DOUBLE_QUOTED_SCALAR_STYLE;
     }
@@ -1341,5 +1355,5 @@
     handle.pointer ++;
 
-    while (handle.pointer != handle.end-1) {
+    while (handle.pointer < handle.end-1) {
         if (!IS_ALPHA(handle)) {
             return yaml_emitter_set_emitter_error(emitter,
@@ -1381,4 +1395,10 @@
         MOVE(string);
     }
+
+    emitter->anchor_data.anchor = string.start;
+    emitter->anchor_data.anchor_length = string.end - string.start;
+    emitter->anchor_data.alias = alias;
+
+    return 1;
 }
 
@@ -1400,5 +1420,5 @@
 
     for (tag_directive = emitter->tag_directives.start;
-            tag_directive != emitter->tag_directives.end; tag_directive ++) {
+            tag_directive != emitter->tag_directives.top; tag_directive ++) {
         size_t prefix_length = strlen((char *)tag_directive->prefix);
         if (prefix_length < (string.end - string.start)
@@ -1587,4 +1607,23 @@
         }
 
+        if ((spaces || breaks) && string.pointer == string.end-1)
+        {
+            if (spaces && breaks) {
+                mixed_breaks_spaces = 1;
+            }
+            else if (spaces) {
+                if (leading) {
+                    leading_spaces = 1;
+                }
+                trailing_spaces = 1;
+            }
+            else if (breaks) {
+                if (leading) {
+                    leading_breaks = 1;
+                }
+                trailing_breaks = 1;
+            }
+        }
+
         preceeded_by_space = IS_BLANKZ(string);
         MOVE(string);
@@ -1840,6 +1879,6 @@
                 value = *(string.pointer++);
                 if (!PUT(emitter, '%')) return 0;
-                if (!PUT(emitter, (value >> 8)
-                            + ((value >> 8) < 10 ? '0' : 'A' - 10)))
+                if (!PUT(emitter, (value >> 4)
+                            + ((value >> 4) < 10 ? '0' : 'A' - 10)))
                     return 0;
                 if (!PUT(emitter, (value & 0x0F)
@@ -2084,6 +2123,8 @@
                         width = 8;
                     }
-                    for (k = width*4; k >= 0; k -= 4) {
-                        if (!PUT(emitter, (value >> k) & 0x0F)) return 0;
+                    for (k = (width-1)*4; k >= 0; k -= 4) {
+                        int digit = (value >> k) & 0x0F;
+                        if (!PUT(emitter, digit + (digit < 10 ? '0' : 'A'-10)))
+                            return 0;
                     }
             }
@@ -2130,14 +2171,14 @@
     if (string.start == string.pointer)
         return -1;
-    while ((string.pointer[-1] & 0xC0) == 0x80) {
+    do {
         string.pointer --;
-    }
+    } while ((*string.pointer & 0xC0) == 0x80);
     if (!IS_BREAK(string))
         return -1;
     if (string.start == string.pointer)
         return 0;
-    while ((string.pointer[-1] & 0xC0) == 0x80) {
+    do {
         string.pointer --;
-    }
+    } while ((*string.pointer & 0xC0) == 0x80);
     if (!IS_BREAK(string))
         return 0;
@@ -2179,6 +2220,4 @@
     }
 
-    if (!yaml_emitter_write_indent(emitter)) return 0;
-
     return 1;
 }
@@ -2190,6 +2229,6 @@
     yaml_string_t string = STRING(value, length);
     int chomp = yaml_emitter_determine_chomping(emitter, string);
-    int breaks = 0;
-    int leading_spaces = 1;
+    int breaks = 1;
+    int leading_spaces = 0;
 
     if (!yaml_emitter_write_indicator(emitter,
@@ -2235,7 +2274,5 @@
     }
 
-    if (!yaml_emitter_write_indent(emitter)) return 0;
-
-    return 1;
-}
-
+    return 1;
+}
+
