blob: 8e2a5daa8c83a9a42aeb767a4d08ede3516cf13c [file] [log] [blame]
Bram Moolenaarf1568ec2011-12-14 21:17:39 +01001" Vim indent file
2" Language: LaTeX
Bram Moolenaar61d35bd2012-03-28 20:51:51 +02003" Maintainer: Zhou YiChao <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
49" }}}
Bram Moolenaarf1568ec2011-12-14 21:17:39 +010050
Bram Moolenaar61d35bd2012-03-28 20:51:51 +020051" Document: {{{
Bram Moolenaarf1568ec2011-12-14 21:17:39 +010052"
53" To set the following options (ok, currently it's just one), add a line like
54" let g:tex_indent_items = 1
55" to your ~/.vimrc.
56"
57" * g:tex_indent_brace
58"
59" If this variable is unset or non-zero, it will use smartindent-like style
60" for "{}" and "[]"
Bram Moolenaar61d35bd2012-03-28 20:51:51 +020061"
Bram Moolenaarf1568ec2011-12-14 21:17:39 +010062" * g:tex_indent_items
63"
64" If this variable is set, item-environments are indented like Emacs does
65" it, i.e., continuation lines are indented with a shiftwidth.
Bram Moolenaar61d35bd2012-03-28 20:51:51 +020066"
Bram Moolenaarf1568ec2011-12-14 21:17:39 +010067" NOTE: I've already set the variable below; delete the corresponding line
68" if you don't like this behaviour.
69"
70" Per default, it is unset.
Bram Moolenaar61d35bd2012-03-28 20:51:51 +020071"
Bram Moolenaarf1568ec2011-12-14 21:17:39 +010072" set unset
73" ----------------------------------------------------------------
Bram Moolenaar61d35bd2012-03-28 20:51:51 +020074" \begin{itemize} \begin{itemize}
Bram Moolenaarf1568ec2011-12-14 21:17:39 +010075" \item blablabla \item blablabla
Bram Moolenaar61d35bd2012-03-28 20:51:51 +020076" bla bla bla bla bla bla
Bram Moolenaarf1568ec2011-12-14 21:17:39 +010077" \item blablabla \item blablabla
Bram Moolenaar61d35bd2012-03-28 20:51:51 +020078" bla bla bla bla bla bla
79" \end{itemize} \end{itemize}
Bram Moolenaarf1568ec2011-12-14 21:17:39 +010080"
81"
82" * g:tex_items
83"
Bram Moolenaar61d35bd2012-03-28 20:51:51 +020084" A list of tokens to be considered as commands for the beginning of an item
85" command. The tokens should be separated with '\|'. The initial '\' should
Bram Moolenaarf1568ec2011-12-14 21:17:39 +010086" be escaped. The default is '\\bibitem\|\\item'.
87"
88" * g:tex_itemize_env
Bram Moolenaar61d35bd2012-03-28 20:51:51 +020089"
90" A list of environment names, separated with '\|', where the items (item
91" commands matching g:tex_items) may appear. The default is
Bram Moolenaarf1568ec2011-12-14 21:17:39 +010092" 'itemize\|description\|enumerate\|thebibliography'.
93"
94" * g:tex_noindent_env
95"
Bram Moolenaar61d35bd2012-03-28 20:51:51 +020096" A list of environment names. separated with '\|', where no indentation is
Bram Moolenaarf1568ec2011-12-14 21:17:39 +010097" required. The default is 'document\|verbatim'.
98"
99" * g:tex_indent_and
100"
101" If this variable is unset or zero, vim will try to align the line with first
102" "&". This is pretty useful when you use environment like table or align.
103" Note that this feature need to search back some line, so vim may become
104" a little slow.
105"
Bram Moolenaar61d35bd2012-03-28 20:51:51 +0200106" }}}
107
108" Only define the function once
109if exists("*GetTeXIndent")
110 finish
111endif
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100112
113if exists("b:did_indent")
114 finish
115endif
Bram Moolenaar61d35bd2012-03-28 20:51:51 +0200116
117let s:cpo_save = &cpo
118set cpo&vim
119
120" Define global variable {{{
121
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100122let b:did_indent = 1
123
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100124if !exists("g:tex_indent_items")
125 let g:tex_indent_items = 1
126endif
127if !exists("g:tex_indent_brace")
128 let g:tex_indent_brace = 1
129endif
130if !exists("g:tex_indent_and")
131 let g:tex_indent_and = 1
132endif
133if g:tex_indent_items
134 if !exists("g:tex_itemize_env")
135 let g:tex_itemize_env = 'itemize\|description\|enumerate\|thebibliography'
136 endif
137 if !exists('g:tex_items')
Bram Moolenaar61d35bd2012-03-28 20:51:51 +0200138 let g:tex_items = '\\bibitem\|\\item'
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100139 endif
140else
141 let g:tex_items = ''
142endif
143
Bram Moolenaar61d35bd2012-03-28 20:51:51 +0200144if !exists("g:tex_indent_paretheses")
145 let g:tex_indent_paretheses = 1
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100146endif
147
Bram Moolenaar61d35bd2012-03-28 20:51:51 +0200148if !exists("g:tex_noindent_env")
149 let g:tex_noindent_env = 'document\|verbatim\|lstlisting'
150endif "}}}
151
152" VIM Setting " {{{
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100153setlocal autoindent
154setlocal nosmartindent
155setlocal indentexpr=GetTeXIndent()
Bram Moolenaar61d35bd2012-03-28 20:51:51 +0200156setlocal indentkeys&
157exec 'setlocal indentkeys+=[,(,{,),},],\&' . substitute(g:tex_items, '^\|\(\\|\)', ',=', 'g')
158let g:tex_items = '^\s*' . substitute(g:tex_items, '^\(\^\\s\*\)*', '', '')
159" }}}
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100160
Bram Moolenaar61d35bd2012-03-28 20:51:51 +0200161function GetTeXIndent() " {{{
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100162 " Find a non-blank line above the current line.
163 let lnum = prevnonblank(v:lnum - 1)
164
165 " Comment line is not what we need.
166 while lnum != 0 && getline(lnum) =~ '^\s*%'
167 let lnum = prevnonblank(lnum - 1)
168 endwhile
169
170 " At the start of the file use zero indent.
171 if lnum == 0
Bram Moolenaar61d35bd2012-03-28 20:51:51 +0200172 return 0
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100173 endif
174
Bram Moolenaar61d35bd2012-03-28 20:51:51 +0200175 let line = substitute(getline(lnum), '%.*', ' ','g') " last line
176 let cline = substitute(getline(v:lnum), '%.*', ' ', 'g') " current line
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100177
Bram Moolenaar61d35bd2012-03-28 20:51:51 +0200178 " We are in verbatim, so do what our user what.
179 if synIDattr(synID(v:lnum, indent(v:lnum), 1), "name") == "texZone"
180 if empty(cline)
181 return indent(lnum)
182 else
183 return indent(v:lnum)
184 end
185 endif
186
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100187 " You want to align with "&"
188 if g:tex_indent_and
Bram Moolenaar61d35bd2012-03-28 20:51:51 +0200189 " Align only when current line start with "&"
190 if line =~ '&.*\\\\' && cline =~ '^\s*&'
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100191 return indent(v:lnum) + stridx(line, "&") - stridx(cline, "&")
192 endif
193
194 " set line & lnum to the line which doesn't contain "&"
195 while lnum != 0 && (stridx(line, "&") != -1 || line =~ '^\s*%')
196 let lnum = prevnonblank(lnum - 1)
197 let line = getline(lnum)
198 endwhile
199 endif
200
201
202 if lnum == 0
Bram Moolenaar61d35bd2012-03-28 20:51:51 +0200203 return 0
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100204 endif
205
206 let ind = indent(lnum)
207
208 " New code for comment: retain the indent of current line
209 if cline =~ '^\s*%'
210 return indent(v:lnum)
211 endif
212
213 " Add a 'shiftwidth' after beginning of environments.
214 " Don't add it for \begin{document} and \begin{verbatim}
Bram Moolenaar61d35bd2012-03-28 20:51:51 +0200215 ""if line =~ '^\s*\\begin{\(.*\)}' && line !~ 'verbatim'
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100216 " LH modification : \begin does not always start a line
217 " ZYC modification : \end after \begin won't cause wrong indent anymore
218 if line =~ '\\begin{.*}' && line !~ g:tex_noindent_env
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100219 let ind = ind + &sw
220
221 if g:tex_indent_items
222 " Add another sw for item-environments
223 if line =~ g:tex_itemize_env
224 let ind = ind + &sw
225 endif
226 endif
227 endif
228
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100229 " Subtract a 'shiftwidth' when an environment ends
Bram Moolenaar61d35bd2012-03-28 20:51:51 +0200230 if cline =~ '\\end{.*}' && cline !~ g:tex_noindent_env
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100231
232 if g:tex_indent_items
233 " Remove another sw for item-environments
234 if cline =~ g:tex_itemize_env
235 let ind = ind - &sw
236 endif
237 endif
238
239 let ind = ind - &sw
240 endif
241
242 if g:tex_indent_brace
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100243 let sum1 = 0
244 for i in range(0, strlen(line)-1)
Bram Moolenaar61d35bd2012-03-28 20:51:51 +0200245 if line[i] == "}" || line[i] == "]" ||
246 \ strpart(line, i, 7) == '\right)'
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100247 let sum1 = max([0, sum1-1])
248 endif
Bram Moolenaar61d35bd2012-03-28 20:51:51 +0200249 if line[i] == "{" || line[i] == "[" ||
250 \ strpart(line, i, 6) == '\left('
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100251 let sum1 += 1
252 endif
253 endfor
254
255 let sum2 = 0
256 for i in reverse(range(0, strlen(cline)-1))
Bram Moolenaar61d35bd2012-03-28 20:51:51 +0200257 if cline[i] == "{" || cline[i] == "[" ||
258 \ strpart(cline, i, 6) == '\left('
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100259 let sum2 = max([0, sum2-1])
260 endif
Bram Moolenaar61d35bd2012-03-28 20:51:51 +0200261 if cline[i] == "}" || cline[i] == "]" ||
262 \ strpart(cline, i, 7) == '\right)'
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100263 let sum2 += 1
264 endif
265 endfor
266
267 let ind += (sum1 - sum2) * &sw
268 endif
269
Bram Moolenaar61d35bd2012-03-28 20:51:51 +0200270 if g:tex_indent_paretheses
271 endif
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100272
273 " Special treatment for 'item'
274 " ----------------------------
275
276 if g:tex_indent_items
277
278 " '\item' or '\bibitem' itself:
279 if cline =~ g:tex_items
280 let ind = ind - &sw
281 endif
282
283 " lines following to '\item' are intented once again:
284 if line =~ g:tex_items
285 let ind = ind + &sw
286 endif
287
288 endif
289
290 return ind
Bram Moolenaar61d35bd2012-03-28 20:51:51 +0200291endfunction "}}}
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100292
Bram Moolenaarb6b046b2011-12-30 13:11:27 +0100293let &cpo = s:cpo_save
294unlet s:cpo_save
295
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100296" vim: set sw=4 textwidth=80: