+static void
+put_strftime (struct string *out, time_t now, const char *format)
+{
+ const struct tm *tm = localtime (&now);
+ char value[128];
+ strftime (value, sizeof value, format, tm);
+ ds_put_cstr (out, value);
+}
+
+static bool
+skip_prefix (struct substring *s, struct substring prefix)
+{
+ if (ss_starts_with (*s, prefix))
+ {
+ ss_advance (s, prefix.length);
+ return true;
+ }
+ else
+ return false;
+}
+
+static void
+put_table_expression (struct string *out, struct lexer *lexer,
+ struct dictionary *dict, int expr_start, int expr_end)
+{
+ size_t nest = 0;
+ for (int ofs = expr_start; ofs < expr_end; ofs++)
+ {
+ const struct token *t = lex_ofs_token (lexer, ofs);
+ if (t->type == T_LBRACK)
+ nest++;
+ else if (t->type == T_RBRACK && nest > 0)
+ nest--;
+ else if (nest > 0)
+ {
+ /* Nothing. */
+ }
+ else if (t->type == T_ID)
+ {
+ const struct variable *var
+ = dict_lookup_var (dict, t->string.string);
+ const char *label = var ? var_get_label (var) : NULL;
+ ds_put_cstr (out, label ? label : t->string.string);
+ }
+ else
+ {
+ if (ofs != expr_start && t->type != T_RPAREN && ds_last (out) != ' ')
+ ds_put_byte (out, ' ');
+
+ char *repr = lex_ofs_representation (lexer, ofs, ofs);
+ ds_put_cstr (out, repr);
+ free (repr);
+
+ if (ofs + 1 != expr_end && t->type != T_LPAREN)
+ ds_put_byte (out, ' ');
+ }
+ }
+}
+
+static void
+put_title_text (struct string *out, struct substring in, time_t now,
+ struct lexer *lexer, struct dictionary *dict,
+ int expr_start, int expr_end)
+{
+ for (;;)
+ {
+ size_t chunk = ss_find_byte (in, ')');
+ ds_put_substring (out, ss_head (in, chunk));
+ ss_advance (&in, chunk);
+ if (ss_is_empty (in))
+ return;
+
+ if (skip_prefix (&in, ss_cstr (")DATE")))
+ put_strftime (out, now, "%x");
+ else if (skip_prefix (&in, ss_cstr (")TIME")))
+ put_strftime (out, now, "%X");
+ else if (skip_prefix (&in, ss_cstr (")TABLE")))
+ put_table_expression (out, lexer, dict, expr_start, expr_end);
+ else
+ {
+ ds_put_byte (out, ')');
+ ss_advance (&in, 1);
+ }
+ }
+}
+