blob: 0fb0013d5ac1075c6d032c75d4634647cbd2668b [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);
Christian Brabandt51d4d842024-12-06 17:26:25 +010019static void check_msg_hist(void);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010020static void hit_return_msg(void);
21static void msg_home_replace_attr(char_u *fname, int attr);
Bram Moolenaar32526b32019-01-19 17:43:09 +010022static void msg_puts_attr_len(char *str, int maxlen, int attr);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010023static void msg_puts_display(char_u *str, int maxlen, int attr, int recurse);
24static void msg_scroll_up(void);
25static void inc_msg_scrolled(void);
26static void store_sb_text(char_u **sb_str, char_u *s, int attr, int *sb_col, int finish);
27static void t_puts(int *t_col, char_u *t_s, char_u *s, int attr);
28static void msg_puts_printf(char_u *str, int maxlen);
29static int do_more_prompt(int typed_char);
30static void msg_screen_putchar(int c, int attr);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020031static void msg_moremsg(int full);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010032static int msg_check_screen(void);
33static void redir_write(char_u *s, int maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000034#ifdef FEAT_CON_DIALOG
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010035static char_u *msg_show_console_dialog(char_u *message, char_u *buttons, int dfltbutton);
Bram Moolenaar85a20022019-12-21 18:25:54 +010036static int confirm_msg_used = FALSE; // displaying confirm_msg
37static char_u *confirm_msg = NULL; // ":confirm" message
38static char_u *confirm_msg_tail; // tail of confirm_msg
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020039static void display_confirm_msg(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000040#endif
Bram Moolenaar3b8c7082022-11-30 20:20:56 +000041#ifdef FEAT_EVAL
Bram Moolenaar958dc692016-12-01 15:34:12 +010042static int emsg_to_channel_log = FALSE;
43#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000044
45struct msg_hist
46{
47 struct msg_hist *next;
48 char_u *msg;
49 int attr;
50};
51
52static struct msg_hist *first_msg_hist = NULL;
53static struct msg_hist *last_msg_hist = NULL;
54static int msg_hist_len = 0;
Christian Brabandt51d4d842024-12-06 17:26:25 +010055static int msg_hist_max = 500; // The default max value is 500
56
57// flags obtained from the 'messagesopt' option
58#define MESSAGES_HIT_ENTER 0x001
59#define MESSAGES_WAIT 0x002
60#define MESSAGES_HISTORY 0x004
61
62// args in 'messagesopt' option
63#define MESSAGES_OPT_HIT_ENTER "hit-enter"
64#define MESSAGES_OPT_WAIT "wait:"
65#define MESSAGES_OPT_HISTORY "history:"
66
67// The default is "hit-enter,history:500"
68static int msg_flags = MESSAGES_HIT_ENTER | MESSAGES_HISTORY;
69static int msg_wait = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000070
Bram Moolenaarb5b5b892011-09-14 15:39:29 +020071static FILE *verbose_fd = NULL;
72static int verbose_did_open = FALSE;
73
Christian Brabandt45e07042024-11-11 20:52:55 +010074static int did_warn_clipboard = FALSE;
Christian Brabandt45e07042024-11-11 20:52:55 +010075
Bram Moolenaar071d4272004-06-13 20:20:40 +000076/*
77 * When writing messages to the screen, there are many different situations.
78 * A number of variables is used to remember the current state:
79 * msg_didany TRUE when messages were written since the last time the
80 * user reacted to a prompt.
81 * Reset: After hitting a key for the hit-return prompt,
82 * hitting <CR> for the command line or input().
83 * Set: When any message is written to the screen.
84 * msg_didout TRUE when something was written to the current line.
85 * Reset: When advancing to the next line, when the current
86 * text can be overwritten.
87 * Set: When any message is written to the screen.
88 * msg_nowait No extra delay for the last drawn message.
89 * Used in normal_cmd() before the mode message is drawn.
90 * emsg_on_display There was an error message recently. Indicates that there
91 * should be a delay before redrawing.
92 * msg_scroll The next message should not overwrite the current one.
93 * msg_scrolled How many lines the screen has been scrolled (because of
94 * messages). Used in update_screen() to scroll the screen
95 * back. Incremented each time the screen scrolls a line.
96 * msg_scrolled_ign TRUE when msg_scrolled is non-zero and msg_puts_attr()
97 * writes something without scrolling should not make
98 * need_wait_return to be set. This is a hack to make ":ts"
99 * work without an extra prompt.
100 * lines_left Number of lines available for messages before the
Bram Moolenaar5d6f75e2011-12-30 14:14:29 +0100101 * more-prompt is to be given. -1 when not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000102 * need_wait_return TRUE when the hit-return prompt is needed.
103 * Reset: After giving the hit-return prompt, when the user
104 * has answered some other prompt.
105 * Set: When the ruler or typeahead display is overwritten,
106 * scrolling the screen for some message.
107 * keep_msg Message to be displayed after redrawing the screen, in
108 * main_loop().
109 * This is an allocated string or NULL when not used.
110 */
111
112/*
113 * msg(s) - displays the string 's' on the status line
114 * When terminal not initialized (yet) mch_errmsg(..) is used.
Bram Moolenaar13608d82022-08-29 15:06:50 +0100115 * return TRUE if wait_return() not called
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116 */
117 int
Bram Moolenaar32526b32019-01-19 17:43:09 +0100118msg(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119{
120 return msg_attr_keep(s, 0, FALSE);
121}
122
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000123/*
124 * Like msg() but keep it silent when 'verbosefile' is set.
125 */
126 int
Bram Moolenaar32526b32019-01-19 17:43:09 +0100127verb_msg(char *s)
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000128{
129 int n;
130
131 verbose_enter();
132 n = msg_attr_keep(s, 0, FALSE);
133 verbose_leave();
134
135 return n;
136}
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000137
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138 int
Bram Moolenaar32526b32019-01-19 17:43:09 +0100139msg_attr(char *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140{
141 return msg_attr_keep(s, attr, FALSE);
142}
143
144 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100145msg_attr_keep(
Bram Moolenaar32526b32019-01-19 17:43:09 +0100146 char *s,
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100147 int attr,
Bram Moolenaar85a20022019-12-21 18:25:54 +0100148 int keep) // TRUE: set keep_msg if it doesn't scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149{
150 static int entered = 0;
151 int retval;
152 char_u *buf = NULL;
153
Bram Moolenaar85a20022019-12-21 18:25:54 +0100154 // Skip messages not matching ":filter pattern".
155 // Don't filter when there is an error.
Bram Moolenaar32526b32019-01-19 17:43:09 +0100156 if (!emsg_on_display && message_filtered((char_u *)s))
Bram Moolenaar7b668e82016-08-23 23:51:21 +0200157 return TRUE;
158
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159#ifdef FEAT_EVAL
160 if (attr == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100161 set_vim_var_string(VV_STATUSMSG, (char_u *)s, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162#endif
163
164 /*
165 * It is possible that displaying a messages causes a problem (e.g.,
166 * when redrawing the window), which causes another message, etc.. To
167 * break this loop, limit the recursiveness to 3 levels.
168 */
169 if (entered >= 3)
170 return TRUE;
171 ++entered;
172
Bram Moolenaar85a20022019-12-21 18:25:54 +0100173 // Add message to history (unless it's a repeated kept message or a
174 // truncated message)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100175 if ((char_u *)s != keep_msg
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176 || (*s != '<'
177 && last_msg_hist != NULL
178 && last_msg_hist->msg != NULL
179 && STRCMP(s, last_msg_hist->msg)))
Bram Moolenaar32526b32019-01-19 17:43:09 +0100180 add_msg_hist((char_u *)s, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181
Bram Moolenaar4c5678f2022-11-30 18:12:19 +0000182#ifdef FEAT_EVAL
Bram Moolenaar958dc692016-12-01 15:34:12 +0100183 if (emsg_to_channel_log)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100184 // Write message in the channel log.
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +0000185 ch_log(NULL, "ERROR: %s", s);
Bram Moolenaar958dc692016-12-01 15:34:12 +0100186#endif
187
Bram Moolenaar85a20022019-12-21 18:25:54 +0100188 // Truncate the message if needed.
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000189 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +0100190 buf = msg_strtrunc((char_u *)s, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191 if (buf != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100192 s = (char *)buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193
Bram Moolenaar32526b32019-01-19 17:43:09 +0100194 msg_outtrans_attr((char_u *)s, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195 msg_clr_eos();
196 retval = msg_end();
197
Bram Moolenaar32526b32019-01-19 17:43:09 +0100198 if (keep && retval && vim_strsize((char_u *)s)
199 < (int)(Rows - cmdline_row - 1) * Columns + sc_col)
200 set_keep_msg((char_u *)s, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201
Rob Pilling726f7f92022-01-20 14:44:38 +0000202 need_fileinfo = FALSE;
203
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204 vim_free(buf);
205 --entered;
206 return retval;
207}
208
209/*
210 * Truncate a string such that it can be printed without causing a scroll.
211 * Returns an allocated string or NULL when no truncating is done.
212 */
213 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100214msg_strtrunc(
215 char_u *s,
Bram Moolenaar85a20022019-12-21 18:25:54 +0100216 int force) // always truncate
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217{
218 char_u *buf = NULL;
219 int len;
220 int room;
221
Bram Moolenaar85a20022019-12-21 18:25:54 +0100222 // May truncate message to avoid a hit-return prompt
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000223 if ((!msg_scroll && !need_wait_return && shortmess(SHM_TRUNCALL)
224 && !exmode_active && msg_silent == 0) || force)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225 {
226 len = vim_strsize(s);
Bram Moolenaar35a4fbc2022-08-28 14:39:53 +0100227 if (msg_scrolled != 0
228#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaara2a89732022-08-31 14:46:18 +0100229 || in_echowindow
Bram Moolenaar35a4fbc2022-08-28 14:39:53 +0100230#endif
231 )
Bram Moolenaar85a20022019-12-21 18:25:54 +0100232 // Use all the columns.
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000233 room = (int)(Rows - msg_row) * Columns - 1;
234 else
Bram Moolenaar85a20022019-12-21 18:25:54 +0100235 // Use up to 'showcmd' column.
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000236 room = (int)(Rows - msg_row - 1) * Columns + sc_col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237 if (len > room && room > 0)
238 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239 if (enc_utf8)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100240 // may have up to 18 bytes per cell (6 per char, up to two
241 // composing chars)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100242 len = (room + 2) * 18;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243 else if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100244 // may have up to 2 bytes per cell for euc-jp
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100245 len = (room + 2) * 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246 else
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100247 len = room + 2;
248 buf = alloc(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249 if (buf != NULL)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100250 trunc_string(s, buf, room, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251 }
252 }
253 return buf;
254}
255
256/*
257 * Truncate a string "s" to "buf" with cell width "room".
258 * "s" and "buf" may be equal.
259 */
260 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100261trunc_string(
262 char_u *s,
263 char_u *buf,
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200264 int room_in,
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100265 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266{
Bram Moolenaar85a20022019-12-21 18:25:54 +0100267 size_t room = room_in - 3; // "..." takes 3 chars
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200268 size_t half;
269 size_t len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270 int e;
271 int i;
272 int n;
273
Bram Moolenaar62818152021-02-14 15:37:30 +0100274 if (*s == NUL)
275 {
276 if (buflen > 0)
277 *buf = NUL;
278 return;
279 }
280
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200281 if (room_in < 3)
282 room = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000283 half = room / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284
Bram Moolenaar85a20022019-12-21 18:25:54 +0100285 // First part: Start of the string.
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100286 for (e = 0; len < half && e < buflen; ++e)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000287 {
288 if (s[e] == NUL)
289 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100290 // text fits without truncating!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291 buf[e] = NUL;
292 return;
293 }
294 n = ptr2cells(s + e);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200295 if (len + n > half)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000296 break;
297 len += n;
298 buf[e] = s[e];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000299 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000300 for (n = (*mb_ptr2len)(s + e); --n > 0; )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100302 if (++e == buflen)
303 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304 buf[e] = s[e];
305 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306 }
307
Bram Moolenaar85a20022019-12-21 18:25:54 +0100308 // Last part: End of the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309 i = e;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000310 if (enc_dbcs != 0)
311 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100312 // For DBCS going backwards in a string is slow, but
313 // computing the cell width isn't too slow: go forward
314 // until the rest fits.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315 n = vim_strsize(s + i);
316 while (len + n > room)
317 {
318 n -= ptr2cells(s + i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000319 i += (*mb_ptr2len)(s + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320 }
321 }
322 else if (enc_utf8)
323 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100324 // For UTF-8 we can go backwards easily.
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000325 half = i = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326 for (;;)
327 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000328 do
Bram Moolenaarace95982017-03-29 17:30:27 +0200329 half = half - utf_head_off(s, s + half - 1) - 1;
Bram Moolenaarb9644432016-07-19 12:33:44 +0200330 while (half > 0 && utf_iscomposing(utf_ptr2char(s + half)));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331 n = ptr2cells(s + half);
Bram Moolenaarb9644432016-07-19 12:33:44 +0200332 if (len + n > room || half == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333 break;
334 len += n;
Bram Moolenaara5c0cc12016-07-30 16:40:39 +0200335 i = (int)half;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336 }
337 }
338 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339 {
Bram Moolenaard0337e32019-12-30 17:55:34 +0100340 for (i = (int)STRLEN(s);
341 i - 1 >= 0 && len + (n = ptr2cells(s + i - 1)) <= room; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342 len += n;
343 }
344
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200345
346 if (i <= e + 3)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100347 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100348 // text fits without truncating
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200349 if (s != buf)
350 {
351 len = STRLEN(s);
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200352 if (len >= (size_t)buflen)
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200353 len = buflen - 1;
354 len = len - e + 1;
355 if (len < 1)
356 buf[e - 1] = NUL;
357 else
358 mch_memmove(buf + e, s + e, len);
359 }
360 }
361 else if (e + 3 < buflen)
362 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100363 // set the middle and copy the last part
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100364 mch_memmove(buf + e, "...", (size_t)3);
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200365 len = STRLEN(s + i) + 1;
366 if (len >= (size_t)buflen - e - 3)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100367 len = buflen - e - 3 - 1;
368 mch_memmove(buf + e + 3, s + i, len);
369 buf[e + 3 + len - 1] = NUL;
370 }
371 else
372 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100373 // can't fit in the "...", just truncate it
Christian Brabandt3bd7fa12023-10-02 20:59:08 +0200374 buf[buflen - 1] = NUL;
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100375 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376}
377
378/*
379 * Automatic prototype generation does not understand this function.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100380 * Note: Caller of smsg() and smsg_attr() must check the resulting string is
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381 * shorter than IOSIZE!!!
382 */
383#ifndef PROTO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100385int vim_snprintf(char *str, size_t str_m, const char *fmt, ...);
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000386
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100388smsg(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((char *)s);
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((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403}
404
405 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100406smsg_attr(int attr, const char *s, ...)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407{
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((char *)s, attr);
413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414
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((char *)IObuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421}
422
Bram Moolenaare0429682018-07-01 16:44:03 +0200423 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100424smsg_attr_keep(int attr, const char *s, ...)
Bram Moolenaare0429682018-07-01 16:44:03 +0200425{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200426 if (IObuff == NULL)
427 {
428 // Very early in initialisation and already something wrong, just
429 // give the raw message so the user at least gets a hint.
430 return msg_attr_keep((char *)s, attr, TRUE);
431 }
Bram Moolenaare0429682018-07-01 16:44:03 +0200432
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000433 va_list arglist;
434
435 va_start(arglist, s);
436 vim_vsnprintf((char *)IObuff, IOSIZE, s, arglist);
437 va_end(arglist);
438 return msg_attr_keep((char *)IObuff, attr, TRUE);
Bram Moolenaare0429682018-07-01 16:44:03 +0200439}
440
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441#endif
442
443/*
444 * Remember the last sourcing name/lnum used in an error message, so that it
445 * isn't printed each time when it didn't change.
446 */
447static int last_sourcing_lnum = 0;
448static char_u *last_sourcing_name = NULL;
449
450/*
451 * Reset the last used sourcing name/lnum. Makes sure it is displayed again
452 * for the next error message;
453 */
Bram Moolenaar4eec5ec2005-06-26 22:26:21 +0000454 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100455reset_last_sourcing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000456{
Bram Moolenaard23a8232018-02-10 18:45:26 +0100457 VIM_CLEAR(last_sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458 last_sourcing_lnum = 0;
459}
460
461/*
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100462 * Return TRUE if "SOURCING_NAME" differs from "last_sourcing_name".
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000463 */
464 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100465other_sourcing_name(void)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000466{
Bram Moolenaar0bd8d052021-11-25 13:39:28 +0000467 if (HAVE_SOURCING_INFO && SOURCING_NAME != NULL)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000468 {
469 if (last_sourcing_name != NULL)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100470 return STRCMP(SOURCING_NAME, last_sourcing_name) != 0;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000471 return TRUE;
472 }
473 return FALSE;
474}
475
476/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477 * Get the message about the source, as used for an error message.
478 * Returns an allocated string with room for one more character.
479 * Returns NULL when no message is to be given.
480 */
481 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100482get_emsg_source(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483{
484 char_u *Buf, *p;
485
Bram Moolenaar0bd8d052021-11-25 13:39:28 +0000486 if (HAVE_SOURCING_INFO && SOURCING_NAME != NULL && other_sourcing_name())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487 {
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200488 char_u *sname = estack_sfile(ESTACK_NONE);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100489 char_u *tofree = sname;
490
491 if (sname == NULL)
492 sname = SOURCING_NAME;
493
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +0200494#ifdef FEAT_EVAL
495 if (estack_compiling)
496 p = (char_u *)_("Error detected while compiling %s:");
497 else
498#endif
499 p = (char_u *)_("Error detected while processing %s:");
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100500 Buf = alloc(STRLEN(sname) + STRLEN(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501 if (Buf != NULL)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100502 sprintf((char *)Buf, (char *)p, sname);
503 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504 return Buf;
505 }
506 return NULL;
507}
508
509/*
510 * Get the message about the source lnum, as used for an error message.
511 * Returns an allocated string with room for one more character.
512 * Returns NULL when no message is to be given.
513 */
514 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100515get_emsg_lnum(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516{
517 char_u *Buf, *p;
518
Bram Moolenaar85a20022019-12-21 18:25:54 +0100519 // lnum is 0 when executing a command from the command line
520 // argument, we don't want a line number then
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100521 if (SOURCING_NAME != NULL
522 && (other_sourcing_name() || SOURCING_LNUM != last_sourcing_lnum)
523 && SOURCING_LNUM != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524 {
525 p = (char_u *)_("line %4ld:");
Bram Moolenaar964b3742019-05-24 18:54:09 +0200526 Buf = alloc(STRLEN(p) + 20);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527 if (Buf != NULL)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100528 sprintf((char *)Buf, (char *)p, (long)SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 return Buf;
530 }
531 return NULL;
532}
533
534/*
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000535 * Display name and line number for the source of an error.
536 * Remember the file name and line number, so that for the next error the info
537 * is only displayed if it changed.
538 */
539 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100540msg_source(int attr)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000541{
542 char_u *p;
Bram Moolenaarba8c9262021-11-25 14:43:18 +0000543 static int recursive = FALSE;
544
545 // Bail out if something called here causes an error.
546 if (recursive)
547 return;
548 recursive = TRUE;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000549
550 ++no_wait_return;
551 p = get_emsg_source();
552 if (p != NULL)
553 {
zeertzjqbdedd2b2022-09-20 12:45:15 +0100554 msg_scroll = TRUE; // this will take more than one line
Bram Moolenaar32526b32019-01-19 17:43:09 +0100555 msg_attr((char *)p, attr);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000556 vim_free(p);
557 }
558 p = get_emsg_lnum();
559 if (p != NULL)
560 {
Bram Moolenaar32526b32019-01-19 17:43:09 +0100561 msg_attr((char *)p, HL_ATTR(HLF_N));
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000562 vim_free(p);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100563 last_sourcing_lnum = SOURCING_LNUM; // only once for each line
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000564 }
565
Bram Moolenaar85a20022019-12-21 18:25:54 +0100566 // remember the last sourcing name printed, also when it's empty
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100567 if (SOURCING_NAME == NULL || other_sourcing_name())
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000568 {
Bram Moolenaarba8c9262021-11-25 14:43:18 +0000569 VIM_CLEAR(last_sourcing_name);
570 if (SOURCING_NAME != NULL)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100571 last_sourcing_name = vim_strsave(SOURCING_NAME);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000572 }
573 --no_wait_return;
Bram Moolenaarba8c9262021-11-25 14:43:18 +0000574
575 recursive = FALSE;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000576}
577
578/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000579 * Return TRUE if not giving error messages right now:
580 * If "emsg_off" is set: no error messages at the moment.
581 * If "msg" is in 'debug': do error message but without side effects.
582 * If "emsg_skip" is set: never do error messages.
583 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +0200584 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100585emsg_not_now(void)
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000586{
587 if ((emsg_off > 0 && vim_strchr(p_debug, 'm') == NULL
588 && vim_strchr(p_debug, 't') == NULL)
589#ifdef FEAT_EVAL
590 || emsg_skip > 0
591#endif
592 )
593 return TRUE;
594 return FALSE;
595}
596
Bram Moolenaar5a66dfb2017-03-01 20:40:39 +0100597#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100598static garray_T ignore_error_list = GA_EMPTY;
599
600 void
601ignore_error_for_testing(char_u *error)
602{
603 if (ignore_error_list.ga_itemsize == 0)
604 ga_init2(&ignore_error_list, sizeof(char_u *), 1);
605
Bram Moolenaar461a7fc2018-12-22 13:28:07 +0100606 if (STRCMP("RESET", error) == 0)
607 ga_clear_strings(&ignore_error_list);
608 else
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000609 ga_copy_string(&ignore_error_list, error);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100610}
611
612 static int
Bram Moolenaar097c5372023-05-24 21:02:24 +0100613ignore_error(const char *msg)
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100614{
615 int i;
616
617 for (i = 0; i < ignore_error_list.ga_len; ++i)
Bram Moolenaar097c5372023-05-24 21:02:24 +0100618 if (strstr(msg,
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100619 (char *)((char_u **)(ignore_error_list.ga_data))[i]) != NULL)
620 return TRUE;
621 return FALSE;
622}
623#endif
624
Bram Moolenaarb869c0d2016-07-20 00:10:51 +0200625#if !defined(HAVE_STRERROR) || defined(PROTO)
626/*
627 * Replacement for perror() that behaves more or less like emsg() was called.
Bram Moolenaar53989552019-12-23 22:59:18 +0100628 * v:errmsg will be set and called_emsg will be incremented.
Bram Moolenaarb869c0d2016-07-20 00:10:51 +0200629 */
630 void
631do_perror(char *msg)
632{
633 perror(msg);
634 ++emsg_silent;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100635 emsg(msg);
Bram Moolenaarb869c0d2016-07-20 00:10:51 +0200636 --emsg_silent;
637}
638#endif
639
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000640/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100641 * emsg_core() - display an error message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 *
643 * Rings the bell, if appropriate, and calls message() to do the real work
644 * When terminal not initialized (yet) mch_errmsg(..) is used.
645 *
Bram Moolenaar13608d82022-08-29 15:06:50 +0100646 * Return TRUE if wait_return() not called.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100647 * Note: caller must check 'emsg_not_now()' before calling this.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100649 static int
Bram Moolenaar097c5372023-05-24 21:02:24 +0100650emsg_core(const char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651{
652 int attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653 char_u *p;
Bram Moolenaar958dc692016-12-01 15:34:12 +0100654 int r;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655#ifdef FEAT_EVAL
656 int ignore = FALSE;
657 int severe;
658#endif
659
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100660#ifdef FEAT_EVAL
Bram Moolenaar85a20022019-12-21 18:25:54 +0100661 // When testing some errors are turned into a normal message.
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100662 if (ignore_error(s))
Bram Moolenaar85a20022019-12-21 18:25:54 +0100663 // don't call msg() if it results in a dialog
Bram Moolenaar32526b32019-01-19 17:43:09 +0100664 return msg_use_printf() ? FALSE : msg((char *)s);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100665#endif
666
Bram Moolenaar53989552019-12-23 22:59:18 +0100667 ++called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669#ifdef FEAT_EVAL
Bram Moolenaar85a20022019-12-21 18:25:54 +0100670 // If "emsg_severe" is TRUE: When an error exception is to be thrown,
671 // prefer this message over previous messages for the same command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 severe = emsg_severe;
673 emsg_severe = FALSE;
674#endif
675
Bram Moolenaar57657d82006-04-21 22:12:41 +0000676 if (!emsg_off || vim_strchr(p_debug, 't') != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 {
678#ifdef FEAT_EVAL
679 /*
680 * Cause a throw of an error exception if appropriate. Don't display
681 * the error message in this case. (If no matching catch clause will
682 * be found, the message will be displayed later on.) "ignore" is set
683 * when the message should be ignored completely (used for the
684 * interrupt message).
685 */
Bram Moolenaar097c5372023-05-24 21:02:24 +0100686 if (cause_errthrow((char_u *)s, severe, &ignore) == TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 {
688 if (!ignore)
Bram Moolenaar76a63452018-11-28 20:38:37 +0100689 ++did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 return TRUE;
691 }
692
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100693 if (in_assert_fails && emsg_assert_fails_msg == NULL)
Bram Moolenaar1d634542020-08-18 13:41:50 +0200694 {
Bram Moolenaar097c5372023-05-24 21:02:24 +0100695 emsg_assert_fails_msg = vim_strsave((char_u *)s);
Bram Moolenaar1d634542020-08-18 13:41:50 +0200696 emsg_assert_fails_lnum = SOURCING_LNUM;
Bram Moolenaar9bd5d872020-09-06 21:47:48 +0200697 vim_free(emsg_assert_fails_context);
698 emsg_assert_fails_context = vim_strsave(
699 SOURCING_NAME == NULL ? (char_u *)"" : SOURCING_NAME);
Bram Moolenaar1d634542020-08-18 13:41:50 +0200700 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200701
Bram Moolenaar85a20022019-12-21 18:25:54 +0100702 // set "v:errmsg", also when using ":silent! cmd"
Bram Moolenaar097c5372023-05-24 21:02:24 +0100703 set_vim_var_string(VV_ERRMSG, (char_u *)s, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704#endif
705
706 /*
Bram Moolenaara7241f52008-06-24 20:39:31 +0000707 * When using ":silent! cmd" ignore error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 * But do write it to the redirection file.
709 */
710 if (emsg_silent != 0)
711 {
Bram Moolenaar599410c2021-04-10 14:03:43 +0200712#ifdef FEAT_EVAL
713 ++did_emsg_silent;
714#endif
Bram Moolenaar79815f12016-07-09 17:07:29 +0200715 if (emsg_noredir == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 {
Bram Moolenaar79815f12016-07-09 17:07:29 +0200717 msg_start();
718 p = get_emsg_source();
719 if (p != NULL)
720 {
721 STRCAT(p, "\n");
722 redir_write(p, -1);
723 vim_free(p);
724 }
725 p = get_emsg_lnum();
726 if (p != NULL)
727 {
728 STRCAT(p, "\n");
729 redir_write(p, -1);
730 vim_free(p);
731 }
Bram Moolenaar097c5372023-05-24 21:02:24 +0100732 redir_write((char_u *)s, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +0100734#ifdef FEAT_EVAL
735 // Only increment did_emsg_def when :silent! wasn't used inside the
736 // :def function.
737 if (emsg_silent == emsg_silent_def)
738 ++did_emsg_def;
739#endif
Bram Moolenaar4c5678f2022-11-30 18:12:19 +0000740#ifdef FEAT_EVAL
Bram Moolenaar097c5372023-05-24 21:02:24 +0100741 ch_log(NULL, "ERROR silent: %s", s);
Bram Moolenaar958dc692016-12-01 15:34:12 +0100742#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743 return TRUE;
744 }
745
Bram Moolenaar2b7bc562017-01-14 19:24:52 +0100746 ex_exitval = 1;
747
Bram Moolenaar85a20022019-12-21 18:25:54 +0100748 // Reset msg_silent, an error causes messages to be switched back on.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 msg_silent = 0;
750 cmd_silent = FALSE;
751
Bram Moolenaar85a20022019-12-21 18:25:54 +0100752 if (global_busy) // break :global command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 ++global_busy;
754
755 if (p_eb)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100756 beep_flush(); // also includes flush_buffers()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 else
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200758 flush_buffers(FLUSH_MINIMAL); // flush internal buffers
Bram Moolenaar76a63452018-11-28 20:38:37 +0100759 ++did_emsg; // flag for DoOneCmd()
Bram Moolenaare723c422017-09-06 23:40:10 +0200760#ifdef FEAT_EVAL
Bram Moolenaar88c89c72021-08-14 14:01:05 +0200761 ++uncaught_emsg;
Bram Moolenaare723c422017-09-06 23:40:10 +0200762#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763 }
764
Bram Moolenaar878e1d22022-08-28 17:53:23 +0100765#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaara2a89732022-08-31 14:46:18 +0100766 if (!in_echowindow)
Bram Moolenaar878e1d22022-08-28 17:53:23 +0100767#endif
768 emsg_on_display = TRUE; // remember there is an error message
769
770 attr = HL_ATTR(HLF_E); // set highlight mode for error messages
Bram Moolenaarbb15b652005-10-03 21:52:09 +0000771 if (msg_scrolled != 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100772 need_wait_return = TRUE; // needed in case emsg() is called after
Bram Moolenaar13608d82022-08-29 15:06:50 +0100773 // wait_return() has reset need_wait_return
Bram Moolenaar85a20022019-12-21 18:25:54 +0100774 // and a redraw is expected because
775 // msg_scrolled is non-zero
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776
Bram Moolenaar958dc692016-12-01 15:34:12 +0100777#ifdef FEAT_JOB_CHANNEL
778 emsg_to_channel_log = TRUE;
779#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 /*
781 * Display name and line number for the source of the error.
782 */
zeertzjqbdedd2b2022-09-20 12:45:15 +0100783 msg_scroll = TRUE;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000784 msg_source(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785
786 /*
787 * Display the error message itself.
788 */
Bram Moolenaar85a20022019-12-21 18:25:54 +0100789 msg_nowait = FALSE; // wait for this msg
Bram Moolenaar32526b32019-01-19 17:43:09 +0100790 r = msg_attr((char *)s, attr);
Bram Moolenaar958dc692016-12-01 15:34:12 +0100791
792#ifdef FEAT_JOB_CHANNEL
793 emsg_to_channel_log = FALSE;
794#endif
795 return r;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796}
797
798/*
Bram Moolenaar097c5372023-05-24 21:02:24 +0100799 * Print error message "s". Should already be translated.
800 * Return TRUE if wait_return() not called.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 */
802 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100803emsg(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804{
Bram Moolenaar85a20022019-12-21 18:25:54 +0100805 // Skip this if not giving error messages at the moment.
Bram Moolenaar097c5372023-05-24 21:02:24 +0100806 if (emsg_not_now())
807 return TRUE;
808
809 return emsg_core(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810}
811
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100812#ifndef PROTO // manual proto with __attribute__
Bram Moolenaar95f09602016-11-10 20:01:45 +0100813/*
Bram Moolenaar097c5372023-05-24 21:02:24 +0100814 * Print error message "s" with format string and variable arguments.
815 * "s" should already be translated.
816 * Note: caller must not use "IObuff" for "s"!
817 * Return TRUE if wait_return() not called.
Bram Moolenaar95f09602016-11-10 20:01:45 +0100818 */
819 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100820semsg(const char *s, ...)
Bram Moolenaar95f09602016-11-10 20:01:45 +0100821{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200822 // Skip this if not giving error messages at the moment.
Bram Moolenaar097c5372023-05-24 21:02:24 +0100823 if (emsg_not_now())
824 return TRUE;
Bram Moolenaar95f09602016-11-10 20:01:45 +0100825
Bram Moolenaar097c5372023-05-24 21:02:24 +0100826 if (IObuff == NULL)
827 // Very early in initialisation and already something wrong, just
828 // give the raw message so the user at least gets a hint.
829 return emsg_core(s);
830
831 va_list ap;
832
833 va_start(ap, s);
834 vim_vsnprintf((char *)IObuff, IOSIZE, s, ap);
835 va_end(ap);
836 return emsg_core((char *)IObuff);
Bram Moolenaar95f09602016-11-10 20:01:45 +0100837}
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100838#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +0100839
840/*
841 * Same as emsg(...), but abort on error when ABORT_ON_INTERNAL_ERROR is
842 * defined. It is used for internal errors only, so that they can be
843 * detected when fuzzing vim.
844 */
845 void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100846iemsg(char *s)
Bram Moolenaar95f09602016-11-10 20:01:45 +0100847{
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000848 if (emsg_not_now())
849 return;
850
Bram Moolenaar097c5372023-05-24 21:02:24 +0100851 // Give a generic error which is translated. The error itself may not be
852 // translated, it almost never shows.
853 emsg_core(_(e_internal_error_please_report_a_bug));
854
855 emsg_core(s);
Bram Moolenaar096ca732022-01-01 00:55:28 +0000856#if defined(ABORT_ON_INTERNAL_ERROR) && defined(FEAT_EVAL)
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000857 set_vim_var_string(VV_ERRMSG, (char_u *)s, -1);
858 msg_putchar('\n'); // avoid overwriting the error message
859 out_flush();
860 abort();
Bram Moolenaar95f09602016-11-10 20:01:45 +0100861#endif
862}
863
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100864#ifndef PROTO // manual proto with __attribute__
Bram Moolenaar95f09602016-11-10 20:01:45 +0100865/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100866 * Same as semsg(...) but abort on error when ABORT_ON_INTERNAL_ERROR is
Bram Moolenaar95f09602016-11-10 20:01:45 +0100867 * defined. It is used for internal errors only, so that they can be
868 * detected when fuzzing vim.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100869 * Note: caller must not pass 'IObuff' as 1st argument.
Bram Moolenaar95f09602016-11-10 20:01:45 +0100870 */
871 void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100872siemsg(const char *s, ...)
Bram Moolenaar95f09602016-11-10 20:01:45 +0100873{
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000874 if (emsg_not_now())
875 return;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100876
Bram Moolenaar097c5372023-05-24 21:02:24 +0100877 // Give a generic error which is translated. The error itself may not be
878 // translated, it almost never shows.
879 emsg_core(_(e_internal_error_please_report_a_bug));
880
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000881 if (IObuff == NULL)
882 {
883 // Very early in initialisation and already something wrong, just
884 // give the raw message so the user at least gets a hint.
Bram Moolenaar097c5372023-05-24 21:02:24 +0100885 emsg_core(s);
Bram Moolenaar213e70e2022-08-19 13:17:21 +0100886 }
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000887 else
888 {
889 va_list ap;
890
891 va_start(ap, s);
892 vim_vsnprintf((char *)IObuff, IOSIZE, s, ap);
893 va_end(ap);
Bram Moolenaar097c5372023-05-24 21:02:24 +0100894 emsg_core((char *)IObuff);
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000895 }
896# ifdef ABORT_ON_INTERNAL_ERROR
897 msg_putchar('\n'); // avoid overwriting the error message
898 out_flush();
899 abort();
900# endif
Bram Moolenaar95f09602016-11-10 20:01:45 +0100901}
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100902#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +0100903
904/*
905 * Give an "Internal error" message.
906 */
907 void
908internal_error(char *where)
909{
Bram Moolenaar097c5372023-05-24 21:02:24 +0100910 // Give a generic error which is translated. The error itself may not be
911 // translated, it almost never shows.
912 emsg_core(_(e_internal_error_please_report_a_bug));
913
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000914 siemsg(_(e_internal_error_str), where);
Bram Moolenaar95f09602016-11-10 20:01:45 +0100915}
916
Dominique Pelle748b3082022-01-08 12:41:16 +0000917#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaardd589232020-02-29 17:38:12 +0100918/*
919 * Like internal_error() but do not call abort(), to avoid tests using
920 * test_unknown() and test_void() causing Vim to exit.
921 */
922 void
923internal_error_no_abort(char *where)
924{
Bram Moolenaar097c5372023-05-24 21:02:24 +0100925 // Give a generic error which is translated. The error itself may not be
926 // translated, it almost never shows.
927 emsg_core(_(e_internal_error_please_report_a_bug));
928
929 semsg(_(e_internal_error_str), where);
Bram Moolenaardd589232020-02-29 17:38:12 +0100930}
Dominique Pelle748b3082022-01-08 12:41:16 +0000931#endif
Bram Moolenaardd589232020-02-29 17:38:12 +0100932
Bram Moolenaar85a20022019-12-21 18:25:54 +0100933// emsg3() and emsgn() are in misc2.c to avoid warnings for the prototypes.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934
Bram Moolenaardf177f62005-02-22 08:39:57 +0000935 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100936emsg_invreg(int name)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000937{
cero19881d87e112023-02-16 15:03:12 +0000938 semsg(_(e_invalid_register_name_str), transchar_buf(NULL, name));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000939}
940
Dominique Pelle748b3082022-01-08 12:41:16 +0000941#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100943 * Give an error message which contains %s for "name[len]".
944 */
945 void
946emsg_namelen(char *msg, char_u *name, int len)
947{
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +0000948 char_u *copy = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100949
950 semsg(msg, copy == NULL ? "NULL" : (char *)copy);
Bram Moolenaar292b90d2020-03-18 15:23:16 +0100951 vim_free(copy);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100952}
Dominique Pelle748b3082022-01-08 12:41:16 +0000953#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100954
955/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 * Like msg(), but truncate to a single line if p_shm contains 't', or when
957 * "force" is TRUE. This truncates in another way as for normal messages.
958 * Careful: The string may be changed by msg_may_trunc()!
959 * Returns a pointer to the printed message, if wait_return() not called.
960 */
Bram Moolenaar32526b32019-01-19 17:43:09 +0100961 char *
962msg_trunc_attr(char *s, int force, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963{
964 int n;
Bram Moolenaar32526b32019-01-19 17:43:09 +0100965 char *ts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966
Bram Moolenaar85a20022019-12-21 18:25:54 +0100967 // Add message to history before truncating
Bram Moolenaar32526b32019-01-19 17:43:09 +0100968 add_msg_hist((char_u *)s, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969
Bram Moolenaar32526b32019-01-19 17:43:09 +0100970 ts = (char *)msg_may_trunc(force, (char_u *)s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971
972 msg_hist_off = TRUE;
Bram Moolenaar32526b32019-01-19 17:43:09 +0100973 n = msg_attr(ts, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 msg_hist_off = FALSE;
975
976 if (n)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100977 return ts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 return NULL;
979}
980
981/*
982 * Check if message "s" should be truncated at the start (for filenames).
983 * Return a pointer to where the truncated message starts.
984 * Note: May change the message by replacing a character with '<'.
985 */
986 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100987msg_may_trunc(int force, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988{
989 int n;
990 int room;
991
Bram Moolenaar9198de32022-08-27 21:30:03 +0100992 // If 'cmdheight' is zero or something unexpected happened "room" may be
993 // negative.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 room = (int)(Rows - cmdline_row - 1) * Columns + sc_col - 1;
Bram Moolenaar0fbc9262022-06-26 11:17:10 +0100995 if (room > 0 && (force || (shortmess(SHM_TRUNC) && !exmode_active))
Bram Moolenaara2a89732022-08-31 14:46:18 +0100996 && (n = (int)STRLEN(s) - room) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 if (has_mbyte)
999 {
1000 int size = vim_strsize(s);
1001
Bram Moolenaar85a20022019-12-21 18:25:54 +01001002 // There may be room anyway when there are multibyte chars.
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00001003 if (size <= room)
1004 return s;
1005
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 for (n = 0; size >= room; )
1007 {
1008 size -= (*mb_ptr2cells)(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001009 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 }
1011 --n;
1012 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 s += n;
1014 *s = '<';
1015 }
1016 return s;
1017}
1018
1019 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001020add_msg_hist(
1021 char_u *s,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001022 int len, // -1 for undetermined length
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001023 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024{
1025 struct msg_hist *p;
1026
1027 if (msg_hist_off || msg_silent != 0)
1028 return;
1029
Bram Moolenaar85a20022019-12-21 18:25:54 +01001030 // allocate an entry and add the message at the end of the history
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001031 p = ALLOC_ONE(struct msg_hist);
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001032 if (p == NULL)
1033 return;
1034
1035 if (len < 0)
1036 len = (int)STRLEN(s);
1037 // remove leading and trailing newlines
1038 while (len > 0 && *s == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 {
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001040 ++s;
1041 --len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 }
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001043 while (len > 0 && s[len - 1] == '\n')
1044 --len;
1045 p->msg = vim_strnsave(s, len);
1046 p->next = NULL;
1047 p->attr = attr;
1048 if (last_msg_hist != NULL)
1049 last_msg_hist->next = p;
1050 last_msg_hist = p;
1051 if (first_msg_hist == NULL)
1052 first_msg_hist = last_msg_hist;
1053 ++msg_hist_len;
Shougo Matsushita9f860a12024-11-24 13:48:05 +01001054
1055 check_msg_hist();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056}
1057
1058/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00001059 * Delete the first (oldest) message from the history.
1060 * Returns FAIL if there are no messages.
1061 */
1062 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001063delete_first_msg(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00001064{
1065 struct msg_hist *p;
1066
1067 if (msg_hist_len <= 0)
1068 return FAIL;
1069 p = first_msg_hist;
1070 first_msg_hist = p->next;
Bram Moolenaara7241f52008-06-24 20:39:31 +00001071 if (first_msg_hist == NULL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001072 last_msg_hist = NULL; // history is empty
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00001073 vim_free(p->msg);
1074 vim_free(p);
1075 --msg_hist_len;
1076 return OK;
1077}
1078
Christian Brabandt51d4d842024-12-06 17:26:25 +01001079 static void
Shougo Matsushita9f860a12024-11-24 13:48:05 +01001080check_msg_hist(void)
1081{
1082 // Don't let the message history get too big
Christian Brabandt51d4d842024-12-06 17:26:25 +01001083 while (msg_hist_len > 0 && msg_hist_len > msg_hist_max)
Shougo Matsushita9f860a12024-11-24 13:48:05 +01001084 (void)delete_first_msg();
1085}
1086
Christian Brabandt51d4d842024-12-06 17:26:25 +01001087
1088 int
1089messagesopt_changed(void)
1090{
1091 char_u *p;
1092 int messages_flags_new = 0;
1093 int messages_wait_new = 0;
1094 int messages_history_new = 0;
1095
zeertzjq8cc43da2024-12-07 16:09:08 +01001096 p = p_mopt;
Christian Brabandt51d4d842024-12-06 17:26:25 +01001097 while (*p != NUL)
1098 {
1099 if (STRNCMP(p, MESSAGES_OPT_HIT_ENTER,
1100 STRLEN_LITERAL(MESSAGES_OPT_HIT_ENTER)) == 0)
1101 {
1102 p += STRLEN_LITERAL(MESSAGES_OPT_HIT_ENTER);
1103 messages_flags_new |= MESSAGES_HIT_ENTER;
1104 }
1105 else if (STRNCMP(p, MESSAGES_OPT_WAIT,
1106 STRLEN_LITERAL(MESSAGES_OPT_WAIT)) == 0
1107 && VIM_ISDIGIT(p[STRLEN_LITERAL(MESSAGES_OPT_WAIT)]))
1108 {
1109 p += STRLEN_LITERAL(MESSAGES_OPT_WAIT);
1110 messages_wait_new = getdigits(&p);
1111 messages_flags_new |= MESSAGES_WAIT;
1112 }
1113 else if (STRNCMP(p, MESSAGES_OPT_HISTORY,
1114 STRLEN_LITERAL(MESSAGES_OPT_HISTORY)) == 0
1115 && VIM_ISDIGIT(p[STRLEN_LITERAL(MESSAGES_OPT_HISTORY)]))
1116 {
1117 p += STRLEN_LITERAL(MESSAGES_OPT_HISTORY);
1118 messages_history_new = getdigits(&p);
1119 messages_flags_new |= MESSAGES_HISTORY;
1120 }
1121
1122 if (*p != ',' && *p != NUL)
1123 return FAIL;
1124 if (*p == ',')
1125 ++p;
1126 }
1127
1128 // Either "wait" or "hit-enter" is required
1129 if (!(messages_flags_new & (MESSAGES_HIT_ENTER | MESSAGES_WAIT)))
1130 return FAIL;
1131
1132 // "history" must be set
1133 if (!(messages_flags_new & MESSAGES_HISTORY))
1134 return FAIL;
1135
h-east65be8342024-12-08 10:05:26 +01001136 if (messages_history_new < 0 || messages_history_new > 10000)
Christian Brabandt51d4d842024-12-06 17:26:25 +01001137 return FAIL;
1138
h-east65be8342024-12-08 10:05:26 +01001139 if (messages_wait_new < 0 || messages_wait_new > 10000)
Shougo Matsushitad9e9f892024-12-07 16:00:25 +01001140 return FAIL;
1141
Christian Brabandt51d4d842024-12-06 17:26:25 +01001142 msg_flags = messages_flags_new;
1143 msg_wait = messages_wait_new;
1144
1145 msg_hist_max = messages_history_new;
1146 check_msg_hist();
1147
1148 return OK;
1149}
1150
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00001151/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 * ":messages" command.
1153 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 void
Bram Moolenaar52196b22016-04-14 17:52:41 +02001155ex_messages(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156{
1157 struct msg_hist *p;
1158 char_u *s;
Bram Moolenaar451f8492016-04-14 17:16:22 +02001159 int c = 0;
1160
1161 if (STRCMP(eap->arg, "clear") == 0)
1162 {
1163 int keep = eap->addr_count == 0 ? 0 : eap->line2;
1164
1165 while (msg_hist_len > keep)
1166 (void)delete_first_msg();
1167 return;
1168 }
1169
1170 if (*eap->arg != NUL)
1171 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001172 emsg(_(e_invalid_argument));
Bram Moolenaar451f8492016-04-14 17:16:22 +02001173 return;
1174 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175
1176 msg_hist_off = TRUE;
1177
Bram Moolenaar451f8492016-04-14 17:16:22 +02001178 p = first_msg_hist;
Bram Moolenaar451f8492016-04-14 17:16:22 +02001179 if (eap->addr_count != 0)
1180 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001181 // Count total messages
Bram Moolenaar451f8492016-04-14 17:16:22 +02001182 for (; p != NULL && !got_int; p = p->next)
1183 c++;
1184
1185 c -= eap->line2;
1186
Bram Moolenaar85a20022019-12-21 18:25:54 +01001187 // Skip without number of messages specified
Bram Moolenaar451f8492016-04-14 17:16:22 +02001188 for (p = first_msg_hist; p != NULL && !got_int && c > 0;
1189 p = p->next, c--);
1190 }
1191
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001192 if (p == first_msg_hist)
1193 {
Bram Moolenaar41f69182020-04-25 15:45:37 +02001194#ifdef FEAT_MULTI_LANG
1195 s = get_mess_lang();
1196#else
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001197 s = mch_getenv((char_u *)"LANG");
Bram Moolenaar41f69182020-04-25 15:45:37 +02001198#endif
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001199 if (s != NULL && *s != NUL)
Bram Moolenaar0c183192018-06-28 14:54:43 +02001200 // The next comment is extracted by xgettext and put in po file for
1201 // translators to read.
Bram Moolenaar32526b32019-01-19 17:43:09 +01001202 msg_attr(
Bram Moolenaar0c183192018-06-28 14:54:43 +02001203 // Translator: Please replace the name and email address
1204 // with the appropriate text for your translation.
Christian Brabandte978b452023-08-13 10:33:05 +02001205 _("Messages maintainer: The Vim Project"),
Bram Moolenaar8820b482017-03-16 17:23:31 +01001206 HL_ATTR(HLF_T));
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001207 }
1208
Bram Moolenaar85a20022019-12-21 18:25:54 +01001209 // Display what was not skipped.
Bram Moolenaar451f8492016-04-14 17:16:22 +02001210 for (; p != NULL && !got_int; p = p->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 if (p->msg != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001212 msg_attr((char *)p->msg, p->attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213
1214 msg_hist_off = FALSE;
1215}
1216
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001217#if defined(FEAT_CON_DIALOG) || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218/*
1219 * Call this after prompting the user. This will avoid a hit-return message
1220 * and a delay.
1221 */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001222 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001223msg_end_prompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224{
1225 need_wait_return = FALSE;
1226 emsg_on_display = FALSE;
1227 cmdline_row = msg_row;
1228 msg_col = 0;
1229 msg_clr_eos();
Bram Moolenaar5d6f75e2011-12-30 14:14:29 +01001230 lines_left = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231}
1232#endif
1233
1234/*
Bram Moolenaar32526b32019-01-19 17:43:09 +01001235 * Wait for the user to hit a key (normally Enter).
1236 * If "redraw" is TRUE, clear and redraw the screen.
1237 * If "redraw" is FALSE, just redraw the screen.
1238 * If "redraw" is -1, don't redraw at all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 */
1240 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001241wait_return(int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242{
1243 int c;
1244 int oldState;
1245 int tmpState;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 int had_got_int;
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001247 int save_reg_recording;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001248 FILE *save_scriptout;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249
1250 if (redraw == TRUE)
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01001251 set_must_redraw(UPD_CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252
Bram Moolenaar85a20022019-12-21 18:25:54 +01001253 // If using ":silent cmd", don't wait for a return. Also don't set
1254 // need_wait_return to do it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 if (msg_silent != 0)
1256 return;
Bram Moolenaarcf0995d2022-09-11 21:36:17 +01001257#ifdef HAS_MESSAGE_WINDOW
1258 if (in_echowindow)
1259 return;
1260#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01001262 /*
1263 * When inside vgetc(), we can't wait for a typed character at all.
1264 * With the global command (and some others) we only need one return at
1265 * the end. Adjust cmdline_row to avoid the next message overwriting the
1266 * last one.
1267 */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00001268 if (vgetc_busy > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 return;
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01001270 need_wait_return = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 if (no_wait_return)
1272 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 if (!exmode_active)
1274 cmdline_row = msg_row;
1275 return;
1276 }
1277
Bram Moolenaar85a20022019-12-21 18:25:54 +01001278 redir_off = TRUE; // don't redirect this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 oldState = State;
1280 if (quit_more)
1281 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001282 c = CAR; // just pretend CR was hit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 quit_more = FALSE;
1284 got_int = FALSE;
1285 }
1286 else if (exmode_active)
1287 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001288 msg_puts(" "); // make sure the cursor is on the right line
1289 c = CAR; // no need for a return in ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 got_int = FALSE;
1291 }
1292 else
1293 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001294 // Make sure the hit-return prompt is on screen when 'guioptions' was
1295 // just changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 screenalloc(FALSE);
1297
Bram Moolenaar24959102022-05-07 20:01:16 +01001298 State = MODE_HITRETURN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar85a20022019-12-21 18:25:54 +01001301 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302#endif
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01001303 cmdline_row = msg_row;
1304
Bram Moolenaar85a20022019-12-21 18:25:54 +01001305 // Avoid the sequence that the user types ":" at the hit-return prompt
1306 // to start an Ex command, but the file-changed dialog gets in the
1307 // way.
Bram Moolenaarec38d692013-04-24 15:12:32 +02001308 if (need_check_timestamps)
1309 check_timestamps(FALSE);
1310
Christian Brabandt51d4d842024-12-06 17:26:25 +01001311 if (msg_flags & MESSAGES_HIT_ENTER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 {
Christian Brabandt51d4d842024-12-06 17:26:25 +01001313 hit_return_msg();
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001314
Christian Brabandt51d4d842024-12-06 17:26:25 +01001315 do
1316 {
1317 // Remember "got_int", if it is set vgetc() probably returns a
1318 // CTRL-C, but we need to loop then.
1319 had_got_int = got_int;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001320
Christian Brabandt51d4d842024-12-06 17:26:25 +01001321 // Don't do mappings here, we put the character back in the
1322 // typeahead buffer.
1323 ++no_mapping;
1324 ++allow_keys;
1325
1326 // Temporarily disable Recording. If Recording is active, the
1327 // character will be recorded later, since it will be added to the
1328 // typebuf after the loop
1329 save_reg_recording = reg_recording;
1330 save_scriptout = scriptout;
1331 reg_recording = 0;
1332 scriptout = NULL;
1333 c = safe_vgetc();
1334 if (had_got_int && !global_busy)
1335 got_int = FALSE;
1336 --no_mapping;
1337 --allow_keys;
1338 reg_recording = save_reg_recording;
1339 scriptout = save_scriptout;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001340
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341#ifdef FEAT_CLIPBOARD
Christian Brabandt51d4d842024-12-06 17:26:25 +01001342 // Strange way to allow copying (yanking) a modeless selection at
1343 // the hit-enter prompt. Use CTRL-Y, because the same is used in
1344 // Cmdline-mode and it's harmless when there is no selection.
1345 if (c == Ctrl_Y && clip_star.state == SELECT_DONE)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001346 {
Christian Brabandt51d4d842024-12-06 17:26:25 +01001347 clip_copy_modeless_selection(TRUE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001348 c = K_IGNORE;
Christian Brabandt51d4d842024-12-06 17:26:25 +01001349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350#endif
Bram Moolenaar88456cd2022-11-18 22:14:09 +00001351
Christian Brabandt51d4d842024-12-06 17:26:25 +01001352 /*
1353 * Allow scrolling back in the messages.
1354 * Also accept scroll-down commands when messages fill the screen,
1355 * to avoid that typing one 'j' too many makes the messages
1356 * disappear.
1357 */
1358 if (p_more && !p_cp)
1359 {
1360 if (c == 'b' || c == 'k' || c == 'u' || c == 'g'
1361 || c == K_UP || c == K_PAGEUP)
1362 {
1363 if (msg_scrolled > Rows)
1364 // scroll back to show older messages
1365 do_more_prompt(c);
1366 else
1367 {
1368 msg_didout = FALSE;
1369 c = K_IGNORE;
1370 msg_col =
1371#ifdef FEAT_RIGHTLEFT
1372 cmdmsg_rl ? Columns - 1 :
1373#endif
1374 0;
1375 }
1376 if (quit_more)
1377 {
1378 c = CAR; // just pretend CR was hit
1379 quit_more = FALSE;
1380 got_int = FALSE;
1381 }
1382 else if (c != K_IGNORE)
1383 {
1384 c = K_IGNORE;
1385 hit_return_msg();
1386 }
1387 }
1388 else if (msg_scrolled > Rows - 2
1389 && (c == 'j' || c == 'd' || c == 'f'
1390 || c == K_DOWN || c == K_PAGEDOWN))
1391 c = K_IGNORE;
1392 }
1393 } while ((had_got_int && c == Ctrl_C)
1394 || c == K_IGNORE
1395#ifdef FEAT_GUI
1396 || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR
1397#endif
1398 || c == K_LEFTDRAG || c == K_LEFTRELEASE
1399 || c == K_MIDDLEDRAG || c == K_MIDDLERELEASE
1400 || c == K_RIGHTDRAG || c == K_RIGHTRELEASE
1401 || c == K_MOUSELEFT || c == K_MOUSERIGHT
1402 || c == K_MOUSEDOWN || c == K_MOUSEUP
1403 || c == K_MOUSEMOVE
1404 || (!mouse_has(MOUSE_RETURN)
1405 && mouse_row < msg_row
1406 && (c == K_LEFTMOUSE
1407 || c == K_MIDDLEMOUSE
1408 || c == K_RIGHTMOUSE
1409 || c == K_X1MOUSE
1410 || c == K_X2MOUSE))
1411 );
1412 ui_breakcheck();
1413
1414 // Avoid that the mouse-up event causes Visual mode to start.
1415 if (c == K_LEFTMOUSE || c == K_MIDDLEMOUSE || c == K_RIGHTMOUSE
1416 || c == K_X1MOUSE || c == K_X2MOUSE)
1417 (void)jump_to_mouse(MOUSE_SETPOS, NULL, 0);
1418 else if (vim_strchr((char_u *)"\r\n ", c) == NULL && c != Ctrl_C)
1419 {
1420 // Put the character back in the typeahead buffer. Don't use the
1421 // stuff buffer, because lmaps wouldn't work.
1422 ins_char_typebuf(vgetc_char, vgetc_mod_mask);
1423 do_redraw = TRUE; // need a redraw even though there is
1424 // typeahead
1425 }
1426 }
1427 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 {
Christian Brabandt51d4d842024-12-06 17:26:25 +01001429 c = CAR;
1430 // Wait to allow the user to verify the output.
1431 do_sleep(msg_wait, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433 }
1434 redir_off = FALSE;
1435
1436 /*
1437 * If the user hits ':', '?' or '/' we get a command line from the next
1438 * line.
1439 */
1440 if (c == ':' || c == '?' || c == '/')
1441 {
1442 if (!exmode_active)
1443 cmdline_row = msg_row;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001444 skip_redraw = TRUE; // skip redraw once
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 do_redraw = FALSE;
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02001446#ifdef FEAT_TERMINAL
1447 skip_term_loop = TRUE;
1448#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 }
1450
1451 /*
1452 * If the window size changed set_shellsize() will redraw the screen.
1453 * Otherwise the screen is only redrawn if 'redraw' is set and no ':'
1454 * typed.
1455 */
1456 tmpState = State;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001457 State = oldState; // restore State before set_shellsize
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 msg_check();
1460
1461#if defined(UNIX) || defined(VMS)
1462 /*
1463 * When switching screens, we need to output an extra newline on exit.
1464 */
1465 if (swapping_screen() && !termcap_active)
1466 newline_on_exit = TRUE;
1467#endif
1468
1469 need_wait_return = FALSE;
1470 did_wait_return = TRUE;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001471 emsg_on_display = FALSE; // can delete error message now
1472 lines_left = -1; // reset lines_left at next msg_start()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 reset_last_sourcing();
1474 if (keep_msg != NULL && vim_strsize(keep_msg) >=
1475 (Rows - cmdline_row - 1) * Columns + sc_col)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001476 VIM_CLEAR(keep_msg); // don't redisplay message, it's too long
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477
Bram Moolenaar24959102022-05-07 20:01:16 +01001478 if (tmpState == MODE_SETWSIZE) // got resize event while in vgetc()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001480 starttermcap(); // start termcap before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481 shell_resized();
1482 }
1483 else if (!skip_redraw
1484 && (redraw == TRUE || (msg_scrolled != 0 && redraw != -1)))
1485 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001486 starttermcap(); // start termcap before redrawing
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001487 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 }
1489}
1490
1491/*
1492 * Write the hit-return prompt.
1493 */
1494 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001495hit_return_msg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001497 int save_p_more = p_more;
1498
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001499 p_more = FALSE; // don't want to see this message when scrolling back
Bram Moolenaar85a20022019-12-21 18:25:54 +01001500 if (msg_didout) // start on a new line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 msg_putchar('\n');
1502 if (got_int)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001503 msg_puts(_("Interrupt: "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504
Bram Moolenaar32526b32019-01-19 17:43:09 +01001505 msg_puts_attr(_("Press ENTER or type command to continue"), HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 if (!msg_use_printf())
1507 msg_clr_eos();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001508 p_more = save_p_more;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509}
1510
1511/*
1512 * Set "keep_msg" to "s". Free the old value and check for NULL pointer.
1513 */
1514 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001515set_keep_msg(char_u *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516{
1517 vim_free(keep_msg);
1518 if (s != NULL && msg_silent == 0)
1519 keep_msg = vim_strsave(s);
1520 else
1521 keep_msg = NULL;
Bram Moolenaaraab21c32005-01-25 21:46:35 +00001522 keep_msg_more = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001523 keep_msg_attr = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524}
1525
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001526/*
1527 * If there currently is a message being displayed, set "keep_msg" to it, so
1528 * that it will be displayed again after redraw.
1529 */
1530 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001531set_keep_msg_from_hist(void)
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001532{
1533 if (keep_msg == NULL && last_msg_hist != NULL && msg_scrolled == 0
Bram Moolenaar24959102022-05-07 20:01:16 +01001534 && (State & MODE_NORMAL))
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001535 set_keep_msg(last_msg_hist->msg, last_msg_hist->attr);
1536}
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001537
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538/*
1539 * Prepare for outputting characters in the command line.
1540 */
1541 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001542msg_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543{
1544 int did_return = FALSE;
1545
zeertzjq40ed6712023-11-23 20:37:01 +01001546 if (msg_row < cmdline_row)
1547 msg_row = cmdline_row;
1548
Bram Moolenaare4ce65d2010-08-04 20:12:32 +02001549 if (!msg_silent)
Rob Pilling726f7f92022-01-20 14:44:38 +00001550 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001551 VIM_CLEAR(keep_msg);
Rob Pilling726f7f92022-01-20 14:44:38 +00001552 need_fileinfo = FALSE;
1553 }
Bram Moolenaara7241f52008-06-24 20:39:31 +00001554
1555#ifdef FEAT_EVAL
Bram Moolenaar3b474dc2022-09-01 17:01:32 +01001556 if (need_clr_eos)
Bram Moolenaara7241f52008-06-24 20:39:31 +00001557 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001558 // Halfway an ":echo" command and getting an (error) message: clear
1559 // any text from the command.
Bram Moolenaara7241f52008-06-24 20:39:31 +00001560 need_clr_eos = FALSE;
1561 msg_clr_eos();
1562 }
1563#endif
1564
Bram Moolenaard54af2e2022-08-27 22:05:13 +01001565#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaara2a89732022-08-31 14:46:18 +01001566 if (in_echowindow)
Bram Moolenaar9198de32022-08-27 21:30:03 +01001567 {
Bram Moolenaar37fef162022-08-29 18:16:32 +01001568 if (popup_message_win_visible()
1569 && ((msg_col > 0 && (msg_scroll || !full_screen))
1570 || in_echowindow))
Bram Moolenaar9198de32022-08-27 21:30:03 +01001571 {
1572 win_T *wp = popup_get_message_win();
1573
Bram Moolenaarf2fb54f2022-08-28 20:58:51 +01001574 // start a new line
Bram Moolenaar9198de32022-08-27 21:30:03 +01001575 curbuf = wp->w_buffer;
1576 ml_append(wp->w_buffer->b_ml.ml_line_count,
1577 (char_u *)"", (colnr_T)0, FALSE);
1578 curbuf = curwin->w_buffer;
1579 }
1580 msg_col = 0;
1581 }
Bram Moolenaard54af2e2022-08-27 22:05:13 +01001582 else
1583#endif
1584 if (!msg_scroll && full_screen) // overwrite last message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 {
1586 msg_row = cmdline_row;
1587 msg_col =
1588#ifdef FEAT_RIGHTLEFT
1589 cmdmsg_rl ? Columns - 1 :
1590#endif
1591 0;
1592 }
Bram Moolenaara2a89732022-08-31 14:46:18 +01001593 else if (msg_didout || in_echowindow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 {
Bram Moolenaar309c4e02022-08-29 12:23:39 +01001595 // start message on next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 msg_putchar('\n');
1597 did_return = TRUE;
1598 if (exmode_active != EXMODE_NORMAL)
1599 cmdline_row = msg_row;
1600 }
1601 if (!msg_didany || lines_left < 0)
1602 msg_starthere();
1603 if (msg_silent == 0)
1604 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001605 msg_didout = FALSE; // no output on current line yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 cursor_off();
1607 }
1608
Bram Moolenaar85a20022019-12-21 18:25:54 +01001609 // when redirecting, may need to start a new line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 if (!did_return)
1611 redir_write((char_u *)"\n", -1);
1612}
1613
1614/*
1615 * Note that the current msg position is where messages start.
1616 */
1617 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001618msg_starthere(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619{
1620 lines_left = cmdline_row;
1621 msg_didany = FALSE;
1622}
1623
1624 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001625msg_putchar(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626{
1627 msg_putchar_attr(c, 0);
1628}
1629
1630 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001631msg_putchar_attr(int c, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632{
Bram Moolenaar63c0ccd2019-01-19 21:06:58 +01001633 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634
1635 if (IS_SPECIAL(c))
1636 {
1637 buf[0] = K_SPECIAL;
1638 buf[1] = K_SECOND(c);
1639 buf[2] = K_THIRD(c);
1640 buf[3] = NUL;
1641 }
1642 else
Bram Moolenaar63c0ccd2019-01-19 21:06:58 +01001643 buf[(*mb_char2bytes)(c, buf)] = NUL;
Bram Moolenaar63c0ccd2019-01-19 21:06:58 +01001644 msg_puts_attr((char *)buf, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645}
1646
1647 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001648msg_outnum(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001650 char buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651
Bram Moolenaar32526b32019-01-19 17:43:09 +01001652 sprintf(buf, "%ld", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 msg_puts(buf);
1654}
1655
1656 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001657msg_home_replace(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658{
1659 msg_home_replace_attr(fname, 0);
1660}
1661
1662#if defined(FEAT_FIND_ID) || defined(PROTO)
1663 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001664msg_home_replace_hl(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665{
Bram Moolenaar8820b482017-03-16 17:23:31 +01001666 msg_home_replace_attr(fname, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667}
1668#endif
1669
1670 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001671msg_home_replace_attr(char_u *fname, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672{
1673 char_u *name;
1674
1675 name = home_replace_save(NULL, fname);
1676 if (name != NULL)
1677 msg_outtrans_attr(name, attr);
1678 vim_free(name);
1679}
1680
1681/*
1682 * Output 'len' characters in 'str' (including NULs) with translation
Bram Moolenaarf48ee3c2019-12-06 22:18:20 +01001683 * if 'len' is -1, output up to a NUL character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 * Use attributes 'attr'.
1685 * Return the number of characters it takes on the screen.
1686 */
1687 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001688msg_outtrans(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689{
1690 return msg_outtrans_attr(str, 0);
1691}
1692
1693 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001694msg_outtrans_attr(char_u *str, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695{
1696 return msg_outtrans_len_attr(str, (int)STRLEN(str), attr);
1697}
1698
1699 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001700msg_outtrans_len(char_u *str, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701{
1702 return msg_outtrans_len_attr(str, len, 0);
1703}
1704
1705/*
1706 * Output one character at "p". Return pointer to the next character.
1707 * Handles multi-byte characters.
1708 */
1709 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001710msg_outtrans_one(char_u *p, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 int l;
1713
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001714 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 {
1716 msg_outtrans_len_attr(p, l, attr);
1717 return p + l;
1718 }
cero19881d87e112023-02-16 15:03:12 +00001719 msg_puts_attr((char *)transchar_byte_buf(NULL, *p), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720 return p + 1;
1721}
1722
1723 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001724msg_outtrans_len_attr(char_u *msgstr, int len, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725{
1726 int retval = 0;
1727 char_u *str = msgstr;
1728 char_u *plain_start = msgstr;
1729 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 int mb_l;
1731 int c;
Bram Moolenaar3d30af82020-10-13 22:15:56 +02001732 int save_got_int = got_int;
1733
1734 // Only quit when got_int was set in here.
1735 got_int = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736
Shougo Matsushitabe2b03c2024-04-08 22:11:50 +02001737 if (attr == 0)
1738 attr = HL_ATTR(HLF_MSG);
1739
Bram Moolenaar85a20022019-12-21 18:25:54 +01001740 // if MSG_HIST flag set, add message to history
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741 if (attr & MSG_HIST)
1742 {
1743 add_msg_hist(str, len, attr);
1744 attr &= ~MSG_HIST;
1745 }
1746
Bram Moolenaar800cdbb2023-06-15 16:40:02 +01001747 // When drawing over the command line no need to clear it later or remove
1748 // the mode message.
Bram Moolenaarda51ad52023-06-15 18:44:50 +01001749 if (msg_row >= cmdline_row && msg_col == 0)
Bram Moolenaar800cdbb2023-06-15 16:40:02 +01001750 {
1751 clear_cmdline = FALSE;
1752 mode_displayed = FALSE;
1753 }
1754
Bram Moolenaar85a20022019-12-21 18:25:54 +01001755 // If the string starts with a composing character first draw a space on
1756 // which the composing char can be drawn.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 if (enc_utf8 && utf_iscomposing(utf_ptr2char(msgstr)))
Bram Moolenaar32526b32019-01-19 17:43:09 +01001758 msg_puts_attr(" ", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759
1760 /*
1761 * Go over the string. Special characters are translated and printed.
1762 * Normal characters are printed several at a time.
1763 */
Bram Moolenaar3d30af82020-10-13 22:15:56 +02001764 while (--len >= 0 && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 if (enc_utf8)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001767 // Don't include composing chars after the end.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001768 mb_l = utfc_ptr2len_len(str, len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769 else if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001770 mb_l = (*mb_ptr2len)(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771 else
1772 mb_l = 1;
1773 if (has_mbyte && mb_l > 1)
1774 {
1775 c = (*mb_ptr2char)(str);
1776 if (vim_isprintc(c))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001777 // printable multi-byte char: count the cells.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 retval += (*mb_ptr2cells)(str);
1779 else
1780 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001781 // unprintable multi-byte char: print the printable chars so
1782 // far and the translation of the unprintable char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 if (str > plain_start)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001784 msg_puts_attr_len((char *)plain_start,
1785 (int)(str - plain_start), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 plain_start = str + mb_l;
cero19881d87e112023-02-16 15:03:12 +00001787 msg_puts_attr((char *)transchar_buf(NULL, c),
Bram Moolenaar32526b32019-01-19 17:43:09 +01001788 attr == 0 ? HL_ATTR(HLF_8) : attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 retval += char2cells(c);
1790 }
1791 len -= mb_l - 1;
1792 str += mb_l;
1793 }
1794 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 {
cero19881d87e112023-02-16 15:03:12 +00001796 s = transchar_byte_buf(NULL, *str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 if (s[1] != NUL)
1798 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001799 // unprintable char: print the printable chars so far and the
1800 // translation of the unprintable char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 if (str > plain_start)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001802 msg_puts_attr_len((char *)plain_start,
1803 (int)(str - plain_start), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 plain_start = str + 1;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001805 msg_puts_attr((char *)s, attr == 0 ? HL_ATTR(HLF_8) : attr);
Bram Moolenaarc236c162008-07-13 17:41:49 +00001806 retval += (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 }
Bram Moolenaarb3c722a2008-07-06 17:16:02 +00001808 else
1809 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 ++str;
1811 }
1812 }
1813
Bram Moolenaar3d30af82020-10-13 22:15:56 +02001814 if (str > plain_start && !got_int)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001815 // print the printable chars at the end
Bram Moolenaar32526b32019-01-19 17:43:09 +01001816 msg_puts_attr_len((char *)plain_start, (int)(str - plain_start), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817
Bram Moolenaar3d30af82020-10-13 22:15:56 +02001818 got_int |= save_got_int;
1819
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 return retval;
1821}
1822
1823#if defined(FEAT_QUICKFIX) || defined(PROTO)
1824 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001825msg_make(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826{
1827 int i;
1828 static char_u *str = (char_u *)"eeffoc", *rs = (char_u *)"Plon#dqg#vxjduB";
1829
1830 arg = skipwhite(arg);
1831 for (i = 5; *arg && i >= 0; --i)
1832 if (*arg++ != str[i])
1833 break;
1834 if (i < 0)
1835 {
1836 msg_putchar('\n');
1837 for (i = 0; rs[i]; ++i)
1838 msg_putchar(rs[i] - 3);
1839 }
1840}
1841#endif
1842
1843/*
Bram Moolenaarf48ee3c2019-12-06 22:18:20 +01001844 * Output the string 'str' up to a NUL character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 * Return the number of characters it takes on the screen.
1846 *
1847 * If K_SPECIAL is encountered, then it is taken in conjunction with the
1848 * following character and shown as <F1>, <S-Up> etc. Any other character
1849 * which is not printable shown in <> form.
1850 * If 'from' is TRUE (lhs of a mapping), a space is shown as <Space>.
1851 * If a character is displayed in one of these special ways, is also
1852 * highlighted (its highlight name is '8' in the p_hl variable).
1853 * Otherwise characters are not highlighted.
1854 * This function is used to show mappings, where we want to see how to type
1855 * the character/string -- webb
1856 */
1857 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001858msg_outtrans_special(
1859 char_u *strstart,
Bram Moolenaar725310d2019-04-24 23:08:23 +02001860 int from, // TRUE for lhs of a mapping
Bram Moolenaar32aa1022019-11-02 22:54:41 +01001861 int maxlen) // screen columns, 0 for unlimited
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862{
1863 char_u *str = strstart;
1864 int retval = 0;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001865 char *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 int attr;
1867 int len;
1868
Bram Moolenaar8820b482017-03-16 17:23:31 +01001869 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 while (*str != NUL)
1871 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001872 // Leading and trailing spaces need to be displayed in <> form.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 if ((str == strstart || str[1] == NUL) && *str == ' ')
1874 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001875 text = "<Space>";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 ++str;
1877 }
1878 else
zeertzjqcdc83932022-09-12 13:38:41 +01001879 text = (char *)str2special(&str, from, FALSE);
zeertzjq0519ce02022-05-09 12:16:19 +01001880 if (text[0] != NUL && text[1] == NUL)
1881 // single-byte character or illegal byte
cero19881d87e112023-02-16 15:03:12 +00001882 text = (char *)transchar_byte_buf(NULL, (char_u)text[0]);
Bram Moolenaar32526b32019-01-19 17:43:09 +01001883 len = vim_strsize((char_u *)text);
Bram Moolenaar725310d2019-04-24 23:08:23 +02001884 if (maxlen > 0 && retval + len >= maxlen)
1885 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001886 // Highlight special keys
Bram Moolenaar32526b32019-01-19 17:43:09 +01001887 msg_puts_attr(text, len > 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001888 && (*mb_ptr2len)((char_u *)text) <= 1 ? attr : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 retval += len;
1890 }
1891 return retval;
1892}
1893
Martin Tournoijba43e762022-10-13 22:12:15 +01001894#if defined(FEAT_EVAL) || defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaarbd743252010-10-20 21:23:33 +02001895/*
1896 * Return the lhs or rhs of a mapping, with the key codes turned into printable
1897 * strings, in an allocated string.
1898 */
1899 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001900str2special_save(
1901 char_u *str,
zeertzjqcdc83932022-09-12 13:38:41 +01001902 int replace_spaces, // TRUE to replace " " with "<Space>".
1903 // used for the lhs of mapping and keytrans().
1904 int replace_lt) // TRUE to replace "<" with "<lt>".
Bram Moolenaarbd743252010-10-20 21:23:33 +02001905{
1906 garray_T ga;
1907 char_u *p = str;
1908
1909 ga_init2(&ga, 1, 40);
1910 while (*p != NUL)
zeertzjqcdc83932022-09-12 13:38:41 +01001911 ga_concat(&ga, str2special(&p, replace_spaces, replace_lt));
Bram Moolenaarbd743252010-10-20 21:23:33 +02001912 ga_append(&ga, NUL);
1913 return (char_u *)ga.ga_data;
1914}
1915#endif
1916
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917/*
1918 * Return the printable string for the key codes at "*sp".
zeertzjq0519ce02022-05-09 12:16:19 +01001919 * On illegal byte return a string with only that byte.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 * Used for translating the lhs or rhs of a mapping to printable chars.
1921 * Advances "sp" to the next code.
1922 */
1923 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001924str2special(
1925 char_u **sp,
zeertzjqcdc83932022-09-12 13:38:41 +01001926 int replace_spaces, // TRUE to replace " " with "<Space>".
1927 // used for the lhs of mapping and keytrans().
1928 int replace_lt) // TRUE to replace "<" with "<lt>".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929{
1930 int c;
1931 static char_u buf[7];
1932 char_u *str = *sp;
1933 int modifiers = 0;
1934 int special = FALSE;
1935
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 if (has_mbyte)
1937 {
1938 char_u *p;
1939
Bram Moolenaar85a20022019-12-21 18:25:54 +01001940 // Try to un-escape a multi-byte character. Return the un-escaped
1941 // string if it is a multi-byte character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 p = mb_unescape(sp);
1943 if (p != NULL)
1944 return p;
1945 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946
1947 c = *str;
zeertzjq1bed9932023-08-30 19:38:24 +02001948 if ((c == K_SPECIAL
1949#ifdef FEAT_GUI
1950 || c == CSI
1951#endif
1952 ) && str[1] != NUL && str[2] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 {
1954 if (str[1] == KS_MODIFIER)
1955 {
1956 modifiers = str[2];
1957 str += 3;
1958 c = *str;
1959 }
zeertzjq1bed9932023-08-30 19:38:24 +02001960 if ((c == K_SPECIAL
1961#ifdef FEAT_GUI
1962 || c == CSI
1963#endif
1964 ) && str[1] != NUL && str[2] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 {
1966 c = TO_SPECIAL(str[1], str[2]);
1967 str += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001969 if (IS_SPECIAL(c) || modifiers) // special key
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 special = TRUE;
1971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972
zeertzjq0519ce02022-05-09 12:16:19 +01001973 if (has_mbyte && !IS_SPECIAL(c) && MB_BYTE2LEN(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 {
zeertzjqac402f42022-05-04 18:51:43 +01001975 char_u *p;
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001976
zeertzjqac402f42022-05-04 18:51:43 +01001977 *sp = str;
1978 // Try to un-escape a multi-byte character after modifiers.
1979 p = mb_unescape(sp);
zeertzjq0519ce02022-05-09 12:16:19 +01001980 if (p != NULL)
1981 // Since 'special' is TRUE the multi-byte character 'c' will be
1982 // processed by get_special_key_name()
1983 c = (*mb_ptr2char)(p);
1984 else
1985 // illegal byte
1986 *sp = str + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 }
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001988 else
Bram Moolenaar083692d2022-06-29 21:16:58 +01001989 // single-byte character, NUL or illegal byte
1990 *sp = str + (*str == NUL ? 0 : 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991
zeertzjq0519ce02022-05-09 12:16:19 +01001992 // Make special keys and C0 control characters in <> form, also <M-Space>.
zeertzjqcdc83932022-09-12 13:38:41 +01001993 if (special
1994 || c < ' '
1995 || (replace_spaces && c == ' ')
1996 || (replace_lt && c == '<'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 return get_special_key_name(c, modifiers);
1998 buf[0] = c;
1999 buf[1] = NUL;
2000 return buf;
2001}
2002
2003/*
2004 * Translate a key sequence into special key names.
2005 */
2006 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002007str2specialbuf(char_u *sp, char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008{
2009 char_u *s;
2010
2011 *buf = NUL;
2012 while (*sp)
2013 {
zeertzjqcdc83932022-09-12 13:38:41 +01002014 s = str2special(&sp, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 if ((int)(STRLEN(s) + STRLEN(buf)) < len)
2016 STRCAT(buf, s);
2017 }
2018}
2019
2020/*
2021 * print line for :print or :list command
2022 */
2023 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002024msg_prt_line(char_u *s, int list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025{
2026 int c;
2027 int col = 0;
2028 int n_extra = 0;
2029 int c_extra = 0;
Bram Moolenaar83a52172019-01-16 22:41:54 +01002030 int c_final = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002031 char_u *p_extra = NULL; // init to make SASC shut up
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 int n;
Bram Moolenaar56da7972007-01-16 14:46:32 +00002033 int attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 char_u *trail = NULL;
Bram Moolenaar91478ae2021-02-03 15:58:13 +01002035 char_u *lead = NULL;
zeertzjqf14b8ba2021-09-10 16:58:30 +02002036 int in_multispace = FALSE;
2037 int multispace_pos = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 int l;
2039 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040
Bram Moolenaardf177f62005-02-22 08:39:57 +00002041 if (curwin->w_p_list)
2042 list = TRUE;
2043
Bram Moolenaar91478ae2021-02-03 15:58:13 +01002044 if (list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045 {
Bram Moolenaar91478ae2021-02-03 15:58:13 +01002046 // find start of trailing whitespace
Bram Moolenaareed9d462021-02-15 20:38:25 +01002047 if (curwin->w_lcs_chars.trail)
Bram Moolenaar91478ae2021-02-03 15:58:13 +01002048 {
2049 trail = s + STRLEN(s);
2050 while (trail > s && VIM_ISWHITE(trail[-1]))
2051 --trail;
2052 }
2053 // find end of leading whitespace
Bram Moolenaarb8329db2022-07-06 13:31:28 +01002054 if (curwin->w_lcs_chars.lead
2055 || curwin->w_lcs_chars.leadmultispace != NULL)
Bram Moolenaar91478ae2021-02-03 15:58:13 +01002056 {
2057 lead = s;
2058 while (VIM_ISWHITE(lead[0]))
2059 lead++;
2060 // in a line full of spaces all of them are treated as trailing
2061 if (*lead == NUL)
2062 lead = NULL;
2063 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 }
2065
Bram Moolenaar85a20022019-12-21 18:25:54 +01002066 // output a space for an empty line, otherwise the line will be
2067 // overwritten
Bram Moolenaareed9d462021-02-15 20:38:25 +01002068 if (*s == NUL && !(list && curwin->w_lcs_chars.eol != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 msg_putchar(' ');
2070
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002071 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 {
Bram Moolenaar56da7972007-01-16 14:46:32 +00002073 if (n_extra > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 {
2075 --n_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01002076 if (n_extra == 0 && c_final)
2077 c = c_final;
2078 else if (c_extra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 c = c_extra;
2080 else
2081 c = *p_extra++;
2082 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002083 else if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 {
2085 col += (*mb_ptr2cells)(s);
Bram Moolenaar1cbfc992020-12-02 12:37:37 +01002086 if (l >= MB_MAXBYTES)
2087 {
Bram Moolenaar29d2f452020-12-04 19:42:52 +01002088 STRCPY(buf, "?");
Bram Moolenaar1cbfc992020-12-02 12:37:37 +01002089 }
Bram Moolenaareed9d462021-02-15 20:38:25 +01002090 else if (curwin->w_lcs_chars.nbsp != NUL && list
Bram Moolenaar73284b92015-05-04 17:28:22 +02002091 && (mb_ptr2char(s) == 160
2092 || mb_ptr2char(s) == 0x202f))
Bram Moolenaaracf17282011-02-01 17:12:25 +01002093 {
Bram Moolenaar74ac29c2022-06-15 12:12:44 +01002094 int len = mb_char2bytes(curwin->w_lcs_chars.nbsp, buf);
2095
2096 buf[len] = NUL;
Bram Moolenaaracf17282011-02-01 17:12:25 +01002097 }
2098 else
2099 {
2100 mch_memmove(buf, s, (size_t)l);
2101 buf[l] = NUL;
2102 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01002103 msg_puts((char *)buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 s += l;
2105 continue;
2106 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 else
2108 {
2109 attr = 0;
2110 c = *s++;
zeertzjqabc80812023-09-24 23:32:18 +02002111 if (list)
2112 {
2113 in_multispace = c == ' ' && (*s == ' '
2114 || (col > 0 && s[-2] == ' '));
2115 if (!in_multispace)
2116 multispace_pos = 0;
2117 }
Bram Moolenaareed9d462021-02-15 20:38:25 +01002118 if (c == TAB && (!list || curwin->w_lcs_chars.tab1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002120 // tab amount depends on current column
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002121#ifdef FEAT_VARTABS
2122 n_extra = tabstop_padding(col, curbuf->b_p_ts,
2123 curbuf->b_p_vts_array) - 1;
2124#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 n_extra = curbuf->b_p_ts - col % curbuf->b_p_ts - 1;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002126#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00002127 if (!list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 {
2129 c = ' ';
2130 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01002131 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 }
2133 else
2134 {
Bram Moolenaareed9d462021-02-15 20:38:25 +01002135 c = (n_extra == 0 && curwin->w_lcs_chars.tab3)
2136 ? curwin->w_lcs_chars.tab3
2137 : curwin->w_lcs_chars.tab1;
2138 c_extra = curwin->w_lcs_chars.tab2;
2139 c_final = curwin->w_lcs_chars.tab3;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002140 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 }
2142 }
Bram Moolenaareed9d462021-02-15 20:38:25 +01002143 else if (c == 160 && list && curwin->w_lcs_chars.nbsp != NUL)
Bram Moolenaaracf17282011-02-01 17:12:25 +01002144 {
Bram Moolenaareed9d462021-02-15 20:38:25 +01002145 c = curwin->w_lcs_chars.nbsp;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002146 attr = HL_ATTR(HLF_8);
Bram Moolenaaracf17282011-02-01 17:12:25 +01002147 }
Bram Moolenaareed9d462021-02-15 20:38:25 +01002148 else if (c == NUL && list && curwin->w_lcs_chars.eol != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 {
2150 p_extra = (char_u *)"";
2151 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01002152 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 n_extra = 1;
Bram Moolenaareed9d462021-02-15 20:38:25 +01002154 c = curwin->w_lcs_chars.eol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002155 attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 --s;
2157 }
2158 else if (c != NUL && (n = byte2cells(c)) > 1)
2159 {
2160 n_extra = n - 1;
cero19881d87e112023-02-16 15:03:12 +00002161 p_extra = transchar_byte_buf(NULL, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01002163 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 c = *p_extra++;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002165 // Use special coloring to be able to distinguish <hex> from
2166 // the same in plain text.
Bram Moolenaar8820b482017-03-16 17:23:31 +01002167 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 }
zeertzjqf14b8ba2021-09-10 16:58:30 +02002169 else if (c == ' ')
Bram Moolenaar91478ae2021-02-03 15:58:13 +01002170 {
zeertzjqabc80812023-09-24 23:32:18 +02002171 if (lead != NULL && s <= lead && in_multispace
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01002172 && curwin->w_lcs_chars.leadmultispace != NULL)
2173 {
2174 c = curwin->w_lcs_chars.leadmultispace[multispace_pos++];
zeertzjqb5f08012022-06-09 13:55:28 +01002175 if (curwin->w_lcs_chars.leadmultispace[multispace_pos]
2176 == NUL)
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01002177 multispace_pos = 0;
2178 attr = HL_ATTR(HLF_8);
2179 }
zeertzjqb5f08012022-06-09 13:55:28 +01002180 else if (lead != NULL && s <= lead
2181 && curwin->w_lcs_chars.lead != NUL)
zeertzjqf14b8ba2021-09-10 16:58:30 +02002182 {
2183 c = curwin->w_lcs_chars.lead;
2184 attr = HL_ATTR(HLF_8);
2185 }
2186 else if (trail != NULL && s > trail)
2187 {
2188 c = curwin->w_lcs_chars.trail;
2189 attr = HL_ATTR(HLF_8);
2190 }
zeertzjqabc80812023-09-24 23:32:18 +02002191 else if (in_multispace
zeertzjqf14b8ba2021-09-10 16:58:30 +02002192 && curwin->w_lcs_chars.multispace != NULL)
2193 {
2194 c = curwin->w_lcs_chars.multispace[multispace_pos++];
2195 if (curwin->w_lcs_chars.multispace[multispace_pos] == NUL)
2196 multispace_pos = 0;
2197 attr = HL_ATTR(HLF_8);
2198 }
2199 else if (list && curwin->w_lcs_chars.space != NUL)
2200 {
2201 c = curwin->w_lcs_chars.space;
2202 attr = HL_ATTR(HLF_8);
2203 }
Bram Moolenaar1510f992015-04-22 22:18:22 +02002204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 }
2206
2207 if (c == NUL)
2208 break;
2209
2210 msg_putchar_attr(c, attr);
2211 col++;
2212 }
2213 msg_clr_eos();
2214}
2215
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216/*
2217 * Use screen_puts() to output one multi-byte character.
2218 * Return the pointer "s" advanced to the next character.
2219 */
2220 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002221screen_puts_mbyte(char_u *s, int l, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222{
2223 int cw;
2224
Bram Moolenaar85a20022019-12-21 18:25:54 +01002225 msg_didout = TRUE; // remember that line is not empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 cw = (*mb_ptr2cells)(s);
2227 if (cw > 1 && (
2228#ifdef FEAT_RIGHTLEFT
2229 cmdmsg_rl ? msg_col <= 1 :
2230#endif
2231 msg_col == Columns - 1))
2232 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002233 // Doesn't fit, print a highlighted '>' to fill it up.
Bram Moolenaar8820b482017-03-16 17:23:31 +01002234 msg_screen_putchar('>', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 return s;
2236 }
2237
2238 screen_puts_len(s, l, msg_row, msg_col, attr);
2239#ifdef FEAT_RIGHTLEFT
2240 if (cmdmsg_rl)
2241 {
2242 msg_col -= cw;
2243 if (msg_col == 0)
2244 {
2245 msg_col = Columns;
2246 ++msg_row;
2247 }
2248 }
2249 else
2250#endif
2251 {
2252 msg_col += cw;
2253 if (msg_col >= Columns)
2254 {
2255 msg_col = 0;
2256 ++msg_row;
2257 }
2258 }
2259 return s + l;
2260}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261
2262/*
2263 * Output a string to the screen at position msg_row, msg_col.
2264 * Update msg_row and msg_col for the next message.
2265 */
2266 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002267msg_puts(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268{
Bram Moolenaaraeac9002016-09-06 22:15:08 +02002269 msg_puts_attr(s, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270}
2271
2272 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002273msg_puts_title(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274{
Bram Moolenaar8820b482017-03-16 17:23:31 +01002275 msg_puts_attr(s, HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276}
2277
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278/*
2279 * Show a message in such a way that it always fits in the line. Cut out a
2280 * part in the middle and replace it with "..." when necessary.
2281 * Does not handle multi-byte characters!
2282 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002283 static void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002284msg_outtrans_long_len_attr(char_u *longstr, int len, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285{
2286 int slen = len;
2287 int room;
2288
2289 room = Columns - msg_col;
2290 if (len > room && room >= 20)
2291 {
2292 slen = (room - 3) / 2;
2293 msg_outtrans_len_attr(longstr, slen, attr);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002294 msg_puts_attr("...", HL_ATTR(HLF_8));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 }
2296 msg_outtrans_len_attr(longstr + len - slen, slen, attr);
2297}
2298
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002299 void
2300msg_outtrans_long_attr(char_u *longstr, int attr)
2301{
2302 msg_outtrans_long_len_attr(longstr, (int)STRLEN(longstr), attr);
2303}
2304
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305/*
2306 * Basic function for writing a message with highlight attributes.
2307 */
2308 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002309msg_puts_attr(char *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310{
2311 msg_puts_attr_len(s, -1, attr);
2312}
2313
2314/*
2315 * Like msg_puts_attr(), but with a maximum length "maxlen" (in bytes).
2316 * When "maxlen" is -1 there is no maximum length.
2317 * When "maxlen" is >= 0 the message is not put in the history.
2318 */
2319 static void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002320msg_puts_attr_len(char *str, int maxlen, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 /*
2323 * If redirection is on, also write to the redirection file.
2324 */
Bram Moolenaar32526b32019-01-19 17:43:09 +01002325 redir_write((char_u *)str, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326
2327 /*
2328 * Don't print anything when using ":silent cmd".
2329 */
2330 if (msg_silent != 0)
2331 return;
2332
Shougo Matsushitabe2b03c2024-04-08 22:11:50 +02002333 if (attr == 0)
2334 attr = HL_ATTR(HLF_MSG);
2335
Bram Moolenaar85a20022019-12-21 18:25:54 +01002336 // if MSG_HIST flag set, add message to history
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 if ((attr & MSG_HIST) && maxlen < 0)
2338 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002339 add_msg_hist((char_u *)str, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 attr &= ~MSG_HIST;
2341 }
2342
Bram Moolenaare8070982019-10-12 17:07:06 +02002343 // When writing something to the screen after it has scrolled, requires a
2344 // wait-return prompt later. Needed when scrolling, resetting
2345 // need_wait_return after some prompt, and then outputting something
2346 // without scrolling
2347 // Not needed when only using CR to move the cursor.
2348 if (msg_scrolled != 0 && !msg_scrolled_ign && STRCMP(str, "\r") != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 need_wait_return = TRUE;
Bram Moolenaare8070982019-10-12 17:07:06 +02002350 msg_didany = TRUE; // remember that something was outputted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351
2352 /*
2353 * If there is no valid screen, use fprintf so we can see error messages.
2354 * If termcap is not active, we may be writing in an alternate console
2355 * window, cursor positioning may not work correctly (window size may be
2356 * different, e.g. for Win32 console) or we just don't know where the
2357 * cursor is.
2358 */
2359 if (msg_use_printf())
Bram Moolenaar32526b32019-01-19 17:43:09 +01002360 msg_puts_printf((char_u *)str, maxlen);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002361 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01002362 msg_puts_display((char_u *)str, maxlen, attr, FALSE);
Rob Pilling726f7f92022-01-20 14:44:38 +00002363
2364 need_fileinfo = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002365}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366
Bram Moolenaar9198de32022-08-27 21:30:03 +01002367// values for "where"
2368#define PUT_APPEND 0 // append to "lnum"
2369#define PUT_TRUNC 1 // replace "lnum"
2370#define PUT_BELOW 2 // add below "lnum"
2371 //
2372#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaar9198de32022-08-27 21:30:03 +01002373/*
Bram Moolenaar24735f22022-08-30 15:44:22 +01002374 * Put text "t_s" until "end" in the message window.
Bram Moolenaar9198de32022-08-27 21:30:03 +01002375 * "where" specifies where to put the text.
2376 */
2377 static void
2378put_msg_win(win_T *wp, int where, char_u *t_s, char_u *end, linenr_T lnum)
2379{
Bram Moolenaarbeedd0a2022-08-27 21:52:52 +01002380 char_u *p;
Bram Moolenaar9198de32022-08-27 21:30:03 +01002381
Bram Moolenaar9198de32022-08-27 21:30:03 +01002382 if (where == PUT_BELOW)
Bram Moolenaarbeedd0a2022-08-27 21:52:52 +01002383 {
2384 if (*end != NUL)
2385 {
2386 p = vim_strnsave(t_s, end - t_s);
2387 if (p == NULL)
2388 return;
2389 }
2390 else
2391 p = t_s;
2392 ml_append_buf(wp->w_buffer, lnum, p, (colnr_T)0, FALSE);
2393 if (p != t_s)
2394 vim_free(p);
2395 }
Bram Moolenaar9198de32022-08-27 21:30:03 +01002396 else
2397 {
2398 char_u *newp;
2399
2400 curbuf = wp->w_buffer;
2401 if (where == PUT_APPEND)
Bram Moolenaarbeedd0a2022-08-27 21:52:52 +01002402 {
Bram Moolenaar9198de32022-08-27 21:30:03 +01002403 newp = concat_str(ml_get(lnum), t_s);
Bram Moolenaarbeedd0a2022-08-27 21:52:52 +01002404 if (newp == NULL)
2405 return;
2406 if (*end != NUL)
2407 newp[STRLEN(ml_get(lnum)) + (end - t_s)] = NUL;
2408 }
Bram Moolenaar9198de32022-08-27 21:30:03 +01002409 else
Bram Moolenaarbeedd0a2022-08-27 21:52:52 +01002410 {
Bram Moolenaar9198de32022-08-27 21:30:03 +01002411 newp = vim_strnsave(t_s, end - t_s);
Bram Moolenaarbeedd0a2022-08-27 21:52:52 +01002412 if (newp == NULL)
2413 return;
2414 }
Bram Moolenaar9198de32022-08-27 21:30:03 +01002415 ml_replace(lnum, newp, FALSE);
2416 curbuf = curwin->w_buffer;
2417 }
2418 redraw_win_later(wp, UPD_NOT_VALID);
2419
2420 // set msg_col so that a newline is written if needed
Bram Moolenaar24735f22022-08-30 15:44:22 +01002421 msg_col += (int)(end - t_s);
Bram Moolenaar9198de32022-08-27 21:30:03 +01002422}
2423#endif
2424
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002425/*
2426 * The display part of msg_puts_attr_len().
2427 * May be called recursively to display scroll-back text.
2428 */
2429 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002430msg_puts_display(
2431 char_u *str,
2432 int maxlen,
2433 int attr,
2434 int recurse)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002435{
2436 char_u *s = str;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002437 char_u *t_s = str; // string from "t_s" to "s" is still todo
2438 int t_col = 0; // screen cells todo, 0 when "t_s" not used
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002439 int l;
2440 int cw;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002441 char_u *sb_str = str;
2442 int sb_col = msg_col;
2443 int wrap;
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002444 int did_last_char;
Bram Moolenaar9198de32022-08-27 21:30:03 +01002445#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaard54af2e2022-08-27 22:05:13 +01002446 int where = PUT_APPEND;
Bram Moolenaar9198de32022-08-27 21:30:03 +01002447 win_T *msg_win = NULL;
2448 linenr_T lnum = 1;
2449
Bram Moolenaara2a89732022-08-31 14:46:18 +01002450 if (in_echowindow)
Bram Moolenaar9198de32022-08-27 21:30:03 +01002451 {
2452 msg_win = popup_get_message_win();
2453
2454 if (msg_win != NULL)
2455 {
2456 if (!popup_message_win_visible())
2457 {
2458 if (*str == NL)
2459 {
2460 // When not showing the message window and the output
2461 // starts with a NL show the message normally.
2462 msg_win = NULL;
2463 }
2464 else
2465 {
2466 // currently hidden, make it empty
2467 curbuf = msg_win->w_buffer;
2468 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
2469 ml_delete(1);
2470 curbuf = curwin->w_buffer;
2471 }
2472 }
2473 else
2474 {
2475 lnum = msg_win->w_buffer->b_ml.ml_line_count;
2476 if (msg_col == 0)
2477 where = PUT_TRUNC;
2478 }
2479 }
2480 }
2481#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482
2483 did_wait_return = FALSE;
Bram Moolenaar57b7fe82007-08-05 17:20:43 +00002484 while ((maxlen < 0 || (int)(s - str) < maxlen) && *s != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 {
2486 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002487 * We are at the end of the screen line when:
2488 * - When outputting a newline.
2489 * - When outputting a character in the last column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002491 if (!recurse && msg_row >= Rows - 1 && (*s == '\n' || (
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492#ifdef FEAT_RIGHTLEFT
2493 cmdmsg_rl
2494 ? (
2495 msg_col <= 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002496 || (*s == TAB && msg_col <= 7)
2497 || (has_mbyte && (*mb_ptr2cells)(s) > 1 && msg_col <= 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 :
2499#endif
Bram Moolenaar0efd1bd2019-12-11 19:00:04 +01002500 ((*s != '\r' && msg_col + t_col >= Columns - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 || (*s == TAB && msg_col + t_col >= ((Columns - 1) & ~7))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 || (has_mbyte && (*mb_ptr2cells)(s) > 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002503 && msg_col + t_col >= Columns - 2)))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002505 /*
2506 * The screen is scrolled up when at the last row (some terminals
2507 * scroll automatically, some don't. To avoid problems we scroll
2508 * ourselves).
2509 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510 if (t_col > 0)
Bram Moolenaar9198de32022-08-27 21:30:03 +01002511 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002512 // output postponed text
Bram Moolenaar9198de32022-08-27 21:30:03 +01002513#ifdef HAS_MESSAGE_WINDOW
2514 if (msg_win != NULL)
2515 {
2516 put_msg_win(msg_win, where, t_s, s, lnum);
2517 t_col = 0;
2518 where = PUT_BELOW;
2519 }
2520 else
2521#endif
2522 t_puts(&t_col, t_s, s, attr);
2523 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524
Bram Moolenaar85a20022019-12-21 18:25:54 +01002525 // When no more prompt and no more room, truncate here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 if (msg_no_more && lines_left == 0)
2527 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528
Bram Moolenaar9198de32022-08-27 21:30:03 +01002529#ifdef HAS_MESSAGE_WINDOW
2530 if (msg_win == NULL)
2531#endif
2532 // Scroll the screen up one line.
2533 msg_scroll_up();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534
2535 msg_row = Rows - 2;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002536 if (msg_col >= Columns) // can happen after screen resize
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 msg_col = Columns - 1;
2538
Bram Moolenaar85a20022019-12-21 18:25:54 +01002539 // Display char in last column before showing more-prompt.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002540 if (*s >= ' '
2541#ifdef FEAT_RIGHTLEFT
2542 && !cmdmsg_rl
2543#endif
2544 )
2545 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002546 if (has_mbyte)
2547 {
2548 if (enc_utf8 && maxlen >= 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002549 // avoid including composing chars after the end
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002550 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002551 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002552 l = (*mb_ptr2len)(s);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002553 s = screen_puts_mbyte(s, l, attr);
2554 }
2555 else
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002556 msg_screen_putchar(*s++, attr);
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002557 did_last_char = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002558 }
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002559 else
2560 did_last_char = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002561
2562 if (p_more)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002563 // store text for scrolling back
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002564 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
2565
Bram Moolenaar9198de32022-08-27 21:30:03 +01002566#ifdef HAS_MESSAGE_WINDOW
2567 if (msg_win == NULL)
2568 {
2569#endif
2570 inc_msg_scrolled();
Bram Moolenaar13608d82022-08-29 15:06:50 +01002571 need_wait_return = TRUE; // may need wait_return() in main()
Bram Moolenaar9198de32022-08-27 21:30:03 +01002572 redraw_cmdline = TRUE;
2573 if (cmdline_row > 0 && !exmode_active)
2574 --cmdline_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575
Bram Moolenaar9198de32022-08-27 21:30:03 +01002576 /*
2577 * If screen is completely filled and 'more' is set then wait
2578 * for a character.
2579 */
2580 if (lines_left > 0)
2581 --lines_left;
2582#ifdef HAS_MESSAGE_WINDOW
2583 }
2584#endif
Bram Moolenaar24959102022-05-07 20:01:16 +01002585 if (p_more && lines_left == 0 && State != MODE_HITRETURN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 && !msg_no_more && !exmode_active)
2587 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588#ifdef FEAT_CON_DIALOG
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002589 if (do_more_prompt(NUL))
2590 s = confirm_msg_tail;
2591#else
2592 (void)do_more_prompt(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593#endif
2594 if (quit_more)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002595 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002597
Bram Moolenaar85a20022019-12-21 18:25:54 +01002598 // When we displayed a char in last column need to check if there
2599 // is still more.
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002600 if (did_last_char)
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002601 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 }
2603
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002604 wrap = *s == '\n'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 || msg_col + t_col >= Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 || (has_mbyte && (*mb_ptr2cells)(s) > 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002607 && msg_col + t_col >= Columns - 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002608 if (t_col > 0 && (wrap || *s == '\r' || *s == '\b'
2609 || *s == '\t' || *s == BELL))
Bram Moolenaar9198de32022-08-27 21:30:03 +01002610 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002611 // output any postponed text
Bram Moolenaar9198de32022-08-27 21:30:03 +01002612#ifdef HAS_MESSAGE_WINDOW
2613 if (msg_win != NULL)
2614 {
2615 put_msg_win(msg_win, where, t_s, s, lnum);
2616 t_col = 0;
2617 where = PUT_BELOW;
2618 }
2619 else
2620#endif
2621 t_puts(&t_col, t_s, s, attr);
2622 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002623
2624 if (wrap && p_more && !recurse)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002625 // store text for scrolling back
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002626 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627
Bram Moolenaar85a20022019-12-21 18:25:54 +01002628 if (*s == '\n') // go to next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 {
Bram Moolenaar9198de32022-08-27 21:30:03 +01002630#ifdef HAS_MESSAGE_WINDOW
2631 if (msg_win != NULL)
2632 {
2633 // Ignore a NL when the buffer is empty, it is used to scroll
2634 // up the text.
2635 if ((msg_win->w_buffer->b_ml.ml_flags & ML_EMPTY) == 0)
2636 {
2637 put_msg_win(msg_win, PUT_BELOW, t_s, t_s, lnum);
2638 ++lnum;
2639 }
2640 }
2641 else
2642#endif
2643 msg_didout = FALSE; // remember that line is empty
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002644#ifdef FEAT_RIGHTLEFT
2645 if (cmdmsg_rl)
2646 msg_col = Columns - 1;
2647 else
2648#endif
2649 msg_col = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002650 if (++msg_row >= Rows) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 msg_row = Rows - 1;
2652 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002653 else if (*s == '\r') // go to column 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 {
2655 msg_col = 0;
Bram Moolenaard54af2e2022-08-27 22:05:13 +01002656#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaar9198de32022-08-27 21:30:03 +01002657 where = PUT_TRUNC;
Bram Moolenaard54af2e2022-08-27 22:05:13 +01002658#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002660 else if (*s == '\b') // go to previous char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 {
2662 if (msg_col)
2663 --msg_col;
2664 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002665 else if (*s == TAB) // translate Tab into spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 {
Bram Moolenaar9198de32022-08-27 21:30:03 +01002667#ifdef HAS_MESSAGE_WINDOW
2668 if (msg_win != NULL)
2669 msg_col = (msg_col + 7) % 8;
2670 else
2671#endif
2672 do
2673 msg_screen_putchar(' ', attr);
2674 while (msg_col & 7);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002676 else if (*s == BELL) // beep (from ":sh")
Bram Moolenaar165bc692015-07-21 17:53:25 +02002677 vim_beep(BO_SH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 else
2679 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 if (has_mbyte)
2681 {
2682 cw = (*mb_ptr2cells)(s);
2683 if (enc_utf8 && maxlen >= 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002684 // avoid including composing chars after the end
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002685 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002687 l = (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 }
2689 else
2690 {
2691 cw = 1;
2692 l = 1;
2693 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002694
Bram Moolenaar85a20022019-12-21 18:25:54 +01002695 // When drawing from right to left or when a double-wide character
2696 // doesn't fit, draw a single character here. Otherwise collect
2697 // characters and draw them all at once later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 if (
2699# ifdef FEAT_RIGHTLEFT
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002700 cmdmsg_rl ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701# endif
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002702 (cw > 1 && msg_col + t_col >= Columns - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 if (l > 1)
2705 s = screen_puts_mbyte(s, l, attr) - 1;
2706 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 msg_screen_putchar(*s, attr);
2708 }
2709 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002711 // postpone this character until later
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 if (t_col == 0)
2713 t_s = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 t_col += cw;
2715 s += l - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 }
2717 }
2718 ++s;
2719 }
2720
Bram Moolenaar85a20022019-12-21 18:25:54 +01002721 // output any postponed text
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 if (t_col > 0)
Bram Moolenaar9198de32022-08-27 21:30:03 +01002723 {
2724#ifdef HAS_MESSAGE_WINDOW
2725 if (msg_win != NULL)
2726 put_msg_win(msg_win, where, t_s, s, lnum);
2727 else
2728#endif
2729 t_puts(&t_col, t_s, s, attr);
2730 }
2731
2732#ifdef HAS_MESSAGE_WINDOW
2733 if (msg_win != NULL)
2734 popup_show_message_win();
2735#endif
Bram Moolenaar11901392022-09-26 19:50:44 +01002736 // Store the text for scroll back, unless it's a newline by itself.
2737 if (p_more && !recurse && !(s == sb_str + 1 && *sb_str == '\n'))
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002738 store_sb_text(&sb_str, s, attr, &sb_col, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739
2740 msg_check();
2741}
2742
2743/*
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002744 * Return TRUE when ":filter pattern" was used and "msg" does not match
2745 * "pattern".
2746 */
2747 int
2748message_filtered(char_u *msg)
2749{
Bram Moolenaard29459b2016-08-26 22:29:11 +02002750 int match;
2751
Bram Moolenaare1004402020-10-24 20:49:43 +02002752 if (cmdmod.cmod_filter_regmatch.regprog == NULL)
Bram Moolenaard29459b2016-08-26 22:29:11 +02002753 return FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02002754 match = vim_regexec(&cmdmod.cmod_filter_regmatch, msg, (colnr_T)0);
2755 return cmdmod.cmod_filter_force ? match : !match;
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002756}
2757
2758/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002759 * Scroll the screen up one line for displaying the next message line.
2760 */
2761 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002762msg_scroll_up(void)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002763{
Bram Moolenaar9198de32022-08-27 21:30:03 +01002764#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaara2a89732022-08-31 14:46:18 +01002765 if (in_echowindow)
Bram Moolenaar9198de32022-08-27 21:30:03 +01002766 return;
2767#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002768#ifdef FEAT_GUI
Bram Moolenaar85a20022019-12-21 18:25:54 +01002769 // Remove the cursor before scrolling, ScreenLines[] is going
2770 // to become invalid.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002771 if (gui.in_use)
2772 gui_undraw_cursor();
2773#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01002774 // scrolling up always works
Bram Moolenaara338adc2018-01-31 20:51:47 +01002775 mch_disable_flush();
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002776 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaara338adc2018-01-31 20:51:47 +01002777 mch_enable_flush();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002778
2779 if (!can_clear((char_u *)" "))
2780 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002781 // Scrolling up doesn't result in the right background. Set the
2782 // background here. It's not efficient, but avoids that we have to do
2783 // it all over the code.
Shougo Matsushitabe2b03c2024-04-08 22:11:50 +02002784 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns,
2785 ' ', ' ', HL_ATTR(HLF_MSG));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002786
Bram Moolenaar85a20022019-12-21 18:25:54 +01002787 // Also clear the last char of the last but one line if it was not
2788 // cleared before to avoid a scroll-up.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002789 if (ScreenAttrs[LineOffset[Rows - 2] + Columns - 1] == (sattr_T)-1)
2790 screen_fill((int)Rows - 2, (int)Rows - 1,
Shougo Matsushitabe2b03c2024-04-08 22:11:50 +02002791 (int)Columns - 1, (int)Columns,
2792 ' ', ' ', HL_ATTR(HLF_MSG));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002793 }
2794}
2795
2796/*
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002797 * Increment "msg_scrolled".
2798 */
2799 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002800inc_msg_scrolled(void)
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002801{
2802#ifdef FEAT_EVAL
2803 if (*get_vim_var_str(VV_SCROLLSTART) == NUL)
2804 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002805 char_u *p = SOURCING_NAME;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002806 char_u *tofree = NULL;
2807 int len;
2808
Bram Moolenaar85a20022019-12-21 18:25:54 +01002809 // v:scrollstart is empty, set it to the script/function name and line
2810 // number
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002811 if (p == NULL)
2812 p = (char_u *)_("Unknown");
2813 else
2814 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002815 len = (int)STRLEN(p) + 40;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002816 tofree = alloc(len);
2817 if (tofree != NULL)
2818 {
2819 vim_snprintf((char *)tofree, len, _("%s line %ld"),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002820 p, (long)SOURCING_LNUM);
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002821 p = tofree;
2822 }
2823 }
2824 set_vim_var_string(VV_SCROLLSTART, p, -1);
2825 vim_free(tofree);
2826 }
2827#endif
2828 ++msg_scrolled;
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01002829 set_must_redraw(UPD_VALID);
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002830}
2831
2832/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002833 * To be able to scroll back at the "more" and "hit-enter" prompts we need to
2834 * store the displayed text and remember where screen lines start.
2835 */
2836typedef struct msgchunk_S msgchunk_T;
2837struct msgchunk_S
2838{
2839 msgchunk_T *sb_next;
2840 msgchunk_T *sb_prev;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002841 char sb_eol; // TRUE when line ends after this text
2842 int sb_msg_col; // column in which text starts
2843 int sb_attr; // text attributes
2844 char_u sb_text[1]; // text to be displayed, actually longer
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002845};
2846
Bram Moolenaar85a20022019-12-21 18:25:54 +01002847static msgchunk_T *last_msgchunk = NULL; // last displayed text
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002848
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002849static msgchunk_T *msg_sb_start(msgchunk_T *mps);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002850
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002851typedef enum {
2852 SB_CLEAR_NONE = 0,
2853 SB_CLEAR_ALL,
2854 SB_CLEAR_CMDLINE_BUSY,
2855 SB_CLEAR_CMDLINE_DONE
2856} sb_clear_T;
2857
Bram Moolenaar85a20022019-12-21 18:25:54 +01002858// When to clear text on next msg.
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002859static sb_clear_T do_clear_sb_text = SB_CLEAR_NONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002860
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002861/*
2862 * Store part of a printed message for displaying when scrolling back.
2863 */
2864 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002865store_sb_text(
Bram Moolenaar85a20022019-12-21 18:25:54 +01002866 char_u **sb_str, // start of string
2867 char_u *s, // just after string
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002868 int attr,
2869 int *sb_col,
Bram Moolenaar85a20022019-12-21 18:25:54 +01002870 int finish) // line ends
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002871{
2872 msgchunk_T *mp;
2873
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002874 if (do_clear_sb_text == SB_CLEAR_ALL
2875 || do_clear_sb_text == SB_CLEAR_CMDLINE_DONE)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002876 {
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002877 clear_sb_text(do_clear_sb_text == SB_CLEAR_ALL);
zeertzjq46af7bc2022-07-28 12:34:09 +01002878 msg_sb_eol(); // prevent messages from overlapping
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002879 do_clear_sb_text = SB_CLEAR_NONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002880 }
2881
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002882 if (s > *sb_str)
2883 {
zeertzjq1b438a82023-02-01 13:11:15 +00002884 mp = alloc(offsetof(msgchunk_T, sb_text) + (s - *sb_str) + 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002885 if (mp != NULL)
2886 {
2887 mp->sb_eol = finish;
2888 mp->sb_msg_col = *sb_col;
2889 mp->sb_attr = attr;
2890 vim_strncpy(mp->sb_text, *sb_str, s - *sb_str);
2891
2892 if (last_msgchunk == NULL)
2893 {
2894 last_msgchunk = mp;
2895 mp->sb_prev = NULL;
2896 }
2897 else
2898 {
2899 mp->sb_prev = last_msgchunk;
2900 last_msgchunk->sb_next = mp;
2901 last_msgchunk = mp;
2902 }
2903 mp->sb_next = NULL;
2904 }
2905 }
2906 else if (finish && last_msgchunk != NULL)
2907 last_msgchunk->sb_eol = TRUE;
2908
2909 *sb_str = s;
2910 *sb_col = 0;
2911}
2912
2913/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002914 * Finished showing messages, clear the scroll-back text on the next message.
2915 */
2916 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002917may_clear_sb_text(void)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002918{
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002919 do_clear_sb_text = SB_CLEAR_ALL;
2920}
2921
2922/*
zeertzjq46af7bc2022-07-28 12:34:09 +01002923 * Starting to edit the command line: do not clear messages now.
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002924 */
2925 void
2926sb_text_start_cmdline(void)
2927{
zeertzjq46af7bc2022-07-28 12:34:09 +01002928 if (do_clear_sb_text == SB_CLEAR_CMDLINE_BUSY)
2929 // Invoking command line recursively: the previous-level command line
2930 // doesn't need to be remembered as it will be redrawn when returning
2931 // to that level.
2932 sb_text_restart_cmdline();
2933 else
2934 {
2935 msg_sb_eol();
2936 do_clear_sb_text = SB_CLEAR_CMDLINE_BUSY;
2937 }
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002938}
2939
2940/*
zeertzjq46af7bc2022-07-28 12:34:09 +01002941 * Redrawing the command line: clear the last unfinished line.
2942 */
2943 void
2944sb_text_restart_cmdline(void)
2945{
2946 msgchunk_T *tofree;
2947
2948 // Needed when returning from nested command line.
2949 do_clear_sb_text = SB_CLEAR_CMDLINE_BUSY;
2950
2951 if (last_msgchunk == NULL || last_msgchunk->sb_eol)
2952 // No unfinished line: don't clear anything.
2953 return;
2954
2955 tofree = msg_sb_start(last_msgchunk);
2956 last_msgchunk = tofree->sb_prev;
2957 if (last_msgchunk != NULL)
2958 last_msgchunk->sb_next = NULL;
2959 while (tofree != NULL)
2960 {
2961 msgchunk_T *tofree_next = tofree->sb_next;
2962
2963 vim_free(tofree);
2964 tofree = tofree_next;
2965 }
2966}
2967
2968/*
2969 * Ending to edit the command line: clear old lines but the last one later.
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002970 */
2971 void
2972sb_text_end_cmdline(void)
2973{
2974 do_clear_sb_text = SB_CLEAR_CMDLINE_DONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002975}
2976
2977/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002978 * Clear any text remembered for scrolling back.
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002979 * When "all" is FALSE keep the last line.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002980 * Called when redrawing the screen.
2981 */
2982 void
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002983clear_sb_text(int all)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002984{
2985 msgchunk_T *mp;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002986 msgchunk_T **lastp;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002987
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002988 if (all)
2989 lastp = &last_msgchunk;
2990 else
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002991 {
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002992 if (last_msgchunk == NULL)
2993 return;
zeertzjqecdc82e2022-07-25 19:50:57 +01002994 lastp = &msg_sb_start(last_msgchunk)->sb_prev;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002995 }
2996
2997 while (*lastp != NULL)
2998 {
2999 mp = (*lastp)->sb_prev;
3000 vim_free(*lastp);
3001 *lastp = mp;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003002 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003003}
3004
3005/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003006 * "g<" command.
3007 */
3008 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003009show_sb_text(void)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003010{
3011 msgchunk_T *mp;
3012
Bram Moolenaar85a20022019-12-21 18:25:54 +01003013 // Only show something if there is more than one line, otherwise it looks
3014 // weird, typing a command without output results in one line.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003015 mp = msg_sb_start(last_msgchunk);
3016 if (mp == NULL || mp->sb_prev == NULL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02003017 vim_beep(BO_MESS);
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003018 else
3019 {
3020 do_more_prompt('G');
3021 wait_return(FALSE);
3022 }
3023}
3024
3025/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003026 * Move to the start of screen line in already displayed text.
3027 */
3028 static msgchunk_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003029msg_sb_start(msgchunk_T *mps)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003030{
3031 msgchunk_T *mp = mps;
3032
3033 while (mp != NULL && mp->sb_prev != NULL && !mp->sb_prev->sb_eol)
3034 mp = mp->sb_prev;
3035 return mp;
3036}
3037
3038/*
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02003039 * Mark the last message chunk as finishing the line.
3040 */
3041 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003042msg_sb_eol(void)
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02003043{
3044 if (last_msgchunk != NULL)
3045 last_msgchunk->sb_eol = TRUE;
3046}
3047
3048/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003049 * Display a screen line from previously displayed text at row "row".
Bram Moolenaar838b7462022-09-26 15:19:56 +01003050 * When "clear_to_eol" is set clear the rest of the screen line.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003051 * Returns a pointer to the text for the next line (can be NULL).
3052 */
3053 static msgchunk_T *
Bram Moolenaar838b7462022-09-26 15:19:56 +01003054disp_sb_line(int row, msgchunk_T *smp, int clear_to_eol)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003055{
3056 msgchunk_T *mp = smp;
3057 char_u *p;
3058
3059 for (;;)
3060 {
3061 msg_row = row;
3062 msg_col = mp->sb_msg_col;
3063 p = mp->sb_text;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003064 if (*p == '\n') // don't display the line break
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003065 ++p;
3066 msg_puts_display(p, -1, mp->sb_attr, TRUE);
Bram Moolenaar838b7462022-09-26 15:19:56 +01003067
3068 // If clearing the screen did not work (e.g. because of a background
3069 // color and t_ut isn't set) clear until the last column here.
3070 if (clear_to_eol)
Shougo Matsushitabe2b03c2024-04-08 22:11:50 +02003071 screen_fill(row, row + 1, msg_col, (int)Columns,
3072 ' ', ' ', HL_ATTR(HLF_MSG));
Bram Moolenaar838b7462022-09-26 15:19:56 +01003073
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003074 if (mp->sb_eol || mp->sb_next == NULL)
3075 break;
3076 mp = mp->sb_next;
3077 }
3078 return mp->sb_next;
3079}
3080
3081/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082 * Output any postponed text for msg_puts_attr_len().
3083 */
3084 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003085t_puts(
3086 int *t_col,
3087 char_u *t_s,
3088 char_u *s,
3089 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090{
Bram Moolenaar85a20022019-12-21 18:25:54 +01003091 // output postponed text
3092 msg_didout = TRUE; // remember that line is not empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 screen_puts_len(t_s, (int)(s - t_s), msg_row, msg_col, attr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003094 msg_col += *t_col;
3095 *t_col = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003096 // If the string starts with a composing character don't increment the
3097 // column position for it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 if (enc_utf8 && utf_iscomposing(utf_ptr2char(t_s)))
3099 --msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 if (msg_col >= Columns)
3101 {
3102 msg_col = 0;
3103 ++msg_row;
3104 }
3105}
3106
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107/*
3108 * Returns TRUE when messages should be printed with mch_errmsg().
3109 * This is used when there is no valid screen, so we can see error messages.
3110 * If termcap is not active, we may be writing in an alternate console
3111 * window, cursor positioning may not work correctly (window size may be
3112 * different, e.g. for Win32 console) or we just don't know where the
3113 * cursor is.
3114 */
3115 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003116msg_use_printf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117{
3118 return (!msg_check_screen()
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003119#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3120# ifdef VIMDLL
3121 || (!gui.in_use && !termcap_active)
3122# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123 || !termcap_active
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003124# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125#endif
3126 || (swapping_screen() && !termcap_active)
3127 );
3128}
3129
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003130/*
3131 * Print a message when there is no valid screen.
3132 */
3133 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003134msg_puts_printf(char_u *str, int maxlen)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003135{
3136 char_u *s = str;
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003137 char_u *buf = NULL;
3138 char_u *p = s;
3139
Bram Moolenaar4f974752019-02-17 17:44:42 +01003140#ifdef MSWIN
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003141 if (!(silent_mode && p_verbose == 0))
Bram Moolenaar85a20022019-12-21 18:25:54 +01003142 mch_settmode(TMODE_COOK); // handle CR and NL correctly
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003143#endif
Bram Moolenaar2321ca22016-09-09 14:17:18 +02003144 while ((maxlen < 0 || (int)(s - str) < maxlen) && *s != NUL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003145 {
3146 if (!(silent_mode && p_verbose == 0))
3147 {
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003148 // NL --> CR NL translation (for Unix, not for "--version")
3149 if (*s == NL)
3150 {
3151 int n = (int)(s - p);
3152
3153 buf = alloc(n + 3);
Bram Moolenaar6f10c702019-08-20 22:58:37 +02003154 if (buf != NULL)
3155 {
3156 memcpy(buf, p, n);
3157 if (!info_message)
3158 buf[n++] = CAR;
3159 buf[n++] = NL;
3160 buf[n++] = NUL;
3161 if (info_message) // informative message, not an error
3162 mch_msg((char *)buf);
3163 else
3164 mch_errmsg((char *)buf);
3165 vim_free(buf);
3166 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003167 p = s + 1;
3168 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003169 }
3170
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003171 // primitive way to compute the current column
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003172#ifdef FEAT_RIGHTLEFT
3173 if (cmdmsg_rl)
3174 {
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003175 if (*s == CAR || *s == NL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003176 msg_col = Columns - 1;
3177 else
3178 --msg_col;
3179 }
3180 else
3181#endif
3182 {
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003183 if (*s == CAR || *s == NL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003184 msg_col = 0;
3185 else
3186 ++msg_col;
3187 }
3188 ++s;
3189 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003190
3191 if (*p != NUL && !(silent_mode && p_verbose == 0))
3192 {
Bram Moolenaara97c3632021-06-15 22:39:11 +02003193 char_u *tofree = NULL;
Bram Moolenaar97c2c052019-02-22 13:42:07 +01003194
Bram Moolenaarc32949b2023-01-04 15:56:51 +00003195 if (maxlen > 0 && vim_strlen_maxlen((char *)p, (size_t)maxlen)
3196 >= (size_t)maxlen)
Bram Moolenaar97c2c052019-02-22 13:42:07 +01003197 {
Bram Moolenaara97c3632021-06-15 22:39:11 +02003198 tofree = vim_strnsave(p, (size_t)maxlen);
3199 p = tofree;
Bram Moolenaar97c2c052019-02-22 13:42:07 +01003200 }
Bram Moolenaara97c3632021-06-15 22:39:11 +02003201 if (p != NULL)
3202 {
3203 if (info_message)
3204 mch_msg((char *)p);
3205 else
3206 mch_errmsg((char *)p);
3207 vim_free(tofree);
3208 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003209 }
3210
3211 msg_didout = TRUE; // assume that line is not empty
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003212
Bram Moolenaar4f974752019-02-17 17:44:42 +01003213#ifdef MSWIN
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003214 if (!(silent_mode && p_verbose == 0))
3215 mch_settmode(TMODE_RAW);
3216#endif
3217}
3218
3219/*
3220 * Show the more-prompt and handle the user response.
3221 * This takes care of scrolling back and displaying previously displayed text.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003222 * When at hit-enter prompt "typed_char" is the already typed character,
3223 * otherwise it's NUL.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003224 * Returns TRUE when jumping ahead to "confirm_msg_tail".
3225 */
3226 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003227do_more_prompt(int typed_char)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003228{
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01003229 static int entered = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003230 int used_typed_char = typed_char;
3231 int oldState = State;
3232 int c;
3233#ifdef FEAT_CON_DIALOG
3234 int retval = FALSE;
3235#endif
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003236 int toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003237 msgchunk_T *mp_last = NULL;
3238 msgchunk_T *mp;
3239 int i;
Shougo Matsushitabe2b03c2024-04-08 22:11:50 +02003240 int msg_attr;
3241
3242 msg_attr = HL_ATTR(HLF_MSG);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003243
Bram Moolenaar85a20022019-12-21 18:25:54 +01003244 // We get called recursively when a timer callback outputs a message. In
3245 // that case don't show another prompt. Also when at the hit-Enter prompt
3246 // and nothing was typed.
Bram Moolenaar24959102022-05-07 20:01:16 +01003247 if (entered || (State == MODE_HITRETURN && typed_char == 0))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01003248 return FALSE;
3249 entered = TRUE;
3250
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003251 if (typed_char == 'G')
3252 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003253 // "g<": Find first line on the last page.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003254 mp_last = msg_sb_start(last_msgchunk);
3255 for (i = 0; i < Rows - 2 && mp_last != NULL
3256 && mp_last->sb_prev != NULL; ++i)
3257 mp_last = msg_sb_start(mp_last->sb_prev);
3258 }
3259
Bram Moolenaar24959102022-05-07 20:01:16 +01003260 State = MODE_ASKMORE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003261 setmouse();
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003262 if (typed_char == NUL)
3263 msg_moremsg(FALSE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003264 for (;;)
3265 {
3266 /*
3267 * Get a typed character directly from the user.
3268 */
3269 if (used_typed_char != NUL)
3270 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003271 c = used_typed_char; // was typed at hit-enter prompt
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003272 used_typed_char = NUL;
3273 }
3274 else
3275 c = get_keystroke();
3276
3277#if defined(FEAT_MENU) && defined(FEAT_GUI)
3278 if (c == K_MENU)
3279 {
Bram Moolenaar24959102022-05-07 20:01:16 +01003280 int idx = get_menu_index(current_menu, MODE_ASKMORE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003281
Bram Moolenaar85a20022019-12-21 18:25:54 +01003282 // Used a menu. If it starts with CTRL-Y, it must
3283 // be a "Copy" for the clipboard. Otherwise
3284 // assume that we end
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003285 if (idx == MENU_INDEX_INVALID)
3286 continue;
3287 c = *current_menu->strings[idx];
3288 if (c != NUL && current_menu->strings[idx][1] != NUL)
3289 ins_typebuf(current_menu->strings[idx] + 1,
3290 current_menu->noremap[idx], 0, TRUE,
3291 current_menu->silent[idx]);
3292 }
3293#endif
3294
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003295 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003296 switch (c)
3297 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003298 case BS: // scroll one line back
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003299 case K_BS:
3300 case 'k':
3301 case K_UP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003302 toscroll = -1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003303 break;
3304
Bram Moolenaar85a20022019-12-21 18:25:54 +01003305 case CAR: // one extra line
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003306 case NL:
3307 case 'j':
3308 case K_DOWN:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003309 toscroll = 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003310 break;
3311
Bram Moolenaar85a20022019-12-21 18:25:54 +01003312 case 'u': // Up half a page
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003313 toscroll = -(Rows / 2);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003314 break;
3315
Bram Moolenaar85a20022019-12-21 18:25:54 +01003316 case 'd': // Down half a page
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003317 toscroll = Rows / 2;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003318 break;
3319
Bram Moolenaar85a20022019-12-21 18:25:54 +01003320 case 'b': // one page back
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00003321 case K_PAGEUP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003322 toscroll = -(Rows - 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003323 break;
3324
Bram Moolenaar85a20022019-12-21 18:25:54 +01003325 case ' ': // one extra page
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00003326 case 'f':
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003327 case K_PAGEDOWN:
3328 case K_LEFTMOUSE:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003329 toscroll = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003330 break;
3331
Bram Moolenaar85a20022019-12-21 18:25:54 +01003332 case 'g': // all the way back to the start
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003333 toscroll = -999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003334 break;
3335
Bram Moolenaar85a20022019-12-21 18:25:54 +01003336 case 'G': // all the way to the end
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003337 toscroll = 999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003338 lines_left = 999999;
3339 break;
3340
Bram Moolenaar85a20022019-12-21 18:25:54 +01003341 case ':': // start new command line
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003342#ifdef FEAT_CON_DIALOG
3343 if (!confirm_msg_used)
3344#endif
3345 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003346 // Since got_int is set all typeahead will be flushed, but we
3347 // want to keep this ':', remember that in a special way.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003348 typeahead_noflush(':');
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02003349#ifdef FEAT_TERMINAL
3350 skip_term_loop = TRUE;
3351#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01003352 cmdline_row = Rows - 1; // put ':' on this line
3353 skip_redraw = TRUE; // skip redraw once
3354 need_wait_return = FALSE; // don't wait in main()
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003355 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003356 // FALLTHROUGH
3357 case 'q': // quit
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003358 case Ctrl_C:
3359 case ESC:
3360#ifdef FEAT_CON_DIALOG
3361 if (confirm_msg_used)
3362 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003363 // Jump to the choices of the dialog.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003364 retval = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003365 }
3366 else
3367#endif
3368 {
3369 got_int = TRUE;
3370 quit_more = TRUE;
3371 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003372 // When there is some more output (wrapping line) display that
3373 // without another prompt.
Bram Moolenaar51306d22009-02-24 03:38:04 +00003374 lines_left = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003375 break;
3376
3377#ifdef FEAT_CLIPBOARD
3378 case Ctrl_Y:
Bram Moolenaar85a20022019-12-21 18:25:54 +01003379 // Strange way to allow copying (yanking) a modeless
3380 // selection at the more prompt. Use CTRL-Y,
3381 // because the same is used in Cmdline-mode and at the
3382 // hit-enter prompt. However, scrolling one line up
3383 // might be expected...
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003384 if (clip_star.state == SELECT_DONE)
3385 clip_copy_modeless_selection(TRUE);
3386 continue;
3387#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01003388 default: // no valid response
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003389 msg_moremsg(TRUE);
3390 continue;
3391 }
3392
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003393 if (toscroll != 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003394 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003395 if (toscroll < 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003396 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003397 // go to start of last line
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003398 if (mp_last == NULL)
3399 mp = msg_sb_start(last_msgchunk);
3400 else if (mp_last->sb_prev != NULL)
3401 mp = msg_sb_start(mp_last->sb_prev);
3402 else
3403 mp = NULL;
3404
Bram Moolenaar85a20022019-12-21 18:25:54 +01003405 // go to start of line at top of the screen
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003406 for (i = 0; i < Rows - 2 && mp != NULL && mp->sb_prev != NULL;
3407 ++i)
3408 mp = msg_sb_start(mp->sb_prev);
3409
3410 if (mp != NULL && mp->sb_prev != NULL)
3411 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003412 // Find line to be displayed at top.
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003413 for (i = 0; i > toscroll; --i)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003414 {
3415 if (mp == NULL || mp->sb_prev == NULL)
3416 break;
3417 mp = msg_sb_start(mp->sb_prev);
3418 if (mp_last == NULL)
3419 mp_last = msg_sb_start(last_msgchunk);
3420 else
3421 mp_last = msg_sb_start(mp_last->sb_prev);
3422 }
3423
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003424 if (toscroll == -1 && screen_ins_lines(0, 0, 1,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003425 (int)Rows, 0, NULL) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003426 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003427 // display line at top
Bram Moolenaar838b7462022-09-26 15:19:56 +01003428 (void)disp_sb_line(0, mp, FALSE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003429 }
3430 else
3431 {
Bram Moolenaar838b7462022-09-26 15:19:56 +01003432 int did_clear = screenclear();
3433
Bram Moolenaar85a20022019-12-21 18:25:54 +01003434 // redisplay all lines
Bram Moolenaar773560b2006-05-06 21:38:18 +00003435 for (i = 0; mp != NULL && i < Rows - 1; ++i)
3436 {
Bram Moolenaar838b7462022-09-26 15:19:56 +01003437 mp = disp_sb_line(i, mp, !did_clear);
Bram Moolenaar773560b2006-05-06 21:38:18 +00003438 ++msg_scrolled;
3439 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003440 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003441 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003442 }
3443 }
3444 else
3445 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003446 // First display any text that we scrolled back.
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003447 while (toscroll > 0 && mp_last != NULL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003448 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003449 // scroll up, display line at bottom
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003450 msg_scroll_up();
Bram Moolenaarbb15b652005-10-03 21:52:09 +00003451 inc_msg_scrolled();
Shougo Matsushitabe2b03c2024-04-08 22:11:50 +02003452 screen_fill((int)Rows - 2, (int)Rows - 1, 0, (int)Columns,
3453 ' ', ' ', msg_attr);
Bram Moolenaar838b7462022-09-26 15:19:56 +01003454 mp_last = disp_sb_line((int)Rows - 2, mp_last, FALSE);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003455 --toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003456 }
3457 }
3458
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003459 if (toscroll <= 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003460 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003461 // displayed the requested text, more prompt again
Shougo Matsushitabe2b03c2024-04-08 22:11:50 +02003462 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns,
3463 ' ', ' ', msg_attr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003464 msg_moremsg(FALSE);
3465 continue;
3466 }
3467
Bram Moolenaar85a20022019-12-21 18:25:54 +01003468 // display more text, return to caller
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003469 lines_left = toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003470 }
3471
3472 break;
3473 }
3474
Bram Moolenaar85a20022019-12-21 18:25:54 +01003475 // clear the --more-- message
Shougo Matsushitabe2b03c2024-04-08 22:11:50 +02003476 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', msg_attr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003477 State = oldState;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003478 setmouse();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003479 if (quit_more)
3480 {
3481 msg_row = Rows - 1;
3482 msg_col = 0;
3483 }
3484#ifdef FEAT_RIGHTLEFT
3485 else if (cmdmsg_rl)
3486 msg_col = Columns - 1;
3487#endif
3488
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01003489 entered = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003490#ifdef FEAT_CON_DIALOG
3491 return retval;
3492#else
3493 return FALSE;
3494#endif
3495}
3496
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497#if defined(USE_MCH_ERRMSG) || defined(PROTO)
3498
3499#ifdef mch_errmsg
3500# undef mch_errmsg
3501#endif
3502#ifdef mch_msg
3503# undef mch_msg
3504#endif
3505
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003506#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3507 static void
3508mch_errmsg_c(char *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509{
Bram Moolenaar0f77d6a2019-02-14 20:55:09 +01003510 int len = (int)STRLEN(str);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003511 DWORD nwrite = 0;
3512 DWORD mode = 0;
3513 HANDLE h = GetStdHandle(STD_ERROR_HANDLE);
3514
3515 if (GetConsoleMode(h, &mode) && enc_codepage >= 0
3516 && (int)GetConsoleCP() != enc_codepage)
3517 {
3518 WCHAR *w = enc_to_utf16((char_u *)str, &len);
3519
3520 WriteConsoleW(h, w, len, &nwrite, NULL);
3521 vim_free(w);
3522 }
3523 else
3524 {
3525 fprintf(stderr, "%s", str);
3526 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003527}
3528#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003530/*
3531 * Give an error message. To be used when the screen hasn't been initialized
3532 * yet. When stderr can't be used, collect error messages until the GUI has
3533 * started and they can be displayed in a message box.
3534 */
3535 void
3536mch_errmsg(char *str)
3537{
3538#if !defined(MSWIN) || defined(FEAT_GUI_MSWIN)
3539 int len;
3540#endif
3541
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02003542#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003543 // On Unix use stderr if it's a tty.
3544 // When not going to start the GUI also use stderr.
3545 // On Mac, when started from Finder, stderr is the console.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 if (
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003547# ifdef UNIX
3548# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003550# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 isatty(2)
3552# endif
3553# ifdef FEAT_GUI
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003554 ||
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003555# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003556# endif
3557# ifdef FEAT_GUI
3558 !(gui.in_use || gui.starting)
3559# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 )
3561 {
3562 fprintf(stderr, "%s", str);
3563 return;
3564 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003565#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003567#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3568# ifdef VIMDLL
3569 if (!(gui.in_use || gui.starting))
3570# endif
3571 {
3572 mch_errmsg_c(str);
3573 return;
3574 }
3575#endif
3576
3577#if !defined(MSWIN) || defined(FEAT_GUI_MSWIN)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003578 // avoid a delay for a message that isn't there
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 emsg_on_display = FALSE;
3580
3581 len = (int)STRLEN(str) + 1;
3582 if (error_ga.ga_growsize == 0)
3583 {
3584 error_ga.ga_growsize = 80;
3585 error_ga.ga_itemsize = 1;
3586 }
3587 if (ga_grow(&error_ga, len) == OK)
3588 {
3589 mch_memmove((char_u *)error_ga.ga_data + error_ga.ga_len,
3590 (char_u *)str, len);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003591# ifdef UNIX
Bram Moolenaar85a20022019-12-21 18:25:54 +01003592 // remove CR characters, they are displayed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 {
3594 char_u *p;
3595
3596 p = (char_u *)error_ga.ga_data + error_ga.ga_len;
3597 for (;;)
3598 {
3599 p = vim_strchr(p, '\r');
3600 if (p == NULL)
3601 break;
3602 *p = ' ';
3603 }
3604 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003605# endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01003606 --len; // don't count the NUL at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 error_ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003609#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610}
3611
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003612#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3613 static void
3614mch_msg_c(char *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615{
Bram Moolenaar0f77d6a2019-02-14 20:55:09 +01003616 int len = (int)STRLEN(str);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003617 DWORD nwrite = 0;
3618 DWORD mode;
3619 HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
3620
3621
3622 if (GetConsoleMode(h, &mode) && enc_codepage >= 0
3623 && (int)GetConsoleCP() != enc_codepage)
3624 {
3625 WCHAR *w = enc_to_utf16((char_u *)str, &len);
3626
3627 WriteConsoleW(h, w, len, &nwrite, NULL);
3628 vim_free(w);
3629 }
3630 else
3631 {
3632 printf("%s", str);
3633 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003634}
3635#endif
3636
3637/*
3638 * Give a message. To be used when the screen hasn't been initialized yet.
3639 * When there is no tty, collect messages until the GUI has started and they
3640 * can be displayed in a message box.
3641 */
3642 void
3643mch_msg(char *str)
3644{
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02003645#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003646 // On Unix use stdout if we have a tty. This allows "vim -h | more" and
3647 // uses mch_errmsg() when started from the desktop.
3648 // When not going to start the GUI also use stdout.
3649 // On Mac, when started from Finder, stderr is the console.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 if (
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003651# ifdef UNIX
3652# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003654# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 isatty(2)
Bram Moolenaareae1b912019-05-09 15:12:55 +02003656# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657# ifdef FEAT_GUI
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003658 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003660# endif
3661# ifdef FEAT_GUI
3662 !(gui.in_use || gui.starting)
3663# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 )
3665 {
3666 printf("%s", str);
3667 return;
3668 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003669#endif
3670
3671#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3672# ifdef VIMDLL
3673 if (!(gui.in_use || gui.starting))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003675 {
3676 mch_msg_c(str);
3677 return;
3678 }
3679#endif
3680#if !defined(MSWIN) || defined(FEAT_GUI_MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 mch_errmsg(str);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003682#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683}
Bram Moolenaar85a20022019-12-21 18:25:54 +01003684#endif // USE_MCH_ERRMSG
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685
3686/*
3687 * Put a character on the screen at the current message position and advance
3688 * to the next position. Only for printable ASCII!
3689 */
3690 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003691msg_screen_putchar(int c, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692{
Bram Moolenaar85a20022019-12-21 18:25:54 +01003693 msg_didout = TRUE; // remember that line is not empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694 screen_putchar(c, msg_row, msg_col, attr);
3695#ifdef FEAT_RIGHTLEFT
3696 if (cmdmsg_rl)
3697 {
3698 if (--msg_col == 0)
3699 {
3700 msg_col = Columns;
3701 ++msg_row;
3702 }
3703 }
3704 else
3705#endif
3706 {
3707 if (++msg_col >= Columns)
3708 {
3709 msg_col = 0;
3710 ++msg_row;
3711 }
3712 }
3713}
3714
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003715 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003716msg_moremsg(int full)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003718 int attr;
3719 char_u *s = (char_u *)_("-- More --");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720
Bram Moolenaar8820b482017-03-16 17:23:31 +01003721 attr = HL_ATTR(HLF_M);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003722 screen_puts(s, (int)Rows - 1, 0, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 if (full)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003724 screen_puts((char_u *)
3725 _(" SPACE/d/j: screen/page/line down, b/u/k: up, q: quit "),
3726 (int)Rows - 1, vim_strsize(s), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727}
3728
3729/*
Bram Moolenaar24959102022-05-07 20:01:16 +01003730 * Repeat the message for the current mode: MODE_ASKMORE, MODE_EXTERNCMD,
3731 * MODE_CONFIRM or exmode_active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 */
3733 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003734repeat_message(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735{
Bram Moolenaar24959102022-05-07 20:01:16 +01003736 if (State == MODE_ASKMORE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003738 msg_moremsg(TRUE); // display --more-- message again
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 msg_row = Rows - 1;
3740 }
3741#ifdef FEAT_CON_DIALOG
Bram Moolenaar24959102022-05-07 20:01:16 +01003742 else if (State == MODE_CONFIRM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003744 display_confirm_msg(); // display ":confirm" message again
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 msg_row = Rows - 1;
3746 }
3747#endif
Bram Moolenaar24959102022-05-07 20:01:16 +01003748 else if (State == MODE_EXTERNCMD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003750 windgoto(msg_row, msg_col); // put cursor back
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 }
Bram Moolenaar24959102022-05-07 20:01:16 +01003752 else if (State == MODE_HITRETURN || State == MODE_SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 {
Bram Moolenaar9f108752007-11-24 14:44:58 +00003754 if (msg_row == Rows - 1)
3755 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003756 // Avoid drawing the "hit-enter" prompt below the previous one,
3757 // overwrite it. Esp. useful when regaining focus and a
3758 // FocusGained autocmd exists but didn't draw anything.
Bram Moolenaar9f108752007-11-24 14:44:58 +00003759 msg_didout = FALSE;
3760 msg_col = 0;
3761 msg_clr_eos();
3762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 hit_return_msg();
3764 msg_row = Rows - 1;
3765 }
3766}
3767
3768/*
3769 * msg_check_screen - check if the screen is initialized.
3770 * Also check msg_row and msg_col, if they are too big it may cause a crash.
3771 * While starting the GUI the terminal codes will be set for the GUI, but the
3772 * output goes to the terminal. Don't use the terminal codes then.
3773 */
3774 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003775msg_check_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776{
3777 if (!full_screen || !screen_valid(FALSE))
3778 return FALSE;
3779
3780 if (msg_row >= Rows)
3781 msg_row = Rows - 1;
3782 if (msg_col >= Columns)
3783 msg_col = Columns - 1;
3784 return TRUE;
3785}
3786
3787/*
3788 * Clear from current message position to end of screen.
3789 * Skip this when ":silent" was used, no need to clear for redirection.
3790 */
3791 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003792msg_clr_eos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793{
3794 if (msg_silent == 0)
3795 msg_clr_eos_force();
3796}
3797
3798/*
3799 * Clear from current message position to end of screen.
3800 * Note: msg_col is not updated, so we remember the end of the message
3801 * for msg_check().
3802 */
3803 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003804msg_clr_eos_force(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805{
Bram Moolenaar3b474dc2022-09-01 17:01:32 +01003806#ifdef HAS_MESSAGE_WINDOW
3807 if (in_echowindow)
3808 return; // messages go into a popup
3809#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 if (msg_use_printf())
3811 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003812 if (full_screen) // only when termcap codes are valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 {
3814 if (*T_CD)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003815 out_str(T_CD); // clear to end of display
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816 else if (*T_CE)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003817 out_str(T_CE); // clear to end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 }
3819 }
Bram Moolenaara2a89732022-08-31 14:46:18 +01003820 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 {
Shougo Matsushitabe2b03c2024-04-08 22:11:50 +02003822 int msg_attr;
3823
3824 msg_attr = HL_ATTR(HLF_MSG);
3825
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826#ifdef FEAT_RIGHTLEFT
3827 if (cmdmsg_rl)
3828 {
Shougo Matsushitabe2b03c2024-04-08 22:11:50 +02003829 screen_fill(msg_row, msg_row + 1, 0, msg_col + 1,
3830 ' ', ' ', msg_attr);
3831 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns,
3832 ' ', ' ', msg_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 }
3834 else
3835#endif
3836 {
3837 screen_fill(msg_row, msg_row + 1, msg_col, (int)Columns,
Shougo Matsushitabe2b03c2024-04-08 22:11:50 +02003838 ' ', ' ', msg_attr);
3839 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns,
3840 ' ', ' ', msg_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 }
3842 }
3843}
3844
3845/*
3846 * Clear the command line.
3847 */
3848 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003849msg_clr_cmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850{
3851 msg_row = cmdline_row;
3852 msg_col = 0;
3853 msg_clr_eos_force();
3854}
3855
3856/*
3857 * end putting a message on the screen
Bram Moolenaar13608d82022-08-29 15:06:50 +01003858 * call wait_return() if the message does not fit in the available space
3859 * return TRUE if wait_return() not called.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 */
3861 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003862msg_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863{
3864 /*
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02003865 * If the string is larger than the window,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 * or the ruler option is set and we run into it,
3867 * we have to redraw the window.
3868 * Do not do this if we are abandoning the file or editing the command line.
3869 */
Bram Moolenaar24959102022-05-07 20:01:16 +01003870 if (!exiting && need_wait_return && !(State & MODE_CMDLINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 {
3872 wait_return(FALSE);
3873 return FALSE;
3874 }
3875 out_flush();
3876 return TRUE;
3877}
3878
3879/*
3880 * If the written message runs into the shown command or ruler, we have to
3881 * wait for hit-return and redraw the window later.
3882 */
3883 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003884msg_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885{
Bram Moolenaar35a4fbc2022-08-28 14:39:53 +01003886 if (msg_row == Rows - 1 && msg_col >= sc_col
3887#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaara2a89732022-08-31 14:46:18 +01003888 && !in_echowindow
Bram Moolenaar35a4fbc2022-08-28 14:39:53 +01003889#endif
3890 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 {
3892 need_wait_return = TRUE;
3893 redraw_cmdline = TRUE;
3894 }
3895}
3896
3897/*
3898 * May write a string to the redirection file.
3899 * When "maxlen" is -1 write the whole string, otherwise up to "maxlen" bytes.
3900 */
3901 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003902redir_write(char_u *str, int maxlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903{
3904 char_u *s = str;
3905 static int cur_col = 0;
3906
Bram Moolenaar85a20022019-12-21 18:25:54 +01003907 // Don't do anything for displaying prompts and the like.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003908 if (redir_off)
3909 return;
3910
Bram Moolenaar85a20022019-12-21 18:25:54 +01003911 // If 'verbosefile' is set prepare for writing in that file.
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003912 if (*p_vfile != NUL && verbose_fd == NULL)
3913 verbose_open();
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003914
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003915 if (redirecting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003917 // If the string doesn't start with CR or NL, go to msg_col
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 if (*s != '\n' && *s != '\r')
3919 {
3920 while (cur_col < msg_col)
3921 {
3922#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003923 if (redir_execute)
3924 execute_redir_str((char_u *)" ", -1);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003925 else if (redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 write_reg_contents(redir_reg, (char_u *)" ", -1, TRUE);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003927 else if (redir_vname)
3928 var_redir_str((char_u *)" ", -1);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003929 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003931 if (redir_fd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 fputs(" ", redir_fd);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003933 if (verbose_fd != NULL)
3934 fputs(" ", verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 ++cur_col;
3936 }
3937 }
3938
3939#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003940 if (redir_execute)
3941 execute_redir_str(s, maxlen);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003942 else if (redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 write_reg_contents(redir_reg, s, maxlen, TRUE);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003944 else if (redir_vname)
Bram Moolenaardf177f62005-02-22 08:39:57 +00003945 var_redir_str(s, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946#endif
3947
Bram Moolenaar85a20022019-12-21 18:25:54 +01003948 // Write and adjust the current column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
3950 {
3951#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003952 if (!redir_reg && !redir_vname && !redir_execute)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003954 if (redir_fd != NULL)
3955 putc(*s, redir_fd);
3956 if (verbose_fd != NULL)
3957 putc(*s, verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 if (*s == '\r' || *s == '\n')
3959 cur_col = 0;
3960 else if (*s == '\t')
3961 cur_col += (8 - cur_col % 8);
3962 else
3963 ++cur_col;
3964 ++s;
3965 }
3966
Bram Moolenaar85a20022019-12-21 18:25:54 +01003967 if (msg_silent != 0) // should update msg_col
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 msg_col = cur_col;
3969 }
3970}
3971
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003972 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003973redirecting(void)
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003974{
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003975 return redir_fd != NULL || *p_vfile != NUL
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003976#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003977 || redir_reg || redir_vname || redir_execute
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003978#endif
3979 ;
3980}
3981
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982/*
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00003983 * Before giving verbose message.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003984 * Must always be called paired with verbose_leave()!
3985 */
3986 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003987verbose_enter(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003988{
3989 if (*p_vfile != NUL)
3990 ++msg_silent;
3991}
3992
3993/*
3994 * After giving verbose message.
3995 * Must always be called paired with verbose_enter()!
3996 */
3997 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003998verbose_leave(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003999{
4000 if (*p_vfile != NUL)
4001 if (--msg_silent < 0)
4002 msg_silent = 0;
4003}
4004
4005/*
4006 * Like verbose_enter() and set msg_scroll when displaying the message.
4007 */
4008 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004009verbose_enter_scroll(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00004010{
4011 if (*p_vfile != NUL)
4012 ++msg_silent;
4013 else
Bram Moolenaar85a20022019-12-21 18:25:54 +01004014 // always scroll up, don't overwrite
Bram Moolenaar8b044b32005-05-31 22:05:58 +00004015 msg_scroll = TRUE;
4016}
4017
4018/*
4019 * Like verbose_leave() and set cmdline_row when displaying the message.
4020 */
4021 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004022verbose_leave_scroll(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00004023{
4024 if (*p_vfile != NUL)
4025 {
4026 if (--msg_silent < 0)
4027 msg_silent = 0;
4028 }
4029 else
4030 cmdline_row = msg_row;
4031}
4032
Bram Moolenaar8b044b32005-05-31 22:05:58 +00004033/*
4034 * Called when 'verbosefile' is set: stop writing to the file.
4035 */
4036 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004037verbose_stop(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00004038{
4039 if (verbose_fd != NULL)
4040 {
4041 fclose(verbose_fd);
4042 verbose_fd = NULL;
4043 }
4044 verbose_did_open = FALSE;
4045}
4046
4047/*
4048 * Open the file 'verbosefile'.
4049 * Return FAIL or OK.
4050 */
4051 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004052verbose_open(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00004053{
4054 if (verbose_fd == NULL && !verbose_did_open)
4055 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004056 // Only give the error message once.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00004057 verbose_did_open = TRUE;
4058
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004059 verbose_fd = mch_fopen((char *)p_vfile, "a");
Bram Moolenaar8b044b32005-05-31 22:05:58 +00004060 if (verbose_fd == NULL)
4061 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004062 semsg(_(e_cant_open_file_str), p_vfile);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00004063 return FAIL;
4064 }
4065 }
4066 return OK;
4067}
4068
4069/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 * Give a warning message (for searching).
4071 * Use 'w' highlighting and may repeat the message after redrawing
4072 */
4073 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004074give_warning(char_u *message, int hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075{
Bram Moolenaarcc7eb2a2021-07-11 19:12:04 +02004076 give_warning_with_source(message, hl, FALSE);
4077}
4078
4079 void
4080give_warning_with_source(char_u *message, int hl, int with_source)
4081{
Bram Moolenaar85a20022019-12-21 18:25:54 +01004082 // Don't do this for ":silent".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 if (msg_silent != 0)
4084 return;
4085
Bram Moolenaar85a20022019-12-21 18:25:54 +01004086 // Don't want a hit-enter prompt here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 ++no_wait_return;
Bram Moolenaared203462004-06-16 11:19:22 +00004088
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089#ifdef FEAT_EVAL
4090 set_vim_var_string(VV_WARNINGMSG, message, -1);
4091#endif
Bram Moolenaard23a8232018-02-10 18:45:26 +01004092 VIM_CLEAR(keep_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 if (hl)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004094 keep_msg_attr = HL_ATTR(HLF_W);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 else
4096 keep_msg_attr = 0;
Bram Moolenaarcc7eb2a2021-07-11 19:12:04 +02004097
4098 if (with_source)
4099 {
4100 // Do what msg() does, but with a column offset if the warning should
4101 // be after the mode message.
4102 msg_start();
4103 msg_source(HL_ATTR(HLF_W));
4104 msg_puts(" ");
4105 msg_puts_attr((char *)message, HL_ATTR(HLF_W) | MSG_HIST);
4106 msg_clr_eos();
4107 (void)msg_end();
4108 }
4109 else if (msg_attr((char *)message, keep_msg_attr) && msg_scrolled == 0)
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004110 set_keep_msg(message, keep_msg_attr);
Bram Moolenaarcc7eb2a2021-07-11 19:12:04 +02004111
Bram Moolenaar85a20022019-12-21 18:25:54 +01004112 msg_didout = FALSE; // overwrite this message
4113 msg_nowait = TRUE; // don't wait for this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 msg_col = 0;
Bram Moolenaared203462004-06-16 11:19:22 +00004115
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 --no_wait_return;
4117}
4118
Bram Moolenaar113e1072019-01-20 15:30:40 +01004119#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarf8be4612017-06-23 20:52:40 +02004120 void
4121give_warning2(char_u *message, char_u *a1, int hl)
4122{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +02004123 if (IObuff == NULL)
4124 {
4125 // Very early in initialisation and already something wrong, just give
4126 // the raw message so the user at least gets a hint.
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00004127 give_warning(message, hl);
Bram Moolenaare3a22cb2019-10-14 22:01:57 +02004128 }
4129 else
4130 {
4131 vim_snprintf((char *)IObuff, IOSIZE, (char *)message, a1);
4132 give_warning(IObuff, hl);
4133 }
Bram Moolenaarf8be4612017-06-23 20:52:40 +02004134}
Bram Moolenaar113e1072019-01-20 15:30:40 +01004135#endif
Bram Moolenaarf8be4612017-06-23 20:52:40 +02004136
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137/*
4138 * Advance msg cursor to column "col".
4139 */
4140 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004141msg_advance(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142{
Bram Moolenaar85a20022019-12-21 18:25:54 +01004143 if (msg_silent != 0) // nothing to advance to
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004145 msg_col = col; // for redirection, may fill it up later
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 return;
4147 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01004148 if (col >= Columns) // not enough room
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 col = Columns - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004150#ifdef FEAT_RIGHTLEFT
4151 if (cmdmsg_rl)
4152 while (msg_col > Columns - col)
4153 msg_putchar(' ');
4154 else
4155#endif
4156 while (msg_col < col)
4157 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158}
4159
Christian Brabandt45e07042024-11-11 20:52:55 +01004160/*
4161 * Warn about missing Clipboard Support
4162 */
4163 void
4164msg_warn_missing_clipboard(void)
4165{
4166 if (!global_busy && !did_warn_clipboard)
4167 {
Christian Brabandt8b94afc2024-11-17 16:02:41 +01004168#ifdef FEAT_CLIPBOARD
4169 msg(_("W23: Clipboard register not available, using register 0"));
4170#else
4171 msg(_("W24: Clipboard register not available. See :h W24"));
4172#endif
Christian Brabandt45e07042024-11-11 20:52:55 +01004173 did_warn_clipboard = TRUE;
4174 }
4175}
4176
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177#if defined(FEAT_CON_DIALOG) || defined(PROTO)
4178/*
4179 * Used for "confirm()" function, and the :confirm command prefix.
4180 * Versions which haven't got flexible dialogs yet, and console
4181 * versions, get this generic handler which uses the command line.
4182 *
4183 * type = one of:
4184 * VIM_QUESTION, VIM_INFO, VIM_WARNING, VIM_ERROR or VIM_GENERIC
4185 * title = title string (can be NULL for default)
4186 * (neither used in console dialogs at the moment)
4187 *
4188 * Format of the "buttons" string:
4189 * "Button1Name\nButton2Name\nButton3Name"
4190 * The first button should normally be the default/accept
4191 * The second button should be the 'Cancel' button
4192 * Other buttons- use your imagination!
4193 * A '&' in a button name becomes a shortcut, so each '&' should be before a
4194 * different letter.
Rob Pilling8196e942022-02-11 15:12:10 +00004195 *
4196 * Returns 0 if cancelled, otherwise the nth button (1-indexed).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004199do_dialog(
4200 int type UNUSED,
4201 char_u *title UNUSED,
4202 char_u *message,
4203 char_u *buttons,
4204 int dfltbutton,
Bram Moolenaar85a20022019-12-21 18:25:54 +01004205 char_u *textfield UNUSED, // IObuff for inputdialog(), NULL
4206 // otherwise
4207 int ex_cmd) // when TRUE pressing : accepts default and starts
4208 // Ex command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209{
4210 int oldState;
4211 int retval = 0;
4212 char_u *hotkeys;
4213 int c;
4214 int i;
Bram Moolenaar27321db2020-07-06 21:24:57 +02004215 tmode_T save_tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216
4217#ifndef NO_CONSOLE
Bram Moolenaar85a20022019-12-21 18:25:54 +01004218 // Don't output anything in silent mode ("ex -s")
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 if (silent_mode)
Bram Moolenaar85a20022019-12-21 18:25:54 +01004220 return dfltbutton; // return default option
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221#endif
4222
4223#ifdef FEAT_GUI_DIALOG
Bram Moolenaar85a20022019-12-21 18:25:54 +01004224 // When GUI is running and 'c' not in 'guioptions', use the GUI dialog
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
4226 {
Bram Moolenaar2d12c252022-06-13 21:42:45 +01004227 // --gui-dialog-file: write text to a file
4228 if (gui_dialog_log(title, message))
4229 c = dfltbutton;
4230 else
4231 c = gui_mch_dialog(type, title, message, buttons, dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004232 textfield, ex_cmd);
Bram Moolenaar85a20022019-12-21 18:25:54 +01004233 // avoid a hit-enter prompt without clearing the cmdline
Bram Moolenaar8de49e12009-02-11 17:47:54 +00004234 need_wait_return = FALSE;
4235 emsg_on_display = FALSE;
4236 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237
Bram Moolenaar85a20022019-12-21 18:25:54 +01004238 // Flush output to avoid that further messages and redrawing is done
4239 // in the wrong order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 out_flush();
4241 gui_mch_update();
4242
4243 return c;
4244 }
4245#endif
4246
4247 oldState = State;
Bram Moolenaar24959102022-05-07 20:01:16 +01004248 State = MODE_CONFIRM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250
Bram Moolenaar27321db2020-07-06 21:24:57 +02004251 // Ensure raw mode here.
4252 save_tmode = cur_tmode;
4253 settmode(TMODE_RAW);
4254
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 /*
4256 * Since we wait for a keypress, don't make the
4257 * user press RETURN as well afterwards.
4258 */
4259 ++no_wait_return;
4260 hotkeys = msg_show_console_dialog(message, buttons, dfltbutton);
4261
4262 if (hotkeys != NULL)
4263 {
4264 for (;;)
4265 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004266 // Get a typed character directly from the user.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 c = get_keystroke();
4268 switch (c)
4269 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004270 case CAR: // User accepts default option
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 case NL:
4272 retval = dfltbutton;
4273 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01004274 case Ctrl_C: // User aborts/cancels
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 case ESC:
4276 retval = 0;
4277 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01004278 default: // Could be a hotkey?
4279 if (c < 0) // special keys are ignored here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 continue;
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004281 if (c == ':' && ex_cmd)
4282 {
4283 retval = dfltbutton;
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02004284 ins_char_typebuf(':', 0);
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004285 break;
4286 }
4287
Bram Moolenaar85a20022019-12-21 18:25:54 +01004288 // Make the character lowercase, as chars in "hotkeys" are.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 c = MB_TOLOWER(c);
4290 retval = 1;
4291 for (i = 0; hotkeys[i]; ++i)
4292 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 if (has_mbyte)
4294 {
4295 if ((*mb_ptr2char)(hotkeys + i) == c)
4296 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004297 i += (*mb_ptr2len)(hotkeys + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 }
4299 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 if (hotkeys[i] == c)
4301 break;
4302 ++retval;
4303 }
4304 if (hotkeys[i])
4305 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01004306 // No hotkey match, so keep waiting
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 continue;
4308 }
4309 break;
4310 }
4311
4312 vim_free(hotkeys);
4313 }
4314
Bram Moolenaar27321db2020-07-06 21:24:57 +02004315 settmode(save_tmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 State = oldState;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 --no_wait_return;
4319 msg_end_prompt();
4320
4321 return retval;
4322}
4323
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324/*
4325 * Copy one character from "*from" to "*to", taking care of multi-byte
4326 * characters. Return the length of the character in bytes.
4327 */
4328 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004329copy_char(
4330 char_u *from,
4331 char_u *to,
Bram Moolenaar85a20022019-12-21 18:25:54 +01004332 int lowercase) // make character lower case
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334 int len;
4335 int c;
4336
4337 if (has_mbyte)
4338 {
4339 if (lowercase)
4340 {
4341 c = MB_TOLOWER((*mb_ptr2char)(from));
4342 return (*mb_char2bytes)(c, to);
4343 }
4344 else
4345 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004346 len = (*mb_ptr2len)(from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 mch_memmove(to, from, (size_t)len);
4348 return len;
4349 }
4350 }
4351 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 {
4353 if (lowercase)
4354 *to = (char_u)TOLOWER_LOC(*from);
4355 else
4356 *to = *from;
4357 return 1;
4358 }
4359}
4360
4361/*
4362 * Format the dialog string, and display it at the bottom of
4363 * the screen. Return a string of hotkey chars (if defined) for
4364 * each 'button'. If a button has no hotkey defined, the first character of
4365 * the button is used.
4366 * The hotkeys can be multi-byte characters, but without combining chars.
4367 *
4368 * Returns an allocated string with hotkeys, or NULL for error.
4369 */
4370 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004371msg_show_console_dialog(
4372 char_u *message,
4373 char_u *buttons,
4374 int dfltbutton)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375{
4376 int len = 0;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01004377#define HOTK_LEN (has_mbyte ? MB_MAXBYTES : 1)
Bram Moolenaar85a20022019-12-21 18:25:54 +01004378 int lenhotkey = HOTK_LEN; // count first button
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 char_u *hotk = NULL;
4380 char_u *msgp = NULL;
4381 char_u *hotkp = NULL;
4382 char_u *r;
4383 int copy;
4384#define HAS_HOTKEY_LEN 30
4385 char_u has_hotkey[HAS_HOTKEY_LEN];
Bram Moolenaar85a20022019-12-21 18:25:54 +01004386 int first_hotkey = FALSE; // first char of button is hotkey
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 int idx;
4388
4389 has_hotkey[0] = FALSE;
4390
4391 /*
4392 * First loop: compute the size of memory to allocate.
4393 * Second loop: copy to the allocated memory.
4394 */
4395 for (copy = 0; copy <= 1; ++copy)
4396 {
4397 r = buttons;
4398 idx = 0;
4399 while (*r)
4400 {
4401 if (*r == DLG_BUTTON_SEP)
4402 {
4403 if (copy)
4404 {
4405 *msgp++ = ',';
Bram Moolenaar85a20022019-12-21 18:25:54 +01004406 *msgp++ = ' '; // '\n' -> ', '
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407
Bram Moolenaar85a20022019-12-21 18:25:54 +01004408 // advance to next hotkey and set default hotkey
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 if (has_mbyte)
Bram Moolenaar6a516062007-07-05 08:11:42 +00004410 hotkp += STRLEN(hotkp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412 ++hotkp;
Bram Moolenaar6a516062007-07-05 08:11:42 +00004413 hotkp[copy_char(r + 1, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 if (dfltbutton)
4415 --dfltbutton;
4416
Bram Moolenaar85a20022019-12-21 18:25:54 +01004417 // If no hotkey is specified first char is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 if (idx < HAS_HOTKEY_LEN - 1 && !has_hotkey[++idx])
4419 first_hotkey = TRUE;
4420 }
4421 else
4422 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004423 len += 3; // '\n' -> ', '; 'x' -> '(x)'
4424 lenhotkey += HOTK_LEN; // each button needs a hotkey
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 if (idx < HAS_HOTKEY_LEN - 1)
4426 has_hotkey[++idx] = FALSE;
4427 }
4428 }
4429 else if (*r == DLG_HOTKEY_CHAR || first_hotkey)
4430 {
4431 if (*r == DLG_HOTKEY_CHAR)
4432 ++r;
4433 first_hotkey = FALSE;
4434 if (copy)
4435 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004436 if (*r == DLG_HOTKEY_CHAR) // '&&a' -> '&a'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 *msgp++ = *r;
4438 else
4439 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004440 // '&a' -> '[a]'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441 *msgp++ = (dfltbutton == 1) ? '[' : '(';
4442 msgp += copy_char(r, msgp, FALSE);
4443 *msgp++ = (dfltbutton == 1) ? ']' : ')';
4444
Bram Moolenaar85a20022019-12-21 18:25:54 +01004445 // redefine hotkey
Bram Moolenaar6a516062007-07-05 08:11:42 +00004446 hotkp[copy_char(r, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 }
4448 }
4449 else
4450 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004451 ++len; // '&a' -> '[a]'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 if (idx < HAS_HOTKEY_LEN - 1)
4453 has_hotkey[idx] = TRUE;
4454 }
4455 }
4456 else
4457 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004458 // everything else copy literally
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 if (copy)
4460 msgp += copy_char(r, msgp, FALSE);
4461 }
4462
Bram Moolenaar85a20022019-12-21 18:25:54 +01004463 // advance to the next character
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004464 MB_PTR_ADV(r);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 }
4466
4467 if (copy)
4468 {
4469 *msgp++ = ':';
4470 *msgp++ = ' ';
4471 *msgp = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 }
4473 else
4474 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004475 len += (int)(STRLEN(message)
Bram Moolenaar85a20022019-12-21 18:25:54 +01004476 + 2 // for the NL's
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004477 + STRLEN(buttons)
Bram Moolenaar85a20022019-12-21 18:25:54 +01004478 + 3); // for the ": " and NUL
4479 lenhotkey++; // for the NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480
Bram Moolenaar85a20022019-12-21 18:25:54 +01004481 // If no hotkey is specified first char is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 if (!has_hotkey[0])
4483 {
4484 first_hotkey = TRUE;
Bram Moolenaar85a20022019-12-21 18:25:54 +01004485 len += 2; // "x" -> "[x]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 }
4487
4488 /*
4489 * Now allocate and load the strings
4490 */
4491 vim_free(confirm_msg);
4492 confirm_msg = alloc(len);
4493 if (confirm_msg == NULL)
4494 return NULL;
4495 *confirm_msg = NUL;
4496 hotk = alloc(lenhotkey);
4497 if (hotk == NULL)
4498 return NULL;
4499
4500 *confirm_msg = '\n';
4501 STRCPY(confirm_msg + 1, message);
4502
4503 msgp = confirm_msg + 1 + STRLEN(message);
4504 hotkp = hotk;
4505
Bram Moolenaar85a20022019-12-21 18:25:54 +01004506 // Define first default hotkey. Keep the hotkey string NUL
4507 // terminated to avoid reading past the end.
Bram Moolenaar6a516062007-07-05 08:11:42 +00004508 hotkp[copy_char(buttons, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509
Bram Moolenaar85a20022019-12-21 18:25:54 +01004510 // Remember where the choices start, displaying starts here when
4511 // "hotkp" typed at the more prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 confirm_msg_tail = msgp;
4513 *msgp++ = '\n';
4514 }
4515 }
4516
4517 display_confirm_msg();
4518 return hotk;
4519}
4520
4521/*
4522 * Display the ":confirm" message. Also called when screen resized.
4523 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02004524 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004525display_confirm_msg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526{
Bram Moolenaar85a20022019-12-21 18:25:54 +01004527 // avoid that 'q' at the more prompt truncates the message here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528 ++confirm_msg_used;
4529 if (confirm_msg != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004530 msg_puts_attr((char *)confirm_msg, HL_ATTR(HLF_M));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 --confirm_msg_used;
4532}
4533
Bram Moolenaar85a20022019-12-21 18:25:54 +01004534#endif // FEAT_CON_DIALOG
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535
4536#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4537
4538 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004539vim_dialog_yesno(
4540 int type,
4541 char_u *title,
4542 char_u *message,
4543 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544{
4545 if (do_dialog(type,
4546 title == NULL ? (char_u *)_("Question") : title,
4547 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004548 (char_u *)_("&Yes\n&No"), dflt, NULL, FALSE) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 return VIM_YES;
4550 return VIM_NO;
4551}
4552
4553 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004554vim_dialog_yesnocancel(
4555 int type,
4556 char_u *title,
4557 char_u *message,
4558 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559{
4560 switch (do_dialog(type,
4561 title == NULL ? (char_u *)_("Question") : title,
4562 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004563 (char_u *)_("&Yes\n&No\n&Cancel"), dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 {
4565 case 1: return VIM_YES;
4566 case 2: return VIM_NO;
4567 }
4568 return VIM_CANCEL;
4569}
4570
4571 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004572vim_dialog_yesnoallcancel(
4573 int type,
4574 char_u *title,
4575 char_u *message,
4576 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577{
4578 switch (do_dialog(type,
4579 title == NULL ? (char_u *)"Question" : title,
4580 message,
4581 (char_u *)_("&Yes\n&No\nSave &All\n&Discard All\n&Cancel"),
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004582 dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 {
4584 case 1: return VIM_YES;
4585 case 2: return VIM_NO;
4586 case 3: return VIM_ALL;
4587 case 4: return VIM_DISCARDALL;
4588 }
4589 return VIM_CANCEL;
4590}
4591
Bram Moolenaar85a20022019-12-21 18:25:54 +01004592#endif // FEAT_GUI_DIALOG || FEAT_CON_DIALOG