I am a recent and very happy Vim User. I write Mathematics and Computer Science Lecture Notes in Markdown Files, organized in a Vimwiki. I use the UltiSnips Snippet Plugin.
This plugin provides me with the possibility of defining snippets which only expand in a TexMathZone
("UltiSnips-context-snippets").
This happens through a Python function (see [1] below) returning true if the cursor is in a TexMathZone
. That means that snippet expands when within $
or $$
delimiters (TexMathZoneX
, TexMathZoneY
).
This works perfectly in .tex
Files, but not in .md
Files.
Setting the syntax to Tex by :set syntax=tex
makes it work, but breaks markdown preview and syntax highlighting, which is unwanted.
I suspect this is because the syntax file for the markdown filetype does not define TexMathZone
How can I define or extend a markdown syntax file, such that the function in [1] delivers the same behaviour in markdown files as in Tex files? It should not inherit the other Tex syntax, and it should keep markdown syntax.
References:
[1] Minimal markdown.snippets
file, as seen on Gilles Castel's Snippet Configuration File :
global !p
texMathZones = ['texMathZone' + x for x in ['A', 'AS', 'B', 'BS', 'C', 'CS', 'D', 'DS', 'E', 'ES', 'F', 'FS', 'G', 'GS', 'H', 'HS', 'I', 'IS', 'J', 'JS', 'K', 'KS', 'L', 'LS', 'DS', 'V', 'W', 'X', 'Y', 'Z', 'AmsA', 'AmsB', 'AmsC', 'AmsD', 'AmsE', 'AmsF', 'AmsG', 'AmsAS', 'AmsBS', 'AmsCS', 'AmsDS', 'AmsES', 'AmsFS', 'AmsGS' ]]
texIgnoreMathZones = ['texMathText']
texMathZoneIds = vim.eval('map('+str(texMathZones)+", 'hlID(v:val)')")
texIgnoreMathZoneIds = vim.eval('map('+str(texIgnoreMathZones)+", 'hlID(v:val)')")
ignore = texIgnoreMathZoneIds[0]
def math():
synstackids = vim.eval("synstack(line('.'), col('.') - (col('.')>=2 ? 1 : 0))")
try:
first = next(i for i in reversed(synstackids) if i in texIgnoreMathZoneIds or i in texMathZoneIds)
return first != ignore
except StopIteration:
return False
endglobal
context "math()"
snippet t "test" iAe
test!
endsnippet