tui_spec.lua 130 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591
  1. -- TUI acceptance tests.
  2. -- Uses :terminal as a way to send keys and assert screen state.
  3. --
  4. -- "bracketed paste" terminal feature:
  5. -- http://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h2-Bracketed-Paste-Mode
  6. local t = require('test.testutil')
  7. local n = require('test.functional.testnvim')()
  8. local Screen = require('test.functional.ui.screen')
  9. local tt = require('test.functional.testterm')
  10. local eq = t.eq
  11. local feed_data = tt.feed_data
  12. local clear = n.clear
  13. local command = n.command
  14. local exec = n.exec
  15. local exec_lua = n.exec_lua
  16. local testprg = n.testprg
  17. local retry = t.retry
  18. local nvim_prog = n.nvim_prog
  19. local nvim_set = n.nvim_set
  20. local ok = t.ok
  21. local read_file = t.read_file
  22. local fn = n.fn
  23. local api = n.api
  24. local is_ci = t.is_ci
  25. local is_os = t.is_os
  26. local new_pipename = n.new_pipename
  27. local set_session = n.set_session
  28. local write_file = t.write_file
  29. local eval = n.eval
  30. local assert_log = t.assert_log
  31. local testlog = 'Xtest-tui-log'
  32. if t.skip(is_os('win')) then
  33. return
  34. end
  35. describe('TUI', function()
  36. local screen --[[@type test.functional.ui.screen]]
  37. local child_session --[[@type test.Session]]
  38. local child_exec_lua
  39. before_each(function()
  40. clear()
  41. local child_server = new_pipename()
  42. screen = tt.setup_child_nvim({
  43. '--listen',
  44. child_server,
  45. '-u',
  46. 'NONE',
  47. '-i',
  48. 'NONE',
  49. '--cmd',
  50. nvim_set .. ' notermguicolors laststatus=2 background=dark',
  51. '--cmd',
  52. 'colorscheme vim',
  53. })
  54. screen:expect([[
  55. ^ |
  56. {4:~ }|*3
  57. {5:[No Name] }|
  58. |
  59. {3:-- TERMINAL --} |
  60. ]])
  61. child_session = n.connect(child_server)
  62. child_exec_lua = tt.make_lua_executor(child_session)
  63. end)
  64. -- Wait for mode in the child Nvim (avoid "typeahead race" #10826).
  65. local function wait_for_mode(mode)
  66. retry(nil, nil, function()
  67. local _, m = child_session:request('nvim_get_mode')
  68. eq(mode, m.mode)
  69. end)
  70. end
  71. -- Assert buffer contents in the child Nvim.
  72. local function expect_child_buf_lines(expected)
  73. assert(type({}) == type(expected))
  74. retry(nil, nil, function()
  75. local _, buflines = child_session:request('nvim_buf_get_lines', 0, 0, -1, false)
  76. eq(expected, buflines)
  77. end)
  78. end
  79. -- Ensure both child client and child server have processed pending events.
  80. local function poke_both_eventloop()
  81. child_exec_lua([[
  82. _G.termresponse = nil
  83. vim.api.nvim_create_autocmd('TermResponse', {
  84. once = true,
  85. callback = function(ev) _G.termresponse = ev.data end,
  86. })
  87. ]])
  88. feed_data('\027P0$r\027\\')
  89. retry(nil, nil, function()
  90. eq('\027P0$r', child_exec_lua('return _G.termresponse'))
  91. end)
  92. end
  93. it('rapid resize #7572 #7628', function()
  94. -- Need buffer rows to provoke the behavior.
  95. feed_data(':edit test/functional/fixtures/bigfile.txt\n')
  96. screen:expect([[
  97. ^0000;<control>;Cc;0;BN;;;;;N;NULL;;;; |
  98. 0001;<control>;Cc;0;BN;;;;;N;START OF HEADING;;;; |
  99. 0002;<control>;Cc;0;BN;;;;;N;START OF TEXT;;;; |
  100. 0003;<control>;Cc;0;BN;;;;;N;END OF TEXT;;;; |
  101. {5:test/functional/fixtures/bigfile.txt }|
  102. :edit test/functional/fixtures/bigfile.txt |
  103. {3:-- TERMINAL --} |
  104. ]])
  105. command('call jobresize(b:terminal_job_id, 58, 9)')
  106. command('call jobresize(b:terminal_job_id, 62, 13)')
  107. command('call jobresize(b:terminal_job_id, 100, 42)')
  108. command('call jobresize(b:terminal_job_id, 37, 1000)')
  109. -- Resize to <5 columns.
  110. screen:try_resize(4, 44)
  111. command('call jobresize(b:terminal_job_id, 4, 1000)')
  112. -- Resize to 1 row, then to 1 column, then increase rows to 4.
  113. screen:try_resize(44, 1)
  114. command('call jobresize(b:terminal_job_id, 44, 1)')
  115. screen:try_resize(1, 1)
  116. command('call jobresize(b:terminal_job_id, 1, 1)')
  117. screen:try_resize(1, 4)
  118. command('call jobresize(b:terminal_job_id, 1, 4)')
  119. screen:try_resize(57, 17)
  120. command('call jobresize(b:terminal_job_id, 57, 17)')
  121. retry(nil, nil, function()
  122. eq({ true, 57 }, { child_session:request('nvim_win_get_width', 0) })
  123. end)
  124. end)
  125. it('accepts resize while pager is active', function()
  126. child_session:request(
  127. 'nvim_exec2',
  128. [[
  129. set more
  130. func! ManyErr()
  131. for i in range(20)
  132. echoerr "FAIL ".i
  133. endfor
  134. endfunc
  135. ]],
  136. {}
  137. )
  138. feed_data(':call ManyErr()\r')
  139. screen:expect {
  140. grid = [[
  141. {8:Error detected while processing function ManyErr:} |
  142. {11:line 2:} |
  143. {8:FAIL 0} |
  144. {8:FAIL 1} |
  145. {8:FAIL 2} |
  146. {10:-- More --}^ |
  147. {3:-- TERMINAL --} |
  148. ]],
  149. }
  150. screen:try_resize(50, 10)
  151. screen:expect {
  152. grid = [[
  153. :call ManyErr() |
  154. {8:Error detected while processing function ManyErr:} |
  155. {11:line 2:} |
  156. {8:FAIL 0} |
  157. {8:FAIL 1} |
  158. {8:FAIL 2} |
  159. |*2
  160. {10:-- More --}^ |
  161. {3:-- TERMINAL --} |
  162. ]],
  163. }
  164. feed_data('j')
  165. screen:expect {
  166. grid = [[
  167. {8:Error detected while processing function ManyErr:} |
  168. {11:line 2:} |
  169. {8:FAIL 0} |
  170. {8:FAIL 1} |
  171. {8:FAIL 2} |
  172. {8:FAIL 3} |
  173. {8:FAIL 4} |
  174. {8:FAIL 5} |
  175. {10:-- More --}^ |
  176. {3:-- TERMINAL --} |
  177. ]],
  178. }
  179. screen:try_resize(50, 7)
  180. screen:expect {
  181. grid = [[
  182. {8:FAIL 1} |
  183. {8:FAIL 2} |
  184. {8:FAIL 3} |
  185. {8:FAIL 4} |
  186. {8:FAIL 5} |
  187. {10:-- More --}^ |
  188. {3:-- TERMINAL --} |
  189. ]],
  190. }
  191. screen:try_resize(50, 5)
  192. screen:expect {
  193. grid = [[
  194. {8:FAIL 3} |
  195. {8:FAIL 4} |
  196. {8:FAIL 5} |
  197. {10:-- More --}^ |
  198. {3:-- TERMINAL --} |
  199. ]],
  200. }
  201. feed_data('g')
  202. screen:expect {
  203. grid = [[
  204. :call ManyErr() |
  205. {8:Error detected while processing function ManyErr:} |
  206. {11:line 2:} |
  207. {10:-- More --}^ |
  208. {3:-- TERMINAL --} |
  209. ]],
  210. }
  211. screen:try_resize(50, 10)
  212. screen:expect {
  213. grid = [[
  214. :call ManyErr() |
  215. {8:Error detected while processing function ManyErr:} |
  216. {11:line 2:} |
  217. {8:FAIL 0} |
  218. {8:FAIL 1} |
  219. {8:FAIL 2} |
  220. {8:FAIL 3} |
  221. {8:FAIL 4} |
  222. {10:-- More --}^ |
  223. {3:-- TERMINAL --} |
  224. ]],
  225. }
  226. feed_data('\003')
  227. screen:expect {
  228. grid = [[
  229. ^ |
  230. {4:~ }|*6
  231. {5:[No Name] }|
  232. |
  233. {3:-- TERMINAL --} |
  234. ]],
  235. }
  236. end)
  237. it('accepts basic utf-8 input', function()
  238. feed_data('iabc\ntest1\ntest2')
  239. screen:expect([[
  240. abc |
  241. test1 |
  242. test2^ |
  243. {4:~ }|
  244. {5:[No Name] [+] }|
  245. {3:-- INSERT --} |
  246. {3:-- TERMINAL --} |
  247. ]])
  248. feed_data('\027')
  249. screen:expect([[
  250. abc |
  251. test1 |
  252. test^2 |
  253. {4:~ }|
  254. {5:[No Name] [+] }|
  255. |
  256. {3:-- TERMINAL --} |
  257. ]])
  258. end)
  259. it('interprets leading <Esc> byte as ALT modifier in normal-mode', function()
  260. local keys = 'dfghjkl'
  261. for c in keys:gmatch('.') do
  262. feed_data(':nnoremap <a-' .. c .. '> ialt-' .. c .. '<cr><esc>\r')
  263. feed_data('\027' .. c)
  264. end
  265. screen:expect([[
  266. alt-j |
  267. alt-k |
  268. alt-l |
  269. ^ |
  270. {5:[No Name] [+] }|
  271. |
  272. {3:-- TERMINAL --} |
  273. ]])
  274. feed_data('gg')
  275. screen:expect([[
  276. ^alt-d |
  277. alt-f |
  278. alt-g |
  279. alt-h |
  280. {5:[No Name] [+] }|
  281. |
  282. {3:-- TERMINAL --} |
  283. ]])
  284. end)
  285. it('interprets ESC+key as ALT chord in i_CTRL-V', function()
  286. -- Vim represents ALT/META by setting the "high bit" of the modified key:
  287. -- ALT+j inserts "ê". Nvim does not (#3982).
  288. feed_data('i\022\027j')
  289. screen:expect([[
  290. <M-j>^ |
  291. {4:~ }|*3
  292. {5:[No Name] [+] }|
  293. {3:-- INSERT --} |
  294. {3:-- TERMINAL --} |
  295. ]])
  296. end)
  297. it('interprets <Esc>[27u as <Esc>', function()
  298. child_session:request(
  299. 'nvim_exec2',
  300. [[
  301. nnoremap <M-;> <Nop>
  302. nnoremap <Esc> AESC<Esc>
  303. nnoremap ; Asemicolon<Esc>
  304. ]],
  305. {}
  306. )
  307. feed_data('\027[27u;')
  308. screen:expect([[
  309. ESCsemicolo^n |
  310. {4:~ }|*3
  311. {5:[No Name] [+] }|
  312. |
  313. {3:-- TERMINAL --} |
  314. ]])
  315. -- <Esc>; should be recognized as <M-;> when <M-;> is mapped
  316. feed_data('\027;')
  317. screen:expect_unchanged()
  318. end)
  319. it('interprets <Esc><Nul> as <M-C-Space> #17198', function()
  320. feed_data('i\022\027\000')
  321. screen:expect([[
  322. <M-C-Space>^ |
  323. {4:~ }|*3
  324. {5:[No Name] [+] }|
  325. {3:-- INSERT --} |
  326. {3:-- TERMINAL --} |
  327. ]])
  328. end)
  329. it('accepts ASCII control sequences', function()
  330. feed_data('i')
  331. feed_data('\022\007') -- ctrl+g
  332. feed_data('\022\022') -- ctrl+v
  333. feed_data('\022\013') -- ctrl+m
  334. screen:expect([[
  335. {6:^G^V^M}^ |
  336. {4:~ }|*3
  337. {5:[No Name] [+] }|
  338. {3:-- INSERT --} |
  339. {3:-- TERMINAL --} |
  340. ]])
  341. end)
  342. local function test_mouse_wheel(esc)
  343. child_session:request(
  344. 'nvim_exec2',
  345. [[
  346. set number nostartofline nowrap mousescroll=hor:1,ver:1
  347. call setline(1, repeat([join(range(10), '----')], 10))
  348. vsplit
  349. ]],
  350. {}
  351. )
  352. screen:expect([[
  353. {11: 1 }^0----1----2----3----4│{11: 1 }0----1----2----3----|
  354. {11: 2 }0----1----2----3----4│{11: 2 }0----1----2----3----|
  355. {11: 3 }0----1----2----3----4│{11: 3 }0----1----2----3----|
  356. {11: 4 }0----1----2----3----4│{11: 4 }0----1----2----3----|
  357. {5:[No Name] [+] }{1:[No Name] [+] }|
  358. |
  359. {3:-- TERMINAL --} |
  360. ]])
  361. -- <ScrollWheelDown> in active window
  362. if esc then
  363. feed_data('\027[<65;8;1M')
  364. else
  365. api.nvim_input_mouse('wheel', 'down', '', 0, 0, 7)
  366. end
  367. screen:expect([[
  368. {11: 2 }^0----1----2----3----4│{11: 1 }0----1----2----3----|
  369. {11: 3 }0----1----2----3----4│{11: 2 }0----1----2----3----|
  370. {11: 4 }0----1----2----3----4│{11: 3 }0----1----2----3----|
  371. {11: 5 }0----1----2----3----4│{11: 4 }0----1----2----3----|
  372. {5:[No Name] [+] }{1:[No Name] [+] }|
  373. |
  374. {3:-- TERMINAL --} |
  375. ]])
  376. -- <ScrollWheelDown> in inactive window
  377. if esc then
  378. feed_data('\027[<65;48;1M')
  379. else
  380. api.nvim_input_mouse('wheel', 'down', '', 0, 0, 47)
  381. end
  382. screen:expect([[
  383. {11: 2 }^0----1----2----3----4│{11: 2 }0----1----2----3----|
  384. {11: 3 }0----1----2----3----4│{11: 3 }0----1----2----3----|
  385. {11: 4 }0----1----2----3----4│{11: 4 }0----1----2----3----|
  386. {11: 5 }0----1----2----3----4│{11: 5 }0----1----2----3----|
  387. {5:[No Name] [+] }{1:[No Name] [+] }|
  388. |
  389. {3:-- TERMINAL --} |
  390. ]])
  391. -- <ScrollWheelRight> in active window
  392. if esc then
  393. feed_data('\027[<67;8;1M')
  394. else
  395. api.nvim_input_mouse('wheel', 'right', '', 0, 0, 7)
  396. end
  397. screen:expect([[
  398. {11: 2 }^----1----2----3----4-│{11: 2 }0----1----2----3----|
  399. {11: 3 }----1----2----3----4-│{11: 3 }0----1----2----3----|
  400. {11: 4 }----1----2----3----4-│{11: 4 }0----1----2----3----|
  401. {11: 5 }----1----2----3----4-│{11: 5 }0----1----2----3----|
  402. {5:[No Name] [+] }{1:[No Name] [+] }|
  403. |
  404. {3:-- TERMINAL --} |
  405. ]])
  406. -- <ScrollWheelRight> in inactive window
  407. if esc then
  408. feed_data('\027[<67;48;1M')
  409. else
  410. api.nvim_input_mouse('wheel', 'right', '', 0, 0, 47)
  411. end
  412. screen:expect([[
  413. {11: 2 }^----1----2----3----4-│{11: 2 }----1----2----3----4|
  414. {11: 3 }----1----2----3----4-│{11: 3 }----1----2----3----4|
  415. {11: 4 }----1----2----3----4-│{11: 4 }----1----2----3----4|
  416. {11: 5 }----1----2----3----4-│{11: 5 }----1----2----3----4|
  417. {5:[No Name] [+] }{1:[No Name] [+] }|
  418. |
  419. {3:-- TERMINAL --} |
  420. ]])
  421. -- <S-ScrollWheelDown> in active window
  422. if esc then
  423. feed_data('\027[<69;8;1M')
  424. else
  425. api.nvim_input_mouse('wheel', 'down', 'S', 0, 0, 7)
  426. end
  427. screen:expect([[
  428. {11: 5 }^----1----2----3----4-│{11: 2 }----1----2----3----4|
  429. {11: 6 }----1----2----3----4-│{11: 3 }----1----2----3----4|
  430. {11: 7 }----1----2----3----4-│{11: 4 }----1----2----3----4|
  431. {11: 8 }----1----2----3----4-│{11: 5 }----1----2----3----4|
  432. {5:[No Name] [+] }{1:[No Name] [+] }|
  433. |
  434. {3:-- TERMINAL --} |
  435. ]])
  436. -- <S-ScrollWheelDown> in inactive window
  437. if esc then
  438. feed_data('\027[<69;48;1M')
  439. else
  440. api.nvim_input_mouse('wheel', 'down', 'S', 0, 0, 47)
  441. end
  442. screen:expect([[
  443. {11: 5 }^----1----2----3----4-│{11: 5 }----1----2----3----4|
  444. {11: 6 }----1----2----3----4-│{11: 6 }----1----2----3----4|
  445. {11: 7 }----1----2----3----4-│{11: 7 }----1----2----3----4|
  446. {11: 8 }----1----2----3----4-│{11: 8 }----1----2----3----4|
  447. {5:[No Name] [+] }{1:[No Name] [+] }|
  448. |
  449. {3:-- TERMINAL --} |
  450. ]])
  451. -- <S-ScrollWheelRight> in active window
  452. if esc then
  453. feed_data('\027[<71;8;1M')
  454. else
  455. api.nvim_input_mouse('wheel', 'right', 'S', 0, 0, 7)
  456. end
  457. screen:expect([[
  458. {11: 5 }^----6----7----8----9 │{11: 5 }----1----2----3----4|
  459. {11: 6 }----6----7----8----9 │{11: 6 }----1----2----3----4|
  460. {11: 7 }----6----7----8----9 │{11: 7 }----1----2----3----4|
  461. {11: 8 }----6----7----8----9 │{11: 8 }----1----2----3----4|
  462. {5:[No Name] [+] }{1:[No Name] [+] }|
  463. |
  464. {3:-- TERMINAL --} |
  465. ]])
  466. -- <S-ScrollWheelRight> in inactive window
  467. if esc then
  468. feed_data('\027[<71;48;1M')
  469. else
  470. api.nvim_input_mouse('wheel', 'right', 'S', 0, 0, 47)
  471. end
  472. screen:expect([[
  473. {11: 5 }^----6----7----8----9 │{11: 5 }5----6----7----8----|
  474. {11: 6 }----6----7----8----9 │{11: 6 }5----6----7----8----|
  475. {11: 7 }----6----7----8----9 │{11: 7 }5----6----7----8----|
  476. {11: 8 }----6----7----8----9 │{11: 8 }5----6----7----8----|
  477. {5:[No Name] [+] }{1:[No Name] [+] }|
  478. |
  479. {3:-- TERMINAL --} |
  480. ]])
  481. -- <ScrollWheelUp> in active window
  482. if esc then
  483. feed_data('\027[<64;8;1M')
  484. else
  485. api.nvim_input_mouse('wheel', 'up', '', 0, 0, 7)
  486. end
  487. screen:expect([[
  488. {11: 4 }----6----7----8----9 │{11: 5 }5----6----7----8----|
  489. {11: 5 }^----6----7----8----9 │{11: 6 }5----6----7----8----|
  490. {11: 6 }----6----7----8----9 │{11: 7 }5----6----7----8----|
  491. {11: 7 }----6----7----8----9 │{11: 8 }5----6----7----8----|
  492. {5:[No Name] [+] }{1:[No Name] [+] }|
  493. |
  494. {3:-- TERMINAL --} |
  495. ]])
  496. -- <ScrollWheelUp> in inactive window
  497. if esc then
  498. feed_data('\027[<64;48;1M')
  499. else
  500. api.nvim_input_mouse('wheel', 'up', '', 0, 0, 47)
  501. end
  502. screen:expect([[
  503. {11: 4 }----6----7----8----9 │{11: 4 }5----6----7----8----|
  504. {11: 5 }^----6----7----8----9 │{11: 5 }5----6----7----8----|
  505. {11: 6 }----6----7----8----9 │{11: 6 }5----6----7----8----|
  506. {11: 7 }----6----7----8----9 │{11: 7 }5----6----7----8----|
  507. {5:[No Name] [+] }{1:[No Name] [+] }|
  508. |
  509. {3:-- TERMINAL --} |
  510. ]])
  511. -- <ScrollWheelLeft> in active window
  512. if esc then
  513. feed_data('\027[<66;8;1M')
  514. else
  515. api.nvim_input_mouse('wheel', 'left', '', 0, 0, 7)
  516. end
  517. screen:expect([[
  518. {11: 4 }5----6----7----8----9│{11: 4 }5----6----7----8----|
  519. {11: 5 }5^----6----7----8----9│{11: 5 }5----6----7----8----|
  520. {11: 6 }5----6----7----8----9│{11: 6 }5----6----7----8----|
  521. {11: 7 }5----6----7----8----9│{11: 7 }5----6----7----8----|
  522. {5:[No Name] [+] }{1:[No Name] [+] }|
  523. |
  524. {3:-- TERMINAL --} |
  525. ]])
  526. -- <ScrollWheelLeft> in inactive window
  527. if esc then
  528. feed_data('\027[<66;48;1M')
  529. else
  530. api.nvim_input_mouse('wheel', 'left', '', 0, 0, 47)
  531. end
  532. screen:expect([[
  533. {11: 4 }5----6----7----8----9│{11: 4 }-5----6----7----8---|
  534. {11: 5 }5^----6----7----8----9│{11: 5 }-5----6----7----8---|
  535. {11: 6 }5----6----7----8----9│{11: 6 }-5----6----7----8---|
  536. {11: 7 }5----6----7----8----9│{11: 7 }-5----6----7----8---|
  537. {5:[No Name] [+] }{1:[No Name] [+] }|
  538. |
  539. {3:-- TERMINAL --} |
  540. ]])
  541. -- <S-ScrollWheelUp> in active window
  542. if esc then
  543. feed_data('\027[<68;8;1M')
  544. else
  545. api.nvim_input_mouse('wheel', 'up', 'S', 0, 0, 7)
  546. end
  547. screen:expect([[
  548. {11: 1 }5----6----7----8----9│{11: 4 }-5----6----7----8---|
  549. {11: 2 }5----6----7----8----9│{11: 5 }-5----6----7----8---|
  550. {11: 3 }5----6----7----8----9│{11: 6 }-5----6----7----8---|
  551. {11: 4 }5^----6----7----8----9│{11: 7 }-5----6----7----8---|
  552. {5:[No Name] [+] }{1:[No Name] [+] }|
  553. |
  554. {3:-- TERMINAL --} |
  555. ]])
  556. -- <S-ScrollWheelUp> in inactive window
  557. if esc then
  558. feed_data('\027[<68;48;1M')
  559. else
  560. api.nvim_input_mouse('wheel', 'up', 'S', 0, 0, 47)
  561. end
  562. screen:expect([[
  563. {11: 1 }5----6----7----8----9│{11: 1 }-5----6----7----8---|
  564. {11: 2 }5----6----7----8----9│{11: 2 }-5----6----7----8---|
  565. {11: 3 }5----6----7----8----9│{11: 3 }-5----6----7----8---|
  566. {11: 4 }5^----6----7----8----9│{11: 4 }-5----6----7----8---|
  567. {5:[No Name] [+] }{1:[No Name] [+] }|
  568. |
  569. {3:-- TERMINAL --} |
  570. ]])
  571. -- <S-ScrollWheelLeft> in active window
  572. if esc then
  573. feed_data('\027[<70;8;1M')
  574. else
  575. api.nvim_input_mouse('wheel', 'left', 'S', 0, 0, 7)
  576. end
  577. screen:expect([[
  578. {11: 1 }0----1----2----3----4│{11: 1 }-5----6----7----8---|
  579. {11: 2 }0----1----2----3----4│{11: 2 }-5----6----7----8---|
  580. {11: 3 }0----1----2----3----4│{11: 3 }-5----6----7----8---|
  581. {11: 4 }0----1----2----3----^4│{11: 4 }-5----6----7----8---|
  582. {5:[No Name] [+] }{1:[No Name] [+] }|
  583. |
  584. {3:-- TERMINAL --} |
  585. ]])
  586. -- <S-ScrollWheelLeft> in inactive window
  587. if esc then
  588. feed_data('\027[<70;48;1M')
  589. else
  590. api.nvim_input_mouse('wheel', 'left', 'S', 0, 0, 47)
  591. end
  592. screen:expect([[
  593. {11: 1 }0----1----2----3----4│{11: 1 }0----1----2----3----|
  594. {11: 2 }0----1----2----3----4│{11: 2 }0----1----2----3----|
  595. {11: 3 }0----1----2----3----4│{11: 3 }0----1----2----3----|
  596. {11: 4 }0----1----2----3----^4│{11: 4 }0----1----2----3----|
  597. {5:[No Name] [+] }{1:[No Name] [+] }|
  598. |
  599. {3:-- TERMINAL --} |
  600. ]])
  601. end
  602. describe('accepts mouse wheel events', function()
  603. it('(mouse events sent to host)', function()
  604. test_mouse_wheel(false)
  605. end)
  606. it('(escape sequences sent to child)', function()
  607. test_mouse_wheel(true)
  608. end)
  609. end)
  610. local function test_mouse_popup(esc)
  611. child_session:request(
  612. 'nvim_exec2',
  613. [[
  614. call setline(1, 'popup menu test')
  615. set mouse=a mousemodel=popup
  616. aunmenu PopUp
  617. " Delete the default MenuPopup event handler.
  618. autocmd! nvim.popupmenu
  619. menu PopUp.foo :let g:menustr = 'foo'<CR>
  620. menu PopUp.bar :let g:menustr = 'bar'<CR>
  621. menu PopUp.baz :let g:menustr = 'baz'<CR>
  622. highlight Pmenu ctermbg=NONE ctermfg=NONE cterm=underline,reverse
  623. highlight PmenuSel ctermbg=NONE ctermfg=NONE cterm=underline,reverse,bold
  624. ]],
  625. {}
  626. )
  627. if esc then
  628. feed_data('\027[<2;5;1M')
  629. else
  630. api.nvim_input_mouse('right', 'press', '', 0, 0, 4)
  631. end
  632. screen:expect([[
  633. ^popup menu test |
  634. {4:~ }{13: foo }{4: }|
  635. {4:~ }{13: bar }{4: }|
  636. {4:~ }{13: baz }{4: }|
  637. {5:[No Name] [+] }|
  638. |
  639. {3:-- TERMINAL --} |
  640. ]])
  641. if esc then
  642. feed_data('\027[<2;5;1m')
  643. else
  644. api.nvim_input_mouse('right', 'release', '', 0, 0, 4)
  645. end
  646. screen:expect_unchanged()
  647. if esc then
  648. feed_data('\027[<64;5;1M')
  649. else
  650. api.nvim_input_mouse('wheel', 'up', '', 0, 0, 4)
  651. end
  652. screen:expect([[
  653. ^popup menu test |
  654. {4:~ }{14: foo }{4: }|
  655. {4:~ }{13: bar }{4: }|
  656. {4:~ }{13: baz }{4: }|
  657. {5:[No Name] [+] }|
  658. |
  659. {3:-- TERMINAL --} |
  660. ]])
  661. if esc then
  662. feed_data('\027[<35;7;4M')
  663. else
  664. api.nvim_input_mouse('move', '', '', 0, 3, 6)
  665. end
  666. screen:expect([[
  667. ^popup menu test |
  668. {4:~ }{13: foo }{4: }|
  669. {4:~ }{13: bar }{4: }|
  670. {4:~ }{14: baz }{4: }|
  671. {5:[No Name] [+] }|
  672. |
  673. {3:-- TERMINAL --} |
  674. ]])
  675. if esc then
  676. feed_data('\027[<65;7;4M')
  677. else
  678. api.nvim_input_mouse('wheel', 'down', '', 0, 3, 6)
  679. end
  680. screen:expect([[
  681. ^popup menu test |
  682. {4:~ }{13: foo }{4: }|
  683. {4:~ }{14: bar }{4: }|
  684. {4:~ }{13: baz }{4: }|
  685. {5:[No Name] [+] }|
  686. |
  687. {3:-- TERMINAL --} |
  688. ]])
  689. if esc then
  690. feed_data('\027[<0;7;3M')
  691. else
  692. api.nvim_input_mouse('left', 'press', '', 0, 2, 6)
  693. end
  694. screen:expect([[
  695. ^popup menu test |
  696. {4:~ }|*3
  697. {5:[No Name] [+] }|
  698. :let g:menustr = 'bar' |
  699. {3:-- TERMINAL --} |
  700. ]])
  701. if esc then
  702. feed_data('\027[<0;7;3m')
  703. else
  704. api.nvim_input_mouse('left', 'release', '', 0, 2, 6)
  705. end
  706. screen:expect_unchanged()
  707. if esc then
  708. feed_data('\027[<2;45;3M')
  709. else
  710. api.nvim_input_mouse('right', 'press', '', 0, 2, 44)
  711. end
  712. screen:expect([[
  713. ^popup menu test |
  714. {4:~ }|*2
  715. {4:~ }{13: foo }{4: }|
  716. {5:[No Name] [+] }{13: bar }{5: }|
  717. :let g:menustr = 'bar' {13: baz } |
  718. {3:-- TERMINAL --} |
  719. ]])
  720. if esc then
  721. feed_data('\027[<34;48;6M')
  722. else
  723. api.nvim_input_mouse('right', 'drag', '', 0, 5, 47)
  724. end
  725. screen:expect([[
  726. ^popup menu test |
  727. {4:~ }|*2
  728. {4:~ }{13: foo }{4: }|
  729. {5:[No Name] [+] }{13: bar }{5: }|
  730. :let g:menustr = 'bar' {14: baz } |
  731. {3:-- TERMINAL --} |
  732. ]])
  733. if esc then
  734. feed_data('\027[<2;48;6m')
  735. else
  736. api.nvim_input_mouse('right', 'release', '', 0, 5, 47)
  737. end
  738. screen:expect([[
  739. ^popup menu test |
  740. {4:~ }|*3
  741. {5:[No Name] [+] }|
  742. :let g:menustr = 'baz' |
  743. {3:-- TERMINAL --} |
  744. ]])
  745. end
  746. describe('mouse events work with right-click menu', function()
  747. it('(mouse events sent to host)', function()
  748. test_mouse_popup(false)
  749. end)
  750. it('(escape sequences sent to child)', function()
  751. test_mouse_popup(true)
  752. end)
  753. end)
  754. it('accepts keypad keys from kitty keyboard protocol #19180', function()
  755. feed_data('i')
  756. feed_data(fn.nr2char(57399)) -- KP_0
  757. feed_data(fn.nr2char(57400)) -- KP_1
  758. feed_data(fn.nr2char(57401)) -- KP_2
  759. feed_data(fn.nr2char(57402)) -- KP_3
  760. feed_data(fn.nr2char(57403)) -- KP_4
  761. feed_data(fn.nr2char(57404)) -- KP_5
  762. feed_data(fn.nr2char(57405)) -- KP_6
  763. feed_data(fn.nr2char(57406)) -- KP_7
  764. feed_data(fn.nr2char(57407)) -- KP_8
  765. feed_data(fn.nr2char(57408)) -- KP_9
  766. feed_data(fn.nr2char(57409)) -- KP_DECIMAL
  767. feed_data(fn.nr2char(57410)) -- KP_DIVIDE
  768. feed_data(fn.nr2char(57411)) -- KP_MULTIPLY
  769. feed_data(fn.nr2char(57412)) -- KP_SUBTRACT
  770. feed_data(fn.nr2char(57413)) -- KP_ADD
  771. feed_data(fn.nr2char(57414)) -- KP_ENTER
  772. feed_data(fn.nr2char(57415)) -- KP_EQUAL
  773. screen:expect([[
  774. 0123456789./*-+ |
  775. =^ |
  776. {4:~ }|*2
  777. {5:[No Name] [+] }|
  778. {3:-- INSERT --} |
  779. {3:-- TERMINAL --} |
  780. ]])
  781. feed_data(fn.nr2char(57417)) -- KP_LEFT
  782. screen:expect([[
  783. 0123456789./*-+ |
  784. ^= |
  785. {4:~ }|*2
  786. {5:[No Name] [+] }|
  787. {3:-- INSERT --} |
  788. {3:-- TERMINAL --} |
  789. ]])
  790. feed_data(fn.nr2char(57418)) -- KP_RIGHT
  791. screen:expect([[
  792. 0123456789./*-+ |
  793. =^ |
  794. {4:~ }|*2
  795. {5:[No Name] [+] }|
  796. {3:-- INSERT --} |
  797. {3:-- TERMINAL --} |
  798. ]])
  799. feed_data(fn.nr2char(57419)) -- KP_UP
  800. screen:expect([[
  801. 0^123456789./*-+ |
  802. = |
  803. {4:~ }|*2
  804. {5:[No Name] [+] }|
  805. {3:-- INSERT --} |
  806. {3:-- TERMINAL --} |
  807. ]])
  808. feed_data(fn.nr2char(57420)) -- KP_DOWN
  809. screen:expect([[
  810. 0123456789./*-+ |
  811. =^ |
  812. {4:~ }|*2
  813. {5:[No Name] [+] }|
  814. {3:-- INSERT --} |
  815. {3:-- TERMINAL --} |
  816. ]])
  817. feed_data(fn.nr2char(57425)) -- KP_INSERT
  818. screen:expect([[
  819. 0123456789./*-+ |
  820. =^ |
  821. {4:~ }|*2
  822. {5:[No Name] [+] }|
  823. {3:-- REPLACE --} |
  824. {3:-- TERMINAL --} |
  825. ]])
  826. feed_data('\027[27u') -- ESC
  827. screen:expect([[
  828. 0123456789./*-+ |
  829. ^= |
  830. {4:~ }|*2
  831. {5:[No Name] [+] }|
  832. |
  833. {3:-- TERMINAL --} |
  834. ]])
  835. feed_data('\027[57417;5u') -- CTRL + KP_LEFT
  836. screen:expect([[
  837. ^0123456789./*-+ |
  838. = |
  839. {4:~ }|*2
  840. {5:[No Name] [+] }|
  841. |
  842. {3:-- TERMINAL --} |
  843. ]])
  844. feed_data('\027[57418;2u') -- SHIFT + KP_RIGHT
  845. screen:expect([[
  846. 0123456789^./*-+ |
  847. = |
  848. {4:~ }|*2
  849. {5:[No Name] [+] }|
  850. |
  851. {3:-- TERMINAL --} |
  852. ]])
  853. feed_data(fn.nr2char(57426)) -- KP_DELETE
  854. screen:expect([[
  855. 0123456789^/*-+ |
  856. = |
  857. {4:~ }|*2
  858. {5:[No Name] [+] }|
  859. |
  860. {3:-- TERMINAL --} |
  861. ]])
  862. feed_data(fn.nr2char(57423)) -- KP_HOME
  863. screen:expect([[
  864. ^0123456789/*-+ |
  865. = |
  866. {4:~ }|*2
  867. {5:[No Name] [+] }|
  868. |
  869. {3:-- TERMINAL --} |
  870. ]])
  871. feed_data(fn.nr2char(57424)) -- KP_END
  872. screen:expect([[
  873. 0123456789/*-^+ |
  874. = |
  875. {4:~ }|*2
  876. {5:[No Name] [+] }|
  877. |
  878. {3:-- TERMINAL --} |
  879. ]])
  880. child_session:request(
  881. 'nvim_exec2',
  882. [[
  883. tab split
  884. tabnew
  885. highlight Tabline ctermbg=NONE ctermfg=NONE cterm=underline
  886. ]],
  887. {}
  888. )
  889. screen:expect([[
  890. {12: + [No Name] + [No Name] }{3: [No Name] }{1: }{12:X}|
  891. ^ |
  892. {4:~ }|*2
  893. {5:[No Name] }|
  894. |
  895. {3:-- TERMINAL --} |
  896. ]])
  897. feed_data('\027[57421;5u') -- CTRL + KP_PAGE_UP
  898. screen:expect([[
  899. {12: + [No Name] }{3: + [No Name] }{12: [No Name] }{1: }{12:X}|
  900. 0123456789/*-^+ |
  901. = |
  902. {4:~ }|
  903. {5:[No Name] [+] }|
  904. |
  905. {3:-- TERMINAL --} |
  906. ]])
  907. feed_data('\027[57422;5u') -- CTRL + KP_PAGE_DOWN
  908. screen:expect([[
  909. {12: + [No Name] + [No Name] }{3: [No Name] }{1: }{12:X}|
  910. ^ |
  911. {4:~ }|*2
  912. {5:[No Name] }|
  913. |
  914. {3:-- TERMINAL --} |
  915. ]])
  916. end)
  917. it('supports Super and Meta modifiers', function()
  918. feed_data('i')
  919. feed_data('\022\027[106;9u') -- Super + j
  920. feed_data('\022\027[107;33u') -- Meta + k
  921. feed_data('\022\027[13;41u') -- Super + Meta + Enter
  922. feed_data('\022\027[127;48u') -- Shift + Alt + Ctrl + Super + Meta + Backspace
  923. feed_data('\n')
  924. feed_data('\022\027[57376;9u') -- Super + F13
  925. feed_data('\022\027[57377;33u') -- Meta + F14
  926. feed_data('\022\027[57378;41u') -- Super + Meta + F15
  927. feed_data('\022\027[57379;48u') -- Shift + Alt + Ctrl + Super + Meta + F16
  928. screen:expect([[
  929. <D-j><T-k><T-D-CR><M-T-C-S-D-BS> |
  930. <D-F13><T-F14><T-D-F15><M-T-C-S-D-F16>^ |
  931. {4:~ }|*2
  932. {5:[No Name] [+] }|
  933. {3:-- INSERT --} |
  934. {3:-- TERMINAL --} |
  935. ]])
  936. end)
  937. it('paste: Insert mode', function()
  938. -- "bracketed paste"
  939. feed_data('i""\027i\027[200~')
  940. screen:expect([[
  941. "^" |
  942. {4:~ }|*3
  943. {5:[No Name] [+] }|
  944. {3:-- INSERT --} |
  945. {3:-- TERMINAL --} |
  946. ]])
  947. feed_data('pasted from terminal')
  948. expect_child_buf_lines({ '"pasted from terminal"' })
  949. screen:expect([[
  950. "pasted from terminal^" |
  951. {4:~ }|*3
  952. {5:[No Name] [+] }|
  953. {3:-- INSERT --} |
  954. {3:-- TERMINAL --} |
  955. ]])
  956. feed_data('\027[201~') -- End paste.
  957. poke_both_eventloop()
  958. screen:expect_unchanged()
  959. feed_data('\027[27u') -- ESC: go to Normal mode.
  960. wait_for_mode('n')
  961. screen:expect([[
  962. "pasted from termina^l" |
  963. {4:~ }|*3
  964. {5:[No Name] [+] }|
  965. |
  966. {3:-- TERMINAL --} |
  967. ]])
  968. -- Dot-repeat/redo.
  969. feed_data('2.')
  970. expect_child_buf_lines({ '"pasted from terminapasted from terminalpasted from terminall"' })
  971. screen:expect([[
  972. "pasted from terminapasted from terminalpasted fro|
  973. m termina^ll" |
  974. {4:~ }|*2
  975. {5:[No Name] [+] }|
  976. |
  977. {3:-- TERMINAL --} |
  978. ]])
  979. -- Undo.
  980. feed_data('u')
  981. expect_child_buf_lines({ '"pasted from terminal"' })
  982. feed_data('u')
  983. expect_child_buf_lines({ '""' })
  984. feed_data('u')
  985. expect_child_buf_lines({ '' })
  986. end)
  987. it('paste: select-mode', function()
  988. feed_data('ithis is line 1\nthis is line 2\nline 3 is here\n\027')
  989. wait_for_mode('n')
  990. screen:expect([[
  991. this is line 1 |
  992. this is line 2 |
  993. line 3 is here |
  994. ^ |
  995. {5:[No Name] [+] }|
  996. |
  997. {3:-- TERMINAL --} |
  998. ]])
  999. -- Select-mode. Use <C-n> to move down.
  1000. feed_data('gg04lgh\14\14')
  1001. screen:expect([[
  1002. this{16: is line 1} |
  1003. {16:this is line 2} |
  1004. {16:line}^ 3 is here |
  1005. |
  1006. {5:[No Name] [+] }|
  1007. {3:-- SELECT --} |
  1008. {3:-- TERMINAL --} |
  1009. ]])
  1010. feed_data('\027[200~')
  1011. feed_data('just paste it™')
  1012. feed_data('\027[201~')
  1013. screen:expect([[
  1014. thisjust paste it^™3 is here |
  1015. |
  1016. {4:~ }|*2
  1017. {5:[No Name] [+] }|
  1018. |
  1019. {3:-- TERMINAL --} |
  1020. ]])
  1021. -- Undo.
  1022. feed_data('u')
  1023. expect_child_buf_lines {
  1024. 'this is line 1',
  1025. 'this is line 2',
  1026. 'line 3 is here',
  1027. '',
  1028. }
  1029. -- Redo.
  1030. feed_data('\18') -- <C-r>
  1031. expect_child_buf_lines {
  1032. 'thisjust paste it™3 is here',
  1033. '',
  1034. }
  1035. end)
  1036. it('paste: terminal mode', function()
  1037. if is_ci('github') then
  1038. pending('tty-test complains about not owning the terminal -- actions/runner#241')
  1039. end
  1040. screen:set_default_attr_ids({
  1041. [1] = { reverse = true }, -- focused cursor
  1042. [3] = { bold = true },
  1043. [19] = { bold = true, background = 121, foreground = 0 }, -- StatusLineTerm
  1044. })
  1045. child_exec_lua('vim.o.statusline="^^^^^^^"')
  1046. child_exec_lua('vim.cmd.terminal(...)', testprg('tty-test'))
  1047. feed_data('i')
  1048. screen:expect([[
  1049. tty ready |
  1050. ^ |
  1051. |*2
  1052. {19:^^^^^^^ }|
  1053. {3:-- TERMINAL --} |*2
  1054. ]])
  1055. feed_data('\027[200~')
  1056. feed_data('hallo')
  1057. feed_data('\027[201~')
  1058. screen:expect([[
  1059. tty ready |
  1060. hallo^ |
  1061. |*2
  1062. {19:^^^^^^^ }|
  1063. {3:-- TERMINAL --} |*2
  1064. ]])
  1065. end)
  1066. it('paste: normal-mode (+CRLF #10872)', function()
  1067. feed_data(':set ruler | echo')
  1068. wait_for_mode('c')
  1069. feed_data('\n')
  1070. wait_for_mode('n')
  1071. local expected_lf = { 'line 1', 'ESC:\027 / CR: \rx' }
  1072. local expected_crlf = { 'line 1', 'ESC:\027 / CR: ', 'x' }
  1073. local expected_grid1 = [[
  1074. line 1 |
  1075. ESC:{6:^[} / CR: |
  1076. ^x |
  1077. {4:~ }|
  1078. {5:[No Name] [+] 3,1 All}|
  1079. |
  1080. {3:-- TERMINAL --} |
  1081. ]]
  1082. -- "bracketed paste"
  1083. feed_data('\027[200~' .. table.concat(expected_lf, '\n') .. '\027[201~')
  1084. screen:expect(expected_grid1)
  1085. -- Dot-repeat/redo.
  1086. feed_data('.')
  1087. local expected_grid2 = [[
  1088. ESC:{6:^[} / CR: |
  1089. xline 1 |
  1090. ESC:{6:^[} / CR: |
  1091. ^x |
  1092. {5:[No Name] [+] 5,1 Bot}|
  1093. |
  1094. {3:-- TERMINAL --} |
  1095. ]]
  1096. screen:expect(expected_grid2)
  1097. -- Undo.
  1098. feed_data('u')
  1099. expect_child_buf_lines(expected_crlf)
  1100. feed_data('u')
  1101. expect_child_buf_lines({ '' })
  1102. feed_data(':echo')
  1103. wait_for_mode('c')
  1104. feed_data('\n')
  1105. wait_for_mode('n')
  1106. -- CRLF input
  1107. feed_data('\027[200~' .. table.concat(expected_lf, '\r\n') .. '\027[201~')
  1108. screen:expect(expected_grid1)
  1109. expect_child_buf_lines(expected_crlf)
  1110. -- Dot-repeat/redo.
  1111. feed_data('.')
  1112. screen:expect(expected_grid2)
  1113. -- Undo.
  1114. feed_data('u')
  1115. expect_child_buf_lines(expected_crlf)
  1116. feed_data('u')
  1117. expect_child_buf_lines({ '' })
  1118. end)
  1119. it('paste: cmdline-mode inserts 1 line', function()
  1120. feed_data('ifoo\n') -- Insert some text (for dot-repeat later).
  1121. feed_data('\027:""') -- Enter Cmdline-mode.
  1122. feed_data('\027[D') -- <Left> to place cursor between quotes.
  1123. wait_for_mode('c')
  1124. screen:expect([[
  1125. foo |
  1126. |
  1127. {4:~ }|*2
  1128. {5:[No Name] [+] }|
  1129. :"^" |
  1130. {3:-- TERMINAL --} |
  1131. ]])
  1132. -- "bracketed paste"
  1133. feed_data('\027[200~line 1\nline 2\n')
  1134. wait_for_mode('c')
  1135. feed_data('line 3\nline 4\n\027[201~')
  1136. poke_both_eventloop()
  1137. wait_for_mode('c')
  1138. screen:expect([[
  1139. foo |
  1140. |
  1141. {4:~ }|*2
  1142. {5:[No Name] [+] }|
  1143. :"line 1^" |
  1144. {3:-- TERMINAL --} |
  1145. ]])
  1146. -- Dot-repeat/redo.
  1147. feed_data('\027[27u')
  1148. wait_for_mode('n')
  1149. feed_data('.')
  1150. screen:expect([[
  1151. foo |*2
  1152. ^ |
  1153. {4:~ }|
  1154. {5:[No Name] [+] }|
  1155. |
  1156. {3:-- TERMINAL --} |
  1157. ]])
  1158. end)
  1159. it('paste: cmdline-mode collects chunks of unfinished line', function()
  1160. local function expect_cmdline(expected)
  1161. retry(nil, nil, function()
  1162. local _, cmdline = child_session:request('nvim_call_function', 'getcmdline', {})
  1163. eq(expected, cmdline)
  1164. local _, pos = child_session:request('nvim_call_function', 'getcmdpos', {})
  1165. eq(#expected, pos) -- Cursor is just before the last char.
  1166. end)
  1167. end
  1168. feed_data('\027:""') -- Enter Cmdline-mode.
  1169. feed_data('\027[D') -- <Left> to place cursor between quotes.
  1170. expect_cmdline('""')
  1171. feed_data('\027[200~stuff 1 ')
  1172. expect_cmdline('"stuff 1 "')
  1173. -- Discards everything after the first line.
  1174. feed_data('more\nstuff 2\nstuff 3\n')
  1175. expect_cmdline('"stuff 1 more"')
  1176. feed_data('stuff 3')
  1177. expect_cmdline('"stuff 1 more"')
  1178. -- End the paste sequence.
  1179. feed_data('\027[201~')
  1180. poke_both_eventloop()
  1181. expect_cmdline('"stuff 1 more"')
  1182. feed_data(' typed')
  1183. expect_cmdline('"stuff 1 more typed"')
  1184. end)
  1185. it('paste: recovers from vim.paste() failure', function()
  1186. child_exec_lua([[
  1187. _G.save_paste_fn = vim.paste
  1188. -- Stack traces for this test are non-deterministic, so disable them
  1189. _G.debug.traceback = function(msg) return msg end
  1190. vim.paste = function(lines, phase) error("fake fail") end
  1191. ]])
  1192. -- Prepare something for dot-repeat/redo.
  1193. feed_data('ifoo\n\027[27u')
  1194. wait_for_mode('n')
  1195. screen:expect([[
  1196. foo |
  1197. ^ |
  1198. {4:~ }|*2
  1199. {5:[No Name] [+] }|
  1200. |
  1201. {3:-- TERMINAL --} |
  1202. ]])
  1203. -- Start pasting...
  1204. feed_data('\027[200~line 1\nline 2\n')
  1205. screen:expect([[
  1206. foo |
  1207. |
  1208. {5: }|
  1209. {8:paste: Error executing lua: [string "<nvim>"]:4: f}|
  1210. {8:ake fail} |
  1211. {10:Press ENTER or type command to continue}^ |
  1212. {3:-- TERMINAL --} |
  1213. ]])
  1214. -- Remaining chunks are discarded after vim.paste() failure.
  1215. feed_data('line 3\nline 4\n')
  1216. feed_data('line 5\nline 6\n')
  1217. feed_data('line 7\nline 8\n')
  1218. -- Stop paste.
  1219. feed_data('\027[201~')
  1220. screen:expect_unchanged()
  1221. feed_data('\n') -- <CR> to dismiss hit-enter prompt
  1222. expect_child_buf_lines({ 'foo', '' })
  1223. -- Dot-repeat/redo is not modified by failed paste.
  1224. feed_data('.')
  1225. screen:expect([[
  1226. foo |*2
  1227. ^ |
  1228. {4:~ }|
  1229. {5:[No Name] [+] }|
  1230. |
  1231. {3:-- TERMINAL --} |
  1232. ]])
  1233. -- Editor should still work after failed/drained paste.
  1234. feed_data('ityped input...\027[27u')
  1235. screen:expect([[
  1236. foo |*2
  1237. typed input..^. |
  1238. {4:~ }|
  1239. {5:[No Name] [+] }|
  1240. |
  1241. {3:-- TERMINAL --} |
  1242. ]])
  1243. -- Paste works if vim.paste() succeeds.
  1244. child_exec_lua([[vim.paste = _G.save_paste_fn]])
  1245. feed_data('\027[200~line A\nline B\n\027[201~')
  1246. screen:expect([[
  1247. foo |
  1248. typed input...line A |
  1249. line B |
  1250. ^ |
  1251. {5:[No Name] [+] }|
  1252. |
  1253. {3:-- TERMINAL --} |
  1254. ]])
  1255. end)
  1256. it('paste: vim.paste() cancel (retval=false) #10865', function()
  1257. -- This test only exercises the "cancel" case. Use-case would be "dangling
  1258. -- paste", but that is not implemented yet. #10865
  1259. child_exec_lua([[
  1260. vim.paste = function(lines, phase) return false end
  1261. ]])
  1262. feed_data('\027[200~line A\nline B\n\027[201~')
  1263. expect_child_buf_lines({ '' })
  1264. feed_data('ifoo\n\027[27u')
  1265. expect_child_buf_lines({ 'foo', '' })
  1266. end)
  1267. it('paste: vim.paste() cancel (retval=false) with streaming #30462', function()
  1268. child_exec_lua([[
  1269. vim.paste = (function(overridden)
  1270. return function(lines, phase)
  1271. for i, line in ipairs(lines) do
  1272. if line:find('!') then
  1273. return false
  1274. end
  1275. end
  1276. return overridden(lines, phase)
  1277. end
  1278. end)(vim.paste)
  1279. ]])
  1280. feed_data('A')
  1281. wait_for_mode('i')
  1282. feed_data('\027[200~aaa')
  1283. expect_child_buf_lines({ 'aaa' })
  1284. feed_data('bbb')
  1285. expect_child_buf_lines({ 'aaabbb' })
  1286. feed_data('ccc!') -- This chunk is cancelled.
  1287. expect_child_buf_lines({ 'aaabbb' })
  1288. feed_data('ddd\027[201~') -- This chunk is ignored.
  1289. poke_both_eventloop()
  1290. expect_child_buf_lines({ 'aaabbb' })
  1291. feed_data('\027[27u')
  1292. wait_for_mode('n')
  1293. feed_data('.') -- Dot-repeat only includes chunks actually pasted.
  1294. expect_child_buf_lines({ 'aaabbbaaabbb' })
  1295. feed_data('$\027[200~eee\027[201~') -- A following paste works normally.
  1296. expect_child_buf_lines({ 'aaabbbaaabbbeee' })
  1297. end)
  1298. it("paste: 'nomodifiable' buffer", function()
  1299. child_exec_lua([[
  1300. vim.bo.modifiable = false
  1301. -- Truncate the error message to hide the line number
  1302. _G.debug.traceback = function(msg) return msg:sub(-49) end
  1303. ]])
  1304. feed_data('\027[200~fail 1\nfail 2\n\027[201~')
  1305. screen:expect([[
  1306. |
  1307. {4:~ }|
  1308. {5: }|
  1309. {8:paste: Error executing lua: Vim:E21: Cannot make c}|
  1310. {8:hanges, 'modifiable' is off} |
  1311. {10:Press ENTER or type command to continue}^ |
  1312. {3:-- TERMINAL --} |
  1313. ]])
  1314. feed_data('\n') -- <Enter> to dismiss hit-enter prompt
  1315. child_exec_lua('vim.bo.modifiable = true')
  1316. feed_data('\027[200~success 1\nsuccess 2\n\027[201~')
  1317. screen:expect([[
  1318. success 1 |
  1319. success 2 |
  1320. ^ |
  1321. {4:~ }|
  1322. {5:[No Name] [+] }|
  1323. |
  1324. {3:-- TERMINAL --} |
  1325. ]])
  1326. end)
  1327. it('paste: exactly 64 bytes #10311', function()
  1328. local expected = string.rep('z', 64)
  1329. feed_data('i')
  1330. wait_for_mode('i')
  1331. -- "bracketed paste"
  1332. feed_data('\027[200~' .. expected .. '\027[201~')
  1333. expect_child_buf_lines({ expected })
  1334. feed_data(' end')
  1335. expected = expected .. ' end'
  1336. screen:expect([[
  1337. zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz|
  1338. zzzzzzzzzzzzzz end^ |
  1339. {4:~ }|*2
  1340. {5:[No Name] [+] }|
  1341. {3:-- INSERT --} |
  1342. {3:-- TERMINAL --} |
  1343. ]])
  1344. expect_child_buf_lines({ expected })
  1345. end)
  1346. it('paste: less-than sign in cmdline #11088', function()
  1347. local expected = '<'
  1348. feed_data(':')
  1349. wait_for_mode('c')
  1350. -- "bracketed paste"
  1351. feed_data('\027[200~' .. expected .. '\027[201~')
  1352. screen:expect([[
  1353. |
  1354. {4:~ }|*3
  1355. {5:[No Name] }|
  1356. :<^ |
  1357. {3:-- TERMINAL --} |
  1358. ]])
  1359. end)
  1360. it('paste: big burst of input', function()
  1361. feed_data(':set ruler\n')
  1362. local q = {}
  1363. for i = 1, 3000 do
  1364. q[i] = 'item ' .. tostring(i)
  1365. end
  1366. feed_data('i')
  1367. wait_for_mode('i')
  1368. -- "bracketed paste"
  1369. feed_data('\027[200~' .. table.concat(q, '\n') .. '\027[201~')
  1370. expect_child_buf_lines(q)
  1371. feed_data(' end')
  1372. screen:expect([[
  1373. item 2997 |
  1374. item 2998 |
  1375. item 2999 |
  1376. item 3000 end^ |
  1377. {5:[No Name] [+] 3000,14 Bot}|
  1378. {3:-- INSERT --} |
  1379. {3:-- TERMINAL --} |
  1380. ]])
  1381. feed_data('\027[27u') -- ESC: go to Normal mode.
  1382. wait_for_mode('n')
  1383. -- Dot-repeat/redo.
  1384. feed_data('.')
  1385. screen:expect([[
  1386. item 2997 |
  1387. item 2998 |
  1388. item 2999 |
  1389. item 3000 en^dd |
  1390. {5:[No Name] [+] 5999,13 Bot}|
  1391. |
  1392. {3:-- TERMINAL --} |
  1393. ]])
  1394. end)
  1395. it('paste: forwards spurious "start paste" code', function()
  1396. -- If multiple "start paste" sequences are sent without a corresponding
  1397. -- "stop paste" sequence, only the first occurrence should be consumed.
  1398. feed_data('i')
  1399. wait_for_mode('i')
  1400. -- Send the "start paste" sequence.
  1401. feed_data('\027[200~')
  1402. feed_data('\npasted from terminal (1)\n')
  1403. -- Send spurious "start paste" sequence.
  1404. feed_data('\027[200~')
  1405. feed_data('\n')
  1406. -- Send the "stop paste" sequence.
  1407. feed_data('\027[201~')
  1408. screen:expect([[
  1409. |
  1410. pasted from terminal (1) |
  1411. {6:^[}[200~ |
  1412. ^ |
  1413. {5:[No Name] [+] }|
  1414. {3:-- INSERT --} |
  1415. {3:-- TERMINAL --} |
  1416. ]])
  1417. end)
  1418. it('paste: ignores spurious "stop paste" code', function()
  1419. -- If "stop paste" sequence is received without a preceding "start paste"
  1420. -- sequence, it should be ignored.
  1421. feed_data('i')
  1422. wait_for_mode('i')
  1423. -- Send "stop paste" sequence.
  1424. feed_data('\027[201~')
  1425. screen:expect([[
  1426. ^ |
  1427. {4:~ }|*3
  1428. {5:[No Name] }|
  1429. {3:-- INSERT --} |
  1430. {3:-- TERMINAL --} |
  1431. ]])
  1432. end)
  1433. it('paste: split "start paste" code', function()
  1434. feed_data('i')
  1435. wait_for_mode('i')
  1436. -- Send split "start paste" sequence.
  1437. feed_data('\027[2')
  1438. feed_data('00~pasted from terminal\027[201~')
  1439. screen:expect([[
  1440. pasted from terminal^ |
  1441. {4:~ }|*3
  1442. {5:[No Name] [+] }|
  1443. {3:-- INSERT --} |
  1444. {3:-- TERMINAL --} |
  1445. ]])
  1446. end)
  1447. it('paste: split "stop paste" code', function()
  1448. feed_data('i')
  1449. wait_for_mode('i')
  1450. -- Send split "stop paste" sequence.
  1451. feed_data('\027[200~pasted from terminal\027[20')
  1452. feed_data('1~')
  1453. screen:expect([[
  1454. pasted from terminal^ |
  1455. {4:~ }|*3
  1456. {5:[No Name] [+] }|
  1457. {3:-- INSERT --} |
  1458. {3:-- TERMINAL --} |
  1459. ]])
  1460. end)
  1461. it('paste: streamed paste with isolated "stop paste" code', function()
  1462. child_exec_lua([[
  1463. _G.paste_phases = {}
  1464. vim.paste = (function(overridden)
  1465. return function(lines, phase)
  1466. table.insert(_G.paste_phases, phase)
  1467. overridden(lines, phase)
  1468. end
  1469. end)(vim.paste)
  1470. ]])
  1471. feed_data('i')
  1472. wait_for_mode('i')
  1473. feed_data('\027[200~pasted') -- phase 1
  1474. screen:expect([[
  1475. pasted^ |
  1476. {4:~ }|*3
  1477. {5:[No Name] [+] }|
  1478. {3:-- INSERT --} |
  1479. {3:-- TERMINAL --} |
  1480. ]])
  1481. feed_data(' from terminal') -- phase 2
  1482. screen:expect([[
  1483. pasted from terminal^ |
  1484. {4:~ }|*3
  1485. {5:[No Name] [+] }|
  1486. {3:-- INSERT --} |
  1487. {3:-- TERMINAL --} |
  1488. ]])
  1489. -- Send isolated "stop paste" sequence.
  1490. feed_data('\027[201~') -- phase 3
  1491. poke_both_eventloop()
  1492. screen:expect_unchanged()
  1493. local rv = child_exec_lua('return _G.paste_phases')
  1494. -- In rare cases there may be multiple chunks of phase 2 because of timing.
  1495. eq({ 1, 2, 3 }, { rv[1], rv[2], rv[#rv] })
  1496. end)
  1497. it('allows termguicolors to be set at runtime', function()
  1498. screen:set_option('rgb', true)
  1499. screen:set_default_attr_ids({
  1500. [1] = { reverse = true },
  1501. [2] = { foreground = tonumber('0x4040ff'), fg_indexed = true },
  1502. [3] = { bold = true, reverse = true },
  1503. [4] = { bold = true },
  1504. [5] = { reverse = true, foreground = tonumber('0xe0e000'), fg_indexed = true },
  1505. [6] = { foreground = tonumber('0xe0e000'), fg_indexed = true },
  1506. [7] = { reverse = true, foreground = Screen.colors.SeaGreen4 },
  1507. [8] = { foreground = Screen.colors.SeaGreen4 },
  1508. [9] = { bold = true, foreground = Screen.colors.Blue1 },
  1509. [10] = { foreground = Screen.colors.Blue },
  1510. })
  1511. feed_data(':hi SpecialKey ctermfg=3 guifg=SeaGreen\n')
  1512. feed_data('i')
  1513. feed_data('\022\007') -- ctrl+g
  1514. feed_data('\028\014') -- crtl+\ ctrl+N
  1515. feed_data(':set termguicolors?\n')
  1516. screen:expect([[
  1517. {6:^^G} |
  1518. {2:~ }|*3
  1519. {3:[No Name] [+] }|
  1520. notermguicolors |
  1521. {4:-- TERMINAL --} |
  1522. ]])
  1523. feed_data(':set termguicolors\n')
  1524. screen:expect([[
  1525. {8:^^G} |
  1526. {9:~}{10: }|*3
  1527. {3:[No Name] [+] }|
  1528. :set termguicolors |
  1529. {4:-- TERMINAL --} |
  1530. ]])
  1531. feed_data(':set notermguicolors\n')
  1532. screen:expect([[
  1533. {6:^^G} |
  1534. {2:~ }|*3
  1535. {3:[No Name] [+] }|
  1536. :set notermguicolors |
  1537. {4:-- TERMINAL --} |
  1538. ]])
  1539. end)
  1540. it('forwards :term palette colors with termguicolors', function()
  1541. if is_ci('github') then
  1542. pending('tty-test complains about not owning the terminal -- actions/runner#241')
  1543. end
  1544. screen:set_rgb_cterm(true)
  1545. screen:set_default_attr_ids({
  1546. [1] = { { reverse = true }, { reverse = true } },
  1547. [2] = {
  1548. { bold = true, background = Screen.colors.LightGreen, foreground = Screen.colors.Black },
  1549. { bold = true },
  1550. },
  1551. [3] = { { bold = true }, { bold = true } },
  1552. [4] = { { fg_indexed = true, foreground = tonumber('0xe0e000') }, { foreground = 3 } },
  1553. [5] = { { foreground = tonumber('0xff8000') }, {} },
  1554. [6] = {
  1555. {
  1556. fg_indexed = true,
  1557. bg_indexed = true,
  1558. bold = true,
  1559. background = tonumber('0x66ff99'),
  1560. foreground = Screen.colors.Black,
  1561. },
  1562. { bold = true, background = 121, foreground = 0 },
  1563. },
  1564. [7] = {
  1565. {
  1566. fg_indexed = true,
  1567. bg_indexed = true,
  1568. background = tonumber('0x66ff99'),
  1569. foreground = Screen.colors.Black,
  1570. },
  1571. { background = 121, foreground = 0 },
  1572. },
  1573. })
  1574. child_exec_lua('vim.o.statusline="^^^^^^^"')
  1575. child_exec_lua('vim.o.termguicolors=true')
  1576. child_exec_lua('vim.cmd.terminal(...)', testprg('tty-test'))
  1577. screen:expect {
  1578. grid = [[
  1579. ^tty ready |
  1580. |*3
  1581. {2:^^^^^^^ }|
  1582. |
  1583. {3:-- TERMINAL --} |
  1584. ]],
  1585. }
  1586. feed_data(
  1587. ':call chansend(&channel, "\\033[38;5;3mtext\\033[38:2:255:128:0mcolor\\033[0;10mtext")\n'
  1588. )
  1589. screen:expect {
  1590. grid = [[
  1591. ^tty ready |
  1592. {4:text}{5:color}text |
  1593. |*2
  1594. {2:^^^^^^^ }|
  1595. |
  1596. {3:-- TERMINAL --} |
  1597. ]],
  1598. }
  1599. feed_data(':set notermguicolors\n')
  1600. screen:expect {
  1601. grid = [[
  1602. ^tty ready |
  1603. {4:text}colortext |
  1604. |*2
  1605. {6:^^^^^^^}{7: }|
  1606. :set notermguicolors |
  1607. {3:-- TERMINAL --} |
  1608. ]],
  1609. }
  1610. end)
  1611. -- Note: libvterm doesn't support colored underline or undercurl.
  1612. it('supports undercurl and underdouble when run in :terminal', function()
  1613. screen:set_default_attr_ids({
  1614. [1] = { reverse = true },
  1615. [2] = { bold = true, reverse = true },
  1616. [3] = { bold = true },
  1617. [4] = { foreground = 12 },
  1618. [5] = { undercurl = true },
  1619. [6] = { underdouble = true },
  1620. })
  1621. child_session:request('nvim_set_hl', 0, 'Visual', { undercurl = true })
  1622. feed_data('ifoobar\027V')
  1623. screen:expect([[
  1624. {5:fooba}^r |
  1625. {4:~ }|*3
  1626. {2:[No Name] [+] }|
  1627. {3:-- VISUAL LINE --} |
  1628. {3:-- TERMINAL --} |
  1629. ]])
  1630. child_session:request('nvim_set_hl', 0, 'Visual', { underdouble = true })
  1631. screen:expect([[
  1632. {6:fooba}^r |
  1633. {4:~ }|*3
  1634. {2:[No Name] [+] }|
  1635. {3:-- VISUAL LINE --} |
  1636. {3:-- TERMINAL --} |
  1637. ]])
  1638. end)
  1639. it('in nvim_list_uis(), sets nvim_set_client_info()', function()
  1640. -- $TERM in :terminal.
  1641. local exp_term = is_os('bsd') and 'builtin_xterm' or 'xterm-256color'
  1642. local ui_chan = 1
  1643. local expected = {
  1644. {
  1645. chan = ui_chan,
  1646. ext_cmdline = false,
  1647. ext_hlstate = false,
  1648. ext_linegrid = true,
  1649. ext_messages = false,
  1650. ext_multigrid = false,
  1651. ext_popupmenu = false,
  1652. ext_tabline = false,
  1653. ext_termcolors = true,
  1654. ext_wildmenu = false,
  1655. height = 6,
  1656. override = false,
  1657. rgb = false,
  1658. stdin_tty = true,
  1659. stdout_tty = true,
  1660. term_background = '',
  1661. term_colors = 256,
  1662. term_name = exp_term,
  1663. width = 50,
  1664. },
  1665. }
  1666. local _, rv = child_session:request('nvim_list_uis')
  1667. eq(expected, rv)
  1668. ---@type table
  1669. local expected_version = child_exec_lua('return vim.version()')
  1670. -- vim.version() returns `prerelease` string. Coerce it to boolean.
  1671. expected_version.prerelease = not not expected_version.prerelease
  1672. local expected_chan_info = {
  1673. client = {
  1674. attributes = {
  1675. license = 'Apache 2',
  1676. -- pid = 5371,
  1677. website = 'https://neovim.io',
  1678. },
  1679. methods = {},
  1680. name = 'nvim-tui',
  1681. type = 'ui',
  1682. version = expected_version,
  1683. },
  1684. id = ui_chan,
  1685. mode = 'rpc',
  1686. stream = 'stdio',
  1687. }
  1688. local status, chan_info = child_session:request('nvim_get_chan_info', ui_chan)
  1689. ok(status)
  1690. local info = chan_info.client
  1691. ok(info.attributes.pid and info.attributes.pid > 0, 'PID', info.attributes.pid or 'nil')
  1692. ok(info.version.major >= 0)
  1693. ok(info.version.minor >= 0)
  1694. ok(info.version.patch >= 0)
  1695. -- Delete variable fields so we can deep-compare.
  1696. info.attributes.pid = nil
  1697. eq(expected_chan_info, chan_info)
  1698. end)
  1699. it('allows grid to assume wider ambiwidth chars than host terminal', function()
  1700. child_session:request(
  1701. 'nvim_buf_set_lines',
  1702. 0,
  1703. 0,
  1704. -1,
  1705. true,
  1706. { ('℃'):rep(60), ('℃'):rep(60) }
  1707. )
  1708. child_session:request('nvim_set_option_value', 'cursorline', true, {})
  1709. child_session:request('nvim_set_option_value', 'list', true, {})
  1710. child_session:request('nvim_set_option_value', 'listchars', 'eol:$', { win = 0 })
  1711. feed_data('gg')
  1712. local singlewidth_screen = [[
  1713. {12:^℃℃℃℃℃℃℃℃℃℃℃℃℃℃℃℃℃℃℃℃℃℃℃℃℃℃℃℃℃℃℃℃℃℃℃℃℃℃℃℃℃℃℃℃℃℃℃℃℃℃}|
  1714. {12:℃℃℃℃℃℃℃℃℃℃}{15:$}{12: }|
  1715. ℃℃℃℃℃℃℃℃℃℃℃℃℃℃℃℃℃℃℃℃℃℃℃℃℃℃℃℃℃℃℃℃℃℃℃℃℃℃℃℃℃℃℃℃℃℃℃℃℃℃|
  1716. ℃℃℃℃℃℃℃℃℃℃{4:$} |
  1717. {5:[No Name] [+] }|
  1718. |
  1719. {3:-- TERMINAL --} |
  1720. ]]
  1721. -- When grid assumes "℃" to be double-width but host terminal assumes it to be single-width,
  1722. -- the second cell of "℃" is a space and the attributes of the "℃" are applied to it.
  1723. local doublewidth_screen = [[
  1724. {12:^℃ ℃ ℃ ℃ ℃ ℃ ℃ ℃ ℃ ℃ ℃ ℃ ℃ ℃ ℃ ℃ ℃ ℃ ℃ ℃ ℃ ℃ ℃ ℃ ℃ }|
  1725. {12:℃ ℃ ℃ ℃ ℃ ℃ ℃ ℃ ℃ ℃ ℃ ℃ ℃ ℃ ℃ ℃ ℃ ℃ ℃ ℃ ℃ ℃ ℃ ℃ ℃ }|
  1726. {12:℃ ℃ ℃ ℃ ℃ ℃ ℃ ℃ ℃ ℃ }{15:$}{12: }|
  1727. ℃ ℃ ℃ ℃ ℃ ℃ ℃ ℃ ℃ ℃ ℃ ℃ ℃ ℃ ℃ ℃ ℃ ℃ ℃ ℃ ℃ ℃ ℃ {4:@@@@}|
  1728. {5:[No Name] [+] }|
  1729. |
  1730. {3:-- TERMINAL --} |
  1731. ]]
  1732. screen:expect(singlewidth_screen)
  1733. child_session:request('nvim_set_option_value', 'ambiwidth', 'double', {})
  1734. screen:expect(doublewidth_screen)
  1735. child_session:request('nvim_set_option_value', 'ambiwidth', 'single', {})
  1736. screen:expect(singlewidth_screen)
  1737. child_session:request('nvim_call_function', 'setcellwidths', { { { 0x2103, 0x2103, 2 } } })
  1738. screen:expect(doublewidth_screen)
  1739. child_session:request('nvim_call_function', 'setcellwidths', { { { 0x2103, 0x2103, 1 } } })
  1740. screen:expect(singlewidth_screen)
  1741. end)
  1742. it('allows grid to assume wider non-ambiwidth chars than host terminal', function()
  1743. child_session:request(
  1744. 'nvim_buf_set_lines',
  1745. 0,
  1746. 0,
  1747. -1,
  1748. true,
  1749. { ('✓'):rep(60), ('✓'):rep(60) }
  1750. )
  1751. child_session:request('nvim_set_option_value', 'cursorline', true, {})
  1752. child_session:request('nvim_set_option_value', 'list', true, {})
  1753. child_session:request('nvim_set_option_value', 'listchars', 'eol:$', { win = 0 })
  1754. feed_data('gg')
  1755. local singlewidth_screen = [[
  1756. {12:^✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓}|
  1757. {12:✓✓✓✓✓✓✓✓✓✓}{15:$}{12: }|
  1758. ✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓|
  1759. ✓✓✓✓✓✓✓✓✓✓{4:$} |
  1760. {5:[No Name] [+] }|
  1761. |
  1762. {3:-- TERMINAL --} |
  1763. ]]
  1764. -- When grid assumes "✓" to be double-width but host terminal assumes it to be single-width,
  1765. -- the second cell of "✓" is a space and the attributes of the "✓" are applied to it.
  1766. local doublewidth_screen = [[
  1767. {12:^✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ }|
  1768. {12:✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ }|
  1769. {12:✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ }{15:$}{12: }|
  1770. ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ {4:@@@@}|
  1771. {5:[No Name] [+] }|
  1772. |
  1773. {3:-- TERMINAL --} |
  1774. ]]
  1775. screen:expect(singlewidth_screen)
  1776. child_session:request('nvim_set_option_value', 'ambiwidth', 'double', {})
  1777. screen:expect_unchanged()
  1778. child_session:request('nvim_call_function', 'setcellwidths', { { { 0x2713, 0x2713, 2 } } })
  1779. screen:expect(doublewidth_screen)
  1780. child_session:request('nvim_set_option_value', 'ambiwidth', 'single', {})
  1781. screen:expect_unchanged()
  1782. child_session:request('nvim_call_function', 'setcellwidths', { { { 0x2713, 0x2713, 1 } } })
  1783. screen:expect(singlewidth_screen)
  1784. end)
  1785. it('draws correctly when cursor_address overflows #21643', function()
  1786. screen:try_resize(70, 333)
  1787. retry(nil, nil, function()
  1788. eq({ true, 330 }, { child_session:request('nvim_win_get_height', 0) })
  1789. end)
  1790. child_session:request('nvim_set_option_value', 'cursorline', true, {})
  1791. -- Use full screen message so that redrawing afterwards is more deterministic.
  1792. child_session:notify('nvim_command', 'intro')
  1793. screen:expect({ any = 'Nvim is open source and freely distributable' })
  1794. -- Going to top-left corner needs 3 bytes.
  1795. -- Setting underline attribute needs 9 bytes.
  1796. -- A Ꝩ character takes 3 bytes.
  1797. -- The whole line needs 3 + 9 + 3 * 21838 + 3 = 65529 bytes.
  1798. -- The cursor_address that comes after will overflow the 65535-byte buffer.
  1799. local line = ('Ꝩ'):rep(21838) .. '℃'
  1800. child_session:notify('nvim_buf_set_lines', 0, 0, -1, true, { line, 'b' })
  1801. -- Close the :intro message and redraw the lines.
  1802. feed_data('\n')
  1803. screen:expect([[
  1804. {12:^ꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨ}|
  1805. {12:ꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨ}|*310
  1806. {12:ꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨ℃ }|
  1807. b |
  1808. {4:~ }|*17
  1809. {5:[No Name] [+] }|
  1810. |
  1811. {3:-- TERMINAL --} |
  1812. ]])
  1813. end)
  1814. it('draws correctly when setting title overflows #30793', function()
  1815. screen:try_resize(67, 327)
  1816. retry(nil, nil, function()
  1817. eq({ true, 324 }, { child_session:request('nvim_win_get_height', 0) })
  1818. end)
  1819. child_exec_lua([[
  1820. vim.o.cmdheight = 0
  1821. vim.o.laststatus = 0
  1822. vim.o.ruler = false
  1823. vim.o.showcmd = false
  1824. vim.o.termsync = false
  1825. vim.o.title = true
  1826. ]])
  1827. retry(nil, nil, function()
  1828. eq('[No Name] - Nvim', api.nvim_buf_get_var(0, 'term_title'))
  1829. eq({ true, 326 }, { child_session:request('nvim_win_get_height', 0) })
  1830. end)
  1831. -- Use full screen message so that redrawing afterwards is more deterministic.
  1832. child_session:notify('nvim_command', 'intro')
  1833. screen:expect({ any = 'Nvim is open source and freely distributable' })
  1834. -- Going to top-left corner needs 3 bytes.
  1835. -- A Ꝩ character takes 3 bytes.
  1836. -- The whole line needs 3 + 3 * 21842 = 65529 bytes.
  1837. -- The title will be updated because the buffer is now modified.
  1838. -- The start of the OSC 0 sequence to set title can fit in the 65535-byte buffer,
  1839. -- but the title string cannot.
  1840. local line = ('Ꝩ'):rep(21842)
  1841. child_session:notify('nvim_buf_set_lines', 0, 0, -1, true, { line })
  1842. -- Close the :intro message and redraw the lines.
  1843. feed_data('\n')
  1844. screen:expect([[
  1845. ^ꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨ|
  1846. ꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨꝨ|*325
  1847. {3:-- TERMINAL --} |
  1848. ]])
  1849. retry(nil, nil, function()
  1850. eq('[No Name] + - Nvim', api.nvim_buf_get_var(0, 'term_title'))
  1851. end)
  1852. end)
  1853. it('visual bell (padding) does not crash #21610', function()
  1854. feed_data ':set visualbell\n'
  1855. screen:expect {
  1856. grid = [[
  1857. ^ |
  1858. {4:~ }|*3
  1859. {5:[No Name] }|
  1860. :set visualbell |
  1861. {3:-- TERMINAL --} |
  1862. ]],
  1863. }
  1864. -- move left is enough to invoke the bell
  1865. feed_data 'h'
  1866. -- visual change to show we process events after this
  1867. feed_data 'i'
  1868. screen:expect {
  1869. grid = [[
  1870. ^ |
  1871. {4:~ }|*3
  1872. {5:[No Name] }|
  1873. {3:-- INSERT --} |
  1874. {3:-- TERMINAL --} |
  1875. ]],
  1876. }
  1877. end)
  1878. it('no assert failure on deadly signal #21896', function()
  1879. exec_lua([[vim.uv.kill(vim.fn.jobpid(vim.bo.channel), 'sigterm')]])
  1880. screen:expect {
  1881. grid = [[
  1882. Vim: Caught deadly signal 'SIGTERM' |
  1883. |*2
  1884. [Process exited 1]^ |
  1885. |*2
  1886. {3:-- TERMINAL --} |
  1887. ]],
  1888. }
  1889. end)
  1890. it('no stack-use-after-scope with cursor color #22432', function()
  1891. screen:set_option('rgb', true)
  1892. command('set termguicolors')
  1893. child_session:request(
  1894. 'nvim_exec2',
  1895. [[
  1896. set tgc
  1897. hi Cursor guifg=Red guibg=Green
  1898. set guicursor=n:block-Cursor/lCursor
  1899. ]],
  1900. {}
  1901. )
  1902. screen:set_default_attr_ids({
  1903. [1] = { reverse = true },
  1904. [2] = { bold = true, foreground = Screen.colors.Blue },
  1905. [3] = { foreground = Screen.colors.Blue },
  1906. [4] = { reverse = true, bold = true },
  1907. [5] = { bold = true },
  1908. })
  1909. screen:expect([[
  1910. ^ |
  1911. {2:~}{3: }|*3
  1912. {4:[No Name] }|
  1913. |
  1914. {5:-- TERMINAL --} |
  1915. ]])
  1916. feed_data('i')
  1917. screen:expect([[
  1918. ^ |
  1919. {2:~}{3: }|*3
  1920. {4:[No Name] }|
  1921. {5:-- INSERT --} |
  1922. {5:-- TERMINAL --} |
  1923. ]])
  1924. end)
  1925. it('redraws on SIGWINCH even if terminal size is unchanged #23411', function()
  1926. child_session:request('nvim_echo', { { 'foo' } }, false, {})
  1927. screen:expect([[
  1928. ^ |
  1929. {4:~ }|*3
  1930. {5:[No Name] }|
  1931. foo |
  1932. {3:-- TERMINAL --} |
  1933. ]])
  1934. exec_lua([[vim.uv.kill(vim.fn.jobpid(vim.bo.channel), 'sigwinch')]])
  1935. screen:expect([[
  1936. ^ |
  1937. {4:~ }|*3
  1938. {5:[No Name] }|
  1939. |
  1940. {3:-- TERMINAL --} |
  1941. ]])
  1942. end)
  1943. it('supports hiding cursor', function()
  1944. child_session:request(
  1945. 'nvim_command',
  1946. "let g:id = jobstart([v:progpath, '--clean', '--headless'])"
  1947. )
  1948. feed_data(':call jobwait([g:id])\n')
  1949. screen:expect([[
  1950. |
  1951. {4:~ }|*3
  1952. {5:[No Name] }|
  1953. :call jobwait([g:id]) |
  1954. {3:-- TERMINAL --} |
  1955. ]])
  1956. feed_data('\003')
  1957. screen:expect([[
  1958. ^ |
  1959. {4:~ }|*3
  1960. {5:[No Name] }|
  1961. Type :qa and press <Enter> to exit Nvim |
  1962. {3:-- TERMINAL --} |
  1963. ]])
  1964. end)
  1965. it('cursor is not hidden on incsearch with no match', function()
  1966. feed_data('ifoo\027')
  1967. feed_data('/foo')
  1968. screen:expect([[
  1969. {1:foo} |
  1970. {4:~ }|*3
  1971. {5:[No Name] [+] }|
  1972. /foo^ |
  1973. {3:-- TERMINAL --} |
  1974. ]])
  1975. screen:sleep(10)
  1976. feed_data('b')
  1977. screen:expect([[
  1978. foo |
  1979. {4:~ }|*3
  1980. {5:[No Name] [+] }|
  1981. /foob^ |
  1982. {3:-- TERMINAL --} |
  1983. ]])
  1984. screen:sleep(10)
  1985. feed_data('a')
  1986. screen:expect([[
  1987. foo |
  1988. {4:~ }|*3
  1989. {5:[No Name] [+] }|
  1990. /fooba^ |
  1991. {3:-- TERMINAL --} |
  1992. ]])
  1993. end)
  1994. it('emits hyperlinks with OSC 8', function()
  1995. exec_lua([[
  1996. local buf = vim.api.nvim_get_current_buf()
  1997. _G.urls = {}
  1998. vim.api.nvim_create_autocmd('TermRequest', {
  1999. buffer = buf,
  2000. callback = function(args)
  2001. local req = args.data
  2002. if not req then
  2003. return
  2004. end
  2005. local id, url = req:match('\027]8;id=(%d+);(.*)$')
  2006. if id ~= nil and url ~= nil then
  2007. table.insert(_G.urls, { id = tonumber(id), url = url })
  2008. end
  2009. end,
  2010. })
  2011. ]])
  2012. child_exec_lua([[
  2013. vim.api.nvim_buf_set_lines(0, 0, 0, true, {'Hello'})
  2014. local ns = vim.api.nvim_create_namespace('test')
  2015. vim.api.nvim_buf_set_extmark(0, ns, 0, 1, {
  2016. end_col = 3,
  2017. url = 'https://example.com',
  2018. })
  2019. ]])
  2020. retry(nil, 1000, function()
  2021. eq({ { id = 0xE1EA0000, url = 'https://example.com' } }, exec_lua([[return _G.urls]]))
  2022. end)
  2023. end)
  2024. end)
  2025. describe('TUI', function()
  2026. before_each(clear)
  2027. it('resize at startup #17285 #15044 #11330', function()
  2028. local screen = Screen.new(50, 10)
  2029. screen:set_default_attr_ids({
  2030. [1] = { reverse = true },
  2031. [2] = { bold = true, foreground = Screen.colors.Blue },
  2032. [3] = { bold = true },
  2033. [4] = { foreground = tonumber('0x4040ff'), fg_indexed = true },
  2034. [5] = { bold = true, reverse = true },
  2035. [6] = { foreground = Screen.colors.White, background = Screen.colors.DarkGreen },
  2036. })
  2037. fn.jobstart({
  2038. nvim_prog,
  2039. '--clean',
  2040. '--cmd',
  2041. 'colorscheme vim',
  2042. '--cmd',
  2043. 'set notermguicolors',
  2044. '--cmd',
  2045. 'let start = reltime() | while v:true | if reltimefloat(reltime(start)) > 2 | break | endif | endwhile',
  2046. }, {
  2047. term = true,
  2048. env = {
  2049. VIMRUNTIME = os.getenv('VIMRUNTIME'),
  2050. },
  2051. })
  2052. exec([[
  2053. sleep 500m
  2054. vs new
  2055. ]])
  2056. screen:expect([[
  2057. ^ │ |
  2058. {2:~ }│{4:~ }|*5
  2059. {2:~ }│{5:[No Name] 0,0-1 All}|
  2060. {2:~ }│ |
  2061. {5:new }{6:{MATCH:<.*[/\]nvim }}|
  2062. |
  2063. ]])
  2064. end)
  2065. -- #28667, #28668
  2066. for _, guicolors in ipairs({ 'notermguicolors', 'termguicolors' }) do
  2067. it('has no black flicker when clearing regions during startup with ' .. guicolors, function()
  2068. local screen = Screen.new(50, 10)
  2069. fn.jobstart({
  2070. nvim_prog,
  2071. '--clean',
  2072. '--cmd',
  2073. 'set ' .. guicolors,
  2074. '--cmd',
  2075. 'sleep 10',
  2076. }, {
  2077. term = true,
  2078. env = {
  2079. VIMRUNTIME = os.getenv('VIMRUNTIME'),
  2080. },
  2081. })
  2082. screen:expect({
  2083. grid = [[
  2084. ^ |
  2085. |*9
  2086. ]],
  2087. intermediate = true,
  2088. })
  2089. screen:try_resize(51, 11)
  2090. screen:expect({
  2091. grid = [[
  2092. ^ |
  2093. |*10
  2094. ]],
  2095. })
  2096. end)
  2097. end
  2098. it('argv[0] can be overridden #23953', function()
  2099. if not exec_lua('return pcall(require, "ffi")') then
  2100. pending('missing LuaJIT FFI')
  2101. end
  2102. local script_file = 'Xargv0.lua'
  2103. write_file(
  2104. script_file,
  2105. [=[
  2106. local ffi = require('ffi')
  2107. ffi.cdef([[int execl(const char *, const char *, ...);]])
  2108. ffi.C.execl(vim.v.progpath, 'Xargv0nvim', '--clean', nil)
  2109. ]=]
  2110. )
  2111. finally(function()
  2112. os.remove(script_file)
  2113. end)
  2114. local screen = tt.setup_child_nvim({ '--clean', '-l', script_file })
  2115. screen:expect {
  2116. grid = [[
  2117. ^ |
  2118. ~ |*3
  2119. [No Name] 0,0-1 All|
  2120. |
  2121. {3:-- TERMINAL --} |
  2122. ]],
  2123. }
  2124. feed_data(':put =v:argv + [v:progname]\n')
  2125. screen:expect {
  2126. grid = [[
  2127. Xargv0nvim |
  2128. --embed |
  2129. --clean |
  2130. ^Xargv0nvim |
  2131. [No Name] [+] 5,1 Bot|
  2132. 4 more lines |
  2133. {3:-- TERMINAL --} |
  2134. ]],
  2135. }
  2136. end)
  2137. it('with non-tty (pipe) stdout/stderr', function()
  2138. finally(function()
  2139. os.remove('testF')
  2140. end)
  2141. local screen = tt.setup_screen(
  2142. 0,
  2143. ('"%s" -u NONE -i NONE --cmd "set noswapfile noshowcmd noruler" --cmd "normal iabc" > /dev/null 2>&1 && cat testF && rm testF'):format(
  2144. nvim_prog
  2145. ),
  2146. nil,
  2147. { VIMRUNTIME = os.getenv('VIMRUNTIME') }
  2148. )
  2149. feed_data(':w testF\n:q\n')
  2150. screen:expect([[
  2151. :w testF |
  2152. :q |
  2153. abc |
  2154. |
  2155. [Process exited 0]^ |
  2156. |
  2157. {3:-- TERMINAL --} |
  2158. ]])
  2159. end)
  2160. it('<C-h> #10134', function()
  2161. local screen = tt.setup_child_nvim({
  2162. '-u',
  2163. 'NONE',
  2164. '-i',
  2165. 'NONE',
  2166. '--cmd',
  2167. 'colorscheme vim',
  2168. '--cmd',
  2169. 'set noruler notermguicolors',
  2170. '--cmd',
  2171. ':nnoremap <C-h> :echomsg "\\<C-h\\>"<CR>',
  2172. })
  2173. screen:expect {
  2174. grid = [[
  2175. ^ |
  2176. {4:~ }|*3
  2177. {5:[No Name] }|
  2178. |
  2179. {3:-- TERMINAL --} |
  2180. ]],
  2181. }
  2182. command([[call chansend(b:terminal_job_id, "\<C-h>")]])
  2183. screen:expect([[
  2184. ^ |
  2185. {4:~ }|*3
  2186. {5:[No Name] }|
  2187. <C-h> |
  2188. {3:-- TERMINAL --} |
  2189. ]])
  2190. end)
  2191. it('draws line with many trailing spaces correctly #24955', function()
  2192. local screen = tt.setup_child_nvim({
  2193. '-u',
  2194. 'NONE',
  2195. '-i',
  2196. 'NONE',
  2197. '--cmd',
  2198. 'set notermguicolors',
  2199. '--cmd',
  2200. 'colorscheme vim',
  2201. '--cmd',
  2202. 'call setline(1, ["1st line" .. repeat(" ", 153), "2nd line"])',
  2203. }, { cols = 80 })
  2204. screen:expect {
  2205. grid = [[
  2206. ^1st line |
  2207. |*2
  2208. 2nd line |
  2209. {5:[No Name] [+] 1,1 All}|
  2210. |
  2211. {3:-- TERMINAL --} |
  2212. ]],
  2213. }
  2214. feed_data('$')
  2215. screen:expect {
  2216. grid = [[
  2217. 1st line |
  2218. |
  2219. ^ |
  2220. 2nd line |
  2221. {5:[No Name] [+] 1,161 All}|
  2222. |
  2223. {3:-- TERMINAL --} |
  2224. ]],
  2225. }
  2226. end)
  2227. it('draws screen lines with leading spaces correctly #29711', function()
  2228. local screen = tt.setup_child_nvim({
  2229. '-u',
  2230. 'NONE',
  2231. '-i',
  2232. 'NONE',
  2233. '--cmd',
  2234. 'set foldcolumn=6 | call setline(1, ["", repeat("aabb", 1000)]) | echo 42',
  2235. }, { extra_rows = 10, cols = 66 })
  2236. screen:expect {
  2237. grid = [[
  2238. ^ |
  2239. aabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabb|*12
  2240. aabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabba@@@|
  2241. [No Name] [+] 1,0-1 Top|
  2242. 42 |
  2243. -- TERMINAL -- |
  2244. ]],
  2245. attr_ids = {},
  2246. }
  2247. feed_data('\12') -- Ctrl-L
  2248. -- The first line counts as 3 cells.
  2249. -- For the second line, 6 repeated spaces at the start counts as 2 cells,
  2250. -- so each screen line of the second line counts as 62 cells.
  2251. -- After drawing the first line and 8 screen lines of the second line,
  2252. -- 3 + 8 * 62 = 499 cells have been counted.
  2253. -- The 6 repeated spaces at the start of the next screen line exceeds the
  2254. -- 500-cell limit, so the buffer is flushed after these spaces.
  2255. screen:expect {
  2256. grid = [[
  2257. ^ |
  2258. aabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabb|*12
  2259. aabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabba@@@|
  2260. [No Name] [+] 1,0-1 Top|
  2261. |
  2262. -- TERMINAL -- |
  2263. ]],
  2264. attr_ids = {},
  2265. }
  2266. end)
  2267. it('no heap-buffer-overflow when changing &columns', function()
  2268. -- Set a different bg colour and change $TERM to something dumber so the `print_spaces()`
  2269. -- codepath in `clear_region()` is hit.
  2270. local screen = tt.setup_child_nvim({
  2271. '-u',
  2272. 'NONE',
  2273. '-i',
  2274. 'NONE',
  2275. '--cmd',
  2276. 'set notermguicolors | highlight Normal ctermbg=red',
  2277. '--cmd',
  2278. 'call setline(1, ["a"->repeat(&columns)])',
  2279. }, { env = { TERM = 'ansi' } })
  2280. screen:expect {
  2281. grid = [[
  2282. ^aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa|
  2283. ~ |*3
  2284. [No Name] [+] 1,1 All|
  2285. |
  2286. -- TERMINAL -- |
  2287. ]],
  2288. attr_ids = {},
  2289. }
  2290. feed_data(':set columns=12\n')
  2291. screen:expect {
  2292. grid = [[
  2293. ^aaaaaaaaaaaa |
  2294. aaaaaaaaaaaa |*3
  2295. < [+] 1,1 |
  2296. |
  2297. -- TERMINAL -- |
  2298. ]],
  2299. attr_ids = {},
  2300. }
  2301. -- Wider than TUI, so screen state will look weird.
  2302. -- Wait for the statusline to redraw to confirm that the TUI lives and ASAN is happy.
  2303. feed_data(':set columns=99|set stl=redrawn%m\n')
  2304. screen:expect({ any = 'redrawn%[%+%]' })
  2305. end)
  2306. end)
  2307. describe('TUI UIEnter/UILeave', function()
  2308. it('fires exactly once, after VimEnter', function()
  2309. clear()
  2310. local screen = tt.setup_child_nvim({
  2311. '-u',
  2312. 'NONE',
  2313. '-i',
  2314. 'NONE',
  2315. '--cmd',
  2316. 'colorscheme vim',
  2317. '--cmd',
  2318. 'set noswapfile noshowcmd noruler notermguicolors',
  2319. '--cmd',
  2320. 'let g:evs = []',
  2321. '--cmd',
  2322. 'autocmd UIEnter * :call add(g:evs, "UIEnter")',
  2323. '--cmd',
  2324. 'autocmd UILeave * :call add(g:evs, "UILeave")',
  2325. '--cmd',
  2326. 'autocmd VimEnter * :call add(g:evs, "VimEnter")',
  2327. })
  2328. screen:expect {
  2329. grid = [[
  2330. ^ |
  2331. {4:~ }|*3
  2332. {5:[No Name] }|
  2333. |
  2334. {3:-- TERMINAL --} |
  2335. ]],
  2336. }
  2337. feed_data(':echo g:evs\n')
  2338. screen:expect {
  2339. grid = [[
  2340. ^ |
  2341. {4:~ }|*3
  2342. {5:[No Name] }|
  2343. ['VimEnter', 'UIEnter'] |
  2344. {3:-- TERMINAL --} |
  2345. ]],
  2346. }
  2347. end)
  2348. end)
  2349. describe('TUI FocusGained/FocusLost', function()
  2350. local screen
  2351. local child_session
  2352. before_each(function()
  2353. clear()
  2354. local child_server = new_pipename()
  2355. screen = tt.setup_child_nvim({
  2356. '--listen',
  2357. child_server,
  2358. '-u',
  2359. 'NONE',
  2360. '-i',
  2361. 'NONE',
  2362. '--cmd',
  2363. 'colorscheme vim',
  2364. '--cmd',
  2365. 'set noswapfile noshowcmd noruler notermguicolors background=dark',
  2366. })
  2367. screen:expect([[
  2368. ^ |
  2369. {4:~ }|*3
  2370. {5:[No Name] }|
  2371. |
  2372. {3:-- TERMINAL --} |
  2373. ]])
  2374. child_session = n.connect(child_server)
  2375. child_session:request(
  2376. 'nvim_exec2',
  2377. [[
  2378. autocmd FocusGained * echo 'gained'
  2379. autocmd FocusLost * echo 'lost'
  2380. ]],
  2381. {}
  2382. )
  2383. feed_data('\034\016') -- CTRL-\ CTRL-N
  2384. end)
  2385. it('in normal-mode', function()
  2386. retry(2, 3 * screen.timeout, function()
  2387. feed_data('\027[I')
  2388. screen:expect([[
  2389. ^ |
  2390. {4:~ }|*3
  2391. {5:[No Name] }|
  2392. gained |
  2393. {3:-- TERMINAL --} |
  2394. ]])
  2395. feed_data('\027[O')
  2396. screen:expect([[
  2397. ^ |
  2398. {4:~ }|*3
  2399. {5:[No Name] }|
  2400. lost |
  2401. {3:-- TERMINAL --} |
  2402. ]])
  2403. end)
  2404. end)
  2405. it('in insert-mode', function()
  2406. feed_data(':set noshowmode\r')
  2407. feed_data('i')
  2408. screen:expect {
  2409. grid = [[
  2410. ^ |
  2411. {4:~ }|*3
  2412. {5:[No Name] }|
  2413. :set noshowmode |
  2414. {3:-- TERMINAL --} |
  2415. ]],
  2416. }
  2417. retry(2, 3 * screen.timeout, function()
  2418. feed_data('\027[I')
  2419. screen:expect([[
  2420. ^ |
  2421. {4:~ }|*3
  2422. {5:[No Name] }|
  2423. gained |
  2424. {3:-- TERMINAL --} |
  2425. ]])
  2426. feed_data('\027[O')
  2427. screen:expect([[
  2428. ^ |
  2429. {4:~ }|*3
  2430. {5:[No Name] }|
  2431. lost |
  2432. {3:-- TERMINAL --} |
  2433. ]])
  2434. end)
  2435. end)
  2436. -- During cmdline-mode we ignore :echo invoked by timers/events.
  2437. -- See commit: 5cc87d4dabd02167117be7a978b5c8faaa975419.
  2438. it('in cmdline-mode does NOT :echo', function()
  2439. feed_data(':')
  2440. feed_data('\027[I')
  2441. screen:expect([[
  2442. |
  2443. {4:~ }|*3
  2444. {5:[No Name] }|
  2445. :^ |
  2446. {3:-- TERMINAL --} |
  2447. ]])
  2448. feed_data('\027[O')
  2449. screen:expect {
  2450. grid = [[
  2451. |
  2452. {4:~ }|*3
  2453. {5:[No Name] }|
  2454. :^ |
  2455. {3:-- TERMINAL --} |
  2456. ]],
  2457. unchanged = true,
  2458. }
  2459. end)
  2460. it('in cmdline-mode', function()
  2461. -- Set up autocmds that modify the buffer, instead of just calling :echo.
  2462. -- This is how we can test handling of focus gained/lost during cmdline-mode.
  2463. -- See commit: 5cc87d4dabd02167117be7a978b5c8faaa975419.
  2464. child_session:request(
  2465. 'nvim_exec2',
  2466. [[
  2467. autocmd!
  2468. autocmd FocusLost * call append(line('$'), 'lost')
  2469. autocmd FocusGained * call append(line('$'), 'gained')
  2470. ]],
  2471. {}
  2472. )
  2473. retry(2, 3 * screen.timeout, function()
  2474. -- Enter cmdline-mode.
  2475. feed_data(':')
  2476. screen:sleep(1)
  2477. -- Send focus lost/gained termcodes.
  2478. feed_data('\027[O')
  2479. feed_data('\027[I')
  2480. screen:sleep(1)
  2481. -- Exit cmdline-mode. Redraws from timers/events are blocked during
  2482. -- cmdline-mode, so the buffer won't be updated until we exit cmdline-mode.
  2483. feed_data('\n')
  2484. screen:expect { any = 'lost' .. (' '):rep(46) .. '|\ngained' }
  2485. end)
  2486. end)
  2487. it('in terminal-mode', function()
  2488. feed_data(':set shell=' .. testprg('shell-test') .. ' shellcmdflag=EXE\n')
  2489. feed_data(':set noshowmode laststatus=0\n')
  2490. feed_data(':terminal zia\n')
  2491. -- Wait for terminal to be ready.
  2492. screen:expect {
  2493. grid = [[
  2494. ^ready $ zia |
  2495. |
  2496. [Process exited 0] |
  2497. |*2
  2498. :terminal zia |
  2499. {3:-- TERMINAL --} |
  2500. ]],
  2501. }
  2502. feed_data('\027[I')
  2503. screen:expect {
  2504. grid = [[
  2505. ^ready $ zia |
  2506. |
  2507. [Process exited 0] |
  2508. |*2
  2509. gained |
  2510. {3:-- TERMINAL --} |
  2511. ]],
  2512. timeout = (4 * screen.timeout),
  2513. }
  2514. feed_data('\027[O')
  2515. screen:expect([[
  2516. ^ready $ zia |
  2517. |
  2518. [Process exited 0] |
  2519. |*2
  2520. lost |
  2521. {3:-- TERMINAL --} |
  2522. ]])
  2523. end)
  2524. it('in press-enter prompt', function()
  2525. feed_data(":echom 'msg1'|echom 'msg2'|echom 'msg3'|echom 'msg4'|echom 'msg5'\n")
  2526. -- Execute :messages to provoke the press-enter prompt.
  2527. feed_data(':messages\n')
  2528. screen:expect {
  2529. grid = [[
  2530. msg1 |
  2531. msg2 |
  2532. msg3 |
  2533. msg4 |
  2534. msg5 |
  2535. {10:Press ENTER or type command to continue}^ |
  2536. {3:-- TERMINAL --} |
  2537. ]],
  2538. }
  2539. feed_data('\027[I')
  2540. feed_data('\027[I')
  2541. screen:expect {
  2542. grid = [[
  2543. msg1 |
  2544. msg2 |
  2545. msg3 |
  2546. msg4 |
  2547. msg5 |
  2548. {10:Press ENTER or type command to continue}^ |
  2549. {3:-- TERMINAL --} |
  2550. ]],
  2551. unchanged = true,
  2552. }
  2553. end)
  2554. end)
  2555. -- These tests require `tt` because --headless/--embed
  2556. -- does not initialize the TUI.
  2557. describe("TUI 't_Co' (terminal colors)", function()
  2558. local screen
  2559. local function assert_term_colors(term, colorterm, maxcolors)
  2560. clear({ env = { TERM = term }, args = {} })
  2561. screen = tt.setup_child_nvim({
  2562. '-u',
  2563. 'NONE',
  2564. '-i',
  2565. 'NONE',
  2566. '--cmd',
  2567. 'colorscheme vim',
  2568. '--cmd',
  2569. nvim_set .. ' notermguicolors',
  2570. }, {
  2571. env = {
  2572. LANG = 'C',
  2573. TERM = term or '',
  2574. COLORTERM = colorterm or '',
  2575. },
  2576. })
  2577. local tline
  2578. if maxcolors == 8 then
  2579. tline = '{9:~ }'
  2580. elseif maxcolors == 16 then
  2581. tline = '~ '
  2582. else
  2583. tline = '{4:~ }'
  2584. end
  2585. screen:expect(string.format(
  2586. [[
  2587. ^ |
  2588. %s|*4
  2589. |
  2590. {3:-- TERMINAL --} |
  2591. ]],
  2592. tline
  2593. ))
  2594. feed_data(':echo &t_Co\n')
  2595. screen:expect(string.format(
  2596. [[
  2597. ^ |
  2598. %s|*4
  2599. %-3s |
  2600. {3:-- TERMINAL --} |
  2601. ]],
  2602. tline,
  2603. tostring(maxcolors and maxcolors or '')
  2604. ))
  2605. end
  2606. -- ansi and no terminal type at all:
  2607. it('no TERM uses 8 colors', function()
  2608. assert_term_colors(nil, nil, 8)
  2609. end)
  2610. it('TERM=ansi no COLORTERM uses 8 colors', function()
  2611. assert_term_colors('ansi', nil, 8)
  2612. end)
  2613. it('TERM=ansi with COLORTERM=anything-no-number uses 16 colors', function()
  2614. assert_term_colors('ansi', 'yet-another-term', 16)
  2615. end)
  2616. it('unknown TERM COLORTERM with 256 in name uses 256 colors', function()
  2617. assert_term_colors('ansi', 'yet-another-term-256color', 256)
  2618. end)
  2619. it('TERM=ansi-256color sets 256 colours', function()
  2620. assert_term_colors('ansi-256color', nil, 256)
  2621. end)
  2622. -- Unknown terminal types:
  2623. it('unknown TERM no COLORTERM sets 8 colours', function()
  2624. assert_term_colors('yet-another-term', nil, 8)
  2625. end)
  2626. it('unknown TERM with COLORTERM=anything-no-number uses 16 colors', function()
  2627. assert_term_colors('yet-another-term', 'yet-another-term', 16)
  2628. end)
  2629. it('unknown TERM with 256 in name sets 256 colours', function()
  2630. assert_term_colors('yet-another-term-256color', nil, 256)
  2631. end)
  2632. it('unknown TERM COLORTERM with 256 in name uses 256 colors', function()
  2633. assert_term_colors('yet-another-term', 'yet-another-term-256color', 256)
  2634. end)
  2635. -- Linux kernel terminal emulator:
  2636. it('TERM=linux uses 256 colors', function()
  2637. assert_term_colors('linux', nil, 256)
  2638. end)
  2639. it('TERM=linux-16color uses 256 colors', function()
  2640. assert_term_colors('linux-16color', nil, 256)
  2641. end)
  2642. it('TERM=linux-256color uses 256 colors', function()
  2643. assert_term_colors('linux-256color', nil, 256)
  2644. end)
  2645. -- screen:
  2646. --
  2647. -- FreeBSD falls back to the built-in screen-256colour entry.
  2648. -- Linux and MacOS have a screen entry in external terminfo with 8 colours,
  2649. -- which is raised to 16 by COLORTERM.
  2650. it('TERM=screen no COLORTERM uses 8/256 colors', function()
  2651. if is_os('freebsd') then
  2652. assert_term_colors('screen', nil, 256)
  2653. else
  2654. assert_term_colors('screen', nil, 8)
  2655. end
  2656. end)
  2657. it('TERM=screen COLORTERM=screen uses 16/256 colors', function()
  2658. if is_os('freebsd') then
  2659. assert_term_colors('screen', 'screen', 256)
  2660. else
  2661. assert_term_colors('screen', 'screen', 16)
  2662. end
  2663. end)
  2664. it('TERM=screen COLORTERM=screen-256color uses 256 colors', function()
  2665. assert_term_colors('screen', 'screen-256color', 256)
  2666. end)
  2667. it('TERM=screen-256color no COLORTERM uses 256 colors', function()
  2668. assert_term_colors('screen-256color', nil, 256)
  2669. end)
  2670. -- tmux:
  2671. --
  2672. -- FreeBSD and MacOS fall back to the built-in tmux-256colour entry.
  2673. -- Linux has a tmux entry in external terminfo with 8 colours,
  2674. -- which is raised to 256.
  2675. it('TERM=tmux no COLORTERM uses 256 colors', function()
  2676. assert_term_colors('tmux', nil, 256)
  2677. end)
  2678. it('TERM=tmux COLORTERM=tmux uses 256 colors', function()
  2679. assert_term_colors('tmux', 'tmux', 256)
  2680. end)
  2681. it('TERM=tmux COLORTERM=tmux-256color uses 256 colors', function()
  2682. assert_term_colors('tmux', 'tmux-256color', 256)
  2683. end)
  2684. it('TERM=tmux-256color no COLORTERM uses 256 colors', function()
  2685. assert_term_colors('tmux-256color', nil, 256)
  2686. end)
  2687. -- xterm and imitators:
  2688. it('TERM=xterm uses 256 colors', function()
  2689. assert_term_colors('xterm', nil, 256)
  2690. end)
  2691. it('TERM=xterm COLORTERM=gnome-terminal uses 256 colors', function()
  2692. assert_term_colors('xterm', 'gnome-terminal', 256)
  2693. end)
  2694. it('TERM=xterm COLORTERM=mate-terminal uses 256 colors', function()
  2695. assert_term_colors('xterm', 'mate-terminal', 256)
  2696. end)
  2697. it('TERM=xterm-256color uses 256 colors', function()
  2698. assert_term_colors('xterm-256color', nil, 256)
  2699. end)
  2700. -- rxvt and stterm:
  2701. --
  2702. -- FreeBSD and MacOS fall back to the built-in rxvt-256color and
  2703. -- st-256colour entries.
  2704. -- Linux has an rxvt, an st, and an st-16color entry in external terminfo
  2705. -- with 8, 8, and 16 colours respectively, which are raised to 256.
  2706. it('TERM=rxvt no COLORTERM uses 256 colors', function()
  2707. assert_term_colors('rxvt', nil, 256)
  2708. end)
  2709. it('TERM=rxvt COLORTERM=rxvt uses 256 colors', function()
  2710. assert_term_colors('rxvt', 'rxvt', 256)
  2711. end)
  2712. it('TERM=rxvt-256color uses 256 colors', function()
  2713. assert_term_colors('rxvt-256color', nil, 256)
  2714. end)
  2715. it('TERM=st no COLORTERM uses 256 colors', function()
  2716. assert_term_colors('st', nil, 256)
  2717. end)
  2718. it('TERM=st COLORTERM=st uses 256 colors', function()
  2719. assert_term_colors('st', 'st', 256)
  2720. end)
  2721. it('TERM=st COLORTERM=st-256color uses 256 colors', function()
  2722. assert_term_colors('st', 'st-256color', 256)
  2723. end)
  2724. it('TERM=st-16color no COLORTERM uses 8/256 colors', function()
  2725. assert_term_colors('st', nil, 256)
  2726. end)
  2727. it('TERM=st-16color COLORTERM=st uses 16/256 colors', function()
  2728. assert_term_colors('st', 'st', 256)
  2729. end)
  2730. it('TERM=st-16color COLORTERM=st-256color uses 256 colors', function()
  2731. assert_term_colors('st', 'st-256color', 256)
  2732. end)
  2733. it('TERM=st-256color uses 256 colors', function()
  2734. assert_term_colors('st-256color', nil, 256)
  2735. end)
  2736. -- gnome and vte:
  2737. --
  2738. -- FreeBSD and MacOS fall back to the built-in vte-256color entry.
  2739. -- Linux has a gnome, a vte, a gnome-256color, and a vte-256color entry in
  2740. -- external terminfo with 8, 8, 256, and 256 colours respectively, which are
  2741. -- raised to 256.
  2742. it('TERM=gnome no COLORTERM uses 256 colors', function()
  2743. assert_term_colors('gnome', nil, 256)
  2744. end)
  2745. it('TERM=gnome COLORTERM=gnome uses 256 colors', function()
  2746. assert_term_colors('gnome', 'gnome', 256)
  2747. end)
  2748. it('TERM=gnome COLORTERM=gnome-256color uses 256 colors', function()
  2749. assert_term_colors('gnome', 'gnome-256color', 256)
  2750. end)
  2751. it('TERM=gnome-256color uses 256 colors', function()
  2752. assert_term_colors('gnome-256color', nil, 256)
  2753. end)
  2754. it('TERM=vte no COLORTERM uses 256 colors', function()
  2755. assert_term_colors('vte', nil, 256)
  2756. end)
  2757. it('TERM=vte COLORTERM=vte uses 256 colors', function()
  2758. assert_term_colors('vte', 'vte', 256)
  2759. end)
  2760. it('TERM=vte COLORTERM=vte-256color uses 256 colors', function()
  2761. assert_term_colors('vte', 'vte-256color', 256)
  2762. end)
  2763. it('TERM=vte-256color uses 256 colors', function()
  2764. assert_term_colors('vte-256color', nil, 256)
  2765. end)
  2766. -- others:
  2767. -- TODO(blueyed): this is made pending, since it causes failure + later hang
  2768. -- when using non-compatible libvterm (#9494/#10179).
  2769. pending('TERM=interix uses 8 colors', function()
  2770. assert_term_colors('interix', nil, 8)
  2771. end)
  2772. it('TERM=iTerm.app uses 256 colors', function()
  2773. assert_term_colors('iTerm.app', nil, 256)
  2774. end)
  2775. it('TERM=iterm uses 256 colors', function()
  2776. assert_term_colors('iterm', nil, 256)
  2777. end)
  2778. end)
  2779. -- These tests require `tt` because --headless/--embed
  2780. -- does not initialize the TUI.
  2781. describe("TUI 'term' option", function()
  2782. local screen
  2783. local function assert_term(term_envvar, term_expected)
  2784. clear()
  2785. screen = tt.setup_child_nvim({
  2786. '-u',
  2787. 'NONE',
  2788. '-i',
  2789. 'NONE',
  2790. '--cmd',
  2791. nvim_set .. ' notermguicolors',
  2792. }, {
  2793. env = {
  2794. LANG = 'C',
  2795. TERM = term_envvar or '',
  2796. },
  2797. })
  2798. local full_timeout = screen.timeout
  2799. retry(nil, 2 * full_timeout, function() -- Wait for TUI thread to set 'term'.
  2800. feed_data(":echo 'term='.(&term)\n")
  2801. screen:expect { any = 'term=' .. term_expected, timeout = 250 }
  2802. end)
  2803. end
  2804. it('gets builtin term if $TERM is invalid', function()
  2805. assert_term('foo', 'builtin_ansi')
  2806. end)
  2807. it('gets system-provided term if $TERM is valid', function()
  2808. if is_os('openbsd') then
  2809. assert_term('xterm', 'xterm')
  2810. elseif is_os('bsd') then -- BSD lacks terminfo, builtin is always used.
  2811. assert_term('xterm', 'builtin_xterm')
  2812. elseif is_os('mac') then
  2813. local status, _ = pcall(assert_term, 'xterm', 'xterm')
  2814. if not status then
  2815. pending('macOS: unibilium could not find terminfo')
  2816. end
  2817. else
  2818. assert_term('xterm', 'xterm')
  2819. end
  2820. end)
  2821. it('builtin terms', function()
  2822. -- These non-standard terminfos are always builtin.
  2823. assert_term('win32con', 'builtin_win32con')
  2824. assert_term('conemu', 'builtin_conemu')
  2825. assert_term('vtpcon', 'builtin_vtpcon')
  2826. end)
  2827. end)
  2828. -- These tests require `tt` because --headless/--embed
  2829. -- does not initialize the TUI.
  2830. describe('TUI', function()
  2831. local screen
  2832. local logfile = 'Xtest_tui_verbose_log'
  2833. after_each(function()
  2834. os.remove(logfile)
  2835. end)
  2836. -- Runs (child) `nvim` in a TTY (:terminal), to start the builtin TUI.
  2837. local function nvim_tui(extra_args)
  2838. clear()
  2839. screen = tt.setup_child_nvim({
  2840. '-u',
  2841. 'NONE',
  2842. '-i',
  2843. 'NONE',
  2844. '--cmd',
  2845. 'colorscheme vim',
  2846. '--cmd',
  2847. nvim_set .. ' notermguicolors',
  2848. extra_args,
  2849. }, {
  2850. env = {
  2851. LANG = 'C',
  2852. },
  2853. })
  2854. end
  2855. it('-V3log logs terminfo values', function()
  2856. nvim_tui('-V3' .. logfile)
  2857. -- Wait for TUI to start.
  2858. feed_data('Gitext')
  2859. screen:expect([[
  2860. text^ |
  2861. {4:~ }|*4
  2862. {3:-- INSERT --} |
  2863. {3:-- TERMINAL --} |
  2864. ]])
  2865. retry(nil, 3000, function() -- Wait for log file to be flushed.
  2866. local log = read_file('Xtest_tui_verbose_log') or ''
  2867. eq('--- Terminal info --- {{{\n', string.match(log, '%-%-%- Terminal.-\n')) -- }}}
  2868. ok(#log > 50)
  2869. end)
  2870. end)
  2871. it('does not crash on large inputs #26099', function()
  2872. nvim_tui()
  2873. screen:expect([[
  2874. ^ |
  2875. {4:~ }|*4
  2876. |
  2877. {3:-- TERMINAL --} |
  2878. ]])
  2879. feed_data(string.format('\027]52;c;%s\027\\', string.rep('A', 8192)))
  2880. screen:expect {
  2881. grid = [[
  2882. ^ |
  2883. {4:~ }|*4
  2884. |
  2885. {3:-- TERMINAL --} |
  2886. ]],
  2887. unchanged = true,
  2888. }
  2889. end)
  2890. it('queries the terminal for truecolor support', function()
  2891. clear()
  2892. exec_lua([[
  2893. vim.api.nvim_create_autocmd('TermRequest', {
  2894. callback = function(args)
  2895. local req = args.data
  2896. local payload = req:match('^\027P%+q([%x;]+)$')
  2897. if payload then
  2898. local t = {}
  2899. for cap in vim.gsplit(payload, ';') do
  2900. local resp = string.format('\027P1+r%s\027\\', payload)
  2901. vim.api.nvim_chan_send(vim.bo[args.buf].channel, resp)
  2902. t[vim.text.hexdecode(cap)] = true
  2903. end
  2904. vim.g.xtgettcap = t
  2905. return true
  2906. end
  2907. end,
  2908. })
  2909. ]])
  2910. local child_server = new_pipename()
  2911. screen = tt.setup_child_nvim({
  2912. '--listen',
  2913. child_server,
  2914. '-u',
  2915. 'NONE',
  2916. '-i',
  2917. 'NONE',
  2918. }, {
  2919. env = {
  2920. VIMRUNTIME = os.getenv('VIMRUNTIME'),
  2921. -- Force COLORTERM to be unset and use a TERM that does not contain Tc or RGB in terminfo.
  2922. -- This will force the nested nvim instance to query with XTGETTCAP
  2923. COLORTERM = '',
  2924. TERM = 'xterm-256colors',
  2925. },
  2926. })
  2927. screen:expect({ any = '%[No Name%]' })
  2928. local child_session = n.connect(child_server)
  2929. retry(nil, 1000, function()
  2930. eq({
  2931. Tc = true,
  2932. RGB = true,
  2933. setrgbf = true,
  2934. setrgbb = true,
  2935. }, eval("get(g:, 'xtgettcap', '')"))
  2936. eq({ true, 1 }, { child_session:request('nvim_eval', '&termguicolors') })
  2937. end)
  2938. end)
  2939. it('does not query the terminal for truecolor support if $COLORTERM is set', function()
  2940. clear()
  2941. exec_lua([[
  2942. vim.api.nvim_create_autocmd('TermRequest', {
  2943. callback = function(args)
  2944. local req = args.data
  2945. vim.g.termrequest = req
  2946. local xtgettcap = req:match('^\027P%+q([%x;]+)$')
  2947. if xtgettcap then
  2948. local t = {}
  2949. for cap in vim.gsplit(xtgettcap, ';') do
  2950. local resp = string.format('\027P1+r%s\027\\', xtgettcap)
  2951. vim.api.nvim_chan_send(vim.bo[args.buf].channel, resp)
  2952. t[vim.text.hexdecode(cap)] = true
  2953. end
  2954. vim.g.xtgettcap = t
  2955. return true
  2956. elseif req:match('^\027P$qm\027\\$') then
  2957. vim.g.decrqss = true
  2958. end
  2959. end,
  2960. })
  2961. ]])
  2962. local child_server = new_pipename()
  2963. screen = tt.setup_child_nvim({
  2964. '--listen',
  2965. child_server,
  2966. '-u',
  2967. 'NONE',
  2968. '-i',
  2969. 'NONE',
  2970. }, {
  2971. env = {
  2972. VIMRUNTIME = os.getenv('VIMRUNTIME'),
  2973. -- With COLORTERM=256, Nvim should not query the terminal and should not set 'tgc'
  2974. COLORTERM = '256',
  2975. TERM = 'xterm-256colors',
  2976. },
  2977. })
  2978. screen:expect({ any = '%[No Name%]' })
  2979. local child_session = n.connect(child_server)
  2980. retry(nil, 1000, function()
  2981. local xtgettcap = eval("get(g:, 'xtgettcap', {})")
  2982. eq(nil, xtgettcap['Tc'])
  2983. eq(nil, xtgettcap['RGB'])
  2984. eq(nil, xtgettcap['setrgbf'])
  2985. eq(nil, xtgettcap['setrgbb'])
  2986. eq(0, eval([[get(g:, 'decrqss')]]))
  2987. eq({ true, 0 }, { child_session:request('nvim_eval', '&termguicolors') })
  2988. end)
  2989. end)
  2990. it('queries the terminal for OSC 52 support', function()
  2991. clear()
  2992. exec_lua([[
  2993. vim.api.nvim_create_autocmd('TermRequest', {
  2994. callback = function(args)
  2995. local req = args.data
  2996. local payload = req:match('^\027P%+q([%x;]+)$')
  2997. if payload and vim.text.hexdecode(payload) == 'Ms' then
  2998. local resp = string.format('\027P1+r%s=%s\027\\', payload, vim.text.hexencode('\027]52;;\027\\'))
  2999. vim.api.nvim_chan_send(vim.bo[args.buf].channel, resp)
  3000. return true
  3001. end
  3002. end,
  3003. })
  3004. ]])
  3005. local child_server = new_pipename()
  3006. screen = tt.setup_child_nvim({
  3007. '--listen',
  3008. child_server,
  3009. -- Use --clean instead of -u NONE to load the osc52 plugin
  3010. '--clean',
  3011. }, {
  3012. env = {
  3013. VIMRUNTIME = os.getenv('VIMRUNTIME'),
  3014. },
  3015. })
  3016. screen:expect({ any = '%[No Name%]' })
  3017. local child_session = n.connect(child_server)
  3018. retry(nil, 1000, function()
  3019. eq({ true, { osc52 = true } }, { child_session:request('nvim_eval', 'g:termfeatures') })
  3020. end)
  3021. end)
  3022. end)
  3023. describe('TUI bg color', function()
  3024. before_each(clear)
  3025. it('is properly set in a nested Nvim instance when background=dark', function()
  3026. command('highlight clear Normal')
  3027. command('set background=dark') -- set outer Nvim background
  3028. local child_server = new_pipename()
  3029. local screen = tt.setup_child_nvim({
  3030. '--listen',
  3031. child_server,
  3032. '-u',
  3033. 'NONE',
  3034. '-i',
  3035. 'NONE',
  3036. '--cmd',
  3037. 'colorscheme vim',
  3038. '--cmd',
  3039. 'set noswapfile',
  3040. })
  3041. screen:expect({ any = '%[No Name%]' })
  3042. local child_session = n.connect(child_server)
  3043. retry(nil, nil, function()
  3044. eq({ true, 'dark' }, { child_session:request('nvim_eval', '&background') })
  3045. end)
  3046. end)
  3047. it('is properly set in a nested Nvim instance when background=light', function()
  3048. command('highlight clear Normal')
  3049. command('set background=light') -- set outer Nvim background
  3050. local child_server = new_pipename()
  3051. local screen = tt.setup_child_nvim({
  3052. '--listen',
  3053. child_server,
  3054. '-u',
  3055. 'NONE',
  3056. '-i',
  3057. 'NONE',
  3058. '--cmd',
  3059. 'colorscheme vim',
  3060. '--cmd',
  3061. 'set noswapfile',
  3062. })
  3063. screen:expect({ any = '%[No Name%]' })
  3064. local child_session = n.connect(child_server)
  3065. retry(nil, nil, function()
  3066. eq({ true, 'light' }, { child_session:request('nvim_eval', '&background') })
  3067. end)
  3068. end)
  3069. it('queries the terminal for background color', function()
  3070. exec_lua([[
  3071. vim.api.nvim_create_autocmd('TermRequest', {
  3072. callback = function(args)
  3073. local req = args.data
  3074. if req == '\027]11;?' then
  3075. vim.g.oscrequest = true
  3076. return true
  3077. end
  3078. end,
  3079. })
  3080. ]])
  3081. tt.setup_child_nvim({
  3082. '-u',
  3083. 'NONE',
  3084. '-i',
  3085. 'NONE',
  3086. '--cmd',
  3087. 'colorscheme vim',
  3088. '--cmd',
  3089. 'set noswapfile',
  3090. })
  3091. retry(nil, 1000, function()
  3092. eq(true, eval("get(g:, 'oscrequest', v:false)"))
  3093. end)
  3094. end)
  3095. it('triggers OptionSet from automatic background processing', function()
  3096. local screen = tt.setup_child_nvim({
  3097. '-u',
  3098. 'NONE',
  3099. '-i',
  3100. 'NONE',
  3101. '--cmd',
  3102. 'colorscheme vim',
  3103. '--cmd',
  3104. 'set noswapfile',
  3105. '-c',
  3106. 'autocmd OptionSet background echo "did OptionSet, yay!"',
  3107. })
  3108. screen:expect([[
  3109. ^ |
  3110. {3:~} |*3
  3111. {5:[No Name] 0,0-1 All}|
  3112. did OptionSet, yay! |
  3113. {3:-- TERMINAL --} |
  3114. ]])
  3115. end)
  3116. it('sends theme update notifications when background changes #31652', function()
  3117. command('set background=dark') -- set outer Nvim background
  3118. local child_server = new_pipename()
  3119. local screen = tt.setup_child_nvim({
  3120. '--listen',
  3121. child_server,
  3122. '-u',
  3123. 'NONE',
  3124. '-i',
  3125. 'NONE',
  3126. '--cmd',
  3127. 'colorscheme vim',
  3128. '--cmd',
  3129. 'set noswapfile',
  3130. })
  3131. screen:expect({ any = '%[No Name%]' })
  3132. local child_session = n.connect(child_server)
  3133. retry(nil, nil, function()
  3134. eq({ true, 'dark' }, { child_session:request('nvim_eval', '&background') })
  3135. end)
  3136. command('set background=light') -- set outer Nvim background
  3137. retry(nil, nil, function()
  3138. eq({ true, 'light' }, { child_session:request('nvim_eval', '&background') })
  3139. end)
  3140. end)
  3141. end)
  3142. -- These tests require `tt` because --headless/--embed
  3143. -- does not initialize the TUI.
  3144. describe('TUI as a client', function()
  3145. after_each(function()
  3146. os.remove(testlog)
  3147. end)
  3148. it('connects to remote instance (with its own TUI)', function()
  3149. local server_super = n.new_session(false)
  3150. local client_super = n.new_session(true)
  3151. set_session(server_super)
  3152. local server_pipe = new_pipename()
  3153. local screen_server = tt.setup_child_nvim({
  3154. '--listen',
  3155. server_pipe,
  3156. '-u',
  3157. 'NONE',
  3158. '-i',
  3159. 'NONE',
  3160. '--cmd',
  3161. 'colorscheme vim',
  3162. '--cmd',
  3163. nvim_set .. ' notermguicolors laststatus=2 background=dark',
  3164. })
  3165. feed_data('iHello, World')
  3166. screen_server:expect {
  3167. grid = [[
  3168. Hello, World^ |
  3169. {4:~ }|*3
  3170. {5:[No Name] [+] }|
  3171. {3:-- INSERT --} |
  3172. {3:-- TERMINAL --} |
  3173. ]],
  3174. }
  3175. feed_data('\027')
  3176. screen_server:expect {
  3177. grid = [[
  3178. Hello, Worl^d |
  3179. {4:~ }|*3
  3180. {5:[No Name] [+] }|
  3181. |
  3182. {3:-- TERMINAL --} |
  3183. ]],
  3184. }
  3185. set_session(client_super)
  3186. local screen_client = tt.setup_child_nvim({
  3187. '--server',
  3188. server_pipe,
  3189. '--remote-ui',
  3190. })
  3191. screen_client:expect {
  3192. grid = [[
  3193. Hello, Worl^d |
  3194. {4:~ }|*3
  3195. {5:[No Name] [+] }|
  3196. |
  3197. {3:-- TERMINAL --} |
  3198. ]],
  3199. }
  3200. -- grid smaller than containing terminal window is cleared properly
  3201. feed_data(":call setline(1,['a'->repeat(&columns)]->repeat(&lines))\n")
  3202. feed_data('0:set lines=3\n')
  3203. screen_server:expect {
  3204. grid = [[
  3205. ^aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa|
  3206. {5:[No Name] [+] }|
  3207. |*4
  3208. {3:-- TERMINAL --} |
  3209. ]],
  3210. }
  3211. feed_data(':q!\n')
  3212. server_super:close()
  3213. client_super:close()
  3214. end)
  3215. it('connects to remote instance (--headless)', function()
  3216. local server = n.new_session(false)
  3217. local client_super = n.new_session(true, { env = { NVIM_LOG_FILE = testlog } })
  3218. set_session(server)
  3219. local server_pipe = api.nvim_get_vvar('servername')
  3220. server:request('nvim_input', 'iHalloj!<Esc>')
  3221. server:request('nvim_command', 'set notermguicolors')
  3222. set_session(client_super)
  3223. local screen_client = tt.setup_child_nvim({
  3224. '--server',
  3225. server_pipe,
  3226. '--remote-ui',
  3227. })
  3228. screen_client:expect {
  3229. grid = [[
  3230. Halloj^! |
  3231. {4:~ }|*4
  3232. |
  3233. {3:-- TERMINAL --} |
  3234. ]],
  3235. }
  3236. -- No heap-use-after-free when receiving UI events after deadly signal #22184
  3237. server:request('nvim_input', ('a'):rep(1000))
  3238. exec_lua([[vim.uv.kill(vim.fn.jobpid(vim.bo.channel), 'sigterm')]])
  3239. screen_client:expect {
  3240. grid = [[
  3241. Vim: Caught deadly signal 'SIGTERM' |
  3242. |*2
  3243. [Process exited 1]^ |
  3244. |*2
  3245. {3:-- TERMINAL --} |
  3246. ]],
  3247. }
  3248. eq(0, api.nvim_get_vvar('shell_error'))
  3249. -- exits on input eof #22244
  3250. fn.system({ nvim_prog, '--server', server_pipe, '--remote-ui' })
  3251. eq(1, api.nvim_get_vvar('shell_error'))
  3252. client_super:close()
  3253. server:close()
  3254. if is_os('mac') then
  3255. assert_log('uv_tty_set_mode failed: Unknown system error %-102', testlog)
  3256. end
  3257. end)
  3258. it('throws error when no server exists', function()
  3259. clear()
  3260. local screen = tt.setup_child_nvim({
  3261. '--server',
  3262. '127.0.0.1:2436546',
  3263. '--remote-ui',
  3264. }, { cols = 60 })
  3265. screen:expect([[
  3266. Remote ui failed to start: {MATCH:.*}|
  3267. |
  3268. [Process exited 1]^ |
  3269. |*3
  3270. {3:-- TERMINAL --} |
  3271. ]])
  3272. end)
  3273. local function test_remote_tui_quit(status)
  3274. local server_super = n.new_session(false)
  3275. local client_super = n.new_session(true)
  3276. set_session(server_super)
  3277. local server_pipe = new_pipename()
  3278. local screen_server = tt.setup_child_nvim({
  3279. '--listen',
  3280. server_pipe,
  3281. '-u',
  3282. 'NONE',
  3283. '-i',
  3284. 'NONE',
  3285. '--cmd',
  3286. 'colorscheme vim',
  3287. '--cmd',
  3288. nvim_set .. ' notermguicolors laststatus=2 background=dark',
  3289. })
  3290. screen_server:expect {
  3291. grid = [[
  3292. ^ |
  3293. {4:~ }|*3
  3294. {5:[No Name] }|
  3295. |
  3296. {3:-- TERMINAL --} |
  3297. ]],
  3298. }
  3299. feed_data('iHello, World')
  3300. screen_server:expect {
  3301. grid = [[
  3302. Hello, World^ |
  3303. {4:~ }|*3
  3304. {5:[No Name] [+] }|
  3305. {3:-- INSERT --} |
  3306. {3:-- TERMINAL --} |
  3307. ]],
  3308. }
  3309. feed_data('\027')
  3310. screen_server:expect {
  3311. grid = [[
  3312. Hello, Worl^d |
  3313. {4:~ }|*3
  3314. {5:[No Name] [+] }|
  3315. |
  3316. {3:-- TERMINAL --} |
  3317. ]],
  3318. }
  3319. set_session(client_super)
  3320. local screen_client = tt.setup_child_nvim({
  3321. '--server',
  3322. server_pipe,
  3323. '--remote-ui',
  3324. })
  3325. screen_client:expect {
  3326. grid = [[
  3327. Hello, Worl^d |
  3328. {4:~ }|*3
  3329. {5:[No Name] [+] }|
  3330. |
  3331. {3:-- TERMINAL --} |
  3332. ]],
  3333. }
  3334. -- quitting the server
  3335. set_session(server_super)
  3336. feed_data(status and ':' .. status .. 'cquit!\n' or ':quit!\n')
  3337. status = status and status or 0
  3338. screen_server:expect {
  3339. grid = [[
  3340. |
  3341. [Process exited ]] .. status .. [[]^ {MATCH:%s+}|
  3342. |*4
  3343. {3:-- TERMINAL --} |
  3344. ]],
  3345. }
  3346. -- assert that client has exited
  3347. screen_client:expect {
  3348. grid = [[
  3349. |
  3350. [Process exited ]] .. status .. [[]^ {MATCH:%s+}|
  3351. |*4
  3352. {3:-- TERMINAL --} |
  3353. ]],
  3354. }
  3355. server_super:close()
  3356. client_super:close()
  3357. end
  3358. describe('exits when server quits', function()
  3359. it('with :quit', function()
  3360. test_remote_tui_quit()
  3361. end)
  3362. it('with :cquit', function()
  3363. test_remote_tui_quit(42)
  3364. end)
  3365. end)
  3366. end)