Skip to content

Commit 976b10b

Browse files
committed
syntax: Prevent infinite recursion
See vim/vim#17687 (comment).
1 parent 7f5cefc commit 976b10b

File tree

1 file changed

+37
-2
lines changed

1 file changed

+37
-2
lines changed

syntax/erlang.vim

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,33 @@ if exists("b:current_syntax")
3030
finish
3131
endif
3232

33+
if !exists('g:main_syntax')
34+
" This is an Erlang source file, and this is the main execution of
35+
" syntax/erlang.vim.
36+
let g:main_syntax = 'erlang'
37+
elseif g:main_syntax == 'erlang'
38+
" This is an Erlang source file, and this is an inner execution of
39+
" syntax/erlang.vim. For example:
40+
"
41+
" 1. The main execution of syntax/erlang.vim included syntax/markdown.vim
42+
" because "g:erlang_use_markdown_for_docs == 1".
43+
"
44+
" 2. syntax/markdown.vim included syntax/erlang.vim because
45+
" "g:markdown_fenced_languages == ['erlang']". This is the inner
46+
" execution of syntax/erlang.vim.
47+
"
48+
" To avoid infinite recursion with Markdown and Erlang including each other,
49+
" and to avoid the inner syntax/erlang.vim execution messing up the
50+
" variables of the outer erlang.vim execution, we finish executing the inner
51+
" erlang.vim.
52+
"
53+
" In the inner execution, we already have the Erlang syntax items included,
54+
" so the highlighting of Erlang within Markdown within Erlang will be
55+
" acceptable. It won't highlight Markdown inside Erlang inside Markdown
56+
" inside Erlang.
57+
finish
58+
endif
59+
3360
let s:cpo_save = &cpo
3461
set cpo&vim
3562

@@ -50,7 +77,12 @@ let s:old_style = (exists("g:erlang_old_style_highlight") &&
5077
"
5178
" * "g:erlang_use_markdown_for_docs == 0" (default): Disable Markdown
5279
" highlighting in docstrings.
53-
if exists("g:erlang_use_markdown_for_docs")
80+
"
81+
" If "g:main_syntax" is not 'erlang', this is not an Erlang source file but
82+
" for example a Markdown file, and syntax/markdown.vim is including
83+
" syntax/erlang.vim. To avoid infinite recursion with Markdown and Erlang
84+
" including each other, we disable sourcing syntax/markdown.vim in this case.
85+
if exists("g:erlang_use_markdown_for_docs") && g:main_syntax == 'erlang'
5486
let s:use_markdown = g:erlang_use_markdown_for_docs
5587
else
5688
let s:use_markdown = 0
@@ -364,9 +396,12 @@ hi def link erlangExtra Statement
364396
hi def link erlangSignal Statement
365397
endif
366398

367-
368399
let b:current_syntax = "erlang"
369400

401+
if g:main_syntax ==# 'erlang'
402+
unlet g:main_syntax
403+
endif
404+
370405
let &cpo = s:cpo_save
371406
unlet s:cpo_save
372407

0 commit comments

Comments
 (0)