vim-LaTeX 更改当前环境

vim-LaTeX 更改当前环境

我使用 vim-LaTeX-suite 作为我的编辑器,我真的很喜欢它,但是我想知道如何改变周围的环境,例如如下:

假设你处于以下情况

% Preamble stuff
\begin{document}
%
\begin{itemize}
\item Who forgot the cake
    \begin{itemize}
        \item Albert
        \item Bob
        \item Robert
    \end{itemize}
\item what cakes do we like
    \begin{itemize}
         \item bananna
         \item chocklate
     \end{itemize}
\item who should bring the cake next week
     \begin{itemize}
        \item Albert
        \item Bob
        \item Robert
     \end{itemize}
\end{itemize}
%
\end{document}

并且您位于(中间)线上\item what cakes do we like,并且想要将最外层的 itemize 环境更改为 enumerate 环境。您该怎么做?

答案1

ViM-LaTeX 附带的脚本本身仅查找最内层环境。但是,您可以使用以下脚本对最外层执行相同操作

function! Tex_ChangeOutestEnvironments() 

  let env_line = searchpair('\\begin{\(document\)\@!', '', '\\end{\(document\)\@!', "bnr")

  if env_line != 0
      let env_name = matchstr(getline(env_line), 'begin{\zs.\{-}\ze}')
  endif

  if !exists('env_name')
    echomsg "You are not inside environment"
    return 0
  endif

  exe 'echomsg "You are within a '.env_name.' environment."'
  let change_env = PromptForEnvironment('What do you want to change it to? ')

  if change_env == 'eqnarray'
    call <SID>Change('eqnarray', 1, '', env_name =~ '\*$')
  elseif change_env == 'align'
    call <SID>Change('align', 1, '', env_name =~ '\*$')
  elseif change_env == 'eqnarray*'
    call <SID>Change('eqnarray*', 0, '\\nonumber', 0)
  elseif change_env == 'align*'
    call <SID>Change('align*', 0, '\\nonumber', 0)
  elseif change_env == 'equation*'
    call <SID>Change('equation*', 0, '&\|\\lefteqn{\|\\nonumber\|\\\\', 0)
  elseif change_env == ''
    return 0
  else
    call <SID>Change(change_env, 0, '', '')
    return 0
  endif

endfunction 

function! <SID>Change(env, label, delete, putInNonumber)

  let start_line = line('.')
  let start_col = virtcol('.')

    let first = '\begin{' . a:env . '}'
    let second = '\end{' . a:env . '}'

    let bottom = searchpair('\\begin{\(document\)\@!','','\\end{\(document\)\@!','r')
    s/\\end{.\{-}}/\=second/
    let top = searchpair('\\begin{\(document\)\@!','','\\end{\(document\)\@!','br')
    s/\\begin{.\{-}}/\=first/

  if a:delete != ''
    exe 'silent '. top . "," . bottom . 's/' . a:delete . '//e'
  endif

  if a:putInNonumber == 1
    exe top
    call search('\\end\|\\\\')
    if line('.') != bottom
      exe '.+1,' . bottom . 's/\\\\/\\nonumber\\\\/e'
      exe (bottom-1) . 's/\s*$/  \\nonumber/'
    endif
  endif

  if a:label == 1
    exe top
    if search("\\label", "W") > bottom
      exe top
      let local_label = input('Label? ')
      if local_label != ''
    put = '\label{'.local_label.'}'
      endif
      normal $
    endif
  else
    exe 'silent '.top . ',' . bottom . ' g/\\label/delete'
  endif

  if exists('local_label') && local_label != ''
    exe start_line + 1.' | normal! '.start_col.'|'
  else
    exe start_line.' | normal! '.start_col.'|'
  endif
endfunction 

它是原始版本的编辑版本,利用函数中的“r”标志searchpair来查找最外层的对,并排除 document这也是一个环境。

目前它不支持改变显示数学环境,因为它们给我带来了错误。

为了使用此功能,您应该将其保存为文件并在某个时候获取它,然后设置映射以使用它。也许

map <C-S-F5> :call Tex_ChangOutestEnvironments()<CR>

将其映射到 control-shift-f5。

相关内容