Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Add g:vimtex_syntax_custom_envs #2756

Merged
merged 3 commits into from
Jul 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions autoload/vimtex/options.vim
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ function! vimtex#options#init() abort " {{{1
call s:init_option('vimtex_syntax_conceal_disable', 0)
call s:init_option('vimtex_syntax_custom_cmds', [])
call s:init_option('vimtex_syntax_custom_cmds_with_concealed_delims', [])
call s:init_option('vimtex_syntax_custom_envs', [])
call s:init_option('vimtex_syntax_nested', {
\ 'aliases' : {
\ 'C' : 'c',
Expand Down
140 changes: 99 additions & 41 deletions autoload/vimtex/syntax/core.vim
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,10 @@ function! vimtex#syntax#core#init_rules() abort " {{{1
" {{{2 Zone: Verbatim

" Verbatim environment
call vimtex#syntax#core#new_region_env('texVerbZone', '[vV]erbatim')
call vimtex#syntax#core#new_env({
\ 'name': '[vV]erbatim',
\ 'region': 'texVerbZone',
\})

" Verbatim inline
syntax match texCmdVerb "\\verb\>\*\?" nextgroup=texVerbZoneInline
Expand Down Expand Up @@ -530,10 +533,26 @@ function! vimtex#syntax#core#init_rules() abort " {{{1
\})

" Math regions: environments
call vimtex#syntax#core#new_region_math('displaymath')
call vimtex#syntax#core#new_region_math('eqnarray')
call vimtex#syntax#core#new_region_math('equation')
call vimtex#syntax#core#new_region_math('math')
call vimtex#syntax#core#new_env({
\ 'name': 'displaymath',
\ 'starred': v:true,
\ 'math': v:true
\})
call vimtex#syntax#core#new_env({
\ 'name': 'eqnarray',
\ 'starred': v:true,
\ 'math': v:true
\})
call vimtex#syntax#core#new_env({
\ 'name': 'equation',
\ 'starred': v:true,
\ 'math': v:true
\})
call vimtex#syntax#core#new_env({
\ 'name': 'math',
\ 'starred': v:true,
\ 'math': v:true
\})

" Math regions: Inline Math Zones
let l:conceal = g:vimtex_syntax_conceal.math_bounds ? 'concealends' : ''
Expand Down Expand Up @@ -704,6 +723,10 @@ function! vimtex#syntax#core#init_custom() abort " {{{1
for l:item in g:vimtex_syntax_custom_cmds_with_concealed_delims
call vimtex#syntax#core#new_cmd_with_concealed_delims(l:item)
endfor

for l:item in g:vimtex_syntax_custom_envs
call vimtex#syntax#core#new_env(l:item)
endfor
endfunction

" }}}1
Expand Down Expand Up @@ -1150,52 +1173,87 @@ function! vimtex#syntax#core#new_cmd_with_concealed_delims(cfg) abort " {{{1
endfunction

" }}}1

function! vimtex#syntax#core#new_region_env(grp, envname, ...) abort " {{{1
function! vimtex#syntax#core#new_env(cfg) abort " {{{1
let l:cfg = extend({
\ 'contains': '',
\ 'name': '',
\ 'region': '',
\ 'math': v:false,
\ 'math_nextgroup': '',
\ 'starred': v:false,
\ 'transparent': v:false,
\ 'opts': '',
\ 'transparent': 0,
\}, a:0 > 0 ? a:1 : {})
\ 'contains': '',
\ 'nested': '',
\ '__predicate': '',
\}, a:cfg)

let l:contains = 'contains=texCmdEnv'
if !empty(l:cfg.contains)
let l:contains .= ',' . l:cfg.contains
if type(l:cfg.nested) == v:t_dict && !empty(l:cfg.nested)
for [l:lang, l:predicate] in items(l:cfg.nested)
let l:nested_cfg = deepcopy(l:cfg)
let l:nested_cfg.nested = l:lang
let l:nested_cfg.__predicate = l:predicate
call vimtex#syntax#core#new_env(l:nested_cfg)
endfor
return
endif

let l:options = 'keepend'
if l:cfg.transparent
let l:options .= ' transparent'
endif
if !empty(l:cfg.opts)
let l:options .= ' ' . l:cfg.opts
endif
let l:env_name = l:cfg.name . (l:cfg.starred ? '\*\?' : '')

execute 'syntax region' a:grp
\ 'start="\\begin{' . a:envname .'}"'
\ 'end="\\end{' . a:envname .'}"'
\ l:contains
\ l:options
endfunction
if l:cfg.math
let l:cfg.region = 'texMathZoneEnv'
let l:options = 'keepend'
let l:contains = 'contains=texMathEnvBgnEnd,@texClusterMath'

" }}}1
function! vimtex#syntax#core#new_region_math(mathzone, ...) abort " {{{1
let l:cfg = extend({
\ 'starred': 1,
\ 'next': '',
\}, a:0 > 0 ? a:1 : {})
let l:next = ''
if !empty(l:cfg.math_nextgroup)
let l:next = 'nextgroup=' . l:cfg.math_nextgroup . ' skipwhite skipnl'
endif

execute 'syntax match texMathEnvBgnEnd'
\ '"\\\%(begin\|end\){' . l:env_name . '}"'
\ 'contained contains=texCmdMathEnv'
\ l:next
execute 'syntax match texMathError "\\end{' . l:env_name . '}"'
else
if empty(l:cfg.region)
let l:cfg.region = printf(
\ 'tex%sZone',
\ toupper(l:cfg.name[0]) . l:cfg.name[1:])
endif

let l:envname = a:mathzone . (l:cfg.starred ? '\*\?' : '')
let l:options = 'keepend'
if l:cfg.transparent
let l:options .= ' transparent'
endif
if !empty(l:cfg.opts)
let l:options .= ' ' . l:cfg.opts
endif

execute 'syntax match texMathEnvBgnEnd "\\\%(begin\|end\)\>{' . l:envname . '}"'
\ 'contained contains=texCmdMathEnv'
\ (empty(l:cfg.next) ? '' : 'nextgroup=' . l:cfg.next . ' skipwhite skipnl')
execute 'syntax match texMathError "\\end{' . l:envname . '}"'
execute 'syntax region texMathZoneEnv'
\ 'start="\\begin{\z(' . l:envname . '\)}"'
let l:contains = 'contains=texCmdEnv'
if !empty(l:cfg.contains)
let l:contains .= ',' . l:cfg.contains
endif

if !empty(l:cfg.nested)
let l:nested = vimtex#syntax#nested#include(l:cfg.nested)
if !empty(l:nested)
let l:contains .= ',' . l:nested
else
execute 'highlight def link' l:cfg.region 'texZone'
endif
endif
endif

let l:start = '\\begin{\z(' . l:env_name .'\)}'
if !empty(l:cfg.__predicate)
let l:start .= '\s*\[\_[^\]]\{-}' . l:cfg.__predicate . '\_[^\]]\{-}\]'
endif

execute 'syntax region' l:cfg.region
\ 'start="' . l:start . '"'
\ 'end="\\end{\z1}"'
\ 'contains=texMathEnvBgnEnd,@texClusterMath'
\ 'keepend'
\ l:contains
\ l:options
endfunction

" }}}1
Expand Down
2 changes: 1 addition & 1 deletion autoload/vimtex/syntax/nested.vim
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function! vimtex#syntax#nested#include(name) abort " {{{1
let s:included[l:inc_name] = s:include(l:inc_name, a:name)
endif

return s:included[l:inc_name] ? l:inc_name : ''
return s:included[l:inc_name] ? '@' . l:inc_name : ''
endfunction

" }}}1
Expand Down
29 changes: 21 additions & 8 deletions autoload/vimtex/syntax/p/amsmath.vim
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,27 @@
scriptencoding utf-8

function! vimtex#syntax#p#amsmath#load(cfg) abort " {{{1
call vimtex#syntax#core#new_region_math('align')
call vimtex#syntax#core#new_region_math('alignat')
call vimtex#syntax#core#new_region_math('flalign')
call vimtex#syntax#core#new_region_math('gather')
call vimtex#syntax#core#new_region_math('mathpar')
call vimtex#syntax#core#new_region_math('multline')
call vimtex#syntax#core#new_region_math('xalignat')
call vimtex#syntax#core#new_region_math('xxalignat', {'starred': 0})
for l:env in [
\ 'align',
\ 'alignat',
\ 'flalign',
\ 'gather',
\ 'mathpar',
\ 'multline',
\ 'xalignat',
\]
call vimtex#syntax#core#new_env(#{
\ name: l:env,
\ starred: v:true,
\ math: v:true
\})
endfor

" This does not accept starred variant
call vimtex#syntax#core#new_env({
\ 'name': 'xxalignat',
\ 'math': v:true
\})

syntax match texMathCmdEnv contained contains=texCmdMathEnv nextgroup=texMathArrayArg skipwhite skipnl "\\begin{subarray}"
syntax match texMathCmdEnv contained contains=texCmdMathEnv nextgroup=texMathArrayArg skipwhite skipnl "\\begin{x\?alignat\*\?}"
Expand Down
15 changes: 5 additions & 10 deletions autoload/vimtex/syntax/p/asymptote.vim
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,11 @@
"

function! vimtex#syntax#p#asymptote#load(cfg) abort " {{{1
let l:opts = empty(vimtex#syntax#nested#include('asy'))
\ ? {}
\ : {'contains': '@vimtex_nested_asy'}

call vimtex#syntax#core#new_region_env('texAsymptoteZone', 'asy', l:opts)
call vimtex#syntax#core#new_region_env('texAsymptoteZone', 'asydef', l:opts)

if empty(l:opts)
highlight def link texAsymptoteZone texZone
endif
call vimtex#syntax#core#new_env({
\ 'name': 'asy\(def\)?',
\ 'region': 'texAsymptoteZone',
\ 'nested': 'asy',
\})
endfunction

" }}}1
13 changes: 7 additions & 6 deletions autoload/vimtex/syntax/p/breqn.vim
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
" Email: [email protected]
"

scriptencoding utf-8

function! vimtex#syntax#p#breqn#load(cfg) abort " {{{1
call vimtex#syntax#core#new_region_math('dmath')
call vimtex#syntax#core#new_region_math('dseries')
call vimtex#syntax#core#new_region_math('dgroup')
call vimtex#syntax#core#new_region_math('darray')
for l:env in ['dmath', 'dseries', 'dgroup', 'darray']
call vimtex#syntax#core#new_env(#{
\ name: l:env,
\ starred: v:true,
\ math: v:true
\})
endfor
endfunction

" }}}1
7 changes: 4 additions & 3 deletions autoload/vimtex/syntax/p/cases.vim
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
" Email: [email protected]
"

scriptencoding utf-8

function! vimtex#syntax#p#cases#load(cfg) abort " {{{1
call vimtex#syntax#core#new_region_math('\(sub\)\?numcases', {'starred': 0})
call vimtex#syntax#core#new_env({
\ 'name': '\(sub\)\?numcases',
\ 'math': v:true,
\})
endfunction

" }}}1
6 changes: 4 additions & 2 deletions autoload/vimtex/syntax/p/circuitikz.vim
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ function! vimtex#syntax#p#circuitikz#load(cfg) abort " {{{1
syntax match texTikzEnvBgn "\\begin{circuitikz}"
\ nextgroup=texTikzOpt skipwhite skipnl
\ contains=texCmdEnv
call vimtex#syntax#core#new_region_env('texTikzZone', 'circuitikz', {
call vimtex#syntax#core#new_env({
\ 'name': 'circuitikz',
\ 'region': 'texTikzZone',
\ 'contains': '@texClusterTikz',
\ 'transparent': 1,
\ 'transparent': v:true,
\})
endfunction

Expand Down
8 changes: 5 additions & 3 deletions autoload/vimtex/syntax/p/dot2texi.vim
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
"

function! vimtex#syntax#p#dot2texi#load(cfg) abort " {{{1
call vimtex#syntax#nested#include('dot')
call vimtex#syntax#core#new_region_env('texDotZone', 'dot2tex',
\ {'contains': '@vimtex_nested_dot'})
call vimtex#syntax#core#new_env({
\ 'name': 'dot2tex',
\ 'region': 'texDotZone',
\ 'nested': 'dot'
\})
endfunction

" }}}1
7 changes: 5 additions & 2 deletions autoload/vimtex/syntax/p/empheq.vim
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
"

function! vimtex#syntax#p#empheq#load(cfg) abort " {{{1
call vimtex#syntax#core#new_region_math('empheq', {
\ 'next': 'texEmpheqArg',
call vimtex#syntax#core#new_env({
\ 'name': 'empheq',
\ 'starred': v:true,
\ 'math': v:true,
\ 'mtah_nextgroup': 'texEmpheqArg',
\})
call vimtex#syntax#core#new_arg('texEmpheqArg')

Expand Down
8 changes: 5 additions & 3 deletions autoload/vimtex/syntax/p/gnuplottex.vim
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
"

function! vimtex#syntax#p#gnuplottex#load(cfg) abort " {{{1
call vimtex#syntax#nested#include('gnuplot')
call vimtex#syntax#core#new_region_env('texGnuplotZone', 'gnuplot',
\ {'contains': '@vimtex_nested_gnuplot'})
call vimtex#syntax#core#new_env({
\ 'name': 'gnuplot',
\ 'region': 'texGnuplotZone',
\ 'nested': 'gnuplot'
\})
endfunction

" }}}1
7 changes: 5 additions & 2 deletions autoload/vimtex/syntax/p/ieeetrantools.vim
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ function! vimtex#syntax#p#ieeetrantools#load(cfg) abort " {{{1
call vimtex#syntax#core#new_opt('texMathEnvIEEEOpt',
\ {'next': 'texMathEnvIEEEArg'})
for l:env in ['IEEEeqnarray', 'IEEEeqnarrayboxm']
call vimtex#syntax#core#new_region_math(l:env, {
\ 'next': 'texMathEnvIEEEOpt,texMathEnvIEEEArg',
call vimtex#syntax#core#new_env({
\ 'name': l:env,
\ 'starred': v:true,
\ 'math': v:true,
\ 'math_nextgroup': 'texMathEnvIEEEOpt,texMathEnvIEEEArg',
\})
endfor

Expand Down
9 changes: 6 additions & 3 deletions autoload/vimtex/syntax/p/listings.vim
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ function! vimtex#syntax#p#listings#load(cfg) abort " {{{1
syntax match texLstEnvBgn "\\begin{lstlisting}"
\ nextgroup=texLstOpt skipwhite skipnl contains=texCmdEnv
call vimtex#syntax#core#new_opt('texLstOpt')
call vimtex#syntax#core#new_region_env('texLstZone', 'lstlisting', {
call vimtex#syntax#core#new_env({
\ 'name': 'lstlisting',
\ 'region': 'texLstZone',
\ 'contains': 'texLstEnvBgn',
\})

Expand All @@ -46,14 +48,15 @@ function! vimtex#syntax#p#listings#load(cfg) abort " {{{1
let l:name = toupper(l:nested[0]) . l:nested[1:]
let l:grp = 'texLstZone' . l:name
let l:grp_inline = 'texLstZoneInline' . l:name
let l:cluster = '@' . l:cluster

execute 'syntax match texLstsetArg'
\ '"\c{\_[^}]*language=' . l:nested . '\%(\s*,\|}\)"'
\ 'nextgroup=' . l:grp 'skipwhite skipnl'
\ 'contains=texLstsetArg'

call vimtex#syntax#core#new_region_env(l:grp, 'lstlisting', {
call vimtex#syntax#core#new_env({
\ 'name': 'lstlisting',
\ 'region': l:grp,
\ 'contains': 'texLstEnvBgn,' . l:cluster,
\ 'opts': 'contained',
\})
Expand Down
Loading