blob: a0cd5bd79ffbac251d265303579e44c52b00052f [file] [log] [blame]
Bram Moolenaar0c0590d2017-01-28 13:48:10 +01001" Tests for Unicode manipulations
Bram Moolenaar94722c52023-01-28 19:19:03 +00002
Christian Brabandteb380b92025-07-07 20:53:55 +02003source util/screendump.vim
Bram Moolenaar0c0590d2017-01-28 13:48:10 +01004
5" Visual block Insert adjusts for multi-byte char
6func Test_visual_block_insert()
7 new
8 call setline(1, ["aaa", "あああ", "bbb"])
9 exe ":norm! gg0l\<C-V>jjIx\<Esc>"
Bram Moolenaarfc6cceb2022-01-20 12:22:35 +000010 call assert_equal(['axaa', ' xあああ', 'bxbb'], getline(1, '$'))
Bram Moolenaar0c0590d2017-01-28 13:48:10 +010011 bwipeout!
12endfunc
13
Bram Moolenaar70ce8a12021-03-14 19:02:09 +010014" Test for built-in functions strchars() and strcharlen()
Bram Moolenaar0c0590d2017-01-28 13:48:10 +010015func Test_strchars()
16 let inp = ["a", "あいa", "A\u20dd", "A\u20dd\u20dd", "\u20dd"]
17 let exp = [[1, 1, 1], [3, 3, 3], [2, 2, 1], [3, 3, 1], [1, 1, 1]]
18 for i in range(len(inp))
19 call assert_equal(exp[i][0], strchars(inp[i]))
Bram Moolenaarf6ed61e2019-09-07 19:05:09 +020020 call assert_equal(exp[i][1], inp[i]->strchars(0))
Bram Moolenaar0c0590d2017-01-28 13:48:10 +010021 call assert_equal(exp[i][2], strchars(inp[i], 1))
22 endfor
Bram Moolenaar70ce8a12021-03-14 19:02:09 +010023
24 let exp = [1, 3, 1, 1, 1]
25 for i in range(len(inp))
26 call assert_equal(exp[i], inp[i]->strcharlen())
27 call assert_equal(exp[i], strcharlen(inp[i]))
28 endfor
29
zeertzjq8cf51372023-05-08 15:31:38 +010030 call assert_fails("call strchars('abc', 2)", ['E1023:', 'E1023:'])
31 call assert_fails("call strchars('abc', -1)", ['E1023:', 'E1023:'])
32 call assert_fails("call strchars('abc', {})", ['E728:', 'E728:'])
33 call assert_fails("call strchars('abc', [])", ['E745:', 'E745:'])
Bram Moolenaar0c0590d2017-01-28 13:48:10 +010034endfunc
35
36" Test for customlist completion
Bram Moolenaar1e115362019-01-09 23:01:02 +010037func CustomComplete1(lead, line, pos)
Bram Moolenaar0c0590d2017-01-28 13:48:10 +010038 return ['あ', 'い']
Bram Moolenaar1e115362019-01-09 23:01:02 +010039endfunc
Bram Moolenaar0c0590d2017-01-28 13:48:10 +010040
Bram Moolenaar1e115362019-01-09 23:01:02 +010041func CustomComplete2(lead, line, pos)
Bram Moolenaar0c0590d2017-01-28 13:48:10 +010042 return ['あたし', 'あたま', 'あたりめ']
Bram Moolenaar1e115362019-01-09 23:01:02 +010043endfunc
Bram Moolenaar0c0590d2017-01-28 13:48:10 +010044
Bram Moolenaar1e115362019-01-09 23:01:02 +010045func CustomComplete3(lead, line, pos)
Bram Moolenaar0c0590d2017-01-28 13:48:10 +010046 return ['Nこ', 'Nん', 'Nぶ']
Bram Moolenaar1e115362019-01-09 23:01:02 +010047endfunc
Bram Moolenaar0c0590d2017-01-28 13:48:10 +010048
49func Test_customlist_completion()
50 command -nargs=1 -complete=customlist,CustomComplete1 Test1 echo
51 call feedkeys(":Test1 \<C-L>\<C-B>\"\<CR>", 'itx')
52 call assert_equal('"Test1 ', getreg(':'))
53
54 command -nargs=1 -complete=customlist,CustomComplete2 Test2 echo
55 call feedkeys(":Test2 \<C-L>\<C-B>\"\<CR>", 'itx')
56 call assert_equal('"Test2 あた', getreg(':'))
57
58 command -nargs=1 -complete=customlist,CustomComplete3 Test3 echo
59 call feedkeys(":Test3 \<C-L>\<C-B>\"\<CR>", 'itx')
60 call assert_equal('"Test3 N', getreg(':'))
61
62 call garbagecollect(1)
zeertzjqa59e0312024-04-15 19:14:38 +020063 delcommand Test1
64 delcommand Test2
65 delcommand Test3
Bram Moolenaar0c0590d2017-01-28 13:48:10 +010066endfunc
67
68" Yank one 3 byte character and check the mark columns.
69func Test_getvcol()
70 new
71 call setline(1, "x\u2500x")
72 normal 0lvy
73 call assert_equal(2, col("'["))
74 call assert_equal(4, col("']"))
75 call assert_equal(2, virtcol("'["))
76 call assert_equal(2, virtcol("']"))
77endfunc
Bram Moolenaar2912abb2019-03-29 14:16:42 +010078
Bram Moolenaar9d401282019-04-06 13:18:12 +020079func Test_list2str_str2list_utf8()
80 " One Unicode codepoint
81 let s = "\u3042\u3044"
82 let l = [0x3042, 0x3044]
83 call assert_equal(l, str2list(s, 1))
84 call assert_equal(s, list2str(l, 1))
85 if &enc ==# 'utf-8'
86 call assert_equal(str2list(s), str2list(s, 1))
87 call assert_equal(list2str(l), list2str(l, 1))
88 endif
89
90 " With composing characters
91 let s = "\u304b\u3099\u3044"
92 let l = [0x304b, 0x3099, 0x3044]
93 call assert_equal(l, str2list(s, 1))
Bram Moolenaar02b31112019-08-31 22:16:38 +020094 call assert_equal(s, l->list2str(1))
Bram Moolenaar9d401282019-04-06 13:18:12 +020095 if &enc ==# 'utf-8'
96 call assert_equal(str2list(s), str2list(s, 1))
97 call assert_equal(list2str(l), list2str(l, 1))
98 endif
99
100 " Null list is the same as an empty list
101 call assert_equal('', list2str([]))
102 call assert_equal('', list2str(test_null_list()))
103endfunc
104
105func Test_list2str_str2list_latin1()
106 " When 'encoding' is not multi-byte can still get utf-8 string.
107 " But we need to create the utf-8 string while 'encoding' is utf-8.
108 let s = "\u3042\u3044"
109 let l = [0x3042, 0x3044]
110
111 let save_encoding = &encoding
112 set encoding=latin1
Bram Moolenaar94722c52023-01-28 19:19:03 +0000113
Bram Moolenaar9d401282019-04-06 13:18:12 +0200114 let lres = str2list(s, 1)
115 let sres = list2str(l, 1)
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100116 call assert_equal([65, 66, 67], str2list("ABC"))
Bram Moolenaar9d401282019-04-06 13:18:12 +0200117
Bram Moolenaar08f41572020-04-20 16:50:00 +0200118 " Try converting a list to a string in latin-1 encoding
119 call assert_equal([1, 2, 3], str2list(list2str([1, 2, 3])))
120
Bram Moolenaar9d401282019-04-06 13:18:12 +0200121 let &encoding = save_encoding
122 call assert_equal(l, lres)
123 call assert_equal(s, sres)
124endfunc
125
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100126func Test_screenchar_utf8()
127 new
128
Bram Moolenaar94722c52023-01-28 19:19:03 +0000129 " 1-cell, with composing characters
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100130 call setline(1, ["ABC\u0308"])
131 redraw
132 call assert_equal([0x0041], screenchars(1, 1))
Bram Moolenaar196b4662019-09-06 21:34:30 +0200133 call assert_equal([0x0042], 1->screenchars(2))
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100134 call assert_equal([0x0043, 0x0308], screenchars(1, 3))
135 call assert_equal("A", screenstring(1, 1))
136 call assert_equal("B", screenstring(1, 2))
137 call assert_equal("C\u0308", screenstring(1, 3))
138
zeertzjq47eec672023-06-01 20:26:55 +0100139 " 1-cell, with 6 composing characters
140 set maxcombine=6
141 call setline(1, ["ABC" .. repeat("\u0308", 6)])
142 redraw
143 call assert_equal([0x0041], screenchars(1, 1))
144 call assert_equal([0x0042], 1->screenchars(2))
145 " This should not use uninitialized memory
146 call assert_equal([0x0043] + repeat([0x0308], 6), screenchars(1, 3))
147 call assert_equal("A", screenstring(1, 1))
148 call assert_equal("B", screenstring(1, 2))
149 call assert_equal("C" .. repeat("\u0308", 6), screenstring(1, 3))
150 set maxcombine&
151
Bram Moolenaar94722c52023-01-28 19:19:03 +0000152 " 2-cells, with composing characters
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100153 let text = "\u3042\u3044\u3046\u3099"
154 call setline(1, text)
155 redraw
156 call assert_equal([0x3042], screenchars(1, 1))
157 call assert_equal([0], screenchars(1, 2))
158 call assert_equal([0x3044], screenchars(1, 3))
159 call assert_equal([0], screenchars(1, 4))
160 call assert_equal([0x3046, 0x3099], screenchars(1, 5))
161
162 call assert_equal("\u3042", screenstring(1, 1))
163 call assert_equal("", screenstring(1, 2))
164 call assert_equal("\u3044", screenstring(1, 3))
165 call assert_equal("", screenstring(1, 4))
166 call assert_equal("\u3046\u3099", screenstring(1, 5))
167
Bram Moolenaar48aed082019-03-30 15:44:17 +0100168 call assert_equal([text . ' '], ScreenLines(1, 8))
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100169
170 bwipe!
171endfunc
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100172
Bram Moolenaar08aac3c2020-08-28 21:04:24 +0200173func Test_setcellwidths()
zeertzjq05aacec2024-04-14 18:52:49 +0200174 new
Bram Moolenaar08aac3c2020-08-28 21:04:24 +0200175 call setcellwidths([
176 \ [0x1330, 0x1330, 2],
Bram Moolenaar08aac3c2020-08-28 21:04:24 +0200177 \ [9999, 10000, 1],
Bram Moolenaarb06a6d52020-08-28 23:27:20 +0200178 \ [0x1337, 0x1339, 2],
Bram Moolenaar08aac3c2020-08-28 21:04:24 +0200179 \])
180
181 call assert_equal(2, strwidth("\u1330"))
182 call assert_equal(1, strwidth("\u1336"))
183 call assert_equal(2, strwidth("\u1337"))
184 call assert_equal(2, strwidth("\u1339"))
185 call assert_equal(1, strwidth("\u133a"))
186
K.Takata71933232023-01-20 16:00:55 +0000187 for aw in ['single', 'double']
188 exe 'set ambiwidth=' . aw
189 " Handle \u0080 to \u009F as control chars even on MS-Windows.
190 set isprint=@,161-255
191
192 call setcellwidths([])
193 " Control chars
194 call assert_equal(4, strwidth("\u0081"))
195 call assert_equal(6, strwidth("\uFEFF"))
196 " Ambiguous width chars
197 call assert_equal((aw == 'single') ? 1 : 2, strwidth("\u00A1"))
198 call assert_equal((aw == 'single') ? 1 : 2, strwidth("\u2010"))
199
200 call setcellwidths([[0x81, 0x81, 1], [0xA1, 0xA1, 1],
201 \ [0x2010, 0x2010, 1], [0xFEFF, 0xFEFF, 1]])
202 " Control chars
203 call assert_equal(4, strwidth("\u0081"))
204 call assert_equal(6, strwidth("\uFEFF"))
205 " Ambiguous width chars
206 call assert_equal(1, strwidth("\u00A1"))
207 call assert_equal(1, strwidth("\u2010"))
208
209 call setcellwidths([[0x81, 0x81, 2], [0xA1, 0xA1, 2],
210 \ [0x2010, 0x2010, 2], [0xFEFF, 0xFEFF, 2]])
211 " Control chars
212 call assert_equal(4, strwidth("\u0081"))
213 call assert_equal(6, strwidth("\uFEFF"))
214 " Ambiguous width chars
215 call assert_equal(2, strwidth("\u00A1"))
216 call assert_equal(2, strwidth("\u2010"))
zeertzjq05aacec2024-04-14 18:52:49 +0200217
218 call setcellwidths([])
219 call setline(1, repeat("\u2103", 10))
220 normal! $
221 redraw
222 call assert_equal((aw == 'single') ? 10 : 19, wincol())
223 call setcellwidths([[0x2103, 0x2103, 1]])
224 redraw
225 call assert_equal(10, wincol())
226 call setcellwidths([[0x2103, 0x2103, 2]])
227 redraw
228 call assert_equal(19, wincol())
Ken Takata539e9b52024-09-05 17:19:34 +0200229 call setcellwidths([])
230 redraw
231 call assert_equal((aw == 'single') ? 10 : 19, wincol())
K.Takata71933232023-01-20 16:00:55 +0000232 endfor
233 set ambiwidth& isprint&
234
Bram Moolenaar08aac3c2020-08-28 21:04:24 +0200235 call setcellwidths([])
236
Bram Moolenaard83392a2022-09-01 12:22:46 +0100237 call assert_fails('call setcellwidths(1)', 'E1211:')
Bram Moolenaar08aac3c2020-08-28 21:04:24 +0200238
239 call assert_fails('call setcellwidths([1, 2, 0])', 'E1109:')
240
241 call assert_fails('call setcellwidths([[0x101]])', 'E1110:')
242 call assert_fails('call setcellwidths([[0x101, 0x102]])', 'E1110:')
243 call assert_fails('call setcellwidths([[0x101, 0x102, 1, 4]])', 'E1110:')
244 call assert_fails('call setcellwidths([["a"]])', 'E1110:')
245
246 call assert_fails('call setcellwidths([[0x102, 0x101, 1]])', 'E1111:')
247
248 call assert_fails('call setcellwidths([[0x101, 0x102, 0]])', 'E1112:')
249 call assert_fails('call setcellwidths([[0x101, 0x102, 3]])', 'E1112:')
250
251 call assert_fails('call setcellwidths([[0x111, 0x122, 1], [0x115, 0x116, 2]])', 'E1113:')
252 call assert_fails('call setcellwidths([[0x111, 0x122, 1], [0x122, 0x123, 2]])', 'E1113:')
253
254 call assert_fails('call setcellwidths([[0x33, 0x44, 2]])', 'E1114:')
zeertzjq94358a12021-10-20 11:01:15 +0100255
zeertzjq66f65a42024-09-06 16:24:41 +0200256 set listchars=tab:--\\u2192 fillchars=stl:\\u2501
zeertzjq94358a12021-10-20 11:01:15 +0100257 call assert_fails('call setcellwidths([[0x2192, 0x2192, 2]])', 'E834:')
zeertzjq94358a12021-10-20 11:01:15 +0100258 call assert_fails('call setcellwidths([[0x2501, 0x2501, 2]])', 'E835:')
259
zeertzjq66f65a42024-09-06 16:24:41 +0200260 call setcellwidths([[0x201c, 0x201d, 1]])
261 set listchars& fillchars& ambiwidth=double
262
263 set listchars=nbsp:\\u201c fillchars=vert:\\u201d
264 call assert_fails('call setcellwidths([])', 'E834:')
zeertzjq94358a12021-10-20 11:01:15 +0100265 set listchars&
zeertzjq66f65a42024-09-06 16:24:41 +0200266 call assert_fails('call setcellwidths([])', 'E835:')
zeertzjq94358a12021-10-20 11:01:15 +0100267 set fillchars&
zeertzjq66f65a42024-09-06 16:24:41 +0200268
zeertzjq94358a12021-10-20 11:01:15 +0100269 call setcellwidths([])
zeertzjq66f65a42024-09-06 16:24:41 +0200270 set ambiwidth&
zeertzjq05aacec2024-04-14 18:52:49 +0200271 bwipe!
Bram Moolenaar08aac3c2020-08-28 21:04:24 +0200272endfunc
273
Kota Kato66bb9ae2023-01-17 18:31:56 +0000274func Test_getcellwidths()
275 call setcellwidths([])
276 call assert_equal([], getcellwidths())
277
278 let widthlist = [
279 \ [0x1330, 0x1330, 2],
280 \ [9999, 10000, 1],
281 \ [0x1337, 0x1339, 2],
282 \]
283 let widthlistsorted = [
284 \ [0x1330, 0x1330, 2],
285 \ [0x1337, 0x1339, 2],
286 \ [9999, 10000, 1],
287 \]
288 call setcellwidths(widthlist)
289 call assert_equal(widthlistsorted, getcellwidths())
290
291 call setcellwidths([])
292endfunc
293
Yasuhiro Matsumoto2bc849f2023-01-10 16:03:08 +0000294func Test_setcellwidths_dump()
Drew Vogelea67ba72025-05-07 22:05:17 +0200295 CheckScreendump
Yasuhiro Matsumoto2bc849f2023-01-10 16:03:08 +0000296 CheckRunVimInTerminal
297
298 let lines =<< trim END
299 call setline(1, "\ue5ffDesktop")
300 END
301 call writefile(lines, 'XCellwidths', 'D')
302 let buf = RunVimInTerminal('-S XCellwidths', {'rows': 6})
303 call VerifyScreenDump(buf, 'Test_setcellwidths_dump_1', {})
304
305 call term_sendkeys(buf, ":call setcellwidths([[0xe5ff, 0xe5ff, 2]])\<CR>")
306 call VerifyScreenDump(buf, 'Test_setcellwidths_dump_2', {})
307
308 call StopVimInTerminal(buf)
309endfunc
310
zeertzjq094c4392024-04-18 22:09:37 +0200311" Test setcellwidths() on characters that are not targets of 'ambiwidth'.
mikoto2000e20fa592024-04-17 22:06:54 +0200312func Test_setcellwidths_with_non_ambiwidth_character_dump()
Drew Vogelea67ba72025-05-07 22:05:17 +0200313 CheckScreendump
mikoto2000e20fa592024-04-17 22:06:54 +0200314 CheckRunVimInTerminal
315
316 let lines =<< trim END
317 call setline(1, [repeat("\u279c", 60), repeat("\u279c", 60)])
318 set ambiwidth=single
319 END
320 call writefile(lines, 'XCellwidthsWithNonAmbiwidthCharacter', 'D')
321 let buf = RunVimInTerminal('-S XCellwidthsWithNonAmbiwidthCharacter', {'rows': 6, 'cols': 50})
322 call term_sendkeys(buf, ":call setcellwidths([[0x279c, 0x279c, 1]])\<CR>")
323 call term_sendkeys(buf, ":echo\<CR>")
324 call VerifyScreenDump(buf, 'Test_setcellwidths_with_non_ambiwidth_character_dump_1', {})
325
326 call term_sendkeys(buf, ":call setcellwidths([[0x279c, 0x279c, 2]])\<CR>")
327 call term_sendkeys(buf, ":echo\<CR>")
328 call VerifyScreenDump(buf, 'Test_setcellwidths_with_non_ambiwidth_character_dump_2', {})
329
330 call StopVimInTerminal(buf)
331endfunc
332
zeertzjqa59e0312024-04-15 19:14:38 +0200333" For some reason this test causes Test_customlist_completion() to fail on CI,
334" so run it as the last test.
335func Test_zz_ambiwidth_hl_dump()
Drew Vogelea67ba72025-05-07 22:05:17 +0200336 CheckScreendump
zeertzjqa59e0312024-04-15 19:14:38 +0200337 CheckRunVimInTerminal
338
339 let lines =<< trim END
340 call setline(1, [repeat("\u2103", 60), repeat("\u2103", 60)])
341 set ambiwidth=single cursorline list display=lastline
342 END
343 call writefile(lines, 'XAmbiwidthHl', 'D')
344 let buf = RunVimInTerminal('-S XAmbiwidthHl', {'rows': 6, 'cols': 50})
345 call VerifyScreenDump(buf, 'Test_ambiwidth_hl_dump_1', {})
346
347 call term_sendkeys(buf, ":set ambiwidth=double\<CR>")
348 call term_sendkeys(buf, ":echo\<CR>")
349 call VerifyScreenDump(buf, 'Test_ambiwidth_hl_dump_2', {})
350
351 call term_sendkeys(buf, ":set ambiwidth=single\<CR>")
352 call term_sendkeys(buf, ":echo\<CR>")
353 call VerifyScreenDump(buf, 'Test_ambiwidth_hl_dump_1', {})
354
zeertzjqa59e0312024-04-15 19:14:38 +0200355 call term_sendkeys(buf, ":call setcellwidths([[0x2103, 0x2103, 2]])\<CR>")
356 call term_sendkeys(buf, ":echo\<CR>")
357 call VerifyScreenDump(buf, 'Test_ambiwidth_hl_dump_2', {})
358
359 call term_sendkeys(buf, ":call setcellwidths([[0x2103, 0x2103, 1]])\<CR>")
360 call term_sendkeys(buf, ":echo\<CR>")
361 call VerifyScreenDump(buf, 'Test_ambiwidth_hl_dump_1', {})
zeertzjqa59e0312024-04-15 19:14:38 +0200362
363 call StopVimInTerminal(buf)
364endfunc
365
Bram Moolenaar1cbfc992020-12-02 12:37:37 +0100366func Test_print_overlong()
367 " Text with more composing characters than MB_MAXBYTES.
368 new
369 call setline(1, 'axxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')
370 s/x/\=nr2char(1629)/g
371 print
372 bwipe!
373endfunc
374
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100375" vim: shiftwidth=2 sts=2 expandtab