Поскольку в vim эта функция отсутствует, я создал ее сам. Я не проводил углубленных тестов, но, похоже, до сих пор он работал довольно хорошо.
" latex_helper.vim
function! GetCustomLatexCommands()
python << EOP
import os
import os.path
import re
def readFile(p):
"""Reads a file and extracts custom commands"""
f = open(p)
commands = []
for _line in f:
line = _line.strip()
# search for included files
tmp = re.search(r"(input|include){(.*)}", line)
if tmp != None:
path = tmp.group(2)
newpath = os.path.join(os.path.dirname(p), path)
if os.path.exists(newpath) and os.path.isfile(newpath):
commands.extend(readFile(newpath))
elif os.path.exists(newpath+".tex") and os.path.isfile(newpath+".tex"):
commands.extend(readFile(newpath+".tex"))
tmp = re.search(r"newcommand{(.*?)}\[(.*?)\]", line)
if tmp != None:
cmd = tmp.group(1)
argc = int(tmp.group(2))
commands.append((cmd[1:], argc))
return commands
def getMain(path, startingpoint = None):
"""Goes folders upwards until it finds a *.latexmain file"""
if startingpoint==None:
startingpoint = path
files = []
if os.path.isdir(path):
files = os.listdir(path)
files = [os.path.join(path, s) for s in files if s.split(".")[-1] == "latexmain"]
if len(files) >= 1:
return os.path.splitext(files[0])[0]
if os.path.dirname(path) != path:
return getMain(os.path.dirname(path), startingpoint)
return startingpoint
def GetCustomLatexCommands():
"""Reads all custom commands and adds them to givm"""
import vim
cmds = readFile(getMain(vim.current.buffer.name))
for (cmd, argc) in cmds:
vim.command('let g:Tex_Com_%s="\\\\%s%s <++>"'%(cmd, cmd, "{<++>}"*argc))
GetCustomLatexCommands()
EOP
endfunction
autocmd BufRead *.tex :call GetCustomLatexCommands()