blob: 715c7beb5a1db3ba51543cb5f4c65083d3dcdd1e [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
534 ++no_wait_return;
535 p = get_emsg_source();
536 if (p != NULL)
537 {
Bram Moolenaar32526b32019-01-19 17:43:09 +0100538 msg_attr((char *)p, attr);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000539 vim_free(p);
540 }
541 p = get_emsg_lnum();
542 if (p != NULL)
543 {
Bram Moolenaar32526b32019-01-19 17:43:09 +0100544 msg_attr((char *)p, HL_ATTR(HLF_N));
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000545 vim_free(p);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100546 last_sourcing_lnum = SOURCING_LNUM; // only once for each line
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000547 }
548
Bram Moolenaar85a20022019-12-21 18:25:54 +0100549 // remember the last sourcing name printed, also when it's empty
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100550 if (SOURCING_NAME == NULL || other_sourcing_name())
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000551 {
Bram Moolenaarba8c9262021-11-25 14:43:18 +0000552 VIM_CLEAR(last_sourcing_name);
553 if (SOURCING_NAME != NULL)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100554 last_sourcing_name = vim_strsave(SOURCING_NAME);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000555 }
556 --no_wait_return;
Bram Moolenaarba8c9262021-11-25 14:43:18 +0000557
558 recursive = FALSE;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000559}
560
561/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000562 * Return TRUE if not giving error messages right now:
563 * If "emsg_off" is set: no error messages at the moment.
564 * If "msg" is in 'debug': do error message but without side effects.
565 * If "emsg_skip" is set: never do error messages.
566 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +0200567 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100568emsg_not_now(void)
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000569{
570 if ((emsg_off > 0 && vim_strchr(p_debug, 'm') == NULL
571 && vim_strchr(p_debug, 't') == NULL)
572#ifdef FEAT_EVAL
573 || emsg_skip > 0
574#endif
575 )
576 return TRUE;
577 return FALSE;
578}
579
Bram Moolenaar5a66dfb2017-03-01 20:40:39 +0100580#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100581static garray_T ignore_error_list = GA_EMPTY;
582
583 void
584ignore_error_for_testing(char_u *error)
585{
586 if (ignore_error_list.ga_itemsize == 0)
587 ga_init2(&ignore_error_list, sizeof(char_u *), 1);
588
Bram Moolenaar461a7fc2018-12-22 13:28:07 +0100589 if (STRCMP("RESET", error) == 0)
590 ga_clear_strings(&ignore_error_list);
591 else
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000592 ga_copy_string(&ignore_error_list, error);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100593}
594
595 static int
596ignore_error(char_u *msg)
597{
598 int i;
599
600 for (i = 0; i < ignore_error_list.ga_len; ++i)
601 if (strstr((char *)msg,
602 (char *)((char_u **)(ignore_error_list.ga_data))[i]) != NULL)
603 return TRUE;
604 return FALSE;
605}
606#endif
607
Bram Moolenaarb869c0d2016-07-20 00:10:51 +0200608#if !defined(HAVE_STRERROR) || defined(PROTO)
609/*
610 * Replacement for perror() that behaves more or less like emsg() was called.
Bram Moolenaar53989552019-12-23 22:59:18 +0100611 * v:errmsg will be set and called_emsg will be incremented.
Bram Moolenaarb869c0d2016-07-20 00:10:51 +0200612 */
613 void
614do_perror(char *msg)
615{
616 perror(msg);
617 ++emsg_silent;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100618 emsg(msg);
Bram Moolenaarb869c0d2016-07-20 00:10:51 +0200619 --emsg_silent;
620}
621#endif
622
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000623/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100624 * emsg_core() - display an error message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625 *
626 * Rings the bell, if appropriate, and calls message() to do the real work
627 * When terminal not initialized (yet) mch_errmsg(..) is used.
628 *
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100629 * Return TRUE if wait_return not called.
630 * Note: caller must check 'emsg_not_now()' before calling this.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100632 static int
633emsg_core(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634{
635 int attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636 char_u *p;
Bram Moolenaar958dc692016-12-01 15:34:12 +0100637 int r;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638#ifdef FEAT_EVAL
639 int ignore = FALSE;
640 int severe;
641#endif
642
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100643#ifdef FEAT_EVAL
Bram Moolenaar85a20022019-12-21 18:25:54 +0100644 // When testing some errors are turned into a normal message.
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100645 if (ignore_error(s))
Bram Moolenaar85a20022019-12-21 18:25:54 +0100646 // don't call msg() if it results in a dialog
Bram Moolenaar32526b32019-01-19 17:43:09 +0100647 return msg_use_printf() ? FALSE : msg((char *)s);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100648#endif
649
Bram Moolenaar53989552019-12-23 22:59:18 +0100650 ++called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652#ifdef FEAT_EVAL
Bram Moolenaar85a20022019-12-21 18:25:54 +0100653 // If "emsg_severe" is TRUE: When an error exception is to be thrown,
654 // prefer this message over previous messages for the same command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655 severe = emsg_severe;
656 emsg_severe = FALSE;
657#endif
658
Bram Moolenaar57657d82006-04-21 22:12:41 +0000659 if (!emsg_off || vim_strchr(p_debug, 't') != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660 {
661#ifdef FEAT_EVAL
662 /*
663 * Cause a throw of an error exception if appropriate. Don't display
664 * the error message in this case. (If no matching catch clause will
665 * be found, the message will be displayed later on.) "ignore" is set
666 * when the message should be ignored completely (used for the
667 * interrupt message).
668 */
669 if (cause_errthrow(s, severe, &ignore) == TRUE)
670 {
671 if (!ignore)
Bram Moolenaar76a63452018-11-28 20:38:37 +0100672 ++did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 return TRUE;
674 }
675
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100676 if (in_assert_fails && emsg_assert_fails_msg == NULL)
Bram Moolenaar1d634542020-08-18 13:41:50 +0200677 {
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200678 emsg_assert_fails_msg = vim_strsave(s);
Bram Moolenaar1d634542020-08-18 13:41:50 +0200679 emsg_assert_fails_lnum = SOURCING_LNUM;
Bram Moolenaar9bd5d872020-09-06 21:47:48 +0200680 vim_free(emsg_assert_fails_context);
681 emsg_assert_fails_context = vim_strsave(
682 SOURCING_NAME == NULL ? (char_u *)"" : SOURCING_NAME);
Bram Moolenaar1d634542020-08-18 13:41:50 +0200683 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200684
Bram Moolenaar85a20022019-12-21 18:25:54 +0100685 // set "v:errmsg", also when using ":silent! cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 set_vim_var_string(VV_ERRMSG, s, -1);
687#endif
688
689 /*
Bram Moolenaara7241f52008-06-24 20:39:31 +0000690 * When using ":silent! cmd" ignore error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 * But do write it to the redirection file.
692 */
693 if (emsg_silent != 0)
694 {
Bram Moolenaar599410c2021-04-10 14:03:43 +0200695#ifdef FEAT_EVAL
696 ++did_emsg_silent;
697#endif
Bram Moolenaar79815f12016-07-09 17:07:29 +0200698 if (emsg_noredir == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 {
Bram Moolenaar79815f12016-07-09 17:07:29 +0200700 msg_start();
701 p = get_emsg_source();
702 if (p != NULL)
703 {
704 STRCAT(p, "\n");
705 redir_write(p, -1);
706 vim_free(p);
707 }
708 p = get_emsg_lnum();
709 if (p != NULL)
710 {
711 STRCAT(p, "\n");
712 redir_write(p, -1);
713 vim_free(p);
714 }
715 redir_write(s, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +0100717#ifdef FEAT_EVAL
718 // Only increment did_emsg_def when :silent! wasn't used inside the
719 // :def function.
720 if (emsg_silent == emsg_silent_def)
721 ++did_emsg_def;
722#endif
Bram Moolenaar958dc692016-12-01 15:34:12 +0100723#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare809a4e2019-07-04 17:35:05 +0200724 ch_log(NULL, "ERROR silent: %s", (char *)s);
Bram Moolenaar958dc692016-12-01 15:34:12 +0100725#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726 return TRUE;
727 }
728
Bram Moolenaar2b7bc562017-01-14 19:24:52 +0100729 ex_exitval = 1;
730
Bram Moolenaar85a20022019-12-21 18:25:54 +0100731 // Reset msg_silent, an error causes messages to be switched back on.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 msg_silent = 0;
733 cmd_silent = FALSE;
734
Bram Moolenaar85a20022019-12-21 18:25:54 +0100735 if (global_busy) // break :global command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736 ++global_busy;
737
738 if (p_eb)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100739 beep_flush(); // also includes flush_buffers()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740 else
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200741 flush_buffers(FLUSH_MINIMAL); // flush internal buffers
Bram Moolenaar76a63452018-11-28 20:38:37 +0100742 ++did_emsg; // flag for DoOneCmd()
Bram Moolenaare723c422017-09-06 23:40:10 +0200743#ifdef FEAT_EVAL
Bram Moolenaar88c89c72021-08-14 14:01:05 +0200744 ++uncaught_emsg;
Bram Moolenaare723c422017-09-06 23:40:10 +0200745#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 }
747
Bram Moolenaar85a20022019-12-21 18:25:54 +0100748 emsg_on_display = TRUE; // remember there is an error message
749 ++msg_scroll; // don't overwrite a previous message
750 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.
762 */
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000763 msg_source(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764
765 /*
766 * Display the error message itself.
767 */
Bram Moolenaar85a20022019-12-21 18:25:54 +0100768 msg_nowait = FALSE; // wait for this msg
Bram Moolenaar32526b32019-01-19 17:43:09 +0100769 r = msg_attr((char *)s, attr);
Bram Moolenaar958dc692016-12-01 15:34:12 +0100770
771#ifdef FEAT_JOB_CHANNEL
772 emsg_to_channel_log = FALSE;
773#endif
774 return r;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775}
776
777/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100778 * Print an error message.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 */
780 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100781emsg(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782{
Bram Moolenaar85a20022019-12-21 18:25:54 +0100783 // Skip this if not giving error messages at the moment.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100784 if (!emsg_not_now())
785 return emsg_core((char_u *)s);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100786 return TRUE; // no error messages at the moment
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787}
788
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100789#ifndef PROTO // manual proto with __attribute__
Bram Moolenaar95f09602016-11-10 20:01:45 +0100790/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100791 * Print an error message with format string and variable arguments.
792 * Note: caller must not pass 'IObuff' as 1st argument.
Bram Moolenaar95f09602016-11-10 20:01:45 +0100793 */
794 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100795semsg(const char *s, ...)
Bram Moolenaar95f09602016-11-10 20:01:45 +0100796{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200797 // Skip this if not giving error messages at the moment.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100798 if (!emsg_not_now())
799 {
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200800 if (IObuff == NULL)
801 {
802 // Very early in initialisation and already something wrong, just
803 // give the raw message so the user at least gets a hint.
804 return emsg_core((char_u *)s);
805 }
806 else
807 {
808 va_list ap;
Bram Moolenaar95f09602016-11-10 20:01:45 +0100809
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200810 va_start(ap, s);
811 vim_vsnprintf((char *)IObuff, IOSIZE, s, ap);
812 va_end(ap);
813 return emsg_core(IObuff);
814 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100815 }
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200816 return TRUE; // no error messages at the moment
Bram Moolenaar95f09602016-11-10 20:01:45 +0100817}
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100818#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +0100819
820/*
821 * Same as emsg(...), but abort on error when ABORT_ON_INTERNAL_ERROR is
822 * defined. It is used for internal errors only, so that they can be
823 * detected when fuzzing vim.
824 */
825 void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100826iemsg(char *s)
Bram Moolenaar95f09602016-11-10 20:01:45 +0100827{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100828 if (!emsg_not_now())
Bram Moolenaar1c67f3a2021-12-30 13:32:09 +0000829 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100830 emsg_core((char_u *)s);
Bram Moolenaar096ca732022-01-01 00:55:28 +0000831#if defined(ABORT_ON_INTERNAL_ERROR) && defined(FEAT_EVAL)
Bram Moolenaar1c67f3a2021-12-30 13:32:09 +0000832 set_vim_var_string(VV_ERRMSG, (char_u *)s, -1);
833 abort();
Bram Moolenaar95f09602016-11-10 20:01:45 +0100834#endif
Bram Moolenaar1c67f3a2021-12-30 13:32:09 +0000835 }
Bram Moolenaar95f09602016-11-10 20:01:45 +0100836}
837
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100838#ifndef PROTO // manual proto with __attribute__
Bram Moolenaar95f09602016-11-10 20:01:45 +0100839/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100840 * Same as semsg(...) but abort on error when ABORT_ON_INTERNAL_ERROR is
Bram Moolenaar95f09602016-11-10 20:01:45 +0100841 * defined. It is used for internal errors only, so that they can be
842 * detected when fuzzing vim.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100843 * Note: caller must not pass 'IObuff' as 1st argument.
Bram Moolenaar95f09602016-11-10 20:01:45 +0100844 */
845 void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100846siemsg(const char *s, ...)
Bram Moolenaar95f09602016-11-10 20:01:45 +0100847{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100848 if (!emsg_not_now())
849 {
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200850 if (IObuff == NULL)
851 {
852 // Very early in initialisation and already something wrong, just
853 // give the raw message so the user at least gets a hint.
854 emsg_core((char_u *)s);
855 }
856 else
857 {
858 va_list ap;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100859
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200860 va_start(ap, s);
861 vim_vsnprintf((char *)IObuff, IOSIZE, s, ap);
862 va_end(ap);
863 emsg_core(IObuff);
864 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100865 }
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100866# ifdef ABORT_ON_INTERNAL_ERROR
Bram Moolenaar95f09602016-11-10 20:01:45 +0100867 abort();
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100868# endif
Bram Moolenaar95f09602016-11-10 20:01:45 +0100869}
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100870#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +0100871
872/*
873 * Give an "Internal error" message.
874 */
875 void
876internal_error(char *where)
877{
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000878 siemsg(_(e_internal_error_str), where);
Bram Moolenaar95f09602016-11-10 20:01:45 +0100879}
880
Dominique Pelle748b3082022-01-08 12:41:16 +0000881#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaardd589232020-02-29 17:38:12 +0100882/*
883 * Like internal_error() but do not call abort(), to avoid tests using
884 * test_unknown() and test_void() causing Vim to exit.
885 */
886 void
887internal_error_no_abort(char *where)
888{
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000889 semsg(_(e_internal_error_str), where);
Bram Moolenaardd589232020-02-29 17:38:12 +0100890}
Dominique Pelle748b3082022-01-08 12:41:16 +0000891#endif
Bram Moolenaardd589232020-02-29 17:38:12 +0100892
Bram Moolenaar85a20022019-12-21 18:25:54 +0100893// emsg3() and emsgn() are in misc2.c to avoid warnings for the prototypes.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894
Bram Moolenaardf177f62005-02-22 08:39:57 +0000895 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100896emsg_invreg(int name)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000897{
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000898 semsg(_(e_invalid_register_name_str), transchar(name));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000899}
900
Dominique Pelle748b3082022-01-08 12:41:16 +0000901#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100903 * Give an error message which contains %s for "name[len]".
904 */
905 void
906emsg_namelen(char *msg, char_u *name, int len)
907{
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +0000908 char_u *copy = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100909
910 semsg(msg, copy == NULL ? "NULL" : (char *)copy);
Bram Moolenaar292b90d2020-03-18 15:23:16 +0100911 vim_free(copy);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100912}
Dominique Pelle748b3082022-01-08 12:41:16 +0000913#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100914
915/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 * Like msg(), but truncate to a single line if p_shm contains 't', or when
917 * "force" is TRUE. This truncates in another way as for normal messages.
918 * Careful: The string may be changed by msg_may_trunc()!
919 * Returns a pointer to the printed message, if wait_return() not called.
920 */
Bram Moolenaar32526b32019-01-19 17:43:09 +0100921 char *
922msg_trunc_attr(char *s, int force, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923{
924 int n;
Bram Moolenaar32526b32019-01-19 17:43:09 +0100925 char *ts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926
Bram Moolenaar85a20022019-12-21 18:25:54 +0100927 // Add message to history before truncating
Bram Moolenaar32526b32019-01-19 17:43:09 +0100928 add_msg_hist((char_u *)s, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929
Bram Moolenaar32526b32019-01-19 17:43:09 +0100930 ts = (char *)msg_may_trunc(force, (char_u *)s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931
932 msg_hist_off = TRUE;
Bram Moolenaar32526b32019-01-19 17:43:09 +0100933 n = msg_attr(ts, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934 msg_hist_off = FALSE;
935
936 if (n)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100937 return ts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 return NULL;
939}
940
941/*
942 * Check if message "s" should be truncated at the start (for filenames).
943 * Return a pointer to where the truncated message starts.
944 * Note: May change the message by replacing a character with '<'.
945 */
946 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100947msg_may_trunc(int force, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948{
949 int n;
950 int room;
951
Bram Moolenaar0fbc9262022-06-26 11:17:10 +0100952 // If something unexpected happened "room" may be negative, check for that
953 // just in case.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954 room = (int)(Rows - cmdline_row - 1) * Columns + sc_col - 1;
Bram Moolenaar0fbc9262022-06-26 11:17:10 +0100955 if (room > 0 && (force || (shortmess(SHM_TRUNC) && !exmode_active))
Shougo Matsushitaf39cfb72022-07-30 16:54:05 +0100956 && (n = (int)STRLEN(s) - room) > 0 && p_ch > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 if (has_mbyte)
959 {
960 int size = vim_strsize(s);
961
Bram Moolenaar85a20022019-12-21 18:25:54 +0100962 // There may be room anyway when there are multibyte chars.
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +0000963 if (size <= room)
964 return s;
965
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 for (n = 0; size >= room; )
967 {
968 size -= (*mb_ptr2cells)(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000969 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 }
971 --n;
972 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 s += n;
974 *s = '<';
975 }
976 return s;
977}
978
979 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100980add_msg_hist(
981 char_u *s,
Bram Moolenaar85a20022019-12-21 18:25:54 +0100982 int len, // -1 for undetermined length
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100983 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984{
985 struct msg_hist *p;
986
987 if (msg_hist_off || msg_silent != 0)
988 return;
989
Bram Moolenaar85a20022019-12-21 18:25:54 +0100990 // Don't let the message history get too big
Bram Moolenaar4770d092006-01-12 23:22:24 +0000991 while (msg_hist_len > MAX_MSG_HIST_LEN)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000992 (void)delete_first_msg();
993
Bram Moolenaar85a20022019-12-21 18:25:54 +0100994 // allocate an entry and add the message at the end of the history
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200995 p = ALLOC_ONE(struct msg_hist);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 if (p != NULL)
997 {
998 if (len < 0)
999 len = (int)STRLEN(s);
Bram Moolenaar85a20022019-12-21 18:25:54 +01001000 // remove leading and trailing newlines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 while (len > 0 && *s == '\n')
1002 {
1003 ++s;
1004 --len;
1005 }
1006 while (len > 0 && s[len - 1] == '\n')
1007 --len;
1008 p->msg = vim_strnsave(s, len);
1009 p->next = NULL;
1010 p->attr = attr;
1011 if (last_msg_hist != NULL)
1012 last_msg_hist->next = p;
1013 last_msg_hist = p;
1014 if (first_msg_hist == NULL)
1015 first_msg_hist = last_msg_hist;
1016 ++msg_hist_len;
1017 }
1018}
1019
1020/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00001021 * Delete the first (oldest) message from the history.
1022 * Returns FAIL if there are no messages.
1023 */
1024 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001025delete_first_msg(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00001026{
1027 struct msg_hist *p;
1028
1029 if (msg_hist_len <= 0)
1030 return FAIL;
1031 p = first_msg_hist;
1032 first_msg_hist = p->next;
Bram Moolenaara7241f52008-06-24 20:39:31 +00001033 if (first_msg_hist == NULL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001034 last_msg_hist = NULL; // history is empty
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00001035 vim_free(p->msg);
1036 vim_free(p);
1037 --msg_hist_len;
1038 return OK;
1039}
1040
1041/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 * ":messages" command.
1043 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 void
Bram Moolenaar52196b22016-04-14 17:52:41 +02001045ex_messages(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046{
1047 struct msg_hist *p;
1048 char_u *s;
Bram Moolenaar451f8492016-04-14 17:16:22 +02001049 int c = 0;
1050
1051 if (STRCMP(eap->arg, "clear") == 0)
1052 {
1053 int keep = eap->addr_count == 0 ? 0 : eap->line2;
1054
1055 while (msg_hist_len > keep)
1056 (void)delete_first_msg();
1057 return;
1058 }
1059
1060 if (*eap->arg != NUL)
1061 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001062 emsg(_(e_invalid_argument));
Bram Moolenaar451f8492016-04-14 17:16:22 +02001063 return;
1064 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065
1066 msg_hist_off = TRUE;
1067
Bram Moolenaar451f8492016-04-14 17:16:22 +02001068 p = first_msg_hist;
Bram Moolenaar451f8492016-04-14 17:16:22 +02001069 if (eap->addr_count != 0)
1070 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001071 // Count total messages
Bram Moolenaar451f8492016-04-14 17:16:22 +02001072 for (; p != NULL && !got_int; p = p->next)
1073 c++;
1074
1075 c -= eap->line2;
1076
Bram Moolenaar85a20022019-12-21 18:25:54 +01001077 // Skip without number of messages specified
Bram Moolenaar451f8492016-04-14 17:16:22 +02001078 for (p = first_msg_hist; p != NULL && !got_int && c > 0;
1079 p = p->next, c--);
1080 }
1081
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001082 if (p == first_msg_hist)
1083 {
Bram Moolenaar41f69182020-04-25 15:45:37 +02001084#ifdef FEAT_MULTI_LANG
1085 s = get_mess_lang();
1086#else
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001087 s = mch_getenv((char_u *)"LANG");
Bram Moolenaar41f69182020-04-25 15:45:37 +02001088#endif
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001089 if (s != NULL && *s != NUL)
Bram Moolenaar0c183192018-06-28 14:54:43 +02001090 // The next comment is extracted by xgettext and put in po file for
1091 // translators to read.
Bram Moolenaar32526b32019-01-19 17:43:09 +01001092 msg_attr(
Bram Moolenaar0c183192018-06-28 14:54:43 +02001093 // Translator: Please replace the name and email address
1094 // with the appropriate text for your translation.
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001095 _("Messages maintainer: Bram Moolenaar <Bram@vim.org>"),
Bram Moolenaar8820b482017-03-16 17:23:31 +01001096 HL_ATTR(HLF_T));
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001097 }
1098
Bram Moolenaar85a20022019-12-21 18:25:54 +01001099 // Display what was not skipped.
Bram Moolenaar451f8492016-04-14 17:16:22 +02001100 for (; p != NULL && !got_int; p = p->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 if (p->msg != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001102 msg_attr((char *)p->msg, p->attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103
1104 msg_hist_off = FALSE;
1105}
1106
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001107#if defined(FEAT_CON_DIALOG) || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108/*
1109 * Call this after prompting the user. This will avoid a hit-return message
1110 * and a delay.
1111 */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001112 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001113msg_end_prompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114{
1115 need_wait_return = FALSE;
1116 emsg_on_display = FALSE;
1117 cmdline_row = msg_row;
1118 msg_col = 0;
1119 msg_clr_eos();
Bram Moolenaar5d6f75e2011-12-30 14:14:29 +01001120 lines_left = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121}
1122#endif
1123
1124/*
Bram Moolenaar32526b32019-01-19 17:43:09 +01001125 * Wait for the user to hit a key (normally Enter).
1126 * If "redraw" is TRUE, clear and redraw the screen.
1127 * If "redraw" is FALSE, just redraw the screen.
1128 * If "redraw" is -1, don't redraw at all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 */
1130 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001131wait_return(int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132{
1133 int c;
1134 int oldState;
1135 int tmpState;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 int had_got_int;
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001137 int save_reg_recording;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001138 FILE *save_scriptout;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139
1140 if (redraw == TRUE)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001141 must_redraw = UPD_CLEAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142
Bram Moolenaar85a20022019-12-21 18:25:54 +01001143 // If using ":silent cmd", don't wait for a return. Also don't set
1144 // need_wait_return to do it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 if (msg_silent != 0)
1146 return;
1147
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01001148 /*
1149 * When inside vgetc(), we can't wait for a typed character at all.
1150 * With the global command (and some others) we only need one return at
1151 * the end. Adjust cmdline_row to avoid the next message overwriting the
1152 * last one.
1153 */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00001154 if (vgetc_busy > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 return;
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01001156 need_wait_return = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 if (no_wait_return)
1158 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 if (!exmode_active)
1160 cmdline_row = msg_row;
1161 return;
1162 }
1163
Bram Moolenaar85a20022019-12-21 18:25:54 +01001164 redir_off = TRUE; // don't redirect this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 oldState = State;
1166 if (quit_more)
1167 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001168 c = CAR; // just pretend CR was hit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 quit_more = FALSE;
1170 got_int = FALSE;
1171 }
1172 else if (exmode_active)
1173 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001174 msg_puts(" "); // make sure the cursor is on the right line
1175 c = CAR; // no need for a return in ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 got_int = FALSE;
1177 }
1178 else
1179 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001180 // Make sure the hit-return prompt is on screen when 'guioptions' was
1181 // just changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182 screenalloc(FALSE);
1183
Bram Moolenaar24959102022-05-07 20:01:16 +01001184 State = MODE_HITRETURN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar85a20022019-12-21 18:25:54 +01001187 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188#endif
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01001189 cmdline_row = msg_row;
1190
Bram Moolenaar85a20022019-12-21 18:25:54 +01001191 // Avoid the sequence that the user types ":" at the hit-return prompt
1192 // to start an Ex command, but the file-changed dialog gets in the
1193 // way.
Bram Moolenaarec38d692013-04-24 15:12:32 +02001194 if (need_check_timestamps)
1195 check_timestamps(FALSE);
1196
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 hit_return_msg();
1198
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 do
1200 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001201 // Remember "got_int", if it is set vgetc() probably returns a
1202 // CTRL-C, but we need to loop then.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 had_got_int = got_int;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001204
Bram Moolenaar85a20022019-12-21 18:25:54 +01001205 // Don't do mappings here, we put the character back in the
1206 // typeahead buffer.
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001207 ++no_mapping;
1208 ++allow_keys;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001209
Bram Moolenaar85a20022019-12-21 18:25:54 +01001210 // Temporarily disable Recording. If Recording is active, the
1211 // character will be recorded later, since it will be added to the
1212 // typebuf after the loop
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001213 save_reg_recording = reg_recording;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001214 save_scriptout = scriptout;
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001215 reg_recording = 0;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001216 scriptout = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 c = safe_vgetc();
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00001218 if (had_got_int && !global_busy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 got_int = FALSE;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001220 --no_mapping;
1221 --allow_keys;
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001222 reg_recording = save_reg_recording;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001223 scriptout = save_scriptout;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001224
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225#ifdef FEAT_CLIPBOARD
Bram Moolenaar85a20022019-12-21 18:25:54 +01001226 // Strange way to allow copying (yanking) a modeless selection at
1227 // the hit-enter prompt. Use CTRL-Y, because the same is used in
1228 // Cmdline-mode and it's harmless when there is no selection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 if (c == Ctrl_Y && clip_star.state == SELECT_DONE)
1230 {
1231 clip_copy_modeless_selection(TRUE);
1232 c = K_IGNORE;
1233 }
1234#endif
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00001235
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001236 /*
1237 * Allow scrolling back in the messages.
1238 * Also accept scroll-down commands when messages fill the screen,
1239 * to avoid that typing one 'j' too many makes the messages
1240 * disappear.
1241 */
1242 if (p_more && !p_cp)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001243 {
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001244 if (c == 'b' || c == 'k' || c == 'u' || c == 'g'
1245 || c == K_UP || c == K_PAGEUP)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001246 {
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001247 if (msg_scrolled > Rows)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001248 // scroll back to show older messages
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001249 do_more_prompt(c);
1250 else
1251 {
1252 msg_didout = FALSE;
1253 c = K_IGNORE;
1254 msg_col =
1255#ifdef FEAT_RIGHTLEFT
1256 cmdmsg_rl ? Columns - 1 :
1257#endif
1258 0;
1259 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001260 if (quit_more)
1261 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001262 c = CAR; // just pretend CR was hit
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001263 quit_more = FALSE;
1264 got_int = FALSE;
1265 }
Bram Moolenaarb0912962013-08-09 20:38:26 +02001266 else if (c != K_IGNORE)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001267 {
1268 c = K_IGNORE;
1269 hit_return_msg();
1270 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001271 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001272 else if (msg_scrolled > Rows - 2
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001273 && (c == 'j' || c == 'd' || c == 'f'
1274 || c == K_DOWN || c == K_PAGEDOWN))
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001275 c = K_IGNORE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001276 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 } while ((had_got_int && c == Ctrl_C)
1278 || c == K_IGNORE
1279#ifdef FEAT_GUI
1280 || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR
1281#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 || c == K_LEFTDRAG || c == K_LEFTRELEASE
1283 || c == K_MIDDLEDRAG || c == K_MIDDLERELEASE
1284 || c == K_RIGHTDRAG || c == K_RIGHTRELEASE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001285 || c == K_MOUSELEFT || c == K_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 || c == K_MOUSEDOWN || c == K_MOUSEUP
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001287 || c == K_MOUSEMOVE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 || (!mouse_has(MOUSE_RETURN)
1289 && mouse_row < msg_row
1290 && (c == K_LEFTMOUSE
1291 || c == K_MIDDLEMOUSE
1292 || c == K_RIGHTMOUSE
1293 || c == K_X1MOUSE
1294 || c == K_X2MOUSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295 );
1296 ui_breakcheck();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 /*
1298 * Avoid that the mouse-up event causes visual mode to start.
1299 */
1300 if (c == K_LEFTMOUSE || c == K_MIDDLEMOUSE || c == K_RIGHTMOUSE
1301 || c == K_X1MOUSE || c == K_X2MOUSE)
1302 (void)jump_to_mouse(MOUSE_SETPOS, NULL, 0);
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001303 else if (vim_strchr((char_u *)"\r\n ", c) == NULL && c != Ctrl_C)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001305 // Put the character back in the typeahead buffer. Don't use the
1306 // stuff buffer, because lmaps wouldn't work.
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001307 ins_char_typebuf(vgetc_char, vgetc_mod_mask);
Bram Moolenaar85a20022019-12-21 18:25:54 +01001308 do_redraw = TRUE; // need a redraw even though there is
1309 // typeahead
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 }
1312 redir_off = FALSE;
1313
1314 /*
1315 * If the user hits ':', '?' or '/' we get a command line from the next
1316 * line.
1317 */
1318 if (c == ':' || c == '?' || c == '/')
1319 {
1320 if (!exmode_active)
1321 cmdline_row = msg_row;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001322 skip_redraw = TRUE; // skip redraw once
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 do_redraw = FALSE;
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02001324#ifdef FEAT_TERMINAL
1325 skip_term_loop = TRUE;
1326#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 }
1328
1329 /*
1330 * If the window size changed set_shellsize() will redraw the screen.
1331 * Otherwise the screen is only redrawn if 'redraw' is set and no ':'
1332 * typed.
1333 */
1334 tmpState = State;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001335 State = oldState; // restore State before set_shellsize
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 msg_check();
1338
1339#if defined(UNIX) || defined(VMS)
1340 /*
1341 * When switching screens, we need to output an extra newline on exit.
1342 */
1343 if (swapping_screen() && !termcap_active)
1344 newline_on_exit = TRUE;
1345#endif
1346
1347 need_wait_return = FALSE;
1348 did_wait_return = TRUE;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001349 emsg_on_display = FALSE; // can delete error message now
1350 lines_left = -1; // reset lines_left at next msg_start()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 reset_last_sourcing();
1352 if (keep_msg != NULL && vim_strsize(keep_msg) >=
1353 (Rows - cmdline_row - 1) * Columns + sc_col)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001354 VIM_CLEAR(keep_msg); // don't redisplay message, it's too long
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355
Bram Moolenaar24959102022-05-07 20:01:16 +01001356 if (tmpState == MODE_SETWSIZE) // got resize event while in vgetc()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001358 starttermcap(); // start termcap before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 shell_resized();
1360 }
1361 else if (!skip_redraw
1362 && (redraw == TRUE || (msg_scrolled != 0 && redraw != -1)))
1363 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001364 starttermcap(); // start termcap before redrawing
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001365 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 }
1367}
1368
1369/*
1370 * Write the hit-return prompt.
1371 */
1372 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001373hit_return_msg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001375 int save_p_more = p_more;
1376
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001377 p_more = FALSE; // don't want to see this message when scrolling back
Bram Moolenaar85a20022019-12-21 18:25:54 +01001378 if (msg_didout) // start on a new line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 msg_putchar('\n');
1380 if (got_int)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001381 msg_puts(_("Interrupt: "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382
Bram Moolenaar32526b32019-01-19 17:43:09 +01001383 msg_puts_attr(_("Press ENTER or type command to continue"), HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 if (!msg_use_printf())
1385 msg_clr_eos();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001386 p_more = save_p_more;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387}
1388
1389/*
1390 * Set "keep_msg" to "s". Free the old value and check for NULL pointer.
1391 */
1392 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001393set_keep_msg(char_u *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394{
1395 vim_free(keep_msg);
1396 if (s != NULL && msg_silent == 0)
1397 keep_msg = vim_strsave(s);
1398 else
1399 keep_msg = NULL;
Bram Moolenaaraab21c32005-01-25 21:46:35 +00001400 keep_msg_more = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001401 keep_msg_attr = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402}
1403
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001404#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
1405/*
1406 * If there currently is a message being displayed, set "keep_msg" to it, so
1407 * that it will be displayed again after redraw.
1408 */
1409 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001410set_keep_msg_from_hist(void)
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001411{
1412 if (keep_msg == NULL && last_msg_hist != NULL && msg_scrolled == 0
Bram Moolenaar24959102022-05-07 20:01:16 +01001413 && (State & MODE_NORMAL))
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001414 set_keep_msg(last_msg_hist->msg, last_msg_hist->attr);
1415}
1416#endif
1417
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418/*
1419 * Prepare for outputting characters in the command line.
1420 */
1421 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001422msg_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423{
1424 int did_return = FALSE;
1425
Bram Moolenaare4ce65d2010-08-04 20:12:32 +02001426 if (!msg_silent)
Rob Pilling726f7f92022-01-20 14:44:38 +00001427 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001428 VIM_CLEAR(keep_msg);
Rob Pilling726f7f92022-01-20 14:44:38 +00001429 need_fileinfo = FALSE;
1430 }
Bram Moolenaara7241f52008-06-24 20:39:31 +00001431
1432#ifdef FEAT_EVAL
Shougo Matsushitaf39cfb72022-07-30 16:54:05 +01001433 if (need_clr_eos || p_ch == 0)
Bram Moolenaara7241f52008-06-24 20:39:31 +00001434 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001435 // Halfway an ":echo" command and getting an (error) message: clear
1436 // any text from the command.
Bram Moolenaara7241f52008-06-24 20:39:31 +00001437 need_clr_eos = FALSE;
1438 msg_clr_eos();
1439 }
1440#endif
1441
Bram Moolenaar85a20022019-12-21 18:25:54 +01001442 if (!msg_scroll && full_screen) // overwrite last message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 {
1444 msg_row = cmdline_row;
1445 msg_col =
1446#ifdef FEAT_RIGHTLEFT
1447 cmdmsg_rl ? Columns - 1 :
1448#endif
1449 0;
1450 }
Shougo Matsushitaf39cfb72022-07-30 16:54:05 +01001451 else if (msg_didout || p_ch == 0) // start message on next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 {
1453 msg_putchar('\n');
1454 did_return = TRUE;
1455 if (exmode_active != EXMODE_NORMAL)
1456 cmdline_row = msg_row;
1457 }
1458 if (!msg_didany || lines_left < 0)
1459 msg_starthere();
1460 if (msg_silent == 0)
1461 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001462 msg_didout = FALSE; // no output on current line yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 cursor_off();
1464 }
1465
Bram Moolenaar85a20022019-12-21 18:25:54 +01001466 // when redirecting, may need to start a new line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 if (!did_return)
1468 redir_write((char_u *)"\n", -1);
1469}
1470
1471/*
1472 * Note that the current msg position is where messages start.
1473 */
1474 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001475msg_starthere(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476{
1477 lines_left = cmdline_row;
1478 msg_didany = FALSE;
1479}
1480
1481 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001482msg_putchar(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483{
1484 msg_putchar_attr(c, 0);
1485}
1486
1487 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001488msg_putchar_attr(int c, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489{
Bram Moolenaar63c0ccd2019-01-19 21:06:58 +01001490 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491
1492 if (IS_SPECIAL(c))
1493 {
1494 buf[0] = K_SPECIAL;
1495 buf[1] = K_SECOND(c);
1496 buf[2] = K_THIRD(c);
1497 buf[3] = NUL;
1498 }
1499 else
Bram Moolenaar63c0ccd2019-01-19 21:06:58 +01001500 buf[(*mb_char2bytes)(c, buf)] = NUL;
Bram Moolenaar63c0ccd2019-01-19 21:06:58 +01001501 msg_puts_attr((char *)buf, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502}
1503
1504 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001505msg_outnum(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001507 char buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508
Bram Moolenaar32526b32019-01-19 17:43:09 +01001509 sprintf(buf, "%ld", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 msg_puts(buf);
1511}
1512
1513 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001514msg_home_replace(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515{
1516 msg_home_replace_attr(fname, 0);
1517}
1518
1519#if defined(FEAT_FIND_ID) || defined(PROTO)
1520 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001521msg_home_replace_hl(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522{
Bram Moolenaar8820b482017-03-16 17:23:31 +01001523 msg_home_replace_attr(fname, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524}
1525#endif
1526
1527 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001528msg_home_replace_attr(char_u *fname, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529{
1530 char_u *name;
1531
1532 name = home_replace_save(NULL, fname);
1533 if (name != NULL)
1534 msg_outtrans_attr(name, attr);
1535 vim_free(name);
1536}
1537
1538/*
1539 * Output 'len' characters in 'str' (including NULs) with translation
Bram Moolenaarf48ee3c2019-12-06 22:18:20 +01001540 * if 'len' is -1, output up to a NUL character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541 * Use attributes 'attr'.
1542 * Return the number of characters it takes on the screen.
1543 */
1544 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001545msg_outtrans(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546{
1547 return msg_outtrans_attr(str, 0);
1548}
1549
1550 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001551msg_outtrans_attr(char_u *str, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552{
1553 return msg_outtrans_len_attr(str, (int)STRLEN(str), attr);
1554}
1555
1556 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001557msg_outtrans_len(char_u *str, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558{
1559 return msg_outtrans_len_attr(str, len, 0);
1560}
1561
1562/*
1563 * Output one character at "p". Return pointer to the next character.
1564 * Handles multi-byte characters.
1565 */
1566 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001567msg_outtrans_one(char_u *p, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 int l;
1570
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001571 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 {
1573 msg_outtrans_len_attr(p, l, attr);
1574 return p + l;
1575 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01001576 msg_puts_attr((char *)transchar_byte(*p), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 return p + 1;
1578}
1579
1580 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001581msg_outtrans_len_attr(char_u *msgstr, int len, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582{
1583 int retval = 0;
1584 char_u *str = msgstr;
1585 char_u *plain_start = msgstr;
1586 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 int mb_l;
1588 int c;
Bram Moolenaar3d30af82020-10-13 22:15:56 +02001589 int save_got_int = got_int;
1590
1591 // Only quit when got_int was set in here.
1592 got_int = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593
Bram Moolenaar85a20022019-12-21 18:25:54 +01001594 // if MSG_HIST flag set, add message to history
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 if (attr & MSG_HIST)
1596 {
1597 add_msg_hist(str, len, attr);
1598 attr &= ~MSG_HIST;
1599 }
1600
Bram Moolenaar85a20022019-12-21 18:25:54 +01001601 // If the string starts with a composing character first draw a space on
1602 // which the composing char can be drawn.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 if (enc_utf8 && utf_iscomposing(utf_ptr2char(msgstr)))
Bram Moolenaar32526b32019-01-19 17:43:09 +01001604 msg_puts_attr(" ", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605
1606 /*
1607 * Go over the string. Special characters are translated and printed.
1608 * Normal characters are printed several at a time.
1609 */
Bram Moolenaar3d30af82020-10-13 22:15:56 +02001610 while (--len >= 0 && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 if (enc_utf8)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001613 // Don't include composing chars after the end.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001614 mb_l = utfc_ptr2len_len(str, len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 else if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001616 mb_l = (*mb_ptr2len)(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617 else
1618 mb_l = 1;
1619 if (has_mbyte && mb_l > 1)
1620 {
1621 c = (*mb_ptr2char)(str);
1622 if (vim_isprintc(c))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001623 // printable multi-byte char: count the cells.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 retval += (*mb_ptr2cells)(str);
1625 else
1626 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001627 // unprintable multi-byte char: print the printable chars so
1628 // far and the translation of the unprintable char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 if (str > plain_start)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001630 msg_puts_attr_len((char *)plain_start,
1631 (int)(str - plain_start), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 plain_start = str + mb_l;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001633 msg_puts_attr((char *)transchar(c),
1634 attr == 0 ? HL_ATTR(HLF_8) : attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 retval += char2cells(c);
1636 }
1637 len -= mb_l - 1;
1638 str += mb_l;
1639 }
1640 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 {
1642 s = transchar_byte(*str);
1643 if (s[1] != NUL)
1644 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001645 // unprintable char: print the printable chars so far and the
1646 // translation of the unprintable char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 if (str > plain_start)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001648 msg_puts_attr_len((char *)plain_start,
1649 (int)(str - plain_start), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 plain_start = str + 1;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001651 msg_puts_attr((char *)s, attr == 0 ? HL_ATTR(HLF_8) : attr);
Bram Moolenaarc236c162008-07-13 17:41:49 +00001652 retval += (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 }
Bram Moolenaarb3c722a2008-07-06 17:16:02 +00001654 else
1655 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 ++str;
1657 }
1658 }
1659
Bram Moolenaar3d30af82020-10-13 22:15:56 +02001660 if (str > plain_start && !got_int)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001661 // print the printable chars at the end
Bram Moolenaar32526b32019-01-19 17:43:09 +01001662 msg_puts_attr_len((char *)plain_start, (int)(str - plain_start), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663
Bram Moolenaar3d30af82020-10-13 22:15:56 +02001664 got_int |= save_got_int;
1665
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 return retval;
1667}
1668
1669#if defined(FEAT_QUICKFIX) || defined(PROTO)
1670 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001671msg_make(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672{
1673 int i;
1674 static char_u *str = (char_u *)"eeffoc", *rs = (char_u *)"Plon#dqg#vxjduB";
1675
1676 arg = skipwhite(arg);
1677 for (i = 5; *arg && i >= 0; --i)
1678 if (*arg++ != str[i])
1679 break;
1680 if (i < 0)
1681 {
1682 msg_putchar('\n');
1683 for (i = 0; rs[i]; ++i)
1684 msg_putchar(rs[i] - 3);
1685 }
1686}
1687#endif
1688
1689/*
Bram Moolenaarf48ee3c2019-12-06 22:18:20 +01001690 * Output the string 'str' up to a NUL character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691 * Return the number of characters it takes on the screen.
1692 *
1693 * If K_SPECIAL is encountered, then it is taken in conjunction with the
1694 * following character and shown as <F1>, <S-Up> etc. Any other character
1695 * which is not printable shown in <> form.
1696 * If 'from' is TRUE (lhs of a mapping), a space is shown as <Space>.
1697 * If a character is displayed in one of these special ways, is also
1698 * highlighted (its highlight name is '8' in the p_hl variable).
1699 * Otherwise characters are not highlighted.
1700 * This function is used to show mappings, where we want to see how to type
1701 * the character/string -- webb
1702 */
1703 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001704msg_outtrans_special(
1705 char_u *strstart,
Bram Moolenaar725310d2019-04-24 23:08:23 +02001706 int from, // TRUE for lhs of a mapping
Bram Moolenaar32aa1022019-11-02 22:54:41 +01001707 int maxlen) // screen columns, 0 for unlimited
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708{
1709 char_u *str = strstart;
1710 int retval = 0;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001711 char *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 int attr;
1713 int len;
1714
Bram Moolenaar8820b482017-03-16 17:23:31 +01001715 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 while (*str != NUL)
1717 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001718 // Leading and trailing spaces need to be displayed in <> form.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 if ((str == strstart || str[1] == NUL) && *str == ' ')
1720 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001721 text = "<Space>";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 ++str;
1723 }
1724 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01001725 text = (char *)str2special(&str, from);
zeertzjq0519ce02022-05-09 12:16:19 +01001726 if (text[0] != NUL && text[1] == NUL)
1727 // single-byte character or illegal byte
1728 text = (char *)transchar_byte((char_u)text[0]);
Bram Moolenaar32526b32019-01-19 17:43:09 +01001729 len = vim_strsize((char_u *)text);
Bram Moolenaar725310d2019-04-24 23:08:23 +02001730 if (maxlen > 0 && retval + len >= maxlen)
1731 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001732 // Highlight special keys
Bram Moolenaar32526b32019-01-19 17:43:09 +01001733 msg_puts_attr(text, len > 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001734 && (*mb_ptr2len)((char_u *)text) <= 1 ? attr : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735 retval += len;
1736 }
1737 return retval;
1738}
1739
Bram Moolenaarbd743252010-10-20 21:23:33 +02001740#if defined(FEAT_EVAL) || defined(PROTO)
1741/*
1742 * Return the lhs or rhs of a mapping, with the key codes turned into printable
1743 * strings, in an allocated string.
1744 */
1745 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001746str2special_save(
1747 char_u *str,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001748 int is_lhs) // TRUE for lhs, FALSE for rhs
Bram Moolenaarbd743252010-10-20 21:23:33 +02001749{
1750 garray_T ga;
1751 char_u *p = str;
1752
1753 ga_init2(&ga, 1, 40);
1754 while (*p != NUL)
1755 ga_concat(&ga, str2special(&p, is_lhs));
1756 ga_append(&ga, NUL);
1757 return (char_u *)ga.ga_data;
1758}
1759#endif
1760
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761/*
1762 * Return the printable string for the key codes at "*sp".
zeertzjq0519ce02022-05-09 12:16:19 +01001763 * On illegal byte return a string with only that byte.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764 * Used for translating the lhs or rhs of a mapping to printable chars.
1765 * Advances "sp" to the next code.
1766 */
1767 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001768str2special(
1769 char_u **sp,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001770 int from) // TRUE for lhs of mapping
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771{
1772 int c;
1773 static char_u buf[7];
1774 char_u *str = *sp;
1775 int modifiers = 0;
1776 int special = FALSE;
1777
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 if (has_mbyte)
1779 {
1780 char_u *p;
1781
Bram Moolenaar85a20022019-12-21 18:25:54 +01001782 // Try to un-escape a multi-byte character. Return the un-escaped
1783 // string if it is a multi-byte character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 p = mb_unescape(sp);
1785 if (p != NULL)
1786 return p;
1787 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788
1789 c = *str;
1790 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1791 {
1792 if (str[1] == KS_MODIFIER)
1793 {
1794 modifiers = str[2];
1795 str += 3;
1796 c = *str;
1797 }
1798 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1799 {
1800 c = TO_SPECIAL(str[1], str[2]);
1801 str += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001803 if (IS_SPECIAL(c) || modifiers) // special key
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 special = TRUE;
1805 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806
zeertzjq0519ce02022-05-09 12:16:19 +01001807 if (has_mbyte && !IS_SPECIAL(c) && MB_BYTE2LEN(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 {
zeertzjqac402f42022-05-04 18:51:43 +01001809 char_u *p;
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001810
zeertzjqac402f42022-05-04 18:51:43 +01001811 *sp = str;
1812 // Try to un-escape a multi-byte character after modifiers.
1813 p = mb_unescape(sp);
zeertzjq0519ce02022-05-09 12:16:19 +01001814 if (p != NULL)
1815 // Since 'special' is TRUE the multi-byte character 'c' will be
1816 // processed by get_special_key_name()
1817 c = (*mb_ptr2char)(p);
1818 else
1819 // illegal byte
1820 *sp = str + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 }
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001822 else
Bram Moolenaar083692d2022-06-29 21:16:58 +01001823 // single-byte character, NUL or illegal byte
1824 *sp = str + (*str == NUL ? 0 : 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825
zeertzjq0519ce02022-05-09 12:16:19 +01001826 // Make special keys and C0 control characters in <> form, also <M-Space>.
Bram Moolenaar85a20022019-12-21 18:25:54 +01001827 // Use <Space> only for lhs of a mapping.
zeertzjq0519ce02022-05-09 12:16:19 +01001828 if (special || c < ' ' || (from && c == ' '))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 return get_special_key_name(c, modifiers);
1830 buf[0] = c;
1831 buf[1] = NUL;
1832 return buf;
1833}
1834
1835/*
1836 * Translate a key sequence into special key names.
1837 */
1838 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001839str2specialbuf(char_u *sp, char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840{
1841 char_u *s;
1842
1843 *buf = NUL;
1844 while (*sp)
1845 {
1846 s = str2special(&sp, FALSE);
1847 if ((int)(STRLEN(s) + STRLEN(buf)) < len)
1848 STRCAT(buf, s);
1849 }
1850}
1851
1852/*
1853 * print line for :print or :list command
1854 */
1855 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001856msg_prt_line(char_u *s, int list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857{
1858 int c;
1859 int col = 0;
1860 int n_extra = 0;
1861 int c_extra = 0;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001862 int c_final = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001863 char_u *p_extra = NULL; // init to make SASC shut up
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 int n;
Bram Moolenaar56da7972007-01-16 14:46:32 +00001865 int attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 char_u *trail = NULL;
Bram Moolenaar91478ae2021-02-03 15:58:13 +01001867 char_u *lead = NULL;
zeertzjqf14b8ba2021-09-10 16:58:30 +02001868 int in_multispace = FALSE;
1869 int multispace_pos = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 int l;
1871 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872
Bram Moolenaardf177f62005-02-22 08:39:57 +00001873 if (curwin->w_p_list)
1874 list = TRUE;
1875
Bram Moolenaar91478ae2021-02-03 15:58:13 +01001876 if (list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 {
Bram Moolenaar91478ae2021-02-03 15:58:13 +01001878 // find start of trailing whitespace
Bram Moolenaareed9d462021-02-15 20:38:25 +01001879 if (curwin->w_lcs_chars.trail)
Bram Moolenaar91478ae2021-02-03 15:58:13 +01001880 {
1881 trail = s + STRLEN(s);
1882 while (trail > s && VIM_ISWHITE(trail[-1]))
1883 --trail;
1884 }
1885 // find end of leading whitespace
Bram Moolenaarb8329db2022-07-06 13:31:28 +01001886 if (curwin->w_lcs_chars.lead
1887 || curwin->w_lcs_chars.leadmultispace != NULL)
Bram Moolenaar91478ae2021-02-03 15:58:13 +01001888 {
1889 lead = s;
1890 while (VIM_ISWHITE(lead[0]))
1891 lead++;
1892 // in a line full of spaces all of them are treated as trailing
1893 if (*lead == NUL)
1894 lead = NULL;
1895 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 }
1897
Bram Moolenaar85a20022019-12-21 18:25:54 +01001898 // output a space for an empty line, otherwise the line will be
1899 // overwritten
Bram Moolenaareed9d462021-02-15 20:38:25 +01001900 if (*s == NUL && !(list && curwin->w_lcs_chars.eol != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 msg_putchar(' ');
1902
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001903 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 {
Bram Moolenaar56da7972007-01-16 14:46:32 +00001905 if (n_extra > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 {
1907 --n_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001908 if (n_extra == 0 && c_final)
1909 c = c_final;
1910 else if (c_extra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 c = c_extra;
1912 else
1913 c = *p_extra++;
1914 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001915 else if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 {
1917 col += (*mb_ptr2cells)(s);
Bram Moolenaar1cbfc992020-12-02 12:37:37 +01001918 if (l >= MB_MAXBYTES)
1919 {
Bram Moolenaar29d2f452020-12-04 19:42:52 +01001920 STRCPY(buf, "?");
Bram Moolenaar1cbfc992020-12-02 12:37:37 +01001921 }
Bram Moolenaareed9d462021-02-15 20:38:25 +01001922 else if (curwin->w_lcs_chars.nbsp != NUL && list
Bram Moolenaar73284b92015-05-04 17:28:22 +02001923 && (mb_ptr2char(s) == 160
1924 || mb_ptr2char(s) == 0x202f))
Bram Moolenaaracf17282011-02-01 17:12:25 +01001925 {
Bram Moolenaar74ac29c2022-06-15 12:12:44 +01001926 int len = mb_char2bytes(curwin->w_lcs_chars.nbsp, buf);
1927
1928 buf[len] = NUL;
Bram Moolenaaracf17282011-02-01 17:12:25 +01001929 }
1930 else
1931 {
1932 mch_memmove(buf, s, (size_t)l);
1933 buf[l] = NUL;
1934 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01001935 msg_puts((char *)buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 s += l;
1937 continue;
1938 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 else
1940 {
1941 attr = 0;
1942 c = *s++;
zeertzjqf14b8ba2021-09-10 16:58:30 +02001943 in_multispace = c == ' '
1944 && ((col > 0 && s[-2] == ' ') || *s == ' ');
1945 if (!in_multispace)
1946 multispace_pos = 0;
Bram Moolenaareed9d462021-02-15 20:38:25 +01001947 if (c == TAB && (!list || curwin->w_lcs_chars.tab1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001949 // tab amount depends on current column
Bram Moolenaar04958cb2018-06-23 19:23:02 +02001950#ifdef FEAT_VARTABS
1951 n_extra = tabstop_padding(col, curbuf->b_p_ts,
1952 curbuf->b_p_vts_array) - 1;
1953#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 n_extra = curbuf->b_p_ts - col % curbuf->b_p_ts - 1;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02001955#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00001956 if (!list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 {
1958 c = ' ';
1959 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01001960 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 }
1962 else
1963 {
Bram Moolenaareed9d462021-02-15 20:38:25 +01001964 c = (n_extra == 0 && curwin->w_lcs_chars.tab3)
1965 ? curwin->w_lcs_chars.tab3
1966 : curwin->w_lcs_chars.tab1;
1967 c_extra = curwin->w_lcs_chars.tab2;
1968 c_final = curwin->w_lcs_chars.tab3;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001969 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 }
1971 }
Bram Moolenaareed9d462021-02-15 20:38:25 +01001972 else if (c == 160 && list && curwin->w_lcs_chars.nbsp != NUL)
Bram Moolenaaracf17282011-02-01 17:12:25 +01001973 {
Bram Moolenaareed9d462021-02-15 20:38:25 +01001974 c = curwin->w_lcs_chars.nbsp;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001975 attr = HL_ATTR(HLF_8);
Bram Moolenaaracf17282011-02-01 17:12:25 +01001976 }
Bram Moolenaareed9d462021-02-15 20:38:25 +01001977 else if (c == NUL && list && curwin->w_lcs_chars.eol != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 {
1979 p_extra = (char_u *)"";
1980 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001981 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 n_extra = 1;
Bram Moolenaareed9d462021-02-15 20:38:25 +01001983 c = curwin->w_lcs_chars.eol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001984 attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 --s;
1986 }
1987 else if (c != NUL && (n = byte2cells(c)) > 1)
1988 {
1989 n_extra = n - 1;
1990 p_extra = transchar_byte(c);
1991 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001992 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 c = *p_extra++;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001994 // Use special coloring to be able to distinguish <hex> from
1995 // the same in plain text.
Bram Moolenaar8820b482017-03-16 17:23:31 +01001996 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 }
zeertzjqf14b8ba2021-09-10 16:58:30 +02001998 else if (c == ' ')
Bram Moolenaar91478ae2021-02-03 15:58:13 +01001999 {
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01002000 if (list && lead != NULL && s <= lead && in_multispace
2001 && curwin->w_lcs_chars.leadmultispace != NULL)
2002 {
2003 c = curwin->w_lcs_chars.leadmultispace[multispace_pos++];
zeertzjqb5f08012022-06-09 13:55:28 +01002004 if (curwin->w_lcs_chars.leadmultispace[multispace_pos]
2005 == NUL)
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01002006 multispace_pos = 0;
2007 attr = HL_ATTR(HLF_8);
2008 }
zeertzjqb5f08012022-06-09 13:55:28 +01002009 else if (lead != NULL && s <= lead
2010 && curwin->w_lcs_chars.lead != NUL)
zeertzjqf14b8ba2021-09-10 16:58:30 +02002011 {
2012 c = curwin->w_lcs_chars.lead;
2013 attr = HL_ATTR(HLF_8);
2014 }
2015 else if (trail != NULL && s > trail)
2016 {
2017 c = curwin->w_lcs_chars.trail;
2018 attr = HL_ATTR(HLF_8);
2019 }
2020 else if (list && in_multispace
2021 && curwin->w_lcs_chars.multispace != NULL)
2022 {
2023 c = curwin->w_lcs_chars.multispace[multispace_pos++];
2024 if (curwin->w_lcs_chars.multispace[multispace_pos] == NUL)
2025 multispace_pos = 0;
2026 attr = HL_ATTR(HLF_8);
2027 }
2028 else if (list && curwin->w_lcs_chars.space != NUL)
2029 {
2030 c = curwin->w_lcs_chars.space;
2031 attr = HL_ATTR(HLF_8);
2032 }
Bram Moolenaar1510f992015-04-22 22:18:22 +02002033 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 }
2035
2036 if (c == NUL)
2037 break;
2038
2039 msg_putchar_attr(c, attr);
2040 col++;
2041 }
2042 msg_clr_eos();
2043}
2044
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045/*
2046 * Use screen_puts() to output one multi-byte character.
2047 * Return the pointer "s" advanced to the next character.
2048 */
2049 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002050screen_puts_mbyte(char_u *s, int l, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051{
2052 int cw;
2053
Bram Moolenaar85a20022019-12-21 18:25:54 +01002054 msg_didout = TRUE; // remember that line is not empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 cw = (*mb_ptr2cells)(s);
2056 if (cw > 1 && (
2057#ifdef FEAT_RIGHTLEFT
2058 cmdmsg_rl ? msg_col <= 1 :
2059#endif
2060 msg_col == Columns - 1))
2061 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002062 // Doesn't fit, print a highlighted '>' to fill it up.
Bram Moolenaar8820b482017-03-16 17:23:31 +01002063 msg_screen_putchar('>', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 return s;
2065 }
2066
2067 screen_puts_len(s, l, msg_row, msg_col, attr);
2068#ifdef FEAT_RIGHTLEFT
2069 if (cmdmsg_rl)
2070 {
2071 msg_col -= cw;
2072 if (msg_col == 0)
2073 {
2074 msg_col = Columns;
2075 ++msg_row;
2076 }
2077 }
2078 else
2079#endif
2080 {
2081 msg_col += cw;
2082 if (msg_col >= Columns)
2083 {
2084 msg_col = 0;
2085 ++msg_row;
2086 }
2087 }
2088 return s + l;
2089}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090
2091/*
2092 * Output a string to the screen at position msg_row, msg_col.
2093 * Update msg_row and msg_col for the next message.
2094 */
2095 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002096msg_puts(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097{
Bram Moolenaaraeac9002016-09-06 22:15:08 +02002098 msg_puts_attr(s, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099}
2100
2101 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002102msg_puts_title(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103{
Bram Moolenaar8820b482017-03-16 17:23:31 +01002104 msg_puts_attr(s, HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105}
2106
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107/*
2108 * Show a message in such a way that it always fits in the line. Cut out a
2109 * part in the middle and replace it with "..." when necessary.
2110 * Does not handle multi-byte characters!
2111 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002112 static void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002113msg_outtrans_long_len_attr(char_u *longstr, int len, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114{
2115 int slen = len;
2116 int room;
2117
2118 room = Columns - msg_col;
2119 if (len > room && room >= 20)
2120 {
2121 slen = (room - 3) / 2;
2122 msg_outtrans_len_attr(longstr, slen, attr);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002123 msg_puts_attr("...", HL_ATTR(HLF_8));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 }
2125 msg_outtrans_len_attr(longstr + len - slen, slen, attr);
2126}
2127
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002128 void
2129msg_outtrans_long_attr(char_u *longstr, int attr)
2130{
2131 msg_outtrans_long_len_attr(longstr, (int)STRLEN(longstr), attr);
2132}
2133
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134/*
2135 * Basic function for writing a message with highlight attributes.
2136 */
2137 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002138msg_puts_attr(char *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139{
2140 msg_puts_attr_len(s, -1, attr);
2141}
2142
2143/*
2144 * Like msg_puts_attr(), but with a maximum length "maxlen" (in bytes).
2145 * When "maxlen" is -1 there is no maximum length.
2146 * When "maxlen" is >= 0 the message is not put in the history.
2147 */
2148 static void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002149msg_puts_attr_len(char *str, int maxlen, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 /*
2152 * If redirection is on, also write to the redirection file.
2153 */
Bram Moolenaar32526b32019-01-19 17:43:09 +01002154 redir_write((char_u *)str, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155
2156 /*
2157 * Don't print anything when using ":silent cmd".
2158 */
2159 if (msg_silent != 0)
2160 return;
2161
Bram Moolenaar85a20022019-12-21 18:25:54 +01002162 // if MSG_HIST flag set, add message to history
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 if ((attr & MSG_HIST) && maxlen < 0)
2164 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002165 add_msg_hist((char_u *)str, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 attr &= ~MSG_HIST;
2167 }
2168
Bram Moolenaare8070982019-10-12 17:07:06 +02002169 // When writing something to the screen after it has scrolled, requires a
2170 // wait-return prompt later. Needed when scrolling, resetting
2171 // need_wait_return after some prompt, and then outputting something
2172 // without scrolling
2173 // Not needed when only using CR to move the cursor.
2174 if (msg_scrolled != 0 && !msg_scrolled_ign && STRCMP(str, "\r") != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 need_wait_return = TRUE;
Bram Moolenaare8070982019-10-12 17:07:06 +02002176 msg_didany = TRUE; // remember that something was outputted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177
2178 /*
2179 * If there is no valid screen, use fprintf so we can see error messages.
2180 * If termcap is not active, we may be writing in an alternate console
2181 * window, cursor positioning may not work correctly (window size may be
2182 * different, e.g. for Win32 console) or we just don't know where the
2183 * cursor is.
2184 */
2185 if (msg_use_printf())
Bram Moolenaar32526b32019-01-19 17:43:09 +01002186 msg_puts_printf((char_u *)str, maxlen);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002187 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01002188 msg_puts_display((char_u *)str, maxlen, attr, FALSE);
Rob Pilling726f7f92022-01-20 14:44:38 +00002189
2190 need_fileinfo = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002191}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002193/*
2194 * The display part of msg_puts_attr_len().
2195 * May be called recursively to display scroll-back text.
2196 */
2197 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002198msg_puts_display(
2199 char_u *str,
2200 int maxlen,
2201 int attr,
2202 int recurse)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002203{
2204 char_u *s = str;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002205 char_u *t_s = str; // string from "t_s" to "s" is still todo
2206 int t_col = 0; // screen cells todo, 0 when "t_s" not used
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002207 int l;
2208 int cw;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002209 char_u *sb_str = str;
2210 int sb_col = msg_col;
2211 int wrap;
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002212 int did_last_char;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213
2214 did_wait_return = FALSE;
Bram Moolenaar57b7fe82007-08-05 17:20:43 +00002215 while ((maxlen < 0 || (int)(s - str) < maxlen) && *s != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 {
2217 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002218 * We are at the end of the screen line when:
2219 * - When outputting a newline.
2220 * - When outputting a character in the last column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002222 if (!recurse && msg_row >= Rows - 1 && (*s == '\n' || (
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223#ifdef FEAT_RIGHTLEFT
2224 cmdmsg_rl
2225 ? (
2226 msg_col <= 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002227 || (*s == TAB && msg_col <= 7)
2228 || (has_mbyte && (*mb_ptr2cells)(s) > 1 && msg_col <= 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 :
2230#endif
Bram Moolenaar0efd1bd2019-12-11 19:00:04 +01002231 ((*s != '\r' && msg_col + t_col >= Columns - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 || (*s == TAB && msg_col + t_col >= ((Columns - 1) & ~7))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 || (has_mbyte && (*mb_ptr2cells)(s) > 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002234 && msg_col + t_col >= Columns - 2)))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002236 /*
2237 * The screen is scrolled up when at the last row (some terminals
2238 * scroll automatically, some don't. To avoid problems we scroll
2239 * ourselves).
2240 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241 if (t_col > 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002242 // output postponed text
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002243 t_puts(&t_col, t_s, s, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244
Bram Moolenaar85a20022019-12-21 18:25:54 +01002245 // When no more prompt and no more room, truncate here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 if (msg_no_more && lines_left == 0)
2247 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248
Bram Moolenaar85a20022019-12-21 18:25:54 +01002249 // Scroll the screen up one line.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002250 msg_scroll_up();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251
2252 msg_row = Rows - 2;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002253 if (msg_col >= Columns) // can happen after screen resize
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 msg_col = Columns - 1;
2255
Bram Moolenaar85a20022019-12-21 18:25:54 +01002256 // Display char in last column before showing more-prompt.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002257 if (*s >= ' '
2258#ifdef FEAT_RIGHTLEFT
2259 && !cmdmsg_rl
2260#endif
2261 )
2262 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002263 if (has_mbyte)
2264 {
2265 if (enc_utf8 && maxlen >= 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002266 // avoid including composing chars after the end
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002267 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002268 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002269 l = (*mb_ptr2len)(s);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002270 s = screen_puts_mbyte(s, l, attr);
2271 }
2272 else
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002273 msg_screen_putchar(*s++, attr);
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002274 did_last_char = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002275 }
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002276 else
2277 did_last_char = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002278
2279 if (p_more)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002280 // store text for scrolling back
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002281 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
2282
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002283 inc_msg_scrolled();
Bram Moolenaar85a20022019-12-21 18:25:54 +01002284 need_wait_return = TRUE; // may need wait_return in main()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 redraw_cmdline = TRUE;
2286 if (cmdline_row > 0 && !exmode_active)
2287 --cmdline_row;
2288
2289 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002290 * If screen is completely filled and 'more' is set then wait
2291 * for a character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 */
Bram Moolenaar1b0b07f2007-08-07 20:00:31 +00002293 if (lines_left > 0)
2294 --lines_left;
Bram Moolenaar24959102022-05-07 20:01:16 +01002295 if (p_more && lines_left == 0 && State != MODE_HITRETURN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 && !msg_no_more && !exmode_active)
2297 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298#ifdef FEAT_CON_DIALOG
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002299 if (do_more_prompt(NUL))
2300 s = confirm_msg_tail;
2301#else
2302 (void)do_more_prompt(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303#endif
2304 if (quit_more)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002305 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002307
Bram Moolenaar85a20022019-12-21 18:25:54 +01002308 // When we displayed a char in last column need to check if there
2309 // is still more.
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002310 if (did_last_char)
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002311 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 }
2313
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002314 wrap = *s == '\n'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 || msg_col + t_col >= Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 || (has_mbyte && (*mb_ptr2cells)(s) > 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002317 && msg_col + t_col >= Columns - 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002318 if (t_col > 0 && (wrap || *s == '\r' || *s == '\b'
2319 || *s == '\t' || *s == BELL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002320 // output any postponed text
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002321 t_puts(&t_col, t_s, s, attr);
2322
2323 if (wrap && p_more && !recurse)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002324 // store text for scrolling back
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002325 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326
Bram Moolenaar85a20022019-12-21 18:25:54 +01002327 if (*s == '\n') // go to next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002329 msg_didout = FALSE; // remember that line is empty
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002330#ifdef FEAT_RIGHTLEFT
2331 if (cmdmsg_rl)
2332 msg_col = Columns - 1;
2333 else
2334#endif
2335 msg_col = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002336 if (++msg_row >= Rows) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 msg_row = Rows - 1;
2338 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002339 else if (*s == '\r') // go to column 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 {
2341 msg_col = 0;
2342 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002343 else if (*s == '\b') // go to previous char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 {
2345 if (msg_col)
2346 --msg_col;
2347 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002348 else if (*s == TAB) // translate Tab into spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 {
2350 do
2351 msg_screen_putchar(' ', attr);
2352 while (msg_col & 7);
2353 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002354 else if (*s == BELL) // beep (from ":sh")
Bram Moolenaar165bc692015-07-21 17:53:25 +02002355 vim_beep(BO_SH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 else
2357 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 if (has_mbyte)
2359 {
2360 cw = (*mb_ptr2cells)(s);
2361 if (enc_utf8 && maxlen >= 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002362 // avoid including composing chars after the end
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002363 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002365 l = (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 }
2367 else
2368 {
2369 cw = 1;
2370 l = 1;
2371 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002372
Bram Moolenaar85a20022019-12-21 18:25:54 +01002373 // When drawing from right to left or when a double-wide character
2374 // doesn't fit, draw a single character here. Otherwise collect
2375 // characters and draw them all at once later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 if (
2377# ifdef FEAT_RIGHTLEFT
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002378 cmdmsg_rl ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379# endif
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002380 (cw > 1 && msg_col + t_col >= Columns - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 if (l > 1)
2383 s = screen_puts_mbyte(s, l, attr) - 1;
2384 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 msg_screen_putchar(*s, attr);
2386 }
2387 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002389 // postpone this character until later
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 if (t_col == 0)
2391 t_s = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 t_col += cw;
2393 s += l - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 }
2395 }
2396 ++s;
2397 }
2398
Bram Moolenaar85a20022019-12-21 18:25:54 +01002399 // output any postponed text
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 if (t_col > 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002401 t_puts(&t_col, t_s, s, attr);
2402 if (p_more && !recurse)
2403 store_sb_text(&sb_str, s, attr, &sb_col, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404
2405 msg_check();
2406}
2407
2408/*
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002409 * Return TRUE when ":filter pattern" was used and "msg" does not match
2410 * "pattern".
2411 */
2412 int
2413message_filtered(char_u *msg)
2414{
Bram Moolenaard29459b2016-08-26 22:29:11 +02002415 int match;
2416
Bram Moolenaare1004402020-10-24 20:49:43 +02002417 if (cmdmod.cmod_filter_regmatch.regprog == NULL)
Bram Moolenaard29459b2016-08-26 22:29:11 +02002418 return FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02002419 match = vim_regexec(&cmdmod.cmod_filter_regmatch, msg, (colnr_T)0);
2420 return cmdmod.cmod_filter_force ? match : !match;
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002421}
2422
2423/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002424 * Scroll the screen up one line for displaying the next message line.
2425 */
2426 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002427msg_scroll_up(void)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002428{
2429#ifdef FEAT_GUI
Bram Moolenaar85a20022019-12-21 18:25:54 +01002430 // Remove the cursor before scrolling, ScreenLines[] is going
2431 // to become invalid.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002432 if (gui.in_use)
2433 gui_undraw_cursor();
2434#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01002435 // scrolling up always works
Bram Moolenaara338adc2018-01-31 20:51:47 +01002436 mch_disable_flush();
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002437 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaara338adc2018-01-31 20:51:47 +01002438 mch_enable_flush();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002439
2440 if (!can_clear((char_u *)" "))
2441 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002442 // Scrolling up doesn't result in the right background. Set the
2443 // background here. It's not efficient, but avoids that we have to do
2444 // it all over the code.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002445 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2446
Bram Moolenaar85a20022019-12-21 18:25:54 +01002447 // Also clear the last char of the last but one line if it was not
2448 // cleared before to avoid a scroll-up.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002449 if (ScreenAttrs[LineOffset[Rows - 2] + Columns - 1] == (sattr_T)-1)
2450 screen_fill((int)Rows - 2, (int)Rows - 1,
2451 (int)Columns - 1, (int)Columns, ' ', ' ', 0);
2452 }
2453}
2454
2455/*
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002456 * Increment "msg_scrolled".
2457 */
2458 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002459inc_msg_scrolled(void)
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002460{
2461#ifdef FEAT_EVAL
2462 if (*get_vim_var_str(VV_SCROLLSTART) == NUL)
2463 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002464 char_u *p = SOURCING_NAME;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002465 char_u *tofree = NULL;
2466 int len;
2467
Bram Moolenaar85a20022019-12-21 18:25:54 +01002468 // v:scrollstart is empty, set it to the script/function name and line
2469 // number
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002470 if (p == NULL)
2471 p = (char_u *)_("Unknown");
2472 else
2473 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002474 len = (int)STRLEN(p) + 40;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002475 tofree = alloc(len);
2476 if (tofree != NULL)
2477 {
2478 vim_snprintf((char *)tofree, len, _("%s line %ld"),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002479 p, (long)SOURCING_LNUM);
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002480 p = tofree;
2481 }
2482 }
2483 set_vim_var_string(VV_SCROLLSTART, p, -1);
2484 vim_free(tofree);
2485 }
2486#endif
2487 ++msg_scrolled;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002488 if (must_redraw < UPD_VALID)
2489 must_redraw = UPD_VALID;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002490}
2491
2492/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002493 * To be able to scroll back at the "more" and "hit-enter" prompts we need to
2494 * store the displayed text and remember where screen lines start.
2495 */
2496typedef struct msgchunk_S msgchunk_T;
2497struct msgchunk_S
2498{
2499 msgchunk_T *sb_next;
2500 msgchunk_T *sb_prev;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002501 char sb_eol; // TRUE when line ends after this text
2502 int sb_msg_col; // column in which text starts
2503 int sb_attr; // text attributes
2504 char_u sb_text[1]; // text to be displayed, actually longer
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002505};
2506
Bram Moolenaar85a20022019-12-21 18:25:54 +01002507static msgchunk_T *last_msgchunk = NULL; // last displayed text
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002508
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002509static msgchunk_T *msg_sb_start(msgchunk_T *mps);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002510
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002511typedef enum {
2512 SB_CLEAR_NONE = 0,
2513 SB_CLEAR_ALL,
2514 SB_CLEAR_CMDLINE_BUSY,
2515 SB_CLEAR_CMDLINE_DONE
2516} sb_clear_T;
2517
Bram Moolenaar85a20022019-12-21 18:25:54 +01002518// When to clear text on next msg.
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002519static sb_clear_T do_clear_sb_text = SB_CLEAR_NONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002520
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002521/*
2522 * Store part of a printed message for displaying when scrolling back.
2523 */
2524 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002525store_sb_text(
Bram Moolenaar85a20022019-12-21 18:25:54 +01002526 char_u **sb_str, // start of string
2527 char_u *s, // just after string
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002528 int attr,
2529 int *sb_col,
Bram Moolenaar85a20022019-12-21 18:25:54 +01002530 int finish) // line ends
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002531{
2532 msgchunk_T *mp;
2533
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002534 if (do_clear_sb_text == SB_CLEAR_ALL
2535 || do_clear_sb_text == SB_CLEAR_CMDLINE_DONE)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002536 {
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002537 clear_sb_text(do_clear_sb_text == SB_CLEAR_ALL);
zeertzjq46af7bc2022-07-28 12:34:09 +01002538 msg_sb_eol(); // prevent messages from overlapping
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002539 do_clear_sb_text = SB_CLEAR_NONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002540 }
2541
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002542 if (s > *sb_str)
2543 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002544 mp = alloc(sizeof(msgchunk_T) + (s - *sb_str));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002545 if (mp != NULL)
2546 {
2547 mp->sb_eol = finish;
2548 mp->sb_msg_col = *sb_col;
2549 mp->sb_attr = attr;
2550 vim_strncpy(mp->sb_text, *sb_str, s - *sb_str);
2551
2552 if (last_msgchunk == NULL)
2553 {
2554 last_msgchunk = mp;
2555 mp->sb_prev = NULL;
2556 }
2557 else
2558 {
2559 mp->sb_prev = last_msgchunk;
2560 last_msgchunk->sb_next = mp;
2561 last_msgchunk = mp;
2562 }
2563 mp->sb_next = NULL;
2564 }
2565 }
2566 else if (finish && last_msgchunk != NULL)
2567 last_msgchunk->sb_eol = TRUE;
2568
2569 *sb_str = s;
2570 *sb_col = 0;
2571}
2572
2573/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002574 * Finished showing messages, clear the scroll-back text on the next message.
2575 */
2576 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002577may_clear_sb_text(void)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002578{
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002579 do_clear_sb_text = SB_CLEAR_ALL;
2580}
2581
2582/*
zeertzjq46af7bc2022-07-28 12:34:09 +01002583 * Starting to edit the command line: do not clear messages now.
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002584 */
2585 void
2586sb_text_start_cmdline(void)
2587{
zeertzjq46af7bc2022-07-28 12:34:09 +01002588 if (do_clear_sb_text == SB_CLEAR_CMDLINE_BUSY)
2589 // Invoking command line recursively: the previous-level command line
2590 // doesn't need to be remembered as it will be redrawn when returning
2591 // to that level.
2592 sb_text_restart_cmdline();
2593 else
2594 {
2595 msg_sb_eol();
2596 do_clear_sb_text = SB_CLEAR_CMDLINE_BUSY;
2597 }
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002598}
2599
2600/*
zeertzjq46af7bc2022-07-28 12:34:09 +01002601 * Redrawing the command line: clear the last unfinished line.
2602 */
2603 void
2604sb_text_restart_cmdline(void)
2605{
2606 msgchunk_T *tofree;
2607
2608 // Needed when returning from nested command line.
2609 do_clear_sb_text = SB_CLEAR_CMDLINE_BUSY;
2610
2611 if (last_msgchunk == NULL || last_msgchunk->sb_eol)
2612 // No unfinished line: don't clear anything.
2613 return;
2614
2615 tofree = msg_sb_start(last_msgchunk);
2616 last_msgchunk = tofree->sb_prev;
2617 if (last_msgchunk != NULL)
2618 last_msgchunk->sb_next = NULL;
2619 while (tofree != NULL)
2620 {
2621 msgchunk_T *tofree_next = tofree->sb_next;
2622
2623 vim_free(tofree);
2624 tofree = tofree_next;
2625 }
2626}
2627
2628/*
2629 * Ending to edit the command line: clear old lines but the last one later.
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002630 */
2631 void
2632sb_text_end_cmdline(void)
2633{
2634 do_clear_sb_text = SB_CLEAR_CMDLINE_DONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002635}
2636
2637/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002638 * Clear any text remembered for scrolling back.
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002639 * When "all" is FALSE keep the last line.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002640 * Called when redrawing the screen.
2641 */
2642 void
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002643clear_sb_text(int all)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002644{
2645 msgchunk_T *mp;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002646 msgchunk_T **lastp;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002647
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002648 if (all)
2649 lastp = &last_msgchunk;
2650 else
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002651 {
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002652 if (last_msgchunk == NULL)
2653 return;
zeertzjqecdc82e2022-07-25 19:50:57 +01002654 lastp = &msg_sb_start(last_msgchunk)->sb_prev;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002655 }
2656
2657 while (*lastp != NULL)
2658 {
2659 mp = (*lastp)->sb_prev;
2660 vim_free(*lastp);
2661 *lastp = mp;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002662 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002663}
2664
2665/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002666 * "g<" command.
2667 */
2668 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002669show_sb_text(void)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002670{
2671 msgchunk_T *mp;
2672
Bram Moolenaar85a20022019-12-21 18:25:54 +01002673 // Only show something if there is more than one line, otherwise it looks
2674 // weird, typing a command without output results in one line.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002675 mp = msg_sb_start(last_msgchunk);
2676 if (mp == NULL || mp->sb_prev == NULL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02002677 vim_beep(BO_MESS);
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002678 else
2679 {
2680 do_more_prompt('G');
2681 wait_return(FALSE);
2682 }
2683}
2684
2685/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002686 * Move to the start of screen line in already displayed text.
2687 */
2688 static msgchunk_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002689msg_sb_start(msgchunk_T *mps)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002690{
2691 msgchunk_T *mp = mps;
2692
2693 while (mp != NULL && mp->sb_prev != NULL && !mp->sb_prev->sb_eol)
2694 mp = mp->sb_prev;
2695 return mp;
2696}
2697
2698/*
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02002699 * Mark the last message chunk as finishing the line.
2700 */
2701 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002702msg_sb_eol(void)
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02002703{
2704 if (last_msgchunk != NULL)
2705 last_msgchunk->sb_eol = TRUE;
2706}
2707
2708/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002709 * Display a screen line from previously displayed text at row "row".
2710 * Returns a pointer to the text for the next line (can be NULL).
2711 */
2712 static msgchunk_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002713disp_sb_line(int row, msgchunk_T *smp)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002714{
2715 msgchunk_T *mp = smp;
2716 char_u *p;
2717
2718 for (;;)
2719 {
2720 msg_row = row;
2721 msg_col = mp->sb_msg_col;
2722 p = mp->sb_text;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002723 if (*p == '\n') // don't display the line break
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002724 ++p;
2725 msg_puts_display(p, -1, mp->sb_attr, TRUE);
2726 if (mp->sb_eol || mp->sb_next == NULL)
2727 break;
2728 mp = mp->sb_next;
2729 }
2730 return mp->sb_next;
2731}
2732
2733/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 * Output any postponed text for msg_puts_attr_len().
2735 */
2736 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002737t_puts(
2738 int *t_col,
2739 char_u *t_s,
2740 char_u *s,
2741 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002743 // output postponed text
2744 msg_didout = TRUE; // remember that line is not empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745 screen_puts_len(t_s, (int)(s - t_s), msg_row, msg_col, attr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002746 msg_col += *t_col;
2747 *t_col = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002748 // If the string starts with a composing character don't increment the
2749 // column position for it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750 if (enc_utf8 && utf_iscomposing(utf_ptr2char(t_s)))
2751 --msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 if (msg_col >= Columns)
2753 {
2754 msg_col = 0;
2755 ++msg_row;
2756 }
2757}
2758
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759/*
2760 * Returns TRUE when messages should be printed with mch_errmsg().
2761 * This is used when there is no valid screen, so we can see error messages.
2762 * If termcap is not active, we may be writing in an alternate console
2763 * window, cursor positioning may not work correctly (window size may be
2764 * different, e.g. for Win32 console) or we just don't know where the
2765 * cursor is.
2766 */
2767 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002768msg_use_printf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769{
2770 return (!msg_check_screen()
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002771#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
2772# ifdef VIMDLL
2773 || (!gui.in_use && !termcap_active)
2774# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 || !termcap_active
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002776# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777#endif
2778 || (swapping_screen() && !termcap_active)
2779 );
2780}
2781
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002782/*
2783 * Print a message when there is no valid screen.
2784 */
2785 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002786msg_puts_printf(char_u *str, int maxlen)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002787{
2788 char_u *s = str;
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002789 char_u *buf = NULL;
2790 char_u *p = s;
2791
Bram Moolenaar4f974752019-02-17 17:44:42 +01002792#ifdef MSWIN
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002793 if (!(silent_mode && p_verbose == 0))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002794 mch_settmode(TMODE_COOK); // handle CR and NL correctly
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002795#endif
Bram Moolenaar2321ca22016-09-09 14:17:18 +02002796 while ((maxlen < 0 || (int)(s - str) < maxlen) && *s != NUL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002797 {
2798 if (!(silent_mode && p_verbose == 0))
2799 {
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002800 // NL --> CR NL translation (for Unix, not for "--version")
2801 if (*s == NL)
2802 {
2803 int n = (int)(s - p);
2804
2805 buf = alloc(n + 3);
Bram Moolenaar6f10c702019-08-20 22:58:37 +02002806 if (buf != NULL)
2807 {
2808 memcpy(buf, p, n);
2809 if (!info_message)
2810 buf[n++] = CAR;
2811 buf[n++] = NL;
2812 buf[n++] = NUL;
2813 if (info_message) // informative message, not an error
2814 mch_msg((char *)buf);
2815 else
2816 mch_errmsg((char *)buf);
2817 vim_free(buf);
2818 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002819 p = s + 1;
2820 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002821 }
2822
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002823 // primitive way to compute the current column
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002824#ifdef FEAT_RIGHTLEFT
2825 if (cmdmsg_rl)
2826 {
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002827 if (*s == CAR || *s == NL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002828 msg_col = Columns - 1;
2829 else
2830 --msg_col;
2831 }
2832 else
2833#endif
2834 {
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002835 if (*s == CAR || *s == NL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002836 msg_col = 0;
2837 else
2838 ++msg_col;
2839 }
2840 ++s;
2841 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002842
2843 if (*p != NUL && !(silent_mode && p_verbose == 0))
2844 {
Bram Moolenaara97c3632021-06-15 22:39:11 +02002845 char_u *tofree = NULL;
Bram Moolenaar97c2c052019-02-22 13:42:07 +01002846
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002847 if (maxlen > 0 && STRLEN(p) > (size_t)maxlen)
Bram Moolenaar97c2c052019-02-22 13:42:07 +01002848 {
Bram Moolenaara97c3632021-06-15 22:39:11 +02002849 tofree = vim_strnsave(p, (size_t)maxlen);
2850 p = tofree;
Bram Moolenaar97c2c052019-02-22 13:42:07 +01002851 }
Bram Moolenaara97c3632021-06-15 22:39:11 +02002852 if (p != NULL)
2853 {
2854 if (info_message)
2855 mch_msg((char *)p);
2856 else
2857 mch_errmsg((char *)p);
2858 vim_free(tofree);
2859 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002860 }
2861
2862 msg_didout = TRUE; // assume that line is not empty
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002863
Bram Moolenaar4f974752019-02-17 17:44:42 +01002864#ifdef MSWIN
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002865 if (!(silent_mode && p_verbose == 0))
2866 mch_settmode(TMODE_RAW);
2867#endif
2868}
2869
2870/*
2871 * Show the more-prompt and handle the user response.
2872 * This takes care of scrolling back and displaying previously displayed text.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002873 * When at hit-enter prompt "typed_char" is the already typed character,
2874 * otherwise it's NUL.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002875 * Returns TRUE when jumping ahead to "confirm_msg_tail".
2876 */
2877 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002878do_more_prompt(int typed_char)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002879{
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002880 static int entered = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002881 int used_typed_char = typed_char;
2882 int oldState = State;
2883 int c;
2884#ifdef FEAT_CON_DIALOG
2885 int retval = FALSE;
2886#endif
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002887 int toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002888 msgchunk_T *mp_last = NULL;
2889 msgchunk_T *mp;
2890 int i;
2891
Bram Moolenaar85a20022019-12-21 18:25:54 +01002892 // We get called recursively when a timer callback outputs a message. In
2893 // that case don't show another prompt. Also when at the hit-Enter prompt
2894 // and nothing was typed.
Bram Moolenaar24959102022-05-07 20:01:16 +01002895 if (entered || (State == MODE_HITRETURN && typed_char == 0))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002896 return FALSE;
2897 entered = TRUE;
2898
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002899 if (typed_char == 'G')
2900 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002901 // "g<": Find first line on the last page.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002902 mp_last = msg_sb_start(last_msgchunk);
2903 for (i = 0; i < Rows - 2 && mp_last != NULL
2904 && mp_last->sb_prev != NULL; ++i)
2905 mp_last = msg_sb_start(mp_last->sb_prev);
2906 }
2907
Bram Moolenaar24959102022-05-07 20:01:16 +01002908 State = MODE_ASKMORE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002909 setmouse();
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002910 if (typed_char == NUL)
2911 msg_moremsg(FALSE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002912 for (;;)
2913 {
2914 /*
2915 * Get a typed character directly from the user.
2916 */
2917 if (used_typed_char != NUL)
2918 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002919 c = used_typed_char; // was typed at hit-enter prompt
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002920 used_typed_char = NUL;
2921 }
2922 else
2923 c = get_keystroke();
2924
2925#if defined(FEAT_MENU) && defined(FEAT_GUI)
2926 if (c == K_MENU)
2927 {
Bram Moolenaar24959102022-05-07 20:01:16 +01002928 int idx = get_menu_index(current_menu, MODE_ASKMORE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002929
Bram Moolenaar85a20022019-12-21 18:25:54 +01002930 // Used a menu. If it starts with CTRL-Y, it must
2931 // be a "Copy" for the clipboard. Otherwise
2932 // assume that we end
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002933 if (idx == MENU_INDEX_INVALID)
2934 continue;
2935 c = *current_menu->strings[idx];
2936 if (c != NUL && current_menu->strings[idx][1] != NUL)
2937 ins_typebuf(current_menu->strings[idx] + 1,
2938 current_menu->noremap[idx], 0, TRUE,
2939 current_menu->silent[idx]);
2940 }
2941#endif
2942
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002943 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002944 switch (c)
2945 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002946 case BS: // scroll one line back
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002947 case K_BS:
2948 case 'k':
2949 case K_UP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002950 toscroll = -1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002951 break;
2952
Bram Moolenaar85a20022019-12-21 18:25:54 +01002953 case CAR: // one extra line
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002954 case NL:
2955 case 'j':
2956 case K_DOWN:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002957 toscroll = 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002958 break;
2959
Bram Moolenaar85a20022019-12-21 18:25:54 +01002960 case 'u': // Up half a page
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002961 toscroll = -(Rows / 2);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002962 break;
2963
Bram Moolenaar85a20022019-12-21 18:25:54 +01002964 case 'd': // Down half a page
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002965 toscroll = Rows / 2;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002966 break;
2967
Bram Moolenaar85a20022019-12-21 18:25:54 +01002968 case 'b': // one page back
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00002969 case K_PAGEUP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002970 toscroll = -(Rows - 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002971 break;
2972
Bram Moolenaar85a20022019-12-21 18:25:54 +01002973 case ' ': // one extra page
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00002974 case 'f':
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002975 case K_PAGEDOWN:
2976 case K_LEFTMOUSE:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002977 toscroll = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002978 break;
2979
Bram Moolenaar85a20022019-12-21 18:25:54 +01002980 case 'g': // all the way back to the start
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002981 toscroll = -999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002982 break;
2983
Bram Moolenaar85a20022019-12-21 18:25:54 +01002984 case 'G': // all the way to the end
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002985 toscroll = 999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002986 lines_left = 999999;
2987 break;
2988
Bram Moolenaar85a20022019-12-21 18:25:54 +01002989 case ':': // start new command line
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002990#ifdef FEAT_CON_DIALOG
2991 if (!confirm_msg_used)
2992#endif
2993 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002994 // Since got_int is set all typeahead will be flushed, but we
2995 // want to keep this ':', remember that in a special way.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002996 typeahead_noflush(':');
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02002997#ifdef FEAT_TERMINAL
2998 skip_term_loop = TRUE;
2999#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01003000 cmdline_row = Rows - 1; // put ':' on this line
3001 skip_redraw = TRUE; // skip redraw once
3002 need_wait_return = FALSE; // don't wait in main()
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003003 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003004 // FALLTHROUGH
3005 case 'q': // quit
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003006 case Ctrl_C:
3007 case ESC:
3008#ifdef FEAT_CON_DIALOG
3009 if (confirm_msg_used)
3010 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003011 // Jump to the choices of the dialog.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003012 retval = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003013 }
3014 else
3015#endif
3016 {
3017 got_int = TRUE;
3018 quit_more = TRUE;
3019 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003020 // When there is some more output (wrapping line) display that
3021 // without another prompt.
Bram Moolenaar51306d22009-02-24 03:38:04 +00003022 lines_left = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003023 break;
3024
3025#ifdef FEAT_CLIPBOARD
3026 case Ctrl_Y:
Bram Moolenaar85a20022019-12-21 18:25:54 +01003027 // Strange way to allow copying (yanking) a modeless
3028 // selection at the more prompt. Use CTRL-Y,
3029 // because the same is used in Cmdline-mode and at the
3030 // hit-enter prompt. However, scrolling one line up
3031 // might be expected...
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003032 if (clip_star.state == SELECT_DONE)
3033 clip_copy_modeless_selection(TRUE);
3034 continue;
3035#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01003036 default: // no valid response
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003037 msg_moremsg(TRUE);
3038 continue;
3039 }
3040
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003041 if (toscroll != 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003042 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003043 if (toscroll < 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003044 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003045 // go to start of last line
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003046 if (mp_last == NULL)
3047 mp = msg_sb_start(last_msgchunk);
3048 else if (mp_last->sb_prev != NULL)
3049 mp = msg_sb_start(mp_last->sb_prev);
3050 else
3051 mp = NULL;
3052
Bram Moolenaar85a20022019-12-21 18:25:54 +01003053 // go to start of line at top of the screen
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003054 for (i = 0; i < Rows - 2 && mp != NULL && mp->sb_prev != NULL;
3055 ++i)
3056 mp = msg_sb_start(mp->sb_prev);
3057
3058 if (mp != NULL && mp->sb_prev != NULL)
3059 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003060 // Find line to be displayed at top.
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003061 for (i = 0; i > toscroll; --i)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003062 {
3063 if (mp == NULL || mp->sb_prev == NULL)
3064 break;
3065 mp = msg_sb_start(mp->sb_prev);
3066 if (mp_last == NULL)
3067 mp_last = msg_sb_start(last_msgchunk);
3068 else
3069 mp_last = msg_sb_start(mp_last->sb_prev);
3070 }
3071
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003072 if (toscroll == -1 && screen_ins_lines(0, 0, 1,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003073 (int)Rows, 0, NULL) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003074 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003075 // display line at top
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003076 (void)disp_sb_line(0, mp);
3077 }
3078 else
3079 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003080 // redisplay all lines
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003081 screenclear();
Bram Moolenaar773560b2006-05-06 21:38:18 +00003082 for (i = 0; mp != NULL && i < Rows - 1; ++i)
3083 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003084 mp = disp_sb_line(i, mp);
Bram Moolenaar773560b2006-05-06 21:38:18 +00003085 ++msg_scrolled;
3086 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003087 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003088 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003089 }
3090 }
3091 else
3092 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003093 // First display any text that we scrolled back.
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003094 while (toscroll > 0 && mp_last != NULL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003095 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003096 // scroll up, display line at bottom
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003097 msg_scroll_up();
Bram Moolenaarbb15b652005-10-03 21:52:09 +00003098 inc_msg_scrolled();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003099 screen_fill((int)Rows - 2, (int)Rows - 1, 0,
3100 (int)Columns, ' ', ' ', 0);
3101 mp_last = disp_sb_line((int)Rows - 2, mp_last);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003102 --toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003103 }
3104 }
3105
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003106 if (toscroll <= 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003107 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003108 // displayed the requested text, more prompt again
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003109 screen_fill((int)Rows - 1, (int)Rows, 0,
3110 (int)Columns, ' ', ' ', 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003111 msg_moremsg(FALSE);
3112 continue;
3113 }
3114
Bram Moolenaar85a20022019-12-21 18:25:54 +01003115 // display more text, return to caller
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003116 lines_left = toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003117 }
3118
3119 break;
3120 }
3121
Bram Moolenaar85a20022019-12-21 18:25:54 +01003122 // clear the --more-- message
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003123 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3124 State = oldState;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003125 setmouse();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003126 if (quit_more)
3127 {
3128 msg_row = Rows - 1;
3129 msg_col = 0;
3130 }
3131#ifdef FEAT_RIGHTLEFT
3132 else if (cmdmsg_rl)
3133 msg_col = Columns - 1;
3134#endif
3135
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01003136 entered = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003137#ifdef FEAT_CON_DIALOG
3138 return retval;
3139#else
3140 return FALSE;
3141#endif
3142}
3143
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144#if defined(USE_MCH_ERRMSG) || defined(PROTO)
3145
3146#ifdef mch_errmsg
3147# undef mch_errmsg
3148#endif
3149#ifdef mch_msg
3150# undef mch_msg
3151#endif
3152
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003153#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3154 static void
3155mch_errmsg_c(char *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156{
Bram Moolenaar0f77d6a2019-02-14 20:55:09 +01003157 int len = (int)STRLEN(str);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003158 DWORD nwrite = 0;
3159 DWORD mode = 0;
3160 HANDLE h = GetStdHandle(STD_ERROR_HANDLE);
3161
3162 if (GetConsoleMode(h, &mode) && enc_codepage >= 0
3163 && (int)GetConsoleCP() != enc_codepage)
3164 {
3165 WCHAR *w = enc_to_utf16((char_u *)str, &len);
3166
3167 WriteConsoleW(h, w, len, &nwrite, NULL);
3168 vim_free(w);
3169 }
3170 else
3171 {
3172 fprintf(stderr, "%s", str);
3173 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003174}
3175#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003177/*
3178 * Give an error message. To be used when the screen hasn't been initialized
3179 * yet. When stderr can't be used, collect error messages until the GUI has
3180 * started and they can be displayed in a message box.
3181 */
3182 void
3183mch_errmsg(char *str)
3184{
3185#if !defined(MSWIN) || defined(FEAT_GUI_MSWIN)
3186 int len;
3187#endif
3188
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02003189#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003190 // On Unix use stderr if it's a tty.
3191 // When not going to start the GUI also use stderr.
3192 // On Mac, when started from Finder, stderr is the console.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 if (
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003194# ifdef UNIX
3195# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003197# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 isatty(2)
3199# endif
3200# ifdef FEAT_GUI
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003201 ||
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003202# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003203# endif
3204# ifdef FEAT_GUI
3205 !(gui.in_use || gui.starting)
3206# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 )
3208 {
3209 fprintf(stderr, "%s", str);
3210 return;
3211 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003212#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003214#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3215# ifdef VIMDLL
3216 if (!(gui.in_use || gui.starting))
3217# endif
3218 {
3219 mch_errmsg_c(str);
3220 return;
3221 }
3222#endif
3223
3224#if !defined(MSWIN) || defined(FEAT_GUI_MSWIN)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003225 // avoid a delay for a message that isn't there
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 emsg_on_display = FALSE;
3227
3228 len = (int)STRLEN(str) + 1;
3229 if (error_ga.ga_growsize == 0)
3230 {
3231 error_ga.ga_growsize = 80;
3232 error_ga.ga_itemsize = 1;
3233 }
3234 if (ga_grow(&error_ga, len) == OK)
3235 {
3236 mch_memmove((char_u *)error_ga.ga_data + error_ga.ga_len,
3237 (char_u *)str, len);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003238# ifdef UNIX
Bram Moolenaar85a20022019-12-21 18:25:54 +01003239 // remove CR characters, they are displayed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 {
3241 char_u *p;
3242
3243 p = (char_u *)error_ga.ga_data + error_ga.ga_len;
3244 for (;;)
3245 {
3246 p = vim_strchr(p, '\r');
3247 if (p == NULL)
3248 break;
3249 *p = ' ';
3250 }
3251 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003252# endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01003253 --len; // don't count the NUL at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254 error_ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003256#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257}
3258
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003259#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3260 static void
3261mch_msg_c(char *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262{
Bram Moolenaar0f77d6a2019-02-14 20:55:09 +01003263 int len = (int)STRLEN(str);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003264 DWORD nwrite = 0;
3265 DWORD mode;
3266 HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
3267
3268
3269 if (GetConsoleMode(h, &mode) && enc_codepage >= 0
3270 && (int)GetConsoleCP() != enc_codepage)
3271 {
3272 WCHAR *w = enc_to_utf16((char_u *)str, &len);
3273
3274 WriteConsoleW(h, w, len, &nwrite, NULL);
3275 vim_free(w);
3276 }
3277 else
3278 {
3279 printf("%s", str);
3280 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003281}
3282#endif
3283
3284/*
3285 * Give a message. To be used when the screen hasn't been initialized yet.
3286 * When there is no tty, collect messages until the GUI has started and they
3287 * can be displayed in a message box.
3288 */
3289 void
3290mch_msg(char *str)
3291{
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02003292#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003293 // On Unix use stdout if we have a tty. This allows "vim -h | more" and
3294 // uses mch_errmsg() when started from the desktop.
3295 // When not going to start the GUI also use stdout.
3296 // On Mac, when started from Finder, stderr is the console.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 if (
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003298# ifdef UNIX
3299# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003301# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 isatty(2)
Bram Moolenaareae1b912019-05-09 15:12:55 +02003303# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304# ifdef FEAT_GUI
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003305 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003307# endif
3308# ifdef FEAT_GUI
3309 !(gui.in_use || gui.starting)
3310# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 )
3312 {
3313 printf("%s", str);
3314 return;
3315 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003316#endif
3317
3318#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3319# ifdef VIMDLL
3320 if (!(gui.in_use || gui.starting))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003322 {
3323 mch_msg_c(str);
3324 return;
3325 }
3326#endif
3327#if !defined(MSWIN) || defined(FEAT_GUI_MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 mch_errmsg(str);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003329#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330}
Bram Moolenaar85a20022019-12-21 18:25:54 +01003331#endif // USE_MCH_ERRMSG
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332
3333/*
3334 * Put a character on the screen at the current message position and advance
3335 * to the next position. Only for printable ASCII!
3336 */
3337 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003338msg_screen_putchar(int c, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339{
Bram Moolenaar85a20022019-12-21 18:25:54 +01003340 msg_didout = TRUE; // remember that line is not empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341 screen_putchar(c, msg_row, msg_col, attr);
3342#ifdef FEAT_RIGHTLEFT
3343 if (cmdmsg_rl)
3344 {
3345 if (--msg_col == 0)
3346 {
3347 msg_col = Columns;
3348 ++msg_row;
3349 }
3350 }
3351 else
3352#endif
3353 {
3354 if (++msg_col >= Columns)
3355 {
3356 msg_col = 0;
3357 ++msg_row;
3358 }
3359 }
3360}
3361
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003362 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003363msg_moremsg(int full)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003365 int attr;
3366 char_u *s = (char_u *)_("-- More --");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367
Bram Moolenaar8820b482017-03-16 17:23:31 +01003368 attr = HL_ATTR(HLF_M);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003369 screen_puts(s, (int)Rows - 1, 0, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 if (full)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003371 screen_puts((char_u *)
3372 _(" SPACE/d/j: screen/page/line down, b/u/k: up, q: quit "),
3373 (int)Rows - 1, vim_strsize(s), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374}
3375
3376/*
Bram Moolenaar24959102022-05-07 20:01:16 +01003377 * Repeat the message for the current mode: MODE_ASKMORE, MODE_EXTERNCMD,
3378 * MODE_CONFIRM or exmode_active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 */
3380 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003381repeat_message(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382{
Bram Moolenaar24959102022-05-07 20:01:16 +01003383 if (State == MODE_ASKMORE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003385 msg_moremsg(TRUE); // display --more-- message again
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 msg_row = Rows - 1;
3387 }
3388#ifdef FEAT_CON_DIALOG
Bram Moolenaar24959102022-05-07 20:01:16 +01003389 else if (State == MODE_CONFIRM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003391 display_confirm_msg(); // display ":confirm" message again
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 msg_row = Rows - 1;
3393 }
3394#endif
Bram Moolenaar24959102022-05-07 20:01:16 +01003395 else if (State == MODE_EXTERNCMD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003397 windgoto(msg_row, msg_col); // put cursor back
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 }
Bram Moolenaar24959102022-05-07 20:01:16 +01003399 else if (State == MODE_HITRETURN || State == MODE_SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 {
Bram Moolenaar9f108752007-11-24 14:44:58 +00003401 if (msg_row == Rows - 1)
3402 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003403 // Avoid drawing the "hit-enter" prompt below the previous one,
3404 // overwrite it. Esp. useful when regaining focus and a
3405 // FocusGained autocmd exists but didn't draw anything.
Bram Moolenaar9f108752007-11-24 14:44:58 +00003406 msg_didout = FALSE;
3407 msg_col = 0;
3408 msg_clr_eos();
3409 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 hit_return_msg();
3411 msg_row = Rows - 1;
3412 }
3413}
3414
3415/*
3416 * msg_check_screen - check if the screen is initialized.
3417 * Also check msg_row and msg_col, if they are too big it may cause a crash.
3418 * While starting the GUI the terminal codes will be set for the GUI, but the
3419 * output goes to the terminal. Don't use the terminal codes then.
3420 */
3421 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003422msg_check_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423{
3424 if (!full_screen || !screen_valid(FALSE))
3425 return FALSE;
3426
3427 if (msg_row >= Rows)
3428 msg_row = Rows - 1;
3429 if (msg_col >= Columns)
3430 msg_col = Columns - 1;
3431 return TRUE;
3432}
3433
3434/*
3435 * Clear from current message position to end of screen.
3436 * Skip this when ":silent" was used, no need to clear for redirection.
3437 */
3438 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003439msg_clr_eos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440{
3441 if (msg_silent == 0)
3442 msg_clr_eos_force();
3443}
3444
3445/*
3446 * Clear from current message position to end of screen.
3447 * Note: msg_col is not updated, so we remember the end of the message
3448 * for msg_check().
3449 */
3450 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003451msg_clr_eos_force(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452{
3453 if (msg_use_printf())
3454 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003455 if (full_screen) // only when termcap codes are valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 {
3457 if (*T_CD)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003458 out_str(T_CD); // clear to end of display
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 else if (*T_CE)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003460 out_str(T_CE); // clear to end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 }
3462 }
Shougo Matsushitaf39cfb72022-07-30 16:54:05 +01003463 else if (p_ch > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 {
3465#ifdef FEAT_RIGHTLEFT
3466 if (cmdmsg_rl)
3467 {
3468 screen_fill(msg_row, msg_row + 1, 0, msg_col + 1, ' ', ' ', 0);
3469 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3470 }
3471 else
3472#endif
3473 {
3474 screen_fill(msg_row, msg_row + 1, msg_col, (int)Columns,
3475 ' ', ' ', 0);
3476 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3477 }
3478 }
3479}
3480
3481/*
3482 * Clear the command line.
3483 */
3484 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003485msg_clr_cmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486{
3487 msg_row = cmdline_row;
3488 msg_col = 0;
3489 msg_clr_eos_force();
3490}
3491
3492/*
3493 * end putting a message on the screen
3494 * call wait_return if the message does not fit in the available space
3495 * return TRUE if wait_return not called.
3496 */
3497 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003498msg_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499{
3500 /*
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02003501 * If the string is larger than the window,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 * or the ruler option is set and we run into it,
3503 * we have to redraw the window.
3504 * Do not do this if we are abandoning the file or editing the command line.
3505 */
Bram Moolenaar24959102022-05-07 20:01:16 +01003506 if (!exiting && need_wait_return && !(State & MODE_CMDLINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 {
3508 wait_return(FALSE);
3509 return FALSE;
3510 }
3511 out_flush();
3512 return TRUE;
3513}
3514
3515/*
3516 * If the written message runs into the shown command or ruler, we have to
3517 * wait for hit-return and redraw the window later.
3518 */
3519 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003520msg_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521{
3522 if (msg_row == Rows - 1 && msg_col >= sc_col)
3523 {
3524 need_wait_return = TRUE;
3525 redraw_cmdline = TRUE;
3526 }
3527}
3528
3529/*
3530 * May write a string to the redirection file.
3531 * When "maxlen" is -1 write the whole string, otherwise up to "maxlen" bytes.
3532 */
3533 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003534redir_write(char_u *str, int maxlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535{
3536 char_u *s = str;
3537 static int cur_col = 0;
3538
Bram Moolenaar85a20022019-12-21 18:25:54 +01003539 // Don't do anything for displaying prompts and the like.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003540 if (redir_off)
3541 return;
3542
Bram Moolenaar85a20022019-12-21 18:25:54 +01003543 // If 'verbosefile' is set prepare for writing in that file.
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003544 if (*p_vfile != NUL && verbose_fd == NULL)
3545 verbose_open();
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003546
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003547 if (redirecting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003549 // If the string doesn't start with CR or NL, go to msg_col
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 if (*s != '\n' && *s != '\r')
3551 {
3552 while (cur_col < msg_col)
3553 {
3554#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003555 if (redir_execute)
3556 execute_redir_str((char_u *)" ", -1);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003557 else if (redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 write_reg_contents(redir_reg, (char_u *)" ", -1, TRUE);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003559 else if (redir_vname)
3560 var_redir_str((char_u *)" ", -1);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003561 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003563 if (redir_fd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 fputs(" ", redir_fd);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003565 if (verbose_fd != NULL)
3566 fputs(" ", verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 ++cur_col;
3568 }
3569 }
3570
3571#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003572 if (redir_execute)
3573 execute_redir_str(s, maxlen);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003574 else if (redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 write_reg_contents(redir_reg, s, maxlen, TRUE);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003576 else if (redir_vname)
Bram Moolenaardf177f62005-02-22 08:39:57 +00003577 var_redir_str(s, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578#endif
3579
Bram Moolenaar85a20022019-12-21 18:25:54 +01003580 // Write and adjust the current column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
3582 {
3583#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003584 if (!redir_reg && !redir_vname && !redir_execute)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003586 if (redir_fd != NULL)
3587 putc(*s, redir_fd);
3588 if (verbose_fd != NULL)
3589 putc(*s, verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 if (*s == '\r' || *s == '\n')
3591 cur_col = 0;
3592 else if (*s == '\t')
3593 cur_col += (8 - cur_col % 8);
3594 else
3595 ++cur_col;
3596 ++s;
3597 }
3598
Bram Moolenaar85a20022019-12-21 18:25:54 +01003599 if (msg_silent != 0) // should update msg_col
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 msg_col = cur_col;
3601 }
3602}
3603
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003604 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003605redirecting(void)
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003606{
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003607 return redir_fd != NULL || *p_vfile != NUL
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003608#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003609 || redir_reg || redir_vname || redir_execute
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003610#endif
3611 ;
3612}
3613
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614/*
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00003615 * Before giving verbose message.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003616 * Must always be called paired with verbose_leave()!
3617 */
3618 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003619verbose_enter(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003620{
3621 if (*p_vfile != NUL)
3622 ++msg_silent;
3623}
3624
3625/*
3626 * After giving verbose message.
3627 * Must always be called paired with verbose_enter()!
3628 */
3629 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003630verbose_leave(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003631{
3632 if (*p_vfile != NUL)
3633 if (--msg_silent < 0)
3634 msg_silent = 0;
3635}
3636
3637/*
3638 * Like verbose_enter() and set msg_scroll when displaying the message.
3639 */
3640 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003641verbose_enter_scroll(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003642{
3643 if (*p_vfile != NUL)
3644 ++msg_silent;
3645 else
Bram Moolenaar85a20022019-12-21 18:25:54 +01003646 // always scroll up, don't overwrite
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003647 msg_scroll = TRUE;
3648}
3649
3650/*
3651 * Like verbose_leave() and set cmdline_row when displaying the message.
3652 */
3653 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003654verbose_leave_scroll(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003655{
3656 if (*p_vfile != NUL)
3657 {
3658 if (--msg_silent < 0)
3659 msg_silent = 0;
3660 }
3661 else
3662 cmdline_row = msg_row;
3663}
3664
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003665/*
3666 * Called when 'verbosefile' is set: stop writing to the file.
3667 */
3668 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003669verbose_stop(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003670{
3671 if (verbose_fd != NULL)
3672 {
3673 fclose(verbose_fd);
3674 verbose_fd = NULL;
3675 }
3676 verbose_did_open = FALSE;
3677}
3678
3679/*
3680 * Open the file 'verbosefile'.
3681 * Return FAIL or OK.
3682 */
3683 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003684verbose_open(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003685{
3686 if (verbose_fd == NULL && !verbose_did_open)
3687 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003688 // Only give the error message once.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003689 verbose_did_open = TRUE;
3690
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003691 verbose_fd = mch_fopen((char *)p_vfile, "a");
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003692 if (verbose_fd == NULL)
3693 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003694 semsg(_(e_cant_open_file_str), p_vfile);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003695 return FAIL;
3696 }
3697 }
3698 return OK;
3699}
3700
3701/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 * Give a warning message (for searching).
3703 * Use 'w' highlighting and may repeat the message after redrawing
3704 */
3705 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003706give_warning(char_u *message, int hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707{
Bram Moolenaarcc7eb2a2021-07-11 19:12:04 +02003708 give_warning_with_source(message, hl, FALSE);
3709}
3710
3711 void
3712give_warning_with_source(char_u *message, int hl, int with_source)
3713{
Bram Moolenaar85a20022019-12-21 18:25:54 +01003714 // Don't do this for ":silent".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 if (msg_silent != 0)
3716 return;
3717
Bram Moolenaar85a20022019-12-21 18:25:54 +01003718 // Don't want a hit-enter prompt here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 ++no_wait_return;
Bram Moolenaared203462004-06-16 11:19:22 +00003720
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721#ifdef FEAT_EVAL
3722 set_vim_var_string(VV_WARNINGMSG, message, -1);
3723#endif
Bram Moolenaard23a8232018-02-10 18:45:26 +01003724 VIM_CLEAR(keep_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 if (hl)
Bram Moolenaar8820b482017-03-16 17:23:31 +01003726 keep_msg_attr = HL_ATTR(HLF_W);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 else
3728 keep_msg_attr = 0;
Bram Moolenaarcc7eb2a2021-07-11 19:12:04 +02003729
3730 if (with_source)
3731 {
3732 // Do what msg() does, but with a column offset if the warning should
3733 // be after the mode message.
3734 msg_start();
3735 msg_source(HL_ATTR(HLF_W));
3736 msg_puts(" ");
3737 msg_puts_attr((char *)message, HL_ATTR(HLF_W) | MSG_HIST);
3738 msg_clr_eos();
3739 (void)msg_end();
3740 }
3741 else if (msg_attr((char *)message, keep_msg_attr) && msg_scrolled == 0)
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003742 set_keep_msg(message, keep_msg_attr);
Bram Moolenaarcc7eb2a2021-07-11 19:12:04 +02003743
Bram Moolenaar85a20022019-12-21 18:25:54 +01003744 msg_didout = FALSE; // overwrite this message
3745 msg_nowait = TRUE; // don't wait for this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 msg_col = 0;
Bram Moolenaared203462004-06-16 11:19:22 +00003747
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 --no_wait_return;
3749}
3750
Bram Moolenaar113e1072019-01-20 15:30:40 +01003751#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarf8be4612017-06-23 20:52:40 +02003752 void
3753give_warning2(char_u *message, char_u *a1, int hl)
3754{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +02003755 if (IObuff == NULL)
3756 {
3757 // Very early in initialisation and already something wrong, just give
3758 // the raw message so the user at least gets a hint.
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003759 give_warning(message, hl);
Bram Moolenaare3a22cb2019-10-14 22:01:57 +02003760 }
3761 else
3762 {
3763 vim_snprintf((char *)IObuff, IOSIZE, (char *)message, a1);
3764 give_warning(IObuff, hl);
3765 }
Bram Moolenaarf8be4612017-06-23 20:52:40 +02003766}
Bram Moolenaar113e1072019-01-20 15:30:40 +01003767#endif
Bram Moolenaarf8be4612017-06-23 20:52:40 +02003768
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769/*
3770 * Advance msg cursor to column "col".
3771 */
3772 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003773msg_advance(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774{
Bram Moolenaar85a20022019-12-21 18:25:54 +01003775 if (msg_silent != 0) // nothing to advance to
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003777 msg_col = col; // for redirection, may fill it up later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 return;
3779 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003780 if (col >= Columns) // not enough room
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781 col = Columns - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003782#ifdef FEAT_RIGHTLEFT
3783 if (cmdmsg_rl)
3784 while (msg_col > Columns - col)
3785 msg_putchar(' ');
3786 else
3787#endif
3788 while (msg_col < col)
3789 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790}
3791
3792#if defined(FEAT_CON_DIALOG) || defined(PROTO)
3793/*
3794 * Used for "confirm()" function, and the :confirm command prefix.
3795 * Versions which haven't got flexible dialogs yet, and console
3796 * versions, get this generic handler which uses the command line.
3797 *
3798 * type = one of:
3799 * VIM_QUESTION, VIM_INFO, VIM_WARNING, VIM_ERROR or VIM_GENERIC
3800 * title = title string (can be NULL for default)
3801 * (neither used in console dialogs at the moment)
3802 *
3803 * Format of the "buttons" string:
3804 * "Button1Name\nButton2Name\nButton3Name"
3805 * The first button should normally be the default/accept
3806 * The second button should be the 'Cancel' button
3807 * Other buttons- use your imagination!
3808 * A '&' in a button name becomes a shortcut, so each '&' should be before a
3809 * different letter.
Rob Pilling8196e942022-02-11 15:12:10 +00003810 *
3811 * Returns 0 if cancelled, otherwise the nth button (1-indexed).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003814do_dialog(
3815 int type UNUSED,
3816 char_u *title UNUSED,
3817 char_u *message,
3818 char_u *buttons,
3819 int dfltbutton,
Bram Moolenaar85a20022019-12-21 18:25:54 +01003820 char_u *textfield UNUSED, // IObuff for inputdialog(), NULL
3821 // otherwise
3822 int ex_cmd) // when TRUE pressing : accepts default and starts
3823 // Ex command
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824{
3825 int oldState;
3826 int retval = 0;
3827 char_u *hotkeys;
3828 int c;
3829 int i;
Bram Moolenaar27321db2020-07-06 21:24:57 +02003830 tmode_T save_tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831
3832#ifndef NO_CONSOLE
Bram Moolenaar85a20022019-12-21 18:25:54 +01003833 // Don't output anything in silent mode ("ex -s")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 if (silent_mode)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003835 return dfltbutton; // return default option
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836#endif
3837
3838#ifdef FEAT_GUI_DIALOG
Bram Moolenaar85a20022019-12-21 18:25:54 +01003839 // When GUI is running and 'c' not in 'guioptions', use the GUI dialog
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
3841 {
Bram Moolenaar2d12c252022-06-13 21:42:45 +01003842 // --gui-dialog-file: write text to a file
3843 if (gui_dialog_log(title, message))
3844 c = dfltbutton;
3845 else
3846 c = gui_mch_dialog(type, title, message, buttons, dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003847 textfield, ex_cmd);
Bram Moolenaar85a20022019-12-21 18:25:54 +01003848 // avoid a hit-enter prompt without clearing the cmdline
Bram Moolenaar8de49e12009-02-11 17:47:54 +00003849 need_wait_return = FALSE;
3850 emsg_on_display = FALSE;
3851 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852
Bram Moolenaar85a20022019-12-21 18:25:54 +01003853 // Flush output to avoid that further messages and redrawing is done
3854 // in the wrong order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 out_flush();
3856 gui_mch_update();
3857
3858 return c;
3859 }
3860#endif
3861
3862 oldState = State;
Bram Moolenaar24959102022-05-07 20:01:16 +01003863 State = MODE_CONFIRM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865
Bram Moolenaar27321db2020-07-06 21:24:57 +02003866 // Ensure raw mode here.
3867 save_tmode = cur_tmode;
3868 settmode(TMODE_RAW);
3869
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 /*
3871 * Since we wait for a keypress, don't make the
3872 * user press RETURN as well afterwards.
3873 */
3874 ++no_wait_return;
3875 hotkeys = msg_show_console_dialog(message, buttons, dfltbutton);
3876
3877 if (hotkeys != NULL)
3878 {
3879 for (;;)
3880 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003881 // Get a typed character directly from the user.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 c = get_keystroke();
3883 switch (c)
3884 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003885 case CAR: // User accepts default option
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 case NL:
3887 retval = dfltbutton;
3888 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003889 case Ctrl_C: // User aborts/cancels
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 case ESC:
3891 retval = 0;
3892 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003893 default: // Could be a hotkey?
3894 if (c < 0) // special keys are ignored here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 continue;
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003896 if (c == ':' && ex_cmd)
3897 {
3898 retval = dfltbutton;
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02003899 ins_char_typebuf(':', 0);
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003900 break;
3901 }
3902
Bram Moolenaar85a20022019-12-21 18:25:54 +01003903 // Make the character lowercase, as chars in "hotkeys" are.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 c = MB_TOLOWER(c);
3905 retval = 1;
3906 for (i = 0; hotkeys[i]; ++i)
3907 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 if (has_mbyte)
3909 {
3910 if ((*mb_ptr2char)(hotkeys + i) == c)
3911 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003912 i += (*mb_ptr2len)(hotkeys + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 }
3914 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 if (hotkeys[i] == c)
3916 break;
3917 ++retval;
3918 }
3919 if (hotkeys[i])
3920 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003921 // No hotkey match, so keep waiting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 continue;
3923 }
3924 break;
3925 }
3926
3927 vim_free(hotkeys);
3928 }
3929
Bram Moolenaar27321db2020-07-06 21:24:57 +02003930 settmode(save_tmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 State = oldState;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 --no_wait_return;
3934 msg_end_prompt();
3935
3936 return retval;
3937}
3938
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939/*
3940 * Copy one character from "*from" to "*to", taking care of multi-byte
3941 * characters. Return the length of the character in bytes.
3942 */
3943 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003944copy_char(
3945 char_u *from,
3946 char_u *to,
Bram Moolenaar85a20022019-12-21 18:25:54 +01003947 int lowercase) // make character lower case
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 int len;
3950 int c;
3951
3952 if (has_mbyte)
3953 {
3954 if (lowercase)
3955 {
3956 c = MB_TOLOWER((*mb_ptr2char)(from));
3957 return (*mb_char2bytes)(c, to);
3958 }
3959 else
3960 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003961 len = (*mb_ptr2len)(from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 mch_memmove(to, from, (size_t)len);
3963 return len;
3964 }
3965 }
3966 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 {
3968 if (lowercase)
3969 *to = (char_u)TOLOWER_LOC(*from);
3970 else
3971 *to = *from;
3972 return 1;
3973 }
3974}
3975
3976/*
3977 * Format the dialog string, and display it at the bottom of
3978 * the screen. Return a string of hotkey chars (if defined) for
3979 * each 'button'. If a button has no hotkey defined, the first character of
3980 * the button is used.
3981 * The hotkeys can be multi-byte characters, but without combining chars.
3982 *
3983 * Returns an allocated string with hotkeys, or NULL for error.
3984 */
3985 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003986msg_show_console_dialog(
3987 char_u *message,
3988 char_u *buttons,
3989 int dfltbutton)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990{
3991 int len = 0;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003992#define HOTK_LEN (has_mbyte ? MB_MAXBYTES : 1)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003993 int lenhotkey = HOTK_LEN; // count first button
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 char_u *hotk = NULL;
3995 char_u *msgp = NULL;
3996 char_u *hotkp = NULL;
3997 char_u *r;
3998 int copy;
3999#define HAS_HOTKEY_LEN 30
4000 char_u has_hotkey[HAS_HOTKEY_LEN];
Bram Moolenaar85a20022019-12-21 18:25:54 +01004001 int first_hotkey = FALSE; // first char of button is hotkey
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 int idx;
4003
4004 has_hotkey[0] = FALSE;
4005
4006 /*
4007 * First loop: compute the size of memory to allocate.
4008 * Second loop: copy to the allocated memory.
4009 */
4010 for (copy = 0; copy <= 1; ++copy)
4011 {
4012 r = buttons;
4013 idx = 0;
4014 while (*r)
4015 {
4016 if (*r == DLG_BUTTON_SEP)
4017 {
4018 if (copy)
4019 {
4020 *msgp++ = ',';
Bram Moolenaar85a20022019-12-21 18:25:54 +01004021 *msgp++ = ' '; // '\n' -> ', '
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022
Bram Moolenaar85a20022019-12-21 18:25:54 +01004023 // advance to next hotkey and set default hotkey
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 if (has_mbyte)
Bram Moolenaar6a516062007-07-05 08:11:42 +00004025 hotkp += STRLEN(hotkp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 ++hotkp;
Bram Moolenaar6a516062007-07-05 08:11:42 +00004028 hotkp[copy_char(r + 1, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 if (dfltbutton)
4030 --dfltbutton;
4031
Bram Moolenaar85a20022019-12-21 18:25:54 +01004032 // If no hotkey is specified first char is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 if (idx < HAS_HOTKEY_LEN - 1 && !has_hotkey[++idx])
4034 first_hotkey = TRUE;
4035 }
4036 else
4037 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004038 len += 3; // '\n' -> ', '; 'x' -> '(x)'
4039 lenhotkey += HOTK_LEN; // each button needs a hotkey
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 if (idx < HAS_HOTKEY_LEN - 1)
4041 has_hotkey[++idx] = FALSE;
4042 }
4043 }
4044 else if (*r == DLG_HOTKEY_CHAR || first_hotkey)
4045 {
4046 if (*r == DLG_HOTKEY_CHAR)
4047 ++r;
4048 first_hotkey = FALSE;
4049 if (copy)
4050 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004051 if (*r == DLG_HOTKEY_CHAR) // '&&a' -> '&a'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 *msgp++ = *r;
4053 else
4054 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004055 // '&a' -> '[a]'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 *msgp++ = (dfltbutton == 1) ? '[' : '(';
4057 msgp += copy_char(r, msgp, FALSE);
4058 *msgp++ = (dfltbutton == 1) ? ']' : ')';
4059
Bram Moolenaar85a20022019-12-21 18:25:54 +01004060 // redefine hotkey
Bram Moolenaar6a516062007-07-05 08:11:42 +00004061 hotkp[copy_char(r, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 }
4063 }
4064 else
4065 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004066 ++len; // '&a' -> '[a]'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 if (idx < HAS_HOTKEY_LEN - 1)
4068 has_hotkey[idx] = TRUE;
4069 }
4070 }
4071 else
4072 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004073 // everything else copy literally
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 if (copy)
4075 msgp += copy_char(r, msgp, FALSE);
4076 }
4077
Bram Moolenaar85a20022019-12-21 18:25:54 +01004078 // advance to the next character
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004079 MB_PTR_ADV(r);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 }
4081
4082 if (copy)
4083 {
4084 *msgp++ = ':';
4085 *msgp++ = ' ';
4086 *msgp = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 }
4088 else
4089 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004090 len += (int)(STRLEN(message)
Bram Moolenaar85a20022019-12-21 18:25:54 +01004091 + 2 // for the NL's
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004092 + STRLEN(buttons)
Bram Moolenaar85a20022019-12-21 18:25:54 +01004093 + 3); // for the ": " and NUL
4094 lenhotkey++; // for the NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095
Bram Moolenaar85a20022019-12-21 18:25:54 +01004096 // If no hotkey is specified first char is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 if (!has_hotkey[0])
4098 {
4099 first_hotkey = TRUE;
Bram Moolenaar85a20022019-12-21 18:25:54 +01004100 len += 2; // "x" -> "[x]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 }
4102
4103 /*
4104 * Now allocate and load the strings
4105 */
4106 vim_free(confirm_msg);
4107 confirm_msg = alloc(len);
4108 if (confirm_msg == NULL)
4109 return NULL;
4110 *confirm_msg = NUL;
4111 hotk = alloc(lenhotkey);
4112 if (hotk == NULL)
4113 return NULL;
4114
4115 *confirm_msg = '\n';
4116 STRCPY(confirm_msg + 1, message);
4117
4118 msgp = confirm_msg + 1 + STRLEN(message);
4119 hotkp = hotk;
4120
Bram Moolenaar85a20022019-12-21 18:25:54 +01004121 // Define first default hotkey. Keep the hotkey string NUL
4122 // terminated to avoid reading past the end.
Bram Moolenaar6a516062007-07-05 08:11:42 +00004123 hotkp[copy_char(buttons, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124
Bram Moolenaar85a20022019-12-21 18:25:54 +01004125 // Remember where the choices start, displaying starts here when
4126 // "hotkp" typed at the more prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 confirm_msg_tail = msgp;
4128 *msgp++ = '\n';
4129 }
4130 }
4131
4132 display_confirm_msg();
4133 return hotk;
4134}
4135
4136/*
4137 * Display the ":confirm" message. Also called when screen resized.
4138 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02004139 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004140display_confirm_msg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141{
Bram Moolenaar85a20022019-12-21 18:25:54 +01004142 // avoid that 'q' at the more prompt truncates the message here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 ++confirm_msg_used;
4144 if (confirm_msg != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004145 msg_puts_attr((char *)confirm_msg, HL_ATTR(HLF_M));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 --confirm_msg_used;
4147}
4148
Bram Moolenaar85a20022019-12-21 18:25:54 +01004149#endif // FEAT_CON_DIALOG
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150
4151#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4152
4153 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004154vim_dialog_yesno(
4155 int type,
4156 char_u *title,
4157 char_u *message,
4158 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159{
4160 if (do_dialog(type,
4161 title == NULL ? (char_u *)_("Question") : title,
4162 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004163 (char_u *)_("&Yes\n&No"), dflt, NULL, FALSE) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 return VIM_YES;
4165 return VIM_NO;
4166}
4167
4168 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004169vim_dialog_yesnocancel(
4170 int type,
4171 char_u *title,
4172 char_u *message,
4173 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174{
4175 switch (do_dialog(type,
4176 title == NULL ? (char_u *)_("Question") : title,
4177 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004178 (char_u *)_("&Yes\n&No\n&Cancel"), dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 {
4180 case 1: return VIM_YES;
4181 case 2: return VIM_NO;
4182 }
4183 return VIM_CANCEL;
4184}
4185
4186 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004187vim_dialog_yesnoallcancel(
4188 int type,
4189 char_u *title,
4190 char_u *message,
4191 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192{
4193 switch (do_dialog(type,
4194 title == NULL ? (char_u *)"Question" : title,
4195 message,
4196 (char_u *)_("&Yes\n&No\nSave &All\n&Discard All\n&Cancel"),
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004197 dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 {
4199 case 1: return VIM_YES;
4200 case 2: return VIM_NO;
4201 case 3: return VIM_ALL;
4202 case 4: return VIM_DISCARDALL;
4203 }
4204 return VIM_CANCEL;
4205}
4206
Bram Moolenaar85a20022019-12-21 18:25:54 +01004207#endif // FEAT_GUI_DIALOG || FEAT_CON_DIALOG