blob: 0150bb96232d1ed04ee9d7bb6db9c8c40de0afd0 [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 Moolenaar328da0d2016-03-04 22:22:32 +01005" Version: 0.9.2
6" Please email me if you found something I can do. Comments, bug report and
7" feature request are welcome.
Bram Moolenaar61d35bd2012-03-28 20:51:51 +02008
9" Last Update: {{{
10" 25th Sep 2002, by LH :
Bram Moolenaarf1568ec2011-12-14 21:17:39 +010011" (*) better support for the option
12" (*) use some regex instead of several '||'.
13" Oct 9th, 2003, by JT:
14" (*) don't change indentation of lines starting with '%'
Bram Moolenaar61d35bd2012-03-28 20:51:51 +020015" 2005/06/15, Moshe Kaminsky <kaminsky AT math.huji.ac.il>
Bram Moolenaarf1568ec2011-12-14 21:17:39 +010016" (*) New variables:
17" g:tex_items, g:tex_itemize_env, g:tex_noindent_env
Bram Moolenaar61d35bd2012-03-28 20:51:51 +020018" 2011/3/6, by Zhou YiChao <broken.zhou AT gmail.com>
Bram Moolenaarf1568ec2011-12-14 21:17:39 +010019" (*) Don't change indentation of lines starting with '%'
20" I don't see any code with '%' and it doesn't work properly
21" so I add some code.
22" (*) New features: Add smartindent-like indent for "{}" and "[]".
23" (*) New variables: g:tex_indent_brace
Bram Moolenaar61d35bd2012-03-28 20:51:51 +020024" 2011/9/25, by Zhou Yichao <broken.zhou AT gmail.com>
Bram Moolenaarf1568ec2011-12-14 21:17:39 +010025" (*) Bug fix: smartindent-like indent for "[]"
26" (*) New features: Align with "&".
Bram Moolenaar61d35bd2012-03-28 20:51:51 +020027" (*) New variable: g:tex_indent_and.
28" 2011/10/23 by Zhou Yichao <broken.zhou AT gmail.com>
Bram Moolenaarf1568ec2011-12-14 21:17:39 +010029" (*) Bug fix: improve the smartindent-like indent for "{}" and
30" "[]".
Bram Moolenaar61d35bd2012-03-28 20:51:51 +020031" 2012/02/27 by Zhou Yichao <broken.zhou AT gmail.com>
32" (*) Bug fix: support default folding marker.
33" (*) Indent with "&" is not very handy. Make it not enable by
34" default.
35" 2012/03/06 by Zhou Yichao <broken.zhou AT gmail.com>
36" (*) Modify "&" behavior and make it default again. Now "&"
37" won't align when there are more then one "&" in the previous
38" line.
39" (*) Add indent "\left(" and "\right)"
40" (*) Trust user when in "verbatim" and "lstlisting"
41" 2012/03/11 by Zhou Yichao <broken.zhou AT gmail.com>
42" (*) Modify "&" so that only indent when current line start with
Bram Moolenaar328da0d2016-03-04 22:22:32 +010043" "&".
Bram Moolenaar61d35bd2012-03-28 20:51:51 +020044" 2012/03/12 by Zhou Yichao <broken.zhou AT gmail.com>
45" (*) Modify indentkeys.
46" 2012/03/18 by Zhou Yichao <broken.zhou AT gmail.com>
47" (*) Add &cpo
Bram Moolenaarad3b3662013-05-17 18:14:19 +020048" 2013/05/02 by Zhou Yichao <broken.zhou AT gmail.com>
49" (*) Fix problem about GetTeXIndent checker. Thank Albert Netymk
50" for reporting this.
Bram Moolenaar328da0d2016-03-04 22:22:32 +010051" 2014/06/23 by Zhou Yichao <broken.zhou AT gmail.com>
52" (*) Remove the feature g:tex_indent_and because it is buggy.
53" (*) If there is not any obvious indentation hints, we do not
54" alert our user's current indentation.
55" (*) g:tex_indent_brace now only works if the open brace is the
56" last character of that line.
57" 2014/08/03 by Zhou Yichao <broken.zhou AT gmail.com>
58" (*) Indent current line if last line has larger indentation
59" 2014/08/09 by Zhou Yichao <broken.zhou AT gmail.com>
60" (*) Add missing return value for s:GetEndIndentation(...)
61"
Bram Moolenaar61d35bd2012-03-28 20:51:51 +020062" }}}
Bram Moolenaarf1568ec2011-12-14 21:17:39 +010063
Bram Moolenaar61d35bd2012-03-28 20:51:51 +020064" Document: {{{
Bram Moolenaarf1568ec2011-12-14 21:17:39 +010065"
66" To set the following options (ok, currently it's just one), add a line like
67" let g:tex_indent_items = 1
68" to your ~/.vimrc.
69"
70" * g:tex_indent_brace
71"
72" If this variable is unset or non-zero, it will use smartindent-like style
Bram Moolenaar328da0d2016-03-04 22:22:32 +010073" for "{}" and "[]". Now this only works if the open brace is the last
74" character of that line.
75"
76" % Example 1
77" \usetikzlibrary{
78" external
79" }
80"
81" % Example 2
82" \tikzexternalize[
83" prefix=tikz]
Bram Moolenaar61d35bd2012-03-28 20:51:51 +020084"
Bram Moolenaarf1568ec2011-12-14 21:17:39 +010085" * g:tex_indent_items
86"
87" If this variable is set, item-environments are indented like Emacs does
88" it, i.e., continuation lines are indented with a shiftwidth.
Bram Moolenaar61d35bd2012-03-28 20:51:51 +020089"
Bram Moolenaarf1568ec2011-12-14 21:17:39 +010090" NOTE: I've already set the variable below; delete the corresponding line
91" if you don't like this behaviour.
92"
93" Per default, it is unset.
Bram Moolenaar61d35bd2012-03-28 20:51:51 +020094"
Bram Moolenaarf1568ec2011-12-14 21:17:39 +010095" set unset
96" ----------------------------------------------------------------
Bram Moolenaar61d35bd2012-03-28 20:51:51 +020097" \begin{itemize} \begin{itemize}
Bram Moolenaarf1568ec2011-12-14 21:17:39 +010098" \item blablabla \item blablabla
Bram Moolenaar61d35bd2012-03-28 20:51:51 +020099" bla bla bla bla bla bla
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100100" \item blablabla \item blablabla
Bram Moolenaar61d35bd2012-03-28 20:51:51 +0200101" bla bla bla bla bla bla
102" \end{itemize} \end{itemize}
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100103"
104"
105" * g:tex_items
106"
Bram Moolenaar61d35bd2012-03-28 20:51:51 +0200107" A list of tokens to be considered as commands for the beginning of an item
108" command. The tokens should be separated with '\|'. The initial '\' should
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100109" be escaped. The default is '\\bibitem\|\\item'.
110"
111" * g:tex_itemize_env
Bram Moolenaar61d35bd2012-03-28 20:51:51 +0200112"
113" A list of environment names, separated with '\|', where the items (item
114" commands matching g:tex_items) may appear. The default is
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100115" 'itemize\|description\|enumerate\|thebibliography'.
116"
117" * g:tex_noindent_env
118"
Bram Moolenaar61d35bd2012-03-28 20:51:51 +0200119" A list of environment names. separated with '\|', where no indentation is
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100120" required. The default is 'document\|verbatim'.
Bram Moolenaar61d35bd2012-03-28 20:51:51 +0200121" }}}
122
123" Only define the function once
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100124if exists("b:did_indent")
125 finish
126endif
Bram Moolenaar61d35bd2012-03-28 20:51:51 +0200127
128let s:cpo_save = &cpo
129set cpo&vim
130
131" Define global variable {{{
132
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100133let b:did_indent = 1
134
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100135if !exists("g:tex_indent_items")
136 let g:tex_indent_items = 1
137endif
138if !exists("g:tex_indent_brace")
139 let g:tex_indent_brace = 1
140endif
Bram Moolenaar328da0d2016-03-04 22:22:32 +0100141if !exists("g:tex_max_scan_line")
142 let g:tex_max_scan_line = 60
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100143endif
144if g:tex_indent_items
145 if !exists("g:tex_itemize_env")
146 let g:tex_itemize_env = 'itemize\|description\|enumerate\|thebibliography'
147 endif
148 if !exists('g:tex_items')
Bram Moolenaar61d35bd2012-03-28 20:51:51 +0200149 let g:tex_items = '\\bibitem\|\\item'
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100150 endif
151else
152 let g:tex_items = ''
153endif
154
Bram Moolenaar61d35bd2012-03-28 20:51:51 +0200155if !exists("g:tex_noindent_env")
156 let g:tex_noindent_env = 'document\|verbatim\|lstlisting'
157endif "}}}
158
159" VIM Setting " {{{
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100160setlocal autoindent
161setlocal nosmartindent
162setlocal indentexpr=GetTeXIndent()
Bram Moolenaar61d35bd2012-03-28 20:51:51 +0200163setlocal indentkeys&
164exec 'setlocal indentkeys+=[,(,{,),},],\&' . substitute(g:tex_items, '^\|\(\\|\)', ',=', 'g')
165let g:tex_items = '^\s*' . substitute(g:tex_items, '^\(\^\\s\*\)*', '', '')
166" }}}
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100167
Bram Moolenaarad3b3662013-05-17 18:14:19 +0200168function! GetTeXIndent() " {{{
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100169 " Find a non-blank line above the current line.
170 let lnum = prevnonblank(v:lnum - 1)
Bram Moolenaar328da0d2016-03-04 22:22:32 +0100171 let cnum = v:lnum
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100172
173 " Comment line is not what we need.
174 while lnum != 0 && getline(lnum) =~ '^\s*%'
175 let lnum = prevnonblank(lnum - 1)
176 endwhile
177
178 " At the start of the file use zero indent.
179 if lnum == 0
Bram Moolenaar61d35bd2012-03-28 20:51:51 +0200180 return 0
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100181 endif
182
Bram Moolenaar328da0d2016-03-04 22:22:32 +0100183 let line = substitute(getline(lnum), '\s*%.*', '','g') " last line
184 let cline = substitute(getline(v:lnum), '\s*%.*', '', 'g') " current line
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100185
Bram Moolenaar61d35bd2012-03-28 20:51:51 +0200186 " We are in verbatim, so do what our user what.
187 if synIDattr(synID(v:lnum, indent(v:lnum), 1), "name") == "texZone"
188 if empty(cline)
189 return indent(lnum)
190 else
191 return indent(v:lnum)
192 end
193 endif
194
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100195 if lnum == 0
Bram Moolenaar61d35bd2012-03-28 20:51:51 +0200196 return 0
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100197 endif
198
199 let ind = indent(lnum)
Bram Moolenaar328da0d2016-03-04 22:22:32 +0100200 let stay = 1
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100201
202 " New code for comment: retain the indent of current line
203 if cline =~ '^\s*%'
204 return indent(v:lnum)
205 endif
206
207 " Add a 'shiftwidth' after beginning of environments.
208 " Don't add it for \begin{document} and \begin{verbatim}
Bram Moolenaar61d35bd2012-03-28 20:51:51 +0200209 ""if line =~ '^\s*\\begin{\(.*\)}' && line !~ 'verbatim'
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100210 " LH modification : \begin does not always start a line
211 " ZYC modification : \end after \begin won't cause wrong indent anymore
212 if line =~ '\\begin{.*}' && line !~ g:tex_noindent_env
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100213 let ind = ind + &sw
Bram Moolenaar328da0d2016-03-04 22:22:32 +0100214 let stay = 0
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100215
216 if g:tex_indent_items
217 " Add another sw for item-environments
218 if line =~ g:tex_itemize_env
219 let ind = ind + &sw
Bram Moolenaar328da0d2016-03-04 22:22:32 +0100220 let stay = 0
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100221 endif
222 endif
223 endif
224
Bram Moolenaar328da0d2016-03-04 22:22:32 +0100225 if cline =~ '\\end{.*}'
226 let retn = s:GetEndIndentation(v:lnum)
227 if retn != -1
228 return retn
229 endif
230 end
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100231 " Subtract a 'shiftwidth' when an environment ends
Bram Moolenaar328da0d2016-03-04 22:22:32 +0100232 if cline =~ '\\end{.*}'
233 \ && cline !~ g:tex_noindent_env
234 \ && cline !~ '\\begin{.*}.*\\end{.*}'
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100235 if g:tex_indent_items
236 " Remove another sw for item-environments
237 if cline =~ g:tex_itemize_env
238 let ind = ind - &sw
Bram Moolenaar328da0d2016-03-04 22:22:32 +0100239 let stay = 0
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100240 endif
241 endif
242
243 let ind = ind - &sw
Bram Moolenaar328da0d2016-03-04 22:22:32 +0100244 let stay = 0
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100245 endif
246
247 if g:tex_indent_brace
Bram Moolenaar328da0d2016-03-04 22:22:32 +0100248 let char = line[strlen(line)-1]
249 if char == '[' || char == '{'
250 let ind += &sw
251 let stay = 0
252 endif
253
254 let cind = indent(v:lnum)
255 let char = cline[cind]
256 if (char == ']' || char == '}') &&
257 \ s:CheckPairedIsLastCharacter(v:lnum, cind)
258 let ind -= &sw
259 let stay = 0
260 endif
261
262 for i in range(indent(lnum)+1, strlen(line)-1)
263 let char = line[i]
264 if char == ']' || char == '}'
265 if s:CheckPairedIsLastCharacter(lnum, i)
266 let ind -= &sw
267 let stay = 0
268 endif
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100269 endif
270 endfor
Bram Moolenaar61d35bd2012-03-28 20:51:51 +0200271 endif
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100272
273 " Special treatment for 'item'
274 " ----------------------------
275
276 if g:tex_indent_items
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100277 " '\item' or '\bibitem' itself:
278 if cline =~ g:tex_items
279 let ind = ind - &sw
Bram Moolenaar328da0d2016-03-04 22:22:32 +0100280 let stay = 0
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100281 endif
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100282 " lines following to '\item' are intented once again:
283 if line =~ g:tex_items
284 let ind = ind + &sw
Bram Moolenaar328da0d2016-03-04 22:22:32 +0100285 let stay = 0
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100286 endif
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100287 endif
288
Bram Moolenaar328da0d2016-03-04 22:22:32 +0100289 if stay
290 " If there is no obvious indentation hint, we trust our user.
291 if empty(cline)
292 return ind
293 else
294 return max([indent(v:lnum), s:GetLastBeginIndentation(v:lnum)])
295 endif
296 else
297 return ind
298 endif
299endfunction "}}}
300
301function! s:GetLastBeginIndentation(lnum) " {{{
302 let matchend = 1
303 for lnum in range(a:lnum-1, max([a:lnum - g:tex_max_scan_line, 1]), -1)
304 let line = getline(lnum)
305 if line =~ '\\end{.*}'
306 let matchend += 1
307 endif
308 if line =~ '\\begin{.*}'
309 let matchend -= 1
310 endif
311 if matchend == 0
312 if line =~ g:tex_itemize_env
313 return indent(lnum) + 2 * &sw
314 endif
315 if line =~ g:tex_noindent_env
316 return indent(lnum)
317 endif
318 return indent(lnum) + &sw
319 endif
320 endfor
321 return -1
322endfunction
323
324function! s:GetEndIndentation(lnum) " {{{
325 if getline(a:lnum) =~ '\\begin{.*}.*\\end{.*}'
326 return -1
327 endif
328
329 let min_indent = 100
330 let matchend = 1
331 for lnum in range(a:lnum-1, max([a:lnum-g:tex_max_scan_line, 1]), -1)
332 let line = getline(lnum)
333 if line =~ '\\end{.*}'
334 let matchend += 1
335 endif
336 if line =~ '\\begin{.*}'
337 let matchend -= 1
338 endif
339 if matchend == 0
340 return indent(lnum)
341 endif
342 if !empty(line)
343 let min_indent = min([min_indent, indent(lnum)])
344 endif
345 endfor
346 return min_indent - &sw
347endfunction
348
349" Most of the code is from matchparen.vim
350function! s:CheckPairedIsLastCharacter(lnum, col) "{{{
351 " Get the character under the cursor and check if it's in 'matchpairs'.
352 let c_lnum = a:lnum
353 let c_col = a:col+1
354
355
356 let c = getline(c_lnum)[c_col-1]
357 let plist = split(&matchpairs, '.\zs[:,]')
358 let i = index(plist, c)
359 if i < 0
360 return 0
361 endif
362
363 " Figure out the arguments for searchpairpos().
364 if i % 2 == 0
365 let s_flags = 'nW'
366 let c2 = plist[i + 1]
367 else
368 let s_flags = 'nbW'
369 let c2 = c
370 let c = plist[i - 1]
371 endif
372 if c == '['
373 let c = '\['
374 let c2 = '\]'
375 endif
376
377 " Find the match. When it was just before the cursor move it there for a
378 " moment.
379 let save_cursor = winsaveview()
380 call cursor(c_lnum, c_col)
381
382 " When not in a string or comment ignore matches inside them.
383 " We match "escape" for special items, such as lispEscapeSpecial.
384 let s_skip ='synIDattr(synID(line("."), col("."), 0), "name") ' .
385 \ '=~? "string\\|character\\|singlequote\\|escape\\|comment"'
386 execute 'if' s_skip '| let s_skip = 0 | endif'
387
388 let stopline = max([0, c_lnum - g:tex_max_scan_line])
389
390 " Limit the search time to 300 msec to avoid a hang on very long lines.
391 " This fails when a timeout is not supported.
392 try
393 let [m_lnum, m_col] = searchpairpos(c, '', c2, s_flags, s_skip, stopline, 100)
394 catch /E118/
395 endtry
396
397 call winrestview(save_cursor)
398
399 if m_lnum > 0
400 let line = getline(m_lnum)
401 return strlen(line) == m_col
402 endif
403
404 return 0
Bram Moolenaar61d35bd2012-03-28 20:51:51 +0200405endfunction "}}}
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100406
Bram Moolenaarb6b046b2011-12-30 13:11:27 +0100407let &cpo = s:cpo_save
408unlet s:cpo_save
409
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100410" vim: set sw=4 textwidth=80: