comparison runtime/indent/tex.vim @ 13912:a9fdf01085a8

Update runtime files. commit https://github.com/vim/vim/commit/7db25fed5de1be922b8cbb0328149469606a0424 Author: Bram Moolenaar <Bram@vim.org> Date: Sun May 13 00:02:36 2018 +0200 Update runtime files.
author Christian Brabandt <cb@256bit.org>
date Sun, 13 May 2018 00:15:05 +0200
parents a62eeee5f116
children cd513458728c
comparison
equal deleted inserted replaced
13911:c02f0e120b11 13912:a9fdf01085a8
1 " Vim indent file 1 " Vim indent file for TeX
2 " Language: LaTeX 2 " Language: TeX
3 " Maintainer: Yichao Zhou <broken.zhou AT gmail.com> 3 " Maintainer: Christian Brabandt <cb@256bit.org>
4 " Created: Sat, 16 Feb 2002 16:50:19 +0100 4 " Previous Maintainer: YiChao Zhou <broken.zhou AT gmail.com>
5 " Version: 1.0.0 5 " Latest Revision: 2017-05-03
6 " Version: 0.9.3
7 " Repository: https://github.com/chrisbra/vim-tex-indent
8 " Documention: :h ft-tex-indent
9 " Created: Sat, 16 Feb 2002 16:50:19 +0100
6 " Please email me if you found something I can do. Comments, bug report and 10 " Please email me if you found something I can do. Comments, bug report and
7 " feature request are welcome. 11 " feature request are welcome.
8 12
9 " Last Update: {{{ 13 " Last Update: {{{1
10 " 25th Sep 2002, by LH : 14 " 25th Sep 2002, by LH :
11 " (*) better support for the option 15 " (*) better support for the option
12 " (*) use some regex instead of several '||'. 16 " (*) use some regex instead of several '||'.
13 " Oct 9th, 2003, by JT: 17 " Oct 9th, 2003, by JT:
14 " (*) don't change indentation of lines starting with '%' 18 " (*) don't change indentation of lines starting with '%'
15 " 2005/06/15, Moshe Kaminsky <kaminsky AT math.huji.ac.il> 19 " 2005/06/15, Moshe Kaminsky <kaminsky AT math.huji.ac.il>
16 " (*) New variables: 20 " (*) New variables:
17 " g:tex_items, g:tex_itemize_env, g:tex_noindent_env 21 " g:tex_items, g:tex_itemize_env, g:tex_noindent_env
18 " 2011/3/6, by Yichao Zhou <broken.zhou AT gmail.com> 22 " 2011/3/6, by Zhou YiChao <broken.zhou AT gmail.com>
19 " (*) Don't change indentation of lines starting with '%' 23 " (*) Don't change indentation of lines starting with '%'
20 " I don't see any code with '%' and it doesn't work properly 24 " I don't see any code with '%' and it doesn't work properly
21 " so I add some code. 25 " so I add some code.
22 " (*) New features: Add smartindent-like indent for "{}" and "[]". 26 " (*) New features: Add smartindent-like indent for "{}" and "[]".
23 " (*) New variables: g:tex_indent_brace 27 " (*) New variables: g:tex_indent_brace
24 " 2011/9/25, by Yichao Zhou <broken.zhou AT gmail.com> 28 " 2011/9/25, by Zhou Yichao <broken.zhou AT gmail.com>
25 " (*) Bug fix: smartindent-like indent for "[]" 29 " (*) Bug fix: smartindent-like indent for "[]"
26 " (*) New features: Align with "&". 30 " (*) New features: Align with "&".
27 " (*) New variable: g:tex_indent_and. 31 " (*) New variable: g:tex_indent_and.
28 " 2011/10/23 by Yichao Zhou <broken.zhou AT gmail.com> 32 " 2011/10/23 by Zhou Yichao <broken.zhou AT gmail.com>
29 " (*) Bug fix: improve the smartindent-like indent for "{}" and 33 " (*) Bug fix: improve the smartindent-like indent for "{}" and
30 " "[]". 34 " "[]".
31 " 2012/02/27 by Yichao Zhou <broken.zhou AT gmail.com> 35 " 2012/02/27 by Zhou Yichao <broken.zhou AT gmail.com>
32 " (*) Bug fix: support default folding marker. 36 " (*) Bug fix: support default folding marker.
33 " (*) Indent with "&" is not very handy. Make it not enable by 37 " (*) Indent with "&" is not very handy. Make it not enable by
34 " default. 38 " default.
35 " 2012/03/06 by Yichao Zhou <broken.zhou AT gmail.com> 39 " 2012/03/06 by Zhou Yichao <broken.zhou AT gmail.com>
36 " (*) Modify "&" behavior and make it default again. Now "&" 40 " (*) Modify "&" behavior and make it default again. Now "&"
37 " won't align when there are more then one "&" in the previous 41 " won't align when there are more then one "&" in the previous
38 " line. 42 " line.
39 " (*) Add indent "\left(" and "\right)" 43 " (*) Add indent "\left(" and "\right)"
40 " (*) Trust user when in "verbatim" and "lstlisting" 44 " (*) Trust user when in "verbatim" and "lstlisting"
41 " 2012/03/11 by Yichao Zhou <broken.zhou AT gmail.com> 45 " 2012/03/11 by Zhou Yichao <broken.zhou AT gmail.com>
42 " (*) Modify "&" so that only indent when current line start with 46 " (*) Modify "&" so that only indent when current line start with
43 " "&". 47 " "&".
44 " 2012/03/12 by Yichao Zhou <broken.zhou AT gmail.com> 48 " 2012/03/12 by Zhou Yichao <broken.zhou AT gmail.com>
45 " (*) Modify indentkeys. 49 " (*) Modify indentkeys.
46 " 2012/03/18 by Yichao Zhou <broken.zhou AT gmail.com> 50 " 2012/03/18 by Zhou Yichao <broken.zhou AT gmail.com>
47 " (*) Add &cpo 51 " (*) Add &cpo
48 " 2013/05/02 by Yichao Zhou <broken.zhou AT gmail.com> 52 " 2013/05/02 by Zhou Yichao <broken.zhou AT gmail.com>
49 " (*) Fix problem about GetTeXIndent checker. Thank Albert Netymk 53 " (*) Fix problem about GetTeXIndent checker. Thank Albert Netymk
50 " for reporting this. 54 " for reporting this.
51 " 2014/06/23 by Yichao Zhou <broken.zhou AT gmail.com> 55 " 2014/06/23 by Zhou Yichao <broken.zhou AT gmail.com>
52 " (*) Remove the feature g:tex_indent_and because it is buggy. 56 " (*) Remove the feature g:tex_indent_and because it is buggy.
53 " (*) If there is not any obvious indentation hints, we do not 57 " (*) If there is not any obvious indentation hints, we do not
54 " alert our user's current indentation. 58 " alert our user's current indentation.
55 " (*) g:tex_indent_brace now only works if the open brace is the 59 " (*) g:tex_indent_brace now only works if the open brace is the
56 " last character of that line. 60 " last character of that line.
57 " 2014/08/03 by Yichao Zhou <broken.zhou AT gmail.com> 61 " 2014/08/03 by Zhou Yichao <broken.zhou AT gmail.com>
58 " (*) Indent current line if last line has larger indentation 62 " (*) Indent current line if last line has larger indentation
59 " 2016/11/08 by Yichao Zhou <broken.zhou AT gmail.com> 63 " 2014/08/09 by Zhou Yichao <broken.zhou AT gmail.com>
60 " (*) Fix problems for \[ and \]. Thanks Bruno for reporting. 64 " (*) Add missing return value for s:GetEndIndentation(...)
61 " 2017/04/30 by Yichao Zhou <broken.zhou AT gmail.com> 65 " 2017/05/02: new maintainer Christian Brabandt
62 " (*) Fix a bug between g:tex_noindent_env and g:tex_indent_items 66 " 2017/05/02: use shiftwidth() function
63 " Now g:tex_noindent_env='document\|verbatim\|itemize' (Emacs 67 " 2017/05/02: do not add indent when environment starts and ends
64 " style) is supported. Thanks Miles Wheeler for reporting. 68 " at previous line
65 " 2018/02/07 by Yichao Zhou <broken.zhou AT gmail.com> 69 " 2017/05/03: release 0.9.3 submitted for inclusion with Vim
66 " (*) Make indentation more smart in the normal mode
67 " 70 "
68 " }}} 71 " }}}
69 72 " Only define the function once {{{1
70 " Document: {{{
71 "
72 " To set the following options (ok, currently it's just one), add a line like
73 " let g:tex_indent_items = 1
74 " to your ~/.vimrc.
75 "
76 " * g:tex_indent_brace
77 "
78 " If this variable is unset or non-zero, it will use smartindent-like style
79 " for "{}" and "[]". Now this only works if the open brace is the last
80 " character of that line.
81 "
82 " % Example 1
83 " \usetikzlibrary{
84 " external
85 " }
86 "
87 " % Example 2
88 " \tikzexternalize[
89 " prefix=tikz]
90 "
91 " * g:tex_indent_items
92 "
93 " If this variable is set, item-environments are indented like Emacs does
94 " it, i.e., continuation lines are indented with a shiftwidth.
95 "
96 " set unset
97 " ------------------------------------------------------
98 " \begin{itemize} \begin{itemize}
99 " \item blablabla \item blablabla
100 " bla bla bla bla bla bla
101 " \item blablabla \item blablabla
102 " bla bla bla bla bla bla
103 " \end{itemize} \end{itemize}
104 "
105 "
106 " * g:tex_items
107 "
108 " A list of tokens to be considered as commands for the beginning of an item
109 " command. The tokens should be separated with '\|'. The initial '\' should
110 " be escaped. The default is '\\bibitem\|\\item'.
111 "
112 " * g:tex_itemize_env
113 "
114 " A list of environment names, separated with '\|', where the items (item
115 " commands matching g:tex_items) may appear. The default is
116 " 'itemize\|description\|enumerate\|thebibliography'.
117 "
118 " * g:tex_noindent_env
119 "
120 " A list of environment names. separated with '\|', where no indentation is
121 " required. The default is 'document\|verbatim'.
122 " }}}
123
124 " Only define the function once
125 if exists("b:did_indent") 73 if exists("b:did_indent")
126 finish 74 finish
127 endif 75 endif
128 76
129 let s:cpo_save = &cpo 77 let s:cpo_save = &cpo
130 set cpo&vim 78 set cpo&vim
131 79 " Define global variable {{{1
132 " Define global variable {{{
133
134 let b:did_indent = 1 80 let b:did_indent = 1
135 81
136 if !exists("g:tex_indent_items") 82 if !exists("g:tex_indent_items")
137 let g:tex_indent_items = 1 83 let g:tex_indent_items = 1
138 endif 84 endif
145 if g:tex_indent_items 91 if g:tex_indent_items
146 if !exists("g:tex_itemize_env") 92 if !exists("g:tex_itemize_env")
147 let g:tex_itemize_env = 'itemize\|description\|enumerate\|thebibliography' 93 let g:tex_itemize_env = 'itemize\|description\|enumerate\|thebibliography'
148 endif 94 endif
149 if !exists('g:tex_items') 95 if !exists('g:tex_items')
150 let g:tex_items = '\\bibitem\|\\item' 96 let g:tex_items = '\\bibitem\|\\item'
151 endif 97 endif
152 else 98 else
153 let g:tex_items = '' 99 let g:tex_items = ''
154 endif 100 endif
155 101
156 if !exists("g:tex_noindent_env") 102 if !exists("g:tex_noindent_env")
157 let g:tex_noindent_env = 'document\|verbatim\|lstlisting' 103 let g:tex_noindent_env = 'document\|verbatim\|lstlisting'
158 endif "}}} 104 endif "}}}
159 105 " VIM Setting " {{{1
160 " VIM Setting " {{{
161 setlocal autoindent 106 setlocal autoindent
162 setlocal nosmartindent 107 setlocal nosmartindent
163 setlocal indentexpr=GetTeXIndent() 108 setlocal indentexpr=GetTeXIndent()
164 setlocal indentkeys& 109 setlocal indentkeys&
165 exec 'setlocal indentkeys+=[,(,{,),},],\&' . substitute(g:tex_items, '^\|\(\\|\)', ',=', 'g') 110 exec 'setlocal indentkeys+=[,(,{,),},],\&' . substitute(g:tex_items, '^\|\(\\|\)', ',=', 'g')
166 let g:tex_items = '^\s*' . substitute(g:tex_items, '^\(\^\\s\*\)*', '', '') 111 let g:tex_items = '^\s*' . substitute(g:tex_items, '^\(\^\\s\*\)*', '', '')
112
113 let b:undo_indent = 'setlocal indentexpr< indentkeys< smartindent< autoindent<'
167 " }}} 114 " }}}
168 115 function! GetTeXIndent() " {{{1
169 function! GetTeXIndent() " {{{
170 " Find a non-blank line above the current line. 116 " Find a non-blank line above the current line.
171 let lnum = prevnonblank(v:lnum - 1) 117 let lnum = prevnonblank(v:lnum - 1)
172 let cnum = v:lnum 118 let cnum = v:lnum
173 119
174 " Comment line is not what we need. 120 " Comment line is not what we need.
176 let lnum = prevnonblank(lnum - 1) 122 let lnum = prevnonblank(lnum - 1)
177 endwhile 123 endwhile
178 124
179 " At the start of the file use zero indent. 125 " At the start of the file use zero indent.
180 if lnum == 0 126 if lnum == 0
181 return 0 127 return 0
182 endif 128 endif
183 129
184 let line = substitute(getline(lnum), '\s*%.*', '','g') " last line 130 let line = substitute(getline(lnum), '\s*%.*', '','g') " last line
185 let cline = substitute(getline(v:lnum), '\s*%.*', '', 'g') " current line 131 let cline = substitute(getline(v:lnum), '\s*%.*', '', 'g') " current line
186 132
190 return indent(lnum) 136 return indent(lnum)
191 else 137 else
192 return indent(v:lnum) 138 return indent(v:lnum)
193 end 139 end
194 endif 140 endif
195 141
196 if lnum == 0 142 if lnum == 0
197 return 0 143 return 0
198 endif 144 endif
199 145
200 let ind = indent(lnum) 146 let ind = indent(lnum)
201 let stay = 1 147 let stay = 1
202 148
203 " New code for comment: retain the indent of current line 149 " New code for comment: retain the indent of current line
204 if cline =~ '^\s*%' 150 if cline =~ '^\s*%'
205 return indent(v:lnum) 151 return indent(v:lnum)
206 endif 152 endif
207 153
208 " Add a 'shiftwidth' after beginning of environments. 154 " Add a 'shiftwidth' after beginning of environments
209 " Don't add it for \begin{document} and \begin{verbatim} 155 " But don't do it for g:tex_noindent_env or when it also ends at the
210 " if line =~ '^\s*\\begin{\(.*\)}' && line !~ 'verbatim' 156 " previous line.
211 " LH modification : \begin does not always start a line 157 if line =~ '\\begin{.*}' && line !~ '\\end{.*}' && line !~ g:tex_noindent_env
212 " ZYC modification : \end after \begin won't cause wrong indent anymore 158 let ind = ind + shiftwidth()
213 if line =~ '\\begin{.*}' 159 let stay = 0
214 if line !~ g:tex_noindent_env
215 let ind = ind + shiftwidth()
216 let stay = 0
217 endif
218 160
219 if g:tex_indent_items 161 if g:tex_indent_items
220 " Add another sw for item-environments 162 " Add another sw for item-environments
221 if line =~ g:tex_itemize_env 163 if line =~ g:tex_itemize_env
222 let ind = ind + shiftwidth() 164 let ind = ind + shiftwidth()
246 let ind = ind - shiftwidth() 188 let ind = ind - shiftwidth()
247 let stay = 0 189 let stay = 0
248 endif 190 endif
249 191
250 if g:tex_indent_brace 192 if g:tex_indent_brace
251 if line =~ '[[{]$' 193 let char = line[strlen(line)-1]
194 if char == '[' || char == '{'
252 let ind += shiftwidth() 195 let ind += shiftwidth()
253 let stay = 0 196 let stay = 0
254 endif 197 endif
255 198
256 if cline =~ '^\s*\\\?[\]}]' && s:CheckPairedIsLastCharacter(v:lnum, indent(v:lnum)) 199 let cind = indent(v:lnum)
200 let char = cline[cind]
201 if (char == ']' || char == '}') &&
202 \ s:CheckPairedIsLastCharacter(v:lnum, cind)
257 let ind -= shiftwidth() 203 let ind -= shiftwidth()
258 let stay = 0 204 let stay = 0
259 endif 205 endif
260 206
261 if line !~ '^\s*\\\?[\]}]' 207 for i in range(indent(lnum)+1, strlen(line)-1)
262 for i in range(indent(lnum)+1, strlen(line)-1) 208 let char = line[i]
263 let char = line[i] 209 if char == ']' || char == '}'
264 if char == ']' || char == '}' 210 if s:CheckPairedIsLastCharacter(lnum, i)
265 if s:CheckPairedIsLastCharacter(lnum, i) 211 let ind -= shiftwidth()
266 let ind -= shiftwidth() 212 let stay = 0
267 let stay = 0
268 endif
269 endif 213 endif
270 endfor 214 endif
271 endif 215 endfor
272 endif 216 endif
273 217
274 " Special treatment for 'item' 218 " Special treatment for 'item'
275 " ---------------------------- 219 " ----------------------------
276 220
285 let ind = ind + shiftwidth() 229 let ind = ind + shiftwidth()
286 let stay = 0 230 let stay = 0
287 endif 231 endif
288 endif 232 endif
289 233
290 if stay && mode() == 'i' 234 if stay
291 " If there is no obvious indentation hint, and indentation is triggered 235 " If there is no obvious indentation hint, we trust our user.
292 " in insert mode, we trust our user.
293 if empty(cline) 236 if empty(cline)
294 return ind 237 return ind
295 else 238 else
296 return max([indent(v:lnum), s:GetLastBeginIndentation(v:lnum)]) 239 return max([indent(v:lnum), s:GetLastBeginIndentation(v:lnum)])
297 endif 240 endif
298 else 241 else
299 return ind 242 return ind
300 endif 243 endif
301 endfunction "}}} 244 endfunction "}}}
302 245 function! s:GetLastBeginIndentation(lnum) " {{{1
303 function! s:GetLastBeginIndentation(lnum) " {{{
304 let matchend = 1 246 let matchend = 1
305 for lnum in range(a:lnum-1, max([a:lnum - g:tex_max_scan_line, 1]), -1) 247 for lnum in range(a:lnum-1, max([a:lnum - g:tex_max_scan_line, 1]), -1)
306 let line = getline(lnum) 248 let line = getline(lnum)
307 if line =~ '\\end{.*}' 249 if line =~ '\\end{.*}'
308 let matchend += 1 250 let matchend += 1
309 endif 251 endif
310 if line =~ '\\begin{.*}' 252 if line =~ '\\begin{.*}'
311 let matchend -= 1 253 let matchend -= 1
312 endif 254 endif
313 if matchend == 0 255 if matchend == 0
256 if line =~ g:tex_itemize_env
257 return indent(lnum) + 2 * shiftwidth()
258 endif
314 if line =~ g:tex_noindent_env 259 if line =~ g:tex_noindent_env
315 return indent(lnum) 260 return indent(lnum)
316 endif
317 if line =~ g:tex_itemize_env
318 return indent(lnum) + 2 * shiftwidth()
319 endif 261 endif
320 return indent(lnum) + shiftwidth() 262 return indent(lnum) + shiftwidth()
321 endif 263 endif
322 endfor 264 endfor
323 return -1 265 return -1
324 endfunction 266 endfunction
325 267
326 function! s:GetEndIndentation(lnum) " {{{ 268 function! s:GetEndIndentation(lnum) " {{{1
327 if getline(a:lnum) =~ '\\begin{.*}.*\\end{.*}' 269 if getline(a:lnum) =~ '\\begin{.*}.*\\end{.*}'
328 return -1 270 return -1
329 endif 271 endif
330 272
331 let min_indent = 100 273 let min_indent = 100
348 return min_indent - shiftwidth() 290 return min_indent - shiftwidth()
349 endfunction 291 endfunction
350 292
351 " Most of the code is from matchparen.vim 293 " Most of the code is from matchparen.vim
352 function! s:CheckPairedIsLastCharacter(lnum, col) "{{{ 294 function! s:CheckPairedIsLastCharacter(lnum, col) "{{{
295 " Get the character under the cursor and check if it's in 'matchpairs'.
353 let c_lnum = a:lnum 296 let c_lnum = a:lnum
354 let c_col = a:col+1 297 let c_col = a:col+1
355 298
356 let line = getline(c_lnum) 299
357 if line[c_col-1] == '\' 300 let c = getline(c_lnum)[c_col-1]
358 let c_col = c_col + 1
359 endif
360 let c = line[c_col-1]
361
362 let plist = split(&matchpairs, '.\zs[:,]') 301 let plist = split(&matchpairs, '.\zs[:,]')
363 let i = index(plist, c) 302 let i = index(plist, c)
364 if i < 0 303 if i < 0
365 return 0 304 return 0
366 endif 305 endif
405 let line = getline(m_lnum) 344 let line = getline(m_lnum)
406 return strlen(line) == m_col 345 return strlen(line) == m_col
407 endif 346 endif
408 347
409 return 0 348 return 0
410 endfunction "}}} 349 endfunction
411 350 " Reset cpo setting {{{1
412 let &cpo = s:cpo_save 351 let &cpo = s:cpo_save
413 unlet s:cpo_save 352 unlet s:cpo_save
414 353
415 " vim: set sw=4 textwidth=80: 354 " vim: set sw=4 textwidth=80: