blob: 00ad2c7d307c6b795c53aa633aee30a891f0d919 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * message.c: functions for displaying messages on the command line
12 */
13
Bram Moolenaar85a20022019-12-21 18:25:54 +010014#define MESSAGE_FILE // don't include prototype for smsg()
Bram Moolenaar071d4272004-06-13 20:20:40 +000015
16#include "vim.h"
17
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010018static void add_msg_hist(char_u *s, int len, int attr);
19static void hit_return_msg(void);
20static void msg_home_replace_attr(char_u *fname, int attr);
Bram Moolenaar32526b32019-01-19 17:43:09 +010021static void msg_puts_attr_len(char *str, int maxlen, int attr);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010022static void msg_puts_display(char_u *str, int maxlen, int attr, int recurse);
23static void msg_scroll_up(void);
24static void inc_msg_scrolled(void);
25static void store_sb_text(char_u **sb_str, char_u *s, int attr, int *sb_col, int finish);
26static void t_puts(int *t_col, char_u *t_s, char_u *s, int attr);
27static void msg_puts_printf(char_u *str, int maxlen);
28static int do_more_prompt(int typed_char);
29static void msg_screen_putchar(int c, int attr);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020030static void msg_moremsg(int full);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010031static int msg_check_screen(void);
32static void redir_write(char_u *s, int maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000033#ifdef FEAT_CON_DIALOG
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010034static char_u *msg_show_console_dialog(char_u *message, char_u *buttons, int dfltbutton);
Bram Moolenaar85a20022019-12-21 18:25:54 +010035static int confirm_msg_used = FALSE; // displaying confirm_msg
36static char_u *confirm_msg = NULL; // ":confirm" message
37static char_u *confirm_msg_tail; // tail of confirm_msg
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020038static void display_confirm_msg(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000039#endif
Bram Moolenaar958dc692016-12-01 15:34:12 +010040#ifdef FEAT_JOB_CHANNEL
41static int emsg_to_channel_log = FALSE;
42#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000043
44struct msg_hist
45{
46 struct msg_hist *next;
47 char_u *msg;
48 int attr;
49};
50
51static struct msg_hist *first_msg_hist = NULL;
52static struct msg_hist *last_msg_hist = NULL;
53static int msg_hist_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000054
Bram Moolenaarb5b5b892011-09-14 15:39:29 +020055static FILE *verbose_fd = NULL;
56static int verbose_did_open = FALSE;
57
Bram Moolenaar071d4272004-06-13 20:20:40 +000058/*
59 * When writing messages to the screen, there are many different situations.
60 * A number of variables is used to remember the current state:
61 * msg_didany TRUE when messages were written since the last time the
62 * user reacted to a prompt.
63 * Reset: After hitting a key for the hit-return prompt,
64 * hitting <CR> for the command line or input().
65 * Set: When any message is written to the screen.
66 * msg_didout TRUE when something was written to the current line.
67 * Reset: When advancing to the next line, when the current
68 * text can be overwritten.
69 * Set: When any message is written to the screen.
70 * msg_nowait No extra delay for the last drawn message.
71 * Used in normal_cmd() before the mode message is drawn.
72 * emsg_on_display There was an error message recently. Indicates that there
73 * should be a delay before redrawing.
74 * msg_scroll The next message should not overwrite the current one.
75 * msg_scrolled How many lines the screen has been scrolled (because of
76 * messages). Used in update_screen() to scroll the screen
77 * back. Incremented each time the screen scrolls a line.
78 * msg_scrolled_ign TRUE when msg_scrolled is non-zero and msg_puts_attr()
79 * writes something without scrolling should not make
80 * need_wait_return to be set. This is a hack to make ":ts"
81 * work without an extra prompt.
82 * lines_left Number of lines available for messages before the
Bram Moolenaar5d6f75e2011-12-30 14:14:29 +010083 * more-prompt is to be given. -1 when not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +000084 * need_wait_return TRUE when the hit-return prompt is needed.
85 * Reset: After giving the hit-return prompt, when the user
86 * has answered some other prompt.
87 * Set: When the ruler or typeahead display is overwritten,
88 * scrolling the screen for some message.
89 * keep_msg Message to be displayed after redrawing the screen, in
90 * main_loop().
91 * This is an allocated string or NULL when not used.
92 */
93
94/*
95 * msg(s) - displays the string 's' on the status line
96 * When terminal not initialized (yet) mch_errmsg(..) is used.
97 * return TRUE if wait_return not called
98 */
99 int
Bram Moolenaar32526b32019-01-19 17:43:09 +0100100msg(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000101{
102 return msg_attr_keep(s, 0, FALSE);
103}
104
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000105/*
106 * Like msg() but keep it silent when 'verbosefile' is set.
107 */
108 int
Bram Moolenaar32526b32019-01-19 17:43:09 +0100109verb_msg(char *s)
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000110{
111 int n;
112
113 verbose_enter();
114 n = msg_attr_keep(s, 0, FALSE);
115 verbose_leave();
116
117 return n;
118}
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000119
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120 int
Bram Moolenaar32526b32019-01-19 17:43:09 +0100121msg_attr(char *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122{
123 return msg_attr_keep(s, attr, FALSE);
124}
125
126 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100127msg_attr_keep(
Bram Moolenaar32526b32019-01-19 17:43:09 +0100128 char *s,
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100129 int attr,
Bram Moolenaar85a20022019-12-21 18:25:54 +0100130 int keep) // TRUE: set keep_msg if it doesn't scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131{
132 static int entered = 0;
133 int retval;
134 char_u *buf = NULL;
135
Bram Moolenaar85a20022019-12-21 18:25:54 +0100136 // Skip messages not matching ":filter pattern".
137 // Don't filter when there is an error.
Bram Moolenaar32526b32019-01-19 17:43:09 +0100138 if (!emsg_on_display && message_filtered((char_u *)s))
Bram Moolenaar7b668e82016-08-23 23:51:21 +0200139 return TRUE;
140
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141#ifdef FEAT_EVAL
142 if (attr == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100143 set_vim_var_string(VV_STATUSMSG, (char_u *)s, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144#endif
145
146 /*
147 * It is possible that displaying a messages causes a problem (e.g.,
148 * when redrawing the window), which causes another message, etc.. To
149 * break this loop, limit the recursiveness to 3 levels.
150 */
151 if (entered >= 3)
152 return TRUE;
153 ++entered;
154
Bram Moolenaar85a20022019-12-21 18:25:54 +0100155 // Add message to history (unless it's a repeated kept message or a
156 // truncated message)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100157 if ((char_u *)s != keep_msg
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158 || (*s != '<'
159 && last_msg_hist != NULL
160 && last_msg_hist->msg != NULL
161 && STRCMP(s, last_msg_hist->msg)))
Bram Moolenaar32526b32019-01-19 17:43:09 +0100162 add_msg_hist((char_u *)s, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163
Bram Moolenaar958dc692016-12-01 15:34:12 +0100164#ifdef FEAT_JOB_CHANNEL
165 if (emsg_to_channel_log)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100166 // Write message in the channel log.
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +0000167 ch_log(NULL, "ERROR: %s", s);
Bram Moolenaar958dc692016-12-01 15:34:12 +0100168#endif
169
Bram Moolenaar85a20022019-12-21 18:25:54 +0100170 // Truncate the message if needed.
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000171 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +0100172 buf = msg_strtrunc((char_u *)s, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173 if (buf != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100174 s = (char *)buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175
Bram Moolenaar32526b32019-01-19 17:43:09 +0100176 msg_outtrans_attr((char_u *)s, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177 msg_clr_eos();
178 retval = msg_end();
179
Bram Moolenaar32526b32019-01-19 17:43:09 +0100180 if (keep && retval && vim_strsize((char_u *)s)
181 < (int)(Rows - cmdline_row - 1) * Columns + sc_col)
182 set_keep_msg((char_u *)s, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183
Rob Pilling726f7f92022-01-20 14:44:38 +0000184 need_fileinfo = FALSE;
185
Bram Moolenaar071d4272004-06-13 20:20:40 +0000186 vim_free(buf);
187 --entered;
188 return retval;
189}
190
191/*
192 * Truncate a string such that it can be printed without causing a scroll.
193 * Returns an allocated string or NULL when no truncating is done.
194 */
195 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100196msg_strtrunc(
197 char_u *s,
Bram Moolenaar85a20022019-12-21 18:25:54 +0100198 int force) // always truncate
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199{
200 char_u *buf = NULL;
201 int len;
202 int room;
203
Bram Moolenaar85a20022019-12-21 18:25:54 +0100204 // May truncate message to avoid a hit-return prompt
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000205 if ((!msg_scroll && !need_wait_return && shortmess(SHM_TRUNCALL)
206 && !exmode_active && msg_silent == 0) || force)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207 {
208 len = vim_strsize(s);
Bram Moolenaarbb15b652005-10-03 21:52:09 +0000209 if (msg_scrolled != 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100210 // Use all the columns.
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000211 room = (int)(Rows - msg_row) * Columns - 1;
212 else
Bram Moolenaar85a20022019-12-21 18:25:54 +0100213 // Use up to 'showcmd' column.
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000214 room = (int)(Rows - msg_row - 1) * Columns + sc_col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215 if (len > room && room > 0)
216 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217 if (enc_utf8)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100218 // may have up to 18 bytes per cell (6 per char, up to two
219 // composing chars)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100220 len = (room + 2) * 18;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221 else if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100222 // may have up to 2 bytes per cell for euc-jp
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100223 len = (room + 2) * 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224 else
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100225 len = room + 2;
226 buf = alloc(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227 if (buf != NULL)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100228 trunc_string(s, buf, room, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229 }
230 }
231 return buf;
232}
233
234/*
235 * Truncate a string "s" to "buf" with cell width "room".
236 * "s" and "buf" may be equal.
237 */
238 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100239trunc_string(
240 char_u *s,
241 char_u *buf,
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200242 int room_in,
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100243 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244{
Bram Moolenaar85a20022019-12-21 18:25:54 +0100245 size_t room = room_in - 3; // "..." takes 3 chars
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200246 size_t half;
247 size_t len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248 int e;
249 int i;
250 int n;
251
Bram Moolenaar62818152021-02-14 15:37:30 +0100252 if (*s == NUL)
253 {
254 if (buflen > 0)
255 *buf = NUL;
256 return;
257 }
258
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200259 if (room_in < 3)
260 room = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261 half = room / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262
Bram Moolenaar85a20022019-12-21 18:25:54 +0100263 // First part: Start of the string.
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100264 for (e = 0; len < half && e < buflen; ++e)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265 {
266 if (s[e] == NUL)
267 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100268 // text fits without truncating!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269 buf[e] = NUL;
270 return;
271 }
272 n = ptr2cells(s + e);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200273 if (len + n > half)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274 break;
275 len += n;
276 buf[e] = s[e];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000278 for (n = (*mb_ptr2len)(s + e); --n > 0; )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100280 if (++e == buflen)
281 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282 buf[e] = s[e];
283 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284 }
285
Bram Moolenaar85a20022019-12-21 18:25:54 +0100286 // Last part: End of the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000287 i = e;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000288 if (enc_dbcs != 0)
289 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100290 // For DBCS going backwards in a string is slow, but
291 // computing the cell width isn't too slow: go forward
292 // until the rest fits.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000293 n = vim_strsize(s + i);
294 while (len + n > room)
295 {
296 n -= ptr2cells(s + i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000297 i += (*mb_ptr2len)(s + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000298 }
299 }
300 else if (enc_utf8)
301 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100302 // For UTF-8 we can go backwards easily.
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000303 half = i = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304 for (;;)
305 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000306 do
Bram Moolenaarace95982017-03-29 17:30:27 +0200307 half = half - utf_head_off(s, s + half - 1) - 1;
Bram Moolenaarb9644432016-07-19 12:33:44 +0200308 while (half > 0 && utf_iscomposing(utf_ptr2char(s + half)));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309 n = ptr2cells(s + half);
Bram Moolenaarb9644432016-07-19 12:33:44 +0200310 if (len + n > room || half == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311 break;
312 len += n;
Bram Moolenaara5c0cc12016-07-30 16:40:39 +0200313 i = (int)half;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314 }
315 }
316 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317 {
Bram Moolenaard0337e32019-12-30 17:55:34 +0100318 for (i = (int)STRLEN(s);
319 i - 1 >= 0 && len + (n = ptr2cells(s + i - 1)) <= room; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320 len += n;
321 }
322
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200323
324 if (i <= e + 3)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100325 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100326 // text fits without truncating
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200327 if (s != buf)
328 {
329 len = STRLEN(s);
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200330 if (len >= (size_t)buflen)
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200331 len = buflen - 1;
332 len = len - e + 1;
333 if (len < 1)
334 buf[e - 1] = NUL;
335 else
336 mch_memmove(buf + e, s + e, len);
337 }
338 }
339 else if (e + 3 < buflen)
340 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100341 // set the middle and copy the last part
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100342 mch_memmove(buf + e, "...", (size_t)3);
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200343 len = STRLEN(s + i) + 1;
344 if (len >= (size_t)buflen - e - 3)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100345 len = buflen - e - 3 - 1;
346 mch_memmove(buf + e + 3, s + i, len);
347 buf[e + 3 + len - 1] = NUL;
348 }
349 else
350 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100351 // can't fit in the "...", just truncate it
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200352 buf[e - 1] = NUL;
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354}
355
356/*
357 * Automatic prototype generation does not understand this function.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100358 * Note: Caller of smsg() and smsg_attr() must check the resulting string is
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359 * shorter than IOSIZE!!!
360 */
361#ifndef PROTO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100363int vim_snprintf(char *str, size_t str_m, const char *fmt, ...);
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000364
Bram Moolenaar071d4272004-06-13 20:20:40 +0000365 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100366smsg(const char *s, ...)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200368 if (IObuff == NULL)
369 {
370 // Very early in initialisation and already something wrong, just
371 // give the raw message so the user at least gets a hint.
372 return msg((char *)s);
373 }
374 else
375 {
376 va_list arglist;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200378 va_start(arglist, s);
379 vim_vsnprintf((char *)IObuff, IOSIZE, s, arglist);
380 va_end(arglist);
381 return msg((char *)IObuff);
382 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383}
384
385 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100386smsg_attr(int attr, const char *s, ...)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200388 if (IObuff == NULL)
389 {
390 // Very early in initialisation and already something wrong, just
391 // give the raw message so the user at least gets a hint.
392 return msg_attr((char *)s, attr);
393 }
394 else
395 {
396 va_list arglist;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200398 va_start(arglist, s);
399 vim_vsnprintf((char *)IObuff, IOSIZE, s, arglist);
400 va_end(arglist);
401 return msg_attr((char *)IObuff, attr);
402 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403}
404
Bram Moolenaare0429682018-07-01 16:44:03 +0200405 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100406smsg_attr_keep(int attr, const char *s, ...)
Bram Moolenaare0429682018-07-01 16:44:03 +0200407{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200408 if (IObuff == NULL)
409 {
410 // Very early in initialisation and already something wrong, just
411 // give the raw message so the user at least gets a hint.
412 return msg_attr_keep((char *)s, attr, TRUE);
413 }
414 else
415 {
416 va_list arglist;
Bram Moolenaare0429682018-07-01 16:44:03 +0200417
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200418 va_start(arglist, s);
419 vim_vsnprintf((char *)IObuff, IOSIZE, s, arglist);
420 va_end(arglist);
421 return msg_attr_keep((char *)IObuff, attr, TRUE);
422 }
Bram Moolenaare0429682018-07-01 16:44:03 +0200423}
424
Bram Moolenaar071d4272004-06-13 20:20:40 +0000425#endif
426
427/*
428 * Remember the last sourcing name/lnum used in an error message, so that it
429 * isn't printed each time when it didn't change.
430 */
431static int last_sourcing_lnum = 0;
432static char_u *last_sourcing_name = NULL;
433
434/*
435 * Reset the last used sourcing name/lnum. Makes sure it is displayed again
436 * for the next error message;
437 */
Bram Moolenaar4eec5ec2005-06-26 22:26:21 +0000438 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100439reset_last_sourcing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440{
Bram Moolenaard23a8232018-02-10 18:45:26 +0100441 VIM_CLEAR(last_sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000442 last_sourcing_lnum = 0;
443}
444
445/*
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100446 * Return TRUE if "SOURCING_NAME" differs from "last_sourcing_name".
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000447 */
448 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100449other_sourcing_name(void)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000450{
Bram Moolenaar0bd8d052021-11-25 13:39:28 +0000451 if (HAVE_SOURCING_INFO && SOURCING_NAME != NULL)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000452 {
453 if (last_sourcing_name != NULL)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100454 return STRCMP(SOURCING_NAME, last_sourcing_name) != 0;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000455 return TRUE;
456 }
457 return FALSE;
458}
459
460/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000461 * Get the message about the source, as used for an error message.
462 * Returns an allocated string with room for one more character.
463 * Returns NULL when no message is to be given.
464 */
465 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100466get_emsg_source(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467{
468 char_u *Buf, *p;
469
Bram Moolenaar0bd8d052021-11-25 13:39:28 +0000470 if (HAVE_SOURCING_INFO && SOURCING_NAME != NULL && other_sourcing_name())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000471 {
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200472 char_u *sname = estack_sfile(ESTACK_NONE);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100473 char_u *tofree = sname;
474
475 if (sname == NULL)
476 sname = SOURCING_NAME;
477
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +0200478#ifdef FEAT_EVAL
479 if (estack_compiling)
480 p = (char_u *)_("Error detected while compiling %s:");
481 else
482#endif
483 p = (char_u *)_("Error detected while processing %s:");
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100484 Buf = alloc(STRLEN(sname) + STRLEN(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000485 if (Buf != NULL)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100486 sprintf((char *)Buf, (char *)p, sname);
487 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488 return Buf;
489 }
490 return NULL;
491}
492
493/*
494 * Get the message about the source lnum, as used for an error message.
495 * Returns an allocated string with room for one more character.
496 * Returns NULL when no message is to be given.
497 */
498 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100499get_emsg_lnum(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500{
501 char_u *Buf, *p;
502
Bram Moolenaar85a20022019-12-21 18:25:54 +0100503 // lnum is 0 when executing a command from the command line
504 // argument, we don't want a line number then
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100505 if (SOURCING_NAME != NULL
506 && (other_sourcing_name() || SOURCING_LNUM != last_sourcing_lnum)
507 && SOURCING_LNUM != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000508 {
509 p = (char_u *)_("line %4ld:");
Bram Moolenaar964b3742019-05-24 18:54:09 +0200510 Buf = alloc(STRLEN(p) + 20);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 if (Buf != NULL)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100512 sprintf((char *)Buf, (char *)p, (long)SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513 return Buf;
514 }
515 return NULL;
516}
517
518/*
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000519 * Display name and line number for the source of an error.
520 * Remember the file name and line number, so that for the next error the info
521 * is only displayed if it changed.
522 */
523 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100524msg_source(int attr)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000525{
526 char_u *p;
Bram Moolenaarba8c9262021-11-25 14:43:18 +0000527 static int recursive = FALSE;
528
529 // Bail out if something called here causes an error.
530 if (recursive)
531 return;
532 recursive = TRUE;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000533
zeertzjq28c162f2022-08-14 14:49:50 +0100534 msg_scroll = TRUE; // this will take more than one line
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000535 ++no_wait_return;
536 p = get_emsg_source();
537 if (p != NULL)
538 {
Bram Moolenaar32526b32019-01-19 17:43:09 +0100539 msg_attr((char *)p, attr);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000540 vim_free(p);
541 }
542 p = get_emsg_lnum();
543 if (p != NULL)
544 {
Bram Moolenaar32526b32019-01-19 17:43:09 +0100545 msg_attr((char *)p, HL_ATTR(HLF_N));
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000546 vim_free(p);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100547 last_sourcing_lnum = SOURCING_LNUM; // only once for each line
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000548 }
549
Bram Moolenaar85a20022019-12-21 18:25:54 +0100550 // remember the last sourcing name printed, also when it's empty
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100551 if (SOURCING_NAME == NULL || other_sourcing_name())
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000552 {
Bram Moolenaarba8c9262021-11-25 14:43:18 +0000553 VIM_CLEAR(last_sourcing_name);
554 if (SOURCING_NAME != NULL)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100555 last_sourcing_name = vim_strsave(SOURCING_NAME);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000556 }
557 --no_wait_return;
Bram Moolenaarba8c9262021-11-25 14:43:18 +0000558
559 recursive = FALSE;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000560}
561
562/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000563 * Return TRUE if not giving error messages right now:
564 * If "emsg_off" is set: no error messages at the moment.
565 * If "msg" is in 'debug': do error message but without side effects.
566 * If "emsg_skip" is set: never do error messages.
567 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +0200568 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100569emsg_not_now(void)
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000570{
571 if ((emsg_off > 0 && vim_strchr(p_debug, 'm') == NULL
572 && vim_strchr(p_debug, 't') == NULL)
573#ifdef FEAT_EVAL
574 || emsg_skip > 0
575#endif
576 )
577 return TRUE;
578 return FALSE;
579}
580
Bram Moolenaar5a66dfb2017-03-01 20:40:39 +0100581#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100582static garray_T ignore_error_list = GA_EMPTY;
583
584 void
585ignore_error_for_testing(char_u *error)
586{
587 if (ignore_error_list.ga_itemsize == 0)
588 ga_init2(&ignore_error_list, sizeof(char_u *), 1);
589
Bram Moolenaar461a7fc2018-12-22 13:28:07 +0100590 if (STRCMP("RESET", error) == 0)
591 ga_clear_strings(&ignore_error_list);
592 else
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000593 ga_copy_string(&ignore_error_list, error);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100594}
595
596 static int
597ignore_error(char_u *msg)
598{
599 int i;
600
601 for (i = 0; i < ignore_error_list.ga_len; ++i)
602 if (strstr((char *)msg,
603 (char *)((char_u **)(ignore_error_list.ga_data))[i]) != NULL)
604 return TRUE;
605 return FALSE;
606}
607#endif
608
Bram Moolenaarb869c0d2016-07-20 00:10:51 +0200609#if !defined(HAVE_STRERROR) || defined(PROTO)
610/*
611 * Replacement for perror() that behaves more or less like emsg() was called.
Bram Moolenaar53989552019-12-23 22:59:18 +0100612 * v:errmsg will be set and called_emsg will be incremented.
Bram Moolenaarb869c0d2016-07-20 00:10:51 +0200613 */
614 void
615do_perror(char *msg)
616{
617 perror(msg);
618 ++emsg_silent;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100619 emsg(msg);
Bram Moolenaarb869c0d2016-07-20 00:10:51 +0200620 --emsg_silent;
621}
622#endif
623
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000624/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100625 * emsg_core() - display an error message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 *
627 * Rings the bell, if appropriate, and calls message() to do the real work
628 * When terminal not initialized (yet) mch_errmsg(..) is used.
629 *
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100630 * Return TRUE if wait_return not called.
631 * Note: caller must check 'emsg_not_now()' before calling this.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100633 static int
634emsg_core(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635{
636 int attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637 char_u *p;
Bram Moolenaar958dc692016-12-01 15:34:12 +0100638 int r;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639#ifdef FEAT_EVAL
640 int ignore = FALSE;
641 int severe;
642#endif
643
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100644#ifdef FEAT_EVAL
Bram Moolenaar85a20022019-12-21 18:25:54 +0100645 // When testing some errors are turned into a normal message.
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100646 if (ignore_error(s))
Bram Moolenaar85a20022019-12-21 18:25:54 +0100647 // don't call msg() if it results in a dialog
Bram Moolenaar32526b32019-01-19 17:43:09 +0100648 return msg_use_printf() ? FALSE : msg((char *)s);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100649#endif
650
Bram Moolenaar53989552019-12-23 22:59:18 +0100651 ++called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653#ifdef FEAT_EVAL
Bram Moolenaar85a20022019-12-21 18:25:54 +0100654 // If "emsg_severe" is TRUE: When an error exception is to be thrown,
655 // prefer this message over previous messages for the same command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 severe = emsg_severe;
657 emsg_severe = FALSE;
658#endif
659
Bram Moolenaar57657d82006-04-21 22:12:41 +0000660 if (!emsg_off || vim_strchr(p_debug, 't') != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 {
662#ifdef FEAT_EVAL
663 /*
664 * Cause a throw of an error exception if appropriate. Don't display
665 * the error message in this case. (If no matching catch clause will
666 * be found, the message will be displayed later on.) "ignore" is set
667 * when the message should be ignored completely (used for the
668 * interrupt message).
669 */
670 if (cause_errthrow(s, severe, &ignore) == TRUE)
671 {
672 if (!ignore)
Bram Moolenaar76a63452018-11-28 20:38:37 +0100673 ++did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 return TRUE;
675 }
676
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100677 if (in_assert_fails && emsg_assert_fails_msg == NULL)
Bram Moolenaar1d634542020-08-18 13:41:50 +0200678 {
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200679 emsg_assert_fails_msg = vim_strsave(s);
Bram Moolenaar1d634542020-08-18 13:41:50 +0200680 emsg_assert_fails_lnum = SOURCING_LNUM;
Bram Moolenaar9bd5d872020-09-06 21:47:48 +0200681 vim_free(emsg_assert_fails_context);
682 emsg_assert_fails_context = vim_strsave(
683 SOURCING_NAME == NULL ? (char_u *)"" : SOURCING_NAME);
Bram Moolenaar1d634542020-08-18 13:41:50 +0200684 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200685
Bram Moolenaar85a20022019-12-21 18:25:54 +0100686 // set "v:errmsg", also when using ":silent! cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 set_vim_var_string(VV_ERRMSG, s, -1);
688#endif
689
690 /*
Bram Moolenaara7241f52008-06-24 20:39:31 +0000691 * When using ":silent! cmd" ignore error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 * But do write it to the redirection file.
693 */
694 if (emsg_silent != 0)
695 {
Bram Moolenaar599410c2021-04-10 14:03:43 +0200696#ifdef FEAT_EVAL
697 ++did_emsg_silent;
698#endif
Bram Moolenaar79815f12016-07-09 17:07:29 +0200699 if (emsg_noredir == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 {
Bram Moolenaar79815f12016-07-09 17:07:29 +0200701 msg_start();
702 p = get_emsg_source();
703 if (p != NULL)
704 {
705 STRCAT(p, "\n");
706 redir_write(p, -1);
707 vim_free(p);
708 }
709 p = get_emsg_lnum();
710 if (p != NULL)
711 {
712 STRCAT(p, "\n");
713 redir_write(p, -1);
714 vim_free(p);
715 }
716 redir_write(s, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +0100718#ifdef FEAT_EVAL
719 // Only increment did_emsg_def when :silent! wasn't used inside the
720 // :def function.
721 if (emsg_silent == emsg_silent_def)
722 ++did_emsg_def;
723#endif
Bram Moolenaar958dc692016-12-01 15:34:12 +0100724#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare809a4e2019-07-04 17:35:05 +0200725 ch_log(NULL, "ERROR silent: %s", (char *)s);
Bram Moolenaar958dc692016-12-01 15:34:12 +0100726#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727 return TRUE;
728 }
729
Bram Moolenaar2b7bc562017-01-14 19:24:52 +0100730 ex_exitval = 1;
731
Bram Moolenaar85a20022019-12-21 18:25:54 +0100732 // Reset msg_silent, an error causes messages to be switched back on.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733 msg_silent = 0;
734 cmd_silent = FALSE;
735
Bram Moolenaar85a20022019-12-21 18:25:54 +0100736 if (global_busy) // break :global command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 ++global_busy;
738
739 if (p_eb)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100740 beep_flush(); // also includes flush_buffers()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 else
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200742 flush_buffers(FLUSH_MINIMAL); // flush internal buffers
Bram Moolenaar76a63452018-11-28 20:38:37 +0100743 ++did_emsg; // flag for DoOneCmd()
Bram Moolenaare723c422017-09-06 23:40:10 +0200744#ifdef FEAT_EVAL
Bram Moolenaar88c89c72021-08-14 14:01:05 +0200745 ++uncaught_emsg;
Bram Moolenaare723c422017-09-06 23:40:10 +0200746#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 }
748
Bram Moolenaar85a20022019-12-21 18:25:54 +0100749 emsg_on_display = TRUE; // remember there is an error message
Bram Moolenaar85a20022019-12-21 18:25:54 +0100750 attr = HL_ATTR(HLF_E); // set highlight mode for error messages
Bram Moolenaarbb15b652005-10-03 21:52:09 +0000751 if (msg_scrolled != 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100752 need_wait_return = TRUE; // needed in case emsg() is called after
753 // wait_return has reset need_wait_return
754 // and a redraw is expected because
755 // msg_scrolled is non-zero
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756
Bram Moolenaar958dc692016-12-01 15:34:12 +0100757#ifdef FEAT_JOB_CHANNEL
758 emsg_to_channel_log = TRUE;
759#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 /*
761 * Display name and line number for the source of the error.
zeertzjq28c162f2022-08-14 14:49:50 +0100762 * Sets "msg_scroll".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763 */
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000764 msg_source(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765
766 /*
767 * Display the error message itself.
768 */
Bram Moolenaar85a20022019-12-21 18:25:54 +0100769 msg_nowait = FALSE; // wait for this msg
Bram Moolenaar32526b32019-01-19 17:43:09 +0100770 r = msg_attr((char *)s, attr);
Bram Moolenaar958dc692016-12-01 15:34:12 +0100771
772#ifdef FEAT_JOB_CHANNEL
773 emsg_to_channel_log = FALSE;
774#endif
775 return r;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776}
777
778/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100779 * Print an error message.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 */
781 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100782emsg(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783{
Bram Moolenaar85a20022019-12-21 18:25:54 +0100784 // Skip this if not giving error messages at the moment.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100785 if (!emsg_not_now())
786 return emsg_core((char_u *)s);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100787 return TRUE; // no error messages at the moment
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788}
789
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100790#ifndef PROTO // manual proto with __attribute__
Bram Moolenaar95f09602016-11-10 20:01:45 +0100791/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100792 * Print an error message with format string and variable arguments.
793 * Note: caller must not pass 'IObuff' as 1st argument.
Bram Moolenaar95f09602016-11-10 20:01:45 +0100794 */
795 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100796semsg(const char *s, ...)
Bram Moolenaar95f09602016-11-10 20:01:45 +0100797{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200798 // Skip this if not giving error messages at the moment.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100799 if (!emsg_not_now())
800 {
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200801 if (IObuff == NULL)
802 {
803 // Very early in initialisation and already something wrong, just
804 // give the raw message so the user at least gets a hint.
805 return emsg_core((char_u *)s);
806 }
807 else
808 {
809 va_list ap;
Bram Moolenaar95f09602016-11-10 20:01:45 +0100810
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200811 va_start(ap, s);
812 vim_vsnprintf((char *)IObuff, IOSIZE, s, ap);
813 va_end(ap);
814 return emsg_core(IObuff);
815 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100816 }
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200817 return TRUE; // no error messages at the moment
Bram Moolenaar95f09602016-11-10 20:01:45 +0100818}
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100819#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +0100820
821/*
822 * Same as emsg(...), but abort on error when ABORT_ON_INTERNAL_ERROR is
823 * defined. It is used for internal errors only, so that they can be
824 * detected when fuzzing vim.
825 */
826 void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100827iemsg(char *s)
Bram Moolenaar95f09602016-11-10 20:01:45 +0100828{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100829 if (!emsg_not_now())
Bram Moolenaar1c67f3a2021-12-30 13:32:09 +0000830 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100831 emsg_core((char_u *)s);
Bram Moolenaar096ca732022-01-01 00:55:28 +0000832#if defined(ABORT_ON_INTERNAL_ERROR) && defined(FEAT_EVAL)
Bram Moolenaar1c67f3a2021-12-30 13:32:09 +0000833 set_vim_var_string(VV_ERRMSG, (char_u *)s, -1);
Bram Moolenaar213e70e2022-08-19 13:17:21 +0100834 msg_putchar('\n'); // avoid overwriting the error message
835 out_flush();
Bram Moolenaar1c67f3a2021-12-30 13:32:09 +0000836 abort();
Bram Moolenaar95f09602016-11-10 20:01:45 +0100837#endif
Bram Moolenaar1c67f3a2021-12-30 13:32:09 +0000838 }
Bram Moolenaar95f09602016-11-10 20:01:45 +0100839}
840
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100841#ifndef PROTO // manual proto with __attribute__
Bram Moolenaar95f09602016-11-10 20:01:45 +0100842/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100843 * Same as semsg(...) but abort on error when ABORT_ON_INTERNAL_ERROR is
Bram Moolenaar95f09602016-11-10 20:01:45 +0100844 * defined. It is used for internal errors only, so that they can be
845 * detected when fuzzing vim.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100846 * Note: caller must not pass 'IObuff' as 1st argument.
Bram Moolenaar95f09602016-11-10 20:01:45 +0100847 */
848 void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100849siemsg(const char *s, ...)
Bram Moolenaar95f09602016-11-10 20:01:45 +0100850{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100851 if (!emsg_not_now())
852 {
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200853 if (IObuff == NULL)
854 {
855 // Very early in initialisation and already something wrong, just
856 // give the raw message so the user at least gets a hint.
857 emsg_core((char_u *)s);
858 }
859 else
860 {
861 va_list ap;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100862
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200863 va_start(ap, s);
864 vim_vsnprintf((char *)IObuff, IOSIZE, s, ap);
865 va_end(ap);
866 emsg_core(IObuff);
867 }
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100868# ifdef ABORT_ON_INTERNAL_ERROR
Bram Moolenaar213e70e2022-08-19 13:17:21 +0100869 msg_putchar('\n'); // avoid overwriting the error message
870 out_flush();
871 abort();
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100872# endif
Bram Moolenaar213e70e2022-08-19 13:17:21 +0100873 }
Bram Moolenaar95f09602016-11-10 20:01:45 +0100874}
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100875#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +0100876
877/*
878 * Give an "Internal error" message.
879 */
880 void
881internal_error(char *where)
882{
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000883 siemsg(_(e_internal_error_str), where);
Bram Moolenaar95f09602016-11-10 20:01:45 +0100884}
885
Dominique Pelle748b3082022-01-08 12:41:16 +0000886#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaardd589232020-02-29 17:38:12 +0100887/*
888 * Like internal_error() but do not call abort(), to avoid tests using
889 * test_unknown() and test_void() causing Vim to exit.
890 */
891 void
892internal_error_no_abort(char *where)
893{
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000894 semsg(_(e_internal_error_str), where);
Bram Moolenaardd589232020-02-29 17:38:12 +0100895}
Dominique Pelle748b3082022-01-08 12:41:16 +0000896#endif
Bram Moolenaardd589232020-02-29 17:38:12 +0100897
Bram Moolenaar85a20022019-12-21 18:25:54 +0100898// emsg3() and emsgn() are in misc2.c to avoid warnings for the prototypes.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899
Bram Moolenaardf177f62005-02-22 08:39:57 +0000900 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100901emsg_invreg(int name)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000902{
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000903 semsg(_(e_invalid_register_name_str), transchar(name));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000904}
905
Dominique Pelle748b3082022-01-08 12:41:16 +0000906#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100908 * Give an error message which contains %s for "name[len]".
909 */
910 void
911emsg_namelen(char *msg, char_u *name, int len)
912{
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +0000913 char_u *copy = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100914
915 semsg(msg, copy == NULL ? "NULL" : (char *)copy);
Bram Moolenaar292b90d2020-03-18 15:23:16 +0100916 vim_free(copy);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100917}
Dominique Pelle748b3082022-01-08 12:41:16 +0000918#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100919
920/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 * Like msg(), but truncate to a single line if p_shm contains 't', or when
922 * "force" is TRUE. This truncates in another way as for normal messages.
923 * Careful: The string may be changed by msg_may_trunc()!
924 * Returns a pointer to the printed message, if wait_return() not called.
925 */
Bram Moolenaar32526b32019-01-19 17:43:09 +0100926 char *
927msg_trunc_attr(char *s, int force, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928{
929 int n;
Bram Moolenaar32526b32019-01-19 17:43:09 +0100930 char *ts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931
Bram Moolenaar85a20022019-12-21 18:25:54 +0100932 // Add message to history before truncating
Bram Moolenaar32526b32019-01-19 17:43:09 +0100933 add_msg_hist((char_u *)s, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934
Bram Moolenaar32526b32019-01-19 17:43:09 +0100935 ts = (char *)msg_may_trunc(force, (char_u *)s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936
937 msg_hist_off = TRUE;
Bram Moolenaar32526b32019-01-19 17:43:09 +0100938 n = msg_attr(ts, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 msg_hist_off = FALSE;
940
941 if (n)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100942 return ts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 return NULL;
944}
945
946/*
947 * Check if message "s" should be truncated at the start (for filenames).
948 * Return a pointer to where the truncated message starts.
949 * Note: May change the message by replacing a character with '<'.
950 */
951 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100952msg_may_trunc(int force, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953{
954 int n;
955 int room;
956
Bram Moolenaar9198de32022-08-27 21:30:03 +0100957 // If 'cmdheight' is zero or something unexpected happened "room" may be
958 // negative.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 room = (int)(Rows - cmdline_row - 1) * Columns + sc_col - 1;
Bram Moolenaar0fbc9262022-06-26 11:17:10 +0100960 if (room > 0 && (force || (shortmess(SHM_TRUNC) && !exmode_active))
Shougo Matsushitaf39cfb72022-07-30 16:54:05 +0100961 && (n = (int)STRLEN(s) - room) > 0 && p_ch > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963 if (has_mbyte)
964 {
965 int size = vim_strsize(s);
966
Bram Moolenaar85a20022019-12-21 18:25:54 +0100967 // There may be room anyway when there are multibyte chars.
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +0000968 if (size <= room)
969 return s;
970
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 for (n = 0; size >= room; )
972 {
973 size -= (*mb_ptr2cells)(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000974 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 }
976 --n;
977 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 s += n;
979 *s = '<';
980 }
981 return s;
982}
983
984 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100985add_msg_hist(
986 char_u *s,
Bram Moolenaar85a20022019-12-21 18:25:54 +0100987 int len, // -1 for undetermined length
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100988 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989{
990 struct msg_hist *p;
991
992 if (msg_hist_off || msg_silent != 0)
993 return;
994
Bram Moolenaar85a20022019-12-21 18:25:54 +0100995 // Don't let the message history get too big
Bram Moolenaar4770d092006-01-12 23:22:24 +0000996 while (msg_hist_len > MAX_MSG_HIST_LEN)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000997 (void)delete_first_msg();
998
Bram Moolenaar85a20022019-12-21 18:25:54 +0100999 // allocate an entry and add the message at the end of the history
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001000 p = ALLOC_ONE(struct msg_hist);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 if (p != NULL)
1002 {
1003 if (len < 0)
1004 len = (int)STRLEN(s);
Bram Moolenaar85a20022019-12-21 18:25:54 +01001005 // remove leading and trailing newlines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 while (len > 0 && *s == '\n')
1007 {
1008 ++s;
1009 --len;
1010 }
1011 while (len > 0 && s[len - 1] == '\n')
1012 --len;
1013 p->msg = vim_strnsave(s, len);
1014 p->next = NULL;
1015 p->attr = attr;
1016 if (last_msg_hist != NULL)
1017 last_msg_hist->next = p;
1018 last_msg_hist = p;
1019 if (first_msg_hist == NULL)
1020 first_msg_hist = last_msg_hist;
1021 ++msg_hist_len;
1022 }
1023}
1024
1025/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00001026 * Delete the first (oldest) message from the history.
1027 * Returns FAIL if there are no messages.
1028 */
1029 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001030delete_first_msg(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00001031{
1032 struct msg_hist *p;
1033
1034 if (msg_hist_len <= 0)
1035 return FAIL;
1036 p = first_msg_hist;
1037 first_msg_hist = p->next;
Bram Moolenaara7241f52008-06-24 20:39:31 +00001038 if (first_msg_hist == NULL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001039 last_msg_hist = NULL; // history is empty
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00001040 vim_free(p->msg);
1041 vim_free(p);
1042 --msg_hist_len;
1043 return OK;
1044}
1045
1046/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 * ":messages" command.
1048 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 void
Bram Moolenaar52196b22016-04-14 17:52:41 +02001050ex_messages(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051{
1052 struct msg_hist *p;
1053 char_u *s;
Bram Moolenaar451f8492016-04-14 17:16:22 +02001054 int c = 0;
1055
1056 if (STRCMP(eap->arg, "clear") == 0)
1057 {
1058 int keep = eap->addr_count == 0 ? 0 : eap->line2;
1059
1060 while (msg_hist_len > keep)
1061 (void)delete_first_msg();
1062 return;
1063 }
1064
1065 if (*eap->arg != NUL)
1066 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001067 emsg(_(e_invalid_argument));
Bram Moolenaar451f8492016-04-14 17:16:22 +02001068 return;
1069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070
1071 msg_hist_off = TRUE;
1072
Bram Moolenaar451f8492016-04-14 17:16:22 +02001073 p = first_msg_hist;
Bram Moolenaar451f8492016-04-14 17:16:22 +02001074 if (eap->addr_count != 0)
1075 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001076 // Count total messages
Bram Moolenaar451f8492016-04-14 17:16:22 +02001077 for (; p != NULL && !got_int; p = p->next)
1078 c++;
1079
1080 c -= eap->line2;
1081
Bram Moolenaar85a20022019-12-21 18:25:54 +01001082 // Skip without number of messages specified
Bram Moolenaar451f8492016-04-14 17:16:22 +02001083 for (p = first_msg_hist; p != NULL && !got_int && c > 0;
1084 p = p->next, c--);
1085 }
1086
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001087 if (p == first_msg_hist)
1088 {
Bram Moolenaar41f69182020-04-25 15:45:37 +02001089#ifdef FEAT_MULTI_LANG
1090 s = get_mess_lang();
1091#else
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001092 s = mch_getenv((char_u *)"LANG");
Bram Moolenaar41f69182020-04-25 15:45:37 +02001093#endif
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001094 if (s != NULL && *s != NUL)
Bram Moolenaar0c183192018-06-28 14:54:43 +02001095 // The next comment is extracted by xgettext and put in po file for
1096 // translators to read.
Bram Moolenaar32526b32019-01-19 17:43:09 +01001097 msg_attr(
Bram Moolenaar0c183192018-06-28 14:54:43 +02001098 // Translator: Please replace the name and email address
1099 // with the appropriate text for your translation.
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001100 _("Messages maintainer: Bram Moolenaar <Bram@vim.org>"),
Bram Moolenaar8820b482017-03-16 17:23:31 +01001101 HL_ATTR(HLF_T));
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001102 }
1103
Bram Moolenaar85a20022019-12-21 18:25:54 +01001104 // Display what was not skipped.
Bram Moolenaar451f8492016-04-14 17:16:22 +02001105 for (; p != NULL && !got_int; p = p->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 if (p->msg != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001107 msg_attr((char *)p->msg, p->attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108
1109 msg_hist_off = FALSE;
1110}
1111
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001112#if defined(FEAT_CON_DIALOG) || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113/*
1114 * Call this after prompting the user. This will avoid a hit-return message
1115 * and a delay.
1116 */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001117 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001118msg_end_prompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119{
1120 need_wait_return = FALSE;
1121 emsg_on_display = FALSE;
1122 cmdline_row = msg_row;
1123 msg_col = 0;
1124 msg_clr_eos();
Bram Moolenaar5d6f75e2011-12-30 14:14:29 +01001125 lines_left = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126}
1127#endif
1128
1129/*
Bram Moolenaar32526b32019-01-19 17:43:09 +01001130 * Wait for the user to hit a key (normally Enter).
1131 * If "redraw" is TRUE, clear and redraw the screen.
1132 * If "redraw" is FALSE, just redraw the screen.
1133 * If "redraw" is -1, don't redraw at all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 */
1135 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001136wait_return(int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137{
1138 int c;
1139 int oldState;
1140 int tmpState;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 int had_got_int;
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001142 int save_reg_recording;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001143 FILE *save_scriptout;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144
1145 if (redraw == TRUE)
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01001146 set_must_redraw(UPD_CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147
Bram Moolenaar85a20022019-12-21 18:25:54 +01001148 // If using ":silent cmd", don't wait for a return. Also don't set
1149 // need_wait_return to do it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 if (msg_silent != 0)
1151 return;
1152
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01001153 /*
1154 * When inside vgetc(), we can't wait for a typed character at all.
1155 * With the global command (and some others) we only need one return at
1156 * the end. Adjust cmdline_row to avoid the next message overwriting the
1157 * last one.
1158 */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00001159 if (vgetc_busy > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 return;
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01001161 need_wait_return = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 if (no_wait_return)
1163 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 if (!exmode_active)
1165 cmdline_row = msg_row;
1166 return;
1167 }
1168
Bram Moolenaar85a20022019-12-21 18:25:54 +01001169 redir_off = TRUE; // don't redirect this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 oldState = State;
1171 if (quit_more)
1172 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001173 c = CAR; // just pretend CR was hit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 quit_more = FALSE;
1175 got_int = FALSE;
1176 }
1177 else if (exmode_active)
1178 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001179 msg_puts(" "); // make sure the cursor is on the right line
1180 c = CAR; // no need for a return in ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 got_int = FALSE;
1182 }
1183 else
1184 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001185 // Make sure the hit-return prompt is on screen when 'guioptions' was
1186 // just changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 screenalloc(FALSE);
1188
Bram Moolenaar24959102022-05-07 20:01:16 +01001189 State = MODE_HITRETURN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar85a20022019-12-21 18:25:54 +01001192 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193#endif
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01001194 cmdline_row = msg_row;
1195
Bram Moolenaar85a20022019-12-21 18:25:54 +01001196 // Avoid the sequence that the user types ":" at the hit-return prompt
1197 // to start an Ex command, but the file-changed dialog gets in the
1198 // way.
Bram Moolenaarec38d692013-04-24 15:12:32 +02001199 if (need_check_timestamps)
1200 check_timestamps(FALSE);
1201
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 hit_return_msg();
1203
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 do
1205 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001206 // Remember "got_int", if it is set vgetc() probably returns a
1207 // CTRL-C, but we need to loop then.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 had_got_int = got_int;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001209
Bram Moolenaar85a20022019-12-21 18:25:54 +01001210 // Don't do mappings here, we put the character back in the
1211 // typeahead buffer.
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001212 ++no_mapping;
1213 ++allow_keys;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001214
Bram Moolenaar85a20022019-12-21 18:25:54 +01001215 // Temporarily disable Recording. If Recording is active, the
1216 // character will be recorded later, since it will be added to the
1217 // typebuf after the loop
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001218 save_reg_recording = reg_recording;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001219 save_scriptout = scriptout;
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001220 reg_recording = 0;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001221 scriptout = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 c = safe_vgetc();
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00001223 if (had_got_int && !global_busy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 got_int = FALSE;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001225 --no_mapping;
1226 --allow_keys;
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001227 reg_recording = save_reg_recording;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001228 scriptout = save_scriptout;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001229
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230#ifdef FEAT_CLIPBOARD
Bram Moolenaar85a20022019-12-21 18:25:54 +01001231 // Strange way to allow copying (yanking) a modeless selection at
1232 // the hit-enter prompt. Use CTRL-Y, because the same is used in
1233 // Cmdline-mode and it's harmless when there is no selection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234 if (c == Ctrl_Y && clip_star.state == SELECT_DONE)
1235 {
1236 clip_copy_modeless_selection(TRUE);
1237 c = K_IGNORE;
1238 }
1239#endif
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00001240
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001241 /*
1242 * Allow scrolling back in the messages.
1243 * Also accept scroll-down commands when messages fill the screen,
1244 * to avoid that typing one 'j' too many makes the messages
1245 * disappear.
1246 */
1247 if (p_more && !p_cp)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001248 {
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001249 if (c == 'b' || c == 'k' || c == 'u' || c == 'g'
1250 || c == K_UP || c == K_PAGEUP)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001251 {
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001252 if (msg_scrolled > Rows)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001253 // scroll back to show older messages
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001254 do_more_prompt(c);
1255 else
1256 {
1257 msg_didout = FALSE;
1258 c = K_IGNORE;
1259 msg_col =
1260#ifdef FEAT_RIGHTLEFT
1261 cmdmsg_rl ? Columns - 1 :
1262#endif
1263 0;
1264 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001265 if (quit_more)
1266 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001267 c = CAR; // just pretend CR was hit
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001268 quit_more = FALSE;
1269 got_int = FALSE;
1270 }
Bram Moolenaarb0912962013-08-09 20:38:26 +02001271 else if (c != K_IGNORE)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001272 {
1273 c = K_IGNORE;
1274 hit_return_msg();
1275 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001276 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001277 else if (msg_scrolled > Rows - 2
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001278 && (c == 'j' || c == 'd' || c == 'f'
1279 || c == K_DOWN || c == K_PAGEDOWN))
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001280 c = K_IGNORE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001281 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 } while ((had_got_int && c == Ctrl_C)
1283 || c == K_IGNORE
1284#ifdef FEAT_GUI
1285 || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR
1286#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 || c == K_LEFTDRAG || c == K_LEFTRELEASE
1288 || c == K_MIDDLEDRAG || c == K_MIDDLERELEASE
1289 || c == K_RIGHTDRAG || c == K_RIGHTRELEASE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001290 || c == K_MOUSELEFT || c == K_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 || c == K_MOUSEDOWN || c == K_MOUSEUP
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001292 || c == K_MOUSEMOVE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 || (!mouse_has(MOUSE_RETURN)
1294 && mouse_row < msg_row
1295 && (c == K_LEFTMOUSE
1296 || c == K_MIDDLEMOUSE
1297 || c == K_RIGHTMOUSE
1298 || c == K_X1MOUSE
1299 || c == K_X2MOUSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 );
1301 ui_breakcheck();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 /*
1303 * Avoid that the mouse-up event causes visual mode to start.
1304 */
1305 if (c == K_LEFTMOUSE || c == K_MIDDLEMOUSE || c == K_RIGHTMOUSE
1306 || c == K_X1MOUSE || c == K_X2MOUSE)
1307 (void)jump_to_mouse(MOUSE_SETPOS, NULL, 0);
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001308 else if (vim_strchr((char_u *)"\r\n ", c) == NULL && c != Ctrl_C)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001310 // Put the character back in the typeahead buffer. Don't use the
1311 // stuff buffer, because lmaps wouldn't work.
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001312 ins_char_typebuf(vgetc_char, vgetc_mod_mask);
Bram Moolenaar85a20022019-12-21 18:25:54 +01001313 do_redraw = TRUE; // need a redraw even though there is
1314 // typeahead
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 }
1317 redir_off = FALSE;
1318
1319 /*
1320 * If the user hits ':', '?' or '/' we get a command line from the next
1321 * line.
1322 */
1323 if (c == ':' || c == '?' || c == '/')
1324 {
1325 if (!exmode_active)
1326 cmdline_row = msg_row;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001327 skip_redraw = TRUE; // skip redraw once
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 do_redraw = FALSE;
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02001329#ifdef FEAT_TERMINAL
1330 skip_term_loop = TRUE;
1331#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 }
1333
1334 /*
1335 * If the window size changed set_shellsize() will redraw the screen.
1336 * Otherwise the screen is only redrawn if 'redraw' is set and no ':'
1337 * typed.
1338 */
1339 tmpState = State;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001340 State = oldState; // restore State before set_shellsize
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 msg_check();
1343
1344#if defined(UNIX) || defined(VMS)
1345 /*
1346 * When switching screens, we need to output an extra newline on exit.
1347 */
1348 if (swapping_screen() && !termcap_active)
1349 newline_on_exit = TRUE;
1350#endif
1351
1352 need_wait_return = FALSE;
1353 did_wait_return = TRUE;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001354 emsg_on_display = FALSE; // can delete error message now
1355 lines_left = -1; // reset lines_left at next msg_start()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 reset_last_sourcing();
1357 if (keep_msg != NULL && vim_strsize(keep_msg) >=
1358 (Rows - cmdline_row - 1) * Columns + sc_col)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001359 VIM_CLEAR(keep_msg); // don't redisplay message, it's too long
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360
Bram Moolenaar24959102022-05-07 20:01:16 +01001361 if (tmpState == MODE_SETWSIZE) // got resize event while in vgetc()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001363 starttermcap(); // start termcap before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 shell_resized();
1365 }
1366 else if (!skip_redraw
1367 && (redraw == TRUE || (msg_scrolled != 0 && redraw != -1)))
1368 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001369 starttermcap(); // start termcap before redrawing
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001370 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 }
1372}
1373
1374/*
1375 * Write the hit-return prompt.
1376 */
1377 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001378hit_return_msg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001380 int save_p_more = p_more;
1381
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001382 p_more = FALSE; // don't want to see this message when scrolling back
Bram Moolenaar85a20022019-12-21 18:25:54 +01001383 if (msg_didout) // start on a new line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 msg_putchar('\n');
1385 if (got_int)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001386 msg_puts(_("Interrupt: "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387
Bram Moolenaar32526b32019-01-19 17:43:09 +01001388 msg_puts_attr(_("Press ENTER or type command to continue"), HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 if (!msg_use_printf())
1390 msg_clr_eos();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001391 p_more = save_p_more;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392}
1393
1394/*
1395 * Set "keep_msg" to "s". Free the old value and check for NULL pointer.
1396 */
1397 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001398set_keep_msg(char_u *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399{
1400 vim_free(keep_msg);
1401 if (s != NULL && msg_silent == 0)
1402 keep_msg = vim_strsave(s);
1403 else
1404 keep_msg = NULL;
Bram Moolenaaraab21c32005-01-25 21:46:35 +00001405 keep_msg_more = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001406 keep_msg_attr = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407}
1408
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001409#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
1410/*
1411 * If there currently is a message being displayed, set "keep_msg" to it, so
1412 * that it will be displayed again after redraw.
1413 */
1414 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001415set_keep_msg_from_hist(void)
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001416{
1417 if (keep_msg == NULL && last_msg_hist != NULL && msg_scrolled == 0
Bram Moolenaar24959102022-05-07 20:01:16 +01001418 && (State & MODE_NORMAL))
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001419 set_keep_msg(last_msg_hist->msg, last_msg_hist->attr);
1420}
1421#endif
1422
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423/*
Bram Moolenaar9198de32022-08-27 21:30:03 +01001424 * Return TRUE when the message popup window should be used.
1425 */
1426 int
1427use_message_window(void)
1428{
1429#ifdef HAS_MESSAGE_WINDOW
1430 // TRUE if there is no command line showing ('cmdheight' is zero and not
1431 // already editing or showing a message) use a popup window for messages.
1432 return p_ch == 0 && cmdline_row >= Rows;
1433#else
1434 return FALSE;
1435#endif
1436}
1437
1438/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439 * Prepare for outputting characters in the command line.
1440 */
1441 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001442msg_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443{
1444 int did_return = FALSE;
1445
Bram Moolenaare4ce65d2010-08-04 20:12:32 +02001446 if (!msg_silent)
Rob Pilling726f7f92022-01-20 14:44:38 +00001447 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001448 VIM_CLEAR(keep_msg);
Rob Pilling726f7f92022-01-20 14:44:38 +00001449 need_fileinfo = FALSE;
1450 }
Bram Moolenaara7241f52008-06-24 20:39:31 +00001451
1452#ifdef FEAT_EVAL
Shougo Matsushitaf39cfb72022-07-30 16:54:05 +01001453 if (need_clr_eos || p_ch == 0)
Bram Moolenaara7241f52008-06-24 20:39:31 +00001454 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001455 // Halfway an ":echo" command and getting an (error) message: clear
1456 // any text from the command.
Bram Moolenaara7241f52008-06-24 20:39:31 +00001457 need_clr_eos = FALSE;
1458 msg_clr_eos();
1459 }
1460#endif
1461
Bram Moolenaar9198de32022-08-27 21:30:03 +01001462 if (use_message_window())
1463 {
1464 if (popup_message_win_visible() && msg_col > 0)
1465 {
1466 win_T *wp = popup_get_message_win();
1467
1468 curbuf = wp->w_buffer;
1469 ml_append(wp->w_buffer->b_ml.ml_line_count,
1470 (char_u *)"", (colnr_T)0, FALSE);
1471 curbuf = curwin->w_buffer;
1472 }
1473 msg_col = 0;
1474 }
1475 else if (!msg_scroll && full_screen) // overwrite last message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 {
1477 msg_row = cmdline_row;
1478 msg_col =
1479#ifdef FEAT_RIGHTLEFT
1480 cmdmsg_rl ? Columns - 1 :
1481#endif
1482 0;
1483 }
Shougo Matsushitaf39cfb72022-07-30 16:54:05 +01001484 else if (msg_didout || p_ch == 0) // start message on next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485 {
1486 msg_putchar('\n');
1487 did_return = TRUE;
1488 if (exmode_active != EXMODE_NORMAL)
1489 cmdline_row = msg_row;
1490 }
1491 if (!msg_didany || lines_left < 0)
1492 msg_starthere();
1493 if (msg_silent == 0)
1494 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001495 msg_didout = FALSE; // no output on current line yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 cursor_off();
1497 }
1498
Bram Moolenaar85a20022019-12-21 18:25:54 +01001499 // when redirecting, may need to start a new line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 if (!did_return)
1501 redir_write((char_u *)"\n", -1);
1502}
1503
1504/*
1505 * Note that the current msg position is where messages start.
1506 */
1507 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001508msg_starthere(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509{
1510 lines_left = cmdline_row;
1511 msg_didany = FALSE;
1512}
1513
1514 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001515msg_putchar(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516{
1517 msg_putchar_attr(c, 0);
1518}
1519
1520 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001521msg_putchar_attr(int c, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522{
Bram Moolenaar63c0ccd2019-01-19 21:06:58 +01001523 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524
1525 if (IS_SPECIAL(c))
1526 {
1527 buf[0] = K_SPECIAL;
1528 buf[1] = K_SECOND(c);
1529 buf[2] = K_THIRD(c);
1530 buf[3] = NUL;
1531 }
1532 else
Bram Moolenaar63c0ccd2019-01-19 21:06:58 +01001533 buf[(*mb_char2bytes)(c, buf)] = NUL;
Bram Moolenaar63c0ccd2019-01-19 21:06:58 +01001534 msg_puts_attr((char *)buf, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535}
1536
1537 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001538msg_outnum(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001540 char buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541
Bram Moolenaar32526b32019-01-19 17:43:09 +01001542 sprintf(buf, "%ld", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 msg_puts(buf);
1544}
1545
1546 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001547msg_home_replace(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548{
1549 msg_home_replace_attr(fname, 0);
1550}
1551
1552#if defined(FEAT_FIND_ID) || defined(PROTO)
1553 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001554msg_home_replace_hl(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555{
Bram Moolenaar8820b482017-03-16 17:23:31 +01001556 msg_home_replace_attr(fname, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557}
1558#endif
1559
1560 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001561msg_home_replace_attr(char_u *fname, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562{
1563 char_u *name;
1564
1565 name = home_replace_save(NULL, fname);
1566 if (name != NULL)
1567 msg_outtrans_attr(name, attr);
1568 vim_free(name);
1569}
1570
1571/*
1572 * Output 'len' characters in 'str' (including NULs) with translation
Bram Moolenaarf48ee3c2019-12-06 22:18:20 +01001573 * if 'len' is -1, output up to a NUL character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574 * Use attributes 'attr'.
1575 * Return the number of characters it takes on the screen.
1576 */
1577 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001578msg_outtrans(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579{
1580 return msg_outtrans_attr(str, 0);
1581}
1582
1583 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001584msg_outtrans_attr(char_u *str, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585{
1586 return msg_outtrans_len_attr(str, (int)STRLEN(str), attr);
1587}
1588
1589 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001590msg_outtrans_len(char_u *str, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591{
1592 return msg_outtrans_len_attr(str, len, 0);
1593}
1594
1595/*
1596 * Output one character at "p". Return pointer to the next character.
1597 * Handles multi-byte characters.
1598 */
1599 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001600msg_outtrans_one(char_u *p, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 int l;
1603
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001604 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 {
1606 msg_outtrans_len_attr(p, l, attr);
1607 return p + l;
1608 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01001609 msg_puts_attr((char *)transchar_byte(*p), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 return p + 1;
1611}
1612
1613 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001614msg_outtrans_len_attr(char_u *msgstr, int len, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615{
1616 int retval = 0;
1617 char_u *str = msgstr;
1618 char_u *plain_start = msgstr;
1619 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 int mb_l;
1621 int c;
Bram Moolenaar3d30af82020-10-13 22:15:56 +02001622 int save_got_int = got_int;
1623
1624 // Only quit when got_int was set in here.
1625 got_int = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626
Bram Moolenaar85a20022019-12-21 18:25:54 +01001627 // if MSG_HIST flag set, add message to history
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 if (attr & MSG_HIST)
1629 {
1630 add_msg_hist(str, len, attr);
1631 attr &= ~MSG_HIST;
1632 }
1633
Bram Moolenaar85a20022019-12-21 18:25:54 +01001634 // If the string starts with a composing character first draw a space on
1635 // which the composing char can be drawn.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 if (enc_utf8 && utf_iscomposing(utf_ptr2char(msgstr)))
Bram Moolenaar32526b32019-01-19 17:43:09 +01001637 msg_puts_attr(" ", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638
1639 /*
1640 * Go over the string. Special characters are translated and printed.
1641 * Normal characters are printed several at a time.
1642 */
Bram Moolenaar3d30af82020-10-13 22:15:56 +02001643 while (--len >= 0 && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 if (enc_utf8)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001646 // Don't include composing chars after the end.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001647 mb_l = utfc_ptr2len_len(str, len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 else if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001649 mb_l = (*mb_ptr2len)(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 else
1651 mb_l = 1;
1652 if (has_mbyte && mb_l > 1)
1653 {
1654 c = (*mb_ptr2char)(str);
1655 if (vim_isprintc(c))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001656 // printable multi-byte char: count the cells.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 retval += (*mb_ptr2cells)(str);
1658 else
1659 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001660 // unprintable multi-byte char: print the printable chars so
1661 // far and the translation of the unprintable char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 if (str > plain_start)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001663 msg_puts_attr_len((char *)plain_start,
1664 (int)(str - plain_start), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 plain_start = str + mb_l;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001666 msg_puts_attr((char *)transchar(c),
1667 attr == 0 ? HL_ATTR(HLF_8) : attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 retval += char2cells(c);
1669 }
1670 len -= mb_l - 1;
1671 str += mb_l;
1672 }
1673 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674 {
1675 s = transchar_byte(*str);
1676 if (s[1] != NUL)
1677 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001678 // unprintable char: print the printable chars so far and the
1679 // translation of the unprintable char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 if (str > plain_start)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001681 msg_puts_attr_len((char *)plain_start,
1682 (int)(str - plain_start), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 plain_start = str + 1;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001684 msg_puts_attr((char *)s, attr == 0 ? HL_ATTR(HLF_8) : attr);
Bram Moolenaarc236c162008-07-13 17:41:49 +00001685 retval += (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 }
Bram Moolenaarb3c722a2008-07-06 17:16:02 +00001687 else
1688 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 ++str;
1690 }
1691 }
1692
Bram Moolenaar3d30af82020-10-13 22:15:56 +02001693 if (str > plain_start && !got_int)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001694 // print the printable chars at the end
Bram Moolenaar32526b32019-01-19 17:43:09 +01001695 msg_puts_attr_len((char *)plain_start, (int)(str - plain_start), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696
Bram Moolenaar3d30af82020-10-13 22:15:56 +02001697 got_int |= save_got_int;
1698
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699 return retval;
1700}
1701
1702#if defined(FEAT_QUICKFIX) || defined(PROTO)
1703 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001704msg_make(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705{
1706 int i;
1707 static char_u *str = (char_u *)"eeffoc", *rs = (char_u *)"Plon#dqg#vxjduB";
1708
1709 arg = skipwhite(arg);
1710 for (i = 5; *arg && i >= 0; --i)
1711 if (*arg++ != str[i])
1712 break;
1713 if (i < 0)
1714 {
1715 msg_putchar('\n');
1716 for (i = 0; rs[i]; ++i)
1717 msg_putchar(rs[i] - 3);
1718 }
1719}
1720#endif
1721
1722/*
Bram Moolenaarf48ee3c2019-12-06 22:18:20 +01001723 * Output the string 'str' up to a NUL character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724 * Return the number of characters it takes on the screen.
1725 *
1726 * If K_SPECIAL is encountered, then it is taken in conjunction with the
1727 * following character and shown as <F1>, <S-Up> etc. Any other character
1728 * which is not printable shown in <> form.
1729 * If 'from' is TRUE (lhs of a mapping), a space is shown as <Space>.
1730 * If a character is displayed in one of these special ways, is also
1731 * highlighted (its highlight name is '8' in the p_hl variable).
1732 * Otherwise characters are not highlighted.
1733 * This function is used to show mappings, where we want to see how to type
1734 * the character/string -- webb
1735 */
1736 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001737msg_outtrans_special(
1738 char_u *strstart,
Bram Moolenaar725310d2019-04-24 23:08:23 +02001739 int from, // TRUE for lhs of a mapping
Bram Moolenaar32aa1022019-11-02 22:54:41 +01001740 int maxlen) // screen columns, 0 for unlimited
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741{
1742 char_u *str = strstart;
1743 int retval = 0;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001744 char *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745 int attr;
1746 int len;
1747
Bram Moolenaar8820b482017-03-16 17:23:31 +01001748 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 while (*str != NUL)
1750 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001751 // Leading and trailing spaces need to be displayed in <> form.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752 if ((str == strstart || str[1] == NUL) && *str == ' ')
1753 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001754 text = "<Space>";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 ++str;
1756 }
1757 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01001758 text = (char *)str2special(&str, from);
zeertzjq0519ce02022-05-09 12:16:19 +01001759 if (text[0] != NUL && text[1] == NUL)
1760 // single-byte character or illegal byte
1761 text = (char *)transchar_byte((char_u)text[0]);
Bram Moolenaar32526b32019-01-19 17:43:09 +01001762 len = vim_strsize((char_u *)text);
Bram Moolenaar725310d2019-04-24 23:08:23 +02001763 if (maxlen > 0 && retval + len >= maxlen)
1764 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001765 // Highlight special keys
Bram Moolenaar32526b32019-01-19 17:43:09 +01001766 msg_puts_attr(text, len > 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001767 && (*mb_ptr2len)((char_u *)text) <= 1 ? attr : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 retval += len;
1769 }
1770 return retval;
1771}
1772
Bram Moolenaarbd743252010-10-20 21:23:33 +02001773#if defined(FEAT_EVAL) || defined(PROTO)
1774/*
1775 * Return the lhs or rhs of a mapping, with the key codes turned into printable
1776 * strings, in an allocated string.
1777 */
1778 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001779str2special_save(
1780 char_u *str,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001781 int is_lhs) // TRUE for lhs, FALSE for rhs
Bram Moolenaarbd743252010-10-20 21:23:33 +02001782{
1783 garray_T ga;
1784 char_u *p = str;
1785
1786 ga_init2(&ga, 1, 40);
1787 while (*p != NUL)
1788 ga_concat(&ga, str2special(&p, is_lhs));
1789 ga_append(&ga, NUL);
1790 return (char_u *)ga.ga_data;
1791}
1792#endif
1793
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794/*
1795 * Return the printable string for the key codes at "*sp".
zeertzjq0519ce02022-05-09 12:16:19 +01001796 * On illegal byte return a string with only that byte.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 * Used for translating the lhs or rhs of a mapping to printable chars.
1798 * Advances "sp" to the next code.
1799 */
1800 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001801str2special(
1802 char_u **sp,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001803 int from) // TRUE for lhs of mapping
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804{
1805 int c;
1806 static char_u buf[7];
1807 char_u *str = *sp;
1808 int modifiers = 0;
1809 int special = FALSE;
1810
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 if (has_mbyte)
1812 {
1813 char_u *p;
1814
Bram Moolenaar85a20022019-12-21 18:25:54 +01001815 // Try to un-escape a multi-byte character. Return the un-escaped
1816 // string if it is a multi-byte character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 p = mb_unescape(sp);
1818 if (p != NULL)
1819 return p;
1820 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821
1822 c = *str;
1823 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1824 {
1825 if (str[1] == KS_MODIFIER)
1826 {
1827 modifiers = str[2];
1828 str += 3;
1829 c = *str;
1830 }
1831 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1832 {
1833 c = TO_SPECIAL(str[1], str[2]);
1834 str += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001836 if (IS_SPECIAL(c) || modifiers) // special key
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 special = TRUE;
1838 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839
zeertzjq0519ce02022-05-09 12:16:19 +01001840 if (has_mbyte && !IS_SPECIAL(c) && MB_BYTE2LEN(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 {
zeertzjqac402f42022-05-04 18:51:43 +01001842 char_u *p;
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001843
zeertzjqac402f42022-05-04 18:51:43 +01001844 *sp = str;
1845 // Try to un-escape a multi-byte character after modifiers.
1846 p = mb_unescape(sp);
zeertzjq0519ce02022-05-09 12:16:19 +01001847 if (p != NULL)
1848 // Since 'special' is TRUE the multi-byte character 'c' will be
1849 // processed by get_special_key_name()
1850 c = (*mb_ptr2char)(p);
1851 else
1852 // illegal byte
1853 *sp = str + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 }
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001855 else
Bram Moolenaar083692d2022-06-29 21:16:58 +01001856 // single-byte character, NUL or illegal byte
1857 *sp = str + (*str == NUL ? 0 : 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858
zeertzjq0519ce02022-05-09 12:16:19 +01001859 // Make special keys and C0 control characters in <> form, also <M-Space>.
Bram Moolenaar85a20022019-12-21 18:25:54 +01001860 // Use <Space> only for lhs of a mapping.
zeertzjq0519ce02022-05-09 12:16:19 +01001861 if (special || c < ' ' || (from && c == ' '))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 return get_special_key_name(c, modifiers);
1863 buf[0] = c;
1864 buf[1] = NUL;
1865 return buf;
1866}
1867
1868/*
1869 * Translate a key sequence into special key names.
1870 */
1871 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001872str2specialbuf(char_u *sp, char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873{
1874 char_u *s;
1875
1876 *buf = NUL;
1877 while (*sp)
1878 {
1879 s = str2special(&sp, FALSE);
1880 if ((int)(STRLEN(s) + STRLEN(buf)) < len)
1881 STRCAT(buf, s);
1882 }
1883}
1884
1885/*
1886 * print line for :print or :list command
1887 */
1888 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001889msg_prt_line(char_u *s, int list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890{
1891 int c;
1892 int col = 0;
1893 int n_extra = 0;
1894 int c_extra = 0;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001895 int c_final = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001896 char_u *p_extra = NULL; // init to make SASC shut up
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 int n;
Bram Moolenaar56da7972007-01-16 14:46:32 +00001898 int attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 char_u *trail = NULL;
Bram Moolenaar91478ae2021-02-03 15:58:13 +01001900 char_u *lead = NULL;
zeertzjqf14b8ba2021-09-10 16:58:30 +02001901 int in_multispace = FALSE;
1902 int multispace_pos = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 int l;
1904 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905
Bram Moolenaardf177f62005-02-22 08:39:57 +00001906 if (curwin->w_p_list)
1907 list = TRUE;
1908
Bram Moolenaar91478ae2021-02-03 15:58:13 +01001909 if (list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 {
Bram Moolenaar91478ae2021-02-03 15:58:13 +01001911 // find start of trailing whitespace
Bram Moolenaareed9d462021-02-15 20:38:25 +01001912 if (curwin->w_lcs_chars.trail)
Bram Moolenaar91478ae2021-02-03 15:58:13 +01001913 {
1914 trail = s + STRLEN(s);
1915 while (trail > s && VIM_ISWHITE(trail[-1]))
1916 --trail;
1917 }
1918 // find end of leading whitespace
Bram Moolenaarb8329db2022-07-06 13:31:28 +01001919 if (curwin->w_lcs_chars.lead
1920 || curwin->w_lcs_chars.leadmultispace != NULL)
Bram Moolenaar91478ae2021-02-03 15:58:13 +01001921 {
1922 lead = s;
1923 while (VIM_ISWHITE(lead[0]))
1924 lead++;
1925 // in a line full of spaces all of them are treated as trailing
1926 if (*lead == NUL)
1927 lead = NULL;
1928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 }
1930
Bram Moolenaar85a20022019-12-21 18:25:54 +01001931 // output a space for an empty line, otherwise the line will be
1932 // overwritten
Bram Moolenaareed9d462021-02-15 20:38:25 +01001933 if (*s == NUL && !(list && curwin->w_lcs_chars.eol != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934 msg_putchar(' ');
1935
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001936 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 {
Bram Moolenaar56da7972007-01-16 14:46:32 +00001938 if (n_extra > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 {
1940 --n_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001941 if (n_extra == 0 && c_final)
1942 c = c_final;
1943 else if (c_extra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 c = c_extra;
1945 else
1946 c = *p_extra++;
1947 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001948 else if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 {
1950 col += (*mb_ptr2cells)(s);
Bram Moolenaar1cbfc992020-12-02 12:37:37 +01001951 if (l >= MB_MAXBYTES)
1952 {
Bram Moolenaar29d2f452020-12-04 19:42:52 +01001953 STRCPY(buf, "?");
Bram Moolenaar1cbfc992020-12-02 12:37:37 +01001954 }
Bram Moolenaareed9d462021-02-15 20:38:25 +01001955 else if (curwin->w_lcs_chars.nbsp != NUL && list
Bram Moolenaar73284b92015-05-04 17:28:22 +02001956 && (mb_ptr2char(s) == 160
1957 || mb_ptr2char(s) == 0x202f))
Bram Moolenaaracf17282011-02-01 17:12:25 +01001958 {
Bram Moolenaar74ac29c2022-06-15 12:12:44 +01001959 int len = mb_char2bytes(curwin->w_lcs_chars.nbsp, buf);
1960
1961 buf[len] = NUL;
Bram Moolenaaracf17282011-02-01 17:12:25 +01001962 }
1963 else
1964 {
1965 mch_memmove(buf, s, (size_t)l);
1966 buf[l] = NUL;
1967 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01001968 msg_puts((char *)buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 s += l;
1970 continue;
1971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 else
1973 {
1974 attr = 0;
1975 c = *s++;
zeertzjqf14b8ba2021-09-10 16:58:30 +02001976 in_multispace = c == ' '
1977 && ((col > 0 && s[-2] == ' ') || *s == ' ');
1978 if (!in_multispace)
1979 multispace_pos = 0;
Bram Moolenaareed9d462021-02-15 20:38:25 +01001980 if (c == TAB && (!list || curwin->w_lcs_chars.tab1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001982 // tab amount depends on current column
Bram Moolenaar04958cb2018-06-23 19:23:02 +02001983#ifdef FEAT_VARTABS
1984 n_extra = tabstop_padding(col, curbuf->b_p_ts,
1985 curbuf->b_p_vts_array) - 1;
1986#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 n_extra = curbuf->b_p_ts - col % curbuf->b_p_ts - 1;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02001988#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00001989 if (!list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 {
1991 c = ' ';
1992 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01001993 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 }
1995 else
1996 {
Bram Moolenaareed9d462021-02-15 20:38:25 +01001997 c = (n_extra == 0 && curwin->w_lcs_chars.tab3)
1998 ? curwin->w_lcs_chars.tab3
1999 : curwin->w_lcs_chars.tab1;
2000 c_extra = curwin->w_lcs_chars.tab2;
2001 c_final = curwin->w_lcs_chars.tab3;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002002 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 }
2004 }
Bram Moolenaareed9d462021-02-15 20:38:25 +01002005 else if (c == 160 && list && curwin->w_lcs_chars.nbsp != NUL)
Bram Moolenaaracf17282011-02-01 17:12:25 +01002006 {
Bram Moolenaareed9d462021-02-15 20:38:25 +01002007 c = curwin->w_lcs_chars.nbsp;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002008 attr = HL_ATTR(HLF_8);
Bram Moolenaaracf17282011-02-01 17:12:25 +01002009 }
Bram Moolenaareed9d462021-02-15 20:38:25 +01002010 else if (c == NUL && list && curwin->w_lcs_chars.eol != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 {
2012 p_extra = (char_u *)"";
2013 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01002014 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 n_extra = 1;
Bram Moolenaareed9d462021-02-15 20:38:25 +01002016 c = curwin->w_lcs_chars.eol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002017 attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 --s;
2019 }
2020 else if (c != NUL && (n = byte2cells(c)) > 1)
2021 {
2022 n_extra = n - 1;
2023 p_extra = transchar_byte(c);
2024 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01002025 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 c = *p_extra++;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002027 // Use special coloring to be able to distinguish <hex> from
2028 // the same in plain text.
Bram Moolenaar8820b482017-03-16 17:23:31 +01002029 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 }
zeertzjqf14b8ba2021-09-10 16:58:30 +02002031 else if (c == ' ')
Bram Moolenaar91478ae2021-02-03 15:58:13 +01002032 {
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01002033 if (list && lead != NULL && s <= lead && in_multispace
2034 && curwin->w_lcs_chars.leadmultispace != NULL)
2035 {
2036 c = curwin->w_lcs_chars.leadmultispace[multispace_pos++];
zeertzjqb5f08012022-06-09 13:55:28 +01002037 if (curwin->w_lcs_chars.leadmultispace[multispace_pos]
2038 == NUL)
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01002039 multispace_pos = 0;
2040 attr = HL_ATTR(HLF_8);
2041 }
zeertzjqb5f08012022-06-09 13:55:28 +01002042 else if (lead != NULL && s <= lead
2043 && curwin->w_lcs_chars.lead != NUL)
zeertzjqf14b8ba2021-09-10 16:58:30 +02002044 {
2045 c = curwin->w_lcs_chars.lead;
2046 attr = HL_ATTR(HLF_8);
2047 }
2048 else if (trail != NULL && s > trail)
2049 {
2050 c = curwin->w_lcs_chars.trail;
2051 attr = HL_ATTR(HLF_8);
2052 }
2053 else if (list && in_multispace
2054 && curwin->w_lcs_chars.multispace != NULL)
2055 {
2056 c = curwin->w_lcs_chars.multispace[multispace_pos++];
2057 if (curwin->w_lcs_chars.multispace[multispace_pos] == NUL)
2058 multispace_pos = 0;
2059 attr = HL_ATTR(HLF_8);
2060 }
2061 else if (list && curwin->w_lcs_chars.space != NUL)
2062 {
2063 c = curwin->w_lcs_chars.space;
2064 attr = HL_ATTR(HLF_8);
2065 }
Bram Moolenaar1510f992015-04-22 22:18:22 +02002066 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 }
2068
2069 if (c == NUL)
2070 break;
2071
2072 msg_putchar_attr(c, attr);
2073 col++;
2074 }
2075 msg_clr_eos();
2076}
2077
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078/*
2079 * Use screen_puts() to output one multi-byte character.
2080 * Return the pointer "s" advanced to the next character.
2081 */
2082 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002083screen_puts_mbyte(char_u *s, int l, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084{
2085 int cw;
2086
Bram Moolenaar85a20022019-12-21 18:25:54 +01002087 msg_didout = TRUE; // remember that line is not empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 cw = (*mb_ptr2cells)(s);
2089 if (cw > 1 && (
2090#ifdef FEAT_RIGHTLEFT
2091 cmdmsg_rl ? msg_col <= 1 :
2092#endif
2093 msg_col == Columns - 1))
2094 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002095 // Doesn't fit, print a highlighted '>' to fill it up.
Bram Moolenaar8820b482017-03-16 17:23:31 +01002096 msg_screen_putchar('>', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097 return s;
2098 }
2099
2100 screen_puts_len(s, l, msg_row, msg_col, attr);
2101#ifdef FEAT_RIGHTLEFT
2102 if (cmdmsg_rl)
2103 {
2104 msg_col -= cw;
2105 if (msg_col == 0)
2106 {
2107 msg_col = Columns;
2108 ++msg_row;
2109 }
2110 }
2111 else
2112#endif
2113 {
2114 msg_col += cw;
2115 if (msg_col >= Columns)
2116 {
2117 msg_col = 0;
2118 ++msg_row;
2119 }
2120 }
2121 return s + l;
2122}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123
2124/*
2125 * Output a string to the screen at position msg_row, msg_col.
2126 * Update msg_row and msg_col for the next message.
2127 */
2128 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002129msg_puts(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130{
Bram Moolenaaraeac9002016-09-06 22:15:08 +02002131 msg_puts_attr(s, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132}
2133
2134 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002135msg_puts_title(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136{
Bram Moolenaar8820b482017-03-16 17:23:31 +01002137 msg_puts_attr(s, HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138}
2139
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140/*
2141 * Show a message in such a way that it always fits in the line. Cut out a
2142 * part in the middle and replace it with "..." when necessary.
2143 * Does not handle multi-byte characters!
2144 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002145 static void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002146msg_outtrans_long_len_attr(char_u *longstr, int len, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147{
2148 int slen = len;
2149 int room;
2150
2151 room = Columns - msg_col;
2152 if (len > room && room >= 20)
2153 {
2154 slen = (room - 3) / 2;
2155 msg_outtrans_len_attr(longstr, slen, attr);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002156 msg_puts_attr("...", HL_ATTR(HLF_8));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 }
2158 msg_outtrans_len_attr(longstr + len - slen, slen, attr);
2159}
2160
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002161 void
2162msg_outtrans_long_attr(char_u *longstr, int attr)
2163{
2164 msg_outtrans_long_len_attr(longstr, (int)STRLEN(longstr), attr);
2165}
2166
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167/*
2168 * Basic function for writing a message with highlight attributes.
2169 */
2170 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002171msg_puts_attr(char *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172{
2173 msg_puts_attr_len(s, -1, attr);
2174}
2175
2176/*
2177 * Like msg_puts_attr(), but with a maximum length "maxlen" (in bytes).
2178 * When "maxlen" is -1 there is no maximum length.
2179 * When "maxlen" is >= 0 the message is not put in the history.
2180 */
2181 static void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002182msg_puts_attr_len(char *str, int maxlen, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 /*
2185 * If redirection is on, also write to the redirection file.
2186 */
Bram Moolenaar32526b32019-01-19 17:43:09 +01002187 redir_write((char_u *)str, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188
2189 /*
2190 * Don't print anything when using ":silent cmd".
2191 */
2192 if (msg_silent != 0)
2193 return;
2194
Bram Moolenaar85a20022019-12-21 18:25:54 +01002195 // if MSG_HIST flag set, add message to history
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196 if ((attr & MSG_HIST) && maxlen < 0)
2197 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002198 add_msg_hist((char_u *)str, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 attr &= ~MSG_HIST;
2200 }
2201
Bram Moolenaare8070982019-10-12 17:07:06 +02002202 // When writing something to the screen after it has scrolled, requires a
2203 // wait-return prompt later. Needed when scrolling, resetting
2204 // need_wait_return after some prompt, and then outputting something
2205 // without scrolling
2206 // Not needed when only using CR to move the cursor.
2207 if (msg_scrolled != 0 && !msg_scrolled_ign && STRCMP(str, "\r") != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 need_wait_return = TRUE;
Bram Moolenaare8070982019-10-12 17:07:06 +02002209 msg_didany = TRUE; // remember that something was outputted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210
2211 /*
2212 * If there is no valid screen, use fprintf so we can see error messages.
2213 * If termcap is not active, we may be writing in an alternate console
2214 * window, cursor positioning may not work correctly (window size may be
2215 * different, e.g. for Win32 console) or we just don't know where the
2216 * cursor is.
2217 */
2218 if (msg_use_printf())
Bram Moolenaar32526b32019-01-19 17:43:09 +01002219 msg_puts_printf((char_u *)str, maxlen);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002220 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01002221 msg_puts_display((char_u *)str, maxlen, attr, FALSE);
Rob Pilling726f7f92022-01-20 14:44:38 +00002222
2223 need_fileinfo = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002224}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225
Bram Moolenaar9198de32022-08-27 21:30:03 +01002226// values for "where"
2227#define PUT_APPEND 0 // append to "lnum"
2228#define PUT_TRUNC 1 // replace "lnum"
2229#define PUT_BELOW 2 // add below "lnum"
2230 //
2231#ifdef HAS_MESSAGE_WINDOW
2232
2233/*
2234 * Put text "t_s" until "s" in the message window.
2235 * "where" specifies where to put the text.
2236 */
2237 static void
2238put_msg_win(win_T *wp, int where, char_u *t_s, char_u *end, linenr_T lnum)
2239{
2240 int c = *end;
2241
2242 *end = NUL;
2243 if (where == PUT_BELOW)
2244 ml_append_buf(wp->w_buffer, lnum, t_s, (colnr_T)0, FALSE);
2245 else
2246 {
2247 char_u *newp;
2248
2249 curbuf = wp->w_buffer;
2250 if (where == PUT_APPEND)
2251 newp = concat_str(ml_get(lnum), t_s);
2252 else
2253 newp = vim_strnsave(t_s, end - t_s);
2254 ml_replace(lnum, newp, FALSE);
2255 curbuf = curwin->w_buffer;
2256 }
2257 redraw_win_later(wp, UPD_NOT_VALID);
2258
2259 // set msg_col so that a newline is written if needed
2260 msg_col = STRLEN(t_s);
2261
2262 *end = c;
2263}
2264#endif
2265
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002266/*
2267 * The display part of msg_puts_attr_len().
2268 * May be called recursively to display scroll-back text.
2269 */
2270 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002271msg_puts_display(
2272 char_u *str,
2273 int maxlen,
2274 int attr,
2275 int recurse)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002276{
2277 char_u *s = str;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002278 char_u *t_s = str; // string from "t_s" to "s" is still todo
2279 int t_col = 0; // screen cells todo, 0 when "t_s" not used
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002280 int l;
2281 int cw;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002282 char_u *sb_str = str;
2283 int sb_col = msg_col;
2284 int wrap;
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002285 int did_last_char;
Bram Moolenaar9198de32022-08-27 21:30:03 +01002286 int where = PUT_APPEND;
2287#ifdef HAS_MESSAGE_WINDOW
2288 win_T *msg_win = NULL;
2289 linenr_T lnum = 1;
2290
2291 if (use_message_window())
2292 {
2293 msg_win = popup_get_message_win();
2294
2295 if (msg_win != NULL)
2296 {
2297 if (!popup_message_win_visible())
2298 {
2299 if (*str == NL)
2300 {
2301 // When not showing the message window and the output
2302 // starts with a NL show the message normally.
2303 msg_win = NULL;
2304 }
2305 else
2306 {
2307 // currently hidden, make it empty
2308 curbuf = msg_win->w_buffer;
2309 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
2310 ml_delete(1);
2311 curbuf = curwin->w_buffer;
2312 }
2313 }
2314 else
2315 {
2316 lnum = msg_win->w_buffer->b_ml.ml_line_count;
2317 if (msg_col == 0)
2318 where = PUT_TRUNC;
2319 }
2320 }
2321 }
2322#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323
2324 did_wait_return = FALSE;
Bram Moolenaar57b7fe82007-08-05 17:20:43 +00002325 while ((maxlen < 0 || (int)(s - str) < maxlen) && *s != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 {
2327 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002328 * We are at the end of the screen line when:
2329 * - When outputting a newline.
2330 * - When outputting a character in the last column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002332 if (!recurse && msg_row >= Rows - 1 && (*s == '\n' || (
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333#ifdef FEAT_RIGHTLEFT
2334 cmdmsg_rl
2335 ? (
2336 msg_col <= 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002337 || (*s == TAB && msg_col <= 7)
2338 || (has_mbyte && (*mb_ptr2cells)(s) > 1 && msg_col <= 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 :
2340#endif
Bram Moolenaar0efd1bd2019-12-11 19:00:04 +01002341 ((*s != '\r' && msg_col + t_col >= Columns - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 || (*s == TAB && msg_col + t_col >= ((Columns - 1) & ~7))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 || (has_mbyte && (*mb_ptr2cells)(s) > 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002344 && msg_col + t_col >= Columns - 2)))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002346 /*
2347 * The screen is scrolled up when at the last row (some terminals
2348 * scroll automatically, some don't. To avoid problems we scroll
2349 * ourselves).
2350 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 if (t_col > 0)
Bram Moolenaar9198de32022-08-27 21:30:03 +01002352 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002353 // output postponed text
Bram Moolenaar9198de32022-08-27 21:30:03 +01002354#ifdef HAS_MESSAGE_WINDOW
2355 if (msg_win != NULL)
2356 {
2357 put_msg_win(msg_win, where, t_s, s, lnum);
2358 t_col = 0;
2359 where = PUT_BELOW;
2360 }
2361 else
2362#endif
2363 t_puts(&t_col, t_s, s, attr);
2364 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365
Bram Moolenaar85a20022019-12-21 18:25:54 +01002366 // When no more prompt and no more room, truncate here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 if (msg_no_more && lines_left == 0)
2368 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369
Bram Moolenaar9198de32022-08-27 21:30:03 +01002370#ifdef HAS_MESSAGE_WINDOW
2371 if (msg_win == NULL)
2372#endif
2373 // Scroll the screen up one line.
2374 msg_scroll_up();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375
2376 msg_row = Rows - 2;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002377 if (msg_col >= Columns) // can happen after screen resize
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378 msg_col = Columns - 1;
2379
Bram Moolenaar85a20022019-12-21 18:25:54 +01002380 // Display char in last column before showing more-prompt.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002381 if (*s >= ' '
2382#ifdef FEAT_RIGHTLEFT
2383 && !cmdmsg_rl
2384#endif
2385 )
2386 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002387 if (has_mbyte)
2388 {
2389 if (enc_utf8 && maxlen >= 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002390 // avoid including composing chars after the end
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002391 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002392 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002393 l = (*mb_ptr2len)(s);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002394 s = screen_puts_mbyte(s, l, attr);
2395 }
2396 else
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002397 msg_screen_putchar(*s++, attr);
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002398 did_last_char = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002399 }
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002400 else
2401 did_last_char = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002402
2403 if (p_more)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002404 // store text for scrolling back
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002405 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
2406
Bram Moolenaar9198de32022-08-27 21:30:03 +01002407#ifdef HAS_MESSAGE_WINDOW
2408 if (msg_win == NULL)
2409 {
2410#endif
2411 inc_msg_scrolled();
2412 need_wait_return = TRUE; // may need wait_return in main()
2413 redraw_cmdline = TRUE;
2414 if (cmdline_row > 0 && !exmode_active)
2415 --cmdline_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416
Bram Moolenaar9198de32022-08-27 21:30:03 +01002417 /*
2418 * If screen is completely filled and 'more' is set then wait
2419 * for a character.
2420 */
2421 if (lines_left > 0)
2422 --lines_left;
2423#ifdef HAS_MESSAGE_WINDOW
2424 }
2425#endif
Bram Moolenaar24959102022-05-07 20:01:16 +01002426 if (p_more && lines_left == 0 && State != MODE_HITRETURN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 && !msg_no_more && !exmode_active)
2428 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429#ifdef FEAT_CON_DIALOG
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002430 if (do_more_prompt(NUL))
2431 s = confirm_msg_tail;
2432#else
2433 (void)do_more_prompt(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434#endif
2435 if (quit_more)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002436 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002438
Bram Moolenaar85a20022019-12-21 18:25:54 +01002439 // When we displayed a char in last column need to check if there
2440 // is still more.
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002441 if (did_last_char)
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002442 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 }
2444
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002445 wrap = *s == '\n'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 || msg_col + t_col >= Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 || (has_mbyte && (*mb_ptr2cells)(s) > 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002448 && msg_col + t_col >= Columns - 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002449 if (t_col > 0 && (wrap || *s == '\r' || *s == '\b'
2450 || *s == '\t' || *s == BELL))
Bram Moolenaar9198de32022-08-27 21:30:03 +01002451 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002452 // output any postponed text
Bram Moolenaar9198de32022-08-27 21:30:03 +01002453#ifdef HAS_MESSAGE_WINDOW
2454 if (msg_win != NULL)
2455 {
2456 put_msg_win(msg_win, where, t_s, s, lnum);
2457 t_col = 0;
2458 where = PUT_BELOW;
2459 }
2460 else
2461#endif
2462 t_puts(&t_col, t_s, s, attr);
2463 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002464
2465 if (wrap && p_more && !recurse)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002466 // store text for scrolling back
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002467 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468
Bram Moolenaar85a20022019-12-21 18:25:54 +01002469 if (*s == '\n') // go to next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 {
Bram Moolenaar9198de32022-08-27 21:30:03 +01002471#ifdef HAS_MESSAGE_WINDOW
2472 if (msg_win != NULL)
2473 {
2474 // Ignore a NL when the buffer is empty, it is used to scroll
2475 // up the text.
2476 if ((msg_win->w_buffer->b_ml.ml_flags & ML_EMPTY) == 0)
2477 {
2478 put_msg_win(msg_win, PUT_BELOW, t_s, t_s, lnum);
2479 ++lnum;
2480 }
2481 }
2482 else
2483#endif
2484 msg_didout = FALSE; // remember that line is empty
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002485#ifdef FEAT_RIGHTLEFT
2486 if (cmdmsg_rl)
2487 msg_col = Columns - 1;
2488 else
2489#endif
2490 msg_col = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002491 if (++msg_row >= Rows) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 msg_row = Rows - 1;
2493 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002494 else if (*s == '\r') // go to column 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 {
2496 msg_col = 0;
Bram Moolenaar9198de32022-08-27 21:30:03 +01002497 where = PUT_TRUNC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002499 else if (*s == '\b') // go to previous char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 {
2501 if (msg_col)
2502 --msg_col;
2503 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002504 else if (*s == TAB) // translate Tab into spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 {
Bram Moolenaar9198de32022-08-27 21:30:03 +01002506#ifdef HAS_MESSAGE_WINDOW
2507 if (msg_win != NULL)
2508 msg_col = (msg_col + 7) % 8;
2509 else
2510#endif
2511 do
2512 msg_screen_putchar(' ', attr);
2513 while (msg_col & 7);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002515 else if (*s == BELL) // beep (from ":sh")
Bram Moolenaar165bc692015-07-21 17:53:25 +02002516 vim_beep(BO_SH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 else
2518 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 if (has_mbyte)
2520 {
2521 cw = (*mb_ptr2cells)(s);
2522 if (enc_utf8 && maxlen >= 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002523 // avoid including composing chars after the end
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002524 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002526 l = (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 }
2528 else
2529 {
2530 cw = 1;
2531 l = 1;
2532 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002533
Bram Moolenaar85a20022019-12-21 18:25:54 +01002534 // When drawing from right to left or when a double-wide character
2535 // doesn't fit, draw a single character here. Otherwise collect
2536 // characters and draw them all at once later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 if (
2538# ifdef FEAT_RIGHTLEFT
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002539 cmdmsg_rl ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540# endif
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002541 (cw > 1 && msg_col + t_col >= Columns - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 if (l > 1)
2544 s = screen_puts_mbyte(s, l, attr) - 1;
2545 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 msg_screen_putchar(*s, attr);
2547 }
2548 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002550 // postpone this character until later
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 if (t_col == 0)
2552 t_s = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 t_col += cw;
2554 s += l - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 }
2556 }
2557 ++s;
2558 }
2559
Bram Moolenaar85a20022019-12-21 18:25:54 +01002560 // output any postponed text
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 if (t_col > 0)
Bram Moolenaar9198de32022-08-27 21:30:03 +01002562 {
2563#ifdef HAS_MESSAGE_WINDOW
2564 if (msg_win != NULL)
2565 put_msg_win(msg_win, where, t_s, s, lnum);
2566 else
2567#endif
2568 t_puts(&t_col, t_s, s, attr);
2569 }
2570
2571#ifdef HAS_MESSAGE_WINDOW
2572 if (msg_win != NULL)
2573 popup_show_message_win();
2574#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002575 if (p_more && !recurse)
2576 store_sb_text(&sb_str, s, attr, &sb_col, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577
2578 msg_check();
2579}
2580
2581/*
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002582 * Return TRUE when ":filter pattern" was used and "msg" does not match
2583 * "pattern".
2584 */
2585 int
2586message_filtered(char_u *msg)
2587{
Bram Moolenaard29459b2016-08-26 22:29:11 +02002588 int match;
2589
Bram Moolenaare1004402020-10-24 20:49:43 +02002590 if (cmdmod.cmod_filter_regmatch.regprog == NULL)
Bram Moolenaard29459b2016-08-26 22:29:11 +02002591 return FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02002592 match = vim_regexec(&cmdmod.cmod_filter_regmatch, msg, (colnr_T)0);
2593 return cmdmod.cmod_filter_force ? match : !match;
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002594}
2595
2596/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002597 * Scroll the screen up one line for displaying the next message line.
2598 */
2599 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002600msg_scroll_up(void)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002601{
Bram Moolenaar9198de32022-08-27 21:30:03 +01002602#ifdef HAS_MESSAGE_WINDOW
2603 if (use_message_window())
2604 return;
2605#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002606#ifdef FEAT_GUI
Bram Moolenaar85a20022019-12-21 18:25:54 +01002607 // Remove the cursor before scrolling, ScreenLines[] is going
2608 // to become invalid.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002609 if (gui.in_use)
2610 gui_undraw_cursor();
2611#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01002612 // scrolling up always works
Bram Moolenaara338adc2018-01-31 20:51:47 +01002613 mch_disable_flush();
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002614 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaara338adc2018-01-31 20:51:47 +01002615 mch_enable_flush();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002616
2617 if (!can_clear((char_u *)" "))
2618 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002619 // Scrolling up doesn't result in the right background. Set the
2620 // background here. It's not efficient, but avoids that we have to do
2621 // it all over the code.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002622 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2623
Bram Moolenaar85a20022019-12-21 18:25:54 +01002624 // Also clear the last char of the last but one line if it was not
2625 // cleared before to avoid a scroll-up.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002626 if (ScreenAttrs[LineOffset[Rows - 2] + Columns - 1] == (sattr_T)-1)
2627 screen_fill((int)Rows - 2, (int)Rows - 1,
2628 (int)Columns - 1, (int)Columns, ' ', ' ', 0);
2629 }
2630}
2631
2632/*
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002633 * Increment "msg_scrolled".
2634 */
2635 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002636inc_msg_scrolled(void)
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002637{
2638#ifdef FEAT_EVAL
2639 if (*get_vim_var_str(VV_SCROLLSTART) == NUL)
2640 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002641 char_u *p = SOURCING_NAME;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002642 char_u *tofree = NULL;
2643 int len;
2644
Bram Moolenaar85a20022019-12-21 18:25:54 +01002645 // v:scrollstart is empty, set it to the script/function name and line
2646 // number
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002647 if (p == NULL)
2648 p = (char_u *)_("Unknown");
2649 else
2650 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002651 len = (int)STRLEN(p) + 40;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002652 tofree = alloc(len);
2653 if (tofree != NULL)
2654 {
2655 vim_snprintf((char *)tofree, len, _("%s line %ld"),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002656 p, (long)SOURCING_LNUM);
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002657 p = tofree;
2658 }
2659 }
2660 set_vim_var_string(VV_SCROLLSTART, p, -1);
2661 vim_free(tofree);
2662 }
2663#endif
2664 ++msg_scrolled;
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01002665 set_must_redraw(UPD_VALID);
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002666}
2667
2668/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002669 * To be able to scroll back at the "more" and "hit-enter" prompts we need to
2670 * store the displayed text and remember where screen lines start.
2671 */
2672typedef struct msgchunk_S msgchunk_T;
2673struct msgchunk_S
2674{
2675 msgchunk_T *sb_next;
2676 msgchunk_T *sb_prev;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002677 char sb_eol; // TRUE when line ends after this text
2678 int sb_msg_col; // column in which text starts
2679 int sb_attr; // text attributes
2680 char_u sb_text[1]; // text to be displayed, actually longer
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002681};
2682
Bram Moolenaar85a20022019-12-21 18:25:54 +01002683static msgchunk_T *last_msgchunk = NULL; // last displayed text
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002684
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002685static msgchunk_T *msg_sb_start(msgchunk_T *mps);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002686
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002687typedef enum {
2688 SB_CLEAR_NONE = 0,
2689 SB_CLEAR_ALL,
2690 SB_CLEAR_CMDLINE_BUSY,
2691 SB_CLEAR_CMDLINE_DONE
2692} sb_clear_T;
2693
Bram Moolenaar85a20022019-12-21 18:25:54 +01002694// When to clear text on next msg.
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002695static sb_clear_T do_clear_sb_text = SB_CLEAR_NONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002696
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002697/*
2698 * Store part of a printed message for displaying when scrolling back.
2699 */
2700 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002701store_sb_text(
Bram Moolenaar85a20022019-12-21 18:25:54 +01002702 char_u **sb_str, // start of string
2703 char_u *s, // just after string
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002704 int attr,
2705 int *sb_col,
Bram Moolenaar85a20022019-12-21 18:25:54 +01002706 int finish) // line ends
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002707{
2708 msgchunk_T *mp;
2709
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002710 if (do_clear_sb_text == SB_CLEAR_ALL
2711 || do_clear_sb_text == SB_CLEAR_CMDLINE_DONE)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002712 {
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002713 clear_sb_text(do_clear_sb_text == SB_CLEAR_ALL);
zeertzjq46af7bc2022-07-28 12:34:09 +01002714 msg_sb_eol(); // prevent messages from overlapping
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002715 do_clear_sb_text = SB_CLEAR_NONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002716 }
2717
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002718 if (s > *sb_str)
2719 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002720 mp = alloc(sizeof(msgchunk_T) + (s - *sb_str));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002721 if (mp != NULL)
2722 {
2723 mp->sb_eol = finish;
2724 mp->sb_msg_col = *sb_col;
2725 mp->sb_attr = attr;
2726 vim_strncpy(mp->sb_text, *sb_str, s - *sb_str);
2727
2728 if (last_msgchunk == NULL)
2729 {
2730 last_msgchunk = mp;
2731 mp->sb_prev = NULL;
2732 }
2733 else
2734 {
2735 mp->sb_prev = last_msgchunk;
2736 last_msgchunk->sb_next = mp;
2737 last_msgchunk = mp;
2738 }
2739 mp->sb_next = NULL;
2740 }
2741 }
2742 else if (finish && last_msgchunk != NULL)
2743 last_msgchunk->sb_eol = TRUE;
2744
2745 *sb_str = s;
2746 *sb_col = 0;
2747}
2748
2749/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002750 * Finished showing messages, clear the scroll-back text on the next message.
2751 */
2752 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002753may_clear_sb_text(void)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002754{
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002755 do_clear_sb_text = SB_CLEAR_ALL;
2756}
2757
2758/*
zeertzjq46af7bc2022-07-28 12:34:09 +01002759 * Starting to edit the command line: do not clear messages now.
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002760 */
2761 void
2762sb_text_start_cmdline(void)
2763{
zeertzjq46af7bc2022-07-28 12:34:09 +01002764 if (do_clear_sb_text == SB_CLEAR_CMDLINE_BUSY)
2765 // Invoking command line recursively: the previous-level command line
2766 // doesn't need to be remembered as it will be redrawn when returning
2767 // to that level.
2768 sb_text_restart_cmdline();
2769 else
2770 {
2771 msg_sb_eol();
2772 do_clear_sb_text = SB_CLEAR_CMDLINE_BUSY;
2773 }
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002774}
2775
2776/*
zeertzjq46af7bc2022-07-28 12:34:09 +01002777 * Redrawing the command line: clear the last unfinished line.
2778 */
2779 void
2780sb_text_restart_cmdline(void)
2781{
2782 msgchunk_T *tofree;
2783
2784 // Needed when returning from nested command line.
2785 do_clear_sb_text = SB_CLEAR_CMDLINE_BUSY;
2786
2787 if (last_msgchunk == NULL || last_msgchunk->sb_eol)
2788 // No unfinished line: don't clear anything.
2789 return;
2790
2791 tofree = msg_sb_start(last_msgchunk);
2792 last_msgchunk = tofree->sb_prev;
2793 if (last_msgchunk != NULL)
2794 last_msgchunk->sb_next = NULL;
2795 while (tofree != NULL)
2796 {
2797 msgchunk_T *tofree_next = tofree->sb_next;
2798
2799 vim_free(tofree);
2800 tofree = tofree_next;
2801 }
2802}
2803
2804/*
2805 * Ending to edit the command line: clear old lines but the last one later.
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002806 */
2807 void
2808sb_text_end_cmdline(void)
2809{
2810 do_clear_sb_text = SB_CLEAR_CMDLINE_DONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002811}
2812
2813/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002814 * Clear any text remembered for scrolling back.
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002815 * When "all" is FALSE keep the last line.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002816 * Called when redrawing the screen.
2817 */
2818 void
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002819clear_sb_text(int all)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002820{
2821 msgchunk_T *mp;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002822 msgchunk_T **lastp;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002823
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002824 if (all)
2825 lastp = &last_msgchunk;
2826 else
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002827 {
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002828 if (last_msgchunk == NULL)
2829 return;
zeertzjqecdc82e2022-07-25 19:50:57 +01002830 lastp = &msg_sb_start(last_msgchunk)->sb_prev;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002831 }
2832
2833 while (*lastp != NULL)
2834 {
2835 mp = (*lastp)->sb_prev;
2836 vim_free(*lastp);
2837 *lastp = mp;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002838 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002839}
2840
2841/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002842 * "g<" command.
2843 */
2844 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002845show_sb_text(void)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002846{
2847 msgchunk_T *mp;
2848
Bram Moolenaar85a20022019-12-21 18:25:54 +01002849 // Only show something if there is more than one line, otherwise it looks
2850 // weird, typing a command without output results in one line.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002851 mp = msg_sb_start(last_msgchunk);
2852 if (mp == NULL || mp->sb_prev == NULL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02002853 vim_beep(BO_MESS);
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002854 else
2855 {
2856 do_more_prompt('G');
2857 wait_return(FALSE);
2858 }
2859}
2860
2861/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002862 * Move to the start of screen line in already displayed text.
2863 */
2864 static msgchunk_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002865msg_sb_start(msgchunk_T *mps)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002866{
2867 msgchunk_T *mp = mps;
2868
2869 while (mp != NULL && mp->sb_prev != NULL && !mp->sb_prev->sb_eol)
2870 mp = mp->sb_prev;
2871 return mp;
2872}
2873
2874/*
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02002875 * Mark the last message chunk as finishing the line.
2876 */
2877 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002878msg_sb_eol(void)
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02002879{
2880 if (last_msgchunk != NULL)
2881 last_msgchunk->sb_eol = TRUE;
2882}
2883
2884/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002885 * Display a screen line from previously displayed text at row "row".
2886 * Returns a pointer to the text for the next line (can be NULL).
2887 */
2888 static msgchunk_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002889disp_sb_line(int row, msgchunk_T *smp)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002890{
2891 msgchunk_T *mp = smp;
2892 char_u *p;
2893
2894 for (;;)
2895 {
2896 msg_row = row;
2897 msg_col = mp->sb_msg_col;
2898 p = mp->sb_text;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002899 if (*p == '\n') // don't display the line break
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002900 ++p;
2901 msg_puts_display(p, -1, mp->sb_attr, TRUE);
2902 if (mp->sb_eol || mp->sb_next == NULL)
2903 break;
2904 mp = mp->sb_next;
2905 }
2906 return mp->sb_next;
2907}
2908
2909/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 * Output any postponed text for msg_puts_attr_len().
2911 */
2912 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002913t_puts(
2914 int *t_col,
2915 char_u *t_s,
2916 char_u *s,
2917 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002919 // output postponed text
2920 msg_didout = TRUE; // remember that line is not empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 screen_puts_len(t_s, (int)(s - t_s), msg_row, msg_col, attr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002922 msg_col += *t_col;
2923 *t_col = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002924 // If the string starts with a composing character don't increment the
2925 // column position for it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 if (enc_utf8 && utf_iscomposing(utf_ptr2char(t_s)))
2927 --msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 if (msg_col >= Columns)
2929 {
2930 msg_col = 0;
2931 ++msg_row;
2932 }
2933}
2934
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935/*
2936 * Returns TRUE when messages should be printed with mch_errmsg().
2937 * This is used when there is no valid screen, so we can see error messages.
2938 * If termcap is not active, we may be writing in an alternate console
2939 * window, cursor positioning may not work correctly (window size may be
2940 * different, e.g. for Win32 console) or we just don't know where the
2941 * cursor is.
2942 */
2943 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002944msg_use_printf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945{
2946 return (!msg_check_screen()
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002947#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
2948# ifdef VIMDLL
2949 || (!gui.in_use && !termcap_active)
2950# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 || !termcap_active
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002952# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953#endif
2954 || (swapping_screen() && !termcap_active)
2955 );
2956}
2957
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002958/*
2959 * Print a message when there is no valid screen.
2960 */
2961 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002962msg_puts_printf(char_u *str, int maxlen)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002963{
2964 char_u *s = str;
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002965 char_u *buf = NULL;
2966 char_u *p = s;
2967
Bram Moolenaar4f974752019-02-17 17:44:42 +01002968#ifdef MSWIN
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002969 if (!(silent_mode && p_verbose == 0))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002970 mch_settmode(TMODE_COOK); // handle CR and NL correctly
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002971#endif
Bram Moolenaar2321ca22016-09-09 14:17:18 +02002972 while ((maxlen < 0 || (int)(s - str) < maxlen) && *s != NUL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002973 {
2974 if (!(silent_mode && p_verbose == 0))
2975 {
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002976 // NL --> CR NL translation (for Unix, not for "--version")
2977 if (*s == NL)
2978 {
2979 int n = (int)(s - p);
2980
2981 buf = alloc(n + 3);
Bram Moolenaar6f10c702019-08-20 22:58:37 +02002982 if (buf != NULL)
2983 {
2984 memcpy(buf, p, n);
2985 if (!info_message)
2986 buf[n++] = CAR;
2987 buf[n++] = NL;
2988 buf[n++] = NUL;
2989 if (info_message) // informative message, not an error
2990 mch_msg((char *)buf);
2991 else
2992 mch_errmsg((char *)buf);
2993 vim_free(buf);
2994 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002995 p = s + 1;
2996 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002997 }
2998
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002999 // primitive way to compute the current column
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003000#ifdef FEAT_RIGHTLEFT
3001 if (cmdmsg_rl)
3002 {
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003003 if (*s == CAR || *s == NL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003004 msg_col = Columns - 1;
3005 else
3006 --msg_col;
3007 }
3008 else
3009#endif
3010 {
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003011 if (*s == CAR || *s == NL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003012 msg_col = 0;
3013 else
3014 ++msg_col;
3015 }
3016 ++s;
3017 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003018
3019 if (*p != NUL && !(silent_mode && p_verbose == 0))
3020 {
Bram Moolenaara97c3632021-06-15 22:39:11 +02003021 char_u *tofree = NULL;
Bram Moolenaar97c2c052019-02-22 13:42:07 +01003022
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003023 if (maxlen > 0 && STRLEN(p) > (size_t)maxlen)
Bram Moolenaar97c2c052019-02-22 13:42:07 +01003024 {
Bram Moolenaara97c3632021-06-15 22:39:11 +02003025 tofree = vim_strnsave(p, (size_t)maxlen);
3026 p = tofree;
Bram Moolenaar97c2c052019-02-22 13:42:07 +01003027 }
Bram Moolenaara97c3632021-06-15 22:39:11 +02003028 if (p != NULL)
3029 {
3030 if (info_message)
3031 mch_msg((char *)p);
3032 else
3033 mch_errmsg((char *)p);
3034 vim_free(tofree);
3035 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003036 }
3037
3038 msg_didout = TRUE; // assume that line is not empty
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003039
Bram Moolenaar4f974752019-02-17 17:44:42 +01003040#ifdef MSWIN
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003041 if (!(silent_mode && p_verbose == 0))
3042 mch_settmode(TMODE_RAW);
3043#endif
3044}
3045
3046/*
3047 * Show the more-prompt and handle the user response.
3048 * This takes care of scrolling back and displaying previously displayed text.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003049 * When at hit-enter prompt "typed_char" is the already typed character,
3050 * otherwise it's NUL.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003051 * Returns TRUE when jumping ahead to "confirm_msg_tail".
3052 */
3053 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003054do_more_prompt(int typed_char)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003055{
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01003056 static int entered = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003057 int used_typed_char = typed_char;
3058 int oldState = State;
3059 int c;
3060#ifdef FEAT_CON_DIALOG
3061 int retval = FALSE;
3062#endif
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003063 int toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003064 msgchunk_T *mp_last = NULL;
3065 msgchunk_T *mp;
3066 int i;
3067
Bram Moolenaar85a20022019-12-21 18:25:54 +01003068 // We get called recursively when a timer callback outputs a message. In
3069 // that case don't show another prompt. Also when at the hit-Enter prompt
3070 // and nothing was typed.
Bram Moolenaar24959102022-05-07 20:01:16 +01003071 if (entered || (State == MODE_HITRETURN && typed_char == 0))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01003072 return FALSE;
3073 entered = TRUE;
3074
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003075 if (typed_char == 'G')
3076 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003077 // "g<": Find first line on the last page.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003078 mp_last = msg_sb_start(last_msgchunk);
3079 for (i = 0; i < Rows - 2 && mp_last != NULL
3080 && mp_last->sb_prev != NULL; ++i)
3081 mp_last = msg_sb_start(mp_last->sb_prev);
3082 }
3083
Bram Moolenaar24959102022-05-07 20:01:16 +01003084 State = MODE_ASKMORE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003085 setmouse();
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003086 if (typed_char == NUL)
3087 msg_moremsg(FALSE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003088 for (;;)
3089 {
3090 /*
3091 * Get a typed character directly from the user.
3092 */
3093 if (used_typed_char != NUL)
3094 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003095 c = used_typed_char; // was typed at hit-enter prompt
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003096 used_typed_char = NUL;
3097 }
3098 else
3099 c = get_keystroke();
3100
3101#if defined(FEAT_MENU) && defined(FEAT_GUI)
3102 if (c == K_MENU)
3103 {
Bram Moolenaar24959102022-05-07 20:01:16 +01003104 int idx = get_menu_index(current_menu, MODE_ASKMORE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003105
Bram Moolenaar85a20022019-12-21 18:25:54 +01003106 // Used a menu. If it starts with CTRL-Y, it must
3107 // be a "Copy" for the clipboard. Otherwise
3108 // assume that we end
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003109 if (idx == MENU_INDEX_INVALID)
3110 continue;
3111 c = *current_menu->strings[idx];
3112 if (c != NUL && current_menu->strings[idx][1] != NUL)
3113 ins_typebuf(current_menu->strings[idx] + 1,
3114 current_menu->noremap[idx], 0, TRUE,
3115 current_menu->silent[idx]);
3116 }
3117#endif
3118
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003119 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003120 switch (c)
3121 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003122 case BS: // scroll one line back
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003123 case K_BS:
3124 case 'k':
3125 case K_UP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003126 toscroll = -1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003127 break;
3128
Bram Moolenaar85a20022019-12-21 18:25:54 +01003129 case CAR: // one extra line
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003130 case NL:
3131 case 'j':
3132 case K_DOWN:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003133 toscroll = 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003134 break;
3135
Bram Moolenaar85a20022019-12-21 18:25:54 +01003136 case 'u': // Up half a page
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003137 toscroll = -(Rows / 2);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003138 break;
3139
Bram Moolenaar85a20022019-12-21 18:25:54 +01003140 case 'd': // Down half a page
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003141 toscroll = Rows / 2;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003142 break;
3143
Bram Moolenaar85a20022019-12-21 18:25:54 +01003144 case 'b': // one page back
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00003145 case K_PAGEUP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003146 toscroll = -(Rows - 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003147 break;
3148
Bram Moolenaar85a20022019-12-21 18:25:54 +01003149 case ' ': // one extra page
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00003150 case 'f':
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003151 case K_PAGEDOWN:
3152 case K_LEFTMOUSE:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003153 toscroll = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003154 break;
3155
Bram Moolenaar85a20022019-12-21 18:25:54 +01003156 case 'g': // all the way back to the start
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003157 toscroll = -999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003158 break;
3159
Bram Moolenaar85a20022019-12-21 18:25:54 +01003160 case 'G': // all the way to the end
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003161 toscroll = 999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003162 lines_left = 999999;
3163 break;
3164
Bram Moolenaar85a20022019-12-21 18:25:54 +01003165 case ':': // start new command line
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003166#ifdef FEAT_CON_DIALOG
3167 if (!confirm_msg_used)
3168#endif
3169 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003170 // Since got_int is set all typeahead will be flushed, but we
3171 // want to keep this ':', remember that in a special way.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003172 typeahead_noflush(':');
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02003173#ifdef FEAT_TERMINAL
3174 skip_term_loop = TRUE;
3175#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01003176 cmdline_row = Rows - 1; // put ':' on this line
3177 skip_redraw = TRUE; // skip redraw once
3178 need_wait_return = FALSE; // don't wait in main()
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003179 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003180 // FALLTHROUGH
3181 case 'q': // quit
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003182 case Ctrl_C:
3183 case ESC:
3184#ifdef FEAT_CON_DIALOG
3185 if (confirm_msg_used)
3186 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003187 // Jump to the choices of the dialog.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003188 retval = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003189 }
3190 else
3191#endif
3192 {
3193 got_int = TRUE;
3194 quit_more = TRUE;
3195 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003196 // When there is some more output (wrapping line) display that
3197 // without another prompt.
Bram Moolenaar51306d22009-02-24 03:38:04 +00003198 lines_left = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003199 break;
3200
3201#ifdef FEAT_CLIPBOARD
3202 case Ctrl_Y:
Bram Moolenaar85a20022019-12-21 18:25:54 +01003203 // Strange way to allow copying (yanking) a modeless
3204 // selection at the more prompt. Use CTRL-Y,
3205 // because the same is used in Cmdline-mode and at the
3206 // hit-enter prompt. However, scrolling one line up
3207 // might be expected...
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003208 if (clip_star.state == SELECT_DONE)
3209 clip_copy_modeless_selection(TRUE);
3210 continue;
3211#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01003212 default: // no valid response
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003213 msg_moremsg(TRUE);
3214 continue;
3215 }
3216
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003217 if (toscroll != 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003218 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003219 if (toscroll < 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003220 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003221 // go to start of last line
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003222 if (mp_last == NULL)
3223 mp = msg_sb_start(last_msgchunk);
3224 else if (mp_last->sb_prev != NULL)
3225 mp = msg_sb_start(mp_last->sb_prev);
3226 else
3227 mp = NULL;
3228
Bram Moolenaar85a20022019-12-21 18:25:54 +01003229 // go to start of line at top of the screen
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003230 for (i = 0; i < Rows - 2 && mp != NULL && mp->sb_prev != NULL;
3231 ++i)
3232 mp = msg_sb_start(mp->sb_prev);
3233
3234 if (mp != NULL && mp->sb_prev != NULL)
3235 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003236 // Find line to be displayed at top.
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003237 for (i = 0; i > toscroll; --i)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003238 {
3239 if (mp == NULL || mp->sb_prev == NULL)
3240 break;
3241 mp = msg_sb_start(mp->sb_prev);
3242 if (mp_last == NULL)
3243 mp_last = msg_sb_start(last_msgchunk);
3244 else
3245 mp_last = msg_sb_start(mp_last->sb_prev);
3246 }
3247
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003248 if (toscroll == -1 && screen_ins_lines(0, 0, 1,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003249 (int)Rows, 0, NULL) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003250 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003251 // display line at top
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003252 (void)disp_sb_line(0, mp);
3253 }
3254 else
3255 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003256 // redisplay all lines
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003257 screenclear();
Bram Moolenaar773560b2006-05-06 21:38:18 +00003258 for (i = 0; mp != NULL && i < Rows - 1; ++i)
3259 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003260 mp = disp_sb_line(i, mp);
Bram Moolenaar773560b2006-05-06 21:38:18 +00003261 ++msg_scrolled;
3262 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003263 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003264 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003265 }
3266 }
3267 else
3268 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003269 // First display any text that we scrolled back.
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003270 while (toscroll > 0 && mp_last != NULL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003271 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003272 // scroll up, display line at bottom
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003273 msg_scroll_up();
Bram Moolenaarbb15b652005-10-03 21:52:09 +00003274 inc_msg_scrolled();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003275 screen_fill((int)Rows - 2, (int)Rows - 1, 0,
3276 (int)Columns, ' ', ' ', 0);
3277 mp_last = disp_sb_line((int)Rows - 2, mp_last);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003278 --toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003279 }
3280 }
3281
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003282 if (toscroll <= 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003283 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003284 // displayed the requested text, more prompt again
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003285 screen_fill((int)Rows - 1, (int)Rows, 0,
3286 (int)Columns, ' ', ' ', 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003287 msg_moremsg(FALSE);
3288 continue;
3289 }
3290
Bram Moolenaar85a20022019-12-21 18:25:54 +01003291 // display more text, return to caller
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003292 lines_left = toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003293 }
3294
3295 break;
3296 }
3297
Bram Moolenaar85a20022019-12-21 18:25:54 +01003298 // clear the --more-- message
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003299 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3300 State = oldState;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003301 setmouse();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003302 if (quit_more)
3303 {
3304 msg_row = Rows - 1;
3305 msg_col = 0;
3306 }
3307#ifdef FEAT_RIGHTLEFT
3308 else if (cmdmsg_rl)
3309 msg_col = Columns - 1;
3310#endif
3311
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01003312 entered = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003313#ifdef FEAT_CON_DIALOG
3314 return retval;
3315#else
3316 return FALSE;
3317#endif
3318}
3319
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320#if defined(USE_MCH_ERRMSG) || defined(PROTO)
3321
3322#ifdef mch_errmsg
3323# undef mch_errmsg
3324#endif
3325#ifdef mch_msg
3326# undef mch_msg
3327#endif
3328
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003329#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3330 static void
3331mch_errmsg_c(char *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332{
Bram Moolenaar0f77d6a2019-02-14 20:55:09 +01003333 int len = (int)STRLEN(str);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003334 DWORD nwrite = 0;
3335 DWORD mode = 0;
3336 HANDLE h = GetStdHandle(STD_ERROR_HANDLE);
3337
3338 if (GetConsoleMode(h, &mode) && enc_codepage >= 0
3339 && (int)GetConsoleCP() != enc_codepage)
3340 {
3341 WCHAR *w = enc_to_utf16((char_u *)str, &len);
3342
3343 WriteConsoleW(h, w, len, &nwrite, NULL);
3344 vim_free(w);
3345 }
3346 else
3347 {
3348 fprintf(stderr, "%s", str);
3349 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003350}
3351#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003353/*
3354 * Give an error message. To be used when the screen hasn't been initialized
3355 * yet. When stderr can't be used, collect error messages until the GUI has
3356 * started and they can be displayed in a message box.
3357 */
3358 void
3359mch_errmsg(char *str)
3360{
3361#if !defined(MSWIN) || defined(FEAT_GUI_MSWIN)
3362 int len;
3363#endif
3364
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02003365#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003366 // On Unix use stderr if it's a tty.
3367 // When not going to start the GUI also use stderr.
3368 // On Mac, when started from Finder, stderr is the console.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 if (
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003370# ifdef UNIX
3371# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003373# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 isatty(2)
3375# endif
3376# ifdef FEAT_GUI
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003377 ||
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003378# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003379# endif
3380# ifdef FEAT_GUI
3381 !(gui.in_use || gui.starting)
3382# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 )
3384 {
3385 fprintf(stderr, "%s", str);
3386 return;
3387 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003388#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003390#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3391# ifdef VIMDLL
3392 if (!(gui.in_use || gui.starting))
3393# endif
3394 {
3395 mch_errmsg_c(str);
3396 return;
3397 }
3398#endif
3399
3400#if !defined(MSWIN) || defined(FEAT_GUI_MSWIN)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003401 // avoid a delay for a message that isn't there
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 emsg_on_display = FALSE;
3403
3404 len = (int)STRLEN(str) + 1;
3405 if (error_ga.ga_growsize == 0)
3406 {
3407 error_ga.ga_growsize = 80;
3408 error_ga.ga_itemsize = 1;
3409 }
3410 if (ga_grow(&error_ga, len) == OK)
3411 {
3412 mch_memmove((char_u *)error_ga.ga_data + error_ga.ga_len,
3413 (char_u *)str, len);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003414# ifdef UNIX
Bram Moolenaar85a20022019-12-21 18:25:54 +01003415 // remove CR characters, they are displayed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 {
3417 char_u *p;
3418
3419 p = (char_u *)error_ga.ga_data + error_ga.ga_len;
3420 for (;;)
3421 {
3422 p = vim_strchr(p, '\r');
3423 if (p == NULL)
3424 break;
3425 *p = ' ';
3426 }
3427 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003428# endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01003429 --len; // don't count the NUL at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 error_ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003432#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433}
3434
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003435#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3436 static void
3437mch_msg_c(char *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438{
Bram Moolenaar0f77d6a2019-02-14 20:55:09 +01003439 int len = (int)STRLEN(str);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003440 DWORD nwrite = 0;
3441 DWORD mode;
3442 HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
3443
3444
3445 if (GetConsoleMode(h, &mode) && enc_codepage >= 0
3446 && (int)GetConsoleCP() != enc_codepage)
3447 {
3448 WCHAR *w = enc_to_utf16((char_u *)str, &len);
3449
3450 WriteConsoleW(h, w, len, &nwrite, NULL);
3451 vim_free(w);
3452 }
3453 else
3454 {
3455 printf("%s", str);
3456 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003457}
3458#endif
3459
3460/*
3461 * Give a message. To be used when the screen hasn't been initialized yet.
3462 * When there is no tty, collect messages until the GUI has started and they
3463 * can be displayed in a message box.
3464 */
3465 void
3466mch_msg(char *str)
3467{
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02003468#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003469 // On Unix use stdout if we have a tty. This allows "vim -h | more" and
3470 // uses mch_errmsg() when started from the desktop.
3471 // When not going to start the GUI also use stdout.
3472 // On Mac, when started from Finder, stderr is the console.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 if (
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003474# ifdef UNIX
3475# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003477# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 isatty(2)
Bram Moolenaareae1b912019-05-09 15:12:55 +02003479# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480# ifdef FEAT_GUI
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003481 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003483# endif
3484# ifdef FEAT_GUI
3485 !(gui.in_use || gui.starting)
3486# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 )
3488 {
3489 printf("%s", str);
3490 return;
3491 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003492#endif
3493
3494#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3495# ifdef VIMDLL
3496 if (!(gui.in_use || gui.starting))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003498 {
3499 mch_msg_c(str);
3500 return;
3501 }
3502#endif
3503#if !defined(MSWIN) || defined(FEAT_GUI_MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 mch_errmsg(str);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003505#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506}
Bram Moolenaar85a20022019-12-21 18:25:54 +01003507#endif // USE_MCH_ERRMSG
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508
3509/*
3510 * Put a character on the screen at the current message position and advance
3511 * to the next position. Only for printable ASCII!
3512 */
3513 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003514msg_screen_putchar(int c, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515{
Bram Moolenaar85a20022019-12-21 18:25:54 +01003516 msg_didout = TRUE; // remember that line is not empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 screen_putchar(c, msg_row, msg_col, attr);
3518#ifdef FEAT_RIGHTLEFT
3519 if (cmdmsg_rl)
3520 {
3521 if (--msg_col == 0)
3522 {
3523 msg_col = Columns;
3524 ++msg_row;
3525 }
3526 }
3527 else
3528#endif
3529 {
3530 if (++msg_col >= Columns)
3531 {
3532 msg_col = 0;
3533 ++msg_row;
3534 }
3535 }
3536}
3537
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003538 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003539msg_moremsg(int full)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003541 int attr;
3542 char_u *s = (char_u *)_("-- More --");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543
Bram Moolenaar8820b482017-03-16 17:23:31 +01003544 attr = HL_ATTR(HLF_M);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003545 screen_puts(s, (int)Rows - 1, 0, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 if (full)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003547 screen_puts((char_u *)
3548 _(" SPACE/d/j: screen/page/line down, b/u/k: up, q: quit "),
3549 (int)Rows - 1, vim_strsize(s), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550}
3551
3552/*
Bram Moolenaar24959102022-05-07 20:01:16 +01003553 * Repeat the message for the current mode: MODE_ASKMORE, MODE_EXTERNCMD,
3554 * MODE_CONFIRM or exmode_active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 */
3556 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003557repeat_message(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558{
Bram Moolenaar24959102022-05-07 20:01:16 +01003559 if (State == MODE_ASKMORE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003561 msg_moremsg(TRUE); // display --more-- message again
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 msg_row = Rows - 1;
3563 }
3564#ifdef FEAT_CON_DIALOG
Bram Moolenaar24959102022-05-07 20:01:16 +01003565 else if (State == MODE_CONFIRM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003567 display_confirm_msg(); // display ":confirm" message again
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 msg_row = Rows - 1;
3569 }
3570#endif
Bram Moolenaar24959102022-05-07 20:01:16 +01003571 else if (State == MODE_EXTERNCMD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003573 windgoto(msg_row, msg_col); // put cursor back
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 }
Bram Moolenaar24959102022-05-07 20:01:16 +01003575 else if (State == MODE_HITRETURN || State == MODE_SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 {
Bram Moolenaar9f108752007-11-24 14:44:58 +00003577 if (msg_row == Rows - 1)
3578 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003579 // Avoid drawing the "hit-enter" prompt below the previous one,
3580 // overwrite it. Esp. useful when regaining focus and a
3581 // FocusGained autocmd exists but didn't draw anything.
Bram Moolenaar9f108752007-11-24 14:44:58 +00003582 msg_didout = FALSE;
3583 msg_col = 0;
3584 msg_clr_eos();
3585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 hit_return_msg();
3587 msg_row = Rows - 1;
3588 }
3589}
3590
3591/*
3592 * msg_check_screen - check if the screen is initialized.
3593 * Also check msg_row and msg_col, if they are too big it may cause a crash.
3594 * While starting the GUI the terminal codes will be set for the GUI, but the
3595 * output goes to the terminal. Don't use the terminal codes then.
3596 */
3597 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003598msg_check_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599{
3600 if (!full_screen || !screen_valid(FALSE))
3601 return FALSE;
3602
3603 if (msg_row >= Rows)
3604 msg_row = Rows - 1;
3605 if (msg_col >= Columns)
3606 msg_col = Columns - 1;
3607 return TRUE;
3608}
3609
3610/*
3611 * Clear from current message position to end of screen.
3612 * Skip this when ":silent" was used, no need to clear for redirection.
3613 */
3614 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003615msg_clr_eos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616{
3617 if (msg_silent == 0)
3618 msg_clr_eos_force();
3619}
3620
3621/*
3622 * Clear from current message position to end of screen.
3623 * Note: msg_col is not updated, so we remember the end of the message
3624 * for msg_check().
3625 */
3626 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003627msg_clr_eos_force(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628{
3629 if (msg_use_printf())
3630 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003631 if (full_screen) // only when termcap codes are valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 {
3633 if (*T_CD)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003634 out_str(T_CD); // clear to end of display
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 else if (*T_CE)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003636 out_str(T_CE); // clear to end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 }
3638 }
Shougo Matsushitaf39cfb72022-07-30 16:54:05 +01003639 else if (p_ch > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 {
3641#ifdef FEAT_RIGHTLEFT
3642 if (cmdmsg_rl)
3643 {
3644 screen_fill(msg_row, msg_row + 1, 0, msg_col + 1, ' ', ' ', 0);
3645 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3646 }
3647 else
3648#endif
3649 {
3650 screen_fill(msg_row, msg_row + 1, msg_col, (int)Columns,
3651 ' ', ' ', 0);
3652 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3653 }
3654 }
3655}
3656
3657/*
3658 * Clear the command line.
3659 */
3660 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003661msg_clr_cmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662{
3663 msg_row = cmdline_row;
3664 msg_col = 0;
3665 msg_clr_eos_force();
3666}
3667
3668/*
3669 * end putting a message on the screen
3670 * call wait_return if the message does not fit in the available space
3671 * return TRUE if wait_return not called.
3672 */
3673 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003674msg_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675{
3676 /*
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02003677 * If the string is larger than the window,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 * or the ruler option is set and we run into it,
3679 * we have to redraw the window.
3680 * Do not do this if we are abandoning the file or editing the command line.
3681 */
Bram Moolenaar24959102022-05-07 20:01:16 +01003682 if (!exiting && need_wait_return && !(State & MODE_CMDLINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 {
3684 wait_return(FALSE);
3685 return FALSE;
3686 }
3687 out_flush();
3688 return TRUE;
3689}
3690
3691/*
3692 * If the written message runs into the shown command or ruler, we have to
3693 * wait for hit-return and redraw the window later.
3694 */
3695 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003696msg_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697{
3698 if (msg_row == Rows - 1 && msg_col >= sc_col)
3699 {
3700 need_wait_return = TRUE;
3701 redraw_cmdline = TRUE;
3702 }
3703}
3704
3705/*
3706 * May write a string to the redirection file.
3707 * When "maxlen" is -1 write the whole string, otherwise up to "maxlen" bytes.
3708 */
3709 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003710redir_write(char_u *str, int maxlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711{
3712 char_u *s = str;
3713 static int cur_col = 0;
3714
Bram Moolenaar85a20022019-12-21 18:25:54 +01003715 // Don't do anything for displaying prompts and the like.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003716 if (redir_off)
3717 return;
3718
Bram Moolenaar85a20022019-12-21 18:25:54 +01003719 // If 'verbosefile' is set prepare for writing in that file.
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003720 if (*p_vfile != NUL && verbose_fd == NULL)
3721 verbose_open();
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003722
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003723 if (redirecting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003725 // If the string doesn't start with CR or NL, go to msg_col
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 if (*s != '\n' && *s != '\r')
3727 {
3728 while (cur_col < msg_col)
3729 {
3730#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003731 if (redir_execute)
3732 execute_redir_str((char_u *)" ", -1);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003733 else if (redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 write_reg_contents(redir_reg, (char_u *)" ", -1, TRUE);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003735 else if (redir_vname)
3736 var_redir_str((char_u *)" ", -1);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003737 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003739 if (redir_fd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 fputs(" ", redir_fd);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003741 if (verbose_fd != NULL)
3742 fputs(" ", verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 ++cur_col;
3744 }
3745 }
3746
3747#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003748 if (redir_execute)
3749 execute_redir_str(s, maxlen);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003750 else if (redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 write_reg_contents(redir_reg, s, maxlen, TRUE);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003752 else if (redir_vname)
Bram Moolenaardf177f62005-02-22 08:39:57 +00003753 var_redir_str(s, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754#endif
3755
Bram Moolenaar85a20022019-12-21 18:25:54 +01003756 // Write and adjust the current column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
3758 {
3759#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003760 if (!redir_reg && !redir_vname && !redir_execute)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003762 if (redir_fd != NULL)
3763 putc(*s, redir_fd);
3764 if (verbose_fd != NULL)
3765 putc(*s, verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 if (*s == '\r' || *s == '\n')
3767 cur_col = 0;
3768 else if (*s == '\t')
3769 cur_col += (8 - cur_col % 8);
3770 else
3771 ++cur_col;
3772 ++s;
3773 }
3774
Bram Moolenaar85a20022019-12-21 18:25:54 +01003775 if (msg_silent != 0) // should update msg_col
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 msg_col = cur_col;
3777 }
3778}
3779
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003780 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003781redirecting(void)
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003782{
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003783 return redir_fd != NULL || *p_vfile != NUL
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003784#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003785 || redir_reg || redir_vname || redir_execute
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003786#endif
3787 ;
3788}
3789
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790/*
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00003791 * Before giving verbose message.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003792 * Must always be called paired with verbose_leave()!
3793 */
3794 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003795verbose_enter(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003796{
3797 if (*p_vfile != NUL)
3798 ++msg_silent;
3799}
3800
3801/*
3802 * After giving verbose message.
3803 * Must always be called paired with verbose_enter()!
3804 */
3805 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003806verbose_leave(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003807{
3808 if (*p_vfile != NUL)
3809 if (--msg_silent < 0)
3810 msg_silent = 0;
3811}
3812
3813/*
3814 * Like verbose_enter() and set msg_scroll when displaying the message.
3815 */
3816 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003817verbose_enter_scroll(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003818{
3819 if (*p_vfile != NUL)
3820 ++msg_silent;
3821 else
Bram Moolenaar85a20022019-12-21 18:25:54 +01003822 // always scroll up, don't overwrite
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003823 msg_scroll = TRUE;
3824}
3825
3826/*
3827 * Like verbose_leave() and set cmdline_row when displaying the message.
3828 */
3829 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003830verbose_leave_scroll(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003831{
3832 if (*p_vfile != NUL)
3833 {
3834 if (--msg_silent < 0)
3835 msg_silent = 0;
3836 }
3837 else
3838 cmdline_row = msg_row;
3839}
3840
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003841/*
3842 * Called when 'verbosefile' is set: stop writing to the file.
3843 */
3844 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003845verbose_stop(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003846{
3847 if (verbose_fd != NULL)
3848 {
3849 fclose(verbose_fd);
3850 verbose_fd = NULL;
3851 }
3852 verbose_did_open = FALSE;
3853}
3854
3855/*
3856 * Open the file 'verbosefile'.
3857 * Return FAIL or OK.
3858 */
3859 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003860verbose_open(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003861{
3862 if (verbose_fd == NULL && !verbose_did_open)
3863 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003864 // Only give the error message once.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003865 verbose_did_open = TRUE;
3866
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003867 verbose_fd = mch_fopen((char *)p_vfile, "a");
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003868 if (verbose_fd == NULL)
3869 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003870 semsg(_(e_cant_open_file_str), p_vfile);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003871 return FAIL;
3872 }
3873 }
3874 return OK;
3875}
3876
3877/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 * Give a warning message (for searching).
3879 * Use 'w' highlighting and may repeat the message after redrawing
3880 */
3881 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003882give_warning(char_u *message, int hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883{
Bram Moolenaarcc7eb2a2021-07-11 19:12:04 +02003884 give_warning_with_source(message, hl, FALSE);
3885}
3886
3887 void
3888give_warning_with_source(char_u *message, int hl, int with_source)
3889{
Bram Moolenaar85a20022019-12-21 18:25:54 +01003890 // Don't do this for ":silent".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 if (msg_silent != 0)
3892 return;
3893
Bram Moolenaar85a20022019-12-21 18:25:54 +01003894 // Don't want a hit-enter prompt here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 ++no_wait_return;
Bram Moolenaared203462004-06-16 11:19:22 +00003896
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897#ifdef FEAT_EVAL
3898 set_vim_var_string(VV_WARNINGMSG, message, -1);
3899#endif
Bram Moolenaard23a8232018-02-10 18:45:26 +01003900 VIM_CLEAR(keep_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 if (hl)
Bram Moolenaar8820b482017-03-16 17:23:31 +01003902 keep_msg_attr = HL_ATTR(HLF_W);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 else
3904 keep_msg_attr = 0;
Bram Moolenaarcc7eb2a2021-07-11 19:12:04 +02003905
3906 if (with_source)
3907 {
3908 // Do what msg() does, but with a column offset if the warning should
3909 // be after the mode message.
3910 msg_start();
3911 msg_source(HL_ATTR(HLF_W));
3912 msg_puts(" ");
3913 msg_puts_attr((char *)message, HL_ATTR(HLF_W) | MSG_HIST);
3914 msg_clr_eos();
3915 (void)msg_end();
3916 }
3917 else if (msg_attr((char *)message, keep_msg_attr) && msg_scrolled == 0)
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003918 set_keep_msg(message, keep_msg_attr);
Bram Moolenaarcc7eb2a2021-07-11 19:12:04 +02003919
Bram Moolenaar85a20022019-12-21 18:25:54 +01003920 msg_didout = FALSE; // overwrite this message
3921 msg_nowait = TRUE; // don't wait for this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 msg_col = 0;
Bram Moolenaared203462004-06-16 11:19:22 +00003923
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 --no_wait_return;
3925}
3926
Bram Moolenaar113e1072019-01-20 15:30:40 +01003927#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarf8be4612017-06-23 20:52:40 +02003928 void
3929give_warning2(char_u *message, char_u *a1, int hl)
3930{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +02003931 if (IObuff == NULL)
3932 {
3933 // Very early in initialisation and already something wrong, just give
3934 // the raw message so the user at least gets a hint.
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003935 give_warning(message, hl);
Bram Moolenaare3a22cb2019-10-14 22:01:57 +02003936 }
3937 else
3938 {
3939 vim_snprintf((char *)IObuff, IOSIZE, (char *)message, a1);
3940 give_warning(IObuff, hl);
3941 }
Bram Moolenaarf8be4612017-06-23 20:52:40 +02003942}
Bram Moolenaar113e1072019-01-20 15:30:40 +01003943#endif
Bram Moolenaarf8be4612017-06-23 20:52:40 +02003944
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945/*
3946 * Advance msg cursor to column "col".
3947 */
3948 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003949msg_advance(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950{
Bram Moolenaar85a20022019-12-21 18:25:54 +01003951 if (msg_silent != 0) // nothing to advance to
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003953 msg_col = col; // for redirection, may fill it up later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 return;
3955 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003956 if (col >= Columns) // not enough room
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 col = Columns - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003958#ifdef FEAT_RIGHTLEFT
3959 if (cmdmsg_rl)
3960 while (msg_col > Columns - col)
3961 msg_putchar(' ');
3962 else
3963#endif
3964 while (msg_col < col)
3965 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966}
3967
3968#if defined(FEAT_CON_DIALOG) || defined(PROTO)
3969/*
3970 * Used for "confirm()" function, and the :confirm command prefix.
3971 * Versions which haven't got flexible dialogs yet, and console
3972 * versions, get this generic handler which uses the command line.
3973 *
3974 * type = one of:
3975 * VIM_QUESTION, VIM_INFO, VIM_WARNING, VIM_ERROR or VIM_GENERIC
3976 * title = title string (can be NULL for default)
3977 * (neither used in console dialogs at the moment)
3978 *
3979 * Format of the "buttons" string:
3980 * "Button1Name\nButton2Name\nButton3Name"
3981 * The first button should normally be the default/accept
3982 * The second button should be the 'Cancel' button
3983 * Other buttons- use your imagination!
3984 * A '&' in a button name becomes a shortcut, so each '&' should be before a
3985 * different letter.
Rob Pilling8196e942022-02-11 15:12:10 +00003986 *
3987 * Returns 0 if cancelled, otherwise the nth button (1-indexed).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003990do_dialog(
3991 int type UNUSED,
3992 char_u *title UNUSED,
3993 char_u *message,
3994 char_u *buttons,
3995 int dfltbutton,
Bram Moolenaar85a20022019-12-21 18:25:54 +01003996 char_u *textfield UNUSED, // IObuff for inputdialog(), NULL
3997 // otherwise
3998 int ex_cmd) // when TRUE pressing : accepts default and starts
3999 // Ex command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000{
4001 int oldState;
4002 int retval = 0;
4003 char_u *hotkeys;
4004 int c;
4005 int i;
Bram Moolenaar27321db2020-07-06 21:24:57 +02004006 tmode_T save_tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007
4008#ifndef NO_CONSOLE
Bram Moolenaar85a20022019-12-21 18:25:54 +01004009 // Don't output anything in silent mode ("ex -s")
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 if (silent_mode)
Bram Moolenaar85a20022019-12-21 18:25:54 +01004011 return dfltbutton; // return default option
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012#endif
4013
4014#ifdef FEAT_GUI_DIALOG
Bram Moolenaar85a20022019-12-21 18:25:54 +01004015 // When GUI is running and 'c' not in 'guioptions', use the GUI dialog
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
4017 {
Bram Moolenaar2d12c252022-06-13 21:42:45 +01004018 // --gui-dialog-file: write text to a file
4019 if (gui_dialog_log(title, message))
4020 c = dfltbutton;
4021 else
4022 c = gui_mch_dialog(type, title, message, buttons, dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004023 textfield, ex_cmd);
Bram Moolenaar85a20022019-12-21 18:25:54 +01004024 // avoid a hit-enter prompt without clearing the cmdline
Bram Moolenaar8de49e12009-02-11 17:47:54 +00004025 need_wait_return = FALSE;
4026 emsg_on_display = FALSE;
4027 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028
Bram Moolenaar85a20022019-12-21 18:25:54 +01004029 // Flush output to avoid that further messages and redrawing is done
4030 // in the wrong order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 out_flush();
4032 gui_mch_update();
4033
4034 return c;
4035 }
4036#endif
4037
4038 oldState = State;
Bram Moolenaar24959102022-05-07 20:01:16 +01004039 State = MODE_CONFIRM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041
Bram Moolenaar27321db2020-07-06 21:24:57 +02004042 // Ensure raw mode here.
4043 save_tmode = cur_tmode;
4044 settmode(TMODE_RAW);
4045
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 /*
4047 * Since we wait for a keypress, don't make the
4048 * user press RETURN as well afterwards.
4049 */
4050 ++no_wait_return;
4051 hotkeys = msg_show_console_dialog(message, buttons, dfltbutton);
4052
4053 if (hotkeys != NULL)
4054 {
4055 for (;;)
4056 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004057 // Get a typed character directly from the user.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 c = get_keystroke();
4059 switch (c)
4060 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004061 case CAR: // User accepts default option
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 case NL:
4063 retval = dfltbutton;
4064 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01004065 case Ctrl_C: // User aborts/cancels
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 case ESC:
4067 retval = 0;
4068 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01004069 default: // Could be a hotkey?
4070 if (c < 0) // special keys are ignored here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 continue;
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004072 if (c == ':' && ex_cmd)
4073 {
4074 retval = dfltbutton;
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02004075 ins_char_typebuf(':', 0);
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004076 break;
4077 }
4078
Bram Moolenaar85a20022019-12-21 18:25:54 +01004079 // Make the character lowercase, as chars in "hotkeys" are.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 c = MB_TOLOWER(c);
4081 retval = 1;
4082 for (i = 0; hotkeys[i]; ++i)
4083 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 if (has_mbyte)
4085 {
4086 if ((*mb_ptr2char)(hotkeys + i) == c)
4087 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004088 i += (*mb_ptr2len)(hotkeys + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 }
4090 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 if (hotkeys[i] == c)
4092 break;
4093 ++retval;
4094 }
4095 if (hotkeys[i])
4096 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01004097 // No hotkey match, so keep waiting
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 continue;
4099 }
4100 break;
4101 }
4102
4103 vim_free(hotkeys);
4104 }
4105
Bram Moolenaar27321db2020-07-06 21:24:57 +02004106 settmode(save_tmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 State = oldState;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 --no_wait_return;
4110 msg_end_prompt();
4111
4112 return retval;
4113}
4114
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115/*
4116 * Copy one character from "*from" to "*to", taking care of multi-byte
4117 * characters. Return the length of the character in bytes.
4118 */
4119 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004120copy_char(
4121 char_u *from,
4122 char_u *to,
Bram Moolenaar85a20022019-12-21 18:25:54 +01004123 int lowercase) // make character lower case
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 int len;
4126 int c;
4127
4128 if (has_mbyte)
4129 {
4130 if (lowercase)
4131 {
4132 c = MB_TOLOWER((*mb_ptr2char)(from));
4133 return (*mb_char2bytes)(c, to);
4134 }
4135 else
4136 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004137 len = (*mb_ptr2len)(from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 mch_memmove(to, from, (size_t)len);
4139 return len;
4140 }
4141 }
4142 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 {
4144 if (lowercase)
4145 *to = (char_u)TOLOWER_LOC(*from);
4146 else
4147 *to = *from;
4148 return 1;
4149 }
4150}
4151
4152/*
4153 * Format the dialog string, and display it at the bottom of
4154 * the screen. Return a string of hotkey chars (if defined) for
4155 * each 'button'. If a button has no hotkey defined, the first character of
4156 * the button is used.
4157 * The hotkeys can be multi-byte characters, but without combining chars.
4158 *
4159 * Returns an allocated string with hotkeys, or NULL for error.
4160 */
4161 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004162msg_show_console_dialog(
4163 char_u *message,
4164 char_u *buttons,
4165 int dfltbutton)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166{
4167 int len = 0;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01004168#define HOTK_LEN (has_mbyte ? MB_MAXBYTES : 1)
Bram Moolenaar85a20022019-12-21 18:25:54 +01004169 int lenhotkey = HOTK_LEN; // count first button
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 char_u *hotk = NULL;
4171 char_u *msgp = NULL;
4172 char_u *hotkp = NULL;
4173 char_u *r;
4174 int copy;
4175#define HAS_HOTKEY_LEN 30
4176 char_u has_hotkey[HAS_HOTKEY_LEN];
Bram Moolenaar85a20022019-12-21 18:25:54 +01004177 int first_hotkey = FALSE; // first char of button is hotkey
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 int idx;
4179
4180 has_hotkey[0] = FALSE;
4181
4182 /*
4183 * First loop: compute the size of memory to allocate.
4184 * Second loop: copy to the allocated memory.
4185 */
4186 for (copy = 0; copy <= 1; ++copy)
4187 {
4188 r = buttons;
4189 idx = 0;
4190 while (*r)
4191 {
4192 if (*r == DLG_BUTTON_SEP)
4193 {
4194 if (copy)
4195 {
4196 *msgp++ = ',';
Bram Moolenaar85a20022019-12-21 18:25:54 +01004197 *msgp++ = ' '; // '\n' -> ', '
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198
Bram Moolenaar85a20022019-12-21 18:25:54 +01004199 // advance to next hotkey and set default hotkey
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 if (has_mbyte)
Bram Moolenaar6a516062007-07-05 08:11:42 +00004201 hotkp += STRLEN(hotkp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 ++hotkp;
Bram Moolenaar6a516062007-07-05 08:11:42 +00004204 hotkp[copy_char(r + 1, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 if (dfltbutton)
4206 --dfltbutton;
4207
Bram Moolenaar85a20022019-12-21 18:25:54 +01004208 // If no hotkey is specified first char is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 if (idx < HAS_HOTKEY_LEN - 1 && !has_hotkey[++idx])
4210 first_hotkey = TRUE;
4211 }
4212 else
4213 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004214 len += 3; // '\n' -> ', '; 'x' -> '(x)'
4215 lenhotkey += HOTK_LEN; // each button needs a hotkey
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 if (idx < HAS_HOTKEY_LEN - 1)
4217 has_hotkey[++idx] = FALSE;
4218 }
4219 }
4220 else if (*r == DLG_HOTKEY_CHAR || first_hotkey)
4221 {
4222 if (*r == DLG_HOTKEY_CHAR)
4223 ++r;
4224 first_hotkey = FALSE;
4225 if (copy)
4226 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004227 if (*r == DLG_HOTKEY_CHAR) // '&&a' -> '&a'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 *msgp++ = *r;
4229 else
4230 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004231 // '&a' -> '[a]'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 *msgp++ = (dfltbutton == 1) ? '[' : '(';
4233 msgp += copy_char(r, msgp, FALSE);
4234 *msgp++ = (dfltbutton == 1) ? ']' : ')';
4235
Bram Moolenaar85a20022019-12-21 18:25:54 +01004236 // redefine hotkey
Bram Moolenaar6a516062007-07-05 08:11:42 +00004237 hotkp[copy_char(r, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 }
4239 }
4240 else
4241 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004242 ++len; // '&a' -> '[a]'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 if (idx < HAS_HOTKEY_LEN - 1)
4244 has_hotkey[idx] = TRUE;
4245 }
4246 }
4247 else
4248 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004249 // everything else copy literally
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 if (copy)
4251 msgp += copy_char(r, msgp, FALSE);
4252 }
4253
Bram Moolenaar85a20022019-12-21 18:25:54 +01004254 // advance to the next character
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004255 MB_PTR_ADV(r);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 }
4257
4258 if (copy)
4259 {
4260 *msgp++ = ':';
4261 *msgp++ = ' ';
4262 *msgp = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 }
4264 else
4265 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004266 len += (int)(STRLEN(message)
Bram Moolenaar85a20022019-12-21 18:25:54 +01004267 + 2 // for the NL's
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004268 + STRLEN(buttons)
Bram Moolenaar85a20022019-12-21 18:25:54 +01004269 + 3); // for the ": " and NUL
4270 lenhotkey++; // for the NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271
Bram Moolenaar85a20022019-12-21 18:25:54 +01004272 // If no hotkey is specified first char is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 if (!has_hotkey[0])
4274 {
4275 first_hotkey = TRUE;
Bram Moolenaar85a20022019-12-21 18:25:54 +01004276 len += 2; // "x" -> "[x]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 }
4278
4279 /*
4280 * Now allocate and load the strings
4281 */
4282 vim_free(confirm_msg);
4283 confirm_msg = alloc(len);
4284 if (confirm_msg == NULL)
4285 return NULL;
4286 *confirm_msg = NUL;
4287 hotk = alloc(lenhotkey);
4288 if (hotk == NULL)
4289 return NULL;
4290
4291 *confirm_msg = '\n';
4292 STRCPY(confirm_msg + 1, message);
4293
4294 msgp = confirm_msg + 1 + STRLEN(message);
4295 hotkp = hotk;
4296
Bram Moolenaar85a20022019-12-21 18:25:54 +01004297 // Define first default hotkey. Keep the hotkey string NUL
4298 // terminated to avoid reading past the end.
Bram Moolenaar6a516062007-07-05 08:11:42 +00004299 hotkp[copy_char(buttons, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300
Bram Moolenaar85a20022019-12-21 18:25:54 +01004301 // Remember where the choices start, displaying starts here when
4302 // "hotkp" typed at the more prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 confirm_msg_tail = msgp;
4304 *msgp++ = '\n';
4305 }
4306 }
4307
4308 display_confirm_msg();
4309 return hotk;
4310}
4311
4312/*
4313 * Display the ":confirm" message. Also called when screen resized.
4314 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02004315 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004316display_confirm_msg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317{
Bram Moolenaar85a20022019-12-21 18:25:54 +01004318 // avoid that 'q' at the more prompt truncates the message here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 ++confirm_msg_used;
4320 if (confirm_msg != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004321 msg_puts_attr((char *)confirm_msg, HL_ATTR(HLF_M));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 --confirm_msg_used;
4323}
4324
Bram Moolenaar85a20022019-12-21 18:25:54 +01004325#endif // FEAT_CON_DIALOG
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326
4327#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4328
4329 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004330vim_dialog_yesno(
4331 int type,
4332 char_u *title,
4333 char_u *message,
4334 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335{
4336 if (do_dialog(type,
4337 title == NULL ? (char_u *)_("Question") : title,
4338 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004339 (char_u *)_("&Yes\n&No"), dflt, NULL, FALSE) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 return VIM_YES;
4341 return VIM_NO;
4342}
4343
4344 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004345vim_dialog_yesnocancel(
4346 int type,
4347 char_u *title,
4348 char_u *message,
4349 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350{
4351 switch (do_dialog(type,
4352 title == NULL ? (char_u *)_("Question") : title,
4353 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004354 (char_u *)_("&Yes\n&No\n&Cancel"), dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 {
4356 case 1: return VIM_YES;
4357 case 2: return VIM_NO;
4358 }
4359 return VIM_CANCEL;
4360}
4361
4362 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004363vim_dialog_yesnoallcancel(
4364 int type,
4365 char_u *title,
4366 char_u *message,
4367 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368{
4369 switch (do_dialog(type,
4370 title == NULL ? (char_u *)"Question" : title,
4371 message,
4372 (char_u *)_("&Yes\n&No\nSave &All\n&Discard All\n&Cancel"),
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004373 dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374 {
4375 case 1: return VIM_YES;
4376 case 2: return VIM_NO;
4377 case 3: return VIM_ALL;
4378 case 4: return VIM_DISCARDALL;
4379 }
4380 return VIM_CANCEL;
4381}
4382
Bram Moolenaar85a20022019-12-21 18:25:54 +01004383#endif // FEAT_GUI_DIALOG || FEAT_CON_DIALOG