Skip to content

Commit

Permalink
Add support for indent conditionals (#1078)
Browse files Browse the repository at this point in the history
  • Loading branch information
lervag committed Mar 16, 2018
1 parent fa3a8ea commit 203d52f
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
6 changes: 6 additions & 0 deletions autoload/vimtex.vim
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,12 @@ function! vimtex#init_options() " {{{1
\ { 'lhs' : 'vr', 'rhs' : '\varrho' },
\])

call s:init_option('vimtex_indent_conditionals', {
\ 'open': '\v\\if%(num|false)?>',
\ 'else': '\\else\>',
\ 'close': '\\fi\>',
\})

call s:init_option('vimtex_index_hide_line_numbers', 1)
call s:init_option('vimtex_index_resize', 0)
call s:init_option('vimtex_index_show_help', 1)
Expand Down
11 changes: 11 additions & 0 deletions doc/vimtex.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1232,6 +1232,17 @@ Options~

Default value: 1

*g:vimtex_indent_conditionals*
This is a dictionary that defines regexes for indenting conditionals. Set it
to an empty dictionary to disable this type of indentation.

Default value: >
let g:vimtex_indent_conditionals = {
\ 'open': '\v\\if%(num|false)?>',
\ 'else': '\\else\>',
\ 'close': '\\fi\>',
\}
*g:vimtex_indent_delims*
A dictionary that defines the list of opening and closing delimiters (in
regex form) that should add/reduce indents.
Expand Down
20 changes: 20 additions & 0 deletions indent/tex.vim
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ function! VimtexIndent(lnum) abort " {{{1
let l:ind = indent(l:prev_lnum)
let l:ind += s:indent_envs(l:line, l:prev_line)
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)
let l:ind += s:indent_tikz(l:prev_lnum, l:prev_line)
return l:ind
endfunction
Expand Down Expand Up @@ -167,6 +168,25 @@ if s:re_opt.include_modified_math
let s:re_close .= '\|' . g:vimtex#delim#re.delim_mod_math.close
endif

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

if a:line =~# g:vimtex_indent_conditionals.close
silent! unlet s:conditional_opened
return -s:sw
elseif get(s:, 'conditional_opened')
\ && a:line =~# g:vimtex_indent_conditionals.else
return -s:sw
elseif get(s:, 'conditional_opened')
\ && a:prev_line =~# g:vimtex_indent_conditionals.else
return s:sw
elseif a:prev_line =~# g:vimtex_indent_conditionals.open
let s:conditional_opened = 1
return s:sw
endif
endfunction

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

0 comments on commit 203d52f

Please sign in to comment.