vim-endwise
vim-endwise copied to clipboard
Closing latex environments
I was trying to add latex environment to endwise by adding the following code:
let b:endwise_addition = '\\end{\=submatch(1)}'
let b:endwise_addition = 'end'
let b:endwise_words = 'begin'
let b:endwise_pattern = '^\s*\\begin{\(.\+\)}.*$'
let b:endwise_syngroups = 'texBeginEndName'
to ftplugin/tex.vim and it fails.
I guess I completely do not understand how configuration is supposed to be done. The idea is to close the following block:
\begin{env}[optional]{parameters}
\end{env}
The block may be indented. Is it possible to add the latex support to the endwise?
Thanks!
So far after some playing I was able to figure out that the following sequence does the job:
let b:endwise_words = '\\begin{\zs[a-zA-Z0-9*]*\ze}' " ignored
let b:endwise_pattern = '\\begin{\zs[a-zA-Z0-9*]*\ze}'
let b:endwise_addition = '\\end{&}'
let b:endwise_syngroups = 'texBeginEndName'
But it doesn't prevent endwise to add the ending even if it already exists. For now I don't know how it works and how it can be fixed.
Nevertheless, this could for better or worse be included in the standard plugin.
Where did texBeginEndName
come from? I see texSection
(though this might be version dependent).
It's from vim's tex syntax file. vim 7.3.1287
" Vim syntax file " Language: TeX " Maintainer: Charles E. Campbell [email protected] " Last Change: Mar 11, 2013 " Version: 78 " URL: http://www.drchip.org/astronaut/vim/index.html#SYNTAX_TEX
That's a curious inconsistency with both 7.3.000 and 7.4.000 but doesn't appear to be relevant. b:endwise_words
needs to be a comma delimited list of words. You might have better luck if you start by trying to target a few specific environments. I doubt a general solution is possible without tweaking the rest of the implementation.
The example given by @maxfl doesn't seem to work for me. This would be very cool to have working somehow in any case.
I can confirm @maxfl example works and the problem with preventing extra endings exists. There's two factors affecting this.
Firstly, b:endwise_addition
is not escaped when endpat
is generated so the double-slashes in \\end
becomes \end
when doing searchpair
.
Secondly, in Latex's syntax highlighting, \begin{foo}
has two different syntax groups, \begin
having texBeginEnd
and foo
with texBeginEndName
. My current method is to introduce a new variable b:endwise_endsyngroups
which when defined, overrides b:endwise_syngroups
in mysearchpair
.
Another minor issue is that the \begin
has a lot of environments, so a new tag may match an already existing, but different tag. I believe it is possible to generate the exact end pattern from word
when doing searchpos
.
This has been working for me:
autocmd FileType tex
\ let b:endwise_addition = '\="\\end" . matchstr(submatch(0), "{.\\{-}}")' |
\ let b:endwise_words = 'begin' |
\ let b:endwise_pattern = '\\begin{.\{-}}' |
\ let b:endwise_syngroups = 'texSection,texBeginEnd,texBeginEndName,texStatement'
Edit: you can do \begin{env*} now too.
Thanks @elliotpatros! That works for me :)