blob: 985161d2e818c3590eed6337c0e42684f7b80862 [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 Moolenaar3b8c7082022-11-30 20:20:56 +000040#ifdef FEAT_EVAL
Bram Moolenaar958dc692016-12-01 15:34:12 +010041static 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 Moolenaar4c5678f2022-11-30 18:12:19 +0000164#ifdef FEAT_EVAL
Bram Moolenaar958dc692016-12-01 15:34:12 +0100165 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 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000379 va_list arglist;
380
381 va_start(arglist, s);
382 vim_vsnprintf((char *)IObuff, IOSIZE, s, arglist);
383 va_end(arglist);
384 return msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385}
386
387 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100388smsg_attr(int attr, const char *s, ...)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200390 if (IObuff == NULL)
391 {
392 // Very early in initialisation and already something wrong, just
393 // give the raw message so the user at least gets a hint.
394 return msg_attr((char *)s, attr);
395 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000397 va_list arglist;
398
399 va_start(arglist, s);
400 vim_vsnprintf((char *)IObuff, IOSIZE, s, arglist);
401 va_end(arglist);
402 return msg_attr((char *)IObuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403}
404
Bram Moolenaare0429682018-07-01 16:44:03 +0200405 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100406smsg_attr_keep(int attr, const char *s, ...)
Bram Moolenaare0429682018-07-01 16:44:03 +0200407{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200408 if (IObuff == NULL)
409 {
410 // Very early in initialisation and already something wrong, just
411 // give the raw message so the user at least gets a hint.
412 return msg_attr_keep((char *)s, attr, TRUE);
413 }
Bram Moolenaare0429682018-07-01 16:44:03 +0200414
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000415 va_list arglist;
416
417 va_start(arglist, s);
418 vim_vsnprintf((char *)IObuff, IOSIZE, s, arglist);
419 va_end(arglist);
420 return msg_attr_keep((char *)IObuff, attr, TRUE);
Bram Moolenaare0429682018-07-01 16:44:03 +0200421}
422
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423#endif
424
425/*
426 * Remember the last sourcing name/lnum used in an error message, so that it
427 * isn't printed each time when it didn't change.
428 */
429static int last_sourcing_lnum = 0;
430static char_u *last_sourcing_name = NULL;
431
432/*
433 * Reset the last used sourcing name/lnum. Makes sure it is displayed again
434 * for the next error message;
435 */
Bram Moolenaar4eec5ec2005-06-26 22:26:21 +0000436 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100437reset_last_sourcing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438{
Bram Moolenaard23a8232018-02-10 18:45:26 +0100439 VIM_CLEAR(last_sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440 last_sourcing_lnum = 0;
441}
442
443/*
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100444 * Return TRUE if "SOURCING_NAME" differs from "last_sourcing_name".
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000445 */
446 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100447other_sourcing_name(void)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000448{
Bram Moolenaar0bd8d052021-11-25 13:39:28 +0000449 if (HAVE_SOURCING_INFO && SOURCING_NAME != NULL)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000450 {
451 if (last_sourcing_name != NULL)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100452 return STRCMP(SOURCING_NAME, last_sourcing_name) != 0;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000453 return TRUE;
454 }
455 return FALSE;
456}
457
458/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459 * Get the message about the source, as used for an error message.
460 * Returns an allocated string with room for one more character.
461 * Returns NULL when no message is to be given.
462 */
463 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100464get_emsg_source(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465{
466 char_u *Buf, *p;
467
Bram Moolenaar0bd8d052021-11-25 13:39:28 +0000468 if (HAVE_SOURCING_INFO && SOURCING_NAME != NULL && other_sourcing_name())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000469 {
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200470 char_u *sname = estack_sfile(ESTACK_NONE);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100471 char_u *tofree = sname;
472
473 if (sname == NULL)
474 sname = SOURCING_NAME;
475
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +0200476#ifdef FEAT_EVAL
477 if (estack_compiling)
478 p = (char_u *)_("Error detected while compiling %s:");
479 else
480#endif
481 p = (char_u *)_("Error detected while processing %s:");
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100482 Buf = alloc(STRLEN(sname) + STRLEN(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483 if (Buf != NULL)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100484 sprintf((char *)Buf, (char *)p, sname);
485 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486 return Buf;
487 }
488 return NULL;
489}
490
491/*
492 * Get the message about the source lnum, as used for an error message.
493 * Returns an allocated string with room for one more character.
494 * Returns NULL when no message is to be given.
495 */
496 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100497get_emsg_lnum(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498{
499 char_u *Buf, *p;
500
Bram Moolenaar85a20022019-12-21 18:25:54 +0100501 // lnum is 0 when executing a command from the command line
502 // argument, we don't want a line number then
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100503 if (SOURCING_NAME != NULL
504 && (other_sourcing_name() || SOURCING_LNUM != last_sourcing_lnum)
505 && SOURCING_LNUM != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506 {
507 p = (char_u *)_("line %4ld:");
Bram Moolenaar964b3742019-05-24 18:54:09 +0200508 Buf = alloc(STRLEN(p) + 20);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509 if (Buf != NULL)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100510 sprintf((char *)Buf, (char *)p, (long)SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 return Buf;
512 }
513 return NULL;
514}
515
516/*
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000517 * Display name and line number for the source of an error.
518 * Remember the file name and line number, so that for the next error the info
519 * is only displayed if it changed.
520 */
521 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100522msg_source(int attr)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000523{
524 char_u *p;
Bram Moolenaarba8c9262021-11-25 14:43:18 +0000525 static int recursive = FALSE;
526
527 // Bail out if something called here causes an error.
528 if (recursive)
529 return;
530 recursive = TRUE;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000531
532 ++no_wait_return;
533 p = get_emsg_source();
534 if (p != NULL)
535 {
zeertzjqbdedd2b2022-09-20 12:45:15 +0100536 msg_scroll = TRUE; // this will take more than one line
Bram Moolenaar32526b32019-01-19 17:43:09 +0100537 msg_attr((char *)p, attr);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000538 vim_free(p);
539 }
540 p = get_emsg_lnum();
541 if (p != NULL)
542 {
Bram Moolenaar32526b32019-01-19 17:43:09 +0100543 msg_attr((char *)p, HL_ATTR(HLF_N));
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000544 vim_free(p);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100545 last_sourcing_lnum = SOURCING_LNUM; // only once for each line
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000546 }
547
Bram Moolenaar85a20022019-12-21 18:25:54 +0100548 // remember the last sourcing name printed, also when it's empty
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100549 if (SOURCING_NAME == NULL || other_sourcing_name())
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000550 {
Bram Moolenaarba8c9262021-11-25 14:43:18 +0000551 VIM_CLEAR(last_sourcing_name);
552 if (SOURCING_NAME != NULL)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100553 last_sourcing_name = vim_strsave(SOURCING_NAME);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000554 }
555 --no_wait_return;
Bram Moolenaarba8c9262021-11-25 14:43:18 +0000556
557 recursive = FALSE;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000558}
559
560/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000561 * Return TRUE if not giving error messages right now:
562 * If "emsg_off" is set: no error messages at the moment.
563 * If "msg" is in 'debug': do error message but without side effects.
564 * If "emsg_skip" is set: never do error messages.
565 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +0200566 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100567emsg_not_now(void)
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000568{
569 if ((emsg_off > 0 && vim_strchr(p_debug, 'm') == NULL
570 && vim_strchr(p_debug, 't') == NULL)
571#ifdef FEAT_EVAL
572 || emsg_skip > 0
573#endif
574 )
575 return TRUE;
576 return FALSE;
577}
578
Bram Moolenaar5a66dfb2017-03-01 20:40:39 +0100579#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100580static garray_T ignore_error_list = GA_EMPTY;
581
582 void
583ignore_error_for_testing(char_u *error)
584{
585 if (ignore_error_list.ga_itemsize == 0)
586 ga_init2(&ignore_error_list, sizeof(char_u *), 1);
587
Bram Moolenaar461a7fc2018-12-22 13:28:07 +0100588 if (STRCMP("RESET", error) == 0)
589 ga_clear_strings(&ignore_error_list);
590 else
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000591 ga_copy_string(&ignore_error_list, error);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100592}
593
594 static int
Bram Moolenaar097c5372023-05-24 21:02:24 +0100595ignore_error(const char *msg)
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100596{
597 int i;
598
599 for (i = 0; i < ignore_error_list.ga_len; ++i)
Bram Moolenaar097c5372023-05-24 21:02:24 +0100600 if (strstr(msg,
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100601 (char *)((char_u **)(ignore_error_list.ga_data))[i]) != NULL)
602 return TRUE;
603 return FALSE;
604}
605#endif
606
Bram Moolenaarb869c0d2016-07-20 00:10:51 +0200607#if !defined(HAVE_STRERROR) || defined(PROTO)
608/*
609 * Replacement for perror() that behaves more or less like emsg() was called.
Bram Moolenaar53989552019-12-23 22:59:18 +0100610 * v:errmsg will be set and called_emsg will be incremented.
Bram Moolenaarb869c0d2016-07-20 00:10:51 +0200611 */
612 void
613do_perror(char *msg)
614{
615 perror(msg);
616 ++emsg_silent;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100617 emsg(msg);
Bram Moolenaarb869c0d2016-07-20 00:10:51 +0200618 --emsg_silent;
619}
620#endif
621
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000622/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100623 * emsg_core() - display an error message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624 *
625 * Rings the bell, if appropriate, and calls message() to do the real work
626 * When terminal not initialized (yet) mch_errmsg(..) is used.
627 *
Bram Moolenaar13608d82022-08-29 15:06:50 +0100628 * Return TRUE if wait_return() not called.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100629 * Note: caller must check 'emsg_not_now()' before calling this.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100631 static int
Bram Moolenaar097c5372023-05-24 21:02:24 +0100632emsg_core(const char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633{
634 int attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635 char_u *p;
Bram Moolenaar958dc692016-12-01 15:34:12 +0100636 int r;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637#ifdef FEAT_EVAL
638 int ignore = FALSE;
639 int severe;
640#endif
641
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100642#ifdef FEAT_EVAL
Bram Moolenaar85a20022019-12-21 18:25:54 +0100643 // When testing some errors are turned into a normal message.
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100644 if (ignore_error(s))
Bram Moolenaar85a20022019-12-21 18:25:54 +0100645 // don't call msg() if it results in a dialog
Bram Moolenaar32526b32019-01-19 17:43:09 +0100646 return msg_use_printf() ? FALSE : msg((char *)s);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100647#endif
648
Bram Moolenaar53989552019-12-23 22:59:18 +0100649 ++called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651#ifdef FEAT_EVAL
Bram Moolenaar85a20022019-12-21 18:25:54 +0100652 // If "emsg_severe" is TRUE: When an error exception is to be thrown,
653 // prefer this message over previous messages for the same command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654 severe = emsg_severe;
655 emsg_severe = FALSE;
656#endif
657
Bram Moolenaar57657d82006-04-21 22:12:41 +0000658 if (!emsg_off || vim_strchr(p_debug, 't') != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 {
660#ifdef FEAT_EVAL
661 /*
662 * Cause a throw of an error exception if appropriate. Don't display
663 * the error message in this case. (If no matching catch clause will
664 * be found, the message will be displayed later on.) "ignore" is set
665 * when the message should be ignored completely (used for the
666 * interrupt message).
667 */
Bram Moolenaar097c5372023-05-24 21:02:24 +0100668 if (cause_errthrow((char_u *)s, severe, &ignore) == TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669 {
670 if (!ignore)
Bram Moolenaar76a63452018-11-28 20:38:37 +0100671 ++did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 return TRUE;
673 }
674
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100675 if (in_assert_fails && emsg_assert_fails_msg == NULL)
Bram Moolenaar1d634542020-08-18 13:41:50 +0200676 {
Bram Moolenaar097c5372023-05-24 21:02:24 +0100677 emsg_assert_fails_msg = vim_strsave((char_u *)s);
Bram Moolenaar1d634542020-08-18 13:41:50 +0200678 emsg_assert_fails_lnum = SOURCING_LNUM;
Bram Moolenaar9bd5d872020-09-06 21:47:48 +0200679 vim_free(emsg_assert_fails_context);
680 emsg_assert_fails_context = vim_strsave(
681 SOURCING_NAME == NULL ? (char_u *)"" : SOURCING_NAME);
Bram Moolenaar1d634542020-08-18 13:41:50 +0200682 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200683
Bram Moolenaar85a20022019-12-21 18:25:54 +0100684 // set "v:errmsg", also when using ":silent! cmd"
Bram Moolenaar097c5372023-05-24 21:02:24 +0100685 set_vim_var_string(VV_ERRMSG, (char_u *)s, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686#endif
687
688 /*
Bram Moolenaara7241f52008-06-24 20:39:31 +0000689 * When using ":silent! cmd" ignore error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 * But do write it to the redirection file.
691 */
692 if (emsg_silent != 0)
693 {
Bram Moolenaar599410c2021-04-10 14:03:43 +0200694#ifdef FEAT_EVAL
695 ++did_emsg_silent;
696#endif
Bram Moolenaar79815f12016-07-09 17:07:29 +0200697 if (emsg_noredir == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 {
Bram Moolenaar79815f12016-07-09 17:07:29 +0200699 msg_start();
700 p = get_emsg_source();
701 if (p != NULL)
702 {
703 STRCAT(p, "\n");
704 redir_write(p, -1);
705 vim_free(p);
706 }
707 p = get_emsg_lnum();
708 if (p != NULL)
709 {
710 STRCAT(p, "\n");
711 redir_write(p, -1);
712 vim_free(p);
713 }
Bram Moolenaar097c5372023-05-24 21:02:24 +0100714 redir_write((char_u *)s, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +0100716#ifdef FEAT_EVAL
717 // Only increment did_emsg_def when :silent! wasn't used inside the
718 // :def function.
719 if (emsg_silent == emsg_silent_def)
720 ++did_emsg_def;
721#endif
Bram Moolenaar4c5678f2022-11-30 18:12:19 +0000722#ifdef FEAT_EVAL
Bram Moolenaar097c5372023-05-24 21:02:24 +0100723 ch_log(NULL, "ERROR silent: %s", s);
Bram Moolenaar958dc692016-12-01 15:34:12 +0100724#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725 return TRUE;
726 }
727
Bram Moolenaar2b7bc562017-01-14 19:24:52 +0100728 ex_exitval = 1;
729
Bram Moolenaar85a20022019-12-21 18:25:54 +0100730 // Reset msg_silent, an error causes messages to be switched back on.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731 msg_silent = 0;
732 cmd_silent = FALSE;
733
Bram Moolenaar85a20022019-12-21 18:25:54 +0100734 if (global_busy) // break :global command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735 ++global_busy;
736
737 if (p_eb)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100738 beep_flush(); // also includes flush_buffers()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 else
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200740 flush_buffers(FLUSH_MINIMAL); // flush internal buffers
Bram Moolenaar76a63452018-11-28 20:38:37 +0100741 ++did_emsg; // flag for DoOneCmd()
Bram Moolenaare723c422017-09-06 23:40:10 +0200742#ifdef FEAT_EVAL
Bram Moolenaar88c89c72021-08-14 14:01:05 +0200743 ++uncaught_emsg;
Bram Moolenaare723c422017-09-06 23:40:10 +0200744#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745 }
746
Bram Moolenaar878e1d22022-08-28 17:53:23 +0100747#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaara2a89732022-08-31 14:46:18 +0100748 if (!in_echowindow)
Bram Moolenaar878e1d22022-08-28 17:53:23 +0100749#endif
750 emsg_on_display = TRUE; // remember there is an error message
751
752 attr = HL_ATTR(HLF_E); // set highlight mode for error messages
Bram Moolenaarbb15b652005-10-03 21:52:09 +0000753 if (msg_scrolled != 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100754 need_wait_return = TRUE; // needed in case emsg() is called after
Bram Moolenaar13608d82022-08-29 15:06:50 +0100755 // wait_return() has reset need_wait_return
Bram Moolenaar85a20022019-12-21 18:25:54 +0100756 // and a redraw is expected because
757 // msg_scrolled is non-zero
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758
Bram Moolenaar958dc692016-12-01 15:34:12 +0100759#ifdef FEAT_JOB_CHANNEL
760 emsg_to_channel_log = TRUE;
761#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 /*
763 * Display name and line number for the source of the error.
764 */
zeertzjqbdedd2b2022-09-20 12:45:15 +0100765 msg_scroll = TRUE;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000766 msg_source(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767
768 /*
769 * Display the error message itself.
770 */
Bram Moolenaar85a20022019-12-21 18:25:54 +0100771 msg_nowait = FALSE; // wait for this msg
Bram Moolenaar32526b32019-01-19 17:43:09 +0100772 r = msg_attr((char *)s, attr);
Bram Moolenaar958dc692016-12-01 15:34:12 +0100773
774#ifdef FEAT_JOB_CHANNEL
775 emsg_to_channel_log = FALSE;
776#endif
777 return r;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778}
779
780/*
Bram Moolenaar097c5372023-05-24 21:02:24 +0100781 * Print error message "s". Should already be translated.
782 * Return TRUE if wait_return() not called.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 */
784 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100785emsg(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786{
Bram Moolenaar85a20022019-12-21 18:25:54 +0100787 // Skip this if not giving error messages at the moment.
Bram Moolenaar097c5372023-05-24 21:02:24 +0100788 if (emsg_not_now())
789 return TRUE;
790
791 return emsg_core(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792}
793
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100794#ifndef PROTO // manual proto with __attribute__
Bram Moolenaar95f09602016-11-10 20:01:45 +0100795/*
Bram Moolenaar097c5372023-05-24 21:02:24 +0100796 * Print error message "s" with format string and variable arguments.
797 * "s" should already be translated.
798 * Note: caller must not use "IObuff" for "s"!
799 * Return TRUE if wait_return() not called.
Bram Moolenaar95f09602016-11-10 20:01:45 +0100800 */
801 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100802semsg(const char *s, ...)
Bram Moolenaar95f09602016-11-10 20:01:45 +0100803{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200804 // Skip this if not giving error messages at the moment.
Bram Moolenaar097c5372023-05-24 21:02:24 +0100805 if (emsg_not_now())
806 return TRUE;
Bram Moolenaar95f09602016-11-10 20:01:45 +0100807
Bram Moolenaar097c5372023-05-24 21:02:24 +0100808 if (IObuff == NULL)
809 // Very early in initialisation and already something wrong, just
810 // give the raw message so the user at least gets a hint.
811 return emsg_core(s);
812
813 va_list ap;
814
815 va_start(ap, s);
816 vim_vsnprintf((char *)IObuff, IOSIZE, s, ap);
817 va_end(ap);
818 return emsg_core((char *)IObuff);
Bram Moolenaar95f09602016-11-10 20:01:45 +0100819}
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100820#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +0100821
822/*
823 * Same as emsg(...), but abort on error when ABORT_ON_INTERNAL_ERROR is
824 * defined. It is used for internal errors only, so that they can be
825 * detected when fuzzing vim.
826 */
827 void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100828iemsg(char *s)
Bram Moolenaar95f09602016-11-10 20:01:45 +0100829{
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000830 if (emsg_not_now())
831 return;
832
Bram Moolenaar097c5372023-05-24 21:02:24 +0100833 // Give a generic error which is translated. The error itself may not be
834 // translated, it almost never shows.
835 emsg_core(_(e_internal_error_please_report_a_bug));
836
837 emsg_core(s);
Bram Moolenaar096ca732022-01-01 00:55:28 +0000838#if defined(ABORT_ON_INTERNAL_ERROR) && defined(FEAT_EVAL)
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000839 set_vim_var_string(VV_ERRMSG, (char_u *)s, -1);
840 msg_putchar('\n'); // avoid overwriting the error message
841 out_flush();
842 abort();
Bram Moolenaar95f09602016-11-10 20:01:45 +0100843#endif
844}
845
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100846#ifndef PROTO // manual proto with __attribute__
Bram Moolenaar95f09602016-11-10 20:01:45 +0100847/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100848 * Same as semsg(...) but abort on error when ABORT_ON_INTERNAL_ERROR is
Bram Moolenaar95f09602016-11-10 20:01:45 +0100849 * defined. It is used for internal errors only, so that they can be
850 * detected when fuzzing vim.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100851 * Note: caller must not pass 'IObuff' as 1st argument.
Bram Moolenaar95f09602016-11-10 20:01:45 +0100852 */
853 void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100854siemsg(const char *s, ...)
Bram Moolenaar95f09602016-11-10 20:01:45 +0100855{
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000856 if (emsg_not_now())
857 return;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100858
Bram Moolenaar097c5372023-05-24 21:02:24 +0100859 // Give a generic error which is translated. The error itself may not be
860 // translated, it almost never shows.
861 emsg_core(_(e_internal_error_please_report_a_bug));
862
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000863 if (IObuff == NULL)
864 {
865 // Very early in initialisation and already something wrong, just
866 // give the raw message so the user at least gets a hint.
Bram Moolenaar097c5372023-05-24 21:02:24 +0100867 emsg_core(s);
Bram Moolenaar213e70e2022-08-19 13:17:21 +0100868 }
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000869 else
870 {
871 va_list ap;
872
873 va_start(ap, s);
874 vim_vsnprintf((char *)IObuff, IOSIZE, s, ap);
875 va_end(ap);
Bram Moolenaar097c5372023-05-24 21:02:24 +0100876 emsg_core((char *)IObuff);
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000877 }
878# ifdef ABORT_ON_INTERNAL_ERROR
879 msg_putchar('\n'); // avoid overwriting the error message
880 out_flush();
881 abort();
882# endif
Bram Moolenaar95f09602016-11-10 20:01:45 +0100883}
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100884#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +0100885
886/*
887 * Give an "Internal error" message.
888 */
889 void
890internal_error(char *where)
891{
Bram Moolenaar097c5372023-05-24 21:02:24 +0100892 // Give a generic error which is translated. The error itself may not be
893 // translated, it almost never shows.
894 emsg_core(_(e_internal_error_please_report_a_bug));
895
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000896 siemsg(_(e_internal_error_str), where);
Bram Moolenaar95f09602016-11-10 20:01:45 +0100897}
898
Dominique Pelle748b3082022-01-08 12:41:16 +0000899#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaardd589232020-02-29 17:38:12 +0100900/*
901 * Like internal_error() but do not call abort(), to avoid tests using
902 * test_unknown() and test_void() causing Vim to exit.
903 */
904 void
905internal_error_no_abort(char *where)
906{
Bram Moolenaar097c5372023-05-24 21:02:24 +0100907 // Give a generic error which is translated. The error itself may not be
908 // translated, it almost never shows.
909 emsg_core(_(e_internal_error_please_report_a_bug));
910
911 semsg(_(e_internal_error_str), where);
Bram Moolenaardd589232020-02-29 17:38:12 +0100912}
Dominique Pelle748b3082022-01-08 12:41:16 +0000913#endif
Bram Moolenaardd589232020-02-29 17:38:12 +0100914
Bram Moolenaar85a20022019-12-21 18:25:54 +0100915// emsg3() and emsgn() are in misc2.c to avoid warnings for the prototypes.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916
Bram Moolenaardf177f62005-02-22 08:39:57 +0000917 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100918emsg_invreg(int name)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000919{
cero19881d87e112023-02-16 15:03:12 +0000920 semsg(_(e_invalid_register_name_str), transchar_buf(NULL, name));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000921}
922
Dominique Pelle748b3082022-01-08 12:41:16 +0000923#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100925 * Give an error message which contains %s for "name[len]".
926 */
927 void
928emsg_namelen(char *msg, char_u *name, int len)
929{
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +0000930 char_u *copy = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100931
932 semsg(msg, copy == NULL ? "NULL" : (char *)copy);
Bram Moolenaar292b90d2020-03-18 15:23:16 +0100933 vim_free(copy);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100934}
Dominique Pelle748b3082022-01-08 12:41:16 +0000935#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100936
937/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 * Like msg(), but truncate to a single line if p_shm contains 't', or when
939 * "force" is TRUE. This truncates in another way as for normal messages.
940 * Careful: The string may be changed by msg_may_trunc()!
941 * Returns a pointer to the printed message, if wait_return() not called.
942 */
Bram Moolenaar32526b32019-01-19 17:43:09 +0100943 char *
944msg_trunc_attr(char *s, int force, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945{
946 int n;
Bram Moolenaar32526b32019-01-19 17:43:09 +0100947 char *ts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948
Bram Moolenaar85a20022019-12-21 18:25:54 +0100949 // Add message to history before truncating
Bram Moolenaar32526b32019-01-19 17:43:09 +0100950 add_msg_hist((char_u *)s, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951
Bram Moolenaar32526b32019-01-19 17:43:09 +0100952 ts = (char *)msg_may_trunc(force, (char_u *)s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953
954 msg_hist_off = TRUE;
Bram Moolenaar32526b32019-01-19 17:43:09 +0100955 n = msg_attr(ts, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 msg_hist_off = FALSE;
957
958 if (n)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100959 return ts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 return NULL;
961}
962
963/*
964 * Check if message "s" should be truncated at the start (for filenames).
965 * Return a pointer to where the truncated message starts.
966 * Note: May change the message by replacing a character with '<'.
967 */
968 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100969msg_may_trunc(int force, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970{
971 int n;
972 int room;
973
Bram Moolenaar9198de32022-08-27 21:30:03 +0100974 // If 'cmdheight' is zero or something unexpected happened "room" may be
975 // negative.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 room = (int)(Rows - cmdline_row - 1) * Columns + sc_col - 1;
Bram Moolenaar0fbc9262022-06-26 11:17:10 +0100977 if (room > 0 && (force || (shortmess(SHM_TRUNC) && !exmode_active))
Bram Moolenaara2a89732022-08-31 14:46:18 +0100978 && (n = (int)STRLEN(s) - room) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980 if (has_mbyte)
981 {
982 int size = vim_strsize(s);
983
Bram Moolenaar85a20022019-12-21 18:25:54 +0100984 // There may be room anyway when there are multibyte chars.
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +0000985 if (size <= room)
986 return s;
987
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 for (n = 0; size >= room; )
989 {
990 size -= (*mb_ptr2cells)(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000991 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 }
993 --n;
994 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 s += n;
996 *s = '<';
997 }
998 return s;
999}
1000
1001 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001002add_msg_hist(
1003 char_u *s,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001004 int len, // -1 for undetermined length
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001005 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006{
1007 struct msg_hist *p;
1008
1009 if (msg_hist_off || msg_silent != 0)
1010 return;
1011
Bram Moolenaar85a20022019-12-21 18:25:54 +01001012 // Don't let the message history get too big
Bram Moolenaar4770d092006-01-12 23:22:24 +00001013 while (msg_hist_len > MAX_MSG_HIST_LEN)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00001014 (void)delete_first_msg();
1015
Bram Moolenaar85a20022019-12-21 18:25:54 +01001016 // allocate an entry and add the message at the end of the history
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001017 p = ALLOC_ONE(struct msg_hist);
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001018 if (p == NULL)
1019 return;
1020
1021 if (len < 0)
1022 len = (int)STRLEN(s);
1023 // remove leading and trailing newlines
1024 while (len > 0 && *s == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 {
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001026 ++s;
1027 --len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 }
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001029 while (len > 0 && s[len - 1] == '\n')
1030 --len;
1031 p->msg = vim_strnsave(s, len);
1032 p->next = NULL;
1033 p->attr = attr;
1034 if (last_msg_hist != NULL)
1035 last_msg_hist->next = p;
1036 last_msg_hist = p;
1037 if (first_msg_hist == NULL)
1038 first_msg_hist = last_msg_hist;
1039 ++msg_hist_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040}
1041
1042/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00001043 * Delete the first (oldest) message from the history.
1044 * Returns FAIL if there are no messages.
1045 */
1046 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001047delete_first_msg(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00001048{
1049 struct msg_hist *p;
1050
1051 if (msg_hist_len <= 0)
1052 return FAIL;
1053 p = first_msg_hist;
1054 first_msg_hist = p->next;
Bram Moolenaara7241f52008-06-24 20:39:31 +00001055 if (first_msg_hist == NULL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001056 last_msg_hist = NULL; // history is empty
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00001057 vim_free(p->msg);
1058 vim_free(p);
1059 --msg_hist_len;
1060 return OK;
1061}
1062
1063/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 * ":messages" command.
1065 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 void
Bram Moolenaar52196b22016-04-14 17:52:41 +02001067ex_messages(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068{
1069 struct msg_hist *p;
1070 char_u *s;
Bram Moolenaar451f8492016-04-14 17:16:22 +02001071 int c = 0;
1072
1073 if (STRCMP(eap->arg, "clear") == 0)
1074 {
1075 int keep = eap->addr_count == 0 ? 0 : eap->line2;
1076
1077 while (msg_hist_len > keep)
1078 (void)delete_first_msg();
1079 return;
1080 }
1081
1082 if (*eap->arg != NUL)
1083 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001084 emsg(_(e_invalid_argument));
Bram Moolenaar451f8492016-04-14 17:16:22 +02001085 return;
1086 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087
1088 msg_hist_off = TRUE;
1089
Bram Moolenaar451f8492016-04-14 17:16:22 +02001090 p = first_msg_hist;
Bram Moolenaar451f8492016-04-14 17:16:22 +02001091 if (eap->addr_count != 0)
1092 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001093 // Count total messages
Bram Moolenaar451f8492016-04-14 17:16:22 +02001094 for (; p != NULL && !got_int; p = p->next)
1095 c++;
1096
1097 c -= eap->line2;
1098
Bram Moolenaar85a20022019-12-21 18:25:54 +01001099 // Skip without number of messages specified
Bram Moolenaar451f8492016-04-14 17:16:22 +02001100 for (p = first_msg_hist; p != NULL && !got_int && c > 0;
1101 p = p->next, c--);
1102 }
1103
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001104 if (p == first_msg_hist)
1105 {
Bram Moolenaar41f69182020-04-25 15:45:37 +02001106#ifdef FEAT_MULTI_LANG
1107 s = get_mess_lang();
1108#else
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001109 s = mch_getenv((char_u *)"LANG");
Bram Moolenaar41f69182020-04-25 15:45:37 +02001110#endif
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001111 if (s != NULL && *s != NUL)
Bram Moolenaar0c183192018-06-28 14:54:43 +02001112 // The next comment is extracted by xgettext and put in po file for
1113 // translators to read.
Bram Moolenaar32526b32019-01-19 17:43:09 +01001114 msg_attr(
Bram Moolenaar0c183192018-06-28 14:54:43 +02001115 // Translator: Please replace the name and email address
1116 // with the appropriate text for your translation.
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001117 _("Messages maintainer: Bram Moolenaar <Bram@vim.org>"),
Bram Moolenaar8820b482017-03-16 17:23:31 +01001118 HL_ATTR(HLF_T));
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001119 }
1120
Bram Moolenaar85a20022019-12-21 18:25:54 +01001121 // Display what was not skipped.
Bram Moolenaar451f8492016-04-14 17:16:22 +02001122 for (; p != NULL && !got_int; p = p->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 if (p->msg != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001124 msg_attr((char *)p->msg, p->attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125
1126 msg_hist_off = FALSE;
1127}
1128
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001129#if defined(FEAT_CON_DIALOG) || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130/*
1131 * Call this after prompting the user. This will avoid a hit-return message
1132 * and a delay.
1133 */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001134 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001135msg_end_prompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136{
1137 need_wait_return = FALSE;
1138 emsg_on_display = FALSE;
1139 cmdline_row = msg_row;
1140 msg_col = 0;
1141 msg_clr_eos();
Bram Moolenaar5d6f75e2011-12-30 14:14:29 +01001142 lines_left = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143}
1144#endif
1145
1146/*
Bram Moolenaar32526b32019-01-19 17:43:09 +01001147 * Wait for the user to hit a key (normally Enter).
1148 * If "redraw" is TRUE, clear and redraw the screen.
1149 * If "redraw" is FALSE, just redraw the screen.
1150 * If "redraw" is -1, don't redraw at all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 */
1152 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001153wait_return(int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154{
1155 int c;
1156 int oldState;
1157 int tmpState;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 int had_got_int;
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001159 int save_reg_recording;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001160 FILE *save_scriptout;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161
1162 if (redraw == TRUE)
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01001163 set_must_redraw(UPD_CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164
Bram Moolenaar85a20022019-12-21 18:25:54 +01001165 // If using ":silent cmd", don't wait for a return. Also don't set
1166 // need_wait_return to do it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 if (msg_silent != 0)
1168 return;
Bram Moolenaarcf0995d2022-09-11 21:36:17 +01001169#ifdef HAS_MESSAGE_WINDOW
1170 if (in_echowindow)
1171 return;
1172#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01001174 /*
1175 * When inside vgetc(), we can't wait for a typed character at all.
1176 * With the global command (and some others) we only need one return at
1177 * the end. Adjust cmdline_row to avoid the next message overwriting the
1178 * last one.
1179 */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00001180 if (vgetc_busy > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 return;
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01001182 need_wait_return = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 if (no_wait_return)
1184 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 if (!exmode_active)
1186 cmdline_row = msg_row;
1187 return;
1188 }
1189
Bram Moolenaar85a20022019-12-21 18:25:54 +01001190 redir_off = TRUE; // don't redirect this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 oldState = State;
1192 if (quit_more)
1193 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001194 c = CAR; // just pretend CR was hit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 quit_more = FALSE;
1196 got_int = FALSE;
1197 }
1198 else if (exmode_active)
1199 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001200 msg_puts(" "); // make sure the cursor is on the right line
1201 c = CAR; // no need for a return in ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 got_int = FALSE;
1203 }
1204 else
1205 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001206 // Make sure the hit-return prompt is on screen when 'guioptions' was
1207 // just changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 screenalloc(FALSE);
1209
Bram Moolenaar24959102022-05-07 20:01:16 +01001210 State = MODE_HITRETURN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar85a20022019-12-21 18:25:54 +01001213 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214#endif
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01001215 cmdline_row = msg_row;
1216
Bram Moolenaar85a20022019-12-21 18:25:54 +01001217 // Avoid the sequence that the user types ":" at the hit-return prompt
1218 // to start an Ex command, but the file-changed dialog gets in the
1219 // way.
Bram Moolenaarec38d692013-04-24 15:12:32 +02001220 if (need_check_timestamps)
1221 check_timestamps(FALSE);
1222
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 hit_return_msg();
1224
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 do
1226 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001227 // Remember "got_int", if it is set vgetc() probably returns a
1228 // CTRL-C, but we need to loop then.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 had_got_int = got_int;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001230
Bram Moolenaar85a20022019-12-21 18:25:54 +01001231 // Don't do mappings here, we put the character back in the
1232 // typeahead buffer.
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001233 ++no_mapping;
1234 ++allow_keys;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001235
Bram Moolenaar85a20022019-12-21 18:25:54 +01001236 // Temporarily disable Recording. If Recording is active, the
1237 // character will be recorded later, since it will be added to the
1238 // typebuf after the loop
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001239 save_reg_recording = reg_recording;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001240 save_scriptout = scriptout;
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001241 reg_recording = 0;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001242 scriptout = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 c = safe_vgetc();
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00001244 if (had_got_int && !global_busy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 got_int = FALSE;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001246 --no_mapping;
1247 --allow_keys;
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001248 reg_recording = save_reg_recording;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001249 scriptout = save_scriptout;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001250
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251#ifdef FEAT_CLIPBOARD
Bram Moolenaar85a20022019-12-21 18:25:54 +01001252 // Strange way to allow copying (yanking) a modeless selection at
1253 // the hit-enter prompt. Use CTRL-Y, because the same is used in
1254 // Cmdline-mode and it's harmless when there is no selection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 if (c == Ctrl_Y && clip_star.state == SELECT_DONE)
1256 {
1257 clip_copy_modeless_selection(TRUE);
1258 c = K_IGNORE;
1259 }
1260#endif
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00001261
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001262 /*
1263 * Allow scrolling back in the messages.
1264 * Also accept scroll-down commands when messages fill the screen,
1265 * to avoid that typing one 'j' too many makes the messages
1266 * disappear.
1267 */
1268 if (p_more && !p_cp)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001269 {
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001270 if (c == 'b' || c == 'k' || c == 'u' || c == 'g'
1271 || c == K_UP || c == K_PAGEUP)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001272 {
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001273 if (msg_scrolled > Rows)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001274 // scroll back to show older messages
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001275 do_more_prompt(c);
1276 else
1277 {
1278 msg_didout = FALSE;
1279 c = K_IGNORE;
1280 msg_col =
1281#ifdef FEAT_RIGHTLEFT
1282 cmdmsg_rl ? Columns - 1 :
1283#endif
1284 0;
1285 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001286 if (quit_more)
1287 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001288 c = CAR; // just pretend CR was hit
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001289 quit_more = FALSE;
1290 got_int = FALSE;
1291 }
Bram Moolenaarb0912962013-08-09 20:38:26 +02001292 else if (c != K_IGNORE)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001293 {
1294 c = K_IGNORE;
1295 hit_return_msg();
1296 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001297 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00001298 else if (msg_scrolled > Rows - 2
Bram Moolenaarb59494c2013-03-19 13:56:08 +01001299 && (c == 'j' || c == 'd' || c == 'f'
1300 || c == K_DOWN || c == K_PAGEDOWN))
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001301 c = K_IGNORE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001302 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 } while ((had_got_int && c == Ctrl_C)
1304 || c == K_IGNORE
1305#ifdef FEAT_GUI
1306 || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR
1307#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 || c == K_LEFTDRAG || c == K_LEFTRELEASE
1309 || c == K_MIDDLEDRAG || c == K_MIDDLERELEASE
1310 || c == K_RIGHTDRAG || c == K_RIGHTRELEASE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001311 || c == K_MOUSELEFT || c == K_MOUSERIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 || c == K_MOUSEDOWN || c == K_MOUSEUP
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001313 || c == K_MOUSEMOVE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 || (!mouse_has(MOUSE_RETURN)
1315 && mouse_row < msg_row
1316 && (c == K_LEFTMOUSE
1317 || c == K_MIDDLEMOUSE
1318 || c == K_RIGHTMOUSE
1319 || c == K_X1MOUSE
1320 || c == K_X2MOUSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 );
1322 ui_breakcheck();
Bram Moolenaar88456cd2022-11-18 22:14:09 +00001323
1324 // Avoid that the mouse-up event causes Visual mode to start.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 if (c == K_LEFTMOUSE || c == K_MIDDLEMOUSE || c == K_RIGHTMOUSE
1326 || c == K_X1MOUSE || c == K_X2MOUSE)
1327 (void)jump_to_mouse(MOUSE_SETPOS, NULL, 0);
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001328 else if (vim_strchr((char_u *)"\r\n ", c) == NULL && c != Ctrl_C)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001330 // Put the character back in the typeahead buffer. Don't use the
1331 // stuff buffer, because lmaps wouldn't work.
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001332 ins_char_typebuf(vgetc_char, vgetc_mod_mask);
Bram Moolenaar85a20022019-12-21 18:25:54 +01001333 do_redraw = TRUE; // need a redraw even though there is
1334 // typeahead
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 }
1337 redir_off = FALSE;
1338
1339 /*
1340 * If the user hits ':', '?' or '/' we get a command line from the next
1341 * line.
1342 */
1343 if (c == ':' || c == '?' || c == '/')
1344 {
1345 if (!exmode_active)
1346 cmdline_row = msg_row;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001347 skip_redraw = TRUE; // skip redraw once
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 do_redraw = FALSE;
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02001349#ifdef FEAT_TERMINAL
1350 skip_term_loop = TRUE;
1351#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 }
1353
1354 /*
1355 * If the window size changed set_shellsize() will redraw the screen.
1356 * Otherwise the screen is only redrawn if 'redraw' is set and no ':'
1357 * typed.
1358 */
1359 tmpState = State;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001360 State = oldState; // restore State before set_shellsize
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 msg_check();
1363
1364#if defined(UNIX) || defined(VMS)
1365 /*
1366 * When switching screens, we need to output an extra newline on exit.
1367 */
1368 if (swapping_screen() && !termcap_active)
1369 newline_on_exit = TRUE;
1370#endif
1371
1372 need_wait_return = FALSE;
1373 did_wait_return = TRUE;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001374 emsg_on_display = FALSE; // can delete error message now
1375 lines_left = -1; // reset lines_left at next msg_start()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 reset_last_sourcing();
1377 if (keep_msg != NULL && vim_strsize(keep_msg) >=
1378 (Rows - cmdline_row - 1) * Columns + sc_col)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001379 VIM_CLEAR(keep_msg); // don't redisplay message, it's too long
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380
Bram Moolenaar24959102022-05-07 20:01:16 +01001381 if (tmpState == MODE_SETWSIZE) // got resize event while in vgetc()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001383 starttermcap(); // start termcap before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 shell_resized();
1385 }
1386 else if (!skip_redraw
1387 && (redraw == TRUE || (msg_scrolled != 0 && redraw != -1)))
1388 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001389 starttermcap(); // start termcap before redrawing
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001390 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 }
1392}
1393
1394/*
1395 * Write the hit-return prompt.
1396 */
1397 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001398hit_return_msg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001400 int save_p_more = p_more;
1401
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001402 p_more = FALSE; // don't want to see this message when scrolling back
Bram Moolenaar85a20022019-12-21 18:25:54 +01001403 if (msg_didout) // start on a new line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 msg_putchar('\n');
1405 if (got_int)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001406 msg_puts(_("Interrupt: "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407
Bram Moolenaar32526b32019-01-19 17:43:09 +01001408 msg_puts_attr(_("Press ENTER or type command to continue"), HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 if (!msg_use_printf())
1410 msg_clr_eos();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001411 p_more = save_p_more;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412}
1413
1414/*
1415 * Set "keep_msg" to "s". Free the old value and check for NULL pointer.
1416 */
1417 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001418set_keep_msg(char_u *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419{
1420 vim_free(keep_msg);
1421 if (s != NULL && msg_silent == 0)
1422 keep_msg = vim_strsave(s);
1423 else
1424 keep_msg = NULL;
Bram Moolenaaraab21c32005-01-25 21:46:35 +00001425 keep_msg_more = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001426 keep_msg_attr = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427}
1428
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001429/*
1430 * If there currently is a message being displayed, set "keep_msg" to it, so
1431 * that it will be displayed again after redraw.
1432 */
1433 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001434set_keep_msg_from_hist(void)
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001435{
1436 if (keep_msg == NULL && last_msg_hist != NULL && msg_scrolled == 0
Bram Moolenaar24959102022-05-07 20:01:16 +01001437 && (State & MODE_NORMAL))
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001438 set_keep_msg(last_msg_hist->msg, last_msg_hist->attr);
1439}
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001440
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441/*
1442 * Prepare for outputting characters in the command line.
1443 */
1444 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001445msg_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446{
1447 int did_return = FALSE;
1448
Bram Moolenaare4ce65d2010-08-04 20:12:32 +02001449 if (!msg_silent)
Rob Pilling726f7f92022-01-20 14:44:38 +00001450 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001451 VIM_CLEAR(keep_msg);
Rob Pilling726f7f92022-01-20 14:44:38 +00001452 need_fileinfo = FALSE;
1453 }
Bram Moolenaara7241f52008-06-24 20:39:31 +00001454
1455#ifdef FEAT_EVAL
Bram Moolenaar3b474dc2022-09-01 17:01:32 +01001456 if (need_clr_eos)
Bram Moolenaara7241f52008-06-24 20:39:31 +00001457 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001458 // Halfway an ":echo" command and getting an (error) message: clear
1459 // any text from the command.
Bram Moolenaara7241f52008-06-24 20:39:31 +00001460 need_clr_eos = FALSE;
1461 msg_clr_eos();
1462 }
1463#endif
1464
Bram Moolenaard54af2e2022-08-27 22:05:13 +01001465#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaara2a89732022-08-31 14:46:18 +01001466 if (in_echowindow)
Bram Moolenaar9198de32022-08-27 21:30:03 +01001467 {
Bram Moolenaar37fef162022-08-29 18:16:32 +01001468 if (popup_message_win_visible()
1469 && ((msg_col > 0 && (msg_scroll || !full_screen))
1470 || in_echowindow))
Bram Moolenaar9198de32022-08-27 21:30:03 +01001471 {
1472 win_T *wp = popup_get_message_win();
1473
Bram Moolenaarf2fb54f2022-08-28 20:58:51 +01001474 // start a new line
Bram Moolenaar9198de32022-08-27 21:30:03 +01001475 curbuf = wp->w_buffer;
1476 ml_append(wp->w_buffer->b_ml.ml_line_count,
1477 (char_u *)"", (colnr_T)0, FALSE);
1478 curbuf = curwin->w_buffer;
1479 }
1480 msg_col = 0;
1481 }
Bram Moolenaard54af2e2022-08-27 22:05:13 +01001482 else
1483#endif
1484 if (!msg_scroll && full_screen) // overwrite last message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485 {
1486 msg_row = cmdline_row;
1487 msg_col =
1488#ifdef FEAT_RIGHTLEFT
1489 cmdmsg_rl ? Columns - 1 :
1490#endif
1491 0;
1492 }
Bram Moolenaara2a89732022-08-31 14:46:18 +01001493 else if (msg_didout || in_echowindow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494 {
Bram Moolenaar309c4e02022-08-29 12:23:39 +01001495 // start message on next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 msg_putchar('\n');
1497 did_return = TRUE;
1498 if (exmode_active != EXMODE_NORMAL)
1499 cmdline_row = msg_row;
1500 }
1501 if (!msg_didany || lines_left < 0)
1502 msg_starthere();
1503 if (msg_silent == 0)
1504 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001505 msg_didout = FALSE; // no output on current line yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 cursor_off();
1507 }
1508
Bram Moolenaar85a20022019-12-21 18:25:54 +01001509 // when redirecting, may need to start a new line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 if (!did_return)
1511 redir_write((char_u *)"\n", -1);
1512}
1513
1514/*
1515 * Note that the current msg position is where messages start.
1516 */
1517 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001518msg_starthere(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519{
1520 lines_left = cmdline_row;
1521 msg_didany = FALSE;
1522}
1523
1524 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001525msg_putchar(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526{
1527 msg_putchar_attr(c, 0);
1528}
1529
1530 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001531msg_putchar_attr(int c, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532{
Bram Moolenaar63c0ccd2019-01-19 21:06:58 +01001533 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534
1535 if (IS_SPECIAL(c))
1536 {
1537 buf[0] = K_SPECIAL;
1538 buf[1] = K_SECOND(c);
1539 buf[2] = K_THIRD(c);
1540 buf[3] = NUL;
1541 }
1542 else
Bram Moolenaar63c0ccd2019-01-19 21:06:58 +01001543 buf[(*mb_char2bytes)(c, buf)] = NUL;
Bram Moolenaar63c0ccd2019-01-19 21:06:58 +01001544 msg_puts_attr((char *)buf, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545}
1546
1547 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001548msg_outnum(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001550 char buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551
Bram Moolenaar32526b32019-01-19 17:43:09 +01001552 sprintf(buf, "%ld", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 msg_puts(buf);
1554}
1555
1556 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001557msg_home_replace(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558{
1559 msg_home_replace_attr(fname, 0);
1560}
1561
1562#if defined(FEAT_FIND_ID) || defined(PROTO)
1563 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001564msg_home_replace_hl(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565{
Bram Moolenaar8820b482017-03-16 17:23:31 +01001566 msg_home_replace_attr(fname, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567}
1568#endif
1569
1570 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001571msg_home_replace_attr(char_u *fname, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572{
1573 char_u *name;
1574
1575 name = home_replace_save(NULL, fname);
1576 if (name != NULL)
1577 msg_outtrans_attr(name, attr);
1578 vim_free(name);
1579}
1580
1581/*
1582 * Output 'len' characters in 'str' (including NULs) with translation
Bram Moolenaarf48ee3c2019-12-06 22:18:20 +01001583 * if 'len' is -1, output up to a NUL character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 * Use attributes 'attr'.
1585 * Return the number of characters it takes on the screen.
1586 */
1587 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001588msg_outtrans(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589{
1590 return msg_outtrans_attr(str, 0);
1591}
1592
1593 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001594msg_outtrans_attr(char_u *str, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595{
1596 return msg_outtrans_len_attr(str, (int)STRLEN(str), attr);
1597}
1598
1599 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001600msg_outtrans_len(char_u *str, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601{
1602 return msg_outtrans_len_attr(str, len, 0);
1603}
1604
1605/*
1606 * Output one character at "p". Return pointer to the next character.
1607 * Handles multi-byte characters.
1608 */
1609 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001610msg_outtrans_one(char_u *p, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 int l;
1613
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001614 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 {
1616 msg_outtrans_len_attr(p, l, attr);
1617 return p + l;
1618 }
cero19881d87e112023-02-16 15:03:12 +00001619 msg_puts_attr((char *)transchar_byte_buf(NULL, *p), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 return p + 1;
1621}
1622
1623 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001624msg_outtrans_len_attr(char_u *msgstr, int len, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625{
1626 int retval = 0;
1627 char_u *str = msgstr;
1628 char_u *plain_start = msgstr;
1629 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 int mb_l;
1631 int c;
Bram Moolenaar3d30af82020-10-13 22:15:56 +02001632 int save_got_int = got_int;
1633
1634 // Only quit when got_int was set in here.
1635 got_int = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636
Bram Moolenaar85a20022019-12-21 18:25:54 +01001637 // if MSG_HIST flag set, add message to history
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 if (attr & MSG_HIST)
1639 {
1640 add_msg_hist(str, len, attr);
1641 attr &= ~MSG_HIST;
1642 }
1643
Bram Moolenaar85a20022019-12-21 18:25:54 +01001644 // If the string starts with a composing character first draw a space on
1645 // which the composing char can be drawn.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 if (enc_utf8 && utf_iscomposing(utf_ptr2char(msgstr)))
Bram Moolenaar32526b32019-01-19 17:43:09 +01001647 msg_puts_attr(" ", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648
1649 /*
1650 * Go over the string. Special characters are translated and printed.
1651 * Normal characters are printed several at a time.
1652 */
Bram Moolenaar3d30af82020-10-13 22:15:56 +02001653 while (--len >= 0 && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655 if (enc_utf8)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001656 // Don't include composing chars after the end.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001657 mb_l = utfc_ptr2len_len(str, len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 else if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001659 mb_l = (*mb_ptr2len)(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 else
1661 mb_l = 1;
1662 if (has_mbyte && mb_l > 1)
1663 {
1664 c = (*mb_ptr2char)(str);
1665 if (vim_isprintc(c))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001666 // printable multi-byte char: count the cells.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 retval += (*mb_ptr2cells)(str);
1668 else
1669 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001670 // unprintable multi-byte char: print the printable chars so
1671 // far and the translation of the unprintable char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672 if (str > plain_start)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001673 msg_puts_attr_len((char *)plain_start,
1674 (int)(str - plain_start), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 plain_start = str + mb_l;
cero19881d87e112023-02-16 15:03:12 +00001676 msg_puts_attr((char *)transchar_buf(NULL, c),
Bram Moolenaar32526b32019-01-19 17:43:09 +01001677 attr == 0 ? HL_ATTR(HLF_8) : attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 retval += char2cells(c);
1679 }
1680 len -= mb_l - 1;
1681 str += mb_l;
1682 }
1683 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 {
cero19881d87e112023-02-16 15:03:12 +00001685 s = transchar_byte_buf(NULL, *str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 if (s[1] != NUL)
1687 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001688 // unprintable char: print the printable chars so far and the
1689 // translation of the unprintable char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 if (str > plain_start)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001691 msg_puts_attr_len((char *)plain_start,
1692 (int)(str - plain_start), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 plain_start = str + 1;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001694 msg_puts_attr((char *)s, attr == 0 ? HL_ATTR(HLF_8) : attr);
Bram Moolenaarc236c162008-07-13 17:41:49 +00001695 retval += (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696 }
Bram Moolenaarb3c722a2008-07-06 17:16:02 +00001697 else
1698 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699 ++str;
1700 }
1701 }
1702
Bram Moolenaar3d30af82020-10-13 22:15:56 +02001703 if (str > plain_start && !got_int)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001704 // print the printable chars at the end
Bram Moolenaar32526b32019-01-19 17:43:09 +01001705 msg_puts_attr_len((char *)plain_start, (int)(str - plain_start), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706
Bram Moolenaar3d30af82020-10-13 22:15:56 +02001707 got_int |= save_got_int;
1708
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709 return retval;
1710}
1711
1712#if defined(FEAT_QUICKFIX) || defined(PROTO)
1713 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001714msg_make(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715{
1716 int i;
1717 static char_u *str = (char_u *)"eeffoc", *rs = (char_u *)"Plon#dqg#vxjduB";
1718
1719 arg = skipwhite(arg);
1720 for (i = 5; *arg && i >= 0; --i)
1721 if (*arg++ != str[i])
1722 break;
1723 if (i < 0)
1724 {
1725 msg_putchar('\n');
1726 for (i = 0; rs[i]; ++i)
1727 msg_putchar(rs[i] - 3);
1728 }
1729}
1730#endif
1731
1732/*
Bram Moolenaarf48ee3c2019-12-06 22:18:20 +01001733 * Output the string 'str' up to a NUL character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 * Return the number of characters it takes on the screen.
1735 *
1736 * If K_SPECIAL is encountered, then it is taken in conjunction with the
1737 * following character and shown as <F1>, <S-Up> etc. Any other character
1738 * which is not printable shown in <> form.
1739 * If 'from' is TRUE (lhs of a mapping), a space is shown as <Space>.
1740 * If a character is displayed in one of these special ways, is also
1741 * highlighted (its highlight name is '8' in the p_hl variable).
1742 * Otherwise characters are not highlighted.
1743 * This function is used to show mappings, where we want to see how to type
1744 * the character/string -- webb
1745 */
1746 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001747msg_outtrans_special(
1748 char_u *strstart,
Bram Moolenaar725310d2019-04-24 23:08:23 +02001749 int from, // TRUE for lhs of a mapping
Bram Moolenaar32aa1022019-11-02 22:54:41 +01001750 int maxlen) // screen columns, 0 for unlimited
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751{
1752 char_u *str = strstart;
1753 int retval = 0;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001754 char *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 int attr;
1756 int len;
1757
Bram Moolenaar8820b482017-03-16 17:23:31 +01001758 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 while (*str != NUL)
1760 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001761 // Leading and trailing spaces need to be displayed in <> form.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 if ((str == strstart || str[1] == NUL) && *str == ' ')
1763 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001764 text = "<Space>";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765 ++str;
1766 }
1767 else
zeertzjqcdc83932022-09-12 13:38:41 +01001768 text = (char *)str2special(&str, from, FALSE);
zeertzjq0519ce02022-05-09 12:16:19 +01001769 if (text[0] != NUL && text[1] == NUL)
1770 // single-byte character or illegal byte
cero19881d87e112023-02-16 15:03:12 +00001771 text = (char *)transchar_byte_buf(NULL, (char_u)text[0]);
Bram Moolenaar32526b32019-01-19 17:43:09 +01001772 len = vim_strsize((char_u *)text);
Bram Moolenaar725310d2019-04-24 23:08:23 +02001773 if (maxlen > 0 && retval + len >= maxlen)
1774 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001775 // Highlight special keys
Bram Moolenaar32526b32019-01-19 17:43:09 +01001776 msg_puts_attr(text, len > 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001777 && (*mb_ptr2len)((char_u *)text) <= 1 ? attr : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 retval += len;
1779 }
1780 return retval;
1781}
1782
Martin Tournoijba43e762022-10-13 22:12:15 +01001783#if defined(FEAT_EVAL) || defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaarbd743252010-10-20 21:23:33 +02001784/*
1785 * Return the lhs or rhs of a mapping, with the key codes turned into printable
1786 * strings, in an allocated string.
1787 */
1788 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001789str2special_save(
1790 char_u *str,
zeertzjqcdc83932022-09-12 13:38:41 +01001791 int replace_spaces, // TRUE to replace " " with "<Space>".
1792 // used for the lhs of mapping and keytrans().
1793 int replace_lt) // TRUE to replace "<" with "<lt>".
Bram Moolenaarbd743252010-10-20 21:23:33 +02001794{
1795 garray_T ga;
1796 char_u *p = str;
1797
1798 ga_init2(&ga, 1, 40);
1799 while (*p != NUL)
zeertzjqcdc83932022-09-12 13:38:41 +01001800 ga_concat(&ga, str2special(&p, replace_spaces, replace_lt));
Bram Moolenaarbd743252010-10-20 21:23:33 +02001801 ga_append(&ga, NUL);
1802 return (char_u *)ga.ga_data;
1803}
1804#endif
1805
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806/*
1807 * Return the printable string for the key codes at "*sp".
zeertzjq0519ce02022-05-09 12:16:19 +01001808 * On illegal byte return a string with only that byte.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 * Used for translating the lhs or rhs of a mapping to printable chars.
1810 * Advances "sp" to the next code.
1811 */
1812 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001813str2special(
1814 char_u **sp,
zeertzjqcdc83932022-09-12 13:38:41 +01001815 int replace_spaces, // TRUE to replace " " with "<Space>".
1816 // used for the lhs of mapping and keytrans().
1817 int replace_lt) // TRUE to replace "<" with "<lt>".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818{
1819 int c;
1820 static char_u buf[7];
1821 char_u *str = *sp;
1822 int modifiers = 0;
1823 int special = FALSE;
1824
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 if (has_mbyte)
1826 {
1827 char_u *p;
1828
Bram Moolenaar85a20022019-12-21 18:25:54 +01001829 // Try to un-escape a multi-byte character. Return the un-escaped
1830 // string if it is a multi-byte character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 p = mb_unescape(sp);
1832 if (p != NULL)
1833 return p;
1834 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835
1836 c = *str;
1837 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1838 {
1839 if (str[1] == KS_MODIFIER)
1840 {
1841 modifiers = str[2];
1842 str += 3;
1843 c = *str;
1844 }
1845 if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1846 {
1847 c = TO_SPECIAL(str[1], str[2]);
1848 str += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001850 if (IS_SPECIAL(c) || modifiers) // special key
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 special = TRUE;
1852 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853
zeertzjq0519ce02022-05-09 12:16:19 +01001854 if (has_mbyte && !IS_SPECIAL(c) && MB_BYTE2LEN(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 {
zeertzjqac402f42022-05-04 18:51:43 +01001856 char_u *p;
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001857
zeertzjqac402f42022-05-04 18:51:43 +01001858 *sp = str;
1859 // Try to un-escape a multi-byte character after modifiers.
1860 p = mb_unescape(sp);
zeertzjq0519ce02022-05-09 12:16:19 +01001861 if (p != NULL)
1862 // Since 'special' is TRUE the multi-byte character 'c' will be
1863 // processed by get_special_key_name()
1864 c = (*mb_ptr2char)(p);
1865 else
1866 // illegal byte
1867 *sp = str + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 }
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001869 else
Bram Moolenaar083692d2022-06-29 21:16:58 +01001870 // single-byte character, NUL or illegal byte
1871 *sp = str + (*str == NUL ? 0 : 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872
zeertzjq0519ce02022-05-09 12:16:19 +01001873 // Make special keys and C0 control characters in <> form, also <M-Space>.
zeertzjqcdc83932022-09-12 13:38:41 +01001874 if (special
1875 || c < ' '
1876 || (replace_spaces && c == ' ')
1877 || (replace_lt && c == '<'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 return get_special_key_name(c, modifiers);
1879 buf[0] = c;
1880 buf[1] = NUL;
1881 return buf;
1882}
1883
1884/*
1885 * Translate a key sequence into special key names.
1886 */
1887 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001888str2specialbuf(char_u *sp, char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889{
1890 char_u *s;
1891
1892 *buf = NUL;
1893 while (*sp)
1894 {
zeertzjqcdc83932022-09-12 13:38:41 +01001895 s = str2special(&sp, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 if ((int)(STRLEN(s) + STRLEN(buf)) < len)
1897 STRCAT(buf, s);
1898 }
1899}
1900
1901/*
1902 * print line for :print or :list command
1903 */
1904 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001905msg_prt_line(char_u *s, int list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906{
1907 int c;
1908 int col = 0;
1909 int n_extra = 0;
1910 int c_extra = 0;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001911 int c_final = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001912 char_u *p_extra = NULL; // init to make SASC shut up
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 int n;
Bram Moolenaar56da7972007-01-16 14:46:32 +00001914 int attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 char_u *trail = NULL;
Bram Moolenaar91478ae2021-02-03 15:58:13 +01001916 char_u *lead = NULL;
zeertzjqf14b8ba2021-09-10 16:58:30 +02001917 int in_multispace = FALSE;
1918 int multispace_pos = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 int l;
1920 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921
Bram Moolenaardf177f62005-02-22 08:39:57 +00001922 if (curwin->w_p_list)
1923 list = TRUE;
1924
Bram Moolenaar91478ae2021-02-03 15:58:13 +01001925 if (list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 {
Bram Moolenaar91478ae2021-02-03 15:58:13 +01001927 // find start of trailing whitespace
Bram Moolenaareed9d462021-02-15 20:38:25 +01001928 if (curwin->w_lcs_chars.trail)
Bram Moolenaar91478ae2021-02-03 15:58:13 +01001929 {
1930 trail = s + STRLEN(s);
1931 while (trail > s && VIM_ISWHITE(trail[-1]))
1932 --trail;
1933 }
1934 // find end of leading whitespace
Bram Moolenaarb8329db2022-07-06 13:31:28 +01001935 if (curwin->w_lcs_chars.lead
1936 || curwin->w_lcs_chars.leadmultispace != NULL)
Bram Moolenaar91478ae2021-02-03 15:58:13 +01001937 {
1938 lead = s;
1939 while (VIM_ISWHITE(lead[0]))
1940 lead++;
1941 // in a line full of spaces all of them are treated as trailing
1942 if (*lead == NUL)
1943 lead = NULL;
1944 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945 }
1946
Bram Moolenaar85a20022019-12-21 18:25:54 +01001947 // output a space for an empty line, otherwise the line will be
1948 // overwritten
Bram Moolenaareed9d462021-02-15 20:38:25 +01001949 if (*s == NUL && !(list && curwin->w_lcs_chars.eol != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 msg_putchar(' ');
1951
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001952 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 {
Bram Moolenaar56da7972007-01-16 14:46:32 +00001954 if (n_extra > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 {
1956 --n_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01001957 if (n_extra == 0 && c_final)
1958 c = c_final;
1959 else if (c_extra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 c = c_extra;
1961 else
1962 c = *p_extra++;
1963 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001964 else if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 {
1966 col += (*mb_ptr2cells)(s);
Bram Moolenaar1cbfc992020-12-02 12:37:37 +01001967 if (l >= MB_MAXBYTES)
1968 {
Bram Moolenaar29d2f452020-12-04 19:42:52 +01001969 STRCPY(buf, "?");
Bram Moolenaar1cbfc992020-12-02 12:37:37 +01001970 }
Bram Moolenaareed9d462021-02-15 20:38:25 +01001971 else if (curwin->w_lcs_chars.nbsp != NUL && list
Bram Moolenaar73284b92015-05-04 17:28:22 +02001972 && (mb_ptr2char(s) == 160
1973 || mb_ptr2char(s) == 0x202f))
Bram Moolenaaracf17282011-02-01 17:12:25 +01001974 {
Bram Moolenaar74ac29c2022-06-15 12:12:44 +01001975 int len = mb_char2bytes(curwin->w_lcs_chars.nbsp, buf);
1976
1977 buf[len] = NUL;
Bram Moolenaaracf17282011-02-01 17:12:25 +01001978 }
1979 else
1980 {
1981 mch_memmove(buf, s, (size_t)l);
1982 buf[l] = NUL;
1983 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01001984 msg_puts((char *)buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 s += l;
1986 continue;
1987 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 else
1989 {
1990 attr = 0;
1991 c = *s++;
zeertzjqf14b8ba2021-09-10 16:58:30 +02001992 in_multispace = c == ' '
1993 && ((col > 0 && s[-2] == ' ') || *s == ' ');
1994 if (!in_multispace)
1995 multispace_pos = 0;
Bram Moolenaareed9d462021-02-15 20:38:25 +01001996 if (c == TAB && (!list || curwin->w_lcs_chars.tab1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001998 // tab amount depends on current column
Bram Moolenaar04958cb2018-06-23 19:23:02 +02001999#ifdef FEAT_VARTABS
2000 n_extra = tabstop_padding(col, curbuf->b_p_ts,
2001 curbuf->b_p_vts_array) - 1;
2002#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 n_extra = curbuf->b_p_ts - col % curbuf->b_p_ts - 1;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002004#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00002005 if (!list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 {
2007 c = ' ';
2008 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01002009 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 }
2011 else
2012 {
Bram Moolenaareed9d462021-02-15 20:38:25 +01002013 c = (n_extra == 0 && curwin->w_lcs_chars.tab3)
2014 ? curwin->w_lcs_chars.tab3
2015 : curwin->w_lcs_chars.tab1;
2016 c_extra = curwin->w_lcs_chars.tab2;
2017 c_final = curwin->w_lcs_chars.tab3;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002018 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 }
2020 }
Bram Moolenaareed9d462021-02-15 20:38:25 +01002021 else if (c == 160 && list && curwin->w_lcs_chars.nbsp != NUL)
Bram Moolenaaracf17282011-02-01 17:12:25 +01002022 {
Bram Moolenaareed9d462021-02-15 20:38:25 +01002023 c = curwin->w_lcs_chars.nbsp;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002024 attr = HL_ATTR(HLF_8);
Bram Moolenaaracf17282011-02-01 17:12:25 +01002025 }
Bram Moolenaareed9d462021-02-15 20:38:25 +01002026 else if (c == NUL && list && curwin->w_lcs_chars.eol != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 {
2028 p_extra = (char_u *)"";
2029 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01002030 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 n_extra = 1;
Bram Moolenaareed9d462021-02-15 20:38:25 +01002032 c = curwin->w_lcs_chars.eol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002033 attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 --s;
2035 }
2036 else if (c != NUL && (n = byte2cells(c)) > 1)
2037 {
2038 n_extra = n - 1;
cero19881d87e112023-02-16 15:03:12 +00002039 p_extra = transchar_byte_buf(NULL, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01002041 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 c = *p_extra++;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002043 // Use special coloring to be able to distinguish <hex> from
2044 // the same in plain text.
Bram Moolenaar8820b482017-03-16 17:23:31 +01002045 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 }
zeertzjqf14b8ba2021-09-10 16:58:30 +02002047 else if (c == ' ')
Bram Moolenaar91478ae2021-02-03 15:58:13 +01002048 {
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01002049 if (list && lead != NULL && s <= lead && in_multispace
2050 && curwin->w_lcs_chars.leadmultispace != NULL)
2051 {
2052 c = curwin->w_lcs_chars.leadmultispace[multispace_pos++];
zeertzjqb5f08012022-06-09 13:55:28 +01002053 if (curwin->w_lcs_chars.leadmultispace[multispace_pos]
2054 == NUL)
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01002055 multispace_pos = 0;
2056 attr = HL_ATTR(HLF_8);
2057 }
zeertzjqb5f08012022-06-09 13:55:28 +01002058 else if (lead != NULL && s <= lead
2059 && curwin->w_lcs_chars.lead != NUL)
zeertzjqf14b8ba2021-09-10 16:58:30 +02002060 {
2061 c = curwin->w_lcs_chars.lead;
2062 attr = HL_ATTR(HLF_8);
2063 }
2064 else if (trail != NULL && s > trail)
2065 {
2066 c = curwin->w_lcs_chars.trail;
2067 attr = HL_ATTR(HLF_8);
2068 }
2069 else if (list && in_multispace
2070 && curwin->w_lcs_chars.multispace != NULL)
2071 {
2072 c = curwin->w_lcs_chars.multispace[multispace_pos++];
2073 if (curwin->w_lcs_chars.multispace[multispace_pos] == NUL)
2074 multispace_pos = 0;
2075 attr = HL_ATTR(HLF_8);
2076 }
2077 else if (list && curwin->w_lcs_chars.space != NUL)
2078 {
2079 c = curwin->w_lcs_chars.space;
2080 attr = HL_ATTR(HLF_8);
2081 }
Bram Moolenaar1510f992015-04-22 22:18:22 +02002082 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 }
2084
2085 if (c == NUL)
2086 break;
2087
2088 msg_putchar_attr(c, attr);
2089 col++;
2090 }
2091 msg_clr_eos();
2092}
2093
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094/*
2095 * Use screen_puts() to output one multi-byte character.
2096 * Return the pointer "s" advanced to the next character.
2097 */
2098 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002099screen_puts_mbyte(char_u *s, int l, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100{
2101 int cw;
2102
Bram Moolenaar85a20022019-12-21 18:25:54 +01002103 msg_didout = TRUE; // remember that line is not empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 cw = (*mb_ptr2cells)(s);
2105 if (cw > 1 && (
2106#ifdef FEAT_RIGHTLEFT
2107 cmdmsg_rl ? msg_col <= 1 :
2108#endif
2109 msg_col == Columns - 1))
2110 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002111 // Doesn't fit, print a highlighted '>' to fill it up.
Bram Moolenaar8820b482017-03-16 17:23:31 +01002112 msg_screen_putchar('>', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 return s;
2114 }
2115
2116 screen_puts_len(s, l, msg_row, msg_col, attr);
2117#ifdef FEAT_RIGHTLEFT
2118 if (cmdmsg_rl)
2119 {
2120 msg_col -= cw;
2121 if (msg_col == 0)
2122 {
2123 msg_col = Columns;
2124 ++msg_row;
2125 }
2126 }
2127 else
2128#endif
2129 {
2130 msg_col += cw;
2131 if (msg_col >= Columns)
2132 {
2133 msg_col = 0;
2134 ++msg_row;
2135 }
2136 }
2137 return s + l;
2138}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139
2140/*
2141 * Output a string to the screen at position msg_row, msg_col.
2142 * Update msg_row and msg_col for the next message.
2143 */
2144 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002145msg_puts(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146{
Bram Moolenaaraeac9002016-09-06 22:15:08 +02002147 msg_puts_attr(s, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148}
2149
2150 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002151msg_puts_title(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152{
Bram Moolenaar8820b482017-03-16 17:23:31 +01002153 msg_puts_attr(s, HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154}
2155
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156/*
2157 * Show a message in such a way that it always fits in the line. Cut out a
2158 * part in the middle and replace it with "..." when necessary.
2159 * Does not handle multi-byte characters!
2160 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002161 static void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002162msg_outtrans_long_len_attr(char_u *longstr, int len, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163{
2164 int slen = len;
2165 int room;
2166
2167 room = Columns - msg_col;
2168 if (len > room && room >= 20)
2169 {
2170 slen = (room - 3) / 2;
2171 msg_outtrans_len_attr(longstr, slen, attr);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002172 msg_puts_attr("...", HL_ATTR(HLF_8));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 }
2174 msg_outtrans_len_attr(longstr + len - slen, slen, attr);
2175}
2176
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002177 void
2178msg_outtrans_long_attr(char_u *longstr, int attr)
2179{
2180 msg_outtrans_long_len_attr(longstr, (int)STRLEN(longstr), attr);
2181}
2182
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183/*
2184 * Basic function for writing a message with highlight attributes.
2185 */
2186 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002187msg_puts_attr(char *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188{
2189 msg_puts_attr_len(s, -1, attr);
2190}
2191
2192/*
2193 * Like msg_puts_attr(), but with a maximum length "maxlen" (in bytes).
2194 * When "maxlen" is -1 there is no maximum length.
2195 * When "maxlen" is >= 0 the message is not put in the history.
2196 */
2197 static void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002198msg_puts_attr_len(char *str, int maxlen, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 /*
2201 * If redirection is on, also write to the redirection file.
2202 */
Bram Moolenaar32526b32019-01-19 17:43:09 +01002203 redir_write((char_u *)str, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204
2205 /*
2206 * Don't print anything when using ":silent cmd".
2207 */
2208 if (msg_silent != 0)
2209 return;
2210
Bram Moolenaar85a20022019-12-21 18:25:54 +01002211 // if MSG_HIST flag set, add message to history
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 if ((attr & MSG_HIST) && maxlen < 0)
2213 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002214 add_msg_hist((char_u *)str, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 attr &= ~MSG_HIST;
2216 }
2217
Bram Moolenaare8070982019-10-12 17:07:06 +02002218 // When writing something to the screen after it has scrolled, requires a
2219 // wait-return prompt later. Needed when scrolling, resetting
2220 // need_wait_return after some prompt, and then outputting something
2221 // without scrolling
2222 // Not needed when only using CR to move the cursor.
2223 if (msg_scrolled != 0 && !msg_scrolled_ign && STRCMP(str, "\r") != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 need_wait_return = TRUE;
Bram Moolenaare8070982019-10-12 17:07:06 +02002225 msg_didany = TRUE; // remember that something was outputted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226
2227 /*
2228 * If there is no valid screen, use fprintf so we can see error messages.
2229 * If termcap is not active, we may be writing in an alternate console
2230 * window, cursor positioning may not work correctly (window size may be
2231 * different, e.g. for Win32 console) or we just don't know where the
2232 * cursor is.
2233 */
2234 if (msg_use_printf())
Bram Moolenaar32526b32019-01-19 17:43:09 +01002235 msg_puts_printf((char_u *)str, maxlen);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002236 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01002237 msg_puts_display((char_u *)str, maxlen, attr, FALSE);
Rob Pilling726f7f92022-01-20 14:44:38 +00002238
2239 need_fileinfo = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002240}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241
Bram Moolenaar9198de32022-08-27 21:30:03 +01002242// values for "where"
2243#define PUT_APPEND 0 // append to "lnum"
2244#define PUT_TRUNC 1 // replace "lnum"
2245#define PUT_BELOW 2 // add below "lnum"
2246 //
2247#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaar9198de32022-08-27 21:30:03 +01002248/*
Bram Moolenaar24735f22022-08-30 15:44:22 +01002249 * Put text "t_s" until "end" in the message window.
Bram Moolenaar9198de32022-08-27 21:30:03 +01002250 * "where" specifies where to put the text.
2251 */
2252 static void
2253put_msg_win(win_T *wp, int where, char_u *t_s, char_u *end, linenr_T lnum)
2254{
Bram Moolenaarbeedd0a2022-08-27 21:52:52 +01002255 char_u *p;
Bram Moolenaar9198de32022-08-27 21:30:03 +01002256
Bram Moolenaar9198de32022-08-27 21:30:03 +01002257 if (where == PUT_BELOW)
Bram Moolenaarbeedd0a2022-08-27 21:52:52 +01002258 {
2259 if (*end != NUL)
2260 {
2261 p = vim_strnsave(t_s, end - t_s);
2262 if (p == NULL)
2263 return;
2264 }
2265 else
2266 p = t_s;
2267 ml_append_buf(wp->w_buffer, lnum, p, (colnr_T)0, FALSE);
2268 if (p != t_s)
2269 vim_free(p);
2270 }
Bram Moolenaar9198de32022-08-27 21:30:03 +01002271 else
2272 {
2273 char_u *newp;
2274
2275 curbuf = wp->w_buffer;
2276 if (where == PUT_APPEND)
Bram Moolenaarbeedd0a2022-08-27 21:52:52 +01002277 {
Bram Moolenaar9198de32022-08-27 21:30:03 +01002278 newp = concat_str(ml_get(lnum), t_s);
Bram Moolenaarbeedd0a2022-08-27 21:52:52 +01002279 if (newp == NULL)
2280 return;
2281 if (*end != NUL)
2282 newp[STRLEN(ml_get(lnum)) + (end - t_s)] = NUL;
2283 }
Bram Moolenaar9198de32022-08-27 21:30:03 +01002284 else
Bram Moolenaarbeedd0a2022-08-27 21:52:52 +01002285 {
Bram Moolenaar9198de32022-08-27 21:30:03 +01002286 newp = vim_strnsave(t_s, end - t_s);
Bram Moolenaarbeedd0a2022-08-27 21:52:52 +01002287 if (newp == NULL)
2288 return;
2289 }
Bram Moolenaar9198de32022-08-27 21:30:03 +01002290 ml_replace(lnum, newp, FALSE);
2291 curbuf = curwin->w_buffer;
2292 }
2293 redraw_win_later(wp, UPD_NOT_VALID);
2294
2295 // set msg_col so that a newline is written if needed
Bram Moolenaar24735f22022-08-30 15:44:22 +01002296 msg_col += (int)(end - t_s);
Bram Moolenaar9198de32022-08-27 21:30:03 +01002297}
2298#endif
2299
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002300/*
2301 * The display part of msg_puts_attr_len().
2302 * May be called recursively to display scroll-back text.
2303 */
2304 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002305msg_puts_display(
2306 char_u *str,
2307 int maxlen,
2308 int attr,
2309 int recurse)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002310{
2311 char_u *s = str;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002312 char_u *t_s = str; // string from "t_s" to "s" is still todo
2313 int t_col = 0; // screen cells todo, 0 when "t_s" not used
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002314 int l;
2315 int cw;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002316 char_u *sb_str = str;
2317 int sb_col = msg_col;
2318 int wrap;
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002319 int did_last_char;
Bram Moolenaar9198de32022-08-27 21:30:03 +01002320#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaard54af2e2022-08-27 22:05:13 +01002321 int where = PUT_APPEND;
Bram Moolenaar9198de32022-08-27 21:30:03 +01002322 win_T *msg_win = NULL;
2323 linenr_T lnum = 1;
2324
Bram Moolenaara2a89732022-08-31 14:46:18 +01002325 if (in_echowindow)
Bram Moolenaar9198de32022-08-27 21:30:03 +01002326 {
2327 msg_win = popup_get_message_win();
2328
2329 if (msg_win != NULL)
2330 {
2331 if (!popup_message_win_visible())
2332 {
2333 if (*str == NL)
2334 {
2335 // When not showing the message window and the output
2336 // starts with a NL show the message normally.
2337 msg_win = NULL;
2338 }
2339 else
2340 {
2341 // currently hidden, make it empty
2342 curbuf = msg_win->w_buffer;
2343 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
2344 ml_delete(1);
2345 curbuf = curwin->w_buffer;
2346 }
2347 }
2348 else
2349 {
2350 lnum = msg_win->w_buffer->b_ml.ml_line_count;
2351 if (msg_col == 0)
2352 where = PUT_TRUNC;
2353 }
2354 }
2355 }
2356#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357
2358 did_wait_return = FALSE;
Bram Moolenaar57b7fe82007-08-05 17:20:43 +00002359 while ((maxlen < 0 || (int)(s - str) < maxlen) && *s != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 {
2361 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002362 * We are at the end of the screen line when:
2363 * - When outputting a newline.
2364 * - When outputting a character in the last column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002366 if (!recurse && msg_row >= Rows - 1 && (*s == '\n' || (
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367#ifdef FEAT_RIGHTLEFT
2368 cmdmsg_rl
2369 ? (
2370 msg_col <= 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002371 || (*s == TAB && msg_col <= 7)
2372 || (has_mbyte && (*mb_ptr2cells)(s) > 1 && msg_col <= 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 :
2374#endif
Bram Moolenaar0efd1bd2019-12-11 19:00:04 +01002375 ((*s != '\r' && msg_col + t_col >= Columns - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 || (*s == TAB && msg_col + t_col >= ((Columns - 1) & ~7))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 || (has_mbyte && (*mb_ptr2cells)(s) > 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002378 && msg_col + t_col >= Columns - 2)))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002380 /*
2381 * The screen is scrolled up when at the last row (some terminals
2382 * scroll automatically, some don't. To avoid problems we scroll
2383 * ourselves).
2384 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 if (t_col > 0)
Bram Moolenaar9198de32022-08-27 21:30:03 +01002386 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002387 // output postponed text
Bram Moolenaar9198de32022-08-27 21:30:03 +01002388#ifdef HAS_MESSAGE_WINDOW
2389 if (msg_win != NULL)
2390 {
2391 put_msg_win(msg_win, where, t_s, s, lnum);
2392 t_col = 0;
2393 where = PUT_BELOW;
2394 }
2395 else
2396#endif
2397 t_puts(&t_col, t_s, s, attr);
2398 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399
Bram Moolenaar85a20022019-12-21 18:25:54 +01002400 // When no more prompt and no more room, truncate here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 if (msg_no_more && lines_left == 0)
2402 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403
Bram Moolenaar9198de32022-08-27 21:30:03 +01002404#ifdef HAS_MESSAGE_WINDOW
2405 if (msg_win == NULL)
2406#endif
2407 // Scroll the screen up one line.
2408 msg_scroll_up();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409
2410 msg_row = Rows - 2;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002411 if (msg_col >= Columns) // can happen after screen resize
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 msg_col = Columns - 1;
2413
Bram Moolenaar85a20022019-12-21 18:25:54 +01002414 // Display char in last column before showing more-prompt.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002415 if (*s >= ' '
2416#ifdef FEAT_RIGHTLEFT
2417 && !cmdmsg_rl
2418#endif
2419 )
2420 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002421 if (has_mbyte)
2422 {
2423 if (enc_utf8 && maxlen >= 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002424 // avoid including composing chars after the end
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002425 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002426 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002427 l = (*mb_ptr2len)(s);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002428 s = screen_puts_mbyte(s, l, attr);
2429 }
2430 else
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002431 msg_screen_putchar(*s++, attr);
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002432 did_last_char = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002433 }
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002434 else
2435 did_last_char = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002436
2437 if (p_more)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002438 // store text for scrolling back
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002439 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
2440
Bram Moolenaar9198de32022-08-27 21:30:03 +01002441#ifdef HAS_MESSAGE_WINDOW
2442 if (msg_win == NULL)
2443 {
2444#endif
2445 inc_msg_scrolled();
Bram Moolenaar13608d82022-08-29 15:06:50 +01002446 need_wait_return = TRUE; // may need wait_return() in main()
Bram Moolenaar9198de32022-08-27 21:30:03 +01002447 redraw_cmdline = TRUE;
2448 if (cmdline_row > 0 && !exmode_active)
2449 --cmdline_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450
Bram Moolenaar9198de32022-08-27 21:30:03 +01002451 /*
2452 * If screen is completely filled and 'more' is set then wait
2453 * for a character.
2454 */
2455 if (lines_left > 0)
2456 --lines_left;
2457#ifdef HAS_MESSAGE_WINDOW
2458 }
2459#endif
Bram Moolenaar24959102022-05-07 20:01:16 +01002460 if (p_more && lines_left == 0 && State != MODE_HITRETURN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 && !msg_no_more && !exmode_active)
2462 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463#ifdef FEAT_CON_DIALOG
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002464 if (do_more_prompt(NUL))
2465 s = confirm_msg_tail;
2466#else
2467 (void)do_more_prompt(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468#endif
2469 if (quit_more)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002470 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002472
Bram Moolenaar85a20022019-12-21 18:25:54 +01002473 // When we displayed a char in last column need to check if there
2474 // is still more.
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002475 if (did_last_char)
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002476 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 }
2478
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002479 wrap = *s == '\n'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 || msg_col + t_col >= Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 || (has_mbyte && (*mb_ptr2cells)(s) > 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002482 && msg_col + t_col >= Columns - 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002483 if (t_col > 0 && (wrap || *s == '\r' || *s == '\b'
2484 || *s == '\t' || *s == BELL))
Bram Moolenaar9198de32022-08-27 21:30:03 +01002485 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002486 // output any postponed text
Bram Moolenaar9198de32022-08-27 21:30:03 +01002487#ifdef HAS_MESSAGE_WINDOW
2488 if (msg_win != NULL)
2489 {
2490 put_msg_win(msg_win, where, t_s, s, lnum);
2491 t_col = 0;
2492 where = PUT_BELOW;
2493 }
2494 else
2495#endif
2496 t_puts(&t_col, t_s, s, attr);
2497 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002498
2499 if (wrap && p_more && !recurse)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002500 // store text for scrolling back
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002501 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502
Bram Moolenaar85a20022019-12-21 18:25:54 +01002503 if (*s == '\n') // go to next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 {
Bram Moolenaar9198de32022-08-27 21:30:03 +01002505#ifdef HAS_MESSAGE_WINDOW
2506 if (msg_win != NULL)
2507 {
2508 // Ignore a NL when the buffer is empty, it is used to scroll
2509 // up the text.
2510 if ((msg_win->w_buffer->b_ml.ml_flags & ML_EMPTY) == 0)
2511 {
2512 put_msg_win(msg_win, PUT_BELOW, t_s, t_s, lnum);
2513 ++lnum;
2514 }
2515 }
2516 else
2517#endif
2518 msg_didout = FALSE; // remember that line is empty
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002519#ifdef FEAT_RIGHTLEFT
2520 if (cmdmsg_rl)
2521 msg_col = Columns - 1;
2522 else
2523#endif
2524 msg_col = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002525 if (++msg_row >= Rows) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 msg_row = Rows - 1;
2527 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002528 else if (*s == '\r') // go to column 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 {
2530 msg_col = 0;
Bram Moolenaard54af2e2022-08-27 22:05:13 +01002531#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaar9198de32022-08-27 21:30:03 +01002532 where = PUT_TRUNC;
Bram Moolenaard54af2e2022-08-27 22:05:13 +01002533#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002535 else if (*s == '\b') // go to previous char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 {
2537 if (msg_col)
2538 --msg_col;
2539 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002540 else if (*s == TAB) // translate Tab into spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 {
Bram Moolenaar9198de32022-08-27 21:30:03 +01002542#ifdef HAS_MESSAGE_WINDOW
2543 if (msg_win != NULL)
2544 msg_col = (msg_col + 7) % 8;
2545 else
2546#endif
2547 do
2548 msg_screen_putchar(' ', attr);
2549 while (msg_col & 7);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002551 else if (*s == BELL) // beep (from ":sh")
Bram Moolenaar165bc692015-07-21 17:53:25 +02002552 vim_beep(BO_SH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 else
2554 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 if (has_mbyte)
2556 {
2557 cw = (*mb_ptr2cells)(s);
2558 if (enc_utf8 && maxlen >= 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002559 // avoid including composing chars after the end
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002560 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002562 l = (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 }
2564 else
2565 {
2566 cw = 1;
2567 l = 1;
2568 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002569
Bram Moolenaar85a20022019-12-21 18:25:54 +01002570 // When drawing from right to left or when a double-wide character
2571 // doesn't fit, draw a single character here. Otherwise collect
2572 // characters and draw them all at once later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 if (
2574# ifdef FEAT_RIGHTLEFT
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002575 cmdmsg_rl ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576# endif
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002577 (cw > 1 && msg_col + t_col >= Columns - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 if (l > 1)
2580 s = screen_puts_mbyte(s, l, attr) - 1;
2581 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 msg_screen_putchar(*s, attr);
2583 }
2584 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002586 // postpone this character until later
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 if (t_col == 0)
2588 t_s = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 t_col += cw;
2590 s += l - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 }
2592 }
2593 ++s;
2594 }
2595
Bram Moolenaar85a20022019-12-21 18:25:54 +01002596 // output any postponed text
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 if (t_col > 0)
Bram Moolenaar9198de32022-08-27 21:30:03 +01002598 {
2599#ifdef HAS_MESSAGE_WINDOW
2600 if (msg_win != NULL)
2601 put_msg_win(msg_win, where, t_s, s, lnum);
2602 else
2603#endif
2604 t_puts(&t_col, t_s, s, attr);
2605 }
2606
2607#ifdef HAS_MESSAGE_WINDOW
2608 if (msg_win != NULL)
2609 popup_show_message_win();
2610#endif
Bram Moolenaar11901392022-09-26 19:50:44 +01002611 // Store the text for scroll back, unless it's a newline by itself.
2612 if (p_more && !recurse && !(s == sb_str + 1 && *sb_str == '\n'))
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002613 store_sb_text(&sb_str, s, attr, &sb_col, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614
2615 msg_check();
2616}
2617
2618/*
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002619 * Return TRUE when ":filter pattern" was used and "msg" does not match
2620 * "pattern".
2621 */
2622 int
2623message_filtered(char_u *msg)
2624{
Bram Moolenaard29459b2016-08-26 22:29:11 +02002625 int match;
2626
Bram Moolenaare1004402020-10-24 20:49:43 +02002627 if (cmdmod.cmod_filter_regmatch.regprog == NULL)
Bram Moolenaard29459b2016-08-26 22:29:11 +02002628 return FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02002629 match = vim_regexec(&cmdmod.cmod_filter_regmatch, msg, (colnr_T)0);
2630 return cmdmod.cmod_filter_force ? match : !match;
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002631}
2632
2633/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002634 * Scroll the screen up one line for displaying the next message line.
2635 */
2636 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002637msg_scroll_up(void)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002638{
Bram Moolenaar9198de32022-08-27 21:30:03 +01002639#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaara2a89732022-08-31 14:46:18 +01002640 if (in_echowindow)
Bram Moolenaar9198de32022-08-27 21:30:03 +01002641 return;
2642#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002643#ifdef FEAT_GUI
Bram Moolenaar85a20022019-12-21 18:25:54 +01002644 // Remove the cursor before scrolling, ScreenLines[] is going
2645 // to become invalid.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002646 if (gui.in_use)
2647 gui_undraw_cursor();
2648#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01002649 // scrolling up always works
Bram Moolenaara338adc2018-01-31 20:51:47 +01002650 mch_disable_flush();
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002651 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaara338adc2018-01-31 20:51:47 +01002652 mch_enable_flush();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002653
2654 if (!can_clear((char_u *)" "))
2655 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002656 // Scrolling up doesn't result in the right background. Set the
2657 // background here. It's not efficient, but avoids that we have to do
2658 // it all over the code.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002659 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
2660
Bram Moolenaar85a20022019-12-21 18:25:54 +01002661 // Also clear the last char of the last but one line if it was not
2662 // cleared before to avoid a scroll-up.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002663 if (ScreenAttrs[LineOffset[Rows - 2] + Columns - 1] == (sattr_T)-1)
2664 screen_fill((int)Rows - 2, (int)Rows - 1,
2665 (int)Columns - 1, (int)Columns, ' ', ' ', 0);
2666 }
2667}
2668
2669/*
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002670 * Increment "msg_scrolled".
2671 */
2672 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002673inc_msg_scrolled(void)
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002674{
2675#ifdef FEAT_EVAL
2676 if (*get_vim_var_str(VV_SCROLLSTART) == NUL)
2677 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002678 char_u *p = SOURCING_NAME;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002679 char_u *tofree = NULL;
2680 int len;
2681
Bram Moolenaar85a20022019-12-21 18:25:54 +01002682 // v:scrollstart is empty, set it to the script/function name and line
2683 // number
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002684 if (p == NULL)
2685 p = (char_u *)_("Unknown");
2686 else
2687 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002688 len = (int)STRLEN(p) + 40;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002689 tofree = alloc(len);
2690 if (tofree != NULL)
2691 {
2692 vim_snprintf((char *)tofree, len, _("%s line %ld"),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002693 p, (long)SOURCING_LNUM);
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002694 p = tofree;
2695 }
2696 }
2697 set_vim_var_string(VV_SCROLLSTART, p, -1);
2698 vim_free(tofree);
2699 }
2700#endif
2701 ++msg_scrolled;
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01002702 set_must_redraw(UPD_VALID);
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002703}
2704
2705/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002706 * To be able to scroll back at the "more" and "hit-enter" prompts we need to
2707 * store the displayed text and remember where screen lines start.
2708 */
2709typedef struct msgchunk_S msgchunk_T;
2710struct msgchunk_S
2711{
2712 msgchunk_T *sb_next;
2713 msgchunk_T *sb_prev;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002714 char sb_eol; // TRUE when line ends after this text
2715 int sb_msg_col; // column in which text starts
2716 int sb_attr; // text attributes
2717 char_u sb_text[1]; // text to be displayed, actually longer
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002718};
2719
Bram Moolenaar85a20022019-12-21 18:25:54 +01002720static msgchunk_T *last_msgchunk = NULL; // last displayed text
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002721
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002722static msgchunk_T *msg_sb_start(msgchunk_T *mps);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002723
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002724typedef enum {
2725 SB_CLEAR_NONE = 0,
2726 SB_CLEAR_ALL,
2727 SB_CLEAR_CMDLINE_BUSY,
2728 SB_CLEAR_CMDLINE_DONE
2729} sb_clear_T;
2730
Bram Moolenaar85a20022019-12-21 18:25:54 +01002731// When to clear text on next msg.
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002732static sb_clear_T do_clear_sb_text = SB_CLEAR_NONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002733
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002734/*
2735 * Store part of a printed message for displaying when scrolling back.
2736 */
2737 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002738store_sb_text(
Bram Moolenaar85a20022019-12-21 18:25:54 +01002739 char_u **sb_str, // start of string
2740 char_u *s, // just after string
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002741 int attr,
2742 int *sb_col,
Bram Moolenaar85a20022019-12-21 18:25:54 +01002743 int finish) // line ends
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002744{
2745 msgchunk_T *mp;
2746
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002747 if (do_clear_sb_text == SB_CLEAR_ALL
2748 || do_clear_sb_text == SB_CLEAR_CMDLINE_DONE)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002749 {
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002750 clear_sb_text(do_clear_sb_text == SB_CLEAR_ALL);
zeertzjq46af7bc2022-07-28 12:34:09 +01002751 msg_sb_eol(); // prevent messages from overlapping
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002752 do_clear_sb_text = SB_CLEAR_NONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002753 }
2754
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002755 if (s > *sb_str)
2756 {
zeertzjq1b438a82023-02-01 13:11:15 +00002757 mp = alloc(offsetof(msgchunk_T, sb_text) + (s - *sb_str) + 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002758 if (mp != NULL)
2759 {
2760 mp->sb_eol = finish;
2761 mp->sb_msg_col = *sb_col;
2762 mp->sb_attr = attr;
2763 vim_strncpy(mp->sb_text, *sb_str, s - *sb_str);
2764
2765 if (last_msgchunk == NULL)
2766 {
2767 last_msgchunk = mp;
2768 mp->sb_prev = NULL;
2769 }
2770 else
2771 {
2772 mp->sb_prev = last_msgchunk;
2773 last_msgchunk->sb_next = mp;
2774 last_msgchunk = mp;
2775 }
2776 mp->sb_next = NULL;
2777 }
2778 }
2779 else if (finish && last_msgchunk != NULL)
2780 last_msgchunk->sb_eol = TRUE;
2781
2782 *sb_str = s;
2783 *sb_col = 0;
2784}
2785
2786/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002787 * Finished showing messages, clear the scroll-back text on the next message.
2788 */
2789 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002790may_clear_sb_text(void)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002791{
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002792 do_clear_sb_text = SB_CLEAR_ALL;
2793}
2794
2795/*
zeertzjq46af7bc2022-07-28 12:34:09 +01002796 * Starting to edit the command line: do not clear messages now.
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002797 */
2798 void
2799sb_text_start_cmdline(void)
2800{
zeertzjq46af7bc2022-07-28 12:34:09 +01002801 if (do_clear_sb_text == SB_CLEAR_CMDLINE_BUSY)
2802 // Invoking command line recursively: the previous-level command line
2803 // doesn't need to be remembered as it will be redrawn when returning
2804 // to that level.
2805 sb_text_restart_cmdline();
2806 else
2807 {
2808 msg_sb_eol();
2809 do_clear_sb_text = SB_CLEAR_CMDLINE_BUSY;
2810 }
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002811}
2812
2813/*
zeertzjq46af7bc2022-07-28 12:34:09 +01002814 * Redrawing the command line: clear the last unfinished line.
2815 */
2816 void
2817sb_text_restart_cmdline(void)
2818{
2819 msgchunk_T *tofree;
2820
2821 // Needed when returning from nested command line.
2822 do_clear_sb_text = SB_CLEAR_CMDLINE_BUSY;
2823
2824 if (last_msgchunk == NULL || last_msgchunk->sb_eol)
2825 // No unfinished line: don't clear anything.
2826 return;
2827
2828 tofree = msg_sb_start(last_msgchunk);
2829 last_msgchunk = tofree->sb_prev;
2830 if (last_msgchunk != NULL)
2831 last_msgchunk->sb_next = NULL;
2832 while (tofree != NULL)
2833 {
2834 msgchunk_T *tofree_next = tofree->sb_next;
2835
2836 vim_free(tofree);
2837 tofree = tofree_next;
2838 }
2839}
2840
2841/*
2842 * Ending to edit the command line: clear old lines but the last one later.
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002843 */
2844 void
2845sb_text_end_cmdline(void)
2846{
2847 do_clear_sb_text = SB_CLEAR_CMDLINE_DONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002848}
2849
2850/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002851 * Clear any text remembered for scrolling back.
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002852 * When "all" is FALSE keep the last line.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002853 * Called when redrawing the screen.
2854 */
2855 void
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002856clear_sb_text(int all)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002857{
2858 msgchunk_T *mp;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002859 msgchunk_T **lastp;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002860
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002861 if (all)
2862 lastp = &last_msgchunk;
2863 else
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002864 {
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002865 if (last_msgchunk == NULL)
2866 return;
zeertzjqecdc82e2022-07-25 19:50:57 +01002867 lastp = &msg_sb_start(last_msgchunk)->sb_prev;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002868 }
2869
2870 while (*lastp != NULL)
2871 {
2872 mp = (*lastp)->sb_prev;
2873 vim_free(*lastp);
2874 *lastp = mp;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002875 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002876}
2877
2878/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002879 * "g<" command.
2880 */
2881 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002882show_sb_text(void)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002883{
2884 msgchunk_T *mp;
2885
Bram Moolenaar85a20022019-12-21 18:25:54 +01002886 // Only show something if there is more than one line, otherwise it looks
2887 // weird, typing a command without output results in one line.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002888 mp = msg_sb_start(last_msgchunk);
2889 if (mp == NULL || mp->sb_prev == NULL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02002890 vim_beep(BO_MESS);
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002891 else
2892 {
2893 do_more_prompt('G');
2894 wait_return(FALSE);
2895 }
2896}
2897
2898/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002899 * Move to the start of screen line in already displayed text.
2900 */
2901 static msgchunk_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002902msg_sb_start(msgchunk_T *mps)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002903{
2904 msgchunk_T *mp = mps;
2905
2906 while (mp != NULL && mp->sb_prev != NULL && !mp->sb_prev->sb_eol)
2907 mp = mp->sb_prev;
2908 return mp;
2909}
2910
2911/*
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02002912 * Mark the last message chunk as finishing the line.
2913 */
2914 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002915msg_sb_eol(void)
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02002916{
2917 if (last_msgchunk != NULL)
2918 last_msgchunk->sb_eol = TRUE;
2919}
2920
2921/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002922 * Display a screen line from previously displayed text at row "row".
Bram Moolenaar838b7462022-09-26 15:19:56 +01002923 * When "clear_to_eol" is set clear the rest of the screen line.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002924 * Returns a pointer to the text for the next line (can be NULL).
2925 */
2926 static msgchunk_T *
Bram Moolenaar838b7462022-09-26 15:19:56 +01002927disp_sb_line(int row, msgchunk_T *smp, int clear_to_eol)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002928{
2929 msgchunk_T *mp = smp;
2930 char_u *p;
2931
2932 for (;;)
2933 {
2934 msg_row = row;
2935 msg_col = mp->sb_msg_col;
2936 p = mp->sb_text;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002937 if (*p == '\n') // don't display the line break
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002938 ++p;
2939 msg_puts_display(p, -1, mp->sb_attr, TRUE);
Bram Moolenaar838b7462022-09-26 15:19:56 +01002940
2941 // If clearing the screen did not work (e.g. because of a background
2942 // color and t_ut isn't set) clear until the last column here.
2943 if (clear_to_eol)
2944 screen_fill(row, row + 1, msg_col, (int)Columns, ' ', ' ', 0);
2945
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002946 if (mp->sb_eol || mp->sb_next == NULL)
2947 break;
2948 mp = mp->sb_next;
2949 }
2950 return mp->sb_next;
2951}
2952
2953/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954 * Output any postponed text for msg_puts_attr_len().
2955 */
2956 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002957t_puts(
2958 int *t_col,
2959 char_u *t_s,
2960 char_u *s,
2961 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962{
Bram Moolenaar85a20022019-12-21 18:25:54 +01002963 // output postponed text
2964 msg_didout = TRUE; // remember that line is not empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965 screen_puts_len(t_s, (int)(s - t_s), msg_row, msg_col, attr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002966 msg_col += *t_col;
2967 *t_col = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002968 // If the string starts with a composing character don't increment the
2969 // column position for it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 if (enc_utf8 && utf_iscomposing(utf_ptr2char(t_s)))
2971 --msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972 if (msg_col >= Columns)
2973 {
2974 msg_col = 0;
2975 ++msg_row;
2976 }
2977}
2978
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979/*
2980 * Returns TRUE when messages should be printed with mch_errmsg().
2981 * This is used when there is no valid screen, so we can see error messages.
2982 * If termcap is not active, we may be writing in an alternate console
2983 * window, cursor positioning may not work correctly (window size may be
2984 * different, e.g. for Win32 console) or we just don't know where the
2985 * cursor is.
2986 */
2987 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002988msg_use_printf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989{
2990 return (!msg_check_screen()
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002991#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
2992# ifdef VIMDLL
2993 || (!gui.in_use && !termcap_active)
2994# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995 || !termcap_active
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002996# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997#endif
2998 || (swapping_screen() && !termcap_active)
2999 );
3000}
3001
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003002/*
3003 * Print a message when there is no valid screen.
3004 */
3005 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003006msg_puts_printf(char_u *str, int maxlen)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003007{
3008 char_u *s = str;
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003009 char_u *buf = NULL;
3010 char_u *p = s;
3011
Bram Moolenaar4f974752019-02-17 17:44:42 +01003012#ifdef MSWIN
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003013 if (!(silent_mode && p_verbose == 0))
Bram Moolenaar85a20022019-12-21 18:25:54 +01003014 mch_settmode(TMODE_COOK); // handle CR and NL correctly
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003015#endif
Bram Moolenaar2321ca22016-09-09 14:17:18 +02003016 while ((maxlen < 0 || (int)(s - str) < maxlen) && *s != NUL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003017 {
3018 if (!(silent_mode && p_verbose == 0))
3019 {
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003020 // NL --> CR NL translation (for Unix, not for "--version")
3021 if (*s == NL)
3022 {
3023 int n = (int)(s - p);
3024
3025 buf = alloc(n + 3);
Bram Moolenaar6f10c702019-08-20 22:58:37 +02003026 if (buf != NULL)
3027 {
3028 memcpy(buf, p, n);
3029 if (!info_message)
3030 buf[n++] = CAR;
3031 buf[n++] = NL;
3032 buf[n++] = NUL;
3033 if (info_message) // informative message, not an error
3034 mch_msg((char *)buf);
3035 else
3036 mch_errmsg((char *)buf);
3037 vim_free(buf);
3038 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003039 p = s + 1;
3040 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003041 }
3042
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003043 // primitive way to compute the current column
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003044#ifdef FEAT_RIGHTLEFT
3045 if (cmdmsg_rl)
3046 {
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003047 if (*s == CAR || *s == NL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003048 msg_col = Columns - 1;
3049 else
3050 --msg_col;
3051 }
3052 else
3053#endif
3054 {
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003055 if (*s == CAR || *s == NL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003056 msg_col = 0;
3057 else
3058 ++msg_col;
3059 }
3060 ++s;
3061 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003062
3063 if (*p != NUL && !(silent_mode && p_verbose == 0))
3064 {
Bram Moolenaara97c3632021-06-15 22:39:11 +02003065 char_u *tofree = NULL;
Bram Moolenaar97c2c052019-02-22 13:42:07 +01003066
Bram Moolenaarc32949b2023-01-04 15:56:51 +00003067 if (maxlen > 0 && vim_strlen_maxlen((char *)p, (size_t)maxlen)
3068 >= (size_t)maxlen)
Bram Moolenaar97c2c052019-02-22 13:42:07 +01003069 {
Bram Moolenaara97c3632021-06-15 22:39:11 +02003070 tofree = vim_strnsave(p, (size_t)maxlen);
3071 p = tofree;
Bram Moolenaar97c2c052019-02-22 13:42:07 +01003072 }
Bram Moolenaara97c3632021-06-15 22:39:11 +02003073 if (p != NULL)
3074 {
3075 if (info_message)
3076 mch_msg((char *)p);
3077 else
3078 mch_errmsg((char *)p);
3079 vim_free(tofree);
3080 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003081 }
3082
3083 msg_didout = TRUE; // assume that line is not empty
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003084
Bram Moolenaar4f974752019-02-17 17:44:42 +01003085#ifdef MSWIN
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003086 if (!(silent_mode && p_verbose == 0))
3087 mch_settmode(TMODE_RAW);
3088#endif
3089}
3090
3091/*
3092 * Show the more-prompt and handle the user response.
3093 * This takes care of scrolling back and displaying previously displayed text.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003094 * When at hit-enter prompt "typed_char" is the already typed character,
3095 * otherwise it's NUL.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003096 * Returns TRUE when jumping ahead to "confirm_msg_tail".
3097 */
3098 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003099do_more_prompt(int typed_char)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003100{
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01003101 static int entered = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003102 int used_typed_char = typed_char;
3103 int oldState = State;
3104 int c;
3105#ifdef FEAT_CON_DIALOG
3106 int retval = FALSE;
3107#endif
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003108 int toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003109 msgchunk_T *mp_last = NULL;
3110 msgchunk_T *mp;
3111 int i;
3112
Bram Moolenaar85a20022019-12-21 18:25:54 +01003113 // We get called recursively when a timer callback outputs a message. In
3114 // that case don't show another prompt. Also when at the hit-Enter prompt
3115 // and nothing was typed.
Bram Moolenaar24959102022-05-07 20:01:16 +01003116 if (entered || (State == MODE_HITRETURN && typed_char == 0))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01003117 return FALSE;
3118 entered = TRUE;
3119
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003120 if (typed_char == 'G')
3121 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003122 // "g<": Find first line on the last page.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003123 mp_last = msg_sb_start(last_msgchunk);
3124 for (i = 0; i < Rows - 2 && mp_last != NULL
3125 && mp_last->sb_prev != NULL; ++i)
3126 mp_last = msg_sb_start(mp_last->sb_prev);
3127 }
3128
Bram Moolenaar24959102022-05-07 20:01:16 +01003129 State = MODE_ASKMORE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003130 setmouse();
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003131 if (typed_char == NUL)
3132 msg_moremsg(FALSE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003133 for (;;)
3134 {
3135 /*
3136 * Get a typed character directly from the user.
3137 */
3138 if (used_typed_char != NUL)
3139 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003140 c = used_typed_char; // was typed at hit-enter prompt
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003141 used_typed_char = NUL;
3142 }
3143 else
3144 c = get_keystroke();
3145
3146#if defined(FEAT_MENU) && defined(FEAT_GUI)
3147 if (c == K_MENU)
3148 {
Bram Moolenaar24959102022-05-07 20:01:16 +01003149 int idx = get_menu_index(current_menu, MODE_ASKMORE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003150
Bram Moolenaar85a20022019-12-21 18:25:54 +01003151 // Used a menu. If it starts with CTRL-Y, it must
3152 // be a "Copy" for the clipboard. Otherwise
3153 // assume that we end
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003154 if (idx == MENU_INDEX_INVALID)
3155 continue;
3156 c = *current_menu->strings[idx];
3157 if (c != NUL && current_menu->strings[idx][1] != NUL)
3158 ins_typebuf(current_menu->strings[idx] + 1,
3159 current_menu->noremap[idx], 0, TRUE,
3160 current_menu->silent[idx]);
3161 }
3162#endif
3163
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003164 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003165 switch (c)
3166 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003167 case BS: // scroll one line back
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003168 case K_BS:
3169 case 'k':
3170 case K_UP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003171 toscroll = -1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003172 break;
3173
Bram Moolenaar85a20022019-12-21 18:25:54 +01003174 case CAR: // one extra line
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003175 case NL:
3176 case 'j':
3177 case K_DOWN:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003178 toscroll = 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003179 break;
3180
Bram Moolenaar85a20022019-12-21 18:25:54 +01003181 case 'u': // Up half a page
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003182 toscroll = -(Rows / 2);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003183 break;
3184
Bram Moolenaar85a20022019-12-21 18:25:54 +01003185 case 'd': // Down half a page
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003186 toscroll = Rows / 2;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003187 break;
3188
Bram Moolenaar85a20022019-12-21 18:25:54 +01003189 case 'b': // one page back
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00003190 case K_PAGEUP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003191 toscroll = -(Rows - 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003192 break;
3193
Bram Moolenaar85a20022019-12-21 18:25:54 +01003194 case ' ': // one extra page
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00003195 case 'f':
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003196 case K_PAGEDOWN:
3197 case K_LEFTMOUSE:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003198 toscroll = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003199 break;
3200
Bram Moolenaar85a20022019-12-21 18:25:54 +01003201 case 'g': // all the way back to the start
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003202 toscroll = -999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003203 break;
3204
Bram Moolenaar85a20022019-12-21 18:25:54 +01003205 case 'G': // all the way to the end
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003206 toscroll = 999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003207 lines_left = 999999;
3208 break;
3209
Bram Moolenaar85a20022019-12-21 18:25:54 +01003210 case ':': // start new command line
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003211#ifdef FEAT_CON_DIALOG
3212 if (!confirm_msg_used)
3213#endif
3214 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003215 // Since got_int is set all typeahead will be flushed, but we
3216 // want to keep this ':', remember that in a special way.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003217 typeahead_noflush(':');
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02003218#ifdef FEAT_TERMINAL
3219 skip_term_loop = TRUE;
3220#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01003221 cmdline_row = Rows - 1; // put ':' on this line
3222 skip_redraw = TRUE; // skip redraw once
3223 need_wait_return = FALSE; // don't wait in main()
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003224 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003225 // FALLTHROUGH
3226 case 'q': // quit
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003227 case Ctrl_C:
3228 case ESC:
3229#ifdef FEAT_CON_DIALOG
3230 if (confirm_msg_used)
3231 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003232 // Jump to the choices of the dialog.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003233 retval = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003234 }
3235 else
3236#endif
3237 {
3238 got_int = TRUE;
3239 quit_more = TRUE;
3240 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003241 // When there is some more output (wrapping line) display that
3242 // without another prompt.
Bram Moolenaar51306d22009-02-24 03:38:04 +00003243 lines_left = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003244 break;
3245
3246#ifdef FEAT_CLIPBOARD
3247 case Ctrl_Y:
Bram Moolenaar85a20022019-12-21 18:25:54 +01003248 // Strange way to allow copying (yanking) a modeless
3249 // selection at the more prompt. Use CTRL-Y,
3250 // because the same is used in Cmdline-mode and at the
3251 // hit-enter prompt. However, scrolling one line up
3252 // might be expected...
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003253 if (clip_star.state == SELECT_DONE)
3254 clip_copy_modeless_selection(TRUE);
3255 continue;
3256#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01003257 default: // no valid response
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003258 msg_moremsg(TRUE);
3259 continue;
3260 }
3261
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003262 if (toscroll != 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003263 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003264 if (toscroll < 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003265 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003266 // go to start of last line
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003267 if (mp_last == NULL)
3268 mp = msg_sb_start(last_msgchunk);
3269 else if (mp_last->sb_prev != NULL)
3270 mp = msg_sb_start(mp_last->sb_prev);
3271 else
3272 mp = NULL;
3273
Bram Moolenaar85a20022019-12-21 18:25:54 +01003274 // go to start of line at top of the screen
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003275 for (i = 0; i < Rows - 2 && mp != NULL && mp->sb_prev != NULL;
3276 ++i)
3277 mp = msg_sb_start(mp->sb_prev);
3278
3279 if (mp != NULL && mp->sb_prev != NULL)
3280 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003281 // Find line to be displayed at top.
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003282 for (i = 0; i > toscroll; --i)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003283 {
3284 if (mp == NULL || mp->sb_prev == NULL)
3285 break;
3286 mp = msg_sb_start(mp->sb_prev);
3287 if (mp_last == NULL)
3288 mp_last = msg_sb_start(last_msgchunk);
3289 else
3290 mp_last = msg_sb_start(mp_last->sb_prev);
3291 }
3292
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003293 if (toscroll == -1 && screen_ins_lines(0, 0, 1,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003294 (int)Rows, 0, NULL) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003295 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003296 // display line at top
Bram Moolenaar838b7462022-09-26 15:19:56 +01003297 (void)disp_sb_line(0, mp, FALSE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003298 }
3299 else
3300 {
Bram Moolenaar838b7462022-09-26 15:19:56 +01003301 int did_clear = screenclear();
3302
Bram Moolenaar85a20022019-12-21 18:25:54 +01003303 // redisplay all lines
Bram Moolenaar773560b2006-05-06 21:38:18 +00003304 for (i = 0; mp != NULL && i < Rows - 1; ++i)
3305 {
Bram Moolenaar838b7462022-09-26 15:19:56 +01003306 mp = disp_sb_line(i, mp, !did_clear);
Bram Moolenaar773560b2006-05-06 21:38:18 +00003307 ++msg_scrolled;
3308 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003309 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003310 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003311 }
3312 }
3313 else
3314 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003315 // First display any text that we scrolled back.
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003316 while (toscroll > 0 && mp_last != NULL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003317 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003318 // scroll up, display line at bottom
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003319 msg_scroll_up();
Bram Moolenaarbb15b652005-10-03 21:52:09 +00003320 inc_msg_scrolled();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003321 screen_fill((int)Rows - 2, (int)Rows - 1, 0,
3322 (int)Columns, ' ', ' ', 0);
Bram Moolenaar838b7462022-09-26 15:19:56 +01003323 mp_last = disp_sb_line((int)Rows - 2, mp_last, FALSE);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003324 --toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003325 }
3326 }
3327
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003328 if (toscroll <= 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003329 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003330 // displayed the requested text, more prompt again
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003331 screen_fill((int)Rows - 1, (int)Rows, 0,
3332 (int)Columns, ' ', ' ', 0);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003333 msg_moremsg(FALSE);
3334 continue;
3335 }
3336
Bram Moolenaar85a20022019-12-21 18:25:54 +01003337 // display more text, return to caller
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003338 lines_left = toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003339 }
3340
3341 break;
3342 }
3343
Bram Moolenaar85a20022019-12-21 18:25:54 +01003344 // clear the --more-- message
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003345 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3346 State = oldState;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003347 setmouse();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003348 if (quit_more)
3349 {
3350 msg_row = Rows - 1;
3351 msg_col = 0;
3352 }
3353#ifdef FEAT_RIGHTLEFT
3354 else if (cmdmsg_rl)
3355 msg_col = Columns - 1;
3356#endif
3357
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01003358 entered = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003359#ifdef FEAT_CON_DIALOG
3360 return retval;
3361#else
3362 return FALSE;
3363#endif
3364}
3365
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366#if defined(USE_MCH_ERRMSG) || defined(PROTO)
3367
3368#ifdef mch_errmsg
3369# undef mch_errmsg
3370#endif
3371#ifdef mch_msg
3372# undef mch_msg
3373#endif
3374
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003375#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3376 static void
3377mch_errmsg_c(char *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378{
Bram Moolenaar0f77d6a2019-02-14 20:55:09 +01003379 int len = (int)STRLEN(str);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003380 DWORD nwrite = 0;
3381 DWORD mode = 0;
3382 HANDLE h = GetStdHandle(STD_ERROR_HANDLE);
3383
3384 if (GetConsoleMode(h, &mode) && enc_codepage >= 0
3385 && (int)GetConsoleCP() != enc_codepage)
3386 {
3387 WCHAR *w = enc_to_utf16((char_u *)str, &len);
3388
3389 WriteConsoleW(h, w, len, &nwrite, NULL);
3390 vim_free(w);
3391 }
3392 else
3393 {
3394 fprintf(stderr, "%s", str);
3395 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003396}
3397#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003399/*
3400 * Give an error message. To be used when the screen hasn't been initialized
3401 * yet. When stderr can't be used, collect error messages until the GUI has
3402 * started and they can be displayed in a message box.
3403 */
3404 void
3405mch_errmsg(char *str)
3406{
3407#if !defined(MSWIN) || defined(FEAT_GUI_MSWIN)
3408 int len;
3409#endif
3410
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02003411#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003412 // On Unix use stderr if it's a tty.
3413 // When not going to start the GUI also use stderr.
3414 // On Mac, when started from Finder, stderr is the console.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 if (
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003416# ifdef UNIX
3417# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003419# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 isatty(2)
3421# endif
3422# ifdef FEAT_GUI
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003423 ||
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003424# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003425# endif
3426# ifdef FEAT_GUI
3427 !(gui.in_use || gui.starting)
3428# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 )
3430 {
3431 fprintf(stderr, "%s", str);
3432 return;
3433 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003434#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003436#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3437# ifdef VIMDLL
3438 if (!(gui.in_use || gui.starting))
3439# endif
3440 {
3441 mch_errmsg_c(str);
3442 return;
3443 }
3444#endif
3445
3446#if !defined(MSWIN) || defined(FEAT_GUI_MSWIN)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003447 // avoid a delay for a message that isn't there
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 emsg_on_display = FALSE;
3449
3450 len = (int)STRLEN(str) + 1;
3451 if (error_ga.ga_growsize == 0)
3452 {
3453 error_ga.ga_growsize = 80;
3454 error_ga.ga_itemsize = 1;
3455 }
3456 if (ga_grow(&error_ga, len) == OK)
3457 {
3458 mch_memmove((char_u *)error_ga.ga_data + error_ga.ga_len,
3459 (char_u *)str, len);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003460# ifdef UNIX
Bram Moolenaar85a20022019-12-21 18:25:54 +01003461 // remove CR characters, they are displayed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 {
3463 char_u *p;
3464
3465 p = (char_u *)error_ga.ga_data + error_ga.ga_len;
3466 for (;;)
3467 {
3468 p = vim_strchr(p, '\r');
3469 if (p == NULL)
3470 break;
3471 *p = ' ';
3472 }
3473 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003474# endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01003475 --len; // don't count the NUL at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 error_ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003478#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479}
3480
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003481#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3482 static void
3483mch_msg_c(char *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484{
Bram Moolenaar0f77d6a2019-02-14 20:55:09 +01003485 int len = (int)STRLEN(str);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003486 DWORD nwrite = 0;
3487 DWORD mode;
3488 HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
3489
3490
3491 if (GetConsoleMode(h, &mode) && enc_codepage >= 0
3492 && (int)GetConsoleCP() != enc_codepage)
3493 {
3494 WCHAR *w = enc_to_utf16((char_u *)str, &len);
3495
3496 WriteConsoleW(h, w, len, &nwrite, NULL);
3497 vim_free(w);
3498 }
3499 else
3500 {
3501 printf("%s", str);
3502 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003503}
3504#endif
3505
3506/*
3507 * Give a message. To be used when the screen hasn't been initialized yet.
3508 * When there is no tty, collect messages until the GUI has started and they
3509 * can be displayed in a message box.
3510 */
3511 void
3512mch_msg(char *str)
3513{
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02003514#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003515 // On Unix use stdout if we have a tty. This allows "vim -h | more" and
3516 // uses mch_errmsg() when started from the desktop.
3517 // When not going to start the GUI also use stdout.
3518 // On Mac, when started from Finder, stderr is the console.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 if (
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003520# ifdef UNIX
3521# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003523# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 isatty(2)
Bram Moolenaareae1b912019-05-09 15:12:55 +02003525# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526# ifdef FEAT_GUI
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003527 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003529# endif
3530# ifdef FEAT_GUI
3531 !(gui.in_use || gui.starting)
3532# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 )
3534 {
3535 printf("%s", str);
3536 return;
3537 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003538#endif
3539
3540#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3541# ifdef VIMDLL
3542 if (!(gui.in_use || gui.starting))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003544 {
3545 mch_msg_c(str);
3546 return;
3547 }
3548#endif
3549#if !defined(MSWIN) || defined(FEAT_GUI_MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 mch_errmsg(str);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003551#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552}
Bram Moolenaar85a20022019-12-21 18:25:54 +01003553#endif // USE_MCH_ERRMSG
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554
3555/*
3556 * Put a character on the screen at the current message position and advance
3557 * to the next position. Only for printable ASCII!
3558 */
3559 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003560msg_screen_putchar(int c, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561{
Bram Moolenaar85a20022019-12-21 18:25:54 +01003562 msg_didout = TRUE; // remember that line is not empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 screen_putchar(c, msg_row, msg_col, attr);
3564#ifdef FEAT_RIGHTLEFT
3565 if (cmdmsg_rl)
3566 {
3567 if (--msg_col == 0)
3568 {
3569 msg_col = Columns;
3570 ++msg_row;
3571 }
3572 }
3573 else
3574#endif
3575 {
3576 if (++msg_col >= Columns)
3577 {
3578 msg_col = 0;
3579 ++msg_row;
3580 }
3581 }
3582}
3583
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003584 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003585msg_moremsg(int full)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003587 int attr;
3588 char_u *s = (char_u *)_("-- More --");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589
Bram Moolenaar8820b482017-03-16 17:23:31 +01003590 attr = HL_ATTR(HLF_M);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003591 screen_puts(s, (int)Rows - 1, 0, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 if (full)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003593 screen_puts((char_u *)
3594 _(" SPACE/d/j: screen/page/line down, b/u/k: up, q: quit "),
3595 (int)Rows - 1, vim_strsize(s), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596}
3597
3598/*
Bram Moolenaar24959102022-05-07 20:01:16 +01003599 * Repeat the message for the current mode: MODE_ASKMORE, MODE_EXTERNCMD,
3600 * MODE_CONFIRM or exmode_active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 */
3602 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003603repeat_message(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604{
Bram Moolenaar24959102022-05-07 20:01:16 +01003605 if (State == MODE_ASKMORE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003607 msg_moremsg(TRUE); // display --more-- message again
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 msg_row = Rows - 1;
3609 }
3610#ifdef FEAT_CON_DIALOG
Bram Moolenaar24959102022-05-07 20:01:16 +01003611 else if (State == MODE_CONFIRM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003613 display_confirm_msg(); // display ":confirm" message again
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 msg_row = Rows - 1;
3615 }
3616#endif
Bram Moolenaar24959102022-05-07 20:01:16 +01003617 else if (State == MODE_EXTERNCMD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003619 windgoto(msg_row, msg_col); // put cursor back
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620 }
Bram Moolenaar24959102022-05-07 20:01:16 +01003621 else if (State == MODE_HITRETURN || State == MODE_SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 {
Bram Moolenaar9f108752007-11-24 14:44:58 +00003623 if (msg_row == Rows - 1)
3624 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003625 // Avoid drawing the "hit-enter" prompt below the previous one,
3626 // overwrite it. Esp. useful when regaining focus and a
3627 // FocusGained autocmd exists but didn't draw anything.
Bram Moolenaar9f108752007-11-24 14:44:58 +00003628 msg_didout = FALSE;
3629 msg_col = 0;
3630 msg_clr_eos();
3631 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 hit_return_msg();
3633 msg_row = Rows - 1;
3634 }
3635}
3636
3637/*
3638 * msg_check_screen - check if the screen is initialized.
3639 * Also check msg_row and msg_col, if they are too big it may cause a crash.
3640 * While starting the GUI the terminal codes will be set for the GUI, but the
3641 * output goes to the terminal. Don't use the terminal codes then.
3642 */
3643 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003644msg_check_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645{
3646 if (!full_screen || !screen_valid(FALSE))
3647 return FALSE;
3648
3649 if (msg_row >= Rows)
3650 msg_row = Rows - 1;
3651 if (msg_col >= Columns)
3652 msg_col = Columns - 1;
3653 return TRUE;
3654}
3655
3656/*
3657 * Clear from current message position to end of screen.
3658 * Skip this when ":silent" was used, no need to clear for redirection.
3659 */
3660 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003661msg_clr_eos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662{
3663 if (msg_silent == 0)
3664 msg_clr_eos_force();
3665}
3666
3667/*
3668 * Clear from current message position to end of screen.
3669 * Note: msg_col is not updated, so we remember the end of the message
3670 * for msg_check().
3671 */
3672 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003673msg_clr_eos_force(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674{
Bram Moolenaar3b474dc2022-09-01 17:01:32 +01003675#ifdef HAS_MESSAGE_WINDOW
3676 if (in_echowindow)
3677 return; // messages go into a popup
3678#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 if (msg_use_printf())
3680 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003681 if (full_screen) // only when termcap codes are valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 {
3683 if (*T_CD)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003684 out_str(T_CD); // clear to end of display
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 else if (*T_CE)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003686 out_str(T_CE); // clear to end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 }
3688 }
Bram Moolenaara2a89732022-08-31 14:46:18 +01003689 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 {
3691#ifdef FEAT_RIGHTLEFT
3692 if (cmdmsg_rl)
3693 {
3694 screen_fill(msg_row, msg_row + 1, 0, msg_col + 1, ' ', ' ', 0);
3695 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3696 }
3697 else
3698#endif
3699 {
3700 screen_fill(msg_row, msg_row + 1, msg_col, (int)Columns,
3701 ' ', ' ', 0);
3702 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns, ' ', ' ', 0);
3703 }
3704 }
3705}
3706
3707/*
3708 * Clear the command line.
3709 */
3710 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003711msg_clr_cmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712{
3713 msg_row = cmdline_row;
3714 msg_col = 0;
3715 msg_clr_eos_force();
3716}
3717
3718/*
3719 * end putting a message on the screen
Bram Moolenaar13608d82022-08-29 15:06:50 +01003720 * call wait_return() if the message does not fit in the available space
3721 * return TRUE if wait_return() not called.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 */
3723 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003724msg_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725{
3726 /*
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02003727 * If the string is larger than the window,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 * or the ruler option is set and we run into it,
3729 * we have to redraw the window.
3730 * Do not do this if we are abandoning the file or editing the command line.
3731 */
Bram Moolenaar24959102022-05-07 20:01:16 +01003732 if (!exiting && need_wait_return && !(State & MODE_CMDLINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 {
3734 wait_return(FALSE);
3735 return FALSE;
3736 }
3737 out_flush();
3738 return TRUE;
3739}
3740
3741/*
3742 * If the written message runs into the shown command or ruler, we have to
3743 * wait for hit-return and redraw the window later.
3744 */
3745 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003746msg_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747{
Bram Moolenaar35a4fbc2022-08-28 14:39:53 +01003748 if (msg_row == Rows - 1 && msg_col >= sc_col
3749#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaara2a89732022-08-31 14:46:18 +01003750 && !in_echowindow
Bram Moolenaar35a4fbc2022-08-28 14:39:53 +01003751#endif
3752 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 {
3754 need_wait_return = TRUE;
3755 redraw_cmdline = TRUE;
3756 }
3757}
3758
3759/*
3760 * May write a string to the redirection file.
3761 * When "maxlen" is -1 write the whole string, otherwise up to "maxlen" bytes.
3762 */
3763 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003764redir_write(char_u *str, int maxlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765{
3766 char_u *s = str;
3767 static int cur_col = 0;
3768
Bram Moolenaar85a20022019-12-21 18:25:54 +01003769 // Don't do anything for displaying prompts and the like.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003770 if (redir_off)
3771 return;
3772
Bram Moolenaar85a20022019-12-21 18:25:54 +01003773 // If 'verbosefile' is set prepare for writing in that file.
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003774 if (*p_vfile != NUL && verbose_fd == NULL)
3775 verbose_open();
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003776
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003777 if (redirecting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003779 // If the string doesn't start with CR or NL, go to msg_col
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 if (*s != '\n' && *s != '\r')
3781 {
3782 while (cur_col < msg_col)
3783 {
3784#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003785 if (redir_execute)
3786 execute_redir_str((char_u *)" ", -1);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003787 else if (redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 write_reg_contents(redir_reg, (char_u *)" ", -1, TRUE);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003789 else if (redir_vname)
3790 var_redir_str((char_u *)" ", -1);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003791 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003793 if (redir_fd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 fputs(" ", redir_fd);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003795 if (verbose_fd != NULL)
3796 fputs(" ", verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 ++cur_col;
3798 }
3799 }
3800
3801#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003802 if (redir_execute)
3803 execute_redir_str(s, maxlen);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003804 else if (redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 write_reg_contents(redir_reg, s, maxlen, TRUE);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003806 else if (redir_vname)
Bram Moolenaardf177f62005-02-22 08:39:57 +00003807 var_redir_str(s, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808#endif
3809
Bram Moolenaar85a20022019-12-21 18:25:54 +01003810 // Write and adjust the current column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
3812 {
3813#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003814 if (!redir_reg && !redir_vname && !redir_execute)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003816 if (redir_fd != NULL)
3817 putc(*s, redir_fd);
3818 if (verbose_fd != NULL)
3819 putc(*s, verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820 if (*s == '\r' || *s == '\n')
3821 cur_col = 0;
3822 else if (*s == '\t')
3823 cur_col += (8 - cur_col % 8);
3824 else
3825 ++cur_col;
3826 ++s;
3827 }
3828
Bram Moolenaar85a20022019-12-21 18:25:54 +01003829 if (msg_silent != 0) // should update msg_col
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 msg_col = cur_col;
3831 }
3832}
3833
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003834 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003835redirecting(void)
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003836{
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003837 return redir_fd != NULL || *p_vfile != NUL
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003838#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003839 || redir_reg || redir_vname || redir_execute
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003840#endif
3841 ;
3842}
3843
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844/*
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00003845 * Before giving verbose message.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003846 * Must always be called paired with verbose_leave()!
3847 */
3848 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003849verbose_enter(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003850{
3851 if (*p_vfile != NUL)
3852 ++msg_silent;
3853}
3854
3855/*
3856 * After giving verbose message.
3857 * Must always be called paired with verbose_enter()!
3858 */
3859 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003860verbose_leave(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003861{
3862 if (*p_vfile != NUL)
3863 if (--msg_silent < 0)
3864 msg_silent = 0;
3865}
3866
3867/*
3868 * Like verbose_enter() and set msg_scroll when displaying the message.
3869 */
3870 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003871verbose_enter_scroll(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003872{
3873 if (*p_vfile != NUL)
3874 ++msg_silent;
3875 else
Bram Moolenaar85a20022019-12-21 18:25:54 +01003876 // always scroll up, don't overwrite
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003877 msg_scroll = TRUE;
3878}
3879
3880/*
3881 * Like verbose_leave() and set cmdline_row when displaying the message.
3882 */
3883 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003884verbose_leave_scroll(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003885{
3886 if (*p_vfile != NUL)
3887 {
3888 if (--msg_silent < 0)
3889 msg_silent = 0;
3890 }
3891 else
3892 cmdline_row = msg_row;
3893}
3894
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003895/*
3896 * Called when 'verbosefile' is set: stop writing to the file.
3897 */
3898 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003899verbose_stop(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003900{
3901 if (verbose_fd != NULL)
3902 {
3903 fclose(verbose_fd);
3904 verbose_fd = NULL;
3905 }
3906 verbose_did_open = FALSE;
3907}
3908
3909/*
3910 * Open the file 'verbosefile'.
3911 * Return FAIL or OK.
3912 */
3913 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003914verbose_open(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003915{
3916 if (verbose_fd == NULL && !verbose_did_open)
3917 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003918 // Only give the error message once.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003919 verbose_did_open = TRUE;
3920
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003921 verbose_fd = mch_fopen((char *)p_vfile, "a");
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003922 if (verbose_fd == NULL)
3923 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003924 semsg(_(e_cant_open_file_str), p_vfile);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003925 return FAIL;
3926 }
3927 }
3928 return OK;
3929}
3930
3931/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 * Give a warning message (for searching).
3933 * Use 'w' highlighting and may repeat the message after redrawing
3934 */
3935 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003936give_warning(char_u *message, int hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937{
Bram Moolenaarcc7eb2a2021-07-11 19:12:04 +02003938 give_warning_with_source(message, hl, FALSE);
3939}
3940
3941 void
3942give_warning_with_source(char_u *message, int hl, int with_source)
3943{
Bram Moolenaar85a20022019-12-21 18:25:54 +01003944 // Don't do this for ":silent".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 if (msg_silent != 0)
3946 return;
3947
Bram Moolenaar85a20022019-12-21 18:25:54 +01003948 // Don't want a hit-enter prompt here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 ++no_wait_return;
Bram Moolenaared203462004-06-16 11:19:22 +00003950
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951#ifdef FEAT_EVAL
3952 set_vim_var_string(VV_WARNINGMSG, message, -1);
3953#endif
Bram Moolenaard23a8232018-02-10 18:45:26 +01003954 VIM_CLEAR(keep_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 if (hl)
Bram Moolenaar8820b482017-03-16 17:23:31 +01003956 keep_msg_attr = HL_ATTR(HLF_W);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 else
3958 keep_msg_attr = 0;
Bram Moolenaarcc7eb2a2021-07-11 19:12:04 +02003959
3960 if (with_source)
3961 {
3962 // Do what msg() does, but with a column offset if the warning should
3963 // be after the mode message.
3964 msg_start();
3965 msg_source(HL_ATTR(HLF_W));
3966 msg_puts(" ");
3967 msg_puts_attr((char *)message, HL_ATTR(HLF_W) | MSG_HIST);
3968 msg_clr_eos();
3969 (void)msg_end();
3970 }
3971 else if (msg_attr((char *)message, keep_msg_attr) && msg_scrolled == 0)
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003972 set_keep_msg(message, keep_msg_attr);
Bram Moolenaarcc7eb2a2021-07-11 19:12:04 +02003973
Bram Moolenaar85a20022019-12-21 18:25:54 +01003974 msg_didout = FALSE; // overwrite this message
3975 msg_nowait = TRUE; // don't wait for this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 msg_col = 0;
Bram Moolenaared203462004-06-16 11:19:22 +00003977
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 --no_wait_return;
3979}
3980
Bram Moolenaar113e1072019-01-20 15:30:40 +01003981#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarf8be4612017-06-23 20:52:40 +02003982 void
3983give_warning2(char_u *message, char_u *a1, int hl)
3984{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +02003985 if (IObuff == NULL)
3986 {
3987 // Very early in initialisation and already something wrong, just give
3988 // the raw message so the user at least gets a hint.
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003989 give_warning(message, hl);
Bram Moolenaare3a22cb2019-10-14 22:01:57 +02003990 }
3991 else
3992 {
3993 vim_snprintf((char *)IObuff, IOSIZE, (char *)message, a1);
3994 give_warning(IObuff, hl);
3995 }
Bram Moolenaarf8be4612017-06-23 20:52:40 +02003996}
Bram Moolenaar113e1072019-01-20 15:30:40 +01003997#endif
Bram Moolenaarf8be4612017-06-23 20:52:40 +02003998
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999/*
4000 * Advance msg cursor to column "col".
4001 */
4002 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004003msg_advance(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004{
Bram Moolenaar85a20022019-12-21 18:25:54 +01004005 if (msg_silent != 0) // nothing to advance to
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004007 msg_col = col; // for redirection, may fill it up later
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 return;
4009 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01004010 if (col >= Columns) // not enough room
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 col = Columns - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004012#ifdef FEAT_RIGHTLEFT
4013 if (cmdmsg_rl)
4014 while (msg_col > Columns - col)
4015 msg_putchar(' ');
4016 else
4017#endif
4018 while (msg_col < col)
4019 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020}
4021
4022#if defined(FEAT_CON_DIALOG) || defined(PROTO)
4023/*
4024 * Used for "confirm()" function, and the :confirm command prefix.
4025 * Versions which haven't got flexible dialogs yet, and console
4026 * versions, get this generic handler which uses the command line.
4027 *
4028 * type = one of:
4029 * VIM_QUESTION, VIM_INFO, VIM_WARNING, VIM_ERROR or VIM_GENERIC
4030 * title = title string (can be NULL for default)
4031 * (neither used in console dialogs at the moment)
4032 *
4033 * Format of the "buttons" string:
4034 * "Button1Name\nButton2Name\nButton3Name"
4035 * The first button should normally be the default/accept
4036 * The second button should be the 'Cancel' button
4037 * Other buttons- use your imagination!
4038 * A '&' in a button name becomes a shortcut, so each '&' should be before a
4039 * different letter.
Rob Pilling8196e942022-02-11 15:12:10 +00004040 *
4041 * Returns 0 if cancelled, otherwise the nth button (1-indexed).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004044do_dialog(
4045 int type UNUSED,
4046 char_u *title UNUSED,
4047 char_u *message,
4048 char_u *buttons,
4049 int dfltbutton,
Bram Moolenaar85a20022019-12-21 18:25:54 +01004050 char_u *textfield UNUSED, // IObuff for inputdialog(), NULL
4051 // otherwise
4052 int ex_cmd) // when TRUE pressing : accepts default and starts
4053 // Ex command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054{
4055 int oldState;
4056 int retval = 0;
4057 char_u *hotkeys;
4058 int c;
4059 int i;
Bram Moolenaar27321db2020-07-06 21:24:57 +02004060 tmode_T save_tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061
4062#ifndef NO_CONSOLE
Bram Moolenaar85a20022019-12-21 18:25:54 +01004063 // Don't output anything in silent mode ("ex -s")
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 if (silent_mode)
Bram Moolenaar85a20022019-12-21 18:25:54 +01004065 return dfltbutton; // return default option
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066#endif
4067
4068#ifdef FEAT_GUI_DIALOG
Bram Moolenaar85a20022019-12-21 18:25:54 +01004069 // When GUI is running and 'c' not in 'guioptions', use the GUI dialog
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
4071 {
Bram Moolenaar2d12c252022-06-13 21:42:45 +01004072 // --gui-dialog-file: write text to a file
4073 if (gui_dialog_log(title, message))
4074 c = dfltbutton;
4075 else
4076 c = gui_mch_dialog(type, title, message, buttons, dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004077 textfield, ex_cmd);
Bram Moolenaar85a20022019-12-21 18:25:54 +01004078 // avoid a hit-enter prompt without clearing the cmdline
Bram Moolenaar8de49e12009-02-11 17:47:54 +00004079 need_wait_return = FALSE;
4080 emsg_on_display = FALSE;
4081 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082
Bram Moolenaar85a20022019-12-21 18:25:54 +01004083 // Flush output to avoid that further messages and redrawing is done
4084 // in the wrong order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 out_flush();
4086 gui_mch_update();
4087
4088 return c;
4089 }
4090#endif
4091
4092 oldState = State;
Bram Moolenaar24959102022-05-07 20:01:16 +01004093 State = MODE_CONFIRM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095
Bram Moolenaar27321db2020-07-06 21:24:57 +02004096 // Ensure raw mode here.
4097 save_tmode = cur_tmode;
4098 settmode(TMODE_RAW);
4099
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 /*
4101 * Since we wait for a keypress, don't make the
4102 * user press RETURN as well afterwards.
4103 */
4104 ++no_wait_return;
4105 hotkeys = msg_show_console_dialog(message, buttons, dfltbutton);
4106
4107 if (hotkeys != NULL)
4108 {
4109 for (;;)
4110 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004111 // Get a typed character directly from the user.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 c = get_keystroke();
4113 switch (c)
4114 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004115 case CAR: // User accepts default option
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 case NL:
4117 retval = dfltbutton;
4118 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01004119 case Ctrl_C: // User aborts/cancels
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 case ESC:
4121 retval = 0;
4122 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01004123 default: // Could be a hotkey?
4124 if (c < 0) // special keys are ignored here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 continue;
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004126 if (c == ':' && ex_cmd)
4127 {
4128 retval = dfltbutton;
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02004129 ins_char_typebuf(':', 0);
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004130 break;
4131 }
4132
Bram Moolenaar85a20022019-12-21 18:25:54 +01004133 // Make the character lowercase, as chars in "hotkeys" are.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 c = MB_TOLOWER(c);
4135 retval = 1;
4136 for (i = 0; hotkeys[i]; ++i)
4137 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 if (has_mbyte)
4139 {
4140 if ((*mb_ptr2char)(hotkeys + i) == c)
4141 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004142 i += (*mb_ptr2len)(hotkeys + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 }
4144 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 if (hotkeys[i] == c)
4146 break;
4147 ++retval;
4148 }
4149 if (hotkeys[i])
4150 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01004151 // No hotkey match, so keep waiting
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 continue;
4153 }
4154 break;
4155 }
4156
4157 vim_free(hotkeys);
4158 }
4159
Bram Moolenaar27321db2020-07-06 21:24:57 +02004160 settmode(save_tmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 State = oldState;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 --no_wait_return;
4164 msg_end_prompt();
4165
4166 return retval;
4167}
4168
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169/*
4170 * Copy one character from "*from" to "*to", taking care of multi-byte
4171 * characters. Return the length of the character in bytes.
4172 */
4173 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004174copy_char(
4175 char_u *from,
4176 char_u *to,
Bram Moolenaar85a20022019-12-21 18:25:54 +01004177 int lowercase) // make character lower case
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 int len;
4180 int c;
4181
4182 if (has_mbyte)
4183 {
4184 if (lowercase)
4185 {
4186 c = MB_TOLOWER((*mb_ptr2char)(from));
4187 return (*mb_char2bytes)(c, to);
4188 }
4189 else
4190 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004191 len = (*mb_ptr2len)(from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 mch_memmove(to, from, (size_t)len);
4193 return len;
4194 }
4195 }
4196 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 {
4198 if (lowercase)
4199 *to = (char_u)TOLOWER_LOC(*from);
4200 else
4201 *to = *from;
4202 return 1;
4203 }
4204}
4205
4206/*
4207 * Format the dialog string, and display it at the bottom of
4208 * the screen. Return a string of hotkey chars (if defined) for
4209 * each 'button'. If a button has no hotkey defined, the first character of
4210 * the button is used.
4211 * The hotkeys can be multi-byte characters, but without combining chars.
4212 *
4213 * Returns an allocated string with hotkeys, or NULL for error.
4214 */
4215 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004216msg_show_console_dialog(
4217 char_u *message,
4218 char_u *buttons,
4219 int dfltbutton)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220{
4221 int len = 0;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01004222#define HOTK_LEN (has_mbyte ? MB_MAXBYTES : 1)
Bram Moolenaar85a20022019-12-21 18:25:54 +01004223 int lenhotkey = HOTK_LEN; // count first button
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 char_u *hotk = NULL;
4225 char_u *msgp = NULL;
4226 char_u *hotkp = NULL;
4227 char_u *r;
4228 int copy;
4229#define HAS_HOTKEY_LEN 30
4230 char_u has_hotkey[HAS_HOTKEY_LEN];
Bram Moolenaar85a20022019-12-21 18:25:54 +01004231 int first_hotkey = FALSE; // first char of button is hotkey
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 int idx;
4233
4234 has_hotkey[0] = FALSE;
4235
4236 /*
4237 * First loop: compute the size of memory to allocate.
4238 * Second loop: copy to the allocated memory.
4239 */
4240 for (copy = 0; copy <= 1; ++copy)
4241 {
4242 r = buttons;
4243 idx = 0;
4244 while (*r)
4245 {
4246 if (*r == DLG_BUTTON_SEP)
4247 {
4248 if (copy)
4249 {
4250 *msgp++ = ',';
Bram Moolenaar85a20022019-12-21 18:25:54 +01004251 *msgp++ = ' '; // '\n' -> ', '
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252
Bram Moolenaar85a20022019-12-21 18:25:54 +01004253 // advance to next hotkey and set default hotkey
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 if (has_mbyte)
Bram Moolenaar6a516062007-07-05 08:11:42 +00004255 hotkp += STRLEN(hotkp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 ++hotkp;
Bram Moolenaar6a516062007-07-05 08:11:42 +00004258 hotkp[copy_char(r + 1, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 if (dfltbutton)
4260 --dfltbutton;
4261
Bram Moolenaar85a20022019-12-21 18:25:54 +01004262 // If no hotkey is specified first char is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 if (idx < HAS_HOTKEY_LEN - 1 && !has_hotkey[++idx])
4264 first_hotkey = TRUE;
4265 }
4266 else
4267 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004268 len += 3; // '\n' -> ', '; 'x' -> '(x)'
4269 lenhotkey += HOTK_LEN; // each button needs a hotkey
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 if (idx < HAS_HOTKEY_LEN - 1)
4271 has_hotkey[++idx] = FALSE;
4272 }
4273 }
4274 else if (*r == DLG_HOTKEY_CHAR || first_hotkey)
4275 {
4276 if (*r == DLG_HOTKEY_CHAR)
4277 ++r;
4278 first_hotkey = FALSE;
4279 if (copy)
4280 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004281 if (*r == DLG_HOTKEY_CHAR) // '&&a' -> '&a'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 *msgp++ = *r;
4283 else
4284 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004285 // '&a' -> '[a]'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 *msgp++ = (dfltbutton == 1) ? '[' : '(';
4287 msgp += copy_char(r, msgp, FALSE);
4288 *msgp++ = (dfltbutton == 1) ? ']' : ')';
4289
Bram Moolenaar85a20022019-12-21 18:25:54 +01004290 // redefine hotkey
Bram Moolenaar6a516062007-07-05 08:11:42 +00004291 hotkp[copy_char(r, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 }
4293 }
4294 else
4295 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004296 ++len; // '&a' -> '[a]'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 if (idx < HAS_HOTKEY_LEN - 1)
4298 has_hotkey[idx] = TRUE;
4299 }
4300 }
4301 else
4302 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004303 // everything else copy literally
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 if (copy)
4305 msgp += copy_char(r, msgp, FALSE);
4306 }
4307
Bram Moolenaar85a20022019-12-21 18:25:54 +01004308 // advance to the next character
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004309 MB_PTR_ADV(r);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 }
4311
4312 if (copy)
4313 {
4314 *msgp++ = ':';
4315 *msgp++ = ' ';
4316 *msgp = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 }
4318 else
4319 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004320 len += (int)(STRLEN(message)
Bram Moolenaar85a20022019-12-21 18:25:54 +01004321 + 2 // for the NL's
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004322 + STRLEN(buttons)
Bram Moolenaar85a20022019-12-21 18:25:54 +01004323 + 3); // for the ": " and NUL
4324 lenhotkey++; // for the NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325
Bram Moolenaar85a20022019-12-21 18:25:54 +01004326 // If no hotkey is specified first char is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 if (!has_hotkey[0])
4328 {
4329 first_hotkey = TRUE;
Bram Moolenaar85a20022019-12-21 18:25:54 +01004330 len += 2; // "x" -> "[x]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 }
4332
4333 /*
4334 * Now allocate and load the strings
4335 */
4336 vim_free(confirm_msg);
4337 confirm_msg = alloc(len);
4338 if (confirm_msg == NULL)
4339 return NULL;
4340 *confirm_msg = NUL;
4341 hotk = alloc(lenhotkey);
4342 if (hotk == NULL)
4343 return NULL;
4344
4345 *confirm_msg = '\n';
4346 STRCPY(confirm_msg + 1, message);
4347
4348 msgp = confirm_msg + 1 + STRLEN(message);
4349 hotkp = hotk;
4350
Bram Moolenaar85a20022019-12-21 18:25:54 +01004351 // Define first default hotkey. Keep the hotkey string NUL
4352 // terminated to avoid reading past the end.
Bram Moolenaar6a516062007-07-05 08:11:42 +00004353 hotkp[copy_char(buttons, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354
Bram Moolenaar85a20022019-12-21 18:25:54 +01004355 // Remember where the choices start, displaying starts here when
4356 // "hotkp" typed at the more prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357 confirm_msg_tail = msgp;
4358 *msgp++ = '\n';
4359 }
4360 }
4361
4362 display_confirm_msg();
4363 return hotk;
4364}
4365
4366/*
4367 * Display the ":confirm" message. Also called when screen resized.
4368 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02004369 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004370display_confirm_msg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371{
Bram Moolenaar85a20022019-12-21 18:25:54 +01004372 // avoid that 'q' at the more prompt truncates the message here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 ++confirm_msg_used;
4374 if (confirm_msg != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004375 msg_puts_attr((char *)confirm_msg, HL_ATTR(HLF_M));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 --confirm_msg_used;
4377}
4378
Bram Moolenaar85a20022019-12-21 18:25:54 +01004379#endif // FEAT_CON_DIALOG
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380
4381#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4382
4383 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004384vim_dialog_yesno(
4385 int type,
4386 char_u *title,
4387 char_u *message,
4388 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389{
4390 if (do_dialog(type,
4391 title == NULL ? (char_u *)_("Question") : title,
4392 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004393 (char_u *)_("&Yes\n&No"), dflt, NULL, FALSE) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 return VIM_YES;
4395 return VIM_NO;
4396}
4397
4398 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004399vim_dialog_yesnocancel(
4400 int type,
4401 char_u *title,
4402 char_u *message,
4403 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404{
4405 switch (do_dialog(type,
4406 title == NULL ? (char_u *)_("Question") : title,
4407 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004408 (char_u *)_("&Yes\n&No\n&Cancel"), dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 {
4410 case 1: return VIM_YES;
4411 case 2: return VIM_NO;
4412 }
4413 return VIM_CANCEL;
4414}
4415
4416 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004417vim_dialog_yesnoallcancel(
4418 int type,
4419 char_u *title,
4420 char_u *message,
4421 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422{
4423 switch (do_dialog(type,
4424 title == NULL ? (char_u *)"Question" : title,
4425 message,
4426 (char_u *)_("&Yes\n&No\nSave &All\n&Discard All\n&Cancel"),
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004427 dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 {
4429 case 1: return VIM_YES;
4430 case 2: return VIM_NO;
4431 case 3: return VIM_ALL;
4432 case 4: return VIM_DISCARDALL;
4433 }
4434 return VIM_CANCEL;
4435}
4436
Bram Moolenaar85a20022019-12-21 18:25:54 +01004437#endif // FEAT_GUI_DIALOG || FEAT_CON_DIALOG