blob: 24984123b4398e4f4f122b53da128a50312ad41f [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * message.c: functions for displaying messages on the command line
12 */
13
Bram Moolenaar85a20022019-12-21 18:25:54 +010014#define MESSAGE_FILE // don't include prototype for smsg()
Bram Moolenaar071d4272004-06-13 20:20:40 +000015
16#include "vim.h"
17
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010018static void add_msg_hist(char_u *s, int len, int attr);
Christian Brabandt51d4d842024-12-06 17:26:25 +010019static void check_msg_hist(void);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010020static void hit_return_msg(void);
21static void msg_home_replace_attr(char_u *fname, int attr);
Bram Moolenaar32526b32019-01-19 17:43:09 +010022static void msg_puts_attr_len(char *str, int maxlen, int attr);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010023static void msg_puts_display(char_u *str, int maxlen, int attr, int recurse);
24static void msg_scroll_up(void);
25static void inc_msg_scrolled(void);
26static void store_sb_text(char_u **sb_str, char_u *s, int attr, int *sb_col, int finish);
27static void t_puts(int *t_col, char_u *t_s, char_u *s, int attr);
28static void msg_puts_printf(char_u *str, int maxlen);
29static int do_more_prompt(int typed_char);
30static void msg_screen_putchar(int c, int attr);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020031static void msg_moremsg(int full);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010032static int msg_check_screen(void);
33static void redir_write(char_u *s, int maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000034#ifdef FEAT_CON_DIALOG
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010035static char_u *msg_show_console_dialog(char_u *message, char_u *buttons, int dfltbutton);
Bram Moolenaar85a20022019-12-21 18:25:54 +010036static int confirm_msg_used = FALSE; // displaying confirm_msg
37static char_u *confirm_msg = NULL; // ":confirm" message
38static char_u *confirm_msg_tail; // tail of confirm_msg
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020039static void display_confirm_msg(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000040#endif
Bram Moolenaar3b8c7082022-11-30 20:20:56 +000041#ifdef FEAT_EVAL
Bram Moolenaar958dc692016-12-01 15:34:12 +010042static int emsg_to_channel_log = FALSE;
43#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000044
45struct msg_hist
46{
47 struct msg_hist *next;
48 char_u *msg;
49 int attr;
50};
51
52static struct msg_hist *first_msg_hist = NULL;
53static struct msg_hist *last_msg_hist = NULL;
54static int msg_hist_len = 0;
Christian Brabandt51d4d842024-12-06 17:26:25 +010055static int msg_hist_max = 500; // The default max value is 500
56
57// flags obtained from the 'messagesopt' option
58#define MESSAGES_HIT_ENTER 0x001
59#define MESSAGES_WAIT 0x002
60#define MESSAGES_HISTORY 0x004
61
62// args in 'messagesopt' option
63#define MESSAGES_OPT_HIT_ENTER "hit-enter"
64#define MESSAGES_OPT_WAIT "wait:"
65#define MESSAGES_OPT_HISTORY "history:"
66
67// The default is "hit-enter,history:500"
68static int msg_flags = MESSAGES_HIT_ENTER | MESSAGES_HISTORY;
69static int msg_wait = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000070
Bram Moolenaarb5b5b892011-09-14 15:39:29 +020071static FILE *verbose_fd = NULL;
72static int verbose_did_open = FALSE;
73
Christian Brabandt45e07042024-11-11 20:52:55 +010074static int did_warn_clipboard = FALSE;
Christian Brabandt45e07042024-11-11 20:52:55 +010075
Bram Moolenaar071d4272004-06-13 20:20:40 +000076/*
77 * When writing messages to the screen, there are many different situations.
78 * A number of variables is used to remember the current state:
79 * msg_didany TRUE when messages were written since the last time the
80 * user reacted to a prompt.
81 * Reset: After hitting a key for the hit-return prompt,
82 * hitting <CR> for the command line or input().
83 * Set: When any message is written to the screen.
84 * msg_didout TRUE when something was written to the current line.
85 * Reset: When advancing to the next line, when the current
86 * text can be overwritten.
87 * Set: When any message is written to the screen.
88 * msg_nowait No extra delay for the last drawn message.
89 * Used in normal_cmd() before the mode message is drawn.
90 * emsg_on_display There was an error message recently. Indicates that there
91 * should be a delay before redrawing.
92 * msg_scroll The next message should not overwrite the current one.
93 * msg_scrolled How many lines the screen has been scrolled (because of
94 * messages). Used in update_screen() to scroll the screen
95 * back. Incremented each time the screen scrolls a line.
96 * msg_scrolled_ign TRUE when msg_scrolled is non-zero and msg_puts_attr()
97 * writes something without scrolling should not make
98 * need_wait_return to be set. This is a hack to make ":ts"
99 * work without an extra prompt.
100 * lines_left Number of lines available for messages before the
Bram Moolenaar5d6f75e2011-12-30 14:14:29 +0100101 * more-prompt is to be given. -1 when not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000102 * need_wait_return TRUE when the hit-return prompt is needed.
103 * Reset: After giving the hit-return prompt, when the user
104 * has answered some other prompt.
105 * Set: When the ruler or typeahead display is overwritten,
106 * scrolling the screen for some message.
107 * keep_msg Message to be displayed after redrawing the screen, in
108 * main_loop().
109 * This is an allocated string or NULL when not used.
110 */
111
112/*
113 * msg(s) - displays the string 's' on the status line
114 * When terminal not initialized (yet) mch_errmsg(..) is used.
Bram Moolenaar13608d82022-08-29 15:06:50 +0100115 * return TRUE if wait_return() not called
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116 */
117 int
Bram Moolenaar32526b32019-01-19 17:43:09 +0100118msg(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119{
120 return msg_attr_keep(s, 0, FALSE);
121}
122
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000123/*
124 * Like msg() but keep it silent when 'verbosefile' is set.
125 */
126 int
Bram Moolenaar32526b32019-01-19 17:43:09 +0100127verb_msg(char *s)
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000128{
129 int n;
130
131 verbose_enter();
132 n = msg_attr_keep(s, 0, FALSE);
133 verbose_leave();
134
135 return n;
136}
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000137
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138 int
Bram Moolenaar32526b32019-01-19 17:43:09 +0100139msg_attr(char *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140{
141 return msg_attr_keep(s, attr, FALSE);
142}
143
144 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100145msg_attr_keep(
Bram Moolenaar32526b32019-01-19 17:43:09 +0100146 char *s,
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100147 int attr,
Bram Moolenaar85a20022019-12-21 18:25:54 +0100148 int keep) // TRUE: set keep_msg if it doesn't scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149{
150 static int entered = 0;
151 int retval;
152 char_u *buf = NULL;
153
Bram Moolenaar85a20022019-12-21 18:25:54 +0100154 // Skip messages not matching ":filter pattern".
155 // Don't filter when there is an error.
Bram Moolenaar32526b32019-01-19 17:43:09 +0100156 if (!emsg_on_display && message_filtered((char_u *)s))
Bram Moolenaar7b668e82016-08-23 23:51:21 +0200157 return TRUE;
158
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159#ifdef FEAT_EVAL
160 if (attr == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100161 set_vim_var_string(VV_STATUSMSG, (char_u *)s, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162#endif
163
164 /*
165 * It is possible that displaying a messages causes a problem (e.g.,
166 * when redrawing the window), which causes another message, etc.. To
167 * break this loop, limit the recursiveness to 3 levels.
168 */
169 if (entered >= 3)
170 return TRUE;
171 ++entered;
172
Bram Moolenaar85a20022019-12-21 18:25:54 +0100173 // Add message to history (unless it's a repeated kept message or a
174 // truncated message)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100175 if ((char_u *)s != keep_msg
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176 || (*s != '<'
177 && last_msg_hist != NULL
178 && last_msg_hist->msg != NULL
179 && STRCMP(s, last_msg_hist->msg)))
Bram Moolenaar32526b32019-01-19 17:43:09 +0100180 add_msg_hist((char_u *)s, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181
Bram Moolenaar4c5678f2022-11-30 18:12:19 +0000182#ifdef FEAT_EVAL
Bram Moolenaar958dc692016-12-01 15:34:12 +0100183 if (emsg_to_channel_log)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100184 // Write message in the channel log.
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +0000185 ch_log(NULL, "ERROR: %s", s);
Bram Moolenaar958dc692016-12-01 15:34:12 +0100186#endif
187
Bram Moolenaar85a20022019-12-21 18:25:54 +0100188 // Truncate the message if needed.
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000189 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +0100190 buf = msg_strtrunc((char_u *)s, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191 if (buf != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100192 s = (char *)buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193
Bram Moolenaar32526b32019-01-19 17:43:09 +0100194 msg_outtrans_attr((char_u *)s, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195 msg_clr_eos();
196 retval = msg_end();
197
Bram Moolenaar32526b32019-01-19 17:43:09 +0100198 if (keep && retval && vim_strsize((char_u *)s)
199 < (int)(Rows - cmdline_row - 1) * Columns + sc_col)
200 set_keep_msg((char_u *)s, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201
Rob Pilling726f7f92022-01-20 14:44:38 +0000202 need_fileinfo = FALSE;
203
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204 vim_free(buf);
205 --entered;
206 return retval;
207}
208
209/*
210 * Truncate a string such that it can be printed without causing a scroll.
211 * Returns an allocated string or NULL when no truncating is done.
212 */
213 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100214msg_strtrunc(
215 char_u *s,
Bram Moolenaar85a20022019-12-21 18:25:54 +0100216 int force) // always truncate
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217{
218 char_u *buf = NULL;
219 int len;
220 int room;
221
Bram Moolenaar85a20022019-12-21 18:25:54 +0100222 // May truncate message to avoid a hit-return prompt
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000223 if ((!msg_scroll && !need_wait_return && shortmess(SHM_TRUNCALL)
224 && !exmode_active && msg_silent == 0) || force)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225 {
226 len = vim_strsize(s);
Bram Moolenaar35a4fbc2022-08-28 14:39:53 +0100227 if (msg_scrolled != 0
228#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaara2a89732022-08-31 14:46:18 +0100229 || in_echowindow
Bram Moolenaar35a4fbc2022-08-28 14:39:53 +0100230#endif
231 )
Bram Moolenaar85a20022019-12-21 18:25:54 +0100232 // Use all the columns.
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000233 room = (int)(Rows - msg_row) * Columns - 1;
234 else
Bram Moolenaar85a20022019-12-21 18:25:54 +0100235 // Use up to 'showcmd' column.
Bram Moolenaar7ca30432005-09-09 19:44:31 +0000236 room = (int)(Rows - msg_row - 1) * Columns + sc_col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237 if (len > room && room > 0)
238 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239 if (enc_utf8)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100240 // may have up to 18 bytes per cell (6 per char, up to two
241 // composing chars)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100242 len = (room + 2) * 18;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243 else if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100244 // may have up to 2 bytes per cell for euc-jp
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100245 len = (room + 2) * 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246 else
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100247 len = room + 2;
248 buf = alloc(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249 if (buf != NULL)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100250 trunc_string(s, buf, room, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251 }
252 }
253 return buf;
254}
255
256/*
257 * Truncate a string "s" to "buf" with cell width "room".
258 * "s" and "buf" may be equal.
259 */
260 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100261trunc_string(
262 char_u *s,
263 char_u *buf,
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200264 int room_in,
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100265 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266{
Bram Moolenaar85a20022019-12-21 18:25:54 +0100267 size_t room = room_in - 3; // "..." takes 3 chars
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200268 size_t half;
269 size_t len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270 int e;
271 int i;
272 int n;
273
Bram Moolenaar62818152021-02-14 15:37:30 +0100274 if (*s == NUL)
275 {
276 if (buflen > 0)
277 *buf = NUL;
278 return;
279 }
280
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200281 if (room_in < 3)
282 room = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000283 half = room / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284
Bram Moolenaar85a20022019-12-21 18:25:54 +0100285 // First part: Start of the string.
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100286 for (e = 0; len < half && e < buflen; ++e)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000287 {
288 if (s[e] == NUL)
289 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100290 // text fits without truncating!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291 buf[e] = NUL;
292 return;
293 }
294 n = ptr2cells(s + e);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200295 if (len + n > half)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000296 break;
297 len += n;
298 buf[e] = s[e];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000299 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000300 for (n = (*mb_ptr2len)(s + e); --n > 0; )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100302 if (++e == buflen)
303 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304 buf[e] = s[e];
305 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306 }
307
Bram Moolenaar85a20022019-12-21 18:25:54 +0100308 // Last part: End of the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309 i = e;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000310 if (enc_dbcs != 0)
311 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100312 // For DBCS going backwards in a string is slow, but
313 // computing the cell width isn't too slow: go forward
314 // until the rest fits.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315 n = vim_strsize(s + i);
316 while (len + n > room)
317 {
318 n -= ptr2cells(s + i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000319 i += (*mb_ptr2len)(s + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320 }
321 }
322 else if (enc_utf8)
323 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100324 // For UTF-8 we can go backwards easily.
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000325 half = i = (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326 for (;;)
327 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000328 do
Bram Moolenaarace95982017-03-29 17:30:27 +0200329 half = half - utf_head_off(s, s + half - 1) - 1;
Bram Moolenaarb9644432016-07-19 12:33:44 +0200330 while (half > 0 && utf_iscomposing(utf_ptr2char(s + half)));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331 n = ptr2cells(s + half);
Bram Moolenaarb9644432016-07-19 12:33:44 +0200332 if (len + n > room || half == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333 break;
334 len += n;
Bram Moolenaara5c0cc12016-07-30 16:40:39 +0200335 i = (int)half;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336 }
337 }
338 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339 {
Bram Moolenaard0337e32019-12-30 17:55:34 +0100340 for (i = (int)STRLEN(s);
341 i - 1 >= 0 && len + (n = ptr2cells(s + i - 1)) <= room; --i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342 len += n;
343 }
344
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200345
346 if (i <= e + 3)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100347 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100348 // text fits without truncating
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200349 if (s != buf)
350 {
351 len = STRLEN(s);
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200352 if (len >= (size_t)buflen)
Bram Moolenaarf6acffb2016-07-16 16:54:24 +0200353 len = buflen - 1;
354 len = len - e + 1;
355 if (len < 1)
356 buf[e - 1] = NUL;
357 else
358 mch_memmove(buf + e, s + e, len);
359 }
360 }
361 else if (e + 3 < buflen)
362 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100363 // set the middle and copy the last part
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100364 mch_memmove(buf + e, "...", (size_t)3);
Bram Moolenaard4f31dc2016-07-23 17:28:22 +0200365 len = STRLEN(s + i) + 1;
366 if (len >= (size_t)buflen - e - 3)
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100367 len = buflen - e - 3 - 1;
368 mch_memmove(buf + e + 3, s + i, len);
369 buf[e + 3 + len - 1] = NUL;
370 }
371 else
372 {
Bram Moolenaar85a20022019-12-21 18:25:54 +0100373 // can't fit in the "...", just truncate it
Christian Brabandt3bd7fa12023-10-02 20:59:08 +0200374 buf[buflen - 1] = NUL;
Bram Moolenaarf31b7642012-01-20 20:44:43 +0100375 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376}
377
378/*
379 * Automatic prototype generation does not understand this function.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100380 * Note: Caller of smsg() and smsg_attr() must check the resulting string is
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381 * shorter than IOSIZE!!!
382 */
383#ifndef PROTO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100385int vim_snprintf(char *str, size_t str_m, const char *fmt, ...);
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000386
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100388smsg(const char *s, ...)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200390 if (IObuff == NULL)
391 {
392 // Very early in initialisation and already something wrong, just
393 // give the raw message so the user at least gets a hint.
394 return msg((char *)s);
395 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000397 va_list arglist;
398
399 va_start(arglist, s);
400 vim_vsnprintf((char *)IObuff, IOSIZE, s, arglist);
401 va_end(arglist);
402 return msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403}
404
405 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100406smsg_attr(int attr, const char *s, ...)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200408 if (IObuff == NULL)
409 {
410 // Very early in initialisation and already something wrong, just
411 // give the raw message so the user at least gets a hint.
412 return msg_attr((char *)s, attr);
413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000415 va_list arglist;
416
417 va_start(arglist, s);
418 vim_vsnprintf((char *)IObuff, IOSIZE, s, arglist);
419 va_end(arglist);
420 return msg_attr((char *)IObuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421}
422
Bram Moolenaare0429682018-07-01 16:44:03 +0200423 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100424smsg_attr_keep(int attr, const char *s, ...)
Bram Moolenaare0429682018-07-01 16:44:03 +0200425{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200426 if (IObuff == NULL)
427 {
428 // Very early in initialisation and already something wrong, just
429 // give the raw message so the user at least gets a hint.
430 return msg_attr_keep((char *)s, attr, TRUE);
431 }
Bram Moolenaare0429682018-07-01 16:44:03 +0200432
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000433 va_list arglist;
434
435 va_start(arglist, s);
436 vim_vsnprintf((char *)IObuff, IOSIZE, s, arglist);
437 va_end(arglist);
438 return msg_attr_keep((char *)IObuff, attr, TRUE);
Bram Moolenaare0429682018-07-01 16:44:03 +0200439}
440
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441#endif
442
443/*
444 * Remember the last sourcing name/lnum used in an error message, so that it
445 * isn't printed each time when it didn't change.
446 */
447static int last_sourcing_lnum = 0;
448static char_u *last_sourcing_name = NULL;
449
450/*
451 * Reset the last used sourcing name/lnum. Makes sure it is displayed again
452 * for the next error message;
453 */
Bram Moolenaar4eec5ec2005-06-26 22:26:21 +0000454 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100455reset_last_sourcing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000456{
Bram Moolenaard23a8232018-02-10 18:45:26 +0100457 VIM_CLEAR(last_sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458 last_sourcing_lnum = 0;
459}
460
461/*
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100462 * Return TRUE if "SOURCING_NAME" differs from "last_sourcing_name".
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000463 */
464 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100465other_sourcing_name(void)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000466{
Bram Moolenaar0bd8d052021-11-25 13:39:28 +0000467 if (HAVE_SOURCING_INFO && SOURCING_NAME != NULL)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000468 {
469 if (last_sourcing_name != NULL)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100470 return STRCMP(SOURCING_NAME, last_sourcing_name) != 0;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000471 return TRUE;
472 }
473 return FALSE;
474}
475
476/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477 * Get the message about the source, as used for an error message.
478 * Returns an allocated string with room for one more character.
479 * Returns NULL when no message is to be given.
480 */
481 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100482get_emsg_source(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483{
484 char_u *Buf, *p;
485
Bram Moolenaar0bd8d052021-11-25 13:39:28 +0000486 if (HAVE_SOURCING_INFO && SOURCING_NAME != NULL && other_sourcing_name())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487 {
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +0200488 char_u *sname = estack_sfile(ESTACK_NONE);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100489 char_u *tofree = sname;
490
491 if (sname == NULL)
492 sname = SOURCING_NAME;
493
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +0200494#ifdef FEAT_EVAL
495 if (estack_compiling)
496 p = (char_u *)_("Error detected while compiling %s:");
497 else
498#endif
499 p = (char_u *)_("Error detected while processing %s:");
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100500 Buf = alloc(STRLEN(sname) + STRLEN(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501 if (Buf != NULL)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100502 sprintf((char *)Buf, (char *)p, sname);
503 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504 return Buf;
505 }
506 return NULL;
507}
508
509/*
510 * Get the message about the source lnum, as used for an error message.
511 * Returns an allocated string with room for one more character.
512 * Returns NULL when no message is to be given.
513 */
514 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100515get_emsg_lnum(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516{
517 char_u *Buf, *p;
518
Bram Moolenaar85a20022019-12-21 18:25:54 +0100519 // lnum is 0 when executing a command from the command line
520 // argument, we don't want a line number then
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100521 if (SOURCING_NAME != NULL
522 && (other_sourcing_name() || SOURCING_LNUM != last_sourcing_lnum)
523 && SOURCING_LNUM != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524 {
525 p = (char_u *)_("line %4ld:");
Bram Moolenaar964b3742019-05-24 18:54:09 +0200526 Buf = alloc(STRLEN(p) + 20);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527 if (Buf != NULL)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100528 sprintf((char *)Buf, (char *)p, (long)SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 return Buf;
530 }
531 return NULL;
532}
533
534/*
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000535 * Display name and line number for the source of an error.
536 * Remember the file name and line number, so that for the next error the info
537 * is only displayed if it changed.
538 */
539 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100540msg_source(int attr)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000541{
542 char_u *p;
Bram Moolenaarba8c9262021-11-25 14:43:18 +0000543 static int recursive = FALSE;
544
545 // Bail out if something called here causes an error.
546 if (recursive)
547 return;
548 recursive = TRUE;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000549
550 ++no_wait_return;
551 p = get_emsg_source();
552 if (p != NULL)
553 {
zeertzjqbdedd2b2022-09-20 12:45:15 +0100554 msg_scroll = TRUE; // this will take more than one line
Bram Moolenaar32526b32019-01-19 17:43:09 +0100555 msg_attr((char *)p, attr);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000556 vim_free(p);
557 }
558 p = get_emsg_lnum();
559 if (p != NULL)
560 {
Bram Moolenaar32526b32019-01-19 17:43:09 +0100561 msg_attr((char *)p, HL_ATTR(HLF_N));
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000562 vim_free(p);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100563 last_sourcing_lnum = SOURCING_LNUM; // only once for each line
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000564 }
565
Bram Moolenaar85a20022019-12-21 18:25:54 +0100566 // remember the last sourcing name printed, also when it's empty
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100567 if (SOURCING_NAME == NULL || other_sourcing_name())
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000568 {
Bram Moolenaarba8c9262021-11-25 14:43:18 +0000569 VIM_CLEAR(last_sourcing_name);
570 if (SOURCING_NAME != NULL)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100571 last_sourcing_name = vim_strsave(SOURCING_NAME);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000572 }
573 --no_wait_return;
Bram Moolenaarba8c9262021-11-25 14:43:18 +0000574
575 recursive = FALSE;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000576}
577
578/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000579 * Return TRUE if not giving error messages right now:
580 * If "emsg_off" is set: no error messages at the moment.
581 * If "msg" is in 'debug': do error message but without side effects.
582 * If "emsg_skip" is set: never do error messages.
583 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +0200584 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100585emsg_not_now(void)
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000586{
587 if ((emsg_off > 0 && vim_strchr(p_debug, 'm') == NULL
588 && vim_strchr(p_debug, 't') == NULL)
589#ifdef FEAT_EVAL
590 || emsg_skip > 0
591#endif
592 )
593 return TRUE;
594 return FALSE;
595}
596
Bram Moolenaar5a66dfb2017-03-01 20:40:39 +0100597#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100598static garray_T ignore_error_list = GA_EMPTY;
599
600 void
601ignore_error_for_testing(char_u *error)
602{
603 if (ignore_error_list.ga_itemsize == 0)
604 ga_init2(&ignore_error_list, sizeof(char_u *), 1);
605
Bram Moolenaar461a7fc2018-12-22 13:28:07 +0100606 if (STRCMP("RESET", error) == 0)
607 ga_clear_strings(&ignore_error_list);
608 else
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000609 ga_copy_string(&ignore_error_list, error);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100610}
611
612 static int
Bram Moolenaar097c5372023-05-24 21:02:24 +0100613ignore_error(const char *msg)
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100614{
615 int i;
616
617 for (i = 0; i < ignore_error_list.ga_len; ++i)
Bram Moolenaar097c5372023-05-24 21:02:24 +0100618 if (strstr(msg,
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100619 (char *)((char_u **)(ignore_error_list.ga_data))[i]) != NULL)
620 return TRUE;
621 return FALSE;
622}
623#endif
624
Bram Moolenaarb869c0d2016-07-20 00:10:51 +0200625#if !defined(HAVE_STRERROR) || defined(PROTO)
626/*
627 * Replacement for perror() that behaves more or less like emsg() was called.
Bram Moolenaar53989552019-12-23 22:59:18 +0100628 * v:errmsg will be set and called_emsg will be incremented.
Bram Moolenaarb869c0d2016-07-20 00:10:51 +0200629 */
630 void
631do_perror(char *msg)
632{
633 perror(msg);
634 ++emsg_silent;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100635 emsg(msg);
Bram Moolenaarb869c0d2016-07-20 00:10:51 +0200636 --emsg_silent;
637}
638#endif
639
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000640/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100641 * emsg_core() - display an error message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 *
643 * Rings the bell, if appropriate, and calls message() to do the real work
644 * When terminal not initialized (yet) mch_errmsg(..) is used.
645 *
Bram Moolenaar13608d82022-08-29 15:06:50 +0100646 * Return TRUE if wait_return() not called.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100647 * Note: caller must check 'emsg_not_now()' before calling this.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100649 static int
Bram Moolenaar097c5372023-05-24 21:02:24 +0100650emsg_core(const char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651{
652 int attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653 char_u *p;
Bram Moolenaar958dc692016-12-01 15:34:12 +0100654 int r;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655#ifdef FEAT_EVAL
656 int ignore = FALSE;
657 int severe;
658#endif
659
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100660#ifdef FEAT_EVAL
Bram Moolenaar85a20022019-12-21 18:25:54 +0100661 // When testing some errors are turned into a normal message.
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100662 if (ignore_error(s))
Bram Moolenaar85a20022019-12-21 18:25:54 +0100663 // don't call msg() if it results in a dialog
Bram Moolenaar32526b32019-01-19 17:43:09 +0100664 return msg_use_printf() ? FALSE : msg((char *)s);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100665#endif
666
Bram Moolenaar53989552019-12-23 22:59:18 +0100667 ++called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669#ifdef FEAT_EVAL
Bram Moolenaar85a20022019-12-21 18:25:54 +0100670 // If "emsg_severe" is TRUE: When an error exception is to be thrown,
671 // prefer this message over previous messages for the same command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 severe = emsg_severe;
673 emsg_severe = FALSE;
674#endif
675
Bram Moolenaar57657d82006-04-21 22:12:41 +0000676 if (!emsg_off || vim_strchr(p_debug, 't') != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 {
678#ifdef FEAT_EVAL
679 /*
680 * Cause a throw of an error exception if appropriate. Don't display
681 * the error message in this case. (If no matching catch clause will
682 * be found, the message will be displayed later on.) "ignore" is set
683 * when the message should be ignored completely (used for the
684 * interrupt message).
685 */
Bram Moolenaar097c5372023-05-24 21:02:24 +0100686 if (cause_errthrow((char_u *)s, severe, &ignore) == TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 {
688 if (!ignore)
Bram Moolenaar76a63452018-11-28 20:38:37 +0100689 ++did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 return TRUE;
691 }
692
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100693 if (in_assert_fails && emsg_assert_fails_msg == NULL)
Bram Moolenaar1d634542020-08-18 13:41:50 +0200694 {
Bram Moolenaar097c5372023-05-24 21:02:24 +0100695 emsg_assert_fails_msg = vim_strsave((char_u *)s);
Bram Moolenaar1d634542020-08-18 13:41:50 +0200696 emsg_assert_fails_lnum = SOURCING_LNUM;
Bram Moolenaar9bd5d872020-09-06 21:47:48 +0200697 vim_free(emsg_assert_fails_context);
698 emsg_assert_fails_context = vim_strsave(
699 SOURCING_NAME == NULL ? (char_u *)"" : SOURCING_NAME);
Bram Moolenaar1d634542020-08-18 13:41:50 +0200700 }
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200701
Bram Moolenaar85a20022019-12-21 18:25:54 +0100702 // set "v:errmsg", also when using ":silent! cmd"
Bram Moolenaar097c5372023-05-24 21:02:24 +0100703 set_vim_var_string(VV_ERRMSG, (char_u *)s, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704#endif
705
706 /*
Bram Moolenaara7241f52008-06-24 20:39:31 +0000707 * When using ":silent! cmd" ignore error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 * But do write it to the redirection file.
709 */
710 if (emsg_silent != 0)
711 {
Bram Moolenaar599410c2021-04-10 14:03:43 +0200712#ifdef FEAT_EVAL
713 ++did_emsg_silent;
714#endif
Bram Moolenaar79815f12016-07-09 17:07:29 +0200715 if (emsg_noredir == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 {
Bram Moolenaar79815f12016-07-09 17:07:29 +0200717 msg_start();
718 p = get_emsg_source();
719 if (p != NULL)
720 {
721 STRCAT(p, "\n");
722 redir_write(p, -1);
723 vim_free(p);
724 }
725 p = get_emsg_lnum();
726 if (p != NULL)
727 {
728 STRCAT(p, "\n");
729 redir_write(p, -1);
730 vim_free(p);
731 }
Bram Moolenaar097c5372023-05-24 21:02:24 +0100732 redir_write((char_u *)s, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +0100734#ifdef FEAT_EVAL
735 // Only increment did_emsg_def when :silent! wasn't used inside the
736 // :def function.
737 if (emsg_silent == emsg_silent_def)
738 ++did_emsg_def;
739#endif
Bram Moolenaar4c5678f2022-11-30 18:12:19 +0000740#ifdef FEAT_EVAL
Bram Moolenaar097c5372023-05-24 21:02:24 +0100741 ch_log(NULL, "ERROR silent: %s", s);
Bram Moolenaar958dc692016-12-01 15:34:12 +0100742#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743 return TRUE;
744 }
745
Bram Moolenaar2b7bc562017-01-14 19:24:52 +0100746 ex_exitval = 1;
747
Bram Moolenaar85a20022019-12-21 18:25:54 +0100748 // Reset msg_silent, an error causes messages to be switched back on.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 msg_silent = 0;
750 cmd_silent = FALSE;
751
Bram Moolenaar85a20022019-12-21 18:25:54 +0100752 if (global_busy) // break :global command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 ++global_busy;
754
755 if (p_eb)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100756 beep_flush(); // also includes flush_buffers()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 else
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200758 flush_buffers(FLUSH_MINIMAL); // flush internal buffers
Bram Moolenaar76a63452018-11-28 20:38:37 +0100759 ++did_emsg; // flag for DoOneCmd()
Bram Moolenaare723c422017-09-06 23:40:10 +0200760#ifdef FEAT_EVAL
Bram Moolenaar88c89c72021-08-14 14:01:05 +0200761 ++uncaught_emsg;
Bram Moolenaare723c422017-09-06 23:40:10 +0200762#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763 }
764
Bram Moolenaar878e1d22022-08-28 17:53:23 +0100765#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaara2a89732022-08-31 14:46:18 +0100766 if (!in_echowindow)
Bram Moolenaar878e1d22022-08-28 17:53:23 +0100767#endif
768 emsg_on_display = TRUE; // remember there is an error message
769
770 attr = HL_ATTR(HLF_E); // set highlight mode for error messages
Bram Moolenaarbb15b652005-10-03 21:52:09 +0000771 if (msg_scrolled != 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +0100772 need_wait_return = TRUE; // needed in case emsg() is called after
Bram Moolenaar13608d82022-08-29 15:06:50 +0100773 // wait_return() has reset need_wait_return
Bram Moolenaar85a20022019-12-21 18:25:54 +0100774 // and a redraw is expected because
775 // msg_scrolled is non-zero
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776
Bram Moolenaar958dc692016-12-01 15:34:12 +0100777#ifdef FEAT_JOB_CHANNEL
778 emsg_to_channel_log = TRUE;
779#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 /*
781 * Display name and line number for the source of the error.
782 */
zeertzjqbdedd2b2022-09-20 12:45:15 +0100783 msg_scroll = TRUE;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000784 msg_source(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785
786 /*
787 * Display the error message itself.
788 */
Bram Moolenaar85a20022019-12-21 18:25:54 +0100789 msg_nowait = FALSE; // wait for this msg
Bram Moolenaar32526b32019-01-19 17:43:09 +0100790 r = msg_attr((char *)s, attr);
Bram Moolenaar958dc692016-12-01 15:34:12 +0100791
792#ifdef FEAT_JOB_CHANNEL
793 emsg_to_channel_log = FALSE;
794#endif
795 return r;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796}
797
798/*
Bram Moolenaar097c5372023-05-24 21:02:24 +0100799 * Print error message "s". Should already be translated.
800 * Return TRUE if wait_return() not called.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 */
802 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100803emsg(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804{
Bram Moolenaar85a20022019-12-21 18:25:54 +0100805 // Skip this if not giving error messages at the moment.
Bram Moolenaar097c5372023-05-24 21:02:24 +0100806 if (emsg_not_now())
807 return TRUE;
808
809 return emsg_core(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810}
811
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100812#ifndef PROTO // manual proto with __attribute__
Bram Moolenaar95f09602016-11-10 20:01:45 +0100813/*
Bram Moolenaar097c5372023-05-24 21:02:24 +0100814 * Print error message "s" with format string and variable arguments.
815 * "s" should already be translated.
816 * Note: caller must not use "IObuff" for "s"!
817 * Return TRUE if wait_return() not called.
Bram Moolenaar95f09602016-11-10 20:01:45 +0100818 */
819 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100820semsg(const char *s, ...)
Bram Moolenaar95f09602016-11-10 20:01:45 +0100821{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +0200822 // Skip this if not giving error messages at the moment.
Bram Moolenaar097c5372023-05-24 21:02:24 +0100823 if (emsg_not_now())
824 return TRUE;
Bram Moolenaar95f09602016-11-10 20:01:45 +0100825
Bram Moolenaar097c5372023-05-24 21:02:24 +0100826 if (IObuff == NULL)
827 // Very early in initialisation and already something wrong, just
828 // give the raw message so the user at least gets a hint.
829 return emsg_core(s);
830
831 va_list ap;
832
833 va_start(ap, s);
834 vim_vsnprintf((char *)IObuff, IOSIZE, s, ap);
835 va_end(ap);
836 return emsg_core((char *)IObuff);
Bram Moolenaar95f09602016-11-10 20:01:45 +0100837}
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100838#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +0100839
840/*
841 * Same as emsg(...), but abort on error when ABORT_ON_INTERNAL_ERROR is
842 * defined. It is used for internal errors only, so that they can be
843 * detected when fuzzing vim.
844 */
845 void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100846iemsg(char *s)
Bram Moolenaar95f09602016-11-10 20:01:45 +0100847{
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000848 if (emsg_not_now())
849 return;
850
Bram Moolenaar097c5372023-05-24 21:02:24 +0100851 // Give a generic error which is translated. The error itself may not be
852 // translated, it almost never shows.
853 emsg_core(_(e_internal_error_please_report_a_bug));
854
855 emsg_core(s);
Bram Moolenaar096ca732022-01-01 00:55:28 +0000856#if defined(ABORT_ON_INTERNAL_ERROR) && defined(FEAT_EVAL)
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000857 set_vim_var_string(VV_ERRMSG, (char_u *)s, -1);
858 msg_putchar('\n'); // avoid overwriting the error message
859 out_flush();
860 abort();
Bram Moolenaar95f09602016-11-10 20:01:45 +0100861#endif
862}
863
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100864#ifndef PROTO // manual proto with __attribute__
Bram Moolenaar95f09602016-11-10 20:01:45 +0100865/*
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100866 * Same as semsg(...) but abort on error when ABORT_ON_INTERNAL_ERROR is
Bram Moolenaar95f09602016-11-10 20:01:45 +0100867 * defined. It is used for internal errors only, so that they can be
868 * detected when fuzzing vim.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100869 * Note: caller must not pass 'IObuff' as 1st argument.
Bram Moolenaar95f09602016-11-10 20:01:45 +0100870 */
871 void
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100872siemsg(const char *s, ...)
Bram Moolenaar95f09602016-11-10 20:01:45 +0100873{
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000874 if (emsg_not_now())
875 return;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100876
Bram Moolenaar097c5372023-05-24 21:02:24 +0100877 // Give a generic error which is translated. The error itself may not be
878 // translated, it almost never shows.
879 emsg_core(_(e_internal_error_please_report_a_bug));
880
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000881 if (IObuff == NULL)
882 {
883 // Very early in initialisation and already something wrong, just
884 // give the raw message so the user at least gets a hint.
Bram Moolenaar097c5372023-05-24 21:02:24 +0100885 emsg_core(s);
Bram Moolenaar213e70e2022-08-19 13:17:21 +0100886 }
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000887 else
888 {
889 va_list ap;
890
891 va_start(ap, s);
892 vim_vsnprintf((char *)IObuff, IOSIZE, s, ap);
893 va_end(ap);
Bram Moolenaar097c5372023-05-24 21:02:24 +0100894 emsg_core((char *)IObuff);
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000895 }
896# ifdef ABORT_ON_INTERNAL_ERROR
897 msg_putchar('\n'); // avoid overwriting the error message
898 out_flush();
899 abort();
900# endif
Bram Moolenaar95f09602016-11-10 20:01:45 +0100901}
Bram Moolenaar0d8562a2019-02-19 21:34:05 +0100902#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +0100903
904/*
905 * Give an "Internal error" message.
906 */
907 void
908internal_error(char *where)
909{
Bram Moolenaar097c5372023-05-24 21:02:24 +0100910 // Give a generic error which is translated. The error itself may not be
911 // translated, it almost never shows.
912 emsg_core(_(e_internal_error_please_report_a_bug));
913
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000914 siemsg(_(e_internal_error_str), where);
Bram Moolenaar95f09602016-11-10 20:01:45 +0100915}
916
Dominique Pelle748b3082022-01-08 12:41:16 +0000917#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaardd589232020-02-29 17:38:12 +0100918/*
919 * Like internal_error() but do not call abort(), to avoid tests using
920 * test_unknown() and test_void() causing Vim to exit.
921 */
922 void
923internal_error_no_abort(char *where)
924{
Bram Moolenaar097c5372023-05-24 21:02:24 +0100925 // Give a generic error which is translated. The error itself may not be
926 // translated, it almost never shows.
927 emsg_core(_(e_internal_error_please_report_a_bug));
928
929 semsg(_(e_internal_error_str), where);
Bram Moolenaardd589232020-02-29 17:38:12 +0100930}
Dominique Pelle748b3082022-01-08 12:41:16 +0000931#endif
Bram Moolenaardd589232020-02-29 17:38:12 +0100932
Bram Moolenaar85a20022019-12-21 18:25:54 +0100933// emsg3() and emsgn() are in misc2.c to avoid warnings for the prototypes.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934
Bram Moolenaardf177f62005-02-22 08:39:57 +0000935 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100936emsg_invreg(int name)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000937{
cero19881d87e112023-02-16 15:03:12 +0000938 semsg(_(e_invalid_register_name_str), transchar_buf(NULL, name));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000939}
940
Dominique Pelle748b3082022-01-08 12:41:16 +0000941#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100943 * Give an error message which contains %s for "name[len]".
944 */
945 void
946emsg_namelen(char *msg, char_u *name, int len)
947{
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +0000948 char_u *copy = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100949
950 semsg(msg, copy == NULL ? "NULL" : (char *)copy);
Bram Moolenaar292b90d2020-03-18 15:23:16 +0100951 vim_free(copy);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100952}
Dominique Pelle748b3082022-01-08 12:41:16 +0000953#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100954
955/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 * Like msg(), but truncate to a single line if p_shm contains 't', or when
957 * "force" is TRUE. This truncates in another way as for normal messages.
958 * Careful: The string may be changed by msg_may_trunc()!
959 * Returns a pointer to the printed message, if wait_return() not called.
960 */
Bram Moolenaar32526b32019-01-19 17:43:09 +0100961 char *
962msg_trunc_attr(char *s, int force, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963{
964 int n;
Bram Moolenaar32526b32019-01-19 17:43:09 +0100965 char *ts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966
Bram Moolenaar85a20022019-12-21 18:25:54 +0100967 // Add message to history before truncating
Bram Moolenaar32526b32019-01-19 17:43:09 +0100968 add_msg_hist((char_u *)s, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969
Bram Moolenaar32526b32019-01-19 17:43:09 +0100970 ts = (char *)msg_may_trunc(force, (char_u *)s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971
972 msg_hist_off = TRUE;
Bram Moolenaar32526b32019-01-19 17:43:09 +0100973 n = msg_attr(ts, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 msg_hist_off = FALSE;
975
976 if (n)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100977 return ts;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 return NULL;
979}
980
981/*
982 * Check if message "s" should be truncated at the start (for filenames).
983 * Return a pointer to where the truncated message starts.
984 * Note: May change the message by replacing a character with '<'.
985 */
986 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100987msg_may_trunc(int force, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988{
989 int n;
990 int room;
991
Bram Moolenaar9198de32022-08-27 21:30:03 +0100992 // If 'cmdheight' is zero or something unexpected happened "room" may be
993 // negative.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 room = (int)(Rows - cmdline_row - 1) * Columns + sc_col - 1;
Bram Moolenaar0fbc9262022-06-26 11:17:10 +0100995 if (room > 0 && (force || (shortmess(SHM_TRUNC) && !exmode_active))
Bram Moolenaara2a89732022-08-31 14:46:18 +0100996 && (n = (int)STRLEN(s) - room) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 if (has_mbyte)
999 {
1000 int size = vim_strsize(s);
1001
Bram Moolenaar85a20022019-12-21 18:25:54 +01001002 // There may be room anyway when there are multibyte chars.
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00001003 if (size <= room)
1004 return s;
1005
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 for (n = 0; size >= room; )
1007 {
1008 size -= (*mb_ptr2cells)(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001009 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 }
1011 --n;
1012 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 s += n;
1014 *s = '<';
1015 }
1016 return s;
1017}
1018
1019 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001020add_msg_hist(
1021 char_u *s,
Bram Moolenaar85a20022019-12-21 18:25:54 +01001022 int len, // -1 for undetermined length
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001023 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024{
1025 struct msg_hist *p;
1026
1027 if (msg_hist_off || msg_silent != 0)
1028 return;
1029
Bram Moolenaar85a20022019-12-21 18:25:54 +01001030 // allocate an entry and add the message at the end of the history
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001031 p = ALLOC_ONE(struct msg_hist);
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001032 if (p == NULL)
1033 return;
1034
1035 if (len < 0)
1036 len = (int)STRLEN(s);
1037 // remove leading and trailing newlines
1038 while (len > 0 && *s == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 {
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001040 ++s;
1041 --len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 }
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001043 while (len > 0 && s[len - 1] == '\n')
1044 --len;
1045 p->msg = vim_strnsave(s, len);
1046 p->next = NULL;
1047 p->attr = attr;
1048 if (last_msg_hist != NULL)
1049 last_msg_hist->next = p;
1050 last_msg_hist = p;
1051 if (first_msg_hist == NULL)
1052 first_msg_hist = last_msg_hist;
1053 ++msg_hist_len;
Shougo Matsushita9f860a12024-11-24 13:48:05 +01001054
1055 check_msg_hist();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056}
1057
1058/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00001059 * Delete the first (oldest) message from the history.
1060 * Returns FAIL if there are no messages.
1061 */
1062 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001063delete_first_msg(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00001064{
1065 struct msg_hist *p;
1066
1067 if (msg_hist_len <= 0)
1068 return FAIL;
1069 p = first_msg_hist;
1070 first_msg_hist = p->next;
Bram Moolenaara7241f52008-06-24 20:39:31 +00001071 if (first_msg_hist == NULL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001072 last_msg_hist = NULL; // history is empty
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00001073 vim_free(p->msg);
1074 vim_free(p);
1075 --msg_hist_len;
1076 return OK;
1077}
1078
Christian Brabandt51d4d842024-12-06 17:26:25 +01001079 static void
Shougo Matsushita9f860a12024-11-24 13:48:05 +01001080check_msg_hist(void)
1081{
1082 // Don't let the message history get too big
Christian Brabandt51d4d842024-12-06 17:26:25 +01001083 while (msg_hist_len > 0 && msg_hist_len > msg_hist_max)
Shougo Matsushita9f860a12024-11-24 13:48:05 +01001084 (void)delete_first_msg();
1085}
1086
Christian Brabandt51d4d842024-12-06 17:26:25 +01001087
1088 int
1089messagesopt_changed(void)
1090{
1091 char_u *p;
1092 int messages_flags_new = 0;
1093 int messages_wait_new = 0;
1094 int messages_history_new = 0;
1095
zeertzjq8cc43da2024-12-07 16:09:08 +01001096 p = p_mopt;
Christian Brabandt51d4d842024-12-06 17:26:25 +01001097 while (*p != NUL)
1098 {
1099 if (STRNCMP(p, MESSAGES_OPT_HIT_ENTER,
1100 STRLEN_LITERAL(MESSAGES_OPT_HIT_ENTER)) == 0)
1101 {
1102 p += STRLEN_LITERAL(MESSAGES_OPT_HIT_ENTER);
1103 messages_flags_new |= MESSAGES_HIT_ENTER;
1104 }
1105 else if (STRNCMP(p, MESSAGES_OPT_WAIT,
1106 STRLEN_LITERAL(MESSAGES_OPT_WAIT)) == 0
1107 && VIM_ISDIGIT(p[STRLEN_LITERAL(MESSAGES_OPT_WAIT)]))
1108 {
1109 p += STRLEN_LITERAL(MESSAGES_OPT_WAIT);
1110 messages_wait_new = getdigits(&p);
1111 messages_flags_new |= MESSAGES_WAIT;
1112 }
1113 else if (STRNCMP(p, MESSAGES_OPT_HISTORY,
1114 STRLEN_LITERAL(MESSAGES_OPT_HISTORY)) == 0
1115 && VIM_ISDIGIT(p[STRLEN_LITERAL(MESSAGES_OPT_HISTORY)]))
1116 {
1117 p += STRLEN_LITERAL(MESSAGES_OPT_HISTORY);
1118 messages_history_new = getdigits(&p);
1119 messages_flags_new |= MESSAGES_HISTORY;
1120 }
1121
1122 if (*p != ',' && *p != NUL)
1123 return FAIL;
1124 if (*p == ',')
1125 ++p;
1126 }
1127
1128 // Either "wait" or "hit-enter" is required
1129 if (!(messages_flags_new & (MESSAGES_HIT_ENTER | MESSAGES_WAIT)))
1130 return FAIL;
1131
1132 // "history" must be set
1133 if (!(messages_flags_new & MESSAGES_HISTORY))
1134 return FAIL;
1135
h-east65be8342024-12-08 10:05:26 +01001136 if (messages_history_new < 0 || messages_history_new > 10000)
Christian Brabandt51d4d842024-12-06 17:26:25 +01001137 return FAIL;
1138
h-east65be8342024-12-08 10:05:26 +01001139 if (messages_wait_new < 0 || messages_wait_new > 10000)
Shougo Matsushitad9e9f892024-12-07 16:00:25 +01001140 return FAIL;
1141
Christian Brabandt51d4d842024-12-06 17:26:25 +01001142 msg_flags = messages_flags_new;
1143 msg_wait = messages_wait_new;
1144
1145 msg_hist_max = messages_history_new;
1146 check_msg_hist();
1147
1148 return OK;
1149}
1150
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00001151/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 * ":messages" command.
1153 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 void
Bram Moolenaar52196b22016-04-14 17:52:41 +02001155ex_messages(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156{
1157 struct msg_hist *p;
1158 char_u *s;
Bram Moolenaar451f8492016-04-14 17:16:22 +02001159 int c = 0;
1160
1161 if (STRCMP(eap->arg, "clear") == 0)
1162 {
1163 int keep = eap->addr_count == 0 ? 0 : eap->line2;
1164
1165 while (msg_hist_len > keep)
1166 (void)delete_first_msg();
1167 return;
1168 }
1169
1170 if (*eap->arg != NUL)
1171 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001172 emsg(_(e_invalid_argument));
Bram Moolenaar451f8492016-04-14 17:16:22 +02001173 return;
1174 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175
1176 msg_hist_off = TRUE;
1177
Bram Moolenaar451f8492016-04-14 17:16:22 +02001178 p = first_msg_hist;
Bram Moolenaar451f8492016-04-14 17:16:22 +02001179 if (eap->addr_count != 0)
1180 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001181 // Count total messages
Bram Moolenaar451f8492016-04-14 17:16:22 +02001182 for (; p != NULL && !got_int; p = p->next)
1183 c++;
1184
1185 c -= eap->line2;
1186
Bram Moolenaar85a20022019-12-21 18:25:54 +01001187 // Skip without number of messages specified
Bram Moolenaar451f8492016-04-14 17:16:22 +02001188 for (p = first_msg_hist; p != NULL && !got_int && c > 0;
1189 p = p->next, c--);
1190 }
1191
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001192 if (p == first_msg_hist)
1193 {
Bram Moolenaar41f69182020-04-25 15:45:37 +02001194#ifdef FEAT_MULTI_LANG
1195 s = get_mess_lang();
1196#else
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001197 s = mch_getenv((char_u *)"LANG");
Bram Moolenaar41f69182020-04-25 15:45:37 +02001198#endif
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001199 if (s != NULL && *s != NUL)
Bram Moolenaar0c183192018-06-28 14:54:43 +02001200 // The next comment is extracted by xgettext and put in po file for
1201 // translators to read.
Bram Moolenaar32526b32019-01-19 17:43:09 +01001202 msg_attr(
Bram Moolenaar0c183192018-06-28 14:54:43 +02001203 // Translator: Please replace the name and email address
1204 // with the appropriate text for your translation.
Christian Brabandte978b452023-08-13 10:33:05 +02001205 _("Messages maintainer: The Vim Project"),
Bram Moolenaar8820b482017-03-16 17:23:31 +01001206 HL_ATTR(HLF_T));
Bram Moolenaarbea1ede2016-04-14 19:44:36 +02001207 }
1208
Bram Moolenaar85a20022019-12-21 18:25:54 +01001209 // Display what was not skipped.
Bram Moolenaar451f8492016-04-14 17:16:22 +02001210 for (; p != NULL && !got_int; p = p->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 if (p->msg != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001212 msg_attr((char *)p->msg, p->attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213
1214 msg_hist_off = FALSE;
1215}
1216
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001217#if defined(FEAT_CON_DIALOG) || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218/*
1219 * Call this after prompting the user. This will avoid a hit-return message
1220 * and a delay.
1221 */
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001222 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001223msg_end_prompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224{
1225 need_wait_return = FALSE;
1226 emsg_on_display = FALSE;
1227 cmdline_row = msg_row;
1228 msg_col = 0;
1229 msg_clr_eos();
Bram Moolenaar5d6f75e2011-12-30 14:14:29 +01001230 lines_left = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231}
1232#endif
1233
1234/*
Bram Moolenaar32526b32019-01-19 17:43:09 +01001235 * Wait for the user to hit a key (normally Enter).
1236 * If "redraw" is TRUE, clear and redraw the screen.
1237 * If "redraw" is FALSE, just redraw the screen.
1238 * If "redraw" is -1, don't redraw at all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 */
1240 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001241wait_return(int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242{
1243 int c;
1244 int oldState;
1245 int tmpState;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 int had_got_int;
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001247 int save_reg_recording;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001248 FILE *save_scriptout;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249
1250 if (redraw == TRUE)
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01001251 set_must_redraw(UPD_CLEAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252
Bram Moolenaar85a20022019-12-21 18:25:54 +01001253 // If using ":silent cmd", don't wait for a return. Also don't set
1254 // need_wait_return to do it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 if (msg_silent != 0)
1256 return;
Bram Moolenaarcf0995d2022-09-11 21:36:17 +01001257#ifdef HAS_MESSAGE_WINDOW
1258 if (in_echowindow)
1259 return;
1260#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01001262 /*
1263 * When inside vgetc(), we can't wait for a typed character at all.
1264 * With the global command (and some others) we only need one return at
1265 * the end. Adjust cmdline_row to avoid the next message overwriting the
1266 * last one.
1267 */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00001268 if (vgetc_busy > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 return;
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01001270 need_wait_return = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 if (no_wait_return)
1272 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 if (!exmode_active)
1274 cmdline_row = msg_row;
1275 return;
1276 }
1277
Bram Moolenaar85a20022019-12-21 18:25:54 +01001278 redir_off = TRUE; // don't redirect this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 oldState = State;
1280 if (quit_more)
1281 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001282 c = CAR; // just pretend CR was hit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 quit_more = FALSE;
1284 got_int = FALSE;
1285 }
1286 else if (exmode_active)
1287 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001288 msg_puts(" "); // make sure the cursor is on the right line
1289 c = CAR; // no need for a return in ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 got_int = FALSE;
1291 }
1292 else
1293 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001294 // Make sure the hit-return prompt is on screen when 'guioptions' was
1295 // just changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 screenalloc(FALSE);
1297
Bram Moolenaar24959102022-05-07 20:01:16 +01001298 State = MODE_HITRETURN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar85a20022019-12-21 18:25:54 +01001301 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302#endif
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01001303 cmdline_row = msg_row;
1304
Bram Moolenaar85a20022019-12-21 18:25:54 +01001305 // Avoid the sequence that the user types ":" at the hit-return prompt
1306 // to start an Ex command, but the file-changed dialog gets in the
1307 // way.
Bram Moolenaarec38d692013-04-24 15:12:32 +02001308 if (need_check_timestamps)
1309 check_timestamps(FALSE);
1310
Christian Brabandt51d4d842024-12-06 17:26:25 +01001311 if (msg_flags & MESSAGES_HIT_ENTER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 {
Christian Brabandt51d4d842024-12-06 17:26:25 +01001313 hit_return_msg();
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001314
Christian Brabandt51d4d842024-12-06 17:26:25 +01001315 do
1316 {
1317 // Remember "got_int", if it is set vgetc() probably returns a
1318 // CTRL-C, but we need to loop then.
1319 had_got_int = got_int;
Bram Moolenaar332a2ca2013-11-04 02:01:01 +01001320
Christian Brabandt51d4d842024-12-06 17:26:25 +01001321 // Don't do mappings here, we put the character back in the
1322 // typeahead buffer.
1323 ++no_mapping;
1324 ++allow_keys;
1325
1326 // Temporarily disable Recording. If Recording is active, the
h-east36f36712024-12-09 20:05:59 +01001327 // character will be recorded later, since it will be added to
1328 // the typebuf after the loop
Christian Brabandt51d4d842024-12-06 17:26:25 +01001329 save_reg_recording = reg_recording;
1330 save_scriptout = scriptout;
1331 reg_recording = 0;
1332 scriptout = NULL;
1333 c = safe_vgetc();
1334 if (had_got_int && !global_busy)
1335 got_int = FALSE;
1336 --no_mapping;
1337 --allow_keys;
1338 reg_recording = save_reg_recording;
1339 scriptout = save_scriptout;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +00001340
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341#ifdef FEAT_CLIPBOARD
h-east36f36712024-12-09 20:05:59 +01001342 // Strange way to allow copying (yanking) a modeless selection
1343 // at the hit-enter prompt. Use CTRL-Y, because the same is
1344 // used in Cmdline-mode and it's harmless when there is no
1345 // selection.
Christian Brabandt51d4d842024-12-06 17:26:25 +01001346 if (c == Ctrl_Y && clip_star.state == SELECT_DONE)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001347 {
Christian Brabandt51d4d842024-12-06 17:26:25 +01001348 clip_copy_modeless_selection(TRUE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001349 c = K_IGNORE;
Christian Brabandt51d4d842024-12-06 17:26:25 +01001350 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351#endif
Bram Moolenaar88456cd2022-11-18 22:14:09 +00001352
Christian Brabandt51d4d842024-12-06 17:26:25 +01001353 /*
1354 * Allow scrolling back in the messages.
h-east36f36712024-12-09 20:05:59 +01001355 * Also accept scroll-down commands when messages fill the
1356 * screen, to avoid that typing one 'j' too many makes the
1357 * messages disappear.
Christian Brabandt51d4d842024-12-06 17:26:25 +01001358 */
1359 if (p_more && !p_cp)
1360 {
1361 if (c == 'b' || c == 'k' || c == 'u' || c == 'g'
h-east36f36712024-12-09 20:05:59 +01001362 || c == K_UP || c == K_PAGEUP)
Christian Brabandt51d4d842024-12-06 17:26:25 +01001363 {
1364 if (msg_scrolled > Rows)
1365 // scroll back to show older messages
1366 do_more_prompt(c);
1367 else
1368 {
1369 msg_didout = FALSE;
1370 c = K_IGNORE;
1371 msg_col =
1372#ifdef FEAT_RIGHTLEFT
1373 cmdmsg_rl ? Columns - 1 :
1374#endif
1375 0;
1376 }
1377 if (quit_more)
1378 {
1379 c = CAR; // just pretend CR was hit
1380 quit_more = FALSE;
1381 got_int = FALSE;
1382 }
1383 else if (c != K_IGNORE)
1384 {
1385 c = K_IGNORE;
1386 hit_return_msg();
1387 }
1388 }
1389 else if (msg_scrolled > Rows - 2
1390 && (c == 'j' || c == 'd' || c == 'f'
1391 || c == K_DOWN || c == K_PAGEDOWN))
1392 c = K_IGNORE;
1393 }
1394 } while ((had_got_int && c == Ctrl_C)
h-east36f36712024-12-09 20:05:59 +01001395 || c == K_IGNORE
Christian Brabandt51d4d842024-12-06 17:26:25 +01001396#ifdef FEAT_GUI
h-east36f36712024-12-09 20:05:59 +01001397 || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR
Christian Brabandt51d4d842024-12-06 17:26:25 +01001398#endif
h-east36f36712024-12-09 20:05:59 +01001399 || c == K_LEFTDRAG || c == K_LEFTRELEASE
1400 || c == K_MIDDLEDRAG || c == K_MIDDLERELEASE
1401 || c == K_RIGHTDRAG || c == K_RIGHTRELEASE
1402 || c == K_MOUSELEFT || c == K_MOUSERIGHT
1403 || c == K_MOUSEDOWN || c == K_MOUSEUP
1404 || c == K_MOUSEMOVE
1405 || (!mouse_has(MOUSE_RETURN)
1406 && mouse_row < msg_row
1407 && (c == K_LEFTMOUSE
1408 || c == K_MIDDLEMOUSE
1409 || c == K_RIGHTMOUSE
1410 || c == K_X1MOUSE
1411 || c == K_X2MOUSE))
1412 );
Christian Brabandt51d4d842024-12-06 17:26:25 +01001413 ui_breakcheck();
1414
1415 // Avoid that the mouse-up event causes Visual mode to start.
1416 if (c == K_LEFTMOUSE || c == K_MIDDLEMOUSE || c == K_RIGHTMOUSE
h-east36f36712024-12-09 20:05:59 +01001417 || c == K_X1MOUSE || c == K_X2MOUSE)
Christian Brabandt51d4d842024-12-06 17:26:25 +01001418 (void)jump_to_mouse(MOUSE_SETPOS, NULL, 0);
1419 else if (vim_strchr((char_u *)"\r\n ", c) == NULL && c != Ctrl_C)
1420 {
h-east36f36712024-12-09 20:05:59 +01001421 // Put the character back in the typeahead buffer. Don't use
1422 // the stuff buffer, because lmaps wouldn't work.
Christian Brabandt51d4d842024-12-06 17:26:25 +01001423 ins_char_typebuf(vgetc_char, vgetc_mod_mask);
h-east36f36712024-12-09 20:05:59 +01001424 do_redraw = TRUE; // need a redraw even though there is
Christian Brabandt51d4d842024-12-06 17:26:25 +01001425 // typeahead
1426 }
1427 }
1428 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429 {
Christian Brabandt51d4d842024-12-06 17:26:25 +01001430 c = CAR;
1431 // Wait to allow the user to verify the output.
1432 do_sleep(msg_wait, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 }
1435 redir_off = FALSE;
1436
1437 /*
1438 * If the user hits ':', '?' or '/' we get a command line from the next
1439 * line.
1440 */
1441 if (c == ':' || c == '?' || c == '/')
1442 {
1443 if (!exmode_active)
1444 cmdline_row = msg_row;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001445 skip_redraw = TRUE; // skip redraw once
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 do_redraw = FALSE;
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02001447#ifdef FEAT_TERMINAL
1448 skip_term_loop = TRUE;
1449#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450 }
1451
1452 /*
1453 * If the window size changed set_shellsize() will redraw the screen.
1454 * Otherwise the screen is only redrawn if 'redraw' is set and no ':'
1455 * typed.
1456 */
1457 tmpState = State;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001458 State = oldState; // restore State before set_shellsize
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 msg_check();
1461
1462#if defined(UNIX) || defined(VMS)
1463 /*
1464 * When switching screens, we need to output an extra newline on exit.
1465 */
1466 if (swapping_screen() && !termcap_active)
1467 newline_on_exit = TRUE;
1468#endif
1469
1470 need_wait_return = FALSE;
1471 did_wait_return = TRUE;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001472 emsg_on_display = FALSE; // can delete error message now
1473 lines_left = -1; // reset lines_left at next msg_start()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 reset_last_sourcing();
1475 if (keep_msg != NULL && vim_strsize(keep_msg) >=
1476 (Rows - cmdline_row - 1) * Columns + sc_col)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001477 VIM_CLEAR(keep_msg); // don't redisplay message, it's too long
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478
Bram Moolenaar24959102022-05-07 20:01:16 +01001479 if (tmpState == MODE_SETWSIZE) // got resize event while in vgetc()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001481 starttermcap(); // start termcap before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 shell_resized();
1483 }
1484 else if (!skip_redraw
1485 && (redraw == TRUE || (msg_scrolled != 0 && redraw != -1)))
1486 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001487 starttermcap(); // start termcap before redrawing
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001488 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489 }
1490}
1491
1492/*
1493 * Write the hit-return prompt.
1494 */
1495 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001496hit_return_msg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001498 int save_p_more = p_more;
1499
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001500 p_more = FALSE; // don't want to see this message when scrolling back
Bram Moolenaar85a20022019-12-21 18:25:54 +01001501 if (msg_didout) // start on a new line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502 msg_putchar('\n');
1503 if (got_int)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001504 msg_puts(_("Interrupt: "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505
Bram Moolenaar32526b32019-01-19 17:43:09 +01001506 msg_puts_attr(_("Press ENTER or type command to continue"), HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 if (!msg_use_printf())
1508 msg_clr_eos();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001509 p_more = save_p_more;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510}
1511
1512/*
1513 * Set "keep_msg" to "s". Free the old value and check for NULL pointer.
1514 */
1515 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001516set_keep_msg(char_u *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517{
1518 vim_free(keep_msg);
1519 if (s != NULL && msg_silent == 0)
1520 keep_msg = vim_strsave(s);
1521 else
1522 keep_msg = NULL;
Bram Moolenaaraab21c32005-01-25 21:46:35 +00001523 keep_msg_more = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001524 keep_msg_attr = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525}
1526
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001527/*
1528 * If there currently is a message being displayed, set "keep_msg" to it, so
1529 * that it will be displayed again after redraw.
1530 */
1531 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001532set_keep_msg_from_hist(void)
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001533{
1534 if (keep_msg == NULL && last_msg_hist != NULL && msg_scrolled == 0
Bram Moolenaar24959102022-05-07 20:01:16 +01001535 && (State & MODE_NORMAL))
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001536 set_keep_msg(last_msg_hist->msg, last_msg_hist->attr);
1537}
Bram Moolenaar030f0df2006-02-21 22:02:53 +00001538
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539/*
1540 * Prepare for outputting characters in the command line.
1541 */
1542 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001543msg_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544{
1545 int did_return = FALSE;
1546
zeertzjq40ed6712023-11-23 20:37:01 +01001547 if (msg_row < cmdline_row)
1548 msg_row = cmdline_row;
1549
Bram Moolenaare4ce65d2010-08-04 20:12:32 +02001550 if (!msg_silent)
Rob Pilling726f7f92022-01-20 14:44:38 +00001551 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001552 VIM_CLEAR(keep_msg);
Rob Pilling726f7f92022-01-20 14:44:38 +00001553 need_fileinfo = FALSE;
1554 }
Bram Moolenaara7241f52008-06-24 20:39:31 +00001555
1556#ifdef FEAT_EVAL
Bram Moolenaar3b474dc2022-09-01 17:01:32 +01001557 if (need_clr_eos)
Bram Moolenaara7241f52008-06-24 20:39:31 +00001558 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001559 // Halfway an ":echo" command and getting an (error) message: clear
1560 // any text from the command.
Bram Moolenaara7241f52008-06-24 20:39:31 +00001561 need_clr_eos = FALSE;
1562 msg_clr_eos();
1563 }
1564#endif
1565
Bram Moolenaard54af2e2022-08-27 22:05:13 +01001566#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaara2a89732022-08-31 14:46:18 +01001567 if (in_echowindow)
Bram Moolenaar9198de32022-08-27 21:30:03 +01001568 {
Bram Moolenaar37fef162022-08-29 18:16:32 +01001569 if (popup_message_win_visible()
1570 && ((msg_col > 0 && (msg_scroll || !full_screen))
1571 || in_echowindow))
Bram Moolenaar9198de32022-08-27 21:30:03 +01001572 {
1573 win_T *wp = popup_get_message_win();
1574
Bram Moolenaarf2fb54f2022-08-28 20:58:51 +01001575 // start a new line
Bram Moolenaar9198de32022-08-27 21:30:03 +01001576 curbuf = wp->w_buffer;
1577 ml_append(wp->w_buffer->b_ml.ml_line_count,
1578 (char_u *)"", (colnr_T)0, FALSE);
1579 curbuf = curwin->w_buffer;
1580 }
1581 msg_col = 0;
1582 }
Bram Moolenaard54af2e2022-08-27 22:05:13 +01001583 else
1584#endif
1585 if (!msg_scroll && full_screen) // overwrite last message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586 {
1587 msg_row = cmdline_row;
1588 msg_col =
1589#ifdef FEAT_RIGHTLEFT
1590 cmdmsg_rl ? Columns - 1 :
1591#endif
1592 0;
1593 }
Bram Moolenaara2a89732022-08-31 14:46:18 +01001594 else if (msg_didout || in_echowindow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 {
Bram Moolenaar309c4e02022-08-29 12:23:39 +01001596 // start message on next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 msg_putchar('\n');
1598 did_return = TRUE;
1599 if (exmode_active != EXMODE_NORMAL)
1600 cmdline_row = msg_row;
1601 }
1602 if (!msg_didany || lines_left < 0)
1603 msg_starthere();
1604 if (msg_silent == 0)
1605 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001606 msg_didout = FALSE; // no output on current line yet
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 cursor_off();
1608 }
1609
Bram Moolenaar85a20022019-12-21 18:25:54 +01001610 // when redirecting, may need to start a new line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 if (!did_return)
1612 redir_write((char_u *)"\n", -1);
1613}
1614
1615/*
1616 * Note that the current msg position is where messages start.
1617 */
1618 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001619msg_starthere(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620{
1621 lines_left = cmdline_row;
1622 msg_didany = FALSE;
1623}
1624
1625 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001626msg_putchar(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627{
1628 msg_putchar_attr(c, 0);
1629}
1630
1631 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001632msg_putchar_attr(int c, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633{
Bram Moolenaar63c0ccd2019-01-19 21:06:58 +01001634 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635
1636 if (IS_SPECIAL(c))
1637 {
1638 buf[0] = K_SPECIAL;
1639 buf[1] = K_SECOND(c);
1640 buf[2] = K_THIRD(c);
1641 buf[3] = NUL;
1642 }
1643 else
Bram Moolenaar63c0ccd2019-01-19 21:06:58 +01001644 buf[(*mb_char2bytes)(c, buf)] = NUL;
Bram Moolenaar63c0ccd2019-01-19 21:06:58 +01001645 msg_puts_attr((char *)buf, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646}
1647
1648 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001649msg_outnum(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001651 char buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652
Bram Moolenaar32526b32019-01-19 17:43:09 +01001653 sprintf(buf, "%ld", n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 msg_puts(buf);
1655}
1656
1657 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001658msg_home_replace(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659{
1660 msg_home_replace_attr(fname, 0);
1661}
1662
1663#if defined(FEAT_FIND_ID) || defined(PROTO)
1664 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001665msg_home_replace_hl(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666{
Bram Moolenaar8820b482017-03-16 17:23:31 +01001667 msg_home_replace_attr(fname, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668}
1669#endif
1670
1671 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001672msg_home_replace_attr(char_u *fname, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673{
1674 char_u *name;
1675
1676 name = home_replace_save(NULL, fname);
1677 if (name != NULL)
1678 msg_outtrans_attr(name, attr);
1679 vim_free(name);
1680}
1681
1682/*
1683 * Output 'len' characters in 'str' (including NULs) with translation
Bram Moolenaarf48ee3c2019-12-06 22:18:20 +01001684 * if 'len' is -1, output up to a NUL character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 * Use attributes 'attr'.
1686 * Return the number of characters it takes on the screen.
1687 */
1688 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001689msg_outtrans(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690{
1691 return msg_outtrans_attr(str, 0);
1692}
1693
1694 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001695msg_outtrans_attr(char_u *str, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696{
1697 return msg_outtrans_len_attr(str, (int)STRLEN(str), attr);
1698}
1699
1700 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001701msg_outtrans_len(char_u *str, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702{
1703 return msg_outtrans_len_attr(str, len, 0);
1704}
1705
1706/*
1707 * Output one character at "p". Return pointer to the next character.
1708 * Handles multi-byte characters.
1709 */
1710 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001711msg_outtrans_one(char_u *p, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 int l;
1714
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001715 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 {
1717 msg_outtrans_len_attr(p, l, attr);
1718 return p + l;
1719 }
cero19881d87e112023-02-16 15:03:12 +00001720 msg_puts_attr((char *)transchar_byte_buf(NULL, *p), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 return p + 1;
1722}
1723
1724 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001725msg_outtrans_len_attr(char_u *msgstr, int len, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726{
1727 int retval = 0;
1728 char_u *str = msgstr;
1729 char_u *plain_start = msgstr;
1730 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 int mb_l;
1732 int c;
Bram Moolenaar3d30af82020-10-13 22:15:56 +02001733 int save_got_int = got_int;
1734
1735 // Only quit when got_int was set in here.
1736 got_int = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737
Shougo Matsushitabe2b03c2024-04-08 22:11:50 +02001738 if (attr == 0)
1739 attr = HL_ATTR(HLF_MSG);
1740
Bram Moolenaar85a20022019-12-21 18:25:54 +01001741 // if MSG_HIST flag set, add message to history
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742 if (attr & MSG_HIST)
1743 {
1744 add_msg_hist(str, len, attr);
1745 attr &= ~MSG_HIST;
1746 }
1747
Bram Moolenaar800cdbb2023-06-15 16:40:02 +01001748 // When drawing over the command line no need to clear it later or remove
1749 // the mode message.
Bram Moolenaarda51ad52023-06-15 18:44:50 +01001750 if (msg_row >= cmdline_row && msg_col == 0)
Bram Moolenaar800cdbb2023-06-15 16:40:02 +01001751 {
1752 clear_cmdline = FALSE;
1753 mode_displayed = FALSE;
1754 }
1755
Bram Moolenaar85a20022019-12-21 18:25:54 +01001756 // If the string starts with a composing character first draw a space on
1757 // which the composing char can be drawn.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 if (enc_utf8 && utf_iscomposing(utf_ptr2char(msgstr)))
Bram Moolenaar32526b32019-01-19 17:43:09 +01001759 msg_puts_attr(" ", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760
1761 /*
1762 * Go over the string. Special characters are translated and printed.
1763 * Normal characters are printed several at a time.
1764 */
Bram Moolenaar3d30af82020-10-13 22:15:56 +02001765 while (--len >= 0 && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 if (enc_utf8)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001768 // Don't include composing chars after the end.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001769 mb_l = utfc_ptr2len_len(str, len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 else if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001771 mb_l = (*mb_ptr2len)(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772 else
1773 mb_l = 1;
1774 if (has_mbyte && mb_l > 1)
1775 {
1776 c = (*mb_ptr2char)(str);
1777 if (vim_isprintc(c))
Bram Moolenaar85a20022019-12-21 18:25:54 +01001778 // printable multi-byte char: count the cells.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 retval += (*mb_ptr2cells)(str);
1780 else
1781 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001782 // unprintable multi-byte char: print the printable chars so
1783 // far and the translation of the unprintable char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 if (str > plain_start)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001785 msg_puts_attr_len((char *)plain_start,
1786 (int)(str - plain_start), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 plain_start = str + mb_l;
cero19881d87e112023-02-16 15:03:12 +00001788 msg_puts_attr((char *)transchar_buf(NULL, c),
Bram Moolenaar32526b32019-01-19 17:43:09 +01001789 attr == 0 ? HL_ATTR(HLF_8) : attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 retval += char2cells(c);
1791 }
1792 len -= mb_l - 1;
1793 str += mb_l;
1794 }
1795 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 {
cero19881d87e112023-02-16 15:03:12 +00001797 s = transchar_byte_buf(NULL, *str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 if (s[1] != NUL)
1799 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001800 // unprintable char: print the printable chars so far and the
1801 // translation of the unprintable char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 if (str > plain_start)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001803 msg_puts_attr_len((char *)plain_start,
1804 (int)(str - plain_start), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 plain_start = str + 1;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001806 msg_puts_attr((char *)s, attr == 0 ? HL_ATTR(HLF_8) : attr);
Bram Moolenaarc236c162008-07-13 17:41:49 +00001807 retval += (int)STRLEN(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 }
Bram Moolenaarb3c722a2008-07-06 17:16:02 +00001809 else
1810 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 ++str;
1812 }
1813 }
1814
Bram Moolenaar3d30af82020-10-13 22:15:56 +02001815 if (str > plain_start && !got_int)
Bram Moolenaar85a20022019-12-21 18:25:54 +01001816 // print the printable chars at the end
Bram Moolenaar32526b32019-01-19 17:43:09 +01001817 msg_puts_attr_len((char *)plain_start, (int)(str - plain_start), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818
Bram Moolenaar3d30af82020-10-13 22:15:56 +02001819 got_int |= save_got_int;
1820
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 return retval;
1822}
1823
1824#if defined(FEAT_QUICKFIX) || defined(PROTO)
1825 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001826msg_make(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827{
1828 int i;
1829 static char_u *str = (char_u *)"eeffoc", *rs = (char_u *)"Plon#dqg#vxjduB";
1830
1831 arg = skipwhite(arg);
1832 for (i = 5; *arg && i >= 0; --i)
1833 if (*arg++ != str[i])
1834 break;
1835 if (i < 0)
1836 {
1837 msg_putchar('\n');
1838 for (i = 0; rs[i]; ++i)
1839 msg_putchar(rs[i] - 3);
1840 }
1841}
1842#endif
1843
1844/*
Bram Moolenaarf48ee3c2019-12-06 22:18:20 +01001845 * Output the string 'str' up to a NUL character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 * Return the number of characters it takes on the screen.
1847 *
1848 * If K_SPECIAL is encountered, then it is taken in conjunction with the
1849 * following character and shown as <F1>, <S-Up> etc. Any other character
1850 * which is not printable shown in <> form.
1851 * If 'from' is TRUE (lhs of a mapping), a space is shown as <Space>.
1852 * If a character is displayed in one of these special ways, is also
1853 * highlighted (its highlight name is '8' in the p_hl variable).
1854 * Otherwise characters are not highlighted.
1855 * This function is used to show mappings, where we want to see how to type
1856 * the character/string -- webb
1857 */
1858 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001859msg_outtrans_special(
1860 char_u *strstart,
Bram Moolenaar725310d2019-04-24 23:08:23 +02001861 int from, // TRUE for lhs of a mapping
Bram Moolenaar32aa1022019-11-02 22:54:41 +01001862 int maxlen) // screen columns, 0 for unlimited
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863{
1864 char_u *str = strstart;
1865 int retval = 0;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001866 char *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 int attr;
1868 int len;
1869
Bram Moolenaar8820b482017-03-16 17:23:31 +01001870 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 while (*str != NUL)
1872 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01001873 // Leading and trailing spaces need to be displayed in <> form.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 if ((str == strstart || str[1] == NUL) && *str == ' ')
1875 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001876 text = "<Space>";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 ++str;
1878 }
1879 else
zeertzjqcdc83932022-09-12 13:38:41 +01001880 text = (char *)str2special(&str, from, FALSE);
zeertzjq0519ce02022-05-09 12:16:19 +01001881 if (text[0] != NUL && text[1] == NUL)
1882 // single-byte character or illegal byte
cero19881d87e112023-02-16 15:03:12 +00001883 text = (char *)transchar_byte_buf(NULL, (char_u)text[0]);
Bram Moolenaar32526b32019-01-19 17:43:09 +01001884 len = vim_strsize((char_u *)text);
Bram Moolenaar725310d2019-04-24 23:08:23 +02001885 if (maxlen > 0 && retval + len >= maxlen)
1886 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01001887 // Highlight special keys
Bram Moolenaar32526b32019-01-19 17:43:09 +01001888 msg_puts_attr(text, len > 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001889 && (*mb_ptr2len)((char_u *)text) <= 1 ? attr : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 retval += len;
1891 }
1892 return retval;
1893}
1894
Martin Tournoijba43e762022-10-13 22:12:15 +01001895#if defined(FEAT_EVAL) || defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaarbd743252010-10-20 21:23:33 +02001896/*
1897 * Return the lhs or rhs of a mapping, with the key codes turned into printable
1898 * strings, in an allocated string.
1899 */
1900 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001901str2special_save(
1902 char_u *str,
zeertzjqcdc83932022-09-12 13:38:41 +01001903 int replace_spaces, // TRUE to replace " " with "<Space>".
1904 // used for the lhs of mapping and keytrans().
1905 int replace_lt) // TRUE to replace "<" with "<lt>".
Bram Moolenaarbd743252010-10-20 21:23:33 +02001906{
1907 garray_T ga;
1908 char_u *p = str;
1909
1910 ga_init2(&ga, 1, 40);
1911 while (*p != NUL)
zeertzjqcdc83932022-09-12 13:38:41 +01001912 ga_concat(&ga, str2special(&p, replace_spaces, replace_lt));
Bram Moolenaarbd743252010-10-20 21:23:33 +02001913 ga_append(&ga, NUL);
1914 return (char_u *)ga.ga_data;
1915}
1916#endif
1917
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918/*
1919 * Return the printable string for the key codes at "*sp".
zeertzjq0519ce02022-05-09 12:16:19 +01001920 * On illegal byte return a string with only that byte.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 * Used for translating the lhs or rhs of a mapping to printable chars.
1922 * Advances "sp" to the next code.
1923 */
1924 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001925str2special(
1926 char_u **sp,
zeertzjqcdc83932022-09-12 13:38:41 +01001927 int replace_spaces, // TRUE to replace " " with "<Space>".
1928 // used for the lhs of mapping and keytrans().
1929 int replace_lt) // TRUE to replace "<" with "<lt>".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930{
1931 int c;
1932 static char_u buf[7];
1933 char_u *str = *sp;
1934 int modifiers = 0;
1935 int special = FALSE;
1936
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 if (has_mbyte)
1938 {
1939 char_u *p;
1940
Bram Moolenaar85a20022019-12-21 18:25:54 +01001941 // Try to un-escape a multi-byte character. Return the un-escaped
1942 // string if it is a multi-byte character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 p = mb_unescape(sp);
1944 if (p != NULL)
1945 return p;
1946 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947
1948 c = *str;
zeertzjq1bed9932023-08-30 19:38:24 +02001949 if ((c == K_SPECIAL
1950#ifdef FEAT_GUI
1951 || c == CSI
1952#endif
1953 ) && str[1] != NUL && str[2] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 {
1955 if (str[1] == KS_MODIFIER)
1956 {
1957 modifiers = str[2];
1958 str += 3;
1959 c = *str;
1960 }
zeertzjq1bed9932023-08-30 19:38:24 +02001961 if ((c == K_SPECIAL
1962#ifdef FEAT_GUI
1963 || c == CSI
1964#endif
1965 ) && str[1] != NUL && str[2] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966 {
1967 c = TO_SPECIAL(str[1], str[2]);
1968 str += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01001970 if (IS_SPECIAL(c) || modifiers) // special key
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 special = TRUE;
1972 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973
zeertzjq0519ce02022-05-09 12:16:19 +01001974 if (has_mbyte && !IS_SPECIAL(c) && MB_BYTE2LEN(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 {
zeertzjqac402f42022-05-04 18:51:43 +01001976 char_u *p;
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001977
zeertzjqac402f42022-05-04 18:51:43 +01001978 *sp = str;
1979 // Try to un-escape a multi-byte character after modifiers.
1980 p = mb_unescape(sp);
zeertzjq0519ce02022-05-09 12:16:19 +01001981 if (p != NULL)
1982 // Since 'special' is TRUE the multi-byte character 'c' will be
1983 // processed by get_special_key_name()
1984 c = (*mb_ptr2char)(p);
1985 else
1986 // illegal byte
1987 *sp = str + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 }
Bram Moolenaarb8bf5412011-08-17 20:33:22 +02001989 else
Bram Moolenaar083692d2022-06-29 21:16:58 +01001990 // single-byte character, NUL or illegal byte
1991 *sp = str + (*str == NUL ? 0 : 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992
zeertzjq0519ce02022-05-09 12:16:19 +01001993 // Make special keys and C0 control characters in <> form, also <M-Space>.
zeertzjqcdc83932022-09-12 13:38:41 +01001994 if (special
1995 || c < ' '
1996 || (replace_spaces && c == ' ')
1997 || (replace_lt && c == '<'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 return get_special_key_name(c, modifiers);
1999 buf[0] = c;
2000 buf[1] = NUL;
2001 return buf;
2002}
2003
2004/*
2005 * Translate a key sequence into special key names.
2006 */
2007 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002008str2specialbuf(char_u *sp, char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009{
2010 char_u *s;
2011
2012 *buf = NUL;
2013 while (*sp)
2014 {
zeertzjqcdc83932022-09-12 13:38:41 +01002015 s = str2special(&sp, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 if ((int)(STRLEN(s) + STRLEN(buf)) < len)
2017 STRCAT(buf, s);
2018 }
2019}
2020
2021/*
2022 * print line for :print or :list command
2023 */
2024 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002025msg_prt_line(char_u *s, int list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026{
2027 int c;
2028 int col = 0;
2029 int n_extra = 0;
2030 int c_extra = 0;
Bram Moolenaar83a52172019-01-16 22:41:54 +01002031 int c_final = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002032 char_u *p_extra = NULL; // init to make SASC shut up
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 int n;
Bram Moolenaar56da7972007-01-16 14:46:32 +00002034 int attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 char_u *trail = NULL;
Bram Moolenaar91478ae2021-02-03 15:58:13 +01002036 char_u *lead = NULL;
zeertzjqf14b8ba2021-09-10 16:58:30 +02002037 int in_multispace = FALSE;
2038 int multispace_pos = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 int l;
2040 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041
Bram Moolenaardf177f62005-02-22 08:39:57 +00002042 if (curwin->w_p_list)
2043 list = TRUE;
2044
Bram Moolenaar91478ae2021-02-03 15:58:13 +01002045 if (list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 {
Bram Moolenaar91478ae2021-02-03 15:58:13 +01002047 // find start of trailing whitespace
Bram Moolenaareed9d462021-02-15 20:38:25 +01002048 if (curwin->w_lcs_chars.trail)
Bram Moolenaar91478ae2021-02-03 15:58:13 +01002049 {
2050 trail = s + STRLEN(s);
2051 while (trail > s && VIM_ISWHITE(trail[-1]))
2052 --trail;
2053 }
2054 // find end of leading whitespace
Bram Moolenaarb8329db2022-07-06 13:31:28 +01002055 if (curwin->w_lcs_chars.lead
2056 || curwin->w_lcs_chars.leadmultispace != NULL)
Bram Moolenaar91478ae2021-02-03 15:58:13 +01002057 {
2058 lead = s;
2059 while (VIM_ISWHITE(lead[0]))
2060 lead++;
2061 // in a line full of spaces all of them are treated as trailing
2062 if (*lead == NUL)
2063 lead = NULL;
2064 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065 }
2066
Bram Moolenaar85a20022019-12-21 18:25:54 +01002067 // output a space for an empty line, otherwise the line will be
2068 // overwritten
Bram Moolenaareed9d462021-02-15 20:38:25 +01002069 if (*s == NUL && !(list && curwin->w_lcs_chars.eol != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 msg_putchar(' ');
2071
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002072 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 {
Bram Moolenaar56da7972007-01-16 14:46:32 +00002074 if (n_extra > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 {
2076 --n_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01002077 if (n_extra == 0 && c_final)
2078 c = c_final;
2079 else if (c_extra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 c = c_extra;
2081 else
2082 c = *p_extra++;
2083 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002084 else if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 {
2086 col += (*mb_ptr2cells)(s);
Bram Moolenaar1cbfc992020-12-02 12:37:37 +01002087 if (l >= MB_MAXBYTES)
2088 {
Bram Moolenaar29d2f452020-12-04 19:42:52 +01002089 STRCPY(buf, "?");
Bram Moolenaar1cbfc992020-12-02 12:37:37 +01002090 }
Bram Moolenaareed9d462021-02-15 20:38:25 +01002091 else if (curwin->w_lcs_chars.nbsp != NUL && list
Bram Moolenaar73284b92015-05-04 17:28:22 +02002092 && (mb_ptr2char(s) == 160
2093 || mb_ptr2char(s) == 0x202f))
Bram Moolenaaracf17282011-02-01 17:12:25 +01002094 {
Bram Moolenaar74ac29c2022-06-15 12:12:44 +01002095 int len = mb_char2bytes(curwin->w_lcs_chars.nbsp, buf);
2096
2097 buf[len] = NUL;
Bram Moolenaaracf17282011-02-01 17:12:25 +01002098 }
2099 else
2100 {
2101 mch_memmove(buf, s, (size_t)l);
2102 buf[l] = NUL;
2103 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01002104 msg_puts((char *)buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 s += l;
2106 continue;
2107 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 else
2109 {
2110 attr = 0;
2111 c = *s++;
zeertzjqabc80812023-09-24 23:32:18 +02002112 if (list)
2113 {
2114 in_multispace = c == ' ' && (*s == ' '
2115 || (col > 0 && s[-2] == ' '));
2116 if (!in_multispace)
2117 multispace_pos = 0;
2118 }
Bram Moolenaareed9d462021-02-15 20:38:25 +01002119 if (c == TAB && (!list || curwin->w_lcs_chars.tab1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002121 // tab amount depends on current column
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002122#ifdef FEAT_VARTABS
2123 n_extra = tabstop_padding(col, curbuf->b_p_ts,
2124 curbuf->b_p_vts_array) - 1;
2125#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 n_extra = curbuf->b_p_ts - col % curbuf->b_p_ts - 1;
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002127#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00002128 if (!list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 {
2130 c = ' ';
2131 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01002132 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 }
2134 else
2135 {
Bram Moolenaareed9d462021-02-15 20:38:25 +01002136 c = (n_extra == 0 && curwin->w_lcs_chars.tab3)
2137 ? curwin->w_lcs_chars.tab3
2138 : curwin->w_lcs_chars.tab1;
2139 c_extra = curwin->w_lcs_chars.tab2;
2140 c_final = curwin->w_lcs_chars.tab3;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002141 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 }
2143 }
Bram Moolenaareed9d462021-02-15 20:38:25 +01002144 else if (c == 160 && list && curwin->w_lcs_chars.nbsp != NUL)
Bram Moolenaaracf17282011-02-01 17:12:25 +01002145 {
Bram Moolenaareed9d462021-02-15 20:38:25 +01002146 c = curwin->w_lcs_chars.nbsp;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002147 attr = HL_ATTR(HLF_8);
Bram Moolenaaracf17282011-02-01 17:12:25 +01002148 }
Bram Moolenaareed9d462021-02-15 20:38:25 +01002149 else if (c == NUL && list && curwin->w_lcs_chars.eol != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 {
2151 p_extra = (char_u *)"";
2152 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01002153 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 n_extra = 1;
Bram Moolenaareed9d462021-02-15 20:38:25 +01002155 c = curwin->w_lcs_chars.eol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002156 attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 --s;
2158 }
2159 else if (c != NUL && (n = byte2cells(c)) > 1)
2160 {
2161 n_extra = n - 1;
cero19881d87e112023-02-16 15:03:12 +00002162 p_extra = transchar_byte_buf(NULL, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01002164 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 c = *p_extra++;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002166 // Use special coloring to be able to distinguish <hex> from
2167 // the same in plain text.
Bram Moolenaar8820b482017-03-16 17:23:31 +01002168 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 }
zeertzjqf14b8ba2021-09-10 16:58:30 +02002170 else if (c == ' ')
Bram Moolenaar91478ae2021-02-03 15:58:13 +01002171 {
zeertzjqabc80812023-09-24 23:32:18 +02002172 if (lead != NULL && s <= lead && in_multispace
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01002173 && curwin->w_lcs_chars.leadmultispace != NULL)
2174 {
2175 c = curwin->w_lcs_chars.leadmultispace[multispace_pos++];
zeertzjqb5f08012022-06-09 13:55:28 +01002176 if (curwin->w_lcs_chars.leadmultispace[multispace_pos]
2177 == NUL)
Bram Moolenaaraca12fd2022-06-07 10:16:15 +01002178 multispace_pos = 0;
2179 attr = HL_ATTR(HLF_8);
2180 }
zeertzjqb5f08012022-06-09 13:55:28 +01002181 else if (lead != NULL && s <= lead
2182 && curwin->w_lcs_chars.lead != NUL)
zeertzjqf14b8ba2021-09-10 16:58:30 +02002183 {
2184 c = curwin->w_lcs_chars.lead;
2185 attr = HL_ATTR(HLF_8);
2186 }
2187 else if (trail != NULL && s > trail)
2188 {
2189 c = curwin->w_lcs_chars.trail;
2190 attr = HL_ATTR(HLF_8);
2191 }
zeertzjqabc80812023-09-24 23:32:18 +02002192 else if (in_multispace
zeertzjqf14b8ba2021-09-10 16:58:30 +02002193 && curwin->w_lcs_chars.multispace != NULL)
2194 {
2195 c = curwin->w_lcs_chars.multispace[multispace_pos++];
2196 if (curwin->w_lcs_chars.multispace[multispace_pos] == NUL)
2197 multispace_pos = 0;
2198 attr = HL_ATTR(HLF_8);
2199 }
2200 else if (list && curwin->w_lcs_chars.space != NUL)
2201 {
2202 c = curwin->w_lcs_chars.space;
2203 attr = HL_ATTR(HLF_8);
2204 }
Bram Moolenaar1510f992015-04-22 22:18:22 +02002205 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 }
2207
2208 if (c == NUL)
2209 break;
2210
2211 msg_putchar_attr(c, attr);
2212 col++;
2213 }
2214 msg_clr_eos();
2215}
2216
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217/*
2218 * Use screen_puts() to output one multi-byte character.
2219 * Return the pointer "s" advanced to the next character.
2220 */
2221 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002222screen_puts_mbyte(char_u *s, int l, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223{
2224 int cw;
2225
Bram Moolenaar85a20022019-12-21 18:25:54 +01002226 msg_didout = TRUE; // remember that line is not empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 cw = (*mb_ptr2cells)(s);
2228 if (cw > 1 && (
2229#ifdef FEAT_RIGHTLEFT
2230 cmdmsg_rl ? msg_col <= 1 :
2231#endif
2232 msg_col == Columns - 1))
2233 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002234 // Doesn't fit, print a highlighted '>' to fill it up.
Bram Moolenaar8820b482017-03-16 17:23:31 +01002235 msg_screen_putchar('>', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 return s;
2237 }
2238
2239 screen_puts_len(s, l, msg_row, msg_col, attr);
2240#ifdef FEAT_RIGHTLEFT
2241 if (cmdmsg_rl)
2242 {
2243 msg_col -= cw;
2244 if (msg_col == 0)
2245 {
2246 msg_col = Columns;
2247 ++msg_row;
2248 }
2249 }
2250 else
2251#endif
2252 {
2253 msg_col += cw;
2254 if (msg_col >= Columns)
2255 {
2256 msg_col = 0;
2257 ++msg_row;
2258 }
2259 }
2260 return s + l;
2261}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262
2263/*
2264 * Output a string to the screen at position msg_row, msg_col.
2265 * Update msg_row and msg_col for the next message.
2266 */
2267 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002268msg_puts(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269{
Bram Moolenaaraeac9002016-09-06 22:15:08 +02002270 msg_puts_attr(s, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271}
2272
2273 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002274msg_puts_title(char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275{
Bram Moolenaar8820b482017-03-16 17:23:31 +01002276 msg_puts_attr(s, HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277}
2278
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279/*
2280 * Show a message in such a way that it always fits in the line. Cut out a
2281 * part in the middle and replace it with "..." when necessary.
2282 * Does not handle multi-byte characters!
2283 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002284 static void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002285msg_outtrans_long_len_attr(char_u *longstr, int len, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286{
2287 int slen = len;
2288 int room;
2289
2290 room = Columns - msg_col;
2291 if (len > room && room >= 20)
2292 {
2293 slen = (room - 3) / 2;
2294 msg_outtrans_len_attr(longstr, slen, attr);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002295 msg_puts_attr("...", HL_ATTR(HLF_8));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 }
2297 msg_outtrans_len_attr(longstr + len - slen, slen, attr);
2298}
2299
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002300 void
2301msg_outtrans_long_attr(char_u *longstr, int attr)
2302{
2303 msg_outtrans_long_len_attr(longstr, (int)STRLEN(longstr), attr);
2304}
2305
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306/*
2307 * Basic function for writing a message with highlight attributes.
2308 */
2309 void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002310msg_puts_attr(char *s, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311{
2312 msg_puts_attr_len(s, -1, attr);
2313}
2314
2315/*
2316 * Like msg_puts_attr(), but with a maximum length "maxlen" (in bytes).
2317 * When "maxlen" is -1 there is no maximum length.
2318 * When "maxlen" is >= 0 the message is not put in the history.
2319 */
2320 static void
Bram Moolenaar32526b32019-01-19 17:43:09 +01002321msg_puts_attr_len(char *str, int maxlen, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 /*
2324 * If redirection is on, also write to the redirection file.
2325 */
Bram Moolenaar32526b32019-01-19 17:43:09 +01002326 redir_write((char_u *)str, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327
2328 /*
2329 * Don't print anything when using ":silent cmd".
2330 */
2331 if (msg_silent != 0)
2332 return;
2333
Shougo Matsushitabe2b03c2024-04-08 22:11:50 +02002334 if (attr == 0)
2335 attr = HL_ATTR(HLF_MSG);
2336
Bram Moolenaar85a20022019-12-21 18:25:54 +01002337 // if MSG_HIST flag set, add message to history
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 if ((attr & MSG_HIST) && maxlen < 0)
2339 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002340 add_msg_hist((char_u *)str, -1, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 attr &= ~MSG_HIST;
2342 }
2343
Bram Moolenaare8070982019-10-12 17:07:06 +02002344 // When writing something to the screen after it has scrolled, requires a
2345 // wait-return prompt later. Needed when scrolling, resetting
2346 // need_wait_return after some prompt, and then outputting something
2347 // without scrolling
2348 // Not needed when only using CR to move the cursor.
2349 if (msg_scrolled != 0 && !msg_scrolled_ign && STRCMP(str, "\r") != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 need_wait_return = TRUE;
Bram Moolenaare8070982019-10-12 17:07:06 +02002351 msg_didany = TRUE; // remember that something was outputted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352
2353 /*
2354 * If there is no valid screen, use fprintf so we can see error messages.
2355 * If termcap is not active, we may be writing in an alternate console
2356 * window, cursor positioning may not work correctly (window size may be
2357 * different, e.g. for Win32 console) or we just don't know where the
2358 * cursor is.
2359 */
2360 if (msg_use_printf())
Bram Moolenaar32526b32019-01-19 17:43:09 +01002361 msg_puts_printf((char_u *)str, maxlen);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002362 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01002363 msg_puts_display((char_u *)str, maxlen, attr, FALSE);
Rob Pilling726f7f92022-01-20 14:44:38 +00002364
2365 need_fileinfo = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002366}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367
Bram Moolenaar9198de32022-08-27 21:30:03 +01002368// values for "where"
2369#define PUT_APPEND 0 // append to "lnum"
2370#define PUT_TRUNC 1 // replace "lnum"
2371#define PUT_BELOW 2 // add below "lnum"
2372 //
2373#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaar9198de32022-08-27 21:30:03 +01002374/*
Bram Moolenaar24735f22022-08-30 15:44:22 +01002375 * Put text "t_s" until "end" in the message window.
Bram Moolenaar9198de32022-08-27 21:30:03 +01002376 * "where" specifies where to put the text.
2377 */
2378 static void
2379put_msg_win(win_T *wp, int where, char_u *t_s, char_u *end, linenr_T lnum)
2380{
Bram Moolenaarbeedd0a2022-08-27 21:52:52 +01002381 char_u *p;
Bram Moolenaar9198de32022-08-27 21:30:03 +01002382
Bram Moolenaar9198de32022-08-27 21:30:03 +01002383 if (where == PUT_BELOW)
Bram Moolenaarbeedd0a2022-08-27 21:52:52 +01002384 {
2385 if (*end != NUL)
2386 {
2387 p = vim_strnsave(t_s, end - t_s);
2388 if (p == NULL)
2389 return;
2390 }
2391 else
2392 p = t_s;
2393 ml_append_buf(wp->w_buffer, lnum, p, (colnr_T)0, FALSE);
2394 if (p != t_s)
2395 vim_free(p);
2396 }
Bram Moolenaar9198de32022-08-27 21:30:03 +01002397 else
2398 {
2399 char_u *newp;
2400
2401 curbuf = wp->w_buffer;
2402 if (where == PUT_APPEND)
Bram Moolenaarbeedd0a2022-08-27 21:52:52 +01002403 {
Bram Moolenaar9198de32022-08-27 21:30:03 +01002404 newp = concat_str(ml_get(lnum), t_s);
Bram Moolenaarbeedd0a2022-08-27 21:52:52 +01002405 if (newp == NULL)
2406 return;
2407 if (*end != NUL)
2408 newp[STRLEN(ml_get(lnum)) + (end - t_s)] = NUL;
2409 }
Bram Moolenaar9198de32022-08-27 21:30:03 +01002410 else
Bram Moolenaarbeedd0a2022-08-27 21:52:52 +01002411 {
Bram Moolenaar9198de32022-08-27 21:30:03 +01002412 newp = vim_strnsave(t_s, end - t_s);
Bram Moolenaarbeedd0a2022-08-27 21:52:52 +01002413 if (newp == NULL)
2414 return;
2415 }
Bram Moolenaar9198de32022-08-27 21:30:03 +01002416 ml_replace(lnum, newp, FALSE);
2417 curbuf = curwin->w_buffer;
2418 }
2419 redraw_win_later(wp, UPD_NOT_VALID);
2420
2421 // set msg_col so that a newline is written if needed
Bram Moolenaar24735f22022-08-30 15:44:22 +01002422 msg_col += (int)(end - t_s);
Bram Moolenaar9198de32022-08-27 21:30:03 +01002423}
2424#endif
2425
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002426/*
2427 * The display part of msg_puts_attr_len().
2428 * May be called recursively to display scroll-back text.
2429 */
2430 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002431msg_puts_display(
2432 char_u *str,
2433 int maxlen,
2434 int attr,
2435 int recurse)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002436{
2437 char_u *s = str;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002438 char_u *t_s = str; // string from "t_s" to "s" is still todo
2439 int t_col = 0; // screen cells todo, 0 when "t_s" not used
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002440 int l;
2441 int cw;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002442 char_u *sb_str = str;
2443 int sb_col = msg_col;
2444 int wrap;
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002445 int did_last_char;
Bram Moolenaar9198de32022-08-27 21:30:03 +01002446#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaard54af2e2022-08-27 22:05:13 +01002447 int where = PUT_APPEND;
Bram Moolenaar9198de32022-08-27 21:30:03 +01002448 win_T *msg_win = NULL;
2449 linenr_T lnum = 1;
2450
Bram Moolenaara2a89732022-08-31 14:46:18 +01002451 if (in_echowindow)
Bram Moolenaar9198de32022-08-27 21:30:03 +01002452 {
2453 msg_win = popup_get_message_win();
2454
2455 if (msg_win != NULL)
2456 {
2457 if (!popup_message_win_visible())
2458 {
2459 if (*str == NL)
2460 {
2461 // When not showing the message window and the output
2462 // starts with a NL show the message normally.
2463 msg_win = NULL;
2464 }
2465 else
2466 {
2467 // currently hidden, make it empty
2468 curbuf = msg_win->w_buffer;
2469 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0)
2470 ml_delete(1);
2471 curbuf = curwin->w_buffer;
2472 }
2473 }
2474 else
2475 {
2476 lnum = msg_win->w_buffer->b_ml.ml_line_count;
2477 if (msg_col == 0)
2478 where = PUT_TRUNC;
2479 }
2480 }
2481 }
2482#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483
2484 did_wait_return = FALSE;
Bram Moolenaar57b7fe82007-08-05 17:20:43 +00002485 while ((maxlen < 0 || (int)(s - str) < maxlen) && *s != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 {
2487 /*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002488 * We are at the end of the screen line when:
2489 * - When outputting a newline.
2490 * - When outputting a character in the last column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002492 if (!recurse && msg_row >= Rows - 1 && (*s == '\n' || (
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493#ifdef FEAT_RIGHTLEFT
2494 cmdmsg_rl
2495 ? (
2496 msg_col <= 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002497 || (*s == TAB && msg_col <= 7)
2498 || (has_mbyte && (*mb_ptr2cells)(s) > 1 && msg_col <= 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 :
2500#endif
Bram Moolenaar0efd1bd2019-12-11 19:00:04 +01002501 ((*s != '\r' && msg_col + t_col >= Columns - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 || (*s == TAB && msg_col + t_col >= ((Columns - 1) & ~7))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 || (has_mbyte && (*mb_ptr2cells)(s) > 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002504 && msg_col + t_col >= Columns - 2)))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002506 /*
2507 * The screen is scrolled up when at the last row (some terminals
2508 * scroll automatically, some don't. To avoid problems we scroll
2509 * ourselves).
2510 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 if (t_col > 0)
Bram Moolenaar9198de32022-08-27 21:30:03 +01002512 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002513 // output postponed text
Bram Moolenaar9198de32022-08-27 21:30:03 +01002514#ifdef HAS_MESSAGE_WINDOW
2515 if (msg_win != NULL)
2516 {
2517 put_msg_win(msg_win, where, t_s, s, lnum);
2518 t_col = 0;
2519 where = PUT_BELOW;
2520 }
2521 else
2522#endif
2523 t_puts(&t_col, t_s, s, attr);
2524 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525
Bram Moolenaar85a20022019-12-21 18:25:54 +01002526 // When no more prompt and no more room, truncate here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 if (msg_no_more && lines_left == 0)
2528 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529
Bram Moolenaar9198de32022-08-27 21:30:03 +01002530#ifdef HAS_MESSAGE_WINDOW
2531 if (msg_win == NULL)
2532#endif
2533 // Scroll the screen up one line.
2534 msg_scroll_up();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535
2536 msg_row = Rows - 2;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002537 if (msg_col >= Columns) // can happen after screen resize
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 msg_col = Columns - 1;
2539
Bram Moolenaar85a20022019-12-21 18:25:54 +01002540 // Display char in last column before showing more-prompt.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002541 if (*s >= ' '
2542#ifdef FEAT_RIGHTLEFT
2543 && !cmdmsg_rl
2544#endif
2545 )
2546 {
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002547 if (has_mbyte)
2548 {
2549 if (enc_utf8 && maxlen >= 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002550 // avoid including composing chars after the end
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002551 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002552 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002553 l = (*mb_ptr2len)(s);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002554 s = screen_puts_mbyte(s, l, attr);
2555 }
2556 else
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002557 msg_screen_putchar(*s++, attr);
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002558 did_last_char = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002559 }
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002560 else
2561 did_last_char = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002562
2563 if (p_more)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002564 // store text for scrolling back
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002565 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
2566
Bram Moolenaar9198de32022-08-27 21:30:03 +01002567#ifdef HAS_MESSAGE_WINDOW
2568 if (msg_win == NULL)
2569 {
2570#endif
2571 inc_msg_scrolled();
Bram Moolenaar13608d82022-08-29 15:06:50 +01002572 need_wait_return = TRUE; // may need wait_return() in main()
Bram Moolenaar9198de32022-08-27 21:30:03 +01002573 redraw_cmdline = TRUE;
2574 if (cmdline_row > 0 && !exmode_active)
2575 --cmdline_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576
Bram Moolenaar9198de32022-08-27 21:30:03 +01002577 /*
2578 * If screen is completely filled and 'more' is set then wait
2579 * for a character.
2580 */
2581 if (lines_left > 0)
2582 --lines_left;
2583#ifdef HAS_MESSAGE_WINDOW
2584 }
2585#endif
Bram Moolenaar24959102022-05-07 20:01:16 +01002586 if (p_more && lines_left == 0 && State != MODE_HITRETURN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 && !msg_no_more && !exmode_active)
2588 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589#ifdef FEAT_CON_DIALOG
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002590 if (do_more_prompt(NUL))
2591 s = confirm_msg_tail;
2592#else
2593 (void)do_more_prompt(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594#endif
2595 if (quit_more)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002596 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002598
Bram Moolenaar85a20022019-12-21 18:25:54 +01002599 // When we displayed a char in last column need to check if there
2600 // is still more.
Bram Moolenaarc27c8d52007-09-06 10:54:51 +00002601 if (did_last_char)
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002602 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603 }
2604
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002605 wrap = *s == '\n'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 || msg_col + t_col >= Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 || (has_mbyte && (*mb_ptr2cells)(s) > 1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002608 && msg_col + t_col >= Columns - 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002609 if (t_col > 0 && (wrap || *s == '\r' || *s == '\b'
2610 || *s == '\t' || *s == BELL))
Bram Moolenaar9198de32022-08-27 21:30:03 +01002611 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002612 // output any postponed text
Bram Moolenaar9198de32022-08-27 21:30:03 +01002613#ifdef HAS_MESSAGE_WINDOW
2614 if (msg_win != NULL)
2615 {
2616 put_msg_win(msg_win, where, t_s, s, lnum);
2617 t_col = 0;
2618 where = PUT_BELOW;
2619 }
2620 else
2621#endif
2622 t_puts(&t_col, t_s, s, attr);
2623 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002624
2625 if (wrap && p_more && !recurse)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002626 // store text for scrolling back
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002627 store_sb_text(&sb_str, s, attr, &sb_col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628
Bram Moolenaar85a20022019-12-21 18:25:54 +01002629 if (*s == '\n') // go to next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 {
Bram Moolenaar9198de32022-08-27 21:30:03 +01002631#ifdef HAS_MESSAGE_WINDOW
2632 if (msg_win != NULL)
2633 {
2634 // Ignore a NL when the buffer is empty, it is used to scroll
2635 // up the text.
2636 if ((msg_win->w_buffer->b_ml.ml_flags & ML_EMPTY) == 0)
2637 {
2638 put_msg_win(msg_win, PUT_BELOW, t_s, t_s, lnum);
2639 ++lnum;
2640 }
2641 }
2642 else
2643#endif
2644 msg_didout = FALSE; // remember that line is empty
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002645#ifdef FEAT_RIGHTLEFT
2646 if (cmdmsg_rl)
2647 msg_col = Columns - 1;
2648 else
2649#endif
2650 msg_col = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002651 if (++msg_row >= Rows) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 msg_row = Rows - 1;
2653 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002654 else if (*s == '\r') // go to column 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 {
2656 msg_col = 0;
Bram Moolenaard54af2e2022-08-27 22:05:13 +01002657#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaar9198de32022-08-27 21:30:03 +01002658 where = PUT_TRUNC;
Bram Moolenaard54af2e2022-08-27 22:05:13 +01002659#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002661 else if (*s == '\b') // go to previous char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 {
2663 if (msg_col)
2664 --msg_col;
2665 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002666 else if (*s == TAB) // translate Tab into spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 {
Bram Moolenaar9198de32022-08-27 21:30:03 +01002668#ifdef HAS_MESSAGE_WINDOW
2669 if (msg_win != NULL)
2670 msg_col = (msg_col + 7) % 8;
2671 else
2672#endif
2673 do
2674 msg_screen_putchar(' ', attr);
2675 while (msg_col & 7);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01002677 else if (*s == BELL) // beep (from ":sh")
Bram Moolenaar165bc692015-07-21 17:53:25 +02002678 vim_beep(BO_SH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 else
2680 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 if (has_mbyte)
2682 {
2683 cw = (*mb_ptr2cells)(s);
2684 if (enc_utf8 && maxlen >= 0)
Bram Moolenaar85a20022019-12-21 18:25:54 +01002685 // avoid including composing chars after the end
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002686 l = utfc_ptr2len_len(s, (int)((str + maxlen) - s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002688 l = (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 }
2690 else
2691 {
2692 cw = 1;
2693 l = 1;
2694 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002695
Bram Moolenaar85a20022019-12-21 18:25:54 +01002696 // When drawing from right to left or when a double-wide character
2697 // doesn't fit, draw a single character here. Otherwise collect
2698 // characters and draw them all at once later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 if (
2700# ifdef FEAT_RIGHTLEFT
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002701 cmdmsg_rl ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702# endif
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002703 (cw > 1 && msg_col + t_col >= Columns - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 if (l > 1)
2706 s = screen_puts_mbyte(s, l, attr) - 1;
2707 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 msg_screen_putchar(*s, attr);
2709 }
2710 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002712 // postpone this character until later
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 if (t_col == 0)
2714 t_s = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 t_col += cw;
2716 s += l - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 }
2718 }
2719 ++s;
2720 }
2721
Bram Moolenaar85a20022019-12-21 18:25:54 +01002722 // output any postponed text
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 if (t_col > 0)
Bram Moolenaar9198de32022-08-27 21:30:03 +01002724 {
2725#ifdef HAS_MESSAGE_WINDOW
2726 if (msg_win != NULL)
2727 put_msg_win(msg_win, where, t_s, s, lnum);
2728 else
2729#endif
2730 t_puts(&t_col, t_s, s, attr);
2731 }
2732
2733#ifdef HAS_MESSAGE_WINDOW
2734 if (msg_win != NULL)
2735 popup_show_message_win();
2736#endif
Bram Moolenaar11901392022-09-26 19:50:44 +01002737 // Store the text for scroll back, unless it's a newline by itself.
2738 if (p_more && !recurse && !(s == sb_str + 1 && *sb_str == '\n'))
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002739 store_sb_text(&sb_str, s, attr, &sb_col, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740
2741 msg_check();
2742}
2743
2744/*
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002745 * Return TRUE when ":filter pattern" was used and "msg" does not match
2746 * "pattern".
2747 */
2748 int
2749message_filtered(char_u *msg)
2750{
Bram Moolenaard29459b2016-08-26 22:29:11 +02002751 int match;
2752
Bram Moolenaare1004402020-10-24 20:49:43 +02002753 if (cmdmod.cmod_filter_regmatch.regprog == NULL)
Bram Moolenaard29459b2016-08-26 22:29:11 +02002754 return FALSE;
Bram Moolenaare1004402020-10-24 20:49:43 +02002755 match = vim_regexec(&cmdmod.cmod_filter_regmatch, msg, (colnr_T)0);
2756 return cmdmod.cmod_filter_force ? match : !match;
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002757}
2758
2759/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002760 * Scroll the screen up one line for displaying the next message line.
2761 */
2762 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002763msg_scroll_up(void)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002764{
Bram Moolenaar9198de32022-08-27 21:30:03 +01002765#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaara2a89732022-08-31 14:46:18 +01002766 if (in_echowindow)
Bram Moolenaar9198de32022-08-27 21:30:03 +01002767 return;
2768#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002769#ifdef FEAT_GUI
Bram Moolenaar85a20022019-12-21 18:25:54 +01002770 // Remove the cursor before scrolling, ScreenLines[] is going
2771 // to become invalid.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002772 if (gui.in_use)
2773 gui_undraw_cursor();
2774#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01002775 // scrolling up always works
Bram Moolenaara338adc2018-01-31 20:51:47 +01002776 mch_disable_flush();
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002777 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaara338adc2018-01-31 20:51:47 +01002778 mch_enable_flush();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002779
2780 if (!can_clear((char_u *)" "))
2781 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01002782 // Scrolling up doesn't result in the right background. Set the
2783 // background here. It's not efficient, but avoids that we have to do
2784 // it all over the code.
Shougo Matsushitabe2b03c2024-04-08 22:11:50 +02002785 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns,
2786 ' ', ' ', HL_ATTR(HLF_MSG));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002787
Bram Moolenaar85a20022019-12-21 18:25:54 +01002788 // Also clear the last char of the last but one line if it was not
2789 // cleared before to avoid a scroll-up.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002790 if (ScreenAttrs[LineOffset[Rows - 2] + Columns - 1] == (sattr_T)-1)
2791 screen_fill((int)Rows - 2, (int)Rows - 1,
Shougo Matsushitabe2b03c2024-04-08 22:11:50 +02002792 (int)Columns - 1, (int)Columns,
2793 ' ', ' ', HL_ATTR(HLF_MSG));
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002794 }
2795}
2796
2797/*
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002798 * Increment "msg_scrolled".
2799 */
2800 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002801inc_msg_scrolled(void)
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002802{
2803#ifdef FEAT_EVAL
2804 if (*get_vim_var_str(VV_SCROLLSTART) == NUL)
2805 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002806 char_u *p = SOURCING_NAME;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002807 char_u *tofree = NULL;
2808 int len;
2809
Bram Moolenaar85a20022019-12-21 18:25:54 +01002810 // v:scrollstart is empty, set it to the script/function name and line
2811 // number
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002812 if (p == NULL)
2813 p = (char_u *)_("Unknown");
2814 else
2815 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002816 len = (int)STRLEN(p) + 40;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002817 tofree = alloc(len);
2818 if (tofree != NULL)
2819 {
2820 vim_snprintf((char *)tofree, len, _("%s line %ld"),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002821 p, (long)SOURCING_LNUM);
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002822 p = tofree;
2823 }
2824 }
2825 set_vim_var_string(VV_SCROLLSTART, p, -1);
2826 vim_free(tofree);
2827 }
2828#endif
2829 ++msg_scrolled;
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01002830 set_must_redraw(UPD_VALID);
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002831}
2832
2833/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002834 * To be able to scroll back at the "more" and "hit-enter" prompts we need to
2835 * store the displayed text and remember where screen lines start.
2836 */
2837typedef struct msgchunk_S msgchunk_T;
2838struct msgchunk_S
2839{
2840 msgchunk_T *sb_next;
2841 msgchunk_T *sb_prev;
Bram Moolenaar85a20022019-12-21 18:25:54 +01002842 char sb_eol; // TRUE when line ends after this text
2843 int sb_msg_col; // column in which text starts
2844 int sb_attr; // text attributes
2845 char_u sb_text[1]; // text to be displayed, actually longer
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002846};
2847
Bram Moolenaar85a20022019-12-21 18:25:54 +01002848static msgchunk_T *last_msgchunk = NULL; // last displayed text
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002849
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002850static msgchunk_T *msg_sb_start(msgchunk_T *mps);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002851
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002852typedef enum {
2853 SB_CLEAR_NONE = 0,
2854 SB_CLEAR_ALL,
2855 SB_CLEAR_CMDLINE_BUSY,
2856 SB_CLEAR_CMDLINE_DONE
2857} sb_clear_T;
2858
Bram Moolenaar85a20022019-12-21 18:25:54 +01002859// When to clear text on next msg.
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002860static sb_clear_T do_clear_sb_text = SB_CLEAR_NONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002861
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002862/*
2863 * Store part of a printed message for displaying when scrolling back.
2864 */
2865 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002866store_sb_text(
Bram Moolenaar85a20022019-12-21 18:25:54 +01002867 char_u **sb_str, // start of string
2868 char_u *s, // just after string
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002869 int attr,
2870 int *sb_col,
Bram Moolenaar85a20022019-12-21 18:25:54 +01002871 int finish) // line ends
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002872{
2873 msgchunk_T *mp;
2874
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002875 if (do_clear_sb_text == SB_CLEAR_ALL
2876 || do_clear_sb_text == SB_CLEAR_CMDLINE_DONE)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002877 {
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002878 clear_sb_text(do_clear_sb_text == SB_CLEAR_ALL);
zeertzjq46af7bc2022-07-28 12:34:09 +01002879 msg_sb_eol(); // prevent messages from overlapping
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002880 do_clear_sb_text = SB_CLEAR_NONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002881 }
2882
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002883 if (s > *sb_str)
2884 {
zeertzjq1b438a82023-02-01 13:11:15 +00002885 mp = alloc(offsetof(msgchunk_T, sb_text) + (s - *sb_str) + 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002886 if (mp != NULL)
2887 {
2888 mp->sb_eol = finish;
2889 mp->sb_msg_col = *sb_col;
2890 mp->sb_attr = attr;
2891 vim_strncpy(mp->sb_text, *sb_str, s - *sb_str);
2892
2893 if (last_msgchunk == NULL)
2894 {
2895 last_msgchunk = mp;
2896 mp->sb_prev = NULL;
2897 }
2898 else
2899 {
2900 mp->sb_prev = last_msgchunk;
2901 last_msgchunk->sb_next = mp;
2902 last_msgchunk = mp;
2903 }
2904 mp->sb_next = NULL;
2905 }
2906 }
2907 else if (finish && last_msgchunk != NULL)
2908 last_msgchunk->sb_eol = TRUE;
2909
2910 *sb_str = s;
2911 *sb_col = 0;
2912}
2913
2914/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002915 * Finished showing messages, clear the scroll-back text on the next message.
2916 */
2917 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002918may_clear_sb_text(void)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002919{
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002920 do_clear_sb_text = SB_CLEAR_ALL;
2921}
2922
2923/*
zeertzjq46af7bc2022-07-28 12:34:09 +01002924 * Starting to edit the command line: do not clear messages now.
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002925 */
2926 void
2927sb_text_start_cmdline(void)
2928{
zeertzjq46af7bc2022-07-28 12:34:09 +01002929 if (do_clear_sb_text == SB_CLEAR_CMDLINE_BUSY)
2930 // Invoking command line recursively: the previous-level command line
2931 // doesn't need to be remembered as it will be redrawn when returning
2932 // to that level.
2933 sb_text_restart_cmdline();
2934 else
2935 {
2936 msg_sb_eol();
2937 do_clear_sb_text = SB_CLEAR_CMDLINE_BUSY;
2938 }
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002939}
2940
2941/*
zeertzjq46af7bc2022-07-28 12:34:09 +01002942 * Redrawing the command line: clear the last unfinished line.
2943 */
2944 void
2945sb_text_restart_cmdline(void)
2946{
2947 msgchunk_T *tofree;
2948
2949 // Needed when returning from nested command line.
2950 do_clear_sb_text = SB_CLEAR_CMDLINE_BUSY;
2951
2952 if (last_msgchunk == NULL || last_msgchunk->sb_eol)
2953 // No unfinished line: don't clear anything.
2954 return;
2955
2956 tofree = msg_sb_start(last_msgchunk);
2957 last_msgchunk = tofree->sb_prev;
2958 if (last_msgchunk != NULL)
2959 last_msgchunk->sb_next = NULL;
2960 while (tofree != NULL)
2961 {
2962 msgchunk_T *tofree_next = tofree->sb_next;
2963
2964 vim_free(tofree);
2965 tofree = tofree_next;
2966 }
2967}
2968
2969/*
2970 * Ending to edit the command line: clear old lines but the last one later.
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002971 */
2972 void
2973sb_text_end_cmdline(void)
2974{
2975 do_clear_sb_text = SB_CLEAR_CMDLINE_DONE;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00002976}
2977
2978/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002979 * Clear any text remembered for scrolling back.
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002980 * When "all" is FALSE keep the last line.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002981 * Called when redrawing the screen.
2982 */
2983 void
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002984clear_sb_text(int all)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002985{
2986 msgchunk_T *mp;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002987 msgchunk_T **lastp;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002988
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002989 if (all)
2990 lastp = &last_msgchunk;
2991 else
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00002992 {
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002993 if (last_msgchunk == NULL)
2994 return;
zeertzjqecdc82e2022-07-25 19:50:57 +01002995 lastp = &msg_sb_start(last_msgchunk)->sb_prev;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002996 }
2997
2998 while (*lastp != NULL)
2999 {
3000 mp = (*lastp)->sb_prev;
3001 vim_free(*lastp);
3002 *lastp = mp;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003003 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003004}
3005
3006/*
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003007 * "g<" command.
3008 */
3009 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003010show_sb_text(void)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003011{
3012 msgchunk_T *mp;
3013
Bram Moolenaar85a20022019-12-21 18:25:54 +01003014 // Only show something if there is more than one line, otherwise it looks
3015 // weird, typing a command without output results in one line.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003016 mp = msg_sb_start(last_msgchunk);
3017 if (mp == NULL || mp->sb_prev == NULL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02003018 vim_beep(BO_MESS);
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003019 else
3020 {
3021 do_more_prompt('G');
3022 wait_return(FALSE);
3023 }
3024}
3025
3026/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003027 * Move to the start of screen line in already displayed text.
3028 */
3029 static msgchunk_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003030msg_sb_start(msgchunk_T *mps)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003031{
3032 msgchunk_T *mp = mps;
3033
3034 while (mp != NULL && mp->sb_prev != NULL && !mp->sb_prev->sb_eol)
3035 mp = mp->sb_prev;
3036 return mp;
3037}
3038
3039/*
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02003040 * Mark the last message chunk as finishing the line.
3041 */
3042 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003043msg_sb_eol(void)
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02003044{
3045 if (last_msgchunk != NULL)
3046 last_msgchunk->sb_eol = TRUE;
3047}
3048
3049/*
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003050 * Display a screen line from previously displayed text at row "row".
Bram Moolenaar838b7462022-09-26 15:19:56 +01003051 * When "clear_to_eol" is set clear the rest of the screen line.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003052 * Returns a pointer to the text for the next line (can be NULL).
3053 */
3054 static msgchunk_T *
Bram Moolenaar838b7462022-09-26 15:19:56 +01003055disp_sb_line(int row, msgchunk_T *smp, int clear_to_eol)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003056{
3057 msgchunk_T *mp = smp;
3058 char_u *p;
3059
3060 for (;;)
3061 {
3062 msg_row = row;
3063 msg_col = mp->sb_msg_col;
3064 p = mp->sb_text;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003065 if (*p == '\n') // don't display the line break
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003066 ++p;
3067 msg_puts_display(p, -1, mp->sb_attr, TRUE);
Bram Moolenaar838b7462022-09-26 15:19:56 +01003068
3069 // If clearing the screen did not work (e.g. because of a background
3070 // color and t_ut isn't set) clear until the last column here.
3071 if (clear_to_eol)
Shougo Matsushitabe2b03c2024-04-08 22:11:50 +02003072 screen_fill(row, row + 1, msg_col, (int)Columns,
3073 ' ', ' ', HL_ATTR(HLF_MSG));
Bram Moolenaar838b7462022-09-26 15:19:56 +01003074
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003075 if (mp->sb_eol || mp->sb_next == NULL)
3076 break;
3077 mp = mp->sb_next;
3078 }
3079 return mp->sb_next;
3080}
3081
3082/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083 * Output any postponed text for msg_puts_attr_len().
3084 */
3085 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003086t_puts(
3087 int *t_col,
3088 char_u *t_s,
3089 char_u *s,
3090 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091{
Bram Moolenaar85a20022019-12-21 18:25:54 +01003092 // output postponed text
3093 msg_didout = TRUE; // remember that line is not empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 screen_puts_len(t_s, (int)(s - t_s), msg_row, msg_col, attr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003095 msg_col += *t_col;
3096 *t_col = 0;
Bram Moolenaar85a20022019-12-21 18:25:54 +01003097 // If the string starts with a composing character don't increment the
3098 // column position for it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099 if (enc_utf8 && utf_iscomposing(utf_ptr2char(t_s)))
3100 --msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 if (msg_col >= Columns)
3102 {
3103 msg_col = 0;
3104 ++msg_row;
3105 }
3106}
3107
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108/*
3109 * Returns TRUE when messages should be printed with mch_errmsg().
3110 * This is used when there is no valid screen, so we can see error messages.
3111 * If termcap is not active, we may be writing in an alternate console
3112 * window, cursor positioning may not work correctly (window size may be
3113 * different, e.g. for Win32 console) or we just don't know where the
3114 * cursor is.
3115 */
3116 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003117msg_use_printf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118{
3119 return (!msg_check_screen()
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003120#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3121# ifdef VIMDLL
3122 || (!gui.in_use && !termcap_active)
3123# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 || !termcap_active
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003125# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126#endif
3127 || (swapping_screen() && !termcap_active)
3128 );
3129}
3130
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003131/*
3132 * Print a message when there is no valid screen.
3133 */
3134 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003135msg_puts_printf(char_u *str, int maxlen)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003136{
3137 char_u *s = str;
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003138 char_u *buf = NULL;
3139 char_u *p = s;
3140
Bram Moolenaar4f974752019-02-17 17:44:42 +01003141#ifdef MSWIN
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003142 if (!(silent_mode && p_verbose == 0))
Bram Moolenaar85a20022019-12-21 18:25:54 +01003143 mch_settmode(TMODE_COOK); // handle CR and NL correctly
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003144#endif
Bram Moolenaar2321ca22016-09-09 14:17:18 +02003145 while ((maxlen < 0 || (int)(s - str) < maxlen) && *s != NUL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003146 {
3147 if (!(silent_mode && p_verbose == 0))
3148 {
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003149 // NL --> CR NL translation (for Unix, not for "--version")
3150 if (*s == NL)
3151 {
3152 int n = (int)(s - p);
3153
3154 buf = alloc(n + 3);
Bram Moolenaar6f10c702019-08-20 22:58:37 +02003155 if (buf != NULL)
3156 {
3157 memcpy(buf, p, n);
3158 if (!info_message)
3159 buf[n++] = CAR;
3160 buf[n++] = NL;
3161 buf[n++] = NUL;
3162 if (info_message) // informative message, not an error
3163 mch_msg((char *)buf);
3164 else
3165 mch_errmsg((char *)buf);
3166 vim_free(buf);
3167 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003168 p = s + 1;
3169 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003170 }
3171
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003172 // primitive way to compute the current column
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003173#ifdef FEAT_RIGHTLEFT
3174 if (cmdmsg_rl)
3175 {
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003176 if (*s == CAR || *s == NL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003177 msg_col = Columns - 1;
3178 else
3179 --msg_col;
3180 }
3181 else
3182#endif
3183 {
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003184 if (*s == CAR || *s == NL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003185 msg_col = 0;
3186 else
3187 ++msg_col;
3188 }
3189 ++s;
3190 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003191
3192 if (*p != NUL && !(silent_mode && p_verbose == 0))
3193 {
Bram Moolenaara97c3632021-06-15 22:39:11 +02003194 char_u *tofree = NULL;
Bram Moolenaar97c2c052019-02-22 13:42:07 +01003195
Bram Moolenaarc32949b2023-01-04 15:56:51 +00003196 if (maxlen > 0 && vim_strlen_maxlen((char *)p, (size_t)maxlen)
3197 >= (size_t)maxlen)
Bram Moolenaar97c2c052019-02-22 13:42:07 +01003198 {
Bram Moolenaara97c3632021-06-15 22:39:11 +02003199 tofree = vim_strnsave(p, (size_t)maxlen);
3200 p = tofree;
Bram Moolenaar97c2c052019-02-22 13:42:07 +01003201 }
Bram Moolenaara97c3632021-06-15 22:39:11 +02003202 if (p != NULL)
3203 {
3204 if (info_message)
3205 mch_msg((char *)p);
3206 else
3207 mch_errmsg((char *)p);
3208 vim_free(tofree);
3209 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003210 }
3211
3212 msg_didout = TRUE; // assume that line is not empty
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003213
Bram Moolenaar4f974752019-02-17 17:44:42 +01003214#ifdef MSWIN
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003215 if (!(silent_mode && p_verbose == 0))
3216 mch_settmode(TMODE_RAW);
3217#endif
3218}
3219
3220/*
3221 * Show the more-prompt and handle the user response.
3222 * This takes care of scrolling back and displaying previously displayed text.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003223 * When at hit-enter prompt "typed_char" is the already typed character,
3224 * otherwise it's NUL.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003225 * Returns TRUE when jumping ahead to "confirm_msg_tail".
3226 */
3227 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003228do_more_prompt(int typed_char)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003229{
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01003230 static int entered = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003231 int used_typed_char = typed_char;
3232 int oldState = State;
3233 int c;
3234#ifdef FEAT_CON_DIALOG
3235 int retval = FALSE;
3236#endif
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003237 int toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003238 msgchunk_T *mp_last = NULL;
3239 msgchunk_T *mp;
3240 int i;
Shougo Matsushitabe2b03c2024-04-08 22:11:50 +02003241 int msg_attr;
3242
3243 msg_attr = HL_ATTR(HLF_MSG);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003244
Bram Moolenaar85a20022019-12-21 18:25:54 +01003245 // We get called recursively when a timer callback outputs a message. In
3246 // that case don't show another prompt. Also when at the hit-Enter prompt
3247 // and nothing was typed.
Bram Moolenaar24959102022-05-07 20:01:16 +01003248 if (entered || (State == MODE_HITRETURN && typed_char == 0))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01003249 return FALSE;
3250 entered = TRUE;
3251
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003252 if (typed_char == 'G')
3253 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003254 // "g<": Find first line on the last page.
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003255 mp_last = msg_sb_start(last_msgchunk);
3256 for (i = 0; i < Rows - 2 && mp_last != NULL
3257 && mp_last->sb_prev != NULL; ++i)
3258 mp_last = msg_sb_start(mp_last->sb_prev);
3259 }
3260
Bram Moolenaar24959102022-05-07 20:01:16 +01003261 State = MODE_ASKMORE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003262 setmouse();
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003263 if (typed_char == NUL)
3264 msg_moremsg(FALSE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003265 for (;;)
3266 {
3267 /*
3268 * Get a typed character directly from the user.
3269 */
3270 if (used_typed_char != NUL)
3271 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003272 c = used_typed_char; // was typed at hit-enter prompt
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003273 used_typed_char = NUL;
3274 }
3275 else
3276 c = get_keystroke();
3277
3278#if defined(FEAT_MENU) && defined(FEAT_GUI)
3279 if (c == K_MENU)
3280 {
Bram Moolenaar24959102022-05-07 20:01:16 +01003281 int idx = get_menu_index(current_menu, MODE_ASKMORE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003282
Bram Moolenaar85a20022019-12-21 18:25:54 +01003283 // Used a menu. If it starts with CTRL-Y, it must
3284 // be a "Copy" for the clipboard. Otherwise
3285 // assume that we end
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003286 if (idx == MENU_INDEX_INVALID)
3287 continue;
3288 c = *current_menu->strings[idx];
3289 if (c != NUL && current_menu->strings[idx][1] != NUL)
3290 ins_typebuf(current_menu->strings[idx] + 1,
3291 current_menu->noremap[idx], 0, TRUE,
3292 current_menu->silent[idx]);
3293 }
3294#endif
3295
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003296 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003297 switch (c)
3298 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003299 case BS: // scroll one line back
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003300 case K_BS:
3301 case 'k':
3302 case K_UP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003303 toscroll = -1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003304 break;
3305
Bram Moolenaar85a20022019-12-21 18:25:54 +01003306 case CAR: // one extra line
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003307 case NL:
3308 case 'j':
3309 case K_DOWN:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003310 toscroll = 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003311 break;
3312
Bram Moolenaar85a20022019-12-21 18:25:54 +01003313 case 'u': // Up half a page
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003314 toscroll = -(Rows / 2);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003315 break;
3316
Bram Moolenaar85a20022019-12-21 18:25:54 +01003317 case 'd': // Down half a page
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003318 toscroll = Rows / 2;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003319 break;
3320
Bram Moolenaar85a20022019-12-21 18:25:54 +01003321 case 'b': // one page back
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00003322 case K_PAGEUP:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003323 toscroll = -(Rows - 1);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003324 break;
3325
Bram Moolenaar85a20022019-12-21 18:25:54 +01003326 case ' ': // one extra page
Bram Moolenaar2a9e4df2009-02-21 23:59:19 +00003327 case 'f':
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003328 case K_PAGEDOWN:
3329 case K_LEFTMOUSE:
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003330 toscroll = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003331 break;
3332
Bram Moolenaar85a20022019-12-21 18:25:54 +01003333 case 'g': // all the way back to the start
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003334 toscroll = -999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003335 break;
3336
Bram Moolenaar85a20022019-12-21 18:25:54 +01003337 case 'G': // all the way to the end
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003338 toscroll = 999999;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003339 lines_left = 999999;
3340 break;
3341
Bram Moolenaar85a20022019-12-21 18:25:54 +01003342 case ':': // start new command line
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003343#ifdef FEAT_CON_DIALOG
3344 if (!confirm_msg_used)
3345#endif
3346 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003347 // Since got_int is set all typeahead will be flushed, but we
3348 // want to keep this ':', remember that in a special way.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003349 typeahead_noflush(':');
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02003350#ifdef FEAT_TERMINAL
3351 skip_term_loop = TRUE;
3352#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01003353 cmdline_row = Rows - 1; // put ':' on this line
3354 skip_redraw = TRUE; // skip redraw once
3355 need_wait_return = FALSE; // don't wait in main()
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003356 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003357 // FALLTHROUGH
3358 case 'q': // quit
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003359 case Ctrl_C:
3360 case ESC:
3361#ifdef FEAT_CON_DIALOG
3362 if (confirm_msg_used)
3363 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003364 // Jump to the choices of the dialog.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003365 retval = TRUE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003366 }
3367 else
3368#endif
3369 {
3370 got_int = TRUE;
3371 quit_more = TRUE;
3372 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01003373 // When there is some more output (wrapping line) display that
3374 // without another prompt.
Bram Moolenaar51306d22009-02-24 03:38:04 +00003375 lines_left = Rows - 1;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003376 break;
3377
3378#ifdef FEAT_CLIPBOARD
3379 case Ctrl_Y:
Bram Moolenaar85a20022019-12-21 18:25:54 +01003380 // Strange way to allow copying (yanking) a modeless
3381 // selection at the more prompt. Use CTRL-Y,
3382 // because the same is used in Cmdline-mode and at the
3383 // hit-enter prompt. However, scrolling one line up
3384 // might be expected...
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003385 if (clip_star.state == SELECT_DONE)
3386 clip_copy_modeless_selection(TRUE);
3387 continue;
3388#endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01003389 default: // no valid response
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003390 msg_moremsg(TRUE);
3391 continue;
3392 }
3393
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003394 if (toscroll != 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003395 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003396 if (toscroll < 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003397 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003398 // go to start of last line
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003399 if (mp_last == NULL)
3400 mp = msg_sb_start(last_msgchunk);
3401 else if (mp_last->sb_prev != NULL)
3402 mp = msg_sb_start(mp_last->sb_prev);
3403 else
3404 mp = NULL;
3405
Bram Moolenaar85a20022019-12-21 18:25:54 +01003406 // go to start of line at top of the screen
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003407 for (i = 0; i < Rows - 2 && mp != NULL && mp->sb_prev != NULL;
3408 ++i)
3409 mp = msg_sb_start(mp->sb_prev);
3410
3411 if (mp != NULL && mp->sb_prev != NULL)
3412 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003413 // Find line to be displayed at top.
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003414 for (i = 0; i > toscroll; --i)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003415 {
3416 if (mp == NULL || mp->sb_prev == NULL)
3417 break;
3418 mp = msg_sb_start(mp->sb_prev);
3419 if (mp_last == NULL)
3420 mp_last = msg_sb_start(last_msgchunk);
3421 else
3422 mp_last = msg_sb_start(mp_last->sb_prev);
3423 }
3424
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003425 if (toscroll == -1 && screen_ins_lines(0, 0, 1,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02003426 (int)Rows, 0, NULL) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003427 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003428 // display line at top
Bram Moolenaar838b7462022-09-26 15:19:56 +01003429 (void)disp_sb_line(0, mp, FALSE);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003430 }
3431 else
3432 {
Bram Moolenaar838b7462022-09-26 15:19:56 +01003433 int did_clear = screenclear();
3434
Bram Moolenaar85a20022019-12-21 18:25:54 +01003435 // redisplay all lines
Bram Moolenaar773560b2006-05-06 21:38:18 +00003436 for (i = 0; mp != NULL && i < Rows - 1; ++i)
3437 {
Bram Moolenaar838b7462022-09-26 15:19:56 +01003438 mp = disp_sb_line(i, mp, !did_clear);
Bram Moolenaar773560b2006-05-06 21:38:18 +00003439 ++msg_scrolled;
3440 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003441 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003442 toscroll = 0;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003443 }
3444 }
3445 else
3446 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003447 // First display any text that we scrolled back.
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003448 while (toscroll > 0 && mp_last != NULL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003449 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003450 // scroll up, display line at bottom
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003451 msg_scroll_up();
Bram Moolenaarbb15b652005-10-03 21:52:09 +00003452 inc_msg_scrolled();
Shougo Matsushitabe2b03c2024-04-08 22:11:50 +02003453 screen_fill((int)Rows - 2, (int)Rows - 1, 0, (int)Columns,
3454 ' ', ' ', msg_attr);
Bram Moolenaar838b7462022-09-26 15:19:56 +01003455 mp_last = disp_sb_line((int)Rows - 2, mp_last, FALSE);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003456 --toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003457 }
3458 }
3459
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003460 if (toscroll <= 0)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003461 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003462 // displayed the requested text, more prompt again
Shougo Matsushitabe2b03c2024-04-08 22:11:50 +02003463 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns,
3464 ' ', ' ', msg_attr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003465 msg_moremsg(FALSE);
3466 continue;
3467 }
3468
Bram Moolenaar85a20022019-12-21 18:25:54 +01003469 // display more text, return to caller
Bram Moolenaar70b2a562012-01-10 22:26:17 +01003470 lines_left = toscroll;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003471 }
3472
3473 break;
3474 }
3475
Bram Moolenaar85a20022019-12-21 18:25:54 +01003476 // clear the --more-- message
Shougo Matsushitabe2b03c2024-04-08 22:11:50 +02003477 screen_fill((int)Rows - 1, (int)Rows, 0, (int)Columns, ' ', ' ', msg_attr);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003478 State = oldState;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003479 setmouse();
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003480 if (quit_more)
3481 {
3482 msg_row = Rows - 1;
3483 msg_col = 0;
3484 }
3485#ifdef FEAT_RIGHTLEFT
3486 else if (cmdmsg_rl)
3487 msg_col = Columns - 1;
3488#endif
3489
Bram Moolenaarbfb96c02016-03-19 17:05:20 +01003490 entered = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003491#ifdef FEAT_CON_DIALOG
3492 return retval;
3493#else
3494 return FALSE;
3495#endif
3496}
3497
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498#if defined(USE_MCH_ERRMSG) || defined(PROTO)
3499
3500#ifdef mch_errmsg
3501# undef mch_errmsg
3502#endif
3503#ifdef mch_msg
3504# undef mch_msg
3505#endif
3506
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003507#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3508 static void
3509mch_errmsg_c(char *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510{
Bram Moolenaar0f77d6a2019-02-14 20:55:09 +01003511 int len = (int)STRLEN(str);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003512 DWORD nwrite = 0;
3513 DWORD mode = 0;
3514 HANDLE h = GetStdHandle(STD_ERROR_HANDLE);
3515
3516 if (GetConsoleMode(h, &mode) && enc_codepage >= 0
3517 && (int)GetConsoleCP() != enc_codepage)
3518 {
3519 WCHAR *w = enc_to_utf16((char_u *)str, &len);
3520
3521 WriteConsoleW(h, w, len, &nwrite, NULL);
3522 vim_free(w);
3523 }
3524 else
3525 {
3526 fprintf(stderr, "%s", str);
3527 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003528}
3529#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003531/*
3532 * Give an error message. To be used when the screen hasn't been initialized
3533 * yet. When stderr can't be used, collect error messages until the GUI has
3534 * started and they can be displayed in a message box.
3535 */
3536 void
3537mch_errmsg(char *str)
3538{
3539#if !defined(MSWIN) || defined(FEAT_GUI_MSWIN)
3540 int len;
3541#endif
3542
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02003543#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003544 // On Unix use stderr if it's a tty.
3545 // When not going to start the GUI also use stderr.
3546 // On Mac, when started from Finder, stderr is the console.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 if (
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003548# ifdef UNIX
3549# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003551# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 isatty(2)
3553# endif
3554# ifdef FEAT_GUI
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003555 ||
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003556# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003557# endif
3558# ifdef FEAT_GUI
3559 !(gui.in_use || gui.starting)
3560# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 )
3562 {
3563 fprintf(stderr, "%s", str);
3564 return;
3565 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003566#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003568#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3569# ifdef VIMDLL
3570 if (!(gui.in_use || gui.starting))
3571# endif
3572 {
3573 mch_errmsg_c(str);
3574 return;
3575 }
3576#endif
3577
3578#if !defined(MSWIN) || defined(FEAT_GUI_MSWIN)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003579 // avoid a delay for a message that isn't there
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 emsg_on_display = FALSE;
3581
3582 len = (int)STRLEN(str) + 1;
3583 if (error_ga.ga_growsize == 0)
3584 {
3585 error_ga.ga_growsize = 80;
3586 error_ga.ga_itemsize = 1;
3587 }
3588 if (ga_grow(&error_ga, len) == OK)
3589 {
3590 mch_memmove((char_u *)error_ga.ga_data + error_ga.ga_len,
3591 (char_u *)str, len);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003592# ifdef UNIX
Bram Moolenaar85a20022019-12-21 18:25:54 +01003593 // remove CR characters, they are displayed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 {
3595 char_u *p;
3596
3597 p = (char_u *)error_ga.ga_data + error_ga.ga_len;
3598 for (;;)
3599 {
3600 p = vim_strchr(p, '\r');
3601 if (p == NULL)
3602 break;
3603 *p = ' ';
3604 }
3605 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003606# endif
Bram Moolenaar85a20022019-12-21 18:25:54 +01003607 --len; // don't count the NUL at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 error_ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 }
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003610#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611}
3612
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003613#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3614 static void
3615mch_msg_c(char *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616{
Bram Moolenaar0f77d6a2019-02-14 20:55:09 +01003617 int len = (int)STRLEN(str);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003618 DWORD nwrite = 0;
3619 DWORD mode;
3620 HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
3621
3622
3623 if (GetConsoleMode(h, &mode) && enc_codepage >= 0
3624 && (int)GetConsoleCP() != enc_codepage)
3625 {
3626 WCHAR *w = enc_to_utf16((char_u *)str, &len);
3627
3628 WriteConsoleW(h, w, len, &nwrite, NULL);
3629 vim_free(w);
3630 }
3631 else
3632 {
3633 printf("%s", str);
3634 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003635}
3636#endif
3637
3638/*
3639 * Give a message. To be used when the screen hasn't been initialized yet.
3640 * When there is no tty, collect messages until the GUI has started and they
3641 * can be displayed in a message box.
3642 */
3643 void
3644mch_msg(char *str)
3645{
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02003646#if (defined(UNIX) || defined(FEAT_GUI)) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003647 // On Unix use stdout if we have a tty. This allows "vim -h | more" and
3648 // uses mch_errmsg() when started from the desktop.
3649 // When not going to start the GUI also use stdout.
3650 // On Mac, when started from Finder, stderr is the console.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 if (
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003652# ifdef UNIX
3653# ifdef MACOS_X
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 (isatty(2) && strcmp("/dev/console", ttyname(2)) != 0)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003655# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 isatty(2)
Bram Moolenaareae1b912019-05-09 15:12:55 +02003657# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658# ifdef FEAT_GUI
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003659 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003661# endif
3662# ifdef FEAT_GUI
3663 !(gui.in_use || gui.starting)
3664# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 )
3666 {
3667 printf("%s", str);
3668 return;
3669 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003670#endif
3671
3672#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3673# ifdef VIMDLL
3674 if (!(gui.in_use || gui.starting))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003676 {
3677 mch_msg_c(str);
3678 return;
3679 }
3680#endif
3681#if !defined(MSWIN) || defined(FEAT_GUI_MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 mch_errmsg(str);
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +01003683#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684}
Bram Moolenaar85a20022019-12-21 18:25:54 +01003685#endif // USE_MCH_ERRMSG
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686
3687/*
3688 * Put a character on the screen at the current message position and advance
3689 * to the next position. Only for printable ASCII!
3690 */
3691 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003692msg_screen_putchar(int c, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693{
Bram Moolenaar85a20022019-12-21 18:25:54 +01003694 msg_didout = TRUE; // remember that line is not empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 screen_putchar(c, msg_row, msg_col, attr);
3696#ifdef FEAT_RIGHTLEFT
3697 if (cmdmsg_rl)
3698 {
3699 if (--msg_col == 0)
3700 {
3701 msg_col = Columns;
3702 ++msg_row;
3703 }
3704 }
3705 else
3706#endif
3707 {
3708 if (++msg_col >= Columns)
3709 {
3710 msg_col = 0;
3711 ++msg_row;
3712 }
3713 }
3714}
3715
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003716 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003717msg_moremsg(int full)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003719 int attr;
3720 char_u *s = (char_u *)_("-- More --");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721
Bram Moolenaar8820b482017-03-16 17:23:31 +01003722 attr = HL_ATTR(HLF_M);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003723 screen_puts(s, (int)Rows - 1, 0, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 if (full)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00003725 screen_puts((char_u *)
3726 _(" SPACE/d/j: screen/page/line down, b/u/k: up, q: quit "),
3727 (int)Rows - 1, vim_strsize(s), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728}
3729
3730/*
Bram Moolenaar24959102022-05-07 20:01:16 +01003731 * Repeat the message for the current mode: MODE_ASKMORE, MODE_EXTERNCMD,
3732 * MODE_CONFIRM or exmode_active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 */
3734 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003735repeat_message(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736{
Bram Moolenaar24959102022-05-07 20:01:16 +01003737 if (State == MODE_ASKMORE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003739 msg_moremsg(TRUE); // display --more-- message again
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 msg_row = Rows - 1;
3741 }
3742#ifdef FEAT_CON_DIALOG
Bram Moolenaar24959102022-05-07 20:01:16 +01003743 else if (State == MODE_CONFIRM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003745 display_confirm_msg(); // display ":confirm" message again
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 msg_row = Rows - 1;
3747 }
3748#endif
Bram Moolenaar24959102022-05-07 20:01:16 +01003749 else if (State == MODE_EXTERNCMD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003751 windgoto(msg_row, msg_col); // put cursor back
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 }
Bram Moolenaar24959102022-05-07 20:01:16 +01003753 else if (State == MODE_HITRETURN || State == MODE_SETWSIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 {
Bram Moolenaar9f108752007-11-24 14:44:58 +00003755 if (msg_row == Rows - 1)
3756 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003757 // Avoid drawing the "hit-enter" prompt below the previous one,
3758 // overwrite it. Esp. useful when regaining focus and a
3759 // FocusGained autocmd exists but didn't draw anything.
Bram Moolenaar9f108752007-11-24 14:44:58 +00003760 msg_didout = FALSE;
3761 msg_col = 0;
3762 msg_clr_eos();
3763 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 hit_return_msg();
3765 msg_row = Rows - 1;
3766 }
3767}
3768
3769/*
3770 * msg_check_screen - check if the screen is initialized.
3771 * Also check msg_row and msg_col, if they are too big it may cause a crash.
3772 * While starting the GUI the terminal codes will be set for the GUI, but the
3773 * output goes to the terminal. Don't use the terminal codes then.
3774 */
3775 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003776msg_check_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777{
3778 if (!full_screen || !screen_valid(FALSE))
3779 return FALSE;
3780
3781 if (msg_row >= Rows)
3782 msg_row = Rows - 1;
3783 if (msg_col >= Columns)
3784 msg_col = Columns - 1;
3785 return TRUE;
3786}
3787
3788/*
3789 * Clear from current message position to end of screen.
3790 * Skip this when ":silent" was used, no need to clear for redirection.
3791 */
3792 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003793msg_clr_eos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794{
3795 if (msg_silent == 0)
3796 msg_clr_eos_force();
3797}
3798
3799/*
3800 * Clear from current message position to end of screen.
3801 * Note: msg_col is not updated, so we remember the end of the message
3802 * for msg_check().
3803 */
3804 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003805msg_clr_eos_force(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806{
Bram Moolenaar3b474dc2022-09-01 17:01:32 +01003807#ifdef HAS_MESSAGE_WINDOW
3808 if (in_echowindow)
3809 return; // messages go into a popup
3810#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 if (msg_use_printf())
3812 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003813 if (full_screen) // only when termcap codes are valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 {
3815 if (*T_CD)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003816 out_str(T_CD); // clear to end of display
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 else if (*T_CE)
Bram Moolenaar85a20022019-12-21 18:25:54 +01003818 out_str(T_CE); // clear to end of line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 }
3820 }
Bram Moolenaara2a89732022-08-31 14:46:18 +01003821 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 {
Shougo Matsushitabe2b03c2024-04-08 22:11:50 +02003823 int msg_attr;
3824
3825 msg_attr = HL_ATTR(HLF_MSG);
3826
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827#ifdef FEAT_RIGHTLEFT
3828 if (cmdmsg_rl)
3829 {
Shougo Matsushitabe2b03c2024-04-08 22:11:50 +02003830 screen_fill(msg_row, msg_row + 1, 0, msg_col + 1,
3831 ' ', ' ', msg_attr);
3832 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns,
3833 ' ', ' ', msg_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 }
3835 else
3836#endif
3837 {
3838 screen_fill(msg_row, msg_row + 1, msg_col, (int)Columns,
Shougo Matsushitabe2b03c2024-04-08 22:11:50 +02003839 ' ', ' ', msg_attr);
3840 screen_fill(msg_row + 1, (int)Rows, 0, (int)Columns,
3841 ' ', ' ', msg_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 }
3843 }
3844}
3845
3846/*
3847 * Clear the command line.
3848 */
3849 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003850msg_clr_cmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851{
3852 msg_row = cmdline_row;
3853 msg_col = 0;
3854 msg_clr_eos_force();
3855}
3856
3857/*
3858 * end putting a message on the screen
Bram Moolenaar13608d82022-08-29 15:06:50 +01003859 * call wait_return() if the message does not fit in the available space
3860 * return TRUE if wait_return() not called.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 */
3862 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003863msg_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864{
3865 /*
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02003866 * If the string is larger than the window,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 * or the ruler option is set and we run into it,
3868 * we have to redraw the window.
3869 * Do not do this if we are abandoning the file or editing the command line.
3870 */
Bram Moolenaar24959102022-05-07 20:01:16 +01003871 if (!exiting && need_wait_return && !(State & MODE_CMDLINE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 {
3873 wait_return(FALSE);
3874 return FALSE;
3875 }
3876 out_flush();
3877 return TRUE;
3878}
3879
3880/*
3881 * If the written message runs into the shown command or ruler, we have to
3882 * wait for hit-return and redraw the window later.
3883 */
3884 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003885msg_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886{
Bram Moolenaar35a4fbc2022-08-28 14:39:53 +01003887 if (msg_row == Rows - 1 && msg_col >= sc_col
3888#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaara2a89732022-08-31 14:46:18 +01003889 && !in_echowindow
Bram Moolenaar35a4fbc2022-08-28 14:39:53 +01003890#endif
3891 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 {
3893 need_wait_return = TRUE;
3894 redraw_cmdline = TRUE;
3895 }
3896}
3897
3898/*
3899 * May write a string to the redirection file.
3900 * When "maxlen" is -1 write the whole string, otherwise up to "maxlen" bytes.
3901 */
3902 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003903redir_write(char_u *str, int maxlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904{
3905 char_u *s = str;
3906 static int cur_col = 0;
3907
Bram Moolenaar85a20022019-12-21 18:25:54 +01003908 // Don't do anything for displaying prompts and the like.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003909 if (redir_off)
3910 return;
3911
Bram Moolenaar85a20022019-12-21 18:25:54 +01003912 // If 'verbosefile' is set prepare for writing in that file.
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003913 if (*p_vfile != NUL && verbose_fd == NULL)
3914 verbose_open();
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003915
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003916 if (redirecting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01003918 // If the string doesn't start with CR or NL, go to msg_col
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 if (*s != '\n' && *s != '\r')
3920 {
3921 while (cur_col < msg_col)
3922 {
3923#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003924 if (redir_execute)
3925 execute_redir_str((char_u *)" ", -1);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003926 else if (redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 write_reg_contents(redir_reg, (char_u *)" ", -1, TRUE);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003928 else if (redir_vname)
3929 var_redir_str((char_u *)" ", -1);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003930 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003932 if (redir_fd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 fputs(" ", redir_fd);
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003934 if (verbose_fd != NULL)
3935 fputs(" ", verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 ++cur_col;
3937 }
3938 }
3939
3940#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003941 if (redir_execute)
3942 execute_redir_str(s, maxlen);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003943 else if (redir_reg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 write_reg_contents(redir_reg, s, maxlen, TRUE);
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +02003945 else if (redir_vname)
Bram Moolenaardf177f62005-02-22 08:39:57 +00003946 var_redir_str(s, maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947#endif
3948
Bram Moolenaar85a20022019-12-21 18:25:54 +01003949 // Write and adjust the current column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 while (*s != NUL && (maxlen < 0 || (int)(s - str) < maxlen))
3951 {
3952#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003953 if (!redir_reg && !redir_vname && !redir_execute)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954#endif
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003955 if (redir_fd != NULL)
3956 putc(*s, redir_fd);
3957 if (verbose_fd != NULL)
3958 putc(*s, verbose_fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 if (*s == '\r' || *s == '\n')
3960 cur_col = 0;
3961 else if (*s == '\t')
3962 cur_col += (8 - cur_col % 8);
3963 else
3964 ++cur_col;
3965 ++s;
3966 }
3967
Bram Moolenaar85a20022019-12-21 18:25:54 +01003968 if (msg_silent != 0) // should update msg_col
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 msg_col = cur_col;
3970 }
3971}
3972
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003973 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003974redirecting(void)
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003975{
Bram Moolenaarb5b5b892011-09-14 15:39:29 +02003976 return redir_fd != NULL || *p_vfile != NUL
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003977#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02003978 || redir_reg || redir_vname || redir_execute
Bram Moolenaar77ab2802009-04-22 12:44:48 +00003979#endif
3980 ;
3981}
3982
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983/*
Bram Moolenaarf1dc4962007-05-10 16:50:23 +00003984 * Before giving verbose message.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003985 * Must always be called paired with verbose_leave()!
3986 */
3987 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003988verbose_enter(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00003989{
3990 if (*p_vfile != NUL)
3991 ++msg_silent;
3992}
3993
3994/*
3995 * After giving verbose message.
3996 * Must always be called paired with verbose_enter()!
3997 */
3998 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003999verbose_leave(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00004000{
4001 if (*p_vfile != NUL)
4002 if (--msg_silent < 0)
4003 msg_silent = 0;
4004}
4005
4006/*
4007 * Like verbose_enter() and set msg_scroll when displaying the message.
4008 */
4009 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004010verbose_enter_scroll(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00004011{
4012 if (*p_vfile != NUL)
4013 ++msg_silent;
4014 else
Bram Moolenaar85a20022019-12-21 18:25:54 +01004015 // always scroll up, don't overwrite
Bram Moolenaar8b044b32005-05-31 22:05:58 +00004016 msg_scroll = TRUE;
4017}
4018
4019/*
4020 * Like verbose_leave() and set cmdline_row when displaying the message.
4021 */
4022 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004023verbose_leave_scroll(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00004024{
4025 if (*p_vfile != NUL)
4026 {
4027 if (--msg_silent < 0)
4028 msg_silent = 0;
4029 }
4030 else
4031 cmdline_row = msg_row;
4032}
4033
Bram Moolenaar8b044b32005-05-31 22:05:58 +00004034/*
4035 * Called when 'verbosefile' is set: stop writing to the file.
4036 */
4037 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004038verbose_stop(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00004039{
4040 if (verbose_fd != NULL)
4041 {
4042 fclose(verbose_fd);
4043 verbose_fd = NULL;
4044 }
4045 verbose_did_open = FALSE;
4046}
4047
4048/*
4049 * Open the file 'verbosefile'.
4050 * Return FAIL or OK.
4051 */
4052 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004053verbose_open(void)
Bram Moolenaar8b044b32005-05-31 22:05:58 +00004054{
4055 if (verbose_fd == NULL && !verbose_did_open)
4056 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004057 // Only give the error message once.
Bram Moolenaar8b044b32005-05-31 22:05:58 +00004058 verbose_did_open = TRUE;
4059
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004060 verbose_fd = mch_fopen((char *)p_vfile, "a");
Bram Moolenaar8b044b32005-05-31 22:05:58 +00004061 if (verbose_fd == NULL)
4062 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004063 semsg(_(e_cant_open_file_str), p_vfile);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00004064 return FAIL;
4065 }
4066 }
4067 return OK;
4068}
4069
4070/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 * Give a warning message (for searching).
4072 * Use 'w' highlighting and may repeat the message after redrawing
4073 */
4074 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004075give_warning(char_u *message, int hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076{
Bram Moolenaarcc7eb2a2021-07-11 19:12:04 +02004077 give_warning_with_source(message, hl, FALSE);
4078}
4079
4080 void
4081give_warning_with_source(char_u *message, int hl, int with_source)
4082{
Bram Moolenaar85a20022019-12-21 18:25:54 +01004083 // Don't do this for ":silent".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 if (msg_silent != 0)
4085 return;
4086
Bram Moolenaar85a20022019-12-21 18:25:54 +01004087 // Don't want a hit-enter prompt here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 ++no_wait_return;
Bram Moolenaared203462004-06-16 11:19:22 +00004089
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090#ifdef FEAT_EVAL
4091 set_vim_var_string(VV_WARNINGMSG, message, -1);
4092#endif
Bram Moolenaard23a8232018-02-10 18:45:26 +01004093 VIM_CLEAR(keep_msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 if (hl)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004095 keep_msg_attr = HL_ATTR(HLF_W);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 else
4097 keep_msg_attr = 0;
Bram Moolenaarcc7eb2a2021-07-11 19:12:04 +02004098
4099 if (with_source)
4100 {
4101 // Do what msg() does, but with a column offset if the warning should
4102 // be after the mode message.
4103 msg_start();
4104 msg_source(HL_ATTR(HLF_W));
4105 msg_puts(" ");
4106 msg_puts_attr((char *)message, HL_ATTR(HLF_W) | MSG_HIST);
4107 msg_clr_eos();
4108 (void)msg_end();
4109 }
4110 else if (msg_attr((char *)message, keep_msg_attr) && msg_scrolled == 0)
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004111 set_keep_msg(message, keep_msg_attr);
Bram Moolenaarcc7eb2a2021-07-11 19:12:04 +02004112
Bram Moolenaar85a20022019-12-21 18:25:54 +01004113 msg_didout = FALSE; // overwrite this message
4114 msg_nowait = TRUE; // don't wait for this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 msg_col = 0;
Bram Moolenaared203462004-06-16 11:19:22 +00004116
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 --no_wait_return;
4118}
4119
Bram Moolenaar113e1072019-01-20 15:30:40 +01004120#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarf8be4612017-06-23 20:52:40 +02004121 void
4122give_warning2(char_u *message, char_u *a1, int hl)
4123{
Bram Moolenaare3a22cb2019-10-14 22:01:57 +02004124 if (IObuff == NULL)
4125 {
4126 // Very early in initialisation and already something wrong, just give
4127 // the raw message so the user at least gets a hint.
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00004128 give_warning(message, hl);
Bram Moolenaare3a22cb2019-10-14 22:01:57 +02004129 }
4130 else
4131 {
4132 vim_snprintf((char *)IObuff, IOSIZE, (char *)message, a1);
4133 give_warning(IObuff, hl);
4134 }
Bram Moolenaarf8be4612017-06-23 20:52:40 +02004135}
Bram Moolenaar113e1072019-01-20 15:30:40 +01004136#endif
Bram Moolenaarf8be4612017-06-23 20:52:40 +02004137
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138/*
4139 * Advance msg cursor to column "col".
4140 */
4141 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004142msg_advance(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143{
Bram Moolenaar85a20022019-12-21 18:25:54 +01004144 if (msg_silent != 0) // nothing to advance to
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004146 msg_col = col; // for redirection, may fill it up later
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 return;
4148 }
Bram Moolenaar85a20022019-12-21 18:25:54 +01004149 if (col >= Columns) // not enough room
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 col = Columns - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004151#ifdef FEAT_RIGHTLEFT
4152 if (cmdmsg_rl)
4153 while (msg_col > Columns - col)
4154 msg_putchar(' ');
4155 else
4156#endif
4157 while (msg_col < col)
4158 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159}
4160
Christian Brabandt45e07042024-11-11 20:52:55 +01004161/*
4162 * Warn about missing Clipboard Support
4163 */
4164 void
4165msg_warn_missing_clipboard(void)
4166{
4167 if (!global_busy && !did_warn_clipboard)
4168 {
Christian Brabandt8b94afc2024-11-17 16:02:41 +01004169#ifdef FEAT_CLIPBOARD
4170 msg(_("W23: Clipboard register not available, using register 0"));
4171#else
4172 msg(_("W24: Clipboard register not available. See :h W24"));
4173#endif
Christian Brabandt45e07042024-11-11 20:52:55 +01004174 did_warn_clipboard = TRUE;
4175 }
4176}
4177
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178#if defined(FEAT_CON_DIALOG) || defined(PROTO)
4179/*
4180 * Used for "confirm()" function, and the :confirm command prefix.
4181 * Versions which haven't got flexible dialogs yet, and console
4182 * versions, get this generic handler which uses the command line.
4183 *
4184 * type = one of:
4185 * VIM_QUESTION, VIM_INFO, VIM_WARNING, VIM_ERROR or VIM_GENERIC
4186 * title = title string (can be NULL for default)
4187 * (neither used in console dialogs at the moment)
4188 *
4189 * Format of the "buttons" string:
4190 * "Button1Name\nButton2Name\nButton3Name"
4191 * The first button should normally be the default/accept
4192 * The second button should be the 'Cancel' button
4193 * Other buttons- use your imagination!
4194 * A '&' in a button name becomes a shortcut, so each '&' should be before a
4195 * different letter.
Rob Pilling8196e942022-02-11 15:12:10 +00004196 *
4197 * Returns 0 if cancelled, otherwise the nth button (1-indexed).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004200do_dialog(
4201 int type UNUSED,
4202 char_u *title UNUSED,
4203 char_u *message,
4204 char_u *buttons,
4205 int dfltbutton,
Bram Moolenaar85a20022019-12-21 18:25:54 +01004206 char_u *textfield UNUSED, // IObuff for inputdialog(), NULL
4207 // otherwise
4208 int ex_cmd) // when TRUE pressing : accepts default and starts
4209 // Ex command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210{
4211 int oldState;
4212 int retval = 0;
4213 char_u *hotkeys;
4214 int c;
4215 int i;
Bram Moolenaar27321db2020-07-06 21:24:57 +02004216 tmode_T save_tmode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217
4218#ifndef NO_CONSOLE
Bram Moolenaar85a20022019-12-21 18:25:54 +01004219 // Don't output anything in silent mode ("ex -s")
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 if (silent_mode)
Bram Moolenaar85a20022019-12-21 18:25:54 +01004221 return dfltbutton; // return default option
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222#endif
4223
4224#ifdef FEAT_GUI_DIALOG
Bram Moolenaar85a20022019-12-21 18:25:54 +01004225 // When GUI is running and 'c' not in 'guioptions', use the GUI dialog
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
4227 {
Bram Moolenaar2d12c252022-06-13 21:42:45 +01004228 // --gui-dialog-file: write text to a file
4229 if (gui_dialog_log(title, message))
4230 c = dfltbutton;
4231 else
4232 c = gui_mch_dialog(type, title, message, buttons, dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004233 textfield, ex_cmd);
Bram Moolenaar85a20022019-12-21 18:25:54 +01004234 // avoid a hit-enter prompt without clearing the cmdline
Bram Moolenaar8de49e12009-02-11 17:47:54 +00004235 need_wait_return = FALSE;
4236 emsg_on_display = FALSE;
4237 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238
Bram Moolenaar85a20022019-12-21 18:25:54 +01004239 // Flush output to avoid that further messages and redrawing is done
4240 // in the wrong order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 out_flush();
4242 gui_mch_update();
4243
4244 return c;
4245 }
4246#endif
4247
4248 oldState = State;
Bram Moolenaar24959102022-05-07 20:01:16 +01004249 State = MODE_CONFIRM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251
Bram Moolenaar27321db2020-07-06 21:24:57 +02004252 // Ensure raw mode here.
4253 save_tmode = cur_tmode;
4254 settmode(TMODE_RAW);
4255
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 /*
4257 * Since we wait for a keypress, don't make the
4258 * user press RETURN as well afterwards.
4259 */
4260 ++no_wait_return;
4261 hotkeys = msg_show_console_dialog(message, buttons, dfltbutton);
4262
4263 if (hotkeys != NULL)
4264 {
4265 for (;;)
4266 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004267 // Get a typed character directly from the user.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 c = get_keystroke();
4269 switch (c)
4270 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004271 case CAR: // User accepts default option
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 case NL:
4273 retval = dfltbutton;
4274 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01004275 case Ctrl_C: // User aborts/cancels
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 case ESC:
4277 retval = 0;
4278 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01004279 default: // Could be a hotkey?
4280 if (c < 0) // special keys are ignored here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 continue;
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004282 if (c == ':' && ex_cmd)
4283 {
4284 retval = dfltbutton;
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02004285 ins_char_typebuf(':', 0);
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004286 break;
4287 }
4288
Bram Moolenaar85a20022019-12-21 18:25:54 +01004289 // Make the character lowercase, as chars in "hotkeys" are.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 c = MB_TOLOWER(c);
4291 retval = 1;
4292 for (i = 0; hotkeys[i]; ++i)
4293 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 if (has_mbyte)
4295 {
4296 if ((*mb_ptr2char)(hotkeys + i) == c)
4297 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004298 i += (*mb_ptr2len)(hotkeys + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 }
4300 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 if (hotkeys[i] == c)
4302 break;
4303 ++retval;
4304 }
4305 if (hotkeys[i])
4306 break;
Bram Moolenaar85a20022019-12-21 18:25:54 +01004307 // No hotkey match, so keep waiting
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 continue;
4309 }
4310 break;
4311 }
4312
4313 vim_free(hotkeys);
4314 }
4315
Bram Moolenaar27321db2020-07-06 21:24:57 +02004316 settmode(save_tmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 State = oldState;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 --no_wait_return;
4320 msg_end_prompt();
4321
4322 return retval;
4323}
4324
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325/*
4326 * Copy one character from "*from" to "*to", taking care of multi-byte
4327 * characters. Return the length of the character in bytes.
4328 */
4329 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004330copy_char(
4331 char_u *from,
4332 char_u *to,
Bram Moolenaar85a20022019-12-21 18:25:54 +01004333 int lowercase) // make character lower case
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 int len;
4336 int c;
4337
4338 if (has_mbyte)
4339 {
4340 if (lowercase)
4341 {
4342 c = MB_TOLOWER((*mb_ptr2char)(from));
4343 return (*mb_char2bytes)(c, to);
4344 }
4345 else
4346 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004347 len = (*mb_ptr2len)(from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 mch_memmove(to, from, (size_t)len);
4349 return len;
4350 }
4351 }
4352 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353 {
4354 if (lowercase)
4355 *to = (char_u)TOLOWER_LOC(*from);
4356 else
4357 *to = *from;
4358 return 1;
4359 }
4360}
4361
4362/*
4363 * Format the dialog string, and display it at the bottom of
4364 * the screen. Return a string of hotkey chars (if defined) for
4365 * each 'button'. If a button has no hotkey defined, the first character of
4366 * the button is used.
4367 * The hotkeys can be multi-byte characters, but without combining chars.
4368 *
4369 * Returns an allocated string with hotkeys, or NULL for error.
4370 */
4371 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004372msg_show_console_dialog(
4373 char_u *message,
4374 char_u *buttons,
4375 int dfltbutton)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376{
4377 int len = 0;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01004378#define HOTK_LEN (has_mbyte ? MB_MAXBYTES : 1)
Bram Moolenaar85a20022019-12-21 18:25:54 +01004379 int lenhotkey = HOTK_LEN; // count first button
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 char_u *hotk = NULL;
4381 char_u *msgp = NULL;
4382 char_u *hotkp = NULL;
4383 char_u *r;
4384 int copy;
4385#define HAS_HOTKEY_LEN 30
4386 char_u has_hotkey[HAS_HOTKEY_LEN];
Bram Moolenaar85a20022019-12-21 18:25:54 +01004387 int first_hotkey = FALSE; // first char of button is hotkey
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 int idx;
4389
4390 has_hotkey[0] = FALSE;
4391
4392 /*
4393 * First loop: compute the size of memory to allocate.
4394 * Second loop: copy to the allocated memory.
4395 */
4396 for (copy = 0; copy <= 1; ++copy)
4397 {
4398 r = buttons;
4399 idx = 0;
4400 while (*r)
4401 {
4402 if (*r == DLG_BUTTON_SEP)
4403 {
4404 if (copy)
4405 {
4406 *msgp++ = ',';
Bram Moolenaar85a20022019-12-21 18:25:54 +01004407 *msgp++ = ' '; // '\n' -> ', '
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408
Bram Moolenaar85a20022019-12-21 18:25:54 +01004409 // advance to next hotkey and set default hotkey
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 if (has_mbyte)
Bram Moolenaar6a516062007-07-05 08:11:42 +00004411 hotkp += STRLEN(hotkp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 ++hotkp;
Bram Moolenaar6a516062007-07-05 08:11:42 +00004414 hotkp[copy_char(r + 1, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 if (dfltbutton)
4416 --dfltbutton;
4417
Bram Moolenaar85a20022019-12-21 18:25:54 +01004418 // If no hotkey is specified first char is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 if (idx < HAS_HOTKEY_LEN - 1 && !has_hotkey[++idx])
4420 first_hotkey = TRUE;
4421 }
4422 else
4423 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004424 len += 3; // '\n' -> ', '; 'x' -> '(x)'
4425 lenhotkey += HOTK_LEN; // each button needs a hotkey
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 if (idx < HAS_HOTKEY_LEN - 1)
4427 has_hotkey[++idx] = FALSE;
4428 }
4429 }
4430 else if (*r == DLG_HOTKEY_CHAR || first_hotkey)
4431 {
4432 if (*r == DLG_HOTKEY_CHAR)
4433 ++r;
4434 first_hotkey = FALSE;
4435 if (copy)
4436 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004437 if (*r == DLG_HOTKEY_CHAR) // '&&a' -> '&a'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 *msgp++ = *r;
4439 else
4440 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004441 // '&a' -> '[a]'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 *msgp++ = (dfltbutton == 1) ? '[' : '(';
4443 msgp += copy_char(r, msgp, FALSE);
4444 *msgp++ = (dfltbutton == 1) ? ']' : ')';
4445
Bram Moolenaar85a20022019-12-21 18:25:54 +01004446 // redefine hotkey
Bram Moolenaar6a516062007-07-05 08:11:42 +00004447 hotkp[copy_char(r, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 }
4449 }
4450 else
4451 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004452 ++len; // '&a' -> '[a]'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 if (idx < HAS_HOTKEY_LEN - 1)
4454 has_hotkey[idx] = TRUE;
4455 }
4456 }
4457 else
4458 {
Bram Moolenaar85a20022019-12-21 18:25:54 +01004459 // everything else copy literally
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 if (copy)
4461 msgp += copy_char(r, msgp, FALSE);
4462 }
4463
Bram Moolenaar85a20022019-12-21 18:25:54 +01004464 // advance to the next character
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004465 MB_PTR_ADV(r);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 }
4467
4468 if (copy)
4469 {
4470 *msgp++ = ':';
4471 *msgp++ = ' ';
4472 *msgp = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 }
4474 else
4475 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004476 len += (int)(STRLEN(message)
Bram Moolenaar85a20022019-12-21 18:25:54 +01004477 + 2 // for the NL's
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004478 + STRLEN(buttons)
Bram Moolenaar85a20022019-12-21 18:25:54 +01004479 + 3); // for the ": " and NUL
4480 lenhotkey++; // for the NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481
Bram Moolenaar85a20022019-12-21 18:25:54 +01004482 // If no hotkey is specified first char is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 if (!has_hotkey[0])
4484 {
4485 first_hotkey = TRUE;
Bram Moolenaar85a20022019-12-21 18:25:54 +01004486 len += 2; // "x" -> "[x]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 }
4488
4489 /*
4490 * Now allocate and load the strings
4491 */
4492 vim_free(confirm_msg);
4493 confirm_msg = alloc(len);
4494 if (confirm_msg == NULL)
4495 return NULL;
4496 *confirm_msg = NUL;
4497 hotk = alloc(lenhotkey);
4498 if (hotk == NULL)
4499 return NULL;
4500
4501 *confirm_msg = '\n';
4502 STRCPY(confirm_msg + 1, message);
4503
4504 msgp = confirm_msg + 1 + STRLEN(message);
4505 hotkp = hotk;
4506
Bram Moolenaar85a20022019-12-21 18:25:54 +01004507 // Define first default hotkey. Keep the hotkey string NUL
4508 // terminated to avoid reading past the end.
Bram Moolenaar6a516062007-07-05 08:11:42 +00004509 hotkp[copy_char(buttons, hotkp, TRUE)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510
Bram Moolenaar85a20022019-12-21 18:25:54 +01004511 // Remember where the choices start, displaying starts here when
4512 // "hotkp" typed at the more prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 confirm_msg_tail = msgp;
4514 *msgp++ = '\n';
4515 }
4516 }
4517
4518 display_confirm_msg();
4519 return hotk;
4520}
4521
4522/*
4523 * Display the ":confirm" message. Also called when screen resized.
4524 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02004525 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004526display_confirm_msg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527{
Bram Moolenaar85a20022019-12-21 18:25:54 +01004528 // avoid that 'q' at the more prompt truncates the message here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 ++confirm_msg_used;
4530 if (confirm_msg != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004531 msg_puts_attr((char *)confirm_msg, HL_ATTR(HLF_M));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532 --confirm_msg_used;
4533}
4534
Bram Moolenaar85a20022019-12-21 18:25:54 +01004535#endif // FEAT_CON_DIALOG
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536
4537#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
4538
4539 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004540vim_dialog_yesno(
4541 int type,
4542 char_u *title,
4543 char_u *message,
4544 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545{
4546 if (do_dialog(type,
4547 title == NULL ? (char_u *)_("Question") : title,
4548 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004549 (char_u *)_("&Yes\n&No"), dflt, NULL, FALSE) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 return VIM_YES;
4551 return VIM_NO;
4552}
4553
4554 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004555vim_dialog_yesnocancel(
4556 int type,
4557 char_u *title,
4558 char_u *message,
4559 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560{
4561 switch (do_dialog(type,
4562 title == NULL ? (char_u *)_("Question") : title,
4563 message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004564 (char_u *)_("&Yes\n&No\n&Cancel"), dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 {
4566 case 1: return VIM_YES;
4567 case 2: return VIM_NO;
4568 }
4569 return VIM_CANCEL;
4570}
4571
4572 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004573vim_dialog_yesnoallcancel(
4574 int type,
4575 char_u *title,
4576 char_u *message,
4577 int dflt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578{
4579 switch (do_dialog(type,
4580 title == NULL ? (char_u *)"Question" : title,
4581 message,
4582 (char_u *)_("&Yes\n&No\nSave &All\n&Discard All\n&Cancel"),
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004583 dflt, NULL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 {
4585 case 1: return VIM_YES;
4586 case 2: return VIM_NO;
4587 case 3: return VIM_ALL;
4588 case 4: return VIM_DISCARDALL;
4589 }
4590 return VIM_CANCEL;
4591}
4592
Bram Moolenaar85a20022019-12-21 18:25:54 +01004593#endif // FEAT_GUI_DIALOG || FEAT_CON_DIALOG