1
1
import { Denops } from "https://deno.land/x/denops_core@v3.4.1/mod.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
- } ;
2
+ import {
3
+ AutocmdEvent ,
4
+ DefineOptions ,
5
+ EmitOptions ,
6
+ ListOptions ,
7
+ RemoveOptions ,
8
+ } from "./types.ts" ;
9
+ import { buildDefineExpr , buildRemoveExpr } from "./utils.ts" ;
20
10
11
+ /**
12
+ * Define an autocmd
13
+ *
14
+ * ```typescript
15
+ * import { Denops } from "../mod.ts";
16
+ * import * as autocmd from "./mod.ts";
17
+ *
18
+ * export async function main(denops: Denops): Promise<void> {
19
+ * // Define new autocmd for BufEnter
20
+ * await autocmd.define(denops, "BufEnter", "*", "echo 'BufEnter'");
21
+ *
22
+ * // Define new autocmd for BufEnter in 'MyGroup'
23
+ * await autocmd.define(denops, "BufEnter", "*", "echo 'BufEnter'", {
24
+ * group: "MyGroup",
25
+ * });
26
+ *
27
+ * // Define new autocmd for BufEnter with '++once'
28
+ * await autocmd.define(denops, "BufEnter", "*", "echo 'BufEnter'", {
29
+ * once: true,
30
+ * });
31
+ *
32
+ * // Define new autocmd for BufEnter with '++nested'
33
+ * await autocmd.define(denops, "BufEnter", "*", "echo 'BufEnter'", {
34
+ * nested: true,
35
+ * });
36
+ *
37
+ * // Define multiple autocmds
38
+ * await autocmd.define(denops, ["BufEnter", "WinEnter"], "*", "echo 'Enter'");
39
+ * }
40
+ * ```
41
+ */
21
42
export async function define (
22
43
denops : Denops ,
23
44
event : AutocmdEvent | AutocmdEvent [ ] ,
@@ -29,6 +50,30 @@ export async function define(
29
50
await denops . cmd ( expr ) ;
30
51
}
31
52
53
+ /**
54
+ * Remove an autocmd
55
+ *
56
+ * ```typescript
57
+ * import { Denops } from "../mod.ts";
58
+ * import * as autocmd from "./mod.ts";
59
+ *
60
+ * export async function main(denops: Denops): Promise<void> {
61
+ * // Remove BufEnter autocmd
62
+ * await autocmd.remove(denops, "BufEnter", "*");
63
+ *
64
+ * // Remove any autocmd in buffer
65
+ * await autocmd.remove(denops, "*", "<buffer>");
66
+ *
67
+ * // Remove BufEnter autocmd in 'MyGroup'
68
+ * await autocmd.remove(denops, "BufEnter", "*", {
69
+ * group: "MyGroup",
70
+ * });
71
+ *
72
+ * // Remove multiple autocmds
73
+ * await autocmd.remove(denops, ["BufEnter", "WinEnter"], "*");
74
+ * }
75
+ * ```
76
+ */
32
77
export async function remove (
33
78
denops : Denops ,
34
79
event ?: "*" | AutocmdEvent | AutocmdEvent [ ] ,
@@ -39,6 +84,28 @@ export async function remove(
39
84
await denops . cmd ( expr ) ;
40
85
}
41
86
87
+ /**
88
+ * List defined autocmds
89
+ *
90
+ * ```typescript
91
+ * import { Denops } from "../mod.ts";
92
+ * import * as autocmd from "./mod.ts";
93
+ *
94
+ * export async function main(denops: Denops): Promise<void> {
95
+ * // List all autocmd
96
+ * console.log(await autocmd.list(denops));
97
+ *
98
+ * // List all BufEnter autocmd
99
+ * console.log(await autocmd.list(denops, "BufEnter"));
100
+ *
101
+ * // List all BufEnter autocmd in buffer
102
+ * console.log(await autocmd.list(denops, "BufEnter", "<buffer>"));
103
+ *
104
+ * // List multiple autocmds
105
+ * console.log(await autocmd.list(denops, ["BufEnter", "WinEnter"]));
106
+ * }
107
+ * ```
108
+ */
42
109
export async function list (
43
110
denops : Denops ,
44
111
event ?: "*" | AutocmdEvent | AutocmdEvent [ ] ,
@@ -67,6 +134,25 @@ export async function list(
67
134
return await denops . call ( "execute" , expr ) ;
68
135
}
69
136
137
+ /**
138
+ * Emit an autocmd in a buffer
139
+ *
140
+ * ```typescript
141
+ * import { Denops } from "../mod.ts";
142
+ * import * as autocmd from "./mod.ts";
143
+ *
144
+ * export async function main(denops: Denops): Promise<void> {
145
+ * // Emit an autocmd in a current buffer
146
+ * await autocmd.emit(denops, "BufEnter");
147
+ *
148
+ * // Emit multiple autocmds in a current buffer
149
+ * await autocmd.emit(denops, ["BufEnter", "WinEnter"]);
150
+ *
151
+ * // Emit an autocmd in a buffer 'Hello'
152
+ * await autocmd.emit(denops, "BufEnter", "Hello");
153
+ * }
154
+ * ```
155
+ */
70
156
export async function emit (
71
157
denops : Denops ,
72
158
event : AutocmdEvent | AutocmdEvent [ ] ,
@@ -92,6 +178,25 @@ export async function emit(
92
178
return await denops . cmd ( expr ) ;
93
179
}
94
180
181
+ /**
182
+ * Emit an autocmd in all buffers
183
+ *
184
+ * ```typescript
185
+ * import { Denops } from "../mod.ts";
186
+ * import * as autocmd from "./mod.ts";
187
+ *
188
+ * export async function main(denops: Denops): Promise<void> {
189
+ * // Emit an autocmd in all buffers
190
+ * await autocmd.emitAll(denops, "BufEnter");
191
+ *
192
+ * // Emit multiple autocmds in all buffers
193
+ * await autocmd.emitAll(denops, ["BufEnter", "WinEnter"]);
194
+ *
195
+ * // Emit an autocmd in all buffers match with 'Hello'
196
+ * await autocmd.emitAll(denops, "BufEnter", "Hello");
197
+ * }
198
+ * ```
199
+ */
95
200
export async function emitAll (
96
201
denops : Denops ,
97
202
event : AutocmdEvent | AutocmdEvent [ ] ,
@@ -116,59 +221,3 @@ export async function emitAll(
116
221
const expr = terms . join ( " " ) ;
117
222
return await denops . cmd ( expr ) ;
118
223
}
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