Repository: neovim/neovim
File: src/nvim/tui/tui.c (C)

Code before:
  FUNC_ATTR_NONNULL_ALL
{
  // 5 bytes for \x1b[?$p, 1 byte for null terminator, 6 bytes for mode digits (more than enough)
  char buf[12];
  int len = snprintf(buf, sizeof(buf), "\x1b[?%d$p", (int)mode);
  assert((len > 0) && (len < (int)sizeof(buf)));
  out(tui, buf, (size_t)len);
}

/// Set (DECSET) or reset (DECRST) a terminal mode.
static void tui_set_term_mode(TUIData *tui, TermMode mode, bool set)
  FUNC_ATTR_NONNULL_ALL
{
  char buf[12];
  int len = snprintf(buf, sizeof(buf), "\x1b[?%d%c", (int)mode, set ? 'h' : 'l');
  assert((len > 0) && (len < (int)sizeof(buf)));
  out(tui, buf, (size_t)len);
}

/// Handle a mode report (DECRPM) from the terminal.
void tui_handle_term_mode(TUIData *tui, TermMode mode, TermModeState state)
  FUNC_ATTR_NONNULL_ALL
{
  bool is_set = false;
  switch (state) {
  case kTermModeNotRecognized:
  case kTermModePermanentlyReset:
    // TODO(bfredl): This is really ILOG but we want it in all builds.
    // add to show_verbose_terminfo() without being too racy ????
    WLOG("TUI: terminal mode %d unavailable, state %d", mode, state);
    // If the mode is not recognized, or if the terminal emulator does not allow it to be changed,
    // then there is nothing to do
    break;
  case kTermModePermanentlySet:
  case kTermModeSet:
    is_set = true;
    FALLTHROUGH;
  case kTermModeReset:
    // The terminal supports changing the given mode
    WLOG("TUI: terminal mode %d detected, state %d", mode, state);
    switch (mode) {
    case kTermModeSynchronizedOutput:
      // Ref: https://gist.github.com/christianparpart/d8a62cc1ab659194337d73e399004036
      tui->unibi_ext.sync = (int)unibi_add_ext_str(tui->ut, "Sync",
                                                   "\x1b[?2026%?%p1%{1}%-%tl%eh%;");
      break;
    case kTermModeGraphemeClusters:
      if (!is_set) {
        tui_set_term_mode(tui, mode, true);
        tui->did_set_grapheme_cluster_mode = true;
      }

Review comment (suggestion):
Off-topic: should we add a terminal feature section to `:checkhealth`? (@gpanders)

Code after:
  char buf[12];
  int len = snprintf(buf, sizeof(buf), "\x1b[?%d$p", (int)mode);
  assert((len > 0) && (len < (int)sizeof(buf)));
  out(tui, buf, (size_t)len);
}

/// Set (DECSET) or reset (DECRST) a terminal mode.
static void tui_set_term_mode(TUIData *tui, TermMode mode, bool set)
  FUNC_ATTR_NONNULL_ALL
{
  char buf[12];
  int len = snprintf(buf, sizeof(buf), "\x1b[?%d%c", (int)mode, set ? 'h' : 'l');
  assert((len > 0) && (len < (int)sizeof(buf)));
  out(tui, buf, (size_t)len);
}

/// Handle a mode report (DECRPM) from the terminal.
void tui_handle_term_mode(TUIData *tui, TermMode mode, TermModeState state)
  FUNC_ATTR_NONNULL_ALL
{
  bool is_set = false;
  switch (state) {
  case kTermModeNotRecognized:
  case kTermModePermanentlyReset:
    // TODO(bfredl): This is really ILOG but we want it in all builds.
    // add to show_verbose_terminfo() without being too racy ????
    WLOG("TUI: terminal mode %d unavailable, state %d", mode, state);
    // If the mode is not recognized, or if the terminal emulator does not allow it to be changed,
    // then there is nothing to do
    break;
  case kTermModePermanentlySet:
  case kTermModeSet:
    is_set = true;
    FALLTHROUGH;
  case kTermModeReset:
    // The terminal supports changing the given mode
    WLOG("TUI: terminal mode %d detected, state %d", mode, state);
    switch (mode) {
    case kTermModeSynchronizedOutput:
      // Ref: https://gist.github.com/christianparpart/d8a62cc1ab659194337d73e399004036
      tui->unibi_ext.sync = (int)unibi_add_ext_str(tui->ut, "Sync",
                                                   "\x1b[?2026%?%p1%{1}%-%tl%eh%;");
      break;
    case kTermModeGraphemeClusters:
      if (!is_set) {
        tui_set_term_mode(tui, mode, true);
        tui->did_set_grapheme_cluster_mode = true;
      }
      break;
    case kTermModeThemeUpdates:
      if (!is_set) {