blob: dcfbccdbe5fe0c114f7a1f3e3e902f11e0829f52 [file] [log] [blame]
Bram Moolenaarf1568ec2011-12-14 21:17:39 +01001" Vim indent file
2" Language: LaTeX
3" Maintainer: Zhou YiChao <broken.zhou@gmail.com>
4" Created: Sat, 16 Feb 2002 16:50:19 +0100
Bram Moolenaarb6b046b2011-12-30 13:11:27 +01005" Last Change: 2011 Dec 24
Bram Moolenaarf1568ec2011-12-14 21:17:39 +01006" Last Update: 25th Sep 2002, by LH :
7" (*) better support for the option
8" (*) use some regex instead of several '||'.
9" Oct 9th, 2003, by JT:
10" (*) don't change indentation of lines starting with '%'
11" 2005/06/15, Moshe Kaminsky <kaminsky@math.huji.ac.il>
12" (*) New variables:
13" g:tex_items, g:tex_itemize_env, g:tex_noindent_env
14" 2011/3/6, by Zhou YiChao <broken.zhou@gmail.com>
15" (*) Don't change indentation of lines starting with '%'
16" I don't see any code with '%' and it doesn't work properly
17" so I add some code.
18" (*) New features: Add smartindent-like indent for "{}" and "[]".
19" (*) New variables: g:tex_indent_brace
20" 2011/9/25, by Zhou Yichao <broken.zhou@gmail.com>
21" (*) Bug fix: smartindent-like indent for "[]"
22" (*) New features: Align with "&".
23" (*) New variable: g:tex_indent_and
24" 2011/10/23 by Zhou Yichao <broken.zhou@gmail.com>
25" (*) Bug fix: improve the smartindent-like indent for "{}" and
26" "[]".
27"
28" Version: 0.62
29
30" Options: {{{
31"
32" To set the following options (ok, currently it's just one), add a line like
33" let g:tex_indent_items = 1
34" to your ~/.vimrc.
35"
36" * g:tex_indent_brace
37"
38" If this variable is unset or non-zero, it will use smartindent-like style
39" for "{}" and "[]"
Bram Moolenaarb6b046b2011-12-30 13:11:27 +010040"
Bram Moolenaarf1568ec2011-12-14 21:17:39 +010041" * g:tex_indent_items
42"
43" If this variable is set, item-environments are indented like Emacs does
44" it, i.e., continuation lines are indented with a shiftwidth.
Bram Moolenaarb6b046b2011-12-30 13:11:27 +010045"
Bram Moolenaarf1568ec2011-12-14 21:17:39 +010046" NOTE: I've already set the variable below; delete the corresponding line
47" if you don't like this behaviour.
48"
49" Per default, it is unset.
Bram Moolenaarb6b046b2011-12-30 13:11:27 +010050"
Bram Moolenaarf1568ec2011-12-14 21:17:39 +010051" set unset
52" ----------------------------------------------------------------
Bram Moolenaarb6b046b2011-12-30 13:11:27 +010053" \begin{itemize} \begin{itemize}
Bram Moolenaarf1568ec2011-12-14 21:17:39 +010054" \item blablabla \item blablabla
Bram Moolenaarb6b046b2011-12-30 13:11:27 +010055" bla bla bla bla bla bla
Bram Moolenaarf1568ec2011-12-14 21:17:39 +010056" \item blablabla \item blablabla
Bram Moolenaarb6b046b2011-12-30 13:11:27 +010057" bla bla bla bla bla bla
58" \end{itemize} \end{itemize}
Bram Moolenaarf1568ec2011-12-14 21:17:39 +010059"
60"
61" * g:tex_items
62"
Bram Moolenaarb6b046b2011-12-30 13:11:27 +010063" A list of tokens to be considered as commands for the beginning of an item
64" command. The tokens should be separated with '\|'. The initial '\' should
Bram Moolenaarf1568ec2011-12-14 21:17:39 +010065" be escaped. The default is '\\bibitem\|\\item'.
66"
67" * g:tex_itemize_env
Bram Moolenaarb6b046b2011-12-30 13:11:27 +010068"
69" A list of environment names, separated with '\|', where the items (item
70" commands matching g:tex_items) may appear. The default is
Bram Moolenaarf1568ec2011-12-14 21:17:39 +010071" 'itemize\|description\|enumerate\|thebibliography'.
72"
73" * g:tex_noindent_env
74"
Bram Moolenaarb6b046b2011-12-30 13:11:27 +010075" A list of environment names. separated with '\|', where no indentation is
Bram Moolenaarf1568ec2011-12-14 21:17:39 +010076" required. The default is 'document\|verbatim'.
77"
78" * g:tex_indent_and
79"
80" If this variable is unset or zero, vim will try to align the line with first
81" "&". This is pretty useful when you use environment like table or align.
82" Note that this feature need to search back some line, so vim may become
83" a little slow.
84"
Bram Moolenaarb6b046b2011-12-30 13:11:27 +010085" }}}
Bram Moolenaarf1568ec2011-12-14 21:17:39 +010086
87if exists("b:did_indent")
88 finish
89endif
90let b:did_indent = 1
91
92" Delete the next line to avoid the special indention of items
93if !exists("g:tex_indent_items")
94 let g:tex_indent_items = 1
95endif
96if !exists("g:tex_indent_brace")
97 let g:tex_indent_brace = 1
98endif
99if !exists("g:tex_indent_and")
100 let g:tex_indent_and = 1
101endif
102if g:tex_indent_items
103 if !exists("g:tex_itemize_env")
104 let g:tex_itemize_env = 'itemize\|description\|enumerate\|thebibliography'
105 endif
106 if !exists('g:tex_items')
Bram Moolenaarb6b046b2011-12-30 13:11:27 +0100107 let g:tex_items = '\\bibitem\|\\item'
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100108 endif
109else
110 let g:tex_items = ''
111endif
112
113if !exists("g:tex_noindent_env")
114 let g:tex_noindent_env = 'document\|verbatim\|lstlisting'
115endif
116
117setlocal autoindent
118setlocal nosmartindent
119setlocal indentexpr=GetTeXIndent()
120exec 'setlocal indentkeys+=},],\&' . substitute(g:tex_items, '^\|\(\\|\)', ',=', 'g')
121let g:tex_items = '^\s*' . g:tex_items
122
123
124" Only define the function once
125if exists("*GetTeXIndent") | finish
126endif
127
Bram Moolenaarb6b046b2011-12-30 13:11:27 +0100128let s:cpo_save = &cpo
129set cpo&vim
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100130
131function GetTeXIndent()
132 " Find a non-blank line above the current line.
133 let lnum = prevnonblank(v:lnum - 1)
134
135 " Comment line is not what we need.
136 while lnum != 0 && getline(lnum) =~ '^\s*%'
137 let lnum = prevnonblank(lnum - 1)
138 endwhile
139
140 " At the start of the file use zero indent.
141 if lnum == 0
Bram Moolenaarb6b046b2011-12-30 13:11:27 +0100142 return 0
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100143 endif
144
145 let line = getline(lnum) " last line
146 let cline = getline(v:lnum) " current line
147
148 " You want to align with "&"
149 if g:tex_indent_and
150 " Align with last line if last line has a "&"
151 if stridx(cline, "&") != -1 && stridx(line, "&") != -1
152 return indent(v:lnum) + stridx(line, "&") - stridx(cline, "&")
153 endif
154
155 " set line & lnum to the line which doesn't contain "&"
156 while lnum != 0 && (stridx(line, "&") != -1 || line =~ '^\s*%')
157 let lnum = prevnonblank(lnum - 1)
158 let line = getline(lnum)
159 endwhile
160 endif
161
162
163 if lnum == 0
Bram Moolenaarb6b046b2011-12-30 13:11:27 +0100164 return 0
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100165 endif
166
167 let ind = indent(lnum)
168
169 " New code for comment: retain the indent of current line
170 if cline =~ '^\s*%'
171 return indent(v:lnum)
172 endif
173
174 " Add a 'shiftwidth' after beginning of environments.
175 " Don't add it for \begin{document} and \begin{verbatim}
Bram Moolenaarb6b046b2011-12-30 13:11:27 +0100176 ""if line =~ '^\s*\\begin{\(.*\)}' && line !~ 'verbatim'
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100177 " LH modification : \begin does not always start a line
178 " ZYC modification : \end after \begin won't cause wrong indent anymore
179 if line =~ '\\begin{.*}' && line !~ g:tex_noindent_env
180 \ && line !~ '\\begin{.\{-}}.*\\end{.*}'
181
182 let ind = ind + &sw
183
184 if g:tex_indent_items
185 " Add another sw for item-environments
186 if line =~ g:tex_itemize_env
187 let ind = ind + &sw
188 endif
189 endif
190 endif
191
192
193 " Subtract a 'shiftwidth' when an environment ends
194 if cline =~ '^\s*\\end' && cline !~ g:tex_noindent_env
195
196 if g:tex_indent_items
197 " Remove another sw for item-environments
198 if cline =~ g:tex_itemize_env
199 let ind = ind - &sw
200 endif
201 endif
202
203 let ind = ind - &sw
204 endif
205
206 if g:tex_indent_brace
207 " Add a 'shiftwidth' after a "{" or "[".
208 let sum1 = 0
209 for i in range(0, strlen(line)-1)
210 if line[i] == "}" || line[i] == "]"
211 let sum1 = max([0, sum1-1])
212 endif
213 if line[i] == "{" || line[i] == "["
214 let sum1 += 1
215 endif
216 endfor
217
218 let sum2 = 0
219 for i in reverse(range(0, strlen(cline)-1))
220 if cline[i] == "{" || cline[i] == "["
221 let sum2 = max([0, sum2-1])
222 endif
223 if cline[i] == "}" || cline[i] == "]"
224 let sum2 += 1
225 endif
226 endfor
227
228 let ind += (sum1 - sum2) * &sw
229 endif
230
231
232 " Special treatment for 'item'
233 " ----------------------------
234
235 if g:tex_indent_items
236
237 " '\item' or '\bibitem' itself:
238 if cline =~ g:tex_items
239 let ind = ind - &sw
240 endif
241
242 " lines following to '\item' are intented once again:
243 if line =~ g:tex_items
244 let ind = ind + &sw
245 endif
246
247 endif
248
249 return ind
250endfunction
251
Bram Moolenaarb6b046b2011-12-30 13:11:27 +0100252let &cpo = s:cpo_save
253unlet s:cpo_save
254
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100255" vim: set sw=4 textwidth=80: