使用vims tagbar插件来处理latex文件

mav*_*ves 2 vim latex ctags tagbar osx-mavericks

我目前正在尝试使用vim的tagbar插件来处理Mac OS X上的latex文件.

这是我的〜/ .ctags文件:

--langdef=latex
--langmap=latex:.tex
--regex-latex=/^\\tableofcontents/TABLE OF CONTENTS/s,toc/
--regex-latex=/^\\frontmatter/FRONTMATTER/s,frontmatter/
--regex-latex=/^\\mainmatter/MAINMATTER/s,mainmatter/
--regex-latex=/^\\backmatter/BACKMATTER/s,backmatter/
--regex-latex=/^\\bibliography\{/BIBLIOGRAPHY/s,bibliography/
--regex-latex=/^\\part[[:space:]]*(\[[^]]*\])?[[:space:]]*\{([^}]+)\}/PART \2/s,part/
--regex-latex=/^\\part[[:space:]]*\*[[:space:]]*\{([^}]+)\}/PART \1/s,part/
--regex-latex=/^\\chapter[[:space:]]*(\[[^]]*\])?[[:space:]]*\{([^}]+)\}/CHAP \2/s,chapter/
--regex-latex=/^\\chapter[[:space:]]*\*[[:space:]]*\{([^}]+)\}/CHAP \1/s,chapter/
--regex-latex=/^\\section[[:space:]]*(\[[^]]*\])?[[:space:]]*\{([^}]+)\}/\. \2/s,section/
--regex-latex=/^\\section[[:space:]]*\*[[:space:]]*\{([^}]+)\}/\. \1/s,section/
--regex-latex=/^\\subsection[[:space:]]*(\[[^]]*\])?[[:space:]]*\{([^}]+)\}/\.\. \2/s,subsection/
--regex-latex=/^\\subsection[[:space:]]*\*[[:space:]]*\{([^}]+)\}/\.\. \1/s,subsection/
--regex-latex=/^\\subsubsection[[:space:]]*(\[[^]]*\])?[[:space:]]*\{([^}]+)\}/\.\.\. \2/s,subsubsection/
--regex-latex=/^\\subsubsection[[:space:]]*\*[[:space:]]*\{([^}]+)\}/\.\.\. \1/s,subsubsection/
--regex-latex=/^\\includegraphics[[:space:]]*(\[[^]]*\])?[[:space:]]*(\[[^]]*\])?[[:space:]]*\{([^}]+)\}/\3/g,graphic+listing/
--regex-latex=/^\\lstinputlisting[[:space:]]*(\[[^]]*\])?[[:space:]]*(\[[^]]*\])?[[:space:]]*\{([^}]+)\}/\3/g,graphic+listing/
--regex-latex=/\\label[[:space:]]*\{([^}]+)\}/\1/l,label/
--regex-latex=/\\ref[[:space:]]*\{([^}]+)\}/\1/r,ref/
--regex-latex=/\\pageref[[:space:]]*\{([^}]+)\}/\1/p,pageref/
Run Code Online (Sandbox Code Playgroud)

这是我的〜/ .vimrc中的相应部分:

let g:tagbar_type_tex = {
    \ 'ctagstype' : 'latex',
    \ 'kinds'     : [
        \ 's:sections',
        \ 'g:graphics:0:0',
        \ 'l:labels',
        \ 'r:refs:1:0',
        \ 'p:pagerefs:1:0'
    \ ],
    \ 'sort'    : 0,
\ }
Run Code Online (Sandbox Code Playgroud)

我基本上从这个链接得到了所有这些:https://github.com/vim-scripts/Tagbar/blob/master/doc/tagbar.txt

不幸的是,当我启动标签栏时,没有任何反应,当我执行时:UpdateTags我收到以下错误:

easytags.vim 3.7: Exuberant Ctags doesn't support the 'plaintex' file type! (at function xolox#easytags#update..<SNR>20_check_cfile, line 22)
Run Code Online (Sandbox Code Playgroud)

ctags --version导致:

Exuberant Ctags 5.8, Copyright (C) 1996-2009 Darren Hiebert
  Compiled: Oct  1 2014, 16:18:15
  Addresses: <dhiebert@users.sourceforge.net>, http://ctags.sourceforge.net
  Optional compiled features: +wildcards, +regex
Run Code Online (Sandbox Code Playgroud)

和"哪个ctags"在:

/usr/local/bin/ctags
Run Code Online (Sandbox Code Playgroud)

当我执行ctags --verbose abstract.tex时,它会找到〜/ .ctags文件并生成此标记文件:

!_TAG_FILE_FORMAT       2       /extended format; --format=1 will not append ;" to lines/
!_TAG_FILE_SORTED       1       /0=unsorted, 1=sorted, 2=foldcase/
!_TAG_PROGRAM_AUTHOR    Darren Hiebert  /dhiebert@users.sourceforge.net/
!_TAG_PROGRAM_NAME      Exuberant Ctags //
!_TAG_PROGRAM_URL       http://ctags.sourceforge.net    /official site/
!_TAG_PROGRAM_VERSION   5.8     //
. Abstract      abstract.tex    /^\\section*{Abstract}$/;"      s
Run Code Online (Sandbox Code Playgroud)

文件本身如下所示:

\section*{Abstract}

Let's see what we can do here...
Run Code Online (Sandbox Code Playgroud)

我错过了什么?:(

亲切的问候,谢谢你的帮助:)

rom*_*inl 5

EasyTags问题

  1. EasyTags使用当前缓冲区的文件类型来确定它应传递给哪些参数ctags.

  2. filetype文件被设置为plaintex通过Vim的,默认的*.tex文件.所以......你得到了那个错误,因为plaintex没有得到认可ctags.

Tagbar问题

  1. 根据文档,您应该filetype在自定义配置中使用:

    g:tagbar_type_{vim filetype}
    
    Run Code Online (Sandbox Code Playgroud)
  2. 由于您的*.tex文件被识别为plaintex,您g:tagbar_type_tex将永远不会被使用.

一个自由的解决方案

  1. 将这些行添加到您的位置~/.vimrc以强制Vim将*.tex文件识别为latex:

    augroup latex
        autocmd!
        autocmd BufRead,BufNewFile *.tex set filetype=latex
    augroup END
    
    Run Code Online (Sandbox Code Playgroud)
  2. 从以下位置更改自定义Tagbar配置:

    g:tagbar_type_tex
    
    Run Code Online (Sandbox Code Playgroud)

    至:

    g:tagbar_type_latex
    
    Run Code Online (Sandbox Code Playgroud)

只有当Vim配置为与latex文件类型(ftplugin,syntax,indent ...)配合良好时,此解决方案才有效,这在最好情况下是可疑的.

一个保守的解决方案

  1. 使用plaintex而不是latex在您的~/.ctags配置中:

    --langdef=plaintex
    --langmap=plaintex:.tex
    --regex-plaintex=/^\\tableofcontents/TABLE OF CONTENTS/s,toc/
    ...
    
    Run Code Online (Sandbox Code Playgroud)
  2. 从以下位置更改自定义Tagbar配置:

    let g:tagbar_type_tex = {
        \ 'ctagstype' : 'latex',
        ...
    
    Run Code Online (Sandbox Code Playgroud)

    至:

    let g:tagbar_type_plaintex = {
        \ 'ctagstype' : 'plaintex',
        ...
    
    Run Code Online (Sandbox Code Playgroud)