aboutsummaryrefslogtreecommitdiff
path: root/test/functional/ui/cmdline_highlight_spec.lua
blob: 0ee994ba0a4f375a3d2d2b35928ea460ea34faaa (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
local t = require('test.testutil')
local n = require('test.functional.testnvim')()
local Screen = require('test.functional.ui.screen')

local eq = t.eq
local feed = n.feed
local clear = n.clear
local api = n.api
local fn = n.fn
local source = n.source
local exec_capture = n.exec_capture
local dedent = t.dedent
local command = n.command

local screen

-- Bug in input() handling: :redraw! will erase the whole prompt up until
-- user types something. It exists in Vim as well, so using `h<BS>` as
-- a workaround.
local function redraw_input()
  feed('{REDRAW}h<BS>')
end

before_each(function()
  clear()
  screen = Screen.new(40, 8)
  source([[
    highlight RBP1 guibg=Red
    highlight RBP2 guibg=Yellow
    highlight RBP3 guibg=Green
    highlight RBP4 guibg=Blue
    let g:NUM_LVLS = 4
    function Redraw()
      mode
      return "\<Ignore>"
    endfunction
    let g:id = ''
    cnoremap <expr> {REDRAW} Redraw()
    function DoPrompt(do_return) abort
      let id = g:id
      let Cb = g:Nvim_color_input{g:id}
      let out = input({'prompt': ':', 'highlight': Cb})
      let g:out{id} = out
      return (a:do_return ? out : "\<Ignore>")
    endfunction
    nnoremap <expr> {PROMPT} DoPrompt(0)
    cnoremap <expr> {PROMPT} DoPrompt(1)
    function RainBowParens(cmdline)
      let ret = []
      let i = 0
      let lvl = 0
      while i < len(a:cmdline)
        if a:cmdline[i] is# '('
          call add(ret, [i, i + 1, 'RBP' . ((lvl % g:NUM_LVLS) + 1)])
          let lvl += 1
        elseif a:cmdline[i] is# ')'
          let lvl -= 1
          call add(ret, [i, i + 1, 'RBP' . ((lvl % g:NUM_LVLS) + 1)])
        endif
        let i += 1
      endwhile
      return ret
    endfunction
    function SplitMultibyteStart(cmdline)
      let ret = []
      let i = 0
      while i < len(a:cmdline)
        let char = nr2char(char2nr(a:cmdline[i:]))
        if a:cmdline[i:i +  len(char) - 1] is# char
          if len(char) > 1
            call add(ret, [i + 1, i + len(char), 'RBP2'])
          endif
          let i += len(char)
        else
          let i += 1
        endif
      endwhile
      return ret
    endfunction
    function SplitMultibyteEnd(cmdline)
      let ret = []
      let i = 0
      while i < len(a:cmdline)
        let char = nr2char(char2nr(a:cmdline[i:]))
        if a:cmdline[i:i +  len(char) - 1] is# char
          if len(char) > 1
            call add(ret, [i, i + 1, 'RBP1'])
          endif
          let i += len(char)
        else
          let i += 1
        endif
      endwhile
      return ret
    endfunction
    function Echoing(cmdline)
      echo 'HERE'
      return v:_null_list
    endfunction
    function Echoning(cmdline)
      echon 'HERE'
      return v:_null_list
    endfunction
    function Echomsging(cmdline)
      echomsg 'HERE'
      return v:_null_list
    endfunction
    function Echoerring(cmdline)
      echoerr 'HERE'
      return v:_null_list
    endfunction
    function Redrawing(cmdline)
      redraw!
      return v:_null_list
    endfunction
    function Throwing(cmdline)
      throw "ABC"
      return v:_null_list
    endfunction
    function Halting(cmdline)
      while 1
      endwhile
    endfunction
    function ReturningGlobal(cmdline)
      return g:callback_return
    endfunction
    function ReturningGlobal2(cmdline)
      return g:callback_return[:len(a:cmdline)-1]
    endfunction
    function ReturningGlobalN(n, cmdline)
      return g:callback_return{a:n}
    endfunction
    let g:recording_calls = []
    function Recording(cmdline)
      call add(g:recording_calls, a:cmdline)
      return []
    endfunction
  ]])
  screen:set_default_attr_ids({
    RBP1 = { background = Screen.colors.Red },
    RBP2 = { background = Screen.colors.Yellow },
    RBP3 = { background = Screen.colors.Green },
    RBP4 = { background = Screen.colors.Blue },
    EOB = { bold = true, foreground = Screen.colors.Blue1 },
    ERR = { foreground = Screen.colors.Grey100, background = Screen.colors.Red },
    SK = { foreground = Screen.colors.Blue },
    PE = { bold = true, foreground = Screen.colors.SeaGreen4 },
    NUM = { foreground = Screen.colors.Blue2 },
    NPAR = { foreground = Screen.colors.Yellow },
    SQ = { foreground = Screen.colors.Blue3 },
    SB = { foreground = Screen.colors.Blue4 },
    E = { foreground = Screen.colors.Red, background = Screen.colors.Blue },
    M = { bold = true },
    MSEP = { bold = true, reverse = true },
  })
end)

local function set_color_cb(funcname, callback_return, id)
  api.nvim_set_var('id', id or '')
  if id and id ~= '' and fn.exists('*' .. funcname .. 'N') then
    command(('let g:Nvim_color_input%s = {cmdline -> %sN(%s, cmdline)}'):format(id, funcname, id))
    if callback_return then
      api.nvim_set_var('callback_return' .. id, callback_return)
    end
  else
    api.nvim_set_var('Nvim_color_input', funcname)
    if callback_return then
      api.nvim_set_var('callback_return', callback_return)
    end
  end
end
local function start_prompt(text)
  feed('{PROMPT}' .. (text or ''))
end

describe('Command-line coloring', function()
  it('works', function()
    set_color_cb('RainBowParens')
    api.nvim_set_option_value('more', false, {})
    start_prompt()
    screen:expect([[
                                              |
      {EOB:~                                       }|*6
      :^                                       |
    ]])
    feed('e')
    screen:expect([[
                                              |
      {EOB:~                                       }|*6
      :e^                                      |
    ]])
    feed('cho ')
    screen:expect([[
                                              |
      {EOB:~                                       }|*6
      :echo ^                                  |
    ]])
    feed('(')
    screen:expect([[
                                              |
      {EOB:~                                       }|*6
      :echo {RBP1:(}^                                 |
    ]])
    feed('(')
    screen:expect([[
                                              |
      {EOB:~                                       }|*6
      :echo {RBP1:(}{RBP2:(}^                                |
    ]])
    feed('42')
    screen:expect([[
                                              |
      {EOB:~                                       }|*6
      :echo {RBP1:(}{RBP2:(}42^                              |
    ]])
    feed('))')
    screen:expect([[
                                              |
      {EOB:~                                       }|*6
      :echo {RBP1:(}{RBP2:(}42{RBP2:)}{RBP1:)}^                            |
    ]])
    feed('<BS>')
    screen:expect([[
                                              |
      {EOB:~                                       }|*6
      :echo {RBP1:(}{RBP2:(}42{RBP2:)}^                             |
    ]])
    redraw_input()
    screen:expect {
      grid = [[
                                              |
      {EOB:~                                       }|*6
      :echo {RBP1:(}{RBP2:(}42{RBP2:)}^                             |
    ]],
      reset = true,
    }
  end)
  for _, func_part in ipairs({ '', 'n', 'msg' }) do
    it('disables :echo' .. func_part .. ' messages', function()
      set_color_cb('Echo' .. func_part .. 'ing')
      start_prompt('echo')
      screen:expect([[
                                                |
        {EOB:~                                       }|*6
        :echo^                                   |
      ]])
    end)
  end
  it('does the right thing when hl start appears to split multibyte char', function()
    set_color_cb('SplitMultibyteStart')
    start_prompt('echo "«')
    screen:expect {
      grid = [[
                                              |
      {EOB:~                                       }|*2
      {MSEP:                                        }|
      :echo "                                 |
      {ERR:E5405: Chunk 0 start 7 splits multibyte }|
      {ERR:character}                               |
      :echo "«^                                |
    ]],
    }
    feed('»')
    screen:expect([[
                                              |
      {EOB:~                                       }|*2
      {MSEP:                                        }|
      :echo "                                 |
      {ERR:E5405: Chunk 0 start 7 splits multibyte }|
      {ERR:character}                               |
      :echo "«»^                               |
    ]])
  end)
  it('does the right thing when hl end appears to split multibyte char', function()
    set_color_cb('SplitMultibyteEnd')
    start_prompt('echo "«')
    screen:expect([[
                                              |
      {EOB:~                                       }|*2
      {MSEP:                                        }|
      :echo "                                 |
      {ERR:E5406: Chunk 0 end 7 splits multibyte ch}|
      {ERR:aracter}                                 |
      :echo "«^                                |
    ]])
  end)
  it('does the right thing when erroring', function()
    set_color_cb('Echoerring')
    start_prompt('e')
    screen:expect([[
                                              |
      {EOB:~                                       }|
      {MSEP:                                        }|
      :                                       |
      {ERR:E5407: Callback has thrown an exception:}|
      {ERR: function DoPrompt[3]..Echoerring, line }|
      {ERR:1: Vim(echoerr):HERE}                    |
      :e^                                      |
    ]])
  end)
  it('silences :echo', function()
    set_color_cb('Echoing')
    start_prompt('e')
    screen:expect([[
                                              |
      {EOB:~                                       }|*6
      :e^                                      |
    ]])
    eq('', exec_capture('messages'))
  end)
  it('silences :echon', function()
    set_color_cb('Echoning')
    start_prompt('e')
    screen:expect([[
                                              |
      {EOB:~                                       }|*6
      :e^                                      |
    ]])
    eq('', exec_capture('messages'))
  end)
  it('silences :echomsg', function()
    set_color_cb('Echomsging')
    start_prompt('e')
    screen:expect([[
                                              |
      {EOB:~                                       }|*6
      :e^                                      |
    ]])
    eq('', exec_capture('messages'))
  end)
  it('does the right thing when throwing', function()
    set_color_cb('Throwing')
    start_prompt('e')
    screen:expect([[
                                              |
      {EOB:~                                       }|
      {MSEP:                                        }|
      :                                       |
      {ERR:E5407: Callback has thrown an exception:}|
      {ERR: function DoPrompt[3]..Throwing, line 1:}|
      {ERR: ABC}                                    |
      :e^                                      |
    ]])
  end)
  it('stops executing callback after a number of errors', function()
    set_color_cb('SplitMultibyteStart')
    start_prompt('let x = "«»«»«»«»«»"')
    screen:expect([[
                                              |
      {EOB:~                                       }|*2
      {MSEP:                                        }|
      :let x = "                              |
      {ERR:E5405: Chunk 0 start 10 splits multibyte}|
      {ERR: character}                              |
      :let x = "«»«»«»«»«»"^                   |
    ]])
    feed('\n')
    screen:expect([[
      ^                                        |
      {EOB:~                                       }|*6
                                              |
    ]])
    feed('\n')
    eq('let x = "«»«»«»«»«»"', api.nvim_get_var('out'))
    local msg = '\nE5405: Chunk 0 start 10 splits multibyte character'
    eq(msg:rep(1), fn.execute('messages'))
  end)
  it('allows interrupting callback with <C-c>', function()
    set_color_cb('Halting')
    start_prompt('echo 42')
    screen:expect([[
      ^                                        |
      {EOB:~                                       }|*6
                                              |
    ]])
    screen:sleep(500)
    feed('<C-c>')
    screen:expect([[
                                              |
      {EOB:~                                       }|*2
      {MSEP:                                        }|
      :                                       |
      {ERR:E5407: Callback has thrown an exception:}|
      {ERR: Keyboard interrupt}                     |
      :echo 42^                                |
    ]])
    redraw_input()
    screen:expect([[
                                              |
      {EOB:~                                       }|*6
      :echo 42^                                |
    ]])
    feed('\n')
    screen:expect([[
      ^                                        |
      {EOB:~                                       }|*6
      :echo 42                                |
    ]])
    feed('\n')
    eq('echo 42', api.nvim_get_var('out'))
    feed('<C-c>')
    screen:expect([[
      ^                                        |
      {EOB:~                                       }|*6
      Type  :qa  and pre...nter> to exit Nvim |
    ]])
  end)
  it('works fine with NUL, NL, CR', function()
    set_color_cb('RainBowParens')
    start_prompt('echo ("<C-v><CR><C-v><Nul><C-v><NL>")')
    screen:expect([[
                                              |
      {EOB:~                                       }|*6
      :echo {RBP1:(}"{SK:^M^@^@}"{RBP1:)}^                        |
    ]])
  end)
  it('errors out when callback returns something wrong', function()
    command('cnoremap + ++')
    set_color_cb('ReturningGlobal', '')
    start_prompt('#')
    screen:expect([[
                                              |
      {EOB:~                                       }|*3
      {MSEP:                                        }|
      :                                       |
      {ERR:E5400: Callback should return list}      |
      :#^                                      |
    ]])

    feed('<CR><CR><CR>')
    set_color_cb('ReturningGlobal', { { 0, 1, 'Normal' }, 42 })
    start_prompt('#')
    screen:expect([[
                                              |
      {EOB:~                                       }|*3
      {MSEP:                                        }|
      :                                       |
      {ERR:E5401: List item 1 is not a List}        |
      :#^                                      |
    ]])

    feed('<CR><CR><CR>')
    set_color_cb('ReturningGlobal2', { { 0, 1, 'Normal' }, { 1 } })
    start_prompt('+')
    screen:expect([[
                                              |
      {EOB:~                                       }|*2
      {MSEP:                                        }|
      :+                                      |
      {ERR:E5402: List item 1 has incorrect length:}|
      {ERR: 1 /= 3}                                 |
      :++^                                     |
    ]])

    feed('<CR><CR><CR>')
    set_color_cb('ReturningGlobal2', { { 0, 1, 'Normal' }, { 2, 3, 'Normal' } })
    start_prompt('+')
    screen:expect([[
                                              |
      {EOB:~                                       }|*2
      {MSEP:                                        }|
      :+                                      |
      {ERR:E5403: Chunk 1 start 2 not in range [1, }|
      {ERR:2)}                                      |
      :++^                                     |
    ]])

    feed('<CR><CR><CR>')
    set_color_cb('ReturningGlobal2', { { 0, 1, 'Normal' }, { 1, 3, 'Normal' } })
    start_prompt('+')
    screen:expect([[
                                              |
      {EOB:~                                       }|*3
      {MSEP:                                        }|
      :+                                      |
      {ERR:E5404: Chunk 1 end 3 not in range (1, 2]}|
      :++^                                     |
    ]])
  end)
  it('does not error out when called from a errorred out cycle', function()
    set_color_cb('ReturningGlobal', { { 0, 1, 'Normal' } })
    feed(dedent([[
      :set regexpengine=2
      :for pat in [' \ze*', ' \zs*']
      :  try
      :    let l = matchlist('x x', pat)
      :    $put =input({'prompt':'>','highlight':'ReturningGlobal'})
      :
      :    $put ='E888 NOT detected for ' . pat
      :  catch
      :    $put =input({'prompt':'>','highlight':'ReturningGlobal'})
      :
      :    $put ='E888 detected for ' . pat
      :  endtry
      :endfor
      :
      :
      :
      :
      :
      :
    ]]))
    eq(
      { '', ':', 'E888 detected for  \\ze*', ':', 'E888 detected for  \\zs*' },
      api.nvim_buf_get_lines(0, 0, -1, false)
    )
    eq('', fn.execute('messages'))
  end)
  it('allows nesting input()s', function()
    set_color_cb('ReturningGlobal', { { 0, 1, 'RBP1' } }, '')
    start_prompt('1')
    screen:expect([[
                                              |
      {EOB:~                                       }|*6
      :{RBP1:1}^                                      |
    ]])

    set_color_cb('ReturningGlobal', { { 0, 1, 'RBP2' } }, '1')
    start_prompt('2')
    screen:expect([[
                                              |
      {EOB:~                                       }|*6
      :{RBP2:2}^                                      |
    ]])

    set_color_cb('ReturningGlobal', { { 0, 1, 'RBP3' } }, '2')
    start_prompt('3')
    screen:expect([[
                                              |
      {EOB:~                                       }|*6
      :{RBP3:3}^                                      |
    ]])

    set_color_cb('ReturningGlobal', { { 0, 1, 'RBP4' } }, '3')
    start_prompt('4')
    screen:expect([[
                                              |
      {EOB:~                                       }|*6
      :{RBP4:4}^                                      |
    ]])

    feed('<CR>')
    screen:expect([[
                                              |
      {EOB:~                                       }|*6
      :{RBP3:3}4^                                     |
    ]])
    feed('<CR>')
    screen:expect([[
                                              |
      {EOB:~                                       }|*6
      :{RBP2:2}34^                                    |
    ]])
    feed('<CR>')
    screen:expect([[
                                              |
      {EOB:~                                       }|*6
      :{RBP1:1}234^                                   |
    ]])
    feed('<CR><CR><C-l>')
    screen:expect([[
      ^                                        |
      {EOB:~                                       }|*6
                                              |
    ]])
    eq('1234', api.nvim_get_var('out'))
    eq('234', api.nvim_get_var('out1'))
    eq('34', api.nvim_get_var('out2'))
    eq('4', api.nvim_get_var('out3'))
    eq(0, fn.exists('g:out4'))
  end)
  it('runs callback with the same data only once', function()
    local function new_recording_calls(...)
      eq({ ... }, api.nvim_get_var('recording_calls'))
      api.nvim_set_var('recording_calls', {})
    end
    set_color_cb('Recording')
    start_prompt('')
    -- Regression test. Disambiguation:
    --
    --     new_recording_calls(expected_result) -- (actual_before_fix)
    --
    feed('a')
    new_recording_calls('a') -- ('a', 'a')
    feed('b')
    new_recording_calls('ab') -- ('a', 'ab', 'ab')
    feed('c')
    new_recording_calls('abc') -- ('ab', 'abc', 'abc')
    feed('<BS>')
    new_recording_calls('ab') -- ('abc', 'ab', 'ab')
    feed('<BS>')
    new_recording_calls('a') -- ('ab', 'a', 'a')
    feed('<BS>')
    new_recording_calls() -- ('a')
    feed('<CR><CR>')
    eq('', api.nvim_get_var('out'))
  end)
  it('does not crash when callback has caught not-a-editor-command exception', function()
    source([[
      function CaughtExc(cmdline) abort
        try
          gibberish
        catch
          " Do nothing
        endtry
        return []
      endfunction
    ]])
    set_color_cb('CaughtExc')
    start_prompt('1')
    eq(1, api.nvim_eval('1'))
  end)
end)
describe('Ex commands coloring', function()
  it('works', function()
    api.nvim_set_var('Nvim_color_cmdline', 'RainBowParens')
    feed(':echo (((1)))')
    screen:expect([[
                                              |
      {EOB:~                                       }|*6
      :echo {RBP1:(}{RBP2:(}{RBP3:(}1{RBP3:)}{RBP2:)}{RBP1:)}^                           |
    ]])
  end)
  it('still executes command-line even if errored out', function()
    api.nvim_set_var('Nvim_color_cmdline', 'SplitMultibyteStart')
    feed(':let x = "«"\n')
    eq('«', api.nvim_get_var('x'))
    local msg = 'E5405: Chunk 0 start 10 splits multibyte character'
    eq('\n' .. msg, fn.execute('messages'))
  end)
  it('does not error out when called from a errorred out cycle', function()
    -- Apparently when there is a cycle in which one of the commands errors out
    -- this error may be caught by color_cmdline before it is presented to the
    -- user.
    feed(dedent([[
      :set regexpengine=2
      :for pat in [' \ze*', ' \zs*']
      :  try
      :    let l = matchlist('x x', pat)
      :    $put ='E888 NOT detected for ' . pat
      :  catch
      :    $put ='E888 detected for ' . pat
      :  endtry
      :endfor
    ]]))
    eq(
      { '', 'E888 detected for  \\ze*', 'E888 detected for  \\zs*' },
      api.nvim_buf_get_lines(0, 0, -1, false)
    )
    eq('', fn.execute('messages'))
  end)
  it('does not crash when using `n` in debug mode', function()
    feed(':debug execute "echo 1"\n')
    screen:expect([[
                                              |
      {EOB:~                                       }|*2
      {MSEP:                                        }|
      Entering Debug mode.  Type "cont" to con|
      tinue.                                  |
      cmd: execute "echo 1"                   |
      >^                                       |
    ]])
    feed('n\n')
    screen:expect([[
                                              |
      {MSEP:                                        }|
      Entering Debug mode.  Type "cont" to con|
      tinue.                                  |
      cmd: execute "echo 1"                   |
      >n                                      |
      1                                       |
      {PE:Press ENTER or type command to continue}^ |
    ]])
    feed('\n')
    screen:expect([[
      ^                                        |
      {EOB:~                                       }|*6
                                              |
    ]])
  end)
  it('mapping error does not cancel prompt', function()
    command("cnoremap <expr> x execute('throw 42')[-1]")
    feed(':#x')
    screen:expect([[
                                              |
      {EOB:~                                       }|*2
      {MSEP:                                        }|
      :#                                      |
      {ERR:Error detected while processing :}       |
      {ERR:E605: Exception not caught: 42}          |
      :#^                                      |
    ]])
    feed('<CR>')
    screen:expect([[
                                              |
      {EOB:~                                       }|
      {MSEP:                                        }|
      :#                                      |
      {ERR:Error detected while processing :}       |
      {ERR:E605: Exception not caught: 42}          |
      {ERR:E749: Empty buffer}                      |
      {PE:Press ENTER or type command to continue}^ |
    ]])
    feed('<CR>')
    eq(
      'Error detected while processing :\nE605: Exception not caught: 42\nE749: Empty buffer',
      exec_capture('messages')
    )
  end)
  it('errors out when failing to get callback', function()
    api.nvim_set_var('Nvim_color_cmdline', 42)
    feed(':#')
    screen:expect([[
                                              |
      {EOB:~                                       }|
      {MSEP:                                        }|
      :                                       |
      {ERR:E5408: Unable to get g:Nvim_color_cmdlin}|
      {ERR:e callback: Vim:E6000: Argument is not a}|
      {ERR: function or function name}              |
      :#^                                      |
    ]])
  end)
end)
describe('Expressions coloring support', function()
  it('works', function()
    command('hi clear NvimNumber')
    command('hi clear NvimNestingParenthesis')
    command('hi NvimNumber guifg=Blue2')
    command('hi NvimNestingParenthesis guifg=Yellow')
    feed(':echo <C-r>=(((1)))')
    screen:expect([[
                                              |
      {EOB:~                                       }|*6
      ={NPAR:(((}{NUM:1}{NPAR:)))}^                                |
    ]])
  end)
  it('does not use Nvim_color_expr', function()
    api.nvim_set_var('Nvim_color_expr', 42)
    -- Used to error out due to failing to get callback.
    command('hi clear NvimNumber')
    command('hi NvimNumber guifg=Blue2')
    feed(':<C-r>=1')
    screen:expect([[
                                              |
      {EOB:~                                       }|*6
      ={NUM:1}^                                      |
    ]])
  end)
  it('works correctly with non-ASCII and control characters', function()
    command('hi clear NvimStringBody')
    command('hi clear NvimStringQuote')
    command('hi clear NvimInvalid')
    command('hi NvimStringQuote guifg=Blue3')
    command('hi NvimStringBody guifg=Blue4')
    command('hi NvimInvalid guifg=Red guibg=Blue')
    feed('i<C-r>="«»"«»')
    screen:expect([[
                                              |
      {EOB:~                                       }|*6
      ={SQ:"}{SB:«»}{SQ:"}{E:«»}^                                 |
    ]])
    feed('<C-c>')
    screen:expect([[
      ^                                        |
      {EOB:~                                       }|*6
      {M:-- INSERT --}                            |
    ]])
    feed('<Esc>')
    screen:expect([[
      ^                                        |
      {EOB:~                                       }|*6
                                              |
    ]])
    feed(':<C-\\>e"<C-v><C-x>"<C-v><C-x>')
    -- TODO(ZyX-I): Parser highlighting should not override special character
    --              highlighting.
    screen:expect([[
                                              |
      {EOB:~                                       }|*6
      ={SQ:"}{SB:^X}{SQ:"}{ERR:^X}^                                 |
    ]])
    feed('<C-c>')
    screen:expect([[
                                              |
      {EOB:~                                       }|*6
      :^                                       |
    ]])
    fn.setreg('a', { '\192' })
    feed('<C-r>="<C-r><C-r>a"<C-r><C-r>a"foo"')
    screen:expect([[
                                              |
      {EOB:~                                       }|*6
      ={SQ:"}{SB:<c0>}{SQ:"}{E:<c0>"}{SB:foo}{E:"}^                        |
    ]])
  end)
end)