blob: 34a2692184341931f548225cb1f05940a75f919e [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
1096 p = p_meo;
1097 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
1136 // "history" must be <= 10000
1137 if (messages_history_new > 10000)
1138 return FAIL;
1139
1140 msg_flags = messages_flags_new;
1141 msg_wait = messages_wait_new;
1142
1143 msg_hist_max = messages_history_new;
1144 check_msg_hist();
1145
1146 return OK;
1147}
1148
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00001149/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 * ":messages" command.
1151 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 void
Bram Moolenaar52196b22016-04-14 17:52:41 +02001153ex_messages(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154{
1155 struct msg_hist *p;
1156 char_u *s;
Bram Moolenaar451f8492016-04-14 17:16:22 +02001157 int c = 0;
1158
1159 if (STRCMP(eap->arg, "clear") == 0)
1160 {
1161 int keep = eap->addr_count == 0 ? 0 : eap->line2;
1162
1163 while (msg_hist_len > keep)
1164 (void)delete_first_msg();
1165 return;
1166 }
1167
1168 if (*eap->arg != NUL)
1169 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001170 emsg(_(e_invalid_argument));
Bram Moolenaar451f8492016-04-14 17:16:22 +02001171 return;
1172 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173
1174 msg_hist_off = TRUE;
1175
Bram Moolenaar451f8492016-04-14 17:16:22 +02001176 p = first_msg_hist;
Bram Moolenaar451f8492016-04-14 17:16:22 +02001177 if (eap->addr_count != 0)
1178 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001179 // Count total messages
Bram Moolenaar451f8492016-04-14 17:16:22 +02001180 for (; p != NULL && !got_int; p = p->next)
1181 c++;
1182
1183 c -= eap->line2;
1184
Bram Moolenaar85a20022019-12-21 18:25:54 +01001185 // Skip without number of messages specified
Bram Moolenaar451f8492016-04-14 17:16:22 +02001186 for (p = first_msg_hist; p != NULL && !got_int && c > 0;
1187 p = p->next, c--);
1188 }
1189
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001190 if (p == first_msg_hist)
1191 {
Bram Moolenaar41f69182020-04-25 15:45:37 +02001192#ifdef FEAT_MULTI_LANG
1193 s = get_mess_lang();
1194#else
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001195 s = mch_getenv((char_u *)"LANG");
Bram Moolenaar41f69182020-04-25 15:45:37 +02001196#endif
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001197 if (s != NULL && *s != NUL)
Bram Moolenaar0c183192018-06-28 14:54:43 +02001198 // The next comment is extracted by xgettext and put in po file for
1199 // translators to read.
Bram Moolenaar32526b32019-01-19 17:43:09 +01001200 msg_attr(
Bram Moolenaar0c183192018-06-28 14:54:43 +02001201 // Translator: Please replace the name and email address
1202 // with the appropriate text for your translation.
Christian Brabandte978b452023-08-13 10:33:05 +02001203 _("Messages maintainer: The Vim Project"),
Bram Moolenaar8820b482017-03-16 17:23:31 +01001204 HL_ATTR(HLF_T));
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001205 }
1206
Bram Moolenaar85a20022019-12-21 18:25:54 +01001207 // Display what was not skipped.
Bram Moolenaar451f8492016-04-14 17:16:22 +02001208 for (; p != NULL && !got_int; p = p->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 if (p->msg != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001210 msg_attr((char *)p->msg, p->attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211
1212 msg_hist_off = FALSE;
1213}
1214
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001215#if defined(FEAT_CON_DIALOG) || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216/*
1217 * Call this after prompting the user. This will avoid a hit-return message
1218 * and a delay.
1219 */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001220 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001221msg_end_prompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222{
1223 need_wait_return = FALSE;
1224 emsg_on_display = FALSE;
1225 cmdline_row = msg_row;
1226 msg_col = 0;
1227 msg_clr_eos();
Bram Moolenaar5d6f75e2011-12-30 14:14:29 +01001228 lines_left = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229}
1230#endif
1231
1232/*
Bram Moolenaar32526b32019-01-19 17:43:09 +01001233 * Wait for the user to hit a key (normally Enter).
1234 * If "redraw" is TRUE, clear and redraw the screen.
1235 * If "redraw" is FALSE, just redraw the screen.
1236 * If "redraw" is -1, don't redraw at all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 */
1238 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001239wait_return(int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240{
1241 int c;
1242 int oldState;
1243 int tmpState;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 int had_got_int;
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001245 int save_reg_recording;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001246 FILE *save_scriptout;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247
1248 if (redraw == TRUE)
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01001249 set_must_redraw(UPD_CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250
Bram Moolenaar85a20022019-12-21 18:25:54 +01001251 // If using ":silent cmd", don't wait for a return. Also don't set
1252 // need_wait_return to do it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 if (msg_silent != 0)
1254 return;
Bram Moolenaarcf0995d2022-09-11 21:36:17 +01001255#ifdef HAS_MESSAGE_WINDOW
1256 if (in_echowindow)
1257 return;
1258#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01001260 /*
1261 * When inside vgetc(), we can't wait for a typed character at all.
1262 * With the global command (and some others) we only need one return at
1263 * the end. Adjust cmdline_row to avoid the next message overwriting the
1264 * last one.
1265 */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00001266 if (vgetc_busy > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 return;
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01001268 need_wait_return = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 if (no_wait_return)
1270 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 if (!exmode_active)
1272 cmdline_row = msg_row;
1273 return;
1274 }
1275
Bram Moolenaar85a20022019-12-21 18:25:54 +01001276 redir_off = TRUE; // don't redirect this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 oldState = State;
1278 if (quit_more)
1279 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001280 c = CAR; // just pretend CR was hit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 quit_more = FALSE;
1282 got_int = FALSE;
1283 }
1284 else if (exmode_active)
1285 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001286 msg_puts(" "); // make sure the cursor is on the right line
1287 c = CAR; // no need for a return in ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 got_int = FALSE;
1289 }
1290 else
1291 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001292 // Make sure the hit-return prompt is on screen when 'guioptions' was
1293 // just changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 screenalloc(FALSE);
1295
Bram Moolenaar24959102022-05-07 20:01:16 +01001296 State = MODE_HITRETURN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar85a20022019-12-21 18:25:54 +01001299 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300#endif
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01001301 cmdline_row = msg_row;
1302
Bram Moolenaar85a20022019-12-21 18:25:54 +01001303 // Avoid the sequence that the user types ":" at the hit-return prompt
1304 // to start an Ex command, but the file-changed dialog gets in the
1305 // way.
Bram Moolenaarec38d692013-04-24 15:12:32 +02001306 if (need_check_timestamps)
1307 check_timestamps(FALSE);
1308
Christian Brabandt51d4d842024-12-06 17:26:25 +01001309 if (msg_flags & MESSAGES_HIT_ENTER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 {
Christian Brabandt51d4d842024-12-06 17:26:25 +01001311 hit_return_msg();
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001312
Christian Brabandt51d4d842024-12-06 17:26:25 +01001313 do
1314 {
1315 // Remember "got_int", if it is set vgetc() probably returns a
1316 // CTRL-C, but we need to loop then.
1317 had_got_int = got_int;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001318
Christian Brabandt51d4d842024-12-06 17:26:25 +01001319 // Don't do mappings here, we put the character back in the
1320 // typeahead buffer.
1321 ++no_mapping;
1322 ++allow_keys;
1323
1324 // Temporarily disable Recording. If Recording is active, the
1325 // character will be recorded later, since it will be added to the
1326 // typebuf after the loop
1327 save_reg_recording = reg_recording;
1328 save_scriptout = scriptout;
1329 reg_recording = 0;
1330 scriptout = NULL;
1331 c = safe_vgetc();
1332 if (had_got_int && !global_busy)
1333 got_int = FALSE;
1334 --no_mapping;
1335 --allow_keys;
1336 reg_recording = save_reg_recording;
1337 scriptout = save_scriptout;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001338
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339#ifdef FEAT_CLIPBOARD
Christian Brabandt51d4d842024-12-06 17:26:25 +01001340 // Strange way to allow copying (yanking) a modeless selection at
1341 // the hit-enter prompt. Use CTRL-Y, because the same is used in
1342 // Cmdline-mode and it's harmless when there is no selection.
1343 if (c == Ctrl_Y && clip_star.state == SELECT_DONE)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001344 {
Christian Brabandt51d4d842024-12-06 17:26:25 +01001345 clip_copy_modeless_selection(TRUE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001346 c = K_IGNORE;
Christian Brabandt51d4d842024-12-06 17:26:25 +01001347 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348#endif
Bram Moolenaar88456cd2022-11-18 22:14:09 +00001349
Christian Brabandt51d4d842024-12-06 17:26:25 +01001350 /*
1351 * Allow scrolling back in the messages.
1352 * Also accept scroll-down commands when messages fill the screen,
1353 * to avoid that typing one 'j' too many makes the messages
1354 * disappear.
1355 */
1356 if (p_more && !p_cp)
1357 {
1358 if (c == 'b' || c == 'k' || c == 'u' || c == 'g'
1359 || c == K_UP || c == K_PAGEUP)
1360 {
1361 if (msg_scrolled > Rows)
1362 // scroll back to show older messages
1363 do_more_prompt(c);
1364 else
1365 {
1366 msg_didout = FALSE;
1367 c = K_IGNORE;
1368 msg_col =
1369#ifdef FEAT_RIGHTLEFT
1370 cmdmsg_rl ? Columns - 1 :
1371#endif
1372 0;
1373 }
1374 if (quit_more)
1375 {
1376 c = CAR; // just pretend CR was hit
1377 quit_more = FALSE;
1378 got_int = FALSE;
1379 }
1380 else if (c != K_IGNORE)
1381 {
1382 c = K_IGNORE;
1383 hit_return_msg();
1384 }
1385 }
1386 else if (msg_scrolled > Rows - 2
1387 && (c == 'j' || c == 'd' || c == 'f'
1388 || c == K_DOWN || c == K_PAGEDOWN))
1389 c = K_IGNORE;
1390 }
1391 } while ((had_got_int && c == Ctrl_C)
1392 || c == K_IGNORE
1393#ifdef FEAT_GUI
1394 || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR
1395#endif
1396 || c == K_LEFTDRAG || c == K_LEFTRELEASE
1397 || c == K_MIDDLEDRAG || c == K_MIDDLERELEASE
1398 || c == K_RIGHTDRAG || c == K_RIGHTRELEASE
1399 || c == K_MOUSELEFT || c == K_MOUSERIGHT
1400 || c == K_MOUSEDOWN || c == K_MOUSEUP
1401 || c == K_MOUSEMOVE
1402 || (!mouse_has(MOUSE_RETURN)
1403 && mouse_row < msg_row
1404 && (c == K_LEFTMOUSE
1405 || c == K_MIDDLEMOUSE
1406 || c == K_RIGHTMOUSE
1407 || c == K_X1MOUSE
1408 || c == K_X2MOUSE))
1409 );
1410 ui_breakcheck();
1411
1412 // Avoid that the mouse-up event causes Visual mode to start.
1413 if (c == K_LEFTMOUSE || c == K_MIDDLEMOUSE || c == K_RIGHTMOUSE
1414 || c == K_X1MOUSE || c == K_X2MOUSE)
1415 (void)jump_to_mouse(MOUSE_SETPOS, NULL, 0);
1416 else if (vim_strchr((char_u *)"\r\n ", c) == NULL && c != Ctrl_C)
1417 {
1418 // Put the character back in the typeahead buffer. Don't use the
1419 // stuff buffer, because lmaps wouldn't work.
1420 ins_char_typebuf(vgetc_char, vgetc_mod_mask);
1421 do_redraw = TRUE; // need a redraw even though there is
1422 // typeahead
1423 }
1424 }
1425 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 {
Christian Brabandt51d4d842024-12-06 17:26:25 +01001427 c = CAR;
1428 // Wait to allow the user to verify the output.
1429 do_sleep(msg_wait, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431 }
1432 redir_off = FALSE;
1433
1434 /*
1435 * If the user hits ':', '?' or '/' we get a command line from the next
1436 * line.
1437 */
1438 if (c == ':' || c == '?' || c == '/')
1439 {
1440 if (!exmode_active)
1441 cmdline_row = msg_row;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001442 skip_redraw = TRUE; // skip redraw once
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 do_redraw = FALSE;
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02001444#ifdef FEAT_TERMINAL
1445 skip_term_loop = TRUE;
1446#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 }
1448
1449 /*
1450 * If the window size changed set_shellsize() will redraw the screen.
1451 * Otherwise the screen is only redrawn if 'redraw' is set and no ':'
1452 * typed.
1453 */
1454 tmpState = State;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001455 State = oldState; // restore State before set_shellsize
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 msg_check();
1458
1459#if defined(UNIX) || defined(VMS)
1460 /*
1461 * When switching screens, we need to output an extra newline on exit.
1462 */
1463 if (swapping_screen() && !termcap_active)
1464 newline_on_exit = TRUE;
1465#endif
1466
1467 need_wait_return = FALSE;
1468 did_wait_return = TRUE;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001469 emsg_on_display = FALSE; // can delete error message now
1470 lines_left = -1; // reset lines_left at next msg_start()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 reset_last_sourcing();
1472 if (keep_msg != NULL && vim_strsize(keep_msg) >=
1473 (Rows - cmdline_row - 1) * Columns + sc_col)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001474 VIM_CLEAR(keep_msg); // don't redisplay message, it's too long
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475
Bram Moolenaar24959102022-05-07 20:01:16 +01001476 if (tmpState == MODE_SETWSIZE) // got resize event while in vgetc()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001478 starttermcap(); // start termcap before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 shell_resized();
1480 }
1481 else if (!skip_redraw
1482 && (redraw == TRUE || (msg_scrolled != 0 && redraw != -1)))
1483 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001484 starttermcap(); // start termcap before redrawing
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001485 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486 }
1487}
1488
1489/*
1490 * Write the hit-return prompt.
1491 */
1492 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001493hit_return_msg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001495 int save_p_more = p_more;
1496
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001497 p_more = FALSE; // don't want to see this message when scrolling back
Bram Moolenaar85a20022019-12-21 18:25:54 +01001498 if (msg_didout) // start on a new line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 msg_putchar('\n');
1500 if (got_int)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001501 msg_puts(_("Interrupt: "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502
Bram Moolenaar32526b32019-01-19 17:43:09 +01001503 msg_puts_attr(_("Press ENTER or type command to continue"), HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504 if (!msg_use_printf())
1505 msg_clr_eos();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001506 p_more = save_p_more;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507}
1508
1509/*
1510 * Set "keep_msg" to "s". Free the old value and check for NULL pointer.
1511 */
1512 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001513set_keep_msg(char_u *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514{
1515 vim_free(keep_msg);
1516 if (s != NULL && msg_silent == 0)
1517 keep_msg = vim_strsave(s);
1518 else
1519 keep_msg = NULL;
Bram Moolenaaraab21c32005-01-25 21:46:35 +00001520 keep_msg_more = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001521 keep_msg_attr = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522}
1523
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001524/*
1525 * If there currently is a message being displayed, set "keep_msg" to it, so
1526 * that it will be displayed again after redraw.
1527 */
1528 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001529set_keep_msg_from_hist(void)
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001530{
1531 if (keep_msg == NULL && last_msg_hist != NULL && msg_scrolled == 0
Bram Moolenaar24959102022-05-07 20:01:16 +01001532 && (State & MODE_NORMAL))
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001533 set_keep_msg(last_msg_hist->msg, last_msg_hist->attr);
1534}
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001535
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536/*
1537 * Prepare for outputting characters in the command line.
1538 */
1539 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001540msg_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541{
1542 int did_return = FALSE;
1543
zeertzjq40ed6712023-11-23 20:37:01 +01001544 if (msg_row < cmdline_row)
1545 msg_row = cmdline_row;
1546
Bram Moolenaare4ce65d2010-08-04 20:12:32 +02001547 if (!msg_silent)
Rob Pilling726f7f92022-01-20 14:44:38 +00001548 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001549 VIM_CLEAR(keep_msg);
Rob Pilling726f7f92022-01-20 14:44:38 +00001550 need_fileinfo = FALSE;
1551 }
Bram Moolenaara7241f52008-06-24 20:39:31 +00001552
1553#ifdef FEAT_EVAL
Bram Moolenaar3b474dc2022-09-01 17:01:32 +01001554 if (need_clr_eos)
Bram Moolenaara7241f52008-06-24 20:39:31 +00001555 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001556 // Halfway an ":echo" command and getting an (error) message: clear
1557 // any text from the command.
Bram Moolenaara7241f52008-06-24 20:39:31 +00001558 need_clr_eos = FALSE;
1559 msg_clr_eos();
1560 }
1561#endif
1562
Bram Moolenaard54af2e2022-08-27 22:05:13 +01001563#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaara2a89732022-08-31 14:46:18 +01001564 if (in_echowindow)
Bram Moolenaar9198de32022-08-27 21:30:03 +01001565 {
Bram Moolenaar37fef162022-08-29 18:16:32 +01001566 if (popup_message_win_visible()
1567 && ((msg_col > 0 && (msg_scroll || !full_screen))
1568 || in_echowindow))
Bram Moolenaar9198de32022-08-27 21:30:03 +01001569 {
1570 win_T *wp = popup_get_message_win();
1571
Bram Moolenaarf2fb54f2022-08-28 20:58:51 +01001572 // start a new line
Bram Moolenaar9198de32022-08-27 21:30:03 +01001573 curbuf = wp->w_buffer;
1574 ml_append(wp->w_buffer->b_ml.ml_line_count,
1575 (char_u *)"", (colnr_T)0, FALSE);
1576 curbuf = curwin->w_buffer;
1577 }
1578 msg_col = 0;
1579 }
Bram Moolenaard54af2e2022-08-27 22:05:13 +01001580 else
1581#endif
1582 if (!msg_scroll && full_screen) // overwrite last message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 {
1584 msg_row = cmdline_row;
1585 msg_col =
1586#ifdef FEAT_RIGHTLEFT
1587 cmdmsg_rl ? Columns - 1 :
1588#endif
1589 0;
1590 }
Bram Moolenaara2a89732022-08-31 14:46:18 +01001591 else if (msg_didout || in_echowindow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 {
Bram Moolenaar309c4e02022-08-29 12:23:39 +01001593 // start message on next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 msg_putchar('\n');
1595 did_return = TRUE;
1596 if (exmode_active != EXMODE_NORMAL)
1597 cmdline_row = msg_row;
1598 }
1599 if (!msg_didany || lines_left < 0)
1600 msg_starthere();
1601 if (msg_silent == 0)
1602 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001603 msg_didout = FALSE; // no output on current line yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 cursor_off();
1605 }
1606
Bram Moolenaar85a20022019-12-21 18:25:54 +01001607 // when redirecting, may need to start a new line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 if (!did_return)
1609 redir_write((char_u *)"\n", -1);
1610}
1611
1612/*
1613 * Note that the current msg position is where messages start.
1614 */
1615 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001616msg_starthere(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617{
1618 lines_left = cmdline_row;
1619 msg_didany = FALSE;
1620}
1621
1622 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001623msg_putchar(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624{
1625 msg_putchar_attr(c, 0);
1626}
1627
1628 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001629msg_putchar_attr(int c, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630{
Bram Moolenaar63c0ccd2019-01-19 21:06:58 +01001631 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632
1633 if (IS_SPECIAL(c))
1634 {
1635 buf[0] = K_SPECIAL;
1636 buf[1] = K_SECOND(c);
1637 buf[2] = K_THIRD(c);
1638 buf[3] = NUL;
1639 }
1640 else
Bram Moolenaar63c0ccd2019-01-19 21:06:58 +01001641 buf[(*mb_char2bytes)(c, buf)] = NUL;
Bram Moolenaar63c0ccd2019-01-19 21:06:58 +01001642 msg_puts_attr((char *)buf, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643}
1644
1645 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001646msg_outnum(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001648 char buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649
Bram Moolenaar32526b32019-01-19 17:43:09 +01001650 sprintf(buf, "%ld", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 msg_puts(buf);
1652}
1653
1654 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001655msg_home_replace(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656{
1657 msg_home_replace_attr(fname, 0);
1658}
1659
1660#if defined(FEAT_FIND_ID) || defined(PROTO)
1661 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001662msg_home_replace_hl(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663{
Bram Moolenaar8820b482017-03-16 17:23:31 +01001664 msg_home_replace_attr(fname, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665}
1666#endif
1667
1668 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001669msg_home_replace_attr(char_u *fname, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670{
1671 char_u *name;
1672
1673 name = home_replace_save(NULL, fname);
1674 if (name != NULL)
1675 msg_outtrans_attr(name, attr);
1676 vim_free(name);
1677}
1678
1679/*
1680 * Output 'len' characters in 'str' (including NULs) with translation
Bram Moolenaarf48ee3c2019-12-06 22:18:20 +01001681 * if 'len' is -1, output up to a NUL character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682 * Use attributes 'attr'.
1683 * Return the number of characters it takes on the screen.
1684 */
1685 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001686msg_outtrans(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687{
1688 return msg_outtrans_attr(str, 0);
1689}
1690
1691 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001692msg_outtrans_attr(char_u *str, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693{
1694 return msg_outtrans_len_attr(str, (int)STRLEN(str), attr);
1695}
1696
1697 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001698msg_outtrans_len(char_u *str, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699{
1700 return msg_outtrans_len_attr(str, len, 0);
1701}
1702
1703/*
1704 * Output one character at "p". Return pointer to the next character.
1705 * Handles multi-byte characters.
1706 */
1707 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001708msg_outtrans_one(char_u *p, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 int l;
1711
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001712 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 {
1714 msg_outtrans_len_attr(p, l, attr);
1715 return p + l;
1716 }
cero19881d87e112023-02-16 15:03:12 +00001717 msg_puts_attr((char *)transchar_byte_buf(NULL, *p), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 return p + 1;
1719}
1720
1721 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001722msg_outtrans_len_attr(char_u *msgstr, int len, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723{
1724 int retval = 0;
1725 char_u *str = msgstr;
1726 char_u *plain_start = msgstr;
1727 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 int mb_l;
1729 int c;
Bram Moolenaar3d30af82020-10-13 22:15:56 +02001730 int save_got_int = got_int;
1731
1732 // Only quit when got_int was set in here.
1733 got_int = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734
Shougo Matsushitabe2b03c2024-04-08 22:11:50 +02001735 if (attr == 0)
1736 attr = HL_ATTR(HLF_MSG);
1737
Bram Moolenaar85a20022019-12-21 18:25:54 +01001738 // if MSG_HIST flag set, add message to history
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 if (attr & MSG_HIST)
1740 {
1741 add_msg_hist(str, len, attr);
1742 attr &= ~MSG_HIST;
1743 }
1744
Bram Moolenaar800cdbb2023-06-15 16:40:02 +01001745 // When drawing over the command line no need to clear it later or remove
1746 // the mode message.
Bram Moolenaarda51ad52023-06-15 18:44:50 +01001747 if (msg_row >= cmdline_row && msg_col == 0)
Bram Moolenaar800cdbb2023-06-15 16:40:02 +01001748 {
1749 clear_cmdline = FALSE;
1750 mode_displayed = FALSE;
1751 }
1752
Bram Moolenaar85a20022019-12-21 18:25:54 +01001753 // If the string starts with a composing character first draw a space on
1754 // which the composing char can be drawn.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 if (enc_utf8 && utf_iscomposing(utf_ptr2char(msgstr)))
Bram Moolenaar32526b32019-01-19 17:43:09 +01001756 msg_puts_attr(" ", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757
1758 /*
1759 * Go over the string. Special characters are translated and printed.
1760 * Normal characters are printed several at a time.
1761 */
Bram Moolenaar3d30af82020-10-13 22:15:56 +02001762 while (--len >= 0 && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764 if (enc_utf8)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001765 // Don't include composing chars after the end.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001766 mb_l = utfc_ptr2len_len(str, len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 else if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001768 mb_l = (*mb_ptr2len)(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769 else
1770 mb_l = 1;
1771 if (has_mbyte && mb_l > 1)
1772 {
1773 c = (*mb_ptr2char)(str);
1774 if (vim_isprintc(c))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001775 // printable multi-byte char: count the cells.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776 retval += (*mb_ptr2cells)(str);
1777 else
1778 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001779 // unprintable multi-byte char: print the printable chars so
1780 // far and the translation of the unprintable char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781 if (str > plain_start)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001782 msg_puts_attr_len((char *)plain_start,
1783 (int)(str - plain_start), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 plain_start = str + mb_l;
cero19881d87e112023-02-16 15:03:12 +00001785 msg_puts_attr((char *)transchar_buf(NULL, c),
Bram Moolenaar32526b32019-01-19 17:43:09 +01001786 attr == 0 ? HL_ATTR(HLF_8) : attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 retval += char2cells(c);
1788 }
1789 len -= mb_l - 1;
1790 str += mb_l;
1791 }
1792 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 {
cero19881d87e112023-02-16 15:03:12 +00001794 s = transchar_byte_buf(NULL, *str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 if (s[1] != NUL)
1796 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001797 // unprintable char: print the printable chars so far and the
1798 // translation of the unprintable char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799 if (str > plain_start)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001800 msg_puts_attr_len((char *)plain_start,
1801 (int)(str - plain_start), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 plain_start = str + 1;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001803 msg_puts_attr((char *)s, attr == 0 ? HL_ATTR(HLF_8) : attr);
Bram Moolenaarc236c162008-07-13 17:41:49 +00001804 retval += (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 }
Bram Moolenaarb3c722a2008-07-06 17:16:02 +00001806 else
1807 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 ++str;
1809 }
1810 }
1811
Bram Moolenaar3d30af82020-10-13 22:15:56 +02001812 if (str > plain_start && !got_int)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001813 // print the printable chars at the end
Bram Moolenaar32526b32019-01-19 17:43:09 +01001814 msg_puts_attr_len((char *)plain_start, (int)(str - plain_start), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815
Bram Moolenaar3d30af82020-10-13 22:15:56 +02001816 got_int |= save_got_int;
1817
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 return retval;
1819}
1820
1821#if defined(FEAT_QUICKFIX) || defined(PROTO)
1822 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001823msg_make(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824{
1825 int i;
1826 static char_u *str = (char_u *)"eeffoc", *rs = (char_u *)"Plon#dqg#vxjduB";
1827
1828 arg = skipwhite(arg);
1829 for (i = 5; *arg && i >= 0; --i)
1830 if (*arg++ != str[i])
1831 break;
1832 if (i < 0)
1833 {
1834 msg_putchar('\n');
1835 for (i = 0; rs[i]; ++i)
1836 msg_putchar(rs[i] - 3);
1837 }
1838}
1839#endif
1840
1841/*
Bram Moolenaarf48ee3c2019-12-06 22:18:20 +01001842 * Output the string 'str' up to a NUL character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 * Return the number of characters it takes on the screen.
1844 *
1845 * If K_SPECIAL is encountered, then it is taken in conjunction with the
1846 * following character and shown as <F1>, <S-Up> etc. Any other character
1847 * which is not printable shown in <> form.
1848 * If 'from' is TRUE (lhs of a mapping), a space is shown as <Space>.
1849 * If a character is displayed in one of these special ways, is also
1850 * highlighted (its highlight name is '8' in the p_hl variable).
1851 * Otherwise characters are not highlighted.
1852 * This function is used to show mappings, where we want to see how to type
1853 * the character/string -- webb
1854 */
1855 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001856msg_outtrans_special(
1857 char_u *strstart,
Bram Moolenaar725310d2019-04-24 23:08:23 +02001858 int from, // TRUE for lhs of a mapping
Bram Moolenaar32aa1022019-11-02 22:54:41 +01001859 int maxlen) // screen columns, 0 for unlimited
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860{
1861 char_u *str = strstart;
1862 int retval = 0;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001863 char *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 int attr;
1865 int len;
1866
Bram Moolenaar8820b482017-03-16 17:23:31 +01001867 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 while (*str != NUL)
1869 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001870 // Leading and trailing spaces need to be displayed in <> form.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 if ((str == strstart || str[1] == NUL) && *str == ' ')
1872 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001873 text = "<Space>";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 ++str;
1875 }
1876 else
zeertzjqcdc83932022-09-12 13:38:41 +01001877 text = (char *)str2special(&str, from, FALSE);
zeertzjq0519ce02022-05-09 12:16:19 +01001878 if (text[0] != NUL && text[1] == NUL)
1879 // single-byte character or illegal byte
cero19881d87e112023-02-16 15:03:12 +00001880 text = (char *)transchar_byte_buf(NULL, (char_u)text[0]);
Bram Moolenaar32526b32019-01-19 17:43:09 +01001881 len = vim_strsize((char_u *)text);
Bram Moolenaar725310d2019-04-24 23:08:23 +02001882 if (maxlen > 0 && retval + len >= maxlen)
1883 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001884 // Highlight special keys
Bram Moolenaar32526b32019-01-19 17:43:09 +01001885 msg_puts_attr(text, len > 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001886 && (*mb_ptr2len)((char_u *)text) <= 1 ? attr : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 retval += len;
1888 }
1889 return retval;
1890}
1891
Martin Tournoijba43e762022-10-13 22:12:15 +01001892#if defined(FEAT_EVAL) || defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaarbd743252010-10-20 21:23:33 +02001893/*
1894 * Return the lhs or rhs of a mapping, with the key codes turned into printable
1895 * strings, in an allocated string.
1896 */
1897 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001898str2special_save(
1899 char_u *str,
zeertzjqcdc83932022-09-12 13:38:41 +01001900 int replace_spaces, // TRUE to replace " " with "<Space>".
1901 // used for the lhs of mapping and keytrans().
1902 int replace_lt) // TRUE to replace "<" with "<lt>".
Bram Moolenaarbd743252010-10-20 21:23:33 +02001903{
1904 garray_T ga;
1905 char_u *p = str;
1906
1907 ga_init2(&ga, 1, 40);
1908 while (*p != NUL)
zeertzjqcdc83932022-09-12 13:38:41 +01001909 ga_concat(&ga, str2special(&p, replace_spaces, replace_lt));
Bram Moolenaarbd743252010-10-20 21:23:33 +02001910 ga_append(&ga, NUL);
1911 return (char_u *)ga.ga_data;
1912}
1913#endif
1914
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915/*
1916 * Return the printable string for the key codes at "*sp".
zeertzjq0519ce02022-05-09 12:16:19 +01001917 * On illegal byte return a string with only that byte.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 * Used for translating the lhs or rhs of a mapping to printable chars.
1919 * Advances "sp" to the next code.
1920 */
1921 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001922str2special(
1923 char_u **sp,
zeertzjqcdc83932022-09-12 13:38:41 +01001924 int replace_spaces, // TRUE to replace " " with "<Space>".
1925 // used for the lhs of mapping and keytrans().
1926 int replace_lt) // TRUE to replace "<" with "<lt>".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927{
1928 int c;
1929 static char_u buf[7];
1930 char_u *str = *sp;
1931 int modifiers = 0;
1932 int special = FALSE;
1933
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934 if (has_mbyte)
1935 {
1936 char_u *p;
1937
Bram Moolenaar85a20022019-12-21 18:25:54 +01001938 // Try to un-escape a multi-byte character. Return the un-escaped
1939 // string if it is a multi-byte character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 p = mb_unescape(sp);
1941 if (p != NULL)
1942 return p;
1943 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944
1945 c = *str;
zeertzjq1bed9932023-08-30 19:38:24 +02001946 if ((c == K_SPECIAL
1947#ifdef FEAT_GUI
1948 || c == CSI
1949#endif
1950 ) && str[1] != NUL && str[2] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 {
1952 if (str[1] == KS_MODIFIER)
1953 {
1954 modifiers = str[2];
1955 str += 3;
1956 c = *str;
1957 }
zeertzjq1bed9932023-08-30 19:38:24 +02001958 if ((c == K_SPECIAL
1959#ifdef FEAT_GUI
1960 || c == CSI
1961#endif
1962 ) && str[1] != NUL && str[2] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 {
1964 c = TO_SPECIAL(str[1], str[2]);
1965 str += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001967 if (IS_SPECIAL(c) || modifiers) // special key
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 special = TRUE;
1969 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970
zeertzjq0519ce02022-05-09 12:16:19 +01001971 if (has_mbyte && !IS_SPECIAL(c) && MB_BYTE2LEN(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 {
zeertzjqac402f42022-05-04 18:51:43 +01001973 char_u *p;
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001974
zeertzjqac402f42022-05-04 18:51:43 +01001975 *sp = str;
1976 // Try to un-escape a multi-byte character after modifiers.
1977 p = mb_unescape(sp);
zeertzjq0519ce02022-05-09 12:16:19 +01001978 if (p != NULL)
1979 // Since 'special' is TRUE the multi-byte character 'c' will be
1980 // processed by get_special_key_name()
1981 c = (*mb_ptr2char)(p);
1982 else
1983 // illegal byte
1984 *sp = str + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 }
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001986 else
Bram Moolenaar083692d2022-06-29 21:16:58 +01001987 // single-byte character, NUL or illegal byte
1988 *sp = str + (*str == NUL ? 0 : 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989
zeertzjq0519ce02022-05-09 12:16:19 +01001990 // Make special keys and C0 control characters in <> form, also <M-Space>.
zeertzjqcdc83932022-09-12 13:38:41 +01001991 if (special
1992 || c < ' '
1993 || (replace_spaces && c == ' ')
1994 || (replace_lt && c == '<'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 return get_special_key_name(c, modifiers);
1996 buf[0] = c;
1997 buf[1] = NUL;
1998 return buf;
1999}
2000
2001/*
2002 * Translate a key sequence into special key names.
2003 */
2004 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002005str2specialbuf(char_u *sp, char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006{
2007 char_u *s;
2008
2009 *buf = NUL;
2010 while (*sp)
2011 {
zeertzjqcdc83932022-09-12 13:38:41 +01002012 s = str2special(&sp, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 if ((int)(STRLEN(s) + STRLEN(buf)) < len)
2014 STRCAT(buf, s);
2015 }
2016}
2017
2018/*
2019 * print line for :print or :list command
2020 */
2021 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002022msg_prt_line(char_u *s, int list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023{
2024 int c;
2025 int col = 0;
2026 int n_extra = 0;
2027 int c_extra = 0;
Bram Moolenaar83a52172019-01-16 22:41:54 +01002028 int c_final = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002029 char_u *p_extra = NULL; // init to make SASC shut up
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 int n;
Bram Moolenaar56da7972007-01-16 14:46:32 +00002031 int attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 char_u *trail = NULL;
Bram Moolenaar91478ae2021-02-03 15:58:13 +01002033 char_u *lead = NULL;
zeertzjqf14b8ba2021-09-10 16:58:30 +02002034 int in_multispace = FALSE;
2035 int multispace_pos = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 int l;
2037 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038
Bram Moolenaardf177f62005-02-22 08:39:57 +00002039 if (curwin->w_p_list)
2040 list = TRUE;
2041
Bram Moolenaar91478ae2021-02-03 15:58:13 +01002042 if (list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 {
Bram Moolenaar91478ae2021-02-03 15:58:13 +01002044 // find start of trailing whitespace
Bram Moolenaareed9d462021-02-15 20:38:25 +01002045 if (curwin->w_lcs_chars.trail)
Bram Moolenaar91478ae2021-02-03 15:58:13 +01002046 {
2047 trail = s + STRLEN(s);
2048 while (trail > s && VIM_ISWHITE(trail[-1]))
2049 --trail;
2050 }
2051 // find end of leading whitespace
Bram Moolenaarb8329db2022-07-06 13:31:28 +01002052 if (curwin->w_lcs_chars.lead
2053 || curwin->w_lcs_chars.leadmultispace != NULL)
Bram Moolenaar91478ae2021-02-03 15:58:13 +01002054 {
2055 lead = s;
2056 while (VIM_ISWHITE(lead[0]))
2057 lead++;
2058 // in a line full of spaces all of them are treated as trailing
2059 if (*lead == NUL)
2060 lead = NULL;
2061 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 }
2063
Bram Moolenaar85a20022019-12-21 18:25:54 +01002064 // output a space for an empty line, otherwise the line will be
2065 // overwritten
Bram Moolenaareed9d462021-02-15 20:38:25 +01002066 if (*s == NUL && !(list && curwin->w_lcs_chars.eol != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 msg_putchar(' ');
2068
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002069 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 {
Bram Moolenaar56da7972007-01-16 14:46:32 +00002071 if (n_extra > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 {
2073 --n_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01002074 if (n_extra == 0 && c_final)
2075 c = c_final;
2076 else if (c_extra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 c = c_extra;
2078 else
2079 c = *p_extra++;
2080 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002081 else if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 {
2083 col += (*mb_ptr2cells)(s);
Bram Moolenaar1cbfc992020-12-02 12:37:37 +01002084 if (l >= MB_MAXBYTES)
2085 {
Bram Moolenaar29d2f452020-12-04 19:42:52 +01002086 STRCPY(buf, "?");
Bram Moolenaar1cbfc992020-12-02 12:37:37 +01002087 }
Bram Moolenaareed9d462021-02-15 20:38:25 +01002088 else if (curwin->w_lcs_chars.nbsp != NUL && list
Bram Moolenaar73284b92015-05-04 17:28:22 +02002089 && (mb_ptr2char(s) == 160
2090 || mb_ptr2char(s) == 0x202f))
Bram Moolenaaracf17282011-02-01 17:12:25 +01002091 {
Bram Moolenaar74ac29c2022-06-15 12:12:44 +01002092 int len = mb_char2bytes(curwin->w_lcs_chars.nbsp, buf);
2093
2094 buf[len] = NUL;
Bram Moolenaaracf17282011-02-01 17:12:25 +01002095 }
2096 else
2097 {
2098 mch_memmove(buf, s, (size_t)l);
2099 buf[l] = NUL;
2100 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01002101 msg_puts((char *)buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 s += l;
2103 continue;
2104 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 else
2106 {
2107 attr = 0;
2108 c = *s++;
zeertzjqabc80812023-09-24 23:32:18 +02002109 if (list)
2110 {
2111 in_multispace = c == ' ' && (*s == ' '
2112 || (col > 0 && s[-2] == ' '));
2113 if (!in_multispace)
2114 multispace_pos = 0;
2115 }
Bram Moolenaareed9d462021-02-15 20:38:25 +01002116 if (c == TAB && (!list || curwin->w_lcs_chars.tab1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002118 // tab amount depends on current column
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002119#ifdef FEAT_VARTABS
2120 n_extra = tabstop_padding(col, curbuf->b_p_ts,
2121 curbuf->b_p_vts_array) - 1;
2122#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 n_extra = curbuf->b_p_ts - col % curbuf->b_p_ts - 1;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002124#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00002125 if (!list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 {
2127 c = ' ';
2128 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01002129 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 }
2131 else
2132 {
Bram Moolenaareed9d462021-02-15 20:38:25 +01002133 c = (n_extra == 0 && curwin->w_lcs_chars.tab3)
2134 ? curwin->w_lcs_chars.tab3
2135 : curwin->w_lcs_chars.tab1;
2136 c_extra = curwin->w_lcs_chars.tab2;
2137 c_final = curwin->w_lcs_chars.tab3;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002138 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 }
2140 }
Bram Moolenaareed9d462021-02-15 20:38:25 +01002141 else if (c == 160 && list && curwin->w_lcs_chars.nbsp != NUL)
Bram Moolenaaracf17282011-02-01 17:12:25 +01002142 {
Bram Moolenaareed9d462021-02-15 20:38:25 +01002143 c = curwin->w_lcs_chars.nbsp;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002144 attr = HL_ATTR(HLF_8);
Bram Moolenaaracf17282011-02-01 17:12:25 +01002145 }
Bram Moolenaareed9d462021-02-15 20:38:25 +01002146 else if (c == NUL && list && curwin->w_lcs_chars.eol != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 {
2148 p_extra = (char_u *)"";
2149 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01002150 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 n_extra = 1;
Bram Moolenaareed9d462021-02-15 20:38:25 +01002152 c = curwin->w_lcs_chars.eol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002153 attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 --s;
2155 }
2156 else if (c != NUL && (n = byte2cells(c)) > 1)
2157 {
2158 n_extra = n - 1;
cero19881d87e112023-02-16 15:03:12 +00002159 p_extra = transchar_byte_buf(NULL, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01002161 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 c = *p_extra++;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002163 // Use special coloring to be able to distinguish <hex> from
2164 // the same in plain text.
Bram Moolenaar8820b482017-03-16 17:23:31 +01002165 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 }
zeertzjqf14b8ba2021-09-10 16:58:30 +02002167 else if (c == ' ')
Bram Moolenaar91478ae2021-02-03 15:58:13 +01002168 {
zeertzjqabc80812023-09-24 23:32:18 +02002169 if (lead != NULL && s <= lead && in_multispace
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01002170 && curwin->w_lcs_chars.leadmultispace != NULL)
2171 {
2172 c = curwin->w_lcs_chars.leadmultispace[multispace_pos++];
zeertzjqb5f08012022-06-09 13:55:28 +01002173 if (curwin->w_lcs_chars.leadmultispace[multispace_pos]
2174 == NUL)
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01002175 multispace_pos = 0;
2176 attr = HL_ATTR(HLF_8);
2177 }
zeertzjqb5f08012022-06-09 13:55:28 +01002178 else if (lead != NULL && s <= lead
2179 && curwin->w_lcs_chars.lead != NUL)
zeertzjqf14b8ba2021-09-10 16:58:30 +02002180 {
2181 c = curwin->w_lcs_chars.lead;
2182 attr = HL_ATTR(HLF_8);
2183 }
2184 else if (trail != NULL && s > trail)
2185 {
2186 c = curwin->w_lcs_chars.trail;
2187 attr = HL_ATTR(HLF_8);
2188 }
zeertzjqabc80812023-09-24 23:32:18 +02002189 else if (in_multispace
zeertzjqf14b8ba2021-09-10 16:58:30 +02002190 && curwin->w_lcs_chars.multispace != NULL)
2191 {
2192 c = curwin->w_lcs_chars.multispace[multispace_pos++];
2193 if (curwin->w_lcs_chars.multispace[multispace_pos] == NUL)
2194 multispace_pos = 0;
2195 attr = HL_ATTR(HLF_8);
2196 }
2197 else if (list && curwin->w_lcs_chars.space != NUL)
2198 {
2199 c = curwin->w_lcs_chars.space;
2200 attr = HL_ATTR(HLF_8);
2201 }
Bram Moolenaar1510f992015-04-22 22:18:22 +02002202 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 }
2204
2205 if (c == NUL)
2206 break;
2207
2208 msg_putchar_attr(c, attr);
2209 col++;
2210 }
2211 msg_clr_eos();
2212}
2213
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214/*
2215 * Use screen_puts() to output one multi-byte character.
2216 * Return the pointer "s" advanced to the next character.
2217 */
2218 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002219screen_puts_mbyte(char_u *s, int l, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220{
2221 int cw;
2222
Bram Moolenaar85a20022019-12-21 18:25:54 +01002223 msg_didout = TRUE; // remember that line is not empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 cw = (*mb_ptr2cells)(s);
2225 if (cw > 1 && (
2226#ifdef FEAT_RIGHTLEFT
2227 cmdmsg_rl ? msg_col <= 1 :
2228#endif
2229 msg_col == Columns - 1))
2230 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002231 // Doesn't fit, print a highlighted '>' to fill it up.
Bram Moolenaar8820b482017-03-16 17:23:31 +01002232 msg_screen_putchar('>', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 return s;
2234 }
2235
2236 screen_puts_len(s, l, msg_row, msg_col, attr);
2237#ifdef FEAT_RIGHTLEFT
2238 if (cmdmsg_rl)
2239 {
2240 msg_col -= cw;
2241 if (msg_col == 0)
2242 {
2243 msg_col = Columns;
2244 ++msg_row;
2245 }
2246 }
2247 else
2248#endif
2249 {
2250 msg_col += cw;
2251 if (msg_col >= Columns)
2252 {
2253 msg_col = 0;
2254 ++msg_row;
2255 }
2256 }
2257 return s + l;
2258}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259
2260/*
2261 * Output a string to the screen at position msg_row, msg_col.
2262 * Update msg_row and msg_col for the next message.
2263 */
2264 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002265msg_puts(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266{
Bram Moolenaaraeac9002016-09-06 22:15:08 +02002267 msg_puts_attr(s, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268}
2269
2270 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002271msg_puts_title(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272{
Bram Moolenaar8820b482017-03-16 17:23:31 +01002273 msg_puts_attr(s, HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274}
2275
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276/*
2277 * Show a message in such a way that it always fits in the line. Cut out a
2278 * part in the middle and replace it with "..." when necessary.
2279 * Does not handle multi-byte characters!
2280 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002281 static void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002282msg_outtrans_long_len_attr(char_u *longstr, int len, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283{
2284 int slen = len;
2285 int room;
2286
2287 room = Columns - msg_col;
2288 if (len > room && room >= 20)
2289 {
2290 slen = (room - 3) / 2;
2291 msg_outtrans_len_attr(longstr, slen, attr);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002292 msg_puts_attr("...", HL_ATTR(HLF_8));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 }
2294 msg_outtrans_len_attr(longstr + len - slen, slen, attr);
2295}
2296
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002297 void
2298msg_outtrans_long_attr(char_u *longstr, int attr)
2299{
2300 msg_outtrans_long_len_attr(longstr, (int)STRLEN(longstr), attr);
2301}
2302
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303/*
2304 * Basic function for writing a message with highlight attributes.
2305 */
2306 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002307msg_puts_attr(char *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308{
2309 msg_puts_attr_len(s, -1, attr);
2310}
2311
2312/*
2313 * Like msg_puts_attr(), but with a maximum length "maxlen" (in bytes).
2314 * When "maxlen" is -1 there is no maximum length.
2315 * When "maxlen" is >= 0 the message is not put in the history.
2316 */
2317 static void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002318msg_puts_attr_len(char *str, int maxlen, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 /*
2321 * If redirection is on, also write to the redirection file.
2322 */
Bram Moolenaar32526b32019-01-19 17:43:09 +01002323 redir_write((char_u *)str, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324
2325 /*
2326 * Don't print anything when using ":silent cmd".
2327 */
2328 if (msg_silent != 0)
2329 return;
2330
Shougo Matsushitabe2b03c2024-04-08 22:11:50 +02002331 if (attr == 0)
2332 attr = HL_ATTR(HLF_MSG);
2333
Bram Moolenaar85a20022019-12-21 18:25:54 +01002334 // if MSG_HIST flag set, add message to history
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 if ((attr & MSG_HIST) && maxlen < 0)
2336 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002337 add_msg_hist((char_u *)str, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 attr &= ~MSG_HIST;
2339 }
2340
Bram Moolenaare8070982019-10-12 17:07:06 +02002341 // When writing something to the screen after it has scrolled, requires a
2342 // wait-return prompt later. Needed when scrolling, resetting
2343 // need_wait_return after some prompt, and then outputting something
2344 // without scrolling
2345 // Not needed when only using CR to move the cursor.
2346 if (msg_scrolled != 0 && !msg_scrolled_ign && STRCMP(str, "\r") != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 need_wait_return = TRUE;
Bram Moolenaare8070982019-10-12 17:07:06 +02002348 msg_didany = TRUE; // remember that something was outputted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349
2350 /*
2351 * If there is no valid screen, use fprintf so we can see error messages.
2352 * If termcap is not active, we may be writing in an alternate console
2353 * window, cursor positioning may not work correctly (window size may be
2354 * different, e.g. for Win32 console) or we just don't know where the
2355 * cursor is.
2356 */
2357 if (msg_use_printf())
Bram Moolenaar32526b32019-01-19 17:43:09 +01002358 msg_puts_printf((char_u *)str, maxlen);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002359 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01002360 msg_puts_display((char_u *)str, maxlen, attr, FALSE);
Rob Pilling726f7f92022-01-20 14:44:38 +00002361
2362 need_fileinfo = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002363}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364
Bram Moolenaar9198de32022-08-27 21:30:03 +01002365// values for "where"
2366#define PUT_APPEND 0 // append to "lnum"
2367#define PUT_TRUNC 1 // replace "lnum"
2368#define PUT_BELOW 2 // add below "lnum"
2369 //
2370#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaar9198de32022-08-27 21:30:03 +01002371/*
Bram Moolenaar24735f22022-08-30 15:44:22 +01002372 * Put text "t_s" until "end" in the message window.
Bram Moolenaar9198de32022-08-27 21:30:03 +01002373 * "where" specifies where to put the text.
2374 */
2375 static void
2376put_msg_win(win_T *wp, int where, char_u *t_s, char_u *end, linenr_T lnum)
2377{
Bram Moolenaarbeedd0a2022-08-27 21:52:52 +01002378 char_u *p;
Bram Moolenaar9198de32022-08-27 21:30:03 +01002379
Bram Moolenaar9198de32022-08-27 21:30:03 +01002380 if (where == PUT_BELOW)
Bram Moolenaarbeedd0a2022-08-27 21:52:52 +01002381 {
2382 if (*end != NUL)
2383 {
2384 p = vim_strnsave(t_s, end - t_s);
2385 if (p == NULL)
2386 return;
2387 }
2388 else
2389 p = t_s;
2390 ml_append_buf(wp->w_buffer, lnum, p, (colnr_T)0, FALSE);
2391 if (p != t_s)
2392 vim_free(p);
2393 }
Bram Moolenaar9198de32022-08-27 21:30:03 +01002394 else
2395 {
2396 char_u *newp;
2397
2398 curbuf = wp->w_buffer;
2399 if (where == PUT_APPEND)
Bram Moolenaarbeedd0a2022-08-27 21:52:52 +01002400 {
Bram Moolenaar9198de32022-08-27 21:30:03 +01002401 newp = concat_str(ml_get(lnum), t_s);
Bram Moolenaarbeedd0a2022-08-27 21:52:52 +01002402 if (newp == NULL)
2403 return;
2404 if (*end != NUL)
2405 newp[STRLEN(ml_get(lnum)) + (end - t_s)] = NUL;
2406 }
Bram Moolenaar9198de32022-08-27 21:30:03 +01002407 else
Bram Moolenaarbeedd0a2022-08-27 21:52:52 +01002408 {
Bram Moolenaar9198de32022-08-27 21:30:03 +01002409 newp = vim_strnsave(t_s, end - t_s);
Bram Moolenaarbeedd0a2022-08-27 21:52:52 +01002410 if (newp == NULL)
2411 return;
2412 }
Bram Moolenaar9198de32022-08-27 21:30:03 +01002413 ml_replace(lnum, newp, FALSE);
2414 curbuf = curwin->w_buffer;
2415 }
2416 redraw_win_later(wp, UPD_NOT_VALID);
2417
2418 // set msg_col so that a newline is written if needed
Bram Moolenaar24735f22022-08-30 15:44:22 +01002419 msg_col += (int)(end - t_s);
Bram Moolenaar9198de32022-08-27 21:30:03 +01002420}
2421#endif
2422
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002423/*
2424 * The display part of msg_puts_attr_len().
2425 * May be called recursively to display scroll-back text.
2426 */
2427 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002428msg_puts_display(
2429 char_u *str,
2430 int maxlen,
2431 int attr,
2432 int recurse)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002433{
2434 char_u *s = str;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002435 char_u *t_s = str; // string from "t_s" to "s" is still todo
2436 int t_col = 0; // screen cells todo, 0 when "t_s" not used
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002437 int l;
2438 int cw;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002439 char_u *sb_str = str;
2440 int sb_col = msg_col;
2441 int wrap;
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002442 int did_last_char;
Bram Moolenaar9198de32022-08-27 21:30:03 +01002443#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaard54af2e2022-08-27 22:05:13 +01002444 int where = PUT_APPEND;
Bram Moolenaar9198de32022-08-27 21:30:03 +01002445 win_T *msg_win = NULL;
2446 linenr_T lnum = 1;
2447
Bram Moolenaara2a89732022-08-31 14:46:18 +01002448 if (in_echowindow)
Bram Moolenaar9198de32022-08-27 21:30:03 +01002449 {
2450 msg_win = popup_get_message_win();
2451
2452 if (msg_win != NULL)
2453 {
2454 if (!popup_message_win_visible())
2455 {
2456 if (*str == NL)
2457 {
2458 // When not showing the message window and the output
2459 // starts with a NL show the message normally.
2460 msg_win = NULL;
2461 }
2462 else
2463 {
2464 // currently hidden, make it empty
2465 curbuf = msg_win->w_buffer;
2466 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
2467 ml_delete(1);
2468 curbuf = curwin->w_buffer;
2469 }
2470 }
2471 else
2472 {
2473 lnum = msg_win->w_buffer->b_ml.ml_line_count;
2474 if (msg_col == 0)
2475 where = PUT_TRUNC;
2476 }
2477 }
2478 }
2479#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480
2481 did_wait_return = FALSE;
Bram Moolenaar57b7fe82007-08-05 17:20:43 +00002482 while ((maxlen < 0 || (int)(s - str) < maxlen) && *s != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 {
2484 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002485 * We are at the end of the screen line when:
2486 * - When outputting a newline.
2487 * - When outputting a character in the last column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002489 if (!recurse && msg_row >= Rows - 1 && (*s == '\n' || (
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490#ifdef FEAT_RIGHTLEFT
2491 cmdmsg_rl
2492 ? (
2493 msg_col <= 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002494 || (*s == TAB && msg_col <= 7)
2495 || (has_mbyte && (*mb_ptr2cells)(s) > 1 && msg_col <= 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496 :
2497#endif
Bram Moolenaar0efd1bd2019-12-11 19:00:04 +01002498 ((*s != '\r' && msg_col + t_col >= Columns - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 || (*s == TAB && msg_col + t_col >= ((Columns - 1) & ~7))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 || (has_mbyte && (*mb_ptr2cells)(s) > 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002501 && msg_col + t_col >= Columns - 2)))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002503 /*
2504 * The screen is scrolled up when at the last row (some terminals
2505 * scroll automatically, some don't. To avoid problems we scroll
2506 * ourselves).
2507 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 if (t_col > 0)
Bram Moolenaar9198de32022-08-27 21:30:03 +01002509 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002510 // output postponed text
Bram Moolenaar9198de32022-08-27 21:30:03 +01002511#ifdef HAS_MESSAGE_WINDOW
2512 if (msg_win != NULL)
2513 {
2514 put_msg_win(msg_win, where, t_s, s, lnum);
2515 t_col = 0;
2516 where = PUT_BELOW;
2517 }
2518 else
2519#endif
2520 t_puts(&t_col, t_s, s, attr);
2521 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522
Bram Moolenaar85a20022019-12-21 18:25:54 +01002523 // When no more prompt and no more room, truncate here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 if (msg_no_more && lines_left == 0)
2525 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526
Bram Moolenaar9198de32022-08-27 21:30:03 +01002527#ifdef HAS_MESSAGE_WINDOW
2528 if (msg_win == NULL)
2529#endif
2530 // Scroll the screen up one line.
2531 msg_scroll_up();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532
2533 msg_row = Rows - 2;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002534 if (msg_col >= Columns) // can happen after screen resize
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 msg_col = Columns - 1;
2536
Bram Moolenaar85a20022019-12-21 18:25:54 +01002537 // Display char in last column before showing more-prompt.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002538 if (*s >= ' '
2539#ifdef FEAT_RIGHTLEFT
2540 && !cmdmsg_rl
2541#endif
2542 )
2543 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002544 if (has_mbyte)
2545 {
2546 if (enc_utf8 && maxlen >= 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002547 // avoid including composing chars after the end
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002548 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002549 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002550 l = (*mb_ptr2len)(s);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002551 s = screen_puts_mbyte(s, l, attr);
2552 }
2553 else
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002554 msg_screen_putchar(*s++, attr);
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002555 did_last_char = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002556 }
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002557 else
2558 did_last_char = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002559
2560 if (p_more)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002561 // store text for scrolling back
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002562 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
2563
Bram Moolenaar9198de32022-08-27 21:30:03 +01002564#ifdef HAS_MESSAGE_WINDOW
2565 if (msg_win == NULL)
2566 {
2567#endif
2568 inc_msg_scrolled();
Bram Moolenaar13608d82022-08-29 15:06:50 +01002569 need_wait_return = TRUE; // may need wait_return() in main()
Bram Moolenaar9198de32022-08-27 21:30:03 +01002570 redraw_cmdline = TRUE;
2571 if (cmdline_row > 0 && !exmode_active)
2572 --cmdline_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573
Bram Moolenaar9198de32022-08-27 21:30:03 +01002574 /*
2575 * If screen is completely filled and 'more' is set then wait
2576 * for a character.
2577 */
2578 if (lines_left > 0)
2579 --lines_left;
2580#ifdef HAS_MESSAGE_WINDOW
2581 }
2582#endif
Bram Moolenaar24959102022-05-07 20:01:16 +01002583 if (p_more && lines_left == 0 && State != MODE_HITRETURN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 && !msg_no_more && !exmode_active)
2585 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586#ifdef FEAT_CON_DIALOG
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002587 if (do_more_prompt(NUL))
2588 s = confirm_msg_tail;
2589#else
2590 (void)do_more_prompt(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591#endif
2592 if (quit_more)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002593 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002595
Bram Moolenaar85a20022019-12-21 18:25:54 +01002596 // When we displayed a char in last column need to check if there
2597 // is still more.
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002598 if (did_last_char)
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002599 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600 }
2601
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002602 wrap = *s == '\n'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603 || msg_col + t_col >= Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 || (has_mbyte && (*mb_ptr2cells)(s) > 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002605 && msg_col + t_col >= Columns - 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002606 if (t_col > 0 && (wrap || *s == '\r' || *s == '\b'
2607 || *s == '\t' || *s == BELL))
Bram Moolenaar9198de32022-08-27 21:30:03 +01002608 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002609 // output any postponed text
Bram Moolenaar9198de32022-08-27 21:30:03 +01002610#ifdef HAS_MESSAGE_WINDOW
2611 if (msg_win != NULL)
2612 {
2613 put_msg_win(msg_win, where, t_s, s, lnum);
2614 t_col = 0;
2615 where = PUT_BELOW;
2616 }
2617 else
2618#endif
2619 t_puts(&t_col, t_s, s, attr);
2620 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002621
2622 if (wrap && p_more && !recurse)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002623 // store text for scrolling back
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002624 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625
Bram Moolenaar85a20022019-12-21 18:25:54 +01002626 if (*s == '\n') // go to next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 {
Bram Moolenaar9198de32022-08-27 21:30:03 +01002628#ifdef HAS_MESSAGE_WINDOW
2629 if (msg_win != NULL)
2630 {
2631 // Ignore a NL when the buffer is empty, it is used to scroll
2632 // up the text.
2633 if ((msg_win->w_buffer->b_ml.ml_flags & ML_EMPTY) == 0)
2634 {
2635 put_msg_win(msg_win, PUT_BELOW, t_s, t_s, lnum);
2636 ++lnum;
2637 }
2638 }
2639 else
2640#endif
2641 msg_didout = FALSE; // remember that line is empty
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002642#ifdef FEAT_RIGHTLEFT
2643 if (cmdmsg_rl)
2644 msg_col = Columns - 1;
2645 else
2646#endif
2647 msg_col = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002648 if (++msg_row >= Rows) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 msg_row = Rows - 1;
2650 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002651 else if (*s == '\r') // go to column 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 {
2653 msg_col = 0;
Bram Moolenaard54af2e2022-08-27 22:05:13 +01002654#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaar9198de32022-08-27 21:30:03 +01002655 where = PUT_TRUNC;
Bram Moolenaard54af2e2022-08-27 22:05:13 +01002656#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002658 else if (*s == '\b') // go to previous char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 {
2660 if (msg_col)
2661 --msg_col;
2662 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002663 else if (*s == TAB) // translate Tab into spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 {
Bram Moolenaar9198de32022-08-27 21:30:03 +01002665#ifdef HAS_MESSAGE_WINDOW
2666 if (msg_win != NULL)
2667 msg_col = (msg_col + 7) % 8;
2668 else
2669#endif
2670 do
2671 msg_screen_putchar(' ', attr);
2672 while (msg_col & 7);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002674 else if (*s == BELL) // beep (from ":sh")
Bram Moolenaar165bc692015-07-21 17:53:25 +02002675 vim_beep(BO_SH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 else
2677 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 if (has_mbyte)
2679 {
2680 cw = (*mb_ptr2cells)(s);
2681 if (enc_utf8 && maxlen >= 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002682 // avoid including composing chars after the end
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002683 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002685 l = (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 }
2687 else
2688 {
2689 cw = 1;
2690 l = 1;
2691 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002692
Bram Moolenaar85a20022019-12-21 18:25:54 +01002693 // When drawing from right to left or when a double-wide character
2694 // doesn't fit, draw a single character here. Otherwise collect
2695 // characters and draw them all at once later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 if (
2697# ifdef FEAT_RIGHTLEFT
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002698 cmdmsg_rl ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699# endif
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002700 (cw > 1 && msg_col + t_col >= Columns - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 if (l > 1)
2703 s = screen_puts_mbyte(s, l, attr) - 1;
2704 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 msg_screen_putchar(*s, attr);
2706 }
2707 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002709 // postpone this character until later
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 if (t_col == 0)
2711 t_s = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 t_col += cw;
2713 s += l - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 }
2715 }
2716 ++s;
2717 }
2718
Bram Moolenaar85a20022019-12-21 18:25:54 +01002719 // output any postponed text
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 if (t_col > 0)
Bram Moolenaar9198de32022-08-27 21:30:03 +01002721 {
2722#ifdef HAS_MESSAGE_WINDOW
2723 if (msg_win != NULL)
2724 put_msg_win(msg_win, where, t_s, s, lnum);
2725 else
2726#endif
2727 t_puts(&t_col, t_s, s, attr);
2728 }
2729
2730#ifdef HAS_MESSAGE_WINDOW
2731 if (msg_win != NULL)
2732 popup_show_message_win();
2733#endif
Bram Moolenaar11901392022-09-26 19:50:44 +01002734 // Store the text for scroll back, unless it's a newline by itself.
2735 if (p_more && !recurse && !(s == sb_str + 1 && *sb_str == '\n'))
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002736 store_sb_text(&sb_str, s, attr, &sb_col, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737
2738 msg_check();
2739}
2740
2741/*
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002742 * Return TRUE when ":filter pattern" was used and "msg" does not match
2743 * "pattern".
2744 */
2745 int
2746message_filtered(char_u *msg)
2747{
Bram Moolenaard29459b2016-08-26 22:29:11 +02002748 int match;
2749
Bram Moolenaare1004402020-10-24 20:49:43 +02002750 if (cmdmod.cmod_filter_regmatch.regprog == NULL)
Bram Moolenaard29459b2016-08-26 22:29:11 +02002751 return FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02002752 match = vim_regexec(&cmdmod.cmod_filter_regmatch, msg, (colnr_T)0);
2753 return cmdmod.cmod_filter_force ? match : !match;
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002754}
2755
2756/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002757 * Scroll the screen up one line for displaying the next message line.
2758 */
2759 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002760msg_scroll_up(void)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002761{
Bram Moolenaar9198de32022-08-27 21:30:03 +01002762#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaara2a89732022-08-31 14:46:18 +01002763 if (in_echowindow)
Bram Moolenaar9198de32022-08-27 21:30:03 +01002764 return;
2765#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002766#ifdef FEAT_GUI
Bram Moolenaar85a20022019-12-21 18:25:54 +01002767 // Remove the cursor before scrolling, ScreenLines[] is going
2768 // to become invalid.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002769 if (gui.in_use)
2770 gui_undraw_cursor();
2771#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01002772 // scrolling up always works
Bram Moolenaara338adc2018-01-31 20:51:47 +01002773 mch_disable_flush();
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002774 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaara338adc2018-01-31 20:51:47 +01002775 mch_enable_flush();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002776
2777 if (!can_clear((char_u *)" "))
2778 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002779 // Scrolling up doesn't result in the right background. Set the
2780 // background here. It's not efficient, but avoids that we have to do
2781 // it all over the code.
Shougo Matsushitabe2b03c2024-04-08 22:11:50 +02002782 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns,
2783 ' ', ' ', HL_ATTR(HLF_MSG));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002784
Bram Moolenaar85a20022019-12-21 18:25:54 +01002785 // Also clear the last char of the last but one line if it was not
2786 // cleared before to avoid a scroll-up.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002787 if (ScreenAttrs[LineOffset[Rows - 2] + Columns - 1] == (sattr_T)-1)
2788 screen_fill((int)Rows - 2, (int)Rows - 1,
Shougo Matsushitabe2b03c2024-04-08 22:11:50 +02002789 (int)Columns - 1, (int)Columns,
2790 ' ', ' ', HL_ATTR(HLF_MSG));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002791 }
2792}
2793
2794/*
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002795 * Increment "msg_scrolled".
2796 */
2797 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002798inc_msg_scrolled(void)
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002799{
2800#ifdef FEAT_EVAL
2801 if (*get_vim_var_str(VV_SCROLLSTART) == NUL)
2802 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002803 char_u *p = SOURCING_NAME;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002804 char_u *tofree = NULL;
2805 int len;
2806
Bram Moolenaar85a20022019-12-21 18:25:54 +01002807 // v:scrollstart is empty, set it to the script/function name and line
2808 // number
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002809 if (p == NULL)
2810 p = (char_u *)_("Unknown");
2811 else
2812 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002813 len = (int)STRLEN(p) + 40;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002814 tofree = alloc(len);
2815 if (tofree != NULL)
2816 {
2817 vim_snprintf((char *)tofree, len, _("%s line %ld"),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002818 p, (long)SOURCING_LNUM);
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002819 p = tofree;
2820 }
2821 }
2822 set_vim_var_string(VV_SCROLLSTART, p, -1);
2823 vim_free(tofree);
2824 }
2825#endif
2826 ++msg_scrolled;
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01002827 set_must_redraw(UPD_VALID);
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002828}
2829
2830/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002831 * To be able to scroll back at the "more" and "hit-enter" prompts we need to
2832 * store the displayed text and remember where screen lines start.
2833 */
2834typedef struct msgchunk_S msgchunk_T;
2835struct msgchunk_S
2836{
2837 msgchunk_T *sb_next;
2838 msgchunk_T *sb_prev;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002839 char sb_eol; // TRUE when line ends after this text
2840 int sb_msg_col; // column in which text starts
2841 int sb_attr; // text attributes
2842 char_u sb_text[1]; // text to be displayed, actually longer
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002843};
2844
Bram Moolenaar85a20022019-12-21 18:25:54 +01002845static msgchunk_T *last_msgchunk = NULL; // last displayed text
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002846
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002847static msgchunk_T *msg_sb_start(msgchunk_T *mps);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002848
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002849typedef enum {
2850 SB_CLEAR_NONE = 0,
2851 SB_CLEAR_ALL,
2852 SB_CLEAR_CMDLINE_BUSY,
2853 SB_CLEAR_CMDLINE_DONE
2854} sb_clear_T;
2855
Bram Moolenaar85a20022019-12-21 18:25:54 +01002856// When to clear text on next msg.
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002857static sb_clear_T do_clear_sb_text = SB_CLEAR_NONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002858
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002859/*
2860 * Store part of a printed message for displaying when scrolling back.
2861 */
2862 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002863store_sb_text(
Bram Moolenaar85a20022019-12-21 18:25:54 +01002864 char_u **sb_str, // start of string
2865 char_u *s, // just after string
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002866 int attr,
2867 int *sb_col,
Bram Moolenaar85a20022019-12-21 18:25:54 +01002868 int finish) // line ends
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002869{
2870 msgchunk_T *mp;
2871
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002872 if (do_clear_sb_text == SB_CLEAR_ALL
2873 || do_clear_sb_text == SB_CLEAR_CMDLINE_DONE)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002874 {
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002875 clear_sb_text(do_clear_sb_text == SB_CLEAR_ALL);
zeertzjq46af7bc2022-07-28 12:34:09 +01002876 msg_sb_eol(); // prevent messages from overlapping
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002877 do_clear_sb_text = SB_CLEAR_NONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002878 }
2879
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002880 if (s > *sb_str)
2881 {
zeertzjq1b438a82023-02-01 13:11:15 +00002882 mp = alloc(offsetof(msgchunk_T, sb_text) + (s - *sb_str) + 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002883 if (mp != NULL)
2884 {
2885 mp->sb_eol = finish;
2886 mp->sb_msg_col = *sb_col;
2887 mp->sb_attr = attr;
2888 vim_strncpy(mp->sb_text, *sb_str, s - *sb_str);
2889
2890 if (last_msgchunk == NULL)
2891 {
2892 last_msgchunk = mp;
2893 mp->sb_prev = NULL;
2894 }
2895 else
2896 {
2897 mp->sb_prev = last_msgchunk;
2898 last_msgchunk->sb_next = mp;
2899 last_msgchunk = mp;
2900 }
2901 mp->sb_next = NULL;
2902 }
2903 }
2904 else if (finish && last_msgchunk != NULL)
2905 last_msgchunk->sb_eol = TRUE;
2906
2907 *sb_str = s;
2908 *sb_col = 0;
2909}
2910
2911/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002912 * Finished showing messages, clear the scroll-back text on the next message.
2913 */
2914 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002915may_clear_sb_text(void)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002916{
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002917 do_clear_sb_text = SB_CLEAR_ALL;
2918}
2919
2920/*
zeertzjq46af7bc2022-07-28 12:34:09 +01002921 * Starting to edit the command line: do not clear messages now.
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002922 */
2923 void
2924sb_text_start_cmdline(void)
2925{
zeertzjq46af7bc2022-07-28 12:34:09 +01002926 if (do_clear_sb_text == SB_CLEAR_CMDLINE_BUSY)
2927 // Invoking command line recursively: the previous-level command line
2928 // doesn't need to be remembered as it will be redrawn when returning
2929 // to that level.
2930 sb_text_restart_cmdline();
2931 else
2932 {
2933 msg_sb_eol();
2934 do_clear_sb_text = SB_CLEAR_CMDLINE_BUSY;
2935 }
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002936}
2937
2938/*
zeertzjq46af7bc2022-07-28 12:34:09 +01002939 * Redrawing the command line: clear the last unfinished line.
2940 */
2941 void
2942sb_text_restart_cmdline(void)
2943{
2944 msgchunk_T *tofree;
2945
2946 // Needed when returning from nested command line.
2947 do_clear_sb_text = SB_CLEAR_CMDLINE_BUSY;
2948
2949 if (last_msgchunk == NULL || last_msgchunk->sb_eol)
2950 // No unfinished line: don't clear anything.
2951 return;
2952
2953 tofree = msg_sb_start(last_msgchunk);
2954 last_msgchunk = tofree->sb_prev;
2955 if (last_msgchunk != NULL)
2956 last_msgchunk->sb_next = NULL;
2957 while (tofree != NULL)
2958 {
2959 msgchunk_T *tofree_next = tofree->sb_next;
2960
2961 vim_free(tofree);
2962 tofree = tofree_next;
2963 }
2964}
2965
2966/*
2967 * Ending to edit the command line: clear old lines but the last one later.
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002968 */
2969 void
2970sb_text_end_cmdline(void)
2971{
2972 do_clear_sb_text = SB_CLEAR_CMDLINE_DONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002973}
2974
2975/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002976 * Clear any text remembered for scrolling back.
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002977 * When "all" is FALSE keep the last line.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002978 * Called when redrawing the screen.
2979 */
2980 void
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002981clear_sb_text(int all)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002982{
2983 msgchunk_T *mp;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002984 msgchunk_T **lastp;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002985
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002986 if (all)
2987 lastp = &last_msgchunk;
2988 else
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002989 {
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002990 if (last_msgchunk == NULL)
2991 return;
zeertzjqecdc82e2022-07-25 19:50:57 +01002992 lastp = &msg_sb_start(last_msgchunk)->sb_prev;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002993 }
2994
2995 while (*lastp != NULL)
2996 {
2997 mp = (*lastp)->sb_prev;
2998 vim_free(*lastp);
2999 *lastp = mp;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003000 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003001}
3002
3003/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003004 * "g<" command.
3005 */
3006 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003007show_sb_text(void)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003008{
3009 msgchunk_T *mp;
3010
Bram Moolenaar85a20022019-12-21 18:25:54 +01003011 // Only show something if there is more than one line, otherwise it looks
3012 // weird, typing a command without output results in one line.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003013 mp = msg_sb_start(last_msgchunk);
3014 if (mp == NULL || mp->sb_prev == NULL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02003015 vim_beep(BO_MESS);
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003016 else
3017 {
3018 do_more_prompt('G');
3019 wait_return(FALSE);
3020 }
3021}
3022
3023/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003024 * Move to the start of screen line in already displayed text.
3025 */
3026 static msgchunk_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003027msg_sb_start(msgchunk_T *mps)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003028{
3029 msgchunk_T *mp = mps;
3030
3031 while (mp != NULL && mp->sb_prev != NULL && !mp->sb_prev->sb_eol)
3032 mp = mp->sb_prev;
3033 return mp;
3034}
3035
3036/*
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02003037 * Mark the last message chunk as finishing the line.
3038 */
3039 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003040msg_sb_eol(void)
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02003041{
3042 if (last_msgchunk != NULL)
3043 last_msgchunk->sb_eol = TRUE;
3044}
3045
3046/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003047 * Display a screen line from previously displayed text at row "row".
Bram Moolenaar838b7462022-09-26 15:19:56 +01003048 * When "clear_to_eol" is set clear the rest of the screen line.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003049 * Returns a pointer to the text for the next line (can be NULL).
3050 */
3051 static msgchunk_T *
Bram Moolenaar838b7462022-09-26 15:19:56 +01003052disp_sb_line(int row, msgchunk_T *smp, int clear_to_eol)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003053{
3054 msgchunk_T *mp = smp;
3055 char_u *p;
3056
3057 for (;;)
3058 {
3059 msg_row = row;
3060 msg_col = mp->sb_msg_col;
3061 p = mp->sb_text;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003062 if (*p == '\n') // don't display the line break
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003063 ++p;
3064 msg_puts_display(p, -1, mp->sb_attr, TRUE);
Bram Moolenaar838b7462022-09-26 15:19:56 +01003065
3066 // If clearing the screen did not work (e.g. because of a background
3067 // color and t_ut isn't set) clear until the last column here.
3068 if (clear_to_eol)
Shougo Matsushitabe2b03c2024-04-08 22:11:50 +02003069 screen_fill(row, row + 1, msg_col, (int)Columns,
3070 ' ', ' ', HL_ATTR(HLF_MSG));
Bram Moolenaar838b7462022-09-26 15:19:56 +01003071
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003072 if (mp->sb_eol || mp->sb_next == NULL)
3073 break;
3074 mp = mp->sb_next;
3075 }
3076 return mp->sb_next;
3077}
3078
3079/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080 * Output any postponed text for msg_puts_attr_len().
3081 */
3082 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003083t_puts(
3084 int *t_col,
3085 char_u *t_s,
3086 char_u *s,
3087 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088{
Bram Moolenaar85a20022019-12-21 18:25:54 +01003089 // output postponed text
3090 msg_didout = TRUE; // remember that line is not empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 screen_puts_len(t_s, (int)(s - t_s), msg_row, msg_col, attr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003092 msg_col += *t_col;
3093 *t_col = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003094 // If the string starts with a composing character don't increment the
3095 // column position for it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096 if (enc_utf8 && utf_iscomposing(utf_ptr2char(t_s)))
3097 --msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 if (msg_col >= Columns)
3099 {
3100 msg_col = 0;
3101 ++msg_row;
3102 }
3103}
3104
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105/*
3106 * Returns TRUE when messages should be printed with mch_errmsg().
3107 * This is used when there is no valid screen, so we can see error messages.
3108 * If termcap is not active, we may be writing in an alternate console
3109 * window, cursor positioning may not work correctly (window size may be
3110 * different, e.g. for Win32 console) or we just don't know where the
3111 * cursor is.
3112 */
3113 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003114msg_use_printf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115{
3116 return (!msg_check_screen()
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003117#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3118# ifdef VIMDLL
3119 || (!gui.in_use && !termcap_active)
3120# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 || !termcap_active
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003122# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123#endif
3124 || (swapping_screen() && !termcap_active)
3125 );
3126}
3127
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003128/*
3129 * Print a message when there is no valid screen.
3130 */
3131 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003132msg_puts_printf(char_u *str, int maxlen)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003133{
3134 char_u *s = str;
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003135 char_u *buf = NULL;
3136 char_u *p = s;
3137
Bram Moolenaar4f974752019-02-17 17:44:42 +01003138#ifdef MSWIN
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003139 if (!(silent_mode && p_verbose == 0))
Bram Moolenaar85a20022019-12-21 18:25:54 +01003140 mch_settmode(TMODE_COOK); // handle CR and NL correctly
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003141#endif
Bram Moolenaar2321ca22016-09-09 14:17:18 +02003142 while ((maxlen < 0 || (int)(s - str) < maxlen) && *s != NUL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003143 {
3144 if (!(silent_mode && p_verbose == 0))
3145 {
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003146 // NL --> CR NL translation (for Unix, not for "--version")
3147 if (*s == NL)
3148 {
3149 int n = (int)(s - p);
3150
3151 buf = alloc(n + 3);
Bram Moolenaar6f10c702019-08-20 22:58:37 +02003152 if (buf != NULL)
3153 {
3154 memcpy(buf, p, n);
3155 if (!info_message)
3156 buf[n++] = CAR;
3157 buf[n++] = NL;
3158 buf[n++] = NUL;
3159 if (info_message) // informative message, not an error
3160 mch_msg((char *)buf);
3161 else
3162 mch_errmsg((char *)buf);
3163 vim_free(buf);
3164 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003165 p = s + 1;
3166 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003167 }
3168
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003169 // primitive way to compute the current column
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003170#ifdef FEAT_RIGHTLEFT
3171 if (cmdmsg_rl)
3172 {
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003173 if (*s == CAR || *s == NL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003174 msg_col = Columns - 1;
3175 else
3176 --msg_col;
3177 }
3178 else
3179#endif
3180 {
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003181 if (*s == CAR || *s == NL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003182 msg_col = 0;
3183 else
3184 ++msg_col;
3185 }
3186 ++s;
3187 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003188
3189 if (*p != NUL && !(silent_mode && p_verbose == 0))
3190 {
Bram Moolenaara97c3632021-06-15 22:39:11 +02003191 char_u *tofree = NULL;
Bram Moolenaar97c2c052019-02-22 13:42:07 +01003192
Bram Moolenaarc32949b2023-01-04 15:56:51 +00003193 if (maxlen > 0 && vim_strlen_maxlen((char *)p, (size_t)maxlen)
3194 >= (size_t)maxlen)
Bram Moolenaar97c2c052019-02-22 13:42:07 +01003195 {
Bram Moolenaara97c3632021-06-15 22:39:11 +02003196 tofree = vim_strnsave(p, (size_t)maxlen);
3197 p = tofree;
Bram Moolenaar97c2c052019-02-22 13:42:07 +01003198 }
Bram Moolenaara97c3632021-06-15 22:39:11 +02003199 if (p != NULL)
3200 {
3201 if (info_message)
3202 mch_msg((char *)p);
3203 else
3204 mch_errmsg((char *)p);
3205 vim_free(tofree);
3206 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003207 }
3208
3209 msg_didout = TRUE; // assume that line is not empty
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003210
Bram Moolenaar4f974752019-02-17 17:44:42 +01003211#ifdef MSWIN
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003212 if (!(silent_mode && p_verbose == 0))
3213 mch_settmode(TMODE_RAW);
3214#endif
3215}
3216
3217/*
3218 * Show the more-prompt and handle the user response.
3219 * This takes care of scrolling back and displaying previously displayed text.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003220 * When at hit-enter prompt "typed_char" is the already typed character,
3221 * otherwise it's NUL.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003222 * Returns TRUE when jumping ahead to "confirm_msg_tail".
3223 */
3224 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003225do_more_prompt(int typed_char)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003226{
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01003227 static int entered = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003228 int used_typed_char = typed_char;
3229 int oldState = State;
3230 int c;
3231#ifdef FEAT_CON_DIALOG
3232 int retval = FALSE;
3233#endif
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003234 int toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003235 msgchunk_T *mp_last = NULL;
3236 msgchunk_T *mp;
3237 int i;
Shougo Matsushitabe2b03c2024-04-08 22:11:50 +02003238 int msg_attr;
3239
3240 msg_attr = HL_ATTR(HLF_MSG);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003241
Bram Moolenaar85a20022019-12-21 18:25:54 +01003242 // We get called recursively when a timer callback outputs a message. In
3243 // that case don't show another prompt. Also when at the hit-Enter prompt
3244 // and nothing was typed.
Bram Moolenaar24959102022-05-07 20:01:16 +01003245 if (entered || (State == MODE_HITRETURN && typed_char == 0))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01003246 return FALSE;
3247 entered = TRUE;
3248
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003249 if (typed_char == 'G')
3250 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003251 // "g<": Find first line on the last page.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003252 mp_last = msg_sb_start(last_msgchunk);
3253 for (i = 0; i < Rows - 2 && mp_last != NULL
3254 && mp_last->sb_prev != NULL; ++i)
3255 mp_last = msg_sb_start(mp_last->sb_prev);
3256 }
3257
Bram Moolenaar24959102022-05-07 20:01:16 +01003258 State = MODE_ASKMORE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003259 setmouse();
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003260 if (typed_char == NUL)
3261 msg_moremsg(FALSE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003262 for (;;)
3263 {
3264 /*
3265 * Get a typed character directly from the user.
3266 */
3267 if (used_typed_char != NUL)
3268 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003269 c = used_typed_char; // was typed at hit-enter prompt
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003270 used_typed_char = NUL;
3271 }
3272 else
3273 c = get_keystroke();
3274
3275#if defined(FEAT_MENU) && defined(FEAT_GUI)
3276 if (c == K_MENU)
3277 {
Bram Moolenaar24959102022-05-07 20:01:16 +01003278 int idx = get_menu_index(current_menu, MODE_ASKMORE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003279
Bram Moolenaar85a20022019-12-21 18:25:54 +01003280 // Used a menu. If it starts with CTRL-Y, it must
3281 // be a "Copy" for the clipboard. Otherwise
3282 // assume that we end
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003283 if (idx == MENU_INDEX_INVALID)
3284 continue;
3285 c = *current_menu->strings[idx];
3286 if (c != NUL && current_menu->strings[idx][1] != NUL)
3287 ins_typebuf(current_menu->strings[idx] + 1,
3288 current_menu->noremap[idx], 0, TRUE,
3289 current_menu->silent[idx]);
3290 }
3291#endif
3292
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003293 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003294 switch (c)
3295 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003296 case BS: // scroll one line back
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003297 case K_BS:
3298 case 'k':
3299 case K_UP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003300 toscroll = -1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003301 break;
3302
Bram Moolenaar85a20022019-12-21 18:25:54 +01003303 case CAR: // one extra line
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003304 case NL:
3305 case 'j':
3306 case K_DOWN:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003307 toscroll = 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003308 break;
3309
Bram Moolenaar85a20022019-12-21 18:25:54 +01003310 case 'u': // Up half a page
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003311 toscroll = -(Rows / 2);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003312 break;
3313
Bram Moolenaar85a20022019-12-21 18:25:54 +01003314 case 'd': // Down half a page
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003315 toscroll = Rows / 2;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003316 break;
3317
Bram Moolenaar85a20022019-12-21 18:25:54 +01003318 case 'b': // one page back
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00003319 case K_PAGEUP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003320 toscroll = -(Rows - 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003321 break;
3322
Bram Moolenaar85a20022019-12-21 18:25:54 +01003323 case ' ': // one extra page
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00003324 case 'f':
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003325 case K_PAGEDOWN:
3326 case K_LEFTMOUSE:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003327 toscroll = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003328 break;
3329
Bram Moolenaar85a20022019-12-21 18:25:54 +01003330 case 'g': // all the way back to the start
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003331 toscroll = -999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003332 break;
3333
Bram Moolenaar85a20022019-12-21 18:25:54 +01003334 case 'G': // all the way to the end
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003335 toscroll = 999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003336 lines_left = 999999;
3337 break;
3338
Bram Moolenaar85a20022019-12-21 18:25:54 +01003339 case ':': // start new command line
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003340#ifdef FEAT_CON_DIALOG
3341 if (!confirm_msg_used)
3342#endif
3343 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003344 // Since got_int is set all typeahead will be flushed, but we
3345 // want to keep this ':', remember that in a special way.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003346 typeahead_noflush(':');
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02003347#ifdef FEAT_TERMINAL
3348 skip_term_loop = TRUE;
3349#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01003350 cmdline_row = Rows - 1; // put ':' on this line
3351 skip_redraw = TRUE; // skip redraw once
3352 need_wait_return = FALSE; // don't wait in main()
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003353 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003354 // FALLTHROUGH
3355 case 'q': // quit
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003356 case Ctrl_C:
3357 case ESC:
3358#ifdef FEAT_CON_DIALOG
3359 if (confirm_msg_used)
3360 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003361 // Jump to the choices of the dialog.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003362 retval = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003363 }
3364 else
3365#endif
3366 {
3367 got_int = TRUE;
3368 quit_more = TRUE;
3369 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003370 // When there is some more output (wrapping line) display that
3371 // without another prompt.
Bram Moolenaar51306d22009-02-24 03:38:04 +00003372 lines_left = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003373 break;
3374
3375#ifdef FEAT_CLIPBOARD
3376 case Ctrl_Y:
Bram Moolenaar85a20022019-12-21 18:25:54 +01003377 // Strange way to allow copying (yanking) a modeless
3378 // selection at the more prompt. Use CTRL-Y,
3379 // because the same is used in Cmdline-mode and at the
3380 // hit-enter prompt. However, scrolling one line up
3381 // might be expected...
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003382 if (clip_star.state == SELECT_DONE)
3383 clip_copy_modeless_selection(TRUE);
3384 continue;
3385#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01003386 default: // no valid response
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003387 msg_moremsg(TRUE);
3388 continue;
3389 }
3390
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003391 if (toscroll != 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003392 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003393 if (toscroll < 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003394 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003395 // go to start of last line
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003396 if (mp_last == NULL)
3397 mp = msg_sb_start(last_msgchunk);
3398 else if (mp_last->sb_prev != NULL)
3399 mp = msg_sb_start(mp_last->sb_prev);
3400 else
3401 mp = NULL;
3402
Bram Moolenaar85a20022019-12-21 18:25:54 +01003403 // go to start of line at top of the screen
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003404 for (i = 0; i < Rows - 2 && mp != NULL && mp->sb_prev != NULL;
3405 ++i)
3406 mp = msg_sb_start(mp->sb_prev);
3407
3408 if (mp != NULL && mp->sb_prev != NULL)
3409 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003410 // Find line to be displayed at top.
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003411 for (i = 0; i > toscroll; --i)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003412 {
3413 if (mp == NULL || mp->sb_prev == NULL)
3414 break;
3415 mp = msg_sb_start(mp->sb_prev);
3416 if (mp_last == NULL)
3417 mp_last = msg_sb_start(last_msgchunk);
3418 else
3419 mp_last = msg_sb_start(mp_last->sb_prev);
3420 }
3421
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003422 if (toscroll == -1 && screen_ins_lines(0, 0, 1,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003423 (int)Rows, 0, NULL) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003424 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003425 // display line at top
Bram Moolenaar838b7462022-09-26 15:19:56 +01003426 (void)disp_sb_line(0, mp, FALSE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003427 }
3428 else
3429 {
Bram Moolenaar838b7462022-09-26 15:19:56 +01003430 int did_clear = screenclear();
3431
Bram Moolenaar85a20022019-12-21 18:25:54 +01003432 // redisplay all lines
Bram Moolenaar773560b2006-05-06 21:38:18 +00003433 for (i = 0; mp != NULL && i < Rows - 1; ++i)
3434 {
Bram Moolenaar838b7462022-09-26 15:19:56 +01003435 mp = disp_sb_line(i, mp, !did_clear);
Bram Moolenaar773560b2006-05-06 21:38:18 +00003436 ++msg_scrolled;
3437 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003438 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003439 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003440 }
3441 }
3442 else
3443 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003444 // First display any text that we scrolled back.
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003445 while (toscroll > 0 && mp_last != NULL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003446 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003447 // scroll up, display line at bottom
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003448 msg_scroll_up();
Bram Moolenaarbb15b652005-10-03 21:52:09 +00003449 inc_msg_scrolled();
Shougo Matsushitabe2b03c2024-04-08 22:11:50 +02003450 screen_fill((int)Rows - 2, (int)Rows - 1, 0, (int)Columns,
3451 ' ', ' ', msg_attr);
Bram Moolenaar838b7462022-09-26 15:19:56 +01003452 mp_last = disp_sb_line((int)Rows - 2, mp_last, FALSE);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003453 --toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003454 }
3455 }
3456
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003457 if (toscroll <= 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003458 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003459 // displayed the requested text, more prompt again
Shougo Matsushitabe2b03c2024-04-08 22:11:50 +02003460 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns,
3461 ' ', ' ', msg_attr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003462 msg_moremsg(FALSE);
3463 continue;
3464 }
3465
Bram Moolenaar85a20022019-12-21 18:25:54 +01003466 // display more text, return to caller
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003467 lines_left = toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003468 }
3469
3470 break;
3471 }
3472
Bram Moolenaar85a20022019-12-21 18:25:54 +01003473 // clear the --more-- message
Shougo Matsushitabe2b03c2024-04-08 22:11:50 +02003474 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', msg_attr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003475 State = oldState;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003476 setmouse();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003477 if (quit_more)
3478 {
3479 msg_row = Rows - 1;
3480 msg_col = 0;
3481 }
3482#ifdef FEAT_RIGHTLEFT
3483 else if (cmdmsg_rl)
3484 msg_col = Columns - 1;
3485#endif
3486
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01003487 entered = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003488#ifdef FEAT_CON_DIALOG
3489 return retval;
3490#else
3491 return FALSE;
3492#endif
3493}
3494
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495#if defined(USE_MCH_ERRMSG) || defined(PROTO)
3496
3497#ifdef mch_errmsg
3498# undef mch_errmsg
3499#endif
3500#ifdef mch_msg
3501# undef mch_msg
3502#endif
3503
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003504#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3505 static void
3506mch_errmsg_c(char *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507{
Bram Moolenaar0f77d6a2019-02-14 20:55:09 +01003508 int len = (int)STRLEN(str);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003509 DWORD nwrite = 0;
3510 DWORD mode = 0;
3511 HANDLE h = GetStdHandle(STD_ERROR_HANDLE);
3512
3513 if (GetConsoleMode(h, &mode) && enc_codepage >= 0
3514 && (int)GetConsoleCP() != enc_codepage)
3515 {
3516 WCHAR *w = enc_to_utf16((char_u *)str, &len);
3517
3518 WriteConsoleW(h, w, len, &nwrite, NULL);
3519 vim_free(w);
3520 }
3521 else
3522 {
3523 fprintf(stderr, "%s", str);
3524 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003525}
3526#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003528/*
3529 * Give an error message. To be used when the screen hasn't been initialized
3530 * yet. When stderr can't be used, collect error messages until the GUI has
3531 * started and they can be displayed in a message box.
3532 */
3533 void
3534mch_errmsg(char *str)
3535{
3536#if !defined(MSWIN) || defined(FEAT_GUI_MSWIN)
3537 int len;
3538#endif
3539
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02003540#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003541 // On Unix use stderr if it's a tty.
3542 // When not going to start the GUI also use stderr.
3543 // On Mac, when started from Finder, stderr is the console.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 if (
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003545# ifdef UNIX
3546# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003548# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 isatty(2)
3550# endif
3551# ifdef FEAT_GUI
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003552 ||
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003553# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003554# endif
3555# ifdef FEAT_GUI
3556 !(gui.in_use || gui.starting)
3557# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 )
3559 {
3560 fprintf(stderr, "%s", str);
3561 return;
3562 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003563#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003565#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3566# ifdef VIMDLL
3567 if (!(gui.in_use || gui.starting))
3568# endif
3569 {
3570 mch_errmsg_c(str);
3571 return;
3572 }
3573#endif
3574
3575#if !defined(MSWIN) || defined(FEAT_GUI_MSWIN)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003576 // avoid a delay for a message that isn't there
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 emsg_on_display = FALSE;
3578
3579 len = (int)STRLEN(str) + 1;
3580 if (error_ga.ga_growsize == 0)
3581 {
3582 error_ga.ga_growsize = 80;
3583 error_ga.ga_itemsize = 1;
3584 }
3585 if (ga_grow(&error_ga, len) == OK)
3586 {
3587 mch_memmove((char_u *)error_ga.ga_data + error_ga.ga_len,
3588 (char_u *)str, len);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003589# ifdef UNIX
Bram Moolenaar85a20022019-12-21 18:25:54 +01003590 // remove CR characters, they are displayed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 {
3592 char_u *p;
3593
3594 p = (char_u *)error_ga.ga_data + error_ga.ga_len;
3595 for (;;)
3596 {
3597 p = vim_strchr(p, '\r');
3598 if (p == NULL)
3599 break;
3600 *p = ' ';
3601 }
3602 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003603# endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01003604 --len; // don't count the NUL at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 error_ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003607#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608}
3609
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003610#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3611 static void
3612mch_msg_c(char *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613{
Bram Moolenaar0f77d6a2019-02-14 20:55:09 +01003614 int len = (int)STRLEN(str);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003615 DWORD nwrite = 0;
3616 DWORD mode;
3617 HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
3618
3619
3620 if (GetConsoleMode(h, &mode) && enc_codepage >= 0
3621 && (int)GetConsoleCP() != enc_codepage)
3622 {
3623 WCHAR *w = enc_to_utf16((char_u *)str, &len);
3624
3625 WriteConsoleW(h, w, len, &nwrite, NULL);
3626 vim_free(w);
3627 }
3628 else
3629 {
3630 printf("%s", str);
3631 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003632}
3633#endif
3634
3635/*
3636 * Give a message. To be used when the screen hasn't been initialized yet.
3637 * When there is no tty, collect messages until the GUI has started and they
3638 * can be displayed in a message box.
3639 */
3640 void
3641mch_msg(char *str)
3642{
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02003643#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003644 // On Unix use stdout if we have a tty. This allows "vim -h | more" and
3645 // uses mch_errmsg() when started from the desktop.
3646 // When not going to start the GUI also use stdout.
3647 // On Mac, when started from Finder, stderr is the console.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 if (
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003649# ifdef UNIX
3650# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003652# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 isatty(2)
Bram Moolenaareae1b912019-05-09 15:12:55 +02003654# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655# ifdef FEAT_GUI
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003656 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003658# endif
3659# ifdef FEAT_GUI
3660 !(gui.in_use || gui.starting)
3661# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 )
3663 {
3664 printf("%s", str);
3665 return;
3666 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003667#endif
3668
3669#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3670# ifdef VIMDLL
3671 if (!(gui.in_use || gui.starting))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003673 {
3674 mch_msg_c(str);
3675 return;
3676 }
3677#endif
3678#if !defined(MSWIN) || defined(FEAT_GUI_MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 mch_errmsg(str);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003680#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681}
Bram Moolenaar85a20022019-12-21 18:25:54 +01003682#endif // USE_MCH_ERRMSG
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683
3684/*
3685 * Put a character on the screen at the current message position and advance
3686 * to the next position. Only for printable ASCII!
3687 */
3688 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003689msg_screen_putchar(int c, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690{
Bram Moolenaar85a20022019-12-21 18:25:54 +01003691 msg_didout = TRUE; // remember that line is not empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 screen_putchar(c, msg_row, msg_col, attr);
3693#ifdef FEAT_RIGHTLEFT
3694 if (cmdmsg_rl)
3695 {
3696 if (--msg_col == 0)
3697 {
3698 msg_col = Columns;
3699 ++msg_row;
3700 }
3701 }
3702 else
3703#endif
3704 {
3705 if (++msg_col >= Columns)
3706 {
3707 msg_col = 0;
3708 ++msg_row;
3709 }
3710 }
3711}
3712
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003713 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003714msg_moremsg(int full)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003716 int attr;
3717 char_u *s = (char_u *)_("-- More --");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718
Bram Moolenaar8820b482017-03-16 17:23:31 +01003719 attr = HL_ATTR(HLF_M);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003720 screen_puts(s, (int)Rows - 1, 0, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 if (full)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003722 screen_puts((char_u *)
3723 _(" SPACE/d/j: screen/page/line down, b/u/k: up, q: quit "),
3724 (int)Rows - 1, vim_strsize(s), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725}
3726
3727/*
Bram Moolenaar24959102022-05-07 20:01:16 +01003728 * Repeat the message for the current mode: MODE_ASKMORE, MODE_EXTERNCMD,
3729 * MODE_CONFIRM or exmode_active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 */
3731 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003732repeat_message(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733{
Bram Moolenaar24959102022-05-07 20:01:16 +01003734 if (State == MODE_ASKMORE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003736 msg_moremsg(TRUE); // display --more-- message again
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 msg_row = Rows - 1;
3738 }
3739#ifdef FEAT_CON_DIALOG
Bram Moolenaar24959102022-05-07 20:01:16 +01003740 else if (State == MODE_CONFIRM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003742 display_confirm_msg(); // display ":confirm" message again
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 msg_row = Rows - 1;
3744 }
3745#endif
Bram Moolenaar24959102022-05-07 20:01:16 +01003746 else if (State == MODE_EXTERNCMD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003748 windgoto(msg_row, msg_col); // put cursor back
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 }
Bram Moolenaar24959102022-05-07 20:01:16 +01003750 else if (State == MODE_HITRETURN || State == MODE_SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 {
Bram Moolenaar9f108752007-11-24 14:44:58 +00003752 if (msg_row == Rows - 1)
3753 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003754 // Avoid drawing the "hit-enter" prompt below the previous one,
3755 // overwrite it. Esp. useful when regaining focus and a
3756 // FocusGained autocmd exists but didn't draw anything.
Bram Moolenaar9f108752007-11-24 14:44:58 +00003757 msg_didout = FALSE;
3758 msg_col = 0;
3759 msg_clr_eos();
3760 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 hit_return_msg();
3762 msg_row = Rows - 1;
3763 }
3764}
3765
3766/*
3767 * msg_check_screen - check if the screen is initialized.
3768 * Also check msg_row and msg_col, if they are too big it may cause a crash.
3769 * While starting the GUI the terminal codes will be set for the GUI, but the
3770 * output goes to the terminal. Don't use the terminal codes then.
3771 */
3772 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003773msg_check_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774{
3775 if (!full_screen || !screen_valid(FALSE))
3776 return FALSE;
3777
3778 if (msg_row >= Rows)
3779 msg_row = Rows - 1;
3780 if (msg_col >= Columns)
3781 msg_col = Columns - 1;
3782 return TRUE;
3783}
3784
3785/*
3786 * Clear from current message position to end of screen.
3787 * Skip this when ":silent" was used, no need to clear for redirection.
3788 */
3789 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003790msg_clr_eos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791{
3792 if (msg_silent == 0)
3793 msg_clr_eos_force();
3794}
3795
3796/*
3797 * Clear from current message position to end of screen.
3798 * Note: msg_col is not updated, so we remember the end of the message
3799 * for msg_check().
3800 */
3801 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003802msg_clr_eos_force(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803{
Bram Moolenaar3b474dc2022-09-01 17:01:32 +01003804#ifdef HAS_MESSAGE_WINDOW
3805 if (in_echowindow)
3806 return; // messages go into a popup
3807#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 if (msg_use_printf())
3809 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003810 if (full_screen) // only when termcap codes are valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 {
3812 if (*T_CD)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003813 out_str(T_CD); // clear to end of display
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 else if (*T_CE)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003815 out_str(T_CE); // clear to end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816 }
3817 }
Bram Moolenaara2a89732022-08-31 14:46:18 +01003818 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 {
Shougo Matsushitabe2b03c2024-04-08 22:11:50 +02003820 int msg_attr;
3821
3822 msg_attr = HL_ATTR(HLF_MSG);
3823
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824#ifdef FEAT_RIGHTLEFT
3825 if (cmdmsg_rl)
3826 {
Shougo Matsushitabe2b03c2024-04-08 22:11:50 +02003827 screen_fill(msg_row, msg_row + 1, 0, msg_col + 1,
3828 ' ', ' ', msg_attr);
3829 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns,
3830 ' ', ' ', msg_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 }
3832 else
3833#endif
3834 {
3835 screen_fill(msg_row, msg_row + 1, msg_col, (int)Columns,
Shougo Matsushitabe2b03c2024-04-08 22:11:50 +02003836 ' ', ' ', msg_attr);
3837 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns,
3838 ' ', ' ', msg_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 }
3840 }
3841}
3842
3843/*
3844 * Clear the command line.
3845 */
3846 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003847msg_clr_cmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848{
3849 msg_row = cmdline_row;
3850 msg_col = 0;
3851 msg_clr_eos_force();
3852}
3853
3854/*
3855 * end putting a message on the screen
Bram Moolenaar13608d82022-08-29 15:06:50 +01003856 * call wait_return() if the message does not fit in the available space
3857 * return TRUE if wait_return() not called.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 */
3859 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003860msg_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861{
3862 /*
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02003863 * If the string is larger than the window,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 * or the ruler option is set and we run into it,
3865 * we have to redraw the window.
3866 * Do not do this if we are abandoning the file or editing the command line.
3867 */
Bram Moolenaar24959102022-05-07 20:01:16 +01003868 if (!exiting && need_wait_return && !(State & MODE_CMDLINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 {
3870 wait_return(FALSE);
3871 return FALSE;
3872 }
3873 out_flush();
3874 return TRUE;
3875}
3876
3877/*
3878 * If the written message runs into the shown command or ruler, we have to
3879 * wait for hit-return and redraw the window later.
3880 */
3881 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003882msg_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883{
Bram Moolenaar35a4fbc2022-08-28 14:39:53 +01003884 if (msg_row == Rows - 1 && msg_col >= sc_col
3885#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaara2a89732022-08-31 14:46:18 +01003886 && !in_echowindow
Bram Moolenaar35a4fbc2022-08-28 14:39:53 +01003887#endif
3888 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 {
3890 need_wait_return = TRUE;
3891 redraw_cmdline = TRUE;
3892 }
3893}
3894
3895/*
3896 * May write a string to the redirection file.
3897 * When "maxlen" is -1 write the whole string, otherwise up to "maxlen" bytes.
3898 */
3899 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003900redir_write(char_u *str, int maxlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901{
3902 char_u *s = str;
3903 static int cur_col = 0;
3904
Bram Moolenaar85a20022019-12-21 18:25:54 +01003905 // Don't do anything for displaying prompts and the like.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003906 if (redir_off)
3907 return;
3908
Bram Moolenaar85a20022019-12-21 18:25:54 +01003909 // If 'verbosefile' is set prepare for writing in that file.
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003910 if (*p_vfile != NUL && verbose_fd == NULL)
3911 verbose_open();
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003912
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003913 if (redirecting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003915 // If the string doesn't start with CR or NL, go to msg_col
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 if (*s != '\n' && *s != '\r')
3917 {
3918 while (cur_col < msg_col)
3919 {
3920#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003921 if (redir_execute)
3922 execute_redir_str((char_u *)" ", -1);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003923 else if (redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 write_reg_contents(redir_reg, (char_u *)" ", -1, TRUE);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003925 else if (redir_vname)
3926 var_redir_str((char_u *)" ", -1);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003927 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003929 if (redir_fd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 fputs(" ", redir_fd);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003931 if (verbose_fd != NULL)
3932 fputs(" ", verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 ++cur_col;
3934 }
3935 }
3936
3937#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003938 if (redir_execute)
3939 execute_redir_str(s, maxlen);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003940 else if (redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 write_reg_contents(redir_reg, s, maxlen, TRUE);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003942 else if (redir_vname)
Bram Moolenaardf177f62005-02-22 08:39:57 +00003943 var_redir_str(s, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944#endif
3945
Bram Moolenaar85a20022019-12-21 18:25:54 +01003946 // Write and adjust the current column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
3948 {
3949#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003950 if (!redir_reg && !redir_vname && !redir_execute)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003952 if (redir_fd != NULL)
3953 putc(*s, redir_fd);
3954 if (verbose_fd != NULL)
3955 putc(*s, verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 if (*s == '\r' || *s == '\n')
3957 cur_col = 0;
3958 else if (*s == '\t')
3959 cur_col += (8 - cur_col % 8);
3960 else
3961 ++cur_col;
3962 ++s;
3963 }
3964
Bram Moolenaar85a20022019-12-21 18:25:54 +01003965 if (msg_silent != 0) // should update msg_col
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 msg_col = cur_col;
3967 }
3968}
3969
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003970 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003971redirecting(void)
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003972{
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003973 return redir_fd != NULL || *p_vfile != NUL
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003974#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003975 || redir_reg || redir_vname || redir_execute
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003976#endif
3977 ;
3978}
3979
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980/*
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00003981 * Before giving verbose message.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003982 * Must always be called paired with verbose_leave()!
3983 */
3984 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003985verbose_enter(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003986{
3987 if (*p_vfile != NUL)
3988 ++msg_silent;
3989}
3990
3991/*
3992 * After giving verbose message.
3993 * Must always be called paired with verbose_enter()!
3994 */
3995 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003996verbose_leave(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003997{
3998 if (*p_vfile != NUL)
3999 if (--msg_silent < 0)
4000 msg_silent = 0;
4001}
4002
4003/*
4004 * Like verbose_enter() and set msg_scroll when displaying the message.
4005 */
4006 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004007verbose_enter_scroll(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00004008{
4009 if (*p_vfile != NUL)
4010 ++msg_silent;
4011 else
Bram Moolenaar85a20022019-12-21 18:25:54 +01004012 // always scroll up, don't overwrite
Bram Moolenaar8b044b32005-05-31 22:05:58 +00004013 msg_scroll = TRUE;
4014}
4015
4016/*
4017 * Like verbose_leave() and set cmdline_row when displaying the message.
4018 */
4019 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004020verbose_leave_scroll(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00004021{
4022 if (*p_vfile != NUL)
4023 {
4024 if (--msg_silent < 0)
4025 msg_silent = 0;
4026 }
4027 else
4028 cmdline_row = msg_row;
4029}
4030
Bram Moolenaar8b044b32005-05-31 22:05:58 +00004031/*
4032 * Called when 'verbosefile' is set: stop writing to the file.
4033 */
4034 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004035verbose_stop(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00004036{
4037 if (verbose_fd != NULL)
4038 {
4039 fclose(verbose_fd);
4040 verbose_fd = NULL;
4041 }
4042 verbose_did_open = FALSE;
4043}
4044
4045/*
4046 * Open the file 'verbosefile'.
4047 * Return FAIL or OK.
4048 */
4049 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004050verbose_open(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00004051{
4052 if (verbose_fd == NULL && !verbose_did_open)
4053 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004054 // Only give the error message once.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00004055 verbose_did_open = TRUE;
4056
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004057 verbose_fd = mch_fopen((char *)p_vfile, "a");
Bram Moolenaar8b044b32005-05-31 22:05:58 +00004058 if (verbose_fd == NULL)
4059 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004060 semsg(_(e_cant_open_file_str), p_vfile);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00004061 return FAIL;
4062 }
4063 }
4064 return OK;
4065}
4066
4067/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 * Give a warning message (for searching).
4069 * Use 'w' highlighting and may repeat the message after redrawing
4070 */
4071 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004072give_warning(char_u *message, int hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073{
Bram Moolenaarcc7eb2a2021-07-11 19:12:04 +02004074 give_warning_with_source(message, hl, FALSE);
4075}
4076
4077 void
4078give_warning_with_source(char_u *message, int hl, int with_source)
4079{
Bram Moolenaar85a20022019-12-21 18:25:54 +01004080 // Don't do this for ":silent".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 if (msg_silent != 0)
4082 return;
4083
Bram Moolenaar85a20022019-12-21 18:25:54 +01004084 // Don't want a hit-enter prompt here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 ++no_wait_return;
Bram Moolenaared203462004-06-16 11:19:22 +00004086
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087#ifdef FEAT_EVAL
4088 set_vim_var_string(VV_WARNINGMSG, message, -1);
4089#endif
Bram Moolenaard23a8232018-02-10 18:45:26 +01004090 VIM_CLEAR(keep_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 if (hl)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004092 keep_msg_attr = HL_ATTR(HLF_W);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 else
4094 keep_msg_attr = 0;
Bram Moolenaarcc7eb2a2021-07-11 19:12:04 +02004095
4096 if (with_source)
4097 {
4098 // Do what msg() does, but with a column offset if the warning should
4099 // be after the mode message.
4100 msg_start();
4101 msg_source(HL_ATTR(HLF_W));
4102 msg_puts(" ");
4103 msg_puts_attr((char *)message, HL_ATTR(HLF_W) | MSG_HIST);
4104 msg_clr_eos();
4105 (void)msg_end();
4106 }
4107 else if (msg_attr((char *)message, keep_msg_attr) && msg_scrolled == 0)
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004108 set_keep_msg(message, keep_msg_attr);
Bram Moolenaarcc7eb2a2021-07-11 19:12:04 +02004109
Bram Moolenaar85a20022019-12-21 18:25:54 +01004110 msg_didout = FALSE; // overwrite this message
4111 msg_nowait = TRUE; // don't wait for this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 msg_col = 0;
Bram Moolenaared203462004-06-16 11:19:22 +00004113
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 --no_wait_return;
4115}
4116
Bram Moolenaar113e1072019-01-20 15:30:40 +01004117#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarf8be4612017-06-23 20:52:40 +02004118 void
4119give_warning2(char_u *message, char_u *a1, int hl)
4120{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +02004121 if (IObuff == NULL)
4122 {
4123 // Very early in initialisation and already something wrong, just give
4124 // the raw message so the user at least gets a hint.
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00004125 give_warning(message, hl);
Bram Moolenaare3a22cb2019-10-14 22:01:57 +02004126 }
4127 else
4128 {
4129 vim_snprintf((char *)IObuff, IOSIZE, (char *)message, a1);
4130 give_warning(IObuff, hl);
4131 }
Bram Moolenaarf8be4612017-06-23 20:52:40 +02004132}
Bram Moolenaar113e1072019-01-20 15:30:40 +01004133#endif
Bram Moolenaarf8be4612017-06-23 20:52:40 +02004134
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135/*
4136 * Advance msg cursor to column "col".
4137 */
4138 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004139msg_advance(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140{
Bram Moolenaar85a20022019-12-21 18:25:54 +01004141 if (msg_silent != 0) // nothing to advance to
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004143 msg_col = col; // for redirection, may fill it up later
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 return;
4145 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01004146 if (col >= Columns) // not enough room
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 col = Columns - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004148#ifdef FEAT_RIGHTLEFT
4149 if (cmdmsg_rl)
4150 while (msg_col > Columns - col)
4151 msg_putchar(' ');
4152 else
4153#endif
4154 while (msg_col < col)
4155 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156}
4157
Christian Brabandt45e07042024-11-11 20:52:55 +01004158/*
4159 * Warn about missing Clipboard Support
4160 */
4161 void
4162msg_warn_missing_clipboard(void)
4163{
4164 if (!global_busy && !did_warn_clipboard)
4165 {
Christian Brabandt8b94afc2024-11-17 16:02:41 +01004166#ifdef FEAT_CLIPBOARD
4167 msg(_("W23: Clipboard register not available, using register 0"));
4168#else
4169 msg(_("W24: Clipboard register not available. See :h W24"));
4170#endif
Christian Brabandt45e07042024-11-11 20:52:55 +01004171 did_warn_clipboard = TRUE;
4172 }
4173}
4174
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175#if defined(FEAT_CON_DIALOG) || defined(PROTO)
4176/*
4177 * Used for "confirm()" function, and the :confirm command prefix.
4178 * Versions which haven't got flexible dialogs yet, and console
4179 * versions, get this generic handler which uses the command line.
4180 *
4181 * type = one of:
4182 * VIM_QUESTION, VIM_INFO, VIM_WARNING, VIM_ERROR or VIM_GENERIC
4183 * title = title string (can be NULL for default)
4184 * (neither used in console dialogs at the moment)
4185 *
4186 * Format of the "buttons" string:
4187 * "Button1Name\nButton2Name\nButton3Name"
4188 * The first button should normally be the default/accept
4189 * The second button should be the 'Cancel' button
4190 * Other buttons- use your imagination!
4191 * A '&' in a button name becomes a shortcut, so each '&' should be before a
4192 * different letter.
Rob Pilling8196e942022-02-11 15:12:10 +00004193 *
4194 * Returns 0 if cancelled, otherwise the nth button (1-indexed).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004197do_dialog(
4198 int type UNUSED,
4199 char_u *title UNUSED,
4200 char_u *message,
4201 char_u *buttons,
4202 int dfltbutton,
Bram Moolenaar85a20022019-12-21 18:25:54 +01004203 char_u *textfield UNUSED, // IObuff for inputdialog(), NULL
4204 // otherwise
4205 int ex_cmd) // when TRUE pressing : accepts default and starts
4206 // Ex command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207{
4208 int oldState;
4209 int retval = 0;
4210 char_u *hotkeys;
4211 int c;
4212 int i;
Bram Moolenaar27321db2020-07-06 21:24:57 +02004213 tmode_T save_tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214
4215#ifndef NO_CONSOLE
Bram Moolenaar85a20022019-12-21 18:25:54 +01004216 // Don't output anything in silent mode ("ex -s")
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 if (silent_mode)
Bram Moolenaar85a20022019-12-21 18:25:54 +01004218 return dfltbutton; // return default option
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219#endif
4220
4221#ifdef FEAT_GUI_DIALOG
Bram Moolenaar85a20022019-12-21 18:25:54 +01004222 // When GUI is running and 'c' not in 'guioptions', use the GUI dialog
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
4224 {
Bram Moolenaar2d12c252022-06-13 21:42:45 +01004225 // --gui-dialog-file: write text to a file
4226 if (gui_dialog_log(title, message))
4227 c = dfltbutton;
4228 else
4229 c = gui_mch_dialog(type, title, message, buttons, dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004230 textfield, ex_cmd);
Bram Moolenaar85a20022019-12-21 18:25:54 +01004231 // avoid a hit-enter prompt without clearing the cmdline
Bram Moolenaar8de49e12009-02-11 17:47:54 +00004232 need_wait_return = FALSE;
4233 emsg_on_display = FALSE;
4234 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235
Bram Moolenaar85a20022019-12-21 18:25:54 +01004236 // Flush output to avoid that further messages and redrawing is done
4237 // in the wrong order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 out_flush();
4239 gui_mch_update();
4240
4241 return c;
4242 }
4243#endif
4244
4245 oldState = State;
Bram Moolenaar24959102022-05-07 20:01:16 +01004246 State = MODE_CONFIRM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248
Bram Moolenaar27321db2020-07-06 21:24:57 +02004249 // Ensure raw mode here.
4250 save_tmode = cur_tmode;
4251 settmode(TMODE_RAW);
4252
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 /*
4254 * Since we wait for a keypress, don't make the
4255 * user press RETURN as well afterwards.
4256 */
4257 ++no_wait_return;
4258 hotkeys = msg_show_console_dialog(message, buttons, dfltbutton);
4259
4260 if (hotkeys != NULL)
4261 {
4262 for (;;)
4263 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004264 // Get a typed character directly from the user.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 c = get_keystroke();
4266 switch (c)
4267 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004268 case CAR: // User accepts default option
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 case NL:
4270 retval = dfltbutton;
4271 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01004272 case Ctrl_C: // User aborts/cancels
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 case ESC:
4274 retval = 0;
4275 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01004276 default: // Could be a hotkey?
4277 if (c < 0) // special keys are ignored here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 continue;
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004279 if (c == ':' && ex_cmd)
4280 {
4281 retval = dfltbutton;
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02004282 ins_char_typebuf(':', 0);
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004283 break;
4284 }
4285
Bram Moolenaar85a20022019-12-21 18:25:54 +01004286 // Make the character lowercase, as chars in "hotkeys" are.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 c = MB_TOLOWER(c);
4288 retval = 1;
4289 for (i = 0; hotkeys[i]; ++i)
4290 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 if (has_mbyte)
4292 {
4293 if ((*mb_ptr2char)(hotkeys + i) == c)
4294 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004295 i += (*mb_ptr2len)(hotkeys + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 }
4297 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 if (hotkeys[i] == c)
4299 break;
4300 ++retval;
4301 }
4302 if (hotkeys[i])
4303 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01004304 // No hotkey match, so keep waiting
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 continue;
4306 }
4307 break;
4308 }
4309
4310 vim_free(hotkeys);
4311 }
4312
Bram Moolenaar27321db2020-07-06 21:24:57 +02004313 settmode(save_tmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 State = oldState;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 --no_wait_return;
4317 msg_end_prompt();
4318
4319 return retval;
4320}
4321
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322/*
4323 * Copy one character from "*from" to "*to", taking care of multi-byte
4324 * characters. Return the length of the character in bytes.
4325 */
4326 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004327copy_char(
4328 char_u *from,
4329 char_u *to,
Bram Moolenaar85a20022019-12-21 18:25:54 +01004330 int lowercase) // make character lower case
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332 int len;
4333 int c;
4334
4335 if (has_mbyte)
4336 {
4337 if (lowercase)
4338 {
4339 c = MB_TOLOWER((*mb_ptr2char)(from));
4340 return (*mb_char2bytes)(c, to);
4341 }
4342 else
4343 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004344 len = (*mb_ptr2len)(from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345 mch_memmove(to, from, (size_t)len);
4346 return len;
4347 }
4348 }
4349 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 {
4351 if (lowercase)
4352 *to = (char_u)TOLOWER_LOC(*from);
4353 else
4354 *to = *from;
4355 return 1;
4356 }
4357}
4358
4359/*
4360 * Format the dialog string, and display it at the bottom of
4361 * the screen. Return a string of hotkey chars (if defined) for
4362 * each 'button'. If a button has no hotkey defined, the first character of
4363 * the button is used.
4364 * The hotkeys can be multi-byte characters, but without combining chars.
4365 *
4366 * Returns an allocated string with hotkeys, or NULL for error.
4367 */
4368 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004369msg_show_console_dialog(
4370 char_u *message,
4371 char_u *buttons,
4372 int dfltbutton)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373{
4374 int len = 0;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01004375#define HOTK_LEN (has_mbyte ? MB_MAXBYTES : 1)
Bram Moolenaar85a20022019-12-21 18:25:54 +01004376 int lenhotkey = HOTK_LEN; // count first button
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 char_u *hotk = NULL;
4378 char_u *msgp = NULL;
4379 char_u *hotkp = NULL;
4380 char_u *r;
4381 int copy;
4382#define HAS_HOTKEY_LEN 30
4383 char_u has_hotkey[HAS_HOTKEY_LEN];
Bram Moolenaar85a20022019-12-21 18:25:54 +01004384 int first_hotkey = FALSE; // first char of button is hotkey
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 int idx;
4386
4387 has_hotkey[0] = FALSE;
4388
4389 /*
4390 * First loop: compute the size of memory to allocate.
4391 * Second loop: copy to the allocated memory.
4392 */
4393 for (copy = 0; copy <= 1; ++copy)
4394 {
4395 r = buttons;
4396 idx = 0;
4397 while (*r)
4398 {
4399 if (*r == DLG_BUTTON_SEP)
4400 {
4401 if (copy)
4402 {
4403 *msgp++ = ',';
Bram Moolenaar85a20022019-12-21 18:25:54 +01004404 *msgp++ = ' '; // '\n' -> ', '
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405
Bram Moolenaar85a20022019-12-21 18:25:54 +01004406 // advance to next hotkey and set default hotkey
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 if (has_mbyte)
Bram Moolenaar6a516062007-07-05 08:11:42 +00004408 hotkp += STRLEN(hotkp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 ++hotkp;
Bram Moolenaar6a516062007-07-05 08:11:42 +00004411 hotkp[copy_char(r + 1, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412 if (dfltbutton)
4413 --dfltbutton;
4414
Bram Moolenaar85a20022019-12-21 18:25:54 +01004415 // If no hotkey is specified first char is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416 if (idx < HAS_HOTKEY_LEN - 1 && !has_hotkey[++idx])
4417 first_hotkey = TRUE;
4418 }
4419 else
4420 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004421 len += 3; // '\n' -> ', '; 'x' -> '(x)'
4422 lenhotkey += HOTK_LEN; // each button needs a hotkey
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 if (idx < HAS_HOTKEY_LEN - 1)
4424 has_hotkey[++idx] = FALSE;
4425 }
4426 }
4427 else if (*r == DLG_HOTKEY_CHAR || first_hotkey)
4428 {
4429 if (*r == DLG_HOTKEY_CHAR)
4430 ++r;
4431 first_hotkey = FALSE;
4432 if (copy)
4433 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004434 if (*r == DLG_HOTKEY_CHAR) // '&&a' -> '&a'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435 *msgp++ = *r;
4436 else
4437 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004438 // '&a' -> '[a]'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 *msgp++ = (dfltbutton == 1) ? '[' : '(';
4440 msgp += copy_char(r, msgp, FALSE);
4441 *msgp++ = (dfltbutton == 1) ? ']' : ')';
4442
Bram Moolenaar85a20022019-12-21 18:25:54 +01004443 // redefine hotkey
Bram Moolenaar6a516062007-07-05 08:11:42 +00004444 hotkp[copy_char(r, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 }
4446 }
4447 else
4448 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004449 ++len; // '&a' -> '[a]'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450 if (idx < HAS_HOTKEY_LEN - 1)
4451 has_hotkey[idx] = TRUE;
4452 }
4453 }
4454 else
4455 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004456 // everything else copy literally
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 if (copy)
4458 msgp += copy_char(r, msgp, FALSE);
4459 }
4460
Bram Moolenaar85a20022019-12-21 18:25:54 +01004461 // advance to the next character
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004462 MB_PTR_ADV(r);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463 }
4464
4465 if (copy)
4466 {
4467 *msgp++ = ':';
4468 *msgp++ = ' ';
4469 *msgp = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 }
4471 else
4472 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004473 len += (int)(STRLEN(message)
Bram Moolenaar85a20022019-12-21 18:25:54 +01004474 + 2 // for the NL's
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004475 + STRLEN(buttons)
Bram Moolenaar85a20022019-12-21 18:25:54 +01004476 + 3); // for the ": " and NUL
4477 lenhotkey++; // for the NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478
Bram Moolenaar85a20022019-12-21 18:25:54 +01004479 // If no hotkey is specified first char is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 if (!has_hotkey[0])
4481 {
4482 first_hotkey = TRUE;
Bram Moolenaar85a20022019-12-21 18:25:54 +01004483 len += 2; // "x" -> "[x]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 }
4485
4486 /*
4487 * Now allocate and load the strings
4488 */
4489 vim_free(confirm_msg);
4490 confirm_msg = alloc(len);
4491 if (confirm_msg == NULL)
4492 return NULL;
4493 *confirm_msg = NUL;
4494 hotk = alloc(lenhotkey);
4495 if (hotk == NULL)
4496 return NULL;
4497
4498 *confirm_msg = '\n';
4499 STRCPY(confirm_msg + 1, message);
4500
4501 msgp = confirm_msg + 1 + STRLEN(message);
4502 hotkp = hotk;
4503
Bram Moolenaar85a20022019-12-21 18:25:54 +01004504 // Define first default hotkey. Keep the hotkey string NUL
4505 // terminated to avoid reading past the end.
Bram Moolenaar6a516062007-07-05 08:11:42 +00004506 hotkp[copy_char(buttons, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507
Bram Moolenaar85a20022019-12-21 18:25:54 +01004508 // Remember where the choices start, displaying starts here when
4509 // "hotkp" typed at the more prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 confirm_msg_tail = msgp;
4511 *msgp++ = '\n';
4512 }
4513 }
4514
4515 display_confirm_msg();
4516 return hotk;
4517}
4518
4519/*
4520 * Display the ":confirm" message. Also called when screen resized.
4521 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02004522 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004523display_confirm_msg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524{
Bram Moolenaar85a20022019-12-21 18:25:54 +01004525 // avoid that 'q' at the more prompt truncates the message here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 ++confirm_msg_used;
4527 if (confirm_msg != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004528 msg_puts_attr((char *)confirm_msg, HL_ATTR(HLF_M));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 --confirm_msg_used;
4530}
4531
Bram Moolenaar85a20022019-12-21 18:25:54 +01004532#endif // FEAT_CON_DIALOG
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533
4534#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4535
4536 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004537vim_dialog_yesno(
4538 int type,
4539 char_u *title,
4540 char_u *message,
4541 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542{
4543 if (do_dialog(type,
4544 title == NULL ? (char_u *)_("Question") : title,
4545 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004546 (char_u *)_("&Yes\n&No"), dflt, NULL, FALSE) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 return VIM_YES;
4548 return VIM_NO;
4549}
4550
4551 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004552vim_dialog_yesnocancel(
4553 int type,
4554 char_u *title,
4555 char_u *message,
4556 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557{
4558 switch (do_dialog(type,
4559 title == NULL ? (char_u *)_("Question") : title,
4560 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004561 (char_u *)_("&Yes\n&No\n&Cancel"), dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 {
4563 case 1: return VIM_YES;
4564 case 2: return VIM_NO;
4565 }
4566 return VIM_CANCEL;
4567}
4568
4569 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004570vim_dialog_yesnoallcancel(
4571 int type,
4572 char_u *title,
4573 char_u *message,
4574 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575{
4576 switch (do_dialog(type,
4577 title == NULL ? (char_u *)"Question" : title,
4578 message,
4579 (char_u *)_("&Yes\n&No\nSave &All\n&Discard All\n&Cancel"),
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004580 dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 {
4582 case 1: return VIM_YES;
4583 case 2: return VIM_NO;
4584 case 3: return VIM_ALL;
4585 case 4: return VIM_DISCARDALL;
4586 }
4587 return VIM_CANCEL;
4588}
4589
Bram Moolenaar85a20022019-12-21 18:25:54 +01004590#endif // FEAT_GUI_DIALOG || FEAT_CON_DIALOG