forked from neovim/neovim
/
message.c
3774 lines (3372 loc) · 103 KB
/
message.c
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
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// This is an open source non-commercial project. Dear PVS-Studio, please check
// it. PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
/*
* message.c: functions for displaying messages on the command line
*/
#include <assert.h>
#include <inttypes.h>
#include <stdarg.h>
#include <stdbool.h>
#include <string.h>
#include "nvim/api/private/helpers.h"
#include "nvim/ascii.h"
#include "nvim/assert.h"
#include "nvim/charset.h"
#include "nvim/eval.h"
#include "nvim/ex_docmd.h"
#include "nvim/ex_eval.h"
#include "nvim/ex_getln.h"
#include "nvim/fileio.h"
#include "nvim/func_attr.h"
#include "nvim/garray.h"
#include "nvim/getchar.h"
#include "nvim/highlight.h"
#include "nvim/input.h"
#include "nvim/keycodes.h"
#include "nvim/main.h"
#include "nvim/mbyte.h"
#include "nvim/memory.h"
#include "nvim/message.h"
#include "nvim/mouse.h"
#include "nvim/normal.h"
#include "nvim/ops.h"
#include "nvim/option.h"
#include "nvim/os/input.h"
#include "nvim/os/os.h"
#include "nvim/os/time.h"
#include "nvim/regexp.h"
#include "nvim/screen.h"
#include "nvim/strings.h"
#include "nvim/syntax.h"
#include "nvim/ui.h"
#include "nvim/ui_compositor.h"
#include "nvim/vim.h"
/*
* To be able to scroll back at the "more" and "hit-enter" prompts we need to
* store the displayed text and remember where screen lines start.
*/
typedef struct msgchunk_S msgchunk_T;
struct msgchunk_S {
msgchunk_T *sb_next;
msgchunk_T *sb_prev;
char sb_eol; // TRUE when line ends after this text
int sb_msg_col; // column in which text starts
int sb_attr; // text attributes
char_u sb_text[1]; // text to be displayed, actually longer
};
// Magic chars used in confirm dialog strings
#define DLG_BUTTON_SEP '\n'
#define DLG_HOTKEY_CHAR '&'
static int confirm_msg_used = FALSE; // displaying confirm_msg
#ifdef INCLUDE_GENERATED_DECLARATIONS
# include "message.c.generated.h"
#endif
static char_u *confirm_msg = NULL; // ":confirm" message
static char_u *confirm_msg_tail; // tail of confirm_msg
MessageHistoryEntry *first_msg_hist = NULL;
MessageHistoryEntry *last_msg_hist = NULL;
static int msg_hist_len = 0;
static FILE *verbose_fd = NULL;
static int verbose_did_open = FALSE;
bool keep_msg_more = false; // keep_msg was set by msgmore()
/*
* When writing messages to the screen, there are many different situations.
* A number of variables is used to remember the current state:
* msg_didany true when messages were written since the last time the
* user reacted to a prompt.
* Reset: After hitting a key for the hit-return prompt,
* hitting <CR> for the command line or input().
* Set: When any message is written to the screen.
* msg_didout true when something was written to the current line.
* Reset: When advancing to the next line, when the current
* text can be overwritten.
* Set: When any message is written to the screen.
* msg_nowait No extra delay for the last drawn message.
* Used in normal_cmd() before the mode message is drawn.
* emsg_on_display There was an error message recently. Indicates that there
* should be a delay before redrawing.
* msg_scroll The next message should not overwrite the current one.
* msg_scrolled How many lines the screen has been scrolled (because of
* messages). Used in update_screen() to scroll the screen
* back. Incremented each time the screen scrolls a line.
* msg_scrolled_ign TRUE when msg_scrolled is non-zero and msg_puts_attr()
* writes something without scrolling should not make
* need_wait_return to be set. This is a hack to make ":ts"
* work without an extra prompt.
* lines_left Number of lines available for messages before the
* more-prompt is to be given. -1 when not set.
* need_wait_return true when the hit-return prompt is needed.
* Reset: After giving the hit-return prompt, when the user
* has answered some other prompt.
* Set: When the ruler or typeahead display is overwritten,
* scrolling the screen for some message.
* keep_msg Message to be displayed after redrawing the screen, in
* main_loop().
* This is an allocated string or NULL when not used.
*/
// Extended msg state, currently used for external UIs with ext_messages
static const char *msg_ext_kind = NULL;
static Array msg_ext_chunks = ARRAY_DICT_INIT;
static garray_T msg_ext_last_chunk = GA_INIT(sizeof(char), 40);
static sattr_T msg_ext_last_attr = -1;
static size_t msg_ext_cur_len = 0;
static bool msg_ext_overwrite = false; ///< will overwrite last message
static int msg_ext_visible = 0; ///< number of messages currently visible
static bool msg_ext_history_visible = false;
/// Shouldn't clear message after leaving cmdline
static bool msg_ext_keep_after_cmdline = false;
static int msg_grid_pos_at_flush = 0;
static int msg_grid_scroll_discount = 0;
static void ui_ext_msg_set_pos(int row, bool scrolled)
{
char buf[MAX_MCO + 1];
size_t size = utf_char2bytes(curwin->w_p_fcs_chars.msgsep, buf);
buf[size] = '\0';
ui_call_msg_set_pos(msg_grid.handle, row, scrolled,
(String){ .data = buf, .size = size });
}
void msg_grid_set_pos(int row, bool scrolled)
{
if (!msg_grid.throttled) {
ui_ext_msg_set_pos(row, scrolled);
msg_grid_pos_at_flush = row;
}
msg_grid_pos = row;
if (msg_grid.chars) {
msg_grid_adj.row_offset = -row;
}
}
bool msg_use_grid(void)
{
return default_grid.chars && msg_use_msgsep()
&& !ui_has(kUIMessages);
}
void msg_grid_validate(void)
{
grid_assign_handle(&msg_grid);
bool should_alloc = msg_use_grid();
int max_rows = Rows - p_ch;
if (should_alloc && (msg_grid.rows != Rows || msg_grid.cols != Columns
|| !msg_grid.chars)) {
// TODO(bfredl): eventually should be set to "invalid". I e all callers
// will use the grid including clear to EOS if necessary.
grid_alloc(&msg_grid, Rows, Columns, false, true);
msg_grid.zindex = kZIndexMessages;
xfree(msg_grid.dirty_col);
msg_grid.dirty_col = xcalloc(Rows, sizeof(*msg_grid.dirty_col));
// Tricky: allow resize while pager is active
int pos = msg_scrolled ? msg_grid_pos : max_rows;
ui_comp_put_grid(&msg_grid, pos, 0, msg_grid.rows, msg_grid.cols,
false, true);
ui_call_grid_resize(msg_grid.handle, msg_grid.cols, msg_grid.rows);
msg_grid.throttled = false; // don't throttle in 'cmdheight' area
msg_scrolled_at_flush = msg_scrolled;
msg_grid.focusable = false;
msg_grid_adj.target = &msg_grid;
if (!msg_scrolled) {
msg_grid_set_pos(max_rows, false);
}
} else if (!should_alloc && msg_grid.chars) {
ui_comp_remove_grid(&msg_grid);
grid_free(&msg_grid);
XFREE_CLEAR(msg_grid.dirty_col);
ui_call_grid_destroy(msg_grid.handle);
msg_grid.throttled = false;
msg_grid_adj.row_offset = 0;
msg_grid_adj.target = &default_grid;
redraw_cmdline = true;
} else if (msg_grid.chars && !msg_scrolled && msg_grid_pos != max_rows) {
msg_grid_set_pos(max_rows, false);
}
if (msg_grid.chars && cmdline_row < msg_grid_pos) {
// TODO(bfredl): this should already be the case, but fails in some
// "batched" executions where compute_cmdrow() use stale positions or
// something.
cmdline_row = msg_grid_pos;
}
}
/// Displays the string 's' on the status line
/// When terminal not initialized (yet) mch_errmsg(..) is used.
///
/// @return TRUE if wait_return not called
int msg(char *s)
{
return msg_attr_keep(s, 0, false, false);
}
/// Like msg() but keep it silent when 'verbosefile' is set.
int verb_msg(char *s)
{
verbose_enter();
int n = msg_attr_keep(s, 0, false, false);
verbose_leave();
return n;
}
int msg_attr(const char *s, const int attr)
FUNC_ATTR_NONNULL_ARG(1)
{
return msg_attr_keep(s, attr, false, false);
}
/// Similar to msg_outtrans_attr, but support newlines and tabs.
void msg_multiline_attr(const char *s, int attr, bool check_int, bool *need_clear)
FUNC_ATTR_NONNULL_ALL
{
const char *next_spec = s;
while (next_spec != NULL) {
if (check_int && got_int) {
return;
}
next_spec = strpbrk(s, "\t\n\r");
if (next_spec != NULL) {
// Printing all char that are before the char found by strpbrk
msg_outtrans_len_attr((const char_u *)s, next_spec - s, attr);
if (*next_spec != TAB && *need_clear) {
msg_clr_eos();
*need_clear = false;
}
msg_putchar_attr((uint8_t)(*next_spec), attr);
s = next_spec + 1;
}
}
// Print the rest of the message. We know there is no special
// character because strpbrk returned NULL
if (*s != NUL) {
msg_outtrans_attr((char_u *)s, attr);
}
}
void msg_multiattr(HlMessage hl_msg, const char *kind, bool history)
{
no_wait_return++;
msg_start();
msg_clr_eos();
bool need_clear = false;
for (uint32_t i = 0; i < kv_size(hl_msg); i++) {
HlMessageChunk chunk = kv_A(hl_msg, i);
msg_multiline_attr((const char *)chunk.text.data, chunk.attr,
true, &need_clear);
}
msg_ext_set_kind(kind);
if (history && kv_size(hl_msg)) {
add_msg_hist_multiattr(NULL, 0, 0, true, hl_msg);
}
no_wait_return--;
msg_end();
}
/// @param keep set keep_msg if it doesn't scroll
bool msg_attr_keep(const char *s, int attr, bool keep, bool multiline)
FUNC_ATTR_NONNULL_ALL
{
static int entered = 0;
int retval;
char_u *buf = NULL;
if (keep && multiline) {
// Not implemented. 'multiline' is only used by nvim-added messages,
// which should avoid 'keep' behavior (just show the message at
// the correct time already).
abort();
}
// Skip messages not match ":filter pattern".
// Don't filter when there is an error.
if (!emsg_on_display && message_filtered((char_u *)s)) {
return true;
}
if (attr == 0) {
set_vim_var_string(VV_STATUSMSG, s, -1);
}
/*
* It is possible that displaying a messages causes a problem (e.g.,
* when redrawing the window), which causes another message, etc.. To
* break this loop, limit the recursiveness to 3 levels.
*/
if (entered >= 3) {
return TRUE;
}
++entered;
// Add message to history (unless it's a repeated kept message or a
// truncated message)
if ((const char_u *)s != keep_msg
|| (*s != '<'
&& last_msg_hist != NULL
&& last_msg_hist->msg != NULL
&& STRCMP(s, last_msg_hist->msg))) {
add_msg_hist(s, -1, attr, multiline);
}
// Truncate the message if needed.
msg_start();
buf = msg_strtrunc((char_u *)s, FALSE);
if (buf != NULL) {
s = (const char *)buf;
}
bool need_clear = true;
if (multiline) {
msg_multiline_attr(s, attr, false, &need_clear);
} else {
msg_outtrans_attr((char_u *)s, attr);
}
if (need_clear) {
msg_clr_eos();
}
retval = msg_end();
if (keep && retval && vim_strsize((char_u *)s) < (Rows - cmdline_row - 1) * Columns + sc_col) {
set_keep_msg((char *)s, 0);
}
need_fileinfo = false;
xfree(buf);
--entered;
return retval;
}
/// Truncate a string such that it can be printed without causing a scroll.
///
/// @return an allocated string or NULL when no truncating is done.
///
/// @param force always truncate
char_u *msg_strtrunc(char_u *s, int force)
{
char_u *buf = NULL;
int len;
int room;
// May truncate message to avoid a hit-return prompt
if ((!msg_scroll && !need_wait_return && shortmess(SHM_TRUNCALL)
&& !exmode_active && msg_silent == 0 && !ui_has(kUIMessages))
|| force) {
len = vim_strsize(s);
if (msg_scrolled != 0) {
// Use all the columns.
room = (Rows - msg_row) * Columns - 1;
} else {
// Use up to 'showcmd' column.
room = (Rows - msg_row - 1) * Columns + sc_col - 1;
}
if (len > room && room > 0) {
// may have up to 18 bytes per cell (6 per char, up to two
// composing chars)
len = (room + 2) * 18;
buf = xmalloc(len);
trunc_string(s, buf, room, len);
}
}
return buf;
}
/// Truncate a string "s" to "buf" with cell width "room".
/// "s" and "buf" may be equal.
void trunc_string(char_u *s, char_u *buf, int room_in, int buflen)
{
size_t room = room_in - 3; // "..." takes 3 chars
size_t half;
size_t len = 0;
int e;
int i;
int n;
if (*s == NUL) {
if (buflen > 0) {
*buf = NUL;
}
return;
}
if (room_in < 3) {
room = 0;
}
half = room / 2;
// First part: Start of the string.
for (e = 0; len < half && e < buflen; ++e) {
if (s[e] == NUL) {
// text fits without truncating!
buf[e] = NUL;
return;
}
n = ptr2cells(s + e);
if (len + n > half) {
break;
}
len += n;
buf[e] = s[e];
for (n = utfc_ptr2len((char *)s + e); --n > 0;) {
if (++e == buflen) {
break;
}
buf[e] = s[e];
}
}
// Last part: End of the string.
half = i = (int)STRLEN(s);
for (;;) {
do {
half = half - utf_head_off(s, s + half - 1) - 1;
} while (half > 0 && utf_iscomposing(utf_ptr2char((char *)s + half)));
n = ptr2cells(s + half);
if (len + n > room || half == 0) {
break;
}
len += n;
i = half;
}
if (i <= e + 3) {
// text fits without truncating
if (s != buf) {
len = STRLEN(s);
if (len >= (size_t)buflen) {
len = buflen - 1;
}
len = len - e + 1;
if (len < 1) {
buf[e - 1] = NUL;
} else {
memmove(buf + e, s + e, len);
}
}
} else if (e + 3 < buflen) {
// set the middle and copy the last part
memmove(buf + e, "...", (size_t)3);
len = STRLEN(s + i) + 1;
if (len >= (size_t)buflen - e - 3) {
len = buflen - e - 3 - 1;
}
memmove(buf + e + 3, s + i, len);
buf[e + 3 + len - 1] = NUL;
} else {
// can't fit in the "...", just truncate it
buf[e - 1] = NUL;
}
}
/*
* Note: Caller of smsg() and smsg_attr() must check the resulting string is
* shorter than IOSIZE!!!
*/
int smsg(const char *s, ...)
FUNC_ATTR_PRINTF(1, 2)
{
va_list arglist;
va_start(arglist, s);
vim_vsnprintf((char *)IObuff, IOSIZE, s, arglist);
va_end(arglist);
return msg((char *)IObuff);
}
int smsg_attr(int attr, const char *s, ...)
FUNC_ATTR_PRINTF(2, 3)
{
va_list arglist;
va_start(arglist, s);
vim_vsnprintf((char *)IObuff, IOSIZE, s, arglist);
va_end(arglist);
return msg_attr((const char *)IObuff, attr);
}
int smsg_attr_keep(int attr, const char *s, ...)
FUNC_ATTR_PRINTF(2, 3)
{
va_list arglist;
va_start(arglist, s);
vim_vsnprintf((char *)IObuff, IOSIZE, s, arglist);
va_end(arglist);
return msg_attr_keep((const char *)IObuff, attr, true, false);
}
/*
* Remember the last sourcing name/lnum used in an error message, so that it
* isn't printed each time when it didn't change.
*/
static int last_sourcing_lnum = 0;
static char_u *last_sourcing_name = NULL;
/// Reset the last used sourcing name/lnum. Makes sure it is displayed again
/// for the next error message;
void reset_last_sourcing(void)
{
XFREE_CLEAR(last_sourcing_name);
last_sourcing_lnum = 0;
}
/// @return TRUE if "sourcing_name" differs from "last_sourcing_name".
static int other_sourcing_name(void)
{
if (sourcing_name != NULL) {
if (last_sourcing_name != NULL) {
return STRCMP(sourcing_name, last_sourcing_name) != 0;
}
return TRUE;
}
return FALSE;
}
/// Get the message about the source, as used for an error message
///
/// @return [allocated] String with room for one more character. NULL when no
/// message is to be given.
static char *get_emsg_source(void)
FUNC_ATTR_MALLOC FUNC_ATTR_WARN_UNUSED_RESULT
{
if (sourcing_name != NULL && other_sourcing_name()) {
const char *const p = _("Error detected while processing %s:");
const size_t buf_len = STRLEN(sourcing_name) + strlen(p) + 1;
char *const buf = xmalloc(buf_len);
snprintf(buf, buf_len, p, sourcing_name);
return buf;
}
return NULL;
}
/// Get the message about the source lnum, as used for an error message.
///
/// @return [allocated] String with room for one more character. NULL when no
/// message is to be given.
static char *get_emsg_lnum(void)
FUNC_ATTR_MALLOC FUNC_ATTR_WARN_UNUSED_RESULT
{
// lnum is 0 when executing a command from the command line
// argument, we don't want a line number then
if (sourcing_name != NULL
&& (other_sourcing_name() || sourcing_lnum != last_sourcing_lnum)
&& sourcing_lnum != 0) {
const char *const p = _("line %4ld:");
const size_t buf_len = 20 + strlen(p);
char *const buf = xmalloc(buf_len);
snprintf(buf, buf_len, p, (long)sourcing_lnum);
return buf;
}
return NULL;
}
/// Display name and line number for the source of an error.
/// Remember the file name and line number, so that for the next error the info
/// is only displayed if it changed.
void msg_source(int attr)
{
no_wait_return++;
char *p = get_emsg_source();
if (p != NULL) {
msg_attr(p, attr);
xfree(p);
}
p = get_emsg_lnum();
if (p != NULL) {
msg_attr(p, HL_ATTR(HLF_N));
xfree(p);
last_sourcing_lnum = sourcing_lnum; // only once for each line
}
// remember the last sourcing name printed, also when it's empty
if (sourcing_name == NULL || other_sourcing_name()) {
xfree(last_sourcing_name);
if (sourcing_name == NULL) {
last_sourcing_name = NULL;
} else {
last_sourcing_name = vim_strsave((char_u *)sourcing_name);
}
}
--no_wait_return;
}
/// @return TRUE if not giving error messages right now:
/// If "emsg_off" is set: no error messages at the moment.
/// If "msg" is in 'debug': do error message but without side effects.
/// If "emsg_skip" is set: never do error messages.
int emsg_not_now(void)
{
if ((emsg_off > 0 && vim_strchr((char *)p_debug, 'm') == NULL
&& vim_strchr((char *)p_debug, 't') == NULL)
|| emsg_skip > 0) {
return TRUE;
}
return FALSE;
}
static bool emsg_multiline(const char *s, bool multiline)
{
int attr;
bool ignore = false;
// Skip this if not giving error messages at the moment.
if (emsg_not_now()) {
return true;
}
called_emsg = true;
// If "emsg_severe" is true: When an error exception is to be thrown,
// prefer this message over previous messages for the same command.
bool severe = emsg_severe;
emsg_severe = false;
if (!emsg_off || vim_strchr((char *)p_debug, 't') != NULL) {
// Cause a throw of an error exception if appropriate. Don't display
// the error message in this case. (If no matching catch clause will
// be found, the message will be displayed later on.) "ignore" is set
// when the message should be ignored completely (used for the
// interrupt message).
if (cause_errthrow(s, severe, &ignore)) {
if (!ignore) {
did_emsg++;
}
return true;
}
// set "v:errmsg", also when using ":silent! cmd"
set_vim_var_string(VV_ERRMSG, s, -1);
/*
* When using ":silent! cmd" ignore error messages.
* But do write it to the redirection file.
*/
if (emsg_silent != 0) {
if (!emsg_noredir) {
msg_start();
char *p = get_emsg_source();
if (p != NULL) {
const size_t p_len = strlen(p);
p[p_len] = '\n';
redir_write(p, p_len + 1);
xfree(p);
}
p = get_emsg_lnum();
if (p != NULL) {
const size_t p_len = strlen(p);
p[p_len] = '\n';
redir_write(p, p_len + 1);
xfree(p);
}
redir_write(s, strlen(s));
}
// Log (silent) errors as debug messages.
if (sourcing_name != NULL && sourcing_lnum != 0) {
DLOG("(:silent) %s (%s (line %ld))",
s, sourcing_name, (long)sourcing_lnum);
} else {
DLOG("(:silent) %s", s);
}
return true;
}
// Log editor errors as INFO.
if (sourcing_name != NULL && sourcing_lnum != 0) {
ILOG("%s (%s (line %ld))", s, sourcing_name, (long)sourcing_lnum);
} else {
ILOG("%s", s);
}
ex_exitval = 1;
// Reset msg_silent, an error causes messages to be switched back on.
msg_silent = 0;
cmd_silent = false;
if (global_busy) { // break :global command
global_busy++;
}
if (p_eb) {
beep_flush(); // also includes flush_buffers()
} else {
flush_buffers(FLUSH_MINIMAL); // flush internal buffers
}
did_emsg++; // flag for DoOneCmd()
}
emsg_on_display = true; // remember there is an error message
msg_scroll++; // don't overwrite a previous message
attr = HL_ATTR(HLF_E); // set highlight mode for error messages
if (msg_scrolled != 0) {
need_wait_return = true; // needed in case emsg() is called after
} // wait_return has reset need_wait_return
// and a redraw is expected because
// msg_scrolled is non-zero
if (msg_ext_kind == NULL) {
msg_ext_set_kind("emsg");
}
/*
* Display name and line number for the source of the error.
*/
msg_source(attr);
// Display the error message itself.
msg_nowait = false; // Wait for this msg.
return msg_attr_keep(s, attr, false, multiline);
}
/// emsg() - display an error message
///
/// Rings the bell, if appropriate, and calls message() to do the real work
/// When terminal not initialized (yet) mch_errmsg(..) is used.
///
/// @return true if wait_return not called
bool emsg(const char *s)
{
return emsg_multiline(s, false);
}
void emsg_invreg(int name)
{
semsg(_("E354: Invalid register name: '%s'"), transchar(name));
}
/// Print an error message with unknown number of arguments
bool semsg(const char *const fmt, ...)
FUNC_ATTR_PRINTF(1, 2)
{
bool ret;
va_list ap;
va_start(ap, fmt);
ret = semsgv(fmt, ap);
va_end(ap);
return ret;
}
#define MULTILINE_BUFSIZE 8192
bool semsg_multiline(const char *const fmt, ...)
{
bool ret;
va_list ap;
static char errbuf[MULTILINE_BUFSIZE];
if (emsg_not_now()) {
return true;
}
va_start(ap, fmt);
vim_vsnprintf(errbuf, sizeof(errbuf), fmt, ap);
va_end(ap);
ret = emsg_multiline(errbuf, true);
return ret;
}
/// Print an error message with unknown number of arguments
static bool semsgv(const char *fmt, va_list ap)
{
static char errbuf[IOSIZE];
if (emsg_not_now()) {
return true;
}
vim_vsnprintf(errbuf, sizeof(errbuf), fmt, ap);
return emsg(errbuf);
}
/// Same as emsg(...), but abort on error when ABORT_ON_INTERNAL_ERROR is
/// defined. It is used for internal errors only, so that they can be
/// detected when fuzzing vim.
void iemsg(const char *s)
{
emsg(s);
#ifdef ABORT_ON_INTERNAL_ERROR
abort();
#endif
}
/// Same as semsg(...) but abort on error when ABORT_ON_INTERNAL_ERROR is
/// defined. It is used for internal errors only, so that they can be
/// detected when fuzzing vim.
void siemsg(const char *s, ...)
{
va_list ap;
va_start(ap, s);
(void)semsgv(s, ap);
va_end(ap);
#ifdef ABORT_ON_INTERNAL_ERROR
abort();
#endif
}
/// Give an "Internal error" message.
void internal_error(char *where)
{
siemsg(_(e_intern2), where);
}
static void msg_semsg_event(void **argv)
{
char *s = argv[0];
(void)emsg(s);
xfree(s);
}
void msg_schedule_semsg(const char *const fmt, ...)
FUNC_ATTR_PRINTF(1, 2)
{
va_list ap;
va_start(ap, fmt);
vim_vsnprintf((char *)IObuff, IOSIZE, fmt, ap);
va_end(ap);
char *s = xstrdup((char *)IObuff);
multiqueue_put(main_loop.events, msg_semsg_event, 1, s);
}
/// Like msg(), but truncate to a single line if p_shm contains 't', or when
/// "force" is true. This truncates in another way as for normal messages.
/// Careful: The string may be changed by msg_may_trunc()!
///
/// @return a pointer to the printed message, if wait_return() not called.
char *msg_trunc_attr(char *s, bool force, int attr)
{
int n;
// Add message to history before truncating.
add_msg_hist(s, -1, attr, false);
char *ts = (char *)msg_may_trunc(force, (char_u *)s);
msg_hist_off = true;
n = msg_attr(ts, attr);
msg_hist_off = false;
if (n) {
return ts;
}
return NULL;
}
/// Check if message "s" should be truncated at the start (for filenames).
///
/// @return a pointer to where the truncated message starts.
///
/// @note: May change the message by replacing a character with '<'.
char_u *msg_may_trunc(bool force, char_u *s)
{
int room;
room = (Rows - cmdline_row - 1) * Columns + sc_col - 1;
if ((force || (shortmess(SHM_TRUNC) && !exmode_active))
&& (int)STRLEN(s) - room > 0) {
int size = vim_strsize(s);
// There may be room anyway when there are multibyte chars.
if (size <= room) {
return s;
}
int n;
for (n = 0; size >= room;) {
size -= utf_ptr2cells((char *)s + n);
n += utfc_ptr2len((char *)s + n);
}
n--;
s += n;
*s = '<';
}
return s;
}
void hl_msg_free(HlMessage hl_msg)
{
for (size_t i = 0; i < kv_size(hl_msg); i++) {
xfree(kv_A(hl_msg, i).text.data);
}
kv_destroy(hl_msg);
}
#define LINE_BUFFER_SIZE 4096
void add_hl_msg_hist(HlMessage hl_msg)
{
if (kv_size(hl_msg)) {
add_msg_hist_multiattr(NULL, 0, 0, true, hl_msg);
}
}
/// @param[in] len Length of s or -1.
static void add_msg_hist(const char *s, int len, int attr, bool multiline)
{
add_msg_hist_multiattr(s, len, attr, multiline, (HlMessage)KV_INITIAL_VALUE);
}
static void add_msg_hist_multiattr(const char *s, int len, int attr, bool multiline,
HlMessage multiattr)
{
if (msg_hist_off || msg_silent != 0) {
hl_msg_free(multiattr);
return;
}
// Don't let the message history get too big
while (msg_hist_len > MAX_MSG_HIST_LEN) {
(void)delete_first_msg();
}
// allocate an entry and add the message at the end of the history
struct msg_hist *p = xmalloc(sizeof(struct msg_hist));
if (s) {
if (len < 0) {
len = (int)STRLEN(s);
}
// remove leading and trailing newlines
while (len > 0 && *s == '\n') {
s++;
len--;
}
while (len > 0 && s[len - 1] == '\n') {
len--;
}
p->msg = (char_u *)xmemdupz(s, (size_t)len);
} else {
p->msg = NULL;
}
p->next = NULL;
p->attr = attr;
p->multiline = multiline;
p->multiattr = multiattr;
p->kind = msg_ext_kind;
if (last_msg_hist != NULL) {
last_msg_hist->next = p;
}
last_msg_hist = p;
if (first_msg_hist == NULL) {
first_msg_hist = last_msg_hist;
}
msg_hist_len++;
}
/// Delete the first (oldest) message from the history.
///
/// @return FAIL if there are no messages.
int delete_first_msg(void)
{
struct msg_hist *p;
if (msg_hist_len <= 0) {
return FAIL;
}
p = first_msg_hist;
first_msg_hist = p->next;
if (first_msg_hist == NULL) { // history is becoming empty
assert(msg_hist_len == 1);
last_msg_hist = NULL;
}
xfree(p->msg);
hl_msg_free(p->multiattr);
xfree(p);