test_visual.vim 87 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722
  1. " Tests for various Visual modes.
  2. source shared.vim
  3. source check.vim
  4. source screendump.vim
  5. source vim9.vim
  6. func Test_block_shift_multibyte()
  7. " Uses double-wide character.
  8. split
  9. call setline(1, ['xヹxxx', 'ヹxxx'])
  10. exe "normal 1G0l\<C-V>jl>"
  11. call assert_equal('x ヹxxx', getline(1))
  12. call assert_equal(' ヹxxx', getline(2))
  13. q!
  14. endfunc
  15. func Test_block_shift_overflow()
  16. " This used to cause a multiplication overflow followed by a crash.
  17. new
  18. normal ii
  19. exe "normal \<C-V>876543210>"
  20. q!
  21. endfunc
  22. func Test_dotregister_paste()
  23. new
  24. exe "norm! ihello world\<esc>"
  25. norm! 0ve".p
  26. call assert_equal('hello world world', getline(1))
  27. q!
  28. endfunc
  29. func Test_Visual_ctrl_o()
  30. new
  31. call setline(1, ['one', 'two', 'three'])
  32. call cursor(1,2)
  33. set noshowmode
  34. set tw=0
  35. call feedkeys("\<c-v>jjlIa\<c-\>\<c-o>:set tw=88\<cr>\<esc>", 'tx')
  36. call assert_equal(['oane', 'tawo', 'tahree'], getline(1, 3))
  37. call assert_equal(88, &tw)
  38. set tw&
  39. bw!
  40. endfu
  41. func Test_Visual_vapo()
  42. new
  43. normal oxx
  44. normal vapo
  45. bwipe!
  46. endfunc
  47. func Test_Visual_inner_quote()
  48. new
  49. normal oxX
  50. normal vki'
  51. bwipe!
  52. endfunc
  53. " Test for Visual mode not being reset causing E315 error.
  54. func TriggerTheProblem()
  55. " At this point there is no visual selection because :call reset it.
  56. " Let's restore the selection:
  57. normal gv
  58. '<,'>del _
  59. try
  60. exe "normal \<Esc>"
  61. catch /^Vim\%((\a\+)\)\=:E315/
  62. echom 'Snap! E315 error!'
  63. let g:msg = 'Snap! E315 error!'
  64. endtry
  65. endfunc
  66. func Test_visual_mode_reset()
  67. enew
  68. let g:msg = "Everything's fine."
  69. enew
  70. setl buftype=nofile
  71. call append(line('$'), 'Delete this line.')
  72. " NOTE: this has to be done by a call to a function because executing :del
  73. " the ex-way will require the colon operator which resets the visual mode
  74. " thus preventing the problem:
  75. exe "normal! GV:call TriggerTheProblem()\<CR>"
  76. call assert_equal("Everything's fine.", g:msg)
  77. endfunc
  78. " Test for visual block shift and tab characters.
  79. func Test_block_shift_tab()
  80. new
  81. call append(0, repeat(['one two three'], 5))
  82. call cursor(1,1)
  83. exe "normal i\<C-G>u"
  84. exe "normal fe\<C-V>4jR\<Esc>ugvr1"
  85. call assert_equal('on1 two three', getline(1))
  86. call assert_equal('on1 two three', getline(2))
  87. call assert_equal('on1 two three', getline(5))
  88. %d _
  89. call append(0, repeat(['abcdefghijklmnopqrstuvwxyz'], 5))
  90. call cursor(1,1)
  91. exe "normal \<C-V>4jI \<Esc>j<<11|D"
  92. exe "normal j7|a\<Tab>\<Tab>"
  93. exe "normal j7|a\<Tab>\<Tab> "
  94. exe "normal j7|a\<Tab> \<Tab>\<Esc>4k13|\<C-V>4j<"
  95. call assert_equal(' abcdefghijklmnopqrstuvwxyz', getline(1))
  96. call assert_equal('abcdefghij', getline(2))
  97. call assert_equal(" abc\<Tab> defghijklmnopqrstuvwxyz", getline(3))
  98. call assert_equal(" abc\<Tab> defghijklmnopqrstuvwxyz", getline(4))
  99. call assert_equal(" abc\<Tab> defghijklmnopqrstuvwxyz", getline(5))
  100. %s/\s\+//g
  101. call cursor(1,1)
  102. exe "normal \<C-V>4jI \<Esc>j<<"
  103. exe "normal j7|a\<Tab>\<Tab>"
  104. exe "normal j7|a\<Tab>\<Tab>\<Tab>\<Tab>\<Tab>"
  105. exe "normal j7|a\<Tab> \<Tab>\<Tab>\<Esc>4k13|\<C-V>4j3<"
  106. call assert_equal(' abcdefghijklmnopqrstuvwxyz', getline(1))
  107. call assert_equal('abcdefghij', getline(2))
  108. call assert_equal(" abc\<Tab> defghijklmnopqrstuvwxyz", getline(3))
  109. call assert_equal(" abc\<Tab>\<Tab>defghijklmnopqrstuvwxyz", getline(4))
  110. call assert_equal(" abc\<Tab> defghijklmnopqrstuvwxyz", getline(5))
  111. " Test for block shift with space characters at the beginning and with
  112. " 'noexpandtab' and 'expandtab'
  113. %d _
  114. call setline(1, [" 1", " 2", " 3"])
  115. setlocal shiftwidth=2 noexpandtab
  116. exe "normal gg\<C-V>3j>"
  117. call assert_equal(["\t1", "\t2", "\t3"], getline(1, '$'))
  118. %d _
  119. call setline(1, [" 1", " 2", " 3"])
  120. setlocal shiftwidth=2 expandtab
  121. exe "normal gg\<C-V>3j>"
  122. call assert_equal([" 1", " 2", " 3"], getline(1, '$'))
  123. setlocal shiftwidth&
  124. bw!
  125. endfunc
  126. " Tests Blockwise Visual when there are TABs before the text.
  127. func Test_blockwise_visual()
  128. new
  129. call append(0, ['123456',
  130. \ '234567',
  131. \ '345678',
  132. \ '',
  133. \ 'test text test tex start here',
  134. \ "\t\tsome text",
  135. \ "\t\ttest text",
  136. \ 'test text'])
  137. call cursor(1,1)
  138. exe "normal /start here$\<CR>"
  139. exe 'normal "by$' . "\<C-V>jjlld"
  140. exe "normal /456$\<CR>"
  141. exe "normal \<C-V>jj" . '"bP'
  142. call assert_equal(['123start here56',
  143. \ '234start here67',
  144. \ '345start here78',
  145. \ '',
  146. \ 'test text test tex rt here',
  147. \ "\t\tsomext",
  148. \ "\t\ttesext"], getline(1, 7))
  149. bw!
  150. endfunc
  151. " Test swapping corners in blockwise visual mode with o and O
  152. func Test_blockwise_visual_o_O()
  153. new
  154. exe "norm! 10i.\<Esc>Y4P3lj\<C-V>4l2jr "
  155. exe "norm! gvO\<Esc>ra"
  156. exe "norm! gvO\<Esc>rb"
  157. exe "norm! gvo\<C-c>rc"
  158. exe "norm! gvO\<C-c>rd"
  159. set selection=exclusive
  160. exe "norm! gvOo\<C-c>re"
  161. call assert_equal('...a be.', getline(4))
  162. exe "norm! gvOO\<C-c>rf"
  163. set selection&
  164. call assert_equal(['..........',
  165. \ '...c d..',
  166. \ '... ..',
  167. \ '...a bf.',
  168. \ '..........'], getline(1, '$'))
  169. bw!
  170. endfun
  171. " Test Virtual replace mode.
  172. func Test_virtual_replace()
  173. if exists('&t_kD')
  174. let save_t_kD = &t_kD
  175. endif
  176. if exists('&t_kb')
  177. let save_t_kb = &t_kb
  178. endif
  179. exe "set t_kD=\<C-V>x7f t_kb=\<C-V>x08"
  180. enew!
  181. exe "normal a\nabcdefghi\njk\tlmn\n opq rst\n\<C-D>uvwxyz"
  182. call cursor(1,1)
  183. set ai bs=2
  184. exe "normal gR0\<C-D> 1\nA\nBCDEFGHIJ\n\tKL\nMNO\nPQR"
  185. call assert_equal([' 1',
  186. \ ' A',
  187. \ ' BCDEFGHIJ',
  188. \ ' KL',
  189. \ ' MNO',
  190. \ ' PQR',
  191. \ ], getline(1, 6))
  192. normal G
  193. mark a
  194. exe "normal o0\<C-D>\nabcdefghi\njk\tlmn\n opq\trst\n\<C-D>uvwxyz\n"
  195. exe "normal 'ajgR0\<C-D> 1\nA\nBCDEFGHIJ\n\tKL\nMNO\nPQR" . repeat("\<BS>", 29)
  196. call assert_equal([' 1',
  197. \ 'abcdefghi',
  198. \ 'jk lmn',
  199. \ ' opq rst',
  200. \ 'uvwxyz'], getline(7, 11))
  201. normal G
  202. exe "normal iab\tcdefghi\tjkl"
  203. exe "normal 0gRAB......CDEFGHI.J\<Esc>o"
  204. exe "normal iabcdefghijklmnopqrst\<Esc>0gRAB\tIJKLMNO\tQR"
  205. call assert_equal(['AB......CDEFGHI.Jkl',
  206. \ 'AB IJKLMNO QRst'], getline(12, 13))
  207. " Test inserting Tab with 'noexpandtab' and 'softabstop' set to 4
  208. %d
  209. call setline(1, 'aaaaaaaaaaaaa')
  210. set softtabstop=4
  211. exe "normal gggR\<Tab>\<Tab>x"
  212. call assert_equal("\txaaaa", getline(1))
  213. set softtabstop&
  214. enew!
  215. set noai bs&vim
  216. if exists('save_t_kD')
  217. let &t_kD = save_t_kD
  218. endif
  219. if exists('save_t_kb')
  220. let &t_kb = save_t_kb
  221. endif
  222. endfunc
  223. " Test Virtual replace mode.
  224. func Test_virtual_replace2()
  225. enew!
  226. set bs=2
  227. exe "normal a\nabcdefghi\njk\tlmn\n opq rst\n\<C-D>uvwxyz"
  228. call cursor(1,1)
  229. " Test 1: Test that del deletes the newline
  230. exe "normal gR0\<del> 1\nA\nBCDEFGHIJ\n\tKL\nMNO\nPQR"
  231. call assert_equal(['0 1',
  232. \ 'A',
  233. \ 'BCDEFGHIJ',
  234. \ ' KL',
  235. \ 'MNO',
  236. \ 'PQR',
  237. \ ], getline(1, 6))
  238. " Test 2:
  239. " a newline is not deleted, if no newline has been added in virtual replace mode
  240. %d_
  241. call setline(1, ['abcd', 'efgh', 'ijkl'])
  242. call cursor(2,1)
  243. exe "norm! gR1234\<cr>5\<bs>\<bs>\<bs>"
  244. call assert_equal(['abcd',
  245. \ '123h',
  246. \ 'ijkl'], getline(1, '$'))
  247. " Test 3:
  248. " a newline is deleted, if a newline has been inserted before in virtual replace mode
  249. %d_
  250. call setline(1, ['abcd', 'efgh', 'ijkl'])
  251. call cursor(2,1)
  252. exe "norm! gR1234\<cr>\<cr>56\<bs>\<bs>\<bs>"
  253. call assert_equal(['abcd',
  254. \ '1234',
  255. \ 'ijkl'], getline(1, '$'))
  256. " Test 4:
  257. " delete add a newline, delete it, add it again and check undo
  258. %d_
  259. call setline(1, ['abcd', 'efgh', 'ijkl'])
  260. call cursor(2,1)
  261. " break undo sequence explicitly
  262. let &ul = &ul
  263. exe "norm! gR1234\<cr>\<bs>\<del>56\<cr>"
  264. let &ul = &ul
  265. call assert_equal(['abcd',
  266. \ '123456',
  267. \ ''], getline(1, '$'))
  268. norm! u
  269. call assert_equal(['abcd',
  270. \ 'efgh',
  271. \ 'ijkl'], getline(1, '$'))
  272. " Test for truncating spaces in a newly added line using 'autoindent' if
  273. " characters are not added to that line.
  274. %d_
  275. call setline(1, [' app', ' bee', ' cat'])
  276. setlocal autoindent
  277. exe "normal gg$gRt\n\nr"
  278. call assert_equal([' apt', '', ' rat'], getline(1, '$'))
  279. " clean up
  280. %d_
  281. set bs&vim
  282. endfunc
  283. func Test_Visual_word_textobject()
  284. new
  285. call setline(1, ['First sentence. Second sentence.'])
  286. " When start and end of visual area are identical, 'aw' or 'iw' select
  287. " the whole word.
  288. norm! 1go2fcvawy
  289. call assert_equal('Second ', @")
  290. norm! 1go2fcviwy
  291. call assert_equal('Second', @")
  292. " When start and end of visual area are not identical, 'aw' or 'iw'
  293. " extend the word in direction of the end of the visual area.
  294. norm! 1go2fcvlawy
  295. call assert_equal('cond ', @")
  296. norm! gv2awy
  297. call assert_equal('cond sentence.', @")
  298. norm! 1go2fcvliwy
  299. call assert_equal('cond', @")
  300. norm! gv2iwy
  301. call assert_equal('cond sentence', @")
  302. " Extend visual area in opposite direction.
  303. norm! 1go2fcvhawy
  304. call assert_equal(' Sec', @")
  305. norm! gv2awy
  306. call assert_equal(' sentence. Sec', @")
  307. norm! 1go2fcvhiwy
  308. call assert_equal('Sec', @")
  309. norm! gv2iwy
  310. call assert_equal('. Sec', @")
  311. bwipe!
  312. endfunc
  313. func Test_Visual_sentence_textobject()
  314. new
  315. call setline(1, ['First sentence. Second sentence. Third', 'sentence. Fourth sentence'])
  316. " When start and end of visual area are identical, 'as' or 'is' select
  317. " the whole sentence.
  318. norm! 1gofdvasy
  319. call assert_equal('Second sentence. ', @")
  320. norm! 1gofdvisy
  321. call assert_equal('Second sentence.', @")
  322. " When start and end of visual area are not identical, 'as' or 'is'
  323. " extend the sentence in direction of the end of the visual area.
  324. norm! 1gofdvlasy
  325. call assert_equal('d sentence. ', @")
  326. norm! gvasy
  327. call assert_equal("d sentence. Third\nsentence. ", @")
  328. norm! 1gofdvlisy
  329. call assert_equal('d sentence.', @")
  330. norm! gvisy
  331. call assert_equal('d sentence. ', @")
  332. norm! gvisy
  333. call assert_equal("d sentence. Third\nsentence.", @")
  334. " Extend visual area in opposite direction.
  335. norm! 1gofdvhasy
  336. call assert_equal(' Second', @")
  337. norm! gvasy
  338. call assert_equal("First sentence. Second", @")
  339. norm! 1gofdvhisy
  340. call assert_equal('Second', @")
  341. norm! gvisy
  342. call assert_equal(' Second', @")
  343. norm! gvisy
  344. call assert_equal('First sentence. Second', @")
  345. bwipe!
  346. endfunc
  347. func Test_Visual_paragraph_textobject()
  348. new
  349. let lines =<< trim [END]
  350. First line.
  351. Second line.
  352. Third line.
  353. Fourth line.
  354. Fifth line.
  355. Sixth line.
  356. [END]
  357. call setline(1, lines)
  358. " When start and end of visual area are identical, 'ap' or 'ip' select
  359. " the whole paragraph.
  360. norm! 4ggvapy
  361. call assert_equal("Second line.\nThird line.\nFourth line.\nFifth line.\n\n", @")
  362. norm! 4ggvipy
  363. call assert_equal("Second line.\nThird line.\nFourth line.\nFifth line.\n", @")
  364. " When start and end of visual area are not identical, 'ap' or 'ip'
  365. " extend the sentence in direction of the end of the visual area.
  366. " FIXME: actually, it is not sufficient to have different start and
  367. " end of visual selection, the start line and end line have to differ,
  368. " which is not consistent with the documentation.
  369. norm! 4ggVjapy
  370. call assert_equal("Third line.\nFourth line.\nFifth line.\n\n", @")
  371. norm! gvapy
  372. call assert_equal("Third line.\nFourth line.\nFifth line.\n\nSixth line.\n", @")
  373. norm! 4ggVjipy
  374. call assert_equal("Third line.\nFourth line.\nFifth line.\n", @")
  375. norm! gvipy
  376. call assert_equal("Third line.\nFourth line.\nFifth line.\n\n", @")
  377. norm! gvipy
  378. call assert_equal("Third line.\nFourth line.\nFifth line.\n\nSixth line.\n", @")
  379. " Extend visual area in opposite direction.
  380. norm! 5ggVkapy
  381. call assert_equal("\nSecond line.\nThird line.\nFourth line.\n", @")
  382. norm! gvapy
  383. call assert_equal("First line.\n\nSecond line.\nThird line.\nFourth line.\n", @")
  384. norm! 5ggVkipy
  385. call assert_equal("Second line.\nThird line.\nFourth line.\n", @")
  386. norma gvipy
  387. call assert_equal("\nSecond line.\nThird line.\nFourth line.\n", @")
  388. norm! gvipy
  389. call assert_equal("First line.\n\nSecond line.\nThird line.\nFourth line.\n", @")
  390. bwipe!
  391. endfunc
  392. func Test_curswant_not_changed()
  393. new
  394. call setline(1, ['one', 'two'])
  395. au InsertLeave * call getcurpos()
  396. call feedkeys("gg0\<C-V>jI123 \<Esc>j", 'xt')
  397. call assert_equal([0, 2, 1, 0, 1], getcurpos())
  398. bwipe!
  399. au! InsertLeave
  400. endfunc
  401. " Tests for "vaBiB", end could be wrong.
  402. func Test_Visual_Block()
  403. new
  404. a
  405. - Bug in "vPPPP" on this text:
  406. {
  407. cmd;
  408. {
  409. cmd;\t/* <-- Start cursor here */
  410. {
  411. }
  412. }
  413. }
  414. .
  415. normal gg
  416. call search('Start cursor here')
  417. normal vaBiBD
  418. call assert_equal(['- Bug in "vPPPP" on this text:',
  419. \ "\t{",
  420. \ "\t}"], getline(1, '$'))
  421. close!
  422. endfunc
  423. " Test for 'p'ut in visual block mode
  424. func Test_visual_block_put()
  425. new
  426. call append(0, ['One', 'Two', 'Three'])
  427. normal gg
  428. yank
  429. call feedkeys("jl\<C-V>ljp", 'xt')
  430. call assert_equal(['One', 'T', 'Tee', 'One', ''], getline(1, '$'))
  431. bw!
  432. endfunc
  433. func Test_visual_block_put_invalid()
  434. enew!
  435. " behave mswin
  436. set selection=exclusive
  437. norm yy
  438. norm v)Ps/^/
  439. " this was causing the column to become negative
  440. silent norm ggv)P
  441. bwipe!
  442. " behave xterm
  443. set selection&
  444. endfunc
  445. " Visual modes (v V CTRL-V) followed by an operator; count; repeating
  446. func Test_visual_mode_op()
  447. new
  448. call append(0, '')
  449. call setline(1, 'apple banana cherry')
  450. call cursor(1, 1)
  451. normal lvld.l3vd.
  452. call assert_equal('a y', getline(1))
  453. call setline(1, ['line 1 line 1', 'line 2 line 2', 'line 3 line 3',
  454. \ 'line 4 line 4', 'line 5 line 5', 'line 6 line 6'])
  455. call cursor(1, 1)
  456. exe "normal Vcnewline\<Esc>j.j2Vd."
  457. call assert_equal(['newline', 'newline'], getline(1, '$'))
  458. call deletebufline('', 1, '$')
  459. call setline(1, ['xxxxxxxxxxxxx', 'xxxxxxxxxxxxx', 'xxxxxxxxxxxxx',
  460. \ 'xxxxxxxxxxxxx'])
  461. exe "normal \<C-V>jlc \<Esc>l.l2\<C-V>c----\<Esc>l."
  462. call assert_equal([' --------x',
  463. \ ' --------x',
  464. \ 'xxxx--------x',
  465. \ 'xxxx--------x'], getline(1, '$'))
  466. bwipe!
  467. endfunc
  468. " Visual mode maps (movement and text object)
  469. " Visual mode maps; count; repeating
  470. " - Simple
  471. " - With an Ex command (custom text object)
  472. func Test_visual_mode_maps()
  473. new
  474. call append(0, '')
  475. func SelectInCaps()
  476. let [line1, col1] = searchpos('\u', 'bcnW')
  477. let [line2, col2] = searchpos('.\u', 'nW')
  478. call setpos("'<", [0, line1, col1, 0])
  479. call setpos("'>", [0, line2, col2, 0])
  480. normal! gv
  481. endfunction
  482. vnoremap W /\u/s-1<CR>
  483. vnoremap iW :<C-U>call SelectInCaps()<CR>
  484. call setline(1, 'KiwiRaspberryDateWatermelonPeach')
  485. call cursor(1, 1)
  486. exe "normal vWcNo\<Esc>l.fD2vd."
  487. call assert_equal('NoNoberryach', getline(1))
  488. call setline(1, 'JambuRambutanBananaTangerineMango')
  489. call cursor(1, 1)
  490. exe "normal llviWc-\<Esc>l.l2vdl."
  491. call assert_equal('--ago', getline(1))
  492. vunmap W
  493. vunmap iW
  494. bwipe!
  495. delfunc SelectInCaps
  496. endfunc
  497. " Operator-pending mode maps (movement and text object)
  498. " - Simple
  499. " - With Ex command moving the cursor
  500. " - With Ex command and Visual selection (custom text object)
  501. func Test_visual_oper_pending_mode_maps()
  502. new
  503. call append(0, '')
  504. func MoveToCap()
  505. call search('\u', 'W')
  506. endfunction
  507. func SelectInCaps()
  508. let [line1, col1] = searchpos('\u', 'bcnW')
  509. let [line2, col2] = searchpos('.\u', 'nW')
  510. call setpos("'<", [0, line1, col1, 0])
  511. call setpos("'>", [0, line2, col2, 0])
  512. normal! gv
  513. endfunction
  514. onoremap W /\u/<CR>
  515. onoremap <Leader>W :<C-U>call MoveToCap()<CR>
  516. onoremap iW :<C-U>call SelectInCaps()<CR>
  517. call setline(1, 'PineappleQuinceLoganberryOrangeGrapefruitKiwiZ')
  518. call cursor(1, 1)
  519. exe "normal cW-\<Esc>l.l2.l."
  520. call assert_equal('----Z', getline(1))
  521. call setline(1, 'JuniperDurianZ')
  522. call cursor(1, 1)
  523. exe "normal g?\WfD."
  524. call assert_equal('WhavcreQhevnaZ', getline(1))
  525. call setline(1, 'LemonNectarineZ')
  526. call cursor(1, 1)
  527. exe "normal yiWPlciWNew\<Esc>fr."
  528. call assert_equal('LemonNewNewZ', getline(1))
  529. ounmap W
  530. ounmap <Leader>W
  531. ounmap iW
  532. bwipe!
  533. delfunc MoveToCap
  534. delfunc SelectInCaps
  535. endfunc
  536. " Patch 7.3.879: Properly abort Operator-pending mode for "dv:<Esc>" etc.
  537. func Test_op_pend_mode_abort()
  538. new
  539. call append(0, '')
  540. call setline(1, ['zzzz', 'zzzz'])
  541. call cursor(1, 1)
  542. exe "normal dV:\<CR>dv:\<CR>"
  543. call assert_equal(['zzz'], getline(1, 2))
  544. set nomodifiable
  545. call assert_fails('exe "normal d:\<CR>"', 'E21:')
  546. set modifiable
  547. call feedkeys("dv:\<Esc>dV:\<Esc>", 'xt')
  548. call assert_equal(['zzz'], getline(1, 2))
  549. set nomodifiable
  550. let v:errmsg = ''
  551. call feedkeys("d:\<Esc>", 'xt')
  552. call assert_true(v:errmsg !~# '^E21:')
  553. set modifiable
  554. bwipe!
  555. endfunc
  556. func Test_characterwise_visual_mode()
  557. new
  558. " characterwise visual mode: replace last line
  559. $put ='a'
  560. let @" = 'x'
  561. normal v$p
  562. call assert_equal('x', getline('$'))
  563. " characterwise visual mode: delete middle line
  564. call deletebufline('', 1, '$')
  565. call append('$', ['a', 'b', 'c'])
  566. normal G
  567. normal kkv$d
  568. call assert_equal(['', 'b', 'c'], getline(1, '$'))
  569. " characterwise visual mode: delete middle two lines
  570. call deletebufline('', 1, '$')
  571. call append('$', ['a', 'b', 'c'])
  572. normal Gkkvj$d
  573. call assert_equal(['', 'c'], getline(1, '$'))
  574. " characterwise visual mode: delete last line
  575. call deletebufline('', 1, '$')
  576. call append('$', ['a', 'b', 'c'])
  577. normal Gv$d
  578. call assert_equal(['', 'a', 'b', ''], getline(1, '$'))
  579. " characterwise visual mode: delete last two lines
  580. call deletebufline('', 1, '$')
  581. call append('$', ['a', 'b', 'c'])
  582. normal Gkvj$d
  583. call assert_equal(['', 'a', ''], getline(1, '$'))
  584. " characterwise visual mode: use a count with the visual mode from the last
  585. " line in the buffer
  586. %d _
  587. call setline(1, ['one', 'two', 'three', 'four'])
  588. norm! vj$y
  589. norm! G1vy
  590. call assert_equal('four', @")
  591. " characterwise visual mode: replace a single character line and the eol
  592. %d _
  593. call setline(1, "a")
  594. normal v$rx
  595. call assert_equal(['x'], getline(1, '$'))
  596. " replace a character with composing characters
  597. call setline(1, "xã̳x")
  598. normal gg0lvrb
  599. call assert_equal("xbx", getline(1))
  600. bwipe!
  601. endfunc
  602. func Test_visual_mode_put()
  603. new
  604. " v_p: replace last character with line register at middle line
  605. call append('$', ['aaa', 'bbb', 'ccc'])
  606. normal G
  607. -2yank
  608. normal k$vp
  609. call assert_equal(['', 'aaa', 'bb', 'aaa', '', 'ccc'], getline(1, '$'))
  610. " v_p: replace last character with line register at middle line selecting
  611. " newline
  612. call deletebufline('', 1, '$')
  613. call append('$', ['aaa', 'bbb', 'ccc'])
  614. normal G
  615. -2yank
  616. normal k$v$p
  617. call assert_equal(['', 'aaa', 'bb', 'aaa', 'ccc'], getline(1, '$'))
  618. " v_p: replace last character with line register at last line
  619. call deletebufline('', 1, '$')
  620. call append('$', ['aaa', 'bbb', 'ccc'])
  621. normal G
  622. -2yank
  623. normal $vp
  624. call assert_equal(['', 'aaa', 'bbb', 'cc', 'aaa', ''], getline(1, '$'))
  625. " v_p: replace last character with line register at last line selecting
  626. " newline
  627. call deletebufline('', 1, '$')
  628. call append('$', ['aaa', 'bbb', 'ccc'])
  629. normal G
  630. -2yank
  631. normal $v$p
  632. call assert_equal(['', 'aaa', 'bbb', 'cc', 'aaa', ''], getline(1, '$'))
  633. bwipe!
  634. endfunc
  635. func Test_gv_with_exclusive_selection()
  636. new
  637. " gv with exclusive selection after an operation
  638. call append('$', ['zzz ', 'äà '])
  639. set selection=exclusive
  640. normal Gkv3lyjv3lpgvcxxx
  641. call assert_equal(['', 'zzz ', 'xxx '], getline(1, '$'))
  642. " gv with exclusive selection without an operation
  643. call deletebufline('', 1, '$')
  644. call append('$', 'zzz ')
  645. set selection=exclusive
  646. exe "normal G0v3l\<Esc>gvcxxx"
  647. call assert_equal(['', 'xxx '], getline(1, '$'))
  648. set selection&vim
  649. bwipe!
  650. endfunc
  651. " Tests for the visual block mode commands
  652. func Test_visual_block_mode()
  653. new
  654. call append(0, '')
  655. call setline(1, repeat(['abcdefghijklm'], 5))
  656. call cursor(1, 1)
  657. " Test shift-right of a block
  658. exe "normal jllll\<C-V>jj>wll\<C-V>jlll>"
  659. " Test shift-left of a block
  660. exe "normal G$hhhh\<C-V>kk<"
  661. " Test block-insert
  662. exe "normal Gkl\<C-V>kkkIxyz"
  663. " Test block-replace
  664. exe "normal Gllll\<C-V>kkklllrq"
  665. " Test block-change
  666. exe "normal G$khhh\<C-V>hhkkcmno"
  667. call assert_equal(['axyzbcdefghijklm',
  668. \ 'axyzqqqq mno ghijklm',
  669. \ 'axyzqqqqef mno ghijklm',
  670. \ 'axyzqqqqefgmnoklm',
  671. \ 'abcdqqqqijklm'], getline(1, 5))
  672. " Test 'C' to change till the end of the line
  673. call cursor(3, 4)
  674. exe "normal! \<C-V>j3lCooo"
  675. call assert_equal(['axyooo', 'axyooo'], getline(3, 4))
  676. " Test 'D' to delete till the end of the line
  677. call cursor(3, 3)
  678. exe "normal! \<C-V>j2lD"
  679. call assert_equal(['ax', 'ax'], getline(3, 4))
  680. " Test block insert with a short line that ends before the block
  681. %d _
  682. call setline(1, [" one", "a", " two"])
  683. exe "normal gg\<C-V>2jIx"
  684. call assert_equal([" xone", "a", " xtwo"], getline(1, '$'))
  685. " Test block append at EOL with '$' and without '$'
  686. %d _
  687. call setline(1, ["one", "a", "two"])
  688. exe "normal gg$\<C-V>2jAx"
  689. call assert_equal(["onex", "ax", "twox"], getline(1, '$'))
  690. %d _
  691. call setline(1, ["one", "a", "two"])
  692. exe "normal gg3l\<C-V>2jAx"
  693. call assert_equal(["onex", "a x", "twox"], getline(1, '$'))
  694. " Test block replace with an empty line in the middle and use $ to jump to
  695. " the end of the line.
  696. %d _
  697. call setline(1, ['one', '', 'two'])
  698. exe "normal gg$\<C-V>2jrx"
  699. call assert_equal(["onx", "", "twx"], getline(1, '$'))
  700. " Test block replace with an empty line in the middle and move cursor to the
  701. " end of the line
  702. %d _
  703. call setline(1, ['one', '', 'two'])
  704. exe "normal gg2l\<C-V>2jrx"
  705. call assert_equal(["onx", "", "twx"], getline(1, '$'))
  706. " Replace odd number of characters with a multibyte character
  707. %d _
  708. call setline(1, ['abcd', 'efgh'])
  709. exe "normal ggl\<C-V>2ljr\u1100"
  710. call assert_equal(["a\u1100 ", "e\u1100 "], getline(1, '$'))
  711. " During visual block append, if the cursor moved outside of the selected
  712. " range, then the edit should not be applied to the block.
  713. %d _
  714. call setline(1, ['aaa', 'bbb', 'ccc'])
  715. exe "normal 2G\<C-V>jAx\<Up>"
  716. call assert_equal(['aaa', 'bxbb', 'ccc'], getline(1, '$'))
  717. " During visual block append, if the cursor is moved before the start of the
  718. " block, then the new text should be appended there.
  719. %d _
  720. call setline(1, ['aaa', 'bbb', 'ccc'])
  721. exe "normal $\<C-V>2jA\<Left>x"
  722. call assert_equal(['aaxa', 'bbxb', 'ccxc'], getline(1, '$'))
  723. " Repeat the previous test but use 'l' to move the cursor instead of '$'
  724. call setline(1, ['aaa', 'bbb', 'ccc'])
  725. exe "normal! gg2l\<C-V>2jA\<Left>x"
  726. call assert_equal(['aaxa', 'bbxb', 'ccxc'], getline(1, '$'))
  727. " Change a characterwise motion to a blockwise motion using CTRL-V
  728. %d _
  729. call setline(1, ['123', '456', '789'])
  730. exe "normal ld\<C-V>j"
  731. call assert_equal(['13', '46', '789'], getline(1, '$'))
  732. " Test from ':help v_b_I_example'
  733. %d _
  734. setlocal tabstop=8 shiftwidth=4
  735. let lines =<< trim END
  736. abcdefghijklmnopqrstuvwxyz
  737. abc defghijklmnopqrstuvwxyz
  738. abcdef ghi jklmnopqrstuvwxyz
  739. abcdefghijklmnopqrstuvwxyz
  740. END
  741. call setline(1, lines)
  742. exe "normal ggfo\<C-V>3jISTRING"
  743. let expected =<< trim END
  744. abcdefghijklmnSTRINGopqrstuvwxyz
  745. abc STRING defghijklmnopqrstuvwxyz
  746. abcdef ghi STRING jklmnopqrstuvwxyz
  747. abcdefghijklmnSTRINGopqrstuvwxyz
  748. END
  749. call assert_equal(expected, getline(1, '$'))
  750. " Test from ':help v_b_A_example'
  751. %d _
  752. let lines =<< trim END
  753. abcdefghijklmnopqrstuvwxyz
  754. abc defghijklmnopqrstuvwxyz
  755. abcdef ghi jklmnopqrstuvwxyz
  756. abcdefghijklmnopqrstuvwxyz
  757. END
  758. call setline(1, lines)
  759. exe "normal ggfo\<C-V>3j$ASTRING"
  760. let expected =<< trim END
  761. abcdefghijklmnopqrstuvwxyzSTRING
  762. abc defghijklmnopqrstuvwxyzSTRING
  763. abcdef ghi jklmnopqrstuvwxyzSTRING
  764. abcdefghijklmnopqrstuvwxyzSTRING
  765. END
  766. call assert_equal(expected, getline(1, '$'))
  767. " Test from ':help v_b_<_example'
  768. %d _
  769. let lines =<< trim END
  770. abcdefghijklmnopqrstuvwxyz
  771. abc defghijklmnopqrstuvwxyz
  772. abcdef ghi jklmnopqrstuvwxyz
  773. abcdefghijklmnopqrstuvwxyz
  774. END
  775. call setline(1, lines)
  776. exe "normal ggfo\<C-V>3j3l<.."
  777. let expected =<< trim END
  778. abcdefghijklmnopqrstuvwxyz
  779. abc defghijklmnopqrstuvwxyz
  780. abcdef ghi jklmnopqrstuvwxyz
  781. abcdefghijklmnopqrstuvwxyz
  782. END
  783. call assert_equal(expected, getline(1, '$'))
  784. " Test from ':help v_b_>_example'
  785. %d _
  786. let lines =<< trim END
  787. abcdefghijklmnopqrstuvwxyz
  788. abc defghijklmnopqrstuvwxyz
  789. abcdef ghi jklmnopqrstuvwxyz
  790. abcdefghijklmnopqrstuvwxyz
  791. END
  792. call setline(1, lines)
  793. exe "normal ggfo\<C-V>3j>.."
  794. let expected =<< trim END
  795. abcdefghijklmn opqrstuvwxyz
  796. abc defghijklmnopqrstuvwxyz
  797. abcdef ghi jklmnopqrstuvwxyz
  798. abcdefghijklmn opqrstuvwxyz
  799. END
  800. call assert_equal(expected, getline(1, '$'))
  801. " Test from ':help v_b_r_example'
  802. %d _
  803. let lines =<< trim END
  804. abcdefghijklmnopqrstuvwxyz
  805. abc defghijklmnopqrstuvwxyz
  806. abcdef ghi jklmnopqrstuvwxyz
  807. abcdefghijklmnopqrstuvwxyz
  808. END
  809. call setline(1, lines)
  810. exe "normal ggfo\<C-V>5l3jrX"
  811. let expected =<< trim END
  812. abcdefghijklmnXXXXXXuvwxyz
  813. abc XXXXXXhijklmnopqrstuvwxyz
  814. abcdef ghi XXXXXX jklmnopqrstuvwxyz
  815. abcdefghijklmnXXXXXXuvwxyz
  816. END
  817. call assert_equal(expected, getline(1, '$'))
  818. bwipe!
  819. set tabstop& shiftwidth&
  820. endfunc
  821. func Test_visual_force_motion_feedkeys()
  822. onoremap <expr> i- execute('let g:mode = mode(1)')->slice(0, 0)
  823. call feedkeys('dvi-', 'x')
  824. call assert_equal('nov', g:mode)
  825. call feedkeys('di-', 'x')
  826. call assert_equal('no', g:mode)
  827. ounmap i-
  828. endfunc
  829. " Test block-insert using cursor keys for movement
  830. func Test_visual_block_insert_cursor_keys()
  831. new
  832. call append(0, ['aaaaaa', 'bbbbbb', 'cccccc', 'dddddd'])
  833. call cursor(1, 1)
  834. exe "norm! l\<C-V>jjjlllI\<Right>\<Right> \<Esc>"
  835. call assert_equal(['aaa aaa', 'bbb bbb', 'ccc ccc', 'ddd ddd'],
  836. \ getline(1, 4))
  837. call deletebufline('', 1, '$')
  838. call setline(1, ['xaaa', 'bbbb', 'cccc', 'dddd'])
  839. call cursor(1, 1)
  840. exe "norm! \<C-V>jjjI<>\<Left>p\<Esc>"
  841. call assert_equal(['<p>xaaa', '<p>bbbb', '<p>cccc', '<p>dddd'],
  842. \ getline(1, 4))
  843. bwipe!
  844. endfunc
  845. func Test_visual_block_create()
  846. new
  847. call append(0, '')
  848. " Test for Visual block was created with the last <C-v>$
  849. call setline(1, ['A23', '4567'])
  850. call cursor(1, 1)
  851. exe "norm! l\<C-V>j$Aab\<Esc>"
  852. call assert_equal(['A23ab', '4567ab'], getline(1, 2))
  853. " Test for Visual block was created with the middle <C-v>$ (1)
  854. call deletebufline('', 1, '$')
  855. call setline(1, ['B23', '4567'])
  856. call cursor(1, 1)
  857. exe "norm! l\<C-V>j$hAab\<Esc>"
  858. call assert_equal(['B23 ab', '4567ab'], getline(1, 2))
  859. " Test for Visual block was created with the middle <C-v>$ (2)
  860. call deletebufline('', 1, '$')
  861. call setline(1, ['C23', '4567'])
  862. call cursor(1, 1)
  863. exe "norm! l\<C-V>j$hhAab\<Esc>"
  864. call assert_equal(['C23ab', '456ab7'], getline(1, 2))
  865. bwipe!
  866. endfunc
  867. " Test for Visual block insert when virtualedit=all
  868. func Test_virtualedit_visual_block()
  869. set ve=all
  870. new
  871. call append(0, ["\t\tline1", "\t\tline2", "\t\tline3"])
  872. call cursor(1, 1)
  873. exe "norm! 07l\<C-V>jjIx\<Esc>"
  874. call assert_equal([" x \tline1",
  875. \ " x \tline2",
  876. \ " x \tline3"], getline(1, 3))
  877. " Test for Visual block append when virtualedit=all
  878. exe "norm! 012l\<C-v>jjAx\<Esc>"
  879. call assert_equal([' x x line1',
  880. \ ' x x line2',
  881. \ ' x x line3'], getline(1, 3))
  882. set ve=
  883. bwipe!
  884. endfunc
  885. " Test for changing case
  886. func Test_visual_change_case()
  887. new
  888. " gUe must uppercase a whole word, also when ß changes to ẞ
  889. exe "normal Gothe youtußeuu end\<Esc>Ypk0wgUe\r"
  890. " gUfx must uppercase until x, inclusive.
  891. exe "normal O- youßtußexu -\<Esc>0fogUfx\r"
  892. " VU must uppercase a whole line
  893. exe "normal YpkVU\r"
  894. " same, when it's the last line in the buffer
  895. exe "normal YPGi111\<Esc>VUddP\r"
  896. " Uppercase two lines
  897. exe "normal Oblah di\rdoh dut\<Esc>VkUj\r"
  898. " Uppercase part of two lines
  899. exe "normal ddppi333\<Esc>k0i222\<Esc>fyllvjfuUk"
  900. call assert_equal(['the YOUTUẞEUU end', '- yOUẞTUẞEXu -',
  901. \ 'THE YOUTUẞEUU END', '111THE YOUTUẞEUU END', 'BLAH DI', 'DOH DUT',
  902. \ '222the yoUTUẞEUU END', '333THE YOUTUßeuu end'], getline(2, '$'))
  903. bwipe!
  904. endfunc
  905. " Test for Visual replace using Enter or NL
  906. func Test_visual_replace_crnl()
  907. new
  908. exe "normal G3o123456789\e2k05l\<C-V>2jr\r"
  909. exe "normal G3o98765\e2k02l\<C-V>2jr\<C-V>\r\n"
  910. exe "normal G3o123456789\e2k05l\<C-V>2jr\n"
  911. exe "normal G3o98765\e2k02l\<C-V>2jr\<C-V>\n"
  912. call assert_equal(['12345', '789', '12345', '789', '12345', '789', "98\r65",
  913. \ "98\r65", "98\r65", '12345', '789', '12345', '789', '12345', '789',
  914. \ "98\n65", "98\n65", "98\n65"], getline(2, '$'))
  915. bwipe!
  916. endfunc
  917. func Test_ve_block_curpos()
  918. new
  919. " Test cursor position. When ve=block and Visual block mode and $gj
  920. call append(0, ['12345', '789'])
  921. call cursor(1, 3)
  922. set virtualedit=block
  923. exe "norm! \<C-V>$gj\<Esc>"
  924. call assert_equal([0, 2, 4, 0], getpos("'>"))
  925. set virtualedit=
  926. bwipe!
  927. endfunc
  928. " Test for block_insert when replacing spaces in front of the a with tabs
  929. func Test_block_insert_replace_tabs()
  930. new
  931. set ts=8 sts=4 sw=4
  932. call append(0, ["#define BO_ALL\t 0x0001",
  933. \ "#define BO_BS\t 0x0002",
  934. \ "#define BO_CRSR\t 0x0004"])
  935. call cursor(1, 1)
  936. exe "norm! f0\<C-V>2jI\<tab>\<esc>"
  937. call assert_equal([
  938. \ "#define BO_ALL\t\t0x0001",
  939. \ "#define BO_BS\t \t0x0002",
  940. \ "#define BO_CRSR\t \t0x0004", ''], getline(1, '$'))
  941. set ts& sts& sw&
  942. bwipe!
  943. endfunc
  944. " Test for * register in :
  945. func Test_star_register()
  946. call assert_fails('*bfirst', 'E16:')
  947. new
  948. call setline(1, ['foo', 'bar', 'baz', 'qux'])
  949. exe "normal jVj\<ESC>"
  950. *yank r
  951. call assert_equal("bar\nbaz\n", @r)
  952. delmarks < >
  953. call assert_fails('*yank', 'E20:')
  954. close!
  955. endfunc
  956. " Test for changing text in visual mode with 'exclusive' selection
  957. func Test_exclusive_selection()
  958. new
  959. call setline(1, ['one', 'two'])
  960. set selection=exclusive
  961. call feedkeys("vwcabc", 'xt')
  962. call assert_equal('abctwo', getline(1))
  963. call setline(1, ["\tone"])
  964. set virtualedit=all
  965. call feedkeys('0v2lcl', 'xt')
  966. call assert_equal('l one', getline(1))
  967. set virtualedit&
  968. set selection&
  969. close!
  970. endfunc
  971. " Test for starting linewise visual with a count.
  972. " This test needs to be run without any previous visual mode. Otherwise the
  973. " count will use the count from the previous visual mode.
  974. func Test_linewise_visual_with_count()
  975. let after =<< trim [CODE]
  976. call setline(1, ['one', 'two', 'three', 'four'])
  977. norm! 3Vy
  978. call assert_equal("one\ntwo\nthree\n", @")
  979. call writefile(v:errors, 'Xtestout')
  980. qall!
  981. [CODE]
  982. if RunVim([], after, '')
  983. call assert_equal([], readfile('Xtestout'))
  984. call delete('Xtestout')
  985. endif
  986. endfunc
  987. " Test for starting characterwise visual with a count.
  988. " This test needs to be run without any previous visual mode. Otherwise the
  989. " count will use the count from the previous visual mode.
  990. func Test_characterwise_visual_with_count()
  991. let after =<< trim [CODE]
  992. call setline(1, ['one two', 'three'])
  993. norm! l5vy
  994. call assert_equal("ne tw", @")
  995. call writefile(v:errors, 'Xtestout')
  996. qall!
  997. [CODE]
  998. if RunVim([], after, '')
  999. call assert_equal([], readfile('Xtestout'))
  1000. call delete('Xtestout')
  1001. endif
  1002. endfunc
  1003. " Test for visually selecting an inner block (iB)
  1004. func Test_visual_inner_block()
  1005. new
  1006. call setline(1, ['one', '{', 'two', '{', 'three', '}', 'four', '}', 'five'])
  1007. call cursor(5, 1)
  1008. " visually select all the lines in the block and then execute iB
  1009. call feedkeys("ViB\<C-C>", 'xt')
  1010. call assert_equal([0, 5, 1, 0], getpos("'<"))
  1011. call assert_equal([0, 5, 6, 0], getpos("'>"))
  1012. " visually select two inner blocks
  1013. call feedkeys("ViBiB\<C-C>", 'xt')
  1014. call assert_equal([0, 3, 1, 0], getpos("'<"))
  1015. call assert_equal([0, 7, 5, 0], getpos("'>"))
  1016. " try to select non-existing inner block
  1017. call cursor(5, 1)
  1018. call assert_beeps('normal ViBiBiB')
  1019. " try to select an unclosed inner block
  1020. 8,9d
  1021. call cursor(5, 1)
  1022. call assert_beeps('normal ViBiB')
  1023. close!
  1024. endfunc
  1025. func Test_visual_put_in_block()
  1026. new
  1027. call setline(1, ['xxxx', 'y∞yy', 'zzzz'])
  1028. normal 1G2yl
  1029. exe "normal 1G2l\<C-V>jjlp"
  1030. call assert_equal(['xxxx', 'y∞xx', 'zzxx'], getline(1, 3))
  1031. bwipe!
  1032. endfunc
  1033. func Test_visual_put_in_block_using_zp()
  1034. new
  1035. " paste using zP
  1036. call setline(1, ['/path;text', '/path;text', '/path;text', '',
  1037. \ '/subdir',
  1038. \ '/longsubdir',
  1039. \ '/longlongsubdir'])
  1040. exe "normal! 5G\<c-v>2j$y"
  1041. norm! 1Gf;zP
  1042. call assert_equal(['/path/subdir;text', '/path/longsubdir;text', '/path/longlongsubdir;text'], getline(1, 3))
  1043. %d
  1044. " paste using zP
  1045. call setline(1, ['/path;text', '/path;text', '/path;text', '',
  1046. \ '/subdir',
  1047. \ '/longsubdir',
  1048. \ '/longlongsubdir'])
  1049. exe "normal! 5G\<c-v>2j$y"
  1050. norm! 1Gf;hzp
  1051. call assert_equal(['/path/subdir;text', '/path/longsubdir;text', '/path/longlongsubdir;text'], getline(1, 3))
  1052. bwipe!
  1053. endfunc
  1054. func Test_visual_put_in_block_using_zy_and_zp()
  1055. new
  1056. " Test 1) Paste using zp - after the cursor without trailing spaces
  1057. call setline(1, ['/path;text', '/path;text', '/path;text', '',
  1058. \ 'texttext /subdir columntext',
  1059. \ 'texttext /longsubdir columntext',
  1060. \ 'texttext /longlongsubdir columntext'])
  1061. exe "normal! 5G0f/\<c-v>2jezy"
  1062. norm! 1G0f;hzp
  1063. call assert_equal(['/path/subdir;text', '/path/longsubdir;text', '/path/longlongsubdir;text'], getline(1, 3))
  1064. " Test 2) Paste using zP - in front of the cursor without trailing spaces
  1065. %d
  1066. call setline(1, ['/path;text', '/path;text', '/path;text', '',
  1067. \ 'texttext /subdir columntext',
  1068. \ 'texttext /longsubdir columntext',
  1069. \ 'texttext /longlongsubdir columntext'])
  1070. exe "normal! 5G0f/\<c-v>2jezy"
  1071. norm! 1G0f;zP
  1072. call assert_equal(['/path/subdir;text', '/path/longsubdir;text', '/path/longlongsubdir;text'], getline(1, 3))
  1073. " Test 3) Paste using p - with trailing spaces
  1074. %d
  1075. call setline(1, ['/path;text', '/path;text', '/path;text', '',
  1076. \ 'texttext /subdir columntext',
  1077. \ 'texttext /longsubdir columntext',
  1078. \ 'texttext /longlongsubdir columntext'])
  1079. exe "normal! 5G0f/\<c-v>2jezy"
  1080. norm! 1G0f;hp
  1081. call assert_equal(['/path/subdir ;text', '/path/longsubdir ;text', '/path/longlongsubdir;text'], getline(1, 3))
  1082. " Test 4) Paste using P - with trailing spaces
  1083. %d
  1084. call setline(1, ['/path;text', '/path;text', '/path;text', '',
  1085. \ 'texttext /subdir columntext',
  1086. \ 'texttext /longsubdir columntext',
  1087. \ 'texttext /longlongsubdir columntext'])
  1088. exe "normal! 5G0f/\<c-v>2jezy"
  1089. norm! 1G0f;P
  1090. call assert_equal(['/path/subdir ;text', '/path/longsubdir ;text', '/path/longlongsubdir;text'], getline(1, 3))
  1091. " Test 5) Yank with spaces inside the block
  1092. %d
  1093. call setline(1, ['/path;text', '/path;text', '/path;text', '',
  1094. \ 'texttext /sub dir/ columntext',
  1095. \ 'texttext /lon gsubdir/ columntext',
  1096. \ 'texttext /lon glongsubdir/ columntext'])
  1097. exe "normal! 5G0f/\<c-v>2jf/zy"
  1098. norm! 1G0f;zP
  1099. call assert_equal(['/path/sub dir/;text', '/path/lon gsubdir/;text', '/path/lon glongsubdir/;text'], getline(1, 3))
  1100. bwipe!
  1101. endfunc
  1102. func Test_visual_put_blockedit_zy_and_zp()
  1103. new
  1104. call setline(1, ['aa', 'bbbbb', 'ccc', '', 'XX', 'GGHHJ', 'RTZU'])
  1105. exe "normal! gg0\<c-v>2j$zy"
  1106. norm! 5gg0zP
  1107. call assert_equal(['aa', 'bbbbb', 'ccc', '', 'aaXX', 'bbbbbGGHHJ', 'cccRTZU'], getline(1, 7))
  1108. "
  1109. " now with blockmode editing
  1110. sil %d
  1111. :set ve=block
  1112. call setline(1, ['aa', 'bbbbb', 'ccc', '', 'XX', 'GGHHJ', 'RTZU'])
  1113. exe "normal! gg0\<c-v>2j$zy"
  1114. norm! 5gg0zP
  1115. call assert_equal(['aa', 'bbbbb', 'ccc', '', 'aaXX', 'bbbbbGGHHJ', 'cccRTZU'], getline(1, 7))
  1116. set ve&vim
  1117. bw!
  1118. endfunc
  1119. func Test_visual_block_yank_zy()
  1120. new
  1121. " this was reading before the start of the line
  1122. exe "norm o\<C-T>\<Esc>\<C-V>zy"
  1123. bwipe!
  1124. endfunc
  1125. func Test_visual_block_with_virtualedit()
  1126. CheckScreendump
  1127. let lines =<< trim END
  1128. call setline(1, ['aaaaaa', 'bbbb', 'cc'])
  1129. set virtualedit=block
  1130. normal G
  1131. END
  1132. call writefile(lines, 'XTest_block')
  1133. let buf = RunVimInTerminal('-S XTest_block', {'rows': 8, 'cols': 50})
  1134. call term_sendkeys(buf, "\<C-V>gg$")
  1135. call VerifyScreenDump(buf, 'Test_visual_block_with_virtualedit', {})
  1136. call term_sendkeys(buf, "\<Esc>gg\<C-V>G$")
  1137. call VerifyScreenDump(buf, 'Test_visual_block_with_virtualedit2', {})
  1138. " clean up
  1139. call term_sendkeys(buf, "\<Esc>")
  1140. call StopVimInTerminal(buf)
  1141. call delete('XTest_block')
  1142. endfunc
  1143. func Test_visual_block_ctrl_w_f()
  1144. " Empty block selected in new buffer should not result in an error.
  1145. au! BufNew foo sil norm f
  1146. edit foo
  1147. au! BufNew
  1148. endfunc
  1149. func Test_visual_block_append_invalid_char()
  1150. " this was going over the end of the line
  1151. set isprint=@,161-255
  1152. new
  1153. call setline(1, [' let xxx', 'xxxxxˆ', 'xxxxxxxxxxx'])
  1154. exe "normal 0\<C-V>jjA-\<Esc>"
  1155. call assert_equal([' - let xxx', 'xxxxx -ˆ', 'xxxxxxxx-xxx'], getline(1, 3))
  1156. bwipe!
  1157. set isprint&
  1158. endfunc
  1159. func Test_visual_block_with_substitute()
  1160. " this was reading beyond the end of the line
  1161. new
  1162. norm a0)
  1163. sil! norm  O
  1164. s/)
  1165. sil! norm 
  1166. bwipe!
  1167. endfunc
  1168. func Test_visual_reselect_with_count()
  1169. enew
  1170. call setline(1, ['aaaaaa', '✗ bbbb', '✗ bbbb'])
  1171. exe "normal! 2Gw\<C-V>jed"
  1172. exe "normal! gg0lP"
  1173. call assert_equal(['abbbbaaaaa', '✗bbbb ', '✗ '], getline(1, '$'))
  1174. exe "normal! 1vr."
  1175. call assert_equal(['a....aaaaa', '✗.... ', '✗ '], getline(1, '$'))
  1176. bwipe!
  1177. " this was causing an illegal memory access
  1178. let lines =<< trim END
  1179. :
  1180. r<sfile>
  1181. exe "%norm e3\<c-v>kr\t"
  1182. :
  1183. :
  1184. END
  1185. call writefile(lines, 'XvisualReselect')
  1186. source XvisualReselect
  1187. bwipe!
  1188. call delete('XvisualReselect')
  1189. endfunc
  1190. func Test_visual_reselect_exclusive()
  1191. new
  1192. call setline(1, ['abcde', 'abcde'])
  1193. set selection=exclusive
  1194. normal 1G0viwd
  1195. normal 2G01vd
  1196. call assert_equal(['', ''], getline(1, 2))
  1197. set selection&
  1198. bwipe!
  1199. endfunc
  1200. func Test_visual_block_insert_round_off()
  1201. new
  1202. " The number of characters are tuned to fill a 4096 byte allocated block,
  1203. " so that valgrind reports going over the end.
  1204. call setline(1, ['xxxxx', repeat('0', 1350), "\t", repeat('x', 60)])
  1205. exe "normal gg0\<C-V>GI" .. repeat('0', 1320) .. "\<Esc>"
  1206. bwipe!
  1207. endfunc
  1208. " this was causing an ml_get error
  1209. func Test_visual_exchange_windows()
  1210. enew!
  1211. new
  1212. call setline(1, ['foo', 'bar'])
  1213. exe "normal G\<C-V>gg\<C-W>\<C-X>OO\<Esc>"
  1214. bwipe!
  1215. bwipe!
  1216. endfunc
  1217. " this was leaving the end of the Visual area beyond the end of a line
  1218. func Test_visual_ex_copy_line()
  1219. new
  1220. call setline(1, ["aaa", "bbbbbbbbbxbb"])
  1221. /x
  1222. exe "normal ggvjfxO"
  1223. t0
  1224. normal gNU
  1225. bwipe!
  1226. endfunc
  1227. " This was leaving the end of the Visual area beyond the end of a line.
  1228. " Set 'undolevels' to start a new undo block.
  1229. func Test_visual_undo_deletes_last_line()
  1230. new
  1231. call setline(1, ["aaa", "ccc", "dyd"])
  1232. set undolevels=100
  1233. exe "normal obbbbbbbbbxbb\<Esc>"
  1234. set undolevels=100
  1235. /y
  1236. exe "normal ggvjfxO"
  1237. undo
  1238. normal gNU
  1239. bwipe!
  1240. endfunc
  1241. func Test_visual_paste()
  1242. new
  1243. " v_p overwrites unnamed register.
  1244. call setline(1, ['xxxx'])
  1245. call setreg('"', 'foo')
  1246. call setreg('-', 'bar')
  1247. normal gg0vp
  1248. call assert_equal('x', @")
  1249. call assert_equal('x', @-)
  1250. call assert_equal('fooxxx', getline(1))
  1251. normal $vp
  1252. call assert_equal('x', @")
  1253. call assert_equal('x', @-)
  1254. call assert_equal('fooxxx', getline(1))
  1255. " Test with a different register as unnamed register.
  1256. call setline(2, ['baz'])
  1257. normal 2gg0"rD
  1258. call assert_equal('baz', @")
  1259. normal gg0vp
  1260. call assert_equal('f', @")
  1261. call assert_equal('f', @-)
  1262. call assert_equal('bazooxxx', getline(1))
  1263. normal $vp
  1264. call assert_equal('x', @")
  1265. call assert_equal('x', @-)
  1266. call assert_equal('bazooxxf', getline(1))
  1267. bwipe!
  1268. endfunc
  1269. func Test_visual_paste_clipboard()
  1270. CheckFeature clipboard_working
  1271. if has('gui')
  1272. " auto select feature breaks tests
  1273. set guioptions-=a
  1274. endif
  1275. " v_P does not overwrite unnamed register.
  1276. call setline(1, ['xxxx'])
  1277. call setreg('"', 'foo')
  1278. call setreg('-', 'bar')
  1279. normal gg0vP
  1280. call assert_equal('foo', @")
  1281. call assert_equal('bar', @-)
  1282. call assert_equal('fooxxx', getline(1))
  1283. normal $vP
  1284. call assert_equal('foo', @")
  1285. call assert_equal('bar', @-)
  1286. call assert_equal('fooxxfoo', getline(1))
  1287. " Test with a different register as unnamed register.
  1288. call setline(2, ['baz'])
  1289. normal 2gg0"rD
  1290. call assert_equal('baz', @")
  1291. normal gg0vP
  1292. call assert_equal('baz', @")
  1293. call assert_equal('bar', @-)
  1294. call assert_equal('bazooxxfoo', getline(1))
  1295. normal $vP
  1296. call assert_equal('baz', @")
  1297. call assert_equal('bar', @-)
  1298. call assert_equal('bazooxxfobaz', getline(1))
  1299. " Test for unnamed clipboard
  1300. set clipboard=unnamed
  1301. call setline(1, ['xxxx'])
  1302. call setreg('"', 'foo')
  1303. call setreg('-', 'bar')
  1304. call setreg('*', 'baz')
  1305. normal gg0vP
  1306. call assert_equal('foo', @")
  1307. call assert_equal('bar', @-)
  1308. call assert_equal('baz', @*)
  1309. call assert_equal('bazxxx', getline(1))
  1310. " Test for unnamedplus clipboard
  1311. if has('unnamedplus')
  1312. set clipboard=unnamedplus
  1313. call setline(1, ['xxxx'])
  1314. call setreg('"', 'foo')
  1315. call setreg('-', 'bar')
  1316. call setreg('+', 'baz')
  1317. normal gg0vP
  1318. call assert_equal('foo', @")
  1319. call assert_equal('bar', @-)
  1320. call assert_equal('baz', @+)
  1321. call assert_equal('bazxxx', getline(1))
  1322. endif
  1323. set clipboard&
  1324. if has('gui')
  1325. set guioptions&
  1326. endif
  1327. bwipe!
  1328. endfunc
  1329. func Test_visual_area_adjusted_when_hiding()
  1330. " The Visual area ended after the end of the line after :hide
  1331. call setline(1, 'xxx')
  1332. vsplit Xfile
  1333. call setline(1, 'xxxxxxxx')
  1334. norm! $o
  1335. hid
  1336. norm! zW
  1337. bwipe!
  1338. bwipe!
  1339. endfunc
  1340. func Test_switch_buffer_ends_visual_mode()
  1341. enew
  1342. call setline(1, 'foo')
  1343. set hidden
  1344. set virtualedit=all
  1345. let buf1 = bufnr()
  1346. enew
  1347. let buf2 = bufnr()
  1348. call setline(1, ['', '', '', ''])
  1349. call cursor(4, 5)
  1350. call feedkeys("\<C-V>3k4h", 'xt')
  1351. exe 'buffer' buf1
  1352. call assert_equal('n', mode())
  1353. set nohidden
  1354. set virtualedit=
  1355. bwipe!
  1356. exe 'bwipe!' buf2
  1357. endfunc
  1358. " Check fix for the heap-based buffer overflow bug found in the function
  1359. " utfc_ptr2len and reported at
  1360. " https://huntr.dev/bounties/ae933869-a1ec-402a-bbea-d51764c6618e
  1361. func Test_heap_buffer_overflow()
  1362. enew
  1363. set updatecount=0
  1364. norm R0
  1365. split other
  1366. norm R000
  1367. exe "norm \<C-V>l"
  1368. ball
  1369. call assert_equal(getpos("."), getpos("v"))
  1370. call assert_equal('n', mode())
  1371. norm zW
  1372. %bwipe!
  1373. set updatecount&
  1374. endfunc
  1375. " Test Visual highlight with cursor at end of screen line and 'showbreak'
  1376. func Test_visual_hl_with_showbreak()
  1377. CheckScreendump
  1378. let lines =<< trim END
  1379. setlocal showbreak=+
  1380. call setline(1, repeat('a', &columns + 10))
  1381. normal g$v4lo
  1382. END
  1383. call writefile(lines, 'XTest_visual_sbr', 'D')
  1384. let buf = RunVimInTerminal('-S XTest_visual_sbr', {'rows': 6, 'cols': 50})
  1385. call VerifyScreenDump(buf, 'Test_visual_hl_with_showbreak', {})
  1386. " clean up
  1387. call term_sendkeys(buf, "\<Esc>")
  1388. call StopVimInTerminal(buf)
  1389. endfunc
  1390. func Test_Visual_r_CTRL_C()
  1391. new
  1392. " visual r_cmd
  1393. call setline(1, [' '])
  1394. call feedkeys("\<c-v>$r\<c-c>", 'tx')
  1395. call assert_equal([''], getline(1, 1))
  1396. " visual gr_cmd
  1397. call setline(1, [' '])
  1398. call feedkeys("\<c-v>$gr\<c-c>", 'tx')
  1399. call assert_equal([''], getline(1, 1))
  1400. bw!
  1401. endfunc
  1402. func Test_visual_drag_out_of_window()
  1403. rightbelow vnew
  1404. call setline(1, '123456789')
  1405. set mouse=a
  1406. func ClickExpr(off)
  1407. call Ntest_setmouse(1, getwininfo(win_getid())[0].wincol + a:off)
  1408. return "\<LeftMouse>"
  1409. endfunc
  1410. func DragExpr(off)
  1411. call Ntest_setmouse(1, getwininfo(win_getid())[0].wincol + a:off)
  1412. return "\<LeftDrag>"
  1413. endfunc
  1414. nnoremap <expr> <F2> ClickExpr(5)
  1415. nnoremap <expr> <F3> DragExpr(-1)
  1416. redraw
  1417. call feedkeys("\<F2>\<F3>\<LeftRelease>", 'tx')
  1418. call assert_equal([1, 6], [col('.'), col('v')])
  1419. call feedkeys("\<Esc>", 'tx')
  1420. nnoremap <expr> <F2> ClickExpr(6)
  1421. nnoremap <expr> <F3> DragExpr(-2)
  1422. redraw
  1423. call feedkeys("\<F2>\<F3>\<LeftRelease>", 'tx')
  1424. call assert_equal([1, 7], [col('.'), col('v')])
  1425. call feedkeys("\<Esc>", 'tx')
  1426. nunmap <F2>
  1427. nunmap <F3>
  1428. delfunc ClickExpr
  1429. delfunc DragExpr
  1430. set mouse&
  1431. bwipe!
  1432. endfunc
  1433. func Test_visual_substitute_visual()
  1434. new
  1435. call setline(1, ['one', 'two', 'three'])
  1436. call feedkeys("Gk\<C-V>j$:s/\\%V\\_.*\\%V/foobar\<CR>", 'tx')
  1437. call assert_equal(['one', 'foobar'], getline(1, '$'))
  1438. bwipe!
  1439. endfunc
  1440. func Test_virtualedit_exclusive_selection()
  1441. new
  1442. set virtualedit=all selection=exclusive
  1443. call setline(1, "a\tb")
  1444. normal! 0v8ly
  1445. call assert_equal("a\t", getreg('"'))
  1446. normal! 0v6ly
  1447. call assert_equal('a ', getreg('"'))
  1448. normal! 06lv2ly
  1449. call assert_equal(' ', getreg('"'))
  1450. set virtualedit& selection&
  1451. bwipe!
  1452. endfunc
  1453. func Test_visual_getregion()
  1454. let lines =<< trim END
  1455. new
  1456. call setline(1, ['one', 'two', 'three'])
  1457. #" Visual mode
  1458. call cursor(1, 1)
  1459. call feedkeys("\<ESC>vjl", 'tx')
  1460. call assert_equal(['one', 'tw'],
  1461. \ 'v'->getpos()->getregion(getpos('.')))
  1462. call assert_equal([
  1463. \ [[bufnr('%'), 1, 1, 0], [bufnr('%'), 1, 3, 0]],
  1464. \ [[bufnr('%'), 2, 1, 0], [bufnr('%'), 2, 2, 0]]
  1465. \ ],
  1466. \ 'v'->getpos()->getregionpos(getpos('.')))
  1467. call assert_equal(['one', 'tw'],
  1468. \ '.'->getpos()->getregion(getpos('v')))
  1469. call assert_equal([
  1470. \ [[bufnr('%'), 1, 1, 0], [bufnr('%'), 1, 3, 0]],
  1471. \ [[bufnr('%'), 2, 1, 0], [bufnr('%'), 2, 2, 0]]
  1472. \ ],
  1473. \ '.'->getpos()->getregionpos(getpos('v')))
  1474. call assert_equal(['o'],
  1475. \ 'v'->getpos()->getregion(getpos('v')))
  1476. call assert_equal([
  1477. \ [[bufnr('%'), 1, 1, 0], [bufnr('%'), 1, 1, 0]],
  1478. \ ],
  1479. \ 'v'->getpos()->getregionpos(getpos('v')))
  1480. call assert_equal(['w'],
  1481. \ '.'->getpos()->getregion(getpos('.'), {'type': 'v' }))
  1482. call assert_equal([
  1483. \ [[bufnr('%'), 2, 2, 0], [bufnr('%'), 2, 2, 0]],
  1484. \ ],
  1485. \ '.'->getpos()->getregionpos(getpos('.'), {'type': 'v' }))
  1486. call assert_equal(['one', 'two'],
  1487. \ getpos('.')->getregion(getpos('v'), {'type': 'V' }))
  1488. call assert_equal([
  1489. \ [[bufnr('%'), 1, 1, 0], [bufnr('%'), 1, 3, 0]],
  1490. \ [[bufnr('%'), 2, 1, 0], [bufnr('%'), 2, 3, 0]],
  1491. \ ],
  1492. \ getpos('.')->getregionpos(getpos('v'), {'type': 'V' }))
  1493. call assert_equal(['on', 'tw'],
  1494. \ getpos('.')->getregion(getpos('v'), {'type': "\<C-v>" }))
  1495. call assert_equal([
  1496. \ [[bufnr('%'), 1, 1, 0], [bufnr('%'), 1, 2, 0]],
  1497. \ [[bufnr('%'), 2, 1, 0], [bufnr('%'), 2, 2, 0]],
  1498. \ ],
  1499. \ getpos('.')->getregionpos(getpos('v'), {'type': "\<C-v>" }))
  1500. #" Line visual mode
  1501. call cursor(1, 1)
  1502. call feedkeys("\<ESC>Vl", 'tx')
  1503. call assert_equal(['one'],
  1504. \ getregion(getpos('v'), getpos('.'), {'type': 'V' }))
  1505. call assert_equal(['one'],
  1506. \ getregion(getpos('.'), getpos('v'), {'type': 'V' }))
  1507. call assert_equal(['one'],
  1508. \ getregion(getpos('v'), getpos('v'), {'type': 'V' }))
  1509. call assert_equal(['one'],
  1510. \ getregion(getpos('.'), getpos('.'), {'type': 'V' }))
  1511. call assert_equal(['on'],
  1512. \ getpos('.')->getregion(getpos('v'), {'type': 'v' }))
  1513. call assert_equal(['on'],
  1514. \ getpos('.')->getregion(getpos('v'), {'type': "\<C-v>" }))
  1515. #" Block visual mode
  1516. call cursor(1, 1)
  1517. call feedkeys("\<ESC>\<C-v>ll", 'tx')
  1518. call assert_equal(['one'],
  1519. \ getregion(getpos('v'), getpos('.'), {'type': "\<C-v>" }))
  1520. call assert_equal(['one'],
  1521. \ getregion(getpos('.'), getpos('v'), {'type': "\<C-v>" }))
  1522. call assert_equal(['o'],
  1523. \ getregion(getpos('v'), getpos('v'), {'type': "\<C-v>" }))
  1524. call assert_equal(['e'],
  1525. \ getregion(getpos('.'), getpos('.'), {'type': "\<C-v>" }))
  1526. call assert_equal(['one'],
  1527. \ '.'->getpos()->getregion(getpos('v'), {'type': 'V' }))
  1528. call assert_equal(['one'],
  1529. \ '.'->getpos()->getregion(getpos('v'), {'type': 'v' }))
  1530. #" Using Marks
  1531. call setpos("'a", [0, 2, 3, 0])
  1532. call cursor(1, 1)
  1533. call assert_equal(['one', 'two'],
  1534. \ "'a"->getpos()->getregion(getpos('.'), {'type': 'v' }))
  1535. call assert_equal(['one', 'two'],
  1536. \ "."->getpos()->getregion(getpos("'a"), {'type': 'v' }))
  1537. call assert_equal(['one', 'two'],
  1538. \ "."->getpos()->getregion(getpos("'a"), {'type': 'V' }))
  1539. call assert_equal(['two'],
  1540. \ "'a"->getpos()->getregion(getpos("'a"), {'type': 'V' }))
  1541. call assert_equal(['one', 'two'],
  1542. \ "."->getpos()->getregion(getpos("'a"), {'type': "\<c-v>" }))
  1543. call feedkeys("\<ESC>jVj\<ESC>", 'tx')
  1544. call assert_equal(['two', 'three'], getregion(getpos("'<"), getpos("'>")))
  1545. call assert_equal(['two', 'three'], getregion(getpos("'>"), getpos("'<")))
  1546. #" Using List
  1547. call cursor(1, 1)
  1548. call assert_equal(['one', 'two'],
  1549. \ [0, 2, 3, 0]->getregion(getpos('.'), {'type': 'v' }))
  1550. call assert_equal(['one', 'two'],
  1551. \ '.'->getpos()->getregion([0, 2, 3, 0], {'type': 'v' }))
  1552. call assert_equal(['one', 'two'],
  1553. \ '.'->getpos()->getregion([0, 2, 3, 0], {'type': 'V' }))
  1554. call assert_equal(['two'],
  1555. \ [0, 2, 3, 0]->getregion([0, 2, 3, 0], {'type': 'V' }))
  1556. call assert_equal(['one', 'two'],
  1557. \ '.'->getpos()->getregion([0, 2, 3, 0], {'type': "\<c-v>" }))
  1558. #" Multiline with line visual mode
  1559. call cursor(1, 1)
  1560. call feedkeys("\<ESC>Vjj", 'tx')
  1561. call assert_equal(['one', 'two', 'three'],
  1562. \ getregion(getpos('v'), getpos('.'), {'type': 'V' }))
  1563. call assert_equal([
  1564. \ [[bufnr('%'), 1, 1, 0], [bufnr('%'), 1, 3, 0]],
  1565. \ [[bufnr('%'), 2, 1, 0], [bufnr('%'), 2, 3, 0]],
  1566. \ [[bufnr('%'), 3, 1, 0], [bufnr('%'), 3, 5, 0]],
  1567. \ ],
  1568. \ getregionpos(getpos('v'), getpos('.'), {'type': 'V' }))
  1569. call assert_equal([
  1570. \ [[bufnr('%'), 1, 1, 0], [bufnr('%'), 1, 4, 0]],
  1571. \ [[bufnr('%'), 2, 1, 0], [bufnr('%'), 2, 4, 0]],
  1572. \ [[bufnr('%'), 3, 1, 0], [bufnr('%'), 3, 6, 0]],
  1573. \ ],
  1574. \ getregionpos(getpos('v'), getpos('.'),
  1575. \ {'type': 'V', 'eol': v:true }))
  1576. #" Multiline with block visual mode
  1577. call cursor(1, 1)
  1578. call feedkeys("\<ESC>\<C-v>jj", 'tx')
  1579. call assert_equal(['o', 't', 't'],
  1580. \ getregion(getpos('v'), getpos('.'), {'type': "\<C-v>" }))
  1581. call assert_equal([
  1582. \ [[bufnr('%'), 1, 1, 0], [bufnr('%'), 1, 1, 0]],
  1583. \ [[bufnr('%'), 2, 1, 0], [bufnr('%'), 2, 1, 0]],
  1584. \ [[bufnr('%'), 3, 1, 0], [bufnr('%'), 3, 1, 0]],
  1585. \ ],
  1586. \ getregionpos(getpos('v'), getpos('.'), {'type': "\<C-v>" }))
  1587. call cursor(1, 1)
  1588. call feedkeys("\<ESC>\<C-v>jj$", 'tx')
  1589. call assert_equal(['one', 'two', 'three'],
  1590. \ getregion(getpos('v'), getpos('.'), {'type': "\<C-v>" }))
  1591. call assert_equal([
  1592. \ [[bufnr('%'), 1, 1, 0], [bufnr('%'), 1, 3, 0]],
  1593. \ [[bufnr('%'), 2, 1, 0], [bufnr('%'), 2, 3, 0]],
  1594. \ [[bufnr('%'), 3, 1, 0], [bufnr('%'), 3, 5, 0]],
  1595. \ ],
  1596. \ getregionpos(getpos('v'), getpos('.'), {'type': "\<C-v>" }))
  1597. call assert_equal([
  1598. \ [[bufnr('%'), 1, 1, 0], [bufnr('%'), 1, 3, 0]],
  1599. \ [[bufnr('%'), 2, 1, 0], [bufnr('%'), 2, 3, 0]],
  1600. \ [[bufnr('%'), 3, 1, 0], [bufnr('%'), 3, 5, 0]],
  1601. \ ],
  1602. \ getregionpos(getpos('v'), getpos('.'),
  1603. \ {'type': "\<C-v>", 'eol': v:true }))
  1604. #" 'virtualedit'
  1605. set virtualedit=all
  1606. call cursor(1, 1)
  1607. call feedkeys("\<ESC>\<C-v>10ljj$", 'tx')
  1608. call assert_equal(['one ', 'two ', 'three '],
  1609. \ getregion(getpos('v'), getpos('.'), {'type': "\<C-v>" }))
  1610. call assert_equal([
  1611. \ [[bufnr('%'), 1, 1, 0], [bufnr('%'), 1, 3, 0]],
  1612. \ [[bufnr('%'), 2, 1, 0], [bufnr('%'), 2, 3, 0]],
  1613. \ [[bufnr('%'), 3, 1, 0], [bufnr('%'), 3, 5, 0]],
  1614. \ ],
  1615. \ getregionpos(getpos('v'), getpos('.'), {'type': "\<C-v>" }))
  1616. call assert_equal([
  1617. \ [[bufnr('%'), 1, 1, 0], [bufnr('%'), 1, 4, 3]],
  1618. \ [[bufnr('%'), 2, 1, 0], [bufnr('%'), 2, 4, 3]],
  1619. \ [[bufnr('%'), 3, 1, 0], [bufnr('%'), 3, 6, 1]],
  1620. \ ],
  1621. \ getregionpos(getpos('v'), getpos('.'),
  1622. \ {'type': "\<C-v>", 'eol': v:true }))
  1623. call cursor(3, 5)
  1624. call feedkeys("\<ESC>\<C-v>hkk", 'tx')
  1625. call assert_equal([' ', ' ', 'ee'],
  1626. \ getregion(getpos('v'), getpos('.'), {'type': "\<C-v>" }))
  1627. call assert_equal([
  1628. \ [[bufnr('%'), 1, 0, 0], [bufnr('%'), 1, 0, 0]],
  1629. \ [[bufnr('%'), 2, 0, 0], [bufnr('%'), 2, 0, 0]],
  1630. \ [[bufnr('%'), 3, 4, 0], [bufnr('%'), 3, 5, 0]],
  1631. \ ],
  1632. \ getregionpos(getpos('v'), getpos('.'), {'type': "\<C-v>" }))
  1633. call assert_equal([
  1634. \ [[bufnr('%'), 1, 4, 0], [bufnr('%'), 1, 4, 2]],
  1635. \ [[bufnr('%'), 2, 4, 0], [bufnr('%'), 2, 4, 2]],
  1636. \ [[bufnr('%'), 3, 4, 0], [bufnr('%'), 3, 5, 0]],
  1637. \ ],
  1638. \ getregionpos(getpos('v'), getpos('.'),
  1639. \ {'type': "\<C-v>", 'eol': v:true }))
  1640. call cursor(3, 5)
  1641. call feedkeys("\<ESC>\<C-v>kk", 'tx')
  1642. call assert_equal([' ', ' ', 'e'],
  1643. \ getregion(getpos('v'), getpos('.'), {'type': "\<C-v>" }))
  1644. call assert_equal([
  1645. \ [[bufnr('%'), 1, 0, 0], [bufnr('%'), 1, 0, 0]],
  1646. \ [[bufnr('%'), 2, 0, 0], [bufnr('%'), 2, 0, 0]],
  1647. \ [[bufnr('%'), 3, 5, 0], [bufnr('%'), 3, 5, 0]],
  1648. \ ],
  1649. \ getregionpos(getpos('v'), getpos('.'), {'type': "\<C-v>" }))
  1650. call assert_equal([
  1651. \ [[bufnr('%'), 1, 4, 1], [bufnr('%'), 1, 4, 2]],
  1652. \ [[bufnr('%'), 2, 4, 1], [bufnr('%'), 2, 4, 2]],
  1653. \ [[bufnr('%'), 3, 5, 0], [bufnr('%'), 3, 5, 0]],
  1654. \ ],
  1655. \ getregionpos(getpos('v'), getpos('.'),
  1656. \ {'type': "\<C-v>", 'eol': v:true }))
  1657. call cursor(1, 3)
  1658. call feedkeys("\<ESC>vjj4l", 'tx')
  1659. call assert_equal(['e', 'two', 'three '],
  1660. \ getregion(getpos('v'), getpos('.'), {'type': 'v' }))
  1661. call assert_equal([
  1662. \ [[bufnr('%'), 1, 3, 0], [bufnr('%'), 1, 3, 0]],
  1663. \ [[bufnr('%'), 2, 1, 0], [bufnr('%'), 2, 3, 0]],
  1664. \ [[bufnr('%'), 3, 1, 0], [bufnr('%'), 3, 5, 0]],
  1665. \ ],
  1666. \ getregionpos(getpos('v'), getpos('.'), {'type': 'v' }))
  1667. call assert_equal([
  1668. \ [[bufnr('%'), 1, 3, 0], [bufnr('%'), 1, 4, 0]],
  1669. \ [[bufnr('%'), 2, 1, 0], [bufnr('%'), 2, 4, 0]],
  1670. \ [[bufnr('%'), 3, 1, 0], [bufnr('%'), 3, 6, 2]],
  1671. \ ],
  1672. \ getregionpos(getpos('v'), getpos('.'),
  1673. \ {'type': 'v', 'eol': v:true }))
  1674. call cursor(1, 3)
  1675. call feedkeys("\<ESC>lvjj3l", 'tx')
  1676. call assert_equal(['', 'two', 'three '],
  1677. \ getregion(getpos('v'), getpos('.'), {'type': 'v' }))
  1678. call assert_equal([
  1679. \ [[bufnr('%'), 1, 0, 0], [bufnr('%'), 1, 0, 0]],
  1680. \ [[bufnr('%'), 2, 1, 0], [bufnr('%'), 2, 3, 0]],
  1681. \ [[bufnr('%'), 3, 1, 0], [bufnr('%'), 3, 5, 0]],
  1682. \ ],
  1683. \ getregionpos(getpos('v'), getpos('.'), {'type': 'v' }))
  1684. call assert_equal([
  1685. \ [[bufnr('%'), 1, 4, 0], [bufnr('%'), 1, 4, 0]],
  1686. \ [[bufnr('%'), 2, 1, 0], [bufnr('%'), 2, 4, 0]],
  1687. \ [[bufnr('%'), 3, 1, 0], [bufnr('%'), 3, 6, 2]],
  1688. \ ],
  1689. \ getregionpos(getpos('v'), getpos('.'),
  1690. \ {'type': 'v', 'eol': v:true }))
  1691. call cursor(3, 5)
  1692. call feedkeys("\<ESC>v3l", 'tx')
  1693. call assert_equal(['e '],
  1694. \ getregion(getpos('v'), getpos('.'), {'type': 'v' }))
  1695. call assert_equal([
  1696. \ [[bufnr('%'), 3, 5, 0], [bufnr('%'), 3, 5, 0]],
  1697. \ ],
  1698. \ getregionpos(getpos('v'), getpos('.'), {'type': 'v' }))
  1699. call assert_equal([
  1700. \ [[bufnr('%'), 3, 5, 0], [bufnr('%'), 3, 6, 3]],
  1701. \ ],
  1702. \ getregionpos(getpos('v'), getpos('.'),
  1703. \ {'type': 'v', 'eol': v:true }))
  1704. call cursor(3, 5)
  1705. call feedkeys("\<ESC>lv3l", 'tx')
  1706. call assert_equal([' '],
  1707. \ getregion(getpos('v'), getpos('.'), {'type': 'v' }))
  1708. call assert_equal([
  1709. \ [[bufnr('%'), 3, 0, 0], [bufnr('%'), 3, 0, 0]],
  1710. \ ],
  1711. \ getregionpos(getpos('v'), getpos('.'), {'type': 'v' }))
  1712. call assert_equal([
  1713. \ [[bufnr('%'), 3, 6, 0], [bufnr('%'), 3, 6, 4]],
  1714. \ ],
  1715. \ getregionpos(getpos('v'), getpos('.'),
  1716. \ {'type': 'v', 'eol': v:true }))
  1717. call cursor(3, 5)
  1718. call feedkeys("\<ESC>3lv3l", 'tx')
  1719. call assert_equal([' '],
  1720. \ getregion(getpos('v'), getpos('.'), {'type': 'v' }))
  1721. call assert_equal([
  1722. \ [[bufnr('%'), 3, 0, 0], [bufnr('%'), 3, 0, 0]],
  1723. \ ],
  1724. \ getregionpos(getpos('v'), getpos('.'), {'type': 'v' }))
  1725. call assert_equal([
  1726. \ [[bufnr('%'), 3, 6, 2], [bufnr('%'), 3, 6, 6]],
  1727. \ ],
  1728. \ getregionpos(getpos('v'), getpos('.'),
  1729. \ {'type': 'v', 'eol': v:true }))
  1730. set virtualedit&
  1731. #" using wrong types for positions
  1732. call cursor(1, 1)
  1733. call feedkeys("\<ESC>vjj$", 'tx')
  1734. call assert_fails("call getregion(1, 2)", 'E1211:')
  1735. call assert_fails("call getregion(getpos('.'), {})", 'E1211:')
  1736. call assert_fails(':echo "."->getpos()->getregion("$", [])', 'E1211:')
  1737. call assert_fails("call getregionpos(1, 2)", 'E1211:')
  1738. call assert_fails("call getregionpos(getpos('.'), {})", 'E1211:')
  1739. call assert_fails(':echo "."->getpos()->getregionpos("$", [])', 'E1211:')
  1740. #" using invalid value for "type"
  1741. call assert_fails("call getregion(getpos('.'), getpos('.'), {'type': '' })", 'E475:')
  1742. call assert_fails("call getregionpos(getpos('.'), getpos('.'), {'type': '' })", 'E475:')
  1743. call assert_fails("call getregion(getpos('.'), getpos('.'), {'type': 'v0' })", 'E475:')
  1744. call assert_fails("call getregionpos(getpos('.'), getpos('.'), {'type': 'v0' })", 'E475:')
  1745. call assert_fails("call getregion(getpos('.'), getpos('.'), {'type': 'V0' })", 'E475:')
  1746. call assert_fails("call getregionpos(getpos('.'), getpos('.'), {'type': 'V0' })", 'E475:')
  1747. call assert_fails("call getregion(getpos('.'), getpos('.'), {'type': '\<C-v>0' })", 'E475:')
  1748. call assert_fails("call getregionpos(getpos('.'), getpos('.'), {'type': '\<C-v>0' })", 'E475:')
  1749. call assert_fails("call getregion(getpos('.'), getpos('.'), {'type': '\<C-v>1:' })", 'E475:')
  1750. call assert_fails("call getregionpos(getpos('.'), getpos('.'), {'type': '\<C-v>1:' })", 'E475:')
  1751. #" using a mark from another buffer to current buffer
  1752. new
  1753. LET g:buf = bufnr()
  1754. call setline(1, range(10))
  1755. normal! GmA
  1756. wincmd p
  1757. call assert_equal([g:buf, 10, 1, 0], getpos("'A"))
  1758. call assert_equal([], getregion(getpos('.'), getpos("'A"), {'type': 'v' }))
  1759. call assert_equal([], getregion(getpos("'A"), getpos('.'), {'type': 'v' }))
  1760. call assert_equal([], getregionpos(getpos('.'), getpos("'A"), {'type': 'v' }))
  1761. call assert_equal([], getregionpos(getpos("'A"), getpos('.'), {'type': 'v' }))
  1762. #" using two marks from another buffer
  1763. wincmd p
  1764. normal! GmB
  1765. wincmd p
  1766. call assert_equal([g:buf, 10, 1, 0], getpos("'B"))
  1767. call assert_equal(['9'],
  1768. \ getregion(getpos("'B"), getpos("'A"), {'type': 'v' }))
  1769. call assert_equal([
  1770. \ [[g:buf, 10, 1, 0], [g:buf, 10, 1, 0]],
  1771. \ ],
  1772. \ getregionpos(getpos("'B"), getpos("'A"), {'type': 'v' }))
  1773. #" using two positions from another buffer
  1774. for type in ['v', 'V', "\<C-V>"]
  1775. for exclusive in [v:false, v:true]
  1776. call assert_equal(range(10)->mapnew('string(v:val)'),
  1777. \ getregion([g:buf, 1, 1, 0], [g:buf, 10, 2, 0],
  1778. \ {'type': type, 'exclusive': exclusive }))
  1779. call assert_equal(range(10)->mapnew('string(v:val)'),
  1780. \ getregion([g:buf, 10, 2, 0], [g:buf, 1, 1, 0],
  1781. \ {'type': type, 'exclusive': exclusive }))
  1782. call assert_equal(range(1, 10)->mapnew('repeat([[g:buf, v:val, 1, 0]], 2)'),
  1783. \ getregionpos([g:buf, 1, 1, 0], [g:buf, 10, 2, 0],
  1784. \ {'type': type, 'exclusive': exclusive }))
  1785. call assert_equal(range(1, 10)->mapnew('repeat([[g:buf, v:val, 1, 0]], 2)'),
  1786. \ getregionpos([g:buf, 10, 2, 0], [g:buf, 1, 1, 0],
  1787. \ {'type': type, 'exclusive': exclusive }))
  1788. endfor
  1789. endfor
  1790. #" using invalid positions in buffer
  1791. call assert_fails('call getregion([g:buf, 0, 1, 0], [g:buf, 10, 2, 0])', 'E966:')
  1792. call assert_fails('call getregion([g:buf, 10, 2, 0], [g:buf, 0, 1, 0])', 'E966:')
  1793. call assert_fails('call getregion([g:buf, 1, 1, 0], [g:buf, 11, 2, 0])', 'E966:')
  1794. call assert_fails('call getregion([g:buf, 11, 2, 0], [g:buf, 1, 1, 0])', 'E966:')
  1795. call assert_fails('call getregion([g:buf, 1, 1, 0], [g:buf, 10, 0, 0])', 'E964:')
  1796. call assert_fails('call getregion([g:buf, 10, 0, 0], [g:buf, 1, 1, 0])', 'E964:')
  1797. call assert_fails('call getregion([g:buf, 1, 1, 0], [g:buf, 10, 3, 0])', 'E964:')
  1798. call assert_fails('call getregion([g:buf, 10, 3, 0], [g:buf, 1, 1, 0])', 'E964:')
  1799. call assert_fails('call getregion([g:buf, 1, 0, 0], [g:buf, 1, 1, 0])', 'E964:')
  1800. call assert_fails('call getregion([g:buf, 1, 1, 0], [g:buf, 1, 0, 0])', 'E964:')
  1801. #" using invalid buffer
  1802. call assert_fails('call getregion([10000, 10, 1, 0], [10000, 10, 1, 0])', 'E681:')
  1803. exe $':{g:buf}bwipe!'
  1804. unlet g:buf
  1805. bwipe!
  1806. END
  1807. call CheckLegacyAndVim9Success(lines)
  1808. let lines =<< trim END
  1809. #" Selection in starts or ends in the middle of a multibyte character
  1810. new
  1811. call setline(1, [
  1812. \ "abcdefghijk\u00ab",
  1813. \ "\U0001f1e6\u00ab\U0001f1e7\u00ab\U0001f1e8\u00ab\U0001f1e9",
  1814. \ "1234567890"
  1815. \ ])
  1816. call cursor(1, 3)
  1817. call feedkeys("\<Esc>\<C-v>ljj", 'xt')
  1818. call assert_equal(['cd', "\u00ab ", '34'],
  1819. \ getregion(getpos('v'), getpos('.'), {'type': "\<C-v>" }))
  1820. call assert_equal([
  1821. \ [[bufnr('%'), 1, 3, 0], [bufnr('%'), 1, 4, 0]],
  1822. \ [[bufnr('%'), 2, 5, 0], [bufnr('%'), 2, 7, 1]],
  1823. \ [[bufnr('%'), 3, 3, 0], [bufnr('%'), 3, 4, 0]],
  1824. \ ],
  1825. \ getregionpos(getpos('v'), getpos('.'), {'type': "\<C-v>" }))
  1826. call cursor(1, 4)
  1827. call feedkeys("\<Esc>\<C-v>ljj", 'xt')
  1828. call assert_equal(['de', "\U0001f1e7", '45'],
  1829. \ getregion(getpos('v'), getpos('.'), {'type': "\<C-v>" }))
  1830. call assert_equal([
  1831. \ [[bufnr('%'), 1, 4, 0], [bufnr('%'), 1, 5, 0]],
  1832. \ [[bufnr('%'), 2, 7, 0], [bufnr('%'), 2, 10, 0]],
  1833. \ [[bufnr('%'), 3, 4, 0], [bufnr('%'), 3, 5, 0]],
  1834. \ ],
  1835. \ getregionpos(getpos('v'), getpos('.'), {'type': "\<C-v>" }))
  1836. call cursor(1, 5)
  1837. call feedkeys("\<Esc>\<C-v>jj", 'xt')
  1838. call assert_equal(['e', ' ', '5'],
  1839. \ getregion(getpos('v'), getpos('.'), {'type': "\<C-v>" }))
  1840. call assert_equal([
  1841. \ [[bufnr('%'), 1, 5, 0], [bufnr('%'), 1, 5, 0]],
  1842. \ [[bufnr('%'), 2, 7, 1], [bufnr('%'), 2, 7, 2]],
  1843. \ [[bufnr('%'), 3, 5, 0], [bufnr('%'), 3, 5, 0]],
  1844. \ ],
  1845. \ getregionpos(getpos('v'), getpos('.'), {'type': "\<C-v>" }))
  1846. call assert_equal(['efghijk«', '🇦«🇧«🇨«🇩', '12345'],
  1847. \ getregion(getpos('v'), getpos('.'), {'type': 'v' }))
  1848. call assert_equal([
  1849. \ [[bufnr('%'), 1, 5, 0], [bufnr('%'), 1, 13, 0]],
  1850. \ [[bufnr('%'), 2, 1, 0], [bufnr('%'), 2, 22, 0]],
  1851. \ [[bufnr('%'), 3, 1, 0], [bufnr('%'), 3, 5, 0]],
  1852. \ ],
  1853. \ getregionpos(getpos('v'), getpos('.'), {'type': 'v' }))
  1854. call cursor(1, 5)
  1855. call feedkeys("\<Esc>\<C-v>5l2j", 'xt')
  1856. call assert_equal(['efghij', ' «🇨« ', '567890'],
  1857. \ getregion(getpos('v'), getpos('.'), {'type': "\<C-v>" }))
  1858. call assert_equal([
  1859. \ [[bufnr('%'), 1, 5, 0], [bufnr('%'), 1, 10, 0]],
  1860. \ [[bufnr('%'), 2, 7, 1], [bufnr('%'), 2, 19, 1]],
  1861. \ [[bufnr('%'), 3, 5, 0], [bufnr('%'), 3, 10, 0]],
  1862. \ ],
  1863. \ getregionpos(getpos('v'), getpos('.'), {'type': "\<C-v>" }))
  1864. call cursor(1, 4)
  1865. call feedkeys("\<Esc>\<C-v>02j", 'xt')
  1866. call assert_equal(['abcd', '🇦« ', '1234'],
  1867. \ getregion(getpos('v'), getpos('.'), {'type': "\<C-v>" }))
  1868. call assert_equal([
  1869. \ [[bufnr('%'), 1, 1, 0], [bufnr('%'), 1, 4, 0]],
  1870. \ [[bufnr('%'), 2, 1, 0], [bufnr('%'), 2, 7, 1]],
  1871. \ [[bufnr('%'), 3, 1, 0], [bufnr('%'), 3, 4, 0]],
  1872. \ ],
  1873. \ getregionpos(getpos('v'), getpos('.'), {'type': "\<C-v>" }))
  1874. #" characterwise selection with multibyte chars
  1875. call cursor(1, 1)
  1876. call feedkeys("\<Esc>vj", 'xt')
  1877. call assert_equal(['abcdefghijk«', "\U0001f1e6"],
  1878. \ getregion(getpos('v'), getpos('.'), {'type': 'v' }))
  1879. call assert_equal([
  1880. \ [[bufnr('%'), 1, 1, 0], [bufnr('%'), 1, 13, 0]],
  1881. \ [[bufnr('%'), 2, 1, 0], [bufnr('%'), 2, 4, 0]],
  1882. \ ],
  1883. \ getregionpos(getpos('v'), getpos('.'), {'type': 'v' }))
  1884. set selection=exclusive
  1885. call feedkeys('l', 'xt')
  1886. call assert_equal(['abcdefghijk«', "\U0001f1e6"],
  1887. \ getregion(getpos('v'), getpos('.'), {'type': 'v' }))
  1888. call assert_equal([
  1889. \ [[bufnr('%'), 1, 1, 0], [bufnr('%'), 1, 13, 0]],
  1890. \ [[bufnr('%'), 2, 1, 0], [bufnr('%'), 2, 4, 0]],
  1891. \ ],
  1892. \ getregionpos(getpos('v'), getpos('.'), {'type': 'v' }))
  1893. #" marks on multibyte chars
  1894. call setpos("'a", [0, 1, 11, 0])
  1895. call setpos("'b", [0, 2, 16, 0])
  1896. call setpos("'c", [0, 2, 0, 0])
  1897. call cursor(1, 1)
  1898. call assert_equal(['ghijk', '🇨«🇩'],
  1899. \ getregion(getpos("'a"), getpos("'b"), {'type': "\<C-v>" }))
  1900. call assert_equal([
  1901. \ [[bufnr('%'), 1, 7, 0], [bufnr('%'), 1, 11, 0]],
  1902. \ [[bufnr('%'), 2, 13, 0], [bufnr('%'), 2, 22, 0]],
  1903. \ ],
  1904. \ getregionpos(getpos("'a"), getpos("'b"), {'type': "\<C-v>" }))
  1905. call assert_equal(['k«', '🇦«🇧«🇨'],
  1906. \ getregion(getpos("'a"), getpos("'b"), {'type': 'v' }))
  1907. call assert_equal([
  1908. \ [[bufnr('%'), 1, 11, 0], [bufnr('%'), 1, 13, 0]],
  1909. \ [[bufnr('%'), 2, 1, 0], [bufnr('%'), 2, 16, 0]],
  1910. \ ],
  1911. \ getregionpos(getpos("'a"), getpos("'b"), {'type': 'v' }))
  1912. call assert_equal(['k«'],
  1913. \ getregion(getpos("'a"), getpos("'c"), {'type': 'v' }))
  1914. call assert_equal([
  1915. \ [[bufnr('%'), 1, 11, 0], [bufnr('%'), 1, 13, 0]],
  1916. \ ],
  1917. \ getregionpos(getpos("'a"), getpos("'c"), {'type': 'v' }))
  1918. #" use inclusive selection, although 'selection' is exclusive
  1919. call setpos("'a", [0, 1, 11, 0])
  1920. call setpos("'b", [0, 1, 1, 0])
  1921. call assert_equal(['abcdefghijk'],
  1922. \ getregion(getpos("'a"), getpos("'b"),
  1923. \ {'type': "\<c-v>", 'exclusive': v:false }))
  1924. call assert_equal(['abcdefghij'],
  1925. \ getregion(getpos("'a"), getpos("'b"),
  1926. \ {'type': "\<c-v>", 'exclusive': v:true }))
  1927. call assert_equal(['abcdefghijk'],
  1928. \ getregion(getpos("'a"), getpos("'b"),
  1929. \ {'type': 'v', 'exclusive': 0 }))
  1930. call assert_equal(['abcdefghij'],
  1931. \ getregion(getpos("'a"), getpos("'b"),
  1932. \ {'type': 'v', 'exclusive': 1 }))
  1933. call assert_equal(['abcdefghijk«'],
  1934. \ getregion(getpos("'a"), getpos("'b"),
  1935. \ {'type': 'V', 'exclusive': 0 }))
  1936. call assert_equal(['abcdefghijk«'],
  1937. \ getregion(getpos("'a"), getpos("'b"),
  1938. \ {'type': 'V', 'exclusive': 1 }))
  1939. set selection&
  1940. bwipe!
  1941. END
  1942. call CheckLegacyAndVim9Success(lines)
  1943. let lines =<< trim END
  1944. #" Exclusive selection
  1945. new
  1946. set selection=exclusive
  1947. call setline(1, ["a\tc", "x\tz", '', ''])
  1948. call cursor(1, 1)
  1949. call feedkeys("\<Esc>v2l", 'xt')
  1950. call assert_equal(["a\t"],
  1951. \ getregion(getpos('v'), getpos('.'), {'type': 'v' }))
  1952. call cursor(1, 1)
  1953. call feedkeys("\<Esc>v$G", 'xt')
  1954. call assert_equal(["a\tc", "x\tz", ''],
  1955. \ getregion(getpos('v'), getpos('.'), {'type': 'v' }))
  1956. call cursor(1, 1)
  1957. call feedkeys("\<Esc>v$j", 'xt')
  1958. call assert_equal(["a\tc", "x\tz"],
  1959. \ getregion(getpos('v'), getpos('.'), {'type': 'v' }))
  1960. call cursor(1, 1)
  1961. call feedkeys("\<Esc>\<C-v>$j", 'xt')
  1962. call assert_equal(["a\tc", "x\tz"],
  1963. \ getregion(getpos('v'), getpos('.'), {'type': "\<C-v>" }))
  1964. call cursor(1, 1)
  1965. call feedkeys("\<Esc>\<C-v>$G", 'xt')
  1966. call assert_equal(["a", "x", '', ''],
  1967. \ getregion(getpos('v'), getpos('.'), {'type': "\<C-v>" }))
  1968. call cursor(1, 1)
  1969. call feedkeys("\<Esc>wv2j", 'xt')
  1970. call assert_equal(["c", "x\tz"],
  1971. \ getregion(getpos('v'), getpos('.'), {'type': 'v' }))
  1972. set selection&
  1973. bwipe!
  1974. #" Exclusive selection 2
  1975. new
  1976. call setline(1, ["a\tc", "x\tz", '', ''])
  1977. call cursor(1, 1)
  1978. call feedkeys("\<Esc>v2l", 'xt')
  1979. call assert_equal(["a\t"],
  1980. \ getregion(getpos('v'), getpos('.'), {'exclusive': v:true }))
  1981. call assert_equal([
  1982. \ [[bufnr('%'), 1, 1, 0], [bufnr('%'), 1, 2, 0]],
  1983. \ ],
  1984. \ getregionpos(getpos('v'), getpos('.'), {'exclusive': v:true }))
  1985. call cursor(1, 1)
  1986. call feedkeys("\<Esc>v$G", 'xt')
  1987. call assert_equal(["a\tc", "x\tz", ''],
  1988. \ getregion(getpos('v'), getpos('.'), {'exclusive': v:true }))
  1989. call assert_equal([
  1990. \ [[bufnr('%'), 1, 1, 0], [bufnr('%'), 1, 3, 0]],
  1991. \ [[bufnr('%'), 2, 1, 0], [bufnr('%'), 2, 3, 0]],
  1992. \ [[bufnr('%'), 3, 0, 0], [bufnr('%'), 3, 0, 0]],
  1993. \ ],
  1994. \ getregionpos(getpos('v'), getpos('.'), {'exclusive': v:true }))
  1995. call cursor(1, 1)
  1996. call feedkeys("\<Esc>v$j", 'xt')
  1997. call assert_equal(["a\tc", "x\tz"],
  1998. \ getregion(getpos('v'), getpos('.'), {'exclusive': v:true }))
  1999. call assert_equal([
  2000. \ [[bufnr('%'), 1, 1, 0], [bufnr('%'), 1, 3, 0]],
  2001. \ [[bufnr('%'), 2, 1, 0], [bufnr('%'), 2, 3, 0]],
  2002. \ ],
  2003. \ getregionpos(getpos('v'), getpos('.'), {'exclusive': v:true }))
  2004. call cursor(1, 1)
  2005. call feedkeys("\<Esc>\<C-v>$j", 'xt')
  2006. call assert_equal(["a\tc", "x\tz"],
  2007. \ getregion(getpos('v'), getpos('.'),
  2008. \ {'exclusive': v:true, 'type': "\<C-v>" }))
  2009. call assert_equal([
  2010. \ [[bufnr('%'), 1, 1, 0], [bufnr('%'), 1, 3, 0]],
  2011. \ [[bufnr('%'), 2, 1, 0], [bufnr('%'), 2, 3, 0]],
  2012. \ ],
  2013. \ getregionpos(getpos('v'), getpos('.'),
  2014. \ {'exclusive': v:true, 'type': "\<C-v>" }))
  2015. call cursor(1, 1)
  2016. call feedkeys("\<Esc>\<C-v>$G", 'xt')
  2017. call assert_equal(["a", "x", '', ''],
  2018. \ getregion(getpos('v'), getpos('.'),
  2019. \ {'exclusive': v:true, 'type': "\<C-v>" }))
  2020. call assert_equal([
  2021. \ [[bufnr('%'), 1, 1, 0], [bufnr('%'), 1, 1, 0]],
  2022. \ [[bufnr('%'), 2, 1, 0], [bufnr('%'), 2, 1, 0]],
  2023. \ [[bufnr('%'), 3, 0, 0], [bufnr('%'), 3, 0, 0]],
  2024. \ [[bufnr('%'), 4, 0, 0], [bufnr('%'), 4, 0, 0]],
  2025. \ ],
  2026. \ getregionpos(getpos('v'), getpos('.'),
  2027. \ {'exclusive': v:true, 'type': "\<C-v>" }))
  2028. call cursor(1, 1)
  2029. call feedkeys("\<Esc>wv2j", 'xt')
  2030. call assert_equal(["c", "x\tz"],
  2031. \ getregion(getpos('v'), getpos('.'), {'exclusive': v:true }))
  2032. call assert_equal([
  2033. \ [[bufnr('%'), 1, 3, 0], [bufnr('%'), 1, 3, 0]],
  2034. \ [[bufnr('%'), 2, 1, 0], [bufnr('%'), 2, 3, 0]],
  2035. \ ],
  2036. \ getregionpos(getpos('v'), getpos('.'), {'exclusive': v:true }))
  2037. #" 'virtualedit' with exclusive selection
  2038. set selection=exclusive
  2039. set virtualedit=all
  2040. call cursor(1, 1)
  2041. call feedkeys("\<Esc>vj", 'xt')
  2042. call assert_equal(["a\tc"],
  2043. \ getregion(getpos('v'), getpos('.'), {'type': 'v' }))
  2044. call assert_equal([
  2045. \ [[bufnr('%'), 1, 1, 0], [bufnr('%'), 1, 3, 0]],
  2046. \ ],
  2047. \ getregionpos(getpos('v'), getpos('.'), {'type': 'v' }))
  2048. call cursor(1, 1)
  2049. call feedkeys("\<Esc>v8l", 'xt')
  2050. call assert_equal(["a\t"],
  2051. \ getregion(getpos('v'), getpos('.'), {'type': 'v' }))
  2052. call assert_equal([
  2053. \ [[bufnr('%'), 1, 1, 0], [bufnr('%'), 1, 2, 0]],
  2054. \ ],
  2055. \ getregionpos(getpos('v'), getpos('.'), {'type': 'v' }))
  2056. call cursor(1, 1)
  2057. call feedkeys("\<Esc>v6l", 'xt')
  2058. call assert_equal(['a '],
  2059. \ getregion(getpos('v'), getpos('.'), {'type': 'v' }))
  2060. call assert_equal([
  2061. \ [[bufnr('%'), 1, 1, 0], [bufnr('%'), 1, 2, 5]],
  2062. \ ],
  2063. \ getregionpos(getpos('v'), getpos('.'), {'type': 'v' }))
  2064. call cursor(1, 1)
  2065. call feedkeys("\<Esc>6lv2l", 'xt')
  2066. call assert_equal([' '],
  2067. \ getregion(getpos('v'), getpos('.'), {'type': 'v' }))
  2068. call assert_equal([
  2069. \ [[bufnr('%'), 1, 2, 5], [bufnr('%'), 1, 2, 0]],
  2070. \ ],
  2071. \ getregionpos(getpos('v'), getpos('.'), {'type': 'v' }))
  2072. call cursor(1, 1)
  2073. call feedkeys("\<Esc>lv2l", 'xt')
  2074. call assert_equal([' '],
  2075. \ getregion(getpos('v'), getpos('.'), {'type': 'v' }))
  2076. call assert_equal([
  2077. \ [[bufnr('%'), 1, 2, 0], [bufnr('%'), 1, 2, 2]],
  2078. \ ],
  2079. \ getregionpos(getpos('v'), getpos('.'), {'type': 'v' }))
  2080. call cursor(1, 1)
  2081. call feedkeys("\<Esc>2lv2l", 'xt')
  2082. call assert_equal([' '],
  2083. \ getregion(getpos('v'), getpos('.'), {'type': 'v' }))
  2084. call assert_equal([
  2085. \ [[bufnr('%'), 1, 2, 1], [bufnr('%'), 1, 2, 3]],
  2086. \ ],
  2087. \ getregionpos(getpos('v'), getpos('.'), {'type': 'v' }))
  2088. call feedkeys('j', 'xt')
  2089. call assert_equal([' c', 'x '],
  2090. \ getregion(getpos('v'), getpos('.'), {'type': 'v' }))
  2091. call assert_equal([
  2092. \ [[bufnr('%'), 1, 2, 1], [bufnr('%'), 1, 3, 0]],
  2093. \ [[bufnr('%'), 2, 1, 0], [bufnr('%'), 2, 2, 3]],
  2094. \ ],
  2095. \ getregionpos(getpos('v'), getpos('.'), {'type': 'v' }))
  2096. call cursor(1, 1)
  2097. call feedkeys("\<Esc>6l\<C-v>2lj", 'xt')
  2098. call assert_equal([' ', ' '],
  2099. \ getregion(getpos('v'), getpos('.'), {'type': "\<C-v>" }))
  2100. call assert_equal([
  2101. \ [[bufnr('%'), 1, 2, 5], [bufnr('%'), 1, 2, 7]],
  2102. \ [[bufnr('%'), 2, 2, 5], [bufnr('%'), 2, 2, 7]],
  2103. \ ],
  2104. \ getregionpos(getpos('v'), getpos('.'), {'type': "\<C-v>" }))
  2105. call cursor(1, 1)
  2106. call feedkeys("\<Esc>l\<C-v>2l2j", 'xt')
  2107. call assert_equal([' ', ' ', ' '],
  2108. \ getregion(getpos('v'), getpos('.'), {'type': "\<C-v>" }))
  2109. call assert_equal([
  2110. \ [[bufnr('%'), 1, 2, 0], [bufnr('%'), 1, 2, 2]],
  2111. \ [[bufnr('%'), 2, 2, 0], [bufnr('%'), 2, 2, 2]],
  2112. \ [[bufnr('%'), 3, 0, 0], [bufnr('%'), 3, 0, 0]],
  2113. \ ],
  2114. \ getregionpos(getpos('v'), getpos('.'), {'type': "\<C-v>" }))
  2115. call assert_equal([
  2116. \ [[bufnr('%'), 1, 2, 0], [bufnr('%'), 1, 2, 2]],
  2117. \ [[bufnr('%'), 2, 2, 0], [bufnr('%'), 2, 2, 2]],
  2118. \ [[bufnr('%'), 3, 1, 1], [bufnr('%'), 3, 1, 3]],
  2119. \ ],
  2120. \ getregionpos(getpos('v'), getpos('.'),
  2121. \ {'type': "\<C-v>", "eol": v:true }))
  2122. call cursor(1, 1)
  2123. call feedkeys("\<Esc>2l\<C-v>2l2j", 'xt')
  2124. call assert_equal([' ', ' ', ' '],
  2125. \ getregion(getpos('v'), getpos('.'), {'type': "\<C-v>" }))
  2126. call assert_equal([
  2127. \ [[bufnr('%'), 1, 2, 1], [bufnr('%'), 1, 2, 3]],
  2128. \ [[bufnr('%'), 2, 2, 1], [bufnr('%'), 2, 2, 3]],
  2129. \ [[bufnr('%'), 3, 0, 0], [bufnr('%'), 3, 0, 0]],
  2130. \ ],
  2131. \ getregionpos(getpos('v'), getpos('.'), {'type': "\<C-v>" }))
  2132. call assert_equal([
  2133. \ [[bufnr('%'), 1, 2, 1], [bufnr('%'), 1, 2, 3]],
  2134. \ [[bufnr('%'), 2, 2, 1], [bufnr('%'), 2, 2, 3]],
  2135. \ [[bufnr('%'), 3, 1, 2], [bufnr('%'), 3, 1, 4]],
  2136. \ ],
  2137. \ getregionpos(getpos('v'), getpos('.'),
  2138. \ {'type': "\<C-v>", "eol": v:true }))
  2139. #" 'virtualedit' with inclusive selection
  2140. set selection&
  2141. call cursor(1, 1)
  2142. call feedkeys("\<Esc>vj", 'xt')
  2143. call assert_equal(["a\tc", 'x'],
  2144. \ getregion(getpos('v'), getpos('.'), {'type': 'v' }))
  2145. call assert_equal([
  2146. \ [[bufnr('%'), 1, 1, 0], [bufnr('%'), 1, 3, 0]],
  2147. \ [[bufnr('%'), 2, 1, 0], [bufnr('%'), 2, 1, 0]],
  2148. \ ],
  2149. \ getregionpos(getpos('v'), getpos('.'), {'type': 'v' }))
  2150. call cursor(1, 1)
  2151. call feedkeys("\<Esc>v8l", 'xt')
  2152. call assert_equal(["a\tc"],
  2153. \ getregion(getpos('v'), getpos('.'), {'type': 'v' }))
  2154. call assert_equal([
  2155. \ [[bufnr('%'), 1, 1, 0], [bufnr('%'), 1, 3, 0]],
  2156. \ ],
  2157. \ getregionpos(getpos('v'), getpos('.'), {'type': 'v' }))
  2158. call cursor(1, 1)
  2159. call feedkeys("\<Esc>v6l", 'xt')
  2160. call assert_equal(['a '],
  2161. \ getregion(getpos('v'), getpos('.'), {'type': 'v' }))
  2162. call assert_equal([
  2163. \ [[bufnr('%'), 1, 1, 0], [bufnr('%'), 1, 2, 6]],
  2164. \ ],
  2165. \ getregionpos(getpos('v'), getpos('.'), {'type': 'v' }))
  2166. call cursor(1, 1)
  2167. call feedkeys("\<Esc>6lv2l", 'xt')
  2168. call assert_equal([' c'],
  2169. \ getregion(getpos('v'), getpos('.'), {'type': 'v' }))
  2170. call assert_equal([
  2171. \ [[bufnr('%'), 1, 2, 5], [bufnr('%'), 1, 3, 0]],
  2172. \ ],
  2173. \ getregionpos(getpos('v'), getpos('.'), {'type': 'v' }))
  2174. call cursor(1, 1)
  2175. call feedkeys("\<Esc>lv2l", 'xt')
  2176. call assert_equal([' '],
  2177. \ getregion(getpos('v'), getpos('.'), {'type': 'v' }))
  2178. call assert_equal([
  2179. \ [[bufnr('%'), 1, 2, 0], [bufnr('%'), 1, 2, 3]],
  2180. \ ],
  2181. \ getregionpos(getpos('v'), getpos('.'), {'type': 'v' }))
  2182. call cursor(1, 1)
  2183. call feedkeys("\<Esc>2lv2l", 'xt')
  2184. call assert_equal([' '],
  2185. \ getregion(getpos('v'), getpos('.'), {'type': 'v' }))
  2186. call assert_equal([
  2187. \ [[bufnr('%'), 1, 2, 1], [bufnr('%'), 1, 2, 4]],
  2188. \ ],
  2189. \ getregionpos(getpos('v'), getpos('.'), {'type': 'v' }))
  2190. call feedkeys('j', 'xt')
  2191. call assert_equal([' c', 'x '],
  2192. \ getregion(getpos('v'), getpos('.'), {'type': 'v' }))
  2193. call assert_equal([
  2194. \ [[bufnr('%'), 1, 2, 1], [bufnr('%'), 1, 3, 0]],
  2195. \ [[bufnr('%'), 2, 1, 0], [bufnr('%'), 2, 2, 4]],
  2196. \ ],
  2197. \ getregionpos(getpos('v'), getpos('.'), {'type': 'v' }))
  2198. call cursor(1, 1)
  2199. call feedkeys("\<Esc>6l\<C-v>2lj", 'xt')
  2200. call assert_equal([' c', ' z'],
  2201. \ getregion(getpos('v'), getpos('.'), {'type': "\<C-v>" }))
  2202. call assert_equal([
  2203. \ [[bufnr('%'), 1, 2, 5], [bufnr('%'), 1, 3, 0]],
  2204. \ [[bufnr('%'), 2, 2, 5], [bufnr('%'), 2, 3, 0]],
  2205. \ ],
  2206. \ getregionpos(getpos('v'), getpos('.'), {'type': "\<C-v>" }))
  2207. call cursor(1, 1)
  2208. call feedkeys("\<Esc>l\<C-v>2l2j", 'xt')
  2209. call assert_equal([' ', ' ', ' '],
  2210. \ getregion(getpos('v'), getpos('.'), {'type': "\<C-v>" }))
  2211. call assert_equal([
  2212. \ [[bufnr('%'), 1, 2, 0], [bufnr('%'), 1, 2, 3]],
  2213. \ [[bufnr('%'), 2, 2, 0], [bufnr('%'), 2, 2, 3]],
  2214. \ [[bufnr('%'), 3, 0, 0], [bufnr('%'), 3, 0, 0]],
  2215. \ ],
  2216. \ getregionpos(getpos('v'), getpos('.'), {'type': "\<C-v>" }))
  2217. call assert_equal([
  2218. \ [[bufnr('%'), 1, 2, 0], [bufnr('%'), 1, 2, 3]],
  2219. \ [[bufnr('%'), 2, 2, 0], [bufnr('%'), 2, 2, 3]],
  2220. \ [[bufnr('%'), 3, 1, 1], [bufnr('%'), 3, 1, 4]],
  2221. \ ],
  2222. \ getregionpos(getpos('v'), getpos('.'),
  2223. \ {'type': "\<C-v>", "eol": v:true }))
  2224. call cursor(1, 1)
  2225. call feedkeys("\<Esc>2l\<C-v>2l2j", 'xt')
  2226. call assert_equal([' ', ' ', ' '],
  2227. \ getregion(getpos('v'), getpos('.'), {'type': "\<C-v>" }))
  2228. call assert_equal([
  2229. \ [[bufnr('%'), 1, 2, 1], [bufnr('%'), 1, 2, 4]],
  2230. \ [[bufnr('%'), 2, 2, 1], [bufnr('%'), 2, 2, 4]],
  2231. \ [[bufnr('%'), 3, 0, 0], [bufnr('%'), 3, 0, 0]],
  2232. \ ],
  2233. \ getregionpos(getpos('v'), getpos('.'), {'type': "\<C-v>" }))
  2234. call assert_equal([
  2235. \ [[bufnr('%'), 1, 2, 1], [bufnr('%'), 1, 2, 4]],
  2236. \ [[bufnr('%'), 2, 2, 1], [bufnr('%'), 2, 2, 4]],
  2237. \ [[bufnr('%'), 3, 1, 2], [bufnr('%'), 3, 1, 5]],
  2238. \ ],
  2239. \ getregionpos(getpos('v'), getpos('.'),
  2240. \ {'type': "\<C-v>", "eol": v:true }))
  2241. set virtualedit&
  2242. bwipe!
  2243. END
  2244. call CheckLegacyAndVim9Success(lines)
  2245. let lines =<< trim END
  2246. #" 'virtualedit' with TABs at end of line
  2247. new
  2248. set virtualedit=all
  2249. call setline(1, ["\t", "a\t", "aa\t"])
  2250. call feedkeys("gg06l\<C-v>3l2j", 'xt')
  2251. call assert_equal([' ', ' ', ' '],
  2252. \ getregion(getpos('v'), getpos('.'), {'type': "\<C-v>" }))
  2253. call assert_equal([
  2254. \ [[bufnr('%'), 1, 1, 6], [bufnr('%'), 1, 1, 0]],
  2255. \ [[bufnr('%'), 2, 2, 5], [bufnr('%'), 2, 2, 0]],
  2256. \ [[bufnr('%'), 3, 3, 4], [bufnr('%'), 3, 3, 0]],
  2257. \ ],
  2258. \ getregionpos(getpos('v'), getpos('.'), {'type': "\<C-v>" }))
  2259. call assert_equal([
  2260. \ [[bufnr('%'), 1, 1, 6], [bufnr('%'), 1, 2, 2]],
  2261. \ [[bufnr('%'), 2, 2, 5], [bufnr('%'), 2, 3, 2]],
  2262. \ [[bufnr('%'), 3, 3, 4], [bufnr('%'), 3, 4, 2]],
  2263. \ ],
  2264. \ getregionpos(getpos('v'), getpos('.'),
  2265. \ {'type': "\<C-v>", "eol": v:true }))
  2266. call feedkeys("gg06lv3l", 'xt')
  2267. call assert_equal([' '],
  2268. \ getregion(getpos('v'), getpos('.'), {'type': 'v' }))
  2269. call assert_equal([
  2270. \ [[bufnr('%'), 1, 1, 6], [bufnr('%'), 1, 1, 0]],
  2271. \ ],
  2272. \ getregionpos(getpos('v'), getpos('.'), {'type': 'v' }))
  2273. call assert_equal([
  2274. \ [[bufnr('%'), 1, 1, 6], [bufnr('%'), 1, 2, 2]],
  2275. \ ],
  2276. \ getregionpos(getpos('v'), getpos('.'),
  2277. \ {'type': 'v', "eol": v:true }))
  2278. set virtualedit&
  2279. bwipe!
  2280. END
  2281. call CheckLegacyAndVim9Success(lines)
  2282. endfunc
  2283. func Test_getregion_invalid_buf()
  2284. new
  2285. help
  2286. call cursor(5, 7)
  2287. norm! mA
  2288. call cursor(5, 18)
  2289. norm! mB
  2290. call assert_equal(['Move around:'], getregion(getpos("'A"), getpos("'B")))
  2291. " close the help window
  2292. q
  2293. call assert_fails("call getregion(getpos(\"'A\"), getpos(\"'B\"))", 'E681:')
  2294. bwipe!
  2295. endfunc
  2296. func Test_getregion_after_yank()
  2297. func! Check_Results(type)
  2298. call assert_equal(g:expected_region,
  2299. \ getregion(getpos("'["), getpos("']"), #{ type: a:type }))
  2300. call assert_equal(g:expected_regionpos,
  2301. \ getregionpos(getpos("'["), getpos("']"), #{ type: a:type }))
  2302. call assert_equal(g:expected_region,
  2303. \ getregion(getpos("']"), getpos("'["), #{ type: a:type }))
  2304. call assert_equal(g:expected_regionpos,
  2305. \ getregionpos(getpos("']"), getpos("'["), #{ type: a:type }))
  2306. let g:checked = 1
  2307. endfunc
  2308. autocmd TextYankPost *
  2309. \ : if v:event.operator ==? 'y'
  2310. \ | call Check_Results(v:event.regtype)
  2311. \ | endif
  2312. new
  2313. call setline(1, ['abcd', 'efghijk', 'lmn'])
  2314. let g:expected_region = ['abcd']
  2315. let g:expected_regionpos = [
  2316. \ [[bufnr('%'), 1, 1, 0], [bufnr('%'), 1, 4, 0]],
  2317. \ ]
  2318. let g:checked = 0
  2319. normal yy
  2320. call assert_equal(1, g:checked)
  2321. call Check_Results(getregtype('"'))
  2322. let g:expected_region = ['cd', 'ghijk', 'n']
  2323. let g:expected_regionpos = [
  2324. \ [[bufnr('%'), 1, 3, 0], [bufnr('%'), 1, 4, 0]],
  2325. \ [[bufnr('%'), 2, 3, 0], [bufnr('%'), 2, 7, 0]],
  2326. \ [[bufnr('%'), 3, 3, 0], [bufnr('%'), 3, 3, 0]],
  2327. \ ]
  2328. let g:checked = 0
  2329. call feedkeys("gg0ll\<C-V>jj$y", 'tx')
  2330. call assert_equal(1, g:checked)
  2331. call Check_Results(getregtype('"'))
  2332. call assert_equal(g:expected_region, getreg('"', v:true, v:true))
  2333. let g:expected_region = ['bc', 'fg', 'mn']
  2334. let g:expected_regionpos = [
  2335. \ [[bufnr('%'), 1, 2, 0], [bufnr('%'), 1, 3, 0]],
  2336. \ [[bufnr('%'), 2, 2, 0], [bufnr('%'), 2, 3, 0]],
  2337. \ [[bufnr('%'), 3, 2, 0], [bufnr('%'), 3, 3, 0]],
  2338. \ ]
  2339. let g:checked = 0
  2340. call feedkeys("gg0l\<C-V>jjly", 'tx')
  2341. call assert_equal(1, g:checked)
  2342. call Check_Results(getregtype('"'))
  2343. call assert_equal(g:expected_region, getreg('"', v:true, v:true))
  2344. bwipe!
  2345. new
  2346. let lines = ['asdfghjkl', '«口=口»', 'qwertyuiop', '口口=口口', 'zxcvbnm']
  2347. call setline(1, lines)
  2348. let g:expected_region = lines
  2349. let g:expected_regionpos = [
  2350. \ [[bufnr('%'), 1, 1, 0], [bufnr('%'), 1, 9, 0]],
  2351. \ [[bufnr('%'), 2, 1, 0], [bufnr('%'), 2, 11, 0]],
  2352. \ [[bufnr('%'), 3, 1, 0], [bufnr('%'), 3, 10, 0]],
  2353. \ [[bufnr('%'), 4, 1, 0], [bufnr('%'), 4, 13, 0]],
  2354. \ [[bufnr('%'), 5, 1, 0], [bufnr('%'), 5, 7, 0]],
  2355. \ ]
  2356. let g:checked = 0
  2357. call feedkeys('ggyG', 'tx')
  2358. call assert_equal(1, g:checked)
  2359. call Check_Results(getregtype('"'))
  2360. call assert_equal(g:expected_region, getreg('"', v:true, v:true))
  2361. let g:expected_region = ['=口»', 'qwertyuiop', '口口=口']
  2362. let g:expected_regionpos = [
  2363. \ [[bufnr('%'), 2, 6, 0], [bufnr('%'), 2, 11, 0]],
  2364. \ [[bufnr('%'), 3, 1, 0], [bufnr('%'), 3, 10, 0]],
  2365. \ [[bufnr('%'), 4, 1, 0], [bufnr('%'), 4, 10, 0]],
  2366. \ ]
  2367. let g:checked = 0
  2368. call feedkeys('2gg02lv2j2ly', 'tx')
  2369. call assert_equal(1, g:checked)
  2370. call Check_Results(getregtype('"'))
  2371. call assert_equal(g:expected_region, getreg('"', v:true, v:true))
  2372. let g:expected_region = ['asdf', '«口=', 'qwer', '口口', 'zxcv']
  2373. let g:expected_regionpos = [
  2374. \ [[bufnr('%'), 1, 1, 0], [bufnr('%'), 1, 4, 0]],
  2375. \ [[bufnr('%'), 2, 1, 0], [bufnr('%'), 2, 6, 0]],
  2376. \ [[bufnr('%'), 3, 1, 0], [bufnr('%'), 3, 4, 0]],
  2377. \ [[bufnr('%'), 4, 1, 0], [bufnr('%'), 4, 6, 0]],
  2378. \ [[bufnr('%'), 5, 1, 0], [bufnr('%'), 5, 4, 0]],
  2379. \ ]
  2380. let g:checked = 0
  2381. call feedkeys("G0\<C-V>3l4ky", 'tx')
  2382. call assert_equal(1, g:checked)
  2383. call Check_Results(getregtype('"'))
  2384. call assert_equal(g:expected_region, getreg('"', v:true, v:true))
  2385. let g:expected_region = ['ghjkl', '口»', 'tyuiop', '=口口', 'bnm']
  2386. let g:expected_regionpos = [
  2387. \ [[bufnr('%'), 1, 5, 0], [bufnr('%'), 1, 9, 0]],
  2388. \ [[bufnr('%'), 2, 7, 0], [bufnr('%'), 2, 11, 0]],
  2389. \ [[bufnr('%'), 3, 5, 0], [bufnr('%'), 3, 10, 0]],
  2390. \ [[bufnr('%'), 4, 7, 0], [bufnr('%'), 4, 13, 0]],
  2391. \ [[bufnr('%'), 5, 5, 0], [bufnr('%'), 5, 7, 0]],
  2392. \ ]
  2393. let g:checked = 0
  2394. call feedkeys("G04l\<C-V>$4ky", 'tx')
  2395. call assert_equal(1, g:checked)
  2396. call Check_Results(getregtype('"'))
  2397. call assert_equal(g:expected_region, getreg('"', v:true, v:true))
  2398. bwipe!
  2399. unlet g:expected_region
  2400. unlet g:expected_regionpos
  2401. unlet g:checked
  2402. autocmd! TextYankPost
  2403. delfunc Check_Results
  2404. endfunc
  2405. func Test_visual_block_cursor_delete()
  2406. new
  2407. call setline(1, 'ab')
  2408. exe ":norm! $\<c-v>hI\<Del>\<ESC>"
  2409. call assert_equal(['b'], getline(1, 1))
  2410. bwipe!
  2411. endfunc
  2412. func Test_visual_block_cursor_insert_enter()
  2413. new
  2414. call setline(1, ['asdf asdf', 'asdf asdf', 'asdf asdf', 'asdf asdf'])
  2415. call cursor(1, 5)
  2416. exe ":norm! \<c-v>3jcw\<cr>"
  2417. call assert_equal(['asdfw', 'asdf', 'asdfasdf', 'asdfasdf', 'asdfasdf'], getline(1, '$'))
  2418. bwipe!
  2419. endfunc
  2420. " vim: shiftwidth=2 sts=2 expandtab