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
7 " Please email me if you found something we can do. Bug report and
8 " feature request is welcome.
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>
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
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
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
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
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
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>
49 " 2013/05/02 by Zhou Yichao <broken.zhou AT gmail.com>
50 " (*) Fix problem about GetTeXIndent checker. Thank Albert Netymk
56 " To set the following options (ok, currently it's just one), add a line like
57 " let g:tex_indent_items = 1
60 " * g:tex_indent_brace
62 " If this variable is unset or non-zero, it will use smartindent-like style
65 " * g:tex_indent_items
67 " If this variable is set, item-environments are indented like Emacs does
68 " it, i.e., continuation lines are indented with a shiftwidth.
70 " NOTE: I've already set the variable below; delete the corresponding line
71 " if you don't like this behaviour.
73 " Per default, it is 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}
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'.
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'.
97 " * g:tex_noindent_env
99 " A list of environment names. separated with '\|', where no indentation is
100 " required. The default is 'document\|verbatim'.
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
111 " Only define the function once
112 if exists("b:did_indent")
116 let s:cpo_save = &cpo
119 " Define global variable {{{
123 if !exists("g:tex_indent_items")
124 let g:tex_indent_items = 1
126 if !exists("g:tex_indent_brace")
127 let g:tex_indent_brace = 1
129 if !exists("g:tex_indent_and")
130 let g:tex_indent_and = 1
132 if g:tex_indent_items
133 if !exists("g:tex_itemize_env")
134 let g:tex_itemize_env = 'itemize\|description\|enumerate\|thebibliography'
136 if !exists('g:tex_items')
137 let g:tex_items = '\\bibitem\|\\item'
143 if !exists("g:tex_indent_paretheses")
144 let g:tex_indent_paretheses = 1
147 if !exists("g:tex_noindent_env")
148 let g:tex_noindent_env = 'document\|verbatim\|lstlisting'
153 setlocal nosmartindent
154 setlocal indentexpr=GetTeXIndent()
156 exec 'setlocal indentkeys+=[,(,{,),},],\&' . substitute(g:tex_items, '^\|\(\\|\)', ',=', 'g')
157 let g:tex_items = '^\s*' . substitute(g:tex_items, '^\(\^\\s\*\)*', '', '')
160 function! GetTeXIndent() " {{{
161 " Find a non-blank line above the current line.
162 let lnum = prevnonblank(v:lnum - 1)
164 " Comment line is not what we need.
165 while lnum != 0 && getline(lnum) =~ '^\s*%'
166 let lnum = prevnonblank(lnum - 1)
169 " At the start of the file use zero indent.
174 let line = substitute(getline(lnum), '%.*', ' ','g') " last line
175 let cline = substitute(getline(v:lnum), '%.*', ' ', 'g') " current line
177 " We are in verbatim, so do what our user what.
178 if synIDattr(synID(v:lnum, indent(v:lnum), 1), "name") == "texZone"
182 return indent(v:lnum)
186 " You want to align with "&"
188 " Align only when current line start with "&"
189 if line =~ '&.*\\\\' && cline =~ '^\s*&'
190 return indent(v:lnum) + stridx(line, "&") - stridx(cline, "&")
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)
205 let ind = indent(lnum)
207 " New code for comment: retain the indent of current line
209 return indent(v:lnum)
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
220 if g:tex_indent_items
221 " Add another sw for item-environments
222 if line =~ g:tex_itemize_env
228 " Subtract a 'shiftwidth' when an environment ends
229 if cline =~ '\\end{.*}' && cline !~ g:tex_noindent_env
231 if g:tex_indent_items
232 " Remove another sw for item-environments
233 if cline =~ g:tex_itemize_env
241 if g:tex_indent_brace
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])
248 if line[i] == "{" || line[i] == "[" ||
249 \ strpart(line, i, 6) == '\left('
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])
260 if cline[i] == "}" || cline[i] == "]" ||
261 \ strpart(cline, i, 7) == '\right)'
266 let ind += (sum1 - sum2) * &sw
269 if g:tex_indent_paretheses
272 " Special treatment for 'item'
273 " ----------------------------
275 if g:tex_indent_items
277 " '\item' or '\bibitem' itself:
278 if cline =~ g:tex_items
282 " lines following to '\item' are intented once again:
283 if line =~ g:tex_items
292 let &cpo = s:cpo_save
295 " vim: set sw=4 textwidth=80: