comparison runtime/indent/tex.vim @ 8392:1bf1b88968a2

commit https://github.com/vim/vim/commit/328da0dcb7be34b594725eef6dc98d3ea6516d69 Author: Bram Moolenaar <Bram@vim.org> Date: Fri Mar 4 22:22:32 2016 +0100 Update runtime files.
author Christian Brabandt <cb@256bit.org>
date Fri, 04 Mar 2016 22:30:06 +0100
parents eb6ab7e78925
children d183d629509e
comparison
equal deleted inserted replaced
8391:b7b6b632a706 8392:1bf1b88968a2
1 " Vim indent file 1 " Vim indent file
2 " Language: LaTeX 2 " Language: LaTeX
3 " Maintainer: YiChao Zhou <broken.zhou AT gmail.com> 3 " Maintainer: YiChao Zhou <broken.zhou AT gmail.com>
4 " Created: Sat, 16 Feb 2002 16:50:19 +0100 4 " Created: Sat, 16 Feb 2002 16:50:19 +0100
5 " Last Change: 2012 Mar 18 19:19:50 5 " Version: 0.9.2
6 " Version: 0.7 6 " Please email me if you found something I can do. Comments, bug report and
7 " Please email me if you found something we can do. Bug report and 7 " feature request are welcome.
8 " feature request is welcome.
9 8
10 " Last Update: {{{ 9 " Last Update: {{{
11 " 25th Sep 2002, by LH : 10 " 25th Sep 2002, by LH :
12 " (*) better support for the option 11 " (*) better support for the option
13 " (*) use some regex instead of several '||'. 12 " (*) use some regex instead of several '||'.
39 " line. 38 " line.
40 " (*) Add indent "\left(" and "\right)" 39 " (*) Add indent "\left(" and "\right)"
41 " (*) Trust user when in "verbatim" and "lstlisting" 40 " (*) Trust user when in "verbatim" and "lstlisting"
42 " 2012/03/11 by Zhou Yichao <broken.zhou AT gmail.com> 41 " 2012/03/11 by Zhou Yichao <broken.zhou AT gmail.com>
43 " (*) Modify "&" so that only indent when current line start with 42 " (*) Modify "&" so that only indent when current line start with
44 " "&". 43 " "&".
45 " 2012/03/12 by Zhou Yichao <broken.zhou AT gmail.com> 44 " 2012/03/12 by Zhou Yichao <broken.zhou AT gmail.com>
46 " (*) Modify indentkeys. 45 " (*) Modify indentkeys.
47 " 2012/03/18 by Zhou Yichao <broken.zhou AT gmail.com> 46 " 2012/03/18 by Zhou Yichao <broken.zhou AT gmail.com>
48 " (*) Add &cpo 47 " (*) Add &cpo
49 " 2013/05/02 by Zhou Yichao <broken.zhou AT gmail.com> 48 " 2013/05/02 by Zhou Yichao <broken.zhou AT gmail.com>
50 " (*) Fix problem about GetTeXIndent checker. Thank Albert Netymk 49 " (*) Fix problem about GetTeXIndent checker. Thank Albert Netymk
51 " for reporting this. 50 " for reporting this.
51 " 2014/06/23 by Zhou Yichao <broken.zhou AT gmail.com>
52 " (*) Remove the feature g:tex_indent_and because it is buggy.
53 " (*) If there is not any obvious indentation hints, we do not
54 " alert our user's current indentation.
55 " (*) g:tex_indent_brace now only works if the open brace is the
56 " last character of that line.
57 " 2014/08/03 by Zhou Yichao <broken.zhou AT gmail.com>
58 " (*) Indent current line if last line has larger indentation
59 " 2014/08/09 by Zhou Yichao <broken.zhou AT gmail.com>
60 " (*) Add missing return value for s:GetEndIndentation(...)
61 "
52 " }}} 62 " }}}
53 63
54 " Document: {{{ 64 " Document: {{{
55 " 65 "
56 " To set the following options (ok, currently it's just one), add a line like 66 " To set the following options (ok, currently it's just one), add a line like
58 " to your ~/.vimrc. 68 " to your ~/.vimrc.
59 " 69 "
60 " * g:tex_indent_brace 70 " * g:tex_indent_brace
61 " 71 "
62 " If this variable is unset or non-zero, it will use smartindent-like style 72 " If this variable is unset or non-zero, it will use smartindent-like style
63 " for "{}" and "[]" 73 " for "{}" and "[]". Now this only works if the open brace is the last
74 " character of that line.
75 "
76 " % Example 1
77 " \usetikzlibrary{
78 " external
79 " }
80 "
81 " % Example 2
82 " \tikzexternalize[
83 " prefix=tikz]
64 " 84 "
65 " * g:tex_indent_items 85 " * g:tex_indent_items
66 " 86 "
67 " If this variable is set, item-environments are indented like Emacs does 87 " If this variable is set, item-environments are indented like Emacs does
68 " it, i.e., continuation lines are indented with a shiftwidth. 88 " it, i.e., continuation lines are indented with a shiftwidth.
96 " 116 "
97 " * g:tex_noindent_env 117 " * g:tex_noindent_env
98 " 118 "
99 " A list of environment names. separated with '\|', where no indentation is 119 " A list of environment names. separated with '\|', where no indentation is
100 " required. The default is 'document\|verbatim'. 120 " 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 "
109 " }}} 121 " }}}
110 122
111 " Only define the function once 123 " Only define the function once
112 if exists("b:did_indent") 124 if exists("b:did_indent")
113 finish 125 finish
124 let g:tex_indent_items = 1 136 let g:tex_indent_items = 1
125 endif 137 endif
126 if !exists("g:tex_indent_brace") 138 if !exists("g:tex_indent_brace")
127 let g:tex_indent_brace = 1 139 let g:tex_indent_brace = 1
128 endif 140 endif
129 if !exists("g:tex_indent_and") 141 if !exists("g:tex_max_scan_line")
130 let g:tex_indent_and = 1 142 let g:tex_max_scan_line = 60
131 endif 143 endif
132 if g:tex_indent_items 144 if g:tex_indent_items
133 if !exists("g:tex_itemize_env") 145 if !exists("g:tex_itemize_env")
134 let g:tex_itemize_env = 'itemize\|description\|enumerate\|thebibliography' 146 let g:tex_itemize_env = 'itemize\|description\|enumerate\|thebibliography'
135 endif 147 endif
136 if !exists('g:tex_items') 148 if !exists('g:tex_items')
137 let g:tex_items = '\\bibitem\|\\item' 149 let g:tex_items = '\\bibitem\|\\item'
138 endif 150 endif
139 else 151 else
140 let g:tex_items = '' 152 let g:tex_items = ''
141 endif
142
143 if !exists("g:tex_indent_paretheses")
144 let g:tex_indent_paretheses = 1
145 endif 153 endif
146 154
147 if !exists("g:tex_noindent_env") 155 if !exists("g:tex_noindent_env")
148 let g:tex_noindent_env = 'document\|verbatim\|lstlisting' 156 let g:tex_noindent_env = 'document\|verbatim\|lstlisting'
149 endif "}}} 157 endif "}}}
158 " }}} 166 " }}}
159 167
160 function! GetTeXIndent() " {{{ 168 function! GetTeXIndent() " {{{
161 " Find a non-blank line above the current line. 169 " Find a non-blank line above the current line.
162 let lnum = prevnonblank(v:lnum - 1) 170 let lnum = prevnonblank(v:lnum - 1)
171 let cnum = v:lnum
163 172
164 " Comment line is not what we need. 173 " Comment line is not what we need.
165 while lnum != 0 && getline(lnum) =~ '^\s*%' 174 while lnum != 0 && getline(lnum) =~ '^\s*%'
166 let lnum = prevnonblank(lnum - 1) 175 let lnum = prevnonblank(lnum - 1)
167 endwhile 176 endwhile
169 " At the start of the file use zero indent. 178 " At the start of the file use zero indent.
170 if lnum == 0 179 if lnum == 0
171 return 0 180 return 0
172 endif 181 endif
173 182
174 let line = substitute(getline(lnum), '%.*', ' ','g') " last line 183 let line = substitute(getline(lnum), '\s*%.*', '','g') " last line
175 let cline = substitute(getline(v:lnum), '%.*', ' ', 'g') " current line 184 let cline = substitute(getline(v:lnum), '\s*%.*', '', 'g') " current line
176 185
177 " We are in verbatim, so do what our user what. 186 " We are in verbatim, so do what our user what.
178 if synIDattr(synID(v:lnum, indent(v:lnum), 1), "name") == "texZone" 187 if synIDattr(synID(v:lnum, indent(v:lnum), 1), "name") == "texZone"
179 if empty(cline) 188 if empty(cline)
180 return indent(lnum) 189 return indent(lnum)
181 else 190 else
182 return indent(v:lnum) 191 return indent(v:lnum)
183 end 192 end
184 endif 193 endif
185 194
186 " You want to align with "&"
187 if g:tex_indent_and
188 " Align only when current line start with "&"
189 if line =~ '&.*\\\\' && cline =~ '^\s*&'
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 195 if lnum == 0
202 return 0 196 return 0
203 endif 197 endif
204 198
205 let ind = indent(lnum) 199 let ind = indent(lnum)
200 let stay = 1
206 201
207 " New code for comment: retain the indent of current line 202 " New code for comment: retain the indent of current line
208 if cline =~ '^\s*%' 203 if cline =~ '^\s*%'
209 return indent(v:lnum) 204 return indent(v:lnum)
210 endif 205 endif
214 ""if line =~ '^\s*\\begin{\(.*\)}' && line !~ 'verbatim' 209 ""if line =~ '^\s*\\begin{\(.*\)}' && line !~ 'verbatim'
215 " LH modification : \begin does not always start a line 210 " LH modification : \begin does not always start a line
216 " ZYC modification : \end after \begin won't cause wrong indent anymore 211 " ZYC modification : \end after \begin won't cause wrong indent anymore
217 if line =~ '\\begin{.*}' && line !~ g:tex_noindent_env 212 if line =~ '\\begin{.*}' && line !~ g:tex_noindent_env
218 let ind = ind + &sw 213 let ind = ind + &sw
214 let stay = 0
219 215
220 if g:tex_indent_items 216 if g:tex_indent_items
221 " Add another sw for item-environments 217 " Add another sw for item-environments
222 if line =~ g:tex_itemize_env 218 if line =~ g:tex_itemize_env
223 let ind = ind + &sw 219 let ind = ind + &sw
220 let stay = 0
224 endif 221 endif
225 endif 222 endif
226 endif 223 endif
227 224
225 if cline =~ '\\end{.*}'
226 let retn = s:GetEndIndentation(v:lnum)
227 if retn != -1
228 return retn
229 endif
230 end
228 " Subtract a 'shiftwidth' when an environment ends 231 " Subtract a 'shiftwidth' when an environment ends
229 if cline =~ '\\end{.*}' && cline !~ g:tex_noindent_env 232 if cline =~ '\\end{.*}'
230 233 \ && cline !~ g:tex_noindent_env
234 \ && cline !~ '\\begin{.*}.*\\end{.*}'
231 if g:tex_indent_items 235 if g:tex_indent_items
232 " Remove another sw for item-environments 236 " Remove another sw for item-environments
233 if cline =~ g:tex_itemize_env 237 if cline =~ g:tex_itemize_env
234 let ind = ind - &sw 238 let ind = ind - &sw
239 let stay = 0
235 endif 240 endif
236 endif 241 endif
237 242
238 let ind = ind - &sw 243 let ind = ind - &sw
244 let stay = 0
239 endif 245 endif
240 246
241 if g:tex_indent_brace 247 if g:tex_indent_brace
242 let sum1 = 0 248 let char = line[strlen(line)-1]
243 for i in range(0, strlen(line)-1) 249 if char == '[' || char == '{'
244 if line[i] == "}" || line[i] == "]" || 250 let ind += &sw
245 \ strpart(line, i, 7) == '\right)' 251 let stay = 0
246 let sum1 = max([0, sum1-1]) 252 endif
247 endif 253
248 if line[i] == "{" || line[i] == "[" || 254 let cind = indent(v:lnum)
249 \ strpart(line, i, 6) == '\left(' 255 let char = cline[cind]
250 let sum1 += 1 256 if (char == ']' || char == '}') &&
257 \ s:CheckPairedIsLastCharacter(v:lnum, cind)
258 let ind -= &sw
259 let stay = 0
260 endif
261
262 for i in range(indent(lnum)+1, strlen(line)-1)
263 let char = line[i]
264 if char == ']' || char == '}'
265 if s:CheckPairedIsLastCharacter(lnum, i)
266 let ind -= &sw
267 let stay = 0
268 endif
251 endif 269 endif
252 endfor 270 endfor
253
254 let sum2 = 0
255 for i in reverse(range(0, strlen(cline)-1))
256 if cline[i] == "{" || cline[i] == "[" ||
257 \ strpart(cline, i, 6) == '\left('
258 let sum2 = max([0, sum2-1])
259 endif
260 if cline[i] == "}" || cline[i] == "]" ||
261 \ strpart(cline, i, 7) == '\right)'
262 let sum2 += 1
263 endif
264 endfor
265
266 let ind += (sum1 - sum2) * &sw
267 endif
268
269 if g:tex_indent_paretheses
270 endif 271 endif
271 272
272 " Special treatment for 'item' 273 " Special treatment for 'item'
273 " ---------------------------- 274 " ----------------------------
274 275
275 if g:tex_indent_items 276 if g:tex_indent_items
276
277 " '\item' or '\bibitem' itself: 277 " '\item' or '\bibitem' itself:
278 if cline =~ g:tex_items 278 if cline =~ g:tex_items
279 let ind = ind - &sw 279 let ind = ind - &sw
280 endif 280 let stay = 0
281 281 endif
282 " lines following to '\item' are intented once again: 282 " lines following to '\item' are intented once again:
283 if line =~ g:tex_items 283 if line =~ g:tex_items
284 let ind = ind + &sw 284 let ind = ind + &sw
285 endif 285 let stay = 0
286 286 endif
287 endif 287 endif
288 288
289 return ind 289 if stay
290 " If there is no obvious indentation hint, we trust our user.
291 if empty(cline)
292 return ind
293 else
294 return max([indent(v:lnum), s:GetLastBeginIndentation(v:lnum)])
295 endif
296 else
297 return ind
298 endif
299 endfunction "}}}
300
301 function! s:GetLastBeginIndentation(lnum) " {{{
302 let matchend = 1
303 for lnum in range(a:lnum-1, max([a:lnum - g:tex_max_scan_line, 1]), -1)
304 let line = getline(lnum)
305 if line =~ '\\end{.*}'
306 let matchend += 1
307 endif
308 if line =~ '\\begin{.*}'
309 let matchend -= 1
310 endif
311 if matchend == 0
312 if line =~ g:tex_itemize_env
313 return indent(lnum) + 2 * &sw
314 endif
315 if line =~ g:tex_noindent_env
316 return indent(lnum)
317 endif
318 return indent(lnum) + &sw
319 endif
320 endfor
321 return -1
322 endfunction
323
324 function! s:GetEndIndentation(lnum) " {{{
325 if getline(a:lnum) =~ '\\begin{.*}.*\\end{.*}'
326 return -1
327 endif
328
329 let min_indent = 100
330 let matchend = 1
331 for lnum in range(a:lnum-1, max([a:lnum-g:tex_max_scan_line, 1]), -1)
332 let line = getline(lnum)
333 if line =~ '\\end{.*}'
334 let matchend += 1
335 endif
336 if line =~ '\\begin{.*}'
337 let matchend -= 1
338 endif
339 if matchend == 0
340 return indent(lnum)
341 endif
342 if !empty(line)
343 let min_indent = min([min_indent, indent(lnum)])
344 endif
345 endfor
346 return min_indent - &sw
347 endfunction
348
349 " Most of the code is from matchparen.vim
350 function! s:CheckPairedIsLastCharacter(lnum, col) "{{{
351 " Get the character under the cursor and check if it's in 'matchpairs'.
352 let c_lnum = a:lnum
353 let c_col = a:col+1
354
355
356 let c = getline(c_lnum)[c_col-1]
357 let plist = split(&matchpairs, '.\zs[:,]')
358 let i = index(plist, c)
359 if i < 0
360 return 0
361 endif
362
363 " Figure out the arguments for searchpairpos().
364 if i % 2 == 0
365 let s_flags = 'nW'
366 let c2 = plist[i + 1]
367 else
368 let s_flags = 'nbW'
369 let c2 = c
370 let c = plist[i - 1]
371 endif
372 if c == '['
373 let c = '\['
374 let c2 = '\]'
375 endif
376
377 " Find the match. When it was just before the cursor move it there for a
378 " moment.
379 let save_cursor = winsaveview()
380 call cursor(c_lnum, c_col)
381
382 " When not in a string or comment ignore matches inside them.
383 " We match "escape" for special items, such as lispEscapeSpecial.
384 let s_skip ='synIDattr(synID(line("."), col("."), 0), "name") ' .
385 \ '=~? "string\\|character\\|singlequote\\|escape\\|comment"'
386 execute 'if' s_skip '| let s_skip = 0 | endif'
387
388 let stopline = max([0, c_lnum - g:tex_max_scan_line])
389
390 " Limit the search time to 300 msec to avoid a hang on very long lines.
391 " This fails when a timeout is not supported.
392 try
393 let [m_lnum, m_col] = searchpairpos(c, '', c2, s_flags, s_skip, stopline, 100)
394 catch /E118/
395 endtry
396
397 call winrestview(save_cursor)
398
399 if m_lnum > 0
400 let line = getline(m_lnum)
401 return strlen(line) == m_col
402 endif
403
404 return 0
290 endfunction "}}} 405 endfunction "}}}
291 406
292 let &cpo = s:cpo_save 407 let &cpo = s:cpo_save
293 unlet s:cpo_save 408 unlet s:cpo_save
294 409