3224
|
1 " Vim indent file
|
|
2 " Language: LaTeX
|
4437
|
3 " Maintainer: YiChao Zhou <broken.zhou AT gmail.com>
|
3224
|
4 " Created: Sat, 16 Feb 2002 16:50:19 +0100
|
3445
|
5 " 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 :
|
3224
|
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 '%'
|
3445
|
16 " 2005/06/15, Moshe Kaminsky <kaminsky AT math.huji.ac.il>
|
3224
|
17 " (*) New variables:
|
|
18 " g:tex_items, g:tex_itemize_env, g:tex_noindent_env
|
3445
|
19 " 2011/3/6, by Zhou YiChao <broken.zhou AT gmail.com>
|
3224
|
20 " (*) 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
|
3445
|
25 " 2011/9/25, by Zhou Yichao <broken.zhou AT gmail.com>
|
3224
|
26 " (*) Bug fix: smartindent-like indent for "[]"
|
|
27 " (*) New features: Align with "&".
|
3445
|
28 " (*) New variable: g:tex_indent_and.
|
|
29 " 2011/10/23 by Zhou Yichao <broken.zhou AT gmail.com>
|
3224
|
30 " (*) Bug fix: improve the smartindent-like indent for "{}" and
|
|
31 " "[]".
|
3445
|
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
|
|
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
|
4437
|
49 " 2013/05/02 by Zhou Yichao <broken.zhou AT gmail.com>
|
|
50 " (*) Fix problem about GetTeXIndent checker. Thank Albert Netymk
|
|
51 " for reporting this.
|
3445
|
52 " }}}
|
3224
|
53
|
3445
|
54 " Document: {{{
|
3224
|
55 "
|
|
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 "[]"
|
3445
|
64 "
|
3224
|
65 " * 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.
|
3445
|
69 "
|
3224
|
70 " 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.
|
3445
|
74 "
|
3224
|
75 " set unset
|
|
76 " ----------------------------------------------------------------
|
3445
|
77 " \begin{itemize} \begin{itemize}
|
3224
|
78 " \item blablabla \item blablabla
|
3445
|
79 " bla bla bla bla bla bla
|
3224
|
80 " \item blablabla \item blablabla
|
3445
|
81 " bla bla bla bla bla bla
|
|
82 " \end{itemize} \end{itemize}
|
3224
|
83 "
|
|
84 "
|
|
85 " * g:tex_items
|
|
86 "
|
3445
|
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
|
3224
|
89 " be escaped. The default is '\\bibitem\|\\item'.
|
|
90 "
|
|
91 " * g:tex_itemize_env
|
3445
|
92 "
|
|
93 " A list of environment names, separated with '\|', where the items (item
|
|
94 " commands matching g:tex_items) may appear. The default is
|
3224
|
95 " 'itemize\|description\|enumerate\|thebibliography'.
|
|
96 "
|
|
97 " * g:tex_noindent_env
|
|
98 "
|
3445
|
99 " A list of environment names. separated with '\|', where no indentation is
|
3224
|
100 " 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 "
|
3445
|
109 " }}}
|
|
110
|
|
111 " Only define the function once
|
3224
|
112 if exists("b:did_indent")
|
|
113 finish
|
|
114 endif
|
3445
|
115
|
|
116 let s:cpo_save = &cpo
|
|
117 set cpo&vim
|
|
118
|
|
119 " Define global variable {{{
|
|
120
|
3224
|
121 let b:did_indent = 1
|
|
122
|
|
123 if !exists("g:tex_indent_items")
|
|
124 let g:tex_indent_items = 1
|
|
125 endif
|
|
126 if !exists("g:tex_indent_brace")
|
|
127 let g:tex_indent_brace = 1
|
|
128 endif
|
|
129 if !exists("g:tex_indent_and")
|
|
130 let g:tex_indent_and = 1
|
|
131 endif
|
|
132 if 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')
|
3445
|
137 let g:tex_items = '\\bibitem\|\\item'
|
3224
|
138 endif
|
|
139 else
|
|
140 let g:tex_items = ''
|
|
141 endif
|
|
142
|
3445
|
143 if !exists("g:tex_indent_paretheses")
|
|
144 let g:tex_indent_paretheses = 1
|
|
145 endif
|
|
146
|
3224
|
147 if !exists("g:tex_noindent_env")
|
|
148 let g:tex_noindent_env = 'document\|verbatim\|lstlisting'
|
3445
|
149 endif "}}}
|
3224
|
150
|
3445
|
151 " VIM Setting " {{{
|
3224
|
152 setlocal autoindent
|
|
153 setlocal nosmartindent
|
|
154 setlocal indentexpr=GetTeXIndent()
|
3445
|
155 setlocal indentkeys&
|
|
156 exec 'setlocal indentkeys+=[,(,{,),},],\&' . substitute(g:tex_items, '^\|\(\\|\)', ',=', 'g')
|
|
157 let g:tex_items = '^\s*' . substitute(g:tex_items, '^\(\^\\s\*\)*', '', '')
|
|
158 " }}}
|
3224
|
159
|
4437
|
160 function! GetTeXIndent() " {{{
|
3224
|
161 " 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
|
3445
|
171 return 0
|
3224
|
172 endif
|
|
173
|
3445
|
174 let line = substitute(getline(lnum), '%.*', ' ','g') " last line
|
|
175 let cline = substitute(getline(v:lnum), '%.*', ' ', 'g') " current line
|
3224
|
176
|
3445
|
177 " 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
|
3224
|
186 " You want to align with "&"
|
|
187 if g:tex_indent_and
|
3445
|
188 " Align only when current line start with "&"
|
|
189 if line =~ '&.*\\\\' && cline =~ '^\s*&'
|
3224
|
190 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
|
3445
|
202 return 0
|
3224
|
203 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}
|
3445
|
214 ""if line =~ '^\s*\\begin{\(.*\)}' && line !~ 'verbatim'
|
3224
|
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
|
|
218 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
|
|
228 " Subtract a 'shiftwidth' when an environment ends
|
3445
|
229 if cline =~ '\\end{.*}' && cline !~ g:tex_noindent_env
|
3224
|
230
|
|
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
|
|
242 let sum1 = 0
|
|
243 for i in range(0, strlen(line)-1)
|
3445
|
244 if line[i] == "}" || line[i] == "]" ||
|
|
245 \ strpart(line, i, 7) == '\right)'
|
3224
|
246 let sum1 = max([0, sum1-1])
|
|
247 endif
|
3445
|
248 if line[i] == "{" || line[i] == "[" ||
|
|
249 \ strpart(line, i, 6) == '\left('
|
3224
|
250 let sum1 += 1
|
|
251 endif
|
|
252 endfor
|
|
253
|
|
254 let sum2 = 0
|
|
255 for i in reverse(range(0, strlen(cline)-1))
|
3445
|
256 if cline[i] == "{" || cline[i] == "[" ||
|
|
257 \ strpart(cline, i, 6) == '\left('
|
3224
|
258 let sum2 = max([0, sum2-1])
|
|
259 endif
|
3445
|
260 if cline[i] == "}" || cline[i] == "]" ||
|
|
261 \ strpart(cline, i, 7) == '\right)'
|
3224
|
262 let sum2 += 1
|
|
263 endif
|
|
264 endfor
|
|
265
|
|
266 let ind += (sum1 - sum2) * &sw
|
|
267 endif
|
|
268
|
3445
|
269 if g:tex_indent_paretheses
|
|
270 endif
|
3224
|
271
|
|
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
|
3445
|
290 endfunction "}}}
|
3224
|
291
|
3237
|
292 let &cpo = s:cpo_save
|
|
293 unlet s:cpo_save
|
|
294
|
3224
|
295 " vim: set sw=4 textwidth=80:
|