blob: 3ba1a592b87eb83ae768725cfe28b11446d24a26 [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.
Bram Moolenaar13608d82022-08-29 15:06:50 +010097 * return TRUE if wait_return() not called
Bram Moolenaar071d4272004-06-13 20:20:40 +000098 */
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 Moolenaar35a4fbc2022-08-28 14:39:53 +0100209 if (msg_scrolled != 0
210#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaara2a89732022-08-31 14:46:18 +0100211 || in_echowindow
Bram Moolenaar35a4fbc2022-08-28 14:39:53 +0100212#endif
213 )
Bram Moolenaar85a20022019-12-21 18:25:54 +0100214 // Use all the columns.
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000215 room = (int)(Rows - msg_row) * Columns - 1;
216 else
Bram Moolenaar85a20022019-12-21 18:25:54 +0100217 // Use up to 'showcmd' column.
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000218 room = (int)(Rows - msg_row - 1) * Columns + sc_col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219 if (len > room && room > 0)
220 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221 if (enc_utf8)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100222 // may have up to 18 bytes per cell (6 per char, up to two
223 // composing chars)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100224 len = (room + 2) * 18;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225 else if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100226 // may have up to 2 bytes per cell for euc-jp
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100227 len = (room + 2) * 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228 else
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100229 len = room + 2;
230 buf = alloc(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231 if (buf != NULL)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100232 trunc_string(s, buf, room, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233 }
234 }
235 return buf;
236}
237
238/*
239 * Truncate a string "s" to "buf" with cell width "room".
240 * "s" and "buf" may be equal.
241 */
242 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100243trunc_string(
244 char_u *s,
245 char_u *buf,
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200246 int room_in,
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100247 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248{
Bram Moolenaar85a20022019-12-21 18:25:54 +0100249 size_t room = room_in - 3; // "..." takes 3 chars
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200250 size_t half;
251 size_t len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252 int e;
253 int i;
254 int n;
255
Bram Moolenaar62818152021-02-14 15:37:30 +0100256 if (*s == NUL)
257 {
258 if (buflen > 0)
259 *buf = NUL;
260 return;
261 }
262
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200263 if (room_in < 3)
264 room = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265 half = room / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266
Bram Moolenaar85a20022019-12-21 18:25:54 +0100267 // First part: Start of the string.
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100268 for (e = 0; len < half && e < buflen; ++e)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269 {
270 if (s[e] == NUL)
271 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100272 // text fits without truncating!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273 buf[e] = NUL;
274 return;
275 }
276 n = ptr2cells(s + e);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200277 if (len + n > half)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278 break;
279 len += n;
280 buf[e] = s[e];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000281 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000282 for (n = (*mb_ptr2len)(s + e); --n > 0; )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000283 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100284 if (++e == buflen)
285 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000286 buf[e] = s[e];
287 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000288 }
289
Bram Moolenaar85a20022019-12-21 18:25:54 +0100290 // Last part: End of the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291 i = e;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000292 if (enc_dbcs != 0)
293 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100294 // For DBCS going backwards in a string is slow, but
295 // computing the cell width isn't too slow: go forward
296 // until the rest fits.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000297 n = vim_strsize(s + i);
298 while (len + n > room)
299 {
300 n -= ptr2cells(s + i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000301 i += (*mb_ptr2len)(s + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000302 }
303 }
304 else if (enc_utf8)
305 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100306 // For UTF-8 we can go backwards easily.
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000307 half = i = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000308 for (;;)
309 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000310 do
Bram Moolenaarace95982017-03-29 17:30:27 +0200311 half = half - utf_head_off(s, s + half - 1) - 1;
Bram Moolenaarb9644432016-07-19 12:33:44 +0200312 while (half > 0 && utf_iscomposing(utf_ptr2char(s + half)));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313 n = ptr2cells(s + half);
Bram Moolenaarb9644432016-07-19 12:33:44 +0200314 if (len + n > room || half == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315 break;
316 len += n;
Bram Moolenaara5c0cc12016-07-30 16:40:39 +0200317 i = (int)half;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000318 }
319 }
320 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321 {
Bram Moolenaard0337e32019-12-30 17:55:34 +0100322 for (i = (int)STRLEN(s);
323 i - 1 >= 0 && len + (n = ptr2cells(s + i - 1)) <= room; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324 len += n;
325 }
326
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200327
328 if (i <= e + 3)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100329 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100330 // text fits without truncating
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200331 if (s != buf)
332 {
333 len = STRLEN(s);
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200334 if (len >= (size_t)buflen)
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200335 len = buflen - 1;
336 len = len - e + 1;
337 if (len < 1)
338 buf[e - 1] = NUL;
339 else
340 mch_memmove(buf + e, s + e, len);
341 }
342 }
343 else if (e + 3 < buflen)
344 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100345 // set the middle and copy the last part
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100346 mch_memmove(buf + e, "...", (size_t)3);
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200347 len = STRLEN(s + i) + 1;
348 if (len >= (size_t)buflen - e - 3)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100349 len = buflen - e - 3 - 1;
350 mch_memmove(buf + e + 3, s + i, len);
351 buf[e + 3 + len - 1] = NUL;
352 }
353 else
354 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100355 // can't fit in the "...", just truncate it
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200356 buf[e - 1] = NUL;
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100357 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358}
359
360/*
361 * Automatic prototype generation does not understand this function.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100362 * Note: Caller of smsg() and smsg_attr() must check the resulting string is
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363 * shorter than IOSIZE!!!
364 */
365#ifndef PROTO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100367int vim_snprintf(char *str, size_t str_m, const char *fmt, ...);
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000368
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100370smsg(const char *s, ...)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000371{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200372 if (IObuff == NULL)
373 {
374 // Very early in initialisation and already something wrong, just
375 // give the raw message so the user at least gets a hint.
376 return msg((char *)s);
377 }
378 else
379 {
380 va_list arglist;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200382 va_start(arglist, s);
383 vim_vsnprintf((char *)IObuff, IOSIZE, s, arglist);
384 va_end(arglist);
385 return msg((char *)IObuff);
386 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387}
388
389 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100390smsg_attr(int attr, const char *s, ...)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200392 if (IObuff == NULL)
393 {
394 // Very early in initialisation and already something wrong, just
395 // give the raw message so the user at least gets a hint.
396 return msg_attr((char *)s, attr);
397 }
398 else
399 {
400 va_list arglist;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200402 va_start(arglist, s);
403 vim_vsnprintf((char *)IObuff, IOSIZE, s, arglist);
404 va_end(arglist);
405 return msg_attr((char *)IObuff, attr);
406 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407}
408
Bram Moolenaare0429682018-07-01 16:44:03 +0200409 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100410smsg_attr_keep(int attr, const char *s, ...)
Bram Moolenaare0429682018-07-01 16:44:03 +0200411{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200412 if (IObuff == NULL)
413 {
414 // Very early in initialisation and already something wrong, just
415 // give the raw message so the user at least gets a hint.
416 return msg_attr_keep((char *)s, attr, TRUE);
417 }
418 else
419 {
420 va_list arglist;
Bram Moolenaare0429682018-07-01 16:44:03 +0200421
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200422 va_start(arglist, s);
423 vim_vsnprintf((char *)IObuff, IOSIZE, s, arglist);
424 va_end(arglist);
425 return msg_attr_keep((char *)IObuff, attr, TRUE);
426 }
Bram Moolenaare0429682018-07-01 16:44:03 +0200427}
428
Bram Moolenaar071d4272004-06-13 20:20:40 +0000429#endif
430
431/*
432 * Remember the last sourcing name/lnum used in an error message, so that it
433 * isn't printed each time when it didn't change.
434 */
435static int last_sourcing_lnum = 0;
436static char_u *last_sourcing_name = NULL;
437
438/*
439 * Reset the last used sourcing name/lnum. Makes sure it is displayed again
440 * for the next error message;
441 */
Bram Moolenaar4eec5ec2005-06-26 22:26:21 +0000442 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100443reset_last_sourcing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444{
Bram Moolenaard23a8232018-02-10 18:45:26 +0100445 VIM_CLEAR(last_sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446 last_sourcing_lnum = 0;
447}
448
449/*
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100450 * Return TRUE if "SOURCING_NAME" differs from "last_sourcing_name".
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000451 */
452 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100453other_sourcing_name(void)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000454{
Bram Moolenaar0bd8d052021-11-25 13:39:28 +0000455 if (HAVE_SOURCING_INFO && SOURCING_NAME != NULL)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000456 {
457 if (last_sourcing_name != NULL)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100458 return STRCMP(SOURCING_NAME, last_sourcing_name) != 0;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000459 return TRUE;
460 }
461 return FALSE;
462}
463
464/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465 * Get the message about the source, as used for an error message.
466 * Returns an allocated string with room for one more character.
467 * Returns NULL when no message is to be given.
468 */
469 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100470get_emsg_source(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000471{
472 char_u *Buf, *p;
473
Bram Moolenaar0bd8d052021-11-25 13:39:28 +0000474 if (HAVE_SOURCING_INFO && SOURCING_NAME != NULL && other_sourcing_name())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000475 {
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200476 char_u *sname = estack_sfile(ESTACK_NONE);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100477 char_u *tofree = sname;
478
479 if (sname == NULL)
480 sname = SOURCING_NAME;
481
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +0200482#ifdef FEAT_EVAL
483 if (estack_compiling)
484 p = (char_u *)_("Error detected while compiling %s:");
485 else
486#endif
487 p = (char_u *)_("Error detected while processing %s:");
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100488 Buf = alloc(STRLEN(sname) + STRLEN(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489 if (Buf != NULL)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100490 sprintf((char *)Buf, (char *)p, sname);
491 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492 return Buf;
493 }
494 return NULL;
495}
496
497/*
498 * Get the message about the source lnum, as used for an error message.
499 * Returns an allocated string with room for one more character.
500 * Returns NULL when no message is to be given.
501 */
502 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100503get_emsg_lnum(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504{
505 char_u *Buf, *p;
506
Bram Moolenaar85a20022019-12-21 18:25:54 +0100507 // lnum is 0 when executing a command from the command line
508 // argument, we don't want a line number then
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100509 if (SOURCING_NAME != NULL
510 && (other_sourcing_name() || SOURCING_LNUM != last_sourcing_lnum)
511 && SOURCING_LNUM != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512 {
513 p = (char_u *)_("line %4ld:");
Bram Moolenaar964b3742019-05-24 18:54:09 +0200514 Buf = alloc(STRLEN(p) + 20);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515 if (Buf != NULL)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100516 sprintf((char *)Buf, (char *)p, (long)SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 return Buf;
518 }
519 return NULL;
520}
521
522/*
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000523 * Display name and line number for the source of an error.
524 * Remember the file name and line number, so that for the next error the info
525 * is only displayed if it changed.
526 */
527 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100528msg_source(int attr)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000529{
530 char_u *p;
Bram Moolenaarba8c9262021-11-25 14:43:18 +0000531 static int recursive = FALSE;
532
533 // Bail out if something called here causes an error.
534 if (recursive)
535 return;
536 recursive = TRUE;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000537
538 ++no_wait_return;
539 p = get_emsg_source();
540 if (p != NULL)
541 {
zeertzjqbdedd2b2022-09-20 12:45:15 +0100542 msg_scroll = TRUE; // this will take more than one line
Bram Moolenaar32526b32019-01-19 17:43:09 +0100543 msg_attr((char *)p, attr);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000544 vim_free(p);
545 }
546 p = get_emsg_lnum();
547 if (p != NULL)
548 {
Bram Moolenaar32526b32019-01-19 17:43:09 +0100549 msg_attr((char *)p, HL_ATTR(HLF_N));
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000550 vim_free(p);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100551 last_sourcing_lnum = SOURCING_LNUM; // only once for each line
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000552 }
553
Bram Moolenaar85a20022019-12-21 18:25:54 +0100554 // remember the last sourcing name printed, also when it's empty
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100555 if (SOURCING_NAME == NULL || other_sourcing_name())
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000556 {
Bram Moolenaarba8c9262021-11-25 14:43:18 +0000557 VIM_CLEAR(last_sourcing_name);
558 if (SOURCING_NAME != NULL)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100559 last_sourcing_name = vim_strsave(SOURCING_NAME);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000560 }
561 --no_wait_return;
Bram Moolenaarba8c9262021-11-25 14:43:18 +0000562
563 recursive = FALSE;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000564}
565
566/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000567 * Return TRUE if not giving error messages right now:
568 * If "emsg_off" is set: no error messages at the moment.
569 * If "msg" is in 'debug': do error message but without side effects.
570 * If "emsg_skip" is set: never do error messages.
571 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +0200572 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100573emsg_not_now(void)
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000574{
575 if ((emsg_off > 0 && vim_strchr(p_debug, 'm') == NULL
576 && vim_strchr(p_debug, 't') == NULL)
577#ifdef FEAT_EVAL
578 || emsg_skip > 0
579#endif
580 )
581 return TRUE;
582 return FALSE;
583}
584
Bram Moolenaar5a66dfb2017-03-01 20:40:39 +0100585#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100586static garray_T ignore_error_list = GA_EMPTY;
587
588 void
589ignore_error_for_testing(char_u *error)
590{
591 if (ignore_error_list.ga_itemsize == 0)
592 ga_init2(&ignore_error_list, sizeof(char_u *), 1);
593
Bram Moolenaar461a7fc2018-12-22 13:28:07 +0100594 if (STRCMP("RESET", error) == 0)
595 ga_clear_strings(&ignore_error_list);
596 else
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000597 ga_copy_string(&ignore_error_list, error);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100598}
599
600 static int
601ignore_error(char_u *msg)
602{
603 int i;
604
605 for (i = 0; i < ignore_error_list.ga_len; ++i)
606 if (strstr((char *)msg,
607 (char *)((char_u **)(ignore_error_list.ga_data))[i]) != NULL)
608 return TRUE;
609 return FALSE;
610}
611#endif
612
Bram Moolenaarb869c0d2016-07-20 00:10:51 +0200613#if !defined(HAVE_STRERROR) || defined(PROTO)
614/*
615 * Replacement for perror() that behaves more or less like emsg() was called.
Bram Moolenaar53989552019-12-23 22:59:18 +0100616 * v:errmsg will be set and called_emsg will be incremented.
Bram Moolenaarb869c0d2016-07-20 00:10:51 +0200617 */
618 void
619do_perror(char *msg)
620{
621 perror(msg);
622 ++emsg_silent;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100623 emsg(msg);
Bram Moolenaarb869c0d2016-07-20 00:10:51 +0200624 --emsg_silent;
625}
626#endif
627
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000628/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100629 * emsg_core() - display an error message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630 *
631 * Rings the bell, if appropriate, and calls message() to do the real work
632 * When terminal not initialized (yet) mch_errmsg(..) is used.
633 *
Bram Moolenaar13608d82022-08-29 15:06:50 +0100634 * Return TRUE if wait_return() not called.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100635 * Note: caller must check 'emsg_not_now()' before calling this.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100637 static int
638emsg_core(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639{
640 int attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 char_u *p;
Bram Moolenaar958dc692016-12-01 15:34:12 +0100642 int r;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643#ifdef FEAT_EVAL
644 int ignore = FALSE;
645 int severe;
646#endif
647
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100648#ifdef FEAT_EVAL
Bram Moolenaar85a20022019-12-21 18:25:54 +0100649 // When testing some errors are turned into a normal message.
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100650 if (ignore_error(s))
Bram Moolenaar85a20022019-12-21 18:25:54 +0100651 // don't call msg() if it results in a dialog
Bram Moolenaar32526b32019-01-19 17:43:09 +0100652 return msg_use_printf() ? FALSE : msg((char *)s);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100653#endif
654
Bram Moolenaar53989552019-12-23 22:59:18 +0100655 ++called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657#ifdef FEAT_EVAL
Bram Moolenaar85a20022019-12-21 18:25:54 +0100658 // If "emsg_severe" is TRUE: When an error exception is to be thrown,
659 // prefer this message over previous messages for the same command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660 severe = emsg_severe;
661 emsg_severe = FALSE;
662#endif
663
Bram Moolenaar57657d82006-04-21 22:12:41 +0000664 if (!emsg_off || vim_strchr(p_debug, 't') != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665 {
666#ifdef FEAT_EVAL
667 /*
668 * Cause a throw of an error exception if appropriate. Don't display
669 * the error message in this case. (If no matching catch clause will
670 * be found, the message will be displayed later on.) "ignore" is set
671 * when the message should be ignored completely (used for the
672 * interrupt message).
673 */
674 if (cause_errthrow(s, severe, &ignore) == TRUE)
675 {
676 if (!ignore)
Bram Moolenaar76a63452018-11-28 20:38:37 +0100677 ++did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 return TRUE;
679 }
680
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100681 if (in_assert_fails && emsg_assert_fails_msg == NULL)
Bram Moolenaar1d634542020-08-18 13:41:50 +0200682 {
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200683 emsg_assert_fails_msg = vim_strsave(s);
Bram Moolenaar1d634542020-08-18 13:41:50 +0200684 emsg_assert_fails_lnum = SOURCING_LNUM;
Bram Moolenaar9bd5d872020-09-06 21:47:48 +0200685 vim_free(emsg_assert_fails_context);
686 emsg_assert_fails_context = vim_strsave(
687 SOURCING_NAME == NULL ? (char_u *)"" : SOURCING_NAME);
Bram Moolenaar1d634542020-08-18 13:41:50 +0200688 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200689
Bram Moolenaar85a20022019-12-21 18:25:54 +0100690 // set "v:errmsg", also when using ":silent! cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 set_vim_var_string(VV_ERRMSG, s, -1);
692#endif
693
694 /*
Bram Moolenaara7241f52008-06-24 20:39:31 +0000695 * When using ":silent! cmd" ignore error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 * But do write it to the redirection file.
697 */
698 if (emsg_silent != 0)
699 {
Bram Moolenaar599410c2021-04-10 14:03:43 +0200700#ifdef FEAT_EVAL
701 ++did_emsg_silent;
702#endif
Bram Moolenaar79815f12016-07-09 17:07:29 +0200703 if (emsg_noredir == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 {
Bram Moolenaar79815f12016-07-09 17:07:29 +0200705 msg_start();
706 p = get_emsg_source();
707 if (p != NULL)
708 {
709 STRCAT(p, "\n");
710 redir_write(p, -1);
711 vim_free(p);
712 }
713 p = get_emsg_lnum();
714 if (p != NULL)
715 {
716 STRCAT(p, "\n");
717 redir_write(p, -1);
718 vim_free(p);
719 }
720 redir_write(s, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +0100722#ifdef FEAT_EVAL
723 // Only increment did_emsg_def when :silent! wasn't used inside the
724 // :def function.
725 if (emsg_silent == emsg_silent_def)
726 ++did_emsg_def;
727#endif
Bram Moolenaar958dc692016-12-01 15:34:12 +0100728#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare809a4e2019-07-04 17:35:05 +0200729 ch_log(NULL, "ERROR silent: %s", (char *)s);
Bram Moolenaar958dc692016-12-01 15:34:12 +0100730#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731 return TRUE;
732 }
733
Bram Moolenaar2b7bc562017-01-14 19:24:52 +0100734 ex_exitval = 1;
735
Bram Moolenaar85a20022019-12-21 18:25:54 +0100736 // Reset msg_silent, an error causes messages to be switched back on.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 msg_silent = 0;
738 cmd_silent = FALSE;
739
Bram Moolenaar85a20022019-12-21 18:25:54 +0100740 if (global_busy) // break :global command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 ++global_busy;
742
743 if (p_eb)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100744 beep_flush(); // also includes flush_buffers()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745 else
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200746 flush_buffers(FLUSH_MINIMAL); // flush internal buffers
Bram Moolenaar76a63452018-11-28 20:38:37 +0100747 ++did_emsg; // flag for DoOneCmd()
Bram Moolenaare723c422017-09-06 23:40:10 +0200748#ifdef FEAT_EVAL
Bram Moolenaar88c89c72021-08-14 14:01:05 +0200749 ++uncaught_emsg;
Bram Moolenaare723c422017-09-06 23:40:10 +0200750#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 }
752
Bram Moolenaar878e1d22022-08-28 17:53:23 +0100753#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaara2a89732022-08-31 14:46:18 +0100754 if (!in_echowindow)
Bram Moolenaar878e1d22022-08-28 17:53:23 +0100755#endif
756 emsg_on_display = TRUE; // remember there is an error message
757
758 attr = HL_ATTR(HLF_E); // set highlight mode for error messages
Bram Moolenaarbb15b652005-10-03 21:52:09 +0000759 if (msg_scrolled != 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100760 need_wait_return = TRUE; // needed in case emsg() is called after
Bram Moolenaar13608d82022-08-29 15:06:50 +0100761 // wait_return() has reset need_wait_return
Bram Moolenaar85a20022019-12-21 18:25:54 +0100762 // and a redraw is expected because
763 // msg_scrolled is non-zero
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764
Bram Moolenaar958dc692016-12-01 15:34:12 +0100765#ifdef FEAT_JOB_CHANNEL
766 emsg_to_channel_log = TRUE;
767#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 /*
769 * Display name and line number for the source of the error.
770 */
zeertzjqbdedd2b2022-09-20 12:45:15 +0100771 msg_scroll = TRUE;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000772 msg_source(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773
774 /*
775 * Display the error message itself.
776 */
Bram Moolenaar85a20022019-12-21 18:25:54 +0100777 msg_nowait = FALSE; // wait for this msg
Bram Moolenaar32526b32019-01-19 17:43:09 +0100778 r = msg_attr((char *)s, attr);
Bram Moolenaar958dc692016-12-01 15:34:12 +0100779
780#ifdef FEAT_JOB_CHANNEL
781 emsg_to_channel_log = FALSE;
782#endif
783 return r;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784}
785
786/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100787 * Print an error message.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788 */
789 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100790emsg(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791{
Bram Moolenaar85a20022019-12-21 18:25:54 +0100792 // Skip this if not giving error messages at the moment.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100793 if (!emsg_not_now())
794 return emsg_core((char_u *)s);
Bram Moolenaar85a20022019-12-21 18:25:54 +0100795 return TRUE; // no error messages at the moment
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796}
797
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100798#ifndef PROTO // manual proto with __attribute__
Bram Moolenaar95f09602016-11-10 20:01:45 +0100799/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100800 * Print an error message with format string and variable arguments.
801 * Note: caller must not pass 'IObuff' as 1st argument.
Bram Moolenaar95f09602016-11-10 20:01:45 +0100802 */
803 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100804semsg(const char *s, ...)
Bram Moolenaar95f09602016-11-10 20:01:45 +0100805{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200806 // Skip this if not giving error messages at the moment.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100807 if (!emsg_not_now())
808 {
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200809 if (IObuff == NULL)
810 {
811 // Very early in initialisation and already something wrong, just
812 // give the raw message so the user at least gets a hint.
813 return emsg_core((char_u *)s);
814 }
815 else
816 {
817 va_list ap;
Bram Moolenaar95f09602016-11-10 20:01:45 +0100818
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200819 va_start(ap, s);
820 vim_vsnprintf((char *)IObuff, IOSIZE, s, ap);
821 va_end(ap);
822 return emsg_core(IObuff);
823 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100824 }
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200825 return TRUE; // no error messages at the moment
Bram Moolenaar95f09602016-11-10 20:01:45 +0100826}
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100827#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +0100828
829/*
830 * Same as emsg(...), but abort on error when ABORT_ON_INTERNAL_ERROR is
831 * defined. It is used for internal errors only, so that they can be
832 * detected when fuzzing vim.
833 */
834 void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100835iemsg(char *s)
Bram Moolenaar95f09602016-11-10 20:01:45 +0100836{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100837 if (!emsg_not_now())
Bram Moolenaar1c67f3a2021-12-30 13:32:09 +0000838 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100839 emsg_core((char_u *)s);
Bram Moolenaar096ca732022-01-01 00:55:28 +0000840#if defined(ABORT_ON_INTERNAL_ERROR) && defined(FEAT_EVAL)
Bram Moolenaar1c67f3a2021-12-30 13:32:09 +0000841 set_vim_var_string(VV_ERRMSG, (char_u *)s, -1);
Bram Moolenaar213e70e2022-08-19 13:17:21 +0100842 msg_putchar('\n'); // avoid overwriting the error message
843 out_flush();
Bram Moolenaar1c67f3a2021-12-30 13:32:09 +0000844 abort();
Bram Moolenaar95f09602016-11-10 20:01:45 +0100845#endif
Bram Moolenaar1c67f3a2021-12-30 13:32:09 +0000846 }
Bram Moolenaar95f09602016-11-10 20:01:45 +0100847}
848
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100849#ifndef PROTO // manual proto with __attribute__
Bram Moolenaar95f09602016-11-10 20:01:45 +0100850/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100851 * Same as semsg(...) but abort on error when ABORT_ON_INTERNAL_ERROR is
Bram Moolenaar95f09602016-11-10 20:01:45 +0100852 * defined. It is used for internal errors only, so that they can be
853 * detected when fuzzing vim.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100854 * Note: caller must not pass 'IObuff' as 1st argument.
Bram Moolenaar95f09602016-11-10 20:01:45 +0100855 */
856 void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100857siemsg(const char *s, ...)
Bram Moolenaar95f09602016-11-10 20:01:45 +0100858{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100859 if (!emsg_not_now())
860 {
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200861 if (IObuff == NULL)
862 {
863 // Very early in initialisation and already something wrong, just
864 // give the raw message so the user at least gets a hint.
865 emsg_core((char_u *)s);
866 }
867 else
868 {
869 va_list ap;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100870
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200871 va_start(ap, s);
872 vim_vsnprintf((char *)IObuff, IOSIZE, s, ap);
873 va_end(ap);
874 emsg_core(IObuff);
875 }
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100876# ifdef ABORT_ON_INTERNAL_ERROR
Bram Moolenaar213e70e2022-08-19 13:17:21 +0100877 msg_putchar('\n'); // avoid overwriting the error message
878 out_flush();
879 abort();
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100880# endif
Bram Moolenaar213e70e2022-08-19 13:17:21 +0100881 }
Bram Moolenaar95f09602016-11-10 20:01:45 +0100882}
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100883#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +0100884
885/*
886 * Give an "Internal error" message.
887 */
888 void
889internal_error(char *where)
890{
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000891 siemsg(_(e_internal_error_str), where);
Bram Moolenaar95f09602016-11-10 20:01:45 +0100892}
893
Dominique Pelle748b3082022-01-08 12:41:16 +0000894#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaardd589232020-02-29 17:38:12 +0100895/*
896 * Like internal_error() but do not call abort(), to avoid tests using
897 * test_unknown() and test_void() causing Vim to exit.
898 */
899 void
900internal_error_no_abort(char *where)
901{
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000902 semsg(_(e_internal_error_str), where);
Bram Moolenaardd589232020-02-29 17:38:12 +0100903}
Dominique Pelle748b3082022-01-08 12:41:16 +0000904#endif
Bram Moolenaardd589232020-02-29 17:38:12 +0100905
Bram Moolenaar85a20022019-12-21 18:25:54 +0100906// emsg3() and emsgn() are in misc2.c to avoid warnings for the prototypes.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907
Bram Moolenaardf177f62005-02-22 08:39:57 +0000908 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100909emsg_invreg(int name)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000910{
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000911 semsg(_(e_invalid_register_name_str), transchar(name));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000912}
913
Dominique Pelle748b3082022-01-08 12:41:16 +0000914#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100916 * Give an error message which contains %s for "name[len]".
917 */
918 void
919emsg_namelen(char *msg, char_u *name, int len)
920{
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +0000921 char_u *copy = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100922
923 semsg(msg, copy == NULL ? "NULL" : (char *)copy);
Bram Moolenaar292b90d2020-03-18 15:23:16 +0100924 vim_free(copy);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100925}
Dominique Pelle748b3082022-01-08 12:41:16 +0000926#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100927
928/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 * Like msg(), but truncate to a single line if p_shm contains 't', or when
930 * "force" is TRUE. This truncates in another way as for normal messages.
931 * Careful: The string may be changed by msg_may_trunc()!
932 * Returns a pointer to the printed message, if wait_return() not called.
933 */
Bram Moolenaar32526b32019-01-19 17:43:09 +0100934 char *
935msg_trunc_attr(char *s, int force, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936{
937 int n;
Bram Moolenaar32526b32019-01-19 17:43:09 +0100938 char *ts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939
Bram Moolenaar85a20022019-12-21 18:25:54 +0100940 // Add message to history before truncating
Bram Moolenaar32526b32019-01-19 17:43:09 +0100941 add_msg_hist((char_u *)s, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942
Bram Moolenaar32526b32019-01-19 17:43:09 +0100943 ts = (char *)msg_may_trunc(force, (char_u *)s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944
945 msg_hist_off = TRUE;
Bram Moolenaar32526b32019-01-19 17:43:09 +0100946 n = msg_attr(ts, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 msg_hist_off = FALSE;
948
949 if (n)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100950 return ts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951 return NULL;
952}
953
954/*
955 * Check if message "s" should be truncated at the start (for filenames).
956 * Return a pointer to where the truncated message starts.
957 * Note: May change the message by replacing a character with '<'.
958 */
959 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100960msg_may_trunc(int force, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961{
962 int n;
963 int room;
964
Bram Moolenaar9198de32022-08-27 21:30:03 +0100965 // If 'cmdheight' is zero or something unexpected happened "room" may be
966 // negative.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 room = (int)(Rows - cmdline_row - 1) * Columns + sc_col - 1;
Bram Moolenaar0fbc9262022-06-26 11:17:10 +0100968 if (room > 0 && (force || (shortmess(SHM_TRUNC) && !exmode_active))
Bram Moolenaara2a89732022-08-31 14:46:18 +0100969 && (n = (int)STRLEN(s) - room) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 if (has_mbyte)
972 {
973 int size = vim_strsize(s);
974
Bram Moolenaar85a20022019-12-21 18:25:54 +0100975 // There may be room anyway when there are multibyte chars.
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +0000976 if (size <= room)
977 return s;
978
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 for (n = 0; size >= room; )
980 {
981 size -= (*mb_ptr2cells)(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000982 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 }
984 --n;
985 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 s += n;
987 *s = '<';
988 }
989 return s;
990}
991
992 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100993add_msg_hist(
994 char_u *s,
Bram Moolenaar85a20022019-12-21 18:25:54 +0100995 int len, // -1 for undetermined length
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100996 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997{
998 struct msg_hist *p;
999
1000 if (msg_hist_off || msg_silent != 0)
1001 return;
1002
Bram Moolenaar85a20022019-12-21 18:25:54 +01001003 // Don't let the message history get too big
Bram Moolenaar4770d092006-01-12 23:22:24 +00001004 while (msg_hist_len > MAX_MSG_HIST_LEN)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00001005 (void)delete_first_msg();
1006
Bram Moolenaar85a20022019-12-21 18:25:54 +01001007 // allocate an entry and add the message at the end of the history
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001008 p = ALLOC_ONE(struct msg_hist);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 if (p != NULL)
1010 {
1011 if (len < 0)
1012 len = (int)STRLEN(s);
Bram Moolenaar85a20022019-12-21 18:25:54 +01001013 // remove leading and trailing newlines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 while (len > 0 && *s == '\n')
1015 {
1016 ++s;
1017 --len;
1018 }
1019 while (len > 0 && s[len - 1] == '\n')
1020 --len;
1021 p->msg = vim_strnsave(s, len);
1022 p->next = NULL;
1023 p->attr = attr;
1024 if (last_msg_hist != NULL)
1025 last_msg_hist->next = p;
1026 last_msg_hist = p;
1027 if (first_msg_hist == NULL)
1028 first_msg_hist = last_msg_hist;
1029 ++msg_hist_len;
1030 }
1031}
1032
1033/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00001034 * Delete the first (oldest) message from the history.
1035 * Returns FAIL if there are no messages.
1036 */
1037 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001038delete_first_msg(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00001039{
1040 struct msg_hist *p;
1041
1042 if (msg_hist_len <= 0)
1043 return FAIL;
1044 p = first_msg_hist;
1045 first_msg_hist = p->next;
Bram Moolenaara7241f52008-06-24 20:39:31 +00001046 if (first_msg_hist == NULL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001047 last_msg_hist = NULL; // history is empty
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00001048 vim_free(p->msg);
1049 vim_free(p);
1050 --msg_hist_len;
1051 return OK;
1052}
1053
1054/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055 * ":messages" command.
1056 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 void
Bram Moolenaar52196b22016-04-14 17:52:41 +02001058ex_messages(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059{
1060 struct msg_hist *p;
1061 char_u *s;
Bram Moolenaar451f8492016-04-14 17:16:22 +02001062 int c = 0;
1063
1064 if (STRCMP(eap->arg, "clear") == 0)
1065 {
1066 int keep = eap->addr_count == 0 ? 0 : eap->line2;
1067
1068 while (msg_hist_len > keep)
1069 (void)delete_first_msg();
1070 return;
1071 }
1072
1073 if (*eap->arg != NUL)
1074 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001075 emsg(_(e_invalid_argument));
Bram Moolenaar451f8492016-04-14 17:16:22 +02001076 return;
1077 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078
1079 msg_hist_off = TRUE;
1080
Bram Moolenaar451f8492016-04-14 17:16:22 +02001081 p = first_msg_hist;
Bram Moolenaar451f8492016-04-14 17:16:22 +02001082 if (eap->addr_count != 0)
1083 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001084 // Count total messages
Bram Moolenaar451f8492016-04-14 17:16:22 +02001085 for (; p != NULL && !got_int; p = p->next)
1086 c++;
1087
1088 c -= eap->line2;
1089
Bram Moolenaar85a20022019-12-21 18:25:54 +01001090 // Skip without number of messages specified
Bram Moolenaar451f8492016-04-14 17:16:22 +02001091 for (p = first_msg_hist; p != NULL && !got_int && c > 0;
1092 p = p->next, c--);
1093 }
1094
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001095 if (p == first_msg_hist)
1096 {
Bram Moolenaar41f69182020-04-25 15:45:37 +02001097#ifdef FEAT_MULTI_LANG
1098 s = get_mess_lang();
1099#else
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001100 s = mch_getenv((char_u *)"LANG");
Bram Moolenaar41f69182020-04-25 15:45:37 +02001101#endif
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001102 if (s != NULL && *s != NUL)
Bram Moolenaar0c183192018-06-28 14:54:43 +02001103 // The next comment is extracted by xgettext and put in po file for
1104 // translators to read.
Bram Moolenaar32526b32019-01-19 17:43:09 +01001105 msg_attr(
Bram Moolenaar0c183192018-06-28 14:54:43 +02001106 // Translator: Please replace the name and email address
1107 // with the appropriate text for your translation.
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001108 _("Messages maintainer: Bram Moolenaar <Bram@vim.org>"),
Bram Moolenaar8820b482017-03-16 17:23:31 +01001109 HL_ATTR(HLF_T));
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001110 }
1111
Bram Moolenaar85a20022019-12-21 18:25:54 +01001112 // Display what was not skipped.
Bram Moolenaar451f8492016-04-14 17:16:22 +02001113 for (; p != NULL && !got_int; p = p->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 if (p->msg != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001115 msg_attr((char *)p->msg, p->attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116
1117 msg_hist_off = FALSE;
1118}
1119
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001120#if defined(FEAT_CON_DIALOG) || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121/*
1122 * Call this after prompting the user. This will avoid a hit-return message
1123 * and a delay.
1124 */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001125 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001126msg_end_prompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127{
1128 need_wait_return = FALSE;
1129 emsg_on_display = FALSE;
1130 cmdline_row = msg_row;
1131 msg_col = 0;
1132 msg_clr_eos();
Bram Moolenaar5d6f75e2011-12-30 14:14:29 +01001133 lines_left = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134}
1135#endif
1136
1137/*
Bram Moolenaar32526b32019-01-19 17:43:09 +01001138 * Wait for the user to hit a key (normally Enter).
1139 * If "redraw" is TRUE, clear and redraw the screen.
1140 * If "redraw" is FALSE, just redraw the screen.
1141 * If "redraw" is -1, don't redraw at all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 */
1143 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001144wait_return(int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145{
1146 int c;
1147 int oldState;
1148 int tmpState;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 int had_got_int;
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001150 int save_reg_recording;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001151 FILE *save_scriptout;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152
1153 if (redraw == TRUE)
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01001154 set_must_redraw(UPD_CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155
Bram Moolenaar85a20022019-12-21 18:25:54 +01001156 // If using ":silent cmd", don't wait for a return. Also don't set
1157 // need_wait_return to do it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 if (msg_silent != 0)
1159 return;
Bram Moolenaarcf0995d2022-09-11 21:36:17 +01001160#ifdef HAS_MESSAGE_WINDOW
1161 if (in_echowindow)
1162 return;
1163#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01001165 /*
1166 * When inside vgetc(), we can't wait for a typed character at all.
1167 * With the global command (and some others) we only need one return at
1168 * the end. Adjust cmdline_row to avoid the next message overwriting the
1169 * last one.
1170 */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00001171 if (vgetc_busy > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 return;
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01001173 need_wait_return = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 if (no_wait_return)
1175 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 if (!exmode_active)
1177 cmdline_row = msg_row;
1178 return;
1179 }
1180
Bram Moolenaar85a20022019-12-21 18:25:54 +01001181 redir_off = TRUE; // don't redirect this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182 oldState = State;
1183 if (quit_more)
1184 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001185 c = CAR; // just pretend CR was hit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 quit_more = FALSE;
1187 got_int = FALSE;
1188 }
1189 else if (exmode_active)
1190 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001191 msg_puts(" "); // make sure the cursor is on the right line
1192 c = CAR; // no need for a return in ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 got_int = FALSE;
1194 }
1195 else
1196 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001197 // Make sure the hit-return prompt is on screen when 'guioptions' was
1198 // just changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 screenalloc(FALSE);
1200
Bram Moolenaar24959102022-05-07 20:01:16 +01001201 State = MODE_HITRETURN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar85a20022019-12-21 18:25:54 +01001204 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205#endif
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01001206 cmdline_row = msg_row;
1207
Bram Moolenaar85a20022019-12-21 18:25:54 +01001208 // Avoid the sequence that the user types ":" at the hit-return prompt
1209 // to start an Ex command, but the file-changed dialog gets in the
1210 // way.
Bram Moolenaarec38d692013-04-24 15:12:32 +02001211 if (need_check_timestamps)
1212 check_timestamps(FALSE);
1213
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 hit_return_msg();
1215
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 do
1217 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001218 // Remember "got_int", if it is set vgetc() probably returns a
1219 // CTRL-C, but we need to loop then.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 had_got_int = got_int;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001221
Bram Moolenaar85a20022019-12-21 18:25:54 +01001222 // Don't do mappings here, we put the character back in the
1223 // typeahead buffer.
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001224 ++no_mapping;
1225 ++allow_keys;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001226
Bram Moolenaar85a20022019-12-21 18:25:54 +01001227 // Temporarily disable Recording. If Recording is active, the
1228 // character will be recorded later, since it will be added to the
1229 // typebuf after the loop
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001230 save_reg_recording = reg_recording;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001231 save_scriptout = scriptout;
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001232 reg_recording = 0;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001233 scriptout = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234 c = safe_vgetc();
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00001235 if (had_got_int && !global_busy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 got_int = FALSE;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001237 --no_mapping;
1238 --allow_keys;
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001239 reg_recording = save_reg_recording;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001240 scriptout = save_scriptout;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001241
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242#ifdef FEAT_CLIPBOARD
Bram Moolenaar85a20022019-12-21 18:25:54 +01001243 // Strange way to allow copying (yanking) a modeless selection at
1244 // the hit-enter prompt. Use CTRL-Y, because the same is used in
1245 // Cmdline-mode and it's harmless when there is no selection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 if (c == Ctrl_Y && clip_star.state == SELECT_DONE)
1247 {
1248 clip_copy_modeless_selection(TRUE);
1249 c = K_IGNORE;
1250 }
1251#endif
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00001252
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001253 /*
1254 * Allow scrolling back in the messages.
1255 * Also accept scroll-down commands when messages fill the screen,
1256 * to avoid that typing one 'j' too many makes the messages
1257 * disappear.
1258 */
1259 if (p_more && !p_cp)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001260 {
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001261 if (c == 'b' || c == 'k' || c == 'u' || c == 'g'
1262 || c == K_UP || c == K_PAGEUP)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001263 {
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001264 if (msg_scrolled > Rows)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001265 // scroll back to show older messages
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001266 do_more_prompt(c);
1267 else
1268 {
1269 msg_didout = FALSE;
1270 c = K_IGNORE;
1271 msg_col =
1272#ifdef FEAT_RIGHTLEFT
1273 cmdmsg_rl ? Columns - 1 :
1274#endif
1275 0;
1276 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001277 if (quit_more)
1278 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001279 c = CAR; // just pretend CR was hit
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001280 quit_more = FALSE;
1281 got_int = FALSE;
1282 }
Bram Moolenaarb0912962013-08-09 20:38:26 +02001283 else if (c != K_IGNORE)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001284 {
1285 c = K_IGNORE;
1286 hit_return_msg();
1287 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001288 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001289 else if (msg_scrolled > Rows - 2
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001290 && (c == 'j' || c == 'd' || c == 'f'
1291 || c == K_DOWN || c == K_PAGEDOWN))
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001292 c = K_IGNORE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001293 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 } while ((had_got_int && c == Ctrl_C)
1295 || c == K_IGNORE
1296#ifdef FEAT_GUI
1297 || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR
1298#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 || c == K_LEFTDRAG || c == K_LEFTRELEASE
1300 || c == K_MIDDLEDRAG || c == K_MIDDLERELEASE
1301 || c == K_RIGHTDRAG || c == K_RIGHTRELEASE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001302 || c == K_MOUSELEFT || c == K_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 || c == K_MOUSEDOWN || c == K_MOUSEUP
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001304 || c == K_MOUSEMOVE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 || (!mouse_has(MOUSE_RETURN)
1306 && mouse_row < msg_row
1307 && (c == K_LEFTMOUSE
1308 || c == K_MIDDLEMOUSE
1309 || c == K_RIGHTMOUSE
1310 || c == K_X1MOUSE
1311 || c == K_X2MOUSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 );
1313 ui_breakcheck();
Bram Moolenaar88456cd2022-11-18 22:14:09 +00001314
1315 // Avoid that the mouse-up event causes Visual mode to start.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 if (c == K_LEFTMOUSE || c == K_MIDDLEMOUSE || c == K_RIGHTMOUSE
1317 || c == K_X1MOUSE || c == K_X2MOUSE)
1318 (void)jump_to_mouse(MOUSE_SETPOS, NULL, 0);
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001319 else if (vim_strchr((char_u *)"\r\n ", c) == NULL && c != Ctrl_C)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001321 // Put the character back in the typeahead buffer. Don't use the
1322 // stuff buffer, because lmaps wouldn't work.
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001323 ins_char_typebuf(vgetc_char, vgetc_mod_mask);
Bram Moolenaar85a20022019-12-21 18:25:54 +01001324 do_redraw = TRUE; // need a redraw even though there is
1325 // typeahead
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 }
1328 redir_off = FALSE;
1329
1330 /*
1331 * If the user hits ':', '?' or '/' we get a command line from the next
1332 * line.
1333 */
1334 if (c == ':' || c == '?' || c == '/')
1335 {
1336 if (!exmode_active)
1337 cmdline_row = msg_row;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001338 skip_redraw = TRUE; // skip redraw once
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 do_redraw = FALSE;
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02001340#ifdef FEAT_TERMINAL
1341 skip_term_loop = TRUE;
1342#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 }
1344
1345 /*
1346 * If the window size changed set_shellsize() will redraw the screen.
1347 * Otherwise the screen is only redrawn if 'redraw' is set and no ':'
1348 * typed.
1349 */
1350 tmpState = State;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001351 State = oldState; // restore State before set_shellsize
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 msg_check();
1354
1355#if defined(UNIX) || defined(VMS)
1356 /*
1357 * When switching screens, we need to output an extra newline on exit.
1358 */
1359 if (swapping_screen() && !termcap_active)
1360 newline_on_exit = TRUE;
1361#endif
1362
1363 need_wait_return = FALSE;
1364 did_wait_return = TRUE;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001365 emsg_on_display = FALSE; // can delete error message now
1366 lines_left = -1; // reset lines_left at next msg_start()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 reset_last_sourcing();
1368 if (keep_msg != NULL && vim_strsize(keep_msg) >=
1369 (Rows - cmdline_row - 1) * Columns + sc_col)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001370 VIM_CLEAR(keep_msg); // don't redisplay message, it's too long
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371
Bram Moolenaar24959102022-05-07 20:01:16 +01001372 if (tmpState == MODE_SETWSIZE) // got resize event while in vgetc()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001374 starttermcap(); // start termcap before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 shell_resized();
1376 }
1377 else if (!skip_redraw
1378 && (redraw == TRUE || (msg_scrolled != 0 && redraw != -1)))
1379 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001380 starttermcap(); // start termcap before redrawing
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001381 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 }
1383}
1384
1385/*
1386 * Write the hit-return prompt.
1387 */
1388 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001389hit_return_msg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001391 int save_p_more = p_more;
1392
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001393 p_more = FALSE; // don't want to see this message when scrolling back
Bram Moolenaar85a20022019-12-21 18:25:54 +01001394 if (msg_didout) // start on a new line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 msg_putchar('\n');
1396 if (got_int)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001397 msg_puts(_("Interrupt: "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398
Bram Moolenaar32526b32019-01-19 17:43:09 +01001399 msg_puts_attr(_("Press ENTER or type command to continue"), HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 if (!msg_use_printf())
1401 msg_clr_eos();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001402 p_more = save_p_more;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403}
1404
1405/*
1406 * Set "keep_msg" to "s". Free the old value and check for NULL pointer.
1407 */
1408 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001409set_keep_msg(char_u *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410{
1411 vim_free(keep_msg);
1412 if (s != NULL && msg_silent == 0)
1413 keep_msg = vim_strsave(s);
1414 else
1415 keep_msg = NULL;
Bram Moolenaaraab21c32005-01-25 21:46:35 +00001416 keep_msg_more = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001417 keep_msg_attr = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418}
1419
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001420/*
1421 * If there currently is a message being displayed, set "keep_msg" to it, so
1422 * that it will be displayed again after redraw.
1423 */
1424 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001425set_keep_msg_from_hist(void)
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001426{
1427 if (keep_msg == NULL && last_msg_hist != NULL && msg_scrolled == 0
Bram Moolenaar24959102022-05-07 20:01:16 +01001428 && (State & MODE_NORMAL))
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001429 set_keep_msg(last_msg_hist->msg, last_msg_hist->attr);
1430}
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001431
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432/*
1433 * Prepare for outputting characters in the command line.
1434 */
1435 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001436msg_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437{
1438 int did_return = FALSE;
1439
Bram Moolenaare4ce65d2010-08-04 20:12:32 +02001440 if (!msg_silent)
Rob Pilling726f7f92022-01-20 14:44:38 +00001441 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001442 VIM_CLEAR(keep_msg);
Rob Pilling726f7f92022-01-20 14:44:38 +00001443 need_fileinfo = FALSE;
1444 }
Bram Moolenaara7241f52008-06-24 20:39:31 +00001445
1446#ifdef FEAT_EVAL
Bram Moolenaar3b474dc2022-09-01 17:01:32 +01001447 if (need_clr_eos)
Bram Moolenaara7241f52008-06-24 20:39:31 +00001448 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001449 // Halfway an ":echo" command and getting an (error) message: clear
1450 // any text from the command.
Bram Moolenaara7241f52008-06-24 20:39:31 +00001451 need_clr_eos = FALSE;
1452 msg_clr_eos();
1453 }
1454#endif
1455
Bram Moolenaard54af2e2022-08-27 22:05:13 +01001456#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaara2a89732022-08-31 14:46:18 +01001457 if (in_echowindow)
Bram Moolenaar9198de32022-08-27 21:30:03 +01001458 {
Bram Moolenaar37fef162022-08-29 18:16:32 +01001459 if (popup_message_win_visible()
1460 && ((msg_col > 0 && (msg_scroll || !full_screen))
1461 || in_echowindow))
Bram Moolenaar9198de32022-08-27 21:30:03 +01001462 {
1463 win_T *wp = popup_get_message_win();
1464
Bram Moolenaarf2fb54f2022-08-28 20:58:51 +01001465 // start a new line
Bram Moolenaar9198de32022-08-27 21:30:03 +01001466 curbuf = wp->w_buffer;
1467 ml_append(wp->w_buffer->b_ml.ml_line_count,
1468 (char_u *)"", (colnr_T)0, FALSE);
1469 curbuf = curwin->w_buffer;
1470 }
1471 msg_col = 0;
1472 }
Bram Moolenaard54af2e2022-08-27 22:05:13 +01001473 else
1474#endif
1475 if (!msg_scroll && full_screen) // overwrite last message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 {
1477 msg_row = cmdline_row;
1478 msg_col =
1479#ifdef FEAT_RIGHTLEFT
1480 cmdmsg_rl ? Columns - 1 :
1481#endif
1482 0;
1483 }
Bram Moolenaara2a89732022-08-31 14:46:18 +01001484 else if (msg_didout || in_echowindow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485 {
Bram Moolenaar309c4e02022-08-29 12:23:39 +01001486 // start message on next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487 msg_putchar('\n');
1488 did_return = TRUE;
1489 if (exmode_active != EXMODE_NORMAL)
1490 cmdline_row = msg_row;
1491 }
1492 if (!msg_didany || lines_left < 0)
1493 msg_starthere();
1494 if (msg_silent == 0)
1495 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001496 msg_didout = FALSE; // no output on current line yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 cursor_off();
1498 }
1499
Bram Moolenaar85a20022019-12-21 18:25:54 +01001500 // when redirecting, may need to start a new line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 if (!did_return)
1502 redir_write((char_u *)"\n", -1);
1503}
1504
1505/*
1506 * Note that the current msg position is where messages start.
1507 */
1508 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001509msg_starthere(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510{
1511 lines_left = cmdline_row;
1512 msg_didany = FALSE;
1513}
1514
1515 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001516msg_putchar(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517{
1518 msg_putchar_attr(c, 0);
1519}
1520
1521 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001522msg_putchar_attr(int c, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523{
Bram Moolenaar63c0ccd2019-01-19 21:06:58 +01001524 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525
1526 if (IS_SPECIAL(c))
1527 {
1528 buf[0] = K_SPECIAL;
1529 buf[1] = K_SECOND(c);
1530 buf[2] = K_THIRD(c);
1531 buf[3] = NUL;
1532 }
1533 else
Bram Moolenaar63c0ccd2019-01-19 21:06:58 +01001534 buf[(*mb_char2bytes)(c, buf)] = NUL;
Bram Moolenaar63c0ccd2019-01-19 21:06:58 +01001535 msg_puts_attr((char *)buf, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536}
1537
1538 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001539msg_outnum(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001541 char buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542
Bram Moolenaar32526b32019-01-19 17:43:09 +01001543 sprintf(buf, "%ld", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 msg_puts(buf);
1545}
1546
1547 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001548msg_home_replace(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549{
1550 msg_home_replace_attr(fname, 0);
1551}
1552
1553#if defined(FEAT_FIND_ID) || defined(PROTO)
1554 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001555msg_home_replace_hl(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556{
Bram Moolenaar8820b482017-03-16 17:23:31 +01001557 msg_home_replace_attr(fname, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558}
1559#endif
1560
1561 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001562msg_home_replace_attr(char_u *fname, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563{
1564 char_u *name;
1565
1566 name = home_replace_save(NULL, fname);
1567 if (name != NULL)
1568 msg_outtrans_attr(name, attr);
1569 vim_free(name);
1570}
1571
1572/*
1573 * Output 'len' characters in 'str' (including NULs) with translation
Bram Moolenaarf48ee3c2019-12-06 22:18:20 +01001574 * if 'len' is -1, output up to a NUL character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 * Use attributes 'attr'.
1576 * Return the number of characters it takes on the screen.
1577 */
1578 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001579msg_outtrans(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580{
1581 return msg_outtrans_attr(str, 0);
1582}
1583
1584 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001585msg_outtrans_attr(char_u *str, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586{
1587 return msg_outtrans_len_attr(str, (int)STRLEN(str), attr);
1588}
1589
1590 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001591msg_outtrans_len(char_u *str, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592{
1593 return msg_outtrans_len_attr(str, len, 0);
1594}
1595
1596/*
1597 * Output one character at "p". Return pointer to the next character.
1598 * Handles multi-byte characters.
1599 */
1600 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001601msg_outtrans_one(char_u *p, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 int l;
1604
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001605 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 {
1607 msg_outtrans_len_attr(p, l, attr);
1608 return p + l;
1609 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01001610 msg_puts_attr((char *)transchar_byte(*p), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 return p + 1;
1612}
1613
1614 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001615msg_outtrans_len_attr(char_u *msgstr, int len, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616{
1617 int retval = 0;
1618 char_u *str = msgstr;
1619 char_u *plain_start = msgstr;
1620 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 int mb_l;
1622 int c;
Bram Moolenaar3d30af82020-10-13 22:15:56 +02001623 int save_got_int = got_int;
1624
1625 // Only quit when got_int was set in here.
1626 got_int = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627
Bram Moolenaar85a20022019-12-21 18:25:54 +01001628 // if MSG_HIST flag set, add message to history
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 if (attr & MSG_HIST)
1630 {
1631 add_msg_hist(str, len, attr);
1632 attr &= ~MSG_HIST;
1633 }
1634
Bram Moolenaar85a20022019-12-21 18:25:54 +01001635 // If the string starts with a composing character first draw a space on
1636 // which the composing char can be drawn.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637 if (enc_utf8 && utf_iscomposing(utf_ptr2char(msgstr)))
Bram Moolenaar32526b32019-01-19 17:43:09 +01001638 msg_puts_attr(" ", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639
1640 /*
1641 * Go over the string. Special characters are translated and printed.
1642 * Normal characters are printed several at a time.
1643 */
Bram Moolenaar3d30af82020-10-13 22:15:56 +02001644 while (--len >= 0 && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 if (enc_utf8)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001647 // Don't include composing chars after the end.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001648 mb_l = utfc_ptr2len_len(str, len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 else if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001650 mb_l = (*mb_ptr2len)(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 else
1652 mb_l = 1;
1653 if (has_mbyte && mb_l > 1)
1654 {
1655 c = (*mb_ptr2char)(str);
1656 if (vim_isprintc(c))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001657 // printable multi-byte char: count the cells.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 retval += (*mb_ptr2cells)(str);
1659 else
1660 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001661 // unprintable multi-byte char: print the printable chars so
1662 // far and the translation of the unprintable char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 if (str > plain_start)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001664 msg_puts_attr_len((char *)plain_start,
1665 (int)(str - plain_start), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 plain_start = str + mb_l;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001667 msg_puts_attr((char *)transchar(c),
1668 attr == 0 ? HL_ATTR(HLF_8) : attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 retval += char2cells(c);
1670 }
1671 len -= mb_l - 1;
1672 str += mb_l;
1673 }
1674 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 {
1676 s = transchar_byte(*str);
1677 if (s[1] != NUL)
1678 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001679 // unprintable char: print the printable chars so far and the
1680 // translation of the unprintable char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 if (str > plain_start)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001682 msg_puts_attr_len((char *)plain_start,
1683 (int)(str - plain_start), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 plain_start = str + 1;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001685 msg_puts_attr((char *)s, attr == 0 ? HL_ATTR(HLF_8) : attr);
Bram Moolenaarc236c162008-07-13 17:41:49 +00001686 retval += (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 }
Bram Moolenaarb3c722a2008-07-06 17:16:02 +00001688 else
1689 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 ++str;
1691 }
1692 }
1693
Bram Moolenaar3d30af82020-10-13 22:15:56 +02001694 if (str > plain_start && !got_int)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001695 // print the printable chars at the end
Bram Moolenaar32526b32019-01-19 17:43:09 +01001696 msg_puts_attr_len((char *)plain_start, (int)(str - plain_start), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697
Bram Moolenaar3d30af82020-10-13 22:15:56 +02001698 got_int |= save_got_int;
1699
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 return retval;
1701}
1702
1703#if defined(FEAT_QUICKFIX) || defined(PROTO)
1704 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001705msg_make(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706{
1707 int i;
1708 static char_u *str = (char_u *)"eeffoc", *rs = (char_u *)"Plon#dqg#vxjduB";
1709
1710 arg = skipwhite(arg);
1711 for (i = 5; *arg && i >= 0; --i)
1712 if (*arg++ != str[i])
1713 break;
1714 if (i < 0)
1715 {
1716 msg_putchar('\n');
1717 for (i = 0; rs[i]; ++i)
1718 msg_putchar(rs[i] - 3);
1719 }
1720}
1721#endif
1722
1723/*
Bram Moolenaarf48ee3c2019-12-06 22:18:20 +01001724 * Output the string 'str' up to a NUL character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 * Return the number of characters it takes on the screen.
1726 *
1727 * If K_SPECIAL is encountered, then it is taken in conjunction with the
1728 * following character and shown as <F1>, <S-Up> etc. Any other character
1729 * which is not printable shown in <> form.
1730 * If 'from' is TRUE (lhs of a mapping), a space is shown as <Space>.
1731 * If a character is displayed in one of these special ways, is also
1732 * highlighted (its highlight name is '8' in the p_hl variable).
1733 * Otherwise characters are not highlighted.
1734 * This function is used to show mappings, where we want to see how to type
1735 * the character/string -- webb
1736 */
1737 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001738msg_outtrans_special(
1739 char_u *strstart,
Bram Moolenaar725310d2019-04-24 23:08:23 +02001740 int from, // TRUE for lhs of a mapping
Bram Moolenaar32aa1022019-11-02 22:54:41 +01001741 int maxlen) // screen columns, 0 for unlimited
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742{
1743 char_u *str = strstart;
1744 int retval = 0;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001745 char *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 int attr;
1747 int len;
1748
Bram Moolenaar8820b482017-03-16 17:23:31 +01001749 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750 while (*str != NUL)
1751 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001752 // Leading and trailing spaces need to be displayed in <> form.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 if ((str == strstart || str[1] == NUL) && *str == ' ')
1754 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001755 text = "<Space>";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 ++str;
1757 }
1758 else
zeertzjqcdc83932022-09-12 13:38:41 +01001759 text = (char *)str2special(&str, from, FALSE);
zeertzjq0519ce02022-05-09 12:16:19 +01001760 if (text[0] != NUL && text[1] == NUL)
1761 // single-byte character or illegal byte
1762 text = (char *)transchar_byte((char_u)text[0]);
Bram Moolenaar32526b32019-01-19 17:43:09 +01001763 len = vim_strsize((char_u *)text);
Bram Moolenaar725310d2019-04-24 23:08:23 +02001764 if (maxlen > 0 && retval + len >= maxlen)
1765 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001766 // Highlight special keys
Bram Moolenaar32526b32019-01-19 17:43:09 +01001767 msg_puts_attr(text, len > 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001768 && (*mb_ptr2len)((char_u *)text) <= 1 ? attr : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769 retval += len;
1770 }
1771 return retval;
1772}
1773
Martin Tournoijba43e762022-10-13 22:12:15 +01001774#if defined(FEAT_EVAL) || defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaarbd743252010-10-20 21:23:33 +02001775/*
1776 * Return the lhs or rhs of a mapping, with the key codes turned into printable
1777 * strings, in an allocated string.
1778 */
1779 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001780str2special_save(
1781 char_u *str,
zeertzjqcdc83932022-09-12 13:38:41 +01001782 int replace_spaces, // TRUE to replace " " with "<Space>".
1783 // used for the lhs of mapping and keytrans().
1784 int replace_lt) // TRUE to replace "<" with "<lt>".
Bram Moolenaarbd743252010-10-20 21:23:33 +02001785{
1786 garray_T ga;
1787 char_u *p = str;
1788
1789 ga_init2(&ga, 1, 40);
1790 while (*p != NUL)
zeertzjqcdc83932022-09-12 13:38:41 +01001791 ga_concat(&ga, str2special(&p, replace_spaces, replace_lt));
Bram Moolenaarbd743252010-10-20 21:23:33 +02001792 ga_append(&ga, NUL);
1793 return (char_u *)ga.ga_data;
1794}
1795#endif
1796
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797/*
1798 * Return the printable string for the key codes at "*sp".
zeertzjq0519ce02022-05-09 12:16:19 +01001799 * On illegal byte return a string with only that byte.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 * Used for translating the lhs or rhs of a mapping to printable chars.
1801 * Advances "sp" to the next code.
1802 */
1803 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001804str2special(
1805 char_u **sp,
zeertzjqcdc83932022-09-12 13:38:41 +01001806 int replace_spaces, // TRUE to replace " " with "<Space>".
1807 // used for the lhs of mapping and keytrans().
1808 int replace_lt) // TRUE to replace "<" with "<lt>".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809{
1810 int c;
1811 static char_u buf[7];
1812 char_u *str = *sp;
1813 int modifiers = 0;
1814 int special = FALSE;
1815
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 if (has_mbyte)
1817 {
1818 char_u *p;
1819
Bram Moolenaar85a20022019-12-21 18:25:54 +01001820 // Try to un-escape a multi-byte character. Return the un-escaped
1821 // string if it is a multi-byte character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 p = mb_unescape(sp);
1823 if (p != NULL)
1824 return p;
1825 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826
1827 c = *str;
1828 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1829 {
1830 if (str[1] == KS_MODIFIER)
1831 {
1832 modifiers = str[2];
1833 str += 3;
1834 c = *str;
1835 }
1836 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1837 {
1838 c = TO_SPECIAL(str[1], str[2]);
1839 str += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001841 if (IS_SPECIAL(c) || modifiers) // special key
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 special = TRUE;
1843 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844
zeertzjq0519ce02022-05-09 12:16:19 +01001845 if (has_mbyte && !IS_SPECIAL(c) && MB_BYTE2LEN(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 {
zeertzjqac402f42022-05-04 18:51:43 +01001847 char_u *p;
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001848
zeertzjqac402f42022-05-04 18:51:43 +01001849 *sp = str;
1850 // Try to un-escape a multi-byte character after modifiers.
1851 p = mb_unescape(sp);
zeertzjq0519ce02022-05-09 12:16:19 +01001852 if (p != NULL)
1853 // Since 'special' is TRUE the multi-byte character 'c' will be
1854 // processed by get_special_key_name()
1855 c = (*mb_ptr2char)(p);
1856 else
1857 // illegal byte
1858 *sp = str + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 }
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001860 else
Bram Moolenaar083692d2022-06-29 21:16:58 +01001861 // single-byte character, NUL or illegal byte
1862 *sp = str + (*str == NUL ? 0 : 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863
zeertzjq0519ce02022-05-09 12:16:19 +01001864 // Make special keys and C0 control characters in <> form, also <M-Space>.
zeertzjqcdc83932022-09-12 13:38:41 +01001865 if (special
1866 || c < ' '
1867 || (replace_spaces && c == ' ')
1868 || (replace_lt && c == '<'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 return get_special_key_name(c, modifiers);
1870 buf[0] = c;
1871 buf[1] = NUL;
1872 return buf;
1873}
1874
1875/*
1876 * Translate a key sequence into special key names.
1877 */
1878 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001879str2specialbuf(char_u *sp, char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880{
1881 char_u *s;
1882
1883 *buf = NUL;
1884 while (*sp)
1885 {
zeertzjqcdc83932022-09-12 13:38:41 +01001886 s = str2special(&sp, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 if ((int)(STRLEN(s) + STRLEN(buf)) < len)
1888 STRCAT(buf, s);
1889 }
1890}
1891
1892/*
1893 * print line for :print or :list command
1894 */
1895 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001896msg_prt_line(char_u *s, int list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897{
1898 int c;
1899 int col = 0;
1900 int n_extra = 0;
1901 int c_extra = 0;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001902 int c_final = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001903 char_u *p_extra = NULL; // init to make SASC shut up
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 int n;
Bram Moolenaar56da7972007-01-16 14:46:32 +00001905 int attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 char_u *trail = NULL;
Bram Moolenaar91478ae2021-02-03 15:58:13 +01001907 char_u *lead = NULL;
zeertzjqf14b8ba2021-09-10 16:58:30 +02001908 int in_multispace = FALSE;
1909 int multispace_pos = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 int l;
1911 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912
Bram Moolenaardf177f62005-02-22 08:39:57 +00001913 if (curwin->w_p_list)
1914 list = TRUE;
1915
Bram Moolenaar91478ae2021-02-03 15:58:13 +01001916 if (list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 {
Bram Moolenaar91478ae2021-02-03 15:58:13 +01001918 // find start of trailing whitespace
Bram Moolenaareed9d462021-02-15 20:38:25 +01001919 if (curwin->w_lcs_chars.trail)
Bram Moolenaar91478ae2021-02-03 15:58:13 +01001920 {
1921 trail = s + STRLEN(s);
1922 while (trail > s && VIM_ISWHITE(trail[-1]))
1923 --trail;
1924 }
1925 // find end of leading whitespace
Bram Moolenaarb8329db2022-07-06 13:31:28 +01001926 if (curwin->w_lcs_chars.lead
1927 || curwin->w_lcs_chars.leadmultispace != NULL)
Bram Moolenaar91478ae2021-02-03 15:58:13 +01001928 {
1929 lead = s;
1930 while (VIM_ISWHITE(lead[0]))
1931 lead++;
1932 // in a line full of spaces all of them are treated as trailing
1933 if (*lead == NUL)
1934 lead = NULL;
1935 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 }
1937
Bram Moolenaar85a20022019-12-21 18:25:54 +01001938 // output a space for an empty line, otherwise the line will be
1939 // overwritten
Bram Moolenaareed9d462021-02-15 20:38:25 +01001940 if (*s == NUL && !(list && curwin->w_lcs_chars.eol != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 msg_putchar(' ');
1942
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001943 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 {
Bram Moolenaar56da7972007-01-16 14:46:32 +00001945 if (n_extra > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 {
1947 --n_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001948 if (n_extra == 0 && c_final)
1949 c = c_final;
1950 else if (c_extra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 c = c_extra;
1952 else
1953 c = *p_extra++;
1954 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001955 else if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 {
1957 col += (*mb_ptr2cells)(s);
Bram Moolenaar1cbfc992020-12-02 12:37:37 +01001958 if (l >= MB_MAXBYTES)
1959 {
Bram Moolenaar29d2f452020-12-04 19:42:52 +01001960 STRCPY(buf, "?");
Bram Moolenaar1cbfc992020-12-02 12:37:37 +01001961 }
Bram Moolenaareed9d462021-02-15 20:38:25 +01001962 else if (curwin->w_lcs_chars.nbsp != NUL && list
Bram Moolenaar73284b92015-05-04 17:28:22 +02001963 && (mb_ptr2char(s) == 160
1964 || mb_ptr2char(s) == 0x202f))
Bram Moolenaaracf17282011-02-01 17:12:25 +01001965 {
Bram Moolenaar74ac29c2022-06-15 12:12:44 +01001966 int len = mb_char2bytes(curwin->w_lcs_chars.nbsp, buf);
1967
1968 buf[len] = NUL;
Bram Moolenaaracf17282011-02-01 17:12:25 +01001969 }
1970 else
1971 {
1972 mch_memmove(buf, s, (size_t)l);
1973 buf[l] = NUL;
1974 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01001975 msg_puts((char *)buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 s += l;
1977 continue;
1978 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 else
1980 {
1981 attr = 0;
1982 c = *s++;
zeertzjqf14b8ba2021-09-10 16:58:30 +02001983 in_multispace = c == ' '
1984 && ((col > 0 && s[-2] == ' ') || *s == ' ');
1985 if (!in_multispace)
1986 multispace_pos = 0;
Bram Moolenaareed9d462021-02-15 20:38:25 +01001987 if (c == TAB && (!list || curwin->w_lcs_chars.tab1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001989 // tab amount depends on current column
Bram Moolenaar04958cb2018-06-23 19:23:02 +02001990#ifdef FEAT_VARTABS
1991 n_extra = tabstop_padding(col, curbuf->b_p_ts,
1992 curbuf->b_p_vts_array) - 1;
1993#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 n_extra = curbuf->b_p_ts - col % curbuf->b_p_ts - 1;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02001995#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00001996 if (!list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 {
1998 c = ' ';
1999 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01002000 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 }
2002 else
2003 {
Bram Moolenaareed9d462021-02-15 20:38:25 +01002004 c = (n_extra == 0 && curwin->w_lcs_chars.tab3)
2005 ? curwin->w_lcs_chars.tab3
2006 : curwin->w_lcs_chars.tab1;
2007 c_extra = curwin->w_lcs_chars.tab2;
2008 c_final = curwin->w_lcs_chars.tab3;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002009 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 }
2011 }
Bram Moolenaareed9d462021-02-15 20:38:25 +01002012 else if (c == 160 && list && curwin->w_lcs_chars.nbsp != NUL)
Bram Moolenaaracf17282011-02-01 17:12:25 +01002013 {
Bram Moolenaareed9d462021-02-15 20:38:25 +01002014 c = curwin->w_lcs_chars.nbsp;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002015 attr = HL_ATTR(HLF_8);
Bram Moolenaaracf17282011-02-01 17:12:25 +01002016 }
Bram Moolenaareed9d462021-02-15 20:38:25 +01002017 else if (c == NUL && list && curwin->w_lcs_chars.eol != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 {
2019 p_extra = (char_u *)"";
2020 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01002021 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 n_extra = 1;
Bram Moolenaareed9d462021-02-15 20:38:25 +01002023 c = curwin->w_lcs_chars.eol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002024 attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 --s;
2026 }
2027 else if (c != NUL && (n = byte2cells(c)) > 1)
2028 {
2029 n_extra = n - 1;
2030 p_extra = transchar_byte(c);
2031 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01002032 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 c = *p_extra++;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002034 // Use special coloring to be able to distinguish <hex> from
2035 // the same in plain text.
Bram Moolenaar8820b482017-03-16 17:23:31 +01002036 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 }
zeertzjqf14b8ba2021-09-10 16:58:30 +02002038 else if (c == ' ')
Bram Moolenaar91478ae2021-02-03 15:58:13 +01002039 {
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01002040 if (list && lead != NULL && s <= lead && in_multispace
2041 && curwin->w_lcs_chars.leadmultispace != NULL)
2042 {
2043 c = curwin->w_lcs_chars.leadmultispace[multispace_pos++];
zeertzjqb5f08012022-06-09 13:55:28 +01002044 if (curwin->w_lcs_chars.leadmultispace[multispace_pos]
2045 == NUL)
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01002046 multispace_pos = 0;
2047 attr = HL_ATTR(HLF_8);
2048 }
zeertzjqb5f08012022-06-09 13:55:28 +01002049 else if (lead != NULL && s <= lead
2050 && curwin->w_lcs_chars.lead != NUL)
zeertzjqf14b8ba2021-09-10 16:58:30 +02002051 {
2052 c = curwin->w_lcs_chars.lead;
2053 attr = HL_ATTR(HLF_8);
2054 }
2055 else if (trail != NULL && s > trail)
2056 {
2057 c = curwin->w_lcs_chars.trail;
2058 attr = HL_ATTR(HLF_8);
2059 }
2060 else if (list && in_multispace
2061 && curwin->w_lcs_chars.multispace != NULL)
2062 {
2063 c = curwin->w_lcs_chars.multispace[multispace_pos++];
2064 if (curwin->w_lcs_chars.multispace[multispace_pos] == NUL)
2065 multispace_pos = 0;
2066 attr = HL_ATTR(HLF_8);
2067 }
2068 else if (list && curwin->w_lcs_chars.space != NUL)
2069 {
2070 c = curwin->w_lcs_chars.space;
2071 attr = HL_ATTR(HLF_8);
2072 }
Bram Moolenaar1510f992015-04-22 22:18:22 +02002073 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 }
2075
2076 if (c == NUL)
2077 break;
2078
2079 msg_putchar_attr(c, attr);
2080 col++;
2081 }
2082 msg_clr_eos();
2083}
2084
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085/*
2086 * Use screen_puts() to output one multi-byte character.
2087 * Return the pointer "s" advanced to the next character.
2088 */
2089 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002090screen_puts_mbyte(char_u *s, int l, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091{
2092 int cw;
2093
Bram Moolenaar85a20022019-12-21 18:25:54 +01002094 msg_didout = TRUE; // remember that line is not empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 cw = (*mb_ptr2cells)(s);
2096 if (cw > 1 && (
2097#ifdef FEAT_RIGHTLEFT
2098 cmdmsg_rl ? msg_col <= 1 :
2099#endif
2100 msg_col == Columns - 1))
2101 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002102 // Doesn't fit, print a highlighted '>' to fill it up.
Bram Moolenaar8820b482017-03-16 17:23:31 +01002103 msg_screen_putchar('>', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 return s;
2105 }
2106
2107 screen_puts_len(s, l, msg_row, msg_col, attr);
2108#ifdef FEAT_RIGHTLEFT
2109 if (cmdmsg_rl)
2110 {
2111 msg_col -= cw;
2112 if (msg_col == 0)
2113 {
2114 msg_col = Columns;
2115 ++msg_row;
2116 }
2117 }
2118 else
2119#endif
2120 {
2121 msg_col += cw;
2122 if (msg_col >= Columns)
2123 {
2124 msg_col = 0;
2125 ++msg_row;
2126 }
2127 }
2128 return s + l;
2129}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130
2131/*
2132 * Output a string to the screen at position msg_row, msg_col.
2133 * Update msg_row and msg_col for the next message.
2134 */
2135 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002136msg_puts(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137{
Bram Moolenaaraeac9002016-09-06 22:15:08 +02002138 msg_puts_attr(s, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139}
2140
2141 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002142msg_puts_title(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143{
Bram Moolenaar8820b482017-03-16 17:23:31 +01002144 msg_puts_attr(s, HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145}
2146
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147/*
2148 * Show a message in such a way that it always fits in the line. Cut out a
2149 * part in the middle and replace it with "..." when necessary.
2150 * Does not handle multi-byte characters!
2151 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002152 static void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002153msg_outtrans_long_len_attr(char_u *longstr, int len, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154{
2155 int slen = len;
2156 int room;
2157
2158 room = Columns - msg_col;
2159 if (len > room && room >= 20)
2160 {
2161 slen = (room - 3) / 2;
2162 msg_outtrans_len_attr(longstr, slen, attr);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002163 msg_puts_attr("...", HL_ATTR(HLF_8));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 }
2165 msg_outtrans_len_attr(longstr + len - slen, slen, attr);
2166}
2167
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002168 void
2169msg_outtrans_long_attr(char_u *longstr, int attr)
2170{
2171 msg_outtrans_long_len_attr(longstr, (int)STRLEN(longstr), attr);
2172}
2173
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174/*
2175 * Basic function for writing a message with highlight attributes.
2176 */
2177 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002178msg_puts_attr(char *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179{
2180 msg_puts_attr_len(s, -1, attr);
2181}
2182
2183/*
2184 * Like msg_puts_attr(), but with a maximum length "maxlen" (in bytes).
2185 * When "maxlen" is -1 there is no maximum length.
2186 * When "maxlen" is >= 0 the message is not put in the history.
2187 */
2188 static void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002189msg_puts_attr_len(char *str, int maxlen, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 /*
2192 * If redirection is on, also write to the redirection file.
2193 */
Bram Moolenaar32526b32019-01-19 17:43:09 +01002194 redir_write((char_u *)str, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195
2196 /*
2197 * Don't print anything when using ":silent cmd".
2198 */
2199 if (msg_silent != 0)
2200 return;
2201
Bram Moolenaar85a20022019-12-21 18:25:54 +01002202 // if MSG_HIST flag set, add message to history
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 if ((attr & MSG_HIST) && maxlen < 0)
2204 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002205 add_msg_hist((char_u *)str, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 attr &= ~MSG_HIST;
2207 }
2208
Bram Moolenaare8070982019-10-12 17:07:06 +02002209 // When writing something to the screen after it has scrolled, requires a
2210 // wait-return prompt later. Needed when scrolling, resetting
2211 // need_wait_return after some prompt, and then outputting something
2212 // without scrolling
2213 // Not needed when only using CR to move the cursor.
2214 if (msg_scrolled != 0 && !msg_scrolled_ign && STRCMP(str, "\r") != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 need_wait_return = TRUE;
Bram Moolenaare8070982019-10-12 17:07:06 +02002216 msg_didany = TRUE; // remember that something was outputted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217
2218 /*
2219 * If there is no valid screen, use fprintf so we can see error messages.
2220 * If termcap is not active, we may be writing in an alternate console
2221 * window, cursor positioning may not work correctly (window size may be
2222 * different, e.g. for Win32 console) or we just don't know where the
2223 * cursor is.
2224 */
2225 if (msg_use_printf())
Bram Moolenaar32526b32019-01-19 17:43:09 +01002226 msg_puts_printf((char_u *)str, maxlen);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002227 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01002228 msg_puts_display((char_u *)str, maxlen, attr, FALSE);
Rob Pilling726f7f92022-01-20 14:44:38 +00002229
2230 need_fileinfo = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002231}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232
Bram Moolenaar9198de32022-08-27 21:30:03 +01002233// values for "where"
2234#define PUT_APPEND 0 // append to "lnum"
2235#define PUT_TRUNC 1 // replace "lnum"
2236#define PUT_BELOW 2 // add below "lnum"
2237 //
2238#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaar9198de32022-08-27 21:30:03 +01002239/*
Bram Moolenaar24735f22022-08-30 15:44:22 +01002240 * Put text "t_s" until "end" in the message window.
Bram Moolenaar9198de32022-08-27 21:30:03 +01002241 * "where" specifies where to put the text.
2242 */
2243 static void
2244put_msg_win(win_T *wp, int where, char_u *t_s, char_u *end, linenr_T lnum)
2245{
Bram Moolenaarbeedd0a2022-08-27 21:52:52 +01002246 char_u *p;
Bram Moolenaar9198de32022-08-27 21:30:03 +01002247
Bram Moolenaar9198de32022-08-27 21:30:03 +01002248 if (where == PUT_BELOW)
Bram Moolenaarbeedd0a2022-08-27 21:52:52 +01002249 {
2250 if (*end != NUL)
2251 {
2252 p = vim_strnsave(t_s, end - t_s);
2253 if (p == NULL)
2254 return;
2255 }
2256 else
2257 p = t_s;
2258 ml_append_buf(wp->w_buffer, lnum, p, (colnr_T)0, FALSE);
2259 if (p != t_s)
2260 vim_free(p);
2261 }
Bram Moolenaar9198de32022-08-27 21:30:03 +01002262 else
2263 {
2264 char_u *newp;
2265
2266 curbuf = wp->w_buffer;
2267 if (where == PUT_APPEND)
Bram Moolenaarbeedd0a2022-08-27 21:52:52 +01002268 {
Bram Moolenaar9198de32022-08-27 21:30:03 +01002269 newp = concat_str(ml_get(lnum), t_s);
Bram Moolenaarbeedd0a2022-08-27 21:52:52 +01002270 if (newp == NULL)
2271 return;
2272 if (*end != NUL)
2273 newp[STRLEN(ml_get(lnum)) + (end - t_s)] = NUL;
2274 }
Bram Moolenaar9198de32022-08-27 21:30:03 +01002275 else
Bram Moolenaarbeedd0a2022-08-27 21:52:52 +01002276 {
Bram Moolenaar9198de32022-08-27 21:30:03 +01002277 newp = vim_strnsave(t_s, end - t_s);
Bram Moolenaarbeedd0a2022-08-27 21:52:52 +01002278 if (newp == NULL)
2279 return;
2280 }
Bram Moolenaar9198de32022-08-27 21:30:03 +01002281 ml_replace(lnum, newp, FALSE);
2282 curbuf = curwin->w_buffer;
2283 }
2284 redraw_win_later(wp, UPD_NOT_VALID);
2285
2286 // set msg_col so that a newline is written if needed
Bram Moolenaar24735f22022-08-30 15:44:22 +01002287 msg_col += (int)(end - t_s);
Bram Moolenaar9198de32022-08-27 21:30:03 +01002288}
2289#endif
2290
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002291/*
2292 * The display part of msg_puts_attr_len().
2293 * May be called recursively to display scroll-back text.
2294 */
2295 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002296msg_puts_display(
2297 char_u *str,
2298 int maxlen,
2299 int attr,
2300 int recurse)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002301{
2302 char_u *s = str;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002303 char_u *t_s = str; // string from "t_s" to "s" is still todo
2304 int t_col = 0; // screen cells todo, 0 when "t_s" not used
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002305 int l;
2306 int cw;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002307 char_u *sb_str = str;
2308 int sb_col = msg_col;
2309 int wrap;
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002310 int did_last_char;
Bram Moolenaar9198de32022-08-27 21:30:03 +01002311#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaard54af2e2022-08-27 22:05:13 +01002312 int where = PUT_APPEND;
Bram Moolenaar9198de32022-08-27 21:30:03 +01002313 win_T *msg_win = NULL;
2314 linenr_T lnum = 1;
2315
Bram Moolenaara2a89732022-08-31 14:46:18 +01002316 if (in_echowindow)
Bram Moolenaar9198de32022-08-27 21:30:03 +01002317 {
2318 msg_win = popup_get_message_win();
2319
2320 if (msg_win != NULL)
2321 {
2322 if (!popup_message_win_visible())
2323 {
2324 if (*str == NL)
2325 {
2326 // When not showing the message window and the output
2327 // starts with a NL show the message normally.
2328 msg_win = NULL;
2329 }
2330 else
2331 {
2332 // currently hidden, make it empty
2333 curbuf = msg_win->w_buffer;
2334 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
2335 ml_delete(1);
2336 curbuf = curwin->w_buffer;
2337 }
2338 }
2339 else
2340 {
2341 lnum = msg_win->w_buffer->b_ml.ml_line_count;
2342 if (msg_col == 0)
2343 where = PUT_TRUNC;
2344 }
2345 }
2346 }
2347#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348
2349 did_wait_return = FALSE;
Bram Moolenaar57b7fe82007-08-05 17:20:43 +00002350 while ((maxlen < 0 || (int)(s - str) < maxlen) && *s != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 {
2352 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002353 * We are at the end of the screen line when:
2354 * - When outputting a newline.
2355 * - When outputting a character in the last column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002357 if (!recurse && msg_row >= Rows - 1 && (*s == '\n' || (
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358#ifdef FEAT_RIGHTLEFT
2359 cmdmsg_rl
2360 ? (
2361 msg_col <= 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002362 || (*s == TAB && msg_col <= 7)
2363 || (has_mbyte && (*mb_ptr2cells)(s) > 1 && msg_col <= 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 :
2365#endif
Bram Moolenaar0efd1bd2019-12-11 19:00:04 +01002366 ((*s != '\r' && msg_col + t_col >= Columns - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 || (*s == TAB && msg_col + t_col >= ((Columns - 1) & ~7))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 || (has_mbyte && (*mb_ptr2cells)(s) > 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002369 && msg_col + t_col >= Columns - 2)))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002371 /*
2372 * The screen is scrolled up when at the last row (some terminals
2373 * scroll automatically, some don't. To avoid problems we scroll
2374 * ourselves).
2375 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 if (t_col > 0)
Bram Moolenaar9198de32022-08-27 21:30:03 +01002377 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002378 // output postponed text
Bram Moolenaar9198de32022-08-27 21:30:03 +01002379#ifdef HAS_MESSAGE_WINDOW
2380 if (msg_win != NULL)
2381 {
2382 put_msg_win(msg_win, where, t_s, s, lnum);
2383 t_col = 0;
2384 where = PUT_BELOW;
2385 }
2386 else
2387#endif
2388 t_puts(&t_col, t_s, s, attr);
2389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390
Bram Moolenaar85a20022019-12-21 18:25:54 +01002391 // When no more prompt and no more room, truncate here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 if (msg_no_more && lines_left == 0)
2393 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394
Bram Moolenaar9198de32022-08-27 21:30:03 +01002395#ifdef HAS_MESSAGE_WINDOW
2396 if (msg_win == NULL)
2397#endif
2398 // Scroll the screen up one line.
2399 msg_scroll_up();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400
2401 msg_row = Rows - 2;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002402 if (msg_col >= Columns) // can happen after screen resize
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 msg_col = Columns - 1;
2404
Bram Moolenaar85a20022019-12-21 18:25:54 +01002405 // Display char in last column before showing more-prompt.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002406 if (*s >= ' '
2407#ifdef FEAT_RIGHTLEFT
2408 && !cmdmsg_rl
2409#endif
2410 )
2411 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002412 if (has_mbyte)
2413 {
2414 if (enc_utf8 && maxlen >= 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002415 // avoid including composing chars after the end
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002416 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002417 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002418 l = (*mb_ptr2len)(s);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002419 s = screen_puts_mbyte(s, l, attr);
2420 }
2421 else
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002422 msg_screen_putchar(*s++, attr);
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002423 did_last_char = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002424 }
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002425 else
2426 did_last_char = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002427
2428 if (p_more)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002429 // store text for scrolling back
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002430 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
2431
Bram Moolenaar9198de32022-08-27 21:30:03 +01002432#ifdef HAS_MESSAGE_WINDOW
2433 if (msg_win == NULL)
2434 {
2435#endif
2436 inc_msg_scrolled();
Bram Moolenaar13608d82022-08-29 15:06:50 +01002437 need_wait_return = TRUE; // may need wait_return() in main()
Bram Moolenaar9198de32022-08-27 21:30:03 +01002438 redraw_cmdline = TRUE;
2439 if (cmdline_row > 0 && !exmode_active)
2440 --cmdline_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441
Bram Moolenaar9198de32022-08-27 21:30:03 +01002442 /*
2443 * If screen is completely filled and 'more' is set then wait
2444 * for a character.
2445 */
2446 if (lines_left > 0)
2447 --lines_left;
2448#ifdef HAS_MESSAGE_WINDOW
2449 }
2450#endif
Bram Moolenaar24959102022-05-07 20:01:16 +01002451 if (p_more && lines_left == 0 && State != MODE_HITRETURN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 && !msg_no_more && !exmode_active)
2453 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454#ifdef FEAT_CON_DIALOG
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002455 if (do_more_prompt(NUL))
2456 s = confirm_msg_tail;
2457#else
2458 (void)do_more_prompt(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459#endif
2460 if (quit_more)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002461 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002463
Bram Moolenaar85a20022019-12-21 18:25:54 +01002464 // When we displayed a char in last column need to check if there
2465 // is still more.
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002466 if (did_last_char)
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002467 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 }
2469
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002470 wrap = *s == '\n'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 || msg_col + t_col >= Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 || (has_mbyte && (*mb_ptr2cells)(s) > 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002473 && msg_col + t_col >= Columns - 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002474 if (t_col > 0 && (wrap || *s == '\r' || *s == '\b'
2475 || *s == '\t' || *s == BELL))
Bram Moolenaar9198de32022-08-27 21:30:03 +01002476 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002477 // output any postponed text
Bram Moolenaar9198de32022-08-27 21:30:03 +01002478#ifdef HAS_MESSAGE_WINDOW
2479 if (msg_win != NULL)
2480 {
2481 put_msg_win(msg_win, where, t_s, s, lnum);
2482 t_col = 0;
2483 where = PUT_BELOW;
2484 }
2485 else
2486#endif
2487 t_puts(&t_col, t_s, s, attr);
2488 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002489
2490 if (wrap && p_more && !recurse)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002491 // store text for scrolling back
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002492 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493
Bram Moolenaar85a20022019-12-21 18:25:54 +01002494 if (*s == '\n') // go to next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 {
Bram Moolenaar9198de32022-08-27 21:30:03 +01002496#ifdef HAS_MESSAGE_WINDOW
2497 if (msg_win != NULL)
2498 {
2499 // Ignore a NL when the buffer is empty, it is used to scroll
2500 // up the text.
2501 if ((msg_win->w_buffer->b_ml.ml_flags & ML_EMPTY) == 0)
2502 {
2503 put_msg_win(msg_win, PUT_BELOW, t_s, t_s, lnum);
2504 ++lnum;
2505 }
2506 }
2507 else
2508#endif
2509 msg_didout = FALSE; // remember that line is empty
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002510#ifdef FEAT_RIGHTLEFT
2511 if (cmdmsg_rl)
2512 msg_col = Columns - 1;
2513 else
2514#endif
2515 msg_col = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002516 if (++msg_row >= Rows) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 msg_row = Rows - 1;
2518 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002519 else if (*s == '\r') // go to column 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 {
2521 msg_col = 0;
Bram Moolenaard54af2e2022-08-27 22:05:13 +01002522#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaar9198de32022-08-27 21:30:03 +01002523 where = PUT_TRUNC;
Bram Moolenaard54af2e2022-08-27 22:05:13 +01002524#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002526 else if (*s == '\b') // go to previous char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 {
2528 if (msg_col)
2529 --msg_col;
2530 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002531 else if (*s == TAB) // translate Tab into spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 {
Bram Moolenaar9198de32022-08-27 21:30:03 +01002533#ifdef HAS_MESSAGE_WINDOW
2534 if (msg_win != NULL)
2535 msg_col = (msg_col + 7) % 8;
2536 else
2537#endif
2538 do
2539 msg_screen_putchar(' ', attr);
2540 while (msg_col & 7);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002542 else if (*s == BELL) // beep (from ":sh")
Bram Moolenaar165bc692015-07-21 17:53:25 +02002543 vim_beep(BO_SH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 else
2545 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 if (has_mbyte)
2547 {
2548 cw = (*mb_ptr2cells)(s);
2549 if (enc_utf8 && maxlen >= 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002550 // avoid including composing chars after the end
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002551 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002553 l = (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 }
2555 else
2556 {
2557 cw = 1;
2558 l = 1;
2559 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002560
Bram Moolenaar85a20022019-12-21 18:25:54 +01002561 // When drawing from right to left or when a double-wide character
2562 // doesn't fit, draw a single character here. Otherwise collect
2563 // characters and draw them all at once later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 if (
2565# ifdef FEAT_RIGHTLEFT
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002566 cmdmsg_rl ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567# endif
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002568 (cw > 1 && msg_col + t_col >= Columns - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 if (l > 1)
2571 s = screen_puts_mbyte(s, l, attr) - 1;
2572 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 msg_screen_putchar(*s, attr);
2574 }
2575 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002577 // postpone this character until later
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 if (t_col == 0)
2579 t_s = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 t_col += cw;
2581 s += l - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 }
2583 }
2584 ++s;
2585 }
2586
Bram Moolenaar85a20022019-12-21 18:25:54 +01002587 // output any postponed text
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 if (t_col > 0)
Bram Moolenaar9198de32022-08-27 21:30:03 +01002589 {
2590#ifdef HAS_MESSAGE_WINDOW
2591 if (msg_win != NULL)
2592 put_msg_win(msg_win, where, t_s, s, lnum);
2593 else
2594#endif
2595 t_puts(&t_col, t_s, s, attr);
2596 }
2597
2598#ifdef HAS_MESSAGE_WINDOW
2599 if (msg_win != NULL)
2600 popup_show_message_win();
2601#endif
Bram Moolenaar11901392022-09-26 19:50:44 +01002602 // Store the text for scroll back, unless it's a newline by itself.
2603 if (p_more && !recurse && !(s == sb_str + 1 && *sb_str == '\n'))
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002604 store_sb_text(&sb_str, s, attr, &sb_col, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605
2606 msg_check();
2607}
2608
2609/*
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002610 * Return TRUE when ":filter pattern" was used and "msg" does not match
2611 * "pattern".
2612 */
2613 int
2614message_filtered(char_u *msg)
2615{
Bram Moolenaard29459b2016-08-26 22:29:11 +02002616 int match;
2617
Bram Moolenaare1004402020-10-24 20:49:43 +02002618 if (cmdmod.cmod_filter_regmatch.regprog == NULL)
Bram Moolenaard29459b2016-08-26 22:29:11 +02002619 return FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02002620 match = vim_regexec(&cmdmod.cmod_filter_regmatch, msg, (colnr_T)0);
2621 return cmdmod.cmod_filter_force ? match : !match;
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002622}
2623
2624/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002625 * Scroll the screen up one line for displaying the next message line.
2626 */
2627 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002628msg_scroll_up(void)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002629{
Bram Moolenaar9198de32022-08-27 21:30:03 +01002630#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaara2a89732022-08-31 14:46:18 +01002631 if (in_echowindow)
Bram Moolenaar9198de32022-08-27 21:30:03 +01002632 return;
2633#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002634#ifdef FEAT_GUI
Bram Moolenaar85a20022019-12-21 18:25:54 +01002635 // Remove the cursor before scrolling, ScreenLines[] is going
2636 // to become invalid.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002637 if (gui.in_use)
2638 gui_undraw_cursor();
2639#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01002640 // scrolling up always works
Bram Moolenaara338adc2018-01-31 20:51:47 +01002641 mch_disable_flush();
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002642 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaara338adc2018-01-31 20:51:47 +01002643 mch_enable_flush();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002644
2645 if (!can_clear((char_u *)" "))
2646 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002647 // Scrolling up doesn't result in the right background. Set the
2648 // background here. It's not efficient, but avoids that we have to do
2649 // it all over the code.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002650 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2651
Bram Moolenaar85a20022019-12-21 18:25:54 +01002652 // Also clear the last char of the last but one line if it was not
2653 // cleared before to avoid a scroll-up.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002654 if (ScreenAttrs[LineOffset[Rows - 2] + Columns - 1] == (sattr_T)-1)
2655 screen_fill((int)Rows - 2, (int)Rows - 1,
2656 (int)Columns - 1, (int)Columns, ' ', ' ', 0);
2657 }
2658}
2659
2660/*
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002661 * Increment "msg_scrolled".
2662 */
2663 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002664inc_msg_scrolled(void)
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002665{
2666#ifdef FEAT_EVAL
2667 if (*get_vim_var_str(VV_SCROLLSTART) == NUL)
2668 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002669 char_u *p = SOURCING_NAME;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002670 char_u *tofree = NULL;
2671 int len;
2672
Bram Moolenaar85a20022019-12-21 18:25:54 +01002673 // v:scrollstart is empty, set it to the script/function name and line
2674 // number
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002675 if (p == NULL)
2676 p = (char_u *)_("Unknown");
2677 else
2678 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002679 len = (int)STRLEN(p) + 40;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002680 tofree = alloc(len);
2681 if (tofree != NULL)
2682 {
2683 vim_snprintf((char *)tofree, len, _("%s line %ld"),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002684 p, (long)SOURCING_LNUM);
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002685 p = tofree;
2686 }
2687 }
2688 set_vim_var_string(VV_SCROLLSTART, p, -1);
2689 vim_free(tofree);
2690 }
2691#endif
2692 ++msg_scrolled;
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01002693 set_must_redraw(UPD_VALID);
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002694}
2695
2696/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002697 * To be able to scroll back at the "more" and "hit-enter" prompts we need to
2698 * store the displayed text and remember where screen lines start.
2699 */
2700typedef struct msgchunk_S msgchunk_T;
2701struct msgchunk_S
2702{
2703 msgchunk_T *sb_next;
2704 msgchunk_T *sb_prev;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002705 char sb_eol; // TRUE when line ends after this text
2706 int sb_msg_col; // column in which text starts
2707 int sb_attr; // text attributes
2708 char_u sb_text[1]; // text to be displayed, actually longer
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002709};
2710
Bram Moolenaar85a20022019-12-21 18:25:54 +01002711static msgchunk_T *last_msgchunk = NULL; // last displayed text
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002712
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002713static msgchunk_T *msg_sb_start(msgchunk_T *mps);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002714
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002715typedef enum {
2716 SB_CLEAR_NONE = 0,
2717 SB_CLEAR_ALL,
2718 SB_CLEAR_CMDLINE_BUSY,
2719 SB_CLEAR_CMDLINE_DONE
2720} sb_clear_T;
2721
Bram Moolenaar85a20022019-12-21 18:25:54 +01002722// When to clear text on next msg.
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002723static sb_clear_T do_clear_sb_text = SB_CLEAR_NONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002724
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002725/*
2726 * Store part of a printed message for displaying when scrolling back.
2727 */
2728 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002729store_sb_text(
Bram Moolenaar85a20022019-12-21 18:25:54 +01002730 char_u **sb_str, // start of string
2731 char_u *s, // just after string
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002732 int attr,
2733 int *sb_col,
Bram Moolenaar85a20022019-12-21 18:25:54 +01002734 int finish) // line ends
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002735{
2736 msgchunk_T *mp;
2737
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002738 if (do_clear_sb_text == SB_CLEAR_ALL
2739 || do_clear_sb_text == SB_CLEAR_CMDLINE_DONE)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002740 {
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002741 clear_sb_text(do_clear_sb_text == SB_CLEAR_ALL);
zeertzjq46af7bc2022-07-28 12:34:09 +01002742 msg_sb_eol(); // prevent messages from overlapping
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002743 do_clear_sb_text = SB_CLEAR_NONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002744 }
2745
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002746 if (s > *sb_str)
2747 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002748 mp = alloc(sizeof(msgchunk_T) + (s - *sb_str));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002749 if (mp != NULL)
2750 {
2751 mp->sb_eol = finish;
2752 mp->sb_msg_col = *sb_col;
2753 mp->sb_attr = attr;
2754 vim_strncpy(mp->sb_text, *sb_str, s - *sb_str);
2755
2756 if (last_msgchunk == NULL)
2757 {
2758 last_msgchunk = mp;
2759 mp->sb_prev = NULL;
2760 }
2761 else
2762 {
2763 mp->sb_prev = last_msgchunk;
2764 last_msgchunk->sb_next = mp;
2765 last_msgchunk = mp;
2766 }
2767 mp->sb_next = NULL;
2768 }
2769 }
2770 else if (finish && last_msgchunk != NULL)
2771 last_msgchunk->sb_eol = TRUE;
2772
2773 *sb_str = s;
2774 *sb_col = 0;
2775}
2776
2777/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002778 * Finished showing messages, clear the scroll-back text on the next message.
2779 */
2780 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002781may_clear_sb_text(void)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002782{
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002783 do_clear_sb_text = SB_CLEAR_ALL;
2784}
2785
2786/*
zeertzjq46af7bc2022-07-28 12:34:09 +01002787 * Starting to edit the command line: do not clear messages now.
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002788 */
2789 void
2790sb_text_start_cmdline(void)
2791{
zeertzjq46af7bc2022-07-28 12:34:09 +01002792 if (do_clear_sb_text == SB_CLEAR_CMDLINE_BUSY)
2793 // Invoking command line recursively: the previous-level command line
2794 // doesn't need to be remembered as it will be redrawn when returning
2795 // to that level.
2796 sb_text_restart_cmdline();
2797 else
2798 {
2799 msg_sb_eol();
2800 do_clear_sb_text = SB_CLEAR_CMDLINE_BUSY;
2801 }
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002802}
2803
2804/*
zeertzjq46af7bc2022-07-28 12:34:09 +01002805 * Redrawing the command line: clear the last unfinished line.
2806 */
2807 void
2808sb_text_restart_cmdline(void)
2809{
2810 msgchunk_T *tofree;
2811
2812 // Needed when returning from nested command line.
2813 do_clear_sb_text = SB_CLEAR_CMDLINE_BUSY;
2814
2815 if (last_msgchunk == NULL || last_msgchunk->sb_eol)
2816 // No unfinished line: don't clear anything.
2817 return;
2818
2819 tofree = msg_sb_start(last_msgchunk);
2820 last_msgchunk = tofree->sb_prev;
2821 if (last_msgchunk != NULL)
2822 last_msgchunk->sb_next = NULL;
2823 while (tofree != NULL)
2824 {
2825 msgchunk_T *tofree_next = tofree->sb_next;
2826
2827 vim_free(tofree);
2828 tofree = tofree_next;
2829 }
2830}
2831
2832/*
2833 * Ending to edit the command line: clear old lines but the last one later.
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002834 */
2835 void
2836sb_text_end_cmdline(void)
2837{
2838 do_clear_sb_text = SB_CLEAR_CMDLINE_DONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002839}
2840
2841/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002842 * Clear any text remembered for scrolling back.
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002843 * When "all" is FALSE keep the last line.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002844 * Called when redrawing the screen.
2845 */
2846 void
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002847clear_sb_text(int all)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002848{
2849 msgchunk_T *mp;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002850 msgchunk_T **lastp;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002851
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002852 if (all)
2853 lastp = &last_msgchunk;
2854 else
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002855 {
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002856 if (last_msgchunk == NULL)
2857 return;
zeertzjqecdc82e2022-07-25 19:50:57 +01002858 lastp = &msg_sb_start(last_msgchunk)->sb_prev;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002859 }
2860
2861 while (*lastp != NULL)
2862 {
2863 mp = (*lastp)->sb_prev;
2864 vim_free(*lastp);
2865 *lastp = mp;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002866 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002867}
2868
2869/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002870 * "g<" command.
2871 */
2872 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002873show_sb_text(void)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002874{
2875 msgchunk_T *mp;
2876
Bram Moolenaar85a20022019-12-21 18:25:54 +01002877 // Only show something if there is more than one line, otherwise it looks
2878 // weird, typing a command without output results in one line.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002879 mp = msg_sb_start(last_msgchunk);
2880 if (mp == NULL || mp->sb_prev == NULL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02002881 vim_beep(BO_MESS);
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002882 else
2883 {
2884 do_more_prompt('G');
2885 wait_return(FALSE);
2886 }
2887}
2888
2889/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002890 * Move to the start of screen line in already displayed text.
2891 */
2892 static msgchunk_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002893msg_sb_start(msgchunk_T *mps)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002894{
2895 msgchunk_T *mp = mps;
2896
2897 while (mp != NULL && mp->sb_prev != NULL && !mp->sb_prev->sb_eol)
2898 mp = mp->sb_prev;
2899 return mp;
2900}
2901
2902/*
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02002903 * Mark the last message chunk as finishing the line.
2904 */
2905 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002906msg_sb_eol(void)
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02002907{
2908 if (last_msgchunk != NULL)
2909 last_msgchunk->sb_eol = TRUE;
2910}
2911
2912/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002913 * Display a screen line from previously displayed text at row "row".
Bram Moolenaar838b7462022-09-26 15:19:56 +01002914 * When "clear_to_eol" is set clear the rest of the screen line.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002915 * Returns a pointer to the text for the next line (can be NULL).
2916 */
2917 static msgchunk_T *
Bram Moolenaar838b7462022-09-26 15:19:56 +01002918disp_sb_line(int row, msgchunk_T *smp, int clear_to_eol)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002919{
2920 msgchunk_T *mp = smp;
2921 char_u *p;
2922
2923 for (;;)
2924 {
2925 msg_row = row;
2926 msg_col = mp->sb_msg_col;
2927 p = mp->sb_text;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002928 if (*p == '\n') // don't display the line break
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002929 ++p;
2930 msg_puts_display(p, -1, mp->sb_attr, TRUE);
Bram Moolenaar838b7462022-09-26 15:19:56 +01002931
2932 // If clearing the screen did not work (e.g. because of a background
2933 // color and t_ut isn't set) clear until the last column here.
2934 if (clear_to_eol)
2935 screen_fill(row, row + 1, msg_col, (int)Columns, ' ', ' ', 0);
2936
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002937 if (mp->sb_eol || mp->sb_next == NULL)
2938 break;
2939 mp = mp->sb_next;
2940 }
2941 return mp->sb_next;
2942}
2943
2944/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 * Output any postponed text for msg_puts_attr_len().
2946 */
2947 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002948t_puts(
2949 int *t_col,
2950 char_u *t_s,
2951 char_u *s,
2952 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002954 // output postponed text
2955 msg_didout = TRUE; // remember that line is not empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 screen_puts_len(t_s, (int)(s - t_s), msg_row, msg_col, attr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002957 msg_col += *t_col;
2958 *t_col = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002959 // If the string starts with a composing character don't increment the
2960 // column position for it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 if (enc_utf8 && utf_iscomposing(utf_ptr2char(t_s)))
2962 --msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963 if (msg_col >= Columns)
2964 {
2965 msg_col = 0;
2966 ++msg_row;
2967 }
2968}
2969
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970/*
2971 * Returns TRUE when messages should be printed with mch_errmsg().
2972 * This is used when there is no valid screen, so we can see error messages.
2973 * If termcap is not active, we may be writing in an alternate console
2974 * window, cursor positioning may not work correctly (window size may be
2975 * different, e.g. for Win32 console) or we just don't know where the
2976 * cursor is.
2977 */
2978 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002979msg_use_printf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980{
2981 return (!msg_check_screen()
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002982#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
2983# ifdef VIMDLL
2984 || (!gui.in_use && !termcap_active)
2985# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986 || !termcap_active
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002987# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988#endif
2989 || (swapping_screen() && !termcap_active)
2990 );
2991}
2992
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002993/*
2994 * Print a message when there is no valid screen.
2995 */
2996 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002997msg_puts_printf(char_u *str, int maxlen)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002998{
2999 char_u *s = str;
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003000 char_u *buf = NULL;
3001 char_u *p = s;
3002
Bram Moolenaar4f974752019-02-17 17:44:42 +01003003#ifdef MSWIN
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003004 if (!(silent_mode && p_verbose == 0))
Bram Moolenaar85a20022019-12-21 18:25:54 +01003005 mch_settmode(TMODE_COOK); // handle CR and NL correctly
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003006#endif
Bram Moolenaar2321ca22016-09-09 14:17:18 +02003007 while ((maxlen < 0 || (int)(s - str) < maxlen) && *s != NUL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003008 {
3009 if (!(silent_mode && p_verbose == 0))
3010 {
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003011 // NL --> CR NL translation (for Unix, not for "--version")
3012 if (*s == NL)
3013 {
3014 int n = (int)(s - p);
3015
3016 buf = alloc(n + 3);
Bram Moolenaar6f10c702019-08-20 22:58:37 +02003017 if (buf != NULL)
3018 {
3019 memcpy(buf, p, n);
3020 if (!info_message)
3021 buf[n++] = CAR;
3022 buf[n++] = NL;
3023 buf[n++] = NUL;
3024 if (info_message) // informative message, not an error
3025 mch_msg((char *)buf);
3026 else
3027 mch_errmsg((char *)buf);
3028 vim_free(buf);
3029 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003030 p = s + 1;
3031 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003032 }
3033
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003034 // primitive way to compute the current column
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003035#ifdef FEAT_RIGHTLEFT
3036 if (cmdmsg_rl)
3037 {
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003038 if (*s == CAR || *s == NL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003039 msg_col = Columns - 1;
3040 else
3041 --msg_col;
3042 }
3043 else
3044#endif
3045 {
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003046 if (*s == CAR || *s == NL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003047 msg_col = 0;
3048 else
3049 ++msg_col;
3050 }
3051 ++s;
3052 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003053
3054 if (*p != NUL && !(silent_mode && p_verbose == 0))
3055 {
Bram Moolenaara97c3632021-06-15 22:39:11 +02003056 char_u *tofree = NULL;
Bram Moolenaar97c2c052019-02-22 13:42:07 +01003057
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003058 if (maxlen > 0 && STRLEN(p) > (size_t)maxlen)
Bram Moolenaar97c2c052019-02-22 13:42:07 +01003059 {
Bram Moolenaara97c3632021-06-15 22:39:11 +02003060 tofree = vim_strnsave(p, (size_t)maxlen);
3061 p = tofree;
Bram Moolenaar97c2c052019-02-22 13:42:07 +01003062 }
Bram Moolenaara97c3632021-06-15 22:39:11 +02003063 if (p != NULL)
3064 {
3065 if (info_message)
3066 mch_msg((char *)p);
3067 else
3068 mch_errmsg((char *)p);
3069 vim_free(tofree);
3070 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003071 }
3072
3073 msg_didout = TRUE; // assume that line is not empty
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003074
Bram Moolenaar4f974752019-02-17 17:44:42 +01003075#ifdef MSWIN
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003076 if (!(silent_mode && p_verbose == 0))
3077 mch_settmode(TMODE_RAW);
3078#endif
3079}
3080
3081/*
3082 * Show the more-prompt and handle the user response.
3083 * This takes care of scrolling back and displaying previously displayed text.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003084 * When at hit-enter prompt "typed_char" is the already typed character,
3085 * otherwise it's NUL.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003086 * Returns TRUE when jumping ahead to "confirm_msg_tail".
3087 */
3088 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003089do_more_prompt(int typed_char)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003090{
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01003091 static int entered = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003092 int used_typed_char = typed_char;
3093 int oldState = State;
3094 int c;
3095#ifdef FEAT_CON_DIALOG
3096 int retval = FALSE;
3097#endif
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003098 int toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003099 msgchunk_T *mp_last = NULL;
3100 msgchunk_T *mp;
3101 int i;
3102
Bram Moolenaar85a20022019-12-21 18:25:54 +01003103 // We get called recursively when a timer callback outputs a message. In
3104 // that case don't show another prompt. Also when at the hit-Enter prompt
3105 // and nothing was typed.
Bram Moolenaar24959102022-05-07 20:01:16 +01003106 if (entered || (State == MODE_HITRETURN && typed_char == 0))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01003107 return FALSE;
3108 entered = TRUE;
3109
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003110 if (typed_char == 'G')
3111 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003112 // "g<": Find first line on the last page.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003113 mp_last = msg_sb_start(last_msgchunk);
3114 for (i = 0; i < Rows - 2 && mp_last != NULL
3115 && mp_last->sb_prev != NULL; ++i)
3116 mp_last = msg_sb_start(mp_last->sb_prev);
3117 }
3118
Bram Moolenaar24959102022-05-07 20:01:16 +01003119 State = MODE_ASKMORE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003120 setmouse();
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003121 if (typed_char == NUL)
3122 msg_moremsg(FALSE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003123 for (;;)
3124 {
3125 /*
3126 * Get a typed character directly from the user.
3127 */
3128 if (used_typed_char != NUL)
3129 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003130 c = used_typed_char; // was typed at hit-enter prompt
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003131 used_typed_char = NUL;
3132 }
3133 else
3134 c = get_keystroke();
3135
3136#if defined(FEAT_MENU) && defined(FEAT_GUI)
3137 if (c == K_MENU)
3138 {
Bram Moolenaar24959102022-05-07 20:01:16 +01003139 int idx = get_menu_index(current_menu, MODE_ASKMORE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003140
Bram Moolenaar85a20022019-12-21 18:25:54 +01003141 // Used a menu. If it starts with CTRL-Y, it must
3142 // be a "Copy" for the clipboard. Otherwise
3143 // assume that we end
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003144 if (idx == MENU_INDEX_INVALID)
3145 continue;
3146 c = *current_menu->strings[idx];
3147 if (c != NUL && current_menu->strings[idx][1] != NUL)
3148 ins_typebuf(current_menu->strings[idx] + 1,
3149 current_menu->noremap[idx], 0, TRUE,
3150 current_menu->silent[idx]);
3151 }
3152#endif
3153
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003154 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003155 switch (c)
3156 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003157 case BS: // scroll one line back
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003158 case K_BS:
3159 case 'k':
3160 case K_UP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003161 toscroll = -1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003162 break;
3163
Bram Moolenaar85a20022019-12-21 18:25:54 +01003164 case CAR: // one extra line
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003165 case NL:
3166 case 'j':
3167 case K_DOWN:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003168 toscroll = 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003169 break;
3170
Bram Moolenaar85a20022019-12-21 18:25:54 +01003171 case 'u': // Up half a page
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003172 toscroll = -(Rows / 2);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003173 break;
3174
Bram Moolenaar85a20022019-12-21 18:25:54 +01003175 case 'd': // Down half a page
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003176 toscroll = Rows / 2;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003177 break;
3178
Bram Moolenaar85a20022019-12-21 18:25:54 +01003179 case 'b': // one page back
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00003180 case K_PAGEUP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003181 toscroll = -(Rows - 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003182 break;
3183
Bram Moolenaar85a20022019-12-21 18:25:54 +01003184 case ' ': // one extra page
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00003185 case 'f':
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003186 case K_PAGEDOWN:
3187 case K_LEFTMOUSE:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003188 toscroll = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003189 break;
3190
Bram Moolenaar85a20022019-12-21 18:25:54 +01003191 case 'g': // all the way back to the start
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003192 toscroll = -999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003193 break;
3194
Bram Moolenaar85a20022019-12-21 18:25:54 +01003195 case 'G': // all the way to the end
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003196 toscroll = 999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003197 lines_left = 999999;
3198 break;
3199
Bram Moolenaar85a20022019-12-21 18:25:54 +01003200 case ':': // start new command line
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003201#ifdef FEAT_CON_DIALOG
3202 if (!confirm_msg_used)
3203#endif
3204 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003205 // Since got_int is set all typeahead will be flushed, but we
3206 // want to keep this ':', remember that in a special way.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003207 typeahead_noflush(':');
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02003208#ifdef FEAT_TERMINAL
3209 skip_term_loop = TRUE;
3210#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01003211 cmdline_row = Rows - 1; // put ':' on this line
3212 skip_redraw = TRUE; // skip redraw once
3213 need_wait_return = FALSE; // don't wait in main()
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003214 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003215 // FALLTHROUGH
3216 case 'q': // quit
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003217 case Ctrl_C:
3218 case ESC:
3219#ifdef FEAT_CON_DIALOG
3220 if (confirm_msg_used)
3221 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003222 // Jump to the choices of the dialog.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003223 retval = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003224 }
3225 else
3226#endif
3227 {
3228 got_int = TRUE;
3229 quit_more = TRUE;
3230 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003231 // When there is some more output (wrapping line) display that
3232 // without another prompt.
Bram Moolenaar51306d22009-02-24 03:38:04 +00003233 lines_left = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003234 break;
3235
3236#ifdef FEAT_CLIPBOARD
3237 case Ctrl_Y:
Bram Moolenaar85a20022019-12-21 18:25:54 +01003238 // Strange way to allow copying (yanking) a modeless
3239 // selection at the more prompt. Use CTRL-Y,
3240 // because the same is used in Cmdline-mode and at the
3241 // hit-enter prompt. However, scrolling one line up
3242 // might be expected...
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003243 if (clip_star.state == SELECT_DONE)
3244 clip_copy_modeless_selection(TRUE);
3245 continue;
3246#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01003247 default: // no valid response
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003248 msg_moremsg(TRUE);
3249 continue;
3250 }
3251
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003252 if (toscroll != 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003253 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003254 if (toscroll < 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003255 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003256 // go to start of last line
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003257 if (mp_last == NULL)
3258 mp = msg_sb_start(last_msgchunk);
3259 else if (mp_last->sb_prev != NULL)
3260 mp = msg_sb_start(mp_last->sb_prev);
3261 else
3262 mp = NULL;
3263
Bram Moolenaar85a20022019-12-21 18:25:54 +01003264 // go to start of line at top of the screen
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003265 for (i = 0; i < Rows - 2 && mp != NULL && mp->sb_prev != NULL;
3266 ++i)
3267 mp = msg_sb_start(mp->sb_prev);
3268
3269 if (mp != NULL && mp->sb_prev != NULL)
3270 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003271 // Find line to be displayed at top.
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003272 for (i = 0; i > toscroll; --i)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003273 {
3274 if (mp == NULL || mp->sb_prev == NULL)
3275 break;
3276 mp = msg_sb_start(mp->sb_prev);
3277 if (mp_last == NULL)
3278 mp_last = msg_sb_start(last_msgchunk);
3279 else
3280 mp_last = msg_sb_start(mp_last->sb_prev);
3281 }
3282
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003283 if (toscroll == -1 && screen_ins_lines(0, 0, 1,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003284 (int)Rows, 0, NULL) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003285 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003286 // display line at top
Bram Moolenaar838b7462022-09-26 15:19:56 +01003287 (void)disp_sb_line(0, mp, FALSE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003288 }
3289 else
3290 {
Bram Moolenaar838b7462022-09-26 15:19:56 +01003291 int did_clear = screenclear();
3292
Bram Moolenaar85a20022019-12-21 18:25:54 +01003293 // redisplay all lines
Bram Moolenaar773560b2006-05-06 21:38:18 +00003294 for (i = 0; mp != NULL && i < Rows - 1; ++i)
3295 {
Bram Moolenaar838b7462022-09-26 15:19:56 +01003296 mp = disp_sb_line(i, mp, !did_clear);
Bram Moolenaar773560b2006-05-06 21:38:18 +00003297 ++msg_scrolled;
3298 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003299 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003300 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003301 }
3302 }
3303 else
3304 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003305 // First display any text that we scrolled back.
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003306 while (toscroll > 0 && mp_last != NULL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003307 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003308 // scroll up, display line at bottom
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003309 msg_scroll_up();
Bram Moolenaarbb15b652005-10-03 21:52:09 +00003310 inc_msg_scrolled();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003311 screen_fill((int)Rows - 2, (int)Rows - 1, 0,
3312 (int)Columns, ' ', ' ', 0);
Bram Moolenaar838b7462022-09-26 15:19:56 +01003313 mp_last = disp_sb_line((int)Rows - 2, mp_last, FALSE);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003314 --toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003315 }
3316 }
3317
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003318 if (toscroll <= 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003319 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003320 // displayed the requested text, more prompt again
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003321 screen_fill((int)Rows - 1, (int)Rows, 0,
3322 (int)Columns, ' ', ' ', 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003323 msg_moremsg(FALSE);
3324 continue;
3325 }
3326
Bram Moolenaar85a20022019-12-21 18:25:54 +01003327 // display more text, return to caller
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003328 lines_left = toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003329 }
3330
3331 break;
3332 }
3333
Bram Moolenaar85a20022019-12-21 18:25:54 +01003334 // clear the --more-- message
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003335 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3336 State = oldState;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003337 setmouse();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003338 if (quit_more)
3339 {
3340 msg_row = Rows - 1;
3341 msg_col = 0;
3342 }
3343#ifdef FEAT_RIGHTLEFT
3344 else if (cmdmsg_rl)
3345 msg_col = Columns - 1;
3346#endif
3347
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01003348 entered = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003349#ifdef FEAT_CON_DIALOG
3350 return retval;
3351#else
3352 return FALSE;
3353#endif
3354}
3355
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356#if defined(USE_MCH_ERRMSG) || defined(PROTO)
3357
3358#ifdef mch_errmsg
3359# undef mch_errmsg
3360#endif
3361#ifdef mch_msg
3362# undef mch_msg
3363#endif
3364
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003365#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3366 static void
3367mch_errmsg_c(char *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368{
Bram Moolenaar0f77d6a2019-02-14 20:55:09 +01003369 int len = (int)STRLEN(str);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003370 DWORD nwrite = 0;
3371 DWORD mode = 0;
3372 HANDLE h = GetStdHandle(STD_ERROR_HANDLE);
3373
3374 if (GetConsoleMode(h, &mode) && enc_codepage >= 0
3375 && (int)GetConsoleCP() != enc_codepage)
3376 {
3377 WCHAR *w = enc_to_utf16((char_u *)str, &len);
3378
3379 WriteConsoleW(h, w, len, &nwrite, NULL);
3380 vim_free(w);
3381 }
3382 else
3383 {
3384 fprintf(stderr, "%s", str);
3385 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003386}
3387#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003389/*
3390 * Give an error message. To be used when the screen hasn't been initialized
3391 * yet. When stderr can't be used, collect error messages until the GUI has
3392 * started and they can be displayed in a message box.
3393 */
3394 void
3395mch_errmsg(char *str)
3396{
3397#if !defined(MSWIN) || defined(FEAT_GUI_MSWIN)
3398 int len;
3399#endif
3400
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02003401#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003402 // On Unix use stderr if it's a tty.
3403 // When not going to start the GUI also use stderr.
3404 // On Mac, when started from Finder, stderr is the console.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 if (
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003406# ifdef UNIX
3407# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003409# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 isatty(2)
3411# endif
3412# ifdef FEAT_GUI
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003413 ||
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003414# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003415# endif
3416# ifdef FEAT_GUI
3417 !(gui.in_use || gui.starting)
3418# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 )
3420 {
3421 fprintf(stderr, "%s", str);
3422 return;
3423 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003424#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003426#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3427# ifdef VIMDLL
3428 if (!(gui.in_use || gui.starting))
3429# endif
3430 {
3431 mch_errmsg_c(str);
3432 return;
3433 }
3434#endif
3435
3436#if !defined(MSWIN) || defined(FEAT_GUI_MSWIN)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003437 // avoid a delay for a message that isn't there
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 emsg_on_display = FALSE;
3439
3440 len = (int)STRLEN(str) + 1;
3441 if (error_ga.ga_growsize == 0)
3442 {
3443 error_ga.ga_growsize = 80;
3444 error_ga.ga_itemsize = 1;
3445 }
3446 if (ga_grow(&error_ga, len) == OK)
3447 {
3448 mch_memmove((char_u *)error_ga.ga_data + error_ga.ga_len,
3449 (char_u *)str, len);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003450# ifdef UNIX
Bram Moolenaar85a20022019-12-21 18:25:54 +01003451 // remove CR characters, they are displayed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 {
3453 char_u *p;
3454
3455 p = (char_u *)error_ga.ga_data + error_ga.ga_len;
3456 for (;;)
3457 {
3458 p = vim_strchr(p, '\r');
3459 if (p == NULL)
3460 break;
3461 *p = ' ';
3462 }
3463 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003464# endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01003465 --len; // don't count the NUL at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 error_ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003468#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469}
3470
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003471#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3472 static void
3473mch_msg_c(char *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474{
Bram Moolenaar0f77d6a2019-02-14 20:55:09 +01003475 int len = (int)STRLEN(str);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003476 DWORD nwrite = 0;
3477 DWORD mode;
3478 HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
3479
3480
3481 if (GetConsoleMode(h, &mode) && enc_codepage >= 0
3482 && (int)GetConsoleCP() != enc_codepage)
3483 {
3484 WCHAR *w = enc_to_utf16((char_u *)str, &len);
3485
3486 WriteConsoleW(h, w, len, &nwrite, NULL);
3487 vim_free(w);
3488 }
3489 else
3490 {
3491 printf("%s", str);
3492 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003493}
3494#endif
3495
3496/*
3497 * Give a message. To be used when the screen hasn't been initialized yet.
3498 * When there is no tty, collect messages until the GUI has started and they
3499 * can be displayed in a message box.
3500 */
3501 void
3502mch_msg(char *str)
3503{
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02003504#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003505 // On Unix use stdout if we have a tty. This allows "vim -h | more" and
3506 // uses mch_errmsg() when started from the desktop.
3507 // When not going to start the GUI also use stdout.
3508 // On Mac, when started from Finder, stderr is the console.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 if (
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003510# ifdef UNIX
3511# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003513# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 isatty(2)
Bram Moolenaareae1b912019-05-09 15:12:55 +02003515# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516# ifdef FEAT_GUI
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003517 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003519# endif
3520# ifdef FEAT_GUI
3521 !(gui.in_use || gui.starting)
3522# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 )
3524 {
3525 printf("%s", str);
3526 return;
3527 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003528#endif
3529
3530#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3531# ifdef VIMDLL
3532 if (!(gui.in_use || gui.starting))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003534 {
3535 mch_msg_c(str);
3536 return;
3537 }
3538#endif
3539#if !defined(MSWIN) || defined(FEAT_GUI_MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 mch_errmsg(str);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003541#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542}
Bram Moolenaar85a20022019-12-21 18:25:54 +01003543#endif // USE_MCH_ERRMSG
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544
3545/*
3546 * Put a character on the screen at the current message position and advance
3547 * to the next position. Only for printable ASCII!
3548 */
3549 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003550msg_screen_putchar(int c, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551{
Bram Moolenaar85a20022019-12-21 18:25:54 +01003552 msg_didout = TRUE; // remember that line is not empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 screen_putchar(c, msg_row, msg_col, attr);
3554#ifdef FEAT_RIGHTLEFT
3555 if (cmdmsg_rl)
3556 {
3557 if (--msg_col == 0)
3558 {
3559 msg_col = Columns;
3560 ++msg_row;
3561 }
3562 }
3563 else
3564#endif
3565 {
3566 if (++msg_col >= Columns)
3567 {
3568 msg_col = 0;
3569 ++msg_row;
3570 }
3571 }
3572}
3573
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003574 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003575msg_moremsg(int full)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003577 int attr;
3578 char_u *s = (char_u *)_("-- More --");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579
Bram Moolenaar8820b482017-03-16 17:23:31 +01003580 attr = HL_ATTR(HLF_M);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003581 screen_puts(s, (int)Rows - 1, 0, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 if (full)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003583 screen_puts((char_u *)
3584 _(" SPACE/d/j: screen/page/line down, b/u/k: up, q: quit "),
3585 (int)Rows - 1, vim_strsize(s), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586}
3587
3588/*
Bram Moolenaar24959102022-05-07 20:01:16 +01003589 * Repeat the message for the current mode: MODE_ASKMORE, MODE_EXTERNCMD,
3590 * MODE_CONFIRM or exmode_active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 */
3592 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003593repeat_message(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594{
Bram Moolenaar24959102022-05-07 20:01:16 +01003595 if (State == MODE_ASKMORE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003597 msg_moremsg(TRUE); // display --more-- message again
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 msg_row = Rows - 1;
3599 }
3600#ifdef FEAT_CON_DIALOG
Bram Moolenaar24959102022-05-07 20:01:16 +01003601 else if (State == MODE_CONFIRM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003603 display_confirm_msg(); // display ":confirm" message again
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 msg_row = Rows - 1;
3605 }
3606#endif
Bram Moolenaar24959102022-05-07 20:01:16 +01003607 else if (State == MODE_EXTERNCMD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003609 windgoto(msg_row, msg_col); // put cursor back
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 }
Bram Moolenaar24959102022-05-07 20:01:16 +01003611 else if (State == MODE_HITRETURN || State == MODE_SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 {
Bram Moolenaar9f108752007-11-24 14:44:58 +00003613 if (msg_row == Rows - 1)
3614 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003615 // Avoid drawing the "hit-enter" prompt below the previous one,
3616 // overwrite it. Esp. useful when regaining focus and a
3617 // FocusGained autocmd exists but didn't draw anything.
Bram Moolenaar9f108752007-11-24 14:44:58 +00003618 msg_didout = FALSE;
3619 msg_col = 0;
3620 msg_clr_eos();
3621 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 hit_return_msg();
3623 msg_row = Rows - 1;
3624 }
3625}
3626
3627/*
3628 * msg_check_screen - check if the screen is initialized.
3629 * Also check msg_row and msg_col, if they are too big it may cause a crash.
3630 * While starting the GUI the terminal codes will be set for the GUI, but the
3631 * output goes to the terminal. Don't use the terminal codes then.
3632 */
3633 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003634msg_check_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635{
3636 if (!full_screen || !screen_valid(FALSE))
3637 return FALSE;
3638
3639 if (msg_row >= Rows)
3640 msg_row = Rows - 1;
3641 if (msg_col >= Columns)
3642 msg_col = Columns - 1;
3643 return TRUE;
3644}
3645
3646/*
3647 * Clear from current message position to end of screen.
3648 * Skip this when ":silent" was used, no need to clear for redirection.
3649 */
3650 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003651msg_clr_eos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652{
3653 if (msg_silent == 0)
3654 msg_clr_eos_force();
3655}
3656
3657/*
3658 * Clear from current message position to end of screen.
3659 * Note: msg_col is not updated, so we remember the end of the message
3660 * for msg_check().
3661 */
3662 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003663msg_clr_eos_force(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664{
Bram Moolenaar3b474dc2022-09-01 17:01:32 +01003665#ifdef HAS_MESSAGE_WINDOW
3666 if (in_echowindow)
3667 return; // messages go into a popup
3668#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 if (msg_use_printf())
3670 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003671 if (full_screen) // only when termcap codes are valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 {
3673 if (*T_CD)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003674 out_str(T_CD); // clear to end of display
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 else if (*T_CE)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003676 out_str(T_CE); // clear to end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 }
3678 }
Bram Moolenaara2a89732022-08-31 14:46:18 +01003679 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 {
3681#ifdef FEAT_RIGHTLEFT
3682 if (cmdmsg_rl)
3683 {
3684 screen_fill(msg_row, msg_row + 1, 0, msg_col + 1, ' ', ' ', 0);
3685 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3686 }
3687 else
3688#endif
3689 {
3690 screen_fill(msg_row, msg_row + 1, msg_col, (int)Columns,
3691 ' ', ' ', 0);
3692 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3693 }
3694 }
3695}
3696
3697/*
3698 * Clear the command line.
3699 */
3700 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003701msg_clr_cmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702{
3703 msg_row = cmdline_row;
3704 msg_col = 0;
3705 msg_clr_eos_force();
3706}
3707
3708/*
3709 * end putting a message on the screen
Bram Moolenaar13608d82022-08-29 15:06:50 +01003710 * call wait_return() if the message does not fit in the available space
3711 * return TRUE if wait_return() not called.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 */
3713 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003714msg_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715{
3716 /*
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02003717 * If the string is larger than the window,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718 * or the ruler option is set and we run into it,
3719 * we have to redraw the window.
3720 * Do not do this if we are abandoning the file or editing the command line.
3721 */
Bram Moolenaar24959102022-05-07 20:01:16 +01003722 if (!exiting && need_wait_return && !(State & MODE_CMDLINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 {
3724 wait_return(FALSE);
3725 return FALSE;
3726 }
3727 out_flush();
3728 return TRUE;
3729}
3730
3731/*
3732 * If the written message runs into the shown command or ruler, we have to
3733 * wait for hit-return and redraw the window later.
3734 */
3735 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003736msg_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737{
Bram Moolenaar35a4fbc2022-08-28 14:39:53 +01003738 if (msg_row == Rows - 1 && msg_col >= sc_col
3739#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaara2a89732022-08-31 14:46:18 +01003740 && !in_echowindow
Bram Moolenaar35a4fbc2022-08-28 14:39:53 +01003741#endif
3742 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 {
3744 need_wait_return = TRUE;
3745 redraw_cmdline = TRUE;
3746 }
3747}
3748
3749/*
3750 * May write a string to the redirection file.
3751 * When "maxlen" is -1 write the whole string, otherwise up to "maxlen" bytes.
3752 */
3753 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003754redir_write(char_u *str, int maxlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755{
3756 char_u *s = str;
3757 static int cur_col = 0;
3758
Bram Moolenaar85a20022019-12-21 18:25:54 +01003759 // Don't do anything for displaying prompts and the like.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003760 if (redir_off)
3761 return;
3762
Bram Moolenaar85a20022019-12-21 18:25:54 +01003763 // If 'verbosefile' is set prepare for writing in that file.
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003764 if (*p_vfile != NUL && verbose_fd == NULL)
3765 verbose_open();
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003766
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003767 if (redirecting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003769 // If the string doesn't start with CR or NL, go to msg_col
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 if (*s != '\n' && *s != '\r')
3771 {
3772 while (cur_col < msg_col)
3773 {
3774#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003775 if (redir_execute)
3776 execute_redir_str((char_u *)" ", -1);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003777 else if (redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 write_reg_contents(redir_reg, (char_u *)" ", -1, TRUE);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003779 else if (redir_vname)
3780 var_redir_str((char_u *)" ", -1);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003781 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003783 if (redir_fd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 fputs(" ", redir_fd);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003785 if (verbose_fd != NULL)
3786 fputs(" ", verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 ++cur_col;
3788 }
3789 }
3790
3791#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003792 if (redir_execute)
3793 execute_redir_str(s, maxlen);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003794 else if (redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 write_reg_contents(redir_reg, s, maxlen, TRUE);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003796 else if (redir_vname)
Bram Moolenaardf177f62005-02-22 08:39:57 +00003797 var_redir_str(s, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798#endif
3799
Bram Moolenaar85a20022019-12-21 18:25:54 +01003800 // Write and adjust the current column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
3802 {
3803#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003804 if (!redir_reg && !redir_vname && !redir_execute)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003806 if (redir_fd != NULL)
3807 putc(*s, redir_fd);
3808 if (verbose_fd != NULL)
3809 putc(*s, verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 if (*s == '\r' || *s == '\n')
3811 cur_col = 0;
3812 else if (*s == '\t')
3813 cur_col += (8 - cur_col % 8);
3814 else
3815 ++cur_col;
3816 ++s;
3817 }
3818
Bram Moolenaar85a20022019-12-21 18:25:54 +01003819 if (msg_silent != 0) // should update msg_col
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820 msg_col = cur_col;
3821 }
3822}
3823
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003824 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003825redirecting(void)
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003826{
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003827 return redir_fd != NULL || *p_vfile != NUL
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003828#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003829 || redir_reg || redir_vname || redir_execute
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003830#endif
3831 ;
3832}
3833
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834/*
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00003835 * Before giving verbose message.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003836 * Must always be called paired with verbose_leave()!
3837 */
3838 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003839verbose_enter(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003840{
3841 if (*p_vfile != NUL)
3842 ++msg_silent;
3843}
3844
3845/*
3846 * After giving verbose message.
3847 * Must always be called paired with verbose_enter()!
3848 */
3849 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003850verbose_leave(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003851{
3852 if (*p_vfile != NUL)
3853 if (--msg_silent < 0)
3854 msg_silent = 0;
3855}
3856
3857/*
3858 * Like verbose_enter() and set msg_scroll when displaying the message.
3859 */
3860 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003861verbose_enter_scroll(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003862{
3863 if (*p_vfile != NUL)
3864 ++msg_silent;
3865 else
Bram Moolenaar85a20022019-12-21 18:25:54 +01003866 // always scroll up, don't overwrite
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003867 msg_scroll = TRUE;
3868}
3869
3870/*
3871 * Like verbose_leave() and set cmdline_row when displaying the message.
3872 */
3873 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003874verbose_leave_scroll(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003875{
3876 if (*p_vfile != NUL)
3877 {
3878 if (--msg_silent < 0)
3879 msg_silent = 0;
3880 }
3881 else
3882 cmdline_row = msg_row;
3883}
3884
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003885/*
3886 * Called when 'verbosefile' is set: stop writing to the file.
3887 */
3888 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003889verbose_stop(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003890{
3891 if (verbose_fd != NULL)
3892 {
3893 fclose(verbose_fd);
3894 verbose_fd = NULL;
3895 }
3896 verbose_did_open = FALSE;
3897}
3898
3899/*
3900 * Open the file 'verbosefile'.
3901 * Return FAIL or OK.
3902 */
3903 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003904verbose_open(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003905{
3906 if (verbose_fd == NULL && !verbose_did_open)
3907 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003908 // Only give the error message once.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003909 verbose_did_open = TRUE;
3910
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003911 verbose_fd = mch_fopen((char *)p_vfile, "a");
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003912 if (verbose_fd == NULL)
3913 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003914 semsg(_(e_cant_open_file_str), p_vfile);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003915 return FAIL;
3916 }
3917 }
3918 return OK;
3919}
3920
3921/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 * Give a warning message (for searching).
3923 * Use 'w' highlighting and may repeat the message after redrawing
3924 */
3925 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003926give_warning(char_u *message, int hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927{
Bram Moolenaarcc7eb2a2021-07-11 19:12:04 +02003928 give_warning_with_source(message, hl, FALSE);
3929}
3930
3931 void
3932give_warning_with_source(char_u *message, int hl, int with_source)
3933{
Bram Moolenaar85a20022019-12-21 18:25:54 +01003934 // Don't do this for ":silent".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 if (msg_silent != 0)
3936 return;
3937
Bram Moolenaar85a20022019-12-21 18:25:54 +01003938 // Don't want a hit-enter prompt here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 ++no_wait_return;
Bram Moolenaared203462004-06-16 11:19:22 +00003940
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941#ifdef FEAT_EVAL
3942 set_vim_var_string(VV_WARNINGMSG, message, -1);
3943#endif
Bram Moolenaard23a8232018-02-10 18:45:26 +01003944 VIM_CLEAR(keep_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 if (hl)
Bram Moolenaar8820b482017-03-16 17:23:31 +01003946 keep_msg_attr = HL_ATTR(HLF_W);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 else
3948 keep_msg_attr = 0;
Bram Moolenaarcc7eb2a2021-07-11 19:12:04 +02003949
3950 if (with_source)
3951 {
3952 // Do what msg() does, but with a column offset if the warning should
3953 // be after the mode message.
3954 msg_start();
3955 msg_source(HL_ATTR(HLF_W));
3956 msg_puts(" ");
3957 msg_puts_attr((char *)message, HL_ATTR(HLF_W) | MSG_HIST);
3958 msg_clr_eos();
3959 (void)msg_end();
3960 }
3961 else if (msg_attr((char *)message, keep_msg_attr) && msg_scrolled == 0)
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003962 set_keep_msg(message, keep_msg_attr);
Bram Moolenaarcc7eb2a2021-07-11 19:12:04 +02003963
Bram Moolenaar85a20022019-12-21 18:25:54 +01003964 msg_didout = FALSE; // overwrite this message
3965 msg_nowait = TRUE; // don't wait for this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 msg_col = 0;
Bram Moolenaared203462004-06-16 11:19:22 +00003967
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 --no_wait_return;
3969}
3970
Bram Moolenaar113e1072019-01-20 15:30:40 +01003971#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarf8be4612017-06-23 20:52:40 +02003972 void
3973give_warning2(char_u *message, char_u *a1, int hl)
3974{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +02003975 if (IObuff == NULL)
3976 {
3977 // Very early in initialisation and already something wrong, just give
3978 // the raw message so the user at least gets a hint.
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003979 give_warning(message, hl);
Bram Moolenaare3a22cb2019-10-14 22:01:57 +02003980 }
3981 else
3982 {
3983 vim_snprintf((char *)IObuff, IOSIZE, (char *)message, a1);
3984 give_warning(IObuff, hl);
3985 }
Bram Moolenaarf8be4612017-06-23 20:52:40 +02003986}
Bram Moolenaar113e1072019-01-20 15:30:40 +01003987#endif
Bram Moolenaarf8be4612017-06-23 20:52:40 +02003988
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989/*
3990 * Advance msg cursor to column "col".
3991 */
3992 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003993msg_advance(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994{
Bram Moolenaar85a20022019-12-21 18:25:54 +01003995 if (msg_silent != 0) // nothing to advance to
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003997 msg_col = col; // for redirection, may fill it up later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 return;
3999 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01004000 if (col >= Columns) // not enough room
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 col = Columns - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004002#ifdef FEAT_RIGHTLEFT
4003 if (cmdmsg_rl)
4004 while (msg_col > Columns - col)
4005 msg_putchar(' ');
4006 else
4007#endif
4008 while (msg_col < col)
4009 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010}
4011
4012#if defined(FEAT_CON_DIALOG) || defined(PROTO)
4013/*
4014 * Used for "confirm()" function, and the :confirm command prefix.
4015 * Versions which haven't got flexible dialogs yet, and console
4016 * versions, get this generic handler which uses the command line.
4017 *
4018 * type = one of:
4019 * VIM_QUESTION, VIM_INFO, VIM_WARNING, VIM_ERROR or VIM_GENERIC
4020 * title = title string (can be NULL for default)
4021 * (neither used in console dialogs at the moment)
4022 *
4023 * Format of the "buttons" string:
4024 * "Button1Name\nButton2Name\nButton3Name"
4025 * The first button should normally be the default/accept
4026 * The second button should be the 'Cancel' button
4027 * Other buttons- use your imagination!
4028 * A '&' in a button name becomes a shortcut, so each '&' should be before a
4029 * different letter.
Rob Pilling8196e942022-02-11 15:12:10 +00004030 *
4031 * Returns 0 if cancelled, otherwise the nth button (1-indexed).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004034do_dialog(
4035 int type UNUSED,
4036 char_u *title UNUSED,
4037 char_u *message,
4038 char_u *buttons,
4039 int dfltbutton,
Bram Moolenaar85a20022019-12-21 18:25:54 +01004040 char_u *textfield UNUSED, // IObuff for inputdialog(), NULL
4041 // otherwise
4042 int ex_cmd) // when TRUE pressing : accepts default and starts
4043 // Ex command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044{
4045 int oldState;
4046 int retval = 0;
4047 char_u *hotkeys;
4048 int c;
4049 int i;
Bram Moolenaar27321db2020-07-06 21:24:57 +02004050 tmode_T save_tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051
4052#ifndef NO_CONSOLE
Bram Moolenaar85a20022019-12-21 18:25:54 +01004053 // Don't output anything in silent mode ("ex -s")
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 if (silent_mode)
Bram Moolenaar85a20022019-12-21 18:25:54 +01004055 return dfltbutton; // return default option
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056#endif
4057
4058#ifdef FEAT_GUI_DIALOG
Bram Moolenaar85a20022019-12-21 18:25:54 +01004059 // When GUI is running and 'c' not in 'guioptions', use the GUI dialog
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
4061 {
Bram Moolenaar2d12c252022-06-13 21:42:45 +01004062 // --gui-dialog-file: write text to a file
4063 if (gui_dialog_log(title, message))
4064 c = dfltbutton;
4065 else
4066 c = gui_mch_dialog(type, title, message, buttons, dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004067 textfield, ex_cmd);
Bram Moolenaar85a20022019-12-21 18:25:54 +01004068 // avoid a hit-enter prompt without clearing the cmdline
Bram Moolenaar8de49e12009-02-11 17:47:54 +00004069 need_wait_return = FALSE;
4070 emsg_on_display = FALSE;
4071 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072
Bram Moolenaar85a20022019-12-21 18:25:54 +01004073 // Flush output to avoid that further messages and redrawing is done
4074 // in the wrong order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 out_flush();
4076 gui_mch_update();
4077
4078 return c;
4079 }
4080#endif
4081
4082 oldState = State;
Bram Moolenaar24959102022-05-07 20:01:16 +01004083 State = MODE_CONFIRM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085
Bram Moolenaar27321db2020-07-06 21:24:57 +02004086 // Ensure raw mode here.
4087 save_tmode = cur_tmode;
4088 settmode(TMODE_RAW);
4089
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 /*
4091 * Since we wait for a keypress, don't make the
4092 * user press RETURN as well afterwards.
4093 */
4094 ++no_wait_return;
4095 hotkeys = msg_show_console_dialog(message, buttons, dfltbutton);
4096
4097 if (hotkeys != NULL)
4098 {
4099 for (;;)
4100 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004101 // Get a typed character directly from the user.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 c = get_keystroke();
4103 switch (c)
4104 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004105 case CAR: // User accepts default option
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 case NL:
4107 retval = dfltbutton;
4108 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01004109 case Ctrl_C: // User aborts/cancels
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 case ESC:
4111 retval = 0;
4112 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01004113 default: // Could be a hotkey?
4114 if (c < 0) // special keys are ignored here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 continue;
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004116 if (c == ':' && ex_cmd)
4117 {
4118 retval = dfltbutton;
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02004119 ins_char_typebuf(':', 0);
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004120 break;
4121 }
4122
Bram Moolenaar85a20022019-12-21 18:25:54 +01004123 // Make the character lowercase, as chars in "hotkeys" are.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 c = MB_TOLOWER(c);
4125 retval = 1;
4126 for (i = 0; hotkeys[i]; ++i)
4127 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 if (has_mbyte)
4129 {
4130 if ((*mb_ptr2char)(hotkeys + i) == c)
4131 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004132 i += (*mb_ptr2len)(hotkeys + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 }
4134 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 if (hotkeys[i] == c)
4136 break;
4137 ++retval;
4138 }
4139 if (hotkeys[i])
4140 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01004141 // No hotkey match, so keep waiting
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 continue;
4143 }
4144 break;
4145 }
4146
4147 vim_free(hotkeys);
4148 }
4149
Bram Moolenaar27321db2020-07-06 21:24:57 +02004150 settmode(save_tmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 State = oldState;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 --no_wait_return;
4154 msg_end_prompt();
4155
4156 return retval;
4157}
4158
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159/*
4160 * Copy one character from "*from" to "*to", taking care of multi-byte
4161 * characters. Return the length of the character in bytes.
4162 */
4163 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004164copy_char(
4165 char_u *from,
4166 char_u *to,
Bram Moolenaar85a20022019-12-21 18:25:54 +01004167 int lowercase) // make character lower case
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 int len;
4170 int c;
4171
4172 if (has_mbyte)
4173 {
4174 if (lowercase)
4175 {
4176 c = MB_TOLOWER((*mb_ptr2char)(from));
4177 return (*mb_char2bytes)(c, to);
4178 }
4179 else
4180 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004181 len = (*mb_ptr2len)(from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 mch_memmove(to, from, (size_t)len);
4183 return len;
4184 }
4185 }
4186 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 {
4188 if (lowercase)
4189 *to = (char_u)TOLOWER_LOC(*from);
4190 else
4191 *to = *from;
4192 return 1;
4193 }
4194}
4195
4196/*
4197 * Format the dialog string, and display it at the bottom of
4198 * the screen. Return a string of hotkey chars (if defined) for
4199 * each 'button'. If a button has no hotkey defined, the first character of
4200 * the button is used.
4201 * The hotkeys can be multi-byte characters, but without combining chars.
4202 *
4203 * Returns an allocated string with hotkeys, or NULL for error.
4204 */
4205 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004206msg_show_console_dialog(
4207 char_u *message,
4208 char_u *buttons,
4209 int dfltbutton)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210{
4211 int len = 0;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01004212#define HOTK_LEN (has_mbyte ? MB_MAXBYTES : 1)
Bram Moolenaar85a20022019-12-21 18:25:54 +01004213 int lenhotkey = HOTK_LEN; // count first button
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 char_u *hotk = NULL;
4215 char_u *msgp = NULL;
4216 char_u *hotkp = NULL;
4217 char_u *r;
4218 int copy;
4219#define HAS_HOTKEY_LEN 30
4220 char_u has_hotkey[HAS_HOTKEY_LEN];
Bram Moolenaar85a20022019-12-21 18:25:54 +01004221 int first_hotkey = FALSE; // first char of button is hotkey
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 int idx;
4223
4224 has_hotkey[0] = FALSE;
4225
4226 /*
4227 * First loop: compute the size of memory to allocate.
4228 * Second loop: copy to the allocated memory.
4229 */
4230 for (copy = 0; copy <= 1; ++copy)
4231 {
4232 r = buttons;
4233 idx = 0;
4234 while (*r)
4235 {
4236 if (*r == DLG_BUTTON_SEP)
4237 {
4238 if (copy)
4239 {
4240 *msgp++ = ',';
Bram Moolenaar85a20022019-12-21 18:25:54 +01004241 *msgp++ = ' '; // '\n' -> ', '
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242
Bram Moolenaar85a20022019-12-21 18:25:54 +01004243 // advance to next hotkey and set default hotkey
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 if (has_mbyte)
Bram Moolenaar6a516062007-07-05 08:11:42 +00004245 hotkp += STRLEN(hotkp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 ++hotkp;
Bram Moolenaar6a516062007-07-05 08:11:42 +00004248 hotkp[copy_char(r + 1, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 if (dfltbutton)
4250 --dfltbutton;
4251
Bram Moolenaar85a20022019-12-21 18:25:54 +01004252 // If no hotkey is specified first char is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 if (idx < HAS_HOTKEY_LEN - 1 && !has_hotkey[++idx])
4254 first_hotkey = TRUE;
4255 }
4256 else
4257 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004258 len += 3; // '\n' -> ', '; 'x' -> '(x)'
4259 lenhotkey += HOTK_LEN; // each button needs a hotkey
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 if (idx < HAS_HOTKEY_LEN - 1)
4261 has_hotkey[++idx] = FALSE;
4262 }
4263 }
4264 else if (*r == DLG_HOTKEY_CHAR || first_hotkey)
4265 {
4266 if (*r == DLG_HOTKEY_CHAR)
4267 ++r;
4268 first_hotkey = FALSE;
4269 if (copy)
4270 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004271 if (*r == DLG_HOTKEY_CHAR) // '&&a' -> '&a'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 *msgp++ = *r;
4273 else
4274 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004275 // '&a' -> '[a]'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 *msgp++ = (dfltbutton == 1) ? '[' : '(';
4277 msgp += copy_char(r, msgp, FALSE);
4278 *msgp++ = (dfltbutton == 1) ? ']' : ')';
4279
Bram Moolenaar85a20022019-12-21 18:25:54 +01004280 // redefine hotkey
Bram Moolenaar6a516062007-07-05 08:11:42 +00004281 hotkp[copy_char(r, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 }
4283 }
4284 else
4285 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004286 ++len; // '&a' -> '[a]'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 if (idx < HAS_HOTKEY_LEN - 1)
4288 has_hotkey[idx] = TRUE;
4289 }
4290 }
4291 else
4292 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004293 // everything else copy literally
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 if (copy)
4295 msgp += copy_char(r, msgp, FALSE);
4296 }
4297
Bram Moolenaar85a20022019-12-21 18:25:54 +01004298 // advance to the next character
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004299 MB_PTR_ADV(r);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 }
4301
4302 if (copy)
4303 {
4304 *msgp++ = ':';
4305 *msgp++ = ' ';
4306 *msgp = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 }
4308 else
4309 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004310 len += (int)(STRLEN(message)
Bram Moolenaar85a20022019-12-21 18:25:54 +01004311 + 2 // for the NL's
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004312 + STRLEN(buttons)
Bram Moolenaar85a20022019-12-21 18:25:54 +01004313 + 3); // for the ": " and NUL
4314 lenhotkey++; // for the NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315
Bram Moolenaar85a20022019-12-21 18:25:54 +01004316 // If no hotkey is specified first char is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 if (!has_hotkey[0])
4318 {
4319 first_hotkey = TRUE;
Bram Moolenaar85a20022019-12-21 18:25:54 +01004320 len += 2; // "x" -> "[x]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 }
4322
4323 /*
4324 * Now allocate and load the strings
4325 */
4326 vim_free(confirm_msg);
4327 confirm_msg = alloc(len);
4328 if (confirm_msg == NULL)
4329 return NULL;
4330 *confirm_msg = NUL;
4331 hotk = alloc(lenhotkey);
4332 if (hotk == NULL)
4333 return NULL;
4334
4335 *confirm_msg = '\n';
4336 STRCPY(confirm_msg + 1, message);
4337
4338 msgp = confirm_msg + 1 + STRLEN(message);
4339 hotkp = hotk;
4340
Bram Moolenaar85a20022019-12-21 18:25:54 +01004341 // Define first default hotkey. Keep the hotkey string NUL
4342 // terminated to avoid reading past the end.
Bram Moolenaar6a516062007-07-05 08:11:42 +00004343 hotkp[copy_char(buttons, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344
Bram Moolenaar85a20022019-12-21 18:25:54 +01004345 // Remember where the choices start, displaying starts here when
4346 // "hotkp" typed at the more prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 confirm_msg_tail = msgp;
4348 *msgp++ = '\n';
4349 }
4350 }
4351
4352 display_confirm_msg();
4353 return hotk;
4354}
4355
4356/*
4357 * Display the ":confirm" message. Also called when screen resized.
4358 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02004359 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004360display_confirm_msg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361{
Bram Moolenaar85a20022019-12-21 18:25:54 +01004362 // avoid that 'q' at the more prompt truncates the message here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 ++confirm_msg_used;
4364 if (confirm_msg != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004365 msg_puts_attr((char *)confirm_msg, HL_ATTR(HLF_M));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 --confirm_msg_used;
4367}
4368
Bram Moolenaar85a20022019-12-21 18:25:54 +01004369#endif // FEAT_CON_DIALOG
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370
4371#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4372
4373 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004374vim_dialog_yesno(
4375 int type,
4376 char_u *title,
4377 char_u *message,
4378 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379{
4380 if (do_dialog(type,
4381 title == NULL ? (char_u *)_("Question") : title,
4382 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004383 (char_u *)_("&Yes\n&No"), dflt, NULL, FALSE) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 return VIM_YES;
4385 return VIM_NO;
4386}
4387
4388 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004389vim_dialog_yesnocancel(
4390 int type,
4391 char_u *title,
4392 char_u *message,
4393 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394{
4395 switch (do_dialog(type,
4396 title == NULL ? (char_u *)_("Question") : title,
4397 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004398 (char_u *)_("&Yes\n&No\n&Cancel"), dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 {
4400 case 1: return VIM_YES;
4401 case 2: return VIM_NO;
4402 }
4403 return VIM_CANCEL;
4404}
4405
4406 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004407vim_dialog_yesnoallcancel(
4408 int type,
4409 char_u *title,
4410 char_u *message,
4411 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412{
4413 switch (do_dialog(type,
4414 title == NULL ? (char_u *)"Question" : title,
4415 message,
4416 (char_u *)_("&Yes\n&No\nSave &All\n&Discard All\n&Cancel"),
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004417 dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 {
4419 case 1: return VIM_YES;
4420 case 2: return VIM_NO;
4421 case 3: return VIM_ALL;
4422 case 4: return VIM_DISCARDALL;
4423 }
4424 return VIM_CANCEL;
4425}
4426
Bram Moolenaar85a20022019-12-21 18:25:54 +01004427#endif // FEAT_GUI_DIALOG || FEAT_CON_DIALOG