comparison runtime/indent/tex.vim @ 3224:8b8ef1fed009

Update runtime files.
author Bram Moolenaar <bram@vim.org>
date Wed, 14 Dec 2011 21:17:39 +0100
parents
children 91e53bcb7946
comparison
equal deleted inserted replaced
3223:576648218cb5 3224:8b8ef1fed009
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
5 " Last Change: Tue, 25 Sep 2011
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 "[]"
40 "
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.
45 "
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.
50 "
51 " set unset
52 " ----------------------------------------------------------------
53 " \begin{itemize} \begin{itemize}
54 " \item blablabla \item blablabla
55 " bla bla bla bla bla bla
56 " \item blablabla \item blablabla
57 " bla bla bla bla bla bla
58 " \end{itemize} \end{itemize}
59 "
60 "
61 " * g:tex_items
62 "
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
65 " be escaped. The default is '\\bibitem\|\\item'.
66 "
67 " * g:tex_itemize_env
68 "
69 " A list of environment names, separated with '\|', where the items (item
70 " commands matching g:tex_items) may appear. The default is
71 " 'itemize\|description\|enumerate\|thebibliography'.
72 "
73 " * g:tex_noindent_env
74 "
75 " A list of environment names. separated with '\|', where no indentation is
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 "
85 " }}}
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')
107 let g:tex_items = '\\bibitem\|\\item'
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
128
129 function GetTeXIndent()
130 " Find a non-blank line above the current line.
131 let lnum = prevnonblank(v:lnum - 1)
132
133 " Comment line is not what we need.
134 while lnum != 0 && getline(lnum) =~ '^\s*%'
135 let lnum = prevnonblank(lnum - 1)
136 endwhile
137
138 " At the start of the file use zero indent.
139 if lnum == 0
140 return 0
141 endif
142
143 let line = getline(lnum) " last line
144 let cline = getline(v:lnum) " current line
145
146 " You want to align with "&"
147 if g:tex_indent_and
148 " Align with last line if last line has a "&"
149 if stridx(cline, "&") != -1 && stridx(line, "&") != -1
150 return indent(v:lnum) + stridx(line, "&") - stridx(cline, "&")
151 endif
152
153 " set line & lnum to the line which doesn't contain "&"
154 while lnum != 0 && (stridx(line, "&") != -1 || line =~ '^\s*%')
155 let lnum = prevnonblank(lnum - 1)
156 let line = getline(lnum)
157 endwhile
158 endif
159
160
161 if lnum == 0
162 return 0
163 endif
164
165 let ind = indent(lnum)
166
167 " New code for comment: retain the indent of current line
168 if cline =~ '^\s*%'
169 return indent(v:lnum)
170 endif
171
172 " Add a 'shiftwidth' after beginning of environments.
173 " Don't add it for \begin{document} and \begin{verbatim}
174 ""if line =~ '^\s*\\begin{\(.*\)}' && line !~ 'verbatim'
175 " LH modification : \begin does not always start a line
176 " ZYC modification : \end after \begin won't cause wrong indent anymore
177 if line =~ '\\begin{.*}' && line !~ g:tex_noindent_env
178 \ && line !~ '\\begin{.\{-}}.*\\end{.*}'
179
180 let ind = ind + &sw
181
182 if g:tex_indent_items
183 " Add another sw for item-environments
184 if line =~ g:tex_itemize_env
185 let ind = ind + &sw
186 endif
187 endif
188 endif
189
190
191 " Subtract a 'shiftwidth' when an environment ends
192 if cline =~ '^\s*\\end' && cline !~ g:tex_noindent_env
193
194 if g:tex_indent_items
195 " Remove another sw for item-environments
196 if cline =~ g:tex_itemize_env
197 let ind = ind - &sw
198 endif
199 endif
200
201 let ind = ind - &sw
202 endif
203
204 if g:tex_indent_brace
205 " Add a 'shiftwidth' after a "{" or "[".
206 let sum1 = 0
207 for i in range(0, strlen(line)-1)
208 if line[i] == "}" || line[i] == "]"
209 let sum1 = max([0, sum1-1])
210 endif
211 if line[i] == "{" || line[i] == "["
212 let sum1 += 1
213 endif
214 endfor
215
216 let sum2 = 0
217 for i in reverse(range(0, strlen(cline)-1))
218 if cline[i] == "{" || cline[i] == "["
219 let sum2 = max([0, sum2-1])
220 endif
221 if cline[i] == "}" || cline[i] == "]"
222 let sum2 += 1
223 endif
224 endfor
225
226 let ind += (sum1 - sum2) * &sw
227 endif
228
229
230 " Special treatment for 'item'
231 " ----------------------------
232
233 if g:tex_indent_items
234
235 " '\item' or '\bibitem' itself:
236 if cline =~ g:tex_items
237 let ind = ind - &sw
238 endif
239
240 " lines following to '\item' are intented once again:
241 if line =~ g:tex_items
242 let ind = ind + &sw
243 endif
244
245 endif
246
247 return ind
248 endfunction
249
250 " vim: set sw=4 textwidth=80: