Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Terminal query artifacts and backward compatibility issue in zsh-dev version



Jun T wrote:
> At least on my Macs I can easily observe it. I don't care it because
> I know it's harmless, but some users would be worried by it.
>
> But the latest Terminal.app (macOS Tahoe, TERM_PROGRAM_VERSION=470)
> already supports truecolor and recognizes the TQ_RGB, so +q524742 is
> not printed. There should be no problem when Apple includes the next
> release version of zsh (with termquery) as /bin/zsh in the next
> (or later) version of macOS.

I'm really glad to hear that. I had prepared a patch and test scripts to
make use of the macOS specific TERMINAL_PROGRAM environment variable.
But I've not had an opportunity to borrow access to a Mac this year
so never got a chance to do the testing. I've put the patch below in
case someone else wants to experiment with it. It's not an approach I'm
especially happy with anyway. But the default macOS terminal on systems
where the next release of zsh might be the default shell would have been
worth making exceptions for.

> Users who install dev-versions of zsh by themself are not
> 'ordinary users', I think, and hopefully they will mange to find
> it harmless, or set .term.extensions in a start up file.

Alternatives like iterm2 and ghostty are also likely to be popular
among such non-ordinary users. What we will encounter is users on older
macOS using ssh to newer Linux/etc systems where they get new zsh.
TERMINAL_PROGRAM doesn't get included in default ssh SendEnv/AcceptEnv
configurations so that approach is of no use there. But the problem is
cosmetic only and will go away in time.

Truecolor can also be detected by setting a colour, retrieving the
colour to compare and then resetting it. I'm reluctant to do that
because there's no safe choice of test colour and leaving terminals
stuck with unreadable text is far worse than a few printed and swiftly
wiped garbage characters.

> > I also wouldn't put this in .zshenv. zle is normally only loaded for
> > interactive shells so .zshrc is more applicable.
>
> On macOS, Apple's /etc/zshrc uses 'bindky' that requires Zle. So we
> need to use ~/.zshenv, maybe in a block 'if [[ -o i ]] ...'.

I've always gone out of my way to disable any system zshrc so this
hadn't occurred to me. But yes, .zshenv and an if block will be needed.

So what keys are they binding? Is that just things like Home/Insert/End?
If we can arrange for those to just work, maybe they'll remove the
bindkeys from /etc/zshrc. I don't suppose the updated Apple terminal
added support for either the kitty or xterm extended key protocols?
It's quite common to use $terminfo to retrieve some common key
sequences. My understanding is that these are only valid if you also
generate sequences for entering and leaving keyboard transmit mode -
smkx / rmkx in terminfo. We could generate these via another modkeys-
extension and then map such common keys by default. Are there any
disavantages to using that keyboard transmit mode? Never tried it
myself.

> Another problem is 'is-at-least 5.9.0.3  5.9' returns true
> (workers/54336, or workers/47314 by Daniel).

My own approach is to take advantage of namerefs being added in the same
release so it is wrapped with:
  if typeset -n .zle.hlgroups=paint 2>/dev/null &&
      zmodload -i zsh/hlgroup 2>/dev/null

Or, once your is-at-least patch goes in, a fresh build will solve that
too.

Oliver

untested patch:
diff --git a/Src/Zle/termquery.c b/Src/Zle/termquery.c
index cfccce42d..b76493c04 100644
--- a/Src/Zle/termquery.c
+++ b/Src/Zle/termquery.c
@@ -507,8 +507,23 @@ query_terminal(void) {
     char *tqend = tquery;
     static seqstate_t states[] = QUERY_STATES;
     char **f, **flist = getaparam(EXTVAR);
+    char *envid = getsparam("TERMINAL_PROGRAM");
+    int appleterm = 0;
     size_t i;
 
+    /* If TERMINAL_PROGRAM is set in the environment, use that and
+     * skip the XTVERSION query */
+    if (envid) {
+	char *envver;
+	handle_query(4, NULL, 0, envid, strlen(envid), NULL);
+	/* Default macOS terminal doesn't consume RGB queries,
+	 * nor does it support truecolor. Given that it's widely
+	 * used, we handle it explicitly. */
+	appleterm = !strcmp(envid, "Apple_Terminal");
+	if ((envver = getsparam("TERMINAL_PROGRAM_VERSION")))
+	    handle_query(5, NULL, 0, envver, strlen(envver), NULL);
+    }
+
     for (f = flist; f && *f; f++)
 	if (!strcmp(*f, "-query"))
 	    return; /* disable all queries */
@@ -536,7 +551,7 @@ query_terminal(void) {
 		    (!strcmp(cterm, "truecolor") ||
 			!strcmp(cterm, "24bit")))))
 	    handle_query(3, NULL, 0, NULL, 0, NULL);
-	else
+	else if ((i != 4 || !appleterm) && (i != 5 || !envid))
 	    struncpy(&tqend, (char *) queries[i], /* collate escape sequences */
 		sizeof(tquery) - (tqend - tquery));
     }




Messages sorted by: Reverse Date, Date, Thread, Author