blob: 556b85931cb684856a6a24e22b4d537b5e76a44b [file] [log] [blame]
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001" Test for tabline
Bram Moolenaare12bab32019-01-08 22:02:56 +01002
3source shared.vim
4
5func TablineWithCaughtError()
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +02006 let s:func_in_tabline_called = 1
7 try
8 call eval('unknown expression')
9 catch
10 endtry
11 return ''
Bram Moolenaare12bab32019-01-08 22:02:56 +010012endfunc
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020013
Bram Moolenaare12bab32019-01-08 22:02:56 +010014func TablineWithError()
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020015 let s:func_in_tabline_called = 1
16 call eval('unknown expression')
17 return ''
Bram Moolenaare12bab32019-01-08 22:02:56 +010018endfunc
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020019
Bram Moolenaare12bab32019-01-08 22:02:56 +010020func Test_caught_error_in_tabline()
Bram Moolenaar73cd8fb2016-04-11 22:49:03 +020021 if has('gui')
22 set guioptions-=e
23 endif
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020024 let showtabline_save = &showtabline
25 set showtabline=2
26 let s:func_in_tabline_called = 0
27 let tabline = '%{TablineWithCaughtError()}'
28 let &tabline = tabline
29 redraw!
30 call assert_true(s:func_in_tabline_called)
31 call assert_equal(tabline, &tabline)
32 set tabline=
33 let &showtabline = showtabline_save
Bram Moolenaare12bab32019-01-08 22:02:56 +010034endfunc
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020035
Bram Moolenaare12bab32019-01-08 22:02:56 +010036func Test_tabline_will_be_disabled_with_error()
Bram Moolenaar73cd8fb2016-04-11 22:49:03 +020037 if has('gui')
38 set guioptions-=e
39 endif
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020040 let showtabline_save = &showtabline
41 set showtabline=2
42 let s:func_in_tabline_called = 0
43 let tabline = '%{TablineWithError()}'
44 try
45 let &tabline = tabline
46 redraw!
47 catch
48 endtry
49 call assert_true(s:func_in_tabline_called)
50 call assert_equal('', &tabline)
51 set tabline=
52 let &showtabline = showtabline_save
Bram Moolenaare12bab32019-01-08 22:02:56 +010053endfunc
54
55func Test_redrawtabline()
56 if has('gui')
57 set guioptions-=e
58 endif
59 let showtabline_save = &showtabline
60 set showtabline=2
61 set tabline=%{bufnr('$')}
62 edit Xtabline1
63 edit Xtabline2
64 redraw
65 call assert_match(bufnr('$') . '', Screenline(1))
66 au BufAdd * redrawtabline
67 badd Xtabline3
68 call assert_match(bufnr('$') . '', Screenline(1))
69
70 set tabline=
71 let &showtabline = showtabline_save
72 au! Bufadd
73endfunc
Bram Moolenaar832adf92020-06-25 19:01:36 +020074
75" Test for the "%T" and "%X" flags in the 'tabline' option
76func MyTabLine()
77 let s = ''
78 for i in range(tabpagenr('$'))
79 " set the tab page number (for mouse clicks)
80 let s .= '%' . (i + 1) . 'T'
81
82 " the label is made by MyTabLabel()
83 let s .= ' %{MyTabLabel(' . (i + 1) . ')} '
84 endfor
85
86 " after the last tab fill with TabLineFill and reset tab page nr
87 let s .= '%T'
88
89 " right-align the label to close the current tab page
90 if tabpagenr('$') > 1
91 let s .= '%=%Xclose'
92 endif
93
94 return s
95endfunc
96
97func MyTabLabel(n)
98 let buflist = tabpagebuflist(a:n)
99 let winnr = tabpagewinnr(a:n)
100 return bufname(buflist[winnr - 1])
101endfunc
102
103func Test_tabline_flags()
104 if has('gui')
105 set guioptions-=e
106 endif
107 set tabline=%!MyTabLine()
108 edit Xtabline1
109 tabnew Xtabline2
110 redrawtabline
111 call assert_match('^ Xtabline1 Xtabline2\s\+close$', Screenline(1))
112 set tabline=
113 %bw!
114endfunc
115
Bram Moolenaarf56c95f2020-07-21 19:25:18 +0200116function EmptyTabname()
117 return ""
118endfunction
119
120function MakeTabLine() abort
121 let titles = map(range(1, tabpagenr('$')), '"%( %" . v:val . "T%{EmptyTabname()}%T %)"')
122 let sep = 'あ'
123 let tabpages = join(titles, sep)
124 return tabpages .. sep .. '%=%999X X'
125endfunction
126
127func Test_tabline_empty_group()
128 " this was reading invalid memory
129 set tabline=%!MakeTabLine()
130 tabnew
131 redraw!
132
133 tabclose
134 set tabline=
135endfunc
136
Brandon Richardsona493b652022-02-19 11:45:03 +0000137" When there are exactly 20 tabline format items (the exact size of the
138" initial tabline items array), test that we don't write beyond the size
139" of the array.
140func Test_tabline_20_format_items_no_overrun()
141 set showtabline=2
Bram Moolenaarf56c95f2020-07-21 19:25:18 +0200142
Brandon Richardsona493b652022-02-19 11:45:03 +0000143 let tabline = repeat('%#StatColorHi2#', 20)
144 let &tabline = tabline
145 redrawtabline
146
147 set showtabline& tabline&
148endfunc
Bram Moolenaarf56c95f2020-07-21 19:25:18 +0200149
Bram Moolenaar80525752022-08-24 19:27:45 +0100150func Test_mouse_click_in_tab()
151 " This used to crash because TabPageIdxs[] was not initialized
152 let lines =<< trim END
153 tabnew
154 set mouse=a
155 exe "norm \<LeftMouse>"
156 END
157 call writefile(lines, 'Xclickscript')
158 call RunVim([], [], "-e -s -S Xclickscript -c qa")
159
160 call delete('Xclickscript')
161endfunc
162
163
Bram Moolenaar832adf92020-06-25 19:01:36 +0200164" vim: shiftwidth=2 sts=2 expandtab