/
message.c
4169 lines (3792 loc) · 93.4 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
/* vi:set ts=8 sts=4 sw=4 noet:
*
* VIM - Vi IMproved by Bram Moolenaar
*
* Do ":help uganda" in Vim to read copying and usage conditions.
* Do ":help credits" in Vim to see a list of people who contributed.
* See README.txt for an overview of the Vim source code.
*/
/*
* message.c: functions for displaying messages on the command line
*/
#define MESSAGE_FILE // don't include prototype for smsg()
#include "vim.h"
static void add_msg_hist(char_u *s, int len, int attr);
static void hit_return_msg(void);
static void msg_home_replace_attr(char_u *fname, int attr);
static void msg_puts_attr_len(char *str, int maxlen, int attr);
static void msg_puts_display(char_u *str, int maxlen, int attr, int recurse);
static void msg_scroll_up(void);
static void inc_msg_scrolled(void);
static void store_sb_text(char_u **sb_str, char_u *s, int attr, int *sb_col, int finish);
static void t_puts(int *t_col, char_u *t_s, char_u *s, int attr);
static void msg_puts_printf(char_u *str, int maxlen);
static int do_more_prompt(int typed_char);
static void msg_screen_putchar(int c, int attr);
static void msg_moremsg(int full);
static int msg_check_screen(void);
static void redir_write(char_u *s, int maxlen);
#ifdef FEAT_CON_DIALOG
static char_u *msg_show_console_dialog(char_u *message, char_u *buttons, int dfltbutton);
static int confirm_msg_used = FALSE; // displaying confirm_msg
static char_u *confirm_msg = NULL; // ":confirm" message
static char_u *confirm_msg_tail; // tail of confirm_msg
static void display_confirm_msg(void);
#endif
#ifdef FEAT_JOB_CHANNEL
static int emsg_to_channel_log = FALSE;
#endif
struct msg_hist
{
struct msg_hist *next;
char_u *msg;
int attr;
};
static struct msg_hist *first_msg_hist = NULL;
static struct msg_hist *last_msg_hist = NULL;
static int msg_hist_len = 0;
static FILE *verbose_fd = NULL;
static int verbose_did_open = FALSE;
/*
* 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.
*/
/*
* msg(s) - 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);
}
/*
* Like msg() but keep it silent when 'verbosefile' is set.
*/
int
verb_msg(char *s)
{
int n;
verbose_enter();
n = msg_attr_keep(s, 0, FALSE);
verbose_leave();
return n;
}
int
msg_attr(char *s, int attr)
{
return msg_attr_keep(s, attr, FALSE);
}
int
msg_attr_keep(
char *s,
int attr,
int keep) // TRUE: set keep_msg if it doesn't scroll
{
static int entered = 0;
int retval;
char_u *buf = NULL;
// Skip messages not matching ":filter pattern".
// Don't filter when there is an error.
if (!emsg_on_display && message_filtered((char_u *)s))
return TRUE;
#ifdef FEAT_EVAL
if (attr == 0)
set_vim_var_string(VV_STATUSMSG, (char_u *)s, -1);
#endif
/*
* 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 ((char_u *)s != keep_msg
|| (*s != '<'
&& last_msg_hist != NULL
&& last_msg_hist->msg != NULL
&& STRCMP(s, last_msg_hist->msg)))
add_msg_hist((char_u *)s, -1, attr);
#ifdef FEAT_JOB_CHANNEL
if (emsg_to_channel_log)
// Write message in the channel log.
ch_log(NULL, "ERROR: %s", s);
#endif
// Truncate the message if needed.
msg_start();
buf = msg_strtrunc((char_u *)s, FALSE);
if (buf != NULL)
s = (char *)buf;
msg_outtrans_attr((char_u *)s, attr);
msg_clr_eos();
retval = msg_end();
if (keep && retval && vim_strsize((char_u *)s)
< (int)(Rows - cmdline_row - 1) * Columns + sc_col)
set_keep_msg((char_u *)s, 0);
need_fileinfo = FALSE;
vim_free(buf);
--entered;
return retval;
}
/*
* Truncate a string such that it can be printed without causing a scroll.
* Returns an allocated string or NULL when no truncating is done.
*/
char_u *
msg_strtrunc(
char_u *s,
int force) // always truncate
{
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) || force)
{
len = vim_strsize(s);
if (msg_scrolled != 0)
// Use all the columns.
room = (int)(Rows - msg_row) * Columns - 1;
else
// Use up to 'showcmd' column.
room = (int)(Rows - msg_row - 1) * Columns + sc_col - 1;
if (len > room && room > 0)
{
if (enc_utf8)
// may have up to 18 bytes per cell (6 per char, up to two
// composing chars)
len = (room + 2) * 18;
else if (enc_dbcs == DBCS_JPNU)
// may have up to 2 bytes per cell for euc-jp
len = (room + 2) * 2;
else
len = room + 2;
buf = alloc(len);
if (buf != NULL)
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];
if (has_mbyte)
for (n = (*mb_ptr2len)(s + e); --n > 0; )
{
if (++e == buflen)
break;
buf[e] = s[e];
}
}
// Last part: End of the string.
i = e;
if (enc_dbcs != 0)
{
// For DBCS going backwards in a string is slow, but
// computing the cell width isn't too slow: go forward
// until the rest fits.
n = vim_strsize(s + i);
while (len + n > room)
{
n -= ptr2cells(s + i);
i += (*mb_ptr2len)(s + i);
}
}
else if (enc_utf8)
{
// For UTF-8 we can go backwards easily.
half = i = (int)STRLEN(s);
for (;;)
{
do
half = half - utf_head_off(s, s + half - 1) - 1;
while (half > 0 && utf_iscomposing(utf_ptr2char(s + half)));
n = ptr2cells(s + half);
if (len + n > room || half == 0)
break;
len += n;
i = (int)half;
}
}
else
{
for (i = (int)STRLEN(s);
i - 1 >= 0 && len + (n = ptr2cells(s + i - 1)) <= room; --i)
len += n;
}
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
mch_memmove(buf + e, s + e, len);
}
}
else if (e + 3 < buflen)
{
// set the middle and copy the last part
mch_memmove(buf + e, "...", (size_t)3);
len = STRLEN(s + i) + 1;
if (len >= (size_t)buflen - e - 3)
len = buflen - e - 3 - 1;
mch_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;
}
}
/*
* Automatic prototype generation does not understand this function.
* Note: Caller of smsg() and smsg_attr() must check the resulting string is
* shorter than IOSIZE!!!
*/
#ifndef PROTO
int vim_snprintf(char *str, size_t str_m, const char *fmt, ...);
int
smsg(const char *s, ...)
{
if (IObuff == NULL)
{
// Very early in initialisation and already something wrong, just
// give the raw message so the user at least gets a hint.
return msg((char *)s);
}
else
{
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, ...)
{
if (IObuff == NULL)
{
// Very early in initialisation and already something wrong, just
// give the raw message so the user at least gets a hint.
return msg_attr((char *)s, attr);
}
else
{
va_list arglist;
va_start(arglist, s);
vim_vsnprintf((char *)IObuff, IOSIZE, s, arglist);
va_end(arglist);
return msg_attr((char *)IObuff, attr);
}
}
int
smsg_attr_keep(int attr, const char *s, ...)
{
if (IObuff == NULL)
{
// Very early in initialisation and already something wrong, just
// give the raw message so the user at least gets a hint.
return msg_attr_keep((char *)s, attr, TRUE);
}
else
{
va_list arglist;
va_start(arglist, s);
vim_vsnprintf((char *)IObuff, IOSIZE, s, arglist);
va_end(arglist);
return msg_attr_keep((char *)IObuff, attr, TRUE);
}
}
#endif
/*
* 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)
{
VIM_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 (HAVE_SOURCING_INFO && 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.
* Returns an allocated string with room for one more character.
* Returns NULL when no message is to be given.
*/
static char_u *
get_emsg_source(void)
{
char_u *Buf, *p;
if (HAVE_SOURCING_INFO && SOURCING_NAME != NULL && other_sourcing_name())
{
char_u *sname = estack_sfile(ESTACK_NONE);
char_u *tofree = sname;
if (sname == NULL)
sname = SOURCING_NAME;
#ifdef FEAT_EVAL
if (estack_compiling)
p = (char_u *)_("Error detected while compiling %s:");
else
#endif
p = (char_u *)_("Error detected while processing %s:");
Buf = alloc(STRLEN(sname) + STRLEN(p));
if (Buf != NULL)
sprintf((char *)Buf, (char *)p, sname);
vim_free(tofree);
return Buf;
}
return NULL;
}
/*
* Get the message about the source lnum, as used for an error message.
* Returns an allocated string with room for one more character.
* Returns NULL when no message is to be given.
*/
static char_u *
get_emsg_lnum(void)
{
char_u *Buf, *p;
// 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)
{
p = (char_u *)_("line %4ld:");
Buf = alloc(STRLEN(p) + 20);
if (Buf != NULL)
sprintf((char *)Buf, (char *)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)
{
char_u *p;
static int recursive = FALSE;
// Bail out if something called here causes an error.
if (recursive)
return;
recursive = TRUE;
++no_wait_return;
p = get_emsg_source();
if (p != NULL)
{
msg_attr((char *)p, attr);
vim_free(p);
}
p = get_emsg_lnum();
if (p != NULL)
{
msg_attr((char *)p, HL_ATTR(HLF_N));
vim_free(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())
{
VIM_CLEAR(last_sourcing_name);
if (SOURCING_NAME != NULL)
last_sourcing_name = vim_strsave(SOURCING_NAME);
}
--no_wait_return;
recursive = FALSE;
}
/*
* 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.
*/
static int
emsg_not_now(void)
{
if ((emsg_off > 0 && vim_strchr(p_debug, 'm') == NULL
&& vim_strchr(p_debug, 't') == NULL)
#ifdef FEAT_EVAL
|| emsg_skip > 0
#endif
)
return TRUE;
return FALSE;
}
#if defined(FEAT_EVAL) || defined(PROTO)
static garray_T ignore_error_list = GA_EMPTY;
void
ignore_error_for_testing(char_u *error)
{
if (ignore_error_list.ga_itemsize == 0)
ga_init2(&ignore_error_list, sizeof(char_u *), 1);
if (STRCMP("RESET", error) == 0)
ga_clear_strings(&ignore_error_list);
else
ga_copy_string(&ignore_error_list, error);
}
static int
ignore_error(char_u *msg)
{
int i;
for (i = 0; i < ignore_error_list.ga_len; ++i)
if (strstr((char *)msg,
(char *)((char_u **)(ignore_error_list.ga_data))[i]) != NULL)
return TRUE;
return FALSE;
}
#endif
#if !defined(HAVE_STRERROR) || defined(PROTO)
/*
* Replacement for perror() that behaves more or less like emsg() was called.
* v:errmsg will be set and called_emsg will be incremented.
*/
void
do_perror(char *msg)
{
perror(msg);
++emsg_silent;
emsg(msg);
--emsg_silent;
}
#endif
/*
* emsg_core() - 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.
* Note: caller must check 'emsg_not_now()' before calling this.
*/
static int
emsg_core(char_u *s)
{
int attr;
char_u *p;
int r;
#ifdef FEAT_EVAL
int ignore = FALSE;
int severe;
#endif
#ifdef FEAT_EVAL
// When testing some errors are turned into a normal message.
if (ignore_error(s))
// don't call msg() if it results in a dialog
return msg_use_printf() ? FALSE : msg((char *)s);
#endif
++called_emsg;
#ifdef FEAT_EVAL
// If "emsg_severe" is TRUE: When an error exception is to be thrown,
// prefer this message over previous messages for the same command.
severe = emsg_severe;
emsg_severe = FALSE;
#endif
if (!emsg_off || vim_strchr(p_debug, 't') != NULL)
{
#ifdef FEAT_EVAL
/*
* 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) == TRUE)
{
if (!ignore)
++did_emsg;
return TRUE;
}
if (in_assert_fails && emsg_assert_fails_msg == NULL)
{
emsg_assert_fails_msg = vim_strsave(s);
emsg_assert_fails_lnum = SOURCING_LNUM;
vim_free(emsg_assert_fails_context);
emsg_assert_fails_context = vim_strsave(
SOURCING_NAME == NULL ? (char_u *)"" : SOURCING_NAME);
}
// set "v:errmsg", also when using ":silent! cmd"
set_vim_var_string(VV_ERRMSG, s, -1);
#endif
/*
* When using ":silent! cmd" ignore error messages.
* But do write it to the redirection file.
*/
if (emsg_silent != 0)
{
#ifdef FEAT_EVAL
++did_emsg_silent;
#endif
if (emsg_noredir == 0)
{
msg_start();
p = get_emsg_source();
if (p != NULL)
{
STRCAT(p, "\n");
redir_write(p, -1);
vim_free(p);
}
p = get_emsg_lnum();
if (p != NULL)
{
STRCAT(p, "\n");
redir_write(p, -1);
vim_free(p);
}
redir_write(s, -1);
}
#ifdef FEAT_EVAL
// Only increment did_emsg_def when :silent! wasn't used inside the
// :def function.
if (emsg_silent == emsg_silent_def)
++did_emsg_def;
#endif
#ifdef FEAT_JOB_CHANNEL
ch_log(NULL, "ERROR silent: %s", (char *)s);
#endif
return TRUE;
}
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()
#ifdef FEAT_EVAL
++uncaught_emsg;
#endif
}
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
#ifdef FEAT_JOB_CHANNEL
emsg_to_channel_log = TRUE;
#endif
/*
* 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
r = msg_attr((char *)s, attr);
#ifdef FEAT_JOB_CHANNEL
emsg_to_channel_log = FALSE;
#endif
return r;
}
/*
* Print an error message.
*/
int
emsg(char *s)
{
// Skip this if not giving error messages at the moment.
if (!emsg_not_now())
return emsg_core((char_u *)s);
return TRUE; // no error messages at the moment
}
#ifndef PROTO // manual proto with __attribute__
/*
* Print an error message with format string and variable arguments.
* Note: caller must not pass 'IObuff' as 1st argument.
*/
int
semsg(const char *s, ...)
{
// Skip this if not giving error messages at the moment.
if (!emsg_not_now())
{
if (IObuff == NULL)
{
// Very early in initialisation and already something wrong, just
// give the raw message so the user at least gets a hint.
return emsg_core((char_u *)s);
}
else
{
va_list ap;
va_start(ap, s);
vim_vsnprintf((char *)IObuff, IOSIZE, s, ap);
va_end(ap);
return emsg_core(IObuff);
}
}
return TRUE; // no error messages at the moment
}
#endif
/*
* 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(char *s)
{
if (!emsg_not_now())
{
emsg_core((char_u *)s);
#if defined(ABORT_ON_INTERNAL_ERROR) && defined(FEAT_EVAL)
set_vim_var_string(VV_ERRMSG, (char_u *)s, -1);
abort();
#endif
}
}
#ifndef PROTO // manual proto with __attribute__
/*
* 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.
* Note: caller must not pass 'IObuff' as 1st argument.
*/
void
siemsg(const char *s, ...)
{
if (!emsg_not_now())
{
if (IObuff == NULL)
{
// Very early in initialisation and already something wrong, just
// give the raw message so the user at least gets a hint.
emsg_core((char_u *)s);
}
else
{
va_list ap;
va_start(ap, s);
vim_vsnprintf((char *)IObuff, IOSIZE, s, ap);
va_end(ap);
emsg_core(IObuff);
}
}
# ifdef ABORT_ON_INTERNAL_ERROR
abort();
# endif
}
#endif
/*
* Give an "Internal error" message.
*/
void
internal_error(char *where)
{
siemsg(_(e_internal_error_str), where);
}
#if defined(FEAT_EVAL) || defined(PROTO)
/*
* Like internal_error() but do not call abort(), to avoid tests using
* test_unknown() and test_void() causing Vim to exit.
*/
void
internal_error_no_abort(char *where)
{
semsg(_(e_internal_error_str), where);
}
#endif
// emsg3() and emsgn() are in misc2.c to avoid warnings for the prototypes.
void
emsg_invreg(int name)
{
semsg(_(e_invalid_register_name_str), transchar(name));
}
#if defined(FEAT_EVAL) || defined(PROTO)
/*
* Give an error message which contains %s for "name[len]".
*/
void
emsg_namelen(char *msg, char_u *name, int len)
{
char_u *copy = vim_strnsave(name, len);
semsg(msg, copy == NULL ? "NULL" : (char *)copy);
vim_free(copy);
}
#endif
/*
* 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()!
* Returns a pointer to the printed message, if wait_return() not called.
*/
char *
msg_trunc_attr(char *s, int force, int attr)
{
int n;
char *ts;
// Add message to history before truncating
add_msg_hist((char_u *)s, -1, attr);
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(int force, char_u *s)
{
int n;
int room;
// If something unexpected happened "room" may be negative, check for that
// just in case.
room = (int)(Rows - cmdline_row - 1) * Columns + sc_col - 1;
if (room > 0 && (force || (shortmess(SHM_TRUNC) && !exmode_active))
&& (n = (int)STRLEN(s) - room) > 0)
{
if (has_mbyte)
{
int size = vim_strsize(s);
// There may be room anyway when there are multibyte chars.
if (size <= room)
return s;
for (n = 0; size >= room; )
{
size -= (*mb_ptr2cells)(s + n);
n += (*mb_ptr2len)(s + n);
}
--n;
}
s += n;
*s = '<';
}
return s;
}
static void
add_msg_hist(
char_u *s,
int len, // -1 for undetermined length
int attr)
{
struct msg_hist *p;
if (msg_hist_off || msg_silent != 0)
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
p = ALLOC_ONE(struct msg_hist);
if (p != NULL)
{
if (len < 0)
len = (int)STRLEN(s);
// remove leading and trailing newlines