vim-latex: автоматически распознает пользовательские команды - PullRequest
2 голосов
/ 04 октября 2011

Я переключился на vim-latex, и у меня возникла следующая проблема: я часто определяю новые удобные команды для более удобного редактирования с помощью \newcommand. Мои собственные команды обычно принимают 2 или более параметров.

Итак, давайте пока предположим, что я создал команду mycommand, которая принимает 3 параметра.

Есть ли способ указать vim-latex, чтобы он автоматически распознавал мои пользовательские команды, чтобы я мог просто набрать mycommand и нажать <F7> (или любой другой эквивалент), и vim автоматически преобразует это в \mycommand{<++>}{<++>}{<++>}<++>?

Примечание: я знаю о Tex_Com_name, но так как я часто создаю новые команды, я не хочу делать это все время.

1 Ответ

0 голосов
/ 06 октября 2011

Поскольку в 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()
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...