blob: ec128bd42518dbc3a1e66cec65d05ffff44bbd03 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * message.c: functions for displaying messages on the command line
12 */
13
Bram Moolenaar85a20022019-12-21 18:25:54 +010014#define MESSAGE_FILE // don't include prototype for smsg()
Bram Moolenaar071d4272004-06-13 20:20:40 +000015
16#include "vim.h"
17
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010018static void add_msg_hist(char_u *s, int len, int attr);
19static void hit_return_msg(void);
20static void msg_home_replace_attr(char_u *fname, int attr);
Bram Moolenaar32526b32019-01-19 17:43:09 +010021static void msg_puts_attr_len(char *str, int maxlen, int attr);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010022static void msg_puts_display(char_u *str, int maxlen, int attr, int recurse);
23static void msg_scroll_up(void);
24static void inc_msg_scrolled(void);
25static void store_sb_text(char_u **sb_str, char_u *s, int attr, int *sb_col, int finish);
26static void t_puts(int *t_col, char_u *t_s, char_u *s, int attr);
27static void msg_puts_printf(char_u *str, int maxlen);
28static int do_more_prompt(int typed_char);
29static void msg_screen_putchar(int c, int attr);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020030static void msg_moremsg(int full);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010031static int msg_check_screen(void);
32static void redir_write(char_u *s, int maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000033#ifdef FEAT_CON_DIALOG
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010034static char_u *msg_show_console_dialog(char_u *message, char_u *buttons, int dfltbutton);
Bram Moolenaar85a20022019-12-21 18:25:54 +010035static int confirm_msg_used = FALSE; // displaying confirm_msg
36static char_u *confirm_msg = NULL; // ":confirm" message
37static char_u *confirm_msg_tail; // tail of confirm_msg
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020038static void display_confirm_msg(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000039#endif
Bram Moolenaar958dc692016-12-01 15:34:12 +010040#ifdef FEAT_JOB_CHANNEL
41static int emsg_to_channel_log = FALSE;
42#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000043
44struct msg_hist
45{
46 struct msg_hist *next;
47 char_u *msg;
48 int attr;
49};
50
51static struct msg_hist *first_msg_hist = NULL;
52static struct msg_hist *last_msg_hist = NULL;
53static int msg_hist_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000054
Bram Moolenaarb5b5b892011-09-14 15:39:29 +020055static FILE *verbose_fd = NULL;
56static int verbose_did_open = FALSE;
57
Bram Moolenaar071d4272004-06-13 20:20:40 +000058/*
59 * When writing messages to the screen, there are many different situations.
60 * A number of variables is used to remember the current state:
61 * msg_didany TRUE when messages were written since the last time the
62 * user reacted to a prompt.
63 * Reset: After hitting a key for the hit-return prompt,
64 * hitting <CR> for the command line or input().
65 * Set: When any message is written to the screen.
66 * msg_didout TRUE when something was written to the current line.
67 * Reset: When advancing to the next line, when the current
68 * text can be overwritten.
69 * Set: When any message is written to the screen.
70 * msg_nowait No extra delay for the last drawn message.
71 * Used in normal_cmd() before the mode message is drawn.
72 * emsg_on_display There was an error message recently. Indicates that there
73 * should be a delay before redrawing.
74 * msg_scroll The next message should not overwrite the current one.
75 * msg_scrolled How many lines the screen has been scrolled (because of
76 * messages). Used in update_screen() to scroll the screen
77 * back. Incremented each time the screen scrolls a line.
78 * msg_scrolled_ign TRUE when msg_scrolled is non-zero and msg_puts_attr()
79 * writes something without scrolling should not make
80 * need_wait_return to be set. This is a hack to make ":ts"
81 * work without an extra prompt.
82 * lines_left Number of lines available for messages before the
Bram Moolenaar5d6f75e2011-12-30 14:14:29 +010083 * more-prompt is to be given. -1 when not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +000084 * need_wait_return TRUE when the hit-return prompt is needed.
85 * Reset: After giving the hit-return prompt, when the user
86 * has answered some other prompt.
87 * Set: When the ruler or typeahead display is overwritten,
88 * scrolling the screen for some message.
89 * keep_msg Message to be displayed after redrawing the screen, in
90 * main_loop().
91 * This is an allocated string or NULL when not used.
92 */
93
94/*
95 * msg(s) - displays the string 's' on the status line
96 * When terminal not initialized (yet) mch_errmsg(..) is used.
97 * return TRUE if wait_return not called
98 */
99 int
Bram Moolenaar32526b32019-01-19 17:43:09 +0100100msg(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000101{
102 return msg_attr_keep(s, 0, FALSE);
103}
104
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000105/*
106 * Like msg() but keep it silent when 'verbosefile' is set.
107 */
108 int
Bram Moolenaar32526b32019-01-19 17:43:09 +0100109verb_msg(char *s)
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000110{
111 int n;
112
113 verbose_enter();
114 n = msg_attr_keep(s, 0, FALSE);
115 verbose_leave();
116
117 return n;
118}
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000119
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120 int
Bram Moolenaar32526b32019-01-19 17:43:09 +0100121msg_attr(char *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122{
123 return msg_attr_keep(s, attr, FALSE);
124}
125
126 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100127msg_attr_keep(
Bram Moolenaar32526b32019-01-19 17:43:09 +0100128 char *s,
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100129 int attr,
Bram Moolenaar85a20022019-12-21 18:25:54 +0100130 int keep) // TRUE: set keep_msg if it doesn't scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131{
132 static int entered = 0;
133 int retval;
134 char_u *buf = NULL;
135
Bram Moolenaar85a20022019-12-21 18:25:54 +0100136 // Skip messages not matching ":filter pattern".
137 // Don't filter when there is an error.
Bram Moolenaar32526b32019-01-19 17:43:09 +0100138 if (!emsg_on_display && message_filtered((char_u *)s))
Bram Moolenaar7b668e82016-08-23 23:51:21 +0200139 return TRUE;
140
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141#ifdef FEAT_EVAL
142 if (attr == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100143 set_vim_var_string(VV_STATUSMSG, (char_u *)s, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144#endif
145
146 /*
147 * It is possible that displaying a messages causes a problem (e.g.,
148 * when redrawing the window), which causes another message, etc.. To
149 * break this loop, limit the recursiveness to 3 levels.
150 */
151 if (entered >= 3)
152 return TRUE;
153 ++entered;
154
Bram Moolenaar85a20022019-12-21 18:25:54 +0100155 // Add message to history (unless it's a repeated kept message or a
156 // truncated message)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100157 if ((char_u *)s != keep_msg
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158 || (*s != '<'
159 && last_msg_hist != NULL
160 && last_msg_hist->msg != NULL
161 && STRCMP(s, last_msg_hist->msg)))
Bram Moolenaar32526b32019-01-19 17:43:09 +0100162 add_msg_hist((char_u *)s, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163
Bram Moolenaar958dc692016-12-01 15:34:12 +0100164#ifdef FEAT_JOB_CHANNEL
165 if (emsg_to_channel_log)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100166 // Write message in the channel log.
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +0000167 ch_log(NULL, "ERROR: %s", s);
Bram Moolenaar958dc692016-12-01 15:34:12 +0100168#endif
169
Bram Moolenaar85a20022019-12-21 18:25:54 +0100170 // Truncate the message if needed.
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000171 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +0100172 buf = msg_strtrunc((char_u *)s, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173 if (buf != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100174 s = (char *)buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175
Bram Moolenaar32526b32019-01-19 17:43:09 +0100176 msg_outtrans_attr((char_u *)s, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177 msg_clr_eos();
178 retval = msg_end();
179
Bram Moolenaar32526b32019-01-19 17:43:09 +0100180 if (keep && retval && vim_strsize((char_u *)s)
181 < (int)(Rows - cmdline_row - 1) * Columns + sc_col)
182 set_keep_msg((char_u *)s, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183
Rob Pilling726f7f92022-01-20 14:44:38 +0000184 need_fileinfo = FALSE;
185
Bram Moolenaar071d4272004-06-13 20:20:40 +0000186 vim_free(buf);
187 --entered;
188 return retval;
189}
190
191/*
192 * Truncate a string such that it can be printed without causing a scroll.
193 * Returns an allocated string or NULL when no truncating is done.
194 */
195 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100196msg_strtrunc(
197 char_u *s,
Bram Moolenaar85a20022019-12-21 18:25:54 +0100198 int force) // always truncate
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199{
200 char_u *buf = NULL;
201 int len;
202 int room;
203
Bram Moolenaar85a20022019-12-21 18:25:54 +0100204 // May truncate message to avoid a hit-return prompt
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000205 if ((!msg_scroll && !need_wait_return && shortmess(SHM_TRUNCALL)
206 && !exmode_active && msg_silent == 0) || force)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207 {
208 len = vim_strsize(s);
Bram Moolenaarbb15b652005-10-03 21:52:09 +0000209 if (msg_scrolled != 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100210 // Use all the columns.
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000211 room = (int)(Rows - msg_row) * Columns - 1;
212 else
Bram Moolenaar85a20022019-12-21 18:25:54 +0100213 // Use up to 'showcmd' column.
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000214 room = (int)(Rows - msg_row - 1) * Columns + sc_col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215 if (len > room && room > 0)
216 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217 if (enc_utf8)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100218 // may have up to 18 bytes per cell (6 per char, up to two
219 // composing chars)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100220 len = (room + 2) * 18;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221 else if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100222 // may have up to 2 bytes per cell for euc-jp
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100223 len = (room + 2) * 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224 else
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100225 len = room + 2;
226 buf = alloc(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227 if (buf != NULL)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100228 trunc_string(s, buf, room, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229 }
230 }
231 return buf;
232}
233
234/*
235 * Truncate a string "s" to "buf" with cell width "room".
236 * "s" and "buf" may be equal.
237 */
238 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100239trunc_string(
240 char_u *s,
241 char_u *buf,
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200242 int room_in,
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100243 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244{
Bram Moolenaar85a20022019-12-21 18:25:54 +0100245 size_t room = room_in - 3; // "..." takes 3 chars
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200246 size_t half;
247 size_t len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248 int e;
249 int i;
250 int n;
251
Bram Moolenaar62818152021-02-14 15:37:30 +0100252 if (*s == NUL)
253 {
254 if (buflen > 0)
255 *buf = NUL;
256 return;
257 }
258
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200259 if (room_in < 3)
260 room = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261 half = room / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262
Bram Moolenaar85a20022019-12-21 18:25:54 +0100263 // First part: Start of the string.
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100264 for (e = 0; len < half && e < buflen; ++e)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265 {
266 if (s[e] == NUL)
267 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100268 // text fits without truncating!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269 buf[e] = NUL;
270 return;
271 }
272 n = ptr2cells(s + e);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200273 if (len + n > half)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274 break;
275 len += n;
276 buf[e] = s[e];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000278 for (n = (*mb_ptr2len)(s + e); --n > 0; )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100280 if (++e == buflen)
281 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282 buf[e] = s[e];
283 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284 }
285
Bram Moolenaar85a20022019-12-21 18:25:54 +0100286 // Last part: End of the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000287 i = e;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000288 if (enc_dbcs != 0)
289 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100290 // For DBCS going backwards in a string is slow, but
291 // computing the cell width isn't too slow: go forward
292 // until the rest fits.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000293 n = vim_strsize(s + i);
294 while (len + n > room)
295 {
296 n -= ptr2cells(s + i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000297 i += (*mb_ptr2len)(s + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000298 }
299 }
300 else if (enc_utf8)
301 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100302 // For UTF-8 we can go backwards easily.
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000303 half = i = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304 for (;;)
305 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000306 do
Bram Moolenaarace95982017-03-29 17:30:27 +0200307 half = half - utf_head_off(s, s + half - 1) - 1;
Bram Moolenaarb9644432016-07-19 12:33:44 +0200308 while (half > 0 && utf_iscomposing(utf_ptr2char(s + half)));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309 n = ptr2cells(s + half);
Bram Moolenaarb9644432016-07-19 12:33:44 +0200310 if (len + n > room || half == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311 break;
312 len += n;
Bram Moolenaara5c0cc12016-07-30 16:40:39 +0200313 i = (int)half;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314 }
315 }
316 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317 {
Bram Moolenaard0337e32019-12-30 17:55:34 +0100318 for (i = (int)STRLEN(s);
319 i - 1 >= 0 && len + (n = ptr2cells(s + i - 1)) <= room; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320 len += n;
321 }
322
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200323
324 if (i <= e + 3)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100325 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100326 // text fits without truncating
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200327 if (s != buf)
328 {
329 len = STRLEN(s);
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200330 if (len >= (size_t)buflen)
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200331 len = buflen - 1;
332 len = len - e + 1;
333 if (len < 1)
334 buf[e - 1] = NUL;
335 else
336 mch_memmove(buf + e, s + e, len);
337 }
338 }
339 else if (e + 3 < buflen)
340 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100341 // set the middle and copy the last part
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100342 mch_memmove(buf + e, "...", (size_t)3);
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200343 len = STRLEN(s + i) + 1;
344 if (len >= (size_t)buflen - e - 3)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100345 len = buflen - e - 3 - 1;
346 mch_memmove(buf + e + 3, s + i, len);
347 buf[e + 3 + len - 1] = NUL;
348 }
349 else
350 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100351 // can't fit in the "...", just truncate it
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200352 buf[e - 1] = NUL;
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354}
355
356/*
357 * Automatic prototype generation does not understand this function.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100358 * Note: Caller of smsg() and smsg_attr() must check the resulting string is
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359 * shorter than IOSIZE!!!
360 */
361#ifndef PROTO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100363int vim_snprintf(char *str, size_t str_m, const char *fmt, ...);
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000364
Bram Moolenaar071d4272004-06-13 20:20:40 +0000365 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100366smsg(const char *s, ...)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200368 if (IObuff == NULL)
369 {
370 // Very early in initialisation and already something wrong, just
371 // give the raw message so the user at least gets a hint.
372 return msg((char *)s);
373 }
374 else
375 {
376 va_list arglist;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200378 va_start(arglist, s);
379 vim_vsnprintf((char *)IObuff, IOSIZE, s, arglist);
380 va_end(arglist);
381 return msg((char *)IObuff);
382 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383}
384
385 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100386smsg_attr(int attr, const char *s, ...)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200388 if (IObuff == NULL)
389 {
390 // Very early in initialisation and already something wrong, just
391 // give the raw message so the user at least gets a hint.
392 return msg_attr((char *)s, attr);
393 }
394 else
395 {
396 va_list arglist;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200398 va_start(arglist, s);
399 vim_vsnprintf((char *)IObuff, IOSIZE, s, arglist);
400 va_end(arglist);
401 return msg_attr((char *)IObuff, attr);
402 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403}
404
Bram Moolenaare0429682018-07-01 16:44:03 +0200405 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100406smsg_attr_keep(int attr, const char *s, ...)
Bram Moolenaare0429682018-07-01 16:44:03 +0200407{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200408 if (IObuff == NULL)
409 {
410 // Very early in initialisation and already something wrong, just
411 // give the raw message so the user at least gets a hint.
412 return msg_attr_keep((char *)s, attr, TRUE);
413 }
414 else
415 {
416 va_list arglist;
Bram Moolenaare0429682018-07-01 16:44:03 +0200417
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200418 va_start(arglist, s);
419 vim_vsnprintf((char *)IObuff, IOSIZE, s, arglist);
420 va_end(arglist);
421 return msg_attr_keep((char *)IObuff, attr, TRUE);
422 }
Bram Moolenaare0429682018-07-01 16:44:03 +0200423}
424
Bram Moolenaar071d4272004-06-13 20:20:40 +0000425#endif
426
427/*
428 * Remember the last sourcing name/lnum used in an error message, so that it
429 * isn't printed each time when it didn't change.
430 */
431static int last_sourcing_lnum = 0;
432static char_u *last_sourcing_name = NULL;
433
434/*
435 * Reset the last used sourcing name/lnum. Makes sure it is displayed again
436 * for the next error message;
437 */
Bram Moolenaar4eec5ec2005-06-26 22:26:21 +0000438 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100439reset_last_sourcing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440{
Bram Moolenaard23a8232018-02-10 18:45:26 +0100441 VIM_CLEAR(last_sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000442 last_sourcing_lnum = 0;
443}
444
445/*
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100446 * Return TRUE if "SOURCING_NAME" differs from "last_sourcing_name".
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000447 */
448 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100449other_sourcing_name(void)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000450{
Bram Moolenaar0bd8d052021-11-25 13:39:28 +0000451 if (HAVE_SOURCING_INFO && SOURCING_NAME != NULL)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000452 {
453 if (last_sourcing_name != NULL)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100454 return STRCMP(SOURCING_NAME, last_sourcing_name) != 0;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000455 return TRUE;
456 }
457 return FALSE;
458}
459
460/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000461 * Get the message about the source, as used for an error message.
462 * Returns an allocated string with room for one more character.
463 * Returns NULL when no message is to be given.
464 */
465 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100466get_emsg_source(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467{
468 char_u *Buf, *p;
469
Bram Moolenaar0bd8d052021-11-25 13:39:28 +0000470 if (HAVE_SOURCING_INFO && SOURCING_NAME != NULL && other_sourcing_name())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000471 {
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200472 char_u *sname = estack_sfile(ESTACK_NONE);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100473 char_u *tofree = sname;
474
475 if (sname == NULL)
476 sname = SOURCING_NAME;
477
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +0200478#ifdef FEAT_EVAL
479 if (estack_compiling)
480 p = (char_u *)_("Error detected while compiling %s:");
481 else
482#endif
483 p = (char_u *)_("Error detected while processing %s:");
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100484 Buf = alloc(STRLEN(sname) + STRLEN(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000485 if (Buf != NULL)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100486 sprintf((char *)Buf, (char *)p, sname);
487 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488 return Buf;
489 }
490 return NULL;
491}
492
493/*
494 * Get the message about the source lnum, as used for an error message.
495 * Returns an allocated string with room for one more character.
496 * Returns NULL when no message is to be given.
497 */
498 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100499get_emsg_lnum(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500{
501 char_u *Buf, *p;
502
Bram Moolenaar85a20022019-12-21 18:25:54 +0100503 // lnum is 0 when executing a command from the command line
504 // argument, we don't want a line number then
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100505 if (SOURCING_NAME != NULL
506 && (other_sourcing_name() || SOURCING_LNUM != last_sourcing_lnum)
507 && SOURCING_LNUM != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000508 {
509 p = (char_u *)_("line %4ld:");
Bram Moolenaar964b3742019-05-24 18:54:09 +0200510 Buf = alloc(STRLEN(p) + 20);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 if (Buf != NULL)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100512 sprintf((char *)Buf, (char *)p, (long)SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513 return Buf;
514 }
515 return NULL;
516}
517
518/*
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000519 * Display name and line number for the source of an error.
520 * Remember the file name and line number, so that for the next error the info
521 * is only displayed if it changed.
522 */
523 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100524msg_source(int attr)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000525{
526 char_u *p;
Bram Moolenaarba8c9262021-11-25 14:43:18 +0000527 static int recursive = FALSE;
528
529 // Bail out if something called here causes an error.
530 if (recursive)
531 return;
532 recursive = TRUE;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000533
zeertzjq28c162f2022-08-14 14:49:50 +0100534 msg_scroll = TRUE; // this will take more than one line
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000535 ++no_wait_return;
536 p = get_emsg_source();
537 if (p != NULL)
538 {
Bram Moolenaar32526b32019-01-19 17:43:09 +0100539 msg_attr((char *)p, attr);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000540 vim_free(p);
541 }
542 p = get_emsg_lnum();
543 if (p != NULL)
544 {
Bram Moolenaar32526b32019-01-19 17:43:09 +0100545 msg_attr((char *)p, HL_ATTR(HLF_N));
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000546 vim_free(p);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100547 last_sourcing_lnum = SOURCING_LNUM; // only once for each line
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000548 }
549
Bram Moolenaar85a20022019-12-21 18:25:54 +0100550 // remember the last sourcing name printed, also when it's empty
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100551 if (SOURCING_NAME == NULL || other_sourcing_name())
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000552 {
Bram Moolenaarba8c9262021-11-25 14:43:18 +0000553 VIM_CLEAR(last_sourcing_name);
554 if (SOURCING_NAME != NULL)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100555 last_sourcing_name = vim_strsave(SOURCING_NAME);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000556 }
557 --no_wait_return;
Bram Moolenaarba8c9262021-11-25 14:43:18 +0000558
559 recursive = FALSE;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000560}
561
562/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000563 * Return TRUE if not giving error messages right now:
564 * If "emsg_off" is set: no error messages at the moment.
565 * If "msg" is in 'debug': do error message but without side effects.
566 * If "emsg_skip" is set: never do error messages.
567 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +0200568 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100569emsg_not_now(void)
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000570{
571 if ((emsg_off > 0 && vim_strchr(p_debug, 'm') == NULL
572 && vim_strchr(p_debug, 't') == NULL)
573#ifdef FEAT_EVAL
574 || emsg_skip > 0
575#endif
576 )
577 return TRUE;
578 return FALSE;
579}
580
Bram Moolenaar5a66dfb2017-03-01 20:40:39 +0100581#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100582static garray_T ignore_error_list = GA_EMPTY;
583
584 void
585ignore_error_for_testing(char_u *error)
586{
587 if (ignore_error_list.ga_itemsize == 0)
588 ga_init2(&ignore_error_list, sizeof(char_u *), 1);
589
Bram Moolenaar461a7fc2018-12-22 13:28:07 +0100590 if (STRCMP("RESET", error) == 0)
591 ga_clear_strings(&ignore_error_list);
592 else
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000593 ga_copy_string(&ignore_error_list, error);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100594}
595
596 static int
597ignore_error(char_u *msg)
598{
599 int i;
600
601 for (i = 0; i < ignore_error_list.ga_len; ++i)
602 if (strstr((char *)msg,
603 (char *)((char_u **)(ignore_error_list.ga_data))[i]) != NULL)
604 return TRUE;
605 return FALSE;
606}
607#endif
608
Bram Moolenaarb869c0d2016-07-20 00:10:51 +0200609#if !defined(HAVE_STRERROR) || defined(PROTO)
610/*
611 * Replacement for perror() that behaves more or less like emsg() was called.
Bram Moolenaar53989552019-12-23 22:59:18 +0100612 * v:errmsg will be set and called_emsg will be incremented.
Bram Moolenaarb869c0d2016-07-20 00:10:51 +0200613 */
614 void
615do_perror(char *msg)
616{
617 perror(msg);
618 ++emsg_silent;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100619 emsg(msg);
Bram Moolenaarb869c0d2016-07-20 00:10:51 +0200620 --emsg_silent;
621}
622#endif
623
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000624/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100625 * emsg_core() - display an error message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 *
627 * Rings the bell, if appropriate, and calls message() to do the real work
628 * When terminal not initialized (yet) mch_errmsg(..) is used.
629 *
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100630 * Return TRUE if wait_return not called.
631 * Note: caller must check 'emsg_not_now()' before calling this.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100633 static int
634emsg_core(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635{
636 int attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637 char_u *p;
Bram Moolenaar958dc692016-12-01 15:34:12 +0100638 int r;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639#ifdef FEAT_EVAL
640 int ignore = FALSE;
641 int severe;
642#endif
643
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100644#ifdef FEAT_EVAL
Bram Moolenaar85a20022019-12-21 18:25:54 +0100645 // When testing some errors are turned into a normal message.
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100646 if (ignore_error(s))
Bram Moolenaar85a20022019-12-21 18:25:54 +0100647 // don't call msg() if it results in a dialog
Bram Moolenaar32526b32019-01-19 17:43:09 +0100648 return msg_use_printf() ? FALSE : msg((char *)s);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100649#endif
650
Bram Moolenaar53989552019-12-23 22:59:18 +0100651 ++called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653#ifdef FEAT_EVAL
Bram Moolenaar85a20022019-12-21 18:25:54 +0100654 // If "emsg_severe" is TRUE: When an error exception is to be thrown,
655 // prefer this message over previous messages for the same command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 severe = emsg_severe;
657 emsg_severe = FALSE;
658#endif
659
Bram Moolenaar57657d82006-04-21 22:12:41 +0000660 if (!emsg_off || vim_strchr(p_debug, 't') != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 {
662#ifdef FEAT_EVAL
663 /*
664 * Cause a throw of an error exception if appropriate. Don't display
665 * the error message in this case. (If no matching catch clause will
666 * be found, the message will be displayed later on.) "ignore" is set
667 * when the message should be ignored completely (used for the
668 * interrupt message).
669 */
670 if (cause_errthrow(s, severe, &ignore) == TRUE)
671 {
672 if (!ignore)
Bram Moolenaar76a63452018-11-28 20:38:37 +0100673 ++did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 return TRUE;
675 }
676
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100677 if (in_assert_fails && emsg_assert_fails_msg == NULL)
Bram Moolenaar1d634542020-08-18 13:41:50 +0200678 {
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200679 emsg_assert_fails_msg = vim_strsave(s);
Bram Moolenaar1d634542020-08-18 13:41:50 +0200680 emsg_assert_fails_lnum = SOURCING_LNUM;
Bram Moolenaar9bd5d872020-09-06 21:47:48 +0200681 vim_free(emsg_assert_fails_context);
682 emsg_assert_fails_context = vim_strsave(
683 SOURCING_NAME == NULL ? (char_u *)"" : SOURCING_NAME);
Bram Moolenaar1d634542020-08-18 13:41:50 +0200684 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200685
Bram Moolenaar85a20022019-12-21 18:25:54 +0100686 // set "v:errmsg", also when using ":silent! cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 set_vim_var_string(VV_ERRMSG, s, -1);
688#endif
689
690 /*
Bram Moolenaara7241f52008-06-24 20:39:31 +0000691 * When using ":silent! cmd" ignore error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 * But do write it to the redirection file.
693 */
694 if (emsg_silent != 0)
695 {
Bram Moolenaar599410c2021-04-10 14:03:43 +0200696#ifdef FEAT_EVAL
697 ++did_emsg_silent;
698#endif
Bram Moolenaar79815f12016-07-09 17:07:29 +0200699 if (emsg_noredir == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 {
Bram Moolenaar79815f12016-07-09 17:07:29 +0200701 msg_start();
702 p = get_emsg_source();
703 if (p != NULL)
704 {
705 STRCAT(p, "\n");
706 redir_write(p, -1);
707 vim_free(p);
708 }
709 p = get_emsg_lnum();
710 if (p != NULL)
711 {
712 STRCAT(p, "\n");
713 redir_write(p, -1);
714 vim_free(p);
715 }
716 redir_write(s, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +0100718#ifdef FEAT_EVAL
719 // Only increment did_emsg_def when :silent! wasn't used inside the
720 // :def function.
721 if (emsg_silent == emsg_silent_def)
722 ++did_emsg_def;
723#endif
Bram Moolenaar958dc692016-12-01 15:34:12 +0100724#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare809a4e2019-07-04 17:35:05 +0200725 ch_log(NULL, "ERROR silent: %s", (char *)s);
Bram Moolenaar958dc692016-12-01 15:34:12 +0100726#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727 return TRUE;
728 }
729
Bram Moolenaar2b7bc562017-01-14 19:24:52 +0100730 ex_exitval = 1;
731
Bram Moolenaar85a20022019-12-21 18:25:54 +0100732 // Reset msg_silent, an error causes messages to be switched back on.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733 msg_silent = 0;
734 cmd_silent = FALSE;
735
Bram Moolenaar85a20022019-12-21 18:25:54 +0100736 if (global_busy) // break :global command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 ++global_busy;
738
739 if (p_eb)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100740 beep_flush(); // also includes flush_buffers()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 else
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200742 flush_buffers(FLUSH_MINIMAL); // flush internal buffers
Bram Moolenaar76a63452018-11-28 20:38:37 +0100743 ++did_emsg; // flag for DoOneCmd()
Bram Moolenaare723c422017-09-06 23:40:10 +0200744#ifdef FEAT_EVAL
Bram Moolenaar88c89c72021-08-14 14:01:05 +0200745 ++uncaught_emsg;
Bram Moolenaare723c422017-09-06 23:40:10 +0200746#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 }
748
Bram Moolenaar85a20022019-12-21 18:25:54 +0100749 emsg_on_display = TRUE; // remember there is an error message
Bram Moolenaar85a20022019-12-21 18:25:54 +0100750 attr = HL_ATTR(HLF_E); // set highlight mode for error messages
Bram Moolenaarbb15b652005-10-03 21:52:09 +0000751 if (msg_scrolled != 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100752 need_wait_return = TRUE; // needed in case emsg() is called after
753 // wait_return has reset need_wait_return
754 // and a redraw is expected because
755 // msg_scrolled is non-zero
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756
Bram Moolenaar958dc692016-12-01 15:34:12 +0100757#ifdef FEAT_JOB_CHANNEL
758 emsg_to_channel_log = TRUE;
759#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 /*
761 * Display name and line number for the source of the error.
zeertzjq28c162f2022-08-14 14:49:50 +0100762 * Sets "msg_scroll".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763 */
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000764 msg_source(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765
766 /*
767 * Display the error message itself.
768 */
Bram Moolenaar85a20022019-12-21 18:25:54 +0100769 msg_nowait = FALSE; // wait for this msg
Bram Moolenaar32526b32019-01-19 17:43:09 +0100770 r = msg_attr((char *)s, attr);
Bram Moolenaar958dc692016-12-01 15:34:12 +0100771
772#ifdef FEAT_JOB_CHANNEL
773 emsg_to_channel_log = FALSE;
774#endif
775 return r;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776}
777
778/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100779 * Print an error message.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 */
781 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100782emsg(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783{
Bram Moolenaar85a20022019-12-21 18:25:54 +0100784 // Skip this if not giving error messages at the moment.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100785 if (!emsg_not_now())
786 return emsg_core((char_u *)s);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100787 return TRUE; // no error messages at the moment
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788}
789
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100790#ifndef PROTO // manual proto with __attribute__
Bram Moolenaar95f09602016-11-10 20:01:45 +0100791/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100792 * Print an error message with format string and variable arguments.
793 * Note: caller must not pass 'IObuff' as 1st argument.
Bram Moolenaar95f09602016-11-10 20:01:45 +0100794 */
795 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100796semsg(const char *s, ...)
Bram Moolenaar95f09602016-11-10 20:01:45 +0100797{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200798 // Skip this if not giving error messages at the moment.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100799 if (!emsg_not_now())
800 {
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200801 if (IObuff == NULL)
802 {
803 // Very early in initialisation and already something wrong, just
804 // give the raw message so the user at least gets a hint.
805 return emsg_core((char_u *)s);
806 }
807 else
808 {
809 va_list ap;
Bram Moolenaar95f09602016-11-10 20:01:45 +0100810
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200811 va_start(ap, s);
812 vim_vsnprintf((char *)IObuff, IOSIZE, s, ap);
813 va_end(ap);
814 return emsg_core(IObuff);
815 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100816 }
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200817 return TRUE; // no error messages at the moment
Bram Moolenaar95f09602016-11-10 20:01:45 +0100818}
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100819#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +0100820
821/*
822 * Same as emsg(...), but abort on error when ABORT_ON_INTERNAL_ERROR is
823 * defined. It is used for internal errors only, so that they can be
824 * detected when fuzzing vim.
825 */
826 void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100827iemsg(char *s)
Bram Moolenaar95f09602016-11-10 20:01:45 +0100828{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100829 if (!emsg_not_now())
Bram Moolenaar1c67f3a2021-12-30 13:32:09 +0000830 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100831 emsg_core((char_u *)s);
Bram Moolenaar096ca732022-01-01 00:55:28 +0000832#if defined(ABORT_ON_INTERNAL_ERROR) && defined(FEAT_EVAL)
Bram Moolenaar1c67f3a2021-12-30 13:32:09 +0000833 set_vim_var_string(VV_ERRMSG, (char_u *)s, -1);
834 abort();
Bram Moolenaar95f09602016-11-10 20:01:45 +0100835#endif
Bram Moolenaar1c67f3a2021-12-30 13:32:09 +0000836 }
Bram Moolenaar95f09602016-11-10 20:01:45 +0100837}
838
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100839#ifndef PROTO // manual proto with __attribute__
Bram Moolenaar95f09602016-11-10 20:01:45 +0100840/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100841 * Same as semsg(...) but abort on error when ABORT_ON_INTERNAL_ERROR is
Bram Moolenaar95f09602016-11-10 20:01:45 +0100842 * defined. It is used for internal errors only, so that they can be
843 * detected when fuzzing vim.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100844 * Note: caller must not pass 'IObuff' as 1st argument.
Bram Moolenaar95f09602016-11-10 20:01:45 +0100845 */
846 void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100847siemsg(const char *s, ...)
Bram Moolenaar95f09602016-11-10 20:01:45 +0100848{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100849 if (!emsg_not_now())
850 {
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200851 if (IObuff == NULL)
852 {
853 // Very early in initialisation and already something wrong, just
854 // give the raw message so the user at least gets a hint.
855 emsg_core((char_u *)s);
856 }
857 else
858 {
859 va_list ap;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100860
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200861 va_start(ap, s);
862 vim_vsnprintf((char *)IObuff, IOSIZE, s, ap);
863 va_end(ap);
864 emsg_core(IObuff);
865 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100866 }
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100867# ifdef ABORT_ON_INTERNAL_ERROR
Bram Moolenaar95f09602016-11-10 20:01:45 +0100868 abort();
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100869# endif
Bram Moolenaar95f09602016-11-10 20:01:45 +0100870}
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100871#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +0100872
873/*
874 * Give an "Internal error" message.
875 */
876 void
877internal_error(char *where)
878{
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000879 siemsg(_(e_internal_error_str), where);
Bram Moolenaar95f09602016-11-10 20:01:45 +0100880}
881
Dominique Pelle748b3082022-01-08 12:41:16 +0000882#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaardd589232020-02-29 17:38:12 +0100883/*
884 * Like internal_error() but do not call abort(), to avoid tests using
885 * test_unknown() and test_void() causing Vim to exit.
886 */
887 void
888internal_error_no_abort(char *where)
889{
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000890 semsg(_(e_internal_error_str), where);
Bram Moolenaardd589232020-02-29 17:38:12 +0100891}
Dominique Pelle748b3082022-01-08 12:41:16 +0000892#endif
Bram Moolenaardd589232020-02-29 17:38:12 +0100893
Bram Moolenaar85a20022019-12-21 18:25:54 +0100894// emsg3() and emsgn() are in misc2.c to avoid warnings for the prototypes.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895
Bram Moolenaardf177f62005-02-22 08:39:57 +0000896 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100897emsg_invreg(int name)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000898{
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000899 semsg(_(e_invalid_register_name_str), transchar(name));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000900}
901
Dominique Pelle748b3082022-01-08 12:41:16 +0000902#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100904 * Give an error message which contains %s for "name[len]".
905 */
906 void
907emsg_namelen(char *msg, char_u *name, int len)
908{
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +0000909 char_u *copy = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100910
911 semsg(msg, copy == NULL ? "NULL" : (char *)copy);
Bram Moolenaar292b90d2020-03-18 15:23:16 +0100912 vim_free(copy);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100913}
Dominique Pelle748b3082022-01-08 12:41:16 +0000914#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100915
916/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 * Like msg(), but truncate to a single line if p_shm contains 't', or when
918 * "force" is TRUE. This truncates in another way as for normal messages.
919 * Careful: The string may be changed by msg_may_trunc()!
920 * Returns a pointer to the printed message, if wait_return() not called.
921 */
Bram Moolenaar32526b32019-01-19 17:43:09 +0100922 char *
923msg_trunc_attr(char *s, int force, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924{
925 int n;
Bram Moolenaar32526b32019-01-19 17:43:09 +0100926 char *ts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927
Bram Moolenaar85a20022019-12-21 18:25:54 +0100928 // Add message to history before truncating
Bram Moolenaar32526b32019-01-19 17:43:09 +0100929 add_msg_hist((char_u *)s, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930
Bram Moolenaar32526b32019-01-19 17:43:09 +0100931 ts = (char *)msg_may_trunc(force, (char_u *)s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932
933 msg_hist_off = TRUE;
Bram Moolenaar32526b32019-01-19 17:43:09 +0100934 n = msg_attr(ts, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935 msg_hist_off = FALSE;
936
937 if (n)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100938 return ts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 return NULL;
940}
941
942/*
943 * Check if message "s" should be truncated at the start (for filenames).
944 * Return a pointer to where the truncated message starts.
945 * Note: May change the message by replacing a character with '<'.
946 */
947 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100948msg_may_trunc(int force, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949{
950 int n;
951 int room;
952
Bram Moolenaar0fbc9262022-06-26 11:17:10 +0100953 // If something unexpected happened "room" may be negative, check for that
954 // just in case.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 room = (int)(Rows - cmdline_row - 1) * Columns + sc_col - 1;
Bram Moolenaar0fbc9262022-06-26 11:17:10 +0100956 if (room > 0 && (force || (shortmess(SHM_TRUNC) && !exmode_active))
Shougo Matsushitaf39cfb72022-07-30 16:54:05 +0100957 && (n = (int)STRLEN(s) - room) > 0 && p_ch > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 if (has_mbyte)
960 {
961 int size = vim_strsize(s);
962
Bram Moolenaar85a20022019-12-21 18:25:54 +0100963 // There may be room anyway when there are multibyte chars.
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +0000964 if (size <= room)
965 return s;
966
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 for (n = 0; size >= room; )
968 {
969 size -= (*mb_ptr2cells)(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000970 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 }
972 --n;
973 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 s += n;
975 *s = '<';
976 }
977 return s;
978}
979
980 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100981add_msg_hist(
982 char_u *s,
Bram Moolenaar85a20022019-12-21 18:25:54 +0100983 int len, // -1 for undetermined length
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100984 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985{
986 struct msg_hist *p;
987
988 if (msg_hist_off || msg_silent != 0)
989 return;
990
Bram Moolenaar85a20022019-12-21 18:25:54 +0100991 // Don't let the message history get too big
Bram Moolenaar4770d092006-01-12 23:22:24 +0000992 while (msg_hist_len > MAX_MSG_HIST_LEN)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +0000993 (void)delete_first_msg();
994
Bram Moolenaar85a20022019-12-21 18:25:54 +0100995 // allocate an entry and add the message at the end of the history
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200996 p = ALLOC_ONE(struct msg_hist);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 if (p != NULL)
998 {
999 if (len < 0)
1000 len = (int)STRLEN(s);
Bram Moolenaar85a20022019-12-21 18:25:54 +01001001 // remove leading and trailing newlines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 while (len > 0 && *s == '\n')
1003 {
1004 ++s;
1005 --len;
1006 }
1007 while (len > 0 && s[len - 1] == '\n')
1008 --len;
1009 p->msg = vim_strnsave(s, len);
1010 p->next = NULL;
1011 p->attr = attr;
1012 if (last_msg_hist != NULL)
1013 last_msg_hist->next = p;
1014 last_msg_hist = p;
1015 if (first_msg_hist == NULL)
1016 first_msg_hist = last_msg_hist;
1017 ++msg_hist_len;
1018 }
1019}
1020
1021/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00001022 * Delete the first (oldest) message from the history.
1023 * Returns FAIL if there are no messages.
1024 */
1025 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001026delete_first_msg(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00001027{
1028 struct msg_hist *p;
1029
1030 if (msg_hist_len <= 0)
1031 return FAIL;
1032 p = first_msg_hist;
1033 first_msg_hist = p->next;
Bram Moolenaara7241f52008-06-24 20:39:31 +00001034 if (first_msg_hist == NULL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001035 last_msg_hist = NULL; // history is empty
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00001036 vim_free(p->msg);
1037 vim_free(p);
1038 --msg_hist_len;
1039 return OK;
1040}
1041
1042/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 * ":messages" command.
1044 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 void
Bram Moolenaar52196b22016-04-14 17:52:41 +02001046ex_messages(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047{
1048 struct msg_hist *p;
1049 char_u *s;
Bram Moolenaar451f8492016-04-14 17:16:22 +02001050 int c = 0;
1051
1052 if (STRCMP(eap->arg, "clear") == 0)
1053 {
1054 int keep = eap->addr_count == 0 ? 0 : eap->line2;
1055
1056 while (msg_hist_len > keep)
1057 (void)delete_first_msg();
1058 return;
1059 }
1060
1061 if (*eap->arg != NUL)
1062 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001063 emsg(_(e_invalid_argument));
Bram Moolenaar451f8492016-04-14 17:16:22 +02001064 return;
1065 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066
1067 msg_hist_off = TRUE;
1068
Bram Moolenaar451f8492016-04-14 17:16:22 +02001069 p = first_msg_hist;
Bram Moolenaar451f8492016-04-14 17:16:22 +02001070 if (eap->addr_count != 0)
1071 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001072 // Count total messages
Bram Moolenaar451f8492016-04-14 17:16:22 +02001073 for (; p != NULL && !got_int; p = p->next)
1074 c++;
1075
1076 c -= eap->line2;
1077
Bram Moolenaar85a20022019-12-21 18:25:54 +01001078 // Skip without number of messages specified
Bram Moolenaar451f8492016-04-14 17:16:22 +02001079 for (p = first_msg_hist; p != NULL && !got_int && c > 0;
1080 p = p->next, c--);
1081 }
1082
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001083 if (p == first_msg_hist)
1084 {
Bram Moolenaar41f69182020-04-25 15:45:37 +02001085#ifdef FEAT_MULTI_LANG
1086 s = get_mess_lang();
1087#else
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001088 s = mch_getenv((char_u *)"LANG");
Bram Moolenaar41f69182020-04-25 15:45:37 +02001089#endif
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001090 if (s != NULL && *s != NUL)
Bram Moolenaar0c183192018-06-28 14:54:43 +02001091 // The next comment is extracted by xgettext and put in po file for
1092 // translators to read.
Bram Moolenaar32526b32019-01-19 17:43:09 +01001093 msg_attr(
Bram Moolenaar0c183192018-06-28 14:54:43 +02001094 // Translator: Please replace the name and email address
1095 // with the appropriate text for your translation.
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001096 _("Messages maintainer: Bram Moolenaar <Bram@vim.org>"),
Bram Moolenaar8820b482017-03-16 17:23:31 +01001097 HL_ATTR(HLF_T));
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001098 }
1099
Bram Moolenaar85a20022019-12-21 18:25:54 +01001100 // Display what was not skipped.
Bram Moolenaar451f8492016-04-14 17:16:22 +02001101 for (; p != NULL && !got_int; p = p->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 if (p->msg != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001103 msg_attr((char *)p->msg, p->attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104
1105 msg_hist_off = FALSE;
1106}
1107
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001108#if defined(FEAT_CON_DIALOG) || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109/*
1110 * Call this after prompting the user. This will avoid a hit-return message
1111 * and a delay.
1112 */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001113 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001114msg_end_prompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115{
1116 need_wait_return = FALSE;
1117 emsg_on_display = FALSE;
1118 cmdline_row = msg_row;
1119 msg_col = 0;
1120 msg_clr_eos();
Bram Moolenaar5d6f75e2011-12-30 14:14:29 +01001121 lines_left = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122}
1123#endif
1124
1125/*
Bram Moolenaar32526b32019-01-19 17:43:09 +01001126 * Wait for the user to hit a key (normally Enter).
1127 * If "redraw" is TRUE, clear and redraw the screen.
1128 * If "redraw" is FALSE, just redraw the screen.
1129 * If "redraw" is -1, don't redraw at all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 */
1131 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001132wait_return(int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133{
1134 int c;
1135 int oldState;
1136 int tmpState;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 int had_got_int;
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001138 int save_reg_recording;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001139 FILE *save_scriptout;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140
1141 if (redraw == TRUE)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001142 must_redraw = UPD_CLEAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143
Bram Moolenaar85a20022019-12-21 18:25:54 +01001144 // If using ":silent cmd", don't wait for a return. Also don't set
1145 // need_wait_return to do it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 if (msg_silent != 0)
1147 return;
1148
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01001149 /*
1150 * When inside vgetc(), we can't wait for a typed character at all.
1151 * With the global command (and some others) we only need one return at
1152 * the end. Adjust cmdline_row to avoid the next message overwriting the
1153 * last one.
1154 */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00001155 if (vgetc_busy > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 return;
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01001157 need_wait_return = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 if (no_wait_return)
1159 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 if (!exmode_active)
1161 cmdline_row = msg_row;
1162 return;
1163 }
1164
Bram Moolenaar85a20022019-12-21 18:25:54 +01001165 redir_off = TRUE; // don't redirect this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 oldState = State;
1167 if (quit_more)
1168 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001169 c = CAR; // just pretend CR was hit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 quit_more = FALSE;
1171 got_int = FALSE;
1172 }
1173 else if (exmode_active)
1174 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001175 msg_puts(" "); // make sure the cursor is on the right line
1176 c = CAR; // no need for a return in ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 got_int = FALSE;
1178 }
1179 else
1180 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001181 // Make sure the hit-return prompt is on screen when 'guioptions' was
1182 // just changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 screenalloc(FALSE);
1184
Bram Moolenaar24959102022-05-07 20:01:16 +01001185 State = MODE_HITRETURN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar85a20022019-12-21 18:25:54 +01001188 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189#endif
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01001190 cmdline_row = msg_row;
1191
Bram Moolenaar85a20022019-12-21 18:25:54 +01001192 // Avoid the sequence that the user types ":" at the hit-return prompt
1193 // to start an Ex command, but the file-changed dialog gets in the
1194 // way.
Bram Moolenaarec38d692013-04-24 15:12:32 +02001195 if (need_check_timestamps)
1196 check_timestamps(FALSE);
1197
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 hit_return_msg();
1199
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 do
1201 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001202 // Remember "got_int", if it is set vgetc() probably returns a
1203 // CTRL-C, but we need to loop then.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 had_got_int = got_int;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001205
Bram Moolenaar85a20022019-12-21 18:25:54 +01001206 // Don't do mappings here, we put the character back in the
1207 // typeahead buffer.
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001208 ++no_mapping;
1209 ++allow_keys;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001210
Bram Moolenaar85a20022019-12-21 18:25:54 +01001211 // Temporarily disable Recording. If Recording is active, the
1212 // character will be recorded later, since it will be added to the
1213 // typebuf after the loop
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001214 save_reg_recording = reg_recording;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001215 save_scriptout = scriptout;
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001216 reg_recording = 0;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001217 scriptout = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 c = safe_vgetc();
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00001219 if (had_got_int && !global_busy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 got_int = FALSE;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001221 --no_mapping;
1222 --allow_keys;
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001223 reg_recording = save_reg_recording;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001224 scriptout = save_scriptout;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001225
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226#ifdef FEAT_CLIPBOARD
Bram Moolenaar85a20022019-12-21 18:25:54 +01001227 // Strange way to allow copying (yanking) a modeless selection at
1228 // the hit-enter prompt. Use CTRL-Y, because the same is used in
1229 // Cmdline-mode and it's harmless when there is no selection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 if (c == Ctrl_Y && clip_star.state == SELECT_DONE)
1231 {
1232 clip_copy_modeless_selection(TRUE);
1233 c = K_IGNORE;
1234 }
1235#endif
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00001236
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001237 /*
1238 * Allow scrolling back in the messages.
1239 * Also accept scroll-down commands when messages fill the screen,
1240 * to avoid that typing one 'j' too many makes the messages
1241 * disappear.
1242 */
1243 if (p_more && !p_cp)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001244 {
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001245 if (c == 'b' || c == 'k' || c == 'u' || c == 'g'
1246 || c == K_UP || c == K_PAGEUP)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001247 {
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001248 if (msg_scrolled > Rows)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001249 // scroll back to show older messages
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001250 do_more_prompt(c);
1251 else
1252 {
1253 msg_didout = FALSE;
1254 c = K_IGNORE;
1255 msg_col =
1256#ifdef FEAT_RIGHTLEFT
1257 cmdmsg_rl ? Columns - 1 :
1258#endif
1259 0;
1260 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001261 if (quit_more)
1262 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001263 c = CAR; // just pretend CR was hit
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001264 quit_more = FALSE;
1265 got_int = FALSE;
1266 }
Bram Moolenaarb0912962013-08-09 20:38:26 +02001267 else if (c != K_IGNORE)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001268 {
1269 c = K_IGNORE;
1270 hit_return_msg();
1271 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001272 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001273 else if (msg_scrolled > Rows - 2
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001274 && (c == 'j' || c == 'd' || c == 'f'
1275 || c == K_DOWN || c == K_PAGEDOWN))
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001276 c = K_IGNORE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001277 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 } while ((had_got_int && c == Ctrl_C)
1279 || c == K_IGNORE
1280#ifdef FEAT_GUI
1281 || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR
1282#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 || c == K_LEFTDRAG || c == K_LEFTRELEASE
1284 || c == K_MIDDLEDRAG || c == K_MIDDLERELEASE
1285 || c == K_RIGHTDRAG || c == K_RIGHTRELEASE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001286 || c == K_MOUSELEFT || c == K_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 || c == K_MOUSEDOWN || c == K_MOUSEUP
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001288 || c == K_MOUSEMOVE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 || (!mouse_has(MOUSE_RETURN)
1290 && mouse_row < msg_row
1291 && (c == K_LEFTMOUSE
1292 || c == K_MIDDLEMOUSE
1293 || c == K_RIGHTMOUSE
1294 || c == K_X1MOUSE
1295 || c == K_X2MOUSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 );
1297 ui_breakcheck();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 /*
1299 * Avoid that the mouse-up event causes visual mode to start.
1300 */
1301 if (c == K_LEFTMOUSE || c == K_MIDDLEMOUSE || c == K_RIGHTMOUSE
1302 || c == K_X1MOUSE || c == K_X2MOUSE)
1303 (void)jump_to_mouse(MOUSE_SETPOS, NULL, 0);
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001304 else if (vim_strchr((char_u *)"\r\n ", c) == NULL && c != Ctrl_C)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001306 // Put the character back in the typeahead buffer. Don't use the
1307 // stuff buffer, because lmaps wouldn't work.
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001308 ins_char_typebuf(vgetc_char, vgetc_mod_mask);
Bram Moolenaar85a20022019-12-21 18:25:54 +01001309 do_redraw = TRUE; // need a redraw even though there is
1310 // typeahead
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 }
1313 redir_off = FALSE;
1314
1315 /*
1316 * If the user hits ':', '?' or '/' we get a command line from the next
1317 * line.
1318 */
1319 if (c == ':' || c == '?' || c == '/')
1320 {
1321 if (!exmode_active)
1322 cmdline_row = msg_row;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001323 skip_redraw = TRUE; // skip redraw once
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 do_redraw = FALSE;
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02001325#ifdef FEAT_TERMINAL
1326 skip_term_loop = TRUE;
1327#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 }
1329
1330 /*
1331 * If the window size changed set_shellsize() will redraw the screen.
1332 * Otherwise the screen is only redrawn if 'redraw' is set and no ':'
1333 * typed.
1334 */
1335 tmpState = State;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001336 State = oldState; // restore State before set_shellsize
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 msg_check();
1339
1340#if defined(UNIX) || defined(VMS)
1341 /*
1342 * When switching screens, we need to output an extra newline on exit.
1343 */
1344 if (swapping_screen() && !termcap_active)
1345 newline_on_exit = TRUE;
1346#endif
1347
1348 need_wait_return = FALSE;
1349 did_wait_return = TRUE;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001350 emsg_on_display = FALSE; // can delete error message now
1351 lines_left = -1; // reset lines_left at next msg_start()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 reset_last_sourcing();
1353 if (keep_msg != NULL && vim_strsize(keep_msg) >=
1354 (Rows - cmdline_row - 1) * Columns + sc_col)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001355 VIM_CLEAR(keep_msg); // don't redisplay message, it's too long
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356
Bram Moolenaar24959102022-05-07 20:01:16 +01001357 if (tmpState == MODE_SETWSIZE) // got resize event while in vgetc()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001359 starttermcap(); // start termcap before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 shell_resized();
1361 }
1362 else if (!skip_redraw
1363 && (redraw == TRUE || (msg_scrolled != 0 && redraw != -1)))
1364 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001365 starttermcap(); // start termcap before redrawing
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001366 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 }
1368}
1369
1370/*
1371 * Write the hit-return prompt.
1372 */
1373 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001374hit_return_msg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001376 int save_p_more = p_more;
1377
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001378 p_more = FALSE; // don't want to see this message when scrolling back
Bram Moolenaar85a20022019-12-21 18:25:54 +01001379 if (msg_didout) // start on a new line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 msg_putchar('\n');
1381 if (got_int)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001382 msg_puts(_("Interrupt: "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383
Bram Moolenaar32526b32019-01-19 17:43:09 +01001384 msg_puts_attr(_("Press ENTER or type command to continue"), HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 if (!msg_use_printf())
1386 msg_clr_eos();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001387 p_more = save_p_more;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388}
1389
1390/*
1391 * Set "keep_msg" to "s". Free the old value and check for NULL pointer.
1392 */
1393 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001394set_keep_msg(char_u *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395{
1396 vim_free(keep_msg);
1397 if (s != NULL && msg_silent == 0)
1398 keep_msg = vim_strsave(s);
1399 else
1400 keep_msg = NULL;
Bram Moolenaaraab21c32005-01-25 21:46:35 +00001401 keep_msg_more = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001402 keep_msg_attr = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403}
1404
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001405#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
1406/*
1407 * If there currently is a message being displayed, set "keep_msg" to it, so
1408 * that it will be displayed again after redraw.
1409 */
1410 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001411set_keep_msg_from_hist(void)
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001412{
1413 if (keep_msg == NULL && last_msg_hist != NULL && msg_scrolled == 0
Bram Moolenaar24959102022-05-07 20:01:16 +01001414 && (State & MODE_NORMAL))
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001415 set_keep_msg(last_msg_hist->msg, last_msg_hist->attr);
1416}
1417#endif
1418
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419/*
1420 * Prepare for outputting characters in the command line.
1421 */
1422 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001423msg_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424{
1425 int did_return = FALSE;
1426
Bram Moolenaare4ce65d2010-08-04 20:12:32 +02001427 if (!msg_silent)
Rob Pilling726f7f92022-01-20 14:44:38 +00001428 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001429 VIM_CLEAR(keep_msg);
Rob Pilling726f7f92022-01-20 14:44:38 +00001430 need_fileinfo = FALSE;
1431 }
Bram Moolenaara7241f52008-06-24 20:39:31 +00001432
1433#ifdef FEAT_EVAL
Shougo Matsushitaf39cfb72022-07-30 16:54:05 +01001434 if (need_clr_eos || p_ch == 0)
Bram Moolenaara7241f52008-06-24 20:39:31 +00001435 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001436 // Halfway an ":echo" command and getting an (error) message: clear
1437 // any text from the command.
Bram Moolenaara7241f52008-06-24 20:39:31 +00001438 need_clr_eos = FALSE;
1439 msg_clr_eos();
1440 }
1441#endif
1442
Bram Moolenaar85a20022019-12-21 18:25:54 +01001443 if (!msg_scroll && full_screen) // overwrite last message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 {
1445 msg_row = cmdline_row;
1446 msg_col =
1447#ifdef FEAT_RIGHTLEFT
1448 cmdmsg_rl ? Columns - 1 :
1449#endif
1450 0;
1451 }
Shougo Matsushitaf39cfb72022-07-30 16:54:05 +01001452 else if (msg_didout || p_ch == 0) // start message on next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453 {
1454 msg_putchar('\n');
1455 did_return = TRUE;
1456 if (exmode_active != EXMODE_NORMAL)
1457 cmdline_row = msg_row;
1458 }
1459 if (!msg_didany || lines_left < 0)
1460 msg_starthere();
1461 if (msg_silent == 0)
1462 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001463 msg_didout = FALSE; // no output on current line yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 cursor_off();
1465 }
1466
Bram Moolenaar85a20022019-12-21 18:25:54 +01001467 // when redirecting, may need to start a new line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 if (!did_return)
1469 redir_write((char_u *)"\n", -1);
1470}
1471
1472/*
1473 * Note that the current msg position is where messages start.
1474 */
1475 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001476msg_starthere(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477{
1478 lines_left = cmdline_row;
1479 msg_didany = FALSE;
1480}
1481
1482 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001483msg_putchar(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484{
1485 msg_putchar_attr(c, 0);
1486}
1487
1488 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001489msg_putchar_attr(int c, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490{
Bram Moolenaar63c0ccd2019-01-19 21:06:58 +01001491 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492
1493 if (IS_SPECIAL(c))
1494 {
1495 buf[0] = K_SPECIAL;
1496 buf[1] = K_SECOND(c);
1497 buf[2] = K_THIRD(c);
1498 buf[3] = NUL;
1499 }
1500 else
Bram Moolenaar63c0ccd2019-01-19 21:06:58 +01001501 buf[(*mb_char2bytes)(c, buf)] = NUL;
Bram Moolenaar63c0ccd2019-01-19 21:06:58 +01001502 msg_puts_attr((char *)buf, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503}
1504
1505 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001506msg_outnum(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001508 char buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509
Bram Moolenaar32526b32019-01-19 17:43:09 +01001510 sprintf(buf, "%ld", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 msg_puts(buf);
1512}
1513
1514 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001515msg_home_replace(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516{
1517 msg_home_replace_attr(fname, 0);
1518}
1519
1520#if defined(FEAT_FIND_ID) || defined(PROTO)
1521 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001522msg_home_replace_hl(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523{
Bram Moolenaar8820b482017-03-16 17:23:31 +01001524 msg_home_replace_attr(fname, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525}
1526#endif
1527
1528 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001529msg_home_replace_attr(char_u *fname, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530{
1531 char_u *name;
1532
1533 name = home_replace_save(NULL, fname);
1534 if (name != NULL)
1535 msg_outtrans_attr(name, attr);
1536 vim_free(name);
1537}
1538
1539/*
1540 * Output 'len' characters in 'str' (including NULs) with translation
Bram Moolenaarf48ee3c2019-12-06 22:18:20 +01001541 * if 'len' is -1, output up to a NUL character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 * Use attributes 'attr'.
1543 * Return the number of characters it takes on the screen.
1544 */
1545 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001546msg_outtrans(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547{
1548 return msg_outtrans_attr(str, 0);
1549}
1550
1551 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001552msg_outtrans_attr(char_u *str, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553{
1554 return msg_outtrans_len_attr(str, (int)STRLEN(str), attr);
1555}
1556
1557 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001558msg_outtrans_len(char_u *str, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559{
1560 return msg_outtrans_len_attr(str, len, 0);
1561}
1562
1563/*
1564 * Output one character at "p". Return pointer to the next character.
1565 * Handles multi-byte characters.
1566 */
1567 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001568msg_outtrans_one(char_u *p, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 int l;
1571
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001572 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 {
1574 msg_outtrans_len_attr(p, l, attr);
1575 return p + l;
1576 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01001577 msg_puts_attr((char *)transchar_byte(*p), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 return p + 1;
1579}
1580
1581 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001582msg_outtrans_len_attr(char_u *msgstr, int len, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583{
1584 int retval = 0;
1585 char_u *str = msgstr;
1586 char_u *plain_start = msgstr;
1587 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588 int mb_l;
1589 int c;
Bram Moolenaar3d30af82020-10-13 22:15:56 +02001590 int save_got_int = got_int;
1591
1592 // Only quit when got_int was set in here.
1593 got_int = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594
Bram Moolenaar85a20022019-12-21 18:25:54 +01001595 // if MSG_HIST flag set, add message to history
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 if (attr & MSG_HIST)
1597 {
1598 add_msg_hist(str, len, attr);
1599 attr &= ~MSG_HIST;
1600 }
1601
Bram Moolenaar85a20022019-12-21 18:25:54 +01001602 // If the string starts with a composing character first draw a space on
1603 // which the composing char can be drawn.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 if (enc_utf8 && utf_iscomposing(utf_ptr2char(msgstr)))
Bram Moolenaar32526b32019-01-19 17:43:09 +01001605 msg_puts_attr(" ", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606
1607 /*
1608 * Go over the string. Special characters are translated and printed.
1609 * Normal characters are printed several at a time.
1610 */
Bram Moolenaar3d30af82020-10-13 22:15:56 +02001611 while (--len >= 0 && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 if (enc_utf8)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001614 // Don't include composing chars after the end.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001615 mb_l = utfc_ptr2len_len(str, len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 else if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001617 mb_l = (*mb_ptr2len)(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 else
1619 mb_l = 1;
1620 if (has_mbyte && mb_l > 1)
1621 {
1622 c = (*mb_ptr2char)(str);
1623 if (vim_isprintc(c))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001624 // printable multi-byte char: count the cells.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 retval += (*mb_ptr2cells)(str);
1626 else
1627 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001628 // unprintable multi-byte char: print the printable chars so
1629 // far and the translation of the unprintable char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 if (str > plain_start)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001631 msg_puts_attr_len((char *)plain_start,
1632 (int)(str - plain_start), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 plain_start = str + mb_l;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001634 msg_puts_attr((char *)transchar(c),
1635 attr == 0 ? HL_ATTR(HLF_8) : attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 retval += char2cells(c);
1637 }
1638 len -= mb_l - 1;
1639 str += mb_l;
1640 }
1641 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642 {
1643 s = transchar_byte(*str);
1644 if (s[1] != NUL)
1645 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001646 // unprintable char: print the printable chars so far and the
1647 // translation of the unprintable char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 if (str > plain_start)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001649 msg_puts_attr_len((char *)plain_start,
1650 (int)(str - plain_start), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 plain_start = str + 1;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001652 msg_puts_attr((char *)s, attr == 0 ? HL_ATTR(HLF_8) : attr);
Bram Moolenaarc236c162008-07-13 17:41:49 +00001653 retval += (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 }
Bram Moolenaarb3c722a2008-07-06 17:16:02 +00001655 else
1656 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 ++str;
1658 }
1659 }
1660
Bram Moolenaar3d30af82020-10-13 22:15:56 +02001661 if (str > plain_start && !got_int)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001662 // print the printable chars at the end
Bram Moolenaar32526b32019-01-19 17:43:09 +01001663 msg_puts_attr_len((char *)plain_start, (int)(str - plain_start), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664
Bram Moolenaar3d30af82020-10-13 22:15:56 +02001665 got_int |= save_got_int;
1666
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 return retval;
1668}
1669
1670#if defined(FEAT_QUICKFIX) || defined(PROTO)
1671 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001672msg_make(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673{
1674 int i;
1675 static char_u *str = (char_u *)"eeffoc", *rs = (char_u *)"Plon#dqg#vxjduB";
1676
1677 arg = skipwhite(arg);
1678 for (i = 5; *arg && i >= 0; --i)
1679 if (*arg++ != str[i])
1680 break;
1681 if (i < 0)
1682 {
1683 msg_putchar('\n');
1684 for (i = 0; rs[i]; ++i)
1685 msg_putchar(rs[i] - 3);
1686 }
1687}
1688#endif
1689
1690/*
Bram Moolenaarf48ee3c2019-12-06 22:18:20 +01001691 * Output the string 'str' up to a NUL character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 * Return the number of characters it takes on the screen.
1693 *
1694 * If K_SPECIAL is encountered, then it is taken in conjunction with the
1695 * following character and shown as <F1>, <S-Up> etc. Any other character
1696 * which is not printable shown in <> form.
1697 * If 'from' is TRUE (lhs of a mapping), a space is shown as <Space>.
1698 * If a character is displayed in one of these special ways, is also
1699 * highlighted (its highlight name is '8' in the p_hl variable).
1700 * Otherwise characters are not highlighted.
1701 * This function is used to show mappings, where we want to see how to type
1702 * the character/string -- webb
1703 */
1704 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001705msg_outtrans_special(
1706 char_u *strstart,
Bram Moolenaar725310d2019-04-24 23:08:23 +02001707 int from, // TRUE for lhs of a mapping
Bram Moolenaar32aa1022019-11-02 22:54:41 +01001708 int maxlen) // screen columns, 0 for unlimited
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709{
1710 char_u *str = strstart;
1711 int retval = 0;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001712 char *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 int attr;
1714 int len;
1715
Bram Moolenaar8820b482017-03-16 17:23:31 +01001716 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 while (*str != NUL)
1718 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001719 // Leading and trailing spaces need to be displayed in <> form.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720 if ((str == strstart || str[1] == NUL) && *str == ' ')
1721 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001722 text = "<Space>";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 ++str;
1724 }
1725 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01001726 text = (char *)str2special(&str, from);
zeertzjq0519ce02022-05-09 12:16:19 +01001727 if (text[0] != NUL && text[1] == NUL)
1728 // single-byte character or illegal byte
1729 text = (char *)transchar_byte((char_u)text[0]);
Bram Moolenaar32526b32019-01-19 17:43:09 +01001730 len = vim_strsize((char_u *)text);
Bram Moolenaar725310d2019-04-24 23:08:23 +02001731 if (maxlen > 0 && retval + len >= maxlen)
1732 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001733 // Highlight special keys
Bram Moolenaar32526b32019-01-19 17:43:09 +01001734 msg_puts_attr(text, len > 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001735 && (*mb_ptr2len)((char_u *)text) <= 1 ? attr : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 retval += len;
1737 }
1738 return retval;
1739}
1740
Bram Moolenaarbd743252010-10-20 21:23:33 +02001741#if defined(FEAT_EVAL) || defined(PROTO)
1742/*
1743 * Return the lhs or rhs of a mapping, with the key codes turned into printable
1744 * strings, in an allocated string.
1745 */
1746 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001747str2special_save(
1748 char_u *str,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001749 int is_lhs) // TRUE for lhs, FALSE for rhs
Bram Moolenaarbd743252010-10-20 21:23:33 +02001750{
1751 garray_T ga;
1752 char_u *p = str;
1753
1754 ga_init2(&ga, 1, 40);
1755 while (*p != NUL)
1756 ga_concat(&ga, str2special(&p, is_lhs));
1757 ga_append(&ga, NUL);
1758 return (char_u *)ga.ga_data;
1759}
1760#endif
1761
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762/*
1763 * Return the printable string for the key codes at "*sp".
zeertzjq0519ce02022-05-09 12:16:19 +01001764 * On illegal byte return a string with only that byte.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765 * Used for translating the lhs or rhs of a mapping to printable chars.
1766 * Advances "sp" to the next code.
1767 */
1768 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001769str2special(
1770 char_u **sp,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001771 int from) // TRUE for lhs of mapping
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772{
1773 int c;
1774 static char_u buf[7];
1775 char_u *str = *sp;
1776 int modifiers = 0;
1777 int special = FALSE;
1778
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 if (has_mbyte)
1780 {
1781 char_u *p;
1782
Bram Moolenaar85a20022019-12-21 18:25:54 +01001783 // Try to un-escape a multi-byte character. Return the un-escaped
1784 // string if it is a multi-byte character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 p = mb_unescape(sp);
1786 if (p != NULL)
1787 return p;
1788 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789
1790 c = *str;
1791 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1792 {
1793 if (str[1] == KS_MODIFIER)
1794 {
1795 modifiers = str[2];
1796 str += 3;
1797 c = *str;
1798 }
1799 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1800 {
1801 c = TO_SPECIAL(str[1], str[2]);
1802 str += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001804 if (IS_SPECIAL(c) || modifiers) // special key
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 special = TRUE;
1806 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807
zeertzjq0519ce02022-05-09 12:16:19 +01001808 if (has_mbyte && !IS_SPECIAL(c) && MB_BYTE2LEN(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 {
zeertzjqac402f42022-05-04 18:51:43 +01001810 char_u *p;
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001811
zeertzjqac402f42022-05-04 18:51:43 +01001812 *sp = str;
1813 // Try to un-escape a multi-byte character after modifiers.
1814 p = mb_unescape(sp);
zeertzjq0519ce02022-05-09 12:16:19 +01001815 if (p != NULL)
1816 // Since 'special' is TRUE the multi-byte character 'c' will be
1817 // processed by get_special_key_name()
1818 c = (*mb_ptr2char)(p);
1819 else
1820 // illegal byte
1821 *sp = str + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 }
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001823 else
Bram Moolenaar083692d2022-06-29 21:16:58 +01001824 // single-byte character, NUL or illegal byte
1825 *sp = str + (*str == NUL ? 0 : 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826
zeertzjq0519ce02022-05-09 12:16:19 +01001827 // Make special keys and C0 control characters in <> form, also <M-Space>.
Bram Moolenaar85a20022019-12-21 18:25:54 +01001828 // Use <Space> only for lhs of a mapping.
zeertzjq0519ce02022-05-09 12:16:19 +01001829 if (special || c < ' ' || (from && c == ' '))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 return get_special_key_name(c, modifiers);
1831 buf[0] = c;
1832 buf[1] = NUL;
1833 return buf;
1834}
1835
1836/*
1837 * Translate a key sequence into special key names.
1838 */
1839 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001840str2specialbuf(char_u *sp, char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841{
1842 char_u *s;
1843
1844 *buf = NUL;
1845 while (*sp)
1846 {
1847 s = str2special(&sp, FALSE);
1848 if ((int)(STRLEN(s) + STRLEN(buf)) < len)
1849 STRCAT(buf, s);
1850 }
1851}
1852
1853/*
1854 * print line for :print or :list command
1855 */
1856 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001857msg_prt_line(char_u *s, int list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858{
1859 int c;
1860 int col = 0;
1861 int n_extra = 0;
1862 int c_extra = 0;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001863 int c_final = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001864 char_u *p_extra = NULL; // init to make SASC shut up
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 int n;
Bram Moolenaar56da7972007-01-16 14:46:32 +00001866 int attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 char_u *trail = NULL;
Bram Moolenaar91478ae2021-02-03 15:58:13 +01001868 char_u *lead = NULL;
zeertzjqf14b8ba2021-09-10 16:58:30 +02001869 int in_multispace = FALSE;
1870 int multispace_pos = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 int l;
1872 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873
Bram Moolenaardf177f62005-02-22 08:39:57 +00001874 if (curwin->w_p_list)
1875 list = TRUE;
1876
Bram Moolenaar91478ae2021-02-03 15:58:13 +01001877 if (list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 {
Bram Moolenaar91478ae2021-02-03 15:58:13 +01001879 // find start of trailing whitespace
Bram Moolenaareed9d462021-02-15 20:38:25 +01001880 if (curwin->w_lcs_chars.trail)
Bram Moolenaar91478ae2021-02-03 15:58:13 +01001881 {
1882 trail = s + STRLEN(s);
1883 while (trail > s && VIM_ISWHITE(trail[-1]))
1884 --trail;
1885 }
1886 // find end of leading whitespace
Bram Moolenaarb8329db2022-07-06 13:31:28 +01001887 if (curwin->w_lcs_chars.lead
1888 || curwin->w_lcs_chars.leadmultispace != NULL)
Bram Moolenaar91478ae2021-02-03 15:58:13 +01001889 {
1890 lead = s;
1891 while (VIM_ISWHITE(lead[0]))
1892 lead++;
1893 // in a line full of spaces all of them are treated as trailing
1894 if (*lead == NUL)
1895 lead = NULL;
1896 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 }
1898
Bram Moolenaar85a20022019-12-21 18:25:54 +01001899 // output a space for an empty line, otherwise the line will be
1900 // overwritten
Bram Moolenaareed9d462021-02-15 20:38:25 +01001901 if (*s == NUL && !(list && curwin->w_lcs_chars.eol != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 msg_putchar(' ');
1903
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001904 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 {
Bram Moolenaar56da7972007-01-16 14:46:32 +00001906 if (n_extra > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 {
1908 --n_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001909 if (n_extra == 0 && c_final)
1910 c = c_final;
1911 else if (c_extra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 c = c_extra;
1913 else
1914 c = *p_extra++;
1915 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001916 else if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 {
1918 col += (*mb_ptr2cells)(s);
Bram Moolenaar1cbfc992020-12-02 12:37:37 +01001919 if (l >= MB_MAXBYTES)
1920 {
Bram Moolenaar29d2f452020-12-04 19:42:52 +01001921 STRCPY(buf, "?");
Bram Moolenaar1cbfc992020-12-02 12:37:37 +01001922 }
Bram Moolenaareed9d462021-02-15 20:38:25 +01001923 else if (curwin->w_lcs_chars.nbsp != NUL && list
Bram Moolenaar73284b92015-05-04 17:28:22 +02001924 && (mb_ptr2char(s) == 160
1925 || mb_ptr2char(s) == 0x202f))
Bram Moolenaaracf17282011-02-01 17:12:25 +01001926 {
Bram Moolenaar74ac29c2022-06-15 12:12:44 +01001927 int len = mb_char2bytes(curwin->w_lcs_chars.nbsp, buf);
1928
1929 buf[len] = NUL;
Bram Moolenaaracf17282011-02-01 17:12:25 +01001930 }
1931 else
1932 {
1933 mch_memmove(buf, s, (size_t)l);
1934 buf[l] = NUL;
1935 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01001936 msg_puts((char *)buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 s += l;
1938 continue;
1939 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 else
1941 {
1942 attr = 0;
1943 c = *s++;
zeertzjqf14b8ba2021-09-10 16:58:30 +02001944 in_multispace = c == ' '
1945 && ((col > 0 && s[-2] == ' ') || *s == ' ');
1946 if (!in_multispace)
1947 multispace_pos = 0;
Bram Moolenaareed9d462021-02-15 20:38:25 +01001948 if (c == TAB && (!list || curwin->w_lcs_chars.tab1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001950 // tab amount depends on current column
Bram Moolenaar04958cb2018-06-23 19:23:02 +02001951#ifdef FEAT_VARTABS
1952 n_extra = tabstop_padding(col, curbuf->b_p_ts,
1953 curbuf->b_p_vts_array) - 1;
1954#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 n_extra = curbuf->b_p_ts - col % curbuf->b_p_ts - 1;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02001956#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00001957 if (!list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 {
1959 c = ' ';
1960 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01001961 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 }
1963 else
1964 {
Bram Moolenaareed9d462021-02-15 20:38:25 +01001965 c = (n_extra == 0 && curwin->w_lcs_chars.tab3)
1966 ? curwin->w_lcs_chars.tab3
1967 : curwin->w_lcs_chars.tab1;
1968 c_extra = curwin->w_lcs_chars.tab2;
1969 c_final = curwin->w_lcs_chars.tab3;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001970 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 }
1972 }
Bram Moolenaareed9d462021-02-15 20:38:25 +01001973 else if (c == 160 && list && curwin->w_lcs_chars.nbsp != NUL)
Bram Moolenaaracf17282011-02-01 17:12:25 +01001974 {
Bram Moolenaareed9d462021-02-15 20:38:25 +01001975 c = curwin->w_lcs_chars.nbsp;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001976 attr = HL_ATTR(HLF_8);
Bram Moolenaaracf17282011-02-01 17:12:25 +01001977 }
Bram Moolenaareed9d462021-02-15 20:38:25 +01001978 else if (c == NUL && list && curwin->w_lcs_chars.eol != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 {
1980 p_extra = (char_u *)"";
1981 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001982 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983 n_extra = 1;
Bram Moolenaareed9d462021-02-15 20:38:25 +01001984 c = curwin->w_lcs_chars.eol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01001985 attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 --s;
1987 }
1988 else if (c != NUL && (n = byte2cells(c)) > 1)
1989 {
1990 n_extra = n - 1;
1991 p_extra = transchar_byte(c);
1992 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001993 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 c = *p_extra++;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001995 // Use special coloring to be able to distinguish <hex> from
1996 // the same in plain text.
Bram Moolenaar8820b482017-03-16 17:23:31 +01001997 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 }
zeertzjqf14b8ba2021-09-10 16:58:30 +02001999 else if (c == ' ')
Bram Moolenaar91478ae2021-02-03 15:58:13 +01002000 {
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01002001 if (list && lead != NULL && s <= lead && in_multispace
2002 && curwin->w_lcs_chars.leadmultispace != NULL)
2003 {
2004 c = curwin->w_lcs_chars.leadmultispace[multispace_pos++];
zeertzjqb5f08012022-06-09 13:55:28 +01002005 if (curwin->w_lcs_chars.leadmultispace[multispace_pos]
2006 == NUL)
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01002007 multispace_pos = 0;
2008 attr = HL_ATTR(HLF_8);
2009 }
zeertzjqb5f08012022-06-09 13:55:28 +01002010 else if (lead != NULL && s <= lead
2011 && curwin->w_lcs_chars.lead != NUL)
zeertzjqf14b8ba2021-09-10 16:58:30 +02002012 {
2013 c = curwin->w_lcs_chars.lead;
2014 attr = HL_ATTR(HLF_8);
2015 }
2016 else if (trail != NULL && s > trail)
2017 {
2018 c = curwin->w_lcs_chars.trail;
2019 attr = HL_ATTR(HLF_8);
2020 }
2021 else if (list && in_multispace
2022 && curwin->w_lcs_chars.multispace != NULL)
2023 {
2024 c = curwin->w_lcs_chars.multispace[multispace_pos++];
2025 if (curwin->w_lcs_chars.multispace[multispace_pos] == NUL)
2026 multispace_pos = 0;
2027 attr = HL_ATTR(HLF_8);
2028 }
2029 else if (list && curwin->w_lcs_chars.space != NUL)
2030 {
2031 c = curwin->w_lcs_chars.space;
2032 attr = HL_ATTR(HLF_8);
2033 }
Bram Moolenaar1510f992015-04-22 22:18:22 +02002034 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 }
2036
2037 if (c == NUL)
2038 break;
2039
2040 msg_putchar_attr(c, attr);
2041 col++;
2042 }
2043 msg_clr_eos();
2044}
2045
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046/*
2047 * Use screen_puts() to output one multi-byte character.
2048 * Return the pointer "s" advanced to the next character.
2049 */
2050 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002051screen_puts_mbyte(char_u *s, int l, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052{
2053 int cw;
2054
Bram Moolenaar85a20022019-12-21 18:25:54 +01002055 msg_didout = TRUE; // remember that line is not empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 cw = (*mb_ptr2cells)(s);
2057 if (cw > 1 && (
2058#ifdef FEAT_RIGHTLEFT
2059 cmdmsg_rl ? msg_col <= 1 :
2060#endif
2061 msg_col == Columns - 1))
2062 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002063 // Doesn't fit, print a highlighted '>' to fill it up.
Bram Moolenaar8820b482017-03-16 17:23:31 +01002064 msg_screen_putchar('>', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065 return s;
2066 }
2067
2068 screen_puts_len(s, l, msg_row, msg_col, attr);
2069#ifdef FEAT_RIGHTLEFT
2070 if (cmdmsg_rl)
2071 {
2072 msg_col -= cw;
2073 if (msg_col == 0)
2074 {
2075 msg_col = Columns;
2076 ++msg_row;
2077 }
2078 }
2079 else
2080#endif
2081 {
2082 msg_col += cw;
2083 if (msg_col >= Columns)
2084 {
2085 msg_col = 0;
2086 ++msg_row;
2087 }
2088 }
2089 return s + l;
2090}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091
2092/*
2093 * Output a string to the screen at position msg_row, msg_col.
2094 * Update msg_row and msg_col for the next message.
2095 */
2096 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002097msg_puts(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098{
Bram Moolenaaraeac9002016-09-06 22:15:08 +02002099 msg_puts_attr(s, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100}
2101
2102 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002103msg_puts_title(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104{
Bram Moolenaar8820b482017-03-16 17:23:31 +01002105 msg_puts_attr(s, HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106}
2107
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108/*
2109 * Show a message in such a way that it always fits in the line. Cut out a
2110 * part in the middle and replace it with "..." when necessary.
2111 * Does not handle multi-byte characters!
2112 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002113 static void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002114msg_outtrans_long_len_attr(char_u *longstr, int len, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115{
2116 int slen = len;
2117 int room;
2118
2119 room = Columns - msg_col;
2120 if (len > room && room >= 20)
2121 {
2122 slen = (room - 3) / 2;
2123 msg_outtrans_len_attr(longstr, slen, attr);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002124 msg_puts_attr("...", HL_ATTR(HLF_8));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 }
2126 msg_outtrans_len_attr(longstr + len - slen, slen, attr);
2127}
2128
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002129 void
2130msg_outtrans_long_attr(char_u *longstr, int attr)
2131{
2132 msg_outtrans_long_len_attr(longstr, (int)STRLEN(longstr), attr);
2133}
2134
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135/*
2136 * Basic function for writing a message with highlight attributes.
2137 */
2138 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002139msg_puts_attr(char *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140{
2141 msg_puts_attr_len(s, -1, attr);
2142}
2143
2144/*
2145 * Like msg_puts_attr(), but with a maximum length "maxlen" (in bytes).
2146 * When "maxlen" is -1 there is no maximum length.
2147 * When "maxlen" is >= 0 the message is not put in the history.
2148 */
2149 static void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002150msg_puts_attr_len(char *str, int maxlen, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 /*
2153 * If redirection is on, also write to the redirection file.
2154 */
Bram Moolenaar32526b32019-01-19 17:43:09 +01002155 redir_write((char_u *)str, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156
2157 /*
2158 * Don't print anything when using ":silent cmd".
2159 */
2160 if (msg_silent != 0)
2161 return;
2162
Bram Moolenaar85a20022019-12-21 18:25:54 +01002163 // if MSG_HIST flag set, add message to history
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 if ((attr & MSG_HIST) && maxlen < 0)
2165 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002166 add_msg_hist((char_u *)str, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 attr &= ~MSG_HIST;
2168 }
2169
Bram Moolenaare8070982019-10-12 17:07:06 +02002170 // When writing something to the screen after it has scrolled, requires a
2171 // wait-return prompt later. Needed when scrolling, resetting
2172 // need_wait_return after some prompt, and then outputting something
2173 // without scrolling
2174 // Not needed when only using CR to move the cursor.
2175 if (msg_scrolled != 0 && !msg_scrolled_ign && STRCMP(str, "\r") != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 need_wait_return = TRUE;
Bram Moolenaare8070982019-10-12 17:07:06 +02002177 msg_didany = TRUE; // remember that something was outputted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178
2179 /*
2180 * If there is no valid screen, use fprintf so we can see error messages.
2181 * If termcap is not active, we may be writing in an alternate console
2182 * window, cursor positioning may not work correctly (window size may be
2183 * different, e.g. for Win32 console) or we just don't know where the
2184 * cursor is.
2185 */
2186 if (msg_use_printf())
Bram Moolenaar32526b32019-01-19 17:43:09 +01002187 msg_puts_printf((char_u *)str, maxlen);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002188 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01002189 msg_puts_display((char_u *)str, maxlen, attr, FALSE);
Rob Pilling726f7f92022-01-20 14:44:38 +00002190
2191 need_fileinfo = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002192}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002194/*
2195 * The display part of msg_puts_attr_len().
2196 * May be called recursively to display scroll-back text.
2197 */
2198 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002199msg_puts_display(
2200 char_u *str,
2201 int maxlen,
2202 int attr,
2203 int recurse)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002204{
2205 char_u *s = str;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002206 char_u *t_s = str; // string from "t_s" to "s" is still todo
2207 int t_col = 0; // screen cells todo, 0 when "t_s" not used
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002208 int l;
2209 int cw;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002210 char_u *sb_str = str;
2211 int sb_col = msg_col;
2212 int wrap;
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002213 int did_last_char;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214
2215 did_wait_return = FALSE;
Bram Moolenaar57b7fe82007-08-05 17:20:43 +00002216 while ((maxlen < 0 || (int)(s - str) < maxlen) && *s != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 {
2218 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002219 * We are at the end of the screen line when:
2220 * - When outputting a newline.
2221 * - When outputting a character in the last column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002223 if (!recurse && msg_row >= Rows - 1 && (*s == '\n' || (
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224#ifdef FEAT_RIGHTLEFT
2225 cmdmsg_rl
2226 ? (
2227 msg_col <= 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002228 || (*s == TAB && msg_col <= 7)
2229 || (has_mbyte && (*mb_ptr2cells)(s) > 1 && msg_col <= 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 :
2231#endif
Bram Moolenaar0efd1bd2019-12-11 19:00:04 +01002232 ((*s != '\r' && msg_col + t_col >= Columns - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 || (*s == TAB && msg_col + t_col >= ((Columns - 1) & ~7))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 || (has_mbyte && (*mb_ptr2cells)(s) > 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002235 && msg_col + t_col >= Columns - 2)))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002237 /*
2238 * The screen is scrolled up when at the last row (some terminals
2239 * scroll automatically, some don't. To avoid problems we scroll
2240 * ourselves).
2241 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 if (t_col > 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002243 // output postponed text
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002244 t_puts(&t_col, t_s, s, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245
Bram Moolenaar85a20022019-12-21 18:25:54 +01002246 // When no more prompt and no more room, truncate here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 if (msg_no_more && lines_left == 0)
2248 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249
Bram Moolenaar85a20022019-12-21 18:25:54 +01002250 // Scroll the screen up one line.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002251 msg_scroll_up();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252
2253 msg_row = Rows - 2;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002254 if (msg_col >= Columns) // can happen after screen resize
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 msg_col = Columns - 1;
2256
Bram Moolenaar85a20022019-12-21 18:25:54 +01002257 // Display char in last column before showing more-prompt.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002258 if (*s >= ' '
2259#ifdef FEAT_RIGHTLEFT
2260 && !cmdmsg_rl
2261#endif
2262 )
2263 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002264 if (has_mbyte)
2265 {
2266 if (enc_utf8 && maxlen >= 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002267 // avoid including composing chars after the end
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002268 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002269 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002270 l = (*mb_ptr2len)(s);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002271 s = screen_puts_mbyte(s, l, attr);
2272 }
2273 else
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002274 msg_screen_putchar(*s++, attr);
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002275 did_last_char = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002276 }
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002277 else
2278 did_last_char = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002279
2280 if (p_more)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002281 // store text for scrolling back
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002282 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
2283
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002284 inc_msg_scrolled();
Bram Moolenaar85a20022019-12-21 18:25:54 +01002285 need_wait_return = TRUE; // may need wait_return in main()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 redraw_cmdline = TRUE;
2287 if (cmdline_row > 0 && !exmode_active)
2288 --cmdline_row;
2289
2290 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002291 * If screen is completely filled and 'more' is set then wait
2292 * for a character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 */
Bram Moolenaar1b0b07f2007-08-07 20:00:31 +00002294 if (lines_left > 0)
2295 --lines_left;
Bram Moolenaar24959102022-05-07 20:01:16 +01002296 if (p_more && lines_left == 0 && State != MODE_HITRETURN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 && !msg_no_more && !exmode_active)
2298 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299#ifdef FEAT_CON_DIALOG
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002300 if (do_more_prompt(NUL))
2301 s = confirm_msg_tail;
2302#else
2303 (void)do_more_prompt(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304#endif
2305 if (quit_more)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002306 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002308
Bram Moolenaar85a20022019-12-21 18:25:54 +01002309 // When we displayed a char in last column need to check if there
2310 // is still more.
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002311 if (did_last_char)
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002312 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 }
2314
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002315 wrap = *s == '\n'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 || msg_col + t_col >= Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 || (has_mbyte && (*mb_ptr2cells)(s) > 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002318 && msg_col + t_col >= Columns - 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002319 if (t_col > 0 && (wrap || *s == '\r' || *s == '\b'
2320 || *s == '\t' || *s == BELL))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002321 // output any postponed text
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002322 t_puts(&t_col, t_s, s, attr);
2323
2324 if (wrap && p_more && !recurse)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002325 // store text for scrolling back
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002326 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327
Bram Moolenaar85a20022019-12-21 18:25:54 +01002328 if (*s == '\n') // go to next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002330 msg_didout = FALSE; // remember that line is empty
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002331#ifdef FEAT_RIGHTLEFT
2332 if (cmdmsg_rl)
2333 msg_col = Columns - 1;
2334 else
2335#endif
2336 msg_col = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002337 if (++msg_row >= Rows) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 msg_row = Rows - 1;
2339 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002340 else if (*s == '\r') // go to column 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 {
2342 msg_col = 0;
2343 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002344 else if (*s == '\b') // go to previous char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 {
2346 if (msg_col)
2347 --msg_col;
2348 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002349 else if (*s == TAB) // translate Tab into spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 {
2351 do
2352 msg_screen_putchar(' ', attr);
2353 while (msg_col & 7);
2354 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002355 else if (*s == BELL) // beep (from ":sh")
Bram Moolenaar165bc692015-07-21 17:53:25 +02002356 vim_beep(BO_SH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 else
2358 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 if (has_mbyte)
2360 {
2361 cw = (*mb_ptr2cells)(s);
2362 if (enc_utf8 && maxlen >= 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002363 // avoid including composing chars after the end
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002364 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002366 l = (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 }
2368 else
2369 {
2370 cw = 1;
2371 l = 1;
2372 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002373
Bram Moolenaar85a20022019-12-21 18:25:54 +01002374 // When drawing from right to left or when a double-wide character
2375 // doesn't fit, draw a single character here. Otherwise collect
2376 // characters and draw them all at once later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 if (
2378# ifdef FEAT_RIGHTLEFT
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002379 cmdmsg_rl ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380# endif
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002381 (cw > 1 && msg_col + t_col >= Columns - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 if (l > 1)
2384 s = screen_puts_mbyte(s, l, attr) - 1;
2385 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 msg_screen_putchar(*s, attr);
2387 }
2388 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002390 // postpone this character until later
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 if (t_col == 0)
2392 t_s = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393 t_col += cw;
2394 s += l - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 }
2396 }
2397 ++s;
2398 }
2399
Bram Moolenaar85a20022019-12-21 18:25:54 +01002400 // output any postponed text
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 if (t_col > 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002402 t_puts(&t_col, t_s, s, attr);
2403 if (p_more && !recurse)
2404 store_sb_text(&sb_str, s, attr, &sb_col, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405
2406 msg_check();
2407}
2408
2409/*
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002410 * Return TRUE when ":filter pattern" was used and "msg" does not match
2411 * "pattern".
2412 */
2413 int
2414message_filtered(char_u *msg)
2415{
Bram Moolenaard29459b2016-08-26 22:29:11 +02002416 int match;
2417
Bram Moolenaare1004402020-10-24 20:49:43 +02002418 if (cmdmod.cmod_filter_regmatch.regprog == NULL)
Bram Moolenaard29459b2016-08-26 22:29:11 +02002419 return FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02002420 match = vim_regexec(&cmdmod.cmod_filter_regmatch, msg, (colnr_T)0);
2421 return cmdmod.cmod_filter_force ? match : !match;
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002422}
2423
2424/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002425 * Scroll the screen up one line for displaying the next message line.
2426 */
2427 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002428msg_scroll_up(void)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002429{
2430#ifdef FEAT_GUI
Bram Moolenaar85a20022019-12-21 18:25:54 +01002431 // Remove the cursor before scrolling, ScreenLines[] is going
2432 // to become invalid.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002433 if (gui.in_use)
2434 gui_undraw_cursor();
2435#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01002436 // scrolling up always works
Bram Moolenaara338adc2018-01-31 20:51:47 +01002437 mch_disable_flush();
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002438 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaara338adc2018-01-31 20:51:47 +01002439 mch_enable_flush();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002440
2441 if (!can_clear((char_u *)" "))
2442 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002443 // Scrolling up doesn't result in the right background. Set the
2444 // background here. It's not efficient, but avoids that we have to do
2445 // it all over the code.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002446 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2447
Bram Moolenaar85a20022019-12-21 18:25:54 +01002448 // Also clear the last char of the last but one line if it was not
2449 // cleared before to avoid a scroll-up.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002450 if (ScreenAttrs[LineOffset[Rows - 2] + Columns - 1] == (sattr_T)-1)
2451 screen_fill((int)Rows - 2, (int)Rows - 1,
2452 (int)Columns - 1, (int)Columns, ' ', ' ', 0);
2453 }
2454}
2455
2456/*
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002457 * Increment "msg_scrolled".
2458 */
2459 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002460inc_msg_scrolled(void)
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002461{
2462#ifdef FEAT_EVAL
2463 if (*get_vim_var_str(VV_SCROLLSTART) == NUL)
2464 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002465 char_u *p = SOURCING_NAME;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002466 char_u *tofree = NULL;
2467 int len;
2468
Bram Moolenaar85a20022019-12-21 18:25:54 +01002469 // v:scrollstart is empty, set it to the script/function name and line
2470 // number
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002471 if (p == NULL)
2472 p = (char_u *)_("Unknown");
2473 else
2474 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002475 len = (int)STRLEN(p) + 40;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002476 tofree = alloc(len);
2477 if (tofree != NULL)
2478 {
2479 vim_snprintf((char *)tofree, len, _("%s line %ld"),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002480 p, (long)SOURCING_LNUM);
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002481 p = tofree;
2482 }
2483 }
2484 set_vim_var_string(VV_SCROLLSTART, p, -1);
2485 vim_free(tofree);
2486 }
2487#endif
2488 ++msg_scrolled;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002489 if (must_redraw < UPD_VALID)
2490 must_redraw = UPD_VALID;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002491}
2492
2493/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002494 * To be able to scroll back at the "more" and "hit-enter" prompts we need to
2495 * store the displayed text and remember where screen lines start.
2496 */
2497typedef struct msgchunk_S msgchunk_T;
2498struct msgchunk_S
2499{
2500 msgchunk_T *sb_next;
2501 msgchunk_T *sb_prev;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002502 char sb_eol; // TRUE when line ends after this text
2503 int sb_msg_col; // column in which text starts
2504 int sb_attr; // text attributes
2505 char_u sb_text[1]; // text to be displayed, actually longer
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002506};
2507
Bram Moolenaar85a20022019-12-21 18:25:54 +01002508static msgchunk_T *last_msgchunk = NULL; // last displayed text
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002509
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002510static msgchunk_T *msg_sb_start(msgchunk_T *mps);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002511
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002512typedef enum {
2513 SB_CLEAR_NONE = 0,
2514 SB_CLEAR_ALL,
2515 SB_CLEAR_CMDLINE_BUSY,
2516 SB_CLEAR_CMDLINE_DONE
2517} sb_clear_T;
2518
Bram Moolenaar85a20022019-12-21 18:25:54 +01002519// When to clear text on next msg.
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002520static sb_clear_T do_clear_sb_text = SB_CLEAR_NONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002521
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002522/*
2523 * Store part of a printed message for displaying when scrolling back.
2524 */
2525 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002526store_sb_text(
Bram Moolenaar85a20022019-12-21 18:25:54 +01002527 char_u **sb_str, // start of string
2528 char_u *s, // just after string
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002529 int attr,
2530 int *sb_col,
Bram Moolenaar85a20022019-12-21 18:25:54 +01002531 int finish) // line ends
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002532{
2533 msgchunk_T *mp;
2534
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002535 if (do_clear_sb_text == SB_CLEAR_ALL
2536 || do_clear_sb_text == SB_CLEAR_CMDLINE_DONE)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002537 {
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002538 clear_sb_text(do_clear_sb_text == SB_CLEAR_ALL);
zeertzjq46af7bc2022-07-28 12:34:09 +01002539 msg_sb_eol(); // prevent messages from overlapping
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002540 do_clear_sb_text = SB_CLEAR_NONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002541 }
2542
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002543 if (s > *sb_str)
2544 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002545 mp = alloc(sizeof(msgchunk_T) + (s - *sb_str));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002546 if (mp != NULL)
2547 {
2548 mp->sb_eol = finish;
2549 mp->sb_msg_col = *sb_col;
2550 mp->sb_attr = attr;
2551 vim_strncpy(mp->sb_text, *sb_str, s - *sb_str);
2552
2553 if (last_msgchunk == NULL)
2554 {
2555 last_msgchunk = mp;
2556 mp->sb_prev = NULL;
2557 }
2558 else
2559 {
2560 mp->sb_prev = last_msgchunk;
2561 last_msgchunk->sb_next = mp;
2562 last_msgchunk = mp;
2563 }
2564 mp->sb_next = NULL;
2565 }
2566 }
2567 else if (finish && last_msgchunk != NULL)
2568 last_msgchunk->sb_eol = TRUE;
2569
2570 *sb_str = s;
2571 *sb_col = 0;
2572}
2573
2574/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002575 * Finished showing messages, clear the scroll-back text on the next message.
2576 */
2577 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002578may_clear_sb_text(void)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002579{
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002580 do_clear_sb_text = SB_CLEAR_ALL;
2581}
2582
2583/*
zeertzjq46af7bc2022-07-28 12:34:09 +01002584 * Starting to edit the command line: do not clear messages now.
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002585 */
2586 void
2587sb_text_start_cmdline(void)
2588{
zeertzjq46af7bc2022-07-28 12:34:09 +01002589 if (do_clear_sb_text == SB_CLEAR_CMDLINE_BUSY)
2590 // Invoking command line recursively: the previous-level command line
2591 // doesn't need to be remembered as it will be redrawn when returning
2592 // to that level.
2593 sb_text_restart_cmdline();
2594 else
2595 {
2596 msg_sb_eol();
2597 do_clear_sb_text = SB_CLEAR_CMDLINE_BUSY;
2598 }
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002599}
2600
2601/*
zeertzjq46af7bc2022-07-28 12:34:09 +01002602 * Redrawing the command line: clear the last unfinished line.
2603 */
2604 void
2605sb_text_restart_cmdline(void)
2606{
2607 msgchunk_T *tofree;
2608
2609 // Needed when returning from nested command line.
2610 do_clear_sb_text = SB_CLEAR_CMDLINE_BUSY;
2611
2612 if (last_msgchunk == NULL || last_msgchunk->sb_eol)
2613 // No unfinished line: don't clear anything.
2614 return;
2615
2616 tofree = msg_sb_start(last_msgchunk);
2617 last_msgchunk = tofree->sb_prev;
2618 if (last_msgchunk != NULL)
2619 last_msgchunk->sb_next = NULL;
2620 while (tofree != NULL)
2621 {
2622 msgchunk_T *tofree_next = tofree->sb_next;
2623
2624 vim_free(tofree);
2625 tofree = tofree_next;
2626 }
2627}
2628
2629/*
2630 * Ending to edit the command line: clear old lines but the last one later.
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002631 */
2632 void
2633sb_text_end_cmdline(void)
2634{
2635 do_clear_sb_text = SB_CLEAR_CMDLINE_DONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002636}
2637
2638/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002639 * Clear any text remembered for scrolling back.
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002640 * When "all" is FALSE keep the last line.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002641 * Called when redrawing the screen.
2642 */
2643 void
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002644clear_sb_text(int all)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002645{
2646 msgchunk_T *mp;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002647 msgchunk_T **lastp;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002648
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002649 if (all)
2650 lastp = &last_msgchunk;
2651 else
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002652 {
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002653 if (last_msgchunk == NULL)
2654 return;
zeertzjqecdc82e2022-07-25 19:50:57 +01002655 lastp = &msg_sb_start(last_msgchunk)->sb_prev;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002656 }
2657
2658 while (*lastp != NULL)
2659 {
2660 mp = (*lastp)->sb_prev;
2661 vim_free(*lastp);
2662 *lastp = mp;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002663 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002664}
2665
2666/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002667 * "g<" command.
2668 */
2669 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002670show_sb_text(void)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002671{
2672 msgchunk_T *mp;
2673
Bram Moolenaar85a20022019-12-21 18:25:54 +01002674 // Only show something if there is more than one line, otherwise it looks
2675 // weird, typing a command without output results in one line.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002676 mp = msg_sb_start(last_msgchunk);
2677 if (mp == NULL || mp->sb_prev == NULL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02002678 vim_beep(BO_MESS);
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002679 else
2680 {
2681 do_more_prompt('G');
2682 wait_return(FALSE);
2683 }
2684}
2685
2686/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002687 * Move to the start of screen line in already displayed text.
2688 */
2689 static msgchunk_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002690msg_sb_start(msgchunk_T *mps)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002691{
2692 msgchunk_T *mp = mps;
2693
2694 while (mp != NULL && mp->sb_prev != NULL && !mp->sb_prev->sb_eol)
2695 mp = mp->sb_prev;
2696 return mp;
2697}
2698
2699/*
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02002700 * Mark the last message chunk as finishing the line.
2701 */
2702 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002703msg_sb_eol(void)
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02002704{
2705 if (last_msgchunk != NULL)
2706 last_msgchunk->sb_eol = TRUE;
2707}
2708
2709/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002710 * Display a screen line from previously displayed text at row "row".
2711 * Returns a pointer to the text for the next line (can be NULL).
2712 */
2713 static msgchunk_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002714disp_sb_line(int row, msgchunk_T *smp)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002715{
2716 msgchunk_T *mp = smp;
2717 char_u *p;
2718
2719 for (;;)
2720 {
2721 msg_row = row;
2722 msg_col = mp->sb_msg_col;
2723 p = mp->sb_text;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002724 if (*p == '\n') // don't display the line break
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002725 ++p;
2726 msg_puts_display(p, -1, mp->sb_attr, TRUE);
2727 if (mp->sb_eol || mp->sb_next == NULL)
2728 break;
2729 mp = mp->sb_next;
2730 }
2731 return mp->sb_next;
2732}
2733
2734/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 * Output any postponed text for msg_puts_attr_len().
2736 */
2737 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002738t_puts(
2739 int *t_col,
2740 char_u *t_s,
2741 char_u *s,
2742 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002744 // output postponed text
2745 msg_didout = TRUE; // remember that line is not empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 screen_puts_len(t_s, (int)(s - t_s), msg_row, msg_col, attr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002747 msg_col += *t_col;
2748 *t_col = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002749 // If the string starts with a composing character don't increment the
2750 // column position for it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 if (enc_utf8 && utf_iscomposing(utf_ptr2char(t_s)))
2752 --msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 if (msg_col >= Columns)
2754 {
2755 msg_col = 0;
2756 ++msg_row;
2757 }
2758}
2759
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760/*
2761 * Returns TRUE when messages should be printed with mch_errmsg().
2762 * This is used when there is no valid screen, so we can see error messages.
2763 * If termcap is not active, we may be writing in an alternate console
2764 * window, cursor positioning may not work correctly (window size may be
2765 * different, e.g. for Win32 console) or we just don't know where the
2766 * cursor is.
2767 */
2768 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002769msg_use_printf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770{
2771 return (!msg_check_screen()
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002772#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
2773# ifdef VIMDLL
2774 || (!gui.in_use && !termcap_active)
2775# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776 || !termcap_active
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002777# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778#endif
2779 || (swapping_screen() && !termcap_active)
2780 );
2781}
2782
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002783/*
2784 * Print a message when there is no valid screen.
2785 */
2786 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002787msg_puts_printf(char_u *str, int maxlen)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002788{
2789 char_u *s = str;
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002790 char_u *buf = NULL;
2791 char_u *p = s;
2792
Bram Moolenaar4f974752019-02-17 17:44:42 +01002793#ifdef MSWIN
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002794 if (!(silent_mode && p_verbose == 0))
Bram Moolenaar85a20022019-12-21 18:25:54 +01002795 mch_settmode(TMODE_COOK); // handle CR and NL correctly
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002796#endif
Bram Moolenaar2321ca22016-09-09 14:17:18 +02002797 while ((maxlen < 0 || (int)(s - str) < maxlen) && *s != NUL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002798 {
2799 if (!(silent_mode && p_verbose == 0))
2800 {
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002801 // NL --> CR NL translation (for Unix, not for "--version")
2802 if (*s == NL)
2803 {
2804 int n = (int)(s - p);
2805
2806 buf = alloc(n + 3);
Bram Moolenaar6f10c702019-08-20 22:58:37 +02002807 if (buf != NULL)
2808 {
2809 memcpy(buf, p, n);
2810 if (!info_message)
2811 buf[n++] = CAR;
2812 buf[n++] = NL;
2813 buf[n++] = NUL;
2814 if (info_message) // informative message, not an error
2815 mch_msg((char *)buf);
2816 else
2817 mch_errmsg((char *)buf);
2818 vim_free(buf);
2819 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002820 p = s + 1;
2821 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002822 }
2823
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002824 // primitive way to compute the current column
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002825#ifdef FEAT_RIGHTLEFT
2826 if (cmdmsg_rl)
2827 {
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002828 if (*s == CAR || *s == NL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002829 msg_col = Columns - 1;
2830 else
2831 --msg_col;
2832 }
2833 else
2834#endif
2835 {
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002836 if (*s == CAR || *s == NL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002837 msg_col = 0;
2838 else
2839 ++msg_col;
2840 }
2841 ++s;
2842 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002843
2844 if (*p != NUL && !(silent_mode && p_verbose == 0))
2845 {
Bram Moolenaara97c3632021-06-15 22:39:11 +02002846 char_u *tofree = NULL;
Bram Moolenaar97c2c052019-02-22 13:42:07 +01002847
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002848 if (maxlen > 0 && STRLEN(p) > (size_t)maxlen)
Bram Moolenaar97c2c052019-02-22 13:42:07 +01002849 {
Bram Moolenaara97c3632021-06-15 22:39:11 +02002850 tofree = vim_strnsave(p, (size_t)maxlen);
2851 p = tofree;
Bram Moolenaar97c2c052019-02-22 13:42:07 +01002852 }
Bram Moolenaara97c3632021-06-15 22:39:11 +02002853 if (p != NULL)
2854 {
2855 if (info_message)
2856 mch_msg((char *)p);
2857 else
2858 mch_errmsg((char *)p);
2859 vim_free(tofree);
2860 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01002861 }
2862
2863 msg_didout = TRUE; // assume that line is not empty
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002864
Bram Moolenaar4f974752019-02-17 17:44:42 +01002865#ifdef MSWIN
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002866 if (!(silent_mode && p_verbose == 0))
2867 mch_settmode(TMODE_RAW);
2868#endif
2869}
2870
2871/*
2872 * Show the more-prompt and handle the user response.
2873 * This takes care of scrolling back and displaying previously displayed text.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002874 * When at hit-enter prompt "typed_char" is the already typed character,
2875 * otherwise it's NUL.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002876 * Returns TRUE when jumping ahead to "confirm_msg_tail".
2877 */
2878 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002879do_more_prompt(int typed_char)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002880{
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002881 static int entered = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002882 int used_typed_char = typed_char;
2883 int oldState = State;
2884 int c;
2885#ifdef FEAT_CON_DIALOG
2886 int retval = FALSE;
2887#endif
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002888 int toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002889 msgchunk_T *mp_last = NULL;
2890 msgchunk_T *mp;
2891 int i;
2892
Bram Moolenaar85a20022019-12-21 18:25:54 +01002893 // We get called recursively when a timer callback outputs a message. In
2894 // that case don't show another prompt. Also when at the hit-Enter prompt
2895 // and nothing was typed.
Bram Moolenaar24959102022-05-07 20:01:16 +01002896 if (entered || (State == MODE_HITRETURN && typed_char == 0))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01002897 return FALSE;
2898 entered = TRUE;
2899
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002900 if (typed_char == 'G')
2901 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002902 // "g<": Find first line on the last page.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002903 mp_last = msg_sb_start(last_msgchunk);
2904 for (i = 0; i < Rows - 2 && mp_last != NULL
2905 && mp_last->sb_prev != NULL; ++i)
2906 mp_last = msg_sb_start(mp_last->sb_prev);
2907 }
2908
Bram Moolenaar24959102022-05-07 20:01:16 +01002909 State = MODE_ASKMORE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002910 setmouse();
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002911 if (typed_char == NUL)
2912 msg_moremsg(FALSE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002913 for (;;)
2914 {
2915 /*
2916 * Get a typed character directly from the user.
2917 */
2918 if (used_typed_char != NUL)
2919 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002920 c = used_typed_char; // was typed at hit-enter prompt
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002921 used_typed_char = NUL;
2922 }
2923 else
2924 c = get_keystroke();
2925
2926#if defined(FEAT_MENU) && defined(FEAT_GUI)
2927 if (c == K_MENU)
2928 {
Bram Moolenaar24959102022-05-07 20:01:16 +01002929 int idx = get_menu_index(current_menu, MODE_ASKMORE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002930
Bram Moolenaar85a20022019-12-21 18:25:54 +01002931 // Used a menu. If it starts with CTRL-Y, it must
2932 // be a "Copy" for the clipboard. Otherwise
2933 // assume that we end
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002934 if (idx == MENU_INDEX_INVALID)
2935 continue;
2936 c = *current_menu->strings[idx];
2937 if (c != NUL && current_menu->strings[idx][1] != NUL)
2938 ins_typebuf(current_menu->strings[idx] + 1,
2939 current_menu->noremap[idx], 0, TRUE,
2940 current_menu->silent[idx]);
2941 }
2942#endif
2943
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002944 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002945 switch (c)
2946 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002947 case BS: // scroll one line back
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002948 case K_BS:
2949 case 'k':
2950 case K_UP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002951 toscroll = -1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002952 break;
2953
Bram Moolenaar85a20022019-12-21 18:25:54 +01002954 case CAR: // one extra line
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002955 case NL:
2956 case 'j':
2957 case K_DOWN:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002958 toscroll = 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002959 break;
2960
Bram Moolenaar85a20022019-12-21 18:25:54 +01002961 case 'u': // Up half a page
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002962 toscroll = -(Rows / 2);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002963 break;
2964
Bram Moolenaar85a20022019-12-21 18:25:54 +01002965 case 'd': // Down half a page
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002966 toscroll = Rows / 2;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002967 break;
2968
Bram Moolenaar85a20022019-12-21 18:25:54 +01002969 case 'b': // one page back
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00002970 case K_PAGEUP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002971 toscroll = -(Rows - 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002972 break;
2973
Bram Moolenaar85a20022019-12-21 18:25:54 +01002974 case ' ': // one extra page
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00002975 case 'f':
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002976 case K_PAGEDOWN:
2977 case K_LEFTMOUSE:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002978 toscroll = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002979 break;
2980
Bram Moolenaar85a20022019-12-21 18:25:54 +01002981 case 'g': // all the way back to the start
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002982 toscroll = -999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002983 break;
2984
Bram Moolenaar85a20022019-12-21 18:25:54 +01002985 case 'G': // all the way to the end
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002986 toscroll = 999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002987 lines_left = 999999;
2988 break;
2989
Bram Moolenaar85a20022019-12-21 18:25:54 +01002990 case ':': // start new command line
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002991#ifdef FEAT_CON_DIALOG
2992 if (!confirm_msg_used)
2993#endif
2994 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002995 // Since got_int is set all typeahead will be flushed, but we
2996 // want to keep this ':', remember that in a special way.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002997 typeahead_noflush(':');
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02002998#ifdef FEAT_TERMINAL
2999 skip_term_loop = TRUE;
3000#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01003001 cmdline_row = Rows - 1; // put ':' on this line
3002 skip_redraw = TRUE; // skip redraw once
3003 need_wait_return = FALSE; // don't wait in main()
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003004 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003005 // FALLTHROUGH
3006 case 'q': // quit
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003007 case Ctrl_C:
3008 case ESC:
3009#ifdef FEAT_CON_DIALOG
3010 if (confirm_msg_used)
3011 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003012 // Jump to the choices of the dialog.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003013 retval = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003014 }
3015 else
3016#endif
3017 {
3018 got_int = TRUE;
3019 quit_more = TRUE;
3020 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003021 // When there is some more output (wrapping line) display that
3022 // without another prompt.
Bram Moolenaar51306d22009-02-24 03:38:04 +00003023 lines_left = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003024 break;
3025
3026#ifdef FEAT_CLIPBOARD
3027 case Ctrl_Y:
Bram Moolenaar85a20022019-12-21 18:25:54 +01003028 // Strange way to allow copying (yanking) a modeless
3029 // selection at the more prompt. Use CTRL-Y,
3030 // because the same is used in Cmdline-mode and at the
3031 // hit-enter prompt. However, scrolling one line up
3032 // might be expected...
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003033 if (clip_star.state == SELECT_DONE)
3034 clip_copy_modeless_selection(TRUE);
3035 continue;
3036#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01003037 default: // no valid response
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003038 msg_moremsg(TRUE);
3039 continue;
3040 }
3041
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003042 if (toscroll != 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003043 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003044 if (toscroll < 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003045 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003046 // go to start of last line
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003047 if (mp_last == NULL)
3048 mp = msg_sb_start(last_msgchunk);
3049 else if (mp_last->sb_prev != NULL)
3050 mp = msg_sb_start(mp_last->sb_prev);
3051 else
3052 mp = NULL;
3053
Bram Moolenaar85a20022019-12-21 18:25:54 +01003054 // go to start of line at top of the screen
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003055 for (i = 0; i < Rows - 2 && mp != NULL && mp->sb_prev != NULL;
3056 ++i)
3057 mp = msg_sb_start(mp->sb_prev);
3058
3059 if (mp != NULL && mp->sb_prev != NULL)
3060 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003061 // Find line to be displayed at top.
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003062 for (i = 0; i > toscroll; --i)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003063 {
3064 if (mp == NULL || mp->sb_prev == NULL)
3065 break;
3066 mp = msg_sb_start(mp->sb_prev);
3067 if (mp_last == NULL)
3068 mp_last = msg_sb_start(last_msgchunk);
3069 else
3070 mp_last = msg_sb_start(mp_last->sb_prev);
3071 }
3072
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003073 if (toscroll == -1 && screen_ins_lines(0, 0, 1,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003074 (int)Rows, 0, NULL) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003075 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003076 // display line at top
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003077 (void)disp_sb_line(0, mp);
3078 }
3079 else
3080 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003081 // redisplay all lines
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003082 screenclear();
Bram Moolenaar773560b2006-05-06 21:38:18 +00003083 for (i = 0; mp != NULL && i < Rows - 1; ++i)
3084 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003085 mp = disp_sb_line(i, mp);
Bram Moolenaar773560b2006-05-06 21:38:18 +00003086 ++msg_scrolled;
3087 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003088 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003089 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003090 }
3091 }
3092 else
3093 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003094 // First display any text that we scrolled back.
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003095 while (toscroll > 0 && mp_last != NULL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003096 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003097 // scroll up, display line at bottom
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003098 msg_scroll_up();
Bram Moolenaarbb15b652005-10-03 21:52:09 +00003099 inc_msg_scrolled();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003100 screen_fill((int)Rows - 2, (int)Rows - 1, 0,
3101 (int)Columns, ' ', ' ', 0);
3102 mp_last = disp_sb_line((int)Rows - 2, mp_last);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003103 --toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003104 }
3105 }
3106
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003107 if (toscroll <= 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003108 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003109 // displayed the requested text, more prompt again
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003110 screen_fill((int)Rows - 1, (int)Rows, 0,
3111 (int)Columns, ' ', ' ', 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003112 msg_moremsg(FALSE);
3113 continue;
3114 }
3115
Bram Moolenaar85a20022019-12-21 18:25:54 +01003116 // display more text, return to caller
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003117 lines_left = toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003118 }
3119
3120 break;
3121 }
3122
Bram Moolenaar85a20022019-12-21 18:25:54 +01003123 // clear the --more-- message
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003124 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3125 State = oldState;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003126 setmouse();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003127 if (quit_more)
3128 {
3129 msg_row = Rows - 1;
3130 msg_col = 0;
3131 }
3132#ifdef FEAT_RIGHTLEFT
3133 else if (cmdmsg_rl)
3134 msg_col = Columns - 1;
3135#endif
3136
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01003137 entered = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003138#ifdef FEAT_CON_DIALOG
3139 return retval;
3140#else
3141 return FALSE;
3142#endif
3143}
3144
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145#if defined(USE_MCH_ERRMSG) || defined(PROTO)
3146
3147#ifdef mch_errmsg
3148# undef mch_errmsg
3149#endif
3150#ifdef mch_msg
3151# undef mch_msg
3152#endif
3153
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003154#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3155 static void
3156mch_errmsg_c(char *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157{
Bram Moolenaar0f77d6a2019-02-14 20:55:09 +01003158 int len = (int)STRLEN(str);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003159 DWORD nwrite = 0;
3160 DWORD mode = 0;
3161 HANDLE h = GetStdHandle(STD_ERROR_HANDLE);
3162
3163 if (GetConsoleMode(h, &mode) && enc_codepage >= 0
3164 && (int)GetConsoleCP() != enc_codepage)
3165 {
3166 WCHAR *w = enc_to_utf16((char_u *)str, &len);
3167
3168 WriteConsoleW(h, w, len, &nwrite, NULL);
3169 vim_free(w);
3170 }
3171 else
3172 {
3173 fprintf(stderr, "%s", str);
3174 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003175}
3176#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003178/*
3179 * Give an error message. To be used when the screen hasn't been initialized
3180 * yet. When stderr can't be used, collect error messages until the GUI has
3181 * started and they can be displayed in a message box.
3182 */
3183 void
3184mch_errmsg(char *str)
3185{
3186#if !defined(MSWIN) || defined(FEAT_GUI_MSWIN)
3187 int len;
3188#endif
3189
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02003190#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003191 // On Unix use stderr if it's a tty.
3192 // When not going to start the GUI also use stderr.
3193 // On Mac, when started from Finder, stderr is the console.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 if (
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003195# ifdef UNIX
3196# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003198# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 isatty(2)
3200# endif
3201# ifdef FEAT_GUI
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003202 ||
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003203# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003204# endif
3205# ifdef FEAT_GUI
3206 !(gui.in_use || gui.starting)
3207# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 )
3209 {
3210 fprintf(stderr, "%s", str);
3211 return;
3212 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003213#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003215#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3216# ifdef VIMDLL
3217 if (!(gui.in_use || gui.starting))
3218# endif
3219 {
3220 mch_errmsg_c(str);
3221 return;
3222 }
3223#endif
3224
3225#if !defined(MSWIN) || defined(FEAT_GUI_MSWIN)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003226 // avoid a delay for a message that isn't there
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 emsg_on_display = FALSE;
3228
3229 len = (int)STRLEN(str) + 1;
3230 if (error_ga.ga_growsize == 0)
3231 {
3232 error_ga.ga_growsize = 80;
3233 error_ga.ga_itemsize = 1;
3234 }
3235 if (ga_grow(&error_ga, len) == OK)
3236 {
3237 mch_memmove((char_u *)error_ga.ga_data + error_ga.ga_len,
3238 (char_u *)str, len);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003239# ifdef UNIX
Bram Moolenaar85a20022019-12-21 18:25:54 +01003240 // remove CR characters, they are displayed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 {
3242 char_u *p;
3243
3244 p = (char_u *)error_ga.ga_data + error_ga.ga_len;
3245 for (;;)
3246 {
3247 p = vim_strchr(p, '\r');
3248 if (p == NULL)
3249 break;
3250 *p = ' ';
3251 }
3252 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003253# endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01003254 --len; // don't count the NUL at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 error_ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003257#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258}
3259
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003260#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3261 static void
3262mch_msg_c(char *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263{
Bram Moolenaar0f77d6a2019-02-14 20:55:09 +01003264 int len = (int)STRLEN(str);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003265 DWORD nwrite = 0;
3266 DWORD mode;
3267 HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
3268
3269
3270 if (GetConsoleMode(h, &mode) && enc_codepage >= 0
3271 && (int)GetConsoleCP() != enc_codepage)
3272 {
3273 WCHAR *w = enc_to_utf16((char_u *)str, &len);
3274
3275 WriteConsoleW(h, w, len, &nwrite, NULL);
3276 vim_free(w);
3277 }
3278 else
3279 {
3280 printf("%s", str);
3281 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003282}
3283#endif
3284
3285/*
3286 * Give a message. To be used when the screen hasn't been initialized yet.
3287 * When there is no tty, collect messages until the GUI has started and they
3288 * can be displayed in a message box.
3289 */
3290 void
3291mch_msg(char *str)
3292{
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02003293#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003294 // On Unix use stdout if we have a tty. This allows "vim -h | more" and
3295 // uses mch_errmsg() when started from the desktop.
3296 // When not going to start the GUI also use stdout.
3297 // On Mac, when started from Finder, stderr is the console.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 if (
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003299# ifdef UNIX
3300# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003302# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 isatty(2)
Bram Moolenaareae1b912019-05-09 15:12:55 +02003304# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305# ifdef FEAT_GUI
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003306 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003308# endif
3309# ifdef FEAT_GUI
3310 !(gui.in_use || gui.starting)
3311# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 )
3313 {
3314 printf("%s", str);
3315 return;
3316 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003317#endif
3318
3319#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3320# ifdef VIMDLL
3321 if (!(gui.in_use || gui.starting))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003323 {
3324 mch_msg_c(str);
3325 return;
3326 }
3327#endif
3328#if !defined(MSWIN) || defined(FEAT_GUI_MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 mch_errmsg(str);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003330#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331}
Bram Moolenaar85a20022019-12-21 18:25:54 +01003332#endif // USE_MCH_ERRMSG
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333
3334/*
3335 * Put a character on the screen at the current message position and advance
3336 * to the next position. Only for printable ASCII!
3337 */
3338 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003339msg_screen_putchar(int c, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340{
Bram Moolenaar85a20022019-12-21 18:25:54 +01003341 msg_didout = TRUE; // remember that line is not empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 screen_putchar(c, msg_row, msg_col, attr);
3343#ifdef FEAT_RIGHTLEFT
3344 if (cmdmsg_rl)
3345 {
3346 if (--msg_col == 0)
3347 {
3348 msg_col = Columns;
3349 ++msg_row;
3350 }
3351 }
3352 else
3353#endif
3354 {
3355 if (++msg_col >= Columns)
3356 {
3357 msg_col = 0;
3358 ++msg_row;
3359 }
3360 }
3361}
3362
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003363 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003364msg_moremsg(int full)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003366 int attr;
3367 char_u *s = (char_u *)_("-- More --");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368
Bram Moolenaar8820b482017-03-16 17:23:31 +01003369 attr = HL_ATTR(HLF_M);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003370 screen_puts(s, (int)Rows - 1, 0, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 if (full)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003372 screen_puts((char_u *)
3373 _(" SPACE/d/j: screen/page/line down, b/u/k: up, q: quit "),
3374 (int)Rows - 1, vim_strsize(s), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375}
3376
3377/*
Bram Moolenaar24959102022-05-07 20:01:16 +01003378 * Repeat the message for the current mode: MODE_ASKMORE, MODE_EXTERNCMD,
3379 * MODE_CONFIRM or exmode_active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 */
3381 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003382repeat_message(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383{
Bram Moolenaar24959102022-05-07 20:01:16 +01003384 if (State == MODE_ASKMORE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003386 msg_moremsg(TRUE); // display --more-- message again
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 msg_row = Rows - 1;
3388 }
3389#ifdef FEAT_CON_DIALOG
Bram Moolenaar24959102022-05-07 20:01:16 +01003390 else if (State == MODE_CONFIRM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003392 display_confirm_msg(); // display ":confirm" message again
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 msg_row = Rows - 1;
3394 }
3395#endif
Bram Moolenaar24959102022-05-07 20:01:16 +01003396 else if (State == MODE_EXTERNCMD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003398 windgoto(msg_row, msg_col); // put cursor back
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 }
Bram Moolenaar24959102022-05-07 20:01:16 +01003400 else if (State == MODE_HITRETURN || State == MODE_SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 {
Bram Moolenaar9f108752007-11-24 14:44:58 +00003402 if (msg_row == Rows - 1)
3403 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003404 // Avoid drawing the "hit-enter" prompt below the previous one,
3405 // overwrite it. Esp. useful when regaining focus and a
3406 // FocusGained autocmd exists but didn't draw anything.
Bram Moolenaar9f108752007-11-24 14:44:58 +00003407 msg_didout = FALSE;
3408 msg_col = 0;
3409 msg_clr_eos();
3410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 hit_return_msg();
3412 msg_row = Rows - 1;
3413 }
3414}
3415
3416/*
3417 * msg_check_screen - check if the screen is initialized.
3418 * Also check msg_row and msg_col, if they are too big it may cause a crash.
3419 * While starting the GUI the terminal codes will be set for the GUI, but the
3420 * output goes to the terminal. Don't use the terminal codes then.
3421 */
3422 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003423msg_check_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424{
3425 if (!full_screen || !screen_valid(FALSE))
3426 return FALSE;
3427
3428 if (msg_row >= Rows)
3429 msg_row = Rows - 1;
3430 if (msg_col >= Columns)
3431 msg_col = Columns - 1;
3432 return TRUE;
3433}
3434
3435/*
3436 * Clear from current message position to end of screen.
3437 * Skip this when ":silent" was used, no need to clear for redirection.
3438 */
3439 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003440msg_clr_eos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441{
3442 if (msg_silent == 0)
3443 msg_clr_eos_force();
3444}
3445
3446/*
3447 * Clear from current message position to end of screen.
3448 * Note: msg_col is not updated, so we remember the end of the message
3449 * for msg_check().
3450 */
3451 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003452msg_clr_eos_force(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453{
3454 if (msg_use_printf())
3455 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003456 if (full_screen) // only when termcap codes are valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 {
3458 if (*T_CD)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003459 out_str(T_CD); // clear to end of display
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 else if (*T_CE)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003461 out_str(T_CE); // clear to end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 }
3463 }
Shougo Matsushitaf39cfb72022-07-30 16:54:05 +01003464 else if (p_ch > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 {
3466#ifdef FEAT_RIGHTLEFT
3467 if (cmdmsg_rl)
3468 {
3469 screen_fill(msg_row, msg_row + 1, 0, msg_col + 1, ' ', ' ', 0);
3470 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3471 }
3472 else
3473#endif
3474 {
3475 screen_fill(msg_row, msg_row + 1, msg_col, (int)Columns,
3476 ' ', ' ', 0);
3477 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3478 }
3479 }
3480}
3481
3482/*
3483 * Clear the command line.
3484 */
3485 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003486msg_clr_cmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487{
3488 msg_row = cmdline_row;
3489 msg_col = 0;
3490 msg_clr_eos_force();
3491}
3492
3493/*
3494 * end putting a message on the screen
3495 * call wait_return if the message does not fit in the available space
3496 * return TRUE if wait_return not called.
3497 */
3498 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003499msg_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500{
3501 /*
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02003502 * If the string is larger than the window,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 * or the ruler option is set and we run into it,
3504 * we have to redraw the window.
3505 * Do not do this if we are abandoning the file or editing the command line.
3506 */
Bram Moolenaar24959102022-05-07 20:01:16 +01003507 if (!exiting && need_wait_return && !(State & MODE_CMDLINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 {
3509 wait_return(FALSE);
3510 return FALSE;
3511 }
3512 out_flush();
3513 return TRUE;
3514}
3515
3516/*
3517 * If the written message runs into the shown command or ruler, we have to
3518 * wait for hit-return and redraw the window later.
3519 */
3520 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003521msg_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522{
3523 if (msg_row == Rows - 1 && msg_col >= sc_col)
3524 {
3525 need_wait_return = TRUE;
3526 redraw_cmdline = TRUE;
3527 }
3528}
3529
3530/*
3531 * May write a string to the redirection file.
3532 * When "maxlen" is -1 write the whole string, otherwise up to "maxlen" bytes.
3533 */
3534 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003535redir_write(char_u *str, int maxlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536{
3537 char_u *s = str;
3538 static int cur_col = 0;
3539
Bram Moolenaar85a20022019-12-21 18:25:54 +01003540 // Don't do anything for displaying prompts and the like.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003541 if (redir_off)
3542 return;
3543
Bram Moolenaar85a20022019-12-21 18:25:54 +01003544 // If 'verbosefile' is set prepare for writing in that file.
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003545 if (*p_vfile != NUL && verbose_fd == NULL)
3546 verbose_open();
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003547
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003548 if (redirecting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003550 // If the string doesn't start with CR or NL, go to msg_col
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 if (*s != '\n' && *s != '\r')
3552 {
3553 while (cur_col < msg_col)
3554 {
3555#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003556 if (redir_execute)
3557 execute_redir_str((char_u *)" ", -1);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003558 else if (redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 write_reg_contents(redir_reg, (char_u *)" ", -1, TRUE);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003560 else if (redir_vname)
3561 var_redir_str((char_u *)" ", -1);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003562 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003564 if (redir_fd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 fputs(" ", redir_fd);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003566 if (verbose_fd != NULL)
3567 fputs(" ", verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 ++cur_col;
3569 }
3570 }
3571
3572#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003573 if (redir_execute)
3574 execute_redir_str(s, maxlen);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003575 else if (redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 write_reg_contents(redir_reg, s, maxlen, TRUE);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003577 else if (redir_vname)
Bram Moolenaardf177f62005-02-22 08:39:57 +00003578 var_redir_str(s, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579#endif
3580
Bram Moolenaar85a20022019-12-21 18:25:54 +01003581 // Write and adjust the current column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
3583 {
3584#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003585 if (!redir_reg && !redir_vname && !redir_execute)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003587 if (redir_fd != NULL)
3588 putc(*s, redir_fd);
3589 if (verbose_fd != NULL)
3590 putc(*s, verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 if (*s == '\r' || *s == '\n')
3592 cur_col = 0;
3593 else if (*s == '\t')
3594 cur_col += (8 - cur_col % 8);
3595 else
3596 ++cur_col;
3597 ++s;
3598 }
3599
Bram Moolenaar85a20022019-12-21 18:25:54 +01003600 if (msg_silent != 0) // should update msg_col
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 msg_col = cur_col;
3602 }
3603}
3604
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003605 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003606redirecting(void)
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003607{
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003608 return redir_fd != NULL || *p_vfile != NUL
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003609#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003610 || redir_reg || redir_vname || redir_execute
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003611#endif
3612 ;
3613}
3614
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615/*
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00003616 * Before giving verbose message.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003617 * Must always be called paired with verbose_leave()!
3618 */
3619 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003620verbose_enter(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003621{
3622 if (*p_vfile != NUL)
3623 ++msg_silent;
3624}
3625
3626/*
3627 * After giving verbose message.
3628 * Must always be called paired with verbose_enter()!
3629 */
3630 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003631verbose_leave(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003632{
3633 if (*p_vfile != NUL)
3634 if (--msg_silent < 0)
3635 msg_silent = 0;
3636}
3637
3638/*
3639 * Like verbose_enter() and set msg_scroll when displaying the message.
3640 */
3641 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003642verbose_enter_scroll(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003643{
3644 if (*p_vfile != NUL)
3645 ++msg_silent;
3646 else
Bram Moolenaar85a20022019-12-21 18:25:54 +01003647 // always scroll up, don't overwrite
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003648 msg_scroll = TRUE;
3649}
3650
3651/*
3652 * Like verbose_leave() and set cmdline_row when displaying the message.
3653 */
3654 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003655verbose_leave_scroll(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003656{
3657 if (*p_vfile != NUL)
3658 {
3659 if (--msg_silent < 0)
3660 msg_silent = 0;
3661 }
3662 else
3663 cmdline_row = msg_row;
3664}
3665
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003666/*
3667 * Called when 'verbosefile' is set: stop writing to the file.
3668 */
3669 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003670verbose_stop(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003671{
3672 if (verbose_fd != NULL)
3673 {
3674 fclose(verbose_fd);
3675 verbose_fd = NULL;
3676 }
3677 verbose_did_open = FALSE;
3678}
3679
3680/*
3681 * Open the file 'verbosefile'.
3682 * Return FAIL or OK.
3683 */
3684 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003685verbose_open(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003686{
3687 if (verbose_fd == NULL && !verbose_did_open)
3688 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003689 // Only give the error message once.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003690 verbose_did_open = TRUE;
3691
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003692 verbose_fd = mch_fopen((char *)p_vfile, "a");
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003693 if (verbose_fd == NULL)
3694 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003695 semsg(_(e_cant_open_file_str), p_vfile);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003696 return FAIL;
3697 }
3698 }
3699 return OK;
3700}
3701
3702/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 * Give a warning message (for searching).
3704 * Use 'w' highlighting and may repeat the message after redrawing
3705 */
3706 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003707give_warning(char_u *message, int hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708{
Bram Moolenaarcc7eb2a2021-07-11 19:12:04 +02003709 give_warning_with_source(message, hl, FALSE);
3710}
3711
3712 void
3713give_warning_with_source(char_u *message, int hl, int with_source)
3714{
Bram Moolenaar85a20022019-12-21 18:25:54 +01003715 // Don't do this for ":silent".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 if (msg_silent != 0)
3717 return;
3718
Bram Moolenaar85a20022019-12-21 18:25:54 +01003719 // Don't want a hit-enter prompt here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 ++no_wait_return;
Bram Moolenaared203462004-06-16 11:19:22 +00003721
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722#ifdef FEAT_EVAL
3723 set_vim_var_string(VV_WARNINGMSG, message, -1);
3724#endif
Bram Moolenaard23a8232018-02-10 18:45:26 +01003725 VIM_CLEAR(keep_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 if (hl)
Bram Moolenaar8820b482017-03-16 17:23:31 +01003727 keep_msg_attr = HL_ATTR(HLF_W);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 else
3729 keep_msg_attr = 0;
Bram Moolenaarcc7eb2a2021-07-11 19:12:04 +02003730
3731 if (with_source)
3732 {
3733 // Do what msg() does, but with a column offset if the warning should
3734 // be after the mode message.
3735 msg_start();
3736 msg_source(HL_ATTR(HLF_W));
3737 msg_puts(" ");
3738 msg_puts_attr((char *)message, HL_ATTR(HLF_W) | MSG_HIST);
3739 msg_clr_eos();
3740 (void)msg_end();
3741 }
3742 else if (msg_attr((char *)message, keep_msg_attr) && msg_scrolled == 0)
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003743 set_keep_msg(message, keep_msg_attr);
Bram Moolenaarcc7eb2a2021-07-11 19:12:04 +02003744
Bram Moolenaar85a20022019-12-21 18:25:54 +01003745 msg_didout = FALSE; // overwrite this message
3746 msg_nowait = TRUE; // don't wait for this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 msg_col = 0;
Bram Moolenaared203462004-06-16 11:19:22 +00003748
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 --no_wait_return;
3750}
3751
Bram Moolenaar113e1072019-01-20 15:30:40 +01003752#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarf8be4612017-06-23 20:52:40 +02003753 void
3754give_warning2(char_u *message, char_u *a1, int hl)
3755{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +02003756 if (IObuff == NULL)
3757 {
3758 // Very early in initialisation and already something wrong, just give
3759 // the raw message so the user at least gets a hint.
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003760 give_warning(message, hl);
Bram Moolenaare3a22cb2019-10-14 22:01:57 +02003761 }
3762 else
3763 {
3764 vim_snprintf((char *)IObuff, IOSIZE, (char *)message, a1);
3765 give_warning(IObuff, hl);
3766 }
Bram Moolenaarf8be4612017-06-23 20:52:40 +02003767}
Bram Moolenaar113e1072019-01-20 15:30:40 +01003768#endif
Bram Moolenaarf8be4612017-06-23 20:52:40 +02003769
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770/*
3771 * Advance msg cursor to column "col".
3772 */
3773 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003774msg_advance(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775{
Bram Moolenaar85a20022019-12-21 18:25:54 +01003776 if (msg_silent != 0) // nothing to advance to
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003778 msg_col = col; // for redirection, may fill it up later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 return;
3780 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003781 if (col >= Columns) // not enough room
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 col = Columns - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003783#ifdef FEAT_RIGHTLEFT
3784 if (cmdmsg_rl)
3785 while (msg_col > Columns - col)
3786 msg_putchar(' ');
3787 else
3788#endif
3789 while (msg_col < col)
3790 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791}
3792
3793#if defined(FEAT_CON_DIALOG) || defined(PROTO)
3794/*
3795 * Used for "confirm()" function, and the :confirm command prefix.
3796 * Versions which haven't got flexible dialogs yet, and console
3797 * versions, get this generic handler which uses the command line.
3798 *
3799 * type = one of:
3800 * VIM_QUESTION, VIM_INFO, VIM_WARNING, VIM_ERROR or VIM_GENERIC
3801 * title = title string (can be NULL for default)
3802 * (neither used in console dialogs at the moment)
3803 *
3804 * Format of the "buttons" string:
3805 * "Button1Name\nButton2Name\nButton3Name"
3806 * The first button should normally be the default/accept
3807 * The second button should be the 'Cancel' button
3808 * Other buttons- use your imagination!
3809 * A '&' in a button name becomes a shortcut, so each '&' should be before a
3810 * different letter.
Rob Pilling8196e942022-02-11 15:12:10 +00003811 *
3812 * Returns 0 if cancelled, otherwise the nth button (1-indexed).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003815do_dialog(
3816 int type UNUSED,
3817 char_u *title UNUSED,
3818 char_u *message,
3819 char_u *buttons,
3820 int dfltbutton,
Bram Moolenaar85a20022019-12-21 18:25:54 +01003821 char_u *textfield UNUSED, // IObuff for inputdialog(), NULL
3822 // otherwise
3823 int ex_cmd) // when TRUE pressing : accepts default and starts
3824 // Ex command
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825{
3826 int oldState;
3827 int retval = 0;
3828 char_u *hotkeys;
3829 int c;
3830 int i;
Bram Moolenaar27321db2020-07-06 21:24:57 +02003831 tmode_T save_tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832
3833#ifndef NO_CONSOLE
Bram Moolenaar85a20022019-12-21 18:25:54 +01003834 // Don't output anything in silent mode ("ex -s")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 if (silent_mode)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003836 return dfltbutton; // return default option
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837#endif
3838
3839#ifdef FEAT_GUI_DIALOG
Bram Moolenaar85a20022019-12-21 18:25:54 +01003840 // When GUI is running and 'c' not in 'guioptions', use the GUI dialog
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
3842 {
Bram Moolenaar2d12c252022-06-13 21:42:45 +01003843 // --gui-dialog-file: write text to a file
3844 if (gui_dialog_log(title, message))
3845 c = dfltbutton;
3846 else
3847 c = gui_mch_dialog(type, title, message, buttons, dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003848 textfield, ex_cmd);
Bram Moolenaar85a20022019-12-21 18:25:54 +01003849 // avoid a hit-enter prompt without clearing the cmdline
Bram Moolenaar8de49e12009-02-11 17:47:54 +00003850 need_wait_return = FALSE;
3851 emsg_on_display = FALSE;
3852 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853
Bram Moolenaar85a20022019-12-21 18:25:54 +01003854 // Flush output to avoid that further messages and redrawing is done
3855 // in the wrong order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 out_flush();
3857 gui_mch_update();
3858
3859 return c;
3860 }
3861#endif
3862
3863 oldState = State;
Bram Moolenaar24959102022-05-07 20:01:16 +01003864 State = MODE_CONFIRM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866
Bram Moolenaar27321db2020-07-06 21:24:57 +02003867 // Ensure raw mode here.
3868 save_tmode = cur_tmode;
3869 settmode(TMODE_RAW);
3870
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 /*
3872 * Since we wait for a keypress, don't make the
3873 * user press RETURN as well afterwards.
3874 */
3875 ++no_wait_return;
3876 hotkeys = msg_show_console_dialog(message, buttons, dfltbutton);
3877
3878 if (hotkeys != NULL)
3879 {
3880 for (;;)
3881 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003882 // Get a typed character directly from the user.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 c = get_keystroke();
3884 switch (c)
3885 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003886 case CAR: // User accepts default option
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 case NL:
3888 retval = dfltbutton;
3889 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003890 case Ctrl_C: // User aborts/cancels
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 case ESC:
3892 retval = 0;
3893 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003894 default: // Could be a hotkey?
3895 if (c < 0) // special keys are ignored here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 continue;
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003897 if (c == ':' && ex_cmd)
3898 {
3899 retval = dfltbutton;
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02003900 ins_char_typebuf(':', 0);
Bram Moolenaard2c340a2011-01-17 20:08:11 +01003901 break;
3902 }
3903
Bram Moolenaar85a20022019-12-21 18:25:54 +01003904 // Make the character lowercase, as chars in "hotkeys" are.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 c = MB_TOLOWER(c);
3906 retval = 1;
3907 for (i = 0; hotkeys[i]; ++i)
3908 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 if (has_mbyte)
3910 {
3911 if ((*mb_ptr2char)(hotkeys + i) == c)
3912 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003913 i += (*mb_ptr2len)(hotkeys + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 }
3915 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 if (hotkeys[i] == c)
3917 break;
3918 ++retval;
3919 }
3920 if (hotkeys[i])
3921 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003922 // No hotkey match, so keep waiting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 continue;
3924 }
3925 break;
3926 }
3927
3928 vim_free(hotkeys);
3929 }
3930
Bram Moolenaar27321db2020-07-06 21:24:57 +02003931 settmode(save_tmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 State = oldState;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 --no_wait_return;
3935 msg_end_prompt();
3936
3937 return retval;
3938}
3939
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940/*
3941 * Copy one character from "*from" to "*to", taking care of multi-byte
3942 * characters. Return the length of the character in bytes.
3943 */
3944 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003945copy_char(
3946 char_u *from,
3947 char_u *to,
Bram Moolenaar85a20022019-12-21 18:25:54 +01003948 int lowercase) // make character lower case
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 int len;
3951 int c;
3952
3953 if (has_mbyte)
3954 {
3955 if (lowercase)
3956 {
3957 c = MB_TOLOWER((*mb_ptr2char)(from));
3958 return (*mb_char2bytes)(c, to);
3959 }
3960 else
3961 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003962 len = (*mb_ptr2len)(from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 mch_memmove(to, from, (size_t)len);
3964 return len;
3965 }
3966 }
3967 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 {
3969 if (lowercase)
3970 *to = (char_u)TOLOWER_LOC(*from);
3971 else
3972 *to = *from;
3973 return 1;
3974 }
3975}
3976
3977/*
3978 * Format the dialog string, and display it at the bottom of
3979 * the screen. Return a string of hotkey chars (if defined) for
3980 * each 'button'. If a button has no hotkey defined, the first character of
3981 * the button is used.
3982 * The hotkeys can be multi-byte characters, but without combining chars.
3983 *
3984 * Returns an allocated string with hotkeys, or NULL for error.
3985 */
3986 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003987msg_show_console_dialog(
3988 char_u *message,
3989 char_u *buttons,
3990 int dfltbutton)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991{
3992 int len = 0;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003993#define HOTK_LEN (has_mbyte ? MB_MAXBYTES : 1)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003994 int lenhotkey = HOTK_LEN; // count first button
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 char_u *hotk = NULL;
3996 char_u *msgp = NULL;
3997 char_u *hotkp = NULL;
3998 char_u *r;
3999 int copy;
4000#define HAS_HOTKEY_LEN 30
4001 char_u has_hotkey[HAS_HOTKEY_LEN];
Bram Moolenaar85a20022019-12-21 18:25:54 +01004002 int first_hotkey = FALSE; // first char of button is hotkey
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 int idx;
4004
4005 has_hotkey[0] = FALSE;
4006
4007 /*
4008 * First loop: compute the size of memory to allocate.
4009 * Second loop: copy to the allocated memory.
4010 */
4011 for (copy = 0; copy <= 1; ++copy)
4012 {
4013 r = buttons;
4014 idx = 0;
4015 while (*r)
4016 {
4017 if (*r == DLG_BUTTON_SEP)
4018 {
4019 if (copy)
4020 {
4021 *msgp++ = ',';
Bram Moolenaar85a20022019-12-21 18:25:54 +01004022 *msgp++ = ' '; // '\n' -> ', '
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023
Bram Moolenaar85a20022019-12-21 18:25:54 +01004024 // advance to next hotkey and set default hotkey
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 if (has_mbyte)
Bram Moolenaar6a516062007-07-05 08:11:42 +00004026 hotkp += STRLEN(hotkp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 ++hotkp;
Bram Moolenaar6a516062007-07-05 08:11:42 +00004029 hotkp[copy_char(r + 1, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 if (dfltbutton)
4031 --dfltbutton;
4032
Bram Moolenaar85a20022019-12-21 18:25:54 +01004033 // If no hotkey is specified first char is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 if (idx < HAS_HOTKEY_LEN - 1 && !has_hotkey[++idx])
4035 first_hotkey = TRUE;
4036 }
4037 else
4038 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004039 len += 3; // '\n' -> ', '; 'x' -> '(x)'
4040 lenhotkey += HOTK_LEN; // each button needs a hotkey
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041 if (idx < HAS_HOTKEY_LEN - 1)
4042 has_hotkey[++idx] = FALSE;
4043 }
4044 }
4045 else if (*r == DLG_HOTKEY_CHAR || first_hotkey)
4046 {
4047 if (*r == DLG_HOTKEY_CHAR)
4048 ++r;
4049 first_hotkey = FALSE;
4050 if (copy)
4051 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004052 if (*r == DLG_HOTKEY_CHAR) // '&&a' -> '&a'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 *msgp++ = *r;
4054 else
4055 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004056 // '&a' -> '[a]'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 *msgp++ = (dfltbutton == 1) ? '[' : '(';
4058 msgp += copy_char(r, msgp, FALSE);
4059 *msgp++ = (dfltbutton == 1) ? ']' : ')';
4060
Bram Moolenaar85a20022019-12-21 18:25:54 +01004061 // redefine hotkey
Bram Moolenaar6a516062007-07-05 08:11:42 +00004062 hotkp[copy_char(r, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 }
4064 }
4065 else
4066 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004067 ++len; // '&a' -> '[a]'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 if (idx < HAS_HOTKEY_LEN - 1)
4069 has_hotkey[idx] = TRUE;
4070 }
4071 }
4072 else
4073 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004074 // everything else copy literally
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 if (copy)
4076 msgp += copy_char(r, msgp, FALSE);
4077 }
4078
Bram Moolenaar85a20022019-12-21 18:25:54 +01004079 // advance to the next character
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004080 MB_PTR_ADV(r);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 }
4082
4083 if (copy)
4084 {
4085 *msgp++ = ':';
4086 *msgp++ = ' ';
4087 *msgp = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 }
4089 else
4090 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004091 len += (int)(STRLEN(message)
Bram Moolenaar85a20022019-12-21 18:25:54 +01004092 + 2 // for the NL's
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004093 + STRLEN(buttons)
Bram Moolenaar85a20022019-12-21 18:25:54 +01004094 + 3); // for the ": " and NUL
4095 lenhotkey++; // for the NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096
Bram Moolenaar85a20022019-12-21 18:25:54 +01004097 // If no hotkey is specified first char is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 if (!has_hotkey[0])
4099 {
4100 first_hotkey = TRUE;
Bram Moolenaar85a20022019-12-21 18:25:54 +01004101 len += 2; // "x" -> "[x]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 }
4103
4104 /*
4105 * Now allocate and load the strings
4106 */
4107 vim_free(confirm_msg);
4108 confirm_msg = alloc(len);
4109 if (confirm_msg == NULL)
4110 return NULL;
4111 *confirm_msg = NUL;
4112 hotk = alloc(lenhotkey);
4113 if (hotk == NULL)
4114 return NULL;
4115
4116 *confirm_msg = '\n';
4117 STRCPY(confirm_msg + 1, message);
4118
4119 msgp = confirm_msg + 1 + STRLEN(message);
4120 hotkp = hotk;
4121
Bram Moolenaar85a20022019-12-21 18:25:54 +01004122 // Define first default hotkey. Keep the hotkey string NUL
4123 // terminated to avoid reading past the end.
Bram Moolenaar6a516062007-07-05 08:11:42 +00004124 hotkp[copy_char(buttons, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125
Bram Moolenaar85a20022019-12-21 18:25:54 +01004126 // Remember where the choices start, displaying starts here when
4127 // "hotkp" typed at the more prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 confirm_msg_tail = msgp;
4129 *msgp++ = '\n';
4130 }
4131 }
4132
4133 display_confirm_msg();
4134 return hotk;
4135}
4136
4137/*
4138 * Display the ":confirm" message. Also called when screen resized.
4139 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02004140 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004141display_confirm_msg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142{
Bram Moolenaar85a20022019-12-21 18:25:54 +01004143 // avoid that 'q' at the more prompt truncates the message here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 ++confirm_msg_used;
4145 if (confirm_msg != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004146 msg_puts_attr((char *)confirm_msg, HL_ATTR(HLF_M));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 --confirm_msg_used;
4148}
4149
Bram Moolenaar85a20022019-12-21 18:25:54 +01004150#endif // FEAT_CON_DIALOG
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151
4152#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4153
4154 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004155vim_dialog_yesno(
4156 int type,
4157 char_u *title,
4158 char_u *message,
4159 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160{
4161 if (do_dialog(type,
4162 title == NULL ? (char_u *)_("Question") : title,
4163 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004164 (char_u *)_("&Yes\n&No"), dflt, NULL, FALSE) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 return VIM_YES;
4166 return VIM_NO;
4167}
4168
4169 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004170vim_dialog_yesnocancel(
4171 int type,
4172 char_u *title,
4173 char_u *message,
4174 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175{
4176 switch (do_dialog(type,
4177 title == NULL ? (char_u *)_("Question") : title,
4178 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004179 (char_u *)_("&Yes\n&No\n&Cancel"), dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 {
4181 case 1: return VIM_YES;
4182 case 2: return VIM_NO;
4183 }
4184 return VIM_CANCEL;
4185}
4186
4187 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004188vim_dialog_yesnoallcancel(
4189 int type,
4190 char_u *title,
4191 char_u *message,
4192 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193{
4194 switch (do_dialog(type,
4195 title == NULL ? (char_u *)"Question" : title,
4196 message,
4197 (char_u *)_("&Yes\n&No\nSave &All\n&Discard All\n&Cancel"),
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004198 dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 {
4200 case 1: return VIM_YES;
4201 case 2: return VIM_NO;
4202 case 3: return VIM_ALL;
4203 case 4: return VIM_DISCARDALL;
4204 }
4205 return VIM_CANCEL;
4206}
4207
Bram Moolenaar85a20022019-12-21 18:25:54 +01004208#endif // FEAT_GUI_DIALOG || FEAT_CON_DIALOG