Skip to content

Commit 99dc457

Browse files
committed
👍 Add buffer module
1 parent 8a7be83 commit 99dc457

File tree

11 files changed

+1112
-0
lines changed

11 files changed

+1112
-0
lines changed

denops_std/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ for more details.
5959
| [`anonymous`](./anonymous) | A module to provide anonymous function |
6060
| [`autocmd`](./autocmd) | A module to provide helper functions to manage `autocmd` |
6161
| [`batch`](./batch) | A module to provide wrapper functions of `denops.batch()` |
62+
| [`buffer`](./buffer) | A module to provide helper functions to manage buffers |
6263
| [`bufname`](./bufname) | A module to provide helper functions to manage Vim's buffer name |
6364
| [`function`](./function) | A module to provide functions of Vim and Neovim native functions |
6465
| [`helper`](./helper) | A module to provide helper functions |

denops_std/buffer/README.md

Lines changed: 227 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,227 @@
1+
# buffer
2+
3+
`buffer` is a module to provide Vim buffer utility functions.
4+
5+
- [API documentation](https://doc.deno.land/https/deno.land/x/denops_std/buffer/mod.ts)
6+
7+
## Usage
8+
9+
### open
10+
11+
Use `open()` to open a `bufname` buffer with given options on the current window
12+
like:
13+
14+
```typescript
15+
import { Denops } from "../mod.ts";
16+
import { open } from "../buffer/mod.ts";
17+
18+
export async function main(denops: Denops): Promise<void> {
19+
// Open `README.md`
20+
// Same as `:edit README.md`
21+
await open(denops, "README.md");
22+
23+
// Open `LICENSE` with given options
24+
// Same as `:keepjumps keepalt edit ++enc=sjis ++ff=dos LICENSE`
25+
await open(denops, "LICENSE", {
26+
mods: "keepjumps keepalt",
27+
cmdarg: "++enc=sjis ++ff=dos",
28+
});
29+
}
30+
```
31+
32+
Use `split`, `vsplit`, `tabedit`, `pedit`, or whatever before if you'd like to
33+
open a buffer with non current window like:
34+
35+
```typescript
36+
import { Denops } from "../mod.ts";
37+
import { batch } from "../batch/mod.ts";
38+
import { open } from "../buffer/mod.ts";
39+
40+
export async function main(denops: Denops): Promise<void> {
41+
await batch(denops, async (denops) => {
42+
await denops.cmd("split");
43+
await open(denops, "README.md");
44+
});
45+
}
46+
```
47+
48+
See [`batch`](../batch/README.md) documentation for `batch()` part.
49+
50+
### reload
51+
52+
Use `reload()` to reload the content of the `bufnr` buffer like:
53+
54+
```typescript
55+
import { Denops } from "../mod.ts";
56+
import * as fn from "../function/mod.ts";
57+
import { open, reload } from "../buffer/mod.ts";
58+
59+
export async function main(denops: Denops): Promise<void> {
60+
await open(denops, "README.md");
61+
const bufnr = await fn.bufnr(denops) as number;
62+
// ...
63+
// Reload the content of the `bufnr` buffer.
64+
await reload(denops, bufnr);
65+
}
66+
```
67+
68+
It may temporary change a current buffer or a current window to properly reload
69+
the content of the `bufnr` buffer.
70+
71+
### replace
72+
73+
Use `replace()` to replace the content of the `bufnr` buffer like:
74+
75+
```typescript
76+
import { Denops } from "../mod.ts";
77+
import * as fn from "../function/mod.ts";
78+
import { open, replace } from "../buffer/mod.ts";
79+
80+
export async function main(denops: Denops): Promise<void> {
81+
await open(denops, "README.md");
82+
const bufnr = await fn.bufnr(denops) as number;
83+
// Set the content of the `bufnr` buffer
84+
await replace(denops, bufnr, ["Hello", "World"]);
85+
}
86+
```
87+
88+
It temporary change `modified`, `modifiable`, and `foldmethod` options to
89+
replace the content of the `buffer` buffer without unmodifiable error or so on.
90+
91+
### assign
92+
93+
Use `assign()` to assign raw binary content into the `bufnr` buffer like:
94+
95+
```typescript
96+
import { Denops } from "../mod.ts";
97+
import * as fn from "../function/mod.ts";
98+
import { assign, open } from "../buffer/mod.ts";
99+
100+
export async function main(denops: Denops): Promise<void> {
101+
await open(denops, "README.md");
102+
const bufnr = await fn.bufnr(denops) as number;
103+
const content = await Deno.readFile("README.md");
104+
await assign(denops, bufnr, content);
105+
}
106+
```
107+
108+
It follows Vim's rule to find a corresponding `fileformat` and `fileencoding` to
109+
decode the `content` if the one is not given by `options`.
110+
111+
Use `preprocessor` to modify the content prior to apply like:
112+
113+
```typescript
114+
import { Denops } from "../mod.ts";
115+
import * as fn from "../function/mod.ts";
116+
import { assign, open } from "../buffer/mod.ts";
117+
118+
export async function main(denops: Denops): Promise<void> {
119+
await open(denops, "README.md");
120+
const bufnr = await fn.bufnr(denops) as number;
121+
const content = await Deno.readFile("README.md");
122+
// A preprocessor that replace all non words to "-"
123+
const preprocessor = (repl: string[]): string[] => {
124+
return repl.map((line) => line.replace(/\W/g, "-"));
125+
};
126+
await assign(denops, bufnr, content, { preprocessor });
127+
}
128+
```
129+
130+
### concrete
131+
132+
Vim will discard the content of a non-file buffer when `:edit` is invoked. Use
133+
`concrete()` to concrete the content of such buffer to prevent this discard
134+
like:
135+
136+
```typescript
137+
import { Denops } from "../mod.ts";
138+
import * as fn from "../function/mod.ts";
139+
import { concrete, open, replace } from "../buffer/mod.ts";
140+
141+
export async function main(denops: Denops): Promise<void> {
142+
await open(denops, "README.md");
143+
const bufnr = await fn.bufnr(denops) as number;
144+
await fn.setbufvar(denops, bufnr, "&buftype", "nofile");
145+
await replace(denops, bufnr, ["Hello", "World"]);
146+
await concrete(denops, bufnr);
147+
}
148+
```
149+
150+
Then `:edit` on the buffer won't discard the content.
151+
152+
### ensure
153+
154+
Use `ensure()` to ensure that the code is called on the `bufnr` buffer like:
155+
156+
```typescript
157+
import { Denops } from "../mod.ts";
158+
import * as option from "../option/mod.ts";
159+
import * as fn from "../function/mod.ts";
160+
import { ensure, open } from "../buffer/mod.ts";
161+
162+
export async function main(denops: Denops): Promise<void> {
163+
await open(denops, "README.md");
164+
const bufnr = await fn.bufnr(denops) as number;
165+
// ...
166+
await ensure(denops, bufnr, async () => {
167+
await option.buftype.set(denops, "nofile");
168+
await option.swapfile.set(denops, false);
169+
await fn.setline(denops, 1, ["Hello", "World"]);
170+
});
171+
}
172+
```
173+
174+
Note that it's better to use `setbufvar` or whatever instead. It's mainly
175+
designed to define mappings that is not possible from outside of the buffer.
176+
177+
### modifiable
178+
179+
Use `modifiable()` to ensure that the `bufnr` buffer is modifiable like:
180+
181+
```typescript
182+
import { Denops } from "../mod.ts";
183+
import * as fn from "../function/mod.ts";
184+
import { modifiable, open } from "../buffer/mod.ts";
185+
186+
export async function main(denops: Denops): Promise<void> {
187+
await open(denops, "README.md");
188+
const bufnr = await fn.bufnr(denops) as number;
189+
// ...
190+
await modifiable(denops, bufnr, async () => {
191+
await fn.setline(denops, 1, ["Hello", "World"]);
192+
});
193+
}
194+
```
195+
196+
### decorate
197+
198+
Use `decorate()` to decorate the `bufnr` buffer with `decorations` like:
199+
200+
```typescript
201+
import { Denops } from "../mod.ts";
202+
import * as fn from "../function/mod.ts";
203+
import { decorate, open } from "../buffer/mod.ts";
204+
205+
export async function main(denops: Denops): Promise<void> {
206+
await open(denops, "README.md");
207+
const bufnr = await fn.bufnr(denops) as number;
208+
// ...
209+
await decorate(denops, bufnr, [
210+
{
211+
line: 1,
212+
column: 1,
213+
length: 10,
214+
highlight: "Special",
215+
},
216+
{
217+
line: 2,
218+
column: 2,
219+
length: 3,
220+
highlight: "Comment",
221+
},
222+
]);
223+
}
224+
```
225+
226+
It uses `prop_add` in Vim and `nvim_buf_add_highlight` in Neovim to decorate the
227+
buffer.

0 commit comments

Comments
 (0)