File: tex.vim

package info (click to toggle)
vim-vimtex 2.16-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 8,660 kB
  • sloc: makefile: 367; python: 103
file content (329 lines) | stat: -rw-r--r-- 9,396 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
" VimTeX - LaTeX plugin for Vim
"
" Maintainer: Karl Yngve LervÄg
" Email:      karl.yngve@gmail.com
"

if exists('b:did_indent')
  finish
endif

call vimtex#options#init()

if !g:vimtex_indent_enabled | finish | endif

let b:did_vimtex_indent = 1
let b:did_indent = 1

let s:cpo_save = &cpoptions
set cpoptions&vim

setlocal autoindent
setlocal indentexpr=VimtexIndentExpr()
setlocal indentkeys=!^F,o,O,0(,0),],},\&,0=\\item\ ,0=\\item[,0=\\else,0=\\fi

" Add standard closing math delimiters to indentkeys
for s:delim in [
      \ 'rangle', 'rbrace', 'rvert', 'rVert', 'rfloor', 'rceil', 'urcorner']
  let &l:indentkeys .= ',0=\' . s:delim
endfor


function! VimtexIndentExpr() abort " {{{1
  " This wrapper function is used because of rnoweb[0] that "misuses" the
  " indentexpr and assumes it takes no arguments.
  "
  " [0]: /usr/share/nvim/runtime/indent/rnoweb.vim:21

  return VimtexIndent(v:lnum)
endfunction

"}}}
function! VimtexIndent(lnum) abort " {{{1
  let s:sw = shiftwidth()

  let [l:prev_lnum, l:prev_line] = s:get_prev_lnum(prevnonblank(a:lnum - 1))
  if l:prev_lnum == 0 | return indent(a:lnum) | endif
  let l:line = s:clean_line(getline(a:lnum))

  " Check for verbatim modes
  if s:in_verbatim(a:lnum)
    return empty(l:line) ? indent(l:prev_lnum) : indent(a:lnum)
  endif

  " Use previous indentation for comments
  if l:line =~# '^\s*%'
    return indent(a:lnum)
  endif

  " Align on ampersands
  let l:ind = s:indent_amps.check(a:lnum, l:line, l:prev_lnum, l:prev_line)
  if s:indent_amps.finished | return l:ind | endif
  let l:prev_lnum = s:indent_amps.prev_lnum
  let l:prev_line = s:indent_amps.prev_line

  " Indent environments, delimiters, and conditionals
  let l:ind += s:indent_envs(l:line, l:prev_line)
  let l:ind += s:indent_items(l:line, a:lnum, l:prev_line, l:prev_lnum)
  let l:ind += s:indent_delims(l:line, a:lnum, l:prev_line, l:prev_lnum)
  let l:ind += s:indent_conditionals(l:line, a:lnum, l:prev_line, l:prev_lnum)

  " Indent tikz commands
  if g:vimtex_indent_tikz_commands
    let l:ind += s:indent_tikz(l:prev_lnum, l:prev_line)
  endif

  return l:ind < 0 ? 0 : l:ind
endfunction

"}}}

function! s:get_prev_lnum(lnum) abort " {{{1
  let l:lnum = a:lnum
  let l:line = getline(l:lnum)

  while l:lnum > 0 && (l:line =~# '^\s*%' || s:in_verbatim(l:lnum))
    let l:lnum = prevnonblank(l:lnum - 1)
    let l:line = getline(l:lnum)
  endwhile

  return [
        \ l:lnum,
        \ l:lnum > 0 ? s:clean_line(l:line) : '',
        \]
endfunction

" }}}1
function! s:clean_line(line) abort " {{{1
  return substitute(a:line, '\s*\\\@<!%.*', '', '')
endfunction

" }}}1
function! s:in_verbatim(lnum) abort " {{{1
  let l:synstack = vimtex#syntax#stack(a:lnum, col([a:lnum, '$']) - 2)

  return match(l:synstack, '\v^tex%(Lst|Verb|Markdown|Minted)Zone') >= 0
        \ && match(l:synstack, '\v^tex%(Minted)?Env') < 0
endfunction

" }}}1

let s:indent_amps = {}
function! s:indent_amps.check(lnum, cline, plnum, pline) abort dict " {{{1
  let self.finished = 0
  let self.amp_ind = -1
  let self.init_ind = -1
  let self.prev_lnum = a:plnum
  let self.prev_line = a:pline
  let self.prev_ind = a:plnum > 0 ? indent(a:plnum) : 0
  if !g:vimtex_indent_on_ampersands | return self.prev_ind | endif

  if a:cline =~# s:re_align
        \ || a:cline =~# s:re_amp
        \ || a:cline =~# '^\v\s*\\%(end|])'
    call self.parse_context(a:lnum, a:cline)
  endif

  if a:cline =~# s:re_align
    let self.finished = 1
    let l:ind_diff =
          \   strdisplaywidth(strpart(a:cline, 0, match(a:cline, s:re_amp)))
          \ - strdisplaywidth(strpart(a:cline, 0, match(a:cline, '\S')))
    return self.amp_ind - l:ind_diff
  endif

  if self.amp_ind >= 0
        \ && (a:cline =~# '^\v\s*\\%(end|])' || a:cline =~# s:re_amp)
    let self.prev_lnum = self.init_lnum
    let self.prev_line = self.init_line
    return self.init_ind
  endif

  return self.prev_ind
endfunction

let s:re_amp = g:vimtex#re#not_bslash . '\&'
let s:re_align = '^[ \t\\]*' . s:re_amp

" }}}1
function! s:indent_amps.parse_context(lnum, line) abort dict " {{{1
  let l:depth = 1
  let l:lnum = prevnonblank(a:lnum - 1)

  while l:lnum >= 1
    let l:line = getline(l:lnum)

    if l:line =~# s:re_depth_end
      let l:depth += 1
    endif

    if l:line =~# s:re_depth_beg
      let l:depth -= 1
      if l:depth == 0
        let self.init_lnum = l:lnum
        let self.init_line = l:line
        let self.init_ind = indent(l:lnum)
        break
      endif
    endif

    if l:depth == 1 && l:line =~# s:re_amp
      if self.amp_ind < 0
        let self.amp_ind = strdisplaywidth(
              \ strpart(l:line, 0, match(l:line, s:re_amp)))
      endif
      if l:line !~# s:re_align
        let self.init_lnum = l:lnum
        let self.init_line = l:line
        let self.init_ind = indent(l:lnum)
        break
      endif
    endif

    let l:lnum = prevnonblank(l:lnum - 1)
  endwhile
endfunction

let s:re_depth_beg = g:vimtex#re#not_bslash . '\\%(begin\s*\{|[|\w+\{\s*$)'
let s:re_depth_end = g:vimtex#re#not_bslash . '\\end\s*\{\w+\*?}|^\s*%(}|\\])'

" }}}1

function! s:indent_envs(line, prev_line) abort " {{{1
  let l:ind = 0

  let l:ind += s:sw*(
        \    a:prev_line =~# s:envs_begin
        \ && a:prev_line !~# s:envs_end
        \ && a:prev_line !~# s:envs_ignored)
  let l:ind -= s:sw*(
        \    a:line !~# s:envs_begin
        \ && a:line =~# s:envs_end
        \ && a:line !~# s:envs_ignored)

  return l:ind
endfunction

let s:envs_begin = '\\begin{.*}\|\\\@<!\\\['
let s:envs_end = '\\end{.*}\|\\\]'
let s:envs_ignored = '\v<%(' . join(g:vimtex_indent_ignored_envs, '|') . ')>'

" }}}1
function! s:indent_items(line, lnum, prev_line, prev_lnum) abort " {{{1
  if s:envs_lists_empty | return 0 | endif

  if a:prev_line =~# s:envs_item
        \ && (a:line !~# s:envs_enditem
        \     || (a:line =~# s:envs_item && a:prev_line =~# s:envs_beglist))
    return s:sw
  elseif a:line =~# s:envs_endlist && a:prev_line !~# s:envs_begitem
    return -s:sw
  elseif a:line =~# s:envs_item && a:prev_line !~# s:envs_item
    let l:prev_lnum = a:prev_lnum
    let l:prev_line = a:prev_line
    while l:prev_lnum >= 1
      if l:prev_line =~# s:envs_begitem
        return -s:sw*(l:prev_line =~# s:envs_item)
      endif
      let l:prev_lnum = prevnonblank(l:prev_lnum - 1)
      let l:prev_line = getline(l:prev_lnum)
    endwhile
  endif

  return 0
endfunction

let s:envs_lists_empty = empty(g:vimtex_indent_lists)
let s:envs_lists = join(g:vimtex_indent_lists, '\|')
let s:envs_item = '^\s*\\item\>'
let s:envs_beglist = '\\begin{\%(' . s:envs_lists . '\)'
let s:envs_endlist =   '\\end{\%(' . s:envs_lists . '\)'
let s:envs_begitem = s:envs_item . '\|' . s:envs_beglist
let s:envs_enditem = s:envs_item . '\|' . s:envs_endlist

" }}}1
function! s:indent_delims(line, lnum, prev_line, prev_lnum) abort " {{{1
  if s:re_delim_trivial | return 0 | endif

  if s:re_opt.close_indented
    return s:sw*(vimtex#util#count(a:prev_line, s:re_open)
          \ - vimtex#util#count(a:prev_line, s:re_close))
  else
    return s:sw*(vimtex#util#count_open(a:prev_line, s:re_open, s:re_close)
          \      - vimtex#util#count_close(a:line, s:re_open, s:re_close))
  endif
endfunction

let s:re_opt = extend({
      \ 'open' : ['{'],
      \ 'close' : ['}'],
      \ 'close_indented' : 0,
      \ 'include_modified_math' : 1,
      \}, g:vimtex_indent_delims)
let s:re_open = join(s:re_opt.open, '\|')
let s:re_close = join(s:re_opt.close, '\|')
if s:re_opt.include_modified_math
  let s:re_open .= (empty(s:re_open) ? '' : '\|') . g:vimtex#delim#re.delim_math_mod.open
  let s:re_close .= (empty(s:re_close) ? '' : '\|') . g:vimtex#delim#re.delim_math_mod.close
endif
let s:re_delim_trivial = empty(s:re_open) || empty(s:re_close)

" }}}1
function! s:indent_conditionals(line, lnum, prev_line, prev_lnum) abort " {{{1
  if empty(s:conditionals) | return 0 | endif

  let l:ind = s:sw*(
        \    (a:prev_line =~# s:conditionals.open
        \     || a:prev_line =~# s:conditionals.else)
        \ && a:prev_line !~# s:conditionals.close)
  let l:ind -= s:sw*(
        \    a:line !~# s:conditionals.open
        \ && (a:line =~# s:conditionals.close
        \     || a:line =~# s:conditionals.else))

  return l:ind
endfunction

let s:conditionals = g:vimtex_indent_conditionals

" }}}1
function! s:indent_tikz(lnum, prev) abort " {{{1
  if !has_key(b:vimtex.packages, 'tikz') | return 0 | endif

  let l:synstack = vimtex#syntax#stack(a:lnum, 1)
  if match(l:synstack, '^texTikzZone') < 0 | return 0 | endif

  let l:env_lnum = search('\\begin\s*{tikzpicture\*\?}', 'bn')
  if l:env_lnum > 0 && l:env_lnum < a:lnum
    let l:prev_starts = a:prev =~# s:tikz_commands
    let l:prev_stops  = a:prev =~# ';\s*$'

    " Increase indent on tikz command start
    if l:prev_starts && ! l:prev_stops
      return s:sw
    endif

    " Decrease indent on tikz command end, i.e. on semicolon
    if ! l:prev_starts && l:prev_stops
      let l:context = join(getline(l:env_lnum, a:lnum-1), '')
      return -s:sw*(l:context =~# s:tikz_commands)
    endif
  endif

  return 0
endfunction

let s:tikz_commands = '\v\\%(' . join([
        \ 'draw',
        \ 'fill',
        \ 'path',
        \ 'node',
        \ 'coordinate',
        \ 'clip',
        \ 'add%(legendentry|plot)',
      \ ], '|') . ')'

" }}}1

let &cpoptions = s:cpo_save
unlet s:cpo_save