comparison runtime/indent/tex.vim @ 48:67300faee616 v7-3-618

v7-3-618
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Wed, 01 Aug 2012 18:08:28 +0900
parents
children
comparison
equal deleted inserted replaced
47:6c0584ec21b1 48:67300faee616
1 " Vim indent file
2 " Language: LaTeX
3 " Maintainer: Zhou YiChao <broken.zhou AT gmail.com>
4 " Created: Sat, 16 Feb 2002 16:50:19 +0100
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 :
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 '%'
16 " 2005/06/15, Moshe Kaminsky <kaminsky AT math.huji.ac.il>
17 " (*) New variables:
18 " g:tex_items, g:tex_itemize_env, g:tex_noindent_env
19 " 2011/3/6, by Zhou YiChao <broken.zhou AT gmail.com>
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
25 " 2011/9/25, by Zhou Yichao <broken.zhou AT gmail.com>
26 " (*) Bug fix: smartindent-like indent for "[]"
27 " (*) New features: Align with "&".
28 " (*) New variable: g:tex_indent_and.
29 " 2011/10/23 by Zhou Yichao <broken.zhou AT gmail.com>
30 " (*) Bug fix: improve the smartindent-like indent for "{}" and
31 " "[]".
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
49 " }}}
50
51 " Document: {{{
52 "
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 "[]"
61 "
62 " * 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.
66 "
67 " 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.
71 "
72 " set unset
73 " ----------------------------------------------------------------
74 " \begin{itemize} \begin{itemize}
75 " \item blablabla \item blablabla
76 " bla bla bla bla bla bla
77 " \item blablabla \item blablabla
78 " bla bla bla bla bla bla
79 " \end{itemize} \end{itemize}
80 "
81 "
82 " * g:tex_items
83 "
84 " 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
86 " be escaped. The default is '\\bibitem\|\\item'.
87 "
88 " * g:tex_itemize_env
89 "
90 " A list of environment names, separated with '\|', where the items (item
91 " commands matching g:tex_items) may appear. The default is
92 " 'itemize\|description\|enumerate\|thebibliography'.
93 "
94 " * g:tex_noindent_env
95 "
96 " A list of environment names. separated with '\|', where no indentation is
97 " 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 "
106 " }}}
107
108 " Only define the function once
109 if exists("*GetTeXIndent")
110 finish
111 endif
112
113 if exists("b:did_indent")
114 finish
115 endif
116
117 let s:cpo_save = &cpo
118 set cpo&vim
119
120 " Define global variable {{{
121
122 let b:did_indent = 1
123
124 if !exists("g:tex_indent_items")
125 let g:tex_indent_items = 1
126 endif
127 if !exists("g:tex_indent_brace")
128 let g:tex_indent_brace = 1
129 endif
130 if !exists("g:tex_indent_and")
131 let g:tex_indent_and = 1
132 endif
133 if 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')
138 let g:tex_items = '\\bibitem\|\\item'
139 endif
140 else
141 let g:tex_items = ''
142 endif
143
144 if !exists("g:tex_indent_paretheses")
145 let g:tex_indent_paretheses = 1
146 endif
147
148 if !exists("g:tex_noindent_env")
149 let g:tex_noindent_env = 'document\|verbatim\|lstlisting'
150 endif "}}}
151
152 " VIM Setting " {{{
153 setlocal autoindent
154 setlocal nosmartindent
155 setlocal indentexpr=GetTeXIndent()
156 setlocal indentkeys&
157 exec 'setlocal indentkeys+=[,(,{,),},],\&' . substitute(g:tex_items, '^\|\(\\|\)', ',=', 'g')
158 let g:tex_items = '^\s*' . substitute(g:tex_items, '^\(\^\\s\*\)*', '', '')
159 " }}}
160
161 function GetTeXIndent() " {{{
162 " 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
172 return 0
173 endif
174
175 let line = substitute(getline(lnum), '%.*', ' ','g') " last line
176 let cline = substitute(getline(v:lnum), '%.*', ' ', 'g') " current line
177
178 " 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
187 " You want to align with "&"
188 if g:tex_indent_and
189 " Align only when current line start with "&"
190 if line =~ '&.*\\\\' && cline =~ '^\s*&'
191 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
203 return 0
204 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}
215 ""if line =~ '^\s*\\begin{\(.*\)}' && line !~ 'verbatim'
216 " 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
219 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
229 " Subtract a 'shiftwidth' when an environment ends
230 if cline =~ '\\end{.*}' && cline !~ g:tex_noindent_env
231
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
243 let sum1 = 0
244 for i in range(0, strlen(line)-1)
245 if line[i] == "}" || line[i] == "]" ||
246 \ strpart(line, i, 7) == '\right)'
247 let sum1 = max([0, sum1-1])
248 endif
249 if line[i] == "{" || line[i] == "[" ||
250 \ strpart(line, i, 6) == '\left('
251 let sum1 += 1
252 endif
253 endfor
254
255 let sum2 = 0
256 for i in reverse(range(0, strlen(cline)-1))
257 if cline[i] == "{" || cline[i] == "[" ||
258 \ strpart(cline, i, 6) == '\left('
259 let sum2 = max([0, sum2-1])
260 endif
261 if cline[i] == "}" || cline[i] == "]" ||
262 \ strpart(cline, i, 7) == '\right)'
263 let sum2 += 1
264 endif
265 endfor
266
267 let ind += (sum1 - sum2) * &sw
268 endif
269
270 if g:tex_indent_paretheses
271 endif
272
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
291 endfunction "}}}
292
293 let &cpo = s:cpo_save
294 unlet s:cpo_save
295
296 " vim: set sw=4 textwidth=80: