This fixes the keyprotocol option when using terminals supporting the
kitty keyboard protocol and modifyOtherKeys2 like foot and wezterm.

Closes: #1029049
---
Hi, I can confirm that patch 9.0.1080 fixes the issue described at
<https://github.com/vim/vim/issues/11781>.

I've submitted a patch on Salsa at
<https://salsa.debian.org/vim-team/vim/-/merge_requests/7>, if you could
merge it it'd be great!

I'm also emailing the patch with git send-email for your convenience.

Bye :)

 ...sing-xterm-kitty-for-term-causes-pro.patch | 145 +++++++++++++++
 ...he-kitty-terminfo-entry-is-not-wides.patch | 169 ++++++++++++++++++
 ...087-autocommand-test-sometimes-fails.patch |   2 +-
 debian/patches/series                         |   2 +
 4 files changed, 317 insertions(+), 1 deletion(-)
 create mode 100644 
debian/patches/patch-9.0.1073-using-xterm-kitty-for-term-causes-pro.patch
 create mode 100644 
debian/patches/patch-9.0.1080-the-kitty-terminfo-entry-is-not-wides.patch

diff --git 
a/debian/patches/patch-9.0.1073-using-xterm-kitty-for-term-causes-pro.patch 
b/debian/patches/patch-9.0.1073-using-xterm-kitty-for-term-causes-pro.patch
new file mode 100644
index 000000000..899a90815
--- /dev/null
+++ b/debian/patches/patch-9.0.1073-using-xterm-kitty-for-term-causes-pro.patch
@@ -0,0 +1,145 @@
+From 731d00770d9006e7dab6a66e2ea86603ed5ef212 Mon Sep 17 00:00:00 2001
+From: Bram Moolenaar <b...@vim.org>
+Date: Sun, 18 Dec 2022 17:47:18 +0000
+Subject: [PATCH] patch 9.0.1073: using "xterm-kitty" for 'term' causes
+ problems
+
+Problem:    Using "xterm-kitty" for 'term' causes problems.
+Solution:   Remove the "xterm-" part when 'term' is set from $TERM.  Detect a
+            few kitty-specific properties based on the version response
+            instead of the terminal name.
+---
+ runtime/doc/term.txt | 20 ++++++++++++++++++++
+ src/term.c           | 44 +++++++++++++++++++++++++++++++++++++-------
+ src/version.c        |  2 ++
+ 3 files changed, 59 insertions(+), 7 deletions(-)
+
+diff --git a/runtime/doc/term.txt b/runtime/doc/term.txt
+index f7b65ea74..50ba187ab 100644
+--- a/runtime/doc/term.txt
++++ b/runtime/doc/term.txt
+@@ -294,6 +294,26 @@ When Vim receives a response to the |t_RV| (request 
version) sequence and it
+ starts with CSI, it assumes that the terminal is in 8-bit mode and will
+ convert all key sequences to their 8-bit variants.
+ 
++                                              *xterm-kitty* *kitty-terminal*
++The Kitty terminal is a special case.  Mainly because it works different from
++most other terminals, but also because, instead of trying the fit in and make
++it behave like other terminals by default, it dictates how applications need
++to work when using Kitty.  This makes it very difficult for Vim to work in a
++Kitty terminal.  Some exceptions have been hard coded, but it is not at all
++nice to have to make exceptions for one specific terminal.
++
++One of the problems is that the value for $TERM is set to "xterm-kitty".  For
++Vim this is an indication that the terminal is xterm-compatible and the
++builtin xterm termcap entries should be used.  Many other terminals depend on
++this.  However, Kitty is not fully xterm compatible.  The author suggested to
++ignore the "xterm-" prefix and use the terminfo entry anyway, but that
++conflicts with what is needed for other terminals.  Therefore Vim removes the
++"xterm-" prefix from "xterm-kitty" when it comes from $TERM.
++
++Note that using the kitty keyboard protocol is a separate feature, see
++|kitty-keyboard-protocol|.
++
++
+ ==============================================================================
+ 2. Terminal options           *terminal-options* *termcap-options* *E436*
+ 
+diff --git a/src/term.c b/src/term.c
+index 7483974ac..7a70af450 100644
+--- a/src/term.c
++++ b/src/term.c
+@@ -2071,6 +2071,12 @@ set_termname(char_u *term)
+     else
+       T_CCS = empty_option;
+ 
++    // Special case: "kitty" does not normally have a "RV" entry in terminfo,
++    // but we need to request the version for several other things to work.
++    if (strstr((char *)term, "kitty") != NULL
++                                         && (T_CRV == NULL || *T_CRV == NUL))
++      T_CRV = (char_u *)"\033[>c";
++
+ #ifdef UNIX
+ /*
+  * Any "stty" settings override the default for t_kb from the termcap.
+@@ -2599,15 +2605,34 @@ tgoto(char *cm, int x, int y)
+     void
+ termcapinit(char_u *name)
+ {
+-    char_u    *term;
++    char_u    *term = name;
+ 
+-    if (name != NULL && *name == NUL)
+-      name = NULL;        // empty name is equal to no name
+-    term = name;
++    if (term != NULL && *term == NUL)
++      term = NULL;        // empty name is equal to no name
+ 
+ #ifndef MSWIN
++    char_u    *tofree = NULL;
+     if (term == NULL)
++    {
+       term = mch_getenv((char_u *)"TERM");
++
++      // "xterm-kitty" is used for Kitty, but it is not actually compatible
++      // with xterm.  Remove the "xterm-" part to avoid trouble.
++      if (term != NULL && STRNCMP(term, "xterm-kitty", 11) == 0)
++      {
++#ifdef FEAT_EVAL
++          ch_log(NULL, "Removing xterm- prefix from terminal name %s", term);
++#endif
++          if (p_verbose > 0)
++          {
++              verbose_enter();
++              smsg(_("Removing xterm- prefix from terminal name %s"), term);
++              verbose_leave();
++          }
++          term = vim_strsave(term + 6);
++          tofree = term;
++      }
++    }
+ #endif
+     if (term == NULL || *term == NUL)
+       term = DEFAULT_TERM;
+@@ -2617,10 +2642,12 @@ termcapinit(char_u *name)
+     set_string_default("term", term);
+     set_string_default("ttytype", term);
+ 
+-    /*
+-     * Avoid using "term" here, because the next mch_getenv() may overwrite 
it.
+-     */
++    // Avoid using "term" here, because the next mch_getenv() may overwrite 
it.
+     set_termname(T_NAME != NULL ? T_NAME : term);
++
++#ifndef MSWIN
++    vim_free(tofree);
++#endif
+ }
+ 
+ /*
+@@ -4999,6 +5026,9 @@ handle_version_response(int first, int *arg, int argc, 
char_u *tp)
+       {
+           term_props[TPR_KITTY].tpr_status = TPR_YES;
+           term_props[TPR_KITTY].tpr_set_by_termresponse = TRUE;
++
++          // Kitty can handle SGR mouse reporting.
++          term_props[TPR_MOUSE].tpr_status = TPR_MOUSE_SGR;
+       }
+ 
+       // GNU screen sends 83;30600;0, 83;40500;0, etc.
+diff --git a/src/version.c b/src/version.c
+index 91313cb3f..0fbc65789 100644
+--- a/src/version.c
++++ b/src/version.c
+@@ -695,6 +695,8 @@ static char *(features[]) =
+ 
+ static int included_patches[] =
+ {   /* Add new patch number below this line */
++/**/
++    1073,
+ /**/
+     1000,
+ /**/
+-- 
+2.39.0
+
diff --git 
a/debian/patches/patch-9.0.1080-the-kitty-terminfo-entry-is-not-wides.patch 
b/debian/patches/patch-9.0.1080-the-kitty-terminfo-entry-is-not-wides.patch
new file mode 100644
index 000000000..16ecac307
--- /dev/null
+++ b/debian/patches/patch-9.0.1080-the-kitty-terminfo-entry-is-not-wides.patch
@@ -0,0 +1,169 @@
+From afa3f1cc7258d34c32a299a3cc6aece89f67fb47 Mon Sep 17 00:00:00 2001
+From: Bram Moolenaar <b...@vim.org>
+Date: Mon, 19 Dec 2022 18:56:48 +0000
+Subject: [PATCH] patch 9.0.1080: the "kitty" terminfo entry is not widespread
+
+Problem:    The "kitty" terminfo entry is not widespread, resulting in the
+            kitty terminal not working properly.
+Solution:   Go back to using "xterm-kitty" and avoid the problems it causes in
+            another way.
+---
+ runtime/doc/term.txt | 11 +++++++---
+ src/os_unix.c        |  5 ++++-
+ src/term.c           | 51 +++++++++++++++++---------------------------
+ src/version.c        |  2 ++
+ 4 files changed, 33 insertions(+), 36 deletions(-)
+
+diff --git a/runtime/doc/term.txt b/runtime/doc/term.txt
+index 50ba187ab..146ef478f 100644
+--- a/runtime/doc/term.txt
++++ b/runtime/doc/term.txt
+@@ -306,9 +306,14 @@ One of the problems is that the value for $TERM is set to 
"xterm-kitty".  For
+ Vim this is an indication that the terminal is xterm-compatible and the
+ builtin xterm termcap entries should be used.  Many other terminals depend on
+ this.  However, Kitty is not fully xterm compatible.  The author suggested to
+-ignore the "xterm-" prefix and use the terminfo entry anyway, but that
+-conflicts with what is needed for other terminals.  Therefore Vim removes the
+-"xterm-" prefix from "xterm-kitty" when it comes from $TERM.
++ignore the "xterm-" prefix and use the terminfo entry anyway, so that is what
++happens now, the builtin xterm termcap entries are not used.  However, the
++t_RV is set, otherwise other things would not work, such as automatically
++setting 'ttymouse' to "sgr".
++
++It is not clear why kitty sets $TERM to "xterm-kitty", the terminal isn't
++really xterm compatible.  "kitty" would be more appropriate, but a terminfo
++entry with that name is not widespread.
+ 
+ Note that using the kitty keyboard protocol is a separate feature, see
+ |kitty-keyboard-protocol|.
+diff --git a/src/os_unix.c b/src/os_unix.c
+index bd75ccae1..9d8d46650 100644
+--- a/src/os_unix.c
++++ b/src/os_unix.c
+@@ -2353,6 +2353,8 @@ mch_restore_title(int which)
+ /*
+  * Return TRUE if "name" looks like some xterm name.
+  * This matches "xterm.*", thus "xterm-256color", "xterm-kitty", etc.
++ * Do not consider "xterm-kitty" an xterm, it is not fully xterm compatible,
++ * using the "xterm-kitty" terminfo entry should work better.
+  * Seiichi Sato mentioned that "mlterm" works like xterm.
+  */
+     int
+@@ -2360,7 +2362,8 @@ vim_is_xterm(char_u *name)
+ {
+     if (name == NULL)
+       return FALSE;
+-    return (STRNICMP(name, "xterm", 5) == 0
++    return ((STRNICMP(name, "xterm", 5) == 0
++                                   && STRNICMP(name, "xterm-kitty", 11) != 0)
+               || STRNICMP(name, "nxterm", 6) == 0
+               || STRNICMP(name, "kterm", 5) == 0
+               || STRNICMP(name, "mlterm", 6) == 0
+diff --git a/src/term.c b/src/term.c
+index 7a70af450..cd6b3b9cf 100644
+--- a/src/term.c
++++ b/src/term.c
+@@ -1675,6 +1675,12 @@ static char *(key_names[]) =
+ #endif
+ 
+ #ifdef HAVE_TGETENT
++/*
++ * Get the termcap entries we need with tgetstr(), tgetflag() and tgetnum().
++ * "invoke_tgetent()" must have been called before.
++ * If "*height" or "*width" are not zero then use the "li" and "col" entries 
to
++ * get their value.
++ */
+     static void
+ get_term_entries(int *height, int *width)
+ {
+@@ -2033,14 +2039,6 @@ set_termname(char_u *term)
+ #endif
+           parse_builtin_tcap(term);
+ 
+-          // Use the 'keyprotocol' option to adjust the t_TE and t_TI
+-          // termcap entries if there is an entry maching "term".
+-          keyprot_T kpc = match_keyprotocol(term);
+-          if (kpc == KEYPROTOCOL_KITTY)
+-              apply_builtin_tcap(term, builtin_kitty, TRUE);
+-          else if (kpc == KEYPROTOCOL_MOK2)
+-              apply_builtin_tcap(term, builtin_mok2, TRUE);
+-
+ #ifdef FEAT_GUI
+           if (term_is_gui(term))
+           {
+@@ -2060,6 +2058,19 @@ set_termname(char_u *term)
+     }
+ #endif
+ 
++#ifdef FEAT_GUI
++    if (!gui.in_use)
++#endif
++    {
++      // Use the 'keyprotocol' option to adjust the t_TE and t_TI
++      // termcap entries if there is an entry maching "term".
++      keyprot_T kpc = match_keyprotocol(term);
++      if (kpc == KEYPROTOCOL_KITTY)
++          apply_builtin_tcap(term, builtin_kitty, TRUE);
++      else if (kpc == KEYPROTOCOL_MOK2)
++          apply_builtin_tcap(term, builtin_mok2, TRUE);
++    }
++
+ /*
+  * special: There is no info in the termcap about whether the cursor
+  * positioning is relative to the start of the screen or to the start of the
+@@ -2611,28 +2622,8 @@ termcapinit(char_u *name)
+       term = NULL;        // empty name is equal to no name
+ 
+ #ifndef MSWIN
+-    char_u    *tofree = NULL;
+     if (term == NULL)
+-    {
+       term = mch_getenv((char_u *)"TERM");
+-
+-      // "xterm-kitty" is used for Kitty, but it is not actually compatible
+-      // with xterm.  Remove the "xterm-" part to avoid trouble.
+-      if (term != NULL && STRNCMP(term, "xterm-kitty", 11) == 0)
+-      {
+-#ifdef FEAT_EVAL
+-          ch_log(NULL, "Removing xterm- prefix from terminal name %s", term);
+-#endif
+-          if (p_verbose > 0)
+-          {
+-              verbose_enter();
+-              smsg(_("Removing xterm- prefix from terminal name %s"), term);
+-              verbose_leave();
+-          }
+-          term = vim_strsave(term + 6);
+-          tofree = term;
+-      }
+-    }
+ #endif
+     if (term == NULL || *term == NUL)
+       term = DEFAULT_TERM;
+@@ -2644,10 +2635,6 @@ termcapinit(char_u *name)
+ 
+     // Avoid using "term" here, because the next mch_getenv() may overwrite 
it.
+     set_termname(T_NAME != NULL ? T_NAME : term);
+-
+-#ifndef MSWIN
+-    vim_free(tofree);
+-#endif
+ }
+ 
+ /*
+diff --git a/src/version.c b/src/version.c
+index 401d78bd3..a2371286c 100644
+--- a/src/version.c
++++ b/src/version.c
+@@ -695,6 +695,8 @@ static char *(features[]) =
+ 
+ static int included_patches[] =
+ {   /* Add new patch number below this line */
++/**/
++    1080,
+ /**/
+     1073,
+ /**/
+-- 
+2.39.0
+
diff --git 
a/debian/patches/patch-9.0.1087-autocommand-test-sometimes-fails.patch 
b/debian/patches/patch-9.0.1087-autocommand-test-sometimes-fails.patch
index 201c60072..2fa5e9f5a 100644
--- a/debian/patches/patch-9.0.1087-autocommand-test-sometimes-fails.patch
+++ b/debian/patches/patch-9.0.1087-autocommand-test-sometimes-fails.patch
@@ -32,5 +32,5 @@ index 10db247..20b1967 100644
 +/**/
 +    1087,
  /**/
-     1000,
+     1080,
  /**/
diff --git a/debian/patches/series b/debian/patches/series
index 9978fbffe..99cc278b1 100644
--- a/debian/patches/series
+++ b/debian/patches/series
@@ -2,6 +2,8 @@ 
debian/Support-sourcing-a-vimrc.tiny-when-Vim-is-invoked-as-vi.patch
 debian/Detect-the-rst-filetype-using-the-contents-of-the-file.patch
 debian/Add-recognition-of-more-LaTeX-commands-for-tex-filetype-d.patch
 debian/Document-Debian-s-decision-to-disable-modelines-by-defaul.patch
+patch-9.0.1073-using-xterm-kitty-for-term-causes-pro.patch
+patch-9.0.1080-the-kitty-terminfo-entry-is-not-wides.patch
 patch-9.0.1087-autocommand-test-sometimes-fails.patch
 patch-9.0.1117-terminfo-entries-for-bracketed-paste-are-n.patch
 patch-9.0.1118-sporadic-test-failures-when-using-a-t.patch
-- 
2.39.0

Reply via email to