vim-latex:自动识别自定义命令

发布于 2024-12-07 22:54:08 字数 522 浏览 0 评论 0原文

我切换到 vim-latex 并遇到以下问题:我经常定义新的方便命令,以便通过 \newcommand 更轻松地进行编辑。我自己的命令通常带有 2 个或更多参数。

现在我们假设我创建了一个带有 3 个参数的命令 mycommand

有没有办法告诉 vim-latex 自动识别我的自定义命令,这样我就可以简单地输入 mycommand 并按 (或任何等效的东西)和 vim自动将其转换为 \mycommand{<++>}{<++>}{<++>}<++>

注意:我知道 Tex_Com_name ,但由于我经常创建新命令,所以我不想一直这样做。

I switched to vim-latex and have the following issue: I frequently define new convenient commands for easier editing via \newcommand. My own commands usually take 2 or more parameters.

So let's for now assume I created a command mycommand that takes 3 parameters.

Is there a way to tell vim-latex to automatically recognize my custom commands, so that I can simply type mycommand and press <F7> (or anything equivalent) and vim automatically converts this to \mycommand{<++>}{<++>}{<++>}<++>?

Note: I know about Tex_Com_name, but since I create new commands that often, I don't want to do this all the time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

放飞的风筝 2024-12-14 22:54:08

由于这似乎是 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()

Since this seems to be a nonexistent feature in vim, I've created it myself. I didn't do in-depth tests, but it seems to work quite well so far.

" 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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文