summaryrefslogtreecommitdiffstats
path: root/indent/octave.vim
blob: 3a6a2e78a6a3403b41f62bb251f1ccc9cab8b9a2 (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
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
if !exists('g:polyglot_disabled') || index(g:polyglot_disabled, 'octave') == -1

" Vim indent file
" Language:	       Octave
" Maintainer:	       Nguyễn Gia Phong <vn.mcsinyx@gmail.com>
" Original Maintainer: Marcus Aurelius Farias <marcus.cf@bol.com.br>
" First Author:        Max Ischenko <mfi@ukr.net>
" Last Change:	       2019-10-11

" Only load this indent file when no other was loaded.
if exists("b:did_indent")
  finish
endif
let b:did_indent = 1

let s:beginBlock = ['for', 'parfor', 'function', 'if', 'switch',
                   \'try', 'unwind_protect', 'while', 'do', 'classdef',
                   \'enumeration', 'events', 'methods', 'properties']
let s:midBlock = ['case', 'catch', 'else', 'elseif', 'otherwise',
                 \'unwind_protect_cleanup']
let s:endBlock = ['end', 'endfor', 'endparfor', 'endfunction', 'endif',
                 \'end_try_catch', 'end_unwind_protect', 'endwhile',
                 \'endclassdef', 'endenumeration', 'endevents',
                 \'endproperties', 'endswitch', 'until', 'endmethods']
let s:openBlock = s:beginBlock + s:midBlock
let s:closeBlock = s:midBlock + s:endBlock

" To make Vim call GetOctaveIndent() when it finds a block closer
" on the current line ('else' is default and includes 'elseif').
setlocal indentkeys+=0=end,0=until,0=case,0=catch,0=otherwise
setlocal indentkeys+=0=unwind_protect_cleanup

" Only define the function once.
if exists("*GetOctaveIndent")
  finish
endif

function! GetOctaveIndent()
  " Find a non-blank line above the current line.
  let prevlnum = prevnonblank(v:lnum - 1)

  " Hit the start of the file, use zero indent.
  if prevlnum == 0
    return 0
  endif

  let ind = indent(prevlnum)
  let prevl = getline(prevlnum)
  let l = getline(v:lnum)

  " Add a 'shiftwidth' after lines starting a block:
  let openCol = match(prevl, '^\s*\%(' . join(s:openBlock, '\>\|') . '\>\)') + 1
  let hasNoEnd = prevl !~ ('\<' . join(s:endBlock, '\>\|\<') . '\>')
  if openCol && hasNoEnd
    let openSynID = synID(prevlnum, openCol, 1)
    if synIDattr(openSynID, "name") != "octaveComment"
      let ind = ind + shiftwidth()
    endif
  endif

  " Subtract a 'shiftwidth' on closure of blocks,
  " i.e. the part that required 'indentkeys'.
  let closeCol = match(l, '^\s*\%(' . join(s:closeBlock, '\>\|') . '\>\)') + 1
  if closeCol
    let closeSynID = synID(v:lnum, closeCol, 1)
    if synIDattr(closeSynID, "name") != "octaveComment"
      let ind = ind - shiftwidth()
    endif
  endif

  return ind
endfunction

setlocal indentexpr=GetOctaveIndent()
setlocal autoindent

endif