2011-10-04 4 views
2

vim-latex로 전환하고 다음과 같은 문제가 있습니다. \newcommand를 통해보다 쉽게 ​​편집 할 수있는 새로운 편리한 명령을 자주 정의합니다. 내 명령은 보통 두 개 이상의 매개 변수를 사용합니다.vim-latex : 사용자 정의 명령을 자동으로 인식합니다.

이제 3 개의 매개 변수를 취하는 mycommand 명령을 작성했다고 가정 해 보겠습니다.

vim-latex에게 내 사용자 지정 명령을 자동으로 인식하도록 지시하는 방법이 있습니까? 그렇기 때문에 mycommand을 입력하고 <F7> (또는 이와 비슷한 모든 항목)를 누르면 vim이 자동으로 \mycommand{<++>}{<++>}{<++>}<++>으로 변환 할 수 있습니까?

참고 : Tex_Com_name에 대해 알고 있습니다. 그러나 자주 새 명령을 작성하기 때문에 항상이 작업을 수행하고 싶지는 않습니다.

+0

당신은 \의 mycommand {<++>} {<++>} {<++>} <++>''와'\ mycommand'를 대체하기 위해 자동 대체 규칙을 정의 할 수 있어야한다. 이 빔이 연결이 분명하다해도, 그 자체 라텍스하지에 대해 때문에 어쨌든,이 질문은 자매 사이트 http://superuser.com에 더 잘 될 수 있습니다. –

답변

0

, 내가 직접 만들었습니다. 나는 심도있는 테스트를하지 않았다, 그러나 지금까지 아주 잘 작동하는 것 같다.

" 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() 
관련 문제