blob: f0d1806c6037caaca32b45452f4c8e65e7e61535 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * message.c: functions for displaying messages on the command line
12 */
13
Bram Moolenaar85a20022019-12-21 18:25:54 +010014#define MESSAGE_FILE // don't include prototype for smsg()
Bram Moolenaar071d4272004-06-13 20:20:40 +000015
16#include "vim.h"
17
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010018static void add_msg_hist(char_u *s, int len, int attr);
Christian Brabandt51d4d842024-12-06 17:26:25 +010019static void check_msg_hist(void);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010020static void hit_return_msg(void);
21static void msg_home_replace_attr(char_u *fname, int attr);
Bram Moolenaar32526b32019-01-19 17:43:09 +010022static void msg_puts_attr_len(char *str, int maxlen, int attr);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010023static void msg_puts_display(char_u *str, int maxlen, int attr, int recurse);
24static void msg_scroll_up(void);
25static void inc_msg_scrolled(void);
26static void store_sb_text(char_u **sb_str, char_u *s, int attr, int *sb_col, int finish);
27static void t_puts(int *t_col, char_u *t_s, char_u *s, int attr);
28static void msg_puts_printf(char_u *str, int maxlen);
29static int do_more_prompt(int typed_char);
30static void msg_screen_putchar(int c, int attr);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020031static void msg_moremsg(int full);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010032static int msg_check_screen(void);
33static void redir_write(char_u *s, int maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000034#ifdef FEAT_CON_DIALOG
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010035static char_u *msg_show_console_dialog(char_u *message, char_u *buttons, int dfltbutton);
Bram Moolenaar85a20022019-12-21 18:25:54 +010036static int confirm_msg_used = FALSE; // displaying confirm_msg
37static char_u *confirm_msg = NULL; // ":confirm" message
38static char_u *confirm_msg_tail; // tail of confirm_msg
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020039static void display_confirm_msg(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000040#endif
Bram Moolenaar3b8c7082022-11-30 20:20:56 +000041#ifdef FEAT_EVAL
Bram Moolenaar958dc692016-12-01 15:34:12 +010042static int emsg_to_channel_log = FALSE;
43#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000044
45struct msg_hist
46{
47 struct msg_hist *next;
48 char_u *msg;
49 int attr;
50};
51
52static struct msg_hist *first_msg_hist = NULL;
53static struct msg_hist *last_msg_hist = NULL;
54static int msg_hist_len = 0;
Christian Brabandt51d4d842024-12-06 17:26:25 +010055static int msg_hist_max = 500; // The default max value is 500
56
57// flags obtained from the 'messagesopt' option
58#define MESSAGES_HIT_ENTER 0x001
59#define MESSAGES_WAIT 0x002
60#define MESSAGES_HISTORY 0x004
61
62// args in 'messagesopt' option
63#define MESSAGES_OPT_HIT_ENTER "hit-enter"
64#define MESSAGES_OPT_WAIT "wait:"
65#define MESSAGES_OPT_HISTORY "history:"
66
67// The default is "hit-enter,history:500"
68static int msg_flags = MESSAGES_HIT_ENTER | MESSAGES_HISTORY;
69static int msg_wait = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000070
Bram Moolenaarb5b5b892011-09-14 15:39:29 +020071static FILE *verbose_fd = NULL;
72static int verbose_did_open = FALSE;
73
Christian Brabandt45e07042024-11-11 20:52:55 +010074static int did_warn_clipboard = FALSE;
Christian Brabandt45e07042024-11-11 20:52:55 +010075
Bram Moolenaar071d4272004-06-13 20:20:40 +000076/*
77 * When writing messages to the screen, there are many different situations.
78 * A number of variables is used to remember the current state:
79 * msg_didany TRUE when messages were written since the last time the
80 * user reacted to a prompt.
81 * Reset: After hitting a key for the hit-return prompt,
82 * hitting <CR> for the command line or input().
83 * Set: When any message is written to the screen.
84 * msg_didout TRUE when something was written to the current line.
85 * Reset: When advancing to the next line, when the current
86 * text can be overwritten.
87 * Set: When any message is written to the screen.
88 * msg_nowait No extra delay for the last drawn message.
89 * Used in normal_cmd() before the mode message is drawn.
90 * emsg_on_display There was an error message recently. Indicates that there
91 * should be a delay before redrawing.
92 * msg_scroll The next message should not overwrite the current one.
93 * msg_scrolled How many lines the screen has been scrolled (because of
94 * messages). Used in update_screen() to scroll the screen
95 * back. Incremented each time the screen scrolls a line.
96 * msg_scrolled_ign TRUE when msg_scrolled is non-zero and msg_puts_attr()
97 * writes something without scrolling should not make
98 * need_wait_return to be set. This is a hack to make ":ts"
99 * work without an extra prompt.
100 * lines_left Number of lines available for messages before the
Bram Moolenaar5d6f75e2011-12-30 14:14:29 +0100101 * more-prompt is to be given. -1 when not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000102 * need_wait_return TRUE when the hit-return prompt is needed.
103 * Reset: After giving the hit-return prompt, when the user
104 * has answered some other prompt.
105 * Set: When the ruler or typeahead display is overwritten,
106 * scrolling the screen for some message.
107 * keep_msg Message to be displayed after redrawing the screen, in
108 * main_loop().
109 * This is an allocated string or NULL when not used.
110 */
111
112/*
113 * msg(s) - displays the string 's' on the status line
114 * When terminal not initialized (yet) mch_errmsg(..) is used.
Bram Moolenaar13608d82022-08-29 15:06:50 +0100115 * return TRUE if wait_return() not called
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116 */
117 int
Bram Moolenaar32526b32019-01-19 17:43:09 +0100118msg(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119{
120 return msg_attr_keep(s, 0, FALSE);
121}
122
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000123/*
124 * Like msg() but keep it silent when 'verbosefile' is set.
125 */
126 int
Bram Moolenaar32526b32019-01-19 17:43:09 +0100127verb_msg(char *s)
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000128{
129 int n;
130
131 verbose_enter();
132 n = msg_attr_keep(s, 0, FALSE);
133 verbose_leave();
134
135 return n;
136}
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000137
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138 int
Bram Moolenaar32526b32019-01-19 17:43:09 +0100139msg_attr(char *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140{
141 return msg_attr_keep(s, attr, FALSE);
142}
143
144 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100145msg_attr_keep(
Bram Moolenaar32526b32019-01-19 17:43:09 +0100146 char *s,
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100147 int attr,
Bram Moolenaar85a20022019-12-21 18:25:54 +0100148 int keep) // TRUE: set keep_msg if it doesn't scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149{
150 static int entered = 0;
151 int retval;
152 char_u *buf = NULL;
153
Bram Moolenaar85a20022019-12-21 18:25:54 +0100154 // Skip messages not matching ":filter pattern".
155 // Don't filter when there is an error.
Bram Moolenaar32526b32019-01-19 17:43:09 +0100156 if (!emsg_on_display && message_filtered((char_u *)s))
Bram Moolenaar7b668e82016-08-23 23:51:21 +0200157 return TRUE;
158
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159#ifdef FEAT_EVAL
160 if (attr == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100161 set_vim_var_string(VV_STATUSMSG, (char_u *)s, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162#endif
163
164 /*
165 * It is possible that displaying a messages causes a problem (e.g.,
166 * when redrawing the window), which causes another message, etc.. To
167 * break this loop, limit the recursiveness to 3 levels.
168 */
169 if (entered >= 3)
170 return TRUE;
171 ++entered;
172
Bram Moolenaar85a20022019-12-21 18:25:54 +0100173 // Add message to history (unless it's a repeated kept message or a
174 // truncated message)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100175 if ((char_u *)s != keep_msg
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176 || (*s != '<'
177 && last_msg_hist != NULL
178 && last_msg_hist->msg != NULL
179 && STRCMP(s, last_msg_hist->msg)))
Bram Moolenaar32526b32019-01-19 17:43:09 +0100180 add_msg_hist((char_u *)s, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181
Bram Moolenaar4c5678f2022-11-30 18:12:19 +0000182#ifdef FEAT_EVAL
Bram Moolenaar958dc692016-12-01 15:34:12 +0100183 if (emsg_to_channel_log)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100184 // Write message in the channel log.
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +0000185 ch_log(NULL, "ERROR: %s", s);
Bram Moolenaar958dc692016-12-01 15:34:12 +0100186#endif
187
Bram Moolenaar85a20022019-12-21 18:25:54 +0100188 // Truncate the message if needed.
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000189 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +0100190 buf = msg_strtrunc((char_u *)s, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191 if (buf != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100192 s = (char *)buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193
Bram Moolenaar32526b32019-01-19 17:43:09 +0100194 msg_outtrans_attr((char_u *)s, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195 msg_clr_eos();
196 retval = msg_end();
197
Bram Moolenaar32526b32019-01-19 17:43:09 +0100198 if (keep && retval && vim_strsize((char_u *)s)
199 < (int)(Rows - cmdline_row - 1) * Columns + sc_col)
200 set_keep_msg((char_u *)s, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201
Rob Pilling726f7f92022-01-20 14:44:38 +0000202 need_fileinfo = FALSE;
203
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204 vim_free(buf);
205 --entered;
206 return retval;
207}
208
209/*
210 * Truncate a string such that it can be printed without causing a scroll.
211 * Returns an allocated string or NULL when no truncating is done.
212 */
213 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100214msg_strtrunc(
215 char_u *s,
Bram Moolenaar85a20022019-12-21 18:25:54 +0100216 int force) // always truncate
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217{
218 char_u *buf = NULL;
219 int len;
220 int room;
221
Bram Moolenaar85a20022019-12-21 18:25:54 +0100222 // May truncate message to avoid a hit-return prompt
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000223 if ((!msg_scroll && !need_wait_return && shortmess(SHM_TRUNCALL)
224 && !exmode_active && msg_silent == 0) || force)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225 {
226 len = vim_strsize(s);
Bram Moolenaar35a4fbc2022-08-28 14:39:53 +0100227 if (msg_scrolled != 0
228#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaara2a89732022-08-31 14:46:18 +0100229 || in_echowindow
Bram Moolenaar35a4fbc2022-08-28 14:39:53 +0100230#endif
231 )
Bram Moolenaar85a20022019-12-21 18:25:54 +0100232 // Use all the columns.
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000233 room = (int)(Rows - msg_row) * Columns - 1;
234 else
Bram Moolenaar85a20022019-12-21 18:25:54 +0100235 // Use up to 'showcmd' column.
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000236 room = (int)(Rows - msg_row - 1) * Columns + sc_col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237 if (len > room && room > 0)
238 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239 if (enc_utf8)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100240 // may have up to 18 bytes per cell (6 per char, up to two
241 // composing chars)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100242 len = (room + 2) * 18;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243 else if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100244 // may have up to 2 bytes per cell for euc-jp
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100245 len = (room + 2) * 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246 else
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100247 len = room + 2;
248 buf = alloc(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249 if (buf != NULL)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100250 trunc_string(s, buf, room, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251 }
252 }
253 return buf;
254}
255
256/*
257 * Truncate a string "s" to "buf" with cell width "room".
258 * "s" and "buf" may be equal.
259 */
260 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100261trunc_string(
262 char_u *s,
263 char_u *buf,
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200264 int room_in,
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100265 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266{
Bram Moolenaar85a20022019-12-21 18:25:54 +0100267 size_t room = room_in - 3; // "..." takes 3 chars
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200268 size_t half;
269 size_t len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270 int e;
271 int i;
272 int n;
273
Bram Moolenaar62818152021-02-14 15:37:30 +0100274 if (*s == NUL)
275 {
276 if (buflen > 0)
277 *buf = NUL;
278 return;
279 }
280
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200281 if (room_in < 3)
282 room = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000283 half = room / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284
Bram Moolenaar85a20022019-12-21 18:25:54 +0100285 // First part: Start of the string.
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100286 for (e = 0; len < half && e < buflen; ++e)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000287 {
288 if (s[e] == NUL)
289 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100290 // text fits without truncating!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291 buf[e] = NUL;
292 return;
293 }
294 n = ptr2cells(s + e);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200295 if (len + n > half)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000296 break;
297 len += n;
298 buf[e] = s[e];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000299 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000300 for (n = (*mb_ptr2len)(s + e); --n > 0; )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100302 if (++e == buflen)
303 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304 buf[e] = s[e];
305 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306 }
307
Bram Moolenaar85a20022019-12-21 18:25:54 +0100308 // Last part: End of the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309 i = e;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000310 if (enc_dbcs != 0)
311 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100312 // For DBCS going backwards in a string is slow, but
313 // computing the cell width isn't too slow: go forward
314 // until the rest fits.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315 n = vim_strsize(s + i);
316 while (len + n > room)
317 {
318 n -= ptr2cells(s + i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000319 i += (*mb_ptr2len)(s + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320 }
321 }
322 else if (enc_utf8)
323 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100324 // For UTF-8 we can go backwards easily.
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000325 half = i = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326 for (;;)
327 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000328 do
Bram Moolenaarace95982017-03-29 17:30:27 +0200329 half = half - utf_head_off(s, s + half - 1) - 1;
Bram Moolenaarb9644432016-07-19 12:33:44 +0200330 while (half > 0 && utf_iscomposing(utf_ptr2char(s + half)));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331 n = ptr2cells(s + half);
Bram Moolenaarb9644432016-07-19 12:33:44 +0200332 if (len + n > room || half == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333 break;
334 len += n;
Bram Moolenaara5c0cc12016-07-30 16:40:39 +0200335 i = (int)half;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336 }
337 }
338 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339 {
Bram Moolenaard0337e32019-12-30 17:55:34 +0100340 for (i = (int)STRLEN(s);
341 i - 1 >= 0 && len + (n = ptr2cells(s + i - 1)) <= room; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342 len += n;
343 }
344
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200345
346 if (i <= e + 3)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100347 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100348 // text fits without truncating
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200349 if (s != buf)
350 {
351 len = STRLEN(s);
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200352 if (len >= (size_t)buflen)
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200353 len = buflen - 1;
354 len = len - e + 1;
355 if (len < 1)
356 buf[e - 1] = NUL;
357 else
358 mch_memmove(buf + e, s + e, len);
359 }
360 }
361 else if (e + 3 < buflen)
362 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100363 // set the middle and copy the last part
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100364 mch_memmove(buf + e, "...", (size_t)3);
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200365 len = STRLEN(s + i) + 1;
366 if (len >= (size_t)buflen - e - 3)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100367 len = buflen - e - 3 - 1;
368 mch_memmove(buf + e + 3, s + i, len);
369 buf[e + 3 + len - 1] = NUL;
370 }
371 else
372 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100373 // can't fit in the "...", just truncate it
Christian Brabandt3bd7fa12023-10-02 20:59:08 +0200374 buf[buflen - 1] = NUL;
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100375 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376}
377
378/*
379 * Automatic prototype generation does not understand this function.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100380 * Note: Caller of smsg() and smsg_attr() must check the resulting string is
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381 * shorter than IOSIZE!!!
382 */
383#ifndef PROTO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100385int vim_snprintf(char *str, size_t str_m, const char *fmt, ...);
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000386
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100388smsg(const char *s, ...)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200390 if (IObuff == NULL)
391 {
392 // Very early in initialisation and already something wrong, just
393 // give the raw message so the user at least gets a hint.
394 return msg((char *)s);
395 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000397 va_list arglist;
398
399 va_start(arglist, s);
400 vim_vsnprintf((char *)IObuff, IOSIZE, s, arglist);
401 va_end(arglist);
402 return msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403}
404
405 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100406smsg_attr(int attr, const char *s, ...)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200408 if (IObuff == NULL)
409 {
410 // Very early in initialisation and already something wrong, just
411 // give the raw message so the user at least gets a hint.
412 return msg_attr((char *)s, attr);
413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000415 va_list arglist;
416
417 va_start(arglist, s);
418 vim_vsnprintf((char *)IObuff, IOSIZE, s, arglist);
419 va_end(arglist);
420 return msg_attr((char *)IObuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421}
422
Bram Moolenaare0429682018-07-01 16:44:03 +0200423 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100424smsg_attr_keep(int attr, const char *s, ...)
Bram Moolenaare0429682018-07-01 16:44:03 +0200425{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200426 if (IObuff == NULL)
427 {
428 // Very early in initialisation and already something wrong, just
429 // give the raw message so the user at least gets a hint.
430 return msg_attr_keep((char *)s, attr, TRUE);
431 }
Bram Moolenaare0429682018-07-01 16:44:03 +0200432
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000433 va_list arglist;
434
435 va_start(arglist, s);
436 vim_vsnprintf((char *)IObuff, IOSIZE, s, arglist);
437 va_end(arglist);
438 return msg_attr_keep((char *)IObuff, attr, TRUE);
Bram Moolenaare0429682018-07-01 16:44:03 +0200439}
440
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441#endif
442
443/*
444 * Remember the last sourcing name/lnum used in an error message, so that it
445 * isn't printed each time when it didn't change.
446 */
447static int last_sourcing_lnum = 0;
448static char_u *last_sourcing_name = NULL;
449
450/*
451 * Reset the last used sourcing name/lnum. Makes sure it is displayed again
452 * for the next error message;
453 */
Bram Moolenaar4eec5ec2005-06-26 22:26:21 +0000454 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100455reset_last_sourcing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000456{
Bram Moolenaard23a8232018-02-10 18:45:26 +0100457 VIM_CLEAR(last_sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458 last_sourcing_lnum = 0;
459}
460
461/*
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100462 * Return TRUE if "SOURCING_NAME" differs from "last_sourcing_name".
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000463 */
464 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100465other_sourcing_name(void)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000466{
Bram Moolenaar0bd8d052021-11-25 13:39:28 +0000467 if (HAVE_SOURCING_INFO && SOURCING_NAME != NULL)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000468 {
469 if (last_sourcing_name != NULL)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100470 return STRCMP(SOURCING_NAME, last_sourcing_name) != 0;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000471 return TRUE;
472 }
473 return FALSE;
474}
475
476/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477 * Get the message about the source, as used for an error message.
478 * Returns an allocated string with room for one more character.
479 * Returns NULL when no message is to be given.
480 */
481 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100482get_emsg_source(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483{
484 char_u *Buf, *p;
485
Bram Moolenaar0bd8d052021-11-25 13:39:28 +0000486 if (HAVE_SOURCING_INFO && SOURCING_NAME != NULL && other_sourcing_name())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487 {
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200488 char_u *sname = estack_sfile(ESTACK_NONE);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100489 char_u *tofree = sname;
490
491 if (sname == NULL)
492 sname = SOURCING_NAME;
493
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +0200494#ifdef FEAT_EVAL
495 if (estack_compiling)
496 p = (char_u *)_("Error detected while compiling %s:");
497 else
498#endif
499 p = (char_u *)_("Error detected while processing %s:");
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100500 Buf = alloc(STRLEN(sname) + STRLEN(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501 if (Buf != NULL)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100502 sprintf((char *)Buf, (char *)p, sname);
503 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504 return Buf;
505 }
506 return NULL;
507}
508
509/*
510 * Get the message about the source lnum, as used for an error message.
511 * Returns an allocated string with room for one more character.
512 * Returns NULL when no message is to be given.
513 */
514 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100515get_emsg_lnum(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516{
517 char_u *Buf, *p;
518
Bram Moolenaar85a20022019-12-21 18:25:54 +0100519 // lnum is 0 when executing a command from the command line
520 // argument, we don't want a line number then
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100521 if (SOURCING_NAME != NULL
522 && (other_sourcing_name() || SOURCING_LNUM != last_sourcing_lnum)
523 && SOURCING_LNUM != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524 {
525 p = (char_u *)_("line %4ld:");
Bram Moolenaar964b3742019-05-24 18:54:09 +0200526 Buf = alloc(STRLEN(p) + 20);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527 if (Buf != NULL)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100528 sprintf((char *)Buf, (char *)p, (long)SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 return Buf;
530 }
531 return NULL;
532}
533
534/*
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000535 * Display name and line number for the source of an error.
536 * Remember the file name and line number, so that for the next error the info
537 * is only displayed if it changed.
538 */
539 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100540msg_source(int attr)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000541{
542 char_u *p;
Bram Moolenaarba8c9262021-11-25 14:43:18 +0000543 static int recursive = FALSE;
544
545 // Bail out if something called here causes an error.
546 if (recursive)
547 return;
548 recursive = TRUE;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000549
550 ++no_wait_return;
551 p = get_emsg_source();
552 if (p != NULL)
553 {
zeertzjqbdedd2b2022-09-20 12:45:15 +0100554 msg_scroll = TRUE; // this will take more than one line
Bram Moolenaar32526b32019-01-19 17:43:09 +0100555 msg_attr((char *)p, attr);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000556 vim_free(p);
557 }
558 p = get_emsg_lnum();
559 if (p != NULL)
560 {
Bram Moolenaar32526b32019-01-19 17:43:09 +0100561 msg_attr((char *)p, HL_ATTR(HLF_N));
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000562 vim_free(p);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100563 last_sourcing_lnum = SOURCING_LNUM; // only once for each line
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000564 }
565
Bram Moolenaar85a20022019-12-21 18:25:54 +0100566 // remember the last sourcing name printed, also when it's empty
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100567 if (SOURCING_NAME == NULL || other_sourcing_name())
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000568 {
Bram Moolenaarba8c9262021-11-25 14:43:18 +0000569 VIM_CLEAR(last_sourcing_name);
570 if (SOURCING_NAME != NULL)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100571 last_sourcing_name = vim_strsave(SOURCING_NAME);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000572 }
573 --no_wait_return;
Bram Moolenaarba8c9262021-11-25 14:43:18 +0000574
575 recursive = FALSE;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000576}
577
578/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000579 * Return TRUE if not giving error messages right now:
580 * If "emsg_off" is set: no error messages at the moment.
581 * If "msg" is in 'debug': do error message but without side effects.
582 * If "emsg_skip" is set: never do error messages.
583 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +0200584 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100585emsg_not_now(void)
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000586{
587 if ((emsg_off > 0 && vim_strchr(p_debug, 'm') == NULL
588 && vim_strchr(p_debug, 't') == NULL)
589#ifdef FEAT_EVAL
590 || emsg_skip > 0
591#endif
592 )
593 return TRUE;
594 return FALSE;
595}
596
Bram Moolenaar5a66dfb2017-03-01 20:40:39 +0100597#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100598static garray_T ignore_error_list = GA_EMPTY;
599
600 void
601ignore_error_for_testing(char_u *error)
602{
603 if (ignore_error_list.ga_itemsize == 0)
604 ga_init2(&ignore_error_list, sizeof(char_u *), 1);
605
Bram Moolenaar461a7fc2018-12-22 13:28:07 +0100606 if (STRCMP("RESET", error) == 0)
607 ga_clear_strings(&ignore_error_list);
608 else
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000609 ga_copy_string(&ignore_error_list, error);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100610}
611
612 static int
Bram Moolenaar097c5372023-05-24 21:02:24 +0100613ignore_error(const char *msg)
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100614{
615 int i;
616
617 for (i = 0; i < ignore_error_list.ga_len; ++i)
Bram Moolenaar097c5372023-05-24 21:02:24 +0100618 if (strstr(msg,
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100619 (char *)((char_u **)(ignore_error_list.ga_data))[i]) != NULL)
620 return TRUE;
621 return FALSE;
622}
623#endif
624
Bram Moolenaarb869c0d2016-07-20 00:10:51 +0200625#if !defined(HAVE_STRERROR) || defined(PROTO)
626/*
627 * Replacement for perror() that behaves more or less like emsg() was called.
Bram Moolenaar53989552019-12-23 22:59:18 +0100628 * v:errmsg will be set and called_emsg will be incremented.
Bram Moolenaarb869c0d2016-07-20 00:10:51 +0200629 */
630 void
631do_perror(char *msg)
632{
633 perror(msg);
634 ++emsg_silent;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100635 emsg(msg);
Bram Moolenaarb869c0d2016-07-20 00:10:51 +0200636 --emsg_silent;
637}
638#endif
639
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000640/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100641 * emsg_core() - display an error message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 *
643 * Rings the bell, if appropriate, and calls message() to do the real work
644 * When terminal not initialized (yet) mch_errmsg(..) is used.
645 *
Bram Moolenaar13608d82022-08-29 15:06:50 +0100646 * Return TRUE if wait_return() not called.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100647 * Note: caller must check 'emsg_not_now()' before calling this.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100649 static int
Bram Moolenaar097c5372023-05-24 21:02:24 +0100650emsg_core(const char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651{
652 int attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653 char_u *p;
Bram Moolenaar958dc692016-12-01 15:34:12 +0100654 int r;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655#ifdef FEAT_EVAL
656 int ignore = FALSE;
657 int severe;
658#endif
659
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100660#ifdef FEAT_EVAL
Bram Moolenaar85a20022019-12-21 18:25:54 +0100661 // When testing some errors are turned into a normal message.
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100662 if (ignore_error(s))
Bram Moolenaar85a20022019-12-21 18:25:54 +0100663 // don't call msg() if it results in a dialog
Bram Moolenaar32526b32019-01-19 17:43:09 +0100664 return msg_use_printf() ? FALSE : msg((char *)s);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100665#endif
666
Bram Moolenaar53989552019-12-23 22:59:18 +0100667 ++called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669#ifdef FEAT_EVAL
Bram Moolenaar85a20022019-12-21 18:25:54 +0100670 // If "emsg_severe" is TRUE: When an error exception is to be thrown,
671 // prefer this message over previous messages for the same command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 severe = emsg_severe;
673 emsg_severe = FALSE;
674#endif
675
Bram Moolenaar57657d82006-04-21 22:12:41 +0000676 if (!emsg_off || vim_strchr(p_debug, 't') != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 {
678#ifdef FEAT_EVAL
679 /*
680 * Cause a throw of an error exception if appropriate. Don't display
681 * the error message in this case. (If no matching catch clause will
682 * be found, the message will be displayed later on.) "ignore" is set
683 * when the message should be ignored completely (used for the
684 * interrupt message).
685 */
Bram Moolenaar097c5372023-05-24 21:02:24 +0100686 if (cause_errthrow((char_u *)s, severe, &ignore) == TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 {
688 if (!ignore)
Bram Moolenaar76a63452018-11-28 20:38:37 +0100689 ++did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 return TRUE;
691 }
692
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100693 if (in_assert_fails && emsg_assert_fails_msg == NULL)
Bram Moolenaar1d634542020-08-18 13:41:50 +0200694 {
Bram Moolenaar097c5372023-05-24 21:02:24 +0100695 emsg_assert_fails_msg = vim_strsave((char_u *)s);
Bram Moolenaar1d634542020-08-18 13:41:50 +0200696 emsg_assert_fails_lnum = SOURCING_LNUM;
Bram Moolenaar9bd5d872020-09-06 21:47:48 +0200697 vim_free(emsg_assert_fails_context);
698 emsg_assert_fails_context = vim_strsave(
699 SOURCING_NAME == NULL ? (char_u *)"" : SOURCING_NAME);
Bram Moolenaar1d634542020-08-18 13:41:50 +0200700 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200701
Bram Moolenaar85a20022019-12-21 18:25:54 +0100702 // set "v:errmsg", also when using ":silent! cmd"
Bram Moolenaar097c5372023-05-24 21:02:24 +0100703 set_vim_var_string(VV_ERRMSG, (char_u *)s, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704#endif
705
706 /*
Bram Moolenaara7241f52008-06-24 20:39:31 +0000707 * When using ":silent! cmd" ignore error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 * But do write it to the redirection file.
709 */
710 if (emsg_silent != 0)
711 {
Bram Moolenaar599410c2021-04-10 14:03:43 +0200712#ifdef FEAT_EVAL
713 ++did_emsg_silent;
714#endif
Bram Moolenaar79815f12016-07-09 17:07:29 +0200715 if (emsg_noredir == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 {
Bram Moolenaar79815f12016-07-09 17:07:29 +0200717 msg_start();
718 p = get_emsg_source();
719 if (p != NULL)
720 {
721 STRCAT(p, "\n");
722 redir_write(p, -1);
723 vim_free(p);
724 }
725 p = get_emsg_lnum();
726 if (p != NULL)
727 {
728 STRCAT(p, "\n");
729 redir_write(p, -1);
730 vim_free(p);
731 }
Bram Moolenaar097c5372023-05-24 21:02:24 +0100732 redir_write((char_u *)s, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +0100734#ifdef FEAT_EVAL
735 // Only increment did_emsg_def when :silent! wasn't used inside the
736 // :def function.
737 if (emsg_silent == emsg_silent_def)
738 ++did_emsg_def;
739#endif
Bram Moolenaar4c5678f2022-11-30 18:12:19 +0000740#ifdef FEAT_EVAL
Bram Moolenaar097c5372023-05-24 21:02:24 +0100741 ch_log(NULL, "ERROR silent: %s", s);
Bram Moolenaar958dc692016-12-01 15:34:12 +0100742#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743 return TRUE;
744 }
745
Bram Moolenaar2b7bc562017-01-14 19:24:52 +0100746 ex_exitval = 1;
747
Bram Moolenaar85a20022019-12-21 18:25:54 +0100748 // Reset msg_silent, an error causes messages to be switched back on.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 msg_silent = 0;
750 cmd_silent = FALSE;
751
Bram Moolenaar85a20022019-12-21 18:25:54 +0100752 if (global_busy) // break :global command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 ++global_busy;
754
755 if (p_eb)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100756 beep_flush(); // also includes flush_buffers()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 else
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200758 flush_buffers(FLUSH_MINIMAL); // flush internal buffers
Bram Moolenaar76a63452018-11-28 20:38:37 +0100759 ++did_emsg; // flag for DoOneCmd()
Bram Moolenaare723c422017-09-06 23:40:10 +0200760#ifdef FEAT_EVAL
Bram Moolenaar88c89c72021-08-14 14:01:05 +0200761 ++uncaught_emsg;
Bram Moolenaare723c422017-09-06 23:40:10 +0200762#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763 }
764
Bram Moolenaar878e1d22022-08-28 17:53:23 +0100765#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaara2a89732022-08-31 14:46:18 +0100766 if (!in_echowindow)
Bram Moolenaar878e1d22022-08-28 17:53:23 +0100767#endif
768 emsg_on_display = TRUE; // remember there is an error message
769
770 attr = HL_ATTR(HLF_E); // set highlight mode for error messages
Bram Moolenaarbb15b652005-10-03 21:52:09 +0000771 if (msg_scrolled != 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100772 need_wait_return = TRUE; // needed in case emsg() is called after
Bram Moolenaar13608d82022-08-29 15:06:50 +0100773 // wait_return() has reset need_wait_return
Bram Moolenaar85a20022019-12-21 18:25:54 +0100774 // and a redraw is expected because
775 // msg_scrolled is non-zero
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776
Bram Moolenaar958dc692016-12-01 15:34:12 +0100777#ifdef FEAT_JOB_CHANNEL
778 emsg_to_channel_log = TRUE;
779#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 /*
781 * Display name and line number for the source of the error.
782 */
zeertzjqbdedd2b2022-09-20 12:45:15 +0100783 msg_scroll = TRUE;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000784 msg_source(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785
786 /*
787 * Display the error message itself.
788 */
Bram Moolenaar85a20022019-12-21 18:25:54 +0100789 msg_nowait = FALSE; // wait for this msg
Bram Moolenaar32526b32019-01-19 17:43:09 +0100790 r = msg_attr((char *)s, attr);
Bram Moolenaar958dc692016-12-01 15:34:12 +0100791
792#ifdef FEAT_JOB_CHANNEL
793 emsg_to_channel_log = FALSE;
794#endif
795 return r;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796}
797
798/*
Bram Moolenaar097c5372023-05-24 21:02:24 +0100799 * Print error message "s". Should already be translated.
800 * Return TRUE if wait_return() not called.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 */
802 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100803emsg(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804{
Bram Moolenaar85a20022019-12-21 18:25:54 +0100805 // Skip this if not giving error messages at the moment.
Bram Moolenaar097c5372023-05-24 21:02:24 +0100806 if (emsg_not_now())
807 return TRUE;
808
809 return emsg_core(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810}
811
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100812#ifndef PROTO // manual proto with __attribute__
Bram Moolenaar95f09602016-11-10 20:01:45 +0100813/*
Bram Moolenaar097c5372023-05-24 21:02:24 +0100814 * Print error message "s" with format string and variable arguments.
815 * "s" should already be translated.
816 * Note: caller must not use "IObuff" for "s"!
817 * Return TRUE if wait_return() not called.
Bram Moolenaar95f09602016-11-10 20:01:45 +0100818 */
819 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100820semsg(const char *s, ...)
Bram Moolenaar95f09602016-11-10 20:01:45 +0100821{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200822 // Skip this if not giving error messages at the moment.
Bram Moolenaar097c5372023-05-24 21:02:24 +0100823 if (emsg_not_now())
824 return TRUE;
Bram Moolenaar95f09602016-11-10 20:01:45 +0100825
Bram Moolenaar097c5372023-05-24 21:02:24 +0100826 if (IObuff == NULL)
827 // Very early in initialisation and already something wrong, just
828 // give the raw message so the user at least gets a hint.
829 return emsg_core(s);
830
831 va_list ap;
832
833 va_start(ap, s);
834 vim_vsnprintf((char *)IObuff, IOSIZE, s, ap);
835 va_end(ap);
836 return emsg_core((char *)IObuff);
Bram Moolenaar95f09602016-11-10 20:01:45 +0100837}
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100838#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +0100839
840/*
841 * Same as emsg(...), but abort on error when ABORT_ON_INTERNAL_ERROR is
842 * defined. It is used for internal errors only, so that they can be
843 * detected when fuzzing vim.
844 */
845 void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100846iemsg(char *s)
Bram Moolenaar95f09602016-11-10 20:01:45 +0100847{
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000848 if (emsg_not_now())
849 return;
850
Bram Moolenaar097c5372023-05-24 21:02:24 +0100851 // Give a generic error which is translated. The error itself may not be
852 // translated, it almost never shows.
853 emsg_core(_(e_internal_error_please_report_a_bug));
854
855 emsg_core(s);
Bram Moolenaar096ca732022-01-01 00:55:28 +0000856#if defined(ABORT_ON_INTERNAL_ERROR) && defined(FEAT_EVAL)
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000857 set_vim_var_string(VV_ERRMSG, (char_u *)s, -1);
858 msg_putchar('\n'); // avoid overwriting the error message
859 out_flush();
860 abort();
Bram Moolenaar95f09602016-11-10 20:01:45 +0100861#endif
862}
863
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100864#ifndef PROTO // manual proto with __attribute__
Bram Moolenaar95f09602016-11-10 20:01:45 +0100865/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100866 * Same as semsg(...) but abort on error when ABORT_ON_INTERNAL_ERROR is
Bram Moolenaar95f09602016-11-10 20:01:45 +0100867 * defined. It is used for internal errors only, so that they can be
868 * detected when fuzzing vim.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100869 * Note: caller must not pass 'IObuff' as 1st argument.
Bram Moolenaar95f09602016-11-10 20:01:45 +0100870 */
871 void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100872siemsg(const char *s, ...)
Bram Moolenaar95f09602016-11-10 20:01:45 +0100873{
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000874 if (emsg_not_now())
875 return;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100876
Bram Moolenaar097c5372023-05-24 21:02:24 +0100877 // Give a generic error which is translated. The error itself may not be
878 // translated, it almost never shows.
879 emsg_core(_(e_internal_error_please_report_a_bug));
880
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000881 if (IObuff == NULL)
882 {
883 // Very early in initialisation and already something wrong, just
884 // give the raw message so the user at least gets a hint.
Bram Moolenaar097c5372023-05-24 21:02:24 +0100885 emsg_core(s);
Bram Moolenaar213e70e2022-08-19 13:17:21 +0100886 }
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000887 else
888 {
889 va_list ap;
890
891 va_start(ap, s);
892 vim_vsnprintf((char *)IObuff, IOSIZE, s, ap);
893 va_end(ap);
Bram Moolenaar097c5372023-05-24 21:02:24 +0100894 emsg_core((char *)IObuff);
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000895 }
896# ifdef ABORT_ON_INTERNAL_ERROR
897 msg_putchar('\n'); // avoid overwriting the error message
898 out_flush();
899 abort();
900# endif
Bram Moolenaar95f09602016-11-10 20:01:45 +0100901}
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100902#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +0100903
904/*
905 * Give an "Internal error" message.
906 */
907 void
908internal_error(char *where)
909{
Bram Moolenaar097c5372023-05-24 21:02:24 +0100910 // Give a generic error which is translated. The error itself may not be
911 // translated, it almost never shows.
912 emsg_core(_(e_internal_error_please_report_a_bug));
913
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000914 siemsg(_(e_internal_error_str), where);
Bram Moolenaar95f09602016-11-10 20:01:45 +0100915}
916
Dominique Pelle748b3082022-01-08 12:41:16 +0000917#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaardd589232020-02-29 17:38:12 +0100918/*
919 * Like internal_error() but do not call abort(), to avoid tests using
920 * test_unknown() and test_void() causing Vim to exit.
921 */
922 void
923internal_error_no_abort(char *where)
924{
Bram Moolenaar097c5372023-05-24 21:02:24 +0100925 // Give a generic error which is translated. The error itself may not be
926 // translated, it almost never shows.
927 emsg_core(_(e_internal_error_please_report_a_bug));
928
929 semsg(_(e_internal_error_str), where);
Bram Moolenaardd589232020-02-29 17:38:12 +0100930}
Dominique Pelle748b3082022-01-08 12:41:16 +0000931#endif
Bram Moolenaardd589232020-02-29 17:38:12 +0100932
Bram Moolenaar85a20022019-12-21 18:25:54 +0100933// emsg3() and emsgn() are in misc2.c to avoid warnings for the prototypes.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934
Bram Moolenaardf177f62005-02-22 08:39:57 +0000935 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100936emsg_invreg(int name)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000937{
cero19881d87e112023-02-16 15:03:12 +0000938 semsg(_(e_invalid_register_name_str), transchar_buf(NULL, name));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000939}
940
Dominique Pelle748b3082022-01-08 12:41:16 +0000941#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100943 * Give an error message which contains %s for "name[len]".
944 */
945 void
946emsg_namelen(char *msg, char_u *name, int len)
947{
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +0000948 char_u *copy = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100949
950 semsg(msg, copy == NULL ? "NULL" : (char *)copy);
Bram Moolenaar292b90d2020-03-18 15:23:16 +0100951 vim_free(copy);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100952}
Dominique Pelle748b3082022-01-08 12:41:16 +0000953#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100954
955/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 * Like msg(), but truncate to a single line if p_shm contains 't', or when
957 * "force" is TRUE. This truncates in another way as for normal messages.
958 * Careful: The string may be changed by msg_may_trunc()!
959 * Returns a pointer to the printed message, if wait_return() not called.
960 */
Bram Moolenaar32526b32019-01-19 17:43:09 +0100961 char *
962msg_trunc_attr(char *s, int force, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963{
964 int n;
Bram Moolenaar32526b32019-01-19 17:43:09 +0100965 char *ts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966
Bram Moolenaar85a20022019-12-21 18:25:54 +0100967 // Add message to history before truncating
Bram Moolenaar32526b32019-01-19 17:43:09 +0100968 add_msg_hist((char_u *)s, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969
Bram Moolenaar32526b32019-01-19 17:43:09 +0100970 ts = (char *)msg_may_trunc(force, (char_u *)s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971
972 msg_hist_off = TRUE;
Bram Moolenaar32526b32019-01-19 17:43:09 +0100973 n = msg_attr(ts, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 msg_hist_off = FALSE;
975
976 if (n)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100977 return ts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 return NULL;
979}
980
981/*
982 * Check if message "s" should be truncated at the start (for filenames).
983 * Return a pointer to where the truncated message starts.
984 * Note: May change the message by replacing a character with '<'.
985 */
986 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100987msg_may_trunc(int force, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988{
989 int n;
990 int room;
991
Bram Moolenaar9198de32022-08-27 21:30:03 +0100992 // If 'cmdheight' is zero or something unexpected happened "room" may be
993 // negative.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 room = (int)(Rows - cmdline_row - 1) * Columns + sc_col - 1;
Bram Moolenaar0fbc9262022-06-26 11:17:10 +0100995 if (room > 0 && (force || (shortmess(SHM_TRUNC) && !exmode_active))
Bram Moolenaara2a89732022-08-31 14:46:18 +0100996 && (n = (int)STRLEN(s) - room) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 if (has_mbyte)
999 {
1000 int size = vim_strsize(s);
1001
Bram Moolenaar85a20022019-12-21 18:25:54 +01001002 // There may be room anyway when there are multibyte chars.
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00001003 if (size <= room)
1004 return s;
1005
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 for (n = 0; size >= room; )
1007 {
1008 size -= (*mb_ptr2cells)(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001009 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 }
1011 --n;
1012 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 s += n;
1014 *s = '<';
1015 }
1016 return s;
1017}
1018
1019 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001020add_msg_hist(
1021 char_u *s,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001022 int len, // -1 for undetermined length
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001023 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024{
1025 struct msg_hist *p;
1026
1027 if (msg_hist_off || msg_silent != 0)
1028 return;
1029
Bram Moolenaar85a20022019-12-21 18:25:54 +01001030 // allocate an entry and add the message at the end of the history
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001031 p = ALLOC_ONE(struct msg_hist);
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001032 if (p == NULL)
1033 return;
1034
1035 if (len < 0)
1036 len = (int)STRLEN(s);
1037 // remove leading and trailing newlines
1038 while (len > 0 && *s == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 {
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001040 ++s;
1041 --len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 }
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001043 while (len > 0 && s[len - 1] == '\n')
1044 --len;
1045 p->msg = vim_strnsave(s, len);
1046 p->next = NULL;
1047 p->attr = attr;
1048 if (last_msg_hist != NULL)
1049 last_msg_hist->next = p;
1050 last_msg_hist = p;
1051 if (first_msg_hist == NULL)
1052 first_msg_hist = last_msg_hist;
1053 ++msg_hist_len;
Shougo Matsushita9f860a12024-11-24 13:48:05 +01001054
1055 check_msg_hist();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056}
1057
1058/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00001059 * Delete the first (oldest) message from the history.
1060 * Returns FAIL if there are no messages.
1061 */
1062 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001063delete_first_msg(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00001064{
1065 struct msg_hist *p;
1066
1067 if (msg_hist_len <= 0)
1068 return FAIL;
1069 p = first_msg_hist;
1070 first_msg_hist = p->next;
Bram Moolenaara7241f52008-06-24 20:39:31 +00001071 if (first_msg_hist == NULL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001072 last_msg_hist = NULL; // history is empty
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00001073 vim_free(p->msg);
1074 vim_free(p);
1075 --msg_hist_len;
1076 return OK;
1077}
1078
Christian Brabandt51d4d842024-12-06 17:26:25 +01001079 static void
Shougo Matsushita9f860a12024-11-24 13:48:05 +01001080check_msg_hist(void)
1081{
1082 // Don't let the message history get too big
Christian Brabandt51d4d842024-12-06 17:26:25 +01001083 while (msg_hist_len > 0 && msg_hist_len > msg_hist_max)
Shougo Matsushita9f860a12024-11-24 13:48:05 +01001084 (void)delete_first_msg();
1085}
1086
Christian Brabandt51d4d842024-12-06 17:26:25 +01001087
1088 int
1089messagesopt_changed(void)
1090{
1091 char_u *p;
1092 int messages_flags_new = 0;
1093 int messages_wait_new = 0;
1094 int messages_history_new = 0;
1095
zeertzjq8cc43da2024-12-07 16:09:08 +01001096 p = p_mopt;
Christian Brabandt51d4d842024-12-06 17:26:25 +01001097 while (*p != NUL)
1098 {
1099 if (STRNCMP(p, MESSAGES_OPT_HIT_ENTER,
1100 STRLEN_LITERAL(MESSAGES_OPT_HIT_ENTER)) == 0)
1101 {
1102 p += STRLEN_LITERAL(MESSAGES_OPT_HIT_ENTER);
1103 messages_flags_new |= MESSAGES_HIT_ENTER;
1104 }
1105 else if (STRNCMP(p, MESSAGES_OPT_WAIT,
1106 STRLEN_LITERAL(MESSAGES_OPT_WAIT)) == 0
1107 && VIM_ISDIGIT(p[STRLEN_LITERAL(MESSAGES_OPT_WAIT)]))
1108 {
1109 p += STRLEN_LITERAL(MESSAGES_OPT_WAIT);
1110 messages_wait_new = getdigits(&p);
1111 messages_flags_new |= MESSAGES_WAIT;
1112 }
1113 else if (STRNCMP(p, MESSAGES_OPT_HISTORY,
1114 STRLEN_LITERAL(MESSAGES_OPT_HISTORY)) == 0
1115 && VIM_ISDIGIT(p[STRLEN_LITERAL(MESSAGES_OPT_HISTORY)]))
1116 {
1117 p += STRLEN_LITERAL(MESSAGES_OPT_HISTORY);
1118 messages_history_new = getdigits(&p);
1119 messages_flags_new |= MESSAGES_HISTORY;
1120 }
1121
1122 if (*p != ',' && *p != NUL)
1123 return FAIL;
1124 if (*p == ',')
1125 ++p;
1126 }
1127
1128 // Either "wait" or "hit-enter" is required
1129 if (!(messages_flags_new & (MESSAGES_HIT_ENTER | MESSAGES_WAIT)))
1130 return FAIL;
1131
1132 // "history" must be set
1133 if (!(messages_flags_new & MESSAGES_HISTORY))
1134 return FAIL;
1135
1136 // "history" must be <= 10000
1137 if (messages_history_new > 10000)
1138 return FAIL;
1139
Shougo Matsushitad9e9f892024-12-07 16:00:25 +01001140 // "wait" must be <= 10000
1141 if (messages_wait_new > 10000)
1142 return FAIL;
1143
Christian Brabandt51d4d842024-12-06 17:26:25 +01001144 msg_flags = messages_flags_new;
1145 msg_wait = messages_wait_new;
1146
1147 msg_hist_max = messages_history_new;
1148 check_msg_hist();
1149
1150 return OK;
1151}
1152
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00001153/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 * ":messages" command.
1155 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 void
Bram Moolenaar52196b22016-04-14 17:52:41 +02001157ex_messages(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158{
1159 struct msg_hist *p;
1160 char_u *s;
Bram Moolenaar451f8492016-04-14 17:16:22 +02001161 int c = 0;
1162
1163 if (STRCMP(eap->arg, "clear") == 0)
1164 {
1165 int keep = eap->addr_count == 0 ? 0 : eap->line2;
1166
1167 while (msg_hist_len > keep)
1168 (void)delete_first_msg();
1169 return;
1170 }
1171
1172 if (*eap->arg != NUL)
1173 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001174 emsg(_(e_invalid_argument));
Bram Moolenaar451f8492016-04-14 17:16:22 +02001175 return;
1176 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177
1178 msg_hist_off = TRUE;
1179
Bram Moolenaar451f8492016-04-14 17:16:22 +02001180 p = first_msg_hist;
Bram Moolenaar451f8492016-04-14 17:16:22 +02001181 if (eap->addr_count != 0)
1182 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001183 // Count total messages
Bram Moolenaar451f8492016-04-14 17:16:22 +02001184 for (; p != NULL && !got_int; p = p->next)
1185 c++;
1186
1187 c -= eap->line2;
1188
Bram Moolenaar85a20022019-12-21 18:25:54 +01001189 // Skip without number of messages specified
Bram Moolenaar451f8492016-04-14 17:16:22 +02001190 for (p = first_msg_hist; p != NULL && !got_int && c > 0;
1191 p = p->next, c--);
1192 }
1193
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001194 if (p == first_msg_hist)
1195 {
Bram Moolenaar41f69182020-04-25 15:45:37 +02001196#ifdef FEAT_MULTI_LANG
1197 s = get_mess_lang();
1198#else
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001199 s = mch_getenv((char_u *)"LANG");
Bram Moolenaar41f69182020-04-25 15:45:37 +02001200#endif
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001201 if (s != NULL && *s != NUL)
Bram Moolenaar0c183192018-06-28 14:54:43 +02001202 // The next comment is extracted by xgettext and put in po file for
1203 // translators to read.
Bram Moolenaar32526b32019-01-19 17:43:09 +01001204 msg_attr(
Bram Moolenaar0c183192018-06-28 14:54:43 +02001205 // Translator: Please replace the name and email address
1206 // with the appropriate text for your translation.
Christian Brabandte978b452023-08-13 10:33:05 +02001207 _("Messages maintainer: The Vim Project"),
Bram Moolenaar8820b482017-03-16 17:23:31 +01001208 HL_ATTR(HLF_T));
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001209 }
1210
Bram Moolenaar85a20022019-12-21 18:25:54 +01001211 // Display what was not skipped.
Bram Moolenaar451f8492016-04-14 17:16:22 +02001212 for (; p != NULL && !got_int; p = p->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 if (p->msg != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001214 msg_attr((char *)p->msg, p->attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215
1216 msg_hist_off = FALSE;
1217}
1218
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001219#if defined(FEAT_CON_DIALOG) || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220/*
1221 * Call this after prompting the user. This will avoid a hit-return message
1222 * and a delay.
1223 */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001224 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001225msg_end_prompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226{
1227 need_wait_return = FALSE;
1228 emsg_on_display = FALSE;
1229 cmdline_row = msg_row;
1230 msg_col = 0;
1231 msg_clr_eos();
Bram Moolenaar5d6f75e2011-12-30 14:14:29 +01001232 lines_left = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233}
1234#endif
1235
1236/*
Bram Moolenaar32526b32019-01-19 17:43:09 +01001237 * Wait for the user to hit a key (normally Enter).
1238 * If "redraw" is TRUE, clear and redraw the screen.
1239 * If "redraw" is FALSE, just redraw the screen.
1240 * If "redraw" is -1, don't redraw at all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 */
1242 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001243wait_return(int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244{
1245 int c;
1246 int oldState;
1247 int tmpState;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 int had_got_int;
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001249 int save_reg_recording;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001250 FILE *save_scriptout;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251
1252 if (redraw == TRUE)
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01001253 set_must_redraw(UPD_CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254
Bram Moolenaar85a20022019-12-21 18:25:54 +01001255 // If using ":silent cmd", don't wait for a return. Also don't set
1256 // need_wait_return to do it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 if (msg_silent != 0)
1258 return;
Bram Moolenaarcf0995d2022-09-11 21:36:17 +01001259#ifdef HAS_MESSAGE_WINDOW
1260 if (in_echowindow)
1261 return;
1262#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01001264 /*
1265 * When inside vgetc(), we can't wait for a typed character at all.
1266 * With the global command (and some others) we only need one return at
1267 * the end. Adjust cmdline_row to avoid the next message overwriting the
1268 * last one.
1269 */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00001270 if (vgetc_busy > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 return;
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01001272 need_wait_return = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 if (no_wait_return)
1274 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 if (!exmode_active)
1276 cmdline_row = msg_row;
1277 return;
1278 }
1279
Bram Moolenaar85a20022019-12-21 18:25:54 +01001280 redir_off = TRUE; // don't redirect this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 oldState = State;
1282 if (quit_more)
1283 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001284 c = CAR; // just pretend CR was hit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 quit_more = FALSE;
1286 got_int = FALSE;
1287 }
1288 else if (exmode_active)
1289 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001290 msg_puts(" "); // make sure the cursor is on the right line
1291 c = CAR; // no need for a return in ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 got_int = FALSE;
1293 }
1294 else
1295 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001296 // Make sure the hit-return prompt is on screen when 'guioptions' was
1297 // just changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 screenalloc(FALSE);
1299
Bram Moolenaar24959102022-05-07 20:01:16 +01001300 State = MODE_HITRETURN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar85a20022019-12-21 18:25:54 +01001303 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304#endif
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01001305 cmdline_row = msg_row;
1306
Bram Moolenaar85a20022019-12-21 18:25:54 +01001307 // Avoid the sequence that the user types ":" at the hit-return prompt
1308 // to start an Ex command, but the file-changed dialog gets in the
1309 // way.
Bram Moolenaarec38d692013-04-24 15:12:32 +02001310 if (need_check_timestamps)
1311 check_timestamps(FALSE);
1312
Christian Brabandt51d4d842024-12-06 17:26:25 +01001313 if (msg_flags & MESSAGES_HIT_ENTER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 {
Christian Brabandt51d4d842024-12-06 17:26:25 +01001315 hit_return_msg();
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001316
Christian Brabandt51d4d842024-12-06 17:26:25 +01001317 do
1318 {
1319 // Remember "got_int", if it is set vgetc() probably returns a
1320 // CTRL-C, but we need to loop then.
1321 had_got_int = got_int;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001322
Christian Brabandt51d4d842024-12-06 17:26:25 +01001323 // Don't do mappings here, we put the character back in the
1324 // typeahead buffer.
1325 ++no_mapping;
1326 ++allow_keys;
1327
1328 // Temporarily disable Recording. If Recording is active, the
1329 // character will be recorded later, since it will be added to the
1330 // typebuf after the loop
1331 save_reg_recording = reg_recording;
1332 save_scriptout = scriptout;
1333 reg_recording = 0;
1334 scriptout = NULL;
1335 c = safe_vgetc();
1336 if (had_got_int && !global_busy)
1337 got_int = FALSE;
1338 --no_mapping;
1339 --allow_keys;
1340 reg_recording = save_reg_recording;
1341 scriptout = save_scriptout;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001342
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343#ifdef FEAT_CLIPBOARD
Christian Brabandt51d4d842024-12-06 17:26:25 +01001344 // Strange way to allow copying (yanking) a modeless selection at
1345 // the hit-enter prompt. Use CTRL-Y, because the same is used in
1346 // Cmdline-mode and it's harmless when there is no selection.
1347 if (c == Ctrl_Y && clip_star.state == SELECT_DONE)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001348 {
Christian Brabandt51d4d842024-12-06 17:26:25 +01001349 clip_copy_modeless_selection(TRUE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001350 c = K_IGNORE;
Christian Brabandt51d4d842024-12-06 17:26:25 +01001351 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352#endif
Bram Moolenaar88456cd2022-11-18 22:14:09 +00001353
Christian Brabandt51d4d842024-12-06 17:26:25 +01001354 /*
1355 * Allow scrolling back in the messages.
1356 * Also accept scroll-down commands when messages fill the screen,
1357 * to avoid that typing one 'j' too many makes the messages
1358 * disappear.
1359 */
1360 if (p_more && !p_cp)
1361 {
1362 if (c == 'b' || c == 'k' || c == 'u' || c == 'g'
1363 || c == K_UP || c == K_PAGEUP)
1364 {
1365 if (msg_scrolled > Rows)
1366 // scroll back to show older messages
1367 do_more_prompt(c);
1368 else
1369 {
1370 msg_didout = FALSE;
1371 c = K_IGNORE;
1372 msg_col =
1373#ifdef FEAT_RIGHTLEFT
1374 cmdmsg_rl ? Columns - 1 :
1375#endif
1376 0;
1377 }
1378 if (quit_more)
1379 {
1380 c = CAR; // just pretend CR was hit
1381 quit_more = FALSE;
1382 got_int = FALSE;
1383 }
1384 else if (c != K_IGNORE)
1385 {
1386 c = K_IGNORE;
1387 hit_return_msg();
1388 }
1389 }
1390 else if (msg_scrolled > Rows - 2
1391 && (c == 'j' || c == 'd' || c == 'f'
1392 || c == K_DOWN || c == K_PAGEDOWN))
1393 c = K_IGNORE;
1394 }
1395 } while ((had_got_int && c == Ctrl_C)
1396 || c == K_IGNORE
1397#ifdef FEAT_GUI
1398 || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR
1399#endif
1400 || c == K_LEFTDRAG || c == K_LEFTRELEASE
1401 || c == K_MIDDLEDRAG || c == K_MIDDLERELEASE
1402 || c == K_RIGHTDRAG || c == K_RIGHTRELEASE
1403 || c == K_MOUSELEFT || c == K_MOUSERIGHT
1404 || c == K_MOUSEDOWN || c == K_MOUSEUP
1405 || c == K_MOUSEMOVE
1406 || (!mouse_has(MOUSE_RETURN)
1407 && mouse_row < msg_row
1408 && (c == K_LEFTMOUSE
1409 || c == K_MIDDLEMOUSE
1410 || c == K_RIGHTMOUSE
1411 || c == K_X1MOUSE
1412 || c == K_X2MOUSE))
1413 );
1414 ui_breakcheck();
1415
1416 // Avoid that the mouse-up event causes Visual mode to start.
1417 if (c == K_LEFTMOUSE || c == K_MIDDLEMOUSE || c == K_RIGHTMOUSE
1418 || c == K_X1MOUSE || c == K_X2MOUSE)
1419 (void)jump_to_mouse(MOUSE_SETPOS, NULL, 0);
1420 else if (vim_strchr((char_u *)"\r\n ", c) == NULL && c != Ctrl_C)
1421 {
1422 // Put the character back in the typeahead buffer. Don't use the
1423 // stuff buffer, because lmaps wouldn't work.
1424 ins_char_typebuf(vgetc_char, vgetc_mod_mask);
1425 do_redraw = TRUE; // need a redraw even though there is
1426 // typeahead
1427 }
1428 }
1429 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430 {
Christian Brabandt51d4d842024-12-06 17:26:25 +01001431 c = CAR;
1432 // Wait to allow the user to verify the output.
1433 do_sleep(msg_wait, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435 }
1436 redir_off = FALSE;
1437
1438 /*
1439 * If the user hits ':', '?' or '/' we get a command line from the next
1440 * line.
1441 */
1442 if (c == ':' || c == '?' || c == '/')
1443 {
1444 if (!exmode_active)
1445 cmdline_row = msg_row;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001446 skip_redraw = TRUE; // skip redraw once
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 do_redraw = FALSE;
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02001448#ifdef FEAT_TERMINAL
1449 skip_term_loop = TRUE;
1450#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 }
1452
1453 /*
1454 * If the window size changed set_shellsize() will redraw the screen.
1455 * Otherwise the screen is only redrawn if 'redraw' is set and no ':'
1456 * typed.
1457 */
1458 tmpState = State;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001459 State = oldState; // restore State before set_shellsize
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 msg_check();
1462
1463#if defined(UNIX) || defined(VMS)
1464 /*
1465 * When switching screens, we need to output an extra newline on exit.
1466 */
1467 if (swapping_screen() && !termcap_active)
1468 newline_on_exit = TRUE;
1469#endif
1470
1471 need_wait_return = FALSE;
1472 did_wait_return = TRUE;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001473 emsg_on_display = FALSE; // can delete error message now
1474 lines_left = -1; // reset lines_left at next msg_start()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 reset_last_sourcing();
1476 if (keep_msg != NULL && vim_strsize(keep_msg) >=
1477 (Rows - cmdline_row - 1) * Columns + sc_col)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001478 VIM_CLEAR(keep_msg); // don't redisplay message, it's too long
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479
Bram Moolenaar24959102022-05-07 20:01:16 +01001480 if (tmpState == MODE_SETWSIZE) // got resize event while in vgetc()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001482 starttermcap(); // start termcap before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 shell_resized();
1484 }
1485 else if (!skip_redraw
1486 && (redraw == TRUE || (msg_scrolled != 0 && redraw != -1)))
1487 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001488 starttermcap(); // start termcap before redrawing
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001489 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 }
1491}
1492
1493/*
1494 * Write the hit-return prompt.
1495 */
1496 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001497hit_return_msg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001499 int save_p_more = p_more;
1500
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001501 p_more = FALSE; // don't want to see this message when scrolling back
Bram Moolenaar85a20022019-12-21 18:25:54 +01001502 if (msg_didout) // start on a new line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 msg_putchar('\n');
1504 if (got_int)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001505 msg_puts(_("Interrupt: "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506
Bram Moolenaar32526b32019-01-19 17:43:09 +01001507 msg_puts_attr(_("Press ENTER or type command to continue"), HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 if (!msg_use_printf())
1509 msg_clr_eos();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001510 p_more = save_p_more;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511}
1512
1513/*
1514 * Set "keep_msg" to "s". Free the old value and check for NULL pointer.
1515 */
1516 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001517set_keep_msg(char_u *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518{
1519 vim_free(keep_msg);
1520 if (s != NULL && msg_silent == 0)
1521 keep_msg = vim_strsave(s);
1522 else
1523 keep_msg = NULL;
Bram Moolenaaraab21c32005-01-25 21:46:35 +00001524 keep_msg_more = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001525 keep_msg_attr = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526}
1527
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001528/*
1529 * If there currently is a message being displayed, set "keep_msg" to it, so
1530 * that it will be displayed again after redraw.
1531 */
1532 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001533set_keep_msg_from_hist(void)
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001534{
1535 if (keep_msg == NULL && last_msg_hist != NULL && msg_scrolled == 0
Bram Moolenaar24959102022-05-07 20:01:16 +01001536 && (State & MODE_NORMAL))
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001537 set_keep_msg(last_msg_hist->msg, last_msg_hist->attr);
1538}
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001539
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540/*
1541 * Prepare for outputting characters in the command line.
1542 */
1543 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001544msg_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545{
1546 int did_return = FALSE;
1547
zeertzjq40ed6712023-11-23 20:37:01 +01001548 if (msg_row < cmdline_row)
1549 msg_row = cmdline_row;
1550
Bram Moolenaare4ce65d2010-08-04 20:12:32 +02001551 if (!msg_silent)
Rob Pilling726f7f92022-01-20 14:44:38 +00001552 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001553 VIM_CLEAR(keep_msg);
Rob Pilling726f7f92022-01-20 14:44:38 +00001554 need_fileinfo = FALSE;
1555 }
Bram Moolenaara7241f52008-06-24 20:39:31 +00001556
1557#ifdef FEAT_EVAL
Bram Moolenaar3b474dc2022-09-01 17:01:32 +01001558 if (need_clr_eos)
Bram Moolenaara7241f52008-06-24 20:39:31 +00001559 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001560 // Halfway an ":echo" command and getting an (error) message: clear
1561 // any text from the command.
Bram Moolenaara7241f52008-06-24 20:39:31 +00001562 need_clr_eos = FALSE;
1563 msg_clr_eos();
1564 }
1565#endif
1566
Bram Moolenaard54af2e2022-08-27 22:05:13 +01001567#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaara2a89732022-08-31 14:46:18 +01001568 if (in_echowindow)
Bram Moolenaar9198de32022-08-27 21:30:03 +01001569 {
Bram Moolenaar37fef162022-08-29 18:16:32 +01001570 if (popup_message_win_visible()
1571 && ((msg_col > 0 && (msg_scroll || !full_screen))
1572 || in_echowindow))
Bram Moolenaar9198de32022-08-27 21:30:03 +01001573 {
1574 win_T *wp = popup_get_message_win();
1575
Bram Moolenaarf2fb54f2022-08-28 20:58:51 +01001576 // start a new line
Bram Moolenaar9198de32022-08-27 21:30:03 +01001577 curbuf = wp->w_buffer;
1578 ml_append(wp->w_buffer->b_ml.ml_line_count,
1579 (char_u *)"", (colnr_T)0, FALSE);
1580 curbuf = curwin->w_buffer;
1581 }
1582 msg_col = 0;
1583 }
Bram Moolenaard54af2e2022-08-27 22:05:13 +01001584 else
1585#endif
1586 if (!msg_scroll && full_screen) // overwrite last message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 {
1588 msg_row = cmdline_row;
1589 msg_col =
1590#ifdef FEAT_RIGHTLEFT
1591 cmdmsg_rl ? Columns - 1 :
1592#endif
1593 0;
1594 }
Bram Moolenaara2a89732022-08-31 14:46:18 +01001595 else if (msg_didout || in_echowindow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 {
Bram Moolenaar309c4e02022-08-29 12:23:39 +01001597 // start message on next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 msg_putchar('\n');
1599 did_return = TRUE;
1600 if (exmode_active != EXMODE_NORMAL)
1601 cmdline_row = msg_row;
1602 }
1603 if (!msg_didany || lines_left < 0)
1604 msg_starthere();
1605 if (msg_silent == 0)
1606 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001607 msg_didout = FALSE; // no output on current line yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 cursor_off();
1609 }
1610
Bram Moolenaar85a20022019-12-21 18:25:54 +01001611 // when redirecting, may need to start a new line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 if (!did_return)
1613 redir_write((char_u *)"\n", -1);
1614}
1615
1616/*
1617 * Note that the current msg position is where messages start.
1618 */
1619 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001620msg_starthere(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621{
1622 lines_left = cmdline_row;
1623 msg_didany = FALSE;
1624}
1625
1626 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001627msg_putchar(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628{
1629 msg_putchar_attr(c, 0);
1630}
1631
1632 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001633msg_putchar_attr(int c, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634{
Bram Moolenaar63c0ccd2019-01-19 21:06:58 +01001635 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636
1637 if (IS_SPECIAL(c))
1638 {
1639 buf[0] = K_SPECIAL;
1640 buf[1] = K_SECOND(c);
1641 buf[2] = K_THIRD(c);
1642 buf[3] = NUL;
1643 }
1644 else
Bram Moolenaar63c0ccd2019-01-19 21:06:58 +01001645 buf[(*mb_char2bytes)(c, buf)] = NUL;
Bram Moolenaar63c0ccd2019-01-19 21:06:58 +01001646 msg_puts_attr((char *)buf, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647}
1648
1649 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001650msg_outnum(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001652 char buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653
Bram Moolenaar32526b32019-01-19 17:43:09 +01001654 sprintf(buf, "%ld", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655 msg_puts(buf);
1656}
1657
1658 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001659msg_home_replace(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660{
1661 msg_home_replace_attr(fname, 0);
1662}
1663
1664#if defined(FEAT_FIND_ID) || defined(PROTO)
1665 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001666msg_home_replace_hl(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667{
Bram Moolenaar8820b482017-03-16 17:23:31 +01001668 msg_home_replace_attr(fname, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669}
1670#endif
1671
1672 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001673msg_home_replace_attr(char_u *fname, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674{
1675 char_u *name;
1676
1677 name = home_replace_save(NULL, fname);
1678 if (name != NULL)
1679 msg_outtrans_attr(name, attr);
1680 vim_free(name);
1681}
1682
1683/*
1684 * Output 'len' characters in 'str' (including NULs) with translation
Bram Moolenaarf48ee3c2019-12-06 22:18:20 +01001685 * if 'len' is -1, output up to a NUL character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 * Use attributes 'attr'.
1687 * Return the number of characters it takes on the screen.
1688 */
1689 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001690msg_outtrans(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691{
1692 return msg_outtrans_attr(str, 0);
1693}
1694
1695 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001696msg_outtrans_attr(char_u *str, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697{
1698 return msg_outtrans_len_attr(str, (int)STRLEN(str), attr);
1699}
1700
1701 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001702msg_outtrans_len(char_u *str, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703{
1704 return msg_outtrans_len_attr(str, len, 0);
1705}
1706
1707/*
1708 * Output one character at "p". Return pointer to the next character.
1709 * Handles multi-byte characters.
1710 */
1711 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001712msg_outtrans_one(char_u *p, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 int l;
1715
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001716 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 {
1718 msg_outtrans_len_attr(p, l, attr);
1719 return p + l;
1720 }
cero19881d87e112023-02-16 15:03:12 +00001721 msg_puts_attr((char *)transchar_byte_buf(NULL, *p), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 return p + 1;
1723}
1724
1725 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001726msg_outtrans_len_attr(char_u *msgstr, int len, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727{
1728 int retval = 0;
1729 char_u *str = msgstr;
1730 char_u *plain_start = msgstr;
1731 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 int mb_l;
1733 int c;
Bram Moolenaar3d30af82020-10-13 22:15:56 +02001734 int save_got_int = got_int;
1735
1736 // Only quit when got_int was set in here.
1737 got_int = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738
Shougo Matsushitabe2b03c2024-04-08 22:11:50 +02001739 if (attr == 0)
1740 attr = HL_ATTR(HLF_MSG);
1741
Bram Moolenaar85a20022019-12-21 18:25:54 +01001742 // if MSG_HIST flag set, add message to history
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 if (attr & MSG_HIST)
1744 {
1745 add_msg_hist(str, len, attr);
1746 attr &= ~MSG_HIST;
1747 }
1748
Bram Moolenaar800cdbb2023-06-15 16:40:02 +01001749 // When drawing over the command line no need to clear it later or remove
1750 // the mode message.
Bram Moolenaarda51ad52023-06-15 18:44:50 +01001751 if (msg_row >= cmdline_row && msg_col == 0)
Bram Moolenaar800cdbb2023-06-15 16:40:02 +01001752 {
1753 clear_cmdline = FALSE;
1754 mode_displayed = FALSE;
1755 }
1756
Bram Moolenaar85a20022019-12-21 18:25:54 +01001757 // If the string starts with a composing character first draw a space on
1758 // which the composing char can be drawn.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 if (enc_utf8 && utf_iscomposing(utf_ptr2char(msgstr)))
Bram Moolenaar32526b32019-01-19 17:43:09 +01001760 msg_puts_attr(" ", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761
1762 /*
1763 * Go over the string. Special characters are translated and printed.
1764 * Normal characters are printed several at a time.
1765 */
Bram Moolenaar3d30af82020-10-13 22:15:56 +02001766 while (--len >= 0 && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 if (enc_utf8)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001769 // Don't include composing chars after the end.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001770 mb_l = utfc_ptr2len_len(str, len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771 else if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001772 mb_l = (*mb_ptr2len)(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 else
1774 mb_l = 1;
1775 if (has_mbyte && mb_l > 1)
1776 {
1777 c = (*mb_ptr2char)(str);
1778 if (vim_isprintc(c))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001779 // printable multi-byte char: count the cells.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 retval += (*mb_ptr2cells)(str);
1781 else
1782 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001783 // unprintable multi-byte char: print the printable chars so
1784 // far and the translation of the unprintable char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 if (str > plain_start)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001786 msg_puts_attr_len((char *)plain_start,
1787 (int)(str - plain_start), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 plain_start = str + mb_l;
cero19881d87e112023-02-16 15:03:12 +00001789 msg_puts_attr((char *)transchar_buf(NULL, c),
Bram Moolenaar32526b32019-01-19 17:43:09 +01001790 attr == 0 ? HL_ATTR(HLF_8) : attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 retval += char2cells(c);
1792 }
1793 len -= mb_l - 1;
1794 str += mb_l;
1795 }
1796 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 {
cero19881d87e112023-02-16 15:03:12 +00001798 s = transchar_byte_buf(NULL, *str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799 if (s[1] != NUL)
1800 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001801 // unprintable char: print the printable chars so far and the
1802 // translation of the unprintable char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 if (str > plain_start)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001804 msg_puts_attr_len((char *)plain_start,
1805 (int)(str - plain_start), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 plain_start = str + 1;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001807 msg_puts_attr((char *)s, attr == 0 ? HL_ATTR(HLF_8) : attr);
Bram Moolenaarc236c162008-07-13 17:41:49 +00001808 retval += (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 }
Bram Moolenaarb3c722a2008-07-06 17:16:02 +00001810 else
1811 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812 ++str;
1813 }
1814 }
1815
Bram Moolenaar3d30af82020-10-13 22:15:56 +02001816 if (str > plain_start && !got_int)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001817 // print the printable chars at the end
Bram Moolenaar32526b32019-01-19 17:43:09 +01001818 msg_puts_attr_len((char *)plain_start, (int)(str - plain_start), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819
Bram Moolenaar3d30af82020-10-13 22:15:56 +02001820 got_int |= save_got_int;
1821
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 return retval;
1823}
1824
1825#if defined(FEAT_QUICKFIX) || defined(PROTO)
1826 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001827msg_make(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828{
1829 int i;
1830 static char_u *str = (char_u *)"eeffoc", *rs = (char_u *)"Plon#dqg#vxjduB";
1831
1832 arg = skipwhite(arg);
1833 for (i = 5; *arg && i >= 0; --i)
1834 if (*arg++ != str[i])
1835 break;
1836 if (i < 0)
1837 {
1838 msg_putchar('\n');
1839 for (i = 0; rs[i]; ++i)
1840 msg_putchar(rs[i] - 3);
1841 }
1842}
1843#endif
1844
1845/*
Bram Moolenaarf48ee3c2019-12-06 22:18:20 +01001846 * Output the string 'str' up to a NUL character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 * Return the number of characters it takes on the screen.
1848 *
1849 * If K_SPECIAL is encountered, then it is taken in conjunction with the
1850 * following character and shown as <F1>, <S-Up> etc. Any other character
1851 * which is not printable shown in <> form.
1852 * If 'from' is TRUE (lhs of a mapping), a space is shown as <Space>.
1853 * If a character is displayed in one of these special ways, is also
1854 * highlighted (its highlight name is '8' in the p_hl variable).
1855 * Otherwise characters are not highlighted.
1856 * This function is used to show mappings, where we want to see how to type
1857 * the character/string -- webb
1858 */
1859 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001860msg_outtrans_special(
1861 char_u *strstart,
Bram Moolenaar725310d2019-04-24 23:08:23 +02001862 int from, // TRUE for lhs of a mapping
Bram Moolenaar32aa1022019-11-02 22:54:41 +01001863 int maxlen) // screen columns, 0 for unlimited
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864{
1865 char_u *str = strstart;
1866 int retval = 0;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001867 char *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 int attr;
1869 int len;
1870
Bram Moolenaar8820b482017-03-16 17:23:31 +01001871 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 while (*str != NUL)
1873 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001874 // Leading and trailing spaces need to be displayed in <> form.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 if ((str == strstart || str[1] == NUL) && *str == ' ')
1876 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001877 text = "<Space>";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 ++str;
1879 }
1880 else
zeertzjqcdc83932022-09-12 13:38:41 +01001881 text = (char *)str2special(&str, from, FALSE);
zeertzjq0519ce02022-05-09 12:16:19 +01001882 if (text[0] != NUL && text[1] == NUL)
1883 // single-byte character or illegal byte
cero19881d87e112023-02-16 15:03:12 +00001884 text = (char *)transchar_byte_buf(NULL, (char_u)text[0]);
Bram Moolenaar32526b32019-01-19 17:43:09 +01001885 len = vim_strsize((char_u *)text);
Bram Moolenaar725310d2019-04-24 23:08:23 +02001886 if (maxlen > 0 && retval + len >= maxlen)
1887 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001888 // Highlight special keys
Bram Moolenaar32526b32019-01-19 17:43:09 +01001889 msg_puts_attr(text, len > 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001890 && (*mb_ptr2len)((char_u *)text) <= 1 ? attr : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 retval += len;
1892 }
1893 return retval;
1894}
1895
Martin Tournoijba43e762022-10-13 22:12:15 +01001896#if defined(FEAT_EVAL) || defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaarbd743252010-10-20 21:23:33 +02001897/*
1898 * Return the lhs or rhs of a mapping, with the key codes turned into printable
1899 * strings, in an allocated string.
1900 */
1901 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001902str2special_save(
1903 char_u *str,
zeertzjqcdc83932022-09-12 13:38:41 +01001904 int replace_spaces, // TRUE to replace " " with "<Space>".
1905 // used for the lhs of mapping and keytrans().
1906 int replace_lt) // TRUE to replace "<" with "<lt>".
Bram Moolenaarbd743252010-10-20 21:23:33 +02001907{
1908 garray_T ga;
1909 char_u *p = str;
1910
1911 ga_init2(&ga, 1, 40);
1912 while (*p != NUL)
zeertzjqcdc83932022-09-12 13:38:41 +01001913 ga_concat(&ga, str2special(&p, replace_spaces, replace_lt));
Bram Moolenaarbd743252010-10-20 21:23:33 +02001914 ga_append(&ga, NUL);
1915 return (char_u *)ga.ga_data;
1916}
1917#endif
1918
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919/*
1920 * Return the printable string for the key codes at "*sp".
zeertzjq0519ce02022-05-09 12:16:19 +01001921 * On illegal byte return a string with only that byte.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 * Used for translating the lhs or rhs of a mapping to printable chars.
1923 * Advances "sp" to the next code.
1924 */
1925 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001926str2special(
1927 char_u **sp,
zeertzjqcdc83932022-09-12 13:38:41 +01001928 int replace_spaces, // TRUE to replace " " with "<Space>".
1929 // used for the lhs of mapping and keytrans().
1930 int replace_lt) // TRUE to replace "<" with "<lt>".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931{
1932 int c;
1933 static char_u buf[7];
1934 char_u *str = *sp;
1935 int modifiers = 0;
1936 int special = FALSE;
1937
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 if (has_mbyte)
1939 {
1940 char_u *p;
1941
Bram Moolenaar85a20022019-12-21 18:25:54 +01001942 // Try to un-escape a multi-byte character. Return the un-escaped
1943 // string if it is a multi-byte character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 p = mb_unescape(sp);
1945 if (p != NULL)
1946 return p;
1947 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948
1949 c = *str;
zeertzjq1bed9932023-08-30 19:38:24 +02001950 if ((c == K_SPECIAL
1951#ifdef FEAT_GUI
1952 || c == CSI
1953#endif
1954 ) && str[1] != NUL && str[2] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 {
1956 if (str[1] == KS_MODIFIER)
1957 {
1958 modifiers = str[2];
1959 str += 3;
1960 c = *str;
1961 }
zeertzjq1bed9932023-08-30 19:38:24 +02001962 if ((c == K_SPECIAL
1963#ifdef FEAT_GUI
1964 || c == CSI
1965#endif
1966 ) && str[1] != NUL && str[2] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 {
1968 c = TO_SPECIAL(str[1], str[2]);
1969 str += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001971 if (IS_SPECIAL(c) || modifiers) // special key
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 special = TRUE;
1973 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974
zeertzjq0519ce02022-05-09 12:16:19 +01001975 if (has_mbyte && !IS_SPECIAL(c) && MB_BYTE2LEN(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 {
zeertzjqac402f42022-05-04 18:51:43 +01001977 char_u *p;
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001978
zeertzjqac402f42022-05-04 18:51:43 +01001979 *sp = str;
1980 // Try to un-escape a multi-byte character after modifiers.
1981 p = mb_unescape(sp);
zeertzjq0519ce02022-05-09 12:16:19 +01001982 if (p != NULL)
1983 // Since 'special' is TRUE the multi-byte character 'c' will be
1984 // processed by get_special_key_name()
1985 c = (*mb_ptr2char)(p);
1986 else
1987 // illegal byte
1988 *sp = str + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 }
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001990 else
Bram Moolenaar083692d2022-06-29 21:16:58 +01001991 // single-byte character, NUL or illegal byte
1992 *sp = str + (*str == NUL ? 0 : 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993
zeertzjq0519ce02022-05-09 12:16:19 +01001994 // Make special keys and C0 control characters in <> form, also <M-Space>.
zeertzjqcdc83932022-09-12 13:38:41 +01001995 if (special
1996 || c < ' '
1997 || (replace_spaces && c == ' ')
1998 || (replace_lt && c == '<'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 return get_special_key_name(c, modifiers);
2000 buf[0] = c;
2001 buf[1] = NUL;
2002 return buf;
2003}
2004
2005/*
2006 * Translate a key sequence into special key names.
2007 */
2008 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002009str2specialbuf(char_u *sp, char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010{
2011 char_u *s;
2012
2013 *buf = NUL;
2014 while (*sp)
2015 {
zeertzjqcdc83932022-09-12 13:38:41 +01002016 s = str2special(&sp, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 if ((int)(STRLEN(s) + STRLEN(buf)) < len)
2018 STRCAT(buf, s);
2019 }
2020}
2021
2022/*
2023 * print line for :print or :list command
2024 */
2025 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002026msg_prt_line(char_u *s, int list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027{
2028 int c;
2029 int col = 0;
2030 int n_extra = 0;
2031 int c_extra = 0;
Bram Moolenaar83a52172019-01-16 22:41:54 +01002032 int c_final = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002033 char_u *p_extra = NULL; // init to make SASC shut up
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 int n;
Bram Moolenaar56da7972007-01-16 14:46:32 +00002035 int attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 char_u *trail = NULL;
Bram Moolenaar91478ae2021-02-03 15:58:13 +01002037 char_u *lead = NULL;
zeertzjqf14b8ba2021-09-10 16:58:30 +02002038 int in_multispace = FALSE;
2039 int multispace_pos = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 int l;
2041 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042
Bram Moolenaardf177f62005-02-22 08:39:57 +00002043 if (curwin->w_p_list)
2044 list = TRUE;
2045
Bram Moolenaar91478ae2021-02-03 15:58:13 +01002046 if (list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 {
Bram Moolenaar91478ae2021-02-03 15:58:13 +01002048 // find start of trailing whitespace
Bram Moolenaareed9d462021-02-15 20:38:25 +01002049 if (curwin->w_lcs_chars.trail)
Bram Moolenaar91478ae2021-02-03 15:58:13 +01002050 {
2051 trail = s + STRLEN(s);
2052 while (trail > s && VIM_ISWHITE(trail[-1]))
2053 --trail;
2054 }
2055 // find end of leading whitespace
Bram Moolenaarb8329db2022-07-06 13:31:28 +01002056 if (curwin->w_lcs_chars.lead
2057 || curwin->w_lcs_chars.leadmultispace != NULL)
Bram Moolenaar91478ae2021-02-03 15:58:13 +01002058 {
2059 lead = s;
2060 while (VIM_ISWHITE(lead[0]))
2061 lead++;
2062 // in a line full of spaces all of them are treated as trailing
2063 if (*lead == NUL)
2064 lead = NULL;
2065 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 }
2067
Bram Moolenaar85a20022019-12-21 18:25:54 +01002068 // output a space for an empty line, otherwise the line will be
2069 // overwritten
Bram Moolenaareed9d462021-02-15 20:38:25 +01002070 if (*s == NUL && !(list && curwin->w_lcs_chars.eol != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 msg_putchar(' ');
2072
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002073 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 {
Bram Moolenaar56da7972007-01-16 14:46:32 +00002075 if (n_extra > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 {
2077 --n_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01002078 if (n_extra == 0 && c_final)
2079 c = c_final;
2080 else if (c_extra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 c = c_extra;
2082 else
2083 c = *p_extra++;
2084 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002085 else if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 {
2087 col += (*mb_ptr2cells)(s);
Bram Moolenaar1cbfc992020-12-02 12:37:37 +01002088 if (l >= MB_MAXBYTES)
2089 {
Bram Moolenaar29d2f452020-12-04 19:42:52 +01002090 STRCPY(buf, "?");
Bram Moolenaar1cbfc992020-12-02 12:37:37 +01002091 }
Bram Moolenaareed9d462021-02-15 20:38:25 +01002092 else if (curwin->w_lcs_chars.nbsp != NUL && list
Bram Moolenaar73284b92015-05-04 17:28:22 +02002093 && (mb_ptr2char(s) == 160
2094 || mb_ptr2char(s) == 0x202f))
Bram Moolenaaracf17282011-02-01 17:12:25 +01002095 {
Bram Moolenaar74ac29c2022-06-15 12:12:44 +01002096 int len = mb_char2bytes(curwin->w_lcs_chars.nbsp, buf);
2097
2098 buf[len] = NUL;
Bram Moolenaaracf17282011-02-01 17:12:25 +01002099 }
2100 else
2101 {
2102 mch_memmove(buf, s, (size_t)l);
2103 buf[l] = NUL;
2104 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01002105 msg_puts((char *)buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 s += l;
2107 continue;
2108 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 else
2110 {
2111 attr = 0;
2112 c = *s++;
zeertzjqabc80812023-09-24 23:32:18 +02002113 if (list)
2114 {
2115 in_multispace = c == ' ' && (*s == ' '
2116 || (col > 0 && s[-2] == ' '));
2117 if (!in_multispace)
2118 multispace_pos = 0;
2119 }
Bram Moolenaareed9d462021-02-15 20:38:25 +01002120 if (c == TAB && (!list || curwin->w_lcs_chars.tab1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002122 // tab amount depends on current column
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002123#ifdef FEAT_VARTABS
2124 n_extra = tabstop_padding(col, curbuf->b_p_ts,
2125 curbuf->b_p_vts_array) - 1;
2126#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127 n_extra = curbuf->b_p_ts - col % curbuf->b_p_ts - 1;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002128#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00002129 if (!list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 {
2131 c = ' ';
2132 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01002133 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 }
2135 else
2136 {
Bram Moolenaareed9d462021-02-15 20:38:25 +01002137 c = (n_extra == 0 && curwin->w_lcs_chars.tab3)
2138 ? curwin->w_lcs_chars.tab3
2139 : curwin->w_lcs_chars.tab1;
2140 c_extra = curwin->w_lcs_chars.tab2;
2141 c_final = curwin->w_lcs_chars.tab3;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002142 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 }
2144 }
Bram Moolenaareed9d462021-02-15 20:38:25 +01002145 else if (c == 160 && list && curwin->w_lcs_chars.nbsp != NUL)
Bram Moolenaaracf17282011-02-01 17:12:25 +01002146 {
Bram Moolenaareed9d462021-02-15 20:38:25 +01002147 c = curwin->w_lcs_chars.nbsp;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002148 attr = HL_ATTR(HLF_8);
Bram Moolenaaracf17282011-02-01 17:12:25 +01002149 }
Bram Moolenaareed9d462021-02-15 20:38:25 +01002150 else if (c == NUL && list && curwin->w_lcs_chars.eol != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 {
2152 p_extra = (char_u *)"";
2153 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01002154 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 n_extra = 1;
Bram Moolenaareed9d462021-02-15 20:38:25 +01002156 c = curwin->w_lcs_chars.eol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002157 attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 --s;
2159 }
2160 else if (c != NUL && (n = byte2cells(c)) > 1)
2161 {
2162 n_extra = n - 1;
cero19881d87e112023-02-16 15:03:12 +00002163 p_extra = transchar_byte_buf(NULL, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01002165 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 c = *p_extra++;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002167 // Use special coloring to be able to distinguish <hex> from
2168 // the same in plain text.
Bram Moolenaar8820b482017-03-16 17:23:31 +01002169 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 }
zeertzjqf14b8ba2021-09-10 16:58:30 +02002171 else if (c == ' ')
Bram Moolenaar91478ae2021-02-03 15:58:13 +01002172 {
zeertzjqabc80812023-09-24 23:32:18 +02002173 if (lead != NULL && s <= lead && in_multispace
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01002174 && curwin->w_lcs_chars.leadmultispace != NULL)
2175 {
2176 c = curwin->w_lcs_chars.leadmultispace[multispace_pos++];
zeertzjqb5f08012022-06-09 13:55:28 +01002177 if (curwin->w_lcs_chars.leadmultispace[multispace_pos]
2178 == NUL)
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01002179 multispace_pos = 0;
2180 attr = HL_ATTR(HLF_8);
2181 }
zeertzjqb5f08012022-06-09 13:55:28 +01002182 else if (lead != NULL && s <= lead
2183 && curwin->w_lcs_chars.lead != NUL)
zeertzjqf14b8ba2021-09-10 16:58:30 +02002184 {
2185 c = curwin->w_lcs_chars.lead;
2186 attr = HL_ATTR(HLF_8);
2187 }
2188 else if (trail != NULL && s > trail)
2189 {
2190 c = curwin->w_lcs_chars.trail;
2191 attr = HL_ATTR(HLF_8);
2192 }
zeertzjqabc80812023-09-24 23:32:18 +02002193 else if (in_multispace
zeertzjqf14b8ba2021-09-10 16:58:30 +02002194 && curwin->w_lcs_chars.multispace != NULL)
2195 {
2196 c = curwin->w_lcs_chars.multispace[multispace_pos++];
2197 if (curwin->w_lcs_chars.multispace[multispace_pos] == NUL)
2198 multispace_pos = 0;
2199 attr = HL_ATTR(HLF_8);
2200 }
2201 else if (list && curwin->w_lcs_chars.space != NUL)
2202 {
2203 c = curwin->w_lcs_chars.space;
2204 attr = HL_ATTR(HLF_8);
2205 }
Bram Moolenaar1510f992015-04-22 22:18:22 +02002206 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207 }
2208
2209 if (c == NUL)
2210 break;
2211
2212 msg_putchar_attr(c, attr);
2213 col++;
2214 }
2215 msg_clr_eos();
2216}
2217
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218/*
2219 * Use screen_puts() to output one multi-byte character.
2220 * Return the pointer "s" advanced to the next character.
2221 */
2222 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002223screen_puts_mbyte(char_u *s, int l, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224{
2225 int cw;
2226
Bram Moolenaar85a20022019-12-21 18:25:54 +01002227 msg_didout = TRUE; // remember that line is not empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 cw = (*mb_ptr2cells)(s);
2229 if (cw > 1 && (
2230#ifdef FEAT_RIGHTLEFT
2231 cmdmsg_rl ? msg_col <= 1 :
2232#endif
2233 msg_col == Columns - 1))
2234 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002235 // Doesn't fit, print a highlighted '>' to fill it up.
Bram Moolenaar8820b482017-03-16 17:23:31 +01002236 msg_screen_putchar('>', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237 return s;
2238 }
2239
2240 screen_puts_len(s, l, msg_row, msg_col, attr);
2241#ifdef FEAT_RIGHTLEFT
2242 if (cmdmsg_rl)
2243 {
2244 msg_col -= cw;
2245 if (msg_col == 0)
2246 {
2247 msg_col = Columns;
2248 ++msg_row;
2249 }
2250 }
2251 else
2252#endif
2253 {
2254 msg_col += cw;
2255 if (msg_col >= Columns)
2256 {
2257 msg_col = 0;
2258 ++msg_row;
2259 }
2260 }
2261 return s + l;
2262}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263
2264/*
2265 * Output a string to the screen at position msg_row, msg_col.
2266 * Update msg_row and msg_col for the next message.
2267 */
2268 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002269msg_puts(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270{
Bram Moolenaaraeac9002016-09-06 22:15:08 +02002271 msg_puts_attr(s, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272}
2273
2274 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002275msg_puts_title(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276{
Bram Moolenaar8820b482017-03-16 17:23:31 +01002277 msg_puts_attr(s, HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278}
2279
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280/*
2281 * Show a message in such a way that it always fits in the line. Cut out a
2282 * part in the middle and replace it with "..." when necessary.
2283 * Does not handle multi-byte characters!
2284 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002285 static void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002286msg_outtrans_long_len_attr(char_u *longstr, int len, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287{
2288 int slen = len;
2289 int room;
2290
2291 room = Columns - msg_col;
2292 if (len > room && room >= 20)
2293 {
2294 slen = (room - 3) / 2;
2295 msg_outtrans_len_attr(longstr, slen, attr);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002296 msg_puts_attr("...", HL_ATTR(HLF_8));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 }
2298 msg_outtrans_len_attr(longstr + len - slen, slen, attr);
2299}
2300
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002301 void
2302msg_outtrans_long_attr(char_u *longstr, int attr)
2303{
2304 msg_outtrans_long_len_attr(longstr, (int)STRLEN(longstr), attr);
2305}
2306
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307/*
2308 * Basic function for writing a message with highlight attributes.
2309 */
2310 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002311msg_puts_attr(char *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312{
2313 msg_puts_attr_len(s, -1, attr);
2314}
2315
2316/*
2317 * Like msg_puts_attr(), but with a maximum length "maxlen" (in bytes).
2318 * When "maxlen" is -1 there is no maximum length.
2319 * When "maxlen" is >= 0 the message is not put in the history.
2320 */
2321 static void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002322msg_puts_attr_len(char *str, int maxlen, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 /*
2325 * If redirection is on, also write to the redirection file.
2326 */
Bram Moolenaar32526b32019-01-19 17:43:09 +01002327 redir_write((char_u *)str, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328
2329 /*
2330 * Don't print anything when using ":silent cmd".
2331 */
2332 if (msg_silent != 0)
2333 return;
2334
Shougo Matsushitabe2b03c2024-04-08 22:11:50 +02002335 if (attr == 0)
2336 attr = HL_ATTR(HLF_MSG);
2337
Bram Moolenaar85a20022019-12-21 18:25:54 +01002338 // if MSG_HIST flag set, add message to history
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 if ((attr & MSG_HIST) && maxlen < 0)
2340 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002341 add_msg_hist((char_u *)str, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 attr &= ~MSG_HIST;
2343 }
2344
Bram Moolenaare8070982019-10-12 17:07:06 +02002345 // When writing something to the screen after it has scrolled, requires a
2346 // wait-return prompt later. Needed when scrolling, resetting
2347 // need_wait_return after some prompt, and then outputting something
2348 // without scrolling
2349 // Not needed when only using CR to move the cursor.
2350 if (msg_scrolled != 0 && !msg_scrolled_ign && STRCMP(str, "\r") != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 need_wait_return = TRUE;
Bram Moolenaare8070982019-10-12 17:07:06 +02002352 msg_didany = TRUE; // remember that something was outputted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353
2354 /*
2355 * If there is no valid screen, use fprintf so we can see error messages.
2356 * If termcap is not active, we may be writing in an alternate console
2357 * window, cursor positioning may not work correctly (window size may be
2358 * different, e.g. for Win32 console) or we just don't know where the
2359 * cursor is.
2360 */
2361 if (msg_use_printf())
Bram Moolenaar32526b32019-01-19 17:43:09 +01002362 msg_puts_printf((char_u *)str, maxlen);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002363 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01002364 msg_puts_display((char_u *)str, maxlen, attr, FALSE);
Rob Pilling726f7f92022-01-20 14:44:38 +00002365
2366 need_fileinfo = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002367}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368
Bram Moolenaar9198de32022-08-27 21:30:03 +01002369// values for "where"
2370#define PUT_APPEND 0 // append to "lnum"
2371#define PUT_TRUNC 1 // replace "lnum"
2372#define PUT_BELOW 2 // add below "lnum"
2373 //
2374#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaar9198de32022-08-27 21:30:03 +01002375/*
Bram Moolenaar24735f22022-08-30 15:44:22 +01002376 * Put text "t_s" until "end" in the message window.
Bram Moolenaar9198de32022-08-27 21:30:03 +01002377 * "where" specifies where to put the text.
2378 */
2379 static void
2380put_msg_win(win_T *wp, int where, char_u *t_s, char_u *end, linenr_T lnum)
2381{
Bram Moolenaarbeedd0a2022-08-27 21:52:52 +01002382 char_u *p;
Bram Moolenaar9198de32022-08-27 21:30:03 +01002383
Bram Moolenaar9198de32022-08-27 21:30:03 +01002384 if (where == PUT_BELOW)
Bram Moolenaarbeedd0a2022-08-27 21:52:52 +01002385 {
2386 if (*end != NUL)
2387 {
2388 p = vim_strnsave(t_s, end - t_s);
2389 if (p == NULL)
2390 return;
2391 }
2392 else
2393 p = t_s;
2394 ml_append_buf(wp->w_buffer, lnum, p, (colnr_T)0, FALSE);
2395 if (p != t_s)
2396 vim_free(p);
2397 }
Bram Moolenaar9198de32022-08-27 21:30:03 +01002398 else
2399 {
2400 char_u *newp;
2401
2402 curbuf = wp->w_buffer;
2403 if (where == PUT_APPEND)
Bram Moolenaarbeedd0a2022-08-27 21:52:52 +01002404 {
Bram Moolenaar9198de32022-08-27 21:30:03 +01002405 newp = concat_str(ml_get(lnum), t_s);
Bram Moolenaarbeedd0a2022-08-27 21:52:52 +01002406 if (newp == NULL)
2407 return;
2408 if (*end != NUL)
2409 newp[STRLEN(ml_get(lnum)) + (end - t_s)] = NUL;
2410 }
Bram Moolenaar9198de32022-08-27 21:30:03 +01002411 else
Bram Moolenaarbeedd0a2022-08-27 21:52:52 +01002412 {
Bram Moolenaar9198de32022-08-27 21:30:03 +01002413 newp = vim_strnsave(t_s, end - t_s);
Bram Moolenaarbeedd0a2022-08-27 21:52:52 +01002414 if (newp == NULL)
2415 return;
2416 }
Bram Moolenaar9198de32022-08-27 21:30:03 +01002417 ml_replace(lnum, newp, FALSE);
2418 curbuf = curwin->w_buffer;
2419 }
2420 redraw_win_later(wp, UPD_NOT_VALID);
2421
2422 // set msg_col so that a newline is written if needed
Bram Moolenaar24735f22022-08-30 15:44:22 +01002423 msg_col += (int)(end - t_s);
Bram Moolenaar9198de32022-08-27 21:30:03 +01002424}
2425#endif
2426
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002427/*
2428 * The display part of msg_puts_attr_len().
2429 * May be called recursively to display scroll-back text.
2430 */
2431 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002432msg_puts_display(
2433 char_u *str,
2434 int maxlen,
2435 int attr,
2436 int recurse)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002437{
2438 char_u *s = str;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002439 char_u *t_s = str; // string from "t_s" to "s" is still todo
2440 int t_col = 0; // screen cells todo, 0 when "t_s" not used
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002441 int l;
2442 int cw;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002443 char_u *sb_str = str;
2444 int sb_col = msg_col;
2445 int wrap;
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002446 int did_last_char;
Bram Moolenaar9198de32022-08-27 21:30:03 +01002447#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaard54af2e2022-08-27 22:05:13 +01002448 int where = PUT_APPEND;
Bram Moolenaar9198de32022-08-27 21:30:03 +01002449 win_T *msg_win = NULL;
2450 linenr_T lnum = 1;
2451
Bram Moolenaara2a89732022-08-31 14:46:18 +01002452 if (in_echowindow)
Bram Moolenaar9198de32022-08-27 21:30:03 +01002453 {
2454 msg_win = popup_get_message_win();
2455
2456 if (msg_win != NULL)
2457 {
2458 if (!popup_message_win_visible())
2459 {
2460 if (*str == NL)
2461 {
2462 // When not showing the message window and the output
2463 // starts with a NL show the message normally.
2464 msg_win = NULL;
2465 }
2466 else
2467 {
2468 // currently hidden, make it empty
2469 curbuf = msg_win->w_buffer;
2470 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
2471 ml_delete(1);
2472 curbuf = curwin->w_buffer;
2473 }
2474 }
2475 else
2476 {
2477 lnum = msg_win->w_buffer->b_ml.ml_line_count;
2478 if (msg_col == 0)
2479 where = PUT_TRUNC;
2480 }
2481 }
2482 }
2483#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484
2485 did_wait_return = FALSE;
Bram Moolenaar57b7fe82007-08-05 17:20:43 +00002486 while ((maxlen < 0 || (int)(s - str) < maxlen) && *s != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 {
2488 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002489 * We are at the end of the screen line when:
2490 * - When outputting a newline.
2491 * - When outputting a character in the last column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002493 if (!recurse && msg_row >= Rows - 1 && (*s == '\n' || (
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494#ifdef FEAT_RIGHTLEFT
2495 cmdmsg_rl
2496 ? (
2497 msg_col <= 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002498 || (*s == TAB && msg_col <= 7)
2499 || (has_mbyte && (*mb_ptr2cells)(s) > 1 && msg_col <= 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 :
2501#endif
Bram Moolenaar0efd1bd2019-12-11 19:00:04 +01002502 ((*s != '\r' && msg_col + t_col >= Columns - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 || (*s == TAB && msg_col + t_col >= ((Columns - 1) & ~7))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 || (has_mbyte && (*mb_ptr2cells)(s) > 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002505 && msg_col + t_col >= Columns - 2)))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002507 /*
2508 * The screen is scrolled up when at the last row (some terminals
2509 * scroll automatically, some don't. To avoid problems we scroll
2510 * ourselves).
2511 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 if (t_col > 0)
Bram Moolenaar9198de32022-08-27 21:30:03 +01002513 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002514 // output postponed text
Bram Moolenaar9198de32022-08-27 21:30:03 +01002515#ifdef HAS_MESSAGE_WINDOW
2516 if (msg_win != NULL)
2517 {
2518 put_msg_win(msg_win, where, t_s, s, lnum);
2519 t_col = 0;
2520 where = PUT_BELOW;
2521 }
2522 else
2523#endif
2524 t_puts(&t_col, t_s, s, attr);
2525 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526
Bram Moolenaar85a20022019-12-21 18:25:54 +01002527 // When no more prompt and no more room, truncate here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 if (msg_no_more && lines_left == 0)
2529 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530
Bram Moolenaar9198de32022-08-27 21:30:03 +01002531#ifdef HAS_MESSAGE_WINDOW
2532 if (msg_win == NULL)
2533#endif
2534 // Scroll the screen up one line.
2535 msg_scroll_up();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536
2537 msg_row = Rows - 2;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002538 if (msg_col >= Columns) // can happen after screen resize
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 msg_col = Columns - 1;
2540
Bram Moolenaar85a20022019-12-21 18:25:54 +01002541 // Display char in last column before showing more-prompt.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002542 if (*s >= ' '
2543#ifdef FEAT_RIGHTLEFT
2544 && !cmdmsg_rl
2545#endif
2546 )
2547 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002548 if (has_mbyte)
2549 {
2550 if (enc_utf8 && maxlen >= 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002551 // avoid including composing chars after the end
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002552 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002553 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002554 l = (*mb_ptr2len)(s);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002555 s = screen_puts_mbyte(s, l, attr);
2556 }
2557 else
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002558 msg_screen_putchar(*s++, attr);
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002559 did_last_char = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002560 }
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002561 else
2562 did_last_char = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002563
2564 if (p_more)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002565 // store text for scrolling back
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002566 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
2567
Bram Moolenaar9198de32022-08-27 21:30:03 +01002568#ifdef HAS_MESSAGE_WINDOW
2569 if (msg_win == NULL)
2570 {
2571#endif
2572 inc_msg_scrolled();
Bram Moolenaar13608d82022-08-29 15:06:50 +01002573 need_wait_return = TRUE; // may need wait_return() in main()
Bram Moolenaar9198de32022-08-27 21:30:03 +01002574 redraw_cmdline = TRUE;
2575 if (cmdline_row > 0 && !exmode_active)
2576 --cmdline_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577
Bram Moolenaar9198de32022-08-27 21:30:03 +01002578 /*
2579 * If screen is completely filled and 'more' is set then wait
2580 * for a character.
2581 */
2582 if (lines_left > 0)
2583 --lines_left;
2584#ifdef HAS_MESSAGE_WINDOW
2585 }
2586#endif
Bram Moolenaar24959102022-05-07 20:01:16 +01002587 if (p_more && lines_left == 0 && State != MODE_HITRETURN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 && !msg_no_more && !exmode_active)
2589 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590#ifdef FEAT_CON_DIALOG
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002591 if (do_more_prompt(NUL))
2592 s = confirm_msg_tail;
2593#else
2594 (void)do_more_prompt(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595#endif
2596 if (quit_more)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002597 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002599
Bram Moolenaar85a20022019-12-21 18:25:54 +01002600 // When we displayed a char in last column need to check if there
2601 // is still more.
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002602 if (did_last_char)
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002603 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 }
2605
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002606 wrap = *s == '\n'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 || msg_col + t_col >= Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 || (has_mbyte && (*mb_ptr2cells)(s) > 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002609 && msg_col + t_col >= Columns - 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002610 if (t_col > 0 && (wrap || *s == '\r' || *s == '\b'
2611 || *s == '\t' || *s == BELL))
Bram Moolenaar9198de32022-08-27 21:30:03 +01002612 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002613 // output any postponed text
Bram Moolenaar9198de32022-08-27 21:30:03 +01002614#ifdef HAS_MESSAGE_WINDOW
2615 if (msg_win != NULL)
2616 {
2617 put_msg_win(msg_win, where, t_s, s, lnum);
2618 t_col = 0;
2619 where = PUT_BELOW;
2620 }
2621 else
2622#endif
2623 t_puts(&t_col, t_s, s, attr);
2624 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002625
2626 if (wrap && p_more && !recurse)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002627 // store text for scrolling back
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002628 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629
Bram Moolenaar85a20022019-12-21 18:25:54 +01002630 if (*s == '\n') // go to next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 {
Bram Moolenaar9198de32022-08-27 21:30:03 +01002632#ifdef HAS_MESSAGE_WINDOW
2633 if (msg_win != NULL)
2634 {
2635 // Ignore a NL when the buffer is empty, it is used to scroll
2636 // up the text.
2637 if ((msg_win->w_buffer->b_ml.ml_flags & ML_EMPTY) == 0)
2638 {
2639 put_msg_win(msg_win, PUT_BELOW, t_s, t_s, lnum);
2640 ++lnum;
2641 }
2642 }
2643 else
2644#endif
2645 msg_didout = FALSE; // remember that line is empty
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002646#ifdef FEAT_RIGHTLEFT
2647 if (cmdmsg_rl)
2648 msg_col = Columns - 1;
2649 else
2650#endif
2651 msg_col = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002652 if (++msg_row >= Rows) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 msg_row = Rows - 1;
2654 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002655 else if (*s == '\r') // go to column 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 {
2657 msg_col = 0;
Bram Moolenaard54af2e2022-08-27 22:05:13 +01002658#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaar9198de32022-08-27 21:30:03 +01002659 where = PUT_TRUNC;
Bram Moolenaard54af2e2022-08-27 22:05:13 +01002660#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002662 else if (*s == '\b') // go to previous char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 {
2664 if (msg_col)
2665 --msg_col;
2666 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002667 else if (*s == TAB) // translate Tab into spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 {
Bram Moolenaar9198de32022-08-27 21:30:03 +01002669#ifdef HAS_MESSAGE_WINDOW
2670 if (msg_win != NULL)
2671 msg_col = (msg_col + 7) % 8;
2672 else
2673#endif
2674 do
2675 msg_screen_putchar(' ', attr);
2676 while (msg_col & 7);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002678 else if (*s == BELL) // beep (from ":sh")
Bram Moolenaar165bc692015-07-21 17:53:25 +02002679 vim_beep(BO_SH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 else
2681 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 if (has_mbyte)
2683 {
2684 cw = (*mb_ptr2cells)(s);
2685 if (enc_utf8 && maxlen >= 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002686 // avoid including composing chars after the end
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002687 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002689 l = (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 }
2691 else
2692 {
2693 cw = 1;
2694 l = 1;
2695 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002696
Bram Moolenaar85a20022019-12-21 18:25:54 +01002697 // When drawing from right to left or when a double-wide character
2698 // doesn't fit, draw a single character here. Otherwise collect
2699 // characters and draw them all at once later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 if (
2701# ifdef FEAT_RIGHTLEFT
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002702 cmdmsg_rl ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703# endif
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002704 (cw > 1 && msg_col + t_col >= Columns - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 if (l > 1)
2707 s = screen_puts_mbyte(s, l, attr) - 1;
2708 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 msg_screen_putchar(*s, attr);
2710 }
2711 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002713 // postpone this character until later
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 if (t_col == 0)
2715 t_s = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 t_col += cw;
2717 s += l - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 }
2719 }
2720 ++s;
2721 }
2722
Bram Moolenaar85a20022019-12-21 18:25:54 +01002723 // output any postponed text
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 if (t_col > 0)
Bram Moolenaar9198de32022-08-27 21:30:03 +01002725 {
2726#ifdef HAS_MESSAGE_WINDOW
2727 if (msg_win != NULL)
2728 put_msg_win(msg_win, where, t_s, s, lnum);
2729 else
2730#endif
2731 t_puts(&t_col, t_s, s, attr);
2732 }
2733
2734#ifdef HAS_MESSAGE_WINDOW
2735 if (msg_win != NULL)
2736 popup_show_message_win();
2737#endif
Bram Moolenaar11901392022-09-26 19:50:44 +01002738 // Store the text for scroll back, unless it's a newline by itself.
2739 if (p_more && !recurse && !(s == sb_str + 1 && *sb_str == '\n'))
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002740 store_sb_text(&sb_str, s, attr, &sb_col, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741
2742 msg_check();
2743}
2744
2745/*
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002746 * Return TRUE when ":filter pattern" was used and "msg" does not match
2747 * "pattern".
2748 */
2749 int
2750message_filtered(char_u *msg)
2751{
Bram Moolenaard29459b2016-08-26 22:29:11 +02002752 int match;
2753
Bram Moolenaare1004402020-10-24 20:49:43 +02002754 if (cmdmod.cmod_filter_regmatch.regprog == NULL)
Bram Moolenaard29459b2016-08-26 22:29:11 +02002755 return FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02002756 match = vim_regexec(&cmdmod.cmod_filter_regmatch, msg, (colnr_T)0);
2757 return cmdmod.cmod_filter_force ? match : !match;
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002758}
2759
2760/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002761 * Scroll the screen up one line for displaying the next message line.
2762 */
2763 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002764msg_scroll_up(void)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002765{
Bram Moolenaar9198de32022-08-27 21:30:03 +01002766#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaara2a89732022-08-31 14:46:18 +01002767 if (in_echowindow)
Bram Moolenaar9198de32022-08-27 21:30:03 +01002768 return;
2769#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002770#ifdef FEAT_GUI
Bram Moolenaar85a20022019-12-21 18:25:54 +01002771 // Remove the cursor before scrolling, ScreenLines[] is going
2772 // to become invalid.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002773 if (gui.in_use)
2774 gui_undraw_cursor();
2775#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01002776 // scrolling up always works
Bram Moolenaara338adc2018-01-31 20:51:47 +01002777 mch_disable_flush();
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002778 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaara338adc2018-01-31 20:51:47 +01002779 mch_enable_flush();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002780
2781 if (!can_clear((char_u *)" "))
2782 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002783 // Scrolling up doesn't result in the right background. Set the
2784 // background here. It's not efficient, but avoids that we have to do
2785 // it all over the code.
Shougo Matsushitabe2b03c2024-04-08 22:11:50 +02002786 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns,
2787 ' ', ' ', HL_ATTR(HLF_MSG));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002788
Bram Moolenaar85a20022019-12-21 18:25:54 +01002789 // Also clear the last char of the last but one line if it was not
2790 // cleared before to avoid a scroll-up.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002791 if (ScreenAttrs[LineOffset[Rows - 2] + Columns - 1] == (sattr_T)-1)
2792 screen_fill((int)Rows - 2, (int)Rows - 1,
Shougo Matsushitabe2b03c2024-04-08 22:11:50 +02002793 (int)Columns - 1, (int)Columns,
2794 ' ', ' ', HL_ATTR(HLF_MSG));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002795 }
2796}
2797
2798/*
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002799 * Increment "msg_scrolled".
2800 */
2801 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002802inc_msg_scrolled(void)
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002803{
2804#ifdef FEAT_EVAL
2805 if (*get_vim_var_str(VV_SCROLLSTART) == NUL)
2806 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002807 char_u *p = SOURCING_NAME;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002808 char_u *tofree = NULL;
2809 int len;
2810
Bram Moolenaar85a20022019-12-21 18:25:54 +01002811 // v:scrollstart is empty, set it to the script/function name and line
2812 // number
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002813 if (p == NULL)
2814 p = (char_u *)_("Unknown");
2815 else
2816 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002817 len = (int)STRLEN(p) + 40;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002818 tofree = alloc(len);
2819 if (tofree != NULL)
2820 {
2821 vim_snprintf((char *)tofree, len, _("%s line %ld"),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002822 p, (long)SOURCING_LNUM);
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002823 p = tofree;
2824 }
2825 }
2826 set_vim_var_string(VV_SCROLLSTART, p, -1);
2827 vim_free(tofree);
2828 }
2829#endif
2830 ++msg_scrolled;
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01002831 set_must_redraw(UPD_VALID);
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002832}
2833
2834/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002835 * To be able to scroll back at the "more" and "hit-enter" prompts we need to
2836 * store the displayed text and remember where screen lines start.
2837 */
2838typedef struct msgchunk_S msgchunk_T;
2839struct msgchunk_S
2840{
2841 msgchunk_T *sb_next;
2842 msgchunk_T *sb_prev;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002843 char sb_eol; // TRUE when line ends after this text
2844 int sb_msg_col; // column in which text starts
2845 int sb_attr; // text attributes
2846 char_u sb_text[1]; // text to be displayed, actually longer
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002847};
2848
Bram Moolenaar85a20022019-12-21 18:25:54 +01002849static msgchunk_T *last_msgchunk = NULL; // last displayed text
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002850
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002851static msgchunk_T *msg_sb_start(msgchunk_T *mps);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002852
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002853typedef enum {
2854 SB_CLEAR_NONE = 0,
2855 SB_CLEAR_ALL,
2856 SB_CLEAR_CMDLINE_BUSY,
2857 SB_CLEAR_CMDLINE_DONE
2858} sb_clear_T;
2859
Bram Moolenaar85a20022019-12-21 18:25:54 +01002860// When to clear text on next msg.
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002861static sb_clear_T do_clear_sb_text = SB_CLEAR_NONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002862
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002863/*
2864 * Store part of a printed message for displaying when scrolling back.
2865 */
2866 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002867store_sb_text(
Bram Moolenaar85a20022019-12-21 18:25:54 +01002868 char_u **sb_str, // start of string
2869 char_u *s, // just after string
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002870 int attr,
2871 int *sb_col,
Bram Moolenaar85a20022019-12-21 18:25:54 +01002872 int finish) // line ends
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002873{
2874 msgchunk_T *mp;
2875
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002876 if (do_clear_sb_text == SB_CLEAR_ALL
2877 || do_clear_sb_text == SB_CLEAR_CMDLINE_DONE)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002878 {
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002879 clear_sb_text(do_clear_sb_text == SB_CLEAR_ALL);
zeertzjq46af7bc2022-07-28 12:34:09 +01002880 msg_sb_eol(); // prevent messages from overlapping
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002881 do_clear_sb_text = SB_CLEAR_NONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002882 }
2883
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002884 if (s > *sb_str)
2885 {
zeertzjq1b438a82023-02-01 13:11:15 +00002886 mp = alloc(offsetof(msgchunk_T, sb_text) + (s - *sb_str) + 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002887 if (mp != NULL)
2888 {
2889 mp->sb_eol = finish;
2890 mp->sb_msg_col = *sb_col;
2891 mp->sb_attr = attr;
2892 vim_strncpy(mp->sb_text, *sb_str, s - *sb_str);
2893
2894 if (last_msgchunk == NULL)
2895 {
2896 last_msgchunk = mp;
2897 mp->sb_prev = NULL;
2898 }
2899 else
2900 {
2901 mp->sb_prev = last_msgchunk;
2902 last_msgchunk->sb_next = mp;
2903 last_msgchunk = mp;
2904 }
2905 mp->sb_next = NULL;
2906 }
2907 }
2908 else if (finish && last_msgchunk != NULL)
2909 last_msgchunk->sb_eol = TRUE;
2910
2911 *sb_str = s;
2912 *sb_col = 0;
2913}
2914
2915/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002916 * Finished showing messages, clear the scroll-back text on the next message.
2917 */
2918 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002919may_clear_sb_text(void)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002920{
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002921 do_clear_sb_text = SB_CLEAR_ALL;
2922}
2923
2924/*
zeertzjq46af7bc2022-07-28 12:34:09 +01002925 * Starting to edit the command line: do not clear messages now.
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002926 */
2927 void
2928sb_text_start_cmdline(void)
2929{
zeertzjq46af7bc2022-07-28 12:34:09 +01002930 if (do_clear_sb_text == SB_CLEAR_CMDLINE_BUSY)
2931 // Invoking command line recursively: the previous-level command line
2932 // doesn't need to be remembered as it will be redrawn when returning
2933 // to that level.
2934 sb_text_restart_cmdline();
2935 else
2936 {
2937 msg_sb_eol();
2938 do_clear_sb_text = SB_CLEAR_CMDLINE_BUSY;
2939 }
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002940}
2941
2942/*
zeertzjq46af7bc2022-07-28 12:34:09 +01002943 * Redrawing the command line: clear the last unfinished line.
2944 */
2945 void
2946sb_text_restart_cmdline(void)
2947{
2948 msgchunk_T *tofree;
2949
2950 // Needed when returning from nested command line.
2951 do_clear_sb_text = SB_CLEAR_CMDLINE_BUSY;
2952
2953 if (last_msgchunk == NULL || last_msgchunk->sb_eol)
2954 // No unfinished line: don't clear anything.
2955 return;
2956
2957 tofree = msg_sb_start(last_msgchunk);
2958 last_msgchunk = tofree->sb_prev;
2959 if (last_msgchunk != NULL)
2960 last_msgchunk->sb_next = NULL;
2961 while (tofree != NULL)
2962 {
2963 msgchunk_T *tofree_next = tofree->sb_next;
2964
2965 vim_free(tofree);
2966 tofree = tofree_next;
2967 }
2968}
2969
2970/*
2971 * Ending to edit the command line: clear old lines but the last one later.
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002972 */
2973 void
2974sb_text_end_cmdline(void)
2975{
2976 do_clear_sb_text = SB_CLEAR_CMDLINE_DONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002977}
2978
2979/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002980 * Clear any text remembered for scrolling back.
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002981 * When "all" is FALSE keep the last line.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002982 * Called when redrawing the screen.
2983 */
2984 void
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002985clear_sb_text(int all)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002986{
2987 msgchunk_T *mp;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002988 msgchunk_T **lastp;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002989
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002990 if (all)
2991 lastp = &last_msgchunk;
2992 else
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002993 {
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002994 if (last_msgchunk == NULL)
2995 return;
zeertzjqecdc82e2022-07-25 19:50:57 +01002996 lastp = &msg_sb_start(last_msgchunk)->sb_prev;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002997 }
2998
2999 while (*lastp != NULL)
3000 {
3001 mp = (*lastp)->sb_prev;
3002 vim_free(*lastp);
3003 *lastp = mp;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003004 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003005}
3006
3007/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003008 * "g<" command.
3009 */
3010 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003011show_sb_text(void)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003012{
3013 msgchunk_T *mp;
3014
Bram Moolenaar85a20022019-12-21 18:25:54 +01003015 // Only show something if there is more than one line, otherwise it looks
3016 // weird, typing a command without output results in one line.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003017 mp = msg_sb_start(last_msgchunk);
3018 if (mp == NULL || mp->sb_prev == NULL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02003019 vim_beep(BO_MESS);
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003020 else
3021 {
3022 do_more_prompt('G');
3023 wait_return(FALSE);
3024 }
3025}
3026
3027/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003028 * Move to the start of screen line in already displayed text.
3029 */
3030 static msgchunk_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003031msg_sb_start(msgchunk_T *mps)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003032{
3033 msgchunk_T *mp = mps;
3034
3035 while (mp != NULL && mp->sb_prev != NULL && !mp->sb_prev->sb_eol)
3036 mp = mp->sb_prev;
3037 return mp;
3038}
3039
3040/*
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02003041 * Mark the last message chunk as finishing the line.
3042 */
3043 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003044msg_sb_eol(void)
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02003045{
3046 if (last_msgchunk != NULL)
3047 last_msgchunk->sb_eol = TRUE;
3048}
3049
3050/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003051 * Display a screen line from previously displayed text at row "row".
Bram Moolenaar838b7462022-09-26 15:19:56 +01003052 * When "clear_to_eol" is set clear the rest of the screen line.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003053 * Returns a pointer to the text for the next line (can be NULL).
3054 */
3055 static msgchunk_T *
Bram Moolenaar838b7462022-09-26 15:19:56 +01003056disp_sb_line(int row, msgchunk_T *smp, int clear_to_eol)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003057{
3058 msgchunk_T *mp = smp;
3059 char_u *p;
3060
3061 for (;;)
3062 {
3063 msg_row = row;
3064 msg_col = mp->sb_msg_col;
3065 p = mp->sb_text;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003066 if (*p == '\n') // don't display the line break
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003067 ++p;
3068 msg_puts_display(p, -1, mp->sb_attr, TRUE);
Bram Moolenaar838b7462022-09-26 15:19:56 +01003069
3070 // If clearing the screen did not work (e.g. because of a background
3071 // color and t_ut isn't set) clear until the last column here.
3072 if (clear_to_eol)
Shougo Matsushitabe2b03c2024-04-08 22:11:50 +02003073 screen_fill(row, row + 1, msg_col, (int)Columns,
3074 ' ', ' ', HL_ATTR(HLF_MSG));
Bram Moolenaar838b7462022-09-26 15:19:56 +01003075
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003076 if (mp->sb_eol || mp->sb_next == NULL)
3077 break;
3078 mp = mp->sb_next;
3079 }
3080 return mp->sb_next;
3081}
3082
3083/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 * Output any postponed text for msg_puts_attr_len().
3085 */
3086 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003087t_puts(
3088 int *t_col,
3089 char_u *t_s,
3090 char_u *s,
3091 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092{
Bram Moolenaar85a20022019-12-21 18:25:54 +01003093 // output postponed text
3094 msg_didout = TRUE; // remember that line is not empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095 screen_puts_len(t_s, (int)(s - t_s), msg_row, msg_col, attr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003096 msg_col += *t_col;
3097 *t_col = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003098 // If the string starts with a composing character don't increment the
3099 // column position for it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 if (enc_utf8 && utf_iscomposing(utf_ptr2char(t_s)))
3101 --msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 if (msg_col >= Columns)
3103 {
3104 msg_col = 0;
3105 ++msg_row;
3106 }
3107}
3108
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109/*
3110 * Returns TRUE when messages should be printed with mch_errmsg().
3111 * This is used when there is no valid screen, so we can see error messages.
3112 * If termcap is not active, we may be writing in an alternate console
3113 * window, cursor positioning may not work correctly (window size may be
3114 * different, e.g. for Win32 console) or we just don't know where the
3115 * cursor is.
3116 */
3117 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003118msg_use_printf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119{
3120 return (!msg_check_screen()
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003121#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3122# ifdef VIMDLL
3123 || (!gui.in_use && !termcap_active)
3124# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125 || !termcap_active
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003126# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127#endif
3128 || (swapping_screen() && !termcap_active)
3129 );
3130}
3131
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003132/*
3133 * Print a message when there is no valid screen.
3134 */
3135 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003136msg_puts_printf(char_u *str, int maxlen)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003137{
3138 char_u *s = str;
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003139 char_u *buf = NULL;
3140 char_u *p = s;
3141
Bram Moolenaar4f974752019-02-17 17:44:42 +01003142#ifdef MSWIN
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003143 if (!(silent_mode && p_verbose == 0))
Bram Moolenaar85a20022019-12-21 18:25:54 +01003144 mch_settmode(TMODE_COOK); // handle CR and NL correctly
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003145#endif
Bram Moolenaar2321ca22016-09-09 14:17:18 +02003146 while ((maxlen < 0 || (int)(s - str) < maxlen) && *s != NUL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003147 {
3148 if (!(silent_mode && p_verbose == 0))
3149 {
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003150 // NL --> CR NL translation (for Unix, not for "--version")
3151 if (*s == NL)
3152 {
3153 int n = (int)(s - p);
3154
3155 buf = alloc(n + 3);
Bram Moolenaar6f10c702019-08-20 22:58:37 +02003156 if (buf != NULL)
3157 {
3158 memcpy(buf, p, n);
3159 if (!info_message)
3160 buf[n++] = CAR;
3161 buf[n++] = NL;
3162 buf[n++] = NUL;
3163 if (info_message) // informative message, not an error
3164 mch_msg((char *)buf);
3165 else
3166 mch_errmsg((char *)buf);
3167 vim_free(buf);
3168 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003169 p = s + 1;
3170 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003171 }
3172
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003173 // primitive way to compute the current column
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003174#ifdef FEAT_RIGHTLEFT
3175 if (cmdmsg_rl)
3176 {
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003177 if (*s == CAR || *s == NL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003178 msg_col = Columns - 1;
3179 else
3180 --msg_col;
3181 }
3182 else
3183#endif
3184 {
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003185 if (*s == CAR || *s == NL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003186 msg_col = 0;
3187 else
3188 ++msg_col;
3189 }
3190 ++s;
3191 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003192
3193 if (*p != NUL && !(silent_mode && p_verbose == 0))
3194 {
Bram Moolenaara97c3632021-06-15 22:39:11 +02003195 char_u *tofree = NULL;
Bram Moolenaar97c2c052019-02-22 13:42:07 +01003196
Bram Moolenaarc32949b2023-01-04 15:56:51 +00003197 if (maxlen > 0 && vim_strlen_maxlen((char *)p, (size_t)maxlen)
3198 >= (size_t)maxlen)
Bram Moolenaar97c2c052019-02-22 13:42:07 +01003199 {
Bram Moolenaara97c3632021-06-15 22:39:11 +02003200 tofree = vim_strnsave(p, (size_t)maxlen);
3201 p = tofree;
Bram Moolenaar97c2c052019-02-22 13:42:07 +01003202 }
Bram Moolenaara97c3632021-06-15 22:39:11 +02003203 if (p != NULL)
3204 {
3205 if (info_message)
3206 mch_msg((char *)p);
3207 else
3208 mch_errmsg((char *)p);
3209 vim_free(tofree);
3210 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003211 }
3212
3213 msg_didout = TRUE; // assume that line is not empty
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003214
Bram Moolenaar4f974752019-02-17 17:44:42 +01003215#ifdef MSWIN
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003216 if (!(silent_mode && p_verbose == 0))
3217 mch_settmode(TMODE_RAW);
3218#endif
3219}
3220
3221/*
3222 * Show the more-prompt and handle the user response.
3223 * This takes care of scrolling back and displaying previously displayed text.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003224 * When at hit-enter prompt "typed_char" is the already typed character,
3225 * otherwise it's NUL.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003226 * Returns TRUE when jumping ahead to "confirm_msg_tail".
3227 */
3228 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003229do_more_prompt(int typed_char)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003230{
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01003231 static int entered = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003232 int used_typed_char = typed_char;
3233 int oldState = State;
3234 int c;
3235#ifdef FEAT_CON_DIALOG
3236 int retval = FALSE;
3237#endif
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003238 int toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003239 msgchunk_T *mp_last = NULL;
3240 msgchunk_T *mp;
3241 int i;
Shougo Matsushitabe2b03c2024-04-08 22:11:50 +02003242 int msg_attr;
3243
3244 msg_attr = HL_ATTR(HLF_MSG);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003245
Bram Moolenaar85a20022019-12-21 18:25:54 +01003246 // We get called recursively when a timer callback outputs a message. In
3247 // that case don't show another prompt. Also when at the hit-Enter prompt
3248 // and nothing was typed.
Bram Moolenaar24959102022-05-07 20:01:16 +01003249 if (entered || (State == MODE_HITRETURN && typed_char == 0))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01003250 return FALSE;
3251 entered = TRUE;
3252
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003253 if (typed_char == 'G')
3254 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003255 // "g<": Find first line on the last page.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003256 mp_last = msg_sb_start(last_msgchunk);
3257 for (i = 0; i < Rows - 2 && mp_last != NULL
3258 && mp_last->sb_prev != NULL; ++i)
3259 mp_last = msg_sb_start(mp_last->sb_prev);
3260 }
3261
Bram Moolenaar24959102022-05-07 20:01:16 +01003262 State = MODE_ASKMORE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003263 setmouse();
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003264 if (typed_char == NUL)
3265 msg_moremsg(FALSE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003266 for (;;)
3267 {
3268 /*
3269 * Get a typed character directly from the user.
3270 */
3271 if (used_typed_char != NUL)
3272 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003273 c = used_typed_char; // was typed at hit-enter prompt
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003274 used_typed_char = NUL;
3275 }
3276 else
3277 c = get_keystroke();
3278
3279#if defined(FEAT_MENU) && defined(FEAT_GUI)
3280 if (c == K_MENU)
3281 {
Bram Moolenaar24959102022-05-07 20:01:16 +01003282 int idx = get_menu_index(current_menu, MODE_ASKMORE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003283
Bram Moolenaar85a20022019-12-21 18:25:54 +01003284 // Used a menu. If it starts with CTRL-Y, it must
3285 // be a "Copy" for the clipboard. Otherwise
3286 // assume that we end
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003287 if (idx == MENU_INDEX_INVALID)
3288 continue;
3289 c = *current_menu->strings[idx];
3290 if (c != NUL && current_menu->strings[idx][1] != NUL)
3291 ins_typebuf(current_menu->strings[idx] + 1,
3292 current_menu->noremap[idx], 0, TRUE,
3293 current_menu->silent[idx]);
3294 }
3295#endif
3296
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003297 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003298 switch (c)
3299 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003300 case BS: // scroll one line back
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003301 case K_BS:
3302 case 'k':
3303 case K_UP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003304 toscroll = -1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003305 break;
3306
Bram Moolenaar85a20022019-12-21 18:25:54 +01003307 case CAR: // one extra line
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003308 case NL:
3309 case 'j':
3310 case K_DOWN:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003311 toscroll = 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003312 break;
3313
Bram Moolenaar85a20022019-12-21 18:25:54 +01003314 case 'u': // Up 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 'd': // Down half a page
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003319 toscroll = Rows / 2;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003320 break;
3321
Bram Moolenaar85a20022019-12-21 18:25:54 +01003322 case 'b': // one page back
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00003323 case K_PAGEUP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003324 toscroll = -(Rows - 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003325 break;
3326
Bram Moolenaar85a20022019-12-21 18:25:54 +01003327 case ' ': // one extra page
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00003328 case 'f':
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003329 case K_PAGEDOWN:
3330 case K_LEFTMOUSE:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003331 toscroll = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003332 break;
3333
Bram Moolenaar85a20022019-12-21 18:25:54 +01003334 case 'g': // all the way back to the start
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003335 toscroll = -999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003336 break;
3337
Bram Moolenaar85a20022019-12-21 18:25:54 +01003338 case 'G': // all the way to the end
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003339 toscroll = 999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003340 lines_left = 999999;
3341 break;
3342
Bram Moolenaar85a20022019-12-21 18:25:54 +01003343 case ':': // start new command line
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003344#ifdef FEAT_CON_DIALOG
3345 if (!confirm_msg_used)
3346#endif
3347 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003348 // Since got_int is set all typeahead will be flushed, but we
3349 // want to keep this ':', remember that in a special way.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003350 typeahead_noflush(':');
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02003351#ifdef FEAT_TERMINAL
3352 skip_term_loop = TRUE;
3353#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01003354 cmdline_row = Rows - 1; // put ':' on this line
3355 skip_redraw = TRUE; // skip redraw once
3356 need_wait_return = FALSE; // don't wait in main()
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003357 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003358 // FALLTHROUGH
3359 case 'q': // quit
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003360 case Ctrl_C:
3361 case ESC:
3362#ifdef FEAT_CON_DIALOG
3363 if (confirm_msg_used)
3364 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003365 // Jump to the choices of the dialog.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003366 retval = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003367 }
3368 else
3369#endif
3370 {
3371 got_int = TRUE;
3372 quit_more = TRUE;
3373 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003374 // When there is some more output (wrapping line) display that
3375 // without another prompt.
Bram Moolenaar51306d22009-02-24 03:38:04 +00003376 lines_left = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003377 break;
3378
3379#ifdef FEAT_CLIPBOARD
3380 case Ctrl_Y:
Bram Moolenaar85a20022019-12-21 18:25:54 +01003381 // Strange way to allow copying (yanking) a modeless
3382 // selection at the more prompt. Use CTRL-Y,
3383 // because the same is used in Cmdline-mode and at the
3384 // hit-enter prompt. However, scrolling one line up
3385 // might be expected...
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003386 if (clip_star.state == SELECT_DONE)
3387 clip_copy_modeless_selection(TRUE);
3388 continue;
3389#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01003390 default: // no valid response
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003391 msg_moremsg(TRUE);
3392 continue;
3393 }
3394
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003395 if (toscroll != 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003396 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003397 if (toscroll < 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003398 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003399 // go to start of last line
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003400 if (mp_last == NULL)
3401 mp = msg_sb_start(last_msgchunk);
3402 else if (mp_last->sb_prev != NULL)
3403 mp = msg_sb_start(mp_last->sb_prev);
3404 else
3405 mp = NULL;
3406
Bram Moolenaar85a20022019-12-21 18:25:54 +01003407 // go to start of line at top of the screen
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003408 for (i = 0; i < Rows - 2 && mp != NULL && mp->sb_prev != NULL;
3409 ++i)
3410 mp = msg_sb_start(mp->sb_prev);
3411
3412 if (mp != NULL && mp->sb_prev != NULL)
3413 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003414 // Find line to be displayed at top.
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003415 for (i = 0; i > toscroll; --i)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003416 {
3417 if (mp == NULL || mp->sb_prev == NULL)
3418 break;
3419 mp = msg_sb_start(mp->sb_prev);
3420 if (mp_last == NULL)
3421 mp_last = msg_sb_start(last_msgchunk);
3422 else
3423 mp_last = msg_sb_start(mp_last->sb_prev);
3424 }
3425
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003426 if (toscroll == -1 && screen_ins_lines(0, 0, 1,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003427 (int)Rows, 0, NULL) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003428 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003429 // display line at top
Bram Moolenaar838b7462022-09-26 15:19:56 +01003430 (void)disp_sb_line(0, mp, FALSE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003431 }
3432 else
3433 {
Bram Moolenaar838b7462022-09-26 15:19:56 +01003434 int did_clear = screenclear();
3435
Bram Moolenaar85a20022019-12-21 18:25:54 +01003436 // redisplay all lines
Bram Moolenaar773560b2006-05-06 21:38:18 +00003437 for (i = 0; mp != NULL && i < Rows - 1; ++i)
3438 {
Bram Moolenaar838b7462022-09-26 15:19:56 +01003439 mp = disp_sb_line(i, mp, !did_clear);
Bram Moolenaar773560b2006-05-06 21:38:18 +00003440 ++msg_scrolled;
3441 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003442 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003443 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003444 }
3445 }
3446 else
3447 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003448 // First display any text that we scrolled back.
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003449 while (toscroll > 0 && mp_last != NULL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003450 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003451 // scroll up, display line at bottom
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003452 msg_scroll_up();
Bram Moolenaarbb15b652005-10-03 21:52:09 +00003453 inc_msg_scrolled();
Shougo Matsushitabe2b03c2024-04-08 22:11:50 +02003454 screen_fill((int)Rows - 2, (int)Rows - 1, 0, (int)Columns,
3455 ' ', ' ', msg_attr);
Bram Moolenaar838b7462022-09-26 15:19:56 +01003456 mp_last = disp_sb_line((int)Rows - 2, mp_last, FALSE);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003457 --toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003458 }
3459 }
3460
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003461 if (toscroll <= 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003462 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003463 // displayed the requested text, more prompt again
Shougo Matsushitabe2b03c2024-04-08 22:11:50 +02003464 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns,
3465 ' ', ' ', msg_attr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003466 msg_moremsg(FALSE);
3467 continue;
3468 }
3469
Bram Moolenaar85a20022019-12-21 18:25:54 +01003470 // display more text, return to caller
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003471 lines_left = toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003472 }
3473
3474 break;
3475 }
3476
Bram Moolenaar85a20022019-12-21 18:25:54 +01003477 // clear the --more-- message
Shougo Matsushitabe2b03c2024-04-08 22:11:50 +02003478 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', msg_attr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003479 State = oldState;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003480 setmouse();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003481 if (quit_more)
3482 {
3483 msg_row = Rows - 1;
3484 msg_col = 0;
3485 }
3486#ifdef FEAT_RIGHTLEFT
3487 else if (cmdmsg_rl)
3488 msg_col = Columns - 1;
3489#endif
3490
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01003491 entered = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003492#ifdef FEAT_CON_DIALOG
3493 return retval;
3494#else
3495 return FALSE;
3496#endif
3497}
3498
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499#if defined(USE_MCH_ERRMSG) || defined(PROTO)
3500
3501#ifdef mch_errmsg
3502# undef mch_errmsg
3503#endif
3504#ifdef mch_msg
3505# undef mch_msg
3506#endif
3507
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003508#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3509 static void
3510mch_errmsg_c(char *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511{
Bram Moolenaar0f77d6a2019-02-14 20:55:09 +01003512 int len = (int)STRLEN(str);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003513 DWORD nwrite = 0;
3514 DWORD mode = 0;
3515 HANDLE h = GetStdHandle(STD_ERROR_HANDLE);
3516
3517 if (GetConsoleMode(h, &mode) && enc_codepage >= 0
3518 && (int)GetConsoleCP() != enc_codepage)
3519 {
3520 WCHAR *w = enc_to_utf16((char_u *)str, &len);
3521
3522 WriteConsoleW(h, w, len, &nwrite, NULL);
3523 vim_free(w);
3524 }
3525 else
3526 {
3527 fprintf(stderr, "%s", str);
3528 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003529}
3530#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003532/*
3533 * Give an error message. To be used when the screen hasn't been initialized
3534 * yet. When stderr can't be used, collect error messages until the GUI has
3535 * started and they can be displayed in a message box.
3536 */
3537 void
3538mch_errmsg(char *str)
3539{
3540#if !defined(MSWIN) || defined(FEAT_GUI_MSWIN)
3541 int len;
3542#endif
3543
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02003544#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003545 // On Unix use stderr if it's a tty.
3546 // When not going to start the GUI also use stderr.
3547 // On Mac, when started from Finder, stderr is the console.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 if (
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003549# ifdef UNIX
3550# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003552# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 isatty(2)
3554# endif
3555# ifdef FEAT_GUI
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003556 ||
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003557# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003558# endif
3559# ifdef FEAT_GUI
3560 !(gui.in_use || gui.starting)
3561# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 )
3563 {
3564 fprintf(stderr, "%s", str);
3565 return;
3566 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003567#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003569#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3570# ifdef VIMDLL
3571 if (!(gui.in_use || gui.starting))
3572# endif
3573 {
3574 mch_errmsg_c(str);
3575 return;
3576 }
3577#endif
3578
3579#if !defined(MSWIN) || defined(FEAT_GUI_MSWIN)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003580 // avoid a delay for a message that isn't there
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 emsg_on_display = FALSE;
3582
3583 len = (int)STRLEN(str) + 1;
3584 if (error_ga.ga_growsize == 0)
3585 {
3586 error_ga.ga_growsize = 80;
3587 error_ga.ga_itemsize = 1;
3588 }
3589 if (ga_grow(&error_ga, len) == OK)
3590 {
3591 mch_memmove((char_u *)error_ga.ga_data + error_ga.ga_len,
3592 (char_u *)str, len);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003593# ifdef UNIX
Bram Moolenaar85a20022019-12-21 18:25:54 +01003594 // remove CR characters, they are displayed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 {
3596 char_u *p;
3597
3598 p = (char_u *)error_ga.ga_data + error_ga.ga_len;
3599 for (;;)
3600 {
3601 p = vim_strchr(p, '\r');
3602 if (p == NULL)
3603 break;
3604 *p = ' ';
3605 }
3606 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003607# endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01003608 --len; // don't count the NUL at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 error_ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003611#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612}
3613
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003614#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3615 static void
3616mch_msg_c(char *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617{
Bram Moolenaar0f77d6a2019-02-14 20:55:09 +01003618 int len = (int)STRLEN(str);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003619 DWORD nwrite = 0;
3620 DWORD mode;
3621 HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
3622
3623
3624 if (GetConsoleMode(h, &mode) && enc_codepage >= 0
3625 && (int)GetConsoleCP() != enc_codepage)
3626 {
3627 WCHAR *w = enc_to_utf16((char_u *)str, &len);
3628
3629 WriteConsoleW(h, w, len, &nwrite, NULL);
3630 vim_free(w);
3631 }
3632 else
3633 {
3634 printf("%s", str);
3635 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003636}
3637#endif
3638
3639/*
3640 * Give a message. To be used when the screen hasn't been initialized yet.
3641 * When there is no tty, collect messages until the GUI has started and they
3642 * can be displayed in a message box.
3643 */
3644 void
3645mch_msg(char *str)
3646{
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02003647#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003648 // On Unix use stdout if we have a tty. This allows "vim -h | more" and
3649 // uses mch_errmsg() when started from the desktop.
3650 // When not going to start the GUI also use stdout.
3651 // On Mac, when started from Finder, stderr is the console.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 if (
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003653# ifdef UNIX
3654# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003656# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 isatty(2)
Bram Moolenaareae1b912019-05-09 15:12:55 +02003658# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659# ifdef FEAT_GUI
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003660 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003662# endif
3663# ifdef FEAT_GUI
3664 !(gui.in_use || gui.starting)
3665# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 )
3667 {
3668 printf("%s", str);
3669 return;
3670 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003671#endif
3672
3673#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3674# ifdef VIMDLL
3675 if (!(gui.in_use || gui.starting))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003677 {
3678 mch_msg_c(str);
3679 return;
3680 }
3681#endif
3682#if !defined(MSWIN) || defined(FEAT_GUI_MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 mch_errmsg(str);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003684#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685}
Bram Moolenaar85a20022019-12-21 18:25:54 +01003686#endif // USE_MCH_ERRMSG
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687
3688/*
3689 * Put a character on the screen at the current message position and advance
3690 * to the next position. Only for printable ASCII!
3691 */
3692 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003693msg_screen_putchar(int c, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694{
Bram Moolenaar85a20022019-12-21 18:25:54 +01003695 msg_didout = TRUE; // remember that line is not empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 screen_putchar(c, msg_row, msg_col, attr);
3697#ifdef FEAT_RIGHTLEFT
3698 if (cmdmsg_rl)
3699 {
3700 if (--msg_col == 0)
3701 {
3702 msg_col = Columns;
3703 ++msg_row;
3704 }
3705 }
3706 else
3707#endif
3708 {
3709 if (++msg_col >= Columns)
3710 {
3711 msg_col = 0;
3712 ++msg_row;
3713 }
3714 }
3715}
3716
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003717 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003718msg_moremsg(int full)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003720 int attr;
3721 char_u *s = (char_u *)_("-- More --");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722
Bram Moolenaar8820b482017-03-16 17:23:31 +01003723 attr = HL_ATTR(HLF_M);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003724 screen_puts(s, (int)Rows - 1, 0, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 if (full)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003726 screen_puts((char_u *)
3727 _(" SPACE/d/j: screen/page/line down, b/u/k: up, q: quit "),
3728 (int)Rows - 1, vim_strsize(s), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729}
3730
3731/*
Bram Moolenaar24959102022-05-07 20:01:16 +01003732 * Repeat the message for the current mode: MODE_ASKMORE, MODE_EXTERNCMD,
3733 * MODE_CONFIRM or exmode_active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 */
3735 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003736repeat_message(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737{
Bram Moolenaar24959102022-05-07 20:01:16 +01003738 if (State == MODE_ASKMORE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003740 msg_moremsg(TRUE); // display --more-- message again
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 msg_row = Rows - 1;
3742 }
3743#ifdef FEAT_CON_DIALOG
Bram Moolenaar24959102022-05-07 20:01:16 +01003744 else if (State == MODE_CONFIRM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003746 display_confirm_msg(); // display ":confirm" message again
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 msg_row = Rows - 1;
3748 }
3749#endif
Bram Moolenaar24959102022-05-07 20:01:16 +01003750 else if (State == MODE_EXTERNCMD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003752 windgoto(msg_row, msg_col); // put cursor back
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 }
Bram Moolenaar24959102022-05-07 20:01:16 +01003754 else if (State == MODE_HITRETURN || State == MODE_SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 {
Bram Moolenaar9f108752007-11-24 14:44:58 +00003756 if (msg_row == Rows - 1)
3757 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003758 // Avoid drawing the "hit-enter" prompt below the previous one,
3759 // overwrite it. Esp. useful when regaining focus and a
3760 // FocusGained autocmd exists but didn't draw anything.
Bram Moolenaar9f108752007-11-24 14:44:58 +00003761 msg_didout = FALSE;
3762 msg_col = 0;
3763 msg_clr_eos();
3764 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 hit_return_msg();
3766 msg_row = Rows - 1;
3767 }
3768}
3769
3770/*
3771 * msg_check_screen - check if the screen is initialized.
3772 * Also check msg_row and msg_col, if they are too big it may cause a crash.
3773 * While starting the GUI the terminal codes will be set for the GUI, but the
3774 * output goes to the terminal. Don't use the terminal codes then.
3775 */
3776 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003777msg_check_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778{
3779 if (!full_screen || !screen_valid(FALSE))
3780 return FALSE;
3781
3782 if (msg_row >= Rows)
3783 msg_row = Rows - 1;
3784 if (msg_col >= Columns)
3785 msg_col = Columns - 1;
3786 return TRUE;
3787}
3788
3789/*
3790 * Clear from current message position to end of screen.
3791 * Skip this when ":silent" was used, no need to clear for redirection.
3792 */
3793 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003794msg_clr_eos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795{
3796 if (msg_silent == 0)
3797 msg_clr_eos_force();
3798}
3799
3800/*
3801 * Clear from current message position to end of screen.
3802 * Note: msg_col is not updated, so we remember the end of the message
3803 * for msg_check().
3804 */
3805 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003806msg_clr_eos_force(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807{
Bram Moolenaar3b474dc2022-09-01 17:01:32 +01003808#ifdef HAS_MESSAGE_WINDOW
3809 if (in_echowindow)
3810 return; // messages go into a popup
3811#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 if (msg_use_printf())
3813 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003814 if (full_screen) // only when termcap codes are valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 {
3816 if (*T_CD)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003817 out_str(T_CD); // clear to end of display
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 else if (*T_CE)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003819 out_str(T_CE); // clear to end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820 }
3821 }
Bram Moolenaara2a89732022-08-31 14:46:18 +01003822 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 {
Shougo Matsushitabe2b03c2024-04-08 22:11:50 +02003824 int msg_attr;
3825
3826 msg_attr = HL_ATTR(HLF_MSG);
3827
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828#ifdef FEAT_RIGHTLEFT
3829 if (cmdmsg_rl)
3830 {
Shougo Matsushitabe2b03c2024-04-08 22:11:50 +02003831 screen_fill(msg_row, msg_row + 1, 0, msg_col + 1,
3832 ' ', ' ', msg_attr);
3833 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns,
3834 ' ', ' ', msg_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 }
3836 else
3837#endif
3838 {
3839 screen_fill(msg_row, msg_row + 1, msg_col, (int)Columns,
Shougo Matsushitabe2b03c2024-04-08 22:11:50 +02003840 ' ', ' ', msg_attr);
3841 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns,
3842 ' ', ' ', msg_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 }
3844 }
3845}
3846
3847/*
3848 * Clear the command line.
3849 */
3850 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003851msg_clr_cmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852{
3853 msg_row = cmdline_row;
3854 msg_col = 0;
3855 msg_clr_eos_force();
3856}
3857
3858/*
3859 * end putting a message on the screen
Bram Moolenaar13608d82022-08-29 15:06:50 +01003860 * call wait_return() if the message does not fit in the available space
3861 * return TRUE if wait_return() not called.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 */
3863 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003864msg_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865{
3866 /*
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02003867 * If the string is larger than the window,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 * or the ruler option is set and we run into it,
3869 * we have to redraw the window.
3870 * Do not do this if we are abandoning the file or editing the command line.
3871 */
Bram Moolenaar24959102022-05-07 20:01:16 +01003872 if (!exiting && need_wait_return && !(State & MODE_CMDLINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 {
3874 wait_return(FALSE);
3875 return FALSE;
3876 }
3877 out_flush();
3878 return TRUE;
3879}
3880
3881/*
3882 * If the written message runs into the shown command or ruler, we have to
3883 * wait for hit-return and redraw the window later.
3884 */
3885 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003886msg_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887{
Bram Moolenaar35a4fbc2022-08-28 14:39:53 +01003888 if (msg_row == Rows - 1 && msg_col >= sc_col
3889#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaara2a89732022-08-31 14:46:18 +01003890 && !in_echowindow
Bram Moolenaar35a4fbc2022-08-28 14:39:53 +01003891#endif
3892 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 {
3894 need_wait_return = TRUE;
3895 redraw_cmdline = TRUE;
3896 }
3897}
3898
3899/*
3900 * May write a string to the redirection file.
3901 * When "maxlen" is -1 write the whole string, otherwise up to "maxlen" bytes.
3902 */
3903 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003904redir_write(char_u *str, int maxlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905{
3906 char_u *s = str;
3907 static int cur_col = 0;
3908
Bram Moolenaar85a20022019-12-21 18:25:54 +01003909 // Don't do anything for displaying prompts and the like.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003910 if (redir_off)
3911 return;
3912
Bram Moolenaar85a20022019-12-21 18:25:54 +01003913 // If 'verbosefile' is set prepare for writing in that file.
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003914 if (*p_vfile != NUL && verbose_fd == NULL)
3915 verbose_open();
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003916
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003917 if (redirecting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003919 // If the string doesn't start with CR or NL, go to msg_col
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 if (*s != '\n' && *s != '\r')
3921 {
3922 while (cur_col < msg_col)
3923 {
3924#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003925 if (redir_execute)
3926 execute_redir_str((char_u *)" ", -1);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003927 else if (redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 write_reg_contents(redir_reg, (char_u *)" ", -1, TRUE);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003929 else if (redir_vname)
3930 var_redir_str((char_u *)" ", -1);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003931 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003933 if (redir_fd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 fputs(" ", redir_fd);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003935 if (verbose_fd != NULL)
3936 fputs(" ", verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 ++cur_col;
3938 }
3939 }
3940
3941#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003942 if (redir_execute)
3943 execute_redir_str(s, maxlen);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003944 else if (redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 write_reg_contents(redir_reg, s, maxlen, TRUE);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003946 else if (redir_vname)
Bram Moolenaardf177f62005-02-22 08:39:57 +00003947 var_redir_str(s, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948#endif
3949
Bram Moolenaar85a20022019-12-21 18:25:54 +01003950 // Write and adjust the current column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
3952 {
3953#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003954 if (!redir_reg && !redir_vname && !redir_execute)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003956 if (redir_fd != NULL)
3957 putc(*s, redir_fd);
3958 if (verbose_fd != NULL)
3959 putc(*s, verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 if (*s == '\r' || *s == '\n')
3961 cur_col = 0;
3962 else if (*s == '\t')
3963 cur_col += (8 - cur_col % 8);
3964 else
3965 ++cur_col;
3966 ++s;
3967 }
3968
Bram Moolenaar85a20022019-12-21 18:25:54 +01003969 if (msg_silent != 0) // should update msg_col
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 msg_col = cur_col;
3971 }
3972}
3973
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003974 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003975redirecting(void)
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003976{
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003977 return redir_fd != NULL || *p_vfile != NUL
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003978#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003979 || redir_reg || redir_vname || redir_execute
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003980#endif
3981 ;
3982}
3983
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984/*
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00003985 * Before giving verbose message.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003986 * Must always be called paired with verbose_leave()!
3987 */
3988 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003989verbose_enter(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003990{
3991 if (*p_vfile != NUL)
3992 ++msg_silent;
3993}
3994
3995/*
3996 * After giving verbose message.
3997 * Must always be called paired with verbose_enter()!
3998 */
3999 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004000verbose_leave(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00004001{
4002 if (*p_vfile != NUL)
4003 if (--msg_silent < 0)
4004 msg_silent = 0;
4005}
4006
4007/*
4008 * Like verbose_enter() and set msg_scroll when displaying the message.
4009 */
4010 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004011verbose_enter_scroll(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00004012{
4013 if (*p_vfile != NUL)
4014 ++msg_silent;
4015 else
Bram Moolenaar85a20022019-12-21 18:25:54 +01004016 // always scroll up, don't overwrite
Bram Moolenaar8b044b32005-05-31 22:05:58 +00004017 msg_scroll = TRUE;
4018}
4019
4020/*
4021 * Like verbose_leave() and set cmdline_row when displaying the message.
4022 */
4023 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004024verbose_leave_scroll(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00004025{
4026 if (*p_vfile != NUL)
4027 {
4028 if (--msg_silent < 0)
4029 msg_silent = 0;
4030 }
4031 else
4032 cmdline_row = msg_row;
4033}
4034
Bram Moolenaar8b044b32005-05-31 22:05:58 +00004035/*
4036 * Called when 'verbosefile' is set: stop writing to the file.
4037 */
4038 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004039verbose_stop(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00004040{
4041 if (verbose_fd != NULL)
4042 {
4043 fclose(verbose_fd);
4044 verbose_fd = NULL;
4045 }
4046 verbose_did_open = FALSE;
4047}
4048
4049/*
4050 * Open the file 'verbosefile'.
4051 * Return FAIL or OK.
4052 */
4053 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004054verbose_open(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00004055{
4056 if (verbose_fd == NULL && !verbose_did_open)
4057 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004058 // Only give the error message once.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00004059 verbose_did_open = TRUE;
4060
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004061 verbose_fd = mch_fopen((char *)p_vfile, "a");
Bram Moolenaar8b044b32005-05-31 22:05:58 +00004062 if (verbose_fd == NULL)
4063 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004064 semsg(_(e_cant_open_file_str), p_vfile);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00004065 return FAIL;
4066 }
4067 }
4068 return OK;
4069}
4070
4071/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 * Give a warning message (for searching).
4073 * Use 'w' highlighting and may repeat the message after redrawing
4074 */
4075 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004076give_warning(char_u *message, int hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077{
Bram Moolenaarcc7eb2a2021-07-11 19:12:04 +02004078 give_warning_with_source(message, hl, FALSE);
4079}
4080
4081 void
4082give_warning_with_source(char_u *message, int hl, int with_source)
4083{
Bram Moolenaar85a20022019-12-21 18:25:54 +01004084 // Don't do this for ":silent".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 if (msg_silent != 0)
4086 return;
4087
Bram Moolenaar85a20022019-12-21 18:25:54 +01004088 // Don't want a hit-enter prompt here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 ++no_wait_return;
Bram Moolenaared203462004-06-16 11:19:22 +00004090
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091#ifdef FEAT_EVAL
4092 set_vim_var_string(VV_WARNINGMSG, message, -1);
4093#endif
Bram Moolenaard23a8232018-02-10 18:45:26 +01004094 VIM_CLEAR(keep_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 if (hl)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004096 keep_msg_attr = HL_ATTR(HLF_W);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 else
4098 keep_msg_attr = 0;
Bram Moolenaarcc7eb2a2021-07-11 19:12:04 +02004099
4100 if (with_source)
4101 {
4102 // Do what msg() does, but with a column offset if the warning should
4103 // be after the mode message.
4104 msg_start();
4105 msg_source(HL_ATTR(HLF_W));
4106 msg_puts(" ");
4107 msg_puts_attr((char *)message, HL_ATTR(HLF_W) | MSG_HIST);
4108 msg_clr_eos();
4109 (void)msg_end();
4110 }
4111 else if (msg_attr((char *)message, keep_msg_attr) && msg_scrolled == 0)
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004112 set_keep_msg(message, keep_msg_attr);
Bram Moolenaarcc7eb2a2021-07-11 19:12:04 +02004113
Bram Moolenaar85a20022019-12-21 18:25:54 +01004114 msg_didout = FALSE; // overwrite this message
4115 msg_nowait = TRUE; // don't wait for this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 msg_col = 0;
Bram Moolenaared203462004-06-16 11:19:22 +00004117
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 --no_wait_return;
4119}
4120
Bram Moolenaar113e1072019-01-20 15:30:40 +01004121#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarf8be4612017-06-23 20:52:40 +02004122 void
4123give_warning2(char_u *message, char_u *a1, int hl)
4124{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +02004125 if (IObuff == NULL)
4126 {
4127 // Very early in initialisation and already something wrong, just give
4128 // the raw message so the user at least gets a hint.
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00004129 give_warning(message, hl);
Bram Moolenaare3a22cb2019-10-14 22:01:57 +02004130 }
4131 else
4132 {
4133 vim_snprintf((char *)IObuff, IOSIZE, (char *)message, a1);
4134 give_warning(IObuff, hl);
4135 }
Bram Moolenaarf8be4612017-06-23 20:52:40 +02004136}
Bram Moolenaar113e1072019-01-20 15:30:40 +01004137#endif
Bram Moolenaarf8be4612017-06-23 20:52:40 +02004138
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139/*
4140 * Advance msg cursor to column "col".
4141 */
4142 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004143msg_advance(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144{
Bram Moolenaar85a20022019-12-21 18:25:54 +01004145 if (msg_silent != 0) // nothing to advance to
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004147 msg_col = col; // for redirection, may fill it up later
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 return;
4149 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01004150 if (col >= Columns) // not enough room
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 col = Columns - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004152#ifdef FEAT_RIGHTLEFT
4153 if (cmdmsg_rl)
4154 while (msg_col > Columns - col)
4155 msg_putchar(' ');
4156 else
4157#endif
4158 while (msg_col < col)
4159 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160}
4161
Christian Brabandt45e07042024-11-11 20:52:55 +01004162/*
4163 * Warn about missing Clipboard Support
4164 */
4165 void
4166msg_warn_missing_clipboard(void)
4167{
4168 if (!global_busy && !did_warn_clipboard)
4169 {
Christian Brabandt8b94afc2024-11-17 16:02:41 +01004170#ifdef FEAT_CLIPBOARD
4171 msg(_("W23: Clipboard register not available, using register 0"));
4172#else
4173 msg(_("W24: Clipboard register not available. See :h W24"));
4174#endif
Christian Brabandt45e07042024-11-11 20:52:55 +01004175 did_warn_clipboard = TRUE;
4176 }
4177}
4178
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179#if defined(FEAT_CON_DIALOG) || defined(PROTO)
4180/*
4181 * Used for "confirm()" function, and the :confirm command prefix.
4182 * Versions which haven't got flexible dialogs yet, and console
4183 * versions, get this generic handler which uses the command line.
4184 *
4185 * type = one of:
4186 * VIM_QUESTION, VIM_INFO, VIM_WARNING, VIM_ERROR or VIM_GENERIC
4187 * title = title string (can be NULL for default)
4188 * (neither used in console dialogs at the moment)
4189 *
4190 * Format of the "buttons" string:
4191 * "Button1Name\nButton2Name\nButton3Name"
4192 * The first button should normally be the default/accept
4193 * The second button should be the 'Cancel' button
4194 * Other buttons- use your imagination!
4195 * A '&' in a button name becomes a shortcut, so each '&' should be before a
4196 * different letter.
Rob Pilling8196e942022-02-11 15:12:10 +00004197 *
4198 * Returns 0 if cancelled, otherwise the nth button (1-indexed).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004201do_dialog(
4202 int type UNUSED,
4203 char_u *title UNUSED,
4204 char_u *message,
4205 char_u *buttons,
4206 int dfltbutton,
Bram Moolenaar85a20022019-12-21 18:25:54 +01004207 char_u *textfield UNUSED, // IObuff for inputdialog(), NULL
4208 // otherwise
4209 int ex_cmd) // when TRUE pressing : accepts default and starts
4210 // Ex command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211{
4212 int oldState;
4213 int retval = 0;
4214 char_u *hotkeys;
4215 int c;
4216 int i;
Bram Moolenaar27321db2020-07-06 21:24:57 +02004217 tmode_T save_tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218
4219#ifndef NO_CONSOLE
Bram Moolenaar85a20022019-12-21 18:25:54 +01004220 // Don't output anything in silent mode ("ex -s")
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 if (silent_mode)
Bram Moolenaar85a20022019-12-21 18:25:54 +01004222 return dfltbutton; // return default option
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223#endif
4224
4225#ifdef FEAT_GUI_DIALOG
Bram Moolenaar85a20022019-12-21 18:25:54 +01004226 // When GUI is running and 'c' not in 'guioptions', use the GUI dialog
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
4228 {
Bram Moolenaar2d12c252022-06-13 21:42:45 +01004229 // --gui-dialog-file: write text to a file
4230 if (gui_dialog_log(title, message))
4231 c = dfltbutton;
4232 else
4233 c = gui_mch_dialog(type, title, message, buttons, dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004234 textfield, ex_cmd);
Bram Moolenaar85a20022019-12-21 18:25:54 +01004235 // avoid a hit-enter prompt without clearing the cmdline
Bram Moolenaar8de49e12009-02-11 17:47:54 +00004236 need_wait_return = FALSE;
4237 emsg_on_display = FALSE;
4238 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239
Bram Moolenaar85a20022019-12-21 18:25:54 +01004240 // Flush output to avoid that further messages and redrawing is done
4241 // in the wrong order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 out_flush();
4243 gui_mch_update();
4244
4245 return c;
4246 }
4247#endif
4248
4249 oldState = State;
Bram Moolenaar24959102022-05-07 20:01:16 +01004250 State = MODE_CONFIRM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252
Bram Moolenaar27321db2020-07-06 21:24:57 +02004253 // Ensure raw mode here.
4254 save_tmode = cur_tmode;
4255 settmode(TMODE_RAW);
4256
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 /*
4258 * Since we wait for a keypress, don't make the
4259 * user press RETURN as well afterwards.
4260 */
4261 ++no_wait_return;
4262 hotkeys = msg_show_console_dialog(message, buttons, dfltbutton);
4263
4264 if (hotkeys != NULL)
4265 {
4266 for (;;)
4267 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004268 // Get a typed character directly from the user.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 c = get_keystroke();
4270 switch (c)
4271 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004272 case CAR: // User accepts default option
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 case NL:
4274 retval = dfltbutton;
4275 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01004276 case Ctrl_C: // User aborts/cancels
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 case ESC:
4278 retval = 0;
4279 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01004280 default: // Could be a hotkey?
4281 if (c < 0) // special keys are ignored here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 continue;
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004283 if (c == ':' && ex_cmd)
4284 {
4285 retval = dfltbutton;
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02004286 ins_char_typebuf(':', 0);
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004287 break;
4288 }
4289
Bram Moolenaar85a20022019-12-21 18:25:54 +01004290 // Make the character lowercase, as chars in "hotkeys" are.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 c = MB_TOLOWER(c);
4292 retval = 1;
4293 for (i = 0; hotkeys[i]; ++i)
4294 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 if (has_mbyte)
4296 {
4297 if ((*mb_ptr2char)(hotkeys + i) == c)
4298 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004299 i += (*mb_ptr2len)(hotkeys + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 }
4301 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 if (hotkeys[i] == c)
4303 break;
4304 ++retval;
4305 }
4306 if (hotkeys[i])
4307 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01004308 // No hotkey match, so keep waiting
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 continue;
4310 }
4311 break;
4312 }
4313
4314 vim_free(hotkeys);
4315 }
4316
Bram Moolenaar27321db2020-07-06 21:24:57 +02004317 settmode(save_tmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 State = oldState;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 --no_wait_return;
4321 msg_end_prompt();
4322
4323 return retval;
4324}
4325
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326/*
4327 * Copy one character from "*from" to "*to", taking care of multi-byte
4328 * characters. Return the length of the character in bytes.
4329 */
4330 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004331copy_char(
4332 char_u *from,
4333 char_u *to,
Bram Moolenaar85a20022019-12-21 18:25:54 +01004334 int lowercase) // make character lower case
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 int len;
4337 int c;
4338
4339 if (has_mbyte)
4340 {
4341 if (lowercase)
4342 {
4343 c = MB_TOLOWER((*mb_ptr2char)(from));
4344 return (*mb_char2bytes)(c, to);
4345 }
4346 else
4347 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004348 len = (*mb_ptr2len)(from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 mch_memmove(to, from, (size_t)len);
4350 return len;
4351 }
4352 }
4353 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 {
4355 if (lowercase)
4356 *to = (char_u)TOLOWER_LOC(*from);
4357 else
4358 *to = *from;
4359 return 1;
4360 }
4361}
4362
4363/*
4364 * Format the dialog string, and display it at the bottom of
4365 * the screen. Return a string of hotkey chars (if defined) for
4366 * each 'button'. If a button has no hotkey defined, the first character of
4367 * the button is used.
4368 * The hotkeys can be multi-byte characters, but without combining chars.
4369 *
4370 * Returns an allocated string with hotkeys, or NULL for error.
4371 */
4372 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004373msg_show_console_dialog(
4374 char_u *message,
4375 char_u *buttons,
4376 int dfltbutton)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377{
4378 int len = 0;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01004379#define HOTK_LEN (has_mbyte ? MB_MAXBYTES : 1)
Bram Moolenaar85a20022019-12-21 18:25:54 +01004380 int lenhotkey = HOTK_LEN; // count first button
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 char_u *hotk = NULL;
4382 char_u *msgp = NULL;
4383 char_u *hotkp = NULL;
4384 char_u *r;
4385 int copy;
4386#define HAS_HOTKEY_LEN 30
4387 char_u has_hotkey[HAS_HOTKEY_LEN];
Bram Moolenaar85a20022019-12-21 18:25:54 +01004388 int first_hotkey = FALSE; // first char of button is hotkey
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 int idx;
4390
4391 has_hotkey[0] = FALSE;
4392
4393 /*
4394 * First loop: compute the size of memory to allocate.
4395 * Second loop: copy to the allocated memory.
4396 */
4397 for (copy = 0; copy <= 1; ++copy)
4398 {
4399 r = buttons;
4400 idx = 0;
4401 while (*r)
4402 {
4403 if (*r == DLG_BUTTON_SEP)
4404 {
4405 if (copy)
4406 {
4407 *msgp++ = ',';
Bram Moolenaar85a20022019-12-21 18:25:54 +01004408 *msgp++ = ' '; // '\n' -> ', '
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409
Bram Moolenaar85a20022019-12-21 18:25:54 +01004410 // advance to next hotkey and set default hotkey
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 if (has_mbyte)
Bram Moolenaar6a516062007-07-05 08:11:42 +00004412 hotkp += STRLEN(hotkp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 ++hotkp;
Bram Moolenaar6a516062007-07-05 08:11:42 +00004415 hotkp[copy_char(r + 1, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416 if (dfltbutton)
4417 --dfltbutton;
4418
Bram Moolenaar85a20022019-12-21 18:25:54 +01004419 // If no hotkey is specified first char is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420 if (idx < HAS_HOTKEY_LEN - 1 && !has_hotkey[++idx])
4421 first_hotkey = TRUE;
4422 }
4423 else
4424 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004425 len += 3; // '\n' -> ', '; 'x' -> '(x)'
4426 lenhotkey += HOTK_LEN; // each button needs a hotkey
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427 if (idx < HAS_HOTKEY_LEN - 1)
4428 has_hotkey[++idx] = FALSE;
4429 }
4430 }
4431 else if (*r == DLG_HOTKEY_CHAR || first_hotkey)
4432 {
4433 if (*r == DLG_HOTKEY_CHAR)
4434 ++r;
4435 first_hotkey = FALSE;
4436 if (copy)
4437 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004438 if (*r == DLG_HOTKEY_CHAR) // '&&a' -> '&a'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 *msgp++ = *r;
4440 else
4441 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004442 // '&a' -> '[a]'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 *msgp++ = (dfltbutton == 1) ? '[' : '(';
4444 msgp += copy_char(r, msgp, FALSE);
4445 *msgp++ = (dfltbutton == 1) ? ']' : ')';
4446
Bram Moolenaar85a20022019-12-21 18:25:54 +01004447 // redefine hotkey
Bram Moolenaar6a516062007-07-05 08:11:42 +00004448 hotkp[copy_char(r, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 }
4450 }
4451 else
4452 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004453 ++len; // '&a' -> '[a]'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 if (idx < HAS_HOTKEY_LEN - 1)
4455 has_hotkey[idx] = TRUE;
4456 }
4457 }
4458 else
4459 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004460 // everything else copy literally
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 if (copy)
4462 msgp += copy_char(r, msgp, FALSE);
4463 }
4464
Bram Moolenaar85a20022019-12-21 18:25:54 +01004465 // advance to the next character
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004466 MB_PTR_ADV(r);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467 }
4468
4469 if (copy)
4470 {
4471 *msgp++ = ':';
4472 *msgp++ = ' ';
4473 *msgp = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 }
4475 else
4476 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004477 len += (int)(STRLEN(message)
Bram Moolenaar85a20022019-12-21 18:25:54 +01004478 + 2 // for the NL's
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004479 + STRLEN(buttons)
Bram Moolenaar85a20022019-12-21 18:25:54 +01004480 + 3); // for the ": " and NUL
4481 lenhotkey++; // for the NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482
Bram Moolenaar85a20022019-12-21 18:25:54 +01004483 // If no hotkey is specified first char is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 if (!has_hotkey[0])
4485 {
4486 first_hotkey = TRUE;
Bram Moolenaar85a20022019-12-21 18:25:54 +01004487 len += 2; // "x" -> "[x]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 }
4489
4490 /*
4491 * Now allocate and load the strings
4492 */
4493 vim_free(confirm_msg);
4494 confirm_msg = alloc(len);
4495 if (confirm_msg == NULL)
4496 return NULL;
4497 *confirm_msg = NUL;
4498 hotk = alloc(lenhotkey);
4499 if (hotk == NULL)
4500 return NULL;
4501
4502 *confirm_msg = '\n';
4503 STRCPY(confirm_msg + 1, message);
4504
4505 msgp = confirm_msg + 1 + STRLEN(message);
4506 hotkp = hotk;
4507
Bram Moolenaar85a20022019-12-21 18:25:54 +01004508 // Define first default hotkey. Keep the hotkey string NUL
4509 // terminated to avoid reading past the end.
Bram Moolenaar6a516062007-07-05 08:11:42 +00004510 hotkp[copy_char(buttons, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511
Bram Moolenaar85a20022019-12-21 18:25:54 +01004512 // Remember where the choices start, displaying starts here when
4513 // "hotkp" typed at the more prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 confirm_msg_tail = msgp;
4515 *msgp++ = '\n';
4516 }
4517 }
4518
4519 display_confirm_msg();
4520 return hotk;
4521}
4522
4523/*
4524 * Display the ":confirm" message. Also called when screen resized.
4525 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02004526 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004527display_confirm_msg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528{
Bram Moolenaar85a20022019-12-21 18:25:54 +01004529 // avoid that 'q' at the more prompt truncates the message here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 ++confirm_msg_used;
4531 if (confirm_msg != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004532 msg_puts_attr((char *)confirm_msg, HL_ATTR(HLF_M));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 --confirm_msg_used;
4534}
4535
Bram Moolenaar85a20022019-12-21 18:25:54 +01004536#endif // FEAT_CON_DIALOG
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537
4538#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4539
4540 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004541vim_dialog_yesno(
4542 int type,
4543 char_u *title,
4544 char_u *message,
4545 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546{
4547 if (do_dialog(type,
4548 title == NULL ? (char_u *)_("Question") : title,
4549 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004550 (char_u *)_("&Yes\n&No"), dflt, NULL, FALSE) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 return VIM_YES;
4552 return VIM_NO;
4553}
4554
4555 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004556vim_dialog_yesnocancel(
4557 int type,
4558 char_u *title,
4559 char_u *message,
4560 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561{
4562 switch (do_dialog(type,
4563 title == NULL ? (char_u *)_("Question") : title,
4564 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004565 (char_u *)_("&Yes\n&No\n&Cancel"), dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 {
4567 case 1: return VIM_YES;
4568 case 2: return VIM_NO;
4569 }
4570 return VIM_CANCEL;
4571}
4572
4573 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004574vim_dialog_yesnoallcancel(
4575 int type,
4576 char_u *title,
4577 char_u *message,
4578 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579{
4580 switch (do_dialog(type,
4581 title == NULL ? (char_u *)"Question" : title,
4582 message,
4583 (char_u *)_("&Yes\n&No\nSave &All\n&Discard All\n&Cancel"),
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004584 dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 {
4586 case 1: return VIM_YES;
4587 case 2: return VIM_NO;
4588 case 3: return VIM_ALL;
4589 case 4: return VIM_DISCARDALL;
4590 }
4591 return VIM_CANCEL;
4592}
4593
Bram Moolenaar85a20022019-12-21 18:25:54 +01004594#endif // FEAT_GUI_DIALOG || FEAT_CON_DIALOG