aboutsummaryrefslogtreecommitdiff
path: root/vim/autoload/indent.vim
blob: 5f62fb0b678fa3948abb4c7b80cb0d92260f68db (plain) (blame)
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
" Set the current buffer to space indent
function! indent#Spaces(...) abort
  setlocal expandtab

  " If an argument was provided, use that for the number of spaces; otherwise,
  " set 'shiftwidth' to 0, which then copies 'tabstop'
  let &l:shiftwidth = a:0
        \ ? a:1
        \ : 0

  " If we have the patch that supports it, set 'softtabstop' to dynamically
  " mirror the value of 'shiftwidth'; failing that, just copy it
  let &l:softtabstop = patch#('7.3.693')
        \ ? -1
        \ : &l:shiftwidth

  call indent#Undo()
endfunction

" Set the current buffer to tab indent
function! indent#Tabs() abort
  setlocal noexpandtab
  setlocal shiftwidth< softtabstop<
  call indent#Undo()
endfunction

" Add commands to b:undo_indent to clean up buffer-local indentation changes
" on a change of filetype
function! indent#Undo() abort

  " Check and set a flag so that we only do this once per buffer
  if exists('b:undo_indent_type_set')
    return
  endif
  let b:undo_indent_type_set = 1

  " Either set or append relevant commands to b:undo_indent
  let l:undo = 'setlocal expandtab< shiftwidth< softtabstop< tabstop<'
  if exists('b:undo_indent')
    let b:undo_indent .= '|'.l:undo
  else
    let b:undo_indent = l:undo
  endif

endfunction