Skip to content

Commit 60a6836

Browse files
committed
patch 8.0.1777: cannot cleanup before loading another colorscheme
Problem: Cannot cleanup before loading another colorscheme. Solution: Add the ColorSchemePre autocommand event.
1 parent 0e9d1ae commit 60a6836

File tree

6 files changed

+31
-2
lines changed

6 files changed

+31
-2
lines changed

runtime/colors/README.txt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,16 @@ this autocmd might be useful:
4242
Replace "blue_sky" with the name of the colorscheme.
4343

4444
In case you want to tweak a colorscheme after it was loaded, check out the
45-
ColorScheme autocmd event.
45+
ColorScheme autocommand event.
46+
47+
To clean up just before loading another colorscheme, use the ColorSchemePre
48+
autocommand event. For example:
49+
let g:term_ansi_colors = ...
50+
augroup MyColorscheme
51+
au!
52+
au ColorSchemePre * unlet g:term_ansi_colors
53+
au ColorSchemePre * au! MyColorscheme
54+
augroup END
4655

4756
To customize a colorscheme use another name, e.g. "~/.vim/colors/mine.vim",
4857
and use `:runtime` to load the original colorscheme:

src/fileio.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7734,6 +7734,7 @@ static struct event_name
77347734
{"CmdwinLeave", EVENT_CMDWINLEAVE},
77357735
{"CmdUndefined", EVENT_CMDUNDEFINED},
77367736
{"ColorScheme", EVENT_COLORSCHEME},
7737+
{"ColorSchemePre", EVENT_COLORSCHEMEPRE},
77377738
{"CompleteDone", EVENT_COMPLETEDONE},
77387739
{"CursorHold", EVENT_CURSORHOLD},
77397740
{"CursorHoldI", EVENT_CURSORHOLDI},
@@ -9479,7 +9480,8 @@ apply_autocmds_group(
94799480
*/
94809481
if (fname_io == NULL)
94819482
{
9482-
if (event == EVENT_COLORSCHEME || event == EVENT_OPTIONSET)
9483+
if (event == EVENT_COLORSCHEME || event == EVENT_COLORSCHEMEPRE
9484+
|| event == EVENT_OPTIONSET)
94839485
autocmd_fname = NULL;
94849486
else if (fname != NULL && !ends_excmd(*fname))
94859487
autocmd_fname = fname;
@@ -9549,6 +9551,7 @@ apply_autocmds_group(
95499551
|| event == EVENT_SPELLFILEMISSING
95509552
|| event == EVENT_QUICKFIXCMDPRE
95519553
|| event == EVENT_COLORSCHEME
9554+
|| event == EVENT_COLORSCHEMEPRE
95529555
|| event == EVENT_OPTIONSET
95539556
|| event == EVENT_QUICKFIXCMDPOST
95549557
|| event == EVENT_DIRCHANGED)

src/syntax.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7224,6 +7224,8 @@ load_colors(char_u *name)
72247224
buf = alloc((unsigned)(STRLEN(name) + 12));
72257225
if (buf != NULL)
72267226
{
7227+
apply_autocmds(EVENT_COLORSCHEMEPRE, name,
7228+
curbuf->b_fname, FALSE, curbuf);
72277229
sprintf((char *)buf, "colors/%s.vim", name);
72287230
retval = source_runtime(buf, DIP_START + DIP_OPT);
72297231
vim_free(buf);

src/testdir/test_gui.vim

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,25 @@ endfunc
3333

3434
func Test_colorscheme()
3535
let colorscheme_saved = exists('g:colors_name') ? g:colors_name : 'default'
36+
let g:color_count = 0
37+
augroup TestColors
38+
au!
39+
au ColorScheme * let g:color_count += 1| let g:after_colors = g:color_count
40+
au ColorSchemePre * let g:color_count += 1 |let g:before_colors = g:color_count
41+
augroup END
3642

3743
colorscheme torte
3844
redraw!
3945
sleep 200m
4046
call assert_equal('dark', &background)
47+
call assert_equal(1, g:before_colors)
48+
call assert_equal(2, g:after_colors)
4149

4250
exec 'colorscheme' colorscheme_saved
51+
augroup TestColors
52+
au!
53+
augroup END
54+
unlet g:color_count g:after_colors g:before_colors
4355
redraw!
4456
endfunc
4557

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -761,6 +761,8 @@ static char *(features[]) =
761761

762762
static int included_patches[] =
763763
{ /* Add new patch number below this line */
764+
/**/
765+
1777,
764766
/**/
765767
1776,
766768
/**/

src/vim.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1277,6 +1277,7 @@ enum auto_event
12771277
EVENT_CMDWINENTER, /* after entering the cmdline window */
12781278
EVENT_CMDWINLEAVE, /* before leaving the cmdline window */
12791279
EVENT_COLORSCHEME, /* after loading a colorscheme */
1280+
EVENT_COLORSCHEMEPRE, /* before loading a colorscheme */
12801281
EVENT_COMPLETEDONE, /* after finishing insert complete */
12811282
EVENT_CURSORHOLD, /* cursor in same position for a while */
12821283
EVENT_CURSORHOLDI, /* idem, in Insert mode */

0 commit comments

Comments
 (0)