|
1 | | -(local {: autoload} (require :conjure.nfnl.module)) |
2 | | -(local nvim (autoload :conjure.aniseed.nvim)) |
| 1 | +(local {: autoload : define} (require :conjure.nfnl.module)) |
3 | 2 | (local core (autoload :conjure.nfnl.core)) |
4 | 3 | (local text (autoload :conjure.text)) |
5 | 4 |
|
6 | | -(fn unlist [buf] |
| 5 | +(local M (define :conjure.buffer)) |
| 6 | + |
| 7 | +(fn M.unlist [buf] |
7 | 8 | "The buflisted attribute is reset when a new window is opened. Since the |
8 | 9 | buffer upsert is decoupled from the window we have to run this whenever we |
9 | 10 | split the buffer into some new window." |
10 | | - (nvim.buf_set_option buf :buflisted false)) |
| 11 | + (vim.api.nvim_buf_set_option buf :buflisted false)) |
11 | 12 |
|
12 | | -(fn resolve [buf-name] |
13 | | - (nvim.buf_get_name (nvim.fn.bufnr buf-name))) |
| 13 | +(fn M.resolve [buf-name] |
| 14 | + (vim.api.nvim_buf_get_name (vim.fn.bufnr buf-name))) |
14 | 15 |
|
15 | | -(fn upsert-hidden [buf-name new-buf-fn] |
16 | | - (let [(ok? buf) (pcall nvim.fn.bufnr buf-name) |
17 | | - loaded? (and ok? (nvim.buf_is_loaded buf))] |
| 16 | +(fn M.upsert-hidden [buf-name new-buf-fn] |
| 17 | + (let [(ok? buf) (pcall vim.fn.bufnr buf-name) |
| 18 | + loaded? (and ok? (vim.api.nvim_buf_is_loaded buf))] |
18 | 19 |
|
19 | 20 | ;; This state happens when the user unloads the buffer somehow. |
20 | 21 | ;; It still exists but is in a bad state, we delete and start over. |
21 | 22 | (when (and (not= -1 buf) (not loaded?)) |
22 | | - (nvim.buf_delete buf {})) |
| 23 | + (vim.api.nvim_buf_delete buf {})) |
23 | 24 |
|
24 | 25 | (if (or (= -1 buf) (not loaded?)) |
25 | 26 | (let [buf (if loaded? |
26 | 27 | buf |
27 | | - (let [buf (nvim.fn.bufadd buf-name)] |
28 | | - (nvim.fn.bufload buf) |
| 28 | + (let [buf (vim.fn.bufadd buf-name)] |
| 29 | + (vim.fn.bufload buf) |
29 | 30 | buf))] |
30 | | - (nvim.buf_set_option buf :buftype :nofile) |
31 | | - (nvim.buf_set_option buf :bufhidden :hide) |
32 | | - (nvim.buf_set_option buf :swapfile false) |
33 | | - (unlist buf) |
| 31 | + (vim.api.nvim_buf_set_option buf :buftype :nofile) |
| 32 | + (vim.api.nvim_buf_set_option buf :bufhidden :hide) |
| 33 | + (vim.api.nvim_buf_set_option buf :swapfile false) |
| 34 | + (M.unlist buf) |
34 | 35 | (when new-buf-fn |
35 | 36 | (new-buf-fn buf)) |
36 | 37 | buf) |
37 | 38 | buf))) |
38 | 39 |
|
39 | | -(fn empty? [buf] |
40 | | - (and (<= (nvim.buf_line_count buf) 1) |
41 | | - (= 0 (core.count (core.first (nvim.buf_get_lines buf 0 -1 false)))))) |
| 40 | +(fn M.empty? [buf] |
| 41 | + (and (<= (vim.api.nvim_buf_line_count buf) 1) |
| 42 | + (= 0 (core.count (core.first (vim.api.nvim_buf_get_lines buf 0 -1 false)))))) |
42 | 43 |
|
43 | | -(fn replace-range [buf range s] |
| 44 | +(fn M.replace-range [buf range s] |
44 | 45 | (let [start-line (core.dec (core.get-in range [:start 1])) |
45 | 46 | end-line (core.get-in range [:end 1]) |
46 | 47 | start-char (core.get-in range [:start 2]) |
47 | 48 | end-char (core.get-in range [:end 2]) |
48 | 49 |
|
49 | 50 | new-lines (text.split-lines s) |
50 | | - old-lines (nvim.buf_get_lines buf start-line end-line false) |
| 51 | + old-lines (vim.api.nvim_buf_get_lines buf start-line end-line false) |
51 | 52 |
|
52 | 53 | head (string.sub (core.first old-lines) 1 start-char) |
53 | 54 | tail (string.sub (core.last old-lines) (+ end-char 2))] |
|
60 | 61 | new-lines (core.count new-lines) |
61 | 62 | (fn [l] (.. l tail))) |
62 | 63 |
|
63 | | - (nvim.buf_set_lines buf start-line end-line false new-lines))) |
| 64 | + (vim.api.nvim_buf_set_lines buf start-line end-line false new-lines))) |
64 | 65 |
|
65 | | -(fn append-prefixed-line [buf [tl tc] prefix body] |
| 66 | +(fn M.append-prefixed-line [buf [tl tc] prefix body] |
66 | 67 | "Appends a string to the end of the current line, or the one below this one |
67 | 68 | if there's already the same suffix on the end. If there's already a matching |
68 | 69 | suffix on the end of this line and the one below, it will insert another |
69 | 70 | below that last one and so on." |
70 | 71 | (let [tl (core.dec tl) |
71 | | - [head-line & lines] (nvim.buf_get_lines buf tl -1 false) |
| 72 | + [head-line & lines] (vim.api.nvim_buf_get_lines buf tl -1 false) |
72 | 73 | to-append (text.prefixed-lines body prefix {})] |
73 | 74 | (if (head-line:find prefix tc) |
74 | 75 | (let [[new-tl lines] |
|
82 | 83 | (core.take-while core.identity) |
83 | 84 | (core.last)) |
84 | 85 | [tl (core.concat [head-line] to-append)])] |
85 | | - (nvim.buf_set_lines buf new-tl (core.inc new-tl) false lines)) |
86 | | - (nvim.buf_set_lines |
| 86 | + (vim.api.nvim_buf_set_lines buf new-tl (core.inc new-tl) false lines)) |
| 87 | + (vim.api.nvim_buf_set_lines |
87 | 88 | buf tl |
88 | 89 | (core.inc tl) |
89 | 90 | false |
|
93 | 94 | [head-line] |
94 | 95 | to-append)))))) |
95 | 96 |
|
96 | | -{: unlist |
97 | | - : resolve |
98 | | - : upsert-hidden |
99 | | - : empty? |
100 | | - : replace-range |
101 | | - : append-prefixed-line} |
| 97 | +M |
0 commit comments