blob: ce8cb58ae3bb1316ad75c207c39442649deabeb7 [file] [log] [blame]
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001" Test for tabline
Bram Moolenaare12bab32019-01-08 22:02:56 +01002
Christian Brabandteb380b92025-07-07 20:53:55 +02003source util/screendump.vim
Bram Moolenaare12bab32019-01-08 22:02:56 +01004
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
zeertzjqd392a742023-07-01 20:24:40 +0100133 bw!
Bram Moolenaarf56c95f2020-07-21 19:25:18 +0200134 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
Bram Moolenaarc4860bd2022-10-15 20:52:26 +0100157 call writefile(lines, 'Xclickscript', 'D')
Bram Moolenaar80525752022-08-24 19:27:45 +0100158 call RunVim([], [], "-e -s -S Xclickscript -c qa")
Bram Moolenaar80525752022-08-24 19:27:45 +0100159endfunc
160
Luuk van Baalba936f62022-12-15 13:15:39 +0000161func Test_tabline_showcmd()
162 CheckScreendump
163
164 let lines =<< trim END
zeertzjq378e6c02023-01-14 11:46:49 +0000165 func MyTabLine()
166 return '%S'
167 endfunc
168
Luuk van Baalba936f62022-12-15 13:15:39 +0000169 set showtabline=2
zeertzjq378e6c02023-01-14 11:46:49 +0000170 set tabline=%!MyTabLine()
Luuk van Baalba936f62022-12-15 13:15:39 +0000171 set showcmdloc=tabline
172 call setline(1, ['a', 'b', 'c'])
zeertzjq378e6c02023-01-14 11:46:49 +0000173 set foldopen+=jump
174 1,2fold
175 3
Luuk van Baalba936f62022-12-15 13:15:39 +0000176 END
177 call writefile(lines, 'XTest_tabline', 'D')
178
179 let buf = RunVimInTerminal('-S XTest_tabline', {'rows': 6})
180
zeertzjq378e6c02023-01-14 11:46:49 +0000181 call term_sendkeys(buf, "g")
Luuk van Baalba936f62022-12-15 13:15:39 +0000182 call VerifyScreenDump(buf, 'Test_tabline_showcmd_1', {})
183
zeertzjq378e6c02023-01-14 11:46:49 +0000184 " typing "gg" should open the fold
185 call term_sendkeys(buf, "g")
Luuk van Baalba936f62022-12-15 13:15:39 +0000186 call VerifyScreenDump(buf, 'Test_tabline_showcmd_2', {})
zeertzjq378e6c02023-01-14 11:46:49 +0000187
188 call term_sendkeys(buf, "\<C-V>Gl")
189 call VerifyScreenDump(buf, 'Test_tabline_showcmd_3', {})
190
191 call term_sendkeys(buf, "\<Esc>1234")
192 call VerifyScreenDump(buf, 'Test_tabline_showcmd_4', {})
193
194 call term_sendkeys(buf, "\<Esc>:set tabline=\<CR>")
195 call term_sendkeys(buf, ":\<CR>")
196 call term_sendkeys(buf, "1234")
197 call VerifyScreenDump(buf, 'Test_tabline_showcmd_5', {})
198
199 call StopVimInTerminal(buf)
Luuk van Baalba936f62022-12-15 13:15:39 +0000200endfunc
Bram Moolenaar80525752022-08-24 19:27:45 +0100201
zeertzjqd392a742023-07-01 20:24:40 +0100202func TruncTabLine()
203 return '%1T口口%2Ta' .. repeat('b', &columns - 4) .. '%999X%#TabLine#c'
204endfunc
205
206" Test 'tabline' with truncated double-width label at the start.
207func Test_tabline_truncated_double_width()
208 tabnew
209 redraw
210 call assert_match('X$', Screenline(1))
211 let attr_TabLineFill = screenattr(1, &columns - 1)
212 let attr_TabLine = screenattr(1, &columns)
213 call assert_notequal(attr_TabLine, attr_TabLineFill)
214
215 set tabline=%!TruncTabLine()
216 redraw
217 call assert_equal('<a' .. repeat('b', &columns - 4) .. 'c', Screenline(1))
218 call assert_equal(attr_TabLineFill, screenattr(1, &columns - 2))
219 call assert_equal(attr_TabLine, screenattr(1, &columns - 1))
220 call assert_equal(attr_TabLine, screenattr(1, &columns))
221
222 bw!
223 set tabline=
224endfunc
225
Bram Moolenaar832adf92020-06-25 19:01:36 +0200226" vim: shiftwidth=2 sts=2 expandtab