1
- local utils = require (' modes.utils' )
1
+ --- *modes.nvim* Prismatic line decorations for the adventurous vim user
2
+ --- *Modes*
3
+ ---
4
+ --- MIT License Copyright (c) mvllow
5
+ ---
6
+ --- ==============================================================================
7
+ ---
8
+ --- Features:
9
+ ---
10
+ --- - Highlight UI elements based on the current mode.
11
+ ---
12
+ --- # Setup ~
13
+ ---
14
+ --- Modes setup can be called with your `config` table to modify default
15
+ --- behaviour.
16
+ ---
17
+ --- See |Modes.config| for `config` options and default values.
18
+
19
+ --- @alias Scene ' copy' | ' delete' | ' insert' | ' normal' | ' replace' | ' visual'
20
+
21
+ local Modes = {}
22
+ local H = {}
2
23
3
- local M = {}
4
24
local config = {}
25
+ local utils = require (' modes.utils' )
26
+
27
+ --- Module config
28
+ ---
29
+ --- Default values:
30
+ --- @eval return MiniDoc.afterlines_to_code(MiniDoc.current.eval_section)
5
31
local default_config = {
6
32
colors = {},
7
33
line_opacity = {
@@ -28,6 +54,8 @@ local default_config = {
28
54
' !minifiles' ,
29
55
},
30
56
}
57
+ -- minidoc_afterlines_end
58
+ --
31
59
local winhighlight = {
32
60
default = {
33
61
CursorLine = ' CursorLine' ,
@@ -81,24 +109,24 @@ local in_ignored_buffer = function()
81
109
return config .ignore ()
82
110
end
83
111
return not vim .tbl_contains (config .ignore , ' !' .. vim .bo .filetype )
84
- and (vim .api .nvim_get_option_value (' buftype' , { buf = 0 }) ~= ' ' -- not a normal buffer
85
- or not vim .api .nvim_get_option_value (' buflisted' , { buf = 0 }) -- unlisted buffer
86
- or vim .tbl_contains (config .ignore , vim .bo .filetype ))
112
+ and (vim .api .nvim_get_option_value (' buftype' , { buf = 0 }) ~= ' ' -- not a normal buffer
113
+ or not vim .api .nvim_get_option_value (' buflisted' , { buf = 0 }) -- unlisted buffer
114
+ or vim .tbl_contains (config .ignore , vim .bo .filetype ))
87
115
end
88
116
89
- M .reset = function ()
90
- M .highlight (' default' )
117
+ H .reset = function ()
118
+ H .highlight (' default' )
91
119
vim .cmd .redraw ()
92
120
end
93
121
94
- M .restore = function ()
95
- local scene = M .get_scene ()
96
- M .highlight (scene )
122
+ H .restore = function ()
123
+ local scene = H .get_scene ()
124
+ H .highlight (scene )
97
125
end
98
126
99
127
--- Update highlights
100
128
--- @param scene ' default' | ' copy' | ' delete' | ' change' | ' format' | ' insert' | ' visual'
101
- M .highlight = function (scene )
129
+ H .highlight = function (scene )
102
130
if in_ignored_buffer () then
103
131
return
104
132
end
@@ -122,7 +150,7 @@ M.highlight = function(scene)
122
150
if not config .set_number then
123
151
winhl_map .CursorLineNr = nil
124
152
elseif not config .set_cursorline then
125
- local detected_scene = M .get_scene ()
153
+ local detected_scene = H .get_scene ()
126
154
if scene == ' default' and detected_scene == ' visual' then
127
155
winhl_map .CursorLineNr = ' ModesVisualUnfocusedCursorLineNr'
128
156
end
@@ -167,7 +195,7 @@ M.highlight = function(scene)
167
195
end
168
196
end
169
197
170
- M .get_scene = function ()
198
+ H .get_scene = function ()
171
199
local mode = vim .api .nvim_get_mode ().mode
172
200
if mode :match (' [iR]' ) then
173
201
return ' insert'
@@ -179,7 +207,7 @@ M.get_scene = function()
179
207
return ' default'
180
208
end
181
209
182
- M .define = function ()
210
+ H .define = function ()
183
211
colors = {
184
212
bg = config .colors .bg or utils .get_bg (' Normal' , ' Normal' ),
185
213
copy = config .colors .copy or utils .get_bg (' ModesCopy' , ' #f5c359' ),
@@ -278,7 +306,7 @@ M.define = function()
278
306
end
279
307
end
280
308
281
- M .enable_managed_ui = function ()
309
+ H .enable_managed_ui = function ()
282
310
if in_ignored_buffer () then
283
311
if config .set_cursorline then
284
312
vim .o .cursorline = false
@@ -294,11 +322,11 @@ M.enable_managed_ui = function()
294
322
vim .opt .guicursor :append (' r-o:ModesOperator' )
295
323
end
296
324
297
- M .restore ()
325
+ H .restore ()
298
326
end
299
327
end
300
328
301
- M .disable_managed_ui = function ()
329
+ H .disable_managed_ui = function ()
302
330
if config .set_cursorline then
303
331
vim .o .cursorline = false
304
332
end
@@ -315,10 +343,15 @@ M.disable_managed_ui = function()
315
343
vim .o .guicursor = cursor
316
344
end
317
345
318
- M .reset ()
346
+ H .reset ()
319
347
end
320
348
321
- M .setup = function (opts )
349
+ --- Module setup
350
+ ---
351
+ --- @param config table | nil Module config table. See | Modes.config | .
352
+ ---
353
+ --- @usage `require('modes').setup({})` (replace `{}` with your `config` table)
354
+ Modes .setup = function (opts )
322
355
opts = vim .tbl_extend (' keep' , opts or {}, default_config )
323
356
if opts .focus_only then
324
357
vim .notify (
@@ -352,13 +385,13 @@ M.setup = function(opts)
352
385
}
353
386
end
354
387
355
- M .define ()
356
- M .enable_managed_ui () -- ensure enabled initially
388
+ H .define ()
389
+ H .enable_managed_ui () -- ensure enabled initially
357
390
358
391
--- Reset normal highlight
359
392
vim .api .nvim_create_autocmd (' ModeChanged' , {
360
393
pattern = ' *:n,*:ni*' ,
361
- callback = M .reset ,
394
+ callback = H .reset ,
362
395
})
363
396
364
397
--- Set operator highlights
@@ -367,52 +400,52 @@ M.setup = function(opts)
367
400
callback = function ()
368
401
local operator = vim .v .operator
369
402
if operator == ' y' then
370
- M .highlight (' copy' )
403
+ H .highlight (' copy' )
371
404
elseif operator == ' d' then
372
- M .highlight (' delete' )
405
+ H .highlight (' delete' )
373
406
elseif operator == ' c' then
374
- M .highlight (' change' )
407
+ H .highlight (' change' )
375
408
elseif operator :match (' [=!><g]' ) then
376
- M .highlight (' format' )
409
+ H .highlight (' format' )
377
410
end
378
411
end ,
379
412
})
380
413
381
414
--- Set highlights when colorscheme changes
382
415
vim .api .nvim_create_autocmd (' ColorScheme' , {
383
416
pattern = ' *' ,
384
- callback = M .define ,
417
+ callback = H .define ,
385
418
})
386
419
387
420
--- Set insert highlight
388
421
vim .api .nvim_create_autocmd (' InsertEnter' , {
389
422
pattern = ' *' ,
390
423
callback = function ()
391
- M .highlight (' insert' )
424
+ H .highlight (' insert' )
392
425
end ,
393
426
})
394
427
395
428
--- Set visual highlight
396
429
vim .api .nvim_create_autocmd (' ModeChanged' , {
397
430
pattern = ' *:[vVsS\x16\x13 ]' ,
398
431
callback = function ()
399
- M .highlight (' visual' )
432
+ H .highlight (' visual' )
400
433
end ,
401
434
})
402
435
403
436
--- Enable managed UI for current window
404
437
vim .api .nvim_create_autocmd ({ ' WinEnter' , ' FocusGained' }, {
405
438
pattern = ' *' ,
406
439
callback = function ()
407
- vim .schedule (M .enable_managed_ui )
440
+ vim .schedule (H .enable_managed_ui )
408
441
end ,
409
442
})
410
443
411
444
--- Disable managed UI
412
445
vim .api .nvim_create_autocmd ({ ' WinLeave' , ' FocusLost' }, {
413
446
pattern = ' *' ,
414
- callback = M .disable_managed_ui ,
447
+ callback = H .disable_managed_ui ,
415
448
})
416
449
end
417
450
418
- return M
451
+ return Modes
0 commit comments