Patch 9.0.0930
Problem:    Cannot debug the Kitty keyboard protocol with TermDebug.
Solution:   Add Kitty keyboard protocol support to the libvterm fork.
            Recognize the escape sequences that the protocol generates.  Add
            the 'keyprotocol' option to allow the user to specify for which
            terminal what protocol is to be used, instead of hard-coding this.
            Add recognizing the kitty keyboard protocol status.
Files:      runtime/doc/map.txt, runtime/doc/options.txt,
            runtime/doc/quickref.txt, src/libvterm/src/state.c,
            src/libvterm/src/vterm_internal.h, src/libvterm/src/keyboard.c,
            src/libvterm/include/vterm.h, src/term.c, src/proto/term.pro,
            src/terminal.c, src/globals.h, src/vim.h, src/edit.c,
            src/normal.c, src/os_unix.c, src/map.c, src/optiondefs.h,
            src/optionstr.c, src/option.h, src/testdir/gen_opt_test.vim,
            src/testdir/keycode_check.json


*** ../vim-9.0.0929/runtime/doc/map.txt 2022-11-14 22:07:41.340221360 +0000
--- runtime/doc/map.txt 2022-11-23 13:45:20.996243300 +0000
***************
*** 10,27 ****
  manual.
  
  1. Key mapping                        |key-mapping|
!    1.1 MAP COMMANDS                   |:map-commands|
!    1.2 Special arguments              |:map-arguments|
!    1.3 Mapping and modes              |:map-modes|
!    1.4 Listing mappings                       |map-listing|
!    1.5 Mapping special keys           |:map-special-keys|
!    1.6 Special characters             |:map-special-chars|
!    1.7 What keys to map                       |map-which-keys|
!    1.8 Examples                               |map-examples|
!    1.9 Using mappings                 |map-typing|
!    1.10 Mapping alt-keys              |:map-alt-keys|
!    1.11 Mapping in modifyOtherKeys mode       |modifyOtherKeys|
!    1.12 Mapping an operator           |:map-operator|
  2. Abbreviations              |abbreviations|
  3. Local mappings and functions       |script-local|
  4. User-defined commands      |user-commands|
--- 10,28 ----
  manual.
  
  1. Key mapping                        |key-mapping|
!    1.1 MAP COMMANDS                           |:map-commands|
!    1.2 Special arguments                      |:map-arguments|
!    1.3 Mapping and modes                      |:map-modes|
!    1.4 Listing mappings                               |map-listing|
!    1.5 Mapping special keys                   |:map-special-keys|
!    1.6 Special characters                     |:map-special-chars|
!    1.7 What keys to map                               |map-which-keys|
!    1.8 Examples                                       |map-examples|
!    1.9 Using mappings                         |map-typing|
!    1.10 Mapping alt-keys                      |:map-alt-keys|
!    1.11 Mapping in modifyOtherKeys mode               |modifyOtherKeys|
!    1.12 Mapping with Kitty keyboard protocol  |kitty-keyboard-protocol|
!    1.13 Mapping an operator                   |:map-operator|
  2. Abbreviations              |abbreviations|
  3. Local mappings and functions       |script-local|
  4. User-defined commands      |user-commands|
***************
*** 1001,1007 ****
  Insert mode to avoid every key with a modifier causing Insert mode to end.
  
  
! 1.12 MAPPING AN OPERATOR                              *:map-operator*
  
  An operator is used before a {motion} command.  To define your own operator
  you must create a mapping that first sets the 'operatorfunc' option and then
--- 1010,1043 ----
  Insert mode to avoid every key with a modifier causing Insert mode to end.
  
  
! 1.12 MAPPING WITH KITTY KEYBOARD PROTOCOL      *kitty-keyboard-protocol*
! 
! If the value of 'term' contains "kitty" then Vim will send out an escape
! sequence to enable the Kitty keyboard protocol.  This can be changed with the
! 'keyprotocol' option.
! 
! Like modifyOtherKeys, this will make it possible to distinguish between more
! keys with modifiers.  Also, this protocol sends an escape sequence for the Esc
! key, so that Vim does not need to use a timeout to know whether receiving an
! Esc character means the Esc key was pressed or it's the start of an escape
! sequence.
! 
! Vim automatically detects if the Kitty keyboard protocol was enabled when it
! spots the response to the status request (this should be part of the |t_TI|
! termcap entry).  To see if Vim detected such an escape sequence use: >
!       :verbose map
! The first line will then show "Kitty keyboard protocol: {value}" (possibly
! translated).  The meaning of {value}:
!       Unknown         no status received yet
!       Off             protocol is not used
!       On              protocol is used
!       Disabled        protocol was used but expected to have been disabled
!                       by 't_TE'
!       Cleared         protocol expected to have beeen disabled by 't_TE',
!                       previous state is unknown 
! 
! 
! 1.13 MAPPING AN OPERATOR                              *:map-operator*
  
  An operator is used before a {motion} command.  To define your own operator
  you must create a mapping that first sets the 'operatorfunc' option and then
*** ../vim-9.0.0929/runtime/doc/options.txt     2022-11-12 11:54:19.205571527 
+0000
--- runtime/doc/options.txt     2022-11-23 15:06:12.151811124 +0000
***************
*** 4859,4864 ****
--- 4892,4941 ----
        <PageUp> and <PageDown>.
        The 'keymodel' option is set by the |:behave| command.
  
+                                               *'keyprotocol'* *'kpc'*
+ 'keyprotocol' 'kpc'   string  (default: see below)
+                       global
+       Specifies what keyboard protocol to use depending on the value of
+       'term'.  The supported keyboard protocols names are:
+               none    whatever the terminal uses
+               mok2    modifyOtherKeys level 2, as supported by xterm
+               kitty   Kitty keyboard protocol, as supported by Kitty
+ 
+       The option value is a list of command separated items.  Each item has
+       a pattern that is matched against the 'term' option, a colon and the
+       protocol name to be used.  To illustrate this, the default value would
+       be set with: >
+          set keyprotocol=kitty:kitty,foot:kitty,wezterm:kitty,xterm:mok2
+ 
+ <     This means that when 'term' contains "kitty, "foot" or "wezterm"
+       somewhere then the "kitty" protocol is used.  When 'term' contains
+       "xterm" somewhere, then the "mok2" protocol is used. 
+ 
+       The first match is used, thus if you want to have "kitty" use the
+       kitty protocol, but "badkitty" not, then you should match "badkitty"
+       first and use the "none" value: >
+          set keyprotocol=badkitty:none,kitty:kitty
+ <
+       The option is used after 'term' has been changed.  First the termcap
+       entries are set, possibly using the builtin list, see |builtin-terms|.
+       Then this option is inspected and if there is a match and a protocol
+       is specified the following happens:
+               none    Nothing, the regular t_TE and t_TI values remain
+ 
+               mok2    The t_TE value is changed to:
+                           CSI >4;m    disables modifyOtherKeys
+                       The t_TI value is changed to:
+                           CSI >4;2m   enables modifyOtherKeys
+ 
+               kitty   The t_TE value is changed to:
+                           CSI >4;m    disables modifyOtherKeys
+                           CSI <u      disables the kitty keyboard protocol
+                       The t_TI value is changed to:
+                           CSI >1u     enables the kitty keyboard protocol
+                           CSI ?u      request kitty keyboard protocol state
+                           CSI >c      request the termresponse
+ 
+ 
                                        *'keywordprg'* *'kp'*
  'keywordprg' 'kp'     string  (default "man" or "man -s",  DOS: ":help",
                                                                  VMS: "help")
*** ../vim-9.0.0929/runtime/doc/quickref.txt    2022-10-03 15:27:30.062072077 
+0100
--- runtime/doc/quickref.txt    2022-11-23 18:24:23.658222566 +0000
***************
*** 689,694 ****
--- 689,695 ----
  'edcompatible'          'ed'      toggle flags of ":substitute" command
  'emoji'                 'emo'     emoji characters are considered full width
  'encoding'      'enc'     encoding used internally
+ 'endoffile'     'eof'     write CTRL-Z at end of the file
  'endofline'     'eol'     write <EOL> for last line in file
  'equalalways'   'ea'      windows are automatically made the same size
  'equalprg'      'ep'      external program to use for "=" command
***************
*** 774,779 ****
--- 775,781 ----
  'key'                     encryption key
  'keymap'        'kmp'     name of a keyboard mapping
  'keymodel'      'km'      enable starting/stopping selection with keys
+ 'keyprotocol'   'kpc'     what keyboard protocol to use for what terminal
  'keywordprg'    'kp'      program to use for the "K" command
  'langmap'       'lmap'    alphabetic characters for other language mode
  'langmenu'      'lm'      language to be used for the menus
***************
*** 785,790 ****
--- 787,793 ----
  'lines'                           number of lines in the display
  'linespace'     'lsp'     number of pixel lines to use between characters
  'lisp'                            automatic indenting for Lisp
+ 'lispoptions'   'lop'     changes how Lisp indenting is done
  'lispwords'     'lw'      words that change how lisp indenting works
  'list'                            show <Tab> and <EOL>
  'listchars'     'lcs'     characters for displaying in list mode
*** ../vim-9.0.0929/src/libvterm/src/state.c    2022-10-16 19:26:31.680328579 
+0100
--- src/libvterm/src/state.c    2022-11-23 19:49:29.609048151 +0000
***************
*** 1423,1431 ****
      }
      break;
  
!   case LEADER('>', 0x6d): // xterm resource modifyOtherKeys
      if (argcount == 2 && args[0] == 4)
        state->mode.modify_other_keys = args[1] == 2;
      break;
  
    case 0x6e: // DSR - ECMA-48 8.3.35
--- 1423,1459 ----
      }
      break;
  
!   case LEADER('>', 0x6d): // CSI > 4 ; Pv m   xterm resource modifyOtherKeys
      if (argcount == 2 && args[0] == 4)
+     {
+       // can't have both modify_other_keys and kitty_keyboard
+       state->mode.kitty_keyboard = 0;
+ 
        state->mode.modify_other_keys = args[1] == 2;
+     }
+     break;
+ 
+   case LEADER('>', 0x75): // CSI > 1 u  enable kitty keyboard protocol
+     if (argcount == 1 && args[0] == 1)
+     {
+       // can't have both modify_other_keys and kitty_keyboard
+       state->mode.modify_other_keys = 0;
+ 
+       state->mode.kitty_keyboard = 1;
+     }
+     break;
+ 
+   case LEADER('<', 0x75): // CSI < u  disable kitty keyboard protocol
+     if (argcount <= 1)
+       state->mode.kitty_keyboard = 0;
+     break;
+ 
+   case LEADER('?', 0x75): // CSI ? u  request kitty keyboard protocol state
+     if (argcount <= 1)
+       // TODO: this only uses the values zero and one.  The protocol specifies
+       // more values, the progressive enhancement flags.
+       vterm_push_output_sprintf_ctrl(state->vt, C1_CSI, "?%du",
+                                                  state->mode.kitty_keyboard);
      break;
  
    case 0x6e: // DSR - ECMA-48 8.3.35
*** ../vim-9.0.0929/src/libvterm/src/vterm_internal.h   2022-10-17 
14:51:31.694963479 +0100
--- src/libvterm/src/vterm_internal.h   2022-11-22 20:59:58.247599793 +0000
***************
*** 130,135 ****
--- 130,136 ----
      unsigned int bracketpaste:1;
      unsigned int report_focus:1;
      unsigned int modify_other_keys:1;
+     unsigned int kitty_keyboard:1;
    } mode;
  
    VTermEncodingInstance encoding[4], encoding_utf8;
*** ../vim-9.0.0929/src/libvterm/src/keyboard.c 2022-11-19 19:02:33.953452663 
+0000
--- src/libvterm/src/keyboard.c 2022-11-23 18:57:16.529085513 +0000
***************
*** 10,15 ****
--- 10,21 ----
    return vt->state->mode.modify_other_keys;
  }
  
+ // VIM: added
+ int vterm_is_kitty_keyboard(VTerm *vt)
+ {
+   return vt->state->mode.kitty_keyboard;
+ }
+ 
  
  void vterm_keyboard_unichar(VTerm *vt, uint32_t c, VTermModifier mod)
  {
***************
*** 19,24 ****
--- 25,36 ----
      return;
    }
  
+   // VIM: added kitty keyboard protocol support
+   if (vterm_is_kitty_keyboard(vt) && mod != 0) {
+     vterm_push_output_sprintf_ctrl(vt, C1_CSI, "%d;%du", c, mod+1);
+     return;
+   }
+ 
    /* The shift modifier is never important for Unicode characters
     * apart from Space
     */
***************
*** 166,173 ****
      break;
  
    case KEYCODE_TAB:
      /* Shift-Tab is CSI Z but plain Tab is 0x09 */
!     if(mod == VTERM_MOD_SHIFT)
        vterm_push_output_sprintf_ctrl(vt, C1_CSI, "Z");
      else if(mod & VTERM_MOD_SHIFT)
        vterm_push_output_sprintf_ctrl(vt, C1_CSI, "1;%dZ", mod+1);
--- 178,187 ----
      break;
  
    case KEYCODE_TAB:
+     if (vterm_is_kitty_keyboard(vt) && mod != 0)
+       vterm_push_output_sprintf_ctrl(vt, C1_CSI, "9;%du", mod+1);
      /* Shift-Tab is CSI Z but plain Tab is 0x09 */
!     else if(mod == VTERM_MOD_SHIFT)
        vterm_push_output_sprintf_ctrl(vt, C1_CSI, "Z");
      else if(mod & VTERM_MOD_SHIFT)
        vterm_push_output_sprintf_ctrl(vt, C1_CSI, "1;%dZ", mod+1);
***************
*** 186,192 ****
    case KEYCODE_LITERAL: case_LITERAL:
      if (vterm_is_modify_other_keys(vt) && mod != 0)
        vterm_push_output_sprintf_ctrl(vt, C1_CSI, "27;%d;%d~", mod+1, 
k.literal);
!     else if(mod & (VTERM_MOD_SHIFT|VTERM_MOD_CTRL))
        vterm_push_output_sprintf_ctrl(vt, C1_CSI, "%d;%du", k.literal, mod+1);
      else
        vterm_push_output_sprintf(vt, mod & VTERM_MOD_ALT ? ESC_S "%c" : "%c", 
k.literal);
--- 200,209 ----
    case KEYCODE_LITERAL: case_LITERAL:
      if (vterm_is_modify_other_keys(vt) && mod != 0)
        vterm_push_output_sprintf_ctrl(vt, C1_CSI, "27;%d;%d~", mod+1, 
k.literal);
!     else if (vterm_is_kitty_keyboard(vt) && mod == 0 && k.literal == '\x1b')
!       vterm_push_output_sprintf_ctrl(vt, C1_CSI, "%du", k.literal);
!     else if ((vterm_is_kitty_keyboard(vt) && mod != 0)
!       || (mod & (VTERM_MOD_SHIFT|VTERM_MOD_CTRL)))
        vterm_push_output_sprintf_ctrl(vt, C1_CSI, "%d;%du", k.literal, mod+1);
      else
        vterm_push_output_sprintf(vt, mod & VTERM_MOD_ALT ? ESC_S "%c" : "%c", 
k.literal);
*** ../vim-9.0.0929/src/libvterm/include/vterm.h        2022-10-17 
14:51:31.694963479 +0100
--- src/libvterm/include/vterm.h        2022-11-22 21:04:34.919765512 +0000
***************
*** 368,373 ****
--- 368,374 ----
  size_t vterm_output_read(VTerm *vt, char *buffer, size_t len);
  
  int vterm_is_modify_other_keys(VTerm *vt);
+ int vterm_is_kitty_keyboard(VTerm *vt);
  void vterm_keyboard_unichar(VTerm *vt, uint32_t c, VTermModifier mod);
  void vterm_keyboard_key(VTerm *vt, VTermKey key, VTermModifier mod);
  
*** ../vim-9.0.0929/src/term.c  2022-11-22 22:03:34.415747693 +0000
--- src/term.c  2022-11-23 20:09:45.733585214 +0000
***************
*** 587,592 ****
--- 587,618 ----
  };
  
  /*
+  * Additions for using modifyOtherKeys level 2.  Same as what is used for
+  * xterm.
+  */
+ static tcap_entry_T builtin_mok2[] = {
+     {(int)KS_CTI,     "\033[>4;2m"},
+     {(int)KS_CTE,     "\033[>4;m"},
+ 
+     {(int)KS_NAME,    NULL}  // end marker
+ };
+ 
+ /*
+  * Additions for using the Kitty keyboard protocol.
+  */
+ static tcap_entry_T builtin_kitty[] = {
+     // t_TI enables the kitty keyboard protocol, requests the kitty keyboard
+     // protocol state and requests the version response.
+     {(int)KS_CTI,     "\033[>1u\033[?u\033[>c"},
+ 
+     // t_TE also disabled modifyOtherKeys, because t_TI from xterm may already
+     // have been used.
+     {(int)KS_CTE,     "\033[>4;m\033[<u"},
+ 
+     {(int)KS_NAME,    NULL}  // end marker
+ };
+ 
+ /*
   * iris-ansi for Silicon Graphics machines.
   */
  static tcap_entry_T builtin_iris_ansi[] = {
***************
*** 1494,1519 ****
  }
  
  /*
!  * Parsing of the builtin termcap entries.
!  * Caller should check if "term" is a valid builtin terminal name.
!  * The terminal's name is not set, as this is already done in termcapinit().
   */
      static void
! parse_builtin_tcap(char_u *term)
  {
-     tcap_entry_T *p = find_builtin_term(term);
-     if (p == NULL)  // builtin term not found
-       return;
- 
      int term_8bit = term_is_8bit(term);
  
!     for ( ; p->bt_entry != (int)KS_NAME && p->bt_entry != BT_EXTRA_KEYS; ++p)
      {
        if ((int)p->bt_entry >= 0)      // KS_xx entry
        {
!           // Only set the value if it wasn't set yet.
            if (term_strings[p->bt_entry] == NULL
!                                || term_strings[p->bt_entry] == empty_option)
            {
  #ifdef FEAT_EVAL
                int opt_idx = -1;
--- 1520,1541 ----
  }
  
  /*
!  * Apply entries from a builtin termcap.
   */
      static void
! apply_builtin_tcap(char_u *term, tcap_entry_T *entries, int overwrite)
  {
      int term_8bit = term_is_8bit(term);
  
!     for (tcap_entry_T *p = entries;
!             p->bt_entry != (int)KS_NAME && p->bt_entry != BT_EXTRA_KEYS; ++p)
      {
        if ((int)p->bt_entry >= 0)      // KS_xx entry
        {
!           // Only set the value if it wasn't set yet or "overwrite" is TRUE.
            if (term_strings[p->bt_entry] == NULL
!                   || term_strings[p->bt_entry] == empty_option
!                   || overwrite)
            {
  #ifdef FEAT_EVAL
                int opt_idx = -1;
***************
*** 1557,1569 ****
            char_u  name[2];
            name[0] = KEY2TERMCAP0((int)p->bt_entry);
            name[1] = KEY2TERMCAP1((int)p->bt_entry);
!           if (find_termcode(name) == NULL)
                add_termcode(name, (char_u *)p->bt_string, term_8bit);
        }
      }
  }
  
  /*
   * Set number of colors.
   * Store it as a number in t_colors.
   * Store it as a string in T_CCO (using nr_colors[]).
--- 1579,1604 ----
            char_u  name[2];
            name[0] = KEY2TERMCAP0((int)p->bt_entry);
            name[1] = KEY2TERMCAP1((int)p->bt_entry);
!           if (find_termcode(name) == NULL || overwrite)
                add_termcode(name, (char_u *)p->bt_string, term_8bit);
        }
      }
  }
  
  /*
+  * Parsing of the builtin termcap entries.
+  * Caller should check if "term" is a valid builtin terminal name.
+  * The terminal's name is not set, as this is already done in termcapinit().
+  */
+     static void
+ parse_builtin_tcap(char_u *term)
+ {
+     tcap_entry_T *entries = find_builtin_term(term);
+     if (entries != NULL)
+       apply_builtin_tcap(term, entries, FALSE);
+ }
+ 
+ /*
   * Set number of colors.
   * Store it as a number in t_colors.
   * Store it as a string in T_CCO (using nr_colors[]).
***************
*** 1799,1804 ****
--- 1834,1900 ----
  }
  
  /*
+  * Parse the 'keyprotocol' option, match against "term" and return the 
protocol
+  * for the first matching entry.
+  * When "term" is NULL then compile all patterns to check for any errors.
+  * Returns KEYPROTOCOL_FAIL if a pattern cannot be compiled.
+  * Returns KEYPROTOCOL_NONE if there is no match.
+  */
+     keyprot_T
+ match_keyprotocol(char_u *term)
+ {
+     int               len = (int)STRLEN(p_kpc) + 1;
+     char_u    *buf = alloc(len);
+     if (buf == NULL)
+       return KEYPROTOCOL_FAIL;
+ 
+     keyprot_T ret = KEYPROTOCOL_FAIL;
+     char_u    *p = p_kpc;
+     while (*p != NUL)
+     {
+       // Isolate one comma separated item.
+       (void)copy_option_part(&p, buf, len, ",");
+       char_u *colon = vim_strchr(buf, ':');
+       if (colon == NULL || colon == buf || colon[1] == NUL)
+           goto theend;
+       *colon = NUL;
+ 
+       keyprot_T prot;
+       if (STRCMP(colon + 1, "none") == 0)
+           prot = KEYPROTOCOL_NONE;
+       else if (STRCMP(colon + 1, "mok2") == 0)
+           prot = KEYPROTOCOL_MOK2;
+       else if (STRCMP(colon + 1, "kitty") == 0)
+           prot = KEYPROTOCOL_KITTY;
+       else
+           goto theend;
+ 
+       regmatch_T regmatch;
+       CLEAR_FIELD(regmatch);
+       regmatch.rm_ic = TRUE;
+       regmatch.regprog = vim_regcomp(buf, RE_MAGIC);
+       if (regmatch.regprog == NULL)
+           goto theend;
+ 
+       int match = term != NULL && vim_regexec(&regmatch, term, (colnr_T)0);
+       vim_regfree(regmatch.regprog);
+       if (match)
+       {
+           ret = prot;
+           goto theend;
+       }
+ 
+     }
+ 
+     // No match found, use "none".
+     ret = KEYPROTOCOL_NONE;
+ 
+ theend:
+     vim_free(buf);
+     return ret;
+ }
+ 
+ /*
   * Set terminal options for terminal "term".
   * Return OK if terminal 'term' was found in a termcap, FAIL otherwise.
   *
***************
*** 1924,1929 ****
--- 2020,2034 ----
            }
  #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))
            {
***************
*** 3562,3567 ****
--- 3667,3691 ----
  }
  
  /*
+  * Output T_CTE, the t_TE termcap entry, and handle expected effects.
+  * The code possibly disables modifyOtherKeys and the Kitty keyboard protocol.
+  */
+     void
+ out_str_t_TE(void)
+ {
+     out_str(T_CTE);
+ 
+     // When the kitty keyboard protocol is enabled we expect t_TE to disable
+     // it.  Remembering that it was detected to be enabled is useful in some
+     // situations.
+     if (kitty_protocol_state == KKPS_ENABLED
+           || kitty_protocol_state == KKPS_DISABLED)
+       kitty_protocol_state = KKPS_DISABLED;
+     else
+       kitty_protocol_state = KKPS_AFTER_T_KE;
+ }
+ 
+ /*
   * Set the terminal to TMODE_RAW (for Normal mode) or TMODE_COOK (for external
   * commands and Ex mode).
   */
***************
*** 3613,3619 ****
                if (tmode != TMODE_RAW)
                {
                    out_str(T_BD);      // disable bracketed paste mode
!                   out_str(T_CTE);     // possibly disables modifyOtherKeys
                }
                else
                {
--- 3737,3743 ----
                if (tmode != TMODE_RAW)
                {
                    out_str(T_BD);      // disable bracketed paste mode
!                   out_str_t_TE();     // possibly disables modifyOtherKeys
                }
                else
                {
***************
*** 3714,3720 ****
        out_flush();
        termcap_active = FALSE;
        cursor_on();                    // just in case it is still off
!       out_str(T_CTE);                 // stop "raw" mode
        out_str(T_TE);                  // stop termcap mode
        screen_start();                 // don't know where cursor is now
        out_flush();
--- 3838,3845 ----
        out_flush();
        termcap_active = FALSE;
        cursor_on();                    // just in case it is still off
!       out_str_t_TE();                 // stop "raw" mode, modifyOtherKeys and
!                                       // Kitty keyboard protocol
        out_str(T_TE);                  // stop termcap mode
        screen_start();                 // don't know where cursor is now
        out_flush();
***************
*** 5107,5112 ****
--- 5232,5246 ----
  # endif
      }
  
+     // Kitty keyboard protocol status response: CSI ? flags u
+     else if (first == '?' && argc == 1 && trail == 'u')
+     {
+       // The protocol has various "progressive enhancement flags" values, but
+       // we only check for zero and non-zero here.
+       kitty_protocol_state = arg[0] == '0' ? KKPS_OFF : KKPS_ENABLED;
+       *slen = csi_len;
+     }
+ 
      // Check for a window position response from the terminal:
      //       {lead}3;{x};{y}t
      else if (did_request_winpos && argc == 3 && arg[0] == 3
*** ../vim-9.0.0929/src/proto/term.pro  2022-10-04 13:17:27.496307898 +0100
--- src/proto/term.pro  2022-11-23 18:08:57.588076924 +0000
***************
*** 4,9 ****
--- 4,10 ----
  void init_term_props(int all);
  void f_terminalprops(typval_T *argvars, typval_T *rettv);
  void set_color_count(int nr);
+ keyprot_T match_keyprotocol(char_u *term);
  int set_termname(char_u *term);
  void free_cur_term(void);
  void getlinecol(long *cp, long *rp);
***************
*** 46,51 ****
--- 47,53 ----
  void shell_resized(void);
  void shell_resized_check(void);
  void set_shellsize(int width, int height, int mustset);
+ void out_str_t_TE(void);
  void settmode(tmode_T tmode);
  void starttermcap(void);
  void stoptermcap(void);
*** ../vim-9.0.0929/src/terminal.c      2022-11-19 19:02:33.957452667 +0000
--- src/terminal.c      2022-11-23 14:43:22.806791976 +0000
***************
*** 1587,1592 ****
--- 1587,1599 ----
      if (modmask & (MOD_MASK_ALT | MOD_MASK_META))
        mod |= VTERM_MOD_ALT;
  
+     // Ctrl-Shift-i may have the key "I" instead of "i", but for the kitty
+     // keyboard protocol should use "i".  Applies to all ascii letters.
+     if (ASCII_ISUPPER(c)
+           && vterm_is_kitty_keyboard(curbuf->b_term->tl_vterm)
+           && mod == (VTERM_MOD_CTRL | VTERM_MOD_SHIFT))
+       c = TOLOWER_ASC(c);
+ 
      /*
       * Convert special keys to vterm keys:
       * - Write keys to vterm: vterm_keyboard_key()
***************
*** 2182,2187 ****
--- 2189,2207 ----
  
  static reduce_key_state_T  no_reduce_key_state = NRKS_NONE;
  
+ /*
+  * Return TRUE if the term is using modifyOtherKeys level 2 or the kitty
+  * keyboard protocol.
+  */
+     static int
+ vterm_using_key_protocol(void)
+ {
+     return curbuf->b_term != NULL
+       && curbuf->b_term->tl_vterm != NULL
+       && (vterm_is_modify_other_keys(curbuf->b_term->tl_vterm)
+               || vterm_is_kitty_keyboard(curbuf->b_term->tl_vterm));
+ }
+ 
      void
  check_no_reduce_keys(void)
  {
***************
*** 2191,2199 ****
            || curbuf->b_term->tl_vterm == NULL)
        return;
  
!     if (vterm_is_modify_other_keys(curbuf->b_term->tl_vterm))
      {
!       // "modify_other_keys" was enabled while waiting.
        no_reduce_key_state = NRKS_SET;
        ++no_reduce_keys;
      }
--- 2211,2220 ----
            || curbuf->b_term->tl_vterm == NULL)
        return;
  
!     if (vterm_using_key_protocol())
      {
!       // "modify_other_keys" or kitty keyboard protocol was enabled while
!       // waiting.
        no_reduce_key_state = NRKS_SET;
        ++no_reduce_keys;
      }
***************
*** 2216,2223 ****
      ctrl_break_was_pressed = FALSE;
  #endif
  
!     if (curbuf->b_term->tl_vterm != NULL
!                      && vterm_is_modify_other_keys(curbuf->b_term->tl_vterm))
      {
        ++no_reduce_keys;
        no_reduce_key_state = NRKS_SET;
--- 2237,2243 ----
      ctrl_break_was_pressed = FALSE;
  #endif
  
!     if (vterm_using_key_protocol())
      {
        ++no_reduce_keys;
        no_reduce_key_state = NRKS_SET;
***************
*** 2641,2652 ****
  
  /*
   * When modify_other_keys is set then do the reverse of raw_c_to_ctrl().
   * May set "mod_mask".
   */
      static int
  ctrl_to_raw_c(int c)
  {
!     if (c < 0x20 && vterm_is_modify_other_keys(curbuf->b_term->tl_vterm))
      {
        mod_mask |= MOD_MASK_CTRL;
        return c + '@';
--- 2661,2673 ----
  
  /*
   * When modify_other_keys is set then do the reverse of raw_c_to_ctrl().
+  * Also when the Kitty keyboard protocol is used.
   * May set "mod_mask".
   */
      static int
  ctrl_to_raw_c(int c)
  {
!     if (c < 0x20 && vterm_using_key_protocol())
      {
        mod_mask |= MOD_MASK_CTRL;
        return c + '@';
*** ../vim-9.0.0929/src/globals.h       2022-10-13 14:00:42.223235679 +0100
--- src/globals.h       2022-11-23 13:12:22.851824767 +0000
***************
*** 1377,1382 ****
--- 1377,1400 ----
  // no longer be used.
  EXTERN int seenModifyOtherKeys INIT(= FALSE);
  
+ // The state for the Kitty keyboard protocol.
+ typedef enum {
+     // Initially we have no clue if the protocol is on or off.
+     KKPS_INITIAL,
+     // Used when receiving the state and the flags are zero.
+     KKPS_OFF,
+     // Used when receiving the state and the flags are non-zero.
+     KKPS_ENABLED,
+     // Used after outputting t_KE when the state was KKPS_ENABLED.  We do not
+     // really know if t_KE actually disabled the protocol, the following t_KI
+     // is expected to request the state, but the response may come only later.
+     KKPS_DISABLED,
+     // Used after outputting t_KE when the state was not KKPS_ENABLED.
+     KKPS_AFTER_T_KE,
+ } kkpstate_T;
+ 
+ EXTERN kkpstate_T kitty_protocol_state INIT(= KKPS_INITIAL);
+ 
  EXTERN int no_mapping INIT(= FALSE);  // currently no mapping allowed
  EXTERN int no_zero_mapping INIT(= 0); // mapping zero not allowed
  EXTERN int allow_keys INIT(= FALSE);  // allow key codes when no_mapping
*** ../vim-9.0.0929/src/vim.h   2022-11-22 12:40:44.062427876 +0000
--- src/vim.h   2022-11-23 15:18:09.199246996 +0000
***************
*** 2257,2262 ****
--- 2257,2270 ----
      ESTACK_SCRIPT,
  } estack_arg_T;
  
+ // Return value of match_keyprotocol()
+ typedef enum {
+     KEYPROTOCOL_NONE,
+     KEYPROTOCOL_MOK2,
+     KEYPROTOCOL_KITTY,
+     KEYPROTOCOL_FAIL
+ } keyprot_T;
+ 
  // Flags for assignment functions.
  #define ASSIGN_VAR    0     // ":var" (nothing special)
  #define ASSIGN_FINAL  0x01  // ":final"
*** ../vim-9.0.0929/src/edit.c  2022-11-22 12:40:44.066427878 +0000
--- src/edit.c  2022-11-23 13:17:02.283856791 +0000
***************
*** 330,336 ****
  
        // Disable modifyOtherKeys, keys with modifiers would cause exiting
        // Insert mode.
!       out_str(T_CTE);
      }
  
      /*
--- 330,336 ----
  
        // Disable modifyOtherKeys, keys with modifiers would cause exiting
        // Insert mode.
!       out_str_t_TE();
      }
  
      /*
*** ../vim-9.0.0929/src/normal.c        2022-11-18 14:07:16.116692387 +0000
--- src/normal.c        2022-11-23 13:19:05.575876163 +0000
***************
*** 424,430 ****
            // Disable bracketed paste and modifyOtherKeys here, we won't
            // recognize the escape sequences with 'esckeys' off.
            out_str(T_BD);
!           out_str(T_CTE);
        }
  
        *cp = plain_vgetc();
--- 424,430 ----
            // Disable bracketed paste and modifyOtherKeys here, we won't
            // recognize the escape sequences with 'esckeys' off.
            out_str(T_BD);
!           out_str_t_TE();
        }
  
        *cp = plain_vgetc();
*** ../vim-9.0.0929/src/os_unix.c       2022-11-18 22:14:04.802988148 +0000
--- src/os_unix.c       2022-11-23 13:19:19.987878564 +0000
***************
*** 5348,5356 ****
                long delay_msec = 1;
  
                if (tmode == TMODE_RAW)
!                   // possibly disables modifyOtherKeys, so that the system
!                   // can recognize CTRL-C
!                   out_str(T_CTE);
  
                /*
                 * Similar to the loop above, but only handle X events, no
--- 5348,5356 ----
                long delay_msec = 1;
  
                if (tmode == TMODE_RAW)
!                   // Possibly disables modifyOtherKeys, so that the system
!                   // can recognize CTRL-C.
!                   out_str_t_TE();
  
                /*
                 * Similar to the loop above, but only handle X events, no
*** ../vim-9.0.0929/src/map.c   2022-11-13 20:43:14.928366211 +0000
--- src/map.c   2022-11-23 13:30:35.244014077 +0000
***************
*** 312,319 ****
      // Prevent mappings to be cleared while at the more prompt.
      ++map_locked;
  
!     if (p_verbose > 0 && keyround == 1 && seenModifyOtherKeys)
!       msg_puts(_("Seen modifyOtherKeys: true"));
  
      // need to loop over all global hash lists
      for (int hash = 0; hash < 256 && !got_int; ++hash)
--- 312,338 ----
      // Prevent mappings to be cleared while at the more prompt.
      ++map_locked;
  
!     if (p_verbose > 0 && keyround == 1)
!     {
!       if (seenModifyOtherKeys)
!           msg_puts(_("Seen modifyOtherKeys: true"));
!       if (kitty_protocol_state != KKPS_INITIAL)
!       {
!           char *name = _("Unknown");
!           switch (kitty_protocol_state)
!           {
!               case KKPS_INITIAL: break;
!               case KKPS_OFF: name = _("Off"); break;
!               case KKPS_ENABLED: name = _("On"); break;
!               case KKPS_DISABLED: name = _("Disabled"); break;
!               case KKPS_AFTER_T_KE: name = _("Cleared"); break;
!           }
! 
!           char buf[200];
!           vim_snprintf(buf, sizeof(buf), _("Kitty keyboard protocol: %s"), 
name);
!           msg_puts(buf);
!       }
!     }
  
      // need to loop over all global hash lists
      for (int hash = 0; hash < 256 && !got_int; ++hash)
*** ../vim-9.0.0929/src/optiondefs.h    2022-11-12 11:54:19.205571527 +0000
--- src/optiondefs.h    2022-11-23 15:11:14.483536955 +0000
***************
*** 1447,1452 ****
--- 1447,1456 ----
      {"keymodel",    "km",   P_STRING|P_VI_DEF|P_ONECOMMA|P_NODUP,
                            (char_u *)&p_km, PV_NONE,
                            {(char_u *)"", (char_u *)0L} SCTX_INIT},
+     {"keyprotocol", "kpc",  P_STRING|P_VI_DEF|P_ONECOMMA|P_NODUP,
+                           (char_u *)&p_kpc, PV_NONE,
+                           {(char_u 
*)"kitty:kitty,foot:kitty,wezterm:kitty,xterm:mok2", (char_u *)0L}
+                           SCTX_INIT},
      {"keywordprg",  "kp",   P_STRING|P_EXPAND|P_VI_DEF|P_SECURE,
                            (char_u *)&p_kp, PV_KP,
                            {
*** ../vim-9.0.0929/src/optionstr.c     2022-10-15 16:04:43.994187227 +0100
--- src/optionstr.c     2022-11-23 15:25:32.714548055 +0000
***************
*** 1663,1668 ****
--- 1663,1675 ----
        }
      }
  
+     // 'keyprotocol'
+     else if (varp == &p_kpc)
+     {
+       if (match_keyprotocol(NULL) == KEYPROTOCOL_FAIL)
+           errmsg = e_invalid_argument;
+     }
+ 
      // 'mousemodel'
      else if (varp == &p_mousem)
      {
*** ../vim-9.0.0929/src/option.h        2022-10-22 10:06:38.424322862 +0100
--- src/option.h        2022-11-23 15:15:22.827355219 +0000
***************
*** 698,703 ****
--- 698,704 ----
  #endif
  EXTERN char_u *p_kp;          // 'keywordprg'
  EXTERN char_u *p_km;          // 'keymodel'
+ EXTERN char_u *p_kpc;         // 'keyprotocol'
  #ifdef FEAT_LANGMAP
  EXTERN char_u *p_langmap;     // 'langmap'
  EXTERN int    p_lnr;          // 'langnoremap'
*** ../vim-9.0.0929/src/testdir/gen_opt_test.vim        2022-10-15 
16:41:49.780490432 +0100
--- src/testdir/gen_opt_test.vim        2022-11-23 18:37:11.779044423 +0000
***************
*** 113,118 ****
--- 113,120 ----
        \ 'isprint': [['', '@', '@,48-52'], ['xxx', '@48']],
        \ 'keymap': [['', 'accents'], ['xxx']],
        \ 'keymodel': [['', 'startsel', 'startsel,stopsel'], ['xxx']],
+       \ 'keyprotocol': [['', 'xxx:none', 'yyy:mok2', 'zzz:kitty'],
+       \               [':none', 'xxx:', 'x:non', 'y:mok3', 'z:kittty']],
        \ 'langmap': [['', 'xX', 'aA,bB'], ['xxx']],
        \ 'lispoptions': [['', 'expr:0', 'expr:1'], ['xxx']],
        \ 'listchars': [['', 'eol:x', 'eol:x,space:y'], ['xxx']],
*** ../vim-9.0.0929/src/testdir/keycode_check.json      2022-11-19 
19:02:33.957452667 +0000
--- src/testdir/keycode_check.json      2022-11-23 20:06:44.121534960 +0000
***************
*** 1 ****
! 
{"12xterm":{"Space":"20","version":"1b5b3e34313b3335363b3063","C-Tab":"1b5b32373b353b397e","A-Esc":"1b5b32373b333b32377e","C-Space":"1b5b32373b353b33327e","status":"","S-C-I":"1b5b32373b363b37337e","C-I":"1b5b32373b353b3130357e","S-Tab":"1b5b5a","Tab":"09","S-Space":"1b5b32373b323b33327e","A-Tab":"1b5b32373b333b397e","C-Esc":"1b5b32373b353b32377e","protocol":"mok2","A-Space":"1b5b32373b333b33327e","S-Esc":"1b","Esc":"1b"},"libvterm":{"Space":"20","version":"1b5b3e303b3130303b3063","C-Tab":"1b5b32373b353b397e","A-Esc":"1b5b32373b333b32377e","C-Space":"1b5b32373b353b33327e","status":"","S-C-I":"1b5b32373b363b37337e","C-I":"1b5b32373b353b3130357e","S-Tab":"1b5b5a","Tab":"09","resource":"","A-Tab":"1b5b32373b333b397e","S-Space":"1b5b32373b323b33327e","C-Esc":"1b5b32373b353b32377e","protocol":"mok2","A-Space":"1b5b32373b333b33327e","S-Esc":"1b","Esc":"1b"},"2kitty":{"Space":"20","version":"1b5b3e313b343030303b323163","C-Tab":"","A-Esc":"1b5b32373b313175","C-Space":"1b5b33323b3575","stat
 
us":"1b5b3f3175","S-C-I":"1b5b3130353b3675","C-I":"1b5b3130353b3575","S-Tab":"1b5b393b3275","Tab":"09","S-Space":"20","A-Tab":"1b5b393b313175","C-Esc":"1b5b32373b3575","protocol":"kitty","A-Space":"1b5b33323b313175","S-Esc":"1b5b32373b3275","Esc":"1b5b323775"},"11xterm":{"Space":"20","version":"1b5b3e34313b3335363b3063","C-Tab":"09","A-Esc":"9b00","status":"","S-C-I":"09","C-I":"09","S-Tab":"1b5b5a","Tab":"09","S-Space":"20","A-Tab":"8900","C-Esc":"1b","protocol":"none","A-Space":"a000","S-Esc":"1b","Esc":"1b"}}
--- 1 ----
! 
{"31kitty":{"Space":"20","version":"1b5b3e313b343030303b323163","C-Tab":"","A-Esc":"1b5b32373b313175","C-Space":"1b5b33323b3575","status":"1b5b3f3175","S-C-I":"1b5b3130353b3675","C-I":"1b5b3130353b3575","S-Tab":"1b5b393b3275","Tab":"09","resource":"","A-Tab":"1b5b393b313175","S-Space":"20","C-Esc":"1b5b32373b3575","protocol":"kitty","A-Space":"1b5b33323b313175","S-Esc":"1b5b32373b3275","Esc":"1b5b323775"},"32libvterm":{"Space":"20","version":"1b5b3e303b3130303b3063","C-Tab":"1b5b393b3575","A-Esc":"1b5b32373b3375","C-Space":"1b5b33323b3575","status":"1b5b3f3175","S-C-I":"1b5b3130353b3675","C-I":"1b5b3130353b3575","S-Tab":"1b5b393b3275","Tab":"09","resource":"","A-Tab":"1b5b393b3375","S-Space":"1b5b33323b3275","C-Esc":"1b5b32373b3575","protocol":"kitty","A-Space":"1b5b33323b3375","S-Esc":"1b5b323775","Esc":"1b5b323775"},"22libvterm":{"Space":"20","version":"1b5b3e303b3130303b3063","C-Tab":"1b5b32373b353b397e","A-Esc":"1b5b32373b333b32377e","C-Space":"1b5b32373b353b33327e","status":""
 
,"S-C-I":"1b5b32373b363b37337e","C-I":"1b5b32373b353b3130357e","S-Tab":"1b5b5a","Tab":"09","resource":"","A-Tab":"1b5b32373b333b397e","S-Space":"1b5b32373b323b33327e","C-Esc":"1b5b32373b353b32377e","protocol":"mok2","A-Space":"1b5b32373b333b33327e","S-Esc":"1b","Esc":"1b"},"13kitty":{"Space":"20","version":"1b5b3e313b343030303b323163","C-Tab":"","A-Esc":"1b1b","status":"","S-C-I":"1b5b3130353b3675","C-I":"09","S-Tab":"1b5b5a","Tab":"09","resource":"","A-Tab":"1b09","S-Space":"20","C-Esc":"1b","protocol":"none","A-Space":"1b5b33323b313175","S-Esc":"1b","Esc":"1b"},"21xterm":{"Space":"20","version":"1b5b3e34313b3335363b3063","C-Tab":"1b5b32373b353b397e","A-Esc":"1b5b32373b333b32377e","C-Space":"1b5b32373b353b33327e","status":"","S-C-I":"1b5b32373b363b37337e","C-I":"1b5b32373b353b3130357e","S-Tab":"1b5b5a","Tab":"09","resource":"=30","A-Tab":"1b5b32373b333b397e","S-Space":"1b5b32373b323b33327e","C-Esc":"1b5b32373b353b32377e","protocol":"mok2","A-Space":"1b5b32373b333b33327e","S-Esc":"1
 
b","Esc":"1b"},"12libvterm":{"Space":"20","version":"1b5b3e303b3130303b3063","C-Tab":"1b5b393b3575","A-Esc":"9b00","status":"","S-C-I":"1b5b5a","C-I":"09","S-Tab":"1b5b5a","Tab":"09","S-Space":"1b5b33323b3275","A-Tab":"8900","resource":"","C-Esc":"1b5b32373b3575","protocol":"none","A-Space":"a000","S-Esc":"1b","Esc":"1b"},"11xterm":{"Space":"20","version":"1b5b3e34313b3335363b3063","C-Tab":"09","A-Esc":"9b00","status":"","S-C-I":"09","C-I":"09","S-Tab":"1b5b5a","Tab":"09","S-Space":"20","A-Tab":"8900","C-Esc":"1b","protocol":"none","A-Space":"a000","S-Esc":"1b","Esc":"1b"}}
*** ../vim-9.0.0929/src/version.c       2022-11-23 14:32:57.970383432 +0000
--- src/version.c       2022-11-23 20:15:19.849649961 +0000
***************
*** 697,698 ****
--- 697,700 ----
  {   /* Add new patch number below this line */
+ /**/
+     930,
  /**/

-- 
hundred-and-one symptoms of being an internet addict:
121. You ask for e-mail adresses instead of telephone numbers.

 /// Bram Moolenaar -- [email protected] -- http://www.Moolenaar.net   \\\
///                                                                      \\\
\\\        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ ///
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///

-- 
-- 
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

--- 
You received this message because you are subscribed to the Google Groups 
"vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/vim_dev/20221123202046.683151C211B%40moolenaar.net.

Raspunde prin e-mail lui