Skip to content

Commit cf25ec0

Browse files
authored
Merge pull request #177 from ryota2357/update-typedef
Update type definition for `function`
2 parents 3a2d827 + 99065f8 commit cf25ec0

File tree

8 files changed

+375
-155
lines changed

8 files changed

+375
-155
lines changed

denops_std/function/buffer.ts

Lines changed: 130 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
import type { Denops } from "https://deno.land/x/denops_core@v4.0.0/mod.ts";
22

33
/**
4-
* Add a buffer to the buffer list with {name}.
4+
* Add a buffer to the buffer list with name {name} (must be a
5+
* String).
56
* If a buffer for file {name} already exists, return that buffer
67
* number. Otherwise return the buffer number of the newly
78
* created buffer. When {name} is an empty string then a new
89
* buffer is always created.
9-
* The buffer will not have' 'buflisted' set.
10+
* The buffer will not have 'buflisted' set and not be loaded
11+
* yet. To add some text to the buffer use this:
12+
* let bufnr = bufadd('someName')
13+
* call bufload(bufnr)
14+
* call setbufline(bufnr, 1, ['some', 'text'])
15+
* Returns 0 on error.
16+
* Can also be used as a |method|:
17+
* let bufnr = 'somename'->bufadd()
1018
*/
1119
export async function bufadd(
1220
denops: Denops,
@@ -18,11 +26,11 @@ export async function bufadd(
1826

1927
/**
2028
* The result is a Number, which is |TRUE| if a buffer called
21-
* {expr} exists.
22-
* If the {expr} argument is a number, buffer numbers are used.
23-
* Number zero is the alternate buffer for the current window.
29+
* {buf} exists.
30+
* If the {buf} argument is a number, buffer numbers are used.
2431
*
25-
* If the {expr} argument is a string it must match a buffer name
32+
* Number zero is the alternate buffer for the current window.
33+
* If the {buf} argument is a string it must match a buffer name
2634
* exactly. The name can be:
2735
* - Relative to the current directory.
2836
* - A full path.
@@ -37,64 +45,80 @@ export async function bufadd(
3745
* for MS-Windows 8.3 names in the form "c:\DOCUME~1"
3846
* Use "bufexists(0)" to test for the existence of an alternate
3947
* file name.
48+
*
49+
* Can also be used as a |method|:
50+
* let exists = 'somename'->bufexists()
51+
*
52+
* Obsolete name: buffer_exists().
4053
*/
4154
export async function bufexists(
4255
denops: Denops,
43-
expr: string | number,
56+
buf: string | number,
4457
): Promise<boolean> {
45-
const result = await denops.call("bufexists", expr) as number;
58+
const result = await denops.call("bufexists", buf) as number;
4659
return !!result;
4760
}
4861

4962
/**
5063
* The result is a Number, which is |TRUE| if a buffer called
51-
* {expr} exists and is listed (has the 'buflisted' option set).
52-
* The {expr} argument is used like with |bufexists()|.
64+
* {buf} exists and is listed (has the 'buflisted' option set).
65+
* The {buf} argument is used like with |bufexists()|.
66+
*
67+
* Can also be used as a |method|:
68+
* let listed = 'somename'->buflisted()
5369
*/
5470
export async function buflisted(
5571
denops: Denops,
56-
expr: string | number,
72+
buf: string | number,
5773
): Promise<boolean> {
58-
const result = await denops.call("buflisted", expr) as number;
74+
const result = await denops.call("buflisted", buf) as number;
5975
return !!result;
6076
}
6177

6278
/**
63-
* Ensure the buffer {expr} is loaded. When the buffer name
79+
* Ensure the buffer {buf} is loaded. When the buffer name
6480
* refers to an existing file then the file is read. Otherwise
6581
* the buffer will be empty. If the buffer was already loaded
66-
* then there is no change.
82+
* then there is no change. If the buffer is not related to a
83+
* file the no file is read (e.g., when 'buftype' is "nofile").
6784
* If there is an existing swap file for the file of the buffer,
6885
* there will be no dialog, the buffer will be loaded anyway.
69-
* The {expr} argument is used like with |bufexists()|.
86+
* The {buf} argument is used like with |bufexists()|.
87+
*
88+
* Can also be used as a |method|:
89+
* eval 'somename'->bufload()
7090
*/
7191
export async function bufload(
7292
denops: Denops,
73-
expr: string | number,
93+
buf: string | number,
7494
): Promise<void> {
75-
await denops.call("bufload", expr);
95+
await denops.call("bufload", buf);
7696
}
7797

7898
/**
7999
* The result is a Number, which is |TRUE| if a buffer called
80-
* {expr} exists and is loaded (shown in a window or hidden).
81-
* The {expr} argument is used like with |bufexists()|.
100+
* {buf} exists and is loaded (shown in a window or hidden).
101+
* The {buf} argument is used like with |bufexists()|.
102+
*
103+
* Can also be used as a |method|:
104+
* let loaded = 'somename'->bufloaded()
82105
*/
83106
export async function bufloaded(
84107
denops: Denops,
85-
expr: string | number,
108+
buf: string | number,
86109
): Promise<boolean> {
87-
const result = await denops.call("bufloaded", expr) as number;
110+
const result = await denops.call("bufloaded", buf) as number;
88111
return !!result;
89112
}
90113

91114
/**
92-
* The result is the name of a buffer, as it is displayed by the
93-
* ":ls" command.
94-
* If {expr} is omitted the current buffer is used.
95-
* If {expr} is a Number, that buffer number's name is given.
115+
* The result is the name of a buffer. Mostly as it is displayed
116+
* by the `:ls` command, but not using special names such as
117+
* "[No Name]".
118+
* If {buf} is omitted the current buffer is used.
119+
* If {buf} is a Number, that buffer number's name is given.
96120
* Number zero is the alternate buffer for the current window.
97-
* If {expr} is a String, it is used as a |file-pattern| to match
121+
* If {buf} is a String, it is used as a |file-pattern| to match
98122
* with the buffer names. This is always done like 'magic' is
99123
* set and 'cpoptions' is empty. When there is more than one
100124
* match an empty string is returned.
@@ -107,87 +131,106 @@ export async function bufloaded(
107131
* Listed buffers are found first. If there is a single match
108132
* with a listed buffer, that one is returned. Next unlisted
109133
* buffers are searched for.
110-
* If the {expr} is a String, but you want to use it as a buffer
111-
* number, force it to be a Number by adding zero to it: >
112-
* :echo bufname("3" + 0)
134+
* If the {buf} is a String, but you want to use it as a buffer
135+
* number, force it to be a Number by adding zero to it:
136+
* :echo bufname("3" + 0)
137+
* Can also be used as a |method|:
138+
* echo bufnr->bufname()
139+
*
113140
* If the buffer doesn't exist, or doesn't have a name, an empty
114141
* string is returned.
115-
* bufname("#") alternate buffer name
116-
* bufname(3) name of buffer 3
117-
* bufname("%") name of current buffer
118-
* bufname("file2") name of buffer where "file2" matches.
142+
* bufname("#") alternate buffer name
143+
* bufname(3) name of buffer 3
144+
* bufname("%") name of current buffer
145+
* bufname("file2") name of buffer where "file2" matches.
146+
*
147+
* Obsolete name: buffer_name().
119148
*/
120149
export async function bufname(
121150
denops: Denops,
122-
expr?: string | number,
151+
buf?: string | number,
123152
): Promise<string> {
124-
return await denops.call("bufname", expr) as string;
153+
return await denops.call("bufname", buf) as string;
125154
}
126155

127156
/**
128157
* The result is the number of a buffer, as it is displayed by
129-
* the ":ls" command. For the use of {expr}, see |bufname()|
158+
* the `:ls` command. For the use of {buf}, see |bufname()|
130159
* above.
131160
* If the buffer doesn't exist, -1 is returned. Or, if the
132-
* {create} argument is present and not zero, a new, unlisted,
133-
* buffer is created and its number is returned.
161+
* {create} argument is present and TRUE, a new, unlisted,
162+
* buffer is created and its number is returned. Example:
163+
* let newbuf = bufnr('Scratch001', 1)
164+
* Using an empty name uses the current buffer. To create a new
165+
* buffer with an empty name use |bufadd()|.
134166
* bufnr("$") is the last buffer:
135167
* :let last_buffer = bufnr("$")
136168
* The result is a Number, which is the highest buffer number
137169
* of existing buffers. Note that not all buffers with a smaller
138170
* number necessarily exist, because ":bwipeout" may have removed
139171
* them. Use bufexists() to test for the existence of a buffer.
172+
*
173+
* Can also be used as a |method|:
174+
* echo bufref->bufnr()
175+
*
176+
* Obsolete name: buffer_number().
177+
* Obsolete name for bufnr("$"): last_buffer_nr().
140178
*/
141179
export async function bufnr(
142180
denops: Denops,
143-
expr?: string | number,
181+
buf?: string | number,
144182
create?: boolean,
145183
): Promise<number> {
146-
return await denops.call("bufnr", expr, create) as number;
184+
return await denops.call("bufnr", buf, create) as number;
147185
}
148186

149187
/**
150188
* The result is a Number, which is the |window-ID| of the first
151-
* window associated with buffer {expr}. For the use of {expr},
152-
* see |bufname()| above. If buffer {expr} doesn't exist or
153-
* there is no such window, -1 is returned. Example: >
189+
* window associated with buffer {buf}. For the use of {buf},
190+
* see |bufname()| above. If buffer {buf} doesn't exist or
191+
* there is no such window, -1 is returned. Example:
154192
*
155-
* echo "A window containing buffer 1 is " . (bufwinid(1))
193+
* echo "A window containing buffer 1 is " .. (bufwinid(1))
156194
*
157195
* Only deals with the current tab page.
196+
*
197+
* Can also be used as a |method|:
198+
* FindBuffer()->bufwinid()
158199
*/
159200
export async function bufwinid(
160201
denops: Denops,
161-
expr: string | number,
202+
buf: string | number,
162203
): Promise<number> {
163-
return await denops.call("bufwinid", expr) as number;
204+
return await denops.call("bufwinid", buf) as number;
164205
}
165206

166207
/**
167-
* The result is a Number, which is the number of the first
168-
* window associated with buffer {expr}. For the use of {expr},
169-
* see |bufname()| above. If buffer {expr} doesn't exist or
170-
* there is no such window, -1 is returned. Example: >
208+
* Like |bufwinid()| but return the window number instead of the
209+
* |window-ID|.
210+
* If buffer {buf} doesn't exist or there is no such window, -1
211+
* is returned. Example:
171212
*
172-
* echo "A window containing buffer 1 is " . (bufwinnr(1))
213+
* echo "A window containing buffer 1 is " .. (bufwinnr(1))
173214
*
174215
* The number can be used with |CTRL-W_w| and ":wincmd w"
175216
* |:wincmd|.
176-
* Only deals with the current tab page.
217+
*
218+
* Can also be used as a |method|:
219+
* FindBuffer()->bufwinnr()
177220
*/
178221
export async function bufwinnr(
179222
denops: Denops,
180-
expr: string | number,
223+
buf: string | number,
181224
): Promise<number> {
182-
return await denops.call("bufwinnr", expr) as number;
225+
return await denops.call("bufwinnr", buf) as number;
183226
}
184227

185228
/**
186229
* Return a |List| with the lines starting from {lnum} to {end}
187-
* (inclusive) in the buffer {expr}. If {end} is omitted, a
230+
* (inclusive) in the buffer {buf}. If {end} is omitted, a
188231
* |List| with only the line {lnum} is returned.
189232
*
190-
* For the use of {expr}, see |bufname()| above.
233+
* For the use of {buf}, see |bufname()| above.
191234
*
192235
* For {lnum} and {end} "$" can be used for the last line of the
193236
* buffer. Otherwise a number must be used.
@@ -203,37 +246,56 @@ export async function bufwinnr(
203246
* This function works only for loaded buffers. For unloaded and
204247
* non-existing buffers, an empty |List| is returned.
205248
*
206-
* Example: >
249+
* Example:
207250
* :let lines = getbufline(bufnr("myfile"), 1, "$")
251+
*
252+
* Can also be used as a |method|:
253+
* GetBufnr()->getbufline(lnum)
208254
*/
209255
export async function getbufline(
210256
denops: Denops,
211-
expr: string | number,
257+
buf: string | number,
212258
lnum: string | number,
213259
end?: string | number,
214260
): Promise<string[]> {
215-
return await denops.call("getbufline", expr, lnum, end) as string[];
261+
return await denops.call("getbufline", buf, lnum, end) as string[];
216262
}
217263

218264
/**
219-
* Set line {lnum} to {text} in buffer {expr}. To insert
220-
* lines use |append()|.
265+
* Set line {lnum} to {text} in buffer {buf}. This works like
266+
* |setline()| for the specified buffer.
267+
*
268+
* This function works only for loaded buffers. First call
269+
* |bufload()| if needed.
221270
*
222-
* For the use of {expr}, see |bufname()| above.
271+
* To insert lines use |appendbufline()|.
272+
* Any text properties in {lnum} are cleared.
273+
* {text} can be a string to set one line, or a list of strings
274+
* to set multiple lines. If the list extends below the last
275+
* line then those lines are added.
276+
*
277+
* For the use of {buf}, see |bufname()| above.
223278
*
224279
* {lnum} is used like with |setline()|.
225-
* This works like |setline()| for the specified buffer.
226-
* On success 0 is returned, on failure 1 is returned.
280+
* Use "$" to refer to the last line in buffer {buf}.
281+
* When {lnum} is just below the last line the {text} will be
282+
* added below the last line.
283+
*
284+
* When {buf} is not a valid buffer, the buffer is not loaded or
285+
* {lnum} is not valid then 1 is returned. In |Vim9| script an
286+
* error is given.
287+
* On success 0 is returned.
227288
*
228-
* If {expr} is not a valid buffer or {lnum} is not valid, an
229-
* error message is given.
289+
* Can also be used as a |method|, the base is passed as the
290+
* third argument:
291+
* GetText()->setbufline(buf, lnum)
230292
*/
231293
export async function setbufline(
232294
denops: Denops,
233-
expr: string | number,
295+
buf: string | number,
234296
lnum: string | number,
235297
text: string | string[],
236298
): Promise<boolean> {
237-
const result = await denops.call("setbufline", expr, lnum, text) as number;
299+
const result = await denops.call("setbufline", buf, lnum, text) as number;
238300
return !!result;
239301
}

0 commit comments

Comments
 (0)