exporting patch:
# HG changeset patch
# User Daniel J Sebald <daniel.sebald@ieee.org>
# Date 1513064583 21600
#      Tue Dec 12 01:43:03 2017 -0600
# Node ID ed164f3603495b356faf66a760ef49b8c2377048
# Parent  5cc1e8b16ab1b9526f3dd13b2904a7613813ddb8
Use separate listener methods for updating legend line and name (bug #52641)

* __getlegenddata__.m: Construct hplots regardless of DisplayName emptiness
  but set text_string entry empty if DisplayName is empty.

* legend.m: When adding updateline listener, drop the logical input.  Change
  the last "updateline" listener construction to "updatename".
  (updateline): Remove the logical input variable from the routine.  Move the
  update_name case to a new listener function.
  (updatename): New listener function sans linelength and update_name input
  variables.  Consists of the update_name case code from updateline.

diff --git a/scripts/plot/appearance/__getlegenddata__.m b/scripts/plot/appearance/__getlegenddata__.m
--- a/scripts/plot/appearance/__getlegenddata__.m
+++ b/scripts/plot/appearance/__getlegenddata__.m
@@ -35,10 +35,12 @@ function [hplots, text_strings] = __getl
   for i = numel (kids):-1:1
     typ = get (kids(i), "type");
     if (any (strcmp (typ, {"line", "patch", "surface", "hggroup"})))
+      hplots(end+1) = kids(i);
       dname = get (kids(i), "DisplayName");
       if (! isempty (dname))
-        hplots(end+1) = kids(i);
         text_strings(end+1) = dname;
+      else
+        text_strings(end+1) = {""};
       endif
     endif
   endfor
diff --git a/scripts/plot/appearance/legend.m b/scripts/plot/appearance/legend.m
--- a/scripts/plot/appearance/legend.m
+++ b/scripts/plot/appearance/legend.m
@@ -847,21 +847,21 @@ function [hleg, hleg_obj, hplot, labels]
 
               if (! any (hplt == old_hplots))
                 addlistener (hplt, "color",
-                             {@updateline, hlegend, linelength, false});
+                             {@updateline, hlegend, linelength});
                 addlistener (hplt, "linestyle",
-                             {@updateline, hlegend, linelength, false});
+                             {@updateline, hlegend, linelength});
                 addlistener (hplt, "linewidth",
-                             {@updateline, hlegend, linelength, false});
+                             {@updateline, hlegend, linelength});
                 addlistener (hplt, "marker",
-                             {@updateline, hlegend, linelength, false});
+                             {@updateline, hlegend, linelength});
                 addlistener (hplt, "markeredgecolor",
-                             {@updateline, hlegend, linelength, false});
+                             {@updateline, hlegend, linelength});
                 addlistener (hplt, "markerfacecolor",
-                             {@updateline, hlegend, linelength, false});
+                             {@updateline, hlegend, linelength});
                 addlistener (hplt, "markersize",
-                             {@updateline, hlegend, linelength, false});
+                             {@updateline, hlegend, linelength});
                 addlistener (hplt, "displayname",
-                             {@updateline, hlegend, linelength, true});
+                             {@updatename, hlegend});
               endif
 
             case "patch"
@@ -1181,64 +1181,64 @@ function deletelegend2 (h, ~, ca, pos, o
 
 endfunction
 
-function updateline (h, ~, hlegend, linelength, update_name)
+function updateline (h, ~, hlegend, linelength)
 
-  if (update_name)
-    ## When string changes, have to rebuild legend completely
-    [hplots, text_strings] = __getlegenddata__ (hlegend);
-    ## FIXME: See bug #52641.  Changing an existing legend string to a blank
-    ##        can trigger this.
-    if (! isempty (hplots))
-      legend (get (hplots(1), "parent"), hplots, text_strings);
+  kids = get (hlegend, "children");
+  ll = lm = [];
+  for i = 1 : numel (kids)
+    if (get (kids(i), "userdata") == h
+        && strcmp (get (kids(i), "type"), "line"))
+      if (strcmp (get (kids(i), "marker"), "none"))
+        ll = kids(i);
+      else
+        lm = kids(i);
+      endif
+    endif
+  endfor
+
+  [linestyle, marker, displayname] = ...
+    get (h, {"linestyle", "marker", "displayname"}){:};
+
+  if (! isempty (ll))
+    [xpos1, ypos1] = get (ll, {"xdata", "ydata"}){:};
+    xpos2 = sum (xpos1) / 2;
+    ypos2 = ypos1(1);
+    delete (ll);
+    if (! isempty (lm))
+      delete (lm);
     endif
   else
-    kids = get (hlegend, "children");
-    ll = lm = [];
-    for i = 1 : numel (kids)
-      if (get (kids(i), "userdata") == h
-          && strcmp (get (kids(i), "type"), "line"))
-        if (strcmp (get (kids(i), "marker"), "none"))
-          ll = kids(i);
-        else
-          lm = kids(i);
-        endif
-      endif
-    endfor
+    [xpos2, ypos2] = get (lm, {"xdata", "ydata"}){:};
+    xpos1 = xpos2 + [-0.5, 0.5] * linelength;
+    ypos1 = [ypos2, ypos2];
+    delete (lm);
+  endif
 
-    [linestyle, marker, displayname] = ...
-      get (h, {"linestyle", "marker", "displayname"}){:};
+  if (! strcmp (linestyle, "none"))
+    line ("xdata", xpos1, "ydata", ypos1, "color", get (h, "color"),
+          "linestyle", get (h, "linestyle"),
+          "linewidth", min (get (h, "linewidth"), 5),
+          "marker", "none",
+          "userdata", h, "parent", hlegend);
+  endif
+  if (! strcmp (marker, "none"))
+    line ("xdata", xpos2, "ydata", ypos2, "color", get (h, "color"),
+          "marker", marker, "markeredgecolor", get (h, "markeredgecolor"),
+          "markerfacecolor", get (h, "markerfacecolor"),
+          "markersize", min (get (h, "markersize"), 10),
+          "linestyle", "none",
+          "linewidth", min (get (h, "linewidth"), 5),
+          "userdata", h, "parent", hlegend);
+  endif
 
-    if (! isempty (ll))
-      [xpos1, ypos1] = get (ll, {"xdata", "ydata"}){:};
-      xpos2 = sum (xpos1) / 2;
-      ypos2 = ypos1(1);
-      delete (ll);
-      if (! isempty (lm))
-        delete (lm);
-      endif
-    else
-      [xpos2, ypos2] = get (lm, {"xdata", "ydata"}){:};
-      xpos1 = xpos2 + [-0.5, 0.5] * linelength;
-      ypos1 = [ypos2, ypos2];
-      delete (lm);
-    endif
+endfunction
 
-    if (! strcmp (linestyle, "none"))
-      line ("xdata", xpos1, "ydata", ypos1, "color", get (h, "color"),
-            "linestyle", get (h, "linestyle"),
-            "linewidth", min (get (h, "linewidth"), 5),
-            "marker", "none",
-            "userdata", h, "parent", hlegend);
-    endif
-    if (! strcmp (marker, "none"))
-      line ("xdata", xpos2, "ydata", ypos2, "color", get (h, "color"),
-            "marker", marker, "markeredgecolor", get (h, "markeredgecolor"),
-            "markerfacecolor", get (h, "markerfacecolor"),
-            "markersize", min (get (h, "markersize"), 10),
-            "linestyle", "none",
-            "linewidth", min (get (h, "linewidth"), 5),
-            "userdata", h, "parent", hlegend);
-    endif
+function updatename (h, ~, hlegend)
+
+  ## When string changes, have to rebuild legend completely
+  [hplots, text_strings] = __getlegenddata__ (hlegend);
+  if (! isempty (hplots))
+    legend (get (hplots(1), "parent"), hplots, text_strings);
   endif
 
 endfunction
