|
| 1 | +import { Denops } from "../deps.ts"; |
| 2 | +import { AutocmdEvent } from "./types.ts"; |
| 3 | + |
| 4 | +type CommonOptions = { |
| 5 | + group?: string; |
| 6 | +}; |
| 7 | + |
| 8 | +export type DefineOptions = CommonOptions & { |
| 9 | + once?: boolean; |
| 10 | + nested?: boolean; |
| 11 | +}; |
| 12 | + |
| 13 | +export type RemoveOptions = CommonOptions; |
| 14 | + |
| 15 | +export type ListOptions = CommonOptions; |
| 16 | + |
| 17 | +export type EmitOptions = CommonOptions & { |
| 18 | + nomodeline?: boolean; |
| 19 | +}; |
| 20 | + |
| 21 | +export async function define( |
| 22 | + denops: Denops, |
| 23 | + event: AutocmdEvent | AutocmdEvent[], |
| 24 | + pat: string | string[], |
| 25 | + cmd: string, |
| 26 | + options: DefineOptions = {}, |
| 27 | +): Promise<void> { |
| 28 | + const expr = buildDefineExpr(event, pat, cmd, options); |
| 29 | + await denops.cmd(expr); |
| 30 | +} |
| 31 | + |
| 32 | +export async function remove( |
| 33 | + denops: Denops, |
| 34 | + event?: "*" | AutocmdEvent | AutocmdEvent[], |
| 35 | + pat?: string | string[], |
| 36 | + options: RemoveOptions = {}, |
| 37 | +): Promise<void> { |
| 38 | + const expr = buildRemoveExpr(event, pat, options); |
| 39 | + await denops.cmd(expr); |
| 40 | +} |
| 41 | + |
| 42 | +export async function list( |
| 43 | + denops: Denops, |
| 44 | + event?: "*" | AutocmdEvent | AutocmdEvent[], |
| 45 | + pat?: string | string[], |
| 46 | + options: ListOptions = {}, |
| 47 | +): Promise<unknown> { |
| 48 | + const terms = ["au"]; |
| 49 | + if (options.group) { |
| 50 | + terms.push(options.group); |
| 51 | + } |
| 52 | + if (event) { |
| 53 | + if (Array.isArray(event)) { |
| 54 | + terms.push(event.join(",")); |
| 55 | + } else { |
| 56 | + terms.push(event); |
| 57 | + } |
| 58 | + if (pat) { |
| 59 | + if (Array.isArray(pat)) { |
| 60 | + terms.push(pat.join(",")); |
| 61 | + } else { |
| 62 | + terms.push(pat); |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + const expr = terms.join(" "); |
| 67 | + return await denops.call("execute", expr); |
| 68 | +} |
| 69 | + |
| 70 | +export async function emit( |
| 71 | + denops: Denops, |
| 72 | + event: AutocmdEvent | AutocmdEvent[], |
| 73 | + fname?: string, |
| 74 | + options: EmitOptions = {}, |
| 75 | +): Promise<unknown> { |
| 76 | + const terms = ["do"]; |
| 77 | + if (options.nomodeline) { |
| 78 | + terms.push("<nomodeline>"); |
| 79 | + } |
| 80 | + if (options.group) { |
| 81 | + terms.push(options.group); |
| 82 | + } |
| 83 | + if (Array.isArray(event)) { |
| 84 | + terms.push(event.join(",")); |
| 85 | + } else { |
| 86 | + terms.push(event); |
| 87 | + } |
| 88 | + if (fname) { |
| 89 | + terms.push(fname); |
| 90 | + } |
| 91 | + const expr = terms.join(" "); |
| 92 | + return await denops.cmd(expr); |
| 93 | +} |
| 94 | + |
| 95 | +export async function emitAll( |
| 96 | + denops: Denops, |
| 97 | + event: AutocmdEvent | AutocmdEvent[], |
| 98 | + fname?: string, |
| 99 | + options: EmitOptions = {}, |
| 100 | +): Promise<unknown> { |
| 101 | + const terms = ["doautoa"]; |
| 102 | + if (options.nomodeline) { |
| 103 | + terms.push("<nomodeline>"); |
| 104 | + } |
| 105 | + if (options.group) { |
| 106 | + terms.push(options.group); |
| 107 | + } |
| 108 | + if (Array.isArray(event)) { |
| 109 | + terms.push(event.join(",")); |
| 110 | + } else { |
| 111 | + terms.push(event); |
| 112 | + } |
| 113 | + if (fname) { |
| 114 | + terms.push(fname); |
| 115 | + } |
| 116 | + const expr = terms.join(" "); |
| 117 | + return await denops.cmd(expr); |
| 118 | +} |
| 119 | + |
| 120 | +export function buildDefineExpr( |
| 121 | + event: AutocmdEvent | AutocmdEvent[], |
| 122 | + pat: string | string[], |
| 123 | + cmd: string, |
| 124 | + options: DefineOptions = {}, |
| 125 | +): string { |
| 126 | + const terms = ["au"]; |
| 127 | + if (options.group) { |
| 128 | + terms.push(options.group); |
| 129 | + } |
| 130 | + if (Array.isArray(event)) { |
| 131 | + terms.push(event.join(",")); |
| 132 | + } else { |
| 133 | + terms.push(event); |
| 134 | + } |
| 135 | + if (Array.isArray(pat)) { |
| 136 | + terms.push(pat.join(",")); |
| 137 | + } else { |
| 138 | + terms.push(pat); |
| 139 | + } |
| 140 | + if (options.once) { |
| 141 | + terms.push("++once"); |
| 142 | + } |
| 143 | + if (options.nested) { |
| 144 | + terms.push("++nested"); |
| 145 | + } |
| 146 | + terms.push(cmd); |
| 147 | + return terms.join(" "); |
| 148 | +} |
| 149 | + |
| 150 | +export function buildRemoveExpr( |
| 151 | + event?: "*" | AutocmdEvent | AutocmdEvent[], |
| 152 | + pat?: string | string[], |
| 153 | + options: RemoveOptions = {}, |
| 154 | +): string { |
| 155 | + const terms = ["au!"]; |
| 156 | + if (options.group) { |
| 157 | + terms.push(options.group); |
| 158 | + } |
| 159 | + if (event) { |
| 160 | + if (Array.isArray(event)) { |
| 161 | + terms.push(event.join(",")); |
| 162 | + } else { |
| 163 | + terms.push(event); |
| 164 | + } |
| 165 | + if (pat) { |
| 166 | + if (Array.isArray(pat)) { |
| 167 | + terms.push(pat.join(",")); |
| 168 | + } else { |
| 169 | + terms.push(pat); |
| 170 | + } |
| 171 | + } |
| 172 | + } |
| 173 | + return terms.join(" "); |
| 174 | +} |
0 commit comments