1" Vim indent file
2" Language:		Meson
3" License:		VIM License
4" Maintainer:		Nirbheek Chauhan <nirbheek.chauhan@gmail.com>
5"	        	Liam Beguin <liambeguin@gmail.com>
6" Original Authors:	David Bustos <bustos@caltech.edu>
7"			Bram Moolenaar <Bram@vim.org>
8" Last Change:		2015 Feb 23
9
10" Only load this indent file when no other was loaded.
11if exists("b:did_indent")
12  finish
13endif
14let b:did_indent = 1
15
16" Some preliminary settings
17setlocal nolisp		" Make sure lisp indenting doesn't supersede us
18setlocal autoindent	" indentexpr isn't much help otherwise
19
20setlocal indentexpr=GetMesonIndent(v:lnum)
21setlocal indentkeys+==elif,=else,=endforeach,=endif,0)
22
23let b:undo_indent = "setl ai< inde< indk< lisp<"
24
25" Only define the function once.
26if exists("*GetMesonIndent")
27  finish
28endif
29let s:keepcpo= &cpo
30set cpo&vim
31
32" Come here when loading the script the first time.
33
34let s:maxoff = 50	" maximum number of lines to look backwards for ()
35
36function GetMesonIndent(lnum)
37  echom getline(line("."))
38
39  " If this line is explicitly joined: If the previous line was also joined,
40  " line it up with that one, otherwise add two 'shiftwidth'
41  if getline(a:lnum - 1) =~ '\\$'
42    if a:lnum > 1 && getline(a:lnum - 2) =~ '\\$'
43      return indent(a:lnum - 1)
44    endif
45    return indent(a:lnum - 1) + (exists("g:mesonindent_continue") ? eval(g:mesonindent_continue) : (shiftwidth() * 2))
46  endif
47
48  " If the start of the line is in a string don't change the indent.
49  if has('syntax_items')
50	\ && synIDattr(synID(a:lnum, 1, 1), "name") =~ "String$"
51    return -1
52  endif
53
54  " Search backwards for the previous non-empty line.
55  let plnum = prevnonblank(v:lnum - 1)
56
57  if plnum == 0
58    " This is the first non-empty line, use zero indent.
59    return 0
60  endif
61
62  " If the previous line is inside parenthesis, use the indent of the starting
63  " line.
64  " Trick: use the non-existing "dummy" variable to break out of the loop when
65  " going too far back.
66  call cursor(plnum, 1)
67  let parlnum = searchpair('(\|{\|\[', '', ')\|}\|\]', 'nbW',
68	  \ "line('.') < " . (plnum - s:maxoff) . " ? dummy :"
69	  \ . " synIDattr(synID(line('.'), col('.'), 1), 'name')"
70	  \ . " =~ '\\(Comment\\|Todo\\|String\\)$'")
71  if parlnum > 0
72    let plindent = indent(parlnum)
73    let plnumstart = parlnum
74  else
75    let plindent = indent(plnum)
76    let plnumstart = plnum
77  endif
78
79
80  " When inside parenthesis: If at the first line below the parenthesis add
81  " a 'shiftwidth', otherwise same as previous line.
82  " i = (a
83  "       + b
84  "       + c)
85  call cursor(a:lnum, 1)
86  let p = searchpair('(\|{\|\[', '', ')\|}\|\]', 'bW',
87	  \ "line('.') < " . (a:lnum - s:maxoff) . " ? dummy :"
88	  \ . " synIDattr(synID(line('.'), col('.'), 1), 'name')"
89	  \ . " =~ '\\(Comment\\|Todo\\|String\\)$'")
90  if p > 0
91    if p == plnum
92      " When the start is inside parenthesis, only indent one 'shiftwidth'.
93      let pp = searchpair('(\|{\|\[', '', ')\|}\|\]', 'bW',
94	  \ "line('.') < " . (a:lnum - s:maxoff) . " ? dummy :"
95	  \ . " synIDattr(synID(line('.'), col('.'), 1), 'name')"
96	  \ . " =~ '\\(Comment\\|Todo\\|String\\)$'")
97      if pp > 0
98	return indent(plnum) + (exists("g:pyindent_nested_paren") ? eval(g:pyindent_nested_paren) : shiftwidth())
99      endif
100      return indent(plnum) + (exists("g:pyindent_open_paren") ? eval(g:pyindent_open_paren) : shiftwidth())
101    endif
102    if plnumstart == p
103      return indent(plnum)
104    endif
105    return plindent
106  endif
107
108
109  " Get the line and remove a trailing comment.
110  " Use syntax highlighting attributes when possible.
111  let pline = getline(plnum)
112  let pline_len = strlen(pline)
113  if has('syntax_items')
114    " If the last character in the line is a comment, do a binary search for
115    " the start of the comment.  synID() is slow, a linear search would take
116    " too long on a long line.
117    if synIDattr(synID(plnum, pline_len, 1), "name") =~ "\\(Comment\\|Todo\\)$"
118      let min = 1
119      let max = pline_len
120      while min < max
121	let col = (min + max) / 2
122	if synIDattr(synID(plnum, col, 1), "name") =~ "\\(Comment\\|Todo\\)$"
123	  let max = col
124	else
125	  let min = col + 1
126	endif
127      endwhile
128      let pline = strpart(pline, 0, min - 1)
129    endif
130  else
131    let col = 0
132    while col < pline_len
133      if pline[col] == '#'
134	let pline = strpart(pline, 0, col)
135	break
136      endif
137      let col = col + 1
138    endwhile
139  endif
140
141  " If the previous line ended the conditional/loop
142  if getline(plnum) =~ '^\s*\(endif\|endforeach\)\>\s*'
143    " Maintain indent
144    return -1
145  endif
146
147  " If the previous line ended with a builtin, indent this line
148  if pline =~ '^\s*\(foreach\|if\|else\|elif\)\>\s*'
149    return plindent + shiftwidth()
150  endif
151
152  " If the current line begins with a header keyword, deindent
153  if getline(a:lnum) =~ '^\s*\(else\|elif\|endif\|endforeach\)'
154
155    " Unless the previous line was a one-liner
156    if getline(plnumstart) =~ '^\s*\(foreach\|if\)\>\s*'
157      return plindent
158    endif
159
160    " Or the user has already dedented
161    if indent(a:lnum) <= plindent - shiftwidth()
162      return -1
163    endif
164
165    return plindent - shiftwidth()
166  endif
167
168  " When after a () construct we probably want to go back to the start line.
169  " a = (b
170  "       + c)
171  " here
172  if parlnum > 0
173    return plindent
174  endif
175
176  return -1
177
178endfunction
179
180let &cpo = s:keepcpo
181unlet s:keepcpo
182
183" vim:sw=2
184