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

Oliver Kiddle <[email protected]>
Newsgroups gmane.comp.shells.zsh.devel
Message-ID <[email protected]>
On 13 Apr, dana wrote:
> atm i think there is still an argument for committing it despite tahoe
> apparently not needing it any more: pre-tahoe releases make up about 47%
> of macos events in homebrew's analytics from the last 30 days. what %
> it'll be when 5.10 actually releases, idk

I've attached an updated patch which corrects the environment variable
naming, compares the version to 470 and avoids breaking the test case.
As before I'm not doing my testing on macOS. I did manage to briefly
check a Mac which appeared to have Tahoe with version 466 of the
terminal (and lacking truecolor support).

While I don't entirely like doing this, I'd prefer to minimise user
complaints about the terminal queries so I'm inclined to include it. A
later future release might remove it again.

Oliver

diff --git a/Src/Zle/termquery.c b/Src/Zle/termquery.c
index cfccce42d..ef6f013b2 100644
--- a/Src/Zle/termquery.c
+++ b/Src/Zle/termquery.c
@@ -507,8 +507,25 @@ query_terminal(void) {
     char *tqend = tquery;
     static seqstate_t states[] = QUERY_STATES;
     char **f, **flist = getaparam(EXTVAR);
+    char *envid = getsparam("TERM_PROGRAM");
+    int badapple = 0;
     size_t i;
 
+    /* If TERM_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);
+	if ((envver = getsparam("TERM_PROGRAM_VERSION"))) {
+	    handle_query(5, NULL, 0, envver, strlen(envver), NULL);
+	    /* Older macOS terminal doesn't consume RGB queries,
+	     * nor does it support truecolor. Given that it's widely
+	     * used, we handle it explicitly. */
+	    badapple = !strcmp(envid, "Apple_Terminal") &&
+		zstrtol(envver, NULL, 10) < 470;
+	}
+    }
+
     for (f = flist; f && *f; f++)
 	if (!strcmp(*f, "-query"))
 	    return; /* disable all queries */
@@ -536,7 +553,7 @@ query_terminal(void) {
 		    (!strcmp(cterm, "truecolor") ||
 			!strcmp(cterm, "24bit")))))
 	    handle_query(3, NULL, 0, NULL, 0, NULL);
-	else
+	else if ((i != 4 || !badapple) && (i != 5 || !envid))
 	    struncpy(&tqend, (char *) queries[i], /* collate escape sequences */
 		sizeof(tquery) - (tqend - tquery));
     }
diff --git a/Test/X06termquery.ztst b/Test/X06termquery.ztst
index 5f3a81aae..701cd625b 100644
--- a/Test/X06termquery.ztst
+++ b/Test/X06termquery.ztst
@@ -5,6 +5,7 @@
     termresp() {
       setopt localoptions extendedglob
       export PS1= PS2= COLORTERM= TERM=
+      typeset +x TERM_PROGRAM
       typeset +x -m .term.\*
       zpty -d
       zpty zsh "${(q)ZTST_testdir}/../Src/zsh -fiV +Z"
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.