Skip to content

Commit 16939cd

Browse files
authored
Add an autocommand to re-add highlight groups (#388)
1 parent 4268535 commit 16939cd

File tree

1 file changed

+157
-124
lines changed

1 file changed

+157
-124
lines changed

syntax/pandoc.vim

+157-124
Original file line numberDiff line numberDiff line change
@@ -581,130 +581,163 @@ syn region pandocYAMLHeader start=/\%(\%^\|\_^\s*\n\)\@<=\_^-\{3}\ze\n.\+/ end=/
581581
" }}}1
582582

583583
" Styling: {{{1
584-
hi link pandocOperator Operator
585-
586-
" override this for consistency
587-
hi pandocTitleBlock term=italic gui=italic
588-
hi link pandocTitleBlockTitle Directory
589-
hi link pandocAtxHeader Title
590-
hi link pandocAtxStart Operator
591-
hi link pandocSetexHeader Title
592-
hi link pandocHeaderAttr Comment
593-
hi link pandocHeaderID Identifier
594-
595-
hi link pandocLaTexSectionCmd texSection
596-
hi link pandocLaTeXDelimiter texDelimiter
597-
598-
hi link pandocHTMLComment Comment
599-
hi link pandocHTMLCommentStart Delimiter
600-
hi link pandocHTMLCommentEnd Delimiter
601-
hi link pandocBlockQuote Comment
602-
hi link pandocBlockQuoteMark Comment
603-
hi link pandocAmpersandEscape Special
604-
605-
" if the user sets g:pandoc#syntax#codeblocks#ignore to contain
606-
" a codeblock type, don't highlight it so that it remains Normal
607-
if index(g:pandoc#syntax#codeblocks#ignore, 'definition') == -1
608-
hi link pandocCodeBlockInsideIndent String
609-
endif
610-
611-
if index(g:pandoc#syntax#codeblocks#ignore, 'delimited') == -1
612-
hi link pandocDelimitedCodeBlock Special
613-
endif
614-
615-
hi link pandocDelimitedCodeBlockStart Delimiter
616-
hi link pandocDelimitedCodeBlockEnd Delimiter
617-
hi link pandocDelimitedCodeBlockLanguage Comment
618-
hi link pandocBlockQuoteinDelimitedCodeBlock pandocBlockQuote
619-
hi link pandocCodePre String
620-
621-
hi link pandocLineBlockDelimiter Delimiter
622-
623-
hi link pandocListItemBullet Operator
624-
hi link pandocUListItemBullet Operator
625-
hi link pandocListItemBulletId Identifier
626-
627-
hi link pandocReferenceLabel Label
628-
hi link pandocReferenceURL Underlined
629-
hi link pandocLinkTip Identifier
630-
hi link pandocImageIcon Operator
631-
632-
hi link pandocReferenceDefinition Operator
633-
hi link pandocReferenceDefinitionLabel Label
634-
hi link pandocReferenceDefinitionAddress Underlined
635-
hi link pandocReferenceDefinitionTip Identifier
636-
637-
hi link pandocAutomaticLink Underlined
638-
639-
hi link pandocDefinitionBlockTerm Identifier
640-
hi link pandocDefinitionBlockMark Operator
641-
642-
hi link pandocSimpleTableDelims Delimiter
643-
hi link pandocSimpleTableHeader pandocStrong
644-
hi link pandocTableMultilineHeader pandocStrong
645-
hi link pandocTableDelims Delimiter
646-
hi link pandocGridTableDelims Delimiter
647-
hi link pandocGridTableHeader Delimiter
648-
hi link pandocPipeTableDelims Delimiter
649-
hi link pandocPipeTableHeader Delimiter
650-
hi link pandocTableHeaderWord pandocStrong
651-
652-
hi link pandocAbbreviationHead Type
653-
hi link pandocAbbreviation Label
654-
hi link pandocAbbreviationTail Type
655-
hi link pandocAbbreviationSeparator Identifier
656-
hi link pandocAbbreviationDefinition Comment
657-
658-
hi link pandocFootnoteID Label
659-
hi link pandocFootnoteIDHead Type
660-
hi link pandocFootnoteIDTail Type
661-
hi link pandocFootnoteDef Comment
662-
hi link pandocFootnoteDefHead Type
663-
hi link pandocFootnoteDefTail Type
664-
hi link pandocFootnoteBlock Comment
665-
hi link pandocFootnoteBlockSeparator Operator
666-
667-
hi link pandocPCite Operator
668-
hi link pandocICite Operator
669-
hi link pandocCiteKey Label
670-
hi link pandocCiteAnchor Operator
671-
hi link pandocCiteLocator Operator
672-
673-
if g:pandoc#syntax#style#emphases == 1
674-
hi pandocEmphasis gui=italic cterm=italic
675-
hi pandocStrong gui=bold cterm=bold
676-
hi pandocStrongEmphasis gui=bold,italic cterm=bold,italic
677-
hi pandocStrongInEmphasis gui=bold,italic cterm=bold,italic
678-
hi pandocEmphasisInStrong gui=bold,italic cterm=bold,italic
679-
if !exists('s:hi_tail')
680-
let s:fg = '' " Vint can't figure ou these get set dynamically
681-
let s:bg = '' " so initialize them manually first
682-
for s:i in ['fg', 'bg']
683-
let s:tmp_val = synIDattr(synIDtrans(hlID('String')), s:i)
684-
let s:tmp_ui = has('gui_running') || (has('termguicolors') && &termguicolors) ? 'gui' : 'cterm'
685-
if !empty(s:tmp_val) && s:tmp_val != -1
686-
exe 'let s:'.s:i . ' = "'.s:tmp_ui.s:i.'='.s:tmp_val.'"'
687-
else
688-
exe 'let s:'.s:i . ' = ""'
689-
endif
690-
endfor
691-
let s:hi_tail = ' '.s:fg.' '.s:bg
692-
endif
693-
exe 'hi pandocNoFormattedInEmphasis gui=italic cterm=italic'.s:hi_tail
694-
exe 'hi pandocNoFormattedInStrong gui=bold cterm=bold'.s:hi_tail
695-
endif
696-
hi link pandocNoFormatted String
697-
hi link pandocNoFormattedAttrs Comment
698-
hi link pandocSubscriptMark Operator
699-
hi link pandocSuperscriptMark Operator
700-
hi link pandocStrikeoutMark Operator
701-
if g:pandoc#syntax#style#underline_special == 1
702-
hi pandocSubscript gui=underline cterm=underline
703-
hi pandocSuperscript gui=underline cterm=underline
704-
hi pandocStrikeout gui=underline cterm=underline
705-
endif
706-
hi link pandocNewLine Error
707-
hi link pandocHRule Delimiter
584+
function! s:SetupPandocHighlights()
585+
586+
hi link pandocOperator Operator
587+
588+
" override this for consistency
589+
hi pandocTitleBlock term=italic gui=italic
590+
hi link pandocTitleBlockTitle Directory
591+
hi link pandocAtxHeader Title
592+
hi link pandocAtxStart Operator
593+
hi link pandocSetexHeader Title
594+
hi link pandocHeaderAttr Comment
595+
hi link pandocHeaderID Identifier
596+
597+
hi link pandocLaTexSectionCmd texSection
598+
hi link pandocLaTeXDelimiter texDelimiter
599+
600+
hi link pandocHTMLComment Comment
601+
hi link pandocHTMLCommentStart Delimiter
602+
hi link pandocHTMLCommentEnd Delimiter
603+
hi link pandocBlockQuote Comment
604+
hi link pandocBlockQuoteMark Comment
605+
hi link pandocAmpersandEscape Special
606+
607+
" if the user sets g:pandoc#syntax#codeblocks#ignore to contain
608+
" a codeblock type, don't highlight it so that it remains Normal
609+
if index(g:pandoc#syntax#codeblocks#ignore, 'definition') == -1
610+
hi link pandocCodeBlockInsideIndent String
611+
endif
612+
613+
if index(g:pandoc#syntax#codeblocks#ignore, 'delimited') == -1
614+
hi link pandocDelimitedCodeBlock Special
615+
endif
616+
617+
hi link pandocDelimitedCodeBlockStart Delimiter
618+
hi link pandocDelimitedCodeBlockEnd Delimiter
619+
hi link pandocDelimitedCodeBlockLanguage Comment
620+
hi link pandocBlockQuoteinDelimitedCodeBlock pandocBlockQuote
621+
hi link pandocCodePre String
622+
623+
hi link pandocLineBlockDelimiter Delimiter
624+
625+
hi link pandocListItemBullet Operator
626+
hi link pandocUListItemBullet Operator
627+
hi link pandocListItemBulletId Identifier
628+
629+
hi link pandocReferenceLabel Label
630+
hi link pandocReferenceURL Underlined
631+
hi link pandocLinkTip Identifier
632+
hi link pandocImageIcon Operator
633+
634+
hi link pandocReferenceDefinition Operator
635+
hi link pandocReferenceDefinitionLabel Label
636+
hi link pandocReferenceDefinitionAddress Underlined
637+
hi link pandocReferenceDefinitionTip Identifier
638+
639+
hi link pandocAutomaticLink Underlined
640+
641+
hi link pandocDefinitionBlockTerm Identifier
642+
hi link pandocDefinitionBlockMark Operator
643+
644+
hi link pandocSimpleTableDelims Delimiter
645+
hi link pandocSimpleTableHeader pandocStrong
646+
hi link pandocTableMultilineHeader pandocStrong
647+
hi link pandocTableDelims Delimiter
648+
hi link pandocGridTableDelims Delimiter
649+
hi link pandocGridTableHeader Delimiter
650+
hi link pandocPipeTableDelims Delimiter
651+
hi link pandocPipeTableHeader Delimiter
652+
hi link pandocTableHeaderWord pandocStrong
653+
654+
hi link pandocAbbreviationHead Type
655+
hi link pandocAbbreviation Label
656+
hi link pandocAbbreviationTail Type
657+
hi link pandocAbbreviationSeparator Identifier
658+
hi link pandocAbbreviationDefinition Comment
659+
660+
hi link pandocFootnoteID Label
661+
hi link pandocFootnoteIDHead Type
662+
hi link pandocFootnoteIDTail Type
663+
hi link pandocFootnoteDef Comment
664+
hi link pandocFootnoteDefHead Type
665+
hi link pandocFootnoteDefTail Type
666+
hi link pandocFootnoteBlock Comment
667+
hi link pandocFootnoteBlockSeparator Operator
668+
669+
hi link pandocPCite Operator
670+
hi link pandocICite Operator
671+
hi link pandocCiteKey Label
672+
hi link pandocCiteAnchor Operator
673+
hi link pandocCiteLocator Operator
674+
675+
if g:pandoc#syntax#style#emphases == 1
676+
hi pandocEmphasis gui=italic cterm=italic
677+
hi pandocStrong gui=bold cterm=bold
678+
hi pandocStrongEmphasis gui=bold,italic cterm=bold,italic
679+
hi pandocStrongInEmphasis gui=bold,italic cterm=bold,italic
680+
hi pandocEmphasisInStrong gui=bold,italic cterm=bold,italic
681+
if !exists('s:hi_tail')
682+
let s:fg = '' " Vint can't figure ou these get set dynamically
683+
let s:bg = '' " so initialize them manually first
684+
for s:i in ['fg', 'bg']
685+
let s:tmp_val = synIDattr(synIDtrans(hlID('String')), s:i)
686+
let s:tmp_ui = has('gui_running') || (has('termguicolors') && &termguicolors) ? 'gui' : 'cterm'
687+
if !empty(s:tmp_val) && s:tmp_val != -1
688+
exe 'let s:'.s:i . ' = "'.s:tmp_ui.s:i.'='.s:tmp_val.'"'
689+
else
690+
exe 'let s:'.s:i . ' = ""'
691+
endif
692+
endfor
693+
let s:hi_tail = ' '.s:fg.' '.s:bg
694+
endif
695+
exe 'hi pandocNoFormattedInEmphasis gui=italic cterm=italic'.s:hi_tail
696+
exe 'hi pandocNoFormattedInStrong gui=bold cterm=bold'.s:hi_tail
697+
endif
698+
hi link pandocNoFormatted String
699+
hi link pandocNoFormattedAttrs Comment
700+
hi link pandocSubscriptMark Operator
701+
hi link pandocSuperscriptMark Operator
702+
hi link pandocStrikeoutMark Operator
703+
if g:pandoc#syntax#style#underline_special == 1
704+
hi pandocSubscript gui=underline cterm=underline
705+
hi pandocSuperscript gui=underline cterm=underline
706+
hi pandocStrikeout gui=underline cterm=underline
707+
endif
708+
hi link pandocNewLine Error
709+
hi link pandocHRule Delimiter
710+
711+
endfunction
712+
713+
" Whenever the colorscheme changes, all highlights are cleared.
714+
"
715+
" The most common circumstance is that the vimrc picks a colorscheme *at
716+
" startup*, then a file is opened and the syntax is set based on that file. So
717+
" the most common situation is that the colorscheme runs, then the syntax
718+
" runs, and that's that. So if the code for the syntax (e.g., this code here
719+
" in vim-pandoc-syntax) *adds* new highlighting groups that weren't defined in
720+
" the colorscheme, that's almost always fine because the colorscheme rarely
721+
" changes after startup.
722+
"
723+
" But the colorscheme *can* change after startup. This happens for example any
724+
" time the user toggles their background (:set bg=light or :set bg=dark), or
725+
" picks another colorscheme (:colorscheme something_else). In these cases, the
726+
" new colorscheme calls `:highlight clear`, clearing any custom pandoc
727+
" highlighting groups.
728+
"
729+
" The solution is to register an autocommand that runs whenever the
730+
" ColorScheme changes, so that we can re-register vim-pandoc-syntax's custom
731+
" highlighting groups, after the new colorscheme has cleared them.
732+
"
733+
" (This also affects popular plugins like goyo.vim, which call `:colorscheme`
734+
" with your chosen colorscheme to approximate undoing any custom highlighting
735+
" modifications that they've made.)
736+
augroup vim-pandoc-syntax
737+
autocmd!
738+
autocmd ColorScheme * call s:SetupPandocHighlights()
739+
augroup end
740+
call s:SetupPandocHighlights()
708741

709742
" }}}1
710743

0 commit comments

Comments
 (0)