-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathinit.lua
261 lines (215 loc) · 6.44 KB
/
init.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
local mark = require'marks.mark'
local bookmark = require'marks.bookmark'
local utils = require'marks.utils'
local M = {}
function M.set()
local err, input = pcall(function()
return string.char(vim.fn.getchar())
end)
if not err then
return
end
if utils.is_valid_mark(input) then
if not M.excluded_fts[vim.bo.ft] and not M.excluded_bts[vim.bo.bt] then
M.mark_state:place_mark_cursor(input)
end
vim.cmd("normal! m" .. input)
end
end
function M.set_next()
if not M.excluded_fts[vim.bo.ft] and not M.excluded_bts[vim.bo.bt] then
M.mark_state:place_next_mark_cursor()
end
end
function M.toggle()
if not M.excluded_fts[vim.bo.ft] and not M.excluded_bts[vim.bo.bt] then
M.mark_state:toggle_mark_cursor()
end
end
function M.delete()
local err, input = pcall(function()
return string.char(vim.fn.getchar())
end)
if not err then
return
end
if utils.is_valid_mark(input) then
M.mark_state:delete_mark(input)
return
end
end
function M.delete_line()
M.mark_state:delete_line_marks()
end
function M.delete_buf()
M.mark_state:delete_buf_marks()
end
function M.preview()
M.mark_state:preview_mark()
end
function M.next()
M.mark_state:next_mark()
end
function M.prev()
M.mark_state:prev_mark()
end
function M.annotate()
M.bookmark_state:annotate()
end
function M.refresh(force_reregister)
if M.excluded_fts[vim.bo.ft] or M.excluded_bts[vim.bo.bt] then
return
end
force_reregister = force_reregister or false
M.mark_state:refresh(nil, force_reregister)
M.bookmark_state:refresh()
end
function M._on_delete()
local bufnr = tonumber(vim.fn.expand("<abuf>"))
if not bufnr then
return
end
M.mark_state.buffers[bufnr] = nil
for _, group in pairs(M.bookmark_state.groups) do
group.marks[bufnr] = nil
end
end
function M.toggle_signs(bufnr)
if not bufnr then
M.mark_state.opt.signs = not M.mark_state.opt.signs
M.bookmark_state.opt.signs = not M.bookmark_state.opt.signs
for buf, _ in pairs(M.mark_state.opt.buf_signs) do
M.mark_state.opt.buf_signs[buf] = M.mark_state.opt.signs
end
for buf, _ in pairs(M.bookmark_state.opt.buf_signs) do
M.bookmark_state.opt.buf_signs[buf] = M.bookmark_state.opt.signs
end
else
M.mark_state.opt.buf_signs[bufnr] = not utils.option_nil(
M.mark_state.opt.buf_signs[bufnr], M.mark_state.opt.signs)
M.bookmark_state.opt.buf_signs[bufnr] = not utils.option_nil(
M.bookmark_state.opt.buf_signs[bufnr], M.bookmark_state.opt.signs)
end
M.refresh(true)
end
-- set_group[0-9] functions
for i=0,9 do
M["set_bookmark" .. i] = function() M.bookmark_state:place_mark(i) end
M["toggle_bookmark" .. i] = function() M.bookmark_state:toggle_mark(i) end
M["delete_bookmark" .. i] = function() M.bookmark_state:delete_all(i) end
M["next_bookmark" .. i] = function() M.bookmark_state:next(i) end
M["prev_bookmark" .. i] = function() M.bookmark_state:prev(i) end
end
function M.delete_bookmark()
M.bookmark_state:delete_mark_cursor()
end
function M.next_bookmark()
M.bookmark_state:next()
end
function M.prev_bookmark()
M.bookmark_state:prev()
end
M.mappings = {
set = "m",
set_next = "m,",
toggle = "m;",
next = "m]",
prev = "m[",
preview = "m:",
next_bookmark = "m}",
prev_bookmark = "m{",
delete = "dm",
delete_line = "dm-",
delete_bookmark = "dm=",
delete_buf = "dm<space>"
}
for i=0,9 do
M.mappings["set_bookmark" .. i] = "m"..tostring(i)
M.mappings["delete_bookmark" .. i] = "dm"..tostring(i)
end
local function user_mappings(config)
for cmd, key in pairs(config.mappings) do
if key ~= false then
M.mappings[cmd] = key
else
M.mappings[cmd] = nil
end
end
end
local function apply_mappings()
for cmd, key in pairs(M.mappings) do
vim.cmd("nnoremap <silent> "..key.." <cmd>lua require'marks'."..cmd.."()<cr>")
end
end
local function setup_mappings(config)
if not config.default_mappings then
M.mappings = {}
end
if config.mappings then
user_mappings(config)
end
apply_mappings()
end
local function setup_autocommands()
vim.cmd [[augroup Marks_autocmds
autocmd!
autocmd BufEnter * lua require'marks'.refresh(true)
autocmd BufDelete * lua require'marks'._on_delete()
augroup end]]
end
function M.setup(config)
config = config or {}
M.mark_state = mark.new()
M.mark_state.builtin_marks = config.builtin_marks or {}
M.bookmark_state = bookmark.new()
local bookmark_config
for i=0,9 do
bookmark_config = config["bookmark_" .. i]
if bookmark_config then
if bookmark_config.sign == false then
M.bookmark_state.signs[i] = nil
else
M.bookmark_state.signs[i] = bookmark_config.sign or M.bookmark_state.signs[i]
end
M.bookmark_state.virt_text[i] = bookmark_config.virt_text or
M.bookmark_state.virt_text[i]
M.bookmark_state.prompt_annotate[i] = bookmark_config.annotate
end
end
local excluded_fts = {}
for _, ft in ipairs(config.excluded_filetypes or {}) do
excluded_fts[ft] = true
end
M.excluded_fts = excluded_fts
local excluded_bts = {}
for _, bt in ipairs(config.excluded_buftypes or {}) do
excluded_bts[bt] = true
end
M.excluded_bts = excluded_bts
M.bookmark_state.opt.signs = true
M.bookmark_state.opt.buf_signs = {}
config.default_mappings = utils.option_nil(config.default_mappings, true)
setup_mappings(config)
setup_autocommands()
M.mark_state.opt.signs = utils.option_nil(config.signs, true)
M.mark_state.opt.buf_signs = {}
M.mark_state.opt.force_write_shada = utils.option_nil(config.force_write_shada, false)
M.mark_state.opt.cyclic = utils.option_nil(config.cyclic, true)
M.mark_state.opt.priority = { 10, 10, 10 }
local mark_priority = M.mark_state.opt.priority
if type(config.sign_priority) == "table" then
mark_priority[1] = config.sign_priority.lower or mark_priority[1]
mark_priority[2] = config.sign_priority.upper or mark_priority[2]
mark_priority[3] = config.sign_priority.builtin or mark_priority[3]
M.bookmark_state.priority = config.sign_priority.bookmark or 10
elseif type(config.sign_priority) == "number" then
mark_priority[1] = config.sign_priority
mark_priority[2] = config.sign_priority
mark_priority[3] = config.sign_priority
M.bookmark_state.priority = config.sign_priority
end
local refresh_interval = utils.option_nil(config.refresh_interval, 150)
local timer = vim.loop.new_timer()
timer:start(0, refresh_interval, vim.schedule_wrap(M.refresh))
end
return M