robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

zeertzjqMerge pull request #40977 from zeertzjq/vim-9.2.0806e5aac37eb3

master
24.6 KiB821 linesraw
1" Test 'statusline'
2"
3" Not tested yet:
4"   %N
5
6source view_util.vim
7source check.vim
8source term_util.vim
9
10func SetUp()
11  set laststatus=2
12endfunc
13
14func TearDown()
15  set laststatus&
16endfunc
17
18func s:get_statusline()
19  redraw!
20  if has('gui_running')
21    sleep 1m
22  endif
23  " Read the screen directly after redraw! instead of going through
24  " ScreenLines(), whose own redraw! may process events and change the window
25  " layout between here and the screenstring() calls.
26  let row = &lines - 1
27  return join(map(range(1, &columns), 'screenstring(row, v:val)'), '')
28endfunc
29
30func StatuslineWithCaughtError()
31  let s:func_in_statusline_called = 1
32  try
33    call eval('unknown expression')
34  catch
35  endtry
36  return ''
37endfunc
38
39func StatuslineWithError()
40  let s:func_in_statusline_called = 1
41  call eval('unknown expression')
42  return ''
43endfunc
44
45" Function used to display syntax group.
46func SyntaxItem()
47  call assert_equal(s:expected_curbuf, g:actual_curbuf)
48  call assert_equal(s:expected_curwin, g:actual_curwin)
49  return synIDattr(synID(line("."), col("."),1), "name")
50endfunc
51
52func Test_caught_error_in_statusline()
53  let s:func_in_statusline_called = 0
54  let statusline = '%{StatuslineWithCaughtError()}'
55  let &statusline = statusline
56  redrawstatus
57  call assert_true(s:func_in_statusline_called)
58  call assert_equal(statusline, &statusline)
59  set statusline=
60endfunc
61
62func Test_statusline_will_be_disabled_with_error()
63  let s:func_in_statusline_called = 0
64  let statusline = '%{StatuslineWithError()}'
65  try
66    let &statusline = statusline
67    redrawstatus
68  catch
69  endtry
70  call assert_true(s:func_in_statusline_called)
71  " Nvim: resets to default value instead.
72  " call assert_equal('', &statusline)
73  call assert_equal(nvim_get_option_info2('statusline', {}).default, &statusline)
74  set statusline=
75endfunc
76
77func Test_statusline()
78  CheckFeature quickfix
79
80  " %a: Argument list ({current} of {max})
81  set statusline=%a
82  call assert_match('^\s*$', s:get_statusline())
83  arglocal a1 a2
84  rewind
85  call assert_match('^ (1 of 2)\s*$', s:get_statusline())
86  next
87  call assert_match('^ (2 of 2)\s*$', s:get_statusline())
88  e Xstatusline
89  call assert_match('^ ((2) of 2)\s*$', s:get_statusline())
90
91  only
92  set splitbelow
93  call setline(1, range(1, 10000))
94
95  " %b: Value of character under cursor.
96  " %B: As above, in hexadecimal.
97  call cursor(9000, 1)
98  set statusline=%b,%B
99  call assert_match('^57,39\s*$', s:get_statusline())
100
101  " %o: Byte number in file of byte under cursor, first byte is 1.
102  " %O: As above, in hexadecimal.
103  set statusline=%o,%O
104  set fileformat=dos
105  call assert_match('^52888,CE98\s*$', s:get_statusline())
106  set fileformat=mac
107  call assert_match('^43889,AB71\s*$', s:get_statusline())
108  set fileformat=unix
109  call assert_match('^43889,AB71\s*$', s:get_statusline())
110  set fileformat&
111
112  " %f: Path to the file in the buffer, as typed or relative to current dir.
113  set statusline=%f
114  call assert_match('^Xstatusline\s*$', s:get_statusline())
115
116  " %F: Full path to the file in the buffer.
117  set statusline=%F
118  call assert_match('/testdir/Xstatusline\s*$', s:get_statusline())
119
120  " Test for min and max width with %(. For some reason, if this test is moved
121  " after the below test for the help buffer flag, then the code to truncate
122  " the string is not executed.
123  set statusline=%015(%f%)
124  call assert_match('^    Xstatusline\s*$', s:get_statusline())
125  set statusline=%.6(%f%)
126  call assert_match('^<sline\s*$', s:get_statusline())
127  set statusline=%.5(1234567%),%2.5(1234567%),%5.5(1234567%),%50.5(1234567%)
128  call assert_match('^<4567,<4567,<4567,<4567\s*$', s:get_statusline())
129  set statusline=%14f
130  call assert_match('^   Xstatusline\s*$', s:get_statusline())
131  set statusline=%.4L
132  call assert_match('^10>3\s*$', s:get_statusline())
133  for filler in ['-', '∙']
134    exec 'set fillchars+=stl:'..filler
135    set statusline=%-5(x%),%-5.(x%),%-5.5(x%),%-5.10(x%);
136    call assert_match(substitute('^x____,x____,x____,x____;_*$', '_', filler, 'g'), s:get_statusline())
137    set statusline=%5(x%),%5.(x%),%5.5(x%),%5.10(x%);
138    call assert_match(substitute('^____x,____x,____x,____x;_*$', '_', filler, 'g'), s:get_statusline())
139    set statusline=%.5(12🙂345%),%4.5(12🙂345%),%5.5(12🙂345%),%50.5(12🙂345%);
140    call assert_match(substitute('^<345,<345,<345_,<345_;_*$', '_', filler, 'g'), s:get_statusline())
141  endfor
142  if has('linux')
143    " This assumes MAXPATHL is 4096 bytes.
144    set stl=%{%repeat('x',4096-6)%}%10(X%)
145    set fillchars+=stl:-
146    call assert_match('^<x\+----X$', s:get_statusline())
147    set fillchars+=stl:∙
148    call assert_match('^<x\+∙X$', s:get_statusline())
149  endif
150  set fillchars&
151
152  " %h: Help buffer flag, text is "[help]".
153  " %H: Help buffer flag, text is ",HLP".
154  set statusline=%h,%H
155  call assert_match('^,\s*$', s:get_statusline())
156  help
157  call assert_match('^\[Help\],HLP\s*$', s:get_statusline())
158  helpclose
159
160  " %k: Value of "b:keymap_name" or 'keymap'
161  "     when :lmap mappings are being used: <keymap>"
162  set statusline=%k
163  if has('keymap')
164    set keymap=esperanto
165    call assert_match('^<Eo>\s*$', s:get_statusline())
166    set keymap&
167  else
168    call assert_match('^\s*$', s:get_statusline())
169  endif
170
171  " %l: Line number.
172  " %L: Number of line in buffer.
173  " %c: Column number.
174  set statusline=%l/%L,%c
175  call assert_match('^9000/10000,1\s*$', s:get_statusline())
176
177  " %m: Modified flag, text is "[+]", "[-]" if 'modifiable' is off.
178  " %M: Modified flag, text is ",+" or ",-".
179  set statusline=%m%M
180  call assert_match('^\[+\],+\s*$', s:get_statusline())
181  set nomodifiable
182  call assert_match('^\[+-\],+-\s*$', s:get_statusline())
183  write
184  call assert_match('^\[-\],-\s*$', s:get_statusline())
185  set modifiable&
186  call assert_match('^\s*$', s:get_statusline())
187
188  " %n: Buffer number.
189  set statusline=%n
190  call assert_match('^'.bufnr('%').'\s*$', s:get_statusline())
191
192  " %p: Percentage through file in lines as in CTRL-G.
193  " %P: Percentage through file of displayed window.
194  set statusline=%p,%P
195  0
196  call assert_match('^0,Top\s*$', s:get_statusline())
197  norm G
198  call assert_match('^100,Bot\s*$', s:get_statusline())
199  " The exact percentage depends on the window height, so create a window with
200  " known height.
201  9000 | botright 10split | setlocal scrolloff=0 | normal! zb
202  call assert_match('^90,89%\s*$', s:get_statusline())
203  normal! zt
204  call assert_match('^90,90%\s*$', s:get_statusline())
205  " %P should result in a string with 3 in length when not translated.
206  normal! 500zb
207  call assert_match('^5, 4%\s*$', s:get_statusline())
208  close
209
210  " %q: "[Quickfix List]", "[Location List]" or empty.
211  set statusline=%q
212  call assert_match('^\s*$', s:get_statusline())
213  copen
214  call assert_match('^\[Quickfix List\]\s*$', s:get_statusline())
215  cclose
216  lexpr getline(1, 2)
217  lopen
218  call assert_match('^\[Location List\]\s*$', s:get_statusline())
219  lclose
220
221  " %r: Readonly flag, text is "[RO]".
222  " %R: Readonly flag, text is ",RO".
223  set statusline=%r,%R
224  call assert_match('^,\s*$', s:get_statusline())
225  help
226  call assert_match('^\[RO\],RO\s*$', s:get_statusline())
227  helpclose
228
229  " %t: File name (tail) of file in the buffer.
230  set statusline=%t
231  call assert_match('^Xstatusline\s*$', s:get_statusline())
232
233  " %v: Virtual column number.
234  " %V: Virtual column number as -{num}. Not displayed if equal to 'c'.
235  call cursor(9000, 2)
236  set statusline=%v,%V
237  call assert_match('^2,\s*$', s:get_statusline())
238  set virtualedit=all
239  norm 10|
240  call assert_match('^10,-10\s*$', s:get_statusline())
241  set list
242  call assert_match('^10,-10\s*$', s:get_statusline())
243  set virtualedit&
244  exe "norm A\<Tab>\<Tab>a\<Esc>"
245  " In list mode a <Tab> is shown as "^I", which is 2-wide.
246  call assert_match('^9,-9\s*$', s:get_statusline())
247  set list&
248  " Now the second <Tab> ends at the 16th screen column.
249  call assert_match('^17,-17\s*$', s:get_statusline())
250  undo
251
252  " %w: Preview window flag, text is "[Preview]".
253  " %W: Preview window flag, text is ",PRV".
254  set statusline=%w%W
255  call assert_match('^\s*$', s:get_statusline())
256  pedit
257  wincmd j
258  call assert_match('^\[Preview\],PRV\s*$', s:get_statusline())
259  pclose
260  pbuffer
261  wincmd j
262  call assert_match('^\[Preview\],PRV\s*$', s:get_statusline())
263  pclose
264
265  " %y: Type of file in the buffer, e.g., "[vim]". See 'filetype'.
266  " %Y: Type of file in the buffer, e.g., ",VIM". See 'filetype'.
267  set statusline=%y\ %Y
268  call assert_match('^\s*$', s:get_statusline())
269  setfiletype vim
270  call assert_match('^\[vim\] VIM\s*$', s:get_statusline())
271
272  " %=: Separation point between left and right aligned items.
273  set statusline=foo%=bar
274  call assert_match('^foo\s\+bar\s*$', s:get_statusline())
275  set statusline=foo%=bar%=baz
276  call assert_match('^foo\s\+bar\s\+baz\s*$', s:get_statusline())
277  set statusline=foo%=bar%=baz%=qux
278  call assert_match('^foo\s\+bar\s\+baz\s\+qux\s*$', s:get_statusline())
279
280  " Test min/max width, leading zeroes, left/right justify.
281  set statusline=%04B
282  call cursor(9000, 1)
283  call assert_match('^0039\s*$', s:get_statusline())
284  set statusline=#%4B#
285  call assert_match('^#  39#\s*$', s:get_statusline())
286  set statusline=#%-4B#
287  call assert_match('^#39  #\s*$', s:get_statusline())
288  set statusline=%.6f
289  call assert_match('^<sline\s*$', s:get_statusline())
290
291  " %<: Where to truncate.
292  " First check with when %< should not truncate with many columns
293  exe 'set statusline=a%<b' . repeat('c', &columns - 3) . 'd'
294  call assert_match('^abc\+d$', s:get_statusline())
295  exe 'set statusline=a' . repeat('b', &columns - 2) . '%<c'
296  call assert_match('^ab\+c$', s:get_statusline())
297  " Then check when %< should truncate when there with too few columns.
298  exe 'set statusline=a%<b' . repeat('c', &columns - 2) . 'd'
299  call assert_match('^a<c\+d$', s:get_statusline())
300  exe 'set statusline=a' . repeat('b', &columns - 1) . '%<c'
301  call assert_match('^ab\+>$', s:get_statusline())
302
303  "%{: Evaluate expression between '%{' and '}' and substitute result.
304  syntax on
305  let s:expected_curbuf = string(bufnr(''))
306  let s:expected_curwin = string(win_getid())
307  set statusline=%{SyntaxItem()}
308  call assert_match('^vimNumber\s*$', s:get_statusline())
309  s/^/"/
310  call assert_match('^vimLineComment\s*$', s:get_statusline())
311  syntax off
312
313  " %0{: result of expression is inserted verbatim
314  set statusline=%{'\ x'}
315  call assert_match('^x\s*$', s:get_statusline())
316  set statusline=%0{'\ x'}
317  call assert_match('^ x\s*$', s:get_statusline())
318  set statusline=%{'000'}
319  call assert_match('^0\s*$', s:get_statusline())
320  set statusline=%0{'000'}
321  call assert_match('^000\s*$', s:get_statusline())
322
323  "%{%expr%}: evaluates expressions present in result of expr
324  func! Inner_eval()
325    return '%n some other text'
326  endfunc
327  func! Outer_eval()
328    return 'some text %{%Inner_eval()%}'
329  endfunc
330  set statusline=%{%Outer_eval()%}
331  call assert_match('^some text ' . bufnr() . ' some other text\s*$', s:get_statusline())
332  delfunc Inner_eval
333  delfunc Outer_eval
334
335  "%{%expr%}: Doesn't get stuck in recursion
336  func! Recurse_eval()
337    return '%{%Recurse_eval()%}'
338  endfunc
339  set statusline=%{%Recurse_eval()%}
340  call assert_match('^%{%Recurse_eval()%}\s*$', s:get_statusline())
341  delfunc Recurse_eval
342
343  "%(: Start of item group.
344  set statusline=ab%(cd%q%)de
345  call assert_match('^abde\s*$', s:get_statusline())
346  copen
347  call assert_match('^abcd\[Quickfix List]de\s*$', s:get_statusline())
348  cclose
349
350  " %#: Set highlight group. The name must follow and then a # again.
351  set statusline=ab%#Todo#cd%#Error#ef
352  call assert_match('^abcdef\s*$', s:get_statusline())
353  let sa1=screenattr(&lines - 1, 1)
354  let sa2=screenattr(&lines - 1, 3)
355  let sa3=screenattr(&lines - 1, 5)
356  call assert_notequal(sa1, sa2)
357  call assert_notequal(sa1, sa3)
358  call assert_notequal(sa2, sa3)
359  call assert_equal(sa1, screenattr(&lines - 1, 2))
360  call assert_equal(sa2, screenattr(&lines - 1, 4))
361  call assert_equal(sa3, screenattr(&lines - 1, 6))
362  call assert_equal(sa3, screenattr(&lines - 1, 7))
363
364  " %*: Set highlight group to User{N}
365  " Nvim: Combined with hl-StatusLine so needs to be set.
366  hi link User1 ErrorMsg
367  set statusline=a%1*b%0*c
368  call assert_match('^abc\s*$', s:get_statusline())
369  let sa1=screenattr(&lines - 1, 1)
370  let sa2=screenattr(&lines - 1, 2)
371  let sa3=screenattr(&lines - 1, 3)
372  call assert_equal(sa1, sa3)
373  call assert_notequal(sa1, sa2)
374
375  " An empty group that contains highlight changes
376  let g:a = ''
377  set statusline=ab%(cd%1*%{g:a}%*%)de
378  call assert_match('^abde\s*$', s:get_statusline())
379  let sa1=screenattr(&lines - 1, 1)
380  let sa2=screenattr(&lines - 1, 4)
381  call assert_equal(sa1, sa2)
382  let g:a = 'X'
383  call assert_match('^abcdXde\s*$', s:get_statusline())
384  let sa1=screenattr(&lines - 1, 1)
385  let sa2=screenattr(&lines - 1, 5)
386  let sa3=screenattr(&lines - 1, 7)
387  call assert_equal(sa1, sa3)
388  call assert_notequal(sa1, sa2)
389
390  let g:a = ''
391  set statusline=ab%1*%(cd%*%{g:a}%1*%)de
392  call assert_match('^abde\s*$', s:get_statusline())
393  let sa1=screenattr(&lines - 1, 1)
394  let sa2=screenattr(&lines - 1, 4)
395  call assert_notequal(sa1, sa2)
396  let g:a = 'X'
397  call assert_match('^abcdXde\s*$', s:get_statusline())
398  let sa1=screenattr(&lines - 1, 1)
399  let sa2=screenattr(&lines - 1, 3)
400  let sa3=screenattr(&lines - 1, 5)
401  let sa4=screenattr(&lines - 1, 7)
402  call assert_notequal(sa1, sa2)
403  call assert_equal(sa1, sa3)
404  call assert_equal(sa2, sa4)
405
406  " An empty group that contains highlight changes and doesn't reset them
407  let g:a = ''
408  set statusline=ab%(cd%1*%{g:a}%)de
409  call assert_match('^abcdde\s*$', s:get_statusline())
410  let sa1=screenattr(&lines - 1, 1)
411  let sa2=screenattr(&lines - 1, 5)
412  call assert_notequal(sa1, sa2)
413  let g:a = 'X'
414  call assert_match('^abcdXde\s*$', s:get_statusline())
415  let sa1=screenattr(&lines - 1, 1)
416  let sa2=screenattr(&lines - 1, 5)
417  let sa3=screenattr(&lines - 1, 7)
418  call assert_notequal(sa1, sa2)
419  call assert_equal(sa2, sa3)
420
421  let g:a = ''
422  set statusline=ab%1*%(cd%*%{g:a}%)de
423  call assert_match('^abcdde\s*$', s:get_statusline())
424  let sa1=screenattr(&lines - 1, 1)
425  let sa2=screenattr(&lines - 1, 3)
426  let sa3=screenattr(&lines - 1, 5)
427  call assert_notequal(sa1, sa2)
428  call assert_equal(sa1, sa3)
429  let g:a = 'X'
430  call assert_match('^abcdXde\s*$', s:get_statusline())
431  let sa1=screenattr(&lines - 1, 1)
432  let sa2=screenattr(&lines - 1, 3)
433  let sa3=screenattr(&lines - 1, 5)
434  let sa4=screenattr(&lines - 1, 7)
435  call assert_notequal(sa1, sa2)
436  call assert_equal(sa1, sa3)
437  call assert_equal(sa1, sa4)
438
439  let g:a = ''
440  set statusline=%#Error#{%(\ %{g:a}\ %)}
441  call assert_match('^{}\s*$', s:get_statusline())
442  let g:a = 'X'
443  call assert_match('^{ X }\s*$', s:get_statusline())
444
445  " %%: a percent sign.
446  set statusline=10%%
447  call assert_match('^10%\s*$', s:get_statusline())
448
449  " %!: evaluated expression is used as the option value
450  set statusline=%!2*3+1
451  call assert_match('7\s*$', s:get_statusline())
452
453  func GetNested()
454    call assert_equal(string(win_getid()), g:actual_curwin)
455    call assert_equal(string(bufnr('')), g:actual_curbuf)
456    return 'nested'
457  endfunc
458  func GetStatusLine()
459    call assert_equal(win_getid(), g:statusline_winid)
460    return 'the %{GetNested()} line'
461  endfunc
462  set statusline=%!GetStatusLine()
463  call assert_match('the nested line', s:get_statusline())
464  call assert_false(exists('g:actual_curwin'))
465  call assert_false(exists('g:actual_curbuf'))
466  call assert_false(exists('g:statusline_winid'))
467  delfunc GetNested
468  delfunc GetStatusLine
469
470  " Test statusline works with 80+ items
471  function! StatusLabel()
472    redrawstatus
473    return '[label]'
474  endfunc
475  let statusline = '%{StatusLabel()}'
476  for i in range(150)
477    let statusline .= '%#TabLine' . (i % 2 == 0 ? 'Fill' : 'Sel') . '#' . string(i)[0]
478  endfor
479  let &statusline = statusline
480  redrawstatus
481  set statusline&
482  delfunc StatusLabel
483
484
485  " Check statusline in current and non-current window
486  " with the 'fillchars' option.
487  set fillchars=stl:^,stlnc:=,vert:\|,fold:-,diff:-
488  vsplit
489  set statusline=x%=y
490  call assert_match('^x^\+y^x=\+y$', s:get_statusline())
491  set fillchars&
492  close
493
494  %bw!
495  call delete('Xstatusline')
496  set statusline&
497  set splitbelow&
498endfunc
499
500func Test_statusline_trailing_percent_zero()
501  " this was causing illegal memory access
502  set laststatus=2 stl=%!%0
503  call assert_fails('redraw', 'E15: Invalid expression: "%0"')
504  set laststatus& stl&
505endfunc
506
507func Test_statusline_visual()
508  func CallWordcount()
509    call wordcount()
510  endfunc
511  new x1
512  setl statusline=count=%{CallWordcount()}
513  " buffer must not be empty
514  call setline(1, 'hello')
515
516  " window with more lines than x1
517  new x2
518  call setline(1, range(10))
519  $
520  " Visual mode in line below liast line in x1 should not give ml_get error
521  call feedkeys("\<C-V>", "xt")
522  redraw
523
524  delfunc CallWordcount
525  bwipe! x1
526  bwipe! x2
527endfunc
528
529func Test_statusline_removed_group()
530  if !CanRunVimInTerminal()
531    throw 'Skipped: cannot make screendumps'
532  endif
533
534  let lines =<< trim END
535    scriptencoding utf-8
536    set laststatus=2
537    let &statusline = '%#StatColorHi2#%(✓%#StatColorHi2#%) Q≡'
538  END
539  call writefile(lines, 'XTest_statusline', 'D')
540
541  let buf = RunVimInTerminal('-S XTest_statusline', {'rows': 10, 'cols': 50})
542  call VerifyScreenDump(buf, 'Test_statusline_1', {})
543
544  " clean up
545  call StopVimInTerminal(buf)
546endfunc
547
548func Test_statusline_using_mode()
549  CheckScreendump
550
551  let lines =<< trim END
552    setlocal statusline=-%{mode()}-
553    split
554    setlocal statusline=+%{mode()}+
555  END
556  call writefile(lines, 'XTest_statusline', 'D')
557
558  let buf = RunVimInTerminal('-S XTest_statusline', {'rows': 7, 'cols': 50})
559  call VerifyScreenDump(buf, 'Test_statusline_mode_1', {})
560
561  call term_sendkeys(buf, ":")
562  call VerifyScreenDump(buf, 'Test_statusline_mode_2', {})
563
564  " clean up
565  call term_sendkeys(buf, "close\<CR>")
566  call StopVimInTerminal(buf)
567endfunc
568
569func Test_statusline_after_split_vsplit()
570  only
571
572  " Make the status line of each window show the window number.
573  set ls=2 stl=%{winnr()}
574
575  split | redraw
576  vsplit | redraw
577
578  " The status line of the third window should read '3' here.
579  call assert_equal('3', nr2char(screenchar(&lines - 1, 1)))
580
581  only
582  set ls& stl&
583endfunc
584
585" Test using a multibyte character for 'stl' and 'stlnc' items in 'fillchars'
586" with a custom 'statusline'
587func Test_statusline_mbyte_fillchar()
588  only
589  set fillchars=vert:\|,fold:-,stl:━,stlnc:═
590  set statusline=a%=b
591  call assert_match('^a\+━\+b$', s:get_statusline())
592  vnew
593  call assert_match('^a\+━\+b━a\+═\+b$', s:get_statusline())
594  wincmd w
595  call assert_match('^a\+═\+b═a\+━\+b$', s:get_statusline())
596  set statusline& fillchars&
597  %bw!
598endfunc
599
600" Used to write beyond allocated memory.  This assumes MAXPATHL is 4096 bytes.
601func Test_statusline_verylong_filename()
602  let fname = repeat('x', 4090)
603  " Nvim's swap file creation fails on Windows (E303) due to fname's length
604  " exe "new " .. fname
605  exe "noswapfile new " .. fname
606  set buftype=help
607  set previewwindow
608  redraw
609  bwipe!
610endfunc
611
612func Test_statusline_highlight_truncate()
613  CheckScreendump
614
615  let lines =<< trim END
616    set laststatus=2
617    hi! link User1 Directory
618    hi! link User2 ErrorMsg
619    set statusline=%.5(%1*ABC%2*DEF%1*GHI%)
620  END
621  call writefile(lines, 'XTest_statusline', 'D')
622
623  let buf = RunVimInTerminal('-S XTest_statusline', {'rows': 6})
624  call VerifyScreenDump(buf, 'Test_statusline_hl', {})
625
626  call StopVimInTerminal(buf)
627endfunc
628
629func Test_statusline_showcmd()
630  CheckScreendump
631
632  let lines =<< trim END
633    func MyStatusLine()
634      return '%S'
635    endfunc
636
637    set laststatus=2
638    set statusline=%!MyStatusLine()
639    set showcmdloc=statusline
640    call setline(1, ['a', 'b', 'c'])
641    set foldopen+=jump
642    1,2fold
643    3
644  END
645  call writefile(lines, 'XTest_statusline', 'D')
646
647  let buf = RunVimInTerminal('-S XTest_statusline', {'rows': 6})
648
649  call term_sendkeys(buf, "g")
650  call VerifyScreenDump(buf, 'Test_statusline_showcmd_1', {})
651
652  " typing "gg" should open the fold
653  call term_sendkeys(buf, "g")
654  call VerifyScreenDump(buf, 'Test_statusline_showcmd_2', {})
655
656  call term_sendkeys(buf, "\<C-V>Gl")
657  call VerifyScreenDump(buf, 'Test_statusline_showcmd_3', {})
658
659  call term_sendkeys(buf, "\<Esc>1234")
660  call VerifyScreenDump(buf, 'Test_statusline_showcmd_4', {})
661
662  call term_sendkeys(buf, "\<Esc>:set statusline=\<CR>")
663  call term_sendkeys(buf, ":\<CR>")
664  call term_sendkeys(buf, "1234")
665  call VerifyScreenDump(buf, 'Test_statusline_showcmd_5', {})
666
667  call StopVimInTerminal(buf)
668endfunc
669
670func Test_statusline_showcmd_cmd_mapping()
671  set showcmd
672  set statusline=AAA%SBBB
673  set showcmdloc=statusline
674  try
675    let g:showcmd_statusline = ''
676    nnoremap <F3> <Cmd>let g:showcmd_statusline = <SID>get_statusline()<CR>
677    call feedkeys("\<F3>", 'xt')
678    call assert_equal('AAABBB', trim(g:showcmd_statusline))
679    let g:showcmd_statusline = ''
680    nunmap <F3>
681
682    "nnoremap <F3> <ScriptCmd>let g:showcmd_statusline = <SID>get_statusline()<CR>
683    "call feedkeys("\<F3>", 'xt')
684    "call assert_equal('AAABBB', trim(g:showcmd_statusline))
685  finally
686    silent! nunmap <F3>
687    unlet! g:showcmd_statusline
688    set showcmd&
689    set statusline&
690    set showcmdloc&
691  endtry
692endfunc
693
694func Test_statusline_showcmd_nop_map()
695  CheckRunVimInTerminal
696
697  let lines =<< trim END
698    set timeoutlen=500 showcmdloc=statusline laststatus=2
699    nnoremap <space> <nop>
700    nnoremap <space><space> <nop>
701  END
702  call writefile(lines, 'XTest_statusline_showcmd_nop', 'D')
703
704  let buf = RunVimInTerminal('-S XTest_statusline_showcmd_nop', {'rows': 6})
705  call term_sendkeys(buf, ' ')
706  call WaitForAssert({-> assert_match('<20>', term_getline(buf, 5))}, 400)
707  sleep 500m
708  call WaitForAssert({-> assert_notmatch('<20>', term_getline(buf, 5))}, 400)
709
710  call StopVimInTerminal(buf)
711endfunc
712
713func Test_statusline_highlight_group_cleared()
714  CheckScreendump
715
716  " the laststatus option is there to prevent
717  " the code-style test from complaining about
718  " trailing whitespace
719  let lines =<< trim END
720    set fillchars=stl:\ ,stlnc:\  laststatus=2
721    split
722    hi clear StatusLine
723    hi clear StatusLineNC
724  END
725  call writefile(lines, 'XTest_statusline_stl', 'D')
726
727  let buf = RunVimInTerminal('-S XTest_statusline_stl', {})
728
729  call VerifyScreenDump(buf, 'Test_statusline_stl_1', {})
730
731  call StopVimInTerminal(buf)
732endfunc
733
734" Test for setting both global and local 'statusline' values in a sandbox
735func Test_statusline_in_sandbox()
736  func SandboxStatusLine()
737    call writefile(['after'], 'Xsandboxstatusline_write')
738    return "status line"
739  endfunc
740
741  func Check_statusline_in_sandbox(set_cmd0, set_cmd1)
742    only
743    11new | 20vsplit
744    call setline(1, 'foo')
745    windo setlocal statusline=SomethingElse
746    wincmd t
747    setlocal statusline=
748    call writefile(['before'], 'Xsandboxstatusline_write', 'D')
749
750    exe 'sandbox' a:set_cmd0 'statusline=%!SandboxStatusLine()'
751    call assert_equal('', &l:statusline)
752    sandbox setlocal statusline=%!SandboxStatusLine()
753    call assert_fails('redrawstatus', 'E48:')
754    call assert_equal(['before'], readfile('Xsandboxstatusline_write'))
755    wincmd b
756    call assert_fails('redrawstatus!', 'E48:')
757    call assert_equal(['before'], readfile('Xsandboxstatusline_write'))
758    wincmd t
759
760    5split
761    call assert_fails('redrawstatus!', 'E48:')
762    call assert_equal(['before'], readfile('Xsandboxstatusline_write'))
763    close
764
765    setlocal statusline=%!SandboxStatusLine() | redrawstatus
766    call assert_equal('status line', Screenline(12))
767    call assert_equal(['after'], readfile('Xsandboxstatusline_write'))
768
769    call writefile(['before'], 'Xsandboxstatusline_write')
770    setlocal statusline=
771    call assert_fails('redrawstatus', 'E48:')
772    call assert_equal(['before'], readfile('Xsandboxstatusline_write'))
773
774    5split
775    call assert_fails('redrawstatus!', 'E48:')
776    call assert_equal(['before'], readfile('Xsandboxstatusline_write'))
777
778    exe a:set_cmd1 'statusline=%!SandboxStatusLine()' | redrawstatus!
779    call assert_equal('', &l:statusline)
780    call assert_equal('status line', Screenline(6))
781    call assert_equal('status line', Screenline(12))
782    call assert_equal(['after'], readfile('Xsandboxstatusline_write'))
783    bwipe!
784  endfunc
785
786  set noequalalways
787  call Check_statusline_in_sandbox('setglobal', 'setglobal')
788  call Check_statusline_in_sandbox('setglobal', 'set')
789  call Check_statusline_in_sandbox('set', 'setglobal')
790  call Check_statusline_in_sandbox('set', 'set')
791
792  set equalalways& statusline&
793  delfunc SandboxStatusLine
794  delfunc Check_statusline_in_sandbox
795endfunc
796
797" This used to call memmove with a negative size and crash Vim
798func Test_statusline_singlebyte_negative()
799  let [_columns, _ls, _stl, _enc]  = [&columns, &ls, &stl, &enc]
800  " set encoding=latin1
801  set laststatus=2 columns=15
802  setl stl=%#ErrorMsg#abcdtàØ?}}o@`s`ÿæCú\xE%#Normal#
803  vsp
804  setl stl=%#ErrorMsg#abcdtàØ?}}o@`s`ÿæCú\xE%#Normal#
805  redraw!
806  redrawstatus
807  bw!
808  let [&columns, &ls, &stl, &enc] = [_columns, _ls, _stl, _enc]
809endfunc
810
811func Test_statusline_empty()
812  set laststatus=2 statusline=%!'%{}%'
813  try
814  redraw!
815  catch
816  endtry
817  set laststatus&
818  set statusline&
819endfunc
820
821" vim: shiftwidth=2 sts=2 expandtab