blob: 3cba2aab47278a68b920415118b75ed9e2d3b343 [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
Luuk van Baalba936f62022-12-15 13:15:39 +00004source view_util.vim
5source check.vim
6source screendump.vim
Bram Moolenaare12bab32019-01-08 22:02:56 +01007
8func TablineWithCaughtError()
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +02009 let s:func_in_tabline_called = 1
10 try
11 call eval('unknown expression')
12 catch
13 endtry
14 return ''
Bram Moolenaare12bab32019-01-08 22:02:56 +010015endfunc
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020016
Bram Moolenaare12bab32019-01-08 22:02:56 +010017func TablineWithError()
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020018 let s:func_in_tabline_called = 1
19 call eval('unknown expression')
20 return ''
Bram Moolenaare12bab32019-01-08 22:02:56 +010021endfunc
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020022
Bram Moolenaare12bab32019-01-08 22:02:56 +010023func Test_caught_error_in_tabline()
Bram Moolenaar73cd8fb2016-04-11 22:49:03 +020024 if has('gui')
25 set guioptions-=e
26 endif
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020027 let showtabline_save = &showtabline
28 set showtabline=2
29 let s:func_in_tabline_called = 0
30 let tabline = '%{TablineWithCaughtError()}'
31 let &tabline = tabline
32 redraw!
33 call assert_true(s:func_in_tabline_called)
34 call assert_equal(tabline, &tabline)
35 set tabline=
36 let &showtabline = showtabline_save
Bram Moolenaare12bab32019-01-08 22:02:56 +010037endfunc
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020038
Bram Moolenaare12bab32019-01-08 22:02:56 +010039func Test_tabline_will_be_disabled_with_error()
Bram Moolenaar73cd8fb2016-04-11 22:49:03 +020040 if has('gui')
41 set guioptions-=e
42 endif
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020043 let showtabline_save = &showtabline
44 set showtabline=2
45 let s:func_in_tabline_called = 0
46 let tabline = '%{TablineWithError()}'
47 try
48 let &tabline = tabline
49 redraw!
50 catch
51 endtry
52 call assert_true(s:func_in_tabline_called)
53 call assert_equal('', &tabline)
54 set tabline=
55 let &showtabline = showtabline_save
Bram Moolenaare12bab32019-01-08 22:02:56 +010056endfunc
57
58func Test_redrawtabline()
59 if has('gui')
60 set guioptions-=e
61 endif
62 let showtabline_save = &showtabline
63 set showtabline=2
64 set tabline=%{bufnr('$')}
65 edit Xtabline1
66 edit Xtabline2
67 redraw
68 call assert_match(bufnr('$') . '', Screenline(1))
69 au BufAdd * redrawtabline
70 badd Xtabline3
71 call assert_match(bufnr('$') . '', Screenline(1))
72
73 set tabline=
74 let &showtabline = showtabline_save
75 au! Bufadd
76endfunc
Bram Moolenaar832adf92020-06-25 19:01:36 +020077
78" Test for the "%T" and "%X" flags in the 'tabline' option
79func MyTabLine()
80 let s = ''
81 for i in range(tabpagenr('$'))
82 " set the tab page number (for mouse clicks)
83 let s .= '%' . (i + 1) . 'T'
84
85 " the label is made by MyTabLabel()
86 let s .= ' %{MyTabLabel(' . (i + 1) . ')} '
87 endfor
88
89 " after the last tab fill with TabLineFill and reset tab page nr
90 let s .= '%T'
91
92 " right-align the label to close the current tab page
93 if tabpagenr('$') > 1
94 let s .= '%=%Xclose'
95 endif
96
97 return s
98endfunc
99
100func MyTabLabel(n)
101 let buflist = tabpagebuflist(a:n)
102 let winnr = tabpagewinnr(a:n)
103 return bufname(buflist[winnr - 1])
104endfunc
105
106func Test_tabline_flags()
107 if has('gui')
108 set guioptions-=e
109 endif
110 set tabline=%!MyTabLine()
111 edit Xtabline1
112 tabnew Xtabline2
113 redrawtabline
114 call assert_match('^ Xtabline1 Xtabline2\s\+close$', Screenline(1))
115 set tabline=
116 %bw!
117endfunc
118
Bram Moolenaarf56c95f2020-07-21 19:25:18 +0200119function EmptyTabname()
120 return ""
121endfunction
122
123function MakeTabLine() abort
124 let titles = map(range(1, tabpagenr('$')), '"%( %" . v:val . "T%{EmptyTabname()}%T %)"')
125 let sep = 'あ'
126 let tabpages = join(titles, sep)
127 return tabpages .. sep .. '%=%999X X'
128endfunction
129
130func Test_tabline_empty_group()
131 " this was reading invalid memory
132 set tabline=%!MakeTabLine()
133 tabnew
134 redraw!
135
zeertzjqd392a742023-07-01 20:24:40 +0100136 bw!
Bram Moolenaarf56c95f2020-07-21 19:25:18 +0200137 set tabline=
138endfunc
139
Brandon Richardsona493b652022-02-19 11:45:03 +0000140" When there are exactly 20 tabline format items (the exact size of the
141" initial tabline items array), test that we don't write beyond the size
142" of the array.
143func Test_tabline_20_format_items_no_overrun()
144 set showtabline=2
Bram Moolenaarf56c95f2020-07-21 19:25:18 +0200145
Brandon Richardsona493b652022-02-19 11:45:03 +0000146 let tabline = repeat('%#StatColorHi2#', 20)
147 let &tabline = tabline
148 redrawtabline
149
150 set showtabline& tabline&
151endfunc
Bram Moolenaarf56c95f2020-07-21 19:25:18 +0200152
Bram Moolenaar80525752022-08-24 19:27:45 +0100153func Test_mouse_click_in_tab()
154 " This used to crash because TabPageIdxs[] was not initialized
155 let lines =<< trim END
156 tabnew
157 set mouse=a
158 exe "norm \<LeftMouse>"
159 END
Bram Moolenaarc4860bd2022-10-15 20:52:26 +0100160 call writefile(lines, 'Xclickscript', 'D')
Bram Moolenaar80525752022-08-24 19:27:45 +0100161 call RunVim([], [], "-e -s -S Xclickscript -c qa")
Bram Moolenaar80525752022-08-24 19:27:45 +0100162endfunc
163
Luuk van Baalba936f62022-12-15 13:15:39 +0000164func Test_tabline_showcmd()
165 CheckScreendump
166
167 let lines =<< trim END
zeertzjq378e6c02023-01-14 11:46:49 +0000168 func MyTabLine()
169 return '%S'
170 endfunc
171
Luuk van Baalba936f62022-12-15 13:15:39 +0000172 set showtabline=2
zeertzjq378e6c02023-01-14 11:46:49 +0000173 set tabline=%!MyTabLine()
Luuk van Baalba936f62022-12-15 13:15:39 +0000174 set showcmdloc=tabline
175 call setline(1, ['a', 'b', 'c'])
zeertzjq378e6c02023-01-14 11:46:49 +0000176 set foldopen+=jump
177 1,2fold
178 3
Luuk van Baalba936f62022-12-15 13:15:39 +0000179 END
180 call writefile(lines, 'XTest_tabline', 'D')
181
182 let buf = RunVimInTerminal('-S XTest_tabline', {'rows': 6})
183
zeertzjq378e6c02023-01-14 11:46:49 +0000184 call term_sendkeys(buf, "g")
Luuk van Baalba936f62022-12-15 13:15:39 +0000185 call VerifyScreenDump(buf, 'Test_tabline_showcmd_1', {})
186
zeertzjq378e6c02023-01-14 11:46:49 +0000187 " typing "gg" should open the fold
188 call term_sendkeys(buf, "g")
Luuk van Baalba936f62022-12-15 13:15:39 +0000189 call VerifyScreenDump(buf, 'Test_tabline_showcmd_2', {})
zeertzjq378e6c02023-01-14 11:46:49 +0000190
191 call term_sendkeys(buf, "\<C-V>Gl")
192 call VerifyScreenDump(buf, 'Test_tabline_showcmd_3', {})
193
194 call term_sendkeys(buf, "\<Esc>1234")
195 call VerifyScreenDump(buf, 'Test_tabline_showcmd_4', {})
196
197 call term_sendkeys(buf, "\<Esc>:set tabline=\<CR>")
198 call term_sendkeys(buf, ":\<CR>")
199 call term_sendkeys(buf, "1234")
200 call VerifyScreenDump(buf, 'Test_tabline_showcmd_5', {})
201
202 call StopVimInTerminal(buf)
Luuk van Baalba936f62022-12-15 13:15:39 +0000203endfunc
Bram Moolenaar80525752022-08-24 19:27:45 +0100204
zeertzjqd392a742023-07-01 20:24:40 +0100205func TruncTabLine()
206 return '%1T口口%2Ta' .. repeat('b', &columns - 4) .. '%999X%#TabLine#c'
207endfunc
208
209" Test 'tabline' with truncated double-width label at the start.
210func Test_tabline_truncated_double_width()
211 tabnew
212 redraw
213 call assert_match('X$', Screenline(1))
214 let attr_TabLineFill = screenattr(1, &columns - 1)
215 let attr_TabLine = screenattr(1, &columns)
216 call assert_notequal(attr_TabLine, attr_TabLineFill)
217
218 set tabline=%!TruncTabLine()
219 redraw
220 call assert_equal('<a' .. repeat('b', &columns - 4) .. 'c', Screenline(1))
221 call assert_equal(attr_TabLineFill, screenattr(1, &columns - 2))
222 call assert_equal(attr_TabLine, screenattr(1, &columns - 1))
223 call assert_equal(attr_TabLine, screenattr(1, &columns))
224
225 bw!
226 set tabline=
227endfunc
228
Bram Moolenaar832adf92020-06-25 19:01:36 +0200229" vim: shiftwidth=2 sts=2 expandtab