blob: ca28379f5dd5c84d7978b0ff698fb227f05c3655 [file] [log] [blame]
Bram Moolenaar300af822017-03-06 20:28:10 +01001" Test 'statusline'
2"
3" Not tested yet:
Bram Moolenaar300af822017-03-06 20:28:10 +01004" %N
Bram Moolenaar300af822017-03-06 20:28:10 +01005
6source view_util.vim
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01007source check.vim
Bram Moolenaardbe5d362020-02-08 18:35:31 +01008source screendump.vim
Bram Moolenaar300af822017-03-06 20:28:10 +01009
10func s:get_statusline()
11 return ScreenLines(&lines - 1, &columns)[0]
12endfunc
13
14func StatuslineWithCaughtError()
Bram Moolenaara742e082016-04-05 21:10:38 +020015 let s:func_in_statusline_called = 1
16 try
17 call eval('unknown expression')
18 catch
19 endtry
20 return ''
Bram Moolenaar300af822017-03-06 20:28:10 +010021endfunc
Bram Moolenaara742e082016-04-05 21:10:38 +020022
Bram Moolenaar300af822017-03-06 20:28:10 +010023func StatuslineWithError()
Bram Moolenaara742e082016-04-05 21:10:38 +020024 let s:func_in_statusline_called = 1
25 call eval('unknown expression')
26 return ''
Bram Moolenaar300af822017-03-06 20:28:10 +010027endfunc
Bram Moolenaara742e082016-04-05 21:10:38 +020028
Bram Moolenaar300af822017-03-06 20:28:10 +010029" Function used to display syntax group.
30func SyntaxItem()
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +020031 call assert_equal(s:expected_curbuf, g:actual_curbuf)
32 call assert_equal(s:expected_curwin, g:actual_curwin)
33 return synIDattr(synID(line("."), col("."),1), "name")
Bram Moolenaar300af822017-03-06 20:28:10 +010034endfunc
35
36func Test_caught_error_in_statusline()
Bram Moolenaara742e082016-04-05 21:10:38 +020037 let s:func_in_statusline_called = 0
38 set laststatus=2
39 let statusline = '%{StatuslineWithCaughtError()}'
40 let &statusline = statusline
41 redrawstatus
42 call assert_true(s:func_in_statusline_called)
43 call assert_equal(statusline, &statusline)
44 set statusline=
Bram Moolenaar300af822017-03-06 20:28:10 +010045endfunc
Bram Moolenaara742e082016-04-05 21:10:38 +020046
Bram Moolenaar300af822017-03-06 20:28:10 +010047func Test_statusline_will_be_disabled_with_error()
Bram Moolenaara742e082016-04-05 21:10:38 +020048 let s:func_in_statusline_called = 0
49 set laststatus=2
50 let statusline = '%{StatuslineWithError()}'
51 try
52 let &statusline = statusline
53 redrawstatus
54 catch
55 endtry
56 call assert_true(s:func_in_statusline_called)
57 call assert_equal('', &statusline)
58 set statusline=
Bram Moolenaar300af822017-03-06 20:28:10 +010059endfunc
60
61func Test_statusline()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +010062 CheckFeature quickfix
63
Bram Moolenaar4014e2c2020-06-23 20:00:50 +020064 " %a: Argument list ({current} of {max})
65 set statusline=%a
66 call assert_match('^\s*$', s:get_statusline())
67 arglocal a1 a2
68 rewind
69 call assert_match('^ (1 of 2)\s*$', s:get_statusline())
70 next
71 call assert_match('^ (2 of 2)\s*$', s:get_statusline())
72 e Xstatusline
73 call assert_match('^ ((2) of 2)\s*$', s:get_statusline())
74
Bram Moolenaar300af822017-03-06 20:28:10 +010075 only
76 set laststatus=2
77 set splitbelow
Bram Moolenaar316c1672019-04-14 13:23:40 +020078 call setline(1, range(1, 10000))
Bram Moolenaar300af822017-03-06 20:28:10 +010079
80 " %b: Value of character under cursor.
81 " %B: As above, in hexadecimal.
Bram Moolenaar316c1672019-04-14 13:23:40 +020082 call cursor(9000, 1)
Bram Moolenaar300af822017-03-06 20:28:10 +010083 set statusline=%b,%B
Bram Moolenaar316c1672019-04-14 13:23:40 +020084 call assert_match('^57,39\s*$', s:get_statusline())
Bram Moolenaar300af822017-03-06 20:28:10 +010085
86 " %o: Byte number in file of byte under cursor, first byte is 1.
87 " %O: As above, in hexadecimal.
88 set statusline=%o,%O
89 set fileformat=dos
Bram Moolenaar316c1672019-04-14 13:23:40 +020090 call assert_match('^52888,CE98\s*$', s:get_statusline())
Bram Moolenaar300af822017-03-06 20:28:10 +010091 set fileformat=mac
Bram Moolenaar316c1672019-04-14 13:23:40 +020092 call assert_match('^43889,AB71\s*$', s:get_statusline())
Bram Moolenaar300af822017-03-06 20:28:10 +010093 set fileformat=unix
Bram Moolenaar316c1672019-04-14 13:23:40 +020094 call assert_match('^43889,AB71\s*$', s:get_statusline())
Bram Moolenaar300af822017-03-06 20:28:10 +010095 set fileformat&
96
97 " %f: Path to the file in the buffer, as typed or relative to current dir.
98 set statusline=%f
99 call assert_match('^Xstatusline\s*$', s:get_statusline())
100
101 " %F: Full path to the file in the buffer.
102 set statusline=%F
103 call assert_match('/testdir/Xstatusline\s*$', s:get_statusline())
104
Bram Moolenaar832adf92020-06-25 19:01:36 +0200105 " Test for min and max width with %(. For some reason, if this test is moved
106 " after the below test for the help buffer flag, then the code to truncate
107 " the string is not executed.
108 set statusline=%015(%f%)
109 call assert_match('^ Xstatusline\s*$', s:get_statusline())
110 set statusline=%.6(%f%)
111 call assert_match('^<sline\s*$', s:get_statusline())
112 set statusline=%14f
113 call assert_match('^ Xstatusline\s*$', s:get_statusline())
114 set statusline=%.4L
115 call assert_match('^10>3\s*$', s:get_statusline())
116
Bram Moolenaar300af822017-03-06 20:28:10 +0100117 " %h: Help buffer flag, text is "[help]".
118 " %H: Help buffer flag, text is ",HLP".
119 set statusline=%h,%H
120 call assert_match('^,\s*$', s:get_statusline())
121 help
122 call assert_match('^\[Help\],HLP\s*$', s:get_statusline())
123 helpclose
124
125 " %k: Value of "b:keymap_name" or 'keymap'
126 " when :lmap mappings are being used: <keymap>"
127 set statusline=%k
128 if has('keymap')
129 set keymap=esperanto
130 call assert_match('^<Eo>\s*$', s:get_statusline())
131 set keymap&
132 else
133 call assert_match('^\s*$', s:get_statusline())
134 endif
135
136 " %l: Line number.
137 " %L: Number of line in buffer.
138 " %c: Column number.
139 set statusline=%l/%L,%c
Bram Moolenaar316c1672019-04-14 13:23:40 +0200140 call assert_match('^9000/10000,1\s*$', s:get_statusline())
Bram Moolenaar300af822017-03-06 20:28:10 +0100141
142 " %m: Modified flag, text is "[+]", "[-]" if 'modifiable' is off.
143 " %M: Modified flag, text is ",+" or ",-".
144 set statusline=%m%M
145 call assert_match('^\[+\],+\s*$', s:get_statusline())
146 set nomodifiable
147 call assert_match('^\[+-\],+-\s*$', s:get_statusline())
148 write
149 call assert_match('^\[-\],-\s*$', s:get_statusline())
150 set modifiable&
151 call assert_match('^\s*$', s:get_statusline())
152
153 " %n: Buffer number.
154 set statusline=%n
155 call assert_match('^'.bufnr('%').'\s*$', s:get_statusline())
156
157 " %p: Percentage through file in lines as in CTRL-G.
158 " %P: Percentage through file of displayed window.
159 set statusline=%p,%P
160 0
161 call assert_match('^0,Top\s*$', s:get_statusline())
162 norm G
163 call assert_match('^100,Bot\s*$', s:get_statusline())
Bram Moolenaar316c1672019-04-14 13:23:40 +0200164 9000
Bram Moolenaar300af822017-03-06 20:28:10 +0100165 " Don't check the exact percentage as it depends on the window size
166 call assert_match('^90,\(Top\|Bot\|\d\+%\)\s*$', s:get_statusline())
167
168 " %q: "[Quickfix List]", "[Location List]" or empty.
169 set statusline=%q
170 call assert_match('^\s*$', s:get_statusline())
171 copen
172 call assert_match('^\[Quickfix List\]\s*$', s:get_statusline())
173 cclose
174 lexpr getline(1, 2)
175 lopen
176 call assert_match('^\[Location List\]\s*$', s:get_statusline())
177 lclose
178
179 " %r: Readonly flag, text is "[RO]".
180 " %R: Readonly flag, text is ",RO".
181 set statusline=%r,%R
182 call assert_match('^,\s*$', s:get_statusline())
183 help
184 call assert_match('^\[RO\],RO\s*$', s:get_statusline())
185 helpclose
186
187 " %t: File name (tail) of file in the buffer.
188 set statusline=%t
189 call assert_match('^Xstatusline\s*$', s:get_statusline())
190
191 " %v: Virtual column number.
192 " %V: Virtual column number as -{num}. Not displayed if equal to 'c'.
Bram Moolenaar316c1672019-04-14 13:23:40 +0200193 call cursor(9000, 2)
Bram Moolenaar300af822017-03-06 20:28:10 +0100194 set statusline=%v,%V
195 call assert_match('^2,\s*$', s:get_statusline())
196 set virtualedit=all
197 norm 10|
198 call assert_match('^10,-10\s*$', s:get_statusline())
199 set virtualedit&
200
201 " %w: Preview window flag, text is "[Preview]".
202 " %W: Preview window flag, text is ",PRV".
203 set statusline=%w%W
204 call assert_match('^\s*$', s:get_statusline())
205 pedit
206 wincmd j
207 call assert_match('^\[Preview\],PRV\s*$', s:get_statusline())
208 pclose
209
210 " %y: Type of file in the buffer, e.g., "[vim]". See 'filetype'.
211 " %Y: Type of file in the buffer, e.g., ",VIM". See 'filetype'.
212 set statusline=%y\ %Y
213 call assert_match('^\s*$', s:get_statusline())
214 setfiletype vim
215 call assert_match('^\[vim\] VIM\s*$', s:get_statusline())
216
217 " %=: Separation point between left and right aligned items.
218 set statusline=foo%=bar
219 call assert_match('^foo\s\+bar\s*$', s:get_statusline())
220
221 " Test min/max width, leading zeroes, left/right justify.
222 set statusline=%04B
Bram Moolenaar316c1672019-04-14 13:23:40 +0200223 call cursor(9000, 1)
224 call assert_match('^0039\s*$', s:get_statusline())
Bram Moolenaar300af822017-03-06 20:28:10 +0100225 set statusline=#%4B#
Bram Moolenaar316c1672019-04-14 13:23:40 +0200226 call assert_match('^# 39#\s*$', s:get_statusline())
Bram Moolenaar300af822017-03-06 20:28:10 +0100227 set statusline=#%-4B#
Bram Moolenaar316c1672019-04-14 13:23:40 +0200228 call assert_match('^#39 #\s*$', s:get_statusline())
Bram Moolenaar300af822017-03-06 20:28:10 +0100229 set statusline=%.6f
230 call assert_match('^<sline\s*$', s:get_statusline())
231
232 " %<: Where to truncate.
Bram Moolenaar316c1672019-04-14 13:23:40 +0200233 " First check with when %< should not truncate with many columns
234 exe 'set statusline=a%<b' . repeat('c', &columns - 3) . 'd'
235 call assert_match('^abc\+d$', s:get_statusline())
236 exe 'set statusline=a' . repeat('b', &columns - 2) . '%<c'
237 call assert_match('^ab\+c$', s:get_statusline())
238 " Then check when %< should truncate when there with too few columns.
239 exe 'set statusline=a%<b' . repeat('c', &columns - 2) . 'd'
240 call assert_match('^a<c\+d$', s:get_statusline())
241 exe 'set statusline=a' . repeat('b', &columns - 1) . '%<c'
242 call assert_match('^ab\+>$', s:get_statusline())
Bram Moolenaar300af822017-03-06 20:28:10 +0100243
244 "%{: Evaluate expression between '%{' and '}' and substitute result.
245 syntax on
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +0200246 let s:expected_curbuf = string(bufnr(''))
247 let s:expected_curwin = string(win_getid())
Bram Moolenaar300af822017-03-06 20:28:10 +0100248 set statusline=%{SyntaxItem()}
249 call assert_match('^vimNumber\s*$', s:get_statusline())
250 s/^/"/
251 call assert_match('^vimLineComment\s*$', s:get_statusline())
252 syntax off
253
254 "%(: Start of item group.
255 set statusline=ab%(cd%q%)de
256 call assert_match('^abde\s*$', s:get_statusline())
257 copen
Bram Moolenaar1ef9bbe2017-06-17 20:08:20 +0200258 call assert_match('^abcd\[Quickfix List]de\s*$', s:get_statusline())
Bram Moolenaar300af822017-03-06 20:28:10 +0100259 cclose
260
261 " %#: Set highlight group. The name must follow and then a # again.
262 set statusline=ab%#Todo#cd%#Error#ef
263 call assert_match('^abcdef\s*$', s:get_statusline())
264 let sa1=screenattr(&lines - 1, 1)
265 let sa2=screenattr(&lines - 1, 3)
266 let sa3=screenattr(&lines - 1, 5)
267 call assert_notequal(sa1, sa2)
268 call assert_notequal(sa1, sa3)
269 call assert_notequal(sa2, sa3)
270 call assert_equal(sa1, screenattr(&lines - 1, 2))
271 call assert_equal(sa2, screenattr(&lines - 1, 4))
272 call assert_equal(sa3, screenattr(&lines - 1, 6))
273 call assert_equal(sa3, screenattr(&lines - 1, 7))
274
275 " %*: Set highlight group to User{N}
276 set statusline=a%1*b%0*c
277 call assert_match('^abc\s*$', s:get_statusline())
278 let sa1=screenattr(&lines - 1, 1)
279 let sa2=screenattr(&lines - 1, 2)
280 let sa3=screenattr(&lines - 1, 3)
281 call assert_equal(sa1, sa3)
282 call assert_notequal(sa1, sa2)
283
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +0200284 " An empty group that contains highlight changes
285 let g:a = ''
286 set statusline=ab%(cd%1*%{g:a}%*%)de
287 call assert_match('^abde\s*$', s:get_statusline())
288 let sa1=screenattr(&lines - 1, 1)
289 let sa2=screenattr(&lines - 1, 4)
290 call assert_equal(sa1, sa2)
291 let g:a = 'X'
292 call assert_match('^abcdXde\s*$', s:get_statusline())
293 let sa1=screenattr(&lines - 1, 1)
294 let sa2=screenattr(&lines - 1, 5)
295 let sa3=screenattr(&lines - 1, 7)
296 call assert_equal(sa1, sa3)
297 call assert_notequal(sa1, sa2)
298
299 let g:a = ''
300 set statusline=ab%1*%(cd%*%{g:a}%1*%)de
301 call assert_match('^abde\s*$', s:get_statusline())
302 let sa1=screenattr(&lines - 1, 1)
303 let sa2=screenattr(&lines - 1, 4)
304 call assert_notequal(sa1, sa2)
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(sa2, sa4)
314
315 " An empty group that contains highlight changes and doesn't reset them
316 let g:a = ''
317 set statusline=ab%(cd%1*%{g:a}%)de
318 call assert_match('^abcdde\s*$', s:get_statusline())
319 let sa1=screenattr(&lines - 1, 1)
320 let sa2=screenattr(&lines - 1, 5)
321 call assert_notequal(sa1, sa2)
322 let g:a = 'X'
323 call assert_match('^abcdXde\s*$', s:get_statusline())
324 let sa1=screenattr(&lines - 1, 1)
325 let sa2=screenattr(&lines - 1, 5)
326 let sa3=screenattr(&lines - 1, 7)
327 call assert_notequal(sa1, sa2)
328 call assert_equal(sa2, sa3)
329
330 let g:a = ''
331 set statusline=ab%1*%(cd%*%{g:a}%)de
332 call assert_match('^abcdde\s*$', s:get_statusline())
333 let sa1=screenattr(&lines - 1, 1)
334 let sa2=screenattr(&lines - 1, 3)
335 let sa3=screenattr(&lines - 1, 5)
336 call assert_notequal(sa1, sa2)
337 call assert_equal(sa1, sa3)
338 let g:a = 'X'
339 call assert_match('^abcdXde\s*$', s:get_statusline())
340 let sa1=screenattr(&lines - 1, 1)
341 let sa2=screenattr(&lines - 1, 3)
342 let sa3=screenattr(&lines - 1, 5)
343 let sa4=screenattr(&lines - 1, 7)
344 call assert_notequal(sa1, sa2)
345 call assert_equal(sa1, sa3)
346 call assert_equal(sa1, sa4)
347
Bram Moolenaar235dddf2017-10-26 18:21:24 +0200348 let g:a = ''
349 set statusline=%#Error#{%(\ %{g:a}\ %)}
350 call assert_match('^{}\s*$', s:get_statusline())
351 let g:a = 'X'
352 call assert_match('^{ X }\s*$', s:get_statusline())
353
Bram Moolenaar300af822017-03-06 20:28:10 +0100354 " %%: a percent sign.
355 set statusline=10%%
356 call assert_match('^10%\s*$', s:get_statusline())
357
358 " %!: evaluated expression is used as the option value
359 set statusline=%!2*3+1
360 call assert_match('7\s*$', s:get_statusline())
361
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +0200362 func GetNested()
363 call assert_equal(string(win_getid()), g:actual_curwin)
364 call assert_equal(string(bufnr('')), g:actual_curbuf)
365 return 'nested'
366 endfunc
367 func GetStatusLine()
368 call assert_equal(win_getid(), g:statusline_winid)
369 return 'the %{GetNested()} line'
370 endfunc
371 set statusline=%!GetStatusLine()
372 call assert_match('the nested line', s:get_statusline())
373 call assert_false(exists('g:actual_curwin'))
374 call assert_false(exists('g:actual_curbuf'))
375 call assert_false(exists('g:statusline_winid'))
376 delfunc GetNested
377 delfunc GetStatusLine
378
Bram Moolenaar8133cc62020-10-26 21:05:27 +0100379 " Test statusline works with 80+ items
380 function! StatusLabel()
381 redrawstatus
382 return '[label]'
383 endfunc
384 let statusline = '%{StatusLabel()}'
385 for i in range(150)
386 let statusline .= '%#TabLine' . (i % 2 == 0 ? 'Fill' : 'Sel') . '#' . string(i)[0]
387 endfor
388 let &statusline = statusline
389 redrawstatus
390 set statusline&
391 delfunc StatusLabel
392
393
Bram Moolenaar300af822017-03-06 20:28:10 +0100394 " Check statusline in current and non-current window
395 " with the 'fillchars' option.
396 set fillchars=stl:^,stlnc:=,vert:\|,fold:-,diff:-
397 vsplit
398 set statusline=x%=y
399 call assert_match('^x^\+y^x=\+y$', s:get_statusline())
400 set fillchars&
401 close
402
403 %bw!
404 call delete('Xstatusline')
405 set statusline&
406 set laststatus&
407 set splitbelow&
408endfunc
Bram Moolenaardee50a52019-11-30 15:05:22 +0100409
410func Test_statusline_visual()
411 func CallWordcount()
412 call wordcount()
413 endfunc
414 new x1
415 setl statusline=count=%{CallWordcount()}
416 " buffer must not be empty
417 call setline(1, 'hello')
418
419 " window with more lines than x1
420 new x2
421 call setline(1, range(10))
422 $
423 " Visual mode in line below liast line in x1 should not give ml_get error
424 call feedkeys("\<C-V>", "xt")
425 redraw
426
427 delfunc CallWordcount
428 bwipe! x1
429 bwipe! x2
430endfunc
Bram Moolenaardbe5d362020-02-08 18:35:31 +0100431
432func Test_statusline_removed_group()
433 CheckScreendump
434
435 let lines =<< trim END
436 scriptencoding utf-8
437 set laststatus=2
438 let &statusline = '%#StatColorHi2#%(✓%#StatColorHi2#%) Q≡'
439 END
440 call writefile(lines, 'XTest_statusline')
441
442 let buf = RunVimInTerminal('-S XTest_statusline', {'rows': 10, 'cols': 50})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200443 call TermWait(buf, 50)
Bram Moolenaardbe5d362020-02-08 18:35:31 +0100444 call VerifyScreenDump(buf, 'Test_statusline_1', {})
445
446 " clean up
447 call StopVimInTerminal(buf)
448 call delete('XTest_statusline')
449endfunc
Bram Moolenaar832adf92020-06-25 19:01:36 +0200450
Bram Moolenaarce0b7572021-04-01 18:47:14 +0200451func Test_statusline_using_mode()
452 CheckScreendump
453
454 let lines =<< trim END
Bram Moolenaard8db8382021-04-08 18:27:53 +0200455 setlocal statusline=-%{mode()}-
456 split
457 setlocal statusline=+%{mode()}+
Bram Moolenaarce0b7572021-04-01 18:47:14 +0200458 END
459 call writefile(lines, 'XTest_statusline')
460
Bram Moolenaard8db8382021-04-08 18:27:53 +0200461 let buf = RunVimInTerminal('-S XTest_statusline', {'rows': 7, 'cols': 50})
Bram Moolenaarce0b7572021-04-01 18:47:14 +0200462 call VerifyScreenDump(buf, 'Test_statusline_mode_1', {})
463
464 call term_sendkeys(buf, ":")
465 call VerifyScreenDump(buf, 'Test_statusline_mode_2', {})
466
467 " clean up
Bram Moolenaard8db8382021-04-08 18:27:53 +0200468 call term_sendkeys(buf, "close\<CR>")
Bram Moolenaarce0b7572021-04-01 18:47:14 +0200469 call StopVimInTerminal(buf)
470 call delete('XTest_statusline')
471endfunc
472
Bram Moolenaar668008b2020-10-01 19:06:35 +0200473func Test_statusline_after_split_vsplit()
474 only
475
476 " Make the status line of each window show the window number.
477 set ls=2 stl=%{winnr()}
478
479 split | redraw
480 vsplit | redraw
481
482 " The status line of the third window should read '3' here.
483 call assert_equal('3', nr2char(screenchar(&lines - 1, 1)))
484
485 only
486 set ls& stl&
487endfunc
488
Bram Moolenaar008bff92021-03-04 21:55:58 +0100489" Test using a multibyte character for 'stl' and 'stlnc' items in 'fillchars'
490" with a custom 'statusline'
491func Test_statusline_mbyte_fillchar()
492 only
493 set laststatus=2
494 set fillchars=vert:\|,fold:-,stl:━,stlnc:═
495 set statusline=a%=b
496 call assert_match('^a\+━\+b$', s:get_statusline())
497 vnew
498 call assert_match('^a\+━\+b━a\+═\+b$', s:get_statusline())
499 wincmd w
500 call assert_match('^a\+═\+b═a\+━\+b$', s:get_statusline())
501 set statusline& fillchars& laststatus&
502 %bw!
503endfunc
Bram Moolenaar668008b2020-10-01 19:06:35 +0200504
Bram Moolenaar832adf92020-06-25 19:01:36 +0200505" vim: shiftwidth=2 sts=2 expandtab