|
1 | 1 | import type { Denops } from "https://deno.land/x/denops_core@v4.0.0/mod.ts";
|
| 2 | +import type { BufInfo } from "./types.ts"; |
2 | 3 |
|
3 | 4 | /**
|
4 | 5 | * If the **{buf}** argument is a number, buffer numbers are used.
|
@@ -36,6 +37,18 @@ export type BufNameArg = string | number;
|
36 | 37 | */
|
37 | 38 | export type BufLnumArg = "$" | number;
|
38 | 39 |
|
| 40 | +/** |
| 41 | + * Only the buffers matching the specified criteria are returned. |
| 42 | + */ |
| 43 | +export interface GetBufInfoDictArg { |
| 44 | + /** Include only listed buffers. */ |
| 45 | + buflisted?: boolean; |
| 46 | + /** Include only loaded buffers. */ |
| 47 | + bufloaded?: boolean; |
| 48 | + /** Include only modified buffers. */ |
| 49 | + bufmodified?: boolean; |
| 50 | +} |
| 51 | + |
39 | 52 | /**
|
40 | 53 | * Add a buffer to the buffer list with name **{name}** (must be a
|
41 | 54 | * String).
|
@@ -282,6 +295,106 @@ export async function bufwinnr(
|
282 | 295 | return await denops.call("bufwinnr", buf) as number;
|
283 | 296 | }
|
284 | 297 |
|
| 298 | +/** |
| 299 | + * Get information about buffers as a List of Dictionaries. |
| 300 | + * |
| 301 | + * Without an argument information about all the buffers is |
| 302 | + * returned. |
| 303 | + * |
| 304 | + * When the argument is a `Dictionary` only the buffers matching |
| 305 | + * the specified criteria are returned. The following keys can |
| 306 | + * be specified in **{dict}**: |
| 307 | + * buflisted include only listed buffers. |
| 308 | + * bufloaded include only loaded buffers. |
| 309 | + * bufmodified include only modified buffers. |
| 310 | + * |
| 311 | + * Otherwise, **{buf}** specifies a particular buffer to return |
| 312 | + * information for. For the use of **{buf}**, see `bufname()` |
| 313 | + * above. If the buffer is found the returned List has one item. |
| 314 | + * Otherwise the result is an empty list. |
| 315 | + * |
| 316 | + * Each returned List item is a dictionary with the following |
| 317 | + * entries: |
| 318 | + * bufnr Buffer number. |
| 319 | + * changed TRUE if the buffer is modified. |
| 320 | + * changedtick Number of changes made to the buffer. |
| 321 | + * hidden TRUE if the buffer is hidden. |
| 322 | + * lastused Timestamp in seconds, like |
| 323 | + * `localtime()`, when the buffer was |
| 324 | + * last used. |
| 325 | + * *only with the `+viminfo` feature* |
| 326 | + * listed TRUE if the buffer is listed. |
| 327 | + * lnum Line number used for the buffer when |
| 328 | + * opened in the current window. |
| 329 | + * Only valid if the buffer has been |
| 330 | + * displayed in the window in the past. |
| 331 | + * If you want the line number of the |
| 332 | + * last known cursor position in a given |
| 333 | + * window, use `line()`: |
| 334 | + * |
| 335 | + * :echo line('.', {winid}) |
| 336 | + * |
| 337 | + * linecount Number of lines in the buffer (only |
| 338 | + * valid when loaded) |
| 339 | + * loaded TRUE if the buffer is loaded. |
| 340 | + * name Full path to the file in the buffer. |
| 341 | + * signs List of signs placed in the buffer. |
| 342 | + * Each list item is a dictionary with |
| 343 | + * the following fields: |
| 344 | + * id sign identifier |
| 345 | + * lnum line number |
| 346 | + * name sign name |
| 347 | + * variables A reference to the dictionary with |
| 348 | + * buffer-local variables. |
| 349 | + * windows List of `window-ID`s that display this |
| 350 | + * buffer |
| 351 | + * popups List of popup `window-ID`s that |
| 352 | + * display this buffer |
| 353 | + * |
| 354 | + * Examples: |
| 355 | + * |
| 356 | + * for buf in getbufinfo() |
| 357 | + * echo buf.name |
| 358 | + * endfor |
| 359 | + * for buf in getbufinfo({'buflisted':1}) |
| 360 | + * if buf.changed |
| 361 | + * .... |
| 362 | + * endif |
| 363 | + * endfor |
| 364 | + * |
| 365 | + * To get buffer-local options use: |
| 366 | + * |
| 367 | + * getbufvar({bufnr}, '&option_name') |
| 368 | + * |
| 369 | + * Can also be used as a `method`: |
| 370 | + * |
| 371 | + * GetBufnr()->getbufinfo() |
| 372 | + */ |
| 373 | +export function getbufinfo( |
| 374 | + denops: Denops, |
| 375 | + buf?: BufNameArg, |
| 376 | +): Promise<BufInfo[]>; |
| 377 | +export function getbufinfo( |
| 378 | + denops: Denops, |
| 379 | + dict?: GetBufInfoDictArg, |
| 380 | +): Promise<BufInfo[]>; |
| 381 | +export async function getbufinfo( |
| 382 | + denops: Denops, |
| 383 | + ...args: unknown[] |
| 384 | +): Promise<BufInfo[]> { |
| 385 | + const bufinfos = await denops.call("getbufinfo", ...args) as Record< |
| 386 | + keyof BufInfo, |
| 387 | + unknown |
| 388 | + >[]; |
| 389 | + return bufinfos.map((bufinfo) => ({ |
| 390 | + ...bufinfo, |
| 391 | + changed: !!bufinfo.changed, |
| 392 | + hidden: !!bufinfo.hidden, |
| 393 | + listed: !!bufinfo.listed, |
| 394 | + loaded: !!bufinfo.loaded, |
| 395 | + } as unknown as BufInfo)); |
| 396 | +} |
| 397 | + |
285 | 398 | /**
|
286 | 399 | * Return a `List` with the lines starting from **{lnum}** to **{end}**
|
287 | 400 | * (inclusive) in the buffer **{buf}**. If **{end}** is omitted, a
|
|
0 commit comments