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

Code before:
  Arena arena = ARENA_EMPTY;
  const list_T *l = get_vim_var_list(VV_ARGV);
  int argc = tv_list_len(l);
  assert(argc > 0);
  Array argv = arena_array(&arena, (size_t)argc + 1);
  bool had_minmin = false;
  bool skipping_minc = false;  // Skip -c <cmd> from argv.
  bool first_minc = true;  // Avoid skipping the first -c <cmd> from argv.
  TV_LIST_ITER_CONST(l, li, {
    const char *arg = tv_get_string(TV_LIST_ITEM_TV(li));
    if (argv.size > 0 && !had_minmin && strequal(arg, "--")) {
      had_minmin = true;
      skipping_minc = false;
    }
    bool startswith_min = strlen(arg) > 0 && arg[0] == '-';
    bool startswith_minmin = strlen(arg) > 1 && arg[0] == '-' && arg[1] == '-';
    if (skipping_minc && (startswith_min || startswith_minmin)) {
      skipping_minc = false;
    }
    if (!had_minmin && !skipping_minc && strequal(arg, "-c")) {
      if (!first_minc) {
        skipping_minc = true;
        continue;
      }
      first_minc = false;
    }
    // Exclude --embed/--headless/-c <cmd> from `argv`, as the client may start the server in a
    // different way than how the server was originally started.
    if (argv.size == 0 || had_minmin
        || (!strequal(arg, "--embed") && !strequal(arg, "--headless") && !skipping_minc)) {
      ADD_C(argv, CSTR_AS_OBJ(arg));
    }
  });
  ADD_C(args, ARRAY_OBJ(argv));

  push_call(ui, "restart", args);
  arena_mem_free(arena_finish(&arena));
  return true;
}

// TODO(bfredl): use me to detach a specific ui from the server
void remote_ui_stop(RemoteUI *ui)
{
}

void nvim_ui_try_resize(uint64_t channel_id, Integer width, Integer height, Error *err)
  FUNC_API_SINCE(1) FUNC_API_REMOTE_ONLY
{
  RemoteUI *ui = get_ui_or_err(channel_id, err);
  if (!ui) {
    return;

Review comment (suggestion):
One problem here. If nvim is started with a `-c` arg and someone just attempts `:restart` on its own, that `-c` arg would be considered. But if they call `:restart <cmd>` then that `-c` they used initially would be skipped (two different behaviours). Don't know how to solve it though... Will think about this...

Code after:
  Arena arena = ARENA_EMPTY;
  const list_T *l = get_vim_var_list(VV_ARGV);
  int argc = tv_list_len(l);
  assert(argc > 0);
  Array argv = arena_array(&arena, (size_t)argc + 1);
  bool had_minmin = false;
  bool skipping_minc = false;  // Skip -c <cmd> from argv.
  bool first_minc = true;  // Avoid skipping the first -c <cmd> from argv.
  TV_LIST_ITER_CONST(l, li, {
    const char *arg = tv_get_string(TV_LIST_ITEM_TV(li));
    if (argv.size > 0 && !had_minmin && strequal(arg, "--")) {
      had_minmin = true;
      skipping_minc = false;
    }
    bool startswith_min = strlen(arg) > 0 && arg[0] == '-';
    bool startswith_minmin = strlen(arg) > 1 && arg[0] == '-' && arg[1] == '-';
    if (skipping_minc && (startswith_min || startswith_minmin)) {
      skipping_minc = false;
    }
    if (!had_minmin && !skipping_minc && strequal(arg, "-c")) {
      if (!first_minc) {
        skipping_minc = true;
        continue;
      }
      first_minc = false;
    }
    // Exclude --embed/--headless/-c <cmd> from `argv`, as the client may start the server in a
    // different way than how the server was originally started.
    // Eg: 'nvim -c foo -c bar --embed --headless -- example.txt' would be parsed as { 'nvim', '-c', 'foo', '--', 'example.txt' }.
    if (argv.size == 0 || had_minmin
        || (!strequal(arg, "--embed") && !strequal(arg, "--headless") && !skipping_minc)) {
      ADD_C(argv, CSTR_AS_OBJ(arg));
    }
  });
  ADD_C(args, ARRAY_OBJ(argv));

  push_call(ui, "restart", args);
  arena_mem_free(arena_finish(&arena));
  return true;
}

// TODO(bfredl): use me to detach a specific ui from the server
void remote_ui_stop(RemoteUI *ui)
{
}

void nvim_ui_try_resize(uint64_t channel_id, Integer width, Integer height, Error *err)
  FUNC_API_SINCE(1) FUNC_API_REMOTE_ONLY
{
  RemoteUI *ui = get_ui_or_err(channel_id, err);
  if (!ui) {