@@ -12,7 +12,7 @@ local Defaults = {
12
12
--- @alias colorbox.BuiltinPolicyConfig " shuffle" | " in_order" | " reverse_order" | " single"
13
13
---
14
14
-- by filetype policy: buffer filetype => color name
15
- --- @alias colorbox.ByFileTypePolicyConfig { implement : colorbox.BuiltinPolicyConfig | table<string , string> }
15
+ --- @alias colorbox.ByFileTypePolicyConfig { mapping : table<string , string>,fallback : string }
16
16
---
17
17
-- fixed interval seconds
18
18
--- @alias colorbox.FixedIntervalPolicyConfig { seconds : integer,implement : colorbox.BuiltinPolicyConfig }
@@ -21,7 +21,7 @@ local Defaults = {
21
21
--- @type colorbox.PolicyConfig
22
22
policy = " shuffle" ,
23
23
24
- --- @type " startup" | " interval" | " filetype "
24
+ --- @type " startup" | " interval" | " bufferchanged "
25
25
timing = " startup" ,
26
26
27
27
-- (Optional) filters that disable some colors that you don't want.
132
132
133
133
local function _init ()
134
134
local home_dir = vim .fn [" colorbox#base_dir" ]()
135
- local pack_dir = " pack/colorbox/start"
136
- local full_pack_dir = string.format (" %s/%s" , home_dir , pack_dir )
135
+ -- local pack_dir = "pack/colorbox/start"
136
+ -- local full_pack_dir = string.format("%s/%s", home_dir, pack_dir)
137
137
-- logger.debug(
138
138
-- "|colorbox.init| home_dir:%s, pack_dir:%s, full_pack_dir:%s",
139
139
-- vim.inspect(home_dir),
@@ -169,23 +169,19 @@ local function _init()
169
169
-- )
170
170
end
171
171
172
- local function _update_background ()
173
- if Configs .background == " dark" or Configs .background == " light" then
174
- vim .opt .background = Configs .background
175
- end
176
- end
177
-
178
172
local function _force_sync_syntax ()
179
173
vim .cmd ([[ syntax sync fromstart]] )
180
174
end
181
175
182
176
--- @alias PreviousTrack { color_name : string,color_number : integer }
183
177
--- @param color_name string
184
178
local function _save_track (color_name )
185
- assert (
186
- type (color_name ) == " string" and string.len (vim .trim (color_name )) > 0 ,
187
- string.format (" invalid color name %s" , vim .inspect (color_name ))
188
- )
179
+ if
180
+ type (color_name ) ~= " string"
181
+ or string.len (vim .trim (color_name )) == 0
182
+ then
183
+ return
184
+ end
189
185
-- start from 0, end with #FilteredColorNamesList-1
190
186
local color_number = FilteredColorNameToIndexMap [color_name ] or 0
191
187
vim .schedule (function ()
@@ -238,8 +234,9 @@ local function _policy_shuffle()
238
234
-- vim.inspect(ColorNames),
239
235
-- vim.inspect()
240
236
-- )
241
- _update_background ()
242
- vim .cmd (string.format ([[ color %s]] , color ))
237
+ --- @diagnostic disable-next-line : param-type-mismatch
238
+ local ok , err = pcall (vim .cmd , string.format ([[ color %s]] , color ))
239
+ assert (ok , err )
243
240
end
244
241
end
245
242
@@ -248,8 +245,9 @@ local function _policy_in_order()
248
245
local previous_track = _load_previous_track () --[[ @as PreviousTrack]]
249
246
local i = previous_track ~= nil and previous_track .color_number or 0
250
247
local color = _get_next_color_name_by_idx (i )
251
- _update_background ()
252
- vim .cmd (string.format ([[ color %s]] , color ))
248
+ --- @diagnostic disable-next-line : param-type-mismatch
249
+ local ok , err = pcall (vim .cmd , string.format ([[ color %s]] , color ))
250
+ assert (ok , err )
253
251
end
254
252
end
255
253
@@ -259,8 +257,9 @@ local function _policy_reverse_order()
259
257
local i = previous_track ~= nil and previous_track .color_number
260
258
or (# FilteredColorNamesList + 1 )
261
259
local color = _get_prev_color_name_by_idx (i )
262
- _update_background ()
263
- vim .cmd (string.format ([[ color %s]] , color ))
260
+ --- @diagnostic disable-next-line : param-type-mismatch
261
+ local ok , err = pcall (vim .cmd , string.format ([[ color %s]] , color ))
262
+ assert (ok , err )
264
263
end
265
264
end
266
265
@@ -274,13 +273,15 @@ local function _policy_single()
274
273
color = _get_next_color_name_by_idx (0 )
275
274
end
276
275
if color ~= vim .g .colors_name then
277
- _update_background ()
278
- vim .cmd (string.format ([[ color %s]] , color ))
276
+ --- @diagnostic disable-next-line : param-type-mismatch
277
+ local ok , err = pcall (vim .cmd , string.format ([[ color %s]] , color ))
278
+ assert (ok , err )
279
279
end
280
280
end
281
281
end
282
282
283
283
--- @param po colorbox.Options ?
284
+ --- @return boolean
284
285
local function _is_fixed_interval_policy (po )
285
286
return type (po ) == " table"
286
287
and type (po .seconds ) == " number"
@@ -312,6 +313,39 @@ local function _policy_fixed_interval()
312
313
impl ()
313
314
end
314
315
316
+ --- @param po colorbox.Options ?
317
+ --- @return boolean
318
+ local function _is_by_filetype_policy (po )
319
+ return type (po ) == " table"
320
+ and type (po .mapping ) == " table"
321
+ and type (po .fallback ) == " string"
322
+ and string.len (po .fallback ) > 0
323
+ end
324
+
325
+ local function _policy_by_filetype ()
326
+ vim .defer_fn (function ()
327
+ local ft = vim .bo .filetype or " "
328
+
329
+ if Configs .policy .mapping [ft ] then
330
+ local ok , err = pcall (
331
+ --- @diagnostic disable-next-line : param-type-mismatch
332
+ vim .cmd ,
333
+ string.format ([[ color %s]] , Configs .policy .mapping [ft ])
334
+ )
335
+ assert (ok , err )
336
+ else
337
+ local ok , err =
338
+ --- @diagnostic disable-next-line : param-type-mismatch
339
+ pcall (
340
+ vim .cmd ,
341
+ string.format ([[ color %s]] , Configs .policy .fallback )
342
+ )
343
+ assert (ok , err )
344
+ end
345
+ _force_sync_syntax ()
346
+ end , 200 )
347
+ end
348
+
315
349
local function _policy ()
316
350
if Configs .policy == " shuffle" then
317
351
_policy_shuffle ()
@@ -321,8 +355,16 @@ local function _policy()
321
355
_policy_reverse_order ()
322
356
elseif Configs .policy == " single" then
323
357
_policy_single ()
324
- elseif _is_fixed_interval_policy (Configs .policy ) then
358
+ elseif
359
+ Configs .timing == " interval"
360
+ and _is_fixed_interval_policy (Configs .policy )
361
+ then
325
362
_policy_fixed_interval ()
363
+ elseif
364
+ Configs .timing == " bufferchanged"
365
+ and _is_by_filetype_policy (Configs .policy )
366
+ then
367
+ _policy_by_filetype ()
326
368
end
327
369
end
328
370
@@ -332,8 +374,10 @@ local function _timing_startup()
332
374
})
333
375
end
334
376
335
- local function _timing_interval ()
336
- -- Configs.timing
377
+ local function _timing_buffer_changed ()
378
+ vim .api .nvim_create_autocmd ({ " BufNew" , " BufReadPre" , " BufNewFile" }, {
379
+ callback = _policy ,
380
+ })
337
381
end
338
382
339
383
local function _timing ()
@@ -348,6 +392,17 @@ local function _timing()
348
392
)
349
393
)
350
394
_policy_fixed_interval ()
395
+ elseif Configs .timing == " bufferchanged" then
396
+ assert (
397
+ _is_by_filetype_policy (Configs .policy ),
398
+ string.format (
399
+ " invalid policy %s for 'bufferchanged' timing!" ,
400
+ vim .inspect (Configs .policy )
401
+ )
402
+ )
403
+ _timing_buffer_changed ()
404
+ else
405
+ error (string.format (" invalid timing %s!" , vim .inspect (Configs .timing )))
351
406
end
352
407
end
353
408
@@ -682,10 +737,10 @@ local function setup(opts)
682
737
683
738
vim .api .nvim_create_autocmd (" ColorSchemePre" , {
684
739
callback = function (event )
685
- logger .debug (
686
- " |colorbox.setup| ColorSchemePre event:%s" ,
687
- vim .inspect (event )
688
- )
740
+ -- logger.debug(
741
+ -- "|colorbox.setup| ColorSchemePre event:%s",
742
+ -- vim.inspect(event)
743
+ -- )
689
744
local ColorNameToColorSpecsMap =
690
745
require (" colorbox.db" ).get_color_name_to_color_specs_map ()
691
746
if
@@ -702,15 +757,21 @@ local function setup(opts)
702
757
then
703
758
Configs .setup [spec .handle ]()
704
759
end
760
+ if
761
+ Configs .background == " dark"
762
+ or Configs .background == " light"
763
+ then
764
+ vim .opt .background = Configs .background
765
+ end
705
766
end ,
706
767
})
707
768
708
769
vim .api .nvim_create_autocmd (" ColorScheme" , {
709
770
callback = function (event )
710
- logger .debug (
711
- " |colorbox.setup| ColorScheme event:%s" ,
712
- vim .inspect (event )
713
- )
771
+ -- logger.debug(
772
+ -- "|colorbox.setup| ColorScheme event:%s",
773
+ -- vim.inspect(event)
774
+ -- )
714
775
_save_track (event .match )
715
776
end ,
716
777
})
0 commit comments