xref: /vim-8.2.3635/runtime/indent/tex.vim (revision cc7ff3fc)
1" Vim indent file
2" Language:     LaTeX
3" Maintainer:   YiChao Zhou <broken.zhou AT gmail.com>
4" Created:      Sat, 16 Feb 2002 16:50:19 +0100
5" Last Change:	2012 Mar 18 19:19:50
6" Version: 0.7
7"   Please email me if you found something we can do.  Bug report and
8"   feature request is welcome.
9
10" Last Update:  {{{
11"               25th Sep 2002, by LH :
12"               (*) better support for the option
13"               (*) use some regex instead of several '||'.
14"               Oct 9th, 2003, by JT:
15"               (*) don't change indentation of lines starting with '%'
16"               2005/06/15, Moshe Kaminsky <kaminsky AT math.huji.ac.il>
17"               (*) New variables:
18"                   g:tex_items, g:tex_itemize_env, g:tex_noindent_env
19"               2011/3/6, by Zhou YiChao <broken.zhou AT gmail.com>
20"               (*) Don't change indentation of lines starting with '%'
21"                   I don't see any code with '%' and it doesn't work properly
22"                   so I add some code.
23"               (*) New features: Add smartindent-like indent for "{}" and  "[]".
24"               (*) New variables: g:tex_indent_brace
25"               2011/9/25, by Zhou Yichao <broken.zhou AT gmail.com>
26"               (*) Bug fix: smartindent-like indent for "[]"
27"               (*) New features: Align with "&".
28"               (*) New variable: g:tex_indent_and.
29"               2011/10/23 by Zhou Yichao <broken.zhou AT gmail.com>
30"               (*) Bug fix: improve the smartindent-like indent for "{}" and
31"               "[]".
32"               2012/02/27 by Zhou Yichao <broken.zhou AT gmail.com>
33"               (*) Bug fix: support default folding marker.
34"               (*) Indent with "&" is not very handy.  Make it not enable by
35"               default.
36"               2012/03/06 by Zhou Yichao <broken.zhou AT gmail.com>
37"               (*) Modify "&" behavior and make it default again.  Now "&"
38"               won't align when there are more then one "&" in the previous
39"               line.
40"               (*) Add indent "\left(" and "\right)"
41"               (*) Trust user when in "verbatim" and "lstlisting"
42"               2012/03/11 by Zhou Yichao <broken.zhou AT gmail.com>
43"               (*) Modify "&" so that only indent when current line start with
44"               "&".
45"               2012/03/12 by Zhou Yichao <broken.zhou AT gmail.com>
46"               (*) Modify indentkeys.
47"               2012/03/18 by Zhou Yichao <broken.zhou AT gmail.com>
48"               (*) Add &cpo
49"               2013/05/02 by Zhou Yichao <broken.zhou AT gmail.com>
50"               (*) Fix problem about GetTeXIndent checker. Thank Albert Netymk
51"                   for reporting this.
52" }}}
53
54" Document: {{{
55"
56" To set the following options (ok, currently it's just one), add a line like
57"   let g:tex_indent_items = 1
58" to your ~/.vimrc.
59"
60" * g:tex_indent_brace
61"
62"   If this variable is unset or non-zero, it will use smartindent-like style
63"   for "{}" and "[]"
64"
65" * g:tex_indent_items
66"
67"   If this variable is set, item-environments are indented like Emacs does
68"   it, i.e., continuation lines are indented with a shiftwidth.
69"
70"   NOTE: I've already set the variable below; delete the corresponding line
71"   if you don't like this behaviour.
72"
73"   Per default, it is unset.
74"
75"              set                                unset
76"   ----------------------------------------------------------------
77"       \begin{itemize}                      \begin{itemize}
78"         \item blablabla                      \item blablabla
79"           bla bla bla                        bla bla bla
80"         \item blablabla                      \item blablabla
81"           bla bla bla                        bla bla bla
82"       \end{itemize}                        \end{itemize}
83"
84"
85" * g:tex_items
86"
87"   A list of tokens to be considered as commands for the beginning of an item
88"   command. The tokens should be separated with '\|'. The initial '\' should
89"   be escaped. The default is '\\bibitem\|\\item'.
90"
91" * g:tex_itemize_env
92"
93"   A list of environment names, separated with '\|', where the items (item
94"   commands matching g:tex_items) may appear. The default is
95"   'itemize\|description\|enumerate\|thebibliography'.
96"
97" * g:tex_noindent_env
98"
99"   A list of environment names. separated with '\|', where no indentation is
100"   required. The default is 'document\|verbatim'.
101"
102" * g:tex_indent_and
103"
104"   If this variable is unset or zero, vim will try to align the line with first
105"   "&". This is pretty useful when you use environment like table or align.
106"   Note that this feature need to search back some line, so vim may become
107"   a little slow.
108"
109" }}}
110
111" Only define the function once
112if exists("b:did_indent")
113    finish
114endif
115
116let s:cpo_save = &cpo
117set cpo&vim
118
119" Define global variable {{{
120
121let b:did_indent = 1
122
123if !exists("g:tex_indent_items")
124    let g:tex_indent_items = 1
125endif
126if !exists("g:tex_indent_brace")
127    let g:tex_indent_brace = 1
128endif
129if !exists("g:tex_indent_and")
130    let g:tex_indent_and = 1
131endif
132if g:tex_indent_items
133    if !exists("g:tex_itemize_env")
134        let g:tex_itemize_env = 'itemize\|description\|enumerate\|thebibliography'
135    endif
136    if !exists('g:tex_items')
137        let g:tex_items = '\\bibitem\|\\item'
138    endif
139else
140    let g:tex_items = ''
141endif
142
143if !exists("g:tex_indent_paretheses")
144    let g:tex_indent_paretheses = 1
145endif
146
147if !exists("g:tex_noindent_env")
148    let g:tex_noindent_env = 'document\|verbatim\|lstlisting'
149endif "}}}
150
151" VIM Setting " {{{
152setlocal autoindent
153setlocal nosmartindent
154setlocal indentexpr=GetTeXIndent()
155setlocal indentkeys&
156exec 'setlocal indentkeys+=[,(,{,),},],\&' . substitute(g:tex_items, '^\|\(\\|\)', ',=', 'g')
157let g:tex_items = '^\s*' . substitute(g:tex_items, '^\(\^\\s\*\)*', '', '')
158" }}}
159
160function! GetTeXIndent() " {{{
161    " Find a non-blank line above the current line.
162    let lnum = prevnonblank(v:lnum - 1)
163
164    " Comment line is not what we need.
165    while lnum != 0 && getline(lnum) =~ '^\s*%'
166        let lnum = prevnonblank(lnum - 1)
167    endwhile
168
169    " At the start of the file use zero indent.
170    if lnum == 0
171        return 0
172    endif
173
174    let line = substitute(getline(lnum), '%.*', ' ','g')     " last line
175    let cline = substitute(getline(v:lnum), '%.*', ' ', 'g') " current line
176
177    "  We are in verbatim, so do what our user what.
178    if synIDattr(synID(v:lnum, indent(v:lnum), 1), "name") == "texZone"
179        if empty(cline)
180            return indent(lnum)
181        else
182            return indent(v:lnum)
183        end
184    endif
185
186    " You want to align with "&"
187    if g:tex_indent_and
188        " Align only when current line start with "&"
189        if line =~ '&.*\\\\' && cline =~ '^\s*&'
190            return indent(v:lnum) + stridx(line, "&") - stridx(cline, "&")
191        endif
192
193        " set line & lnum to the line which doesn't contain "&"
194        while lnum != 0 && (stridx(line, "&") != -1 || line =~ '^\s*%')
195            let lnum = prevnonblank(lnum - 1)
196            let line = getline(lnum)
197        endwhile
198    endif
199
200
201    if lnum == 0
202        return 0
203    endif
204
205    let ind = indent(lnum)
206
207    " New code for comment: retain the indent of current line
208    if cline =~ '^\s*%'
209        return indent(v:lnum)
210    endif
211
212    " Add a 'shiftwidth' after beginning of environments.
213    " Don't add it for \begin{document} and \begin{verbatim}
214    ""if line =~ '^\s*\\begin{\(.*\)}'  && line !~ 'verbatim'
215    " LH modification : \begin does not always start a line
216    " ZYC modification : \end after \begin won't cause wrong indent anymore
217    if line =~ '\\begin{.*}' && line !~ g:tex_noindent_env
218        let ind = ind + &sw
219
220        if g:tex_indent_items
221            " Add another sw for item-environments
222            if line =~ g:tex_itemize_env
223                let ind = ind + &sw
224            endif
225        endif
226    endif
227
228    " Subtract a 'shiftwidth' when an environment ends
229    if cline =~ '\\end{.*}' && cline !~ g:tex_noindent_env
230
231        if g:tex_indent_items
232            " Remove another sw for item-environments
233            if cline =~ g:tex_itemize_env
234                let ind = ind - &sw
235            endif
236        endif
237
238        let ind = ind - &sw
239    endif
240
241    if g:tex_indent_brace
242        let sum1 = 0
243        for i in range(0, strlen(line)-1)
244            if line[i] == "}" || line[i] == "]" ||
245                        \ strpart(line, i, 7) == '\right)'
246                let sum1 = max([0, sum1-1])
247            endif
248            if line[i] == "{" || line[i] == "[" ||
249                        \ strpart(line, i, 6) == '\left('
250                let sum1 += 1
251            endif
252        endfor
253
254        let sum2 = 0
255        for i in reverse(range(0, strlen(cline)-1))
256            if cline[i] == "{" || cline[i] == "[" ||
257                        \ strpart(cline, i, 6) == '\left('
258                let sum2 = max([0, sum2-1])
259            endif
260            if cline[i] == "}" || cline[i] == "]" ||
261                        \ strpart(cline, i, 7) == '\right)'
262                let sum2 += 1
263            endif
264        endfor
265
266        let ind += (sum1 - sum2) * &sw
267    endif
268
269    if g:tex_indent_paretheses
270    endif
271
272    " Special treatment for 'item'
273    " ----------------------------
274
275    if g:tex_indent_items
276
277        " '\item' or '\bibitem' itself:
278        if cline =~ g:tex_items
279            let ind = ind - &sw
280        endif
281
282        " lines following to '\item' are intented once again:
283        if line =~ g:tex_items
284            let ind = ind + &sw
285        endif
286
287    endif
288
289    return ind
290endfunction "}}}
291
292let &cpo = s:cpo_save
293unlet s:cpo_save
294
295" vim: set sw=4 textwidth=80:
296