-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopts.lua
More file actions
286 lines (256 loc) · 8.13 KB
/
opts.lua
File metadata and controls
286 lines (256 loc) · 8.13 KB
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
local MODSTR = 'user_api.opts'
local ERROR = vim.log.levels.ERROR
local INFO = vim.log.levels.INFO
local in_list = vim.list_contains
local curr_buf = vim.api.nvim_get_current_buf
local validate = require('user_api.check').validate
---@class User.Opts
local Opts = {}
Opts.options = {} ---@type User.Opts.Spec
---@return User.Opts.AllOpts all_opts
function Opts.get_all_opts()
return require('user_api.opts.all_opts')
end
---@return User.Opts.Spec defaults
function Opts.get_defaults()
return require('user_api.opts.config')
end
---@param ArgLead string
---@param CursorPos integer
---@return string[] items
local function toggle_completer(ArgLead, _, CursorPos)
local len = ArgLead:len()
local CMD_LEN = ('OptsToggle '):len() + 1
if len == 0 or CursorPos < CMD_LEN then
return Opts.toggleable
end
local valid = {} ---@type string[]
for _, o in ipairs(Opts.toggleable) do
if o:match(ArgLead) ~= nil and o:find('^' .. ArgLead) then
table.insert(valid, o)
end
end
return valid
end
---@param short? boolean
---@return string[] valid
function Opts.gen_toggleable(short)
validate({ short = { short, { 'boolean', 'nil' }, true } })
if short == nil then
short = false
end
local valid = {} ---@type string[]
local T = Opts.get_all_opts()
local o_long, o_short = vim.tbl_keys(T), vim.tbl_values(T) ---@type string[], string[]
for _, opt in ipairs(o_long) do
if opt ~= '' and in_list({ 'no', 'yes', true, false }, vim.o[opt]) then
table.insert(valid, opt)
end
end
if short then
for _, opt in ipairs(o_short) do
if opt ~= '' and in_list({ 'no', 'yes', true, false }, vim.o[opt]) then
table.insert(valid, opt)
end
end
end
table.sort(valid)
return valid
end
Opts.toggleable = Opts.gen_toggleable(true)
---@param T User.Opts.Spec
---@param verbose boolean
---@return User.Opts.Spec parsed_opts
---@overload fun(T: User.Opts.Spec): parsed_opts: User.Opts.Spec
function Opts.long_opts_convert(T, verbose)
validate({
T = { T, { 'table' } },
verbose = { verbose, { 'boolean', 'nil' }, true },
})
verbose = verbose ~= nil and verbose or false
local Value = require('user_api.check.value')
local parsed_opts = {} ---@type User.Opts.Spec
local msg, verb_msg = '', ''
if not Value.type_not_empty('table', T) then
if verbose then
vim.notify('(user.opts.long_opts_convert): All seems good', INFO)
end
return parsed_opts
end
local ALL_OPTIONS = Opts.get_all_opts()
local keys = vim.tbl_keys(ALL_OPTIONS) ---@type string[]
table.sort(keys)
for opt, val in pairs(T) do
if in_list(keys, opt) then
parsed_opts[opt] = val
elseif not Value.tbl_values({ opt }, ALL_OPTIONS) then
-- If neither long nor short (known) option, append to warning message
msg = ('%s- Option `%s` not valid!\n'):format(msg, opt)
else
local new_opt = Value.tbl_values({ opt }, ALL_OPTIONS, true)
if Value.is_str(new_opt) and new_opt ~= '' then
parsed_opts[new_opt] = val
verb_msg = ('%s%s ==> %s\n'):format(verb_msg, opt, new_opt)
else
msg = ('%s- Option `%s` non valid!\n'):format(msg, new_opt)
end
end
end
if msg ~= '' then
vim.notify(msg, ERROR)
elseif verbose and verb_msg ~= '' then
vim.notify(verb_msg, INFO)
end
return parsed_opts
end
---Option setter for the aforementioned options dictionary.
--- ---
---@param O User.Opts.Spec A dictionary with keys acting as `vim.o` fields, and values
---@param verbose boolean Enable verbose printing if `true`
---@overload fun(O: User.Opts.Spec)
function Opts.optset(O, verbose)
validate({
O = { O, { 'table' } },
verbose = { verbose, { 'boolean', 'nil' }, true },
})
verbose = verbose ~= nil and verbose or false
if not vim.api.nvim_get_option_value('modifiable', { buf = curr_buf() }) then
return
end
local msg, verb_msg = '', ''
local opts = Opts.long_opts_convert(O, verbose)
for k, v in pairs(opts) do
if type(vim.o[k]) == type(v) then
Opts.options[k] = v
vim.o[k] = Opts.options[k]
verb_msg = ('%s- %s: %s\n'):format(verb_msg, k, vim.inspect(v))
end
end
if msg ~= '' then
vim.notify(msg, ERROR)
return
end
if verbose then
vim.notify(verb_msg, INFO)
end
end
---Set up `guicursor` so that cursor blinks.
--- ---
function Opts.set_cursor_blink()
if require('user_api.check').in_console() then
return
end
Opts.optset({
guicursor = 'n-v-c:block'
.. ',i-ci-ve:ver25'
.. ',r-cr:hor20'
.. ',o:hor50'
.. ',a:blinkwait700-blinkoff400-blinkon250-Cursor/lCursor'
.. ',sm:block-blinkwait175-blinkoff150-blinkon175',
})
end
function Opts.print_set_opts()
local T = vim.deepcopy(Opts.options)
table.sort(T)
vim.notify(vim.inspect(T), INFO)
end
---@param O string[]|string
---@param verbose boolean
---@overload fun(O: string)
---@overload fun(O: string[])
---@overload fun(O: string, verbose: boolean)
---@overload fun(O: string[], verbose: boolean)
function Opts.toggle(O, verbose)
validate({
O = { O, { 'string', 'table' } },
verbose = { verbose, { 'boolean', 'nil' }, true },
})
verbose = verbose ~= nil and verbose or false
local Value = require('user_api.check.value')
---@cast O string
if Value.is_str(O) then
O = { O }
end
---@cast O string[]
if vim.tbl_isempty(O) then
return
end
for _, opt in ipairs(O) do
if in_list(Opts.toggleable, opt) then
local value = vim.o[opt]
if Value.is_bool(value) then
value = not value
else
value = value == 'yes' and 'no' or 'yes'
end
Opts.optset({ [opt] = value }, verbose)
end
end
end
function Opts.setup_cmds()
local Commands = require('user_api.commands')
local desc = require('user_api.commands').desc
Commands.add_command('OptsToggle', function(ctx)
local cmds = {}
for _, v in ipairs(ctx.fargs) do
if not (in_list(Opts.toggleable, v) or ctx.bang) then
vim.notify(('(OptsToggle): Cannot toggle option `%s`, aborting'):format(v), ERROR)
return
end
if in_list(Opts.toggleable, v) and not in_list(cmds, v) then
table.insert(cmds, v)
end
end
Opts.toggle(cmds, ctx.bang)
end, desc('Toggle Vim Options', true, '+', toggle_completer))
Commands.add_command('OptsToggleable', function(ctx)
local toggleable = Opts.gen_toggleable(ctx.bang)
local msg = ''
for i, v in ipairs(toggleable) do
msg = ('%s%s%s'):format(msg, i == 1 and '' or '\n', v)
end
vim.notify(msg)
end, desc('Print all toggleable options', true))
end
function Opts.setup_maps()
local desc = require('user_api.maps').desc
require('user_api.config.keymaps').set({
n = {
['<leader>UO'] = { group = '+Options' },
['<leader>UOl'] = { Opts.print_set_opts, desc('Print options set by `user.opts`') },
['<leader>UOT'] = { ':OptsToggle ', desc('Prompt To Toggle Opts', false) },
},
})
end
---@param override User.Opts.Spec A table with custom options
---@param verbose boolean Flag to make the function return a string with invalid values, if any
---@param cursor_blink boolean Whether to enable cursor blinking
---@overload fun()
---@overload fun(override: User.Opts.Spec)
---@overload fun(override: User.Opts.Spec, verbose: boolean)
function Opts.setup(override, verbose, cursor_blink)
validate({
override = { override, { 'table', 'nil' }, true },
verbose = { verbose, { 'boolean', 'nil' }, true },
cursor_blink = { cursor_blink, { 'boolean', 'nil' }, true },
})
verbose = verbose ~= nil and verbose or false
cursor_blink = cursor_blink ~= nil and cursor_blink or false
if vim.tbl_isempty(Opts.options) then
Opts.options = Opts.long_opts_convert(Opts.get_defaults(), verbose)
end
local parsed_opts = Opts.long_opts_convert(override or {}, verbose)
Opts.options = vim.tbl_deep_extend('keep', parsed_opts, Opts.options) ---@type vim.bo|vim.wo
Opts.optset(Opts.options, verbose)
if cursor_blink then
Opts.set_cursor_blink()
end
end
local M = setmetatable(Opts, { ---@type User.Opts
__index = Opts,
__newindex = function()
vim.notify(('(%s): This module is read only!'):format(MODSTR), ERROR)
end,
})
return M
-- vim: set ts=2 sts=2 sw=2 et ai si sta: