3224
|
1 " Vim indent file
|
|
2 " Language: LaTeX
|
|
3 " Maintainer: Zhou YiChao <broken.zhou@gmail.com>
|
|
4 " Created: Sat, 16 Feb 2002 16:50:19 +0100
|
3237
|
5 " Last Change: 2011 Dec 24
|
3224
|
6 " 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 "[]"
|
3237
|
40 "
|
3224
|
41 " * 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.
|
3237
|
45 "
|
3224
|
46 " 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.
|
3237
|
50 "
|
3224
|
51 " set unset
|
|
52 " ----------------------------------------------------------------
|
3237
|
53 " \begin{itemize} \begin{itemize}
|
3224
|
54 " \item blablabla \item blablabla
|
3237
|
55 " bla bla bla bla bla bla
|
3224
|
56 " \item blablabla \item blablabla
|
3237
|
57 " bla bla bla bla bla bla
|
|
58 " \end{itemize} \end{itemize}
|
3224
|
59 "
|
|
60 "
|
|
61 " * g:tex_items
|
|
62 "
|
3237
|
63 " 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
|
3224
|
65 " be escaped. The default is '\\bibitem\|\\item'.
|
|
66 "
|
|
67 " * g:tex_itemize_env
|
3237
|
68 "
|
|
69 " A list of environment names, separated with '\|', where the items (item
|
|
70 " commands matching g:tex_items) may appear. The default is
|
3224
|
71 " 'itemize\|description\|enumerate\|thebibliography'.
|
|
72 "
|
|
73 " * g:tex_noindent_env
|
|
74 "
|
3237
|
75 " A list of environment names. separated with '\|', where no indentation is
|
3224
|
76 " 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 "
|
3237
|
85 " }}}
|
3224
|
86
|
|
87 if exists("b:did_indent")
|
|
88 finish
|
|
89 endif
|
|
90 let b:did_indent = 1
|
|
91
|
|
92 " Delete the next line to avoid the special indention of items
|
|
93 if !exists("g:tex_indent_items")
|
|
94 let g:tex_indent_items = 1
|
|
95 endif
|
|
96 if !exists("g:tex_indent_brace")
|
|
97 let g:tex_indent_brace = 1
|
|
98 endif
|
|
99 if !exists("g:tex_indent_and")
|
|
100 let g:tex_indent_and = 1
|
|
101 endif
|
|
102 if 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')
|
3237
|
107 let g:tex_items = '\\bibitem\|\\item'
|
3224
|
108 endif
|
|
109 else
|
|
110 let g:tex_items = ''
|
|
111 endif
|
|
112
|
|
113 if !exists("g:tex_noindent_env")
|
|
114 let g:tex_noindent_env = 'document\|verbatim\|lstlisting'
|
|
115 endif
|
|
116
|
|
117 setlocal autoindent
|
|
118 setlocal nosmartindent
|
|
119 setlocal indentexpr=GetTeXIndent()
|
|
120 exec 'setlocal indentkeys+=},],\&' . substitute(g:tex_items, '^\|\(\\|\)', ',=', 'g')
|
|
121 let g:tex_items = '^\s*' . g:tex_items
|
|
122
|
|
123
|
|
124 " Only define the function once
|
|
125 if exists("*GetTeXIndent") | finish
|
|
126 endif
|
|
127
|
3237
|
128 let s:cpo_save = &cpo
|
|
129 set cpo&vim
|
3224
|
130
|
|
131 function 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
|
3237
|
142 return 0
|
3224
|
143 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
|
3237
|
164 return 0
|
3224
|
165 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}
|
3237
|
176 ""if line =~ '^\s*\\begin{\(.*\)}' && line !~ 'verbatim'
|
3224
|
177 " 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
|
|
250 endfunction
|
|
251
|
3237
|
252 let &cpo = s:cpo_save
|
|
253 unlet s:cpo_save
|
|
254
|
3224
|
255 " vim: set sw=4 textwidth=80:
|