Skip to main content
Deno 2 is finally here ๐ŸŽ‰๏ธ
Learn more
Module

x/denops_std/batch/mod.ts>batch

๐Ÿ“š Standard module for denops.vim
Latest
function batch
import { batch } from "https://deno.land/x/denops_std@v6.5.1/batch/mod.ts";

Call multiple denops functions sequentially without RPC overhead

import type { Entrypoint } from "https://deno.land/x/denops_std@v6.5.1/mod.ts";
import { batch } from "https://deno.land/x/denops_std@v6.5.1/batch/mod.ts";

export const main: Entrypoint = async (denops) => {
  await batch(denops, async (denops) => {
    await denops.cmd("setlocal modifiable");
    await denops.cmd("setlocal noswap");
    await denops.cmd("setlocal nobackup");
  });
}

The function can be nested thus users can use functions which may internally use batch() like:

import type { Denops, Entrypoint } from "https://deno.land/x/denops_std@v6.5.1/mod.ts";
import { batch } from "https://deno.land/x/denops_std@v6.5.1/batch/mod.ts";

async function replace(denops: Denops, content: string): Promise<void> {
  await batch(denops, async (denops) => {
    await denops.cmd("setlocal modifiable");
    await denops.cmd("setlocal foldmethod=manual");
    await denops.call("setline", 1, content.split(/\r?\n/));
    await denops.cmd("setlocal nomodifiable");
    await denops.cmd("setlocal nomodified");
  });
}

export const main: Entrypoint = async (denops) => {
  await batch(denops, async (denops) => {
    await denops.cmd("edit Hello");
    await replace(denops, "Hello Darkness My Old Friend");
  });
}

Note that denops.call(), denops.batch(), or denops.eval() always return falsy value in batch(), indicating that you cannot write code like below:

import type { Entrypoint } from "https://deno.land/x/denops_std@v6.5.1/mod.ts";
import { batch } from "https://deno.land/x/denops_std@v6.5.1/batch/mod.ts";

export const main: Entrypoint = async (denops) => {
  await batch(denops, async (denops) => {
    // !!! DON'T DO THIS !!!
    if (await denops.eval("&verbose")) {
      await denops.cmd("echomsg 'VERBOSE'");
    }
    await denops.cmd("echomsg 'Hello world'");
  });
}

The denops instance passed to the batch block is available even outside of the block. It works like a real denops instance, mean that you can write code like:

import type { Entrypoint } from "https://deno.land/x/denops_std@v6.5.1/mod.ts";
import * as lambda from "https://deno.land/x/denops_std@v6.5.1/lambda/mod.ts";
import { batch } from "https://deno.land/x/denops_std@v6.5.1/batch/mod.ts";

export const main: Entrypoint = async (denops) => {
  await batch(denops, async (denops) => {
    const id = lambda.register(denops, async () => {
      // This code is called outside of 'batch' block
      // thus the 'denops' instance works like a real one.
      // That's why you can write code like below
      if (await denops.eval("&verbose")) {
        await denops.cmd("echomsg 'VERBOSE'");
      }
      await denops.cmd("echomsg 'Hello world'");
    });
    await denops.cmd(
      `command! Test call denops#request('${denops.name}', '${id}', [])`,
    );
  });
}

Note that denops.redraw() is executed only once after the batch is actually executed, no matter how many times it is called in the batch(). If the force option is specified even once, the last call will be the one with the force option specified.

Parameters

denops: Denops
executor: (helper: BatchHelper) => Promise<void>

Returns

Promise<void>