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

From: Eli Zaretskii <[email-masked]>
Date: Sat, 08 Aug 2020 15:55:25 +0300

> From: Lars Ingebrigtsen <[email-masked]>
> Cc: [email-masked],  [email-masked],  [email-masked]
> Date: Sat, 08 Aug 2020 13:18:30 +0200
> 
> > Should I perhaps post the post-processing code I had in mind?
> 
> Yes, please.  :-)

Something like the below, tested very lightly (need test cases with
various non-default faces and fonts in the mode line, and also
images).

Note a few subtle issues:

 . I've limited the feature to the mode line; programs that set
   header-line and tab-line either don't want this, or should format
   those lines to not waste screen space to begin with;
 . I've refrained from squeezing spaces if they have non-default
   faces, on the assumption that those spaces are significant -- the
   result is that the trailing spaces of the buffer name are not
   squeezed, but I think we have no choice here, as down that path
   lies madness (think a mode line that plays some fancy games with
   font sizes).

diff --git a/src/xdisp.c b/src/xdisp.c
index 4fe1c42..24a621d 100644
--- a/src/xdisp.c
+++ b/src/xdisp.c
@@ -25228,6 +25228,51 @@ display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
 
   unbind_to (count, Qnil);
 
+  /* Optionally, squeeze embedded whitespace in the mode line to
+     prevent unnecessary truncation of meaningful data shown there.  */
+  if ((face_id == MODE_LINE_FACE_ID
+       || face_id == MODE_LINE_INACTIVE_FACE_ID)
+      && Vmode_line_compact)
+    {
+      /* Find the last non-blank glyph.  */
+      int last_idx = it.glyph_row->used[TEXT_AREA] - 1;
+      struct glyph *glyphs = it.glyph_row->glyphs[TEXT_AREA];
+      for ( ; last_idx > 0; last_idx--)
+	{
+	  if (!(glyphs[last_idx].type == CHAR_GLYPH
+		&& glyphs[last_idx].u.ch == ' '))
+	    break;
+	}
+
+      /* Now squeeeze embedded repeating spaces.  */
+      int from_idx, to_idx;
+      last_idx++;
+      for (from_idx = 0, to_idx = 0; from_idx < last_idx; to_idx++)
+	{
+	  if (to_idx != from_idx)
+	    glyphs[to_idx] = glyphs[from_idx];
+	  if (glyphs[from_idx].type == CHAR_GLYPH
+	      /* Leave alone spaces that have non-default face ID.  */
+	      && glyphs[from_idx].face_id == face_id
+	      && glyphs[from_idx].u.ch == ' ')
+	    {
+	      from_idx++;
+	      while (from_idx < last_idx
+		     && glyphs[from_idx].face_id == face_id
+		     && glyphs[from_idx].type == CHAR_GLYPH
+		     && glyphs[from_idx].u.ch == ' ')
+		{
+		  it.current_x -= glyphs[from_idx].pixel_width;
+		  from_idx++;
+		}
+	    }
+	  else
+	    from_idx++;
+	}
+      /* Update the count of the glyph slots we've used.  */
+      it.glyph_row->used[TEXT_AREA] -= from_idx - to_idx;
+    }
+
   /* Fill up with spaces.  */
   display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);