Subject: Re: bug#63825: 29.0.90; The header line should be hidden when empty

From: Eshel Yaron <[email-masked]>
Date: Fri, 02 Jun 2023 09:19:17 +0300

Spencer Baugh <[email-masked]> writes:

> Eli Zaretskii <[email-masked]> writes:
>> Specifically, if the header-line-format is just a single cons cell,
>> and the car of that cons cell is either :eval or a symbol, and the
>> result of evaluation those yields nil, don't display the header line.
>> (I don't really like the idea of not displaying the mode line under
>> the same conditions.)
>>
>> Patches welcome.
>
> Ok, the easy way to achieve that is to run format-mode-line on the
> header-line-format and if it evaluates to "", don't display the header
> line.  That also ignores the cases where header-line-format is multiple
> cons cells, all of which evaluate to nil, and other such scenarios.  Is
> that an acceptable approach to you?

This might be a bit too coarse IMO, because it would make it a lot
harder to create an empty header line.  Namely, setting
`header-line-format` to an empty string would no longer create an empty
header line.

I'm far from fluent in Emacs's internals, but AFAIU from skimming
src/xdisp.c there's another issue which is that Emacs checks whether a
window should have a header line in many circumstances, as it affects
the window's effective dimensions.  So formatting the entire header line
each time Emacs just wants to know whether there is or isn't a header
line might not be ideal in terms of efficiency.

The way I read Eli's above message, it boils down to extending the C
function `window_wants_mode_line` with a couple of special cases.
Something like the following (mildly tested):

diff --git a/src/window.c b/src/window.c
index f4e09f49eae..10bccc6df65 100644
--- a/src/window.c
+++ b/src/window.c
@@ -5471,6 +5471,36 @@ window_wants_mode_line (struct window *w)
 }

+/**
+ * null_header_line_format:
+ *
+ * Return 1 when header line format F indicates that the header line
+ * should not be displayed at all.
+ *
+ * This is when F is nil, or F is a cons cell and either its car is a
+ * symbol whose value as a variable is nil, or its car is the symbol
+ * ':eval' and its cddr evaluates to nil.
+ */
+static bool
+null_header_line_format (Lisp_Object f)
+{
+  Lisp_Object car;
+
+  if (NILP (f))
+    return 1;
+
+  if (CONSP (f)) {
+    car = XCAR (f);
+    return (SYMBOLP (car)
+	    && ((EQ (car, QCeval)
+		 && NILP (Feval (XCAR (XCDR (f)), Qnil)))
+		|| NILP (find_symbol_value (car))));
+  }
+
+  return 0;
+}
+
+
 /**
  * window_wants_header_line:
  *
@@ -5495,8 +5525,8 @@ window_wants_header_line (struct window *w)
 	  && !MINI_WINDOW_P (w)
 	  && !WINDOW_PSEUDO_P (w)
 	  && !EQ (window_header_line_format, Qnone)
-	  && (!NILP (window_header_line_format)
-	      || !NILP (BVAR (XBUFFER (WINDOW_BUFFER (w)), header_line_format)))
+	  && (!null_header_line_format (window_header_line_format)
+	      || !null_header_line_format (BVAR (XBUFFER (WINDOW_BUFFER (w)), header_line_format)))
 	  && (WINDOW_PIXEL_HEIGHT (w)
 	      > (window_wants_mode_line (w)
 		 ? 2 * WINDOW_FRAME_LINE_HEIGHT (w)