# HG changeset patch
# User Ben Abbott  <bpabbott@mac.com>
# Date 1324962614 18000
# Branch stable
# Node ID 1c1fa5b07e4bd4650503e94a321725c38c63c8b1
# Parent  c5222658dc3cb938b6250b2e01f2daf052d8c501
gnuplot backend improvements.

* __go_draw_axes__.m: Allow edgecolor to render for 3D triangular patches.
  Reliably interpret edgecolor == "auto".

diff --git a/scripts/plot/private/__go_draw_axes__.m b/scripts/plot/private/__go_draw_axes__.m
--- a/scripts/plot/private/__go_draw_axes__.m
+++ b/scripts/plot/private/__go_draw_axes__.m
@@ -442,6 +442,17 @@
 
       obj = get (kids(end));
 
+     if (isfield (obj, "markeredgecolor")
+         && strcmp (obj.markeredgecolor, "auto"))
+       if (strcmp (obj.marker, "none"))
+         obj.markeredgecolor = obj.marker;
+       elseif (strcmp (obj.type, "line"))
+         obj.markeredgecolor = obj.color;
+       elseif (any (strcmp (obj.type, {"patch", "surface"})))
+         obj.markeredgecolor = obj.facecolor;
+       endif
+     endif
+
       if (isfield (obj, "units"))
         units = obj.units;
         unwind_protect
@@ -486,6 +497,7 @@
             cdatadirect = true;
           endif
           data_idx++;
+          gp_cmds(data_idx) = {{}};
           is_image_data(data_idx) = true;
           parametric(data_idx) = false;
           have_cdata(data_idx) = false;
@@ -546,6 +558,7 @@
             continue;
           endif
           data_idx++;
+          gp_cmds(data_idx) = {{}};
           is_image_data(data_idx) = false;
           parametric(data_idx) = true;
           have_cdata(data_idx) = false;
@@ -585,6 +598,7 @@
 
           if (length (style) > 1)
             data_idx++;
+            gp_cmds(data_idx) = {{}};
             is_image_data(data_idx) = is_image_data(data_idx - 1);
             parametric(data_idx) = parametric(data_idx - 1);
             have_cdata(data_idx) = have_cdata(data_idx - 1);
@@ -597,6 +611,7 @@
           endif
           if (length (style) > 2)
             data_idx++;
+            gp_cmds(data_idx) = {{}};
             is_image_data(data_idx) = is_image_data(data_idx - 1);
             parametric(data_idx) = parametric(data_idx - 1);
             have_cdata(data_idx) = have_cdata(data_idx - 1);
@@ -647,6 +662,7 @@
                    error ("__go_draw_axes__: gnuplot (as of v4.2) only supports 3D filled triangular patches");
                  else
                    if (isnan (data_3d_idx))
+                     ## This produces the faces and the edges
                      data_idx++;
                      data_3d_idx = data_idx;
                      is_image_data(data_idx) = false;
@@ -656,12 +672,24 @@
                      withclause{data_3d_idx} = sprintf ("with pm3d");
                      usingclause{data_3d_idx} =  "using 1:2:3:4";
                      data{data_3d_idx} = [];
+                     if (! strcmp (obj.edgecolor, "none"))
+                       style = do_linestyle_command (obj, obj.edgecolor,
+                                                     data_idx, mono,
+                                                     plot_stream);
+                       cmd = sprintf ("set pm3d depthorder hidden3d %d", data_idx);
+                       gp_cmds(data_idx) = {{cmd}};
+                       ## The edgecolor will already be rendered
+                       obj.edgecolor = "none";
+                     else
+                       gp_cmds(data_idx) = {{}};
+                     endif
                    endif
                    local_idx = data_3d_idx;
                    ccdat = NaN;
                  endif
                else
                  data_idx++;
+                 gp_cmds(data_idx) = {{}};
                  local_idx = data_idx;
                  is_image_data(data_idx) = false;
                  parametric(data_idx) = false;
@@ -780,6 +808,7 @@
                           && strncmp (obj.markerfacecolor, "none", 4)))))
 
              data_idx++;
+             gp_cmds(data_idx) = {{}};
              is_image_data(data_idx) = false;
              parametric(data_idx) = false;
              have_cdata(data_idx) = false;
@@ -1069,6 +1098,7 @@
 
              if (length (tmpwith) > 1)
                data_idx++;
+               gp_cmds(data_idx) = {{}};
                is_image_data(data_idx) = is_image_data(data_idx - 1);
                parametric(data_idx) = parametric(data_idx - 1);
                have_cdata(data_idx) = have_cdata(data_idx - 1);
@@ -1080,6 +1110,7 @@
              endif
              if (length (tmpwith) > 2)
                data_idx++;
+               gp_cmds(data_idx) = {{}};
                is_image_data(data_idx) = is_image_data(data_idx - 1);
                parametric(data_idx) = parametric(data_idx - 1);
                have_cdata(data_idx) = have_cdata(data_idx - 1);
@@ -1097,6 +1128,7 @@
           if (! (strncmp (obj.edgecolor, "none", 4)
                  && strncmp (obj.facecolor, "none", 4)))
             data_idx++;
+            gp_cmds(data_idx) = {{}};
             is_image_data(data_idx) = false;
             parametric(data_idx) = false;
             have_cdata(data_idx) = true;
@@ -1243,6 +1275,7 @@
               zz = zz.';
 
               data_idx++;
+              gp_cmds(data_idx) = {{}};
               is_image_data(data_idx) = is_image_data(data_idx - 1);
               parametric(data_idx) = parametric(data_idx - 1);
               have_cdata(data_idx) = false;
@@ -1256,6 +1289,7 @@
             endif
             if (length (style) > 2)
               data_idx++;
+              gp_cmds(data_idx) = {{}};
               is_image_data(data_idx) = is_image_data(data_idx - 1);
               parametric(data_idx) = parametric(data_idx - 1);
               have_cdata(data_idx) = false;
@@ -1280,6 +1314,7 @@
                 zz = zz.';
               endif
               data_idx++;
+              gp_cmds(data_idx) = {{}};
               is_image_data(data_idx) = is_image_data(data_idx - 1);
               parametric(data_idx) = parametric(data_idx - 1);
               have_cdata(data_idx) = false;
@@ -1620,31 +1655,78 @@
           fprintf (plot_stream, "set view %.15g, %.15g;\n", rot_x, rot_z);
         endif
       endif
-      if (have_3d_patch (1))
-        fputs (plot_stream, "set pm3d depthorder\n");
-        fprintf (plot_stream, "%s \"-\" %s %s %s \\\n", plot_cmd,
-                 usingclause{1}, titlespec{1}, withclause{1});
-      elseif (is_image_data (1))
-        if (numel (is_image_data) > 1 && is_image_data(2))
-          ## Remove terminating semicolon
-          n = max (strfind (withclause{1}, ";"));
-          if (! isempty(n))
-            withclause{1} = withclause{1}(1:n-1);
+
+      for i = 1:data_idx
+
+        if (! isempty (gp_cmds{i}) || i == 1)
+          continue_plot_cmd = false;
+        endif
+
+        if (have_3d_patch (i))
+          fputs (plot_stream, "set pm3d depthorder\n");
+          if (continue_plot_cmd)
+            fprintf (plot_stream, ", \"-\" %s %s %s \\\n",
+                     usingclause{i}, titlespec{i}, withclause{i});
+          else
+            if (i > 1)
+              ## Terminate prior plot command
+              fputs (plot_stream, ";\n");
+            endif
+            if (! isempty (gp_cmds{i}))
+              fprintf (plot_stream, "%s\n", gp_cmds{i}{:})
+            endif
+            fprintf (plot_stream, "%s \"-\" %s %s %s \\\n", plot_cmd,
+                     usingclause{i}, titlespec{i}, withclause{i});
+            continue_plot_cmd = true;
           endif
-        endif
-        fprintf (plot_stream, "%s \"-\" %s %s %s \\\n", plot_cmd,
-                 usingclause{1}, titlespec{1}, withclause{1});
-      else
-        fprintf (plot_stream, "%s \"-\" binary format='%%float64' %s %s %s \\\n", plot_cmd,
-                 usingclause{1}, titlespec{1}, withclause{1});
-      endif
-      for i = 2:data_idx
-        if (have_3d_patch (i))
-          fprintf (plot_stream, ", \"-\" %s %s %s \\\n",
-                   usingclause{i}, titlespec{i}, withclause{i});
-        elseif (is_image_data (i))
-          if (! is_image_data (i-1))
-            fputs (plot_stream, "; ");
+        elseif (is_image_data(i))
+          ## Verify changes to this block with "demo image"
+          if (i > 1 && ! is_image_data(i-1))
+            if (bg_is_set || fg_is_set)
+              ## Terminate prior plot command, no "\n" here.
+              fputs (plot_stream, "; ");
+              if (bg_is_set)
+                fputs (plot_stream, "unset obj 1; \\\n");
+                bg_is_set = false;
+                continue_plot_cmd = false;
+              endif
+              if (fg_is_set)
+                fputs (plot_stream, "unset obj 2; \\\n");
+                fg_is_set = false;
+              endif
+              continue_plot_cmd = false;
+            elseif (! continue_plot_cmd)
+              ## Terminate prior plot command
+              fputs (plot_stream, ";\n");
+            endif
+          elseif (i > 1 && ! continue_plot_cmd)
+            ## Terminate prior plot command
+            fputs (plot_stream, ";\n");
+          endif
+          if (i < data_idx && is_image_data(i+1))
+            ## TODO - should terminating semicolon for images be removed ?
+            n = max (strfind (withclause{i}, ";"));
+            if (! isempty(n))
+              withclause{i} = withclause{i}(1:n-1);
+            endif
+          endif
+          if (continue_plot_cmd)
+            fprintf (plot_stream, ", \"-\" %s %s %s \\\n",
+                     usingclause{i}, titlespec{i}, withclause{i});
+          else
+            if (! isempty (gp_cmds{i}))
+              fprintf (plot_stream, "%s\n", gp_cmds{i}{:})
+            endif
+            fprintf (plot_stream, "%s \"-\" %s %s %s \\\n", plot_cmd,
+                     usingclause{i}, titlespec{i}, withclause{i});
+          endif
+          if (i < data_idx && is_image_data(i+1))
+            continue_plot_cmd = true;
+          else
+            continue_plot_cmd = false;
+          endif
+        else
+          if (i > 1 && is_image_data(i-1))
             if (bg_is_set)
               fputs (plot_stream, "unset obj 1; \\\n");
               bg_is_set = false;
@@ -1653,36 +1735,27 @@
               fputs (plot_stream, "unset obj 2; \\\n");
               fg_is_set = false;
             endif
-            if (numel (is_image_data) > i && is_image_data(i+1))
-              ## Remove terminating semicolon
-              n = max (strfind (withclause{i}, ";"));
-              if (! isempty(n))
-                withclause{i} = withclause{i}(1:n-1);
-              endif
-            endif
-            fprintf (plot_stream, "%s \"-\" %s %s %s \\\n", plot_cmd,
+            continue_plot_cmd = false;
+          endif
+          if (continue_plot_cmd)
+            fprintf (plot_stream, ", \"-\" binary format='%%float64' %s %s %s \\\n",
                      usingclause{i}, titlespec{i}, withclause{i});
           else
-            ## For consecutive images continue with the same plot command
-            fprintf (plot_stream, "%s \"-\" %s %s %s \\\n", ",",
-                     usingclause{i}, titlespec{i}, withclause{i});
+            if (i > 1 && ! is_image_data(i-1))
+              ## Terminate prior plot command
+              fputs (plot_stream, ";\n");
+            endif
+            if (! isempty (gp_cmds{i}))
+              fprintf (plot_stream, "%s\n", gp_cmds{i}{:})
+            endif
+            fprintf (plot_stream, "%s \"-\" binary format='%%float64' %s %s %s \\\n",
+                     plot_cmd, usingclause{i}, titlespec{i}, withclause{i});
+            continue_plot_cmd = true;
           endif
-        elseif (is_image_data (i-1))
-          if (bg_is_set)
-            fputs (plot_stream, "unset obj 1; \\\n");
-            bg_is_set = false;
-          endif
-          if (fg_is_set)
-            fputs (plot_stream, "unset obj 2; \\\n");
-            fg_is_set = false;
-          endif
-          fprintf (plot_stream, "%s \"-\" binary format='%%float64' %s %s %s \\\n", plot_cmd,
-                   usingclause{i}, titlespec{i}, withclause{i});
-        else
-          fprintf (plot_stream, ", \"-\" binary format='%%float64' %s %s %s \\\n",
-                   usingclause{i}, titlespec{i}, withclause{i});
         endif
+
       endfor
+
       fputs (plot_stream, ";\n");
       for i = 1:data_idx
         if (have_3d_patch (i))
@@ -1852,7 +1925,7 @@
       endif
     endif
     if (isfield (obj, "markeredgecolor")
-        && !strncmp (obj.markeredgecolor, "none", 4))
+        && ! strncmp (obj.markeredgecolor, "none", 4))
       if (facesame && !isempty (pt)
           && (strncmp (obj.markeredgecolor, "auto", 4)
               || ! isnumeric (obj.markeredgecolor)
