1" Test 'statusline'
2"
3" Not tested yet:
4"   %a
5"   %N
6"   %T
7"   %X
8
9source view_util.vim
10
11func s:get_statusline()
12  return ScreenLines(&lines - 1, &columns)[0]
13endfunc
14
15func StatuslineWithCaughtError()
16  let s:func_in_statusline_called = 1
17  try
18    call eval('unknown expression')
19  catch
20  endtry
21  return ''
22endfunc
23
24func StatuslineWithError()
25  let s:func_in_statusline_called = 1
26  call eval('unknown expression')
27  return ''
28endfunc
29
30" Function used to display syntax group.
31func SyntaxItem()
32  return synIDattr(synID(line("."),col("."),1),"name")
33endfunc
34
35func Test_caught_error_in_statusline()
36  let s:func_in_statusline_called = 0
37  set laststatus=2
38  let statusline = '%{StatuslineWithCaughtError()}'
39  let &statusline = statusline
40  redrawstatus
41  call assert_true(s:func_in_statusline_called)
42  call assert_equal(statusline, &statusline)
43  set statusline=
44endfunc
45
46func Test_statusline_will_be_disabled_with_error()
47  let s:func_in_statusline_called = 0
48  set laststatus=2
49  let statusline = '%{StatuslineWithError()}'
50  try
51    let &statusline = statusline
52    redrawstatus
53  catch
54  endtry
55  call assert_true(s:func_in_statusline_called)
56  call assert_equal('', &statusline)
57  set statusline=
58endfunc
59
60func Test_statusline()
61  new Xstatusline
62  only
63  set laststatus=2
64  set splitbelow
65  call setline(1, range(1, 200))
66
67  " %b: Value of character under cursor.
68  " %B: As above, in hexadecimal.
69  call cursor(180, 2)
70  set statusline=%b,%B
71  call assert_match('^56,38\s*$', s:get_statusline())
72
73  " %o: Byte number in file of byte under cursor, first byte is 1.
74  " %O: As above, in hexadecimal.
75  set statusline=%o,%O
76  set fileformat=dos
77  call assert_match('^789,315\s*$', s:get_statusline())
78  set fileformat=mac
79  call assert_match('^610,262\s*$', s:get_statusline())
80  set fileformat=unix
81  call assert_match('^610,262\s*$', s:get_statusline())
82  set fileformat&
83
84  " %f: Path to the file in the buffer, as typed or relative to current dir.
85  set statusline=%f
86  call assert_match('^Xstatusline\s*$', s:get_statusline())
87
88  " %F: Full path to the file in the buffer.
89  set statusline=%F
90  call assert_match('/testdir/Xstatusline\s*$', s:get_statusline())
91
92  " %h: Help buffer flag, text is "[help]".
93  " %H: Help buffer flag, text is ",HLP".
94  set statusline=%h,%H
95  call assert_match('^,\s*$', s:get_statusline())
96  help
97  call assert_match('^\[Help\],HLP\s*$', s:get_statusline())
98  helpclose
99
100  " %k: Value of "b:keymap_name" or 'keymap'
101  "     when :lmap mappings are being used: <keymap>"
102  set statusline=%k
103  if has('keymap')
104    set keymap=esperanto
105    call assert_match('^<Eo>\s*$', s:get_statusline())
106    set keymap&
107  else
108    call assert_match('^\s*$', s:get_statusline())
109  endif
110
111  " %l: Line number.
112  " %L: Number of line in buffer.
113  " %c: Column number.
114  set statusline=%l/%L,%c
115  call assert_match('^180/200,2\s*$', s:get_statusline())
116
117  " %m: Modified flag, text is "[+]", "[-]" if 'modifiable' is off.
118  " %M: Modified flag, text is ",+" or ",-".
119  set statusline=%m%M
120  call assert_match('^\[+\],+\s*$', s:get_statusline())
121  set nomodifiable
122  call assert_match('^\[+-\],+-\s*$', s:get_statusline())
123  write
124  call assert_match('^\[-\],-\s*$', s:get_statusline())
125  set modifiable&
126  call assert_match('^\s*$', s:get_statusline())
127
128  " %n: Buffer number.
129  set statusline=%n
130  call assert_match('^'.bufnr('%').'\s*$', s:get_statusline())
131
132  " %p: Percentage through file in lines as in CTRL-G.
133  " %P: Percentage through file of displayed window.
134  set statusline=%p,%P
135  0
136  call assert_match('^0,Top\s*$', s:get_statusline())
137  norm G
138  call assert_match('^100,Bot\s*$', s:get_statusline())
139  180
140  " Don't check the exact percentage as it depends on the window size
141  call assert_match('^90,\(Top\|Bot\|\d\+%\)\s*$', s:get_statusline())
142
143  " %q: "[Quickfix List]", "[Location List]" or empty.
144  set statusline=%q
145  call assert_match('^\s*$', s:get_statusline())
146  copen
147  call assert_match('^\[Quickfix List\]\s*$', s:get_statusline())
148  cclose
149  lexpr getline(1, 2)
150  lopen
151  call assert_match('^\[Location List\]\s*$', s:get_statusline())
152  lclose
153
154  " %r: Readonly flag, text is "[RO]".
155  " %R: Readonly flag, text is ",RO".
156  set statusline=%r,%R
157  call assert_match('^,\s*$', s:get_statusline())
158  help
159  call assert_match('^\[RO\],RO\s*$', s:get_statusline())
160  helpclose
161
162  " %t: File name (tail) of file in the buffer.
163  set statusline=%t
164  call assert_match('^Xstatusline\s*$', s:get_statusline())
165
166  " %v: Virtual column number.
167  " %V: Virtual column number as -{num}. Not displayed if equal to 'c'.
168  call cursor(180, 2)
169  set statusline=%v,%V
170  call assert_match('^2,\s*$', s:get_statusline())
171  set virtualedit=all
172  norm 10|
173  call assert_match('^10,-10\s*$', s:get_statusline())
174  set virtualedit&
175
176  " %w: Preview window flag, text is "[Preview]".
177  " %W: Preview window flag, text is ",PRV".
178  set statusline=%w%W
179  call assert_match('^\s*$', s:get_statusline())
180  pedit
181  wincmd j
182  call assert_match('^\[Preview\],PRV\s*$', s:get_statusline())
183  pclose
184
185  " %y: Type of file in the buffer, e.g., "[vim]". See 'filetype'.
186  " %Y: Type of file in the buffer, e.g., ",VIM". See 'filetype'.
187  set statusline=%y\ %Y
188  call assert_match('^\s*$', s:get_statusline())
189  setfiletype vim
190  call assert_match('^\[vim\] VIM\s*$', s:get_statusline())
191
192  " %=: Separation point between left and right aligned items.
193  set statusline=foo%=bar
194  call assert_match('^foo\s\+bar\s*$', s:get_statusline())
195
196  " Test min/max width, leading zeroes, left/right justify.
197  set statusline=%04B
198  call cursor(180, 2)
199  call assert_match('^0038\s*$', s:get_statusline())
200  set statusline=#%4B#
201  call assert_match('^#  38#\s*$', s:get_statusline())
202  set statusline=#%-4B#
203  call assert_match('^#38  #\s*$', s:get_statusline())
204  set statusline=%.6f
205  call assert_match('^<sline\s*$', s:get_statusline())
206
207  " %<: Where to truncate.
208  exe 'set statusline=a%<b' . repeat('c', 1000) . 'd'
209  call assert_match('^a<c*d$', s:get_statusline())
210  exe 'set statusline=a' . repeat('b', 1000) . '%<c'
211  call assert_match('^ab*>$', s:get_statusline())
212
213  "%{: Evaluate expression between '%{' and '}' and substitute result.
214  syntax on
215  set statusline=%{SyntaxItem()}
216  call assert_match('^vimNumber\s*$', s:get_statusline())
217  s/^/"/
218  call assert_match('^vimLineComment\s*$', s:get_statusline())
219  syntax off
220
221  "%(: Start of item group.
222  set statusline=ab%(cd%q%)de
223  call assert_match('^abde\s*$', s:get_statusline())
224  copen
225  call assert_match('^abcd\[Quickfix List]de\s*$', s:get_statusline())
226  cclose
227
228  " %#: Set highlight group. The name must follow and then a # again.
229  set statusline=ab%#Todo#cd%#Error#ef
230  call assert_match('^abcdef\s*$', s:get_statusline())
231  let sa1=screenattr(&lines - 1, 1)
232  let sa2=screenattr(&lines - 1, 3)
233  let sa3=screenattr(&lines - 1, 5)
234  call assert_notequal(sa1, sa2)
235  call assert_notequal(sa1, sa3)
236  call assert_notequal(sa2, sa3)
237  call assert_equal(sa1, screenattr(&lines - 1, 2))
238  call assert_equal(sa2, screenattr(&lines - 1, 4))
239  call assert_equal(sa3, screenattr(&lines - 1, 6))
240  call assert_equal(sa3, screenattr(&lines - 1, 7))
241
242  " %*: Set highlight group to User{N}
243  set statusline=a%1*b%0*c
244  call assert_match('^abc\s*$', s:get_statusline())
245  let sa1=screenattr(&lines - 1, 1)
246  let sa2=screenattr(&lines - 1, 2)
247  let sa3=screenattr(&lines - 1, 3)
248  call assert_equal(sa1, sa3)
249  call assert_notequal(sa1, sa2)
250
251  " An empty group that contains highlight changes
252  let g:a = ''
253  set statusline=ab%(cd%1*%{g:a}%*%)de
254  call assert_match('^abde\s*$', s:get_statusline())
255  let sa1=screenattr(&lines - 1, 1)
256  let sa2=screenattr(&lines - 1, 4)
257  call assert_equal(sa1, sa2)
258  let g:a = 'X'
259  call assert_match('^abcdXde\s*$', s:get_statusline())
260  let sa1=screenattr(&lines - 1, 1)
261  let sa2=screenattr(&lines - 1, 5)
262  let sa3=screenattr(&lines - 1, 7)
263  call assert_equal(sa1, sa3)
264  call assert_notequal(sa1, sa2)
265
266  let g:a = ''
267  set statusline=ab%1*%(cd%*%{g:a}%1*%)de
268  call assert_match('^abde\s*$', s:get_statusline())
269  let sa1=screenattr(&lines - 1, 1)
270  let sa2=screenattr(&lines - 1, 4)
271  call assert_notequal(sa1, sa2)
272  let g:a = 'X'
273  call assert_match('^abcdXde\s*$', s:get_statusline())
274  let sa1=screenattr(&lines - 1, 1)
275  let sa2=screenattr(&lines - 1, 3)
276  let sa3=screenattr(&lines - 1, 5)
277  let sa4=screenattr(&lines - 1, 7)
278  call assert_notequal(sa1, sa2)
279  call assert_equal(sa1, sa3)
280  call assert_equal(sa2, sa4)
281
282  " An empty group that contains highlight changes and doesn't reset them
283  let g:a = ''
284  set statusline=ab%(cd%1*%{g:a}%)de
285  call assert_match('^abcdde\s*$', s:get_statusline())
286  let sa1=screenattr(&lines - 1, 1)
287  let sa2=screenattr(&lines - 1, 5)
288  call assert_notequal(sa1, sa2)
289  let g:a = 'X'
290  call assert_match('^abcdXde\s*$', s:get_statusline())
291  let sa1=screenattr(&lines - 1, 1)
292  let sa2=screenattr(&lines - 1, 5)
293  let sa3=screenattr(&lines - 1, 7)
294  call assert_notequal(sa1, sa2)
295  call assert_equal(sa2, sa3)
296
297  let g:a = ''
298  set statusline=ab%1*%(cd%*%{g:a}%)de
299  call assert_match('^abcdde\s*$', s:get_statusline())
300  let sa1=screenattr(&lines - 1, 1)
301  let sa2=screenattr(&lines - 1, 3)
302  let sa3=screenattr(&lines - 1, 5)
303  call assert_notequal(sa1, sa2)
304  call assert_equal(sa1, sa3)
305  let g:a = 'X'
306  call assert_match('^abcdXde\s*$', s:get_statusline())
307  let sa1=screenattr(&lines - 1, 1)
308  let sa2=screenattr(&lines - 1, 3)
309  let sa3=screenattr(&lines - 1, 5)
310  let sa4=screenattr(&lines - 1, 7)
311  call assert_notequal(sa1, sa2)
312  call assert_equal(sa1, sa3)
313  call assert_equal(sa1, sa4)
314
315  let g:a = ''
316  set statusline=%#Error#{%(\ %{g:a}\ %)}
317  call assert_match('^{}\s*$', s:get_statusline())
318  let g:a = 'X'
319  call assert_match('^{ X }\s*$', s:get_statusline())
320
321  " %%: a percent sign.
322  set statusline=10%%
323  call assert_match('^10%\s*$', s:get_statusline())
324
325  " %!: evaluated expression is used as the option value
326  set statusline=%!2*3+1
327  call assert_match('7\s*$', s:get_statusline())
328
329  " Check statusline in current and non-current window
330  " with the 'fillchars' option.
331  set fillchars=stl:^,stlnc:=,vert:\|,fold:-,diff:-
332  vsplit
333  set statusline=x%=y
334  call assert_match('^x^\+y^x=\+y$', s:get_statusline())
335  set fillchars&
336  close
337
338  %bw!
339  call delete('Xstatusline')
340  set statusline&
341  set laststatus&
342  set splitbelow&
343endfunc
344