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
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
|
local api, fn, o = vim.api, vim.fn, vim.o
local nvim_on = require('vim._core.util').nvim_on
local ui = require('vim._core.ui2')
local more_msg = api.nvim_get_hl_id_by_name('MoreMsg') -- Highlight for the [+x] indicators.
---@alias Msg { extid: integer, timer: uv.uv_timer_t? }
---@class vim._core.ui2.messages
local M = {
-- Message window. Used for regular messages with cfg.msg.target == 'msg'.
-- Automatically resizes to the text dimensions up to a point, at which point
-- only the most recent messages will fit and be shown. A timer is started for
-- each message whose callback will remove the message from the window again.
msg = {
ids = {}, ---@type table<string|integer, Msg> List of visible messages.
width = 1, -- Current width of the message window.
prev_msg = '', -- Concatenated content of the previous message.
prev_id = 0, ---@type string|integer Message id of the previous message.
dupe = 0, -- Number of times message is repeated.
},
-- Cmdline message window. Used for regular messages with cfg.msg.target == 'cmd'.
-- Also contains 'ruler', 'showcmd' and search_cmd/count messages as virt_text.
-- Messages that don't fit the 'cmdheight' are first shown in an expanded cmdline.
-- Otherwise, or after an expanded cmdline is closed upon the first keypress, the
-- cmdline contains the messages with spilled and duplicate lines indicators.
cmd = {
ids = {}, ---@type table<string|integer, Msg> List of visible messages.
msg_row = -1, -- Last row of message to distinguish for placing virt_text.
last_col = o.columns, -- Crop text to start column of 'last' virt_text.
last_msg = 0, -- Time an important message was printed that should not be overwritten.
prev_msg = '', -- Concatenated content of the previous message.
prev_id = 0, ---@type string|integer Message id of the previous message.
dupe = 0, -- Number of times message is repeated.
},
virt = { -- Stored virt_text state.
---@type [MsgContent, MsgContent, MsgContent, MsgContent]
last = { {}, {}, {}, {} }, -- Status in last cmdline row.
cmd = { {}, {} }, ---@type [MsgContent, MsgContent] # [(x)] indicators in cmd window.
msg = { {}, {} }, ---@type [MsgContent, MsgContent] # [(x)] indicators in msg window.
top = { {} }, ---@type [MsgContent] # [+x] top indicator in dialog window.
bot = { {} }, ---@type [MsgContent] # [+x] bottom indicator in dialog window.
idx = { mode = 1, search = 2, cmd = 3, ruler = 4, spill = 1, dupe = 2 },
ids = {}, ---@type { ['last'|'cmd'|'msg'|'top'|'bot']: integer? } Table of mark IDs.
delayed = false, -- Whether placement of 'last' virt_text is delayed.
},
dialog_on_key = nil, ---@type integer? vim.on_key namespace for paging in the dialog window.
cmd_on_key = nil, ---@type integer? vim.on_key namespace while cmdline is expanded.
}
--- Resolved options values from 'messagesopt', 'fillchars'.
local mopt = { maxrows = 0, msgsep = ' ', pager = nil, timeout = 4000 } ---@type table<string,any>
--- Resolves the option values used by this module.
function M.on_option_changed()
local fcs = assert(vim.opt_global.fcs:get())
mopt.msgsep = fcs.msgsep or ' '
local v = vim.opt.messagesopt:get() --[[@as table<string,string|boolean>]]
mopt.maxrows = math.ceil(o.lines * (tonumber(v.maxheight) or 50) / 100)
mopt.timeout = tonumber(v.timeout) or 4000
-- Normalize to the keytrans() form that cmd_on_key() compares against: "<cr>" => "<CR>".
local key = v.pager
mopt.pager = type(key) == 'string' and key ~= '' and fn.keytrans(vim.keycode(key)) or nil
end
-- An external redraw indicates the start of a new batch of messages in the cmdline.
api.nvim_set_decoration_provider(ui.ns, {
on_start = function()
M.cmd.ids = (ui.redrawing or M.cmd_on_key) and M.cmd.ids or {}
end,
})
--- Start a timer whose callback will remove the message from the message window.
---
---@param buf integer Buffer the message was written to.
---@param id integer|string Message ID.
function M.msg:start_timer(buf, id)
if self.ids[id].timer then
self.ids[id].timer:stop()
end
self.ids[id].timer = vim.defer_fn(function()
-- Postpone message removal when user has entered the msg window.
if api.nvim_get_current_win() == ui.wins.msg then
M.msg:start_timer(buf, id)
return
end
local extid = api.nvim_buf_is_valid(buf) and self.ids[id] and self.ids[id].extid
local mark = extid and api.nvim_buf_get_extmark_by_id(buf, ui.ns, extid, { details = true })
self.ids[id] = nil
if not mark or not mark[1] then
return
end
-- Message span marks are created with both endpoints in show_msg().
local details = assert(mark[3])
local end_row, end_col = assert(details.end_row), assert(details.end_col)
-- Clear prev_msg when line that may have dupe marker is removed.
local erow = api.nvim_buf_line_count(buf) - 1
self.prev_msg = end_row == erow and '' or self.prev_msg
-- Remove message (including potentially leftover empty line).
api.nvim_buf_set_text(buf, mark[1], mark[2], end_row, end_col, {})
if api.nvim_buf_get_lines(buf, mark[1], mark[1] + 1, false)[1] == '' then
api.nvim_buf_set_lines(buf, mark[1], mark[1] + 1, false, {})
end
-- Resize or hide message window for removed message.
if next(self.ids) then
M.set_pos('msg')
else
self:clear()
end
end, mopt.timeout)
end
--- Place or delete a virtual text mark in the cmdline or message window.
---
---@param type 'last'|'cmd'|'msg'|'top'|'bot'
---@param tgt 'cmd'|'msg'|'dialog'|'pager'
local function set_virttext(type, tgt)
if type == 'last' and (ui.cmdheight == 0 or M.virt.delayed) then
return -- Don't show virtual text while cmdline is expanded or delaying for error.
end
-- Concatenate the components of M.virt[type] and calculate the concatenated width.
local width = 0 ---@type integer
local chunks = {} ---@type [string, (integer|string)?][]
local contents = M.virt[type] ---@type MsgContent[]
for _, content in ipairs(contents) do
for _, chunk in ipairs(content) do
chunks[#chunks + 1] = { chunk[2], chunk[3] }
width = width + api.nvim_strwidth(chunk[2])
end
end
if M.virt.ids[type] and #chunks == 0 then
api.nvim_buf_del_extmark(ui.bufs[tgt], ui.ns, M.virt.ids[type])
M.cmd.last_col = type == 'last' and o.columns or M.cmd.last_col
M.virt.ids[type] = nil
elseif #chunks > 0 then
local win = ui.wins[tgt]
local line = (tgt == 'msg' or type == 'top') and 'w0' or type == 'bot' and 'w$'
local srow = line and fn.line(line, ui.wins[tgt]) - 1
local erow = tgt == 'cmd' and math.min(M.cmd.msg_row, api.nvim_buf_line_count(ui.bufs.cmd) - 1)
local texth = api.nvim_win_text_height(win, {
max_height = (type == 'top' or type == 'bot') and 1 or api.nvim_win_get_height(win),
start_row = srow or nil,
end_row = erow or nil,
})
local row = texth.end_row
local col = fn.virtcol2col(win, row + 1, texth.end_vcol)
local scol = fn.screenpos(win, row + 1, col).endcol ---@type integer
if type ~= 'last' then
-- Calculate at which column to place the virt_text such that it is at the end
-- of the last visible message line, overlapping the message text if necessary,
-- but not overlapping the 'last' virt_text.
local offset = tgt ~= 'msg' and 0
or api.nvim_win_get_position(win)[2]
+ (api.nvim_win_get_config(win).border ~= 'none' and 1 or 0)
-- Check if adding the virt_text on this line will exceed the current window width.
local maxwidth = math.max(M.msg.width, math.min(o.columns, scol - offset + width))
if tgt == 'msg' and api.nvim_win_get_width(win) < maxwidth then
api.nvim_win_resize(win, maxwidth, -1)
M.msg.width = maxwidth
end
local mwidth = tgt == 'msg' and M.msg.width or tgt == 'dialog' and o.columns or M.cmd.last_col
if scol - offset + width > mwidth then
col = fn.virtcol2col(win, row + 1, texth.end_vcol - (scol - offset + width - mwidth))
end
-- Give virt_text without its own highlight the same highlight as the message tail.
local pos, opts = { row, col }, { details = true, overlap = true, type = 'highlight' }
local hl = api.nvim_buf_get_extmarks(ui.bufs[tgt], ui.ns, pos, pos, opts)[1]
if hl then
local details = assert(hl[4])
for _, chunk in ipairs(chunks) do
chunk[2] = chunk[2] or details.hl_group
end
end
else
local mode = #M.virt.last[M.virt.idx.mode]
local pad = o.columns - width ---@type integer
local newlines = math.max(0, ui.cmdheight - texth.all)
row = row + newlines
M.cmd.last_col = mode > 0 and 0 or o.columns - (newlines > 0
|