2011-10-04 67 views
2

我切換到vim-latex並有以下問題:我經常通過\newcommand定義新的便捷命令以便於編輯。我自己的命令通常需要2個或更多的參數。vim-latex:自動識別自定義命令

因此,現在假設我創建了一個需要3個參數的命令mycommand

有沒有辦法來告訴VIM乳膠自動識別我的自定義命令,讓我可以簡單地輸入mycommand並按<F7>(或任何等價物)和VIM會自動將其轉換爲\mycommand{<++>}{<++>}{<++>}<++>

注:我知道Tex_Com_name,但由於我經常創建新的命令,我不想一直這樣做。

+0

你應該能夠定義自動替換規則與'\ {mycommand的<++>} {<++>} {} <++>'<++>更換'\ mycommand'。無論如何,這個問題可能會在你的姐妹網站http://superuser.com上更好,因爲它是關於Vim而不是LaTeX本身,即使連接是清楚的。 –

回答

0

因爲這似乎是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()