blob: 7e3a3510832eb6ef899928381d3ddaf73acae3ef [file] [log] [blame]
Bram Moolenaarf1568ec2011-12-14 21:17:39 +01001" Vim indent file
2" Language: LaTeX
Bram Moolenaarad3b3662013-05-17 18:14:19 +02003" Maintainer: YiChao Zhou <broken.zhou AT gmail.com>
Bram Moolenaarf1568ec2011-12-14 21:17:39 +01004" Created: Sat, 16 Feb 2002 16:50:19 +0100
Bram Moolenaar61d35bd2012-03-28 20:51:51 +02005" 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 :
Bram Moolenaarf1568ec2011-12-14 21:17:39 +010012" (*) 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 '%'
Bram Moolenaar61d35bd2012-03-28 20:51:51 +020016" 2005/06/15, Moshe Kaminsky <kaminsky AT math.huji.ac.il>
Bram Moolenaarf1568ec2011-12-14 21:17:39 +010017" (*) New variables:
18" g:tex_items, g:tex_itemize_env, g:tex_noindent_env
Bram Moolenaar61d35bd2012-03-28 20:51:51 +020019" 2011/3/6, by Zhou YiChao <broken.zhou AT gmail.com>
Bram Moolenaarf1568ec2011-12-14 21:17:39 +010020" (*) 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
Bram Moolenaar61d35bd2012-03-28 20:51:51 +020025" 2011/9/25, by Zhou Yichao <broken.zhou AT gmail.com>
Bram Moolenaarf1568ec2011-12-14 21:17:39 +010026" (*) Bug fix: smartindent-like indent for "[]"
27" (*) New features: Align with "&".
Bram Moolenaar61d35bd2012-03-28 20:51:51 +020028" (*) New variable: g:tex_indent_and.
29" 2011/10/23 by Zhou Yichao <broken.zhou AT gmail.com>
Bram Moolenaarf1568ec2011-12-14 21:17:39 +010030" (*) Bug fix: improve the smartindent-like indent for "{}" and
31" "[]".
Bram Moolenaar61d35bd2012-03-28 20:51:51 +020032" 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
Bram Moolenaarad3b3662013-05-17 18:14:19 +020049" 2013/05/02 by Zhou Yichao <broken.zhou AT gmail.com>
50" (*) Fix problem about GetTeXIndent checker. Thank Albert Netymk
51" for reporting this.
Bram Moolenaar61d35bd2012-03-28 20:51:51 +020052" }}}
Bram Moolenaarf1568ec2011-12-14 21:17:39 +010053
Bram Moolenaar61d35bd2012-03-28 20:51:51 +020054" Document: {{{
Bram Moolenaarf1568ec2011-12-14 21:17:39 +010055"
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 "[]"
Bram Moolenaar61d35bd2012-03-28 20:51:51 +020064"
Bram Moolenaarf1568ec2011-12-14 21:17:39 +010065" * 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.
Bram Moolenaar61d35bd2012-03-28 20:51:51 +020069"
Bram Moolenaarf1568ec2011-12-14 21:17:39 +010070" 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.
Bram Moolenaar61d35bd2012-03-28 20:51:51 +020074"
Bram Moolenaarf1568ec2011-12-14 21:17:39 +010075" set unset
76" ----------------------------------------------------------------
Bram Moolenaar61d35bd2012-03-28 20:51:51 +020077" \begin{itemize} \begin{itemize}
Bram Moolenaarf1568ec2011-12-14 21:17:39 +010078" \item blablabla \item blablabla
Bram Moolenaar61d35bd2012-03-28 20:51:51 +020079" bla bla bla bla bla bla
Bram Moolenaarf1568ec2011-12-14 21:17:39 +010080" \item blablabla \item blablabla
Bram Moolenaar61d35bd2012-03-28 20:51:51 +020081" bla bla bla bla bla bla
82" \end{itemize} \end{itemize}
Bram Moolenaarf1568ec2011-12-14 21:17:39 +010083"
84"
85" * g:tex_items
86"
Bram Moolenaar61d35bd2012-03-28 20:51:51 +020087" 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
Bram Moolenaarf1568ec2011-12-14 21:17:39 +010089" be escaped. The default is '\\bibitem\|\\item'.
90"
91" * g:tex_itemize_env
Bram Moolenaar61d35bd2012-03-28 20:51:51 +020092"
93" A list of environment names, separated with '\|', where the items (item
94" commands matching g:tex_items) may appear. The default is
Bram Moolenaarf1568ec2011-12-14 21:17:39 +010095" 'itemize\|description\|enumerate\|thebibliography'.
96"
97" * g:tex_noindent_env
98"
Bram Moolenaar61d35bd2012-03-28 20:51:51 +020099" A list of environment names. separated with '\|', where no indentation is
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100100" 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"
Bram Moolenaar61d35bd2012-03-28 20:51:51 +0200109" }}}
110
111" Only define the function once
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100112if exists("b:did_indent")
113 finish
114endif
Bram Moolenaar61d35bd2012-03-28 20:51:51 +0200115
116let s:cpo_save = &cpo
117set cpo&vim
118
119" Define global variable {{{
120
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100121let b:did_indent = 1
122
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100123if !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')
Bram Moolenaar61d35bd2012-03-28 20:51:51 +0200137 let g:tex_items = '\\bibitem\|\\item'
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100138 endif
139else
140 let g:tex_items = ''
141endif
142
Bram Moolenaar61d35bd2012-03-28 20:51:51 +0200143if !exists("g:tex_indent_paretheses")
144 let g:tex_indent_paretheses = 1
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100145endif
146
Bram Moolenaar61d35bd2012-03-28 20:51:51 +0200147if !exists("g:tex_noindent_env")
148 let g:tex_noindent_env = 'document\|verbatim\|lstlisting'
149endif "}}}
150
151" VIM Setting " {{{
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100152setlocal autoindent
153setlocal nosmartindent
154setlocal indentexpr=GetTeXIndent()
Bram Moolenaar61d35bd2012-03-28 20:51:51 +0200155setlocal indentkeys&
156exec 'setlocal indentkeys+=[,(,{,),},],\&' . substitute(g:tex_items, '^\|\(\\|\)', ',=', 'g')
157let g:tex_items = '^\s*' . substitute(g:tex_items, '^\(\^\\s\*\)*', '', '')
158" }}}
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100159
Bram Moolenaarad3b3662013-05-17 18:14:19 +0200160function! GetTeXIndent() " {{{
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100161 " 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
Bram Moolenaar61d35bd2012-03-28 20:51:51 +0200171 return 0
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100172 endif
173
Bram Moolenaar61d35bd2012-03-28 20:51:51 +0200174 let line = substitute(getline(lnum), '%.*', ' ','g') " last line
175 let cline = substitute(getline(v:lnum), '%.*', ' ', 'g') " current line
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100176
Bram Moolenaar61d35bd2012-03-28 20:51:51 +0200177 " 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
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100186 " You want to align with "&"
187 if g:tex_indent_and
Bram Moolenaar61d35bd2012-03-28 20:51:51 +0200188 " Align only when current line start with "&"
189 if line =~ '&.*\\\\' && cline =~ '^\s*&'
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100190 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
Bram Moolenaar61d35bd2012-03-28 20:51:51 +0200202 return 0
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100203 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}
Bram Moolenaar61d35bd2012-03-28 20:51:51 +0200214 ""if line =~ '^\s*\\begin{\(.*\)}' && line !~ 'verbatim'
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100215 " 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
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100218 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
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100228 " Subtract a 'shiftwidth' when an environment ends
Bram Moolenaar61d35bd2012-03-28 20:51:51 +0200229 if cline =~ '\\end{.*}' && cline !~ g:tex_noindent_env
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100230
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
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100242 let sum1 = 0
243 for i in range(0, strlen(line)-1)
Bram Moolenaar61d35bd2012-03-28 20:51:51 +0200244 if line[i] == "}" || line[i] == "]" ||
245 \ strpart(line, i, 7) == '\right)'
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100246 let sum1 = max([0, sum1-1])
247 endif
Bram Moolenaar61d35bd2012-03-28 20:51:51 +0200248 if line[i] == "{" || line[i] == "[" ||
249 \ strpart(line, i, 6) == '\left('
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100250 let sum1 += 1
251 endif
252 endfor
253
254 let sum2 = 0
255 for i in reverse(range(0, strlen(cline)-1))
Bram Moolenaar61d35bd2012-03-28 20:51:51 +0200256 if cline[i] == "{" || cline[i] == "[" ||
257 \ strpart(cline, i, 6) == '\left('
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100258 let sum2 = max([0, sum2-1])
259 endif
Bram Moolenaar61d35bd2012-03-28 20:51:51 +0200260 if cline[i] == "}" || cline[i] == "]" ||
261 \ strpart(cline, i, 7) == '\right)'
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100262 let sum2 += 1
263 endif
264 endfor
265
266 let ind += (sum1 - sum2) * &sw
267 endif
268
Bram Moolenaar61d35bd2012-03-28 20:51:51 +0200269 if g:tex_indent_paretheses
270 endif
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100271
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
Bram Moolenaar61d35bd2012-03-28 20:51:51 +0200290endfunction "}}}
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100291
Bram Moolenaarb6b046b2011-12-30 13:11:27 +0100292let &cpo = s:cpo_save
293unlet s:cpo_save
294
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100295" vim: set sw=4 textwidth=80: