vim-endwise icon indicating copy to clipboard operation
vim-endwise copied to clipboard

Closing latex environments

Open maxfl opened this issue 11 years ago • 9 comments

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!

maxfl avatar Nov 13 '13 03:11 maxfl

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.

maxfl avatar Nov 17 '13 02:11 maxfl

Nevertheless, this could for better or worse be included in the standard plugin.

ghost avatar Mar 27 '14 11:03 ghost

Where did texBeginEndName come from? I see texSection (though this might be version dependent).

tpope avatar Mar 27 '14 16:03 tpope

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

maxfl avatar Mar 27 '14 17:03 maxfl

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.

tpope avatar Mar 27 '14 21:03 tpope

The example given by @maxfl doesn't seem to work for me. This would be very cool to have working somehow in any case.

michaelmior avatar Aug 11 '14 16:08 michaelmior

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.

gelguy avatar Feb 28 '15 02:02 gelguy

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.

elliotpatros avatar Oct 25 '18 03:10 elliotpatros

Thanks @elliotpatros! That works for me :)

michaelmior avatar Oct 25 '18 06:10 michaelmior