|
| 1 | +import { Denops } from "../deps.ts"; |
| 2 | +import * as fn from "../function/mod.ts"; |
| 3 | +import { Mapping, Mode } from "./types.ts"; |
| 4 | +import { parse } from "./parser.ts"; |
| 5 | + |
| 6 | +export type MapOptions = { |
| 7 | + mode?: Mode; |
| 8 | + noremap?: boolean; |
| 9 | + script?: boolean; |
| 10 | + buffer?: boolean; |
| 11 | + expr?: boolean; |
| 12 | + nowait?: boolean; |
| 13 | + silent?: boolean; |
| 14 | + unique?: boolean; |
| 15 | +}; |
| 16 | + |
| 17 | +/** |
| 18 | + * Register a mapping for `lhs` to `rhs` with given options. |
| 19 | + */ |
| 20 | +export async function map( |
| 21 | + denops: Denops, |
| 22 | + lhs: string, |
| 23 | + rhs: string, |
| 24 | + options: MapOptions = {}, |
| 25 | +): Promise<void> { |
| 26 | + const mode = options.mode ?? ""; |
| 27 | + const prefix = options.noremap ? "nore" : ""; |
| 28 | + const arg = [ |
| 29 | + options.buffer ? "<buffer>" : "", |
| 30 | + options.nowait ? "<nowait>" : "", |
| 31 | + options.silent ? "<silent>" : "", |
| 32 | + options.script ? "<script>" : "", |
| 33 | + options.expr ? "<expr>" : "", |
| 34 | + options.unique ? "<unique>" : "", |
| 35 | + ].filter((v) => v).join(""); |
| 36 | + await denops.cmd(`${mode}${prefix}map ${arg} ${lhs} ${rhs}`); |
| 37 | +} |
| 38 | + |
| 39 | +export type UnmapOptions = { |
| 40 | + mode?: Mode; |
| 41 | +}; |
| 42 | + |
| 43 | +/** |
| 44 | + * Remove a mapping for `lhs`. |
| 45 | + */ |
| 46 | +export async function unmap( |
| 47 | + denops: Denops, |
| 48 | + lhs: string, |
| 49 | + options: UnmapOptions = {}, |
| 50 | +): Promise<void> { |
| 51 | + const mode = options.mode ?? ""; |
| 52 | + await denops.cmd(`${mode}unmap ${lhs}`); |
| 53 | +} |
| 54 | + |
| 55 | +export type ReadOptions = { |
| 56 | + mode?: Mode; |
| 57 | +}; |
| 58 | + |
| 59 | +/** |
| 60 | + * Read a mapping and return a `Mapping` instance. |
| 61 | + * |
| 62 | + * Note that prior to Vim 8.2.0491 or Neovim 0.5.0, `script` is |
| 63 | + * not detectable by `maparg` function internally used in this |
| 64 | + * function. |
| 65 | + */ |
| 66 | +export async function read( |
| 67 | + denops: Denops, |
| 68 | + lhs: string, |
| 69 | + options: ReadOptions = {}, |
| 70 | +): Promise<Mapping> { |
| 71 | + const info = await fn.maparg( |
| 72 | + denops, |
| 73 | + lhs, |
| 74 | + options.mode ?? "", |
| 75 | + false, |
| 76 | + true, |
| 77 | + ) as Record<string, unknown>; |
| 78 | + if (Object.keys(info).length === 0) { |
| 79 | + throw new Error(`No mapping found for '${lhs}'`); |
| 80 | + } |
| 81 | + return { |
| 82 | + mode: (info.mode as string).trim() as Mode, |
| 83 | + lhs: info.lhs as string, |
| 84 | + rhs: info.rhs as string, |
| 85 | + noremap: !!info.noremap, |
| 86 | + script: !!info.script, |
| 87 | + buffer: !!info.buffer, |
| 88 | + sid: info.sid as number, |
| 89 | + lnum: info.lnum as number, |
| 90 | + expr: !!info.expr, |
| 91 | + nowait: !!info.nowait, |
| 92 | + silent: !!info.silent, |
| 93 | + }; |
| 94 | +} |
| 95 | + |
| 96 | +export type ListOptions = { |
| 97 | + mode?: Mode; |
| 98 | +}; |
| 99 | + |
| 100 | +/** |
| 101 | + * List mappings which starts from `lhs`. |
| 102 | + */ |
| 103 | +export async function list( |
| 104 | + denops: Denops, |
| 105 | + lhs: string, |
| 106 | + options: ListOptions = {}, |
| 107 | +): Promise<Mapping[]> { |
| 108 | + const mode = options.mode ?? ""; |
| 109 | + const result = await fn.execute(denops, `${mode}map ${lhs}`) as string; |
| 110 | + return result.split(/\r?\n/).filter((v) => v).map(parse); |
| 111 | +} |
0 commit comments