|
| 1 | +import { Denops } from "./deps.ts"; |
| 2 | +import { execute } from "./execute.ts"; |
| 3 | + |
| 4 | +export async function autocmd( |
| 5 | + denops: Denops, |
| 6 | + group: string, |
| 7 | + main: (helper: AutocmdHelper) => void, |
| 8 | +): Promise<void> { |
| 9 | + const commands: string[] = []; |
| 10 | + const helper = new AutocmdHelper(commands); |
| 11 | + main(helper); |
| 12 | + if (group) { |
| 13 | + commands.unshift(`aug ${group}`); |
| 14 | + commands.push("aug END"); |
| 15 | + } |
| 16 | + await execute(denops, commands); |
| 17 | +} |
| 18 | + |
| 19 | +export class AutocmdHelper { |
| 20 | + #commands: string[]; |
| 21 | + |
| 22 | + constructor(commands: string[]) { |
| 23 | + this.#commands = commands; |
| 24 | + } |
| 25 | + |
| 26 | + define( |
| 27 | + event: AutocmdEvent | AutocmdEvent[], |
| 28 | + pat: string | string[], |
| 29 | + cmd: string, |
| 30 | + options: AutocmdOptions = {}, |
| 31 | + ): void { |
| 32 | + const terms = ["au"]; |
| 33 | + if (Array.isArray(event)) { |
| 34 | + terms.push(event.join(",")); |
| 35 | + } else { |
| 36 | + terms.push(event); |
| 37 | + } |
| 38 | + if (Array.isArray(pat)) { |
| 39 | + terms.push(pat.join(",")); |
| 40 | + } else { |
| 41 | + terms.push(pat); |
| 42 | + } |
| 43 | + if (options.once) { |
| 44 | + terms.push("++once"); |
| 45 | + } |
| 46 | + if (options.nested) { |
| 47 | + terms.push("++nested"); |
| 48 | + } |
| 49 | + terms.push(cmd); |
| 50 | + this.#commands.push(terms.join(" ")); |
| 51 | + } |
| 52 | + |
| 53 | + remove( |
| 54 | + event?: AutocmdEvent | AutocmdEvent[], |
| 55 | + pat?: string | string[], |
| 56 | + options: AutocmdOptions = {}, |
| 57 | + ): void { |
| 58 | + const terms = ["au!"]; |
| 59 | + if (event) { |
| 60 | + if (Array.isArray(event)) { |
| 61 | + terms.push(event.join(",")); |
| 62 | + } else { |
| 63 | + terms.push(event); |
| 64 | + } |
| 65 | + if (pat) { |
| 66 | + if (Array.isArray(pat)) { |
| 67 | + terms.push(pat.join(",")); |
| 68 | + } else { |
| 69 | + terms.push(pat); |
| 70 | + } |
| 71 | + if (options.once) { |
| 72 | + terms.push("++once"); |
| 73 | + } |
| 74 | + if (options.nested) { |
| 75 | + terms.push("++nested"); |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + this.#commands.push(terms.join(" ")); |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +export interface AutocmdOptions { |
| 84 | + once?: boolean; |
| 85 | + nested?: boolean; |
| 86 | +} |
| 87 | + |
| 88 | +export type AutocmdEvent = |
| 89 | + | "*" |
| 90 | + | "BufAdd" |
| 91 | + | "BufDelete" |
| 92 | + | "BufEnter" |
| 93 | + | "BufFilePost" |
| 94 | + | "BufFilePre" |
| 95 | + | "BufHidden" |
| 96 | + | "BufLeave" |
| 97 | + | "BufModifiedSet" |
| 98 | + | "BufNew" |
| 99 | + | "BufNewFile" |
| 100 | + | "BufRead" |
| 101 | + | "BufReadPost" |
| 102 | + | "BufReadCmd" |
| 103 | + | "BufReadPre" |
| 104 | + | "BufUnload" |
| 105 | + | "BufWinEnter" |
| 106 | + | "BufWinLeave" |
| 107 | + | "BufWipeout" |
| 108 | + | "BufWrite" |
| 109 | + | "BufWritePre" |
| 110 | + | "BufWriteCmd" |
| 111 | + | "BufWritePost" |
| 112 | + | "ChanInfo" |
| 113 | + | "ChanOpen" |
| 114 | + | "CmdUndefined" |
| 115 | + | "CmdlineChanged" |
| 116 | + | "CmdlineEnter" |
| 117 | + | "CmdlineLeave" |
| 118 | + | "CmdwinEnter" |
| 119 | + | "CmdwinLeave" |
| 120 | + | "ColorScheme" |
| 121 | + | "ColorSchemePre" |
| 122 | + | "CompleteChanged" |
| 123 | + | "CompleteDonePre" |
| 124 | + | "CompleteDone" |
| 125 | + | "CursorHold" |
| 126 | + | "CursorHoldI" |
| 127 | + | "CursorMoved" |
| 128 | + | "CursorMovedI" |
| 129 | + | "DiffUpdated" |
| 130 | + | "DirChanged" |
| 131 | + | "FileAppendCmd" |
| 132 | + | "FileAppendPost" |
| 133 | + | "FileAppendPre" |
| 134 | + | "FileChangedRO" |
| 135 | + | "ExitPre" |
| 136 | + | "FileChangedShell" |
| 137 | + | "FileChangedShellPost" |
| 138 | + | "FileReadCmd" |
| 139 | + | "FileReadPost" |
| 140 | + | "FileReadPre" |
| 141 | + | "FileType" |
| 142 | + | "FileWriteCmd" |
| 143 | + | "FileWritePost" |
| 144 | + | "FileWritePre" |
| 145 | + | "FilterReadPost" |
| 146 | + | "FilterReadPre" |
| 147 | + | "FilterWritePost" |
| 148 | + | "FilterWritePre" |
| 149 | + | "FocusGained" |
| 150 | + | "FocusLost" |
| 151 | + | "FuncUndefined" |
| 152 | + | "UIEnter" |
| 153 | + | "UILeave" |
| 154 | + | "InsertChange" |
| 155 | + | "InsertCharPre" |
| 156 | + | "TextYankPost" |
| 157 | + | "InsertEnter" |
| 158 | + | "InsertLeavePre" |
| 159 | + | "InsertLeave" |
| 160 | + | "MenuPopup" |
| 161 | + | "OptionSet" |
| 162 | + | "QuickFixCmdPre" |
| 163 | + | "QuickFixCmdPost" |
| 164 | + | "QuitPre" |
| 165 | + | "RemoteReply" |
| 166 | + | "SessionLoadPost" |
| 167 | + | "ShellCmdPost" |
| 168 | + | "Signal" |
| 169 | + | "ShellFilterPost" |
| 170 | + | "SourcePre" |
| 171 | + | "SourcePost" |
| 172 | + | "SourceCmd" |
| 173 | + | "SpellFileMissing" |
| 174 | + | "StdinReadPost" |
| 175 | + | "StdinReadPre" |
| 176 | + | "SwapExists" |
| 177 | + | "Syntax" |
| 178 | + | "TabEnter" |
| 179 | + | "TabLeave" |
| 180 | + | "TabNew" |
| 181 | + | "TabNewEntered" |
| 182 | + | "TabClosed" |
| 183 | + | "TermOpen" |
| 184 | + | "TermEnter" |
| 185 | + | "TermLeave" |
| 186 | + | "TermClose" |
| 187 | + | "TermResponse" |
| 188 | + | "TextChanged" |
| 189 | + | "TextChangedI" |
| 190 | + | "TextChangedP" |
| 191 | + | "User" |
| 192 | + | "UserGettingBored" |
| 193 | + | "VimEnter" |
| 194 | + | "VimLeave" |
| 195 | + | "VimLeavePre" |
| 196 | + | "VimResized" |
| 197 | + | "VimResume" |
| 198 | + | "VimSuspend" |
| 199 | + | "WinClosed" |
| 200 | + | "WinEnter" |
| 201 | + | "WinLeave" |
| 202 | + | "WinNew" |
| 203 | + | "WinScrolled"; |
0 commit comments