Subject: bug#34476: fluffy whitespace in the mode-line, despite it running off the screen

From: Eli Zaretskii <[email-masked]>
Date: Fri, 07 Aug 2020 14:32:39 +0300

> From: Lars Ingebrigtsen <[email-masked]>
> Date: Fri, 07 Aug 2020 10:31:22 +0200
> Cc: [email-masked],
>  積丹尼 Dan Jacobson <[email-masked]>
> 
> -  display_mode_element (&it, 0, 0, 0, format, Qnil, false);
> +
> +  if (NILP (Vmode_line_compact))
> +    {
> +      mode_line_target = MODE_LINE_DISPLAY;
> +      display_mode_element (&it, 0, 0, 0, format, Qnil, false);
> +    }
> +  else
> +    {
> +      Lisp_Object mode_string = Fformat_mode_line (format, Qnil, Qnil, Qnil);
> +      char *string = xmalloc (SBYTES (mode_string) + 1),
> +	*ostring = SSDATA (mode_string);
> +      char *s = string, prev = 0;
> +
> +      /* Copy over the data from the mode line string, but ignore
> +	 repeating spaces.  This should be safe even for multibyte
> +	 strings, since this is UTF-8. */
> +      for (int i = 0; i < SBYTES (mode_string); i++)
> +	{
> +	  char c = ostring[i];
> +	  if (!(c == ' ' && prev == ' '))
> +	    {
> +	      *s++ = c;
> +	      prev = c;
> +	    }
> +	}
> +      *s = 0;
> +      
> +      display_string (string, Qnil, Qnil, 0, 0, &it, 0, 0, 0,
> +		      STRING_MULTIBYTE (mode_string));
> +      xfree (string);
> +    }

Ouch!  This is Lisp converted into C, yes?  And it formats the
mode-line twice: once in format-mode-line, then again in
display_string, right?

You don't need all this inelegance.  After display_mode_element
returns, you have all the glyphs it produced in it.glyph_row, so you
can simply remove the unneeded space glyphs from the glyph row (and
adjust the metrics accordingly).  Let me know if you need more
detailed help in how to do that.